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 nfs4_free_ol_stateid(stid);
934 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
936 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
938 spin_lock(&oo->oo_owner.so_client->cl_lock);
939 list_del(&stp->st_locks);
940 unhash_generic_stateid(stp);
941 unhash_stid(&stp->st_stid);
942 spin_unlock(&oo->oo_owner.so_client->cl_lock);
943 nfs4_put_stid(&stp->st_stid);
946 static void unhash_lockowner(struct nfs4_lockowner *lo)
948 list_del_init(&lo->lo_owner.so_strhash);
951 static void release_lockowner_stateids(struct nfs4_lockowner *lo)
953 struct nfs4_ol_stateid *stp;
955 while (!list_empty(&lo->lo_owner.so_stateids)) {
956 stp = list_first_entry(&lo->lo_owner.so_stateids,
957 struct nfs4_ol_stateid, st_perstateowner);
958 __release_lock_stateid(stp);
962 static void release_lockowner(struct nfs4_lockowner *lo)
964 unhash_lockowner(lo);
965 release_lockowner_stateids(lo);
966 nfs4_put_stateowner(&lo->lo_owner);
969 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
971 if (list_empty(&lo->lo_owner.so_stateids))
972 release_lockowner(lo);
975 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
977 struct nfs4_lockowner *lo;
979 lo = lockowner(stp->st_stateowner);
980 __release_lock_stateid(stp);
981 release_lockowner_if_empty(lo);
984 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
985 __releases(&open_stp->st_stateowner->so_client->cl_lock)
986 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
988 struct nfs4_ol_stateid *stp;
990 while (!list_empty(&open_stp->st_locks)) {
991 stp = list_entry(open_stp->st_locks.next,
992 struct nfs4_ol_stateid, st_locks);
993 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
994 release_lock_stateid(stp);
995 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
999 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
1001 spin_lock(&stp->st_stateowner->so_client->cl_lock);
1002 unhash_generic_stateid(stp);
1003 release_open_stateid_locks(stp);
1004 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
1007 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1009 unhash_open_stateid(stp);
1010 nfs4_put_stid(&stp->st_stid);
1013 static void unhash_openowner(struct nfs4_openowner *oo)
1015 list_del_init(&oo->oo_owner.so_strhash);
1016 list_del_init(&oo->oo_perclient);
1019 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1021 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1024 nfs4_put_stid(&s->st_stid);
1025 oo->oo_last_closed_stid = NULL;
1029 static void release_openowner_stateids(struct nfs4_openowner *oo)
1031 struct nfs4_ol_stateid *stp;
1033 while (!list_empty(&oo->oo_owner.so_stateids)) {
1034 stp = list_first_entry(&oo->oo_owner.so_stateids,
1035 struct nfs4_ol_stateid, st_perstateowner);
1036 release_open_stateid(stp);
1040 static void release_openowner(struct nfs4_openowner *oo)
1042 unhash_openowner(oo);
1043 release_openowner_stateids(oo);
1044 list_del(&oo->oo_close_lru);
1045 release_last_closed_stateid(oo);
1046 nfs4_put_stateowner(&oo->oo_owner);
1050 hash_sessionid(struct nfs4_sessionid *sessionid)
1052 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1054 return sid->sequence % SESSION_HASH_SIZE;
1059 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1061 u32 *ptr = (u32 *)(&sessionid->data[0]);
1062 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1066 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1072 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1073 * won't be used for replay.
1075 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1077 struct nfs4_stateowner *so = cstate->replay_owner;
1079 if (nfserr == nfserr_replay_me)
1082 if (!seqid_mutating_err(ntohl(nfserr))) {
1083 nfsd4_cstate_clear_replay(cstate);
1088 if (so->so_is_open_owner)
1089 release_last_closed_stateid(openowner(so));
1095 gen_sessionid(struct nfsd4_session *ses)
1097 struct nfs4_client *clp = ses->se_client;
1098 struct nfsd4_sessionid *sid;
1100 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1101 sid->clientid = clp->cl_clientid;
1102 sid->sequence = current_sessionid++;
1107 * The protocol defines ca_maxresponssize_cached to include the size of
1108 * the rpc header, but all we need to cache is the data starting after
1109 * the end of the initial SEQUENCE operation--the rest we regenerate
1110 * each time. Therefore we can advertise a ca_maxresponssize_cached
1111 * value that is the number of bytes in our cache plus a few additional
1112 * bytes. In order to stay on the safe side, and not promise more than
1113 * we can cache, those additional bytes must be the minimum possible: 24
1114 * bytes of rpc header (xid through accept state, with AUTH_NULL
1115 * verifier), 12 for the compound header (with zero-length tag), and 44
1116 * for the SEQUENCE op response:
1118 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1121 free_session_slots(struct nfsd4_session *ses)
1125 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1126 kfree(ses->se_slots[i]);
1130 * We don't actually need to cache the rpc and session headers, so we
1131 * can allocate a little less for each slot:
1133 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1137 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1140 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1141 return size + sizeof(struct nfsd4_slot);
1145 * XXX: If we run out of reserved DRC memory we could (up to a point)
1146 * re-negotiate active sessions and reduce their slot usage to make
1147 * room for new connections. For now we just fail the create session.
1149 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1151 u32 slotsize = slot_bytes(ca);
1152 u32 num = ca->maxreqs;
1155 spin_lock(&nfsd_drc_lock);
1156 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1157 nfsd_drc_max_mem - nfsd_drc_mem_used);
1158 num = min_t(int, num, avail / slotsize);
1159 nfsd_drc_mem_used += num * slotsize;
1160 spin_unlock(&nfsd_drc_lock);
1165 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1167 int slotsize = slot_bytes(ca);
1169 spin_lock(&nfsd_drc_lock);
1170 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1171 spin_unlock(&nfsd_drc_lock);
1174 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1175 struct nfsd4_channel_attrs *battrs)
1177 int numslots = fattrs->maxreqs;
1178 int slotsize = slot_bytes(fattrs);
1179 struct nfsd4_session *new;
1182 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1183 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1184 mem = numslots * sizeof(struct nfsd4_slot *);
1186 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1189 /* allocate each struct nfsd4_slot and data cache in one piece */
1190 for (i = 0; i < numslots; i++) {
1191 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1192 if (!new->se_slots[i])
1196 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1197 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1202 kfree(new->se_slots[i]);
1207 static void free_conn(struct nfsd4_conn *c)
1209 svc_xprt_put(c->cn_xprt);
1213 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1215 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1216 struct nfs4_client *clp = c->cn_session->se_client;
1218 spin_lock(&clp->cl_lock);
1219 if (!list_empty(&c->cn_persession)) {
1220 list_del(&c->cn_persession);
1223 nfsd4_probe_callback(clp);
1224 spin_unlock(&clp->cl_lock);
1227 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1229 struct nfsd4_conn *conn;
1231 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1234 svc_xprt_get(rqstp->rq_xprt);
1235 conn->cn_xprt = rqstp->rq_xprt;
1236 conn->cn_flags = flags;
1237 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1241 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1243 conn->cn_session = ses;
1244 list_add(&conn->cn_persession, &ses->se_conns);
1247 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1249 struct nfs4_client *clp = ses->se_client;
1251 spin_lock(&clp->cl_lock);
1252 __nfsd4_hash_conn(conn, ses);
1253 spin_unlock(&clp->cl_lock);
1256 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1258 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1259 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1262 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1266 nfsd4_hash_conn(conn, ses);
1267 ret = nfsd4_register_conn(conn);
1269 /* oops; xprt is already down: */
1270 nfsd4_conn_lost(&conn->cn_xpt_user);
1271 /* We may have gained or lost a callback channel: */
1272 nfsd4_probe_callback_sync(ses->se_client);
1275 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1277 u32 dir = NFS4_CDFC4_FORE;
1279 if (cses->flags & SESSION4_BACK_CHAN)
1280 dir |= NFS4_CDFC4_BACK;
1281 return alloc_conn(rqstp, dir);
1284 /* must be called under client_lock */
1285 static void nfsd4_del_conns(struct nfsd4_session *s)
1287 struct nfs4_client *clp = s->se_client;
1288 struct nfsd4_conn *c;
1290 spin_lock(&clp->cl_lock);
1291 while (!list_empty(&s->se_conns)) {
1292 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1293 list_del_init(&c->cn_persession);
1294 spin_unlock(&clp->cl_lock);
1296 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1299 spin_lock(&clp->cl_lock);
1301 spin_unlock(&clp->cl_lock);
1304 static void __free_session(struct nfsd4_session *ses)
1306 free_session_slots(ses);
1310 static void free_session(struct nfsd4_session *ses)
1312 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1314 lockdep_assert_held(&nn->client_lock);
1315 nfsd4_del_conns(ses);
1316 nfsd4_put_drc_mem(&ses->se_fchannel);
1317 __free_session(ses);
1320 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1323 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1325 new->se_client = clp;
1328 INIT_LIST_HEAD(&new->se_conns);
1330 new->se_cb_seq_nr = 1;
1331 new->se_flags = cses->flags;
1332 new->se_cb_prog = cses->callback_prog;
1333 new->se_cb_sec = cses->cb_sec;
1334 atomic_set(&new->se_ref, 0);
1335 idx = hash_sessionid(&new->se_sessionid);
1336 spin_lock(&nn->client_lock);
1337 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1338 spin_lock(&clp->cl_lock);
1339 list_add(&new->se_perclnt, &clp->cl_sessions);
1340 spin_unlock(&clp->cl_lock);
1341 spin_unlock(&nn->client_lock);
1343 if (cses->flags & SESSION4_BACK_CHAN) {
1344 struct sockaddr *sa = svc_addr(rqstp);
1346 * This is a little silly; with sessions there's no real
1347 * use for the callback address. Use the peer address
1348 * as a reasonable default for now, but consider fixing
1349 * the rpc client not to require an address in the
1352 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1353 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1357 /* caller must hold client_lock */
1358 static struct nfsd4_session *
1359 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1361 struct nfsd4_session *elem;
1363 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1365 dump_sessionid(__func__, sessionid);
1366 idx = hash_sessionid(sessionid);
1367 /* Search in the appropriate list */
1368 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1369 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1370 NFS4_MAX_SESSIONID_LEN)) {
1375 dprintk("%s: session not found\n", __func__);
1379 static struct nfsd4_session *
1380 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1383 struct nfsd4_session *session;
1384 __be32 status = nfserr_badsession;
1386 session = __find_in_sessionid_hashtbl(sessionid, net);
1389 status = nfsd4_get_session_locked(session);
1397 /* caller must hold client_lock */
1399 unhash_session(struct nfsd4_session *ses)
1401 list_del(&ses->se_hash);
1402 spin_lock(&ses->se_client->cl_lock);
1403 list_del(&ses->se_perclnt);
1404 spin_unlock(&ses->se_client->cl_lock);
1407 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1409 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1411 if (clid->cl_boot == nn->boot_time)
1413 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1414 clid->cl_boot, clid->cl_id, nn->boot_time);
1419 * XXX Should we use a slab cache ?
1420 * This type of memory management is somewhat inefficient, but we use it
1421 * anyway since SETCLIENTID is not a common operation.
1423 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1425 struct nfs4_client *clp;
1427 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1430 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1431 if (clp->cl_name.data == NULL) {
1435 clp->cl_name.len = name.len;
1436 INIT_LIST_HEAD(&clp->cl_sessions);
1437 idr_init(&clp->cl_stateids);
1438 atomic_set(&clp->cl_refcount, 0);
1439 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1440 INIT_LIST_HEAD(&clp->cl_idhash);
1441 INIT_LIST_HEAD(&clp->cl_openowners);
1442 INIT_LIST_HEAD(&clp->cl_delegations);
1443 INIT_LIST_HEAD(&clp->cl_lru);
1444 INIT_LIST_HEAD(&clp->cl_callbacks);
1445 INIT_LIST_HEAD(&clp->cl_revoked);
1446 spin_lock_init(&clp->cl_lock);
1447 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1452 free_client(struct nfs4_client *clp)
1454 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1456 lockdep_assert_held(&nn->client_lock);
1457 while (!list_empty(&clp->cl_sessions)) {
1458 struct nfsd4_session *ses;
1459 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1461 list_del(&ses->se_perclnt);
1462 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1465 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1466 free_svc_cred(&clp->cl_cred);
1467 kfree(clp->cl_name.data);
1468 idr_destroy(&clp->cl_stateids);
1472 /* must be called under the client_lock */
1474 unhash_client_locked(struct nfs4_client *clp)
1476 struct nfsd4_session *ses;
1478 list_del(&clp->cl_lru);
1479 spin_lock(&clp->cl_lock);
1480 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1481 list_del_init(&ses->se_hash);
1482 spin_unlock(&clp->cl_lock);
1486 destroy_client(struct nfs4_client *clp)
1488 struct nfs4_openowner *oo;
1489 struct nfs4_delegation *dp;
1490 struct list_head reaplist;
1491 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1493 INIT_LIST_HEAD(&reaplist);
1494 spin_lock(&state_lock);
1495 while (!list_empty(&clp->cl_delegations)) {
1496 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1497 unhash_delegation_locked(dp);
1498 list_add(&dp->dl_recall_lru, &reaplist);
1500 spin_unlock(&state_lock);
1501 while (!list_empty(&reaplist)) {
1502 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1503 list_del_init(&dp->dl_recall_lru);
1504 nfs4_put_stid(&dp->dl_stid);
1506 while (!list_empty(&clp->cl_revoked)) {
1507 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1508 list_del_init(&dp->dl_recall_lru);
1509 nfs4_put_stid(&dp->dl_stid);
1511 while (!list_empty(&clp->cl_openowners)) {
1512 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1513 release_openowner(oo);
1515 nfsd4_shutdown_callback(clp);
1516 if (clp->cl_cb_conn.cb_xprt)
1517 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1518 list_del(&clp->cl_idhash);
1519 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1520 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1522 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1523 spin_lock(&nn->client_lock);
1524 unhash_client_locked(clp);
1525 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1527 spin_unlock(&nn->client_lock);
1530 static void expire_client(struct nfs4_client *clp)
1532 nfsd4_client_record_remove(clp);
1533 destroy_client(clp);
1536 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1538 memcpy(target->cl_verifier.data, source->data,
1539 sizeof(target->cl_verifier.data));
1542 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1544 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1545 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1548 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1550 if (source->cr_principal) {
1551 target->cr_principal =
1552 kstrdup(source->cr_principal, GFP_KERNEL);
1553 if (target->cr_principal == NULL)
1556 target->cr_principal = NULL;
1557 target->cr_flavor = source->cr_flavor;
1558 target->cr_uid = source->cr_uid;
1559 target->cr_gid = source->cr_gid;
1560 target->cr_group_info = source->cr_group_info;
1561 get_group_info(target->cr_group_info);
1562 target->cr_gss_mech = source->cr_gss_mech;
1563 if (source->cr_gss_mech)
1564 gss_mech_get(source->cr_gss_mech);
1569 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1573 res = o1->len - o2->len;
1576 return (long long)memcmp(o1->data, o2->data, o1->len);
1579 static int same_name(const char *n1, const char *n2)
1581 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1585 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1587 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1591 same_clid(clientid_t *cl1, clientid_t *cl2)
1593 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1596 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1600 if (g1->ngroups != g2->ngroups)
1602 for (i=0; i<g1->ngroups; i++)
1603 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1609 * RFC 3530 language requires clid_inuse be returned when the
1610 * "principal" associated with a requests differs from that previously
1611 * used. We use uid, gid's, and gss principal string as our best
1612 * approximation. We also don't want to allow non-gss use of a client
1613 * established using gss: in theory cr_principal should catch that
1614 * change, but in practice cr_principal can be null even in the gss case
1615 * since gssd doesn't always pass down a principal string.
1617 static bool is_gss_cred(struct svc_cred *cr)
1619 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1620 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1625 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1627 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1628 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1629 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1630 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1632 if (cr1->cr_principal == cr2->cr_principal)
1634 if (!cr1->cr_principal || !cr2->cr_principal)
1636 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1639 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1641 struct svc_cred *cr = &rqstp->rq_cred;
1644 if (!cr->cr_gss_mech)
1646 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1647 return service == RPC_GSS_SVC_INTEGRITY ||
1648 service == RPC_GSS_SVC_PRIVACY;
1651 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1653 struct svc_cred *cr = &rqstp->rq_cred;
1655 if (!cl->cl_mach_cred)
1657 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1659 if (!svc_rqst_integrity_protected(rqstp))
1661 if (!cr->cr_principal)
1663 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1666 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1668 static u32 current_clientid = 1;
1670 clp->cl_clientid.cl_boot = nn->boot_time;
1671 clp->cl_clientid.cl_id = current_clientid++;
1674 static void gen_confirm(struct nfs4_client *clp)
1680 * This is opaque to client, so no need to byte-swap. Use
1681 * __force to keep sparse happy
1683 verf[0] = (__force __be32)get_seconds();
1684 verf[1] = (__force __be32)i++;
1685 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1688 static struct nfs4_stid *
1689 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1691 struct nfs4_stid *ret;
1693 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1694 if (!ret || !ret->sc_type)
1699 static struct nfs4_stid *
1700 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1702 struct nfs4_stid *s;
1704 spin_lock(&cl->cl_lock);
1705 s = find_stateid_locked(cl, t);
1707 if (typemask & s->sc_type)
1708 atomic_inc(&s->sc_count);
1712 spin_unlock(&cl->cl_lock);
1716 static struct nfs4_client *create_client(struct xdr_netobj name,
1717 struct svc_rqst *rqstp, nfs4_verifier *verf)
1719 struct nfs4_client *clp;
1720 struct sockaddr *sa = svc_addr(rqstp);
1722 struct net *net = SVC_NET(rqstp);
1723 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1725 clp = alloc_client(name);
1729 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1731 spin_lock(&nn->client_lock);
1733 spin_unlock(&nn->client_lock);
1736 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1737 clp->cl_time = get_seconds();
1738 clear_bit(0, &clp->cl_cb_slot_busy);
1739 copy_verf(clp, verf);
1740 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1742 clp->cl_cb_session = NULL;
1748 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1750 struct rb_node **new = &(root->rb_node), *parent = NULL;
1751 struct nfs4_client *clp;
1754 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1757 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1758 new = &((*new)->rb_left);
1760 new = &((*new)->rb_right);
1763 rb_link_node(&new_clp->cl_namenode, parent, new);
1764 rb_insert_color(&new_clp->cl_namenode, root);
1767 static struct nfs4_client *
1768 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1771 struct rb_node *node = root->rb_node;
1772 struct nfs4_client *clp;
1775 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1776 cmp = compare_blob(&clp->cl_name, name);
1778 node = node->rb_left;
1780 node = node->rb_right;
1788 add_to_unconfirmed(struct nfs4_client *clp)
1790 unsigned int idhashval;
1791 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1793 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1794 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1795 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1796 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1801 move_to_confirmed(struct nfs4_client *clp)
1803 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1804 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1806 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1807 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1808 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1809 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1810 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1814 static struct nfs4_client *
1815 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1817 struct nfs4_client *clp;
1818 unsigned int idhashval = clientid_hashval(clid->cl_id);
1820 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1821 if (same_clid(&clp->cl_clientid, clid)) {
1822 if ((bool)clp->cl_minorversion != sessions)
1831 static struct nfs4_client *
1832 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1834 struct list_head *tbl = nn->conf_id_hashtbl;
1836 return find_client_in_id_table(tbl, clid, sessions);
1839 static struct nfs4_client *
1840 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1842 struct list_head *tbl = nn->unconf_id_hashtbl;
1844 return find_client_in_id_table(tbl, clid, sessions);
1847 static bool clp_used_exchangeid(struct nfs4_client *clp)
1849 return clp->cl_exchange_flags != 0;
1852 static struct nfs4_client *
1853 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1855 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1858 static struct nfs4_client *
1859 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1861 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1865 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1867 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1868 struct sockaddr *sa = svc_addr(rqstp);
1869 u32 scopeid = rpc_get_scope_id(sa);
1870 unsigned short expected_family;
1872 /* Currently, we only support tcp and tcp6 for the callback channel */
1873 if (se->se_callback_netid_len == 3 &&
1874 !memcmp(se->se_callback_netid_val, "tcp", 3))
1875 expected_family = AF_INET;
1876 else if (se->se_callback_netid_len == 4 &&
1877 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1878 expected_family = AF_INET6;
1882 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1883 se->se_callback_addr_len,
1884 (struct sockaddr *)&conn->cb_addr,
1885 sizeof(conn->cb_addr));
1887 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1890 if (conn->cb_addr.ss_family == AF_INET6)
1891 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1893 conn->cb_prog = se->se_callback_prog;
1894 conn->cb_ident = se->se_callback_ident;
1895 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1898 conn->cb_addr.ss_family = AF_UNSPEC;
1899 conn->cb_addrlen = 0;
1900 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1901 "will not receive delegations\n",
1902 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1908 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1911 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1913 struct xdr_buf *buf = resp->xdr.buf;
1914 struct nfsd4_slot *slot = resp->cstate.slot;
1917 dprintk("--> %s slot %p\n", __func__, slot);
1919 slot->sl_opcnt = resp->opcnt;
1920 slot->sl_status = resp->cstate.status;
1922 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1923 if (nfsd4_not_cached(resp)) {
1924 slot->sl_datalen = 0;
1927 base = resp->cstate.data_offset;
1928 slot->sl_datalen = buf->len - base;
1929 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1930 WARN("%s: sessions DRC could not cache compound\n", __func__);
1935 * Encode the replay sequence operation from the slot values.
1936 * If cachethis is FALSE encode the uncached rep error on the next
1937 * operation which sets resp->p and increments resp->opcnt for
1938 * nfs4svc_encode_compoundres.
1942 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1943 struct nfsd4_compoundres *resp)
1945 struct nfsd4_op *op;
1946 struct nfsd4_slot *slot = resp->cstate.slot;
1948 /* Encode the replayed sequence operation */
1949 op = &args->ops[resp->opcnt - 1];
1950 nfsd4_encode_operation(resp, op);
1952 /* Return nfserr_retry_uncached_rep in next operation. */
1953 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1954 op = &args->ops[resp->opcnt++];
1955 op->status = nfserr_retry_uncached_rep;
1956 nfsd4_encode_operation(resp, op);
1962 * The sequence operation is not cached because we can use the slot and
1966 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1967 struct nfsd4_sequence *seq)
1969 struct nfsd4_slot *slot = resp->cstate.slot;
1970 struct xdr_stream *xdr = &resp->xdr;
1974 dprintk("--> %s slot %p\n", __func__, slot);
1976 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1980 p = xdr_reserve_space(xdr, slot->sl_datalen);
1983 return nfserr_serverfault;
1985 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1986 xdr_commit_encode(xdr);
1988 resp->opcnt = slot->sl_opcnt;
1989 return slot->sl_status;
1993 * Set the exchange_id flags returned by the server.
1996 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1998 /* pNFS is not supported */
1999 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2001 /* Referrals are supported, Migration is not. */
2002 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2004 /* set the wire flags to return to client. */
2005 clid->flags = new->cl_exchange_flags;
2008 static bool client_has_state(struct nfs4_client *clp)
2011 * Note clp->cl_openowners check isn't quite right: there's no
2012 * need to count owners without stateid's.
2014 * Also note we should probably be using this in 4.0 case too.
2016 return !list_empty(&clp->cl_openowners)
2017 || !list_empty(&clp->cl_delegations)
2018 || !list_empty(&clp->cl_sessions);
2022 nfsd4_exchange_id(struct svc_rqst *rqstp,
2023 struct nfsd4_compound_state *cstate,
2024 struct nfsd4_exchange_id *exid)
2026 struct nfs4_client *unconf, *conf, *new;
2028 char addr_str[INET6_ADDRSTRLEN];
2029 nfs4_verifier verf = exid->verifier;
2030 struct sockaddr *sa = svc_addr(rqstp);
2031 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2032 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2034 rpc_ntop(sa, addr_str, sizeof(addr_str));
2035 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2036 "ip_addr=%s flags %x, spa_how %d\n",
2037 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2038 addr_str, exid->flags, exid->spa_how);
2040 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2041 return nfserr_inval;
2043 switch (exid->spa_how) {
2045 if (!svc_rqst_integrity_protected(rqstp))
2046 return nfserr_inval;
2049 default: /* checked by xdr code */
2052 return nfserr_encr_alg_unsupp;
2055 /* Cases below refer to rfc 5661 section 18.35.4: */
2057 conf = find_confirmed_client_by_name(&exid->clname, nn);
2059 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2060 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2063 if (!clp_used_exchangeid(conf)) { /* buggy client */
2064 status = nfserr_inval;
2067 if (!mach_creds_match(conf, rqstp)) {
2068 status = nfserr_wrong_cred;
2071 if (!creds_match) { /* case 9 */
2072 status = nfserr_perm;
2075 if (!verfs_match) { /* case 8 */
2076 status = nfserr_not_same;
2080 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2084 if (!creds_match) { /* case 3 */
2085 if (client_has_state(conf)) {
2086 status = nfserr_clid_inuse;
2089 expire_client(conf);
2092 if (verfs_match) { /* case 2 */
2093 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2097 /* case 5, client reboot */
2101 if (update) { /* case 7 */
2102 status = nfserr_noent;
2106 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2107 if (unconf) /* case 4, possible retry or client restart */
2108 expire_client(unconf);
2110 /* case 1 (normal case) */
2112 new = create_client(exid->clname, rqstp, &verf);
2114 status = nfserr_jukebox;
2117 new->cl_minorversion = cstate->minorversion;
2118 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2121 add_to_unconfirmed(new);
2123 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2124 exid->clientid.cl_id = new->cl_clientid.cl_id;
2126 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2127 nfsd4_set_ex_flags(new, exid);
2129 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2130 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2134 nfs4_unlock_state();
2139 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2141 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2144 /* The slot is in use, and no response has been sent. */
2146 if (seqid == slot_seqid)
2147 return nfserr_jukebox;
2149 return nfserr_seq_misordered;
2151 /* Note unsigned 32-bit arithmetic handles wraparound: */
2152 if (likely(seqid == slot_seqid + 1))
2154 if (seqid == slot_seqid)
2155 return nfserr_replay_cache;
2156 return nfserr_seq_misordered;
2160 * Cache the create session result into the create session single DRC
2161 * slot cache by saving the xdr structure. sl_seqid has been set.
2162 * Do this for solo or embedded create session operations.
2165 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2166 struct nfsd4_clid_slot *slot, __be32 nfserr)
2168 slot->sl_status = nfserr;
2169 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2173 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2174 struct nfsd4_clid_slot *slot)
2176 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2177 return slot->sl_status;
2180 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2181 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2182 1 + /* MIN tag is length with zero, only length */ \
2183 3 + /* version, opcount, opcode */ \
2184 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2185 /* seqid, slotID, slotID, cache */ \
2186 4 ) * sizeof(__be32))
2188 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2189 2 + /* verifier: AUTH_NULL, length 0 */\
2191 1 + /* MIN tag is length with zero, only length */ \
2192 3 + /* opcount, opcode, opstatus*/ \
2193 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2194 /* seqid, slotID, slotID, slotID, status */ \
2195 5 ) * sizeof(__be32))
2197 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2199 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2201 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2202 return nfserr_toosmall;
2203 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2204 return nfserr_toosmall;
2205 ca->headerpadsz = 0;
2206 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2207 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2208 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2209 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2210 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2211 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2213 * Note decreasing slot size below client's request may make it
2214 * difficult for client to function correctly, whereas
2215 * decreasing the number of slots will (just?) affect
2216 * performance. When short on memory we therefore prefer to
2217 * decrease number of slots instead of their size. Clients that
2218 * request larger slots than they need will get poor results:
2220 ca->maxreqs = nfsd4_get_drc_mem(ca);
2222 return nfserr_jukebox;
2227 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2228 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2229 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2230 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2232 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2234 ca->headerpadsz = 0;
2237 * These RPC_MAX_HEADER macros are overkill, especially since we
2238 * don't even do gss on the backchannel yet. But this is still
2239 * less than 1k. Tighten up this estimate in the unlikely event
2240 * it turns out to be a problem for some client:
2242 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2243 return nfserr_toosmall;
2244 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2245 return nfserr_toosmall;
2246 ca->maxresp_cached = 0;
2248 return nfserr_toosmall;
2253 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2255 switch (cbs->flavor) {
2261 * GSS case: the spec doesn't allow us to return this
2262 * error. But it also doesn't allow us not to support
2264 * I'd rather this fail hard than return some error the
2265 * client might think it can already handle:
2267 return nfserr_encr_alg_unsupp;
2272 nfsd4_create_session(struct svc_rqst *rqstp,
2273 struct nfsd4_compound_state *cstate,
2274 struct nfsd4_create_session *cr_ses)
2276 struct sockaddr *sa = svc_addr(rqstp);
2277 struct nfs4_client *conf, *unconf;
2278 struct nfsd4_session *new;
2279 struct nfsd4_conn *conn;
2280 struct nfsd4_clid_slot *cs_slot = NULL;
2282 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2284 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2285 return nfserr_inval;
2286 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2289 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2292 status = check_backchannel_attrs(&cr_ses->back_channel);
2294 goto out_release_drc_mem;
2295 status = nfserr_jukebox;
2296 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2298 goto out_release_drc_mem;
2299 conn = alloc_conn_from_crses(rqstp, cr_ses);
2301 goto out_free_session;
2304 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2305 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2306 WARN_ON_ONCE(conf && unconf);
2309 status = nfserr_wrong_cred;
2310 if (!mach_creds_match(conf, rqstp))
2312 cs_slot = &conf->cl_cs_slot;
2313 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2314 if (status == nfserr_replay_cache) {
2315 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2317 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2318 status = nfserr_seq_misordered;
2321 } else if (unconf) {
2322 struct nfs4_client *old;
2323 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2324 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2325 status = nfserr_clid_inuse;
2328 status = nfserr_wrong_cred;
2329 if (!mach_creds_match(unconf, rqstp))
2331 cs_slot = &unconf->cl_cs_slot;
2332 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2334 /* an unconfirmed replay returns misordered */
2335 status = nfserr_seq_misordered;
2338 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2340 status = mark_client_expired(old);
2345 move_to_confirmed(unconf);
2348 status = nfserr_stale_clientid;
2353 * We do not support RDMA or persistent sessions
2355 cr_ses->flags &= ~SESSION4_PERSIST;
2356 cr_ses->flags &= ~SESSION4_RDMA;
2358 init_session(rqstp, new, conf, cr_ses);
2359 nfsd4_init_conn(rqstp, conn, new);
2361 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2362 NFS4_MAX_SESSIONID_LEN);
2363 cs_slot->sl_seqid++;
2364 cr_ses->seqid = cs_slot->sl_seqid;
2366 /* cache solo and embedded create sessions under the state lock */
2367 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2368 nfs4_unlock_state();
2371 nfs4_unlock_state();
2374 __free_session(new);
2375 out_release_drc_mem:
2376 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2380 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2383 case NFS4_CDFC4_FORE:
2384 case NFS4_CDFC4_BACK:
2386 case NFS4_CDFC4_FORE_OR_BOTH:
2387 case NFS4_CDFC4_BACK_OR_BOTH:
2388 *dir = NFS4_CDFC4_BOTH;
2391 return nfserr_inval;
2394 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2396 struct nfsd4_session *session = cstate->session;
2397 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2400 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2403 spin_lock(&nn->client_lock);
2404 session->se_cb_prog = bc->bc_cb_program;
2405 session->se_cb_sec = bc->bc_cb_sec;
2406 spin_unlock(&nn->client_lock);
2408 nfsd4_probe_callback(session->se_client);
2413 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2414 struct nfsd4_compound_state *cstate,
2415 struct nfsd4_bind_conn_to_session *bcts)
2418 struct nfsd4_conn *conn;
2419 struct nfsd4_session *session;
2420 struct net *net = SVC_NET(rqstp);
2421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2423 if (!nfsd4_last_compound_op(rqstp))
2424 return nfserr_not_only_op;
2426 spin_lock(&nn->client_lock);
2427 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2428 spin_unlock(&nn->client_lock);
2430 goto out_no_session;
2431 status = nfserr_wrong_cred;
2432 if (!mach_creds_match(session->se_client, rqstp))
2434 status = nfsd4_map_bcts_dir(&bcts->dir);
2437 conn = alloc_conn(rqstp, bcts->dir);
2438 status = nfserr_jukebox;
2441 nfsd4_init_conn(rqstp, conn, session);
2444 nfsd4_put_session(session);
2446 nfs4_unlock_state();
2450 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2454 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2458 nfsd4_destroy_session(struct svc_rqst *r,
2459 struct nfsd4_compound_state *cstate,
2460 struct nfsd4_destroy_session *sessionid)
2462 struct nfsd4_session *ses;
2464 int ref_held_by_me = 0;
2465 struct net *net = SVC_NET(r);
2466 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2469 status = nfserr_not_only_op;
2470 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2471 if (!nfsd4_last_compound_op(r))
2475 dump_sessionid(__func__, &sessionid->sessionid);
2476 spin_lock(&nn->client_lock);
2477 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2479 goto out_client_lock;
2480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(ses->se_client, r))
2482 goto out_put_session;
2483 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2485 goto out_put_session;
2486 unhash_session(ses);
2487 spin_unlock(&nn->client_lock);
2489 nfsd4_probe_callback_sync(ses->se_client);
2491 spin_lock(&nn->client_lock);
2494 nfsd4_put_session_locked(ses);
2496 spin_unlock(&nn->client_lock);
2498 nfs4_unlock_state();
2502 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2504 struct nfsd4_conn *c;
2506 list_for_each_entry(c, &s->se_conns, cn_persession) {
2507 if (c->cn_xprt == xpt) {
2514 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2516 struct nfs4_client *clp = ses->se_client;
2517 struct nfsd4_conn *c;
2518 __be32 status = nfs_ok;
2521 spin_lock(&clp->cl_lock);
2522 c = __nfsd4_find_conn(new->cn_xprt, ses);
2525 status = nfserr_conn_not_bound_to_session;
2526 if (clp->cl_mach_cred)
2528 __nfsd4_hash_conn(new, ses);
2529 spin_unlock(&clp->cl_lock);
2530 ret = nfsd4_register_conn(new);
2532 /* oops; xprt is already down: */
2533 nfsd4_conn_lost(&new->cn_xpt_user);
2536 spin_unlock(&clp->cl_lock);
2541 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2543 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2545 return args->opcnt > session->se_fchannel.maxops;
2548 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2549 struct nfsd4_session *session)
2551 struct xdr_buf *xb = &rqstp->rq_arg;
2553 return xb->len > session->se_fchannel.maxreq_sz;
2557 nfsd4_sequence(struct svc_rqst *rqstp,
2558 struct nfsd4_compound_state *cstate,
2559 struct nfsd4_sequence *seq)
2561 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2562 struct xdr_stream *xdr = &resp->xdr;
2563 struct nfsd4_session *session;
2564 struct nfs4_client *clp;
2565 struct nfsd4_slot *slot;
2566 struct nfsd4_conn *conn;
2569 struct net *net = SVC_NET(rqstp);
2570 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2572 if (resp->opcnt != 1)
2573 return nfserr_sequence_pos;
2576 * Will be either used or freed by nfsd4_sequence_check_conn
2579 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2581 return nfserr_jukebox;
2583 spin_lock(&nn->client_lock);
2584 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2586 goto out_no_session;
2587 clp = session->se_client;
2589 status = nfserr_too_many_ops;
2590 if (nfsd4_session_too_many_ops(rqstp, session))
2591 goto out_put_session;
2593 status = nfserr_req_too_big;
2594 if (nfsd4_request_too_big(rqstp, session))
2595 goto out_put_session;
2597 status = nfserr_badslot;
2598 if (seq->slotid >= session->se_fchannel.maxreqs)
2599 goto out_put_session;
2601 slot = session->se_slots[seq->slotid];
2602 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2604 /* We do not negotiate the number of slots yet, so set the
2605 * maxslots to the session maxreqs which is used to encode
2606 * sr_highest_slotid and the sr_target_slot id to maxslots */
2607 seq->maxslots = session->se_fchannel.maxreqs;
2609 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2610 slot->sl_flags & NFSD4_SLOT_INUSE);
2611 if (status == nfserr_replay_cache) {
2612 status = nfserr_seq_misordered;
2613 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2614 goto out_put_session;
2615 cstate->slot = slot;
2616 cstate->session = session;
2618 /* Return the cached reply status and set cstate->status
2619 * for nfsd4_proc_compound processing */
2620 status = nfsd4_replay_cache_entry(resp, seq);
2621 cstate->status = nfserr_replay_cache;
2625 goto out_put_session;
2627 status = nfsd4_sequence_check_conn(conn, session);
2630 goto out_put_session;
2632 buflen = (seq->cachethis) ?
2633 session->se_fchannel.maxresp_cached :
2634 session->se_fchannel.maxresp_sz;
2635 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2637 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2638 goto out_put_session;
2639 svc_reserve(rqstp, buflen);
2642 /* Success! bump slot seqid */
2643 slot->sl_seqid = seq->seqid;
2644 slot->sl_flags |= NFSD4_SLOT_INUSE;
2646 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2648 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2650 cstate->slot = slot;
2651 cstate->session = session;
2655 switch (clp->cl_cb_state) {
2657 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2659 case NFSD4_CB_FAULT:
2660 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2663 seq->status_flags = 0;
2665 if (!list_empty(&clp->cl_revoked))
2666 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2670 spin_unlock(&nn->client_lock);
2673 nfsd4_put_session_locked(session);
2674 goto out_no_session;
2678 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2680 struct nfsd4_compound_state *cs = &resp->cstate;
2682 if (nfsd4_has_session(cs)) {
2683 if (cs->status != nfserr_replay_cache) {
2684 nfsd4_store_cache_entry(resp);
2685 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2687 /* Drop session reference that was taken in nfsd4_sequence() */
2688 nfsd4_put_session(cs->session);
2690 put_client_renew(cs->clp);
2694 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2696 struct nfs4_client *conf, *unconf, *clp;
2698 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2701 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2702 conf = find_confirmed_client(&dc->clientid, true, nn);
2703 WARN_ON_ONCE(conf && unconf);
2708 if (client_has_state(conf)) {
2709 status = nfserr_clientid_busy;
2715 status = nfserr_stale_clientid;
2718 if (!mach_creds_match(clp, rqstp)) {
2719 status = nfserr_wrong_cred;
2724 nfs4_unlock_state();
2729 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2733 if (rc->rca_one_fs) {
2734 if (!cstate->current_fh.fh_dentry)
2735 return nfserr_nofilehandle;
2737 * We don't take advantage of the rca_one_fs case.
2738 * That's OK, it's optional, we can safely ignore it.
2744 status = nfserr_complete_already;
2745 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2746 &cstate->session->se_client->cl_flags))
2749 status = nfserr_stale_clientid;
2750 if (is_client_expired(cstate->session->se_client))
2752 * The following error isn't really legal.
2753 * But we only get here if the client just explicitly
2754 * destroyed the client. Surely it no longer cares what
2755 * error it gets back on an operation for the dead
2761 nfsd4_client_record_create(cstate->session->se_client);
2763 nfs4_unlock_state();
2768 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2769 struct nfsd4_setclientid *setclid)
2771 struct xdr_netobj clname = setclid->se_name;
2772 nfs4_verifier clverifier = setclid->se_verf;
2773 struct nfs4_client *conf, *unconf, *new;
2775 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2777 /* Cases below refer to rfc 3530 section 14.2.33: */
2779 conf = find_confirmed_client_by_name(&clname, nn);
2782 status = nfserr_clid_inuse;
2783 if (clp_used_exchangeid(conf))
2785 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2786 char addr_str[INET6_ADDRSTRLEN];
2787 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2789 dprintk("NFSD: setclientid: string in use by client "
2790 "at %s\n", addr_str);
2794 unconf = find_unconfirmed_client_by_name(&clname, nn);
2796 expire_client(unconf);
2797 status = nfserr_jukebox;
2798 new = create_client(clname, rqstp, &clverifier);
2801 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2802 /* case 1: probable callback update */
2803 copy_clid(new, conf);
2804 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2806 new->cl_minorversion = 0;
2807 gen_callback(new, setclid, rqstp);
2808 add_to_unconfirmed(new);
2809 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2810 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2811 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2814 nfs4_unlock_state();
2820 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2821 struct nfsd4_compound_state *cstate,
2822 struct nfsd4_setclientid_confirm *setclientid_confirm)
2824 struct nfs4_client *conf, *unconf;
2825 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2826 clientid_t * clid = &setclientid_confirm->sc_clientid;
2828 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2830 if (STALE_CLIENTID(clid, nn))
2831 return nfserr_stale_clientid;
2834 conf = find_confirmed_client(clid, false, nn);
2835 unconf = find_unconfirmed_client(clid, false, nn);
2837 * We try hard to give out unique clientid's, so if we get an
2838 * attempt to confirm the same clientid with a different cred,
2839 * there's a bug somewhere. Let's charitably assume it's our
2842 status = nfserr_serverfault;
2843 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2845 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2847 /* cases below refer to rfc 3530 section 14.2.34: */
2848 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2849 if (conf && !unconf) /* case 2: probable retransmit */
2851 else /* case 4: client hasn't noticed we rebooted yet? */
2852 status = nfserr_stale_clientid;
2856 if (conf) { /* case 1: callback update */
2857 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2858 nfsd4_probe_callback(conf);
2859 expire_client(unconf);
2860 } else { /* case 3: normal case; new or rebooted client */
2861 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2863 status = mark_client_expired(conf);
2866 expire_client(conf);
2868 move_to_confirmed(unconf);
2869 nfsd4_probe_callback(unconf);
2872 nfs4_unlock_state();
2876 static struct nfs4_file *nfsd4_alloc_file(void)
2878 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2881 /* OPEN Share state helper functions */
2882 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2884 unsigned int hashval = file_hashval(fh);
2886 lockdep_assert_held(&state_lock);
2888 atomic_set(&fp->fi_ref, 1);
2889 spin_lock_init(&fp->fi_lock);
2890 INIT_LIST_HEAD(&fp->fi_stateids);
2891 INIT_LIST_HEAD(&fp->fi_delegations);
2892 fh_copy_shallow(&fp->fi_fhandle, fh);
2893 fp->fi_had_conflict = false;
2894 fp->fi_lease = NULL;
2895 fp->fi_share_deny = 0;
2896 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2897 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2898 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2902 nfsd4_free_slabs(void)
2904 kmem_cache_destroy(openowner_slab);
2905 kmem_cache_destroy(lockowner_slab);
2906 kmem_cache_destroy(file_slab);
2907 kmem_cache_destroy(stateid_slab);
2908 kmem_cache_destroy(deleg_slab);
2912 nfsd4_init_slabs(void)
2914 openowner_slab = kmem_cache_create("nfsd4_openowners",
2915 sizeof(struct nfs4_openowner), 0, 0, NULL);
2916 if (openowner_slab == NULL)
2918 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2919 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2920 if (lockowner_slab == NULL)
2921 goto out_free_openowner_slab;
2922 file_slab = kmem_cache_create("nfsd4_files",
2923 sizeof(struct nfs4_file), 0, 0, NULL);
2924 if (file_slab == NULL)
2925 goto out_free_lockowner_slab;
2926 stateid_slab = kmem_cache_create("nfsd4_stateids",
2927 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2928 if (stateid_slab == NULL)
2929 goto out_free_file_slab;
2930 deleg_slab = kmem_cache_create("nfsd4_delegations",
2931 sizeof(struct nfs4_delegation), 0, 0, NULL);
2932 if (deleg_slab == NULL)
2933 goto out_free_stateid_slab;
2936 out_free_stateid_slab:
2937 kmem_cache_destroy(stateid_slab);
2939 kmem_cache_destroy(file_slab);
2940 out_free_lockowner_slab:
2941 kmem_cache_destroy(lockowner_slab);
2942 out_free_openowner_slab:
2943 kmem_cache_destroy(openowner_slab);
2945 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2949 static void init_nfs4_replay(struct nfs4_replay *rp)
2951 rp->rp_status = nfserr_serverfault;
2953 rp->rp_buf = rp->rp_ibuf;
2954 mutex_init(&rp->rp_mutex);
2957 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
2958 struct nfs4_stateowner *so)
2960 if (!nfsd4_has_session(cstate)) {
2961 mutex_lock(&so->so_replay.rp_mutex);
2962 cstate->replay_owner = so;
2963 atomic_inc(&so->so_count);
2967 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
2969 struct nfs4_stateowner *so = cstate->replay_owner;
2972 cstate->replay_owner = NULL;
2973 mutex_unlock(&so->so_replay.rp_mutex);
2974 nfs4_put_stateowner(so);
2978 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2980 struct nfs4_stateowner *sop;
2982 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2986 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2987 if (!sop->so_owner.data) {
2988 kmem_cache_free(slab, sop);
2991 sop->so_owner.len = owner->len;
2993 INIT_LIST_HEAD(&sop->so_stateids);
2994 sop->so_client = clp;
2995 init_nfs4_replay(&sop->so_replay);
2996 atomic_set(&sop->so_count, 1);
3000 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3002 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
3004 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
3005 list_add(&oo->oo_perclient, &clp->cl_openowners);
3008 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3010 struct nfs4_openowner *oo = openowner(so);
3012 unhash_openowner(oo);
3015 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3017 struct nfs4_openowner *oo = openowner(so);
3019 kmem_cache_free(openowner_slab, oo);
3022 static const struct nfs4_stateowner_operations openowner_ops = {
3023 .so_unhash = nfs4_unhash_openowner,
3024 .so_free = nfs4_free_openowner,
3027 static struct nfs4_openowner *
3028 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3029 struct nfsd4_compound_state *cstate)
3031 struct nfs4_client *clp = cstate->clp;
3032 struct nfs4_openowner *oo;
3034 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3037 oo->oo_owner.so_ops = &openowner_ops;
3038 oo->oo_owner.so_is_open_owner = 1;
3039 oo->oo_owner.so_seqid = open->op_seqid;
3040 oo->oo_flags = NFS4_OO_NEW;
3041 if (nfsd4_has_session(cstate))
3042 oo->oo_flags |= NFS4_OO_CONFIRMED;
3044 oo->oo_last_closed_stid = NULL;
3045 INIT_LIST_HEAD(&oo->oo_close_lru);
3046 hash_openowner(oo, clp, strhashval);
3050 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3051 struct nfs4_openowner *oo = open->op_openowner;
3053 atomic_inc(&stp->st_stid.sc_count);
3054 stp->st_stid.sc_type = NFS4_OPEN_STID;
3055 INIT_LIST_HEAD(&stp->st_locks);
3056 stp->st_stateowner = &oo->oo_owner;
3058 stp->st_stid.sc_file = fp;
3059 stp->st_access_bmap = 0;
3060 stp->st_deny_bmap = 0;
3061 stp->st_openstp = NULL;
3062 spin_lock(&oo->oo_owner.so_client->cl_lock);
3063 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3064 spin_lock(&fp->fi_lock);
3065 list_add(&stp->st_perfile, &fp->fi_stateids);
3066 spin_unlock(&fp->fi_lock);
3067 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3071 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
3073 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3075 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3077 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3078 oo->oo_time = get_seconds();
3082 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3085 return (sop->so_owner.len == owner->len) &&
3086 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3087 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3090 static struct nfs4_openowner *
3091 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3092 bool sessions, struct nfsd_net *nn)
3094 struct nfs4_stateowner *so;
3095 struct nfs4_openowner *oo;
3096 struct nfs4_client *clp;
3098 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3099 if (!so->so_is_open_owner)
3101 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3103 clp = oo->oo_owner.so_client;
3104 if ((bool)clp->cl_minorversion != sessions)
3106 renew_client(oo->oo_owner.so_client);
3113 /* search file_hashtbl[] for file */
3114 static struct nfs4_file *
3115 find_file_locked(struct knfsd_fh *fh)
3117 unsigned int hashval = file_hashval(fh);
3118 struct nfs4_file *fp;
3120 lockdep_assert_held(&state_lock);
3122 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3123 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3131 static struct nfs4_file *
3132 find_file(struct knfsd_fh *fh)
3134 struct nfs4_file *fp;
3136 spin_lock(&state_lock);
3137 fp = find_file_locked(fh);
3138 spin_unlock(&state_lock);
3142 static struct nfs4_file *
3143 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3145 struct nfs4_file *fp;
3147 spin_lock(&state_lock);
3148 fp = find_file_locked(fh);
3150 nfsd4_init_file(new, fh);
3153 spin_unlock(&state_lock);
3159 * Called to check deny when READ with all zero stateid or
3160 * WRITE with all zero or all one stateid
3163 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3165 struct nfs4_file *fp;
3166 __be32 ret = nfs_ok;
3168 fp = find_file(¤t_fh->fh_handle);
3171 /* Check for conflicting share reservations */
3172 spin_lock(&fp->fi_lock);
3173 if (fp->fi_share_deny & deny_type)
3174 ret = nfserr_locked;
3175 spin_unlock(&fp->fi_lock);
3180 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3182 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3185 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3188 * We can't do this in nfsd_break_deleg_cb because it is
3189 * already holding inode->i_lock.
3191 * If the dl_time != 0, then we know that it has already been
3192 * queued for a lease break. Don't queue it again.
3194 spin_lock(&state_lock);
3195 if (dp->dl_time == 0) {
3196 dp->dl_time = get_seconds();
3197 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3199 spin_unlock(&state_lock);
3202 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3205 * We're assuming the state code never drops its reference
3206 * without first removing the lease. Since we're in this lease
3207 * callback (and since the lease code is serialized by the kernel
3208 * lock) we know the server hasn't removed the lease yet, we know
3209 * it's safe to take a reference.
3211 atomic_inc(&dp->dl_stid.sc_count);
3212 nfsd4_cb_recall(dp);
3215 /* Called from break_lease() with i_lock held. */
3216 static void nfsd_break_deleg_cb(struct file_lock *fl)
3218 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3219 struct nfs4_delegation *dp;
3222 WARN(1, "(%p)->fl_owner NULL\n", fl);
3225 if (fp->fi_had_conflict) {
3226 WARN(1, "duplicate break on %p\n", fp);
3230 * We don't want the locks code to timeout the lease for us;
3231 * we'll remove it ourself if a delegation isn't returned
3234 fl->fl_break_time = 0;
3236 spin_lock(&fp->fi_lock);
3237 fp->fi_had_conflict = true;
3239 * If there are no delegations on the list, then we can't count on this
3240 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3241 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3242 * true should keep any new delegations from being hashed.
3244 if (list_empty(&fp->fi_delegations))
3245 fl->fl_break_time = jiffies;
3247 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3248 nfsd_break_one_deleg(dp);
3249 spin_unlock(&fp->fi_lock);
3253 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3256 return lease_modify(onlist, arg);
3261 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3262 .lm_break = nfsd_break_deleg_cb,
3263 .lm_change = nfsd_change_deleg_cb,
3266 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3268 if (nfsd4_has_session(cstate))
3270 if (seqid == so->so_seqid - 1)
3271 return nfserr_replay_me;
3272 if (seqid == so->so_seqid)
3274 return nfserr_bad_seqid;
3277 static __be32 lookup_clientid(clientid_t *clid,
3278 struct nfsd4_compound_state *cstate,
3279 struct nfsd_net *nn)
3281 struct nfs4_client *found;
3284 found = cstate->clp;
3285 if (!same_clid(&found->cl_clientid, clid))
3286 return nfserr_stale_clientid;
3290 if (STALE_CLIENTID(clid, nn))
3291 return nfserr_stale_clientid;
3294 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3295 * cached already then we know this is for is for v4.0 and "sessions"
3298 WARN_ON_ONCE(cstate->session);
3299 found = find_confirmed_client(clid, false, nn);
3301 return nfserr_expired;
3303 /* Cache the nfs4_client in cstate! */
3304 cstate->clp = found;
3305 atomic_inc(&found->cl_refcount);
3310 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3311 struct nfsd4_open *open, struct nfsd_net *nn)
3313 clientid_t *clientid = &open->op_clientid;
3314 struct nfs4_client *clp = NULL;
3315 unsigned int strhashval;
3316 struct nfs4_openowner *oo = NULL;
3319 if (STALE_CLIENTID(&open->op_clientid, nn))
3320 return nfserr_stale_clientid;
3322 * In case we need it later, after we've already created the
3323 * file and don't want to risk a further failure:
3325 open->op_file = nfsd4_alloc_file();
3326 if (open->op_file == NULL)
3327 return nfserr_jukebox;
3329 status = lookup_clientid(clientid, cstate, nn);
3334 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3335 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3336 open->op_openowner = oo;
3340 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3341 /* Replace unconfirmed owners without checking for replay. */
3342 release_openowner(oo);
3343 open->op_openowner = NULL;
3346 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3351 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3353 return nfserr_jukebox;
3354 open->op_openowner = oo;
3356 open->op_stp = nfs4_alloc_open_stateid(clp);
3358 return nfserr_jukebox;
3362 static inline __be32
3363 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3365 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3366 return nfserr_openmode;
3371 static int share_access_to_flags(u32 share_access)
3373 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3376 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3378 struct nfs4_stid *ret;
3380 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3383 return delegstateid(ret);
3386 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3388 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3389 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3393 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3394 struct nfs4_delegation **dp)
3397 __be32 status = nfserr_bad_stateid;
3398 struct nfs4_delegation *deleg;
3400 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3403 flags = share_access_to_flags(open->op_share_access);
3404 status = nfs4_check_delegmode(deleg, flags);
3406 nfs4_put_stid(&deleg->dl_stid);
3411 if (!nfsd4_is_deleg_cur(open))
3415 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3419 static struct nfs4_ol_stateid *
3420 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3422 struct nfs4_ol_stateid *local, *ret = NULL;
3423 struct nfs4_openowner *oo = open->op_openowner;
3425 spin_lock(&fp->fi_lock);
3426 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3427 /* ignore lock owners */
3428 if (local->st_stateowner->so_is_open_owner == 0)
3430 if (local->st_stateowner == &oo->oo_owner) {
3432 atomic_inc(&ret->st_stid.sc_count);
3436 spin_unlock(&fp->fi_lock);
3440 static inline int nfs4_access_to_access(u32 nfs4_access)
3444 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3445 flags |= NFSD_MAY_READ;
3446 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3447 flags |= NFSD_MAY_WRITE;
3451 static inline __be32
3452 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3453 struct nfsd4_open *open)
3455 struct iattr iattr = {
3456 .ia_valid = ATTR_SIZE,
3459 if (!open->op_truncate)
3461 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3462 return nfserr_inval;
3463 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3466 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3467 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3468 struct nfsd4_open *open)
3470 struct file *filp = NULL;
3472 int oflag = nfs4_access_to_omode(open->op_share_access);
3473 int access = nfs4_access_to_access(open->op_share_access);
3474 unsigned char old_access_bmap, old_deny_bmap;
3476 spin_lock(&fp->fi_lock);
3479 * Are we trying to set a deny mode that would conflict with
3482 status = nfs4_file_check_deny(fp, open->op_share_deny);
3483 if (status != nfs_ok) {
3484 spin_unlock(&fp->fi_lock);
3488 /* set access to the file */
3489 status = nfs4_file_get_access(fp, open->op_share_access);
3490 if (status != nfs_ok) {
3491 spin_unlock(&fp->fi_lock);
3495 /* Set access bits in stateid */
3496 old_access_bmap = stp->st_access_bmap;
3497 set_access(open->op_share_access, stp);
3499 /* Set new deny mask */
3500 old_deny_bmap = stp->st_deny_bmap;
3501 set_deny(open->op_share_deny, stp);
3502 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3504 if (!fp->fi_fds[oflag]) {
3505 spin_unlock(&fp->fi_lock);
3506 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3508 goto out_put_access;
3509 spin_lock(&fp->fi_lock);
3510 if (!fp->fi_fds[oflag]) {
3511 fp->fi_fds[oflag] = filp;
3515 spin_unlock(&fp->fi_lock);
3519 status = nfsd4_truncate(rqstp, cur_fh, open);
3521 goto out_put_access;
3525 stp->st_access_bmap = old_access_bmap;
3526 nfs4_file_put_access(fp, open->op_share_access);
3527 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3532 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)
3535 unsigned char old_deny_bmap;
3537 if (!test_access(open->op_share_access, stp))
3538 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3540 /* test and set deny mode */
3541 spin_lock(&fp->fi_lock);
3542 status = nfs4_file_check_deny(fp, open->op_share_deny);
3543 if (status == nfs_ok) {
3544 old_deny_bmap = stp->st_deny_bmap;
3545 set_deny(open->op_share_deny, stp);
3546 fp->fi_share_deny |=
3547 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3549 spin_unlock(&fp->fi_lock);
3551 if (status != nfs_ok)
3554 status = nfsd4_truncate(rqstp, cur_fh, open);
3555 if (status != nfs_ok)
3556 reset_union_bmap_deny(old_deny_bmap, stp);
3561 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3563 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3566 /* Should we give out recallable state?: */
3567 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3569 if (clp->cl_cb_state == NFSD4_CB_UP)
3572 * In the sessions case, since we don't have to establish a
3573 * separate connection for callbacks, we assume it's OK
3574 * until we hear otherwise:
3576 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3579 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3581 struct file_lock *fl;
3583 fl = locks_alloc_lock();
3586 locks_init_lock(fl);
3587 fl->fl_lmops = &nfsd_lease_mng_ops;
3588 fl->fl_flags = FL_DELEG;
3589 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3590 fl->fl_end = OFFSET_MAX;
3591 fl->fl_owner = (fl_owner_t)fp;
3592 fl->fl_pid = current->tgid;
3596 static int nfs4_setlease(struct nfs4_delegation *dp)
3598 struct nfs4_file *fp = dp->dl_stid.sc_file;
3599 struct file_lock *fl;
3603 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3606 filp = find_readable_file(fp);
3608 /* We should always have a readable file here */
3613 status = vfs_setlease(filp, fl->fl_type, &fl);
3615 locks_free_lock(fl);
3618 spin_lock(&state_lock);
3619 spin_lock(&fp->fi_lock);
3620 /* Did the lease get broken before we took the lock? */
3622 if (fp->fi_had_conflict)
3627 atomic_inc(&fp->fi_delegees);
3628 hash_delegation_locked(dp, fp);
3632 fp->fi_deleg_file = filp;
3633 atomic_set(&fp->fi_delegees, 1);
3634 hash_delegation_locked(dp, fp);
3635 spin_unlock(&fp->fi_lock);
3636 spin_unlock(&state_lock);
3639 spin_unlock(&fp->fi_lock);
3640 spin_unlock(&state_lock);
3646 static struct nfs4_delegation *
3647 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3648 struct nfs4_file *fp)
3651 struct nfs4_delegation *dp;
3653 if (fp->fi_had_conflict)
3654 return ERR_PTR(-EAGAIN);
3656 dp = alloc_init_deleg(clp, fh);
3658 return ERR_PTR(-ENOMEM);
3661 spin_lock(&state_lock);
3662 spin_lock(&fp->fi_lock);
3663 dp->dl_stid.sc_file = fp;
3664 if (!fp->fi_lease) {
3665 spin_unlock(&fp->fi_lock);
3666 spin_unlock(&state_lock);
3667 status = nfs4_setlease(dp);
3670 atomic_inc(&fp->fi_delegees);
3671 if (fp->fi_had_conflict) {
3675 hash_delegation_locked(dp, fp);
3678 spin_unlock(&fp->fi_lock);
3679 spin_unlock(&state_lock);
3682 nfs4_put_stid(&dp->dl_stid);
3683 return ERR_PTR(status);
3688 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3690 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3691 if (status == -EAGAIN)
3692 open->op_why_no_deleg = WND4_CONTENTION;
3694 open->op_why_no_deleg = WND4_RESOURCE;
3695 switch (open->op_deleg_want) {
3696 case NFS4_SHARE_WANT_READ_DELEG:
3697 case NFS4_SHARE_WANT_WRITE_DELEG:
3698 case NFS4_SHARE_WANT_ANY_DELEG:
3700 case NFS4_SHARE_WANT_CANCEL:
3701 open->op_why_no_deleg = WND4_CANCELLED;
3703 case NFS4_SHARE_WANT_NO_DELEG:
3710 * Attempt to hand out a delegation.
3712 * Note we don't support write delegations, and won't until the vfs has
3713 * proper support for them.
3716 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3717 struct nfs4_ol_stateid *stp)
3719 struct nfs4_delegation *dp;
3720 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3721 struct nfs4_client *clp = stp->st_stid.sc_client;
3725 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3726 open->op_recall = 0;
3727 switch (open->op_claim_type) {
3728 case NFS4_OPEN_CLAIM_PREVIOUS:
3730 open->op_recall = 1;
3731 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3734 case NFS4_OPEN_CLAIM_NULL:
3735 case NFS4_OPEN_CLAIM_FH:
3737 * Let's not give out any delegations till everyone's
3738 * had the chance to reclaim theirs....
3740 if (locks_in_grace(clp->net))
3742 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3745 * Also, if the file was opened for write or
3746 * create, there's a good chance the client's
3747 * about to write to it, resulting in an
3748 * immediate recall (since we don't support
3749 * write delegations):
3751 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3753 if (open->op_create == NFS4_OPEN_CREATE)
3759 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3763 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3765 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3766 STATEID_VAL(&dp->dl_stid.sc_stateid));
3767 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3768 nfs4_put_stid(&dp->dl_stid);
3771 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3772 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3773 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3774 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3775 open->op_recall = 1;
3778 /* 4.1 client asking for a delegation? */
3779 if (open->op_deleg_want)
3780 nfsd4_open_deleg_none_ext(open, status);
3784 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3785 struct nfs4_delegation *dp)
3787 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3788 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3789 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3790 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3791 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3792 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3793 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3794 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3796 /* Otherwise the client must be confused wanting a delegation
3797 * it already has, therefore we don't return
3798 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3803 * called with nfs4_lock_state() held.
3806 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3808 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3809 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3810 struct nfs4_file *fp = NULL;
3811 struct nfs4_ol_stateid *stp = NULL;
3812 struct nfs4_delegation *dp = NULL;
3816 * Lookup file; if found, lookup stateid and check open request,
3817 * and check for delegations in the process of being recalled.
3818 * If not found, create the nfs4_file struct
3820 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3821 if (fp != open->op_file) {
3822 status = nfs4_check_deleg(cl, open, &dp);
3825 stp = nfsd4_find_existing_open(fp, open);
3827 open->op_file = NULL;
3828 status = nfserr_bad_stateid;
3829 if (nfsd4_is_deleg_cur(open))
3831 status = nfserr_jukebox;
3835 * OPEN the file, or upgrade an existing OPEN.
3836 * If truncate fails, the OPEN fails.
3839 /* Stateid was found, this is an OPEN upgrade */
3840 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3845 open->op_stp = NULL;
3846 init_open_stateid(stp, fp, open);
3847 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3849 release_open_stateid(stp);
3853 update_stateid(&stp->st_stid.sc_stateid);
3854 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3856 if (nfsd4_has_session(&resp->cstate)) {
3857 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3858 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3859 open->op_why_no_deleg = WND4_NOT_WANTED;
3865 * Attempt to hand out a delegation. No error return, because the
3866 * OPEN succeeds even if we fail.
3868 nfs4_open_delegation(current_fh, open, stp);
3872 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3873 STATEID_VAL(&stp->st_stid.sc_stateid));
3875 /* 4.1 client trying to upgrade/downgrade delegation? */
3876 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3877 open->op_deleg_want)
3878 nfsd4_deleg_xgrade_none_ext(open, dp);
3882 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3883 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3885 * To finish the open response, we just need to set the rflags.
3887 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3888 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3889 !nfsd4_has_session(&resp->cstate))
3890 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3892 nfs4_put_stid(&dp->dl_stid);
3894 nfs4_put_stid(&stp->st_stid);
3899 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3900 struct nfsd4_open *open, __be32 status)
3902 if (open->op_openowner) {
3903 struct nfs4_openowner *oo = open->op_openowner;
3905 if (!list_empty(&oo->oo_owner.so_stateids))
3906 list_del_init(&oo->oo_close_lru);
3907 if (oo->oo_flags & NFS4_OO_NEW) {
3909 release_openowner(oo);
3910 open->op_openowner = NULL;
3912 oo->oo_flags &= ~NFS4_OO_NEW;
3914 if (open->op_openowner)
3915 nfsd4_cstate_assign_replay(cstate, &oo->oo_owner);
3918 nfsd4_free_file(open->op_file);
3920 nfs4_put_stid(&open->op_stp->st_stid);
3924 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3927 struct nfs4_client *clp;
3929 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3932 dprintk("process_renew(%08x/%08x): starting\n",
3933 clid->cl_boot, clid->cl_id);
3934 status = lookup_clientid(clid, cstate, nn);
3938 status = nfserr_cb_path_down;
3939 if (!list_empty(&clp->cl_delegations)
3940 && clp->cl_cb_state != NFSD4_CB_UP)
3944 nfs4_unlock_state();
3949 nfsd4_end_grace(struct nfsd_net *nn)
3951 /* do nothing if grace period already ended */
3952 if (nn->grace_ended)
3955 dprintk("NFSD: end of grace period\n");
3956 nn->grace_ended = true;
3957 nfsd4_record_grace_done(nn, nn->boot_time);
3958 locks_end_grace(&nn->nfsd4_manager);
3960 * Now that every NFSv4 client has had the chance to recover and
3961 * to see the (possibly new, possibly shorter) lease time, we
3962 * can safely set the next grace time to the current lease time:
3964 nn->nfsd4_grace = nn->nfsd4_lease;
3968 nfs4_laundromat(struct nfsd_net *nn)
3970 struct nfs4_client *clp;
3971 struct nfs4_openowner *oo;
3972 struct nfs4_delegation *dp;
3973 struct list_head *pos, *next, reaplist;
3974 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3975 time_t t, new_timeo = nn->nfsd4_lease;
3979 dprintk("NFSD: laundromat service - starting\n");
3980 nfsd4_end_grace(nn);
3981 INIT_LIST_HEAD(&reaplist);
3982 spin_lock(&nn->client_lock);
3983 list_for_each_safe(pos, next, &nn->client_lru) {
3984 clp = list_entry(pos, struct nfs4_client, cl_lru);
3985 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3986 t = clp->cl_time - cutoff;
3987 new_timeo = min(new_timeo, t);
3990 if (mark_client_expired_locked(clp)) {
3991 dprintk("NFSD: client in use (clientid %08x)\n",
3992 clp->cl_clientid.cl_id);
3995 list_move(&clp->cl_lru, &reaplist);
3997 spin_unlock(&nn->client_lock);
3998 list_for_each_safe(pos, next, &reaplist) {
3999 clp = list_entry(pos, struct nfs4_client, cl_lru);
4000 dprintk("NFSD: purging unused client (clientid %08x)\n",
4001 clp->cl_clientid.cl_id);
4004 spin_lock(&state_lock);
4005 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4006 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4007 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4009 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4010 t = dp->dl_time - cutoff;
4011 new_timeo = min(new_timeo, t);
4014 unhash_delegation_locked(dp);
4015 list_add(&dp->dl_recall_lru, &reaplist);
4017 spin_unlock(&state_lock);
4018 while (!list_empty(&reaplist)) {
4019 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4021 list_del_init(&dp->dl_recall_lru);
4022 revoke_delegation(dp);
4024 list_for_each_safe(pos, next, &nn->close_lru) {
4025 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4026 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4027 t = oo->oo_time - cutoff;
4028 new_timeo = min(new_timeo, t);
4031 release_openowner(oo);
4033 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4034 nfs4_unlock_state();
4038 static struct workqueue_struct *laundry_wq;
4039 static void laundromat_main(struct work_struct *);
4042 laundromat_main(struct work_struct *laundry)
4045 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4047 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4050 t = nfs4_laundromat(nn);
4051 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4052 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4055 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4057 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4058 return nfserr_bad_stateid;
4063 access_permit_read(struct nfs4_ol_stateid *stp)
4065 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4066 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4067 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4071 access_permit_write(struct nfs4_ol_stateid *stp)
4073 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4074 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4078 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4080 __be32 status = nfserr_openmode;
4082 /* For lock stateid's, we test the parent open, not the lock: */
4083 if (stp->st_openstp)
4084 stp = stp->st_openstp;
4085 if ((flags & WR_STATE) && !access_permit_write(stp))
4087 if ((flags & RD_STATE) && !access_permit_read(stp))
4094 static inline __be32
4095 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4097 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4099 else if (locks_in_grace(net)) {
4100 /* Answer in remaining cases depends on existence of
4101 * conflicting state; so we must wait out the grace period. */
4102 return nfserr_grace;
4103 } else if (flags & WR_STATE)
4104 return nfs4_share_conflict(current_fh,
4105 NFS4_SHARE_DENY_WRITE);
4106 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4107 return nfs4_share_conflict(current_fh,
4108 NFS4_SHARE_DENY_READ);
4112 * Allow READ/WRITE during grace period on recovered state only for files
4113 * that are not able to provide mandatory locking.
4116 grace_disallows_io(struct net *net, struct inode *inode)
4118 return locks_in_grace(net) && mandatory_lock(inode);
4121 /* Returns true iff a is later than b: */
4122 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4124 return (s32)(a->si_generation - b->si_generation) > 0;
4127 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4130 * When sessions are used the stateid generation number is ignored
4133 if (has_session && in->si_generation == 0)
4136 if (in->si_generation == ref->si_generation)
4139 /* If the client sends us a stateid from the future, it's buggy: */
4140 if (stateid_generation_after(in, ref))
4141 return nfserr_bad_stateid;
4143 * However, we could see a stateid from the past, even from a
4144 * non-buggy client. For example, if the client sends a lock
4145 * while some IO is outstanding, the lock may bump si_generation
4146 * while the IO is still in flight. The client could avoid that
4147 * situation by waiting for responses on all the IO requests,
4148 * but better performance may result in retrying IO that
4149 * receives an old_stateid error if requests are rarely
4150 * reordered in flight:
4152 return nfserr_old_stateid;
4155 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4157 struct nfs4_stid *s;
4158 struct nfs4_ol_stateid *ols;
4159 __be32 status = nfserr_bad_stateid;
4161 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4163 /* Client debugging aid. */
4164 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4165 char addr_str[INET6_ADDRSTRLEN];
4166 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4168 pr_warn_ratelimited("NFSD: client %s testing state ID "
4169 "with incorrect client ID\n", addr_str);
4172 spin_lock(&cl->cl_lock);
4173 s = find_stateid_locked(cl, stateid);
4176 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4179 switch (s->sc_type) {
4180 case NFS4_DELEG_STID:
4183 case NFS4_REVOKED_DELEG_STID:
4184 status = nfserr_deleg_revoked;
4186 case NFS4_OPEN_STID:
4187 case NFS4_LOCK_STID:
4188 ols = openlockstateid(s);
4189 if (ols->st_stateowner->so_is_open_owner
4190 && !(openowner(ols->st_stateowner)->oo_flags
4191 & NFS4_OO_CONFIRMED))
4192 status = nfserr_bad_stateid;
4197 printk("unknown stateid type %x\n", s->sc_type);
4199 case NFS4_CLOSED_STID:
4200 case NFS4_CLOSED_DELEG_STID:
4201 status = nfserr_bad_stateid;
4204 spin_unlock(&cl->cl_lock);
4209 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4210 stateid_t *stateid, unsigned char typemask,
4211 struct nfs4_stid **s, struct nfsd_net *nn)
4215 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4216 return nfserr_bad_stateid;
4217 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4218 if (status == nfserr_stale_clientid) {
4219 if (cstate->session)
4220 return nfserr_bad_stateid;
4221 return nfserr_stale_stateid;
4225 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4227 return nfserr_bad_stateid;
4232 * Checks for stateid operations
4235 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4236 stateid_t *stateid, int flags, struct file **filpp)
4238 struct nfs4_stid *s;
4239 struct nfs4_ol_stateid *stp = NULL;
4240 struct nfs4_delegation *dp = NULL;
4241 struct svc_fh *current_fh = &cstate->current_fh;
4242 struct inode *ino = current_fh->fh_dentry->d_inode;
4243 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4244 struct file *file = NULL;
4250 if (grace_disallows_io(net, ino))
4251 return nfserr_grace;
4253 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4254 return check_special_stateids(net, current_fh, stateid, flags);
4258 status = nfsd4_lookup_stateid(cstate, stateid,
4259 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4263 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4266 switch (s->sc_type) {
4267 case NFS4_DELEG_STID:
4268 dp = delegstateid(s);
4269 status = nfs4_check_delegmode(dp, flags);
4273 file = dp->dl_stid.sc_file->fi_deleg_file;
4276 status = nfserr_serverfault;
4282 case NFS4_OPEN_STID:
4283 case NFS4_LOCK_STID:
4284 stp = openlockstateid(s);
4285 status = nfs4_check_fh(current_fh, stp);
4288 if (stp->st_stateowner->so_is_open_owner
4289 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4291 status = nfs4_check_openmode(stp, flags);
4295 struct nfs4_file *fp = stp->st_stid.sc_file;
4297 if (flags & RD_STATE)
4298 file = find_readable_file(fp);
4300 file = find_writeable_file(fp);
4304 status = nfserr_bad_stateid;
4313 nfs4_unlock_state();
4318 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4320 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4322 if (check_for_locks(stp->st_stid.sc_file, lo))
4323 return nfserr_locks_held;
4324 release_lockowner_if_empty(lo);
4329 * Test if the stateid is valid
4332 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4333 struct nfsd4_test_stateid *test_stateid)
4335 struct nfsd4_test_stateid_id *stateid;
4336 struct nfs4_client *cl = cstate->session->se_client;
4339 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4340 stateid->ts_id_status =
4341 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4342 nfs4_unlock_state();
4348 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4349 struct nfsd4_free_stateid *free_stateid)
4351 stateid_t *stateid = &free_stateid->fr_stateid;
4352 struct nfs4_stid *s;
4353 struct nfs4_delegation *dp;
4354 struct nfs4_client *cl = cstate->session->se_client;
4355 __be32 ret = nfserr_bad_stateid;
4358 spin_lock(&cl->cl_lock);
4359 s = find_stateid_locked(cl, stateid);
4362 switch (s->sc_type) {
4363 case NFS4_DELEG_STID:
4364 ret = nfserr_locks_held;
4366 case NFS4_OPEN_STID:
4367 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4370 ret = nfserr_locks_held;
4372 case NFS4_LOCK_STID:
4373 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4376 spin_unlock(&cl->cl_lock);
4377 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4379 case NFS4_REVOKED_DELEG_STID:
4380 dp = delegstateid(s);
4381 list_del_init(&dp->dl_recall_lru);
4382 spin_unlock(&cl->cl_lock);
4386 /* Default falls through and returns nfserr_bad_stateid */
4389 spin_unlock(&cl->cl_lock);
4391 nfs4_unlock_state();
4398 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4399 RD_STATE : WR_STATE;
4402 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4404 struct svc_fh *current_fh = &cstate->current_fh;
4405 struct nfs4_stateowner *sop = stp->st_stateowner;
4408 status = nfsd4_check_seqid(cstate, sop, seqid);
4411 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4412 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4414 * "Closed" stateid's exist *only* to return
4415 * nfserr_replay_me from the previous step, and
4416 * revoked delegations are kept only for free_stateid.
4418 return nfserr_bad_stateid;
4419 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4422 return nfs4_check_fh(current_fh, stp);
4426 * Checks for sequence id mutating operations.
4429 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4430 stateid_t *stateid, char typemask,
4431 struct nfs4_ol_stateid **stpp,
4432 struct nfsd_net *nn)
4435 struct nfs4_stid *s;
4436 struct nfs4_ol_stateid *stp = NULL;
4438 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4439 seqid, STATEID_VAL(stateid));
4442 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4445 stp = openlockstateid(s);
4446 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4448 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4452 nfs4_put_stid(&stp->st_stid);
4456 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4457 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4460 struct nfs4_openowner *oo;
4461 struct nfs4_ol_stateid *stp;
4463 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4464 NFS4_OPEN_STID, &stp, nn);
4467 oo = openowner(stp->st_stateowner);
4468 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4469 nfs4_put_stid(&stp->st_stid);
4470 return nfserr_bad_stateid;
4477 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4478 struct nfsd4_open_confirm *oc)
4481 struct nfs4_openowner *oo;
4482 struct nfs4_ol_stateid *stp;
4483 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4485 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4486 cstate->current_fh.fh_dentry);
4488 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4494 status = nfs4_preprocess_seqid_op(cstate,
4495 oc->oc_seqid, &oc->oc_req_stateid,
4496 NFS4_OPEN_STID, &stp, nn);
4499 oo = openowner(stp->st_stateowner);
4500 status = nfserr_bad_stateid;
4501 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4503 oo->oo_flags |= NFS4_OO_CONFIRMED;
4504 update_stateid(&stp->st_stid.sc_stateid);
4505 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4506 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4507 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4509 nfsd4_client_record_create(oo->oo_owner.so_client);
4512 nfs4_put_stid(&stp->st_stid);
4514 nfsd4_bump_seqid(cstate, status);
4515 nfs4_unlock_state();
4519 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4521 if (!test_access(access, stp))
4523 nfs4_file_put_access(stp->st_stid.sc_file, access);
4524 clear_access(access, stp);
4527 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4529 switch (to_access) {
4530 case NFS4_SHARE_ACCESS_READ:
4531 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4532 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4534 case NFS4_SHARE_ACCESS_WRITE:
4535 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4536 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4538 case NFS4_SHARE_ACCESS_BOTH:
4546 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4547 struct nfsd4_compound_state *cstate,
4548 struct nfsd4_open_downgrade *od)
4551 struct nfs4_ol_stateid *stp;
4552 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4554 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4555 cstate->current_fh.fh_dentry);
4557 /* We don't yet support WANT bits: */
4558 if (od->od_deleg_want)
4559 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4563 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4564 &od->od_stateid, &stp, nn);
4567 status = nfserr_inval;
4568 if (!test_access(od->od_share_access, stp)) {
4569 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4570 stp->st_access_bmap, od->od_share_access);
4573 if (!test_deny(od->od_share_deny, stp)) {
4574 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4575 stp->st_deny_bmap, od->od_share_deny);
4578 nfs4_stateid_downgrade(stp, od->od_share_access);
4580 reset_union_bmap_deny(od->od_share_deny, stp);
4582 update_stateid(&stp->st_stid.sc_stateid);
4583 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4586 nfs4_put_stid(&stp->st_stid);
4588 nfsd4_bump_seqid(cstate, status);
4589 nfs4_unlock_state();
4593 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4595 struct nfs4_client *clp = s->st_stid.sc_client;
4596 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4598 s->st_stid.sc_type = NFS4_CLOSED_STID;
4599 unhash_open_stateid(s);
4601 if (clp->cl_minorversion) {
4602 if (list_empty(&oo->oo_owner.so_stateids))
4603 release_openowner(oo);
4604 nfs4_put_stid(&s->st_stid);
4607 * In the 4.0 case we need to keep the owners around a
4608 * little while to handle CLOSE replay. We still do need
4609 * to release any file access that is held by them
4610 * before returning however.
4612 release_all_access(s);
4613 if (s->st_stid.sc_file) {
4614 put_nfs4_file(s->st_stid.sc_file);
4615 s->st_stid.sc_file = NULL;
4617 oo->oo_last_closed_stid = s;
4618 if (list_empty(&oo->oo_owner.so_stateids))
4619 move_to_close_lru(oo, clp->net);
4624 * nfs4_unlock_state() called after encode
4627 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4628 struct nfsd4_close *close)
4631 struct nfs4_ol_stateid *stp;
4632 struct net *net = SVC_NET(rqstp);
4633 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4635 dprintk("NFSD: nfsd4_close on file %pd\n",
4636 cstate->current_fh.fh_dentry);
4639 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4641 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4643 nfsd4_bump_seqid(cstate, status);
4646 update_stateid(&stp->st_stid.sc_stateid);
4647 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4649 nfsd4_close_open_stateid(stp);
4651 /* put reference from nfs4_preprocess_seqid_op */
4652 nfs4_put_stid(&stp->st_stid);
4654 nfs4_unlock_state();
4659 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4660 struct nfsd4_delegreturn *dr)
4662 struct nfs4_delegation *dp;
4663 stateid_t *stateid = &dr->dr_stateid;
4664 struct nfs4_stid *s;
4666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4668 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4672 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4675 dp = delegstateid(s);
4676 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4680 destroy_delegation(dp);
4682 nfs4_put_stid(&dp->dl_stid);
4684 nfs4_unlock_state();
4690 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4693 end_offset(u64 start, u64 len)
4698 return end >= start ? end: NFS4_MAX_UINT64;
4701 /* last octet in a range */
4703 last_byte_offset(u64 start, u64 len)
4709 return end > start ? end - 1: NFS4_MAX_UINT64;
4713 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4714 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4715 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4716 * locking, this prevents us from being completely protocol-compliant. The
4717 * real solution to this problem is to start using unsigned file offsets in
4718 * the VFS, but this is a very deep change!
4721 nfs4_transform_lock_offset(struct file_lock *lock)
4723 if (lock->fl_start < 0)
4724 lock->fl_start = OFFSET_MAX;
4725 if (lock->fl_end < 0)
4726 lock->fl_end = OFFSET_MAX;
4729 /* Hack!: For now, we're defining this just so we can use a pointer to it
4730 * as a unique cookie to identify our (NFSv4's) posix locks. */
4731 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4735 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4737 struct nfs4_lockowner *lo;
4739 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4740 lo = (struct nfs4_lockowner *) fl->fl_owner;
4741 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4742 lo->lo_owner.so_owner.len, GFP_KERNEL);
4743 if (!deny->ld_owner.data)
4744 /* We just don't care that much */
4746 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4747 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4750 deny->ld_owner.len = 0;
4751 deny->ld_owner.data = NULL;
4752 deny->ld_clientid.cl_boot = 0;
4753 deny->ld_clientid.cl_id = 0;
4755 deny->ld_start = fl->fl_start;
4756 deny->ld_length = NFS4_MAX_UINT64;
4757 if (fl->fl_end != NFS4_MAX_UINT64)
4758 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4759 deny->ld_type = NFS4_READ_LT;
4760 if (fl->fl_type != F_RDLCK)
4761 deny->ld_type = NFS4_WRITE_LT;
4764 static struct nfs4_lockowner *
4765 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4766 struct nfsd_net *nn)
4768 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4769 struct nfs4_stateowner *so;
4771 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4772 if (so->so_is_open_owner)
4774 if (!same_owner_str(so, owner, clid))
4776 atomic_inc(&so->so_count);
4777 return lockowner(so);
4782 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4784 unhash_lockowner(lockowner(sop));
4787 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4789 struct nfs4_lockowner *lo = lockowner(sop);
4791 kmem_cache_free(lockowner_slab, lo);
4794 static const struct nfs4_stateowner_operations lockowner_ops = {
4795 .so_unhash = nfs4_unhash_lockowner,
4796 .so_free = nfs4_free_lockowner,
4800 * Alloc a lock owner structure.
4801 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4804 * strhashval = ownerstr_hashval
4806 static struct nfs4_lockowner *
4807 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4808 struct nfs4_lockowner *lo;
4809 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4811 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4814 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4815 lo->lo_owner.so_is_open_owner = 0;
4816 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4817 lo->lo_owner.so_ops = &lockowner_ops;
4818 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4823 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4824 struct nfs4_file *fp, struct inode *inode,
4825 struct nfs4_ol_stateid *open_stp)
4827 struct nfs4_client *clp = lo->lo_owner.so_client;
4829 lockdep_assert_held(&clp->cl_lock);
4831 atomic_inc(&stp->st_stid.sc_count);
4832 stp->st_stid.sc_type = NFS4_LOCK_STID;
4833 stp->st_stateowner = &lo->lo_owner;
4835 stp->st_stid.sc_file = fp;
4836 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4837 stp->st_access_bmap = 0;
4838 stp->st_deny_bmap = open_stp->st_deny_bmap;
4839 stp->st_openstp = open_stp;
4840 list_add(&stp->st_locks, &open_stp->st_locks);
4841 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4842 spin_lock(&fp->fi_lock);
4843 list_add(&stp->st_perfile, &fp->fi_stateids);
4844 spin_unlock(&fp->fi_lock);
4847 static struct nfs4_ol_stateid *
4848 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4850 struct nfs4_ol_stateid *lst;
4851 struct nfs4_client *clp = lo->lo_owner.so_client;
4853 lockdep_assert_held(&clp->cl_lock);
4855 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4856 if (lst->st_stid.sc_file == fp) {
4857 atomic_inc(&lst->st_stid.sc_count);
4864 static struct nfs4_ol_stateid *
4865 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4866 struct inode *inode, struct nfs4_ol_stateid *ost,
4869 struct nfs4_stid *ns = NULL;
4870 struct nfs4_ol_stateid *lst;
4871 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4872 struct nfs4_client *clp = oo->oo_owner.so_client;
4874 spin_lock(&clp->cl_lock);
4875 lst = find_lock_stateid(lo, fi);
4877 spin_unlock(&clp->cl_lock);
4878 ns = nfs4_alloc_stid(clp, stateid_slab);
4882 spin_lock(&clp->cl_lock);
4883 lst = find_lock_stateid(lo, fi);
4885 lst = openlockstateid(ns);
4886 init_lock_stateid(lst, lo, fi, inode, ost);
4891 spin_unlock(&clp->cl_lock);
4898 check_lock_length(u64 offset, u64 length)
4900 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4901 LOFF_OVERFLOW(offset, length)));
4904 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4906 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4908 lockdep_assert_held(&fp->fi_lock);
4910 if (test_access(access, lock_stp))
4912 __nfs4_file_get_access(fp, access);
4913 set_access(access, lock_stp);
4917 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4918 struct nfs4_ol_stateid *ost,
4919 struct nfsd4_lock *lock,
4920 struct nfs4_ol_stateid **lst, bool *new)
4923 struct nfs4_file *fi = ost->st_stid.sc_file;
4924 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4925 struct nfs4_client *cl = oo->oo_owner.so_client;
4926 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4927 struct nfs4_lockowner *lo;
4928 unsigned int strhashval;
4929 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4931 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4933 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4934 &lock->v.new.owner);
4935 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4937 return nfserr_jukebox;
4938 /* FIXME: extra reference for new lockowners for the client */
4939 atomic_inc(&lo->lo_owner.so_count);
4941 /* with an existing lockowner, seqids must be the same */
4942 status = nfserr_bad_seqid;
4943 if (!cstate->minorversion &&
4944 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4948 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
4950 release_lockowner_if_empty(lo);
4951 status = nfserr_jukebox;
4956 nfs4_put_stateowner(&lo->lo_owner);
4964 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4965 struct nfsd4_lock *lock)
4967 struct nfs4_openowner *open_sop = NULL;
4968 struct nfs4_lockowner *lock_sop = NULL;
4969 struct nfs4_ol_stateid *lock_stp = NULL;
4970 struct nfs4_ol_stateid *open_stp = NULL;
4971 struct nfs4_file *fp;
4972 struct file *filp = NULL;
4973 struct file_lock *file_lock = NULL;
4974 struct file_lock *conflock = NULL;
4979 struct net *net = SVC_NET(rqstp);
4980 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4982 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4983 (long long) lock->lk_offset,
4984 (long long) lock->lk_length);
4986 if (check_lock_length(lock->lk_offset, lock->lk_length))
4987 return nfserr_inval;
4989 if ((status = fh_verify(rqstp, &cstate->current_fh,
4990 S_IFREG, NFSD_MAY_LOCK))) {
4991 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4997 if (lock->lk_is_new) {
4998 if (nfsd4_has_session(cstate))
4999 /* See rfc 5661 18.10.3: given clientid is ignored: */
5000 memcpy(&lock->v.new.clientid,
5001 &cstate->session->se_client->cl_clientid,
5002 sizeof(clientid_t));
5004 status = nfserr_stale_clientid;
5005 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5008 /* validate and update open stateid and open seqid */
5009 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5010 lock->lk_new_open_seqid,
5011 &lock->lk_new_open_stateid,
5015 open_sop = openowner(open_stp->st_stateowner);
5016 status = nfserr_bad_stateid;
5017 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5018 &lock->v.new.clientid))
5020 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5023 status = nfs4_preprocess_seqid_op(cstate,
5024 lock->lk_old_lock_seqid,
5025 &lock->lk_old_lock_stateid,
5026 NFS4_LOCK_STID, &lock_stp, nn);
5030 lock_sop = lockowner(lock_stp->st_stateowner);
5032 lkflg = setlkflg(lock->lk_type);
5033 status = nfs4_check_openmode(lock_stp, lkflg);
5037 status = nfserr_grace;
5038 if (locks_in_grace(net) && !lock->lk_reclaim)
5040 status = nfserr_no_grace;
5041 if (!locks_in_grace(net) && lock->lk_reclaim)
5044 file_lock = locks_alloc_lock();
5046 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5047 status = nfserr_jukebox;
5051 fp = lock_stp->st_stid.sc_file;
5052 locks_init_lock(file_lock);
5053 switch (lock->lk_type) {
5056 spin_lock(&fp->fi_lock);
5057 filp = find_readable_file_locked(fp);
5059 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5060 spin_unlock(&fp->fi_lock);
5061 file_lock->fl_type = F_RDLCK;
5064 case NFS4_WRITEW_LT:
5065 spin_lock(&fp->fi_lock);
5066 filp = find_writeable_file_locked(fp);
5068 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5069 spin_unlock(&fp->fi_lock);
5070 file_lock->fl_type = F_WRLCK;
5073 status = nfserr_inval;
5077 status = nfserr_openmode;
5080 file_lock->fl_owner = (fl_owner_t)lock_sop;
5081 file_lock->fl_pid = current->tgid;
5082 file_lock->fl_file = filp;
5083 file_lock->fl_flags = FL_POSIX;
5084 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5085 file_lock->fl_start = lock->lk_offset;
5086 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5087 nfs4_transform_lock_offset(file_lock);
5089 conflock = locks_alloc_lock();
5091 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5092 status = nfserr_jukebox;
5096 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5098 case 0: /* success! */
5099 update_stateid(&lock_stp->st_stid.sc_stateid);
5100 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5104 case (EAGAIN): /* conflock holds conflicting lock */
5105 status = nfserr_denied;
5106 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5107 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5110 status = nfserr_deadlock;
5113 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5114 status = nfserrno(err);
5121 /* Bump seqid manually if the 4.0 replay owner is openowner */
5122 if (cstate->replay_owner &&
5123 cstate->replay_owner != &lock_sop->lo_owner &&
5124 seqid_mutating_err(ntohl(status)))
5125 lock_sop->lo_owner.so_seqid++;
5128 * If this is a new, never-before-used stateid, and we are
5129 * returning an error, then just go ahead and release it.
5132 release_lock_stateid(lock_stp);
5134 nfs4_put_stid(&lock_stp->st_stid);
5137 nfs4_put_stid(&open_stp->st_stid);
5138 nfsd4_bump_seqid(cstate, status);
5139 nfs4_unlock_state();
5141 locks_free_lock(file_lock);
5143 locks_free_lock(conflock);
5148 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5149 * so we do a temporary open here just to get an open file to pass to
5150 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5153 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5156 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5158 err = nfserrno(vfs_test_lock(file, lock));
5168 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5169 struct nfsd4_lockt *lockt)
5171 struct file_lock *file_lock = NULL;
5172 struct nfs4_lockowner *lo = NULL;
5174 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5176 if (locks_in_grace(SVC_NET(rqstp)))
5177 return nfserr_grace;
5179 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5180 return nfserr_inval;
5184 if (!nfsd4_has_session(cstate)) {
5185 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5190 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5193 file_lock = locks_alloc_lock();
5195 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5196 status = nfserr_jukebox;
5199 locks_init_lock(file_lock);
5200 switch (lockt->lt_type) {
5203 file_lock->fl_type = F_RDLCK;
5206 case NFS4_WRITEW_LT:
5207 file_lock->fl_type = F_WRLCK;
5210 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5211 status = nfserr_inval;
5215 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
5217 file_lock->fl_owner = (fl_owner_t)lo;
5218 file_lock->fl_pid = current->tgid;
5219 file_lock->fl_flags = FL_POSIX;
5221 file_lock->fl_start = lockt->lt_offset;
5222 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5224 nfs4_transform_lock_offset(file_lock);
5226 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5230 if (file_lock->fl_type != F_UNLCK) {
5231 status = nfserr_denied;
5232 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5236 nfs4_put_stateowner(&lo->lo_owner);
5237 nfs4_unlock_state();
5239 locks_free_lock(file_lock);
5244 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5245 struct nfsd4_locku *locku)
5247 struct nfs4_ol_stateid *stp;
5248 struct file *filp = NULL;
5249 struct file_lock *file_lock = NULL;
5252 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5254 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5255 (long long) locku->lu_offset,
5256 (long long) locku->lu_length);
5258 if (check_lock_length(locku->lu_offset, locku->lu_length))
5259 return nfserr_inval;
5263 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5264 &locku->lu_stateid, NFS4_LOCK_STID,
5268 filp = find_any_file(stp->st_stid.sc_file);
5270 status = nfserr_lock_range;
5273 file_lock = locks_alloc_lock();
5275 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5276 status = nfserr_jukebox;
5279 locks_init_lock(file_lock);
5280 file_lock->fl_type = F_UNLCK;
5281 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5282 file_lock->fl_pid = current->tgid;
5283 file_lock->fl_file = filp;
5284 file_lock->fl_flags = FL_POSIX;
5285 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5286 file_lock->fl_start = locku->lu_offset;
5288 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5290 nfs4_transform_lock_offset(file_lock);
5292 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5294 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5297 update_stateid(&stp->st_stid.sc_stateid);
5298 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5302 nfs4_put_stid(&stp->st_stid);
5304 nfsd4_bump_seqid(cstate, status);
5305 nfs4_unlock_state();
5307 locks_free_lock(file_lock);
5311 status = nfserrno(err);
5317 * true: locks held by lockowner
5318 * false: no locks held by lockowner
5321 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5323 struct file_lock **flpp;
5325 struct file *filp = find_any_file(fp);
5326 struct inode *inode;
5329 /* Any valid lock stateid should have some sort of access */
5334 inode = file_inode(filp);
5336 spin_lock(&inode->i_lock);
5337 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5338 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5343 spin_unlock(&inode->i_lock);
5349 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5350 struct nfsd4_compound_state *cstate,
5351 struct nfsd4_release_lockowner *rlockowner)
5353 clientid_t *clid = &rlockowner->rl_clientid;
5354 struct nfs4_stateowner *sop = NULL, *tmp;
5355 struct nfs4_lockowner *lo;
5356 struct nfs4_ol_stateid *stp;
5357 struct xdr_netobj *owner = &rlockowner->rl_owner;
5358 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5360 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5362 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5363 clid->cl_boot, clid->cl_id);
5367 status = lookup_clientid(clid, cstate, nn);
5371 status = nfserr_locks_held;
5373 /* Find the matching lock stateowner */
5374 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5375 if (tmp->so_is_open_owner)
5377 if (same_owner_str(tmp, owner, clid)) {
5383 /* No matching owner found, maybe a replay? Just declare victory... */
5389 lo = lockowner(sop);
5390 /* see if there are still any locks associated with it */
5391 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5392 if (check_for_locks(stp->st_stid.sc_file, lo))
5397 release_lockowner(lo);
5399 nfs4_unlock_state();
5403 static inline struct nfs4_client_reclaim *
5406 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5410 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5412 struct nfs4_client_reclaim *crp;
5414 crp = nfsd4_find_reclaim_client(name, nn);
5415 return (crp && crp->cr_clp);
5419 * failure => all reset bets are off, nfserr_no_grace...
5421 struct nfs4_client_reclaim *
5422 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5424 unsigned int strhashval;
5425 struct nfs4_client_reclaim *crp;
5427 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5428 crp = alloc_reclaim();
5430 strhashval = clientstr_hashval(name);
5431 INIT_LIST_HEAD(&crp->cr_strhash);
5432 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5433 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5435 nn->reclaim_str_hashtbl_size++;
5441 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5443 list_del(&crp->cr_strhash);
5445 nn->reclaim_str_hashtbl_size--;
5449 nfs4_release_reclaim(struct nfsd_net *nn)
5451 struct nfs4_client_reclaim *crp = NULL;
5454 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5455 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5456 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5457 struct nfs4_client_reclaim, cr_strhash);
5458 nfs4_remove_reclaim_record(crp, nn);
5461 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5465 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5466 struct nfs4_client_reclaim *
5467 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5469 unsigned int strhashval;
5470 struct nfs4_client_reclaim *crp = NULL;
5472 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5474 strhashval = clientstr_hashval(recdir);
5475 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5476 if (same_name(crp->cr_recdir, recdir)) {
5484 * Called from OPEN. Look for clientid in reclaim list.
5487 nfs4_check_open_reclaim(clientid_t *clid,
5488 struct nfsd4_compound_state *cstate,
5489 struct nfsd_net *nn)
5493 /* find clientid in conf_id_hashtbl */
5494 status = lookup_clientid(clid, cstate, nn);
5496 return nfserr_reclaim_bad;
5498 if (nfsd4_client_record_check(cstate->clp))
5499 return nfserr_reclaim_bad;
5504 #ifdef CONFIG_NFSD_FAULT_INJECTION
5506 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5508 if (mark_client_expired(clp))
5514 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5516 char buf[INET6_ADDRSTRLEN];
5517 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5518 printk(KERN_INFO "NFS Client: %s\n", buf);
5522 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5525 char buf[INET6_ADDRSTRLEN];
5526 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5527 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5530 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5531 void (*func)(struct nfs4_ol_stateid *))
5533 struct nfs4_openowner *oop;
5534 struct nfs4_ol_stateid *stp, *st_next;
5535 struct nfs4_ol_stateid *lst, *lst_next;
5538 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5539 list_for_each_entry_safe(stp, st_next,
5540 &oop->oo_owner.so_stateids, st_perstateowner) {
5541 list_for_each_entry_safe(lst, lst_next,
5542 &stp->st_locks, st_locks) {
5554 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5556 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5559 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5561 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5562 nfsd_print_count(clp, count, "locked files");
5566 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5568 struct nfs4_openowner *oop, *next;
5571 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5581 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5583 return nfsd_foreach_client_open(clp, max, release_openowner);
5586 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5588 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5589 nfsd_print_count(clp, count, "open files");
5593 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5594 struct list_head *victims)
5596 struct nfs4_delegation *dp, *next;
5599 lockdep_assert_held(&state_lock);
5600 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5603 * It's not safe to mess with delegations that have a
5604 * non-zero dl_time. They might have already been broken
5605 * and could be processed by the laundromat outside of
5606 * the state_lock. Just leave them be.
5608 if (dp->dl_time != 0)
5611 unhash_delegation_locked(dp);
5612 list_add(&dp->dl_recall_lru, victims);
5620 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5622 struct nfs4_delegation *dp, *next;
5626 spin_lock(&state_lock);
5627 count = nfsd_find_all_delegations(clp, max, &victims);
5628 spin_unlock(&state_lock);
5630 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5631 list_del_init(&dp->dl_recall_lru);
5632 revoke_delegation(dp);
5638 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5640 struct nfs4_delegation *dp;
5644 spin_lock(&state_lock);
5645 count = nfsd_find_all_delegations(clp, max, &victims);
5646 while (!list_empty(&victims)) {
5647 dp = list_first_entry(&victims, struct nfs4_delegation,
5649 list_del_init(&dp->dl_recall_lru);
5651 nfsd_break_one_deleg(dp);
5653 spin_unlock(&state_lock);
5658 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5662 spin_lock(&state_lock);
5663 count = nfsd_find_all_delegations(clp, max, NULL);
5664 spin_unlock(&state_lock);
5666 nfsd_print_count(clp, count, "delegations");
5670 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5672 struct nfs4_client *clp, *next;
5674 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5676 if (!nfsd_netns_ready(nn))
5679 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5680 count += func(clp, max - count);
5681 if ((max != 0) && (count >= max))
5688 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5690 struct nfs4_client *clp;
5691 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5693 if (!nfsd_netns_ready(nn))
5696 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5697 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5703 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5706 * Since the lifetime of a delegation isn't limited to that of an open, a
5707 * client may quite reasonably hang on to a delegation as long as it has
5708 * the inode cached. This becomes an obvious problem the first time a
5709 * client's inode cache approaches the size of the server's total memory.
5711 * For now we avoid this problem by imposing a hard limit on the number
5712 * of delegations, which varies according to the server's memory size.
5715 set_max_delegations(void)
5718 * Allow at most 4 delegations per megabyte of RAM. Quick
5719 * estimates suggest that in the worst case (where every delegation
5720 * is for a different inode), a delegation could take about 1.5K,
5721 * giving a worst case usage of about 6% of memory.
5723 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5726 static int nfs4_state_create_net(struct net *net)
5728 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5731 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5732 CLIENT_HASH_SIZE, GFP_KERNEL);
5733 if (!nn->conf_id_hashtbl)
5735 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5736 CLIENT_HASH_SIZE, GFP_KERNEL);
5737 if (!nn->unconf_id_hashtbl)
5739 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5740 OWNER_HASH_SIZE, GFP_KERNEL);
5741 if (!nn->ownerstr_hashtbl)
5743 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5744 SESSION_HASH_SIZE, GFP_KERNEL);
5745 if (!nn->sessionid_hashtbl)
5748 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5749 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5750 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5752 for (i = 0; i < OWNER_HASH_SIZE; i++)
5753 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5754 for (i = 0; i < SESSION_HASH_SIZE; i++)
5755 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5756 nn->conf_name_tree = RB_ROOT;
5757 nn->unconf_name_tree = RB_ROOT;
5758 INIT_LIST_HEAD(&nn->client_lru);
5759 INIT_LIST_HEAD(&nn->close_lru);
5760 INIT_LIST_HEAD(&nn->del_recall_lru);
5761 spin_lock_init(&nn->client_lock);
5763 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5769 kfree(nn->ownerstr_hashtbl);
5771 kfree(nn->unconf_id_hashtbl);
5773 kfree(nn->conf_id_hashtbl);
5779 nfs4_state_destroy_net(struct net *net)
5782 struct nfs4_client *clp = NULL;
5783 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5785 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5786 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5787 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5788 destroy_client(clp);
5792 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5793 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5794 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5795 destroy_client(clp);
5799 kfree(nn->sessionid_hashtbl);
5800 kfree(nn->ownerstr_hashtbl);
5801 kfree(nn->unconf_id_hashtbl);
5802 kfree(nn->conf_id_hashtbl);
5807 nfs4_state_start_net(struct net *net)
5809 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5812 ret = nfs4_state_create_net(net);
5815 nfsd4_client_tracking_init(net);
5816 nn->boot_time = get_seconds();
5817 locks_start_grace(net, &nn->nfsd4_manager);
5818 nn->grace_ended = false;
5819 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5820 nn->nfsd4_grace, net);
5821 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5825 /* initialization to perform when the nfsd service is started: */
5828 nfs4_state_start(void)
5832 ret = set_callback_cred();
5835 laundry_wq = create_singlethread_workqueue("nfsd4");
5836 if (laundry_wq == NULL) {
5840 ret = nfsd4_create_callback_queue();
5842 goto out_free_laundry;
5844 set_max_delegations();
5849 destroy_workqueue(laundry_wq);
5855 nfs4_state_shutdown_net(struct net *net)
5857 struct nfs4_delegation *dp = NULL;
5858 struct list_head *pos, *next, reaplist;
5859 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5861 cancel_delayed_work_sync(&nn->laundromat_work);
5862 locks_end_grace(&nn->nfsd4_manager);
5865 INIT_LIST_HEAD(&reaplist);
5866 spin_lock(&state_lock);
5867 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5868 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5869 unhash_delegation_locked(dp);
5870 list_add(&dp->dl_recall_lru, &reaplist);
5872 spin_unlock(&state_lock);
5873 list_for_each_safe(pos, next, &reaplist) {
5874 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5875 list_del_init(&dp->dl_recall_lru);
5876 nfs4_put_stid(&dp->dl_stid);
5879 nfsd4_client_tracking_exit(net);
5880 nfs4_state_destroy_net(net);
5881 nfs4_unlock_state();
5885 nfs4_state_shutdown(void)
5887 destroy_workqueue(laundry_wq);
5888 nfsd4_destroy_callback_queue();
5892 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5894 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5895 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5899 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5901 if (cstate->minorversion) {
5902 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5903 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5908 clear_current_stateid(struct nfsd4_compound_state *cstate)
5910 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5914 * functions to set current state id
5917 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5919 put_stateid(cstate, &odp->od_stateid);
5923 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5925 put_stateid(cstate, &open->op_stateid);
5929 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5931 put_stateid(cstate, &close->cl_stateid);
5935 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5937 put_stateid(cstate, &lock->lk_resp_stateid);
5941 * functions to consume current state id
5945 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5947 get_stateid(cstate, &odp->od_stateid);
5951 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5953 get_stateid(cstate, &drp->dr_stateid);
5957 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5959 get_stateid(cstate, &fsp->fr_stateid);
5963 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5965 get_stateid(cstate, &setattr->sa_stateid);
5969 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5971 get_stateid(cstate, &close->cl_stateid);
5975 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5977 get_stateid(cstate, &locku->lu_stateid);
5981 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5983 get_stateid(cstate, &read->rd_stateid);
5987 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5989 get_stateid(cstate, &write->wr_stateid);