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 unhash_generic_stateid(struct nfs4_ol_stateid *stp)
895 struct nfs4_file *fp = stp->st_stid.sc_file;
897 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
899 spin_lock(&fp->fi_lock);
900 list_del(&stp->st_perfile);
901 spin_unlock(&fp->fi_lock);
902 list_del(&stp->st_perstateowner);
905 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
907 struct nfs4_ol_stateid *stp = openlockstateid(stid);
909 release_all_access(stp);
910 kmem_cache_free(stateid_slab, stid);
913 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
915 struct nfs4_ol_stateid *stp = openlockstateid(stid);
916 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
919 file = find_any_file(stp->st_stid.sc_file);
921 filp_close(file, (fl_owner_t)lo);
922 nfs4_free_ol_stateid(stid);
925 static void __release_lock_stateid(struct nfs4_ol_stateid *stp)
927 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
929 spin_lock(&oo->oo_owner.so_client->cl_lock);
930 list_del(&stp->st_locks);
931 unhash_generic_stateid(stp);
932 unhash_stid(&stp->st_stid);
933 spin_unlock(&oo->oo_owner.so_client->cl_lock);
934 nfs4_put_stid(&stp->st_stid);
937 static void unhash_lockowner(struct nfs4_lockowner *lo)
939 struct nfs4_ol_stateid *stp;
941 list_del(&lo->lo_owner.so_strhash);
942 while (!list_empty(&lo->lo_owner.so_stateids)) {
943 stp = list_first_entry(&lo->lo_owner.so_stateids,
944 struct nfs4_ol_stateid, st_perstateowner);
945 __release_lock_stateid(stp);
949 static void nfs4_free_lockowner(struct nfs4_lockowner *lo)
951 kfree(lo->lo_owner.so_owner.data);
952 kmem_cache_free(lockowner_slab, lo);
955 static void release_lockowner(struct nfs4_lockowner *lo)
957 unhash_lockowner(lo);
958 nfs4_free_lockowner(lo);
961 static void release_lockowner_if_empty(struct nfs4_lockowner *lo)
963 if (list_empty(&lo->lo_owner.so_stateids))
964 release_lockowner(lo);
967 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
969 struct nfs4_lockowner *lo;
971 lo = lockowner(stp->st_stateowner);
972 __release_lock_stateid(stp);
973 release_lockowner_if_empty(lo);
976 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
977 __releases(&open_stp->st_stateowner->so_client->cl_lock)
978 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
980 struct nfs4_ol_stateid *stp;
982 while (!list_empty(&open_stp->st_locks)) {
983 stp = list_entry(open_stp->st_locks.next,
984 struct nfs4_ol_stateid, st_locks);
985 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
986 release_lock_stateid(stp);
987 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
991 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
993 spin_lock(&stp->st_stateowner->so_client->cl_lock);
994 unhash_generic_stateid(stp);
995 release_open_stateid_locks(stp);
996 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
999 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1001 unhash_open_stateid(stp);
1002 nfs4_put_stid(&stp->st_stid);
1005 static void unhash_openowner(struct nfs4_openowner *oo)
1007 struct nfs4_ol_stateid *stp;
1009 list_del(&oo->oo_owner.so_strhash);
1010 list_del(&oo->oo_perclient);
1011 while (!list_empty(&oo->oo_owner.so_stateids)) {
1012 stp = list_first_entry(&oo->oo_owner.so_stateids,
1013 struct nfs4_ol_stateid, st_perstateowner);
1014 release_open_stateid(stp);
1018 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1020 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1023 nfs4_put_stid(&s->st_stid);
1024 oo->oo_last_closed_stid = NULL;
1028 static void nfs4_free_openowner(struct nfs4_openowner *oo)
1030 kfree(oo->oo_owner.so_owner.data);
1031 kmem_cache_free(openowner_slab, oo);
1034 static void release_openowner(struct nfs4_openowner *oo)
1036 unhash_openowner(oo);
1037 list_del(&oo->oo_close_lru);
1038 release_last_closed_stateid(oo);
1039 nfs4_free_openowner(oo);
1043 hash_sessionid(struct nfs4_sessionid *sessionid)
1045 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1047 return sid->sequence % SESSION_HASH_SIZE;
1052 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1054 u32 *ptr = (u32 *)(&sessionid->data[0]);
1055 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1059 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1065 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1066 * won't be used for replay.
1068 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1070 struct nfs4_stateowner *so = cstate->replay_owner;
1072 if (nfserr == nfserr_replay_me)
1075 if (!seqid_mutating_err(ntohl(nfserr))) {
1076 cstate->replay_owner = NULL;
1081 if (so->so_is_open_owner)
1082 release_last_closed_stateid(openowner(so));
1088 gen_sessionid(struct nfsd4_session *ses)
1090 struct nfs4_client *clp = ses->se_client;
1091 struct nfsd4_sessionid *sid;
1093 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1094 sid->clientid = clp->cl_clientid;
1095 sid->sequence = current_sessionid++;
1100 * The protocol defines ca_maxresponssize_cached to include the size of
1101 * the rpc header, but all we need to cache is the data starting after
1102 * the end of the initial SEQUENCE operation--the rest we regenerate
1103 * each time. Therefore we can advertise a ca_maxresponssize_cached
1104 * value that is the number of bytes in our cache plus a few additional
1105 * bytes. In order to stay on the safe side, and not promise more than
1106 * we can cache, those additional bytes must be the minimum possible: 24
1107 * bytes of rpc header (xid through accept state, with AUTH_NULL
1108 * verifier), 12 for the compound header (with zero-length tag), and 44
1109 * for the SEQUENCE op response:
1111 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1114 free_session_slots(struct nfsd4_session *ses)
1118 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1119 kfree(ses->se_slots[i]);
1123 * We don't actually need to cache the rpc and session headers, so we
1124 * can allocate a little less for each slot:
1126 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1130 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1133 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1134 return size + sizeof(struct nfsd4_slot);
1138 * XXX: If we run out of reserved DRC memory we could (up to a point)
1139 * re-negotiate active sessions and reduce their slot usage to make
1140 * room for new connections. For now we just fail the create session.
1142 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1144 u32 slotsize = slot_bytes(ca);
1145 u32 num = ca->maxreqs;
1148 spin_lock(&nfsd_drc_lock);
1149 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1150 nfsd_drc_max_mem - nfsd_drc_mem_used);
1151 num = min_t(int, num, avail / slotsize);
1152 nfsd_drc_mem_used += num * slotsize;
1153 spin_unlock(&nfsd_drc_lock);
1158 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1160 int slotsize = slot_bytes(ca);
1162 spin_lock(&nfsd_drc_lock);
1163 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1164 spin_unlock(&nfsd_drc_lock);
1167 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1168 struct nfsd4_channel_attrs *battrs)
1170 int numslots = fattrs->maxreqs;
1171 int slotsize = slot_bytes(fattrs);
1172 struct nfsd4_session *new;
1175 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1176 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1177 mem = numslots * sizeof(struct nfsd4_slot *);
1179 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1182 /* allocate each struct nfsd4_slot and data cache in one piece */
1183 for (i = 0; i < numslots; i++) {
1184 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1185 if (!new->se_slots[i])
1189 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1190 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1195 kfree(new->se_slots[i]);
1200 static void free_conn(struct nfsd4_conn *c)
1202 svc_xprt_put(c->cn_xprt);
1206 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1208 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1209 struct nfs4_client *clp = c->cn_session->se_client;
1211 spin_lock(&clp->cl_lock);
1212 if (!list_empty(&c->cn_persession)) {
1213 list_del(&c->cn_persession);
1216 nfsd4_probe_callback(clp);
1217 spin_unlock(&clp->cl_lock);
1220 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1222 struct nfsd4_conn *conn;
1224 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1227 svc_xprt_get(rqstp->rq_xprt);
1228 conn->cn_xprt = rqstp->rq_xprt;
1229 conn->cn_flags = flags;
1230 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1234 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1236 conn->cn_session = ses;
1237 list_add(&conn->cn_persession, &ses->se_conns);
1240 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1242 struct nfs4_client *clp = ses->se_client;
1244 spin_lock(&clp->cl_lock);
1245 __nfsd4_hash_conn(conn, ses);
1246 spin_unlock(&clp->cl_lock);
1249 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1251 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1252 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1255 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1259 nfsd4_hash_conn(conn, ses);
1260 ret = nfsd4_register_conn(conn);
1262 /* oops; xprt is already down: */
1263 nfsd4_conn_lost(&conn->cn_xpt_user);
1264 /* We may have gained or lost a callback channel: */
1265 nfsd4_probe_callback_sync(ses->se_client);
1268 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1270 u32 dir = NFS4_CDFC4_FORE;
1272 if (cses->flags & SESSION4_BACK_CHAN)
1273 dir |= NFS4_CDFC4_BACK;
1274 return alloc_conn(rqstp, dir);
1277 /* must be called under client_lock */
1278 static void nfsd4_del_conns(struct nfsd4_session *s)
1280 struct nfs4_client *clp = s->se_client;
1281 struct nfsd4_conn *c;
1283 spin_lock(&clp->cl_lock);
1284 while (!list_empty(&s->se_conns)) {
1285 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1286 list_del_init(&c->cn_persession);
1287 spin_unlock(&clp->cl_lock);
1289 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1292 spin_lock(&clp->cl_lock);
1294 spin_unlock(&clp->cl_lock);
1297 static void __free_session(struct nfsd4_session *ses)
1299 free_session_slots(ses);
1303 static void free_session(struct nfsd4_session *ses)
1305 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1307 lockdep_assert_held(&nn->client_lock);
1308 nfsd4_del_conns(ses);
1309 nfsd4_put_drc_mem(&ses->se_fchannel);
1310 __free_session(ses);
1313 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1316 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1318 new->se_client = clp;
1321 INIT_LIST_HEAD(&new->se_conns);
1323 new->se_cb_seq_nr = 1;
1324 new->se_flags = cses->flags;
1325 new->se_cb_prog = cses->callback_prog;
1326 new->se_cb_sec = cses->cb_sec;
1327 atomic_set(&new->se_ref, 0);
1328 idx = hash_sessionid(&new->se_sessionid);
1329 spin_lock(&nn->client_lock);
1330 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1331 spin_lock(&clp->cl_lock);
1332 list_add(&new->se_perclnt, &clp->cl_sessions);
1333 spin_unlock(&clp->cl_lock);
1334 spin_unlock(&nn->client_lock);
1336 if (cses->flags & SESSION4_BACK_CHAN) {
1337 struct sockaddr *sa = svc_addr(rqstp);
1339 * This is a little silly; with sessions there's no real
1340 * use for the callback address. Use the peer address
1341 * as a reasonable default for now, but consider fixing
1342 * the rpc client not to require an address in the
1345 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1346 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1350 /* caller must hold client_lock */
1351 static struct nfsd4_session *
1352 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1354 struct nfsd4_session *elem;
1356 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1358 dump_sessionid(__func__, sessionid);
1359 idx = hash_sessionid(sessionid);
1360 /* Search in the appropriate list */
1361 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1362 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1363 NFS4_MAX_SESSIONID_LEN)) {
1368 dprintk("%s: session not found\n", __func__);
1372 static struct nfsd4_session *
1373 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1376 struct nfsd4_session *session;
1377 __be32 status = nfserr_badsession;
1379 session = __find_in_sessionid_hashtbl(sessionid, net);
1382 status = nfsd4_get_session_locked(session);
1390 /* caller must hold client_lock */
1392 unhash_session(struct nfsd4_session *ses)
1394 list_del(&ses->se_hash);
1395 spin_lock(&ses->se_client->cl_lock);
1396 list_del(&ses->se_perclnt);
1397 spin_unlock(&ses->se_client->cl_lock);
1400 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1402 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1404 if (clid->cl_boot == nn->boot_time)
1406 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1407 clid->cl_boot, clid->cl_id, nn->boot_time);
1412 * XXX Should we use a slab cache ?
1413 * This type of memory management is somewhat inefficient, but we use it
1414 * anyway since SETCLIENTID is not a common operation.
1416 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1418 struct nfs4_client *clp;
1420 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1423 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1424 if (clp->cl_name.data == NULL) {
1428 clp->cl_name.len = name.len;
1429 INIT_LIST_HEAD(&clp->cl_sessions);
1430 idr_init(&clp->cl_stateids);
1431 atomic_set(&clp->cl_refcount, 0);
1432 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1433 INIT_LIST_HEAD(&clp->cl_idhash);
1434 INIT_LIST_HEAD(&clp->cl_openowners);
1435 INIT_LIST_HEAD(&clp->cl_delegations);
1436 INIT_LIST_HEAD(&clp->cl_lru);
1437 INIT_LIST_HEAD(&clp->cl_callbacks);
1438 INIT_LIST_HEAD(&clp->cl_revoked);
1439 spin_lock_init(&clp->cl_lock);
1440 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1445 free_client(struct nfs4_client *clp)
1447 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1449 lockdep_assert_held(&nn->client_lock);
1450 while (!list_empty(&clp->cl_sessions)) {
1451 struct nfsd4_session *ses;
1452 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1454 list_del(&ses->se_perclnt);
1455 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1458 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1459 free_svc_cred(&clp->cl_cred);
1460 kfree(clp->cl_name.data);
1461 idr_destroy(&clp->cl_stateids);
1465 /* must be called under the client_lock */
1467 unhash_client_locked(struct nfs4_client *clp)
1469 struct nfsd4_session *ses;
1471 list_del(&clp->cl_lru);
1472 spin_lock(&clp->cl_lock);
1473 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1474 list_del_init(&ses->se_hash);
1475 spin_unlock(&clp->cl_lock);
1479 destroy_client(struct nfs4_client *clp)
1481 struct nfs4_openowner *oo;
1482 struct nfs4_delegation *dp;
1483 struct list_head reaplist;
1484 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1486 INIT_LIST_HEAD(&reaplist);
1487 spin_lock(&state_lock);
1488 while (!list_empty(&clp->cl_delegations)) {
1489 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1490 unhash_delegation_locked(dp);
1491 list_add(&dp->dl_recall_lru, &reaplist);
1493 spin_unlock(&state_lock);
1494 while (!list_empty(&reaplist)) {
1495 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1496 list_del_init(&dp->dl_recall_lru);
1497 nfs4_put_stid(&dp->dl_stid);
1499 while (!list_empty(&clp->cl_revoked)) {
1500 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1501 list_del_init(&dp->dl_recall_lru);
1502 nfs4_put_stid(&dp->dl_stid);
1504 while (!list_empty(&clp->cl_openowners)) {
1505 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1506 release_openowner(oo);
1508 nfsd4_shutdown_callback(clp);
1509 if (clp->cl_cb_conn.cb_xprt)
1510 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1511 list_del(&clp->cl_idhash);
1512 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1513 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1515 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1516 spin_lock(&nn->client_lock);
1517 unhash_client_locked(clp);
1518 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1520 spin_unlock(&nn->client_lock);
1523 static void expire_client(struct nfs4_client *clp)
1525 nfsd4_client_record_remove(clp);
1526 destroy_client(clp);
1529 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1531 memcpy(target->cl_verifier.data, source->data,
1532 sizeof(target->cl_verifier.data));
1535 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1537 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1538 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1541 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1543 if (source->cr_principal) {
1544 target->cr_principal =
1545 kstrdup(source->cr_principal, GFP_KERNEL);
1546 if (target->cr_principal == NULL)
1549 target->cr_principal = NULL;
1550 target->cr_flavor = source->cr_flavor;
1551 target->cr_uid = source->cr_uid;
1552 target->cr_gid = source->cr_gid;
1553 target->cr_group_info = source->cr_group_info;
1554 get_group_info(target->cr_group_info);
1555 target->cr_gss_mech = source->cr_gss_mech;
1556 if (source->cr_gss_mech)
1557 gss_mech_get(source->cr_gss_mech);
1562 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1566 res = o1->len - o2->len;
1569 return (long long)memcmp(o1->data, o2->data, o1->len);
1572 static int same_name(const char *n1, const char *n2)
1574 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1578 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1580 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1584 same_clid(clientid_t *cl1, clientid_t *cl2)
1586 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1589 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1593 if (g1->ngroups != g2->ngroups)
1595 for (i=0; i<g1->ngroups; i++)
1596 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1602 * RFC 3530 language requires clid_inuse be returned when the
1603 * "principal" associated with a requests differs from that previously
1604 * used. We use uid, gid's, and gss principal string as our best
1605 * approximation. We also don't want to allow non-gss use of a client
1606 * established using gss: in theory cr_principal should catch that
1607 * change, but in practice cr_principal can be null even in the gss case
1608 * since gssd doesn't always pass down a principal string.
1610 static bool is_gss_cred(struct svc_cred *cr)
1612 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1613 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1618 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1620 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1621 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1622 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1623 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1625 if (cr1->cr_principal == cr2->cr_principal)
1627 if (!cr1->cr_principal || !cr2->cr_principal)
1629 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1632 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1634 struct svc_cred *cr = &rqstp->rq_cred;
1637 if (!cr->cr_gss_mech)
1639 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1640 return service == RPC_GSS_SVC_INTEGRITY ||
1641 service == RPC_GSS_SVC_PRIVACY;
1644 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1646 struct svc_cred *cr = &rqstp->rq_cred;
1648 if (!cl->cl_mach_cred)
1650 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1652 if (!svc_rqst_integrity_protected(rqstp))
1654 if (!cr->cr_principal)
1656 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1659 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1661 static u32 current_clientid = 1;
1663 clp->cl_clientid.cl_boot = nn->boot_time;
1664 clp->cl_clientid.cl_id = current_clientid++;
1667 static void gen_confirm(struct nfs4_client *clp)
1673 * This is opaque to client, so no need to byte-swap. Use
1674 * __force to keep sparse happy
1676 verf[0] = (__force __be32)get_seconds();
1677 verf[1] = (__force __be32)i++;
1678 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1681 static struct nfs4_stid *
1682 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1684 struct nfs4_stid *ret;
1686 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1687 if (!ret || !ret->sc_type)
1692 static struct nfs4_stid *
1693 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1695 struct nfs4_stid *s;
1697 spin_lock(&cl->cl_lock);
1698 s = find_stateid_locked(cl, t);
1699 if (s != NULL && !(typemask & s->sc_type))
1701 spin_unlock(&cl->cl_lock);
1705 static struct nfs4_client *create_client(struct xdr_netobj name,
1706 struct svc_rqst *rqstp, nfs4_verifier *verf)
1708 struct nfs4_client *clp;
1709 struct sockaddr *sa = svc_addr(rqstp);
1711 struct net *net = SVC_NET(rqstp);
1712 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1714 clp = alloc_client(name);
1718 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1720 spin_lock(&nn->client_lock);
1722 spin_unlock(&nn->client_lock);
1725 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1726 clp->cl_time = get_seconds();
1727 clear_bit(0, &clp->cl_cb_slot_busy);
1728 copy_verf(clp, verf);
1729 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1731 clp->cl_cb_session = NULL;
1737 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1739 struct rb_node **new = &(root->rb_node), *parent = NULL;
1740 struct nfs4_client *clp;
1743 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1746 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1747 new = &((*new)->rb_left);
1749 new = &((*new)->rb_right);
1752 rb_link_node(&new_clp->cl_namenode, parent, new);
1753 rb_insert_color(&new_clp->cl_namenode, root);
1756 static struct nfs4_client *
1757 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1760 struct rb_node *node = root->rb_node;
1761 struct nfs4_client *clp;
1764 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1765 cmp = compare_blob(&clp->cl_name, name);
1767 node = node->rb_left;
1769 node = node->rb_right;
1777 add_to_unconfirmed(struct nfs4_client *clp)
1779 unsigned int idhashval;
1780 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1782 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1783 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1784 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1785 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1790 move_to_confirmed(struct nfs4_client *clp)
1792 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1793 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1795 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1796 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1797 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1798 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1799 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1803 static struct nfs4_client *
1804 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1806 struct nfs4_client *clp;
1807 unsigned int idhashval = clientid_hashval(clid->cl_id);
1809 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1810 if (same_clid(&clp->cl_clientid, clid)) {
1811 if ((bool)clp->cl_minorversion != sessions)
1820 static struct nfs4_client *
1821 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1823 struct list_head *tbl = nn->conf_id_hashtbl;
1825 return find_client_in_id_table(tbl, clid, sessions);
1828 static struct nfs4_client *
1829 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1831 struct list_head *tbl = nn->unconf_id_hashtbl;
1833 return find_client_in_id_table(tbl, clid, sessions);
1836 static bool clp_used_exchangeid(struct nfs4_client *clp)
1838 return clp->cl_exchange_flags != 0;
1841 static struct nfs4_client *
1842 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1844 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1847 static struct nfs4_client *
1848 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1850 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1854 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1856 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1857 struct sockaddr *sa = svc_addr(rqstp);
1858 u32 scopeid = rpc_get_scope_id(sa);
1859 unsigned short expected_family;
1861 /* Currently, we only support tcp and tcp6 for the callback channel */
1862 if (se->se_callback_netid_len == 3 &&
1863 !memcmp(se->se_callback_netid_val, "tcp", 3))
1864 expected_family = AF_INET;
1865 else if (se->se_callback_netid_len == 4 &&
1866 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1867 expected_family = AF_INET6;
1871 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1872 se->se_callback_addr_len,
1873 (struct sockaddr *)&conn->cb_addr,
1874 sizeof(conn->cb_addr));
1876 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1879 if (conn->cb_addr.ss_family == AF_INET6)
1880 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1882 conn->cb_prog = se->se_callback_prog;
1883 conn->cb_ident = se->se_callback_ident;
1884 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1887 conn->cb_addr.ss_family = AF_UNSPEC;
1888 conn->cb_addrlen = 0;
1889 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1890 "will not receive delegations\n",
1891 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1897 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1900 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1902 struct xdr_buf *buf = resp->xdr.buf;
1903 struct nfsd4_slot *slot = resp->cstate.slot;
1906 dprintk("--> %s slot %p\n", __func__, slot);
1908 slot->sl_opcnt = resp->opcnt;
1909 slot->sl_status = resp->cstate.status;
1911 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1912 if (nfsd4_not_cached(resp)) {
1913 slot->sl_datalen = 0;
1916 base = resp->cstate.data_offset;
1917 slot->sl_datalen = buf->len - base;
1918 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
1919 WARN("%s: sessions DRC could not cache compound\n", __func__);
1924 * Encode the replay sequence operation from the slot values.
1925 * If cachethis is FALSE encode the uncached rep error on the next
1926 * operation which sets resp->p and increments resp->opcnt for
1927 * nfs4svc_encode_compoundres.
1931 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1932 struct nfsd4_compoundres *resp)
1934 struct nfsd4_op *op;
1935 struct nfsd4_slot *slot = resp->cstate.slot;
1937 /* Encode the replayed sequence operation */
1938 op = &args->ops[resp->opcnt - 1];
1939 nfsd4_encode_operation(resp, op);
1941 /* Return nfserr_retry_uncached_rep in next operation. */
1942 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1943 op = &args->ops[resp->opcnt++];
1944 op->status = nfserr_retry_uncached_rep;
1945 nfsd4_encode_operation(resp, op);
1951 * The sequence operation is not cached because we can use the slot and
1955 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1956 struct nfsd4_sequence *seq)
1958 struct nfsd4_slot *slot = resp->cstate.slot;
1959 struct xdr_stream *xdr = &resp->xdr;
1963 dprintk("--> %s slot %p\n", __func__, slot);
1965 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1969 p = xdr_reserve_space(xdr, slot->sl_datalen);
1972 return nfserr_serverfault;
1974 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
1975 xdr_commit_encode(xdr);
1977 resp->opcnt = slot->sl_opcnt;
1978 return slot->sl_status;
1982 * Set the exchange_id flags returned by the server.
1985 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1987 /* pNFS is not supported */
1988 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1990 /* Referrals are supported, Migration is not. */
1991 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1993 /* set the wire flags to return to client. */
1994 clid->flags = new->cl_exchange_flags;
1997 static bool client_has_state(struct nfs4_client *clp)
2000 * Note clp->cl_openowners check isn't quite right: there's no
2001 * need to count owners without stateid's.
2003 * Also note we should probably be using this in 4.0 case too.
2005 return !list_empty(&clp->cl_openowners)
2006 || !list_empty(&clp->cl_delegations)
2007 || !list_empty(&clp->cl_sessions);
2011 nfsd4_exchange_id(struct svc_rqst *rqstp,
2012 struct nfsd4_compound_state *cstate,
2013 struct nfsd4_exchange_id *exid)
2015 struct nfs4_client *unconf, *conf, *new;
2017 char addr_str[INET6_ADDRSTRLEN];
2018 nfs4_verifier verf = exid->verifier;
2019 struct sockaddr *sa = svc_addr(rqstp);
2020 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2021 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2023 rpc_ntop(sa, addr_str, sizeof(addr_str));
2024 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2025 "ip_addr=%s flags %x, spa_how %d\n",
2026 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2027 addr_str, exid->flags, exid->spa_how);
2029 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2030 return nfserr_inval;
2032 switch (exid->spa_how) {
2034 if (!svc_rqst_integrity_protected(rqstp))
2035 return nfserr_inval;
2038 default: /* checked by xdr code */
2041 return nfserr_encr_alg_unsupp;
2044 /* Cases below refer to rfc 5661 section 18.35.4: */
2046 conf = find_confirmed_client_by_name(&exid->clname, nn);
2048 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2049 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2052 if (!clp_used_exchangeid(conf)) { /* buggy client */
2053 status = nfserr_inval;
2056 if (!mach_creds_match(conf, rqstp)) {
2057 status = nfserr_wrong_cred;
2060 if (!creds_match) { /* case 9 */
2061 status = nfserr_perm;
2064 if (!verfs_match) { /* case 8 */
2065 status = nfserr_not_same;
2069 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2073 if (!creds_match) { /* case 3 */
2074 if (client_has_state(conf)) {
2075 status = nfserr_clid_inuse;
2078 expire_client(conf);
2081 if (verfs_match) { /* case 2 */
2082 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2086 /* case 5, client reboot */
2090 if (update) { /* case 7 */
2091 status = nfserr_noent;
2095 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2096 if (unconf) /* case 4, possible retry or client restart */
2097 expire_client(unconf);
2099 /* case 1 (normal case) */
2101 new = create_client(exid->clname, rqstp, &verf);
2103 status = nfserr_jukebox;
2106 new->cl_minorversion = cstate->minorversion;
2107 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2110 add_to_unconfirmed(new);
2112 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2113 exid->clientid.cl_id = new->cl_clientid.cl_id;
2115 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2116 nfsd4_set_ex_flags(new, exid);
2118 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2119 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2123 nfs4_unlock_state();
2128 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2130 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2133 /* The slot is in use, and no response has been sent. */
2135 if (seqid == slot_seqid)
2136 return nfserr_jukebox;
2138 return nfserr_seq_misordered;
2140 /* Note unsigned 32-bit arithmetic handles wraparound: */
2141 if (likely(seqid == slot_seqid + 1))
2143 if (seqid == slot_seqid)
2144 return nfserr_replay_cache;
2145 return nfserr_seq_misordered;
2149 * Cache the create session result into the create session single DRC
2150 * slot cache by saving the xdr structure. sl_seqid has been set.
2151 * Do this for solo or embedded create session operations.
2154 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2155 struct nfsd4_clid_slot *slot, __be32 nfserr)
2157 slot->sl_status = nfserr;
2158 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2162 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2163 struct nfsd4_clid_slot *slot)
2165 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2166 return slot->sl_status;
2169 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2170 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2171 1 + /* MIN tag is length with zero, only length */ \
2172 3 + /* version, opcount, opcode */ \
2173 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2174 /* seqid, slotID, slotID, cache */ \
2175 4 ) * sizeof(__be32))
2177 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2178 2 + /* verifier: AUTH_NULL, length 0 */\
2180 1 + /* MIN tag is length with zero, only length */ \
2181 3 + /* opcount, opcode, opstatus*/ \
2182 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2183 /* seqid, slotID, slotID, slotID, status */ \
2184 5 ) * sizeof(__be32))
2186 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2188 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2190 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2191 return nfserr_toosmall;
2192 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2193 return nfserr_toosmall;
2194 ca->headerpadsz = 0;
2195 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2196 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2197 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2198 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2199 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2200 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2202 * Note decreasing slot size below client's request may make it
2203 * difficult for client to function correctly, whereas
2204 * decreasing the number of slots will (just?) affect
2205 * performance. When short on memory we therefore prefer to
2206 * decrease number of slots instead of their size. Clients that
2207 * request larger slots than they need will get poor results:
2209 ca->maxreqs = nfsd4_get_drc_mem(ca);
2211 return nfserr_jukebox;
2216 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2217 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2218 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2219 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2221 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2223 ca->headerpadsz = 0;
2226 * These RPC_MAX_HEADER macros are overkill, especially since we
2227 * don't even do gss on the backchannel yet. But this is still
2228 * less than 1k. Tighten up this estimate in the unlikely event
2229 * it turns out to be a problem for some client:
2231 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2232 return nfserr_toosmall;
2233 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2234 return nfserr_toosmall;
2235 ca->maxresp_cached = 0;
2237 return nfserr_toosmall;
2242 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2244 switch (cbs->flavor) {
2250 * GSS case: the spec doesn't allow us to return this
2251 * error. But it also doesn't allow us not to support
2253 * I'd rather this fail hard than return some error the
2254 * client might think it can already handle:
2256 return nfserr_encr_alg_unsupp;
2261 nfsd4_create_session(struct svc_rqst *rqstp,
2262 struct nfsd4_compound_state *cstate,
2263 struct nfsd4_create_session *cr_ses)
2265 struct sockaddr *sa = svc_addr(rqstp);
2266 struct nfs4_client *conf, *unconf;
2267 struct nfsd4_session *new;
2268 struct nfsd4_conn *conn;
2269 struct nfsd4_clid_slot *cs_slot = NULL;
2271 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2273 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2274 return nfserr_inval;
2275 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2278 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2281 status = check_backchannel_attrs(&cr_ses->back_channel);
2283 goto out_release_drc_mem;
2284 status = nfserr_jukebox;
2285 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2287 goto out_release_drc_mem;
2288 conn = alloc_conn_from_crses(rqstp, cr_ses);
2290 goto out_free_session;
2293 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2294 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2295 WARN_ON_ONCE(conf && unconf);
2298 status = nfserr_wrong_cred;
2299 if (!mach_creds_match(conf, rqstp))
2301 cs_slot = &conf->cl_cs_slot;
2302 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2303 if (status == nfserr_replay_cache) {
2304 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2306 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2307 status = nfserr_seq_misordered;
2310 } else if (unconf) {
2311 struct nfs4_client *old;
2312 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2313 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2314 status = nfserr_clid_inuse;
2317 status = nfserr_wrong_cred;
2318 if (!mach_creds_match(unconf, rqstp))
2320 cs_slot = &unconf->cl_cs_slot;
2321 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2323 /* an unconfirmed replay returns misordered */
2324 status = nfserr_seq_misordered;
2327 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2329 status = mark_client_expired(old);
2334 move_to_confirmed(unconf);
2337 status = nfserr_stale_clientid;
2342 * We do not support RDMA or persistent sessions
2344 cr_ses->flags &= ~SESSION4_PERSIST;
2345 cr_ses->flags &= ~SESSION4_RDMA;
2347 init_session(rqstp, new, conf, cr_ses);
2348 nfsd4_init_conn(rqstp, conn, new);
2350 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2351 NFS4_MAX_SESSIONID_LEN);
2352 cs_slot->sl_seqid++;
2353 cr_ses->seqid = cs_slot->sl_seqid;
2355 /* cache solo and embedded create sessions under the state lock */
2356 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2357 nfs4_unlock_state();
2360 nfs4_unlock_state();
2363 __free_session(new);
2364 out_release_drc_mem:
2365 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2369 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2372 case NFS4_CDFC4_FORE:
2373 case NFS4_CDFC4_BACK:
2375 case NFS4_CDFC4_FORE_OR_BOTH:
2376 case NFS4_CDFC4_BACK_OR_BOTH:
2377 *dir = NFS4_CDFC4_BOTH;
2380 return nfserr_inval;
2383 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2385 struct nfsd4_session *session = cstate->session;
2386 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2389 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2392 spin_lock(&nn->client_lock);
2393 session->se_cb_prog = bc->bc_cb_program;
2394 session->se_cb_sec = bc->bc_cb_sec;
2395 spin_unlock(&nn->client_lock);
2397 nfsd4_probe_callback(session->se_client);
2402 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2403 struct nfsd4_compound_state *cstate,
2404 struct nfsd4_bind_conn_to_session *bcts)
2407 struct nfsd4_conn *conn;
2408 struct nfsd4_session *session;
2409 struct net *net = SVC_NET(rqstp);
2410 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2412 if (!nfsd4_last_compound_op(rqstp))
2413 return nfserr_not_only_op;
2415 spin_lock(&nn->client_lock);
2416 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2417 spin_unlock(&nn->client_lock);
2419 goto out_no_session;
2420 status = nfserr_wrong_cred;
2421 if (!mach_creds_match(session->se_client, rqstp))
2423 status = nfsd4_map_bcts_dir(&bcts->dir);
2426 conn = alloc_conn(rqstp, bcts->dir);
2427 status = nfserr_jukebox;
2430 nfsd4_init_conn(rqstp, conn, session);
2433 nfsd4_put_session(session);
2435 nfs4_unlock_state();
2439 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2443 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2447 nfsd4_destroy_session(struct svc_rqst *r,
2448 struct nfsd4_compound_state *cstate,
2449 struct nfsd4_destroy_session *sessionid)
2451 struct nfsd4_session *ses;
2453 int ref_held_by_me = 0;
2454 struct net *net = SVC_NET(r);
2455 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2458 status = nfserr_not_only_op;
2459 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2460 if (!nfsd4_last_compound_op(r))
2464 dump_sessionid(__func__, &sessionid->sessionid);
2465 spin_lock(&nn->client_lock);
2466 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2468 goto out_client_lock;
2469 status = nfserr_wrong_cred;
2470 if (!mach_creds_match(ses->se_client, r))
2471 goto out_put_session;
2472 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2474 goto out_put_session;
2475 unhash_session(ses);
2476 spin_unlock(&nn->client_lock);
2478 nfsd4_probe_callback_sync(ses->se_client);
2480 spin_lock(&nn->client_lock);
2483 nfsd4_put_session_locked(ses);
2485 spin_unlock(&nn->client_lock);
2487 nfs4_unlock_state();
2491 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2493 struct nfsd4_conn *c;
2495 list_for_each_entry(c, &s->se_conns, cn_persession) {
2496 if (c->cn_xprt == xpt) {
2503 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2505 struct nfs4_client *clp = ses->se_client;
2506 struct nfsd4_conn *c;
2507 __be32 status = nfs_ok;
2510 spin_lock(&clp->cl_lock);
2511 c = __nfsd4_find_conn(new->cn_xprt, ses);
2514 status = nfserr_conn_not_bound_to_session;
2515 if (clp->cl_mach_cred)
2517 __nfsd4_hash_conn(new, ses);
2518 spin_unlock(&clp->cl_lock);
2519 ret = nfsd4_register_conn(new);
2521 /* oops; xprt is already down: */
2522 nfsd4_conn_lost(&new->cn_xpt_user);
2525 spin_unlock(&clp->cl_lock);
2530 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2532 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2534 return args->opcnt > session->se_fchannel.maxops;
2537 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2538 struct nfsd4_session *session)
2540 struct xdr_buf *xb = &rqstp->rq_arg;
2542 return xb->len > session->se_fchannel.maxreq_sz;
2546 nfsd4_sequence(struct svc_rqst *rqstp,
2547 struct nfsd4_compound_state *cstate,
2548 struct nfsd4_sequence *seq)
2550 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2551 struct xdr_stream *xdr = &resp->xdr;
2552 struct nfsd4_session *session;
2553 struct nfs4_client *clp;
2554 struct nfsd4_slot *slot;
2555 struct nfsd4_conn *conn;
2558 struct net *net = SVC_NET(rqstp);
2559 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2561 if (resp->opcnt != 1)
2562 return nfserr_sequence_pos;
2565 * Will be either used or freed by nfsd4_sequence_check_conn
2568 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2570 return nfserr_jukebox;
2572 spin_lock(&nn->client_lock);
2573 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2575 goto out_no_session;
2576 clp = session->se_client;
2578 status = nfserr_too_many_ops;
2579 if (nfsd4_session_too_many_ops(rqstp, session))
2580 goto out_put_session;
2582 status = nfserr_req_too_big;
2583 if (nfsd4_request_too_big(rqstp, session))
2584 goto out_put_session;
2586 status = nfserr_badslot;
2587 if (seq->slotid >= session->se_fchannel.maxreqs)
2588 goto out_put_session;
2590 slot = session->se_slots[seq->slotid];
2591 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2593 /* We do not negotiate the number of slots yet, so set the
2594 * maxslots to the session maxreqs which is used to encode
2595 * sr_highest_slotid and the sr_target_slot id to maxslots */
2596 seq->maxslots = session->se_fchannel.maxreqs;
2598 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2599 slot->sl_flags & NFSD4_SLOT_INUSE);
2600 if (status == nfserr_replay_cache) {
2601 status = nfserr_seq_misordered;
2602 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2603 goto out_put_session;
2604 cstate->slot = slot;
2605 cstate->session = session;
2607 /* Return the cached reply status and set cstate->status
2608 * for nfsd4_proc_compound processing */
2609 status = nfsd4_replay_cache_entry(resp, seq);
2610 cstate->status = nfserr_replay_cache;
2614 goto out_put_session;
2616 status = nfsd4_sequence_check_conn(conn, session);
2619 goto out_put_session;
2621 buflen = (seq->cachethis) ?
2622 session->se_fchannel.maxresp_cached :
2623 session->se_fchannel.maxresp_sz;
2624 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2626 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2627 goto out_put_session;
2628 svc_reserve(rqstp, buflen);
2631 /* Success! bump slot seqid */
2632 slot->sl_seqid = seq->seqid;
2633 slot->sl_flags |= NFSD4_SLOT_INUSE;
2635 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2637 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2639 cstate->slot = slot;
2640 cstate->session = session;
2644 switch (clp->cl_cb_state) {
2646 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2648 case NFSD4_CB_FAULT:
2649 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2652 seq->status_flags = 0;
2654 if (!list_empty(&clp->cl_revoked))
2655 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2659 spin_unlock(&nn->client_lock);
2662 nfsd4_put_session_locked(session);
2663 goto out_no_session;
2667 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2669 struct nfsd4_compound_state *cs = &resp->cstate;
2671 if (nfsd4_has_session(cs)) {
2672 if (cs->status != nfserr_replay_cache) {
2673 nfsd4_store_cache_entry(resp);
2674 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2676 /* Drop session reference that was taken in nfsd4_sequence() */
2677 nfsd4_put_session(cs->session);
2679 put_client_renew(cs->clp);
2683 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2685 struct nfs4_client *conf, *unconf, *clp;
2687 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2690 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2691 conf = find_confirmed_client(&dc->clientid, true, nn);
2692 WARN_ON_ONCE(conf && unconf);
2697 if (client_has_state(conf)) {
2698 status = nfserr_clientid_busy;
2704 status = nfserr_stale_clientid;
2707 if (!mach_creds_match(clp, rqstp)) {
2708 status = nfserr_wrong_cred;
2713 nfs4_unlock_state();
2718 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2722 if (rc->rca_one_fs) {
2723 if (!cstate->current_fh.fh_dentry)
2724 return nfserr_nofilehandle;
2726 * We don't take advantage of the rca_one_fs case.
2727 * That's OK, it's optional, we can safely ignore it.
2733 status = nfserr_complete_already;
2734 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2735 &cstate->session->se_client->cl_flags))
2738 status = nfserr_stale_clientid;
2739 if (is_client_expired(cstate->session->se_client))
2741 * The following error isn't really legal.
2742 * But we only get here if the client just explicitly
2743 * destroyed the client. Surely it no longer cares what
2744 * error it gets back on an operation for the dead
2750 nfsd4_client_record_create(cstate->session->se_client);
2752 nfs4_unlock_state();
2757 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2758 struct nfsd4_setclientid *setclid)
2760 struct xdr_netobj clname = setclid->se_name;
2761 nfs4_verifier clverifier = setclid->se_verf;
2762 struct nfs4_client *conf, *unconf, *new;
2764 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2766 /* Cases below refer to rfc 3530 section 14.2.33: */
2768 conf = find_confirmed_client_by_name(&clname, nn);
2771 status = nfserr_clid_inuse;
2772 if (clp_used_exchangeid(conf))
2774 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2775 char addr_str[INET6_ADDRSTRLEN];
2776 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2778 dprintk("NFSD: setclientid: string in use by client "
2779 "at %s\n", addr_str);
2783 unconf = find_unconfirmed_client_by_name(&clname, nn);
2785 expire_client(unconf);
2786 status = nfserr_jukebox;
2787 new = create_client(clname, rqstp, &clverifier);
2790 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2791 /* case 1: probable callback update */
2792 copy_clid(new, conf);
2793 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2795 new->cl_minorversion = 0;
2796 gen_callback(new, setclid, rqstp);
2797 add_to_unconfirmed(new);
2798 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2799 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2800 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2803 nfs4_unlock_state();
2809 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2810 struct nfsd4_compound_state *cstate,
2811 struct nfsd4_setclientid_confirm *setclientid_confirm)
2813 struct nfs4_client *conf, *unconf;
2814 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2815 clientid_t * clid = &setclientid_confirm->sc_clientid;
2817 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2819 if (STALE_CLIENTID(clid, nn))
2820 return nfserr_stale_clientid;
2823 conf = find_confirmed_client(clid, false, nn);
2824 unconf = find_unconfirmed_client(clid, false, nn);
2826 * We try hard to give out unique clientid's, so if we get an
2827 * attempt to confirm the same clientid with a different cred,
2828 * there's a bug somewhere. Let's charitably assume it's our
2831 status = nfserr_serverfault;
2832 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2834 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2836 /* cases below refer to rfc 3530 section 14.2.34: */
2837 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2838 if (conf && !unconf) /* case 2: probable retransmit */
2840 else /* case 4: client hasn't noticed we rebooted yet? */
2841 status = nfserr_stale_clientid;
2845 if (conf) { /* case 1: callback update */
2846 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2847 nfsd4_probe_callback(conf);
2848 expire_client(unconf);
2849 } else { /* case 3: normal case; new or rebooted client */
2850 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2852 status = mark_client_expired(conf);
2855 expire_client(conf);
2857 move_to_confirmed(unconf);
2858 nfsd4_probe_callback(unconf);
2861 nfs4_unlock_state();
2865 static struct nfs4_file *nfsd4_alloc_file(void)
2867 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2870 /* OPEN Share state helper functions */
2871 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2873 unsigned int hashval = file_hashval(fh);
2875 lockdep_assert_held(&state_lock);
2877 atomic_set(&fp->fi_ref, 1);
2878 spin_lock_init(&fp->fi_lock);
2879 INIT_LIST_HEAD(&fp->fi_stateids);
2880 INIT_LIST_HEAD(&fp->fi_delegations);
2881 fh_copy_shallow(&fp->fi_fhandle, fh);
2882 fp->fi_had_conflict = false;
2883 fp->fi_lease = NULL;
2884 fp->fi_share_deny = 0;
2885 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2886 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2887 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2891 nfsd4_free_slabs(void)
2893 kmem_cache_destroy(openowner_slab);
2894 kmem_cache_destroy(lockowner_slab);
2895 kmem_cache_destroy(file_slab);
2896 kmem_cache_destroy(stateid_slab);
2897 kmem_cache_destroy(deleg_slab);
2901 nfsd4_init_slabs(void)
2903 openowner_slab = kmem_cache_create("nfsd4_openowners",
2904 sizeof(struct nfs4_openowner), 0, 0, NULL);
2905 if (openowner_slab == NULL)
2907 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2908 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2909 if (lockowner_slab == NULL)
2910 goto out_free_openowner_slab;
2911 file_slab = kmem_cache_create("nfsd4_files",
2912 sizeof(struct nfs4_file), 0, 0, NULL);
2913 if (file_slab == NULL)
2914 goto out_free_lockowner_slab;
2915 stateid_slab = kmem_cache_create("nfsd4_stateids",
2916 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2917 if (stateid_slab == NULL)
2918 goto out_free_file_slab;
2919 deleg_slab = kmem_cache_create("nfsd4_delegations",
2920 sizeof(struct nfs4_delegation), 0, 0, NULL);
2921 if (deleg_slab == NULL)
2922 goto out_free_stateid_slab;
2925 out_free_stateid_slab:
2926 kmem_cache_destroy(stateid_slab);
2928 kmem_cache_destroy(file_slab);
2929 out_free_lockowner_slab:
2930 kmem_cache_destroy(lockowner_slab);
2931 out_free_openowner_slab:
2932 kmem_cache_destroy(openowner_slab);
2934 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2938 static void init_nfs4_replay(struct nfs4_replay *rp)
2940 rp->rp_status = nfserr_serverfault;
2942 rp->rp_buf = rp->rp_ibuf;
2945 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2947 struct nfs4_stateowner *sop;
2949 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2953 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2954 if (!sop->so_owner.data) {
2955 kmem_cache_free(slab, sop);
2958 sop->so_owner.len = owner->len;
2960 INIT_LIST_HEAD(&sop->so_stateids);
2961 sop->so_client = clp;
2962 init_nfs4_replay(&sop->so_replay);
2966 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2968 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2970 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2971 list_add(&oo->oo_perclient, &clp->cl_openowners);
2974 static struct nfs4_openowner *
2975 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
2976 struct nfsd4_compound_state *cstate)
2978 struct nfs4_client *clp = cstate->clp;
2979 struct nfs4_openowner *oo;
2981 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2984 oo->oo_owner.so_is_open_owner = 1;
2985 oo->oo_owner.so_seqid = open->op_seqid;
2986 oo->oo_flags = NFS4_OO_NEW;
2987 if (nfsd4_has_session(cstate))
2988 oo->oo_flags |= NFS4_OO_CONFIRMED;
2990 oo->oo_last_closed_stid = NULL;
2991 INIT_LIST_HEAD(&oo->oo_close_lru);
2992 hash_openowner(oo, clp, strhashval);
2996 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2997 struct nfs4_openowner *oo = open->op_openowner;
2999 stp->st_stid.sc_type = NFS4_OPEN_STID;
3000 INIT_LIST_HEAD(&stp->st_locks);
3001 stp->st_stateowner = &oo->oo_owner;
3003 stp->st_stid.sc_file = fp;
3004 stp->st_access_bmap = 0;
3005 stp->st_deny_bmap = 0;
3006 stp->st_openstp = NULL;
3007 spin_lock(&oo->oo_owner.so_client->cl_lock);
3008 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3009 spin_lock(&fp->fi_lock);
3010 list_add(&stp->st_perfile, &fp->fi_stateids);
3011 spin_unlock(&fp->fi_lock);
3012 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3016 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
3018 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3020 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3022 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3023 oo->oo_time = get_seconds();
3027 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
3030 return (sop->so_owner.len == owner->len) &&
3031 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
3032 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
3035 static struct nfs4_openowner *
3036 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
3037 bool sessions, struct nfsd_net *nn)
3039 struct nfs4_stateowner *so;
3040 struct nfs4_openowner *oo;
3041 struct nfs4_client *clp;
3043 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
3044 if (!so->so_is_open_owner)
3046 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
3048 clp = oo->oo_owner.so_client;
3049 if ((bool)clp->cl_minorversion != sessions)
3051 renew_client(oo->oo_owner.so_client);
3058 /* search file_hashtbl[] for file */
3059 static struct nfs4_file *
3060 find_file_locked(struct knfsd_fh *fh)
3062 unsigned int hashval = file_hashval(fh);
3063 struct nfs4_file *fp;
3065 lockdep_assert_held(&state_lock);
3067 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3068 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3076 static struct nfs4_file *
3077 find_file(struct knfsd_fh *fh)
3079 struct nfs4_file *fp;
3081 spin_lock(&state_lock);
3082 fp = find_file_locked(fh);
3083 spin_unlock(&state_lock);
3087 static struct nfs4_file *
3088 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3090 struct nfs4_file *fp;
3092 spin_lock(&state_lock);
3093 fp = find_file_locked(fh);
3095 nfsd4_init_file(new, fh);
3098 spin_unlock(&state_lock);
3104 * Called to check deny when READ with all zero stateid or
3105 * WRITE with all zero or all one stateid
3108 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3110 struct nfs4_file *fp;
3111 __be32 ret = nfs_ok;
3113 fp = find_file(¤t_fh->fh_handle);
3116 /* Check for conflicting share reservations */
3117 spin_lock(&fp->fi_lock);
3118 if (fp->fi_share_deny & deny_type)
3119 ret = nfserr_locked;
3120 spin_unlock(&fp->fi_lock);
3125 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3127 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3130 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3133 * We can't do this in nfsd_break_deleg_cb because it is
3134 * already holding inode->i_lock.
3136 * If the dl_time != 0, then we know that it has already been
3137 * queued for a lease break. Don't queue it again.
3139 spin_lock(&state_lock);
3140 if (dp->dl_time == 0) {
3141 dp->dl_time = get_seconds();
3142 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3144 spin_unlock(&state_lock);
3147 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3150 * We're assuming the state code never drops its reference
3151 * without first removing the lease. Since we're in this lease
3152 * callback (and since the lease code is serialized by the kernel
3153 * lock) we know the server hasn't removed the lease yet, we know
3154 * it's safe to take a reference.
3156 atomic_inc(&dp->dl_stid.sc_count);
3157 nfsd4_cb_recall(dp);
3160 /* Called from break_lease() with i_lock held. */
3161 static void nfsd_break_deleg_cb(struct file_lock *fl)
3163 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3164 struct nfs4_delegation *dp;
3167 WARN(1, "(%p)->fl_owner NULL\n", fl);
3170 if (fp->fi_had_conflict) {
3171 WARN(1, "duplicate break on %p\n", fp);
3175 * We don't want the locks code to timeout the lease for us;
3176 * we'll remove it ourself if a delegation isn't returned
3179 fl->fl_break_time = 0;
3181 spin_lock(&fp->fi_lock);
3182 fp->fi_had_conflict = true;
3184 * If there are no delegations on the list, then we can't count on this
3185 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3186 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3187 * true should keep any new delegations from being hashed.
3189 if (list_empty(&fp->fi_delegations))
3190 fl->fl_break_time = jiffies;
3192 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3193 nfsd_break_one_deleg(dp);
3194 spin_unlock(&fp->fi_lock);
3198 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3201 return lease_modify(onlist, arg);
3206 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3207 .lm_break = nfsd_break_deleg_cb,
3208 .lm_change = nfsd_change_deleg_cb,
3211 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3213 if (nfsd4_has_session(cstate))
3215 if (seqid == so->so_seqid - 1)
3216 return nfserr_replay_me;
3217 if (seqid == so->so_seqid)
3219 return nfserr_bad_seqid;
3222 static __be32 lookup_clientid(clientid_t *clid,
3223 struct nfsd4_compound_state *cstate,
3224 struct nfsd_net *nn)
3226 struct nfs4_client *found;
3229 found = cstate->clp;
3230 if (!same_clid(&found->cl_clientid, clid))
3231 return nfserr_stale_clientid;
3235 if (STALE_CLIENTID(clid, nn))
3236 return nfserr_stale_clientid;
3239 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3240 * cached already then we know this is for is for v4.0 and "sessions"
3243 WARN_ON_ONCE(cstate->session);
3244 found = find_confirmed_client(clid, false, nn);
3246 return nfserr_expired;
3248 /* Cache the nfs4_client in cstate! */
3249 cstate->clp = found;
3250 atomic_inc(&found->cl_refcount);
3255 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3256 struct nfsd4_open *open, struct nfsd_net *nn)
3258 clientid_t *clientid = &open->op_clientid;
3259 struct nfs4_client *clp = NULL;
3260 unsigned int strhashval;
3261 struct nfs4_openowner *oo = NULL;
3264 if (STALE_CLIENTID(&open->op_clientid, nn))
3265 return nfserr_stale_clientid;
3267 * In case we need it later, after we've already created the
3268 * file and don't want to risk a further failure:
3270 open->op_file = nfsd4_alloc_file();
3271 if (open->op_file == NULL)
3272 return nfserr_jukebox;
3274 status = lookup_clientid(clientid, cstate, nn);
3279 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
3280 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
3281 open->op_openowner = oo;
3285 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3286 /* Replace unconfirmed owners without checking for replay. */
3287 release_openowner(oo);
3288 open->op_openowner = NULL;
3291 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3296 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3298 return nfserr_jukebox;
3299 open->op_openowner = oo;
3301 open->op_stp = nfs4_alloc_open_stateid(clp);
3303 return nfserr_jukebox;
3307 static inline __be32
3308 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3310 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3311 return nfserr_openmode;
3316 static int share_access_to_flags(u32 share_access)
3318 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3321 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3323 struct nfs4_stid *ret;
3325 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3328 /* FIXME: move into find_stateid_by_type */
3329 atomic_inc(&ret->sc_count);
3330 return delegstateid(ret);
3333 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3335 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3336 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3340 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3341 struct nfs4_delegation **dp)
3344 __be32 status = nfserr_bad_stateid;
3345 struct nfs4_delegation *deleg;
3347 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3350 flags = share_access_to_flags(open->op_share_access);
3351 status = nfs4_check_delegmode(deleg, flags);
3353 nfs4_put_stid(&deleg->dl_stid);
3358 if (!nfsd4_is_deleg_cur(open))
3362 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3366 static struct nfs4_ol_stateid *
3367 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3369 struct nfs4_ol_stateid *local, *ret = NULL;
3370 struct nfs4_openowner *oo = open->op_openowner;
3372 spin_lock(&fp->fi_lock);
3373 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3374 /* ignore lock owners */
3375 if (local->st_stateowner->so_is_open_owner == 0)
3377 if (local->st_stateowner == &oo->oo_owner) {
3382 spin_unlock(&fp->fi_lock);
3386 static inline int nfs4_access_to_access(u32 nfs4_access)
3390 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3391 flags |= NFSD_MAY_READ;
3392 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3393 flags |= NFSD_MAY_WRITE;
3397 static inline __be32
3398 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3399 struct nfsd4_open *open)
3401 struct iattr iattr = {
3402 .ia_valid = ATTR_SIZE,
3405 if (!open->op_truncate)
3407 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3408 return nfserr_inval;
3409 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3412 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3413 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3414 struct nfsd4_open *open)
3416 struct file *filp = NULL;
3418 int oflag = nfs4_access_to_omode(open->op_share_access);
3419 int access = nfs4_access_to_access(open->op_share_access);
3420 unsigned char old_access_bmap, old_deny_bmap;
3422 spin_lock(&fp->fi_lock);
3425 * Are we trying to set a deny mode that would conflict with
3428 status = nfs4_file_check_deny(fp, open->op_share_deny);
3429 if (status != nfs_ok) {
3430 spin_unlock(&fp->fi_lock);
3434 /* set access to the file */
3435 status = nfs4_file_get_access(fp, open->op_share_access);
3436 if (status != nfs_ok) {
3437 spin_unlock(&fp->fi_lock);
3441 /* Set access bits in stateid */
3442 old_access_bmap = stp->st_access_bmap;
3443 set_access(open->op_share_access, stp);
3445 /* Set new deny mask */
3446 old_deny_bmap = stp->st_deny_bmap;
3447 set_deny(open->op_share_deny, stp);
3448 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3450 if (!fp->fi_fds[oflag]) {
3451 spin_unlock(&fp->fi_lock);
3452 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3454 goto out_put_access;
3455 spin_lock(&fp->fi_lock);
3456 if (!fp->fi_fds[oflag]) {
3457 fp->fi_fds[oflag] = filp;
3461 spin_unlock(&fp->fi_lock);
3465 status = nfsd4_truncate(rqstp, cur_fh, open);
3467 goto out_put_access;
3471 stp->st_access_bmap = old_access_bmap;
3472 nfs4_file_put_access(fp, open->op_share_access);
3473 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3478 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)
3481 unsigned char old_deny_bmap;
3483 if (!test_access(open->op_share_access, stp))
3484 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3486 /* test and set deny mode */
3487 spin_lock(&fp->fi_lock);
3488 status = nfs4_file_check_deny(fp, open->op_share_deny);
3489 if (status == nfs_ok) {
3490 old_deny_bmap = stp->st_deny_bmap;
3491 set_deny(open->op_share_deny, stp);
3492 fp->fi_share_deny |=
3493 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3495 spin_unlock(&fp->fi_lock);
3497 if (status != nfs_ok)
3500 status = nfsd4_truncate(rqstp, cur_fh, open);
3501 if (status != nfs_ok)
3502 reset_union_bmap_deny(old_deny_bmap, stp);
3507 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3509 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3512 /* Should we give out recallable state?: */
3513 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3515 if (clp->cl_cb_state == NFSD4_CB_UP)
3518 * In the sessions case, since we don't have to establish a
3519 * separate connection for callbacks, we assume it's OK
3520 * until we hear otherwise:
3522 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3525 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3527 struct file_lock *fl;
3529 fl = locks_alloc_lock();
3532 locks_init_lock(fl);
3533 fl->fl_lmops = &nfsd_lease_mng_ops;
3534 fl->fl_flags = FL_DELEG;
3535 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3536 fl->fl_end = OFFSET_MAX;
3537 fl->fl_owner = (fl_owner_t)fp;
3538 fl->fl_pid = current->tgid;
3542 static int nfs4_setlease(struct nfs4_delegation *dp)
3544 struct nfs4_file *fp = dp->dl_stid.sc_file;
3545 struct file_lock *fl;
3549 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3552 filp = find_readable_file(fp);
3554 /* We should always have a readable file here */
3559 status = vfs_setlease(filp, fl->fl_type, &fl);
3561 locks_free_lock(fl);
3564 spin_lock(&state_lock);
3565 spin_lock(&fp->fi_lock);
3566 /* Did the lease get broken before we took the lock? */
3568 if (fp->fi_had_conflict)
3573 atomic_inc(&fp->fi_delegees);
3574 hash_delegation_locked(dp, fp);
3578 fp->fi_deleg_file = filp;
3579 atomic_set(&fp->fi_delegees, 1);
3580 hash_delegation_locked(dp, fp);
3581 spin_unlock(&fp->fi_lock);
3582 spin_unlock(&state_lock);
3585 spin_unlock(&fp->fi_lock);
3586 spin_unlock(&state_lock);
3592 static struct nfs4_delegation *
3593 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3594 struct nfs4_file *fp)
3597 struct nfs4_delegation *dp;
3599 if (fp->fi_had_conflict)
3600 return ERR_PTR(-EAGAIN);
3602 dp = alloc_init_deleg(clp, fh);
3604 return ERR_PTR(-ENOMEM);
3607 spin_lock(&state_lock);
3608 spin_lock(&fp->fi_lock);
3609 dp->dl_stid.sc_file = fp;
3610 if (!fp->fi_lease) {
3611 spin_unlock(&fp->fi_lock);
3612 spin_unlock(&state_lock);
3613 status = nfs4_setlease(dp);
3616 atomic_inc(&fp->fi_delegees);
3617 if (fp->fi_had_conflict) {
3621 hash_delegation_locked(dp, fp);
3624 spin_unlock(&fp->fi_lock);
3625 spin_unlock(&state_lock);
3628 nfs4_put_stid(&dp->dl_stid);
3629 return ERR_PTR(status);
3634 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3636 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3637 if (status == -EAGAIN)
3638 open->op_why_no_deleg = WND4_CONTENTION;
3640 open->op_why_no_deleg = WND4_RESOURCE;
3641 switch (open->op_deleg_want) {
3642 case NFS4_SHARE_WANT_READ_DELEG:
3643 case NFS4_SHARE_WANT_WRITE_DELEG:
3644 case NFS4_SHARE_WANT_ANY_DELEG:
3646 case NFS4_SHARE_WANT_CANCEL:
3647 open->op_why_no_deleg = WND4_CANCELLED;
3649 case NFS4_SHARE_WANT_NO_DELEG:
3656 * Attempt to hand out a delegation.
3658 * Note we don't support write delegations, and won't until the vfs has
3659 * proper support for them.
3662 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3663 struct nfs4_ol_stateid *stp)
3665 struct nfs4_delegation *dp;
3666 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3667 struct nfs4_client *clp = stp->st_stid.sc_client;
3671 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3672 open->op_recall = 0;
3673 switch (open->op_claim_type) {
3674 case NFS4_OPEN_CLAIM_PREVIOUS:
3676 open->op_recall = 1;
3677 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3680 case NFS4_OPEN_CLAIM_NULL:
3681 case NFS4_OPEN_CLAIM_FH:
3683 * Let's not give out any delegations till everyone's
3684 * had the chance to reclaim theirs....
3686 if (locks_in_grace(clp->net))
3688 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3691 * Also, if the file was opened for write or
3692 * create, there's a good chance the client's
3693 * about to write to it, resulting in an
3694 * immediate recall (since we don't support
3695 * write delegations):
3697 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3699 if (open->op_create == NFS4_OPEN_CREATE)
3705 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3709 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3711 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3712 STATEID_VAL(&dp->dl_stid.sc_stateid));
3713 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3714 nfs4_put_stid(&dp->dl_stid);
3717 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3718 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3719 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3720 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3721 open->op_recall = 1;
3724 /* 4.1 client asking for a delegation? */
3725 if (open->op_deleg_want)
3726 nfsd4_open_deleg_none_ext(open, status);
3730 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3731 struct nfs4_delegation *dp)
3733 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3734 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3735 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3736 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3737 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3738 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3739 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3740 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3742 /* Otherwise the client must be confused wanting a delegation
3743 * it already has, therefore we don't return
3744 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3749 * called with nfs4_lock_state() held.
3752 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3754 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3755 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3756 struct nfs4_file *fp = NULL;
3757 struct nfs4_ol_stateid *stp = NULL;
3758 struct nfs4_delegation *dp = NULL;
3762 * Lookup file; if found, lookup stateid and check open request,
3763 * and check for delegations in the process of being recalled.
3764 * If not found, create the nfs4_file struct
3766 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3767 if (fp != open->op_file) {
3768 status = nfs4_check_deleg(cl, open, &dp);
3771 stp = nfsd4_find_existing_open(fp, open);
3773 open->op_file = NULL;
3774 status = nfserr_bad_stateid;
3775 if (nfsd4_is_deleg_cur(open))
3777 status = nfserr_jukebox;
3781 * OPEN the file, or upgrade an existing OPEN.
3782 * If truncate fails, the OPEN fails.
3785 /* Stateid was found, this is an OPEN upgrade */
3786 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3791 open->op_stp = NULL;
3792 init_open_stateid(stp, fp, open);
3793 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3795 release_open_stateid(stp);
3799 update_stateid(&stp->st_stid.sc_stateid);
3800 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3802 if (nfsd4_has_session(&resp->cstate)) {
3803 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3804 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3805 open->op_why_no_deleg = WND4_NOT_WANTED;
3811 * Attempt to hand out a delegation. No error return, because the
3812 * OPEN succeeds even if we fail.
3814 nfs4_open_delegation(current_fh, open, stp);
3818 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3819 STATEID_VAL(&stp->st_stid.sc_stateid));
3821 /* 4.1 client trying to upgrade/downgrade delegation? */
3822 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3823 open->op_deleg_want)
3824 nfsd4_deleg_xgrade_none_ext(open, dp);
3828 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3829 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3831 * To finish the open response, we just need to set the rflags.
3833 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3834 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3835 !nfsd4_has_session(&resp->cstate))
3836 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3838 nfs4_put_stid(&dp->dl_stid);
3843 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3845 if (open->op_openowner) {
3846 struct nfs4_openowner *oo = open->op_openowner;
3848 if (!list_empty(&oo->oo_owner.so_stateids))
3849 list_del_init(&oo->oo_close_lru);
3850 if (oo->oo_flags & NFS4_OO_NEW) {
3852 release_openowner(oo);
3853 open->op_openowner = NULL;
3855 oo->oo_flags &= ~NFS4_OO_NEW;
3859 nfsd4_free_file(open->op_file);
3861 nfs4_put_stid(&open->op_stp->st_stid);
3865 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3868 struct nfs4_client *clp;
3870 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3873 dprintk("process_renew(%08x/%08x): starting\n",
3874 clid->cl_boot, clid->cl_id);
3875 status = lookup_clientid(clid, cstate, nn);
3879 status = nfserr_cb_path_down;
3880 if (!list_empty(&clp->cl_delegations)
3881 && clp->cl_cb_state != NFSD4_CB_UP)
3885 nfs4_unlock_state();
3890 nfsd4_end_grace(struct nfsd_net *nn)
3892 /* do nothing if grace period already ended */
3893 if (nn->grace_ended)
3896 dprintk("NFSD: end of grace period\n");
3897 nn->grace_ended = true;
3898 nfsd4_record_grace_done(nn, nn->boot_time);
3899 locks_end_grace(&nn->nfsd4_manager);
3901 * Now that every NFSv4 client has had the chance to recover and
3902 * to see the (possibly new, possibly shorter) lease time, we
3903 * can safely set the next grace time to the current lease time:
3905 nn->nfsd4_grace = nn->nfsd4_lease;
3909 nfs4_laundromat(struct nfsd_net *nn)
3911 struct nfs4_client *clp;
3912 struct nfs4_openowner *oo;
3913 struct nfs4_delegation *dp;
3914 struct list_head *pos, *next, reaplist;
3915 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3916 time_t t, new_timeo = nn->nfsd4_lease;
3920 dprintk("NFSD: laundromat service - starting\n");
3921 nfsd4_end_grace(nn);
3922 INIT_LIST_HEAD(&reaplist);
3923 spin_lock(&nn->client_lock);
3924 list_for_each_safe(pos, next, &nn->client_lru) {
3925 clp = list_entry(pos, struct nfs4_client, cl_lru);
3926 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3927 t = clp->cl_time - cutoff;
3928 new_timeo = min(new_timeo, t);
3931 if (mark_client_expired_locked(clp)) {
3932 dprintk("NFSD: client in use (clientid %08x)\n",
3933 clp->cl_clientid.cl_id);
3936 list_move(&clp->cl_lru, &reaplist);
3938 spin_unlock(&nn->client_lock);
3939 list_for_each_safe(pos, next, &reaplist) {
3940 clp = list_entry(pos, struct nfs4_client, cl_lru);
3941 dprintk("NFSD: purging unused client (clientid %08x)\n",
3942 clp->cl_clientid.cl_id);
3945 spin_lock(&state_lock);
3946 list_for_each_safe(pos, next, &nn->del_recall_lru) {
3947 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3948 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3950 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3951 t = dp->dl_time - cutoff;
3952 new_timeo = min(new_timeo, t);
3955 unhash_delegation_locked(dp);
3956 list_add(&dp->dl_recall_lru, &reaplist);
3958 spin_unlock(&state_lock);
3959 while (!list_empty(&reaplist)) {
3960 dp = list_first_entry(&reaplist, struct nfs4_delegation,
3962 list_del_init(&dp->dl_recall_lru);
3963 revoke_delegation(dp);
3965 list_for_each_safe(pos, next, &nn->close_lru) {
3966 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3967 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3968 t = oo->oo_time - cutoff;
3969 new_timeo = min(new_timeo, t);
3972 release_openowner(oo);
3974 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
3975 nfs4_unlock_state();
3979 static struct workqueue_struct *laundry_wq;
3980 static void laundromat_main(struct work_struct *);
3983 laundromat_main(struct work_struct *laundry)
3986 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3988 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3991 t = nfs4_laundromat(nn);
3992 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3993 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3996 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3998 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
3999 return nfserr_bad_stateid;
4004 access_permit_read(struct nfs4_ol_stateid *stp)
4006 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4007 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4008 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4012 access_permit_write(struct nfs4_ol_stateid *stp)
4014 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4015 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4019 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4021 __be32 status = nfserr_openmode;
4023 /* For lock stateid's, we test the parent open, not the lock: */
4024 if (stp->st_openstp)
4025 stp = stp->st_openstp;
4026 if ((flags & WR_STATE) && !access_permit_write(stp))
4028 if ((flags & RD_STATE) && !access_permit_read(stp))
4035 static inline __be32
4036 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4038 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4040 else if (locks_in_grace(net)) {
4041 /* Answer in remaining cases depends on existence of
4042 * conflicting state; so we must wait out the grace period. */
4043 return nfserr_grace;
4044 } else if (flags & WR_STATE)
4045 return nfs4_share_conflict(current_fh,
4046 NFS4_SHARE_DENY_WRITE);
4047 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4048 return nfs4_share_conflict(current_fh,
4049 NFS4_SHARE_DENY_READ);
4053 * Allow READ/WRITE during grace period on recovered state only for files
4054 * that are not able to provide mandatory locking.
4057 grace_disallows_io(struct net *net, struct inode *inode)
4059 return locks_in_grace(net) && mandatory_lock(inode);
4062 /* Returns true iff a is later than b: */
4063 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4065 return (s32)(a->si_generation - b->si_generation) > 0;
4068 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4071 * When sessions are used the stateid generation number is ignored
4074 if (has_session && in->si_generation == 0)
4077 if (in->si_generation == ref->si_generation)
4080 /* If the client sends us a stateid from the future, it's buggy: */
4081 if (stateid_generation_after(in, ref))
4082 return nfserr_bad_stateid;
4084 * However, we could see a stateid from the past, even from a
4085 * non-buggy client. For example, if the client sends a lock
4086 * while some IO is outstanding, the lock may bump si_generation
4087 * while the IO is still in flight. The client could avoid that
4088 * situation by waiting for responses on all the IO requests,
4089 * but better performance may result in retrying IO that
4090 * receives an old_stateid error if requests are rarely
4091 * reordered in flight:
4093 return nfserr_old_stateid;
4096 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4098 struct nfs4_stid *s;
4099 struct nfs4_ol_stateid *ols;
4100 __be32 status = nfserr_bad_stateid;
4102 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4104 /* Client debugging aid. */
4105 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4106 char addr_str[INET6_ADDRSTRLEN];
4107 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4109 pr_warn_ratelimited("NFSD: client %s testing state ID "
4110 "with incorrect client ID\n", addr_str);
4113 spin_lock(&cl->cl_lock);
4114 s = find_stateid_locked(cl, stateid);
4117 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4120 switch (s->sc_type) {
4121 case NFS4_DELEG_STID:
4124 case NFS4_REVOKED_DELEG_STID:
4125 status = nfserr_deleg_revoked;
4127 case NFS4_OPEN_STID:
4128 case NFS4_LOCK_STID:
4129 ols = openlockstateid(s);
4130 if (ols->st_stateowner->so_is_open_owner
4131 && !(openowner(ols->st_stateowner)->oo_flags
4132 & NFS4_OO_CONFIRMED))
4133 status = nfserr_bad_stateid;
4138 printk("unknown stateid type %x\n", s->sc_type);
4140 case NFS4_CLOSED_STID:
4141 case NFS4_CLOSED_DELEG_STID:
4142 status = nfserr_bad_stateid;
4145 spin_unlock(&cl->cl_lock);
4150 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4151 stateid_t *stateid, unsigned char typemask,
4152 struct nfs4_stid **s, struct nfsd_net *nn)
4156 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4157 return nfserr_bad_stateid;
4158 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4159 if (status == nfserr_stale_clientid) {
4160 if (cstate->session)
4161 return nfserr_bad_stateid;
4162 return nfserr_stale_stateid;
4166 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4168 return nfserr_bad_stateid;
4173 * Checks for stateid operations
4176 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4177 stateid_t *stateid, int flags, struct file **filpp)
4179 struct nfs4_stid *s;
4180 struct nfs4_ol_stateid *stp = NULL;
4181 struct nfs4_delegation *dp = NULL;
4182 struct svc_fh *current_fh = &cstate->current_fh;
4183 struct inode *ino = current_fh->fh_dentry->d_inode;
4184 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4185 struct file *file = NULL;
4191 if (grace_disallows_io(net, ino))
4192 return nfserr_grace;
4194 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4195 return check_special_stateids(net, current_fh, stateid, flags);
4199 status = nfsd4_lookup_stateid(cstate, stateid,
4200 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4204 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4207 switch (s->sc_type) {
4208 case NFS4_DELEG_STID:
4209 dp = delegstateid(s);
4210 status = nfs4_check_delegmode(dp, flags);
4214 file = dp->dl_stid.sc_file->fi_deleg_file;
4217 status = nfserr_serverfault;
4223 case NFS4_OPEN_STID:
4224 case NFS4_LOCK_STID:
4225 stp = openlockstateid(s);
4226 status = nfs4_check_fh(current_fh, stp);
4229 if (stp->st_stateowner->so_is_open_owner
4230 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4232 status = nfs4_check_openmode(stp, flags);
4236 struct nfs4_file *fp = stp->st_stid.sc_file;
4238 if (flags & RD_STATE)
4239 file = find_readable_file(fp);
4241 file = find_writeable_file(fp);
4245 status = nfserr_bad_stateid;
4252 nfs4_unlock_state();
4257 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4259 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4261 if (check_for_locks(stp->st_stid.sc_file, lo))
4262 return nfserr_locks_held;
4263 release_lockowner_if_empty(lo);
4268 * Test if the stateid is valid
4271 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4272 struct nfsd4_test_stateid *test_stateid)
4274 struct nfsd4_test_stateid_id *stateid;
4275 struct nfs4_client *cl = cstate->session->se_client;
4278 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4279 stateid->ts_id_status =
4280 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4281 nfs4_unlock_state();
4287 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4288 struct nfsd4_free_stateid *free_stateid)
4290 stateid_t *stateid = &free_stateid->fr_stateid;
4291 struct nfs4_stid *s;
4292 struct nfs4_delegation *dp;
4293 struct nfs4_client *cl = cstate->session->se_client;
4294 __be32 ret = nfserr_bad_stateid;
4297 spin_lock(&cl->cl_lock);
4298 s = find_stateid_locked(cl, stateid);
4301 switch (s->sc_type) {
4302 case NFS4_DELEG_STID:
4303 ret = nfserr_locks_held;
4305 case NFS4_OPEN_STID:
4306 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4309 ret = nfserr_locks_held;
4311 case NFS4_LOCK_STID:
4312 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4315 spin_unlock(&cl->cl_lock);
4316 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4318 case NFS4_REVOKED_DELEG_STID:
4319 dp = delegstateid(s);
4320 list_del_init(&dp->dl_recall_lru);
4321 spin_unlock(&cl->cl_lock);
4325 /* Default falls through and returns nfserr_bad_stateid */
4328 spin_unlock(&cl->cl_lock);
4330 nfs4_unlock_state();
4337 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4338 RD_STATE : WR_STATE;
4341 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4343 struct svc_fh *current_fh = &cstate->current_fh;
4344 struct nfs4_stateowner *sop = stp->st_stateowner;
4347 status = nfsd4_check_seqid(cstate, sop, seqid);
4350 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4351 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4353 * "Closed" stateid's exist *only* to return
4354 * nfserr_replay_me from the previous step, and
4355 * revoked delegations are kept only for free_stateid.
4357 return nfserr_bad_stateid;
4358 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4361 return nfs4_check_fh(current_fh, stp);
4365 * Checks for sequence id mutating operations.
4368 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4369 stateid_t *stateid, char typemask,
4370 struct nfs4_ol_stateid **stpp,
4371 struct nfsd_net *nn)
4374 struct nfs4_stid *s;
4375 struct nfs4_ol_stateid *stp = NULL;
4377 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4378 seqid, STATEID_VAL(stateid));
4381 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4384 stp = openlockstateid(s);
4385 if (!nfsd4_has_session(cstate))
4386 cstate->replay_owner = stp->st_stateowner;
4388 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4394 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4395 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4398 struct nfs4_openowner *oo;
4400 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4401 NFS4_OPEN_STID, stpp, nn);
4404 oo = openowner((*stpp)->st_stateowner);
4405 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
4406 return nfserr_bad_stateid;
4411 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4412 struct nfsd4_open_confirm *oc)
4415 struct nfs4_openowner *oo;
4416 struct nfs4_ol_stateid *stp;
4417 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4419 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4420 cstate->current_fh.fh_dentry);
4422 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4428 status = nfs4_preprocess_seqid_op(cstate,
4429 oc->oc_seqid, &oc->oc_req_stateid,
4430 NFS4_OPEN_STID, &stp, nn);
4433 oo = openowner(stp->st_stateowner);
4434 status = nfserr_bad_stateid;
4435 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4437 oo->oo_flags |= NFS4_OO_CONFIRMED;
4438 update_stateid(&stp->st_stid.sc_stateid);
4439 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4440 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4441 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4443 nfsd4_client_record_create(oo->oo_owner.so_client);
4446 nfsd4_bump_seqid(cstate, status);
4447 if (!cstate->replay_owner)
4448 nfs4_unlock_state();
4452 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4454 if (!test_access(access, stp))
4456 nfs4_file_put_access(stp->st_stid.sc_file, access);
4457 clear_access(access, stp);
4460 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4462 switch (to_access) {
4463 case NFS4_SHARE_ACCESS_READ:
4464 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4465 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4467 case NFS4_SHARE_ACCESS_WRITE:
4468 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4469 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4471 case NFS4_SHARE_ACCESS_BOTH:
4479 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4480 struct nfsd4_compound_state *cstate,
4481 struct nfsd4_open_downgrade *od)
4484 struct nfs4_ol_stateid *stp;
4485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4487 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4488 cstate->current_fh.fh_dentry);
4490 /* We don't yet support WANT bits: */
4491 if (od->od_deleg_want)
4492 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4496 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4497 &od->od_stateid, &stp, nn);
4500 status = nfserr_inval;
4501 if (!test_access(od->od_share_access, stp)) {
4502 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4503 stp->st_access_bmap, od->od_share_access);
4506 if (!test_deny(od->od_share_deny, stp)) {
4507 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4508 stp->st_deny_bmap, od->od_share_deny);
4511 nfs4_stateid_downgrade(stp, od->od_share_access);
4513 reset_union_bmap_deny(od->od_share_deny, stp);
4515 update_stateid(&stp->st_stid.sc_stateid);
4516 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4519 nfsd4_bump_seqid(cstate, status);
4520 if (!cstate->replay_owner)
4521 nfs4_unlock_state();
4525 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4527 struct nfs4_client *clp = s->st_stid.sc_client;
4528 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4530 s->st_stid.sc_type = NFS4_CLOSED_STID;
4531 unhash_open_stateid(s);
4533 if (clp->cl_minorversion) {
4534 if (list_empty(&oo->oo_owner.so_stateids))
4535 release_openowner(oo);
4536 nfs4_put_stid(&s->st_stid);
4539 * In the 4.0 case we need to keep the owners around a
4540 * little while to handle CLOSE replay. We still do need
4541 * to release any file access that is held by them
4542 * before returning however.
4544 release_all_access(s);
4545 if (s->st_stid.sc_file) {
4546 put_nfs4_file(s->st_stid.sc_file);
4547 s->st_stid.sc_file = NULL;
4549 oo->oo_last_closed_stid = s;
4550 if (list_empty(&oo->oo_owner.so_stateids))
4551 move_to_close_lru(oo, clp->net);
4556 * nfs4_unlock_state() called after encode
4559 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4560 struct nfsd4_close *close)
4563 struct nfs4_ol_stateid *stp;
4564 struct net *net = SVC_NET(rqstp);
4565 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4567 dprintk("NFSD: nfsd4_close on file %pd\n",
4568 cstate->current_fh.fh_dentry);
4571 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4573 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4575 nfsd4_bump_seqid(cstate, status);
4578 update_stateid(&stp->st_stid.sc_stateid);
4579 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4581 nfsd4_close_open_stateid(stp);
4583 if (!cstate->replay_owner)
4584 nfs4_unlock_state();
4589 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4590 struct nfsd4_delegreturn *dr)
4592 struct nfs4_delegation *dp;
4593 stateid_t *stateid = &dr->dr_stateid;
4594 struct nfs4_stid *s;
4596 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4598 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4602 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4605 dp = delegstateid(s);
4606 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4610 destroy_delegation(dp);
4612 nfs4_unlock_state();
4618 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4621 end_offset(u64 start, u64 len)
4626 return end >= start ? end: NFS4_MAX_UINT64;
4629 /* last octet in a range */
4631 last_byte_offset(u64 start, u64 len)
4637 return end > start ? end - 1: NFS4_MAX_UINT64;
4641 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4642 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4643 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4644 * locking, this prevents us from being completely protocol-compliant. The
4645 * real solution to this problem is to start using unsigned file offsets in
4646 * the VFS, but this is a very deep change!
4649 nfs4_transform_lock_offset(struct file_lock *lock)
4651 if (lock->fl_start < 0)
4652 lock->fl_start = OFFSET_MAX;
4653 if (lock->fl_end < 0)
4654 lock->fl_end = OFFSET_MAX;
4657 /* Hack!: For now, we're defining this just so we can use a pointer to it
4658 * as a unique cookie to identify our (NFSv4's) posix locks. */
4659 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4663 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4665 struct nfs4_lockowner *lo;
4667 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4668 lo = (struct nfs4_lockowner *) fl->fl_owner;
4669 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4670 lo->lo_owner.so_owner.len, GFP_KERNEL);
4671 if (!deny->ld_owner.data)
4672 /* We just don't care that much */
4674 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4675 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4678 deny->ld_owner.len = 0;
4679 deny->ld_owner.data = NULL;
4680 deny->ld_clientid.cl_boot = 0;
4681 deny->ld_clientid.cl_id = 0;
4683 deny->ld_start = fl->fl_start;
4684 deny->ld_length = NFS4_MAX_UINT64;
4685 if (fl->fl_end != NFS4_MAX_UINT64)
4686 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4687 deny->ld_type = NFS4_READ_LT;
4688 if (fl->fl_type != F_RDLCK)
4689 deny->ld_type = NFS4_WRITE_LT;
4692 static struct nfs4_lockowner *
4693 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4694 struct nfsd_net *nn)
4696 unsigned int strhashval = ownerstr_hashval(clid->cl_id, owner);
4697 struct nfs4_stateowner *so;
4699 list_for_each_entry(so, &nn->ownerstr_hashtbl[strhashval], so_strhash) {
4700 if (so->so_is_open_owner)
4702 if (!same_owner_str(so, owner, clid))
4704 return lockowner(so);
4710 * Alloc a lock owner structure.
4711 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4714 * strhashval = ownerstr_hashval
4716 static struct nfs4_lockowner *
4717 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4718 struct nfs4_lockowner *lo;
4719 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4721 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4724 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4725 lo->lo_owner.so_is_open_owner = 0;
4726 /* It is the openowner seqid that will be incremented in encode in the
4727 * case of new lockowners; so increment the lock seqid manually: */
4728 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4729 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4734 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4735 struct nfs4_file *fp, struct inode *inode,
4736 struct nfs4_ol_stateid *open_stp)
4738 struct nfs4_client *clp = lo->lo_owner.so_client;
4740 lockdep_assert_held(&clp->cl_lock);
4742 atomic_inc(&stp->st_stid.sc_count);
4743 stp->st_stid.sc_type = NFS4_LOCK_STID;
4744 stp->st_stateowner = &lo->lo_owner;
4746 stp->st_stid.sc_file = fp;
4747 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4748 stp->st_access_bmap = 0;
4749 stp->st_deny_bmap = open_stp->st_deny_bmap;
4750 stp->st_openstp = open_stp;
4751 list_add(&stp->st_locks, &open_stp->st_locks);
4752 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4753 spin_lock(&fp->fi_lock);
4754 list_add(&stp->st_perfile, &fp->fi_stateids);
4755 spin_unlock(&fp->fi_lock);
4758 static struct nfs4_ol_stateid *
4759 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4761 struct nfs4_ol_stateid *lst;
4762 struct nfs4_client *clp = lo->lo_owner.so_client;
4764 lockdep_assert_held(&clp->cl_lock);
4766 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4767 if (lst->st_stid.sc_file == fp) {
4768 atomic_inc(&lst->st_stid.sc_count);
4775 static struct nfs4_ol_stateid *
4776 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4777 struct inode *inode, struct nfs4_ol_stateid *ost,
4780 struct nfs4_stid *ns = NULL;
4781 struct nfs4_ol_stateid *lst;
4782 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4783 struct nfs4_client *clp = oo->oo_owner.so_client;
4785 spin_lock(&clp->cl_lock);
4786 lst = find_lock_stateid(lo, fi);
4788 spin_unlock(&clp->cl_lock);
4789 ns = nfs4_alloc_stid(clp, stateid_slab);
4793 spin_lock(&clp->cl_lock);
4794 lst = find_lock_stateid(lo, fi);
4796 lst = openlockstateid(ns);
4797 init_lock_stateid(lst, lo, fi, inode, ost);
4802 spin_unlock(&clp->cl_lock);
4809 check_lock_length(u64 offset, u64 length)
4811 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4812 LOFF_OVERFLOW(offset, length)));
4815 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4817 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4819 lockdep_assert_held(&fp->fi_lock);
4821 if (test_access(access, lock_stp))
4823 __nfs4_file_get_access(fp, access);
4824 set_access(access, lock_stp);
4828 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4829 struct nfs4_ol_stateid *ost,
4830 struct nfsd4_lock *lock,
4831 struct nfs4_ol_stateid **lst, bool *new)
4833 struct nfs4_file *fi = ost->st_stid.sc_file;
4834 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4835 struct nfs4_client *cl = oo->oo_owner.so_client;
4836 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
4837 struct nfs4_lockowner *lo;
4838 unsigned int strhashval;
4839 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4841 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, nn);
4843 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4844 &lock->v.new.owner);
4845 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4847 return nfserr_jukebox;
4849 /* with an existing lockowner, seqids must be the same */
4850 if (!cstate->minorversion &&
4851 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
4852 return nfserr_bad_seqid;
4855 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
4857 release_lockowner_if_empty(lo);
4858 return nfserr_jukebox;
4867 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4868 struct nfsd4_lock *lock)
4870 struct nfs4_openowner *open_sop = NULL;
4871 struct nfs4_lockowner *lock_sop = NULL;
4872 struct nfs4_ol_stateid *lock_stp = NULL;
4873 struct nfs4_file *fp;
4874 struct file *filp = NULL;
4875 struct file_lock *file_lock = NULL;
4876 struct file_lock *conflock = NULL;
4878 bool new_state = false;
4881 struct net *net = SVC_NET(rqstp);
4882 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4884 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4885 (long long) lock->lk_offset,
4886 (long long) lock->lk_length);
4888 if (check_lock_length(lock->lk_offset, lock->lk_length))
4889 return nfserr_inval;
4891 if ((status = fh_verify(rqstp, &cstate->current_fh,
4892 S_IFREG, NFSD_MAY_LOCK))) {
4893 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4899 if (lock->lk_is_new) {
4900 struct nfs4_ol_stateid *open_stp = NULL;
4902 if (nfsd4_has_session(cstate))
4903 /* See rfc 5661 18.10.3: given clientid is ignored: */
4904 memcpy(&lock->v.new.clientid,
4905 &cstate->session->se_client->cl_clientid,
4906 sizeof(clientid_t));
4908 status = nfserr_stale_clientid;
4909 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4912 /* validate and update open stateid and open seqid */
4913 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4914 lock->lk_new_open_seqid,
4915 &lock->lk_new_open_stateid,
4919 open_sop = openowner(open_stp->st_stateowner);
4920 status = nfserr_bad_stateid;
4921 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4922 &lock->v.new.clientid))
4924 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4925 &lock_stp, &new_state);
4927 status = nfs4_preprocess_seqid_op(cstate,
4928 lock->lk_old_lock_seqid,
4929 &lock->lk_old_lock_stateid,
4930 NFS4_LOCK_STID, &lock_stp, nn);
4931 /* FIXME: move into nfs4_preprocess_seqid_op */
4933 atomic_inc(&lock_stp->st_stid.sc_count);
4937 lock_sop = lockowner(lock_stp->st_stateowner);
4939 lkflg = setlkflg(lock->lk_type);
4940 status = nfs4_check_openmode(lock_stp, lkflg);
4944 status = nfserr_grace;
4945 if (locks_in_grace(net) && !lock->lk_reclaim)
4947 status = nfserr_no_grace;
4948 if (!locks_in_grace(net) && lock->lk_reclaim)
4951 file_lock = locks_alloc_lock();
4953 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4954 status = nfserr_jukebox;
4958 fp = lock_stp->st_stid.sc_file;
4959 locks_init_lock(file_lock);
4960 switch (lock->lk_type) {
4963 spin_lock(&fp->fi_lock);
4964 filp = find_readable_file_locked(fp);
4966 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4967 spin_unlock(&fp->fi_lock);
4968 file_lock->fl_type = F_RDLCK;
4971 case NFS4_WRITEW_LT:
4972 spin_lock(&fp->fi_lock);
4973 filp = find_writeable_file_locked(fp);
4975 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4976 spin_unlock(&fp->fi_lock);
4977 file_lock->fl_type = F_WRLCK;
4980 status = nfserr_inval;
4984 status = nfserr_openmode;
4987 file_lock->fl_owner = (fl_owner_t)lock_sop;
4988 file_lock->fl_pid = current->tgid;
4989 file_lock->fl_file = filp;
4990 file_lock->fl_flags = FL_POSIX;
4991 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4992 file_lock->fl_start = lock->lk_offset;
4993 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4994 nfs4_transform_lock_offset(file_lock);
4996 conflock = locks_alloc_lock();
4998 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4999 status = nfserr_jukebox;
5003 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5005 case 0: /* success! */
5006 update_stateid(&lock_stp->st_stid.sc_stateid);
5007 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5011 case (EAGAIN): /* conflock holds conflicting lock */
5012 status = nfserr_denied;
5013 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5014 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5017 status = nfserr_deadlock;
5020 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5021 status = nfserrno(err);
5028 nfs4_put_stid(&lock_stp->st_stid);
5029 if (status && new_state)
5030 release_lock_stateid(lock_stp);
5031 nfsd4_bump_seqid(cstate, status);
5032 if (!cstate->replay_owner)
5033 nfs4_unlock_state();
5035 locks_free_lock(file_lock);
5037 locks_free_lock(conflock);
5042 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5043 * so we do a temporary open here just to get an open file to pass to
5044 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5047 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5050 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5052 err = nfserrno(vfs_test_lock(file, lock));
5062 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5063 struct nfsd4_lockt *lockt)
5065 struct file_lock *file_lock = NULL;
5066 struct nfs4_lockowner *lo;
5068 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5070 if (locks_in_grace(SVC_NET(rqstp)))
5071 return nfserr_grace;
5073 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5074 return nfserr_inval;
5078 if (!nfsd4_has_session(cstate)) {
5079 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5084 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5087 file_lock = locks_alloc_lock();
5089 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5090 status = nfserr_jukebox;
5093 locks_init_lock(file_lock);
5094 switch (lockt->lt_type) {
5097 file_lock->fl_type = F_RDLCK;
5100 case NFS4_WRITEW_LT:
5101 file_lock->fl_type = F_WRLCK;
5104 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5105 status = nfserr_inval;
5109 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner, nn);
5111 file_lock->fl_owner = (fl_owner_t)lo;
5112 file_lock->fl_pid = current->tgid;
5113 file_lock->fl_flags = FL_POSIX;
5115 file_lock->fl_start = lockt->lt_offset;
5116 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5118 nfs4_transform_lock_offset(file_lock);
5120 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5124 if (file_lock->fl_type != F_UNLCK) {
5125 status = nfserr_denied;
5126 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5129 nfs4_unlock_state();
5131 locks_free_lock(file_lock);
5136 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5137 struct nfsd4_locku *locku)
5139 struct nfs4_ol_stateid *stp;
5140 struct file *filp = NULL;
5141 struct file_lock *file_lock = NULL;
5144 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5146 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5147 (long long) locku->lu_offset,
5148 (long long) locku->lu_length);
5150 if (check_lock_length(locku->lu_offset, locku->lu_length))
5151 return nfserr_inval;
5155 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5156 &locku->lu_stateid, NFS4_LOCK_STID,
5160 /* FIXME: move into nfs4_preprocess_seqid_op */
5161 atomic_inc(&stp->st_stid.sc_count);
5162 filp = find_any_file(stp->st_stid.sc_file);
5164 status = nfserr_lock_range;
5167 file_lock = locks_alloc_lock();
5169 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5170 status = nfserr_jukebox;
5173 locks_init_lock(file_lock);
5174 file_lock->fl_type = F_UNLCK;
5175 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5176 file_lock->fl_pid = current->tgid;
5177 file_lock->fl_file = filp;
5178 file_lock->fl_flags = FL_POSIX;
5179 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5180 file_lock->fl_start = locku->lu_offset;
5182 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5184 nfs4_transform_lock_offset(file_lock);
5186 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5188 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5191 update_stateid(&stp->st_stid.sc_stateid);
5192 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5196 nfs4_put_stid(&stp->st_stid);
5198 nfsd4_bump_seqid(cstate, status);
5199 if (!cstate->replay_owner)
5200 nfs4_unlock_state();
5202 locks_free_lock(file_lock);
5206 status = nfserrno(err);
5212 * true: locks held by lockowner
5213 * false: no locks held by lockowner
5216 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5218 struct file_lock **flpp;
5220 struct file *filp = find_any_file(fp);
5221 struct inode *inode;
5224 /* Any valid lock stateid should have some sort of access */
5229 inode = file_inode(filp);
5231 spin_lock(&inode->i_lock);
5232 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5233 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5238 spin_unlock(&inode->i_lock);
5244 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5245 struct nfsd4_compound_state *cstate,
5246 struct nfsd4_release_lockowner *rlockowner)
5248 clientid_t *clid = &rlockowner->rl_clientid;
5249 struct nfs4_stateowner *sop = NULL, *tmp;
5250 struct nfs4_lockowner *lo;
5251 struct nfs4_ol_stateid *stp;
5252 struct xdr_netobj *owner = &rlockowner->rl_owner;
5253 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
5255 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5257 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5258 clid->cl_boot, clid->cl_id);
5262 status = lookup_clientid(clid, cstate, nn);
5266 status = nfserr_locks_held;
5268 /* Find the matching lock stateowner */
5269 list_for_each_entry(tmp, &nn->ownerstr_hashtbl[hashval], so_strhash) {
5270 if (tmp->so_is_open_owner)
5272 if (same_owner_str(tmp, owner, clid)) {
5278 /* No matching owner found, maybe a replay? Just declare victory... */
5284 lo = lockowner(sop);
5285 /* see if there are still any locks associated with it */
5286 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5287 if (check_for_locks(stp->st_stid.sc_file, lo))
5292 release_lockowner(lo);
5294 nfs4_unlock_state();
5298 static inline struct nfs4_client_reclaim *
5301 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5305 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5307 struct nfs4_client_reclaim *crp;
5309 crp = nfsd4_find_reclaim_client(name, nn);
5310 return (crp && crp->cr_clp);
5314 * failure => all reset bets are off, nfserr_no_grace...
5316 struct nfs4_client_reclaim *
5317 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5319 unsigned int strhashval;
5320 struct nfs4_client_reclaim *crp;
5322 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5323 crp = alloc_reclaim();
5325 strhashval = clientstr_hashval(name);
5326 INIT_LIST_HEAD(&crp->cr_strhash);
5327 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5328 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5330 nn->reclaim_str_hashtbl_size++;
5336 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5338 list_del(&crp->cr_strhash);
5340 nn->reclaim_str_hashtbl_size--;
5344 nfs4_release_reclaim(struct nfsd_net *nn)
5346 struct nfs4_client_reclaim *crp = NULL;
5349 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5350 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5351 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5352 struct nfs4_client_reclaim, cr_strhash);
5353 nfs4_remove_reclaim_record(crp, nn);
5356 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5360 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5361 struct nfs4_client_reclaim *
5362 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5364 unsigned int strhashval;
5365 struct nfs4_client_reclaim *crp = NULL;
5367 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5369 strhashval = clientstr_hashval(recdir);
5370 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5371 if (same_name(crp->cr_recdir, recdir)) {
5379 * Called from OPEN. Look for clientid in reclaim list.
5382 nfs4_check_open_reclaim(clientid_t *clid,
5383 struct nfsd4_compound_state *cstate,
5384 struct nfsd_net *nn)
5388 /* find clientid in conf_id_hashtbl */
5389 status = lookup_clientid(clid, cstate, nn);
5391 return nfserr_reclaim_bad;
5393 if (nfsd4_client_record_check(cstate->clp))
5394 return nfserr_reclaim_bad;
5399 #ifdef CONFIG_NFSD_FAULT_INJECTION
5401 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5403 if (mark_client_expired(clp))
5409 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5411 char buf[INET6_ADDRSTRLEN];
5412 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5413 printk(KERN_INFO "NFS Client: %s\n", buf);
5417 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5420 char buf[INET6_ADDRSTRLEN];
5421 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5422 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5425 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5426 void (*func)(struct nfs4_ol_stateid *))
5428 struct nfs4_openowner *oop;
5429 struct nfs4_ol_stateid *stp, *st_next;
5430 struct nfs4_ol_stateid *lst, *lst_next;
5433 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5434 list_for_each_entry_safe(stp, st_next,
5435 &oop->oo_owner.so_stateids, st_perstateowner) {
5436 list_for_each_entry_safe(lst, lst_next,
5437 &stp->st_locks, st_locks) {
5449 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5451 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5454 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5456 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5457 nfsd_print_count(clp, count, "locked files");
5461 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5463 struct nfs4_openowner *oop, *next;
5466 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5476 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5478 return nfsd_foreach_client_open(clp, max, release_openowner);
5481 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5483 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5484 nfsd_print_count(clp, count, "open files");
5488 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5489 struct list_head *victims)
5491 struct nfs4_delegation *dp, *next;
5494 lockdep_assert_held(&state_lock);
5495 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5498 * It's not safe to mess with delegations that have a
5499 * non-zero dl_time. They might have already been broken
5500 * and could be processed by the laundromat outside of
5501 * the state_lock. Just leave them be.
5503 if (dp->dl_time != 0)
5506 unhash_delegation_locked(dp);
5507 list_add(&dp->dl_recall_lru, victims);
5515 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5517 struct nfs4_delegation *dp, *next;
5521 spin_lock(&state_lock);
5522 count = nfsd_find_all_delegations(clp, max, &victims);
5523 spin_unlock(&state_lock);
5525 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5526 list_del_init(&dp->dl_recall_lru);
5527 revoke_delegation(dp);
5533 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5535 struct nfs4_delegation *dp;
5539 spin_lock(&state_lock);
5540 count = nfsd_find_all_delegations(clp, max, &victims);
5541 while (!list_empty(&victims)) {
5542 dp = list_first_entry(&victims, struct nfs4_delegation,
5544 list_del_init(&dp->dl_recall_lru);
5546 nfsd_break_one_deleg(dp);
5548 spin_unlock(&state_lock);
5553 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5557 spin_lock(&state_lock);
5558 count = nfsd_find_all_delegations(clp, max, NULL);
5559 spin_unlock(&state_lock);
5561 nfsd_print_count(clp, count, "delegations");
5565 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5567 struct nfs4_client *clp, *next;
5569 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5571 if (!nfsd_netns_ready(nn))
5574 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5575 count += func(clp, max - count);
5576 if ((max != 0) && (count >= max))
5583 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5585 struct nfs4_client *clp;
5586 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5588 if (!nfsd_netns_ready(nn))
5591 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5592 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5598 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5601 * Since the lifetime of a delegation isn't limited to that of an open, a
5602 * client may quite reasonably hang on to a delegation as long as it has
5603 * the inode cached. This becomes an obvious problem the first time a
5604 * client's inode cache approaches the size of the server's total memory.
5606 * For now we avoid this problem by imposing a hard limit on the number
5607 * of delegations, which varies according to the server's memory size.
5610 set_max_delegations(void)
5613 * Allow at most 4 delegations per megabyte of RAM. Quick
5614 * estimates suggest that in the worst case (where every delegation
5615 * is for a different inode), a delegation could take about 1.5K,
5616 * giving a worst case usage of about 6% of memory.
5618 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5621 static int nfs4_state_create_net(struct net *net)
5623 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5626 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5627 CLIENT_HASH_SIZE, GFP_KERNEL);
5628 if (!nn->conf_id_hashtbl)
5630 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5631 CLIENT_HASH_SIZE, GFP_KERNEL);
5632 if (!nn->unconf_id_hashtbl)
5634 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
5635 OWNER_HASH_SIZE, GFP_KERNEL);
5636 if (!nn->ownerstr_hashtbl)
5638 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5639 SESSION_HASH_SIZE, GFP_KERNEL);
5640 if (!nn->sessionid_hashtbl)
5643 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5644 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5645 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5647 for (i = 0; i < OWNER_HASH_SIZE; i++)
5648 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
5649 for (i = 0; i < SESSION_HASH_SIZE; i++)
5650 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5651 nn->conf_name_tree = RB_ROOT;
5652 nn->unconf_name_tree = RB_ROOT;
5653 INIT_LIST_HEAD(&nn->client_lru);
5654 INIT_LIST_HEAD(&nn->close_lru);
5655 INIT_LIST_HEAD(&nn->del_recall_lru);
5656 spin_lock_init(&nn->client_lock);
5658 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5664 kfree(nn->ownerstr_hashtbl);
5666 kfree(nn->unconf_id_hashtbl);
5668 kfree(nn->conf_id_hashtbl);
5674 nfs4_state_destroy_net(struct net *net)
5677 struct nfs4_client *clp = NULL;
5678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5680 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5681 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5682 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5683 destroy_client(clp);
5687 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5688 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5689 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5690 destroy_client(clp);
5694 kfree(nn->sessionid_hashtbl);
5695 kfree(nn->ownerstr_hashtbl);
5696 kfree(nn->unconf_id_hashtbl);
5697 kfree(nn->conf_id_hashtbl);
5702 nfs4_state_start_net(struct net *net)
5704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5707 ret = nfs4_state_create_net(net);
5710 nfsd4_client_tracking_init(net);
5711 nn->boot_time = get_seconds();
5712 locks_start_grace(net, &nn->nfsd4_manager);
5713 nn->grace_ended = false;
5714 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5715 nn->nfsd4_grace, net);
5716 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5720 /* initialization to perform when the nfsd service is started: */
5723 nfs4_state_start(void)
5727 ret = set_callback_cred();
5730 laundry_wq = create_singlethread_workqueue("nfsd4");
5731 if (laundry_wq == NULL) {
5735 ret = nfsd4_create_callback_queue();
5737 goto out_free_laundry;
5739 set_max_delegations();
5744 destroy_workqueue(laundry_wq);
5750 nfs4_state_shutdown_net(struct net *net)
5752 struct nfs4_delegation *dp = NULL;
5753 struct list_head *pos, *next, reaplist;
5754 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5756 cancel_delayed_work_sync(&nn->laundromat_work);
5757 locks_end_grace(&nn->nfsd4_manager);
5760 INIT_LIST_HEAD(&reaplist);
5761 spin_lock(&state_lock);
5762 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5763 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5764 unhash_delegation_locked(dp);
5765 list_add(&dp->dl_recall_lru, &reaplist);
5767 spin_unlock(&state_lock);
5768 list_for_each_safe(pos, next, &reaplist) {
5769 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5770 list_del_init(&dp->dl_recall_lru);
5771 nfs4_put_stid(&dp->dl_stid);
5774 nfsd4_client_tracking_exit(net);
5775 nfs4_state_destroy_net(net);
5776 nfs4_unlock_state();
5780 nfs4_state_shutdown(void)
5782 destroy_workqueue(laundry_wq);
5783 nfsd4_destroy_callback_queue();
5787 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5789 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5790 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5794 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5796 if (cstate->minorversion) {
5797 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5798 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5803 clear_current_stateid(struct nfsd4_compound_state *cstate)
5805 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5809 * functions to set current state id
5812 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5814 put_stateid(cstate, &odp->od_stateid);
5818 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5820 put_stateid(cstate, &open->op_stateid);
5824 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5826 put_stateid(cstate, &close->cl_stateid);
5830 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5832 put_stateid(cstate, &lock->lk_resp_stateid);
5836 * functions to consume current state id
5840 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5842 get_stateid(cstate, &odp->od_stateid);
5846 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5848 get_stateid(cstate, &drp->dr_stateid);
5852 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5854 get_stateid(cstate, &fsp->fr_stateid);
5858 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5860 get_stateid(cstate, &setattr->sa_stateid);
5864 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5866 get_stateid(cstate, &close->cl_stateid);
5870 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5872 get_stateid(cstate, &locku->lu_stateid);
5876 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5878 get_stateid(cstate, &read->rd_stateid);
5882 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5884 get_stateid(cstate, &write->wr_stateid);