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);
89 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
90 * the refcount on the open stateid to drop.
92 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
94 static struct kmem_cache *openowner_slab;
95 static struct kmem_cache *lockowner_slab;
96 static struct kmem_cache *file_slab;
97 static struct kmem_cache *stateid_slab;
98 static struct kmem_cache *deleg_slab;
101 nfs4_lock_state(void)
103 mutex_lock(&client_mutex);
106 static void free_session(struct nfsd4_session *);
108 static bool is_session_dead(struct nfsd4_session *ses)
110 return ses->se_flags & NFS4_SESSION_DEAD;
113 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
115 if (atomic_read(&ses->se_ref) > ref_held_by_me)
116 return nfserr_jukebox;
117 ses->se_flags |= NFS4_SESSION_DEAD;
122 nfs4_unlock_state(void)
124 mutex_unlock(&client_mutex);
127 static bool is_client_expired(struct nfs4_client *clp)
129 return clp->cl_time == 0;
132 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
134 if (atomic_read(&clp->cl_refcount))
135 return nfserr_jukebox;
140 static __be32 get_client_locked(struct nfs4_client *clp)
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
144 lockdep_assert_held(&nn->client_lock);
146 if (is_client_expired(clp))
147 return nfserr_expired;
148 atomic_inc(&clp->cl_refcount);
152 /* must be called under the client_lock */
154 renew_client_locked(struct nfs4_client *clp)
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158 if (is_client_expired(clp)) {
160 printk("%s: client (clientid %08x/%08x) already expired\n",
162 clp->cl_clientid.cl_boot,
163 clp->cl_clientid.cl_id);
167 dprintk("renewing client (clientid %08x/%08x)\n",
168 clp->cl_clientid.cl_boot,
169 clp->cl_clientid.cl_id);
170 list_move_tail(&clp->cl_lru, &nn->client_lru);
171 clp->cl_time = get_seconds();
175 renew_client(struct nfs4_client *clp)
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179 spin_lock(&nn->client_lock);
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
184 static void put_client_renew_locked(struct nfs4_client *clp)
186 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
188 lockdep_assert_held(&nn->client_lock);
190 if (!atomic_dec_and_test(&clp->cl_refcount))
192 if (!is_client_expired(clp))
193 renew_client_locked(clp);
196 static void put_client_renew(struct nfs4_client *clp)
198 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
200 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
202 if (!is_client_expired(clp))
203 renew_client_locked(clp);
204 spin_unlock(&nn->client_lock);
207 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
211 if (is_session_dead(ses))
212 return nfserr_badsession;
213 status = get_client_locked(ses->se_client);
216 atomic_inc(&ses->se_ref);
220 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
222 struct nfs4_client *clp = ses->se_client;
223 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
225 lockdep_assert_held(&nn->client_lock);
227 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
229 put_client_renew_locked(clp);
232 static void nfsd4_put_session(struct nfsd4_session *ses)
234 struct nfs4_client *clp = ses->se_client;
235 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
237 spin_lock(&nn->client_lock);
238 nfsd4_put_session_locked(ses);
239 spin_unlock(&nn->client_lock);
243 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
245 return (sop->so_owner.len == owner->len) &&
246 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
249 static struct nfs4_openowner *
250 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
251 struct nfs4_client *clp)
253 struct nfs4_stateowner *so;
255 lockdep_assert_held(&clp->cl_lock);
257 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
259 if (!so->so_is_open_owner)
261 if (same_owner_str(so, &open->op_owner)) {
262 atomic_inc(&so->so_count);
263 return openowner(so);
269 static struct nfs4_openowner *
270 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
271 struct nfs4_client *clp)
273 struct nfs4_openowner *oo;
275 spin_lock(&clp->cl_lock);
276 oo = find_openstateowner_str_locked(hashval, open, clp);
277 spin_unlock(&clp->cl_lock);
282 opaque_hashval(const void *ptr, int nbytes)
284 unsigned char *cptr = (unsigned char *) ptr;
294 static void nfsd4_free_file(struct nfs4_file *f)
296 kmem_cache_free(file_slab, f);
300 put_nfs4_file(struct nfs4_file *fi)
302 might_lock(&state_lock);
304 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
305 hlist_del(&fi->fi_hash);
306 spin_unlock(&state_lock);
312 get_nfs4_file(struct nfs4_file *fi)
314 atomic_inc(&fi->fi_ref);
318 __nfs4_get_fd(struct nfs4_file *f, int oflag)
320 if (f->fi_fds[oflag])
321 return get_file(f->fi_fds[oflag]);
326 find_writeable_file_locked(struct nfs4_file *f)
330 lockdep_assert_held(&f->fi_lock);
332 ret = __nfs4_get_fd(f, O_WRONLY);
334 ret = __nfs4_get_fd(f, O_RDWR);
339 find_writeable_file(struct nfs4_file *f)
343 spin_lock(&f->fi_lock);
344 ret = find_writeable_file_locked(f);
345 spin_unlock(&f->fi_lock);
350 static struct file *find_readable_file_locked(struct nfs4_file *f)
354 lockdep_assert_held(&f->fi_lock);
356 ret = __nfs4_get_fd(f, O_RDONLY);
358 ret = __nfs4_get_fd(f, O_RDWR);
363 find_readable_file(struct nfs4_file *f)
367 spin_lock(&f->fi_lock);
368 ret = find_readable_file_locked(f);
369 spin_unlock(&f->fi_lock);
375 find_any_file(struct nfs4_file *f)
379 spin_lock(&f->fi_lock);
380 ret = __nfs4_get_fd(f, O_RDWR);
382 ret = __nfs4_get_fd(f, O_WRONLY);
384 ret = __nfs4_get_fd(f, O_RDONLY);
386 spin_unlock(&f->fi_lock);
390 static atomic_long_t num_delegations;
391 unsigned long max_delegations;
394 * Open owner state (share locks)
397 /* hash tables for lock and open owners */
398 #define OWNER_HASH_BITS 8
399 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
400 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
402 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
406 ret = opaque_hashval(ownername->data, ownername->len);
407 return ret & OWNER_HASH_MASK;
410 /* hash table for nfs4_file */
411 #define FILE_HASH_BITS 8
412 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
414 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
416 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
419 static unsigned int file_hashval(struct knfsd_fh *fh)
421 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
424 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
426 return fh1->fh_size == fh2->fh_size &&
427 !memcmp(fh1->fh_base.fh_pad,
432 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
435 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
437 lockdep_assert_held(&fp->fi_lock);
439 if (access & NFS4_SHARE_ACCESS_WRITE)
440 atomic_inc(&fp->fi_access[O_WRONLY]);
441 if (access & NFS4_SHARE_ACCESS_READ)
442 atomic_inc(&fp->fi_access[O_RDONLY]);
446 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
448 lockdep_assert_held(&fp->fi_lock);
450 /* Does this access mode make sense? */
451 if (access & ~NFS4_SHARE_ACCESS_BOTH)
454 /* Does it conflict with a deny mode already set? */
455 if ((access & fp->fi_share_deny) != 0)
456 return nfserr_share_denied;
458 __nfs4_file_get_access(fp, access);
462 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
464 /* Common case is that there is no deny mode. */
466 /* Does this deny mode make sense? */
467 if (deny & ~NFS4_SHARE_DENY_BOTH)
470 if ((deny & NFS4_SHARE_DENY_READ) &&
471 atomic_read(&fp->fi_access[O_RDONLY]))
472 return nfserr_share_denied;
474 if ((deny & NFS4_SHARE_DENY_WRITE) &&
475 atomic_read(&fp->fi_access[O_WRONLY]))
476 return nfserr_share_denied;
481 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
483 might_lock(&fp->fi_lock);
485 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
486 struct file *f1 = NULL;
487 struct file *f2 = NULL;
489 swap(f1, fp->fi_fds[oflag]);
490 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
491 swap(f2, fp->fi_fds[O_RDWR]);
492 spin_unlock(&fp->fi_lock);
500 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
502 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
504 if (access & NFS4_SHARE_ACCESS_WRITE)
505 __nfs4_file_put_access(fp, O_WRONLY);
506 if (access & NFS4_SHARE_ACCESS_READ)
507 __nfs4_file_put_access(fp, O_RDONLY);
510 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
511 struct kmem_cache *slab)
513 struct nfs4_stid *stid;
516 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
520 idr_preload(GFP_KERNEL);
521 spin_lock(&cl->cl_lock);
522 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
523 spin_unlock(&cl->cl_lock);
527 stid->sc_client = cl;
528 stid->sc_stateid.si_opaque.so_id = new_id;
529 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
530 /* Will be incremented before return to client: */
531 atomic_set(&stid->sc_count, 1);
534 * It shouldn't be a problem to reuse an opaque stateid value.
535 * I don't think it is for 4.1. But with 4.0 I worry that, for
536 * example, a stray write retransmission could be accepted by
537 * the server when it should have been rejected. Therefore,
538 * adopt a trick from the sctp code to attempt to maximize the
539 * amount of time until an id is reused, by ensuring they always
540 * "increase" (mod INT_MAX):
544 kmem_cache_free(slab, stid);
548 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
550 struct nfs4_stid *stid;
551 struct nfs4_ol_stateid *stp;
553 stid = nfs4_alloc_stid(clp, stateid_slab);
557 stp = openlockstateid(stid);
558 stp->st_stid.sc_free = nfs4_free_ol_stateid;
562 static void nfs4_free_deleg(struct nfs4_stid *stid)
564 kmem_cache_free(deleg_slab, stid);
565 atomic_long_dec(&num_delegations);
569 * When we recall a delegation, we should be careful not to hand it
570 * out again straight away.
571 * To ensure this we keep a pair of bloom filters ('new' and 'old')
572 * in which the filehandles of recalled delegations are "stored".
573 * If a filehandle appear in either filter, a delegation is blocked.
574 * When a delegation is recalled, the filehandle is stored in the "new"
576 * Every 30 seconds we swap the filters and clear the "new" one,
577 * unless both are empty of course.
579 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
580 * low 3 bytes as hash-table indices.
582 * 'blocked_delegations_lock', which is always taken in block_delegations(),
583 * is used to manage concurrent access. Testing does not need the lock
584 * except when swapping the two filters.
586 static DEFINE_SPINLOCK(blocked_delegations_lock);
587 static struct bloom_pair {
588 int entries, old_entries;
590 int new; /* index into 'set' */
591 DECLARE_BITMAP(set[2], 256);
592 } blocked_delegations;
594 static int delegation_blocked(struct knfsd_fh *fh)
597 struct bloom_pair *bd = &blocked_delegations;
599 if (bd->entries == 0)
601 if (seconds_since_boot() - bd->swap_time > 30) {
602 spin_lock(&blocked_delegations_lock);
603 if (seconds_since_boot() - bd->swap_time > 30) {
604 bd->entries -= bd->old_entries;
605 bd->old_entries = bd->entries;
606 memset(bd->set[bd->new], 0,
609 bd->swap_time = seconds_since_boot();
611 spin_unlock(&blocked_delegations_lock);
613 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
614 if (test_bit(hash&255, bd->set[0]) &&
615 test_bit((hash>>8)&255, bd->set[0]) &&
616 test_bit((hash>>16)&255, bd->set[0]))
619 if (test_bit(hash&255, bd->set[1]) &&
620 test_bit((hash>>8)&255, bd->set[1]) &&
621 test_bit((hash>>16)&255, bd->set[1]))
627 static void block_delegations(struct knfsd_fh *fh)
630 struct bloom_pair *bd = &blocked_delegations;
632 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
634 spin_lock(&blocked_delegations_lock);
635 __set_bit(hash&255, bd->set[bd->new]);
636 __set_bit((hash>>8)&255, bd->set[bd->new]);
637 __set_bit((hash>>16)&255, bd->set[bd->new]);
638 if (bd->entries == 0)
639 bd->swap_time = seconds_since_boot();
641 spin_unlock(&blocked_delegations_lock);
644 static struct nfs4_delegation *
645 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
647 struct nfs4_delegation *dp;
650 dprintk("NFSD alloc_init_deleg\n");
651 n = atomic_long_inc_return(&num_delegations);
652 if (n < 0 || n > max_delegations)
654 if (delegation_blocked(¤t_fh->fh_handle))
656 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
660 dp->dl_stid.sc_free = nfs4_free_deleg;
662 * delegation seqid's are never incremented. The 4.1 special
663 * meaning of seqid 0 isn't meaningful, really, but let's avoid
664 * 0 anyway just for consistency and use 1:
666 dp->dl_stid.sc_stateid.si_generation = 1;
667 INIT_LIST_HEAD(&dp->dl_perfile);
668 INIT_LIST_HEAD(&dp->dl_perclnt);
669 INIT_LIST_HEAD(&dp->dl_recall_lru);
670 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
671 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
674 atomic_long_dec(&num_delegations);
679 nfs4_put_stid(struct nfs4_stid *s)
681 struct nfs4_file *fp = s->sc_file;
682 struct nfs4_client *clp = s->sc_client;
684 might_lock(&clp->cl_lock);
686 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
687 wake_up_all(&close_wq);
690 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
691 spin_unlock(&clp->cl_lock);
697 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
699 lockdep_assert_held(&state_lock);
703 if (atomic_dec_and_test(&fp->fi_delegees)) {
704 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
706 fput(fp->fi_deleg_file);
707 fp->fi_deleg_file = NULL;
711 static void unhash_stid(struct nfs4_stid *s)
717 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
719 lockdep_assert_held(&state_lock);
720 lockdep_assert_held(&fp->fi_lock);
722 atomic_inc(&dp->dl_stid.sc_count);
723 dp->dl_stid.sc_type = NFS4_DELEG_STID;
724 list_add(&dp->dl_perfile, &fp->fi_delegations);
725 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
729 unhash_delegation_locked(struct nfs4_delegation *dp)
731 struct nfs4_file *fp = dp->dl_stid.sc_file;
733 lockdep_assert_held(&state_lock);
735 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
736 /* Ensure that deleg break won't try to requeue it */
738 spin_lock(&fp->fi_lock);
739 list_del_init(&dp->dl_perclnt);
740 list_del_init(&dp->dl_recall_lru);
741 list_del_init(&dp->dl_perfile);
742 spin_unlock(&fp->fi_lock);
744 nfs4_put_deleg_lease(fp);
747 static void destroy_delegation(struct nfs4_delegation *dp)
749 spin_lock(&state_lock);
750 unhash_delegation_locked(dp);
751 spin_unlock(&state_lock);
752 nfs4_put_stid(&dp->dl_stid);
755 static void revoke_delegation(struct nfs4_delegation *dp)
757 struct nfs4_client *clp = dp->dl_stid.sc_client;
759 WARN_ON(!list_empty(&dp->dl_recall_lru));
761 if (clp->cl_minorversion == 0)
762 nfs4_put_stid(&dp->dl_stid);
764 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
765 spin_lock(&clp->cl_lock);
766 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
767 spin_unlock(&clp->cl_lock);
775 static unsigned int clientid_hashval(u32 id)
777 return id & CLIENT_HASH_MASK;
780 static unsigned int clientstr_hashval(const char *name)
782 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
786 * We store the NONE, READ, WRITE, and BOTH bits separately in the
787 * st_{access,deny}_bmap field of the stateid, in order to track not
788 * only what share bits are currently in force, but also what
789 * combinations of share bits previous opens have used. This allows us
790 * to enforce the recommendation of rfc 3530 14.2.19 that the server
791 * return an error if the client attempt to downgrade to a combination
792 * of share bits not explicable by closing some of its previous opens.
794 * XXX: This enforcement is actually incomplete, since we don't keep
795 * track of access/deny bit combinations; so, e.g., we allow:
797 * OPEN allow read, deny write
798 * OPEN allow both, deny none
799 * DOWNGRADE allow read, deny none
801 * which we should reject.
804 bmap_to_share_mode(unsigned long bmap) {
806 unsigned int access = 0;
808 for (i = 1; i < 4; i++) {
809 if (test_bit(i, &bmap))
815 /* set share access for a given stateid */
817 set_access(u32 access, struct nfs4_ol_stateid *stp)
819 unsigned char mask = 1 << access;
821 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
822 stp->st_access_bmap |= mask;
825 /* clear share access for a given stateid */
827 clear_access(u32 access, struct nfs4_ol_stateid *stp)
829 unsigned char mask = 1 << access;
831 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
832 stp->st_access_bmap &= ~mask;
835 /* test whether a given stateid has access */
837 test_access(u32 access, struct nfs4_ol_stateid *stp)
839 unsigned char mask = 1 << access;
841 return (bool)(stp->st_access_bmap & mask);
844 /* set share deny for a given stateid */
846 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
848 unsigned char mask = 1 << deny;
850 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
851 stp->st_deny_bmap |= mask;
854 /* clear share deny for a given stateid */
856 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
858 unsigned char mask = 1 << deny;
860 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
861 stp->st_deny_bmap &= ~mask;
864 /* test whether a given stateid is denying specific access */
866 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
868 unsigned char mask = 1 << deny;
870 return (bool)(stp->st_deny_bmap & mask);
873 static int nfs4_access_to_omode(u32 access)
875 switch (access & NFS4_SHARE_ACCESS_BOTH) {
876 case NFS4_SHARE_ACCESS_READ:
878 case NFS4_SHARE_ACCESS_WRITE:
880 case NFS4_SHARE_ACCESS_BOTH:
888 * A stateid that had a deny mode associated with it is being released
889 * or downgraded. Recalculate the deny mode on the file.
892 recalculate_deny_mode(struct nfs4_file *fp)
894 struct nfs4_ol_stateid *stp;
896 spin_lock(&fp->fi_lock);
897 fp->fi_share_deny = 0;
898 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
899 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
900 spin_unlock(&fp->fi_lock);
904 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
909 for (i = 1; i < 4; i++) {
910 if ((i & deny) != i) {
916 /* Recalculate per-file deny mode if there was a change */
918 recalculate_deny_mode(stp->st_stid.sc_file);
921 /* release all access and file references for a given stateid */
923 release_all_access(struct nfs4_ol_stateid *stp)
926 struct nfs4_file *fp = stp->st_stid.sc_file;
928 if (fp && stp->st_deny_bmap != 0)
929 recalculate_deny_mode(fp);
931 for (i = 1; i < 4; i++) {
932 if (test_access(i, stp))
933 nfs4_file_put_access(stp->st_stid.sc_file, i);
934 clear_access(i, stp);
938 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
940 struct nfs4_client *clp = sop->so_client;
942 might_lock(&clp->cl_lock);
944 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
946 sop->so_ops->so_unhash(sop);
947 spin_unlock(&clp->cl_lock);
948 kfree(sop->so_owner.data);
949 sop->so_ops->so_free(sop);
952 static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
954 struct nfs4_file *fp = stp->st_stid.sc_file;
956 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
958 spin_lock(&fp->fi_lock);
959 list_del(&stp->st_perfile);
960 spin_unlock(&fp->fi_lock);
961 list_del(&stp->st_perstateowner);
964 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
966 struct nfs4_ol_stateid *stp = openlockstateid(stid);
968 release_all_access(stp);
969 if (stp->st_stateowner)
970 nfs4_put_stateowner(stp->st_stateowner);
971 kmem_cache_free(stateid_slab, stid);
974 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
976 struct nfs4_ol_stateid *stp = openlockstateid(stid);
977 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
980 file = find_any_file(stp->st_stid.sc_file);
982 filp_close(file, (fl_owner_t)lo);
983 nfs4_free_ol_stateid(stid);
987 * Put the persistent reference to an already unhashed generic stateid, while
988 * holding the cl_lock. If it's the last reference, then put it onto the
989 * reaplist for later destruction.
991 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
992 struct list_head *reaplist)
994 struct nfs4_stid *s = &stp->st_stid;
995 struct nfs4_client *clp = s->sc_client;
997 lockdep_assert_held(&clp->cl_lock);
999 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1001 if (!atomic_dec_and_test(&s->sc_count)) {
1002 wake_up_all(&close_wq);
1006 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1007 list_add(&stp->st_locks, reaplist);
1010 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1012 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1014 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1016 list_del_init(&stp->st_locks);
1017 unhash_ol_stateid(stp);
1018 unhash_stid(&stp->st_stid);
1021 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1023 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1025 spin_lock(&oo->oo_owner.so_client->cl_lock);
1026 unhash_lock_stateid(stp);
1027 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1028 nfs4_put_stid(&stp->st_stid);
1031 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1033 struct nfs4_client *clp = lo->lo_owner.so_client;
1035 lockdep_assert_held(&clp->cl_lock);
1037 list_del_init(&lo->lo_owner.so_strhash);
1041 * Free a list of generic stateids that were collected earlier after being
1045 free_ol_stateid_reaplist(struct list_head *reaplist)
1047 struct nfs4_ol_stateid *stp;
1051 while (!list_empty(reaplist)) {
1052 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1054 list_del(&stp->st_locks);
1055 stp->st_stid.sc_free(&stp->st_stid);
1059 static void release_lockowner(struct nfs4_lockowner *lo)
1061 struct nfs4_client *clp = lo->lo_owner.so_client;
1062 struct nfs4_ol_stateid *stp;
1063 struct list_head reaplist;
1065 INIT_LIST_HEAD(&reaplist);
1067 spin_lock(&clp->cl_lock);
1068 unhash_lockowner_locked(lo);
1069 while (!list_empty(&lo->lo_owner.so_stateids)) {
1070 stp = list_first_entry(&lo->lo_owner.so_stateids,
1071 struct nfs4_ol_stateid, st_perstateowner);
1072 unhash_lock_stateid(stp);
1073 put_ol_stateid_locked(stp, &reaplist);
1075 spin_unlock(&clp->cl_lock);
1076 free_ol_stateid_reaplist(&reaplist);
1077 nfs4_put_stateowner(&lo->lo_owner);
1080 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1081 struct list_head *reaplist)
1083 struct nfs4_ol_stateid *stp;
1085 while (!list_empty(&open_stp->st_locks)) {
1086 stp = list_entry(open_stp->st_locks.next,
1087 struct nfs4_ol_stateid, st_locks);
1088 unhash_lock_stateid(stp);
1089 put_ol_stateid_locked(stp, reaplist);
1093 static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1094 struct list_head *reaplist)
1096 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1098 unhash_ol_stateid(stp);
1099 release_open_stateid_locks(stp, reaplist);
1102 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1104 LIST_HEAD(reaplist);
1106 spin_lock(&stp->st_stid.sc_client->cl_lock);
1107 unhash_open_stateid(stp, &reaplist);
1108 put_ol_stateid_locked(stp, &reaplist);
1109 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1110 free_ol_stateid_reaplist(&reaplist);
1113 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1115 struct nfs4_client *clp = oo->oo_owner.so_client;
1117 lockdep_assert_held(&clp->cl_lock);
1119 list_del_init(&oo->oo_owner.so_strhash);
1120 list_del_init(&oo->oo_perclient);
1123 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1125 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1128 list_del_init(&oo->oo_close_lru);
1129 oo->oo_last_closed_stid = NULL;
1130 nfs4_put_stid(&s->st_stid);
1134 static void release_openowner(struct nfs4_openowner *oo)
1136 struct nfs4_ol_stateid *stp;
1137 struct nfs4_client *clp = oo->oo_owner.so_client;
1138 struct list_head reaplist;
1140 INIT_LIST_HEAD(&reaplist);
1142 spin_lock(&clp->cl_lock);
1143 unhash_openowner_locked(oo);
1144 while (!list_empty(&oo->oo_owner.so_stateids)) {
1145 stp = list_first_entry(&oo->oo_owner.so_stateids,
1146 struct nfs4_ol_stateid, st_perstateowner);
1147 unhash_open_stateid(stp, &reaplist);
1148 put_ol_stateid_locked(stp, &reaplist);
1150 spin_unlock(&clp->cl_lock);
1151 free_ol_stateid_reaplist(&reaplist);
1152 release_last_closed_stateid(oo);
1153 nfs4_put_stateowner(&oo->oo_owner);
1157 hash_sessionid(struct nfs4_sessionid *sessionid)
1159 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1161 return sid->sequence % SESSION_HASH_SIZE;
1166 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1168 u32 *ptr = (u32 *)(&sessionid->data[0]);
1169 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1173 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1179 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1180 * won't be used for replay.
1182 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1184 struct nfs4_stateowner *so = cstate->replay_owner;
1186 if (nfserr == nfserr_replay_me)
1189 if (!seqid_mutating_err(ntohl(nfserr))) {
1190 nfsd4_cstate_clear_replay(cstate);
1195 if (so->so_is_open_owner)
1196 release_last_closed_stateid(openowner(so));
1202 gen_sessionid(struct nfsd4_session *ses)
1204 struct nfs4_client *clp = ses->se_client;
1205 struct nfsd4_sessionid *sid;
1207 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1208 sid->clientid = clp->cl_clientid;
1209 sid->sequence = current_sessionid++;
1214 * The protocol defines ca_maxresponssize_cached to include the size of
1215 * the rpc header, but all we need to cache is the data starting after
1216 * the end of the initial SEQUENCE operation--the rest we regenerate
1217 * each time. Therefore we can advertise a ca_maxresponssize_cached
1218 * value that is the number of bytes in our cache plus a few additional
1219 * bytes. In order to stay on the safe side, and not promise more than
1220 * we can cache, those additional bytes must be the minimum possible: 24
1221 * bytes of rpc header (xid through accept state, with AUTH_NULL
1222 * verifier), 12 for the compound header (with zero-length tag), and 44
1223 * for the SEQUENCE op response:
1225 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1228 free_session_slots(struct nfsd4_session *ses)
1232 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1233 kfree(ses->se_slots[i]);
1237 * We don't actually need to cache the rpc and session headers, so we
1238 * can allocate a little less for each slot:
1240 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1244 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1247 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1248 return size + sizeof(struct nfsd4_slot);
1252 * XXX: If we run out of reserved DRC memory we could (up to a point)
1253 * re-negotiate active sessions and reduce their slot usage to make
1254 * room for new connections. For now we just fail the create session.
1256 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1258 u32 slotsize = slot_bytes(ca);
1259 u32 num = ca->maxreqs;
1262 spin_lock(&nfsd_drc_lock);
1263 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1264 nfsd_drc_max_mem - nfsd_drc_mem_used);
1265 num = min_t(int, num, avail / slotsize);
1266 nfsd_drc_mem_used += num * slotsize;
1267 spin_unlock(&nfsd_drc_lock);
1272 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1274 int slotsize = slot_bytes(ca);
1276 spin_lock(&nfsd_drc_lock);
1277 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1278 spin_unlock(&nfsd_drc_lock);
1281 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1282 struct nfsd4_channel_attrs *battrs)
1284 int numslots = fattrs->maxreqs;
1285 int slotsize = slot_bytes(fattrs);
1286 struct nfsd4_session *new;
1289 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1290 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1291 mem = numslots * sizeof(struct nfsd4_slot *);
1293 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1296 /* allocate each struct nfsd4_slot and data cache in one piece */
1297 for (i = 0; i < numslots; i++) {
1298 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1299 if (!new->se_slots[i])
1303 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1304 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1309 kfree(new->se_slots[i]);
1314 static void free_conn(struct nfsd4_conn *c)
1316 svc_xprt_put(c->cn_xprt);
1320 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1322 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1323 struct nfs4_client *clp = c->cn_session->se_client;
1325 spin_lock(&clp->cl_lock);
1326 if (!list_empty(&c->cn_persession)) {
1327 list_del(&c->cn_persession);
1330 nfsd4_probe_callback(clp);
1331 spin_unlock(&clp->cl_lock);
1334 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1336 struct nfsd4_conn *conn;
1338 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1341 svc_xprt_get(rqstp->rq_xprt);
1342 conn->cn_xprt = rqstp->rq_xprt;
1343 conn->cn_flags = flags;
1344 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1348 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1350 conn->cn_session = ses;
1351 list_add(&conn->cn_persession, &ses->se_conns);
1354 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1356 struct nfs4_client *clp = ses->se_client;
1358 spin_lock(&clp->cl_lock);
1359 __nfsd4_hash_conn(conn, ses);
1360 spin_unlock(&clp->cl_lock);
1363 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1365 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1366 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1369 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1373 nfsd4_hash_conn(conn, ses);
1374 ret = nfsd4_register_conn(conn);
1376 /* oops; xprt is already down: */
1377 nfsd4_conn_lost(&conn->cn_xpt_user);
1378 /* We may have gained or lost a callback channel: */
1379 nfsd4_probe_callback_sync(ses->se_client);
1382 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1384 u32 dir = NFS4_CDFC4_FORE;
1386 if (cses->flags & SESSION4_BACK_CHAN)
1387 dir |= NFS4_CDFC4_BACK;
1388 return alloc_conn(rqstp, dir);
1391 /* must be called under client_lock */
1392 static void nfsd4_del_conns(struct nfsd4_session *s)
1394 struct nfs4_client *clp = s->se_client;
1395 struct nfsd4_conn *c;
1397 spin_lock(&clp->cl_lock);
1398 while (!list_empty(&s->se_conns)) {
1399 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1400 list_del_init(&c->cn_persession);
1401 spin_unlock(&clp->cl_lock);
1403 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1406 spin_lock(&clp->cl_lock);
1408 spin_unlock(&clp->cl_lock);
1411 static void __free_session(struct nfsd4_session *ses)
1413 free_session_slots(ses);
1417 static void free_session(struct nfsd4_session *ses)
1419 nfsd4_del_conns(ses);
1420 nfsd4_put_drc_mem(&ses->se_fchannel);
1421 __free_session(ses);
1424 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1427 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1429 new->se_client = clp;
1432 INIT_LIST_HEAD(&new->se_conns);
1434 new->se_cb_seq_nr = 1;
1435 new->se_flags = cses->flags;
1436 new->se_cb_prog = cses->callback_prog;
1437 new->se_cb_sec = cses->cb_sec;
1438 atomic_set(&new->se_ref, 0);
1439 idx = hash_sessionid(&new->se_sessionid);
1440 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1441 spin_lock(&clp->cl_lock);
1442 list_add(&new->se_perclnt, &clp->cl_sessions);
1443 spin_unlock(&clp->cl_lock);
1445 if (cses->flags & SESSION4_BACK_CHAN) {
1446 struct sockaddr *sa = svc_addr(rqstp);
1448 * This is a little silly; with sessions there's no real
1449 * use for the callback address. Use the peer address
1450 * as a reasonable default for now, but consider fixing
1451 * the rpc client not to require an address in the
1454 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1455 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1459 /* caller must hold client_lock */
1460 static struct nfsd4_session *
1461 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1463 struct nfsd4_session *elem;
1465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1467 lockdep_assert_held(&nn->client_lock);
1469 dump_sessionid(__func__, sessionid);
1470 idx = hash_sessionid(sessionid);
1471 /* Search in the appropriate list */
1472 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1473 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1474 NFS4_MAX_SESSIONID_LEN)) {
1479 dprintk("%s: session not found\n", __func__);
1483 static struct nfsd4_session *
1484 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1487 struct nfsd4_session *session;
1488 __be32 status = nfserr_badsession;
1490 session = __find_in_sessionid_hashtbl(sessionid, net);
1493 status = nfsd4_get_session_locked(session);
1501 /* caller must hold client_lock */
1503 unhash_session(struct nfsd4_session *ses)
1505 struct nfs4_client *clp = ses->se_client;
1506 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1508 lockdep_assert_held(&nn->client_lock);
1510 list_del(&ses->se_hash);
1511 spin_lock(&ses->se_client->cl_lock);
1512 list_del(&ses->se_perclnt);
1513 spin_unlock(&ses->se_client->cl_lock);
1516 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1518 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1520 if (clid->cl_boot == nn->boot_time)
1522 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1523 clid->cl_boot, clid->cl_id, nn->boot_time);
1528 * XXX Should we use a slab cache ?
1529 * This type of memory management is somewhat inefficient, but we use it
1530 * anyway since SETCLIENTID is not a common operation.
1532 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1534 struct nfs4_client *clp;
1537 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1540 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1541 if (clp->cl_name.data == NULL)
1543 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1544 OWNER_HASH_SIZE, GFP_KERNEL);
1545 if (!clp->cl_ownerstr_hashtbl)
1546 goto err_no_hashtbl;
1547 for (i = 0; i < OWNER_HASH_SIZE; i++)
1548 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1549 clp->cl_name.len = name.len;
1550 INIT_LIST_HEAD(&clp->cl_sessions);
1551 idr_init(&clp->cl_stateids);
1552 atomic_set(&clp->cl_refcount, 0);
1553 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1554 INIT_LIST_HEAD(&clp->cl_idhash);
1555 INIT_LIST_HEAD(&clp->cl_openowners);
1556 INIT_LIST_HEAD(&clp->cl_delegations);
1557 INIT_LIST_HEAD(&clp->cl_lru);
1558 INIT_LIST_HEAD(&clp->cl_callbacks);
1559 INIT_LIST_HEAD(&clp->cl_revoked);
1560 spin_lock_init(&clp->cl_lock);
1561 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1564 kfree(clp->cl_name.data);
1571 free_client(struct nfs4_client *clp)
1573 while (!list_empty(&clp->cl_sessions)) {
1574 struct nfsd4_session *ses;
1575 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1577 list_del(&ses->se_perclnt);
1578 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1581 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1582 free_svc_cred(&clp->cl_cred);
1583 kfree(clp->cl_ownerstr_hashtbl);
1584 kfree(clp->cl_name.data);
1585 idr_destroy(&clp->cl_stateids);
1589 /* must be called under the client_lock */
1591 unhash_client_locked(struct nfs4_client *clp)
1593 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1594 struct nfsd4_session *ses;
1596 lockdep_assert_held(&nn->client_lock);
1598 /* Mark the client as expired! */
1600 /* Make it invisible */
1601 if (!list_empty(&clp->cl_idhash)) {
1602 list_del_init(&clp->cl_idhash);
1603 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1604 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1606 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1608 list_del_init(&clp->cl_lru);
1609 spin_lock(&clp->cl_lock);
1610 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1611 list_del_init(&ses->se_hash);
1612 spin_unlock(&clp->cl_lock);
1616 unhash_client(struct nfs4_client *clp)
1618 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1620 spin_lock(&nn->client_lock);
1621 unhash_client_locked(clp);
1622 spin_unlock(&nn->client_lock);
1626 __destroy_client(struct nfs4_client *clp)
1628 struct nfs4_openowner *oo;
1629 struct nfs4_delegation *dp;
1630 struct list_head reaplist;
1632 INIT_LIST_HEAD(&reaplist);
1633 spin_lock(&state_lock);
1634 while (!list_empty(&clp->cl_delegations)) {
1635 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1636 unhash_delegation_locked(dp);
1637 list_add(&dp->dl_recall_lru, &reaplist);
1639 spin_unlock(&state_lock);
1640 while (!list_empty(&reaplist)) {
1641 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1642 list_del_init(&dp->dl_recall_lru);
1643 nfs4_put_stid(&dp->dl_stid);
1645 while (!list_empty(&clp->cl_revoked)) {
1646 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1647 list_del_init(&dp->dl_recall_lru);
1648 nfs4_put_stid(&dp->dl_stid);
1650 while (!list_empty(&clp->cl_openowners)) {
1651 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1652 atomic_inc(&oo->oo_owner.so_count);
1653 release_openowner(oo);
1655 nfsd4_shutdown_callback(clp);
1656 if (clp->cl_cb_conn.cb_xprt)
1657 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1662 destroy_client(struct nfs4_client *clp)
1665 __destroy_client(clp);
1668 static void expire_client(struct nfs4_client *clp)
1671 nfsd4_client_record_remove(clp);
1672 __destroy_client(clp);
1675 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1677 memcpy(target->cl_verifier.data, source->data,
1678 sizeof(target->cl_verifier.data));
1681 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1683 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1684 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1687 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1689 if (source->cr_principal) {
1690 target->cr_principal =
1691 kstrdup(source->cr_principal, GFP_KERNEL);
1692 if (target->cr_principal == NULL)
1695 target->cr_principal = NULL;
1696 target->cr_flavor = source->cr_flavor;
1697 target->cr_uid = source->cr_uid;
1698 target->cr_gid = source->cr_gid;
1699 target->cr_group_info = source->cr_group_info;
1700 get_group_info(target->cr_group_info);
1701 target->cr_gss_mech = source->cr_gss_mech;
1702 if (source->cr_gss_mech)
1703 gss_mech_get(source->cr_gss_mech);
1708 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1712 res = o1->len - o2->len;
1715 return (long long)memcmp(o1->data, o2->data, o1->len);
1718 static int same_name(const char *n1, const char *n2)
1720 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1724 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1726 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1730 same_clid(clientid_t *cl1, clientid_t *cl2)
1732 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1735 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1739 if (g1->ngroups != g2->ngroups)
1741 for (i=0; i<g1->ngroups; i++)
1742 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1748 * RFC 3530 language requires clid_inuse be returned when the
1749 * "principal" associated with a requests differs from that previously
1750 * used. We use uid, gid's, and gss principal string as our best
1751 * approximation. We also don't want to allow non-gss use of a client
1752 * established using gss: in theory cr_principal should catch that
1753 * change, but in practice cr_principal can be null even in the gss case
1754 * since gssd doesn't always pass down a principal string.
1756 static bool is_gss_cred(struct svc_cred *cr)
1758 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1759 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1764 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1766 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1767 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1768 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1769 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1771 if (cr1->cr_principal == cr2->cr_principal)
1773 if (!cr1->cr_principal || !cr2->cr_principal)
1775 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1778 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1780 struct svc_cred *cr = &rqstp->rq_cred;
1783 if (!cr->cr_gss_mech)
1785 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1786 return service == RPC_GSS_SVC_INTEGRITY ||
1787 service == RPC_GSS_SVC_PRIVACY;
1790 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1792 struct svc_cred *cr = &rqstp->rq_cred;
1794 if (!cl->cl_mach_cred)
1796 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1798 if (!svc_rqst_integrity_protected(rqstp))
1800 if (!cr->cr_principal)
1802 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1805 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1807 static u32 current_clientid = 1;
1809 clp->cl_clientid.cl_boot = nn->boot_time;
1810 clp->cl_clientid.cl_id = current_clientid++;
1813 static void gen_confirm(struct nfs4_client *clp)
1819 * This is opaque to client, so no need to byte-swap. Use
1820 * __force to keep sparse happy
1822 verf[0] = (__force __be32)get_seconds();
1823 verf[1] = (__force __be32)i++;
1824 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1827 static struct nfs4_stid *
1828 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1830 struct nfs4_stid *ret;
1832 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1833 if (!ret || !ret->sc_type)
1838 static struct nfs4_stid *
1839 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1841 struct nfs4_stid *s;
1843 spin_lock(&cl->cl_lock);
1844 s = find_stateid_locked(cl, t);
1846 if (typemask & s->sc_type)
1847 atomic_inc(&s->sc_count);
1851 spin_unlock(&cl->cl_lock);
1855 static struct nfs4_client *create_client(struct xdr_netobj name,
1856 struct svc_rqst *rqstp, nfs4_verifier *verf)
1858 struct nfs4_client *clp;
1859 struct sockaddr *sa = svc_addr(rqstp);
1861 struct net *net = SVC_NET(rqstp);
1863 clp = alloc_client(name);
1867 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1872 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1873 clp->cl_time = get_seconds();
1874 clear_bit(0, &clp->cl_cb_slot_busy);
1875 copy_verf(clp, verf);
1876 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1878 clp->cl_cb_session = NULL;
1884 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1886 struct rb_node **new = &(root->rb_node), *parent = NULL;
1887 struct nfs4_client *clp;
1890 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1893 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1894 new = &((*new)->rb_left);
1896 new = &((*new)->rb_right);
1899 rb_link_node(&new_clp->cl_namenode, parent, new);
1900 rb_insert_color(&new_clp->cl_namenode, root);
1903 static struct nfs4_client *
1904 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1907 struct rb_node *node = root->rb_node;
1908 struct nfs4_client *clp;
1911 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1912 cmp = compare_blob(&clp->cl_name, name);
1914 node = node->rb_left;
1916 node = node->rb_right;
1924 add_to_unconfirmed(struct nfs4_client *clp)
1926 unsigned int idhashval;
1927 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1929 lockdep_assert_held(&nn->client_lock);
1931 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1932 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1933 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1934 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1935 renew_client_locked(clp);
1939 move_to_confirmed(struct nfs4_client *clp)
1941 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1942 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1944 lockdep_assert_held(&nn->client_lock);
1946 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1947 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1948 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1949 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1950 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1951 renew_client_locked(clp);
1954 static struct nfs4_client *
1955 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1957 struct nfs4_client *clp;
1958 unsigned int idhashval = clientid_hashval(clid->cl_id);
1960 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1961 if (same_clid(&clp->cl_clientid, clid)) {
1962 if ((bool)clp->cl_minorversion != sessions)
1964 renew_client_locked(clp);
1971 static struct nfs4_client *
1972 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1974 struct list_head *tbl = nn->conf_id_hashtbl;
1976 lockdep_assert_held(&nn->client_lock);
1977 return find_client_in_id_table(tbl, clid, sessions);
1980 static struct nfs4_client *
1981 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1983 struct list_head *tbl = nn->unconf_id_hashtbl;
1985 lockdep_assert_held(&nn->client_lock);
1986 return find_client_in_id_table(tbl, clid, sessions);
1989 static bool clp_used_exchangeid(struct nfs4_client *clp)
1991 return clp->cl_exchange_flags != 0;
1994 static struct nfs4_client *
1995 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1997 lockdep_assert_held(&nn->client_lock);
1998 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2001 static struct nfs4_client *
2002 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2004 lockdep_assert_held(&nn->client_lock);
2005 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2009 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2011 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2012 struct sockaddr *sa = svc_addr(rqstp);
2013 u32 scopeid = rpc_get_scope_id(sa);
2014 unsigned short expected_family;
2016 /* Currently, we only support tcp and tcp6 for the callback channel */
2017 if (se->se_callback_netid_len == 3 &&
2018 !memcmp(se->se_callback_netid_val, "tcp", 3))
2019 expected_family = AF_INET;
2020 else if (se->se_callback_netid_len == 4 &&
2021 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2022 expected_family = AF_INET6;
2026 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2027 se->se_callback_addr_len,
2028 (struct sockaddr *)&conn->cb_addr,
2029 sizeof(conn->cb_addr));
2031 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2034 if (conn->cb_addr.ss_family == AF_INET6)
2035 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2037 conn->cb_prog = se->se_callback_prog;
2038 conn->cb_ident = se->se_callback_ident;
2039 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2042 conn->cb_addr.ss_family = AF_UNSPEC;
2043 conn->cb_addrlen = 0;
2044 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2045 "will not receive delegations\n",
2046 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2052 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2055 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2057 struct xdr_buf *buf = resp->xdr.buf;
2058 struct nfsd4_slot *slot = resp->cstate.slot;
2061 dprintk("--> %s slot %p\n", __func__, slot);
2063 slot->sl_opcnt = resp->opcnt;
2064 slot->sl_status = resp->cstate.status;
2066 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2067 if (nfsd4_not_cached(resp)) {
2068 slot->sl_datalen = 0;
2071 base = resp->cstate.data_offset;
2072 slot->sl_datalen = buf->len - base;
2073 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2074 WARN("%s: sessions DRC could not cache compound\n", __func__);
2079 * Encode the replay sequence operation from the slot values.
2080 * If cachethis is FALSE encode the uncached rep error on the next
2081 * operation which sets resp->p and increments resp->opcnt for
2082 * nfs4svc_encode_compoundres.
2086 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2087 struct nfsd4_compoundres *resp)
2089 struct nfsd4_op *op;
2090 struct nfsd4_slot *slot = resp->cstate.slot;
2092 /* Encode the replayed sequence operation */
2093 op = &args->ops[resp->opcnt - 1];
2094 nfsd4_encode_operation(resp, op);
2096 /* Return nfserr_retry_uncached_rep in next operation. */
2097 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2098 op = &args->ops[resp->opcnt++];
2099 op->status = nfserr_retry_uncached_rep;
2100 nfsd4_encode_operation(resp, op);
2106 * The sequence operation is not cached because we can use the slot and
2110 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2111 struct nfsd4_sequence *seq)
2113 struct nfsd4_slot *slot = resp->cstate.slot;
2114 struct xdr_stream *xdr = &resp->xdr;
2118 dprintk("--> %s slot %p\n", __func__, slot);
2120 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2124 p = xdr_reserve_space(xdr, slot->sl_datalen);
2127 return nfserr_serverfault;
2129 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2130 xdr_commit_encode(xdr);
2132 resp->opcnt = slot->sl_opcnt;
2133 return slot->sl_status;
2137 * Set the exchange_id flags returned by the server.
2140 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2142 /* pNFS is not supported */
2143 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2145 /* Referrals are supported, Migration is not. */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2148 /* set the wire flags to return to client. */
2149 clid->flags = new->cl_exchange_flags;
2152 static bool client_has_state(struct nfs4_client *clp)
2155 * Note clp->cl_openowners check isn't quite right: there's no
2156 * need to count owners without stateid's.
2158 * Also note we should probably be using this in 4.0 case too.
2160 return !list_empty(&clp->cl_openowners)
2161 || !list_empty(&clp->cl_delegations)
2162 || !list_empty(&clp->cl_sessions);
2166 nfsd4_exchange_id(struct svc_rqst *rqstp,
2167 struct nfsd4_compound_state *cstate,
2168 struct nfsd4_exchange_id *exid)
2170 struct nfs4_client *conf, *new;
2171 struct nfs4_client *unconf = NULL;
2173 char addr_str[INET6_ADDRSTRLEN];
2174 nfs4_verifier verf = exid->verifier;
2175 struct sockaddr *sa = svc_addr(rqstp);
2176 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2179 rpc_ntop(sa, addr_str, sizeof(addr_str));
2180 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2181 "ip_addr=%s flags %x, spa_how %d\n",
2182 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2183 addr_str, exid->flags, exid->spa_how);
2185 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2186 return nfserr_inval;
2188 switch (exid->spa_how) {
2190 if (!svc_rqst_integrity_protected(rqstp))
2191 return nfserr_inval;
2194 default: /* checked by xdr code */
2197 return nfserr_encr_alg_unsupp;
2200 new = create_client(exid->clname, rqstp, &verf);
2202 return nfserr_jukebox;
2204 /* Cases below refer to rfc 5661 section 18.35.4: */
2206 spin_lock(&nn->client_lock);
2207 conf = find_confirmed_client_by_name(&exid->clname, nn);
2209 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2210 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2213 if (!clp_used_exchangeid(conf)) { /* buggy client */
2214 status = nfserr_inval;
2217 if (!mach_creds_match(conf, rqstp)) {
2218 status = nfserr_wrong_cred;
2221 if (!creds_match) { /* case 9 */
2222 status = nfserr_perm;
2225 if (!verfs_match) { /* case 8 */
2226 status = nfserr_not_same;
2230 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2233 if (!creds_match) { /* case 3 */
2234 if (client_has_state(conf)) {
2235 status = nfserr_clid_inuse;
2240 if (verfs_match) { /* case 2 */
2241 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2244 /* case 5, client reboot */
2249 if (update) { /* case 7 */
2250 status = nfserr_noent;
2254 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2255 if (unconf) /* case 4, possible retry or client restart */
2256 unhash_client_locked(unconf);
2258 /* case 1 (normal case) */
2261 unhash_client_locked(conf);
2262 new->cl_minorversion = cstate->minorversion;
2263 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2266 add_to_unconfirmed(new);
2269 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2270 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2272 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2273 nfsd4_set_ex_flags(conf, exid);
2275 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2276 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2280 spin_unlock(&nn->client_lock);
2281 nfs4_unlock_state();
2285 expire_client(unconf);
2290 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2292 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2295 /* The slot is in use, and no response has been sent. */
2297 if (seqid == slot_seqid)
2298 return nfserr_jukebox;
2300 return nfserr_seq_misordered;
2302 /* Note unsigned 32-bit arithmetic handles wraparound: */
2303 if (likely(seqid == slot_seqid + 1))
2305 if (seqid == slot_seqid)
2306 return nfserr_replay_cache;
2307 return nfserr_seq_misordered;
2311 * Cache the create session result into the create session single DRC
2312 * slot cache by saving the xdr structure. sl_seqid has been set.
2313 * Do this for solo or embedded create session operations.
2316 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2317 struct nfsd4_clid_slot *slot, __be32 nfserr)
2319 slot->sl_status = nfserr;
2320 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2324 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2325 struct nfsd4_clid_slot *slot)
2327 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2328 return slot->sl_status;
2331 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2332 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2333 1 + /* MIN tag is length with zero, only length */ \
2334 3 + /* version, opcount, opcode */ \
2335 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2336 /* seqid, slotID, slotID, cache */ \
2337 4 ) * sizeof(__be32))
2339 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2340 2 + /* verifier: AUTH_NULL, length 0 */\
2342 1 + /* MIN tag is length with zero, only length */ \
2343 3 + /* opcount, opcode, opstatus*/ \
2344 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2345 /* seqid, slotID, slotID, slotID, status */ \
2346 5 ) * sizeof(__be32))
2348 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2350 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2352 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2353 return nfserr_toosmall;
2354 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2355 return nfserr_toosmall;
2356 ca->headerpadsz = 0;
2357 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2358 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2359 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2360 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2361 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2362 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2364 * Note decreasing slot size below client's request may make it
2365 * difficult for client to function correctly, whereas
2366 * decreasing the number of slots will (just?) affect
2367 * performance. When short on memory we therefore prefer to
2368 * decrease number of slots instead of their size. Clients that
2369 * request larger slots than they need will get poor results:
2371 ca->maxreqs = nfsd4_get_drc_mem(ca);
2373 return nfserr_jukebox;
2378 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2379 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2380 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2381 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2383 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2385 ca->headerpadsz = 0;
2388 * These RPC_MAX_HEADER macros are overkill, especially since we
2389 * don't even do gss on the backchannel yet. But this is still
2390 * less than 1k. Tighten up this estimate in the unlikely event
2391 * it turns out to be a problem for some client:
2393 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2394 return nfserr_toosmall;
2395 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2396 return nfserr_toosmall;
2397 ca->maxresp_cached = 0;
2399 return nfserr_toosmall;
2404 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2406 switch (cbs->flavor) {
2412 * GSS case: the spec doesn't allow us to return this
2413 * error. But it also doesn't allow us not to support
2415 * I'd rather this fail hard than return some error the
2416 * client might think it can already handle:
2418 return nfserr_encr_alg_unsupp;
2423 nfsd4_create_session(struct svc_rqst *rqstp,
2424 struct nfsd4_compound_state *cstate,
2425 struct nfsd4_create_session *cr_ses)
2427 struct sockaddr *sa = svc_addr(rqstp);
2428 struct nfs4_client *conf, *unconf;
2429 struct nfs4_client *old = NULL;
2430 struct nfsd4_session *new;
2431 struct nfsd4_conn *conn;
2432 struct nfsd4_clid_slot *cs_slot = NULL;
2434 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2436 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2437 return nfserr_inval;
2438 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2441 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2444 status = check_backchannel_attrs(&cr_ses->back_channel);
2446 goto out_release_drc_mem;
2447 status = nfserr_jukebox;
2448 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2450 goto out_release_drc_mem;
2451 conn = alloc_conn_from_crses(rqstp, cr_ses);
2453 goto out_free_session;
2456 spin_lock(&nn->client_lock);
2457 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2458 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2459 WARN_ON_ONCE(conf && unconf);
2462 status = nfserr_wrong_cred;
2463 if (!mach_creds_match(conf, rqstp))
2465 cs_slot = &conf->cl_cs_slot;
2466 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2467 if (status == nfserr_replay_cache) {
2468 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2470 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2471 status = nfserr_seq_misordered;
2474 } else if (unconf) {
2475 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2476 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2477 status = nfserr_clid_inuse;
2480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(unconf, rqstp))
2483 cs_slot = &unconf->cl_cs_slot;
2484 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2486 /* an unconfirmed replay returns misordered */
2487 status = nfserr_seq_misordered;
2490 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2492 status = mark_client_expired_locked(old);
2495 unhash_client_locked(old);
2497 move_to_confirmed(unconf);
2500 status = nfserr_stale_clientid;
2505 * We do not support RDMA or persistent sessions
2507 cr_ses->flags &= ~SESSION4_PERSIST;
2508 cr_ses->flags &= ~SESSION4_RDMA;
2510 init_session(rqstp, new, conf, cr_ses);
2511 nfsd4_get_session_locked(new);
2513 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2514 NFS4_MAX_SESSIONID_LEN);
2515 cs_slot->sl_seqid++;
2516 cr_ses->seqid = cs_slot->sl_seqid;
2518 /* cache solo and embedded create sessions under the client_lock */
2519 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2520 spin_unlock(&nn->client_lock);
2521 /* init connection and backchannel */
2522 nfsd4_init_conn(rqstp, conn, new);
2523 nfsd4_put_session(new);
2524 nfs4_unlock_state();
2529 spin_unlock(&nn->client_lock);
2530 nfs4_unlock_state();
2535 __free_session(new);
2536 out_release_drc_mem:
2537 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2541 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2544 case NFS4_CDFC4_FORE:
2545 case NFS4_CDFC4_BACK:
2547 case NFS4_CDFC4_FORE_OR_BOTH:
2548 case NFS4_CDFC4_BACK_OR_BOTH:
2549 *dir = NFS4_CDFC4_BOTH;
2552 return nfserr_inval;
2555 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2557 struct nfsd4_session *session = cstate->session;
2558 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2561 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2564 spin_lock(&nn->client_lock);
2565 session->se_cb_prog = bc->bc_cb_program;
2566 session->se_cb_sec = bc->bc_cb_sec;
2567 spin_unlock(&nn->client_lock);
2569 nfsd4_probe_callback(session->se_client);
2574 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2575 struct nfsd4_compound_state *cstate,
2576 struct nfsd4_bind_conn_to_session *bcts)
2579 struct nfsd4_conn *conn;
2580 struct nfsd4_session *session;
2581 struct net *net = SVC_NET(rqstp);
2582 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2584 if (!nfsd4_last_compound_op(rqstp))
2585 return nfserr_not_only_op;
2587 spin_lock(&nn->client_lock);
2588 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2589 spin_unlock(&nn->client_lock);
2591 goto out_no_session;
2592 status = nfserr_wrong_cred;
2593 if (!mach_creds_match(session->se_client, rqstp))
2595 status = nfsd4_map_bcts_dir(&bcts->dir);
2598 conn = alloc_conn(rqstp, bcts->dir);
2599 status = nfserr_jukebox;
2602 nfsd4_init_conn(rqstp, conn, session);
2605 nfsd4_put_session(session);
2607 nfs4_unlock_state();
2611 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2615 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2619 nfsd4_destroy_session(struct svc_rqst *r,
2620 struct nfsd4_compound_state *cstate,
2621 struct nfsd4_destroy_session *sessionid)
2623 struct nfsd4_session *ses;
2625 int ref_held_by_me = 0;
2626 struct net *net = SVC_NET(r);
2627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2630 status = nfserr_not_only_op;
2631 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2632 if (!nfsd4_last_compound_op(r))
2636 dump_sessionid(__func__, &sessionid->sessionid);
2637 spin_lock(&nn->client_lock);
2638 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2640 goto out_client_lock;
2641 status = nfserr_wrong_cred;
2642 if (!mach_creds_match(ses->se_client, r))
2643 goto out_put_session;
2644 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2646 goto out_put_session;
2647 unhash_session(ses);
2648 spin_unlock(&nn->client_lock);
2650 nfsd4_probe_callback_sync(ses->se_client);
2652 spin_lock(&nn->client_lock);
2655 nfsd4_put_session_locked(ses);
2657 spin_unlock(&nn->client_lock);
2659 nfs4_unlock_state();
2663 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2665 struct nfsd4_conn *c;
2667 list_for_each_entry(c, &s->se_conns, cn_persession) {
2668 if (c->cn_xprt == xpt) {
2675 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2677 struct nfs4_client *clp = ses->se_client;
2678 struct nfsd4_conn *c;
2679 __be32 status = nfs_ok;
2682 spin_lock(&clp->cl_lock);
2683 c = __nfsd4_find_conn(new->cn_xprt, ses);
2686 status = nfserr_conn_not_bound_to_session;
2687 if (clp->cl_mach_cred)
2689 __nfsd4_hash_conn(new, ses);
2690 spin_unlock(&clp->cl_lock);
2691 ret = nfsd4_register_conn(new);
2693 /* oops; xprt is already down: */
2694 nfsd4_conn_lost(&new->cn_xpt_user);
2697 spin_unlock(&clp->cl_lock);
2702 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2704 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2706 return args->opcnt > session->se_fchannel.maxops;
2709 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2710 struct nfsd4_session *session)
2712 struct xdr_buf *xb = &rqstp->rq_arg;
2714 return xb->len > session->se_fchannel.maxreq_sz;
2718 nfsd4_sequence(struct svc_rqst *rqstp,
2719 struct nfsd4_compound_state *cstate,
2720 struct nfsd4_sequence *seq)
2722 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2723 struct xdr_stream *xdr = &resp->xdr;
2724 struct nfsd4_session *session;
2725 struct nfs4_client *clp;
2726 struct nfsd4_slot *slot;
2727 struct nfsd4_conn *conn;
2730 struct net *net = SVC_NET(rqstp);
2731 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2733 if (resp->opcnt != 1)
2734 return nfserr_sequence_pos;
2737 * Will be either used or freed by nfsd4_sequence_check_conn
2740 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2742 return nfserr_jukebox;
2744 spin_lock(&nn->client_lock);
2745 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2747 goto out_no_session;
2748 clp = session->se_client;
2750 status = nfserr_too_many_ops;
2751 if (nfsd4_session_too_many_ops(rqstp, session))
2752 goto out_put_session;
2754 status = nfserr_req_too_big;
2755 if (nfsd4_request_too_big(rqstp, session))
2756 goto out_put_session;
2758 status = nfserr_badslot;
2759 if (seq->slotid >= session->se_fchannel.maxreqs)
2760 goto out_put_session;
2762 slot = session->se_slots[seq->slotid];
2763 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2765 /* We do not negotiate the number of slots yet, so set the
2766 * maxslots to the session maxreqs which is used to encode
2767 * sr_highest_slotid and the sr_target_slot id to maxslots */
2768 seq->maxslots = session->se_fchannel.maxreqs;
2770 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2771 slot->sl_flags & NFSD4_SLOT_INUSE);
2772 if (status == nfserr_replay_cache) {
2773 status = nfserr_seq_misordered;
2774 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2775 goto out_put_session;
2776 cstate->slot = slot;
2777 cstate->session = session;
2779 /* Return the cached reply status and set cstate->status
2780 * for nfsd4_proc_compound processing */
2781 status = nfsd4_replay_cache_entry(resp, seq);
2782 cstate->status = nfserr_replay_cache;
2786 goto out_put_session;
2788 status = nfsd4_sequence_check_conn(conn, session);
2791 goto out_put_session;
2793 buflen = (seq->cachethis) ?
2794 session->se_fchannel.maxresp_cached :
2795 session->se_fchannel.maxresp_sz;
2796 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2798 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2799 goto out_put_session;
2800 svc_reserve(rqstp, buflen);
2803 /* Success! bump slot seqid */
2804 slot->sl_seqid = seq->seqid;
2805 slot->sl_flags |= NFSD4_SLOT_INUSE;
2807 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2809 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2811 cstate->slot = slot;
2812 cstate->session = session;
2816 switch (clp->cl_cb_state) {
2818 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2820 case NFSD4_CB_FAULT:
2821 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2824 seq->status_flags = 0;
2826 if (!list_empty(&clp->cl_revoked))
2827 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2831 spin_unlock(&nn->client_lock);
2834 nfsd4_put_session_locked(session);
2835 goto out_no_session;
2839 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2841 struct nfsd4_compound_state *cs = &resp->cstate;
2843 if (nfsd4_has_session(cs)) {
2844 if (cs->status != nfserr_replay_cache) {
2845 nfsd4_store_cache_entry(resp);
2846 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2848 /* Drop session reference that was taken in nfsd4_sequence() */
2849 nfsd4_put_session(cs->session);
2851 put_client_renew(cs->clp);
2855 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2857 struct nfs4_client *conf, *unconf;
2858 struct nfs4_client *clp = NULL;
2860 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2863 spin_lock(&nn->client_lock);
2864 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2865 conf = find_confirmed_client(&dc->clientid, true, nn);
2866 WARN_ON_ONCE(conf && unconf);
2869 if (client_has_state(conf)) {
2870 status = nfserr_clientid_busy;
2877 status = nfserr_stale_clientid;
2880 if (!mach_creds_match(clp, rqstp)) {
2882 status = nfserr_wrong_cred;
2885 unhash_client_locked(clp);
2887 spin_unlock(&nn->client_lock);
2888 nfs4_unlock_state();
2895 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2899 if (rc->rca_one_fs) {
2900 if (!cstate->current_fh.fh_dentry)
2901 return nfserr_nofilehandle;
2903 * We don't take advantage of the rca_one_fs case.
2904 * That's OK, it's optional, we can safely ignore it.
2910 status = nfserr_complete_already;
2911 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2912 &cstate->session->se_client->cl_flags))
2915 status = nfserr_stale_clientid;
2916 if (is_client_expired(cstate->session->se_client))
2918 * The following error isn't really legal.
2919 * But we only get here if the client just explicitly
2920 * destroyed the client. Surely it no longer cares what
2921 * error it gets back on an operation for the dead
2927 nfsd4_client_record_create(cstate->session->se_client);
2929 nfs4_unlock_state();
2934 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2935 struct nfsd4_setclientid *setclid)
2937 struct xdr_netobj clname = setclid->se_name;
2938 nfs4_verifier clverifier = setclid->se_verf;
2939 struct nfs4_client *conf, *new;
2940 struct nfs4_client *unconf = NULL;
2942 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2944 new = create_client(clname, rqstp, &clverifier);
2946 return nfserr_jukebox;
2947 /* Cases below refer to rfc 3530 section 14.2.33: */
2949 spin_lock(&nn->client_lock);
2950 conf = find_confirmed_client_by_name(&clname, nn);
2953 status = nfserr_clid_inuse;
2954 if (clp_used_exchangeid(conf))
2956 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2957 char addr_str[INET6_ADDRSTRLEN];
2958 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2960 dprintk("NFSD: setclientid: string in use by client "
2961 "at %s\n", addr_str);
2965 unconf = find_unconfirmed_client_by_name(&clname, nn);
2967 unhash_client_locked(unconf);
2968 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2969 /* case 1: probable callback update */
2970 copy_clid(new, conf);
2971 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2973 new->cl_minorversion = 0;
2974 gen_callback(new, setclid, rqstp);
2975 add_to_unconfirmed(new);
2976 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2977 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2978 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2982 spin_unlock(&nn->client_lock);
2983 nfs4_unlock_state();
2987 expire_client(unconf);
2993 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2994 struct nfsd4_compound_state *cstate,
2995 struct nfsd4_setclientid_confirm *setclientid_confirm)
2997 struct nfs4_client *conf, *unconf;
2998 struct nfs4_client *old = NULL;
2999 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3000 clientid_t * clid = &setclientid_confirm->sc_clientid;
3002 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3004 if (STALE_CLIENTID(clid, nn))
3005 return nfserr_stale_clientid;
3008 spin_lock(&nn->client_lock);
3009 conf = find_confirmed_client(clid, false, nn);
3010 unconf = find_unconfirmed_client(clid, false, nn);
3012 * We try hard to give out unique clientid's, so if we get an
3013 * attempt to confirm the same clientid with a different cred,
3014 * there's a bug somewhere. Let's charitably assume it's our
3017 status = nfserr_serverfault;
3018 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3020 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3022 /* cases below refer to rfc 3530 section 14.2.34: */
3023 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3024 if (conf && !unconf) /* case 2: probable retransmit */
3026 else /* case 4: client hasn't noticed we rebooted yet? */
3027 status = nfserr_stale_clientid;
3031 if (conf) { /* case 1: callback update */
3033 unhash_client_locked(old);
3034 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3035 } else { /* case 3: normal case; new or rebooted client */
3036 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3038 status = mark_client_expired_locked(old);
3041 unhash_client_locked(old);
3043 move_to_confirmed(unconf);
3046 get_client_locked(conf);
3047 spin_unlock(&nn->client_lock);
3048 nfsd4_probe_callback(conf);
3049 spin_lock(&nn->client_lock);
3050 put_client_renew_locked(conf);
3052 spin_unlock(&nn->client_lock);
3055 nfs4_unlock_state();
3059 static struct nfs4_file *nfsd4_alloc_file(void)
3061 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3064 /* OPEN Share state helper functions */
3065 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3067 unsigned int hashval = file_hashval(fh);
3069 lockdep_assert_held(&state_lock);
3071 atomic_set(&fp->fi_ref, 1);
3072 spin_lock_init(&fp->fi_lock);
3073 INIT_LIST_HEAD(&fp->fi_stateids);
3074 INIT_LIST_HEAD(&fp->fi_delegations);
3075 fh_copy_shallow(&fp->fi_fhandle, fh);
3076 fp->fi_had_conflict = false;
3077 fp->fi_lease = NULL;
3078 fp->fi_share_deny = 0;
3079 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3080 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3081 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3085 nfsd4_free_slabs(void)
3087 kmem_cache_destroy(openowner_slab);
3088 kmem_cache_destroy(lockowner_slab);
3089 kmem_cache_destroy(file_slab);
3090 kmem_cache_destroy(stateid_slab);
3091 kmem_cache_destroy(deleg_slab);
3095 nfsd4_init_slabs(void)
3097 openowner_slab = kmem_cache_create("nfsd4_openowners",
3098 sizeof(struct nfs4_openowner), 0, 0, NULL);
3099 if (openowner_slab == NULL)
3101 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3102 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3103 if (lockowner_slab == NULL)
3104 goto out_free_openowner_slab;
3105 file_slab = kmem_cache_create("nfsd4_files",
3106 sizeof(struct nfs4_file), 0, 0, NULL);
3107 if (file_slab == NULL)
3108 goto out_free_lockowner_slab;
3109 stateid_slab = kmem_cache_create("nfsd4_stateids",
3110 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3111 if (stateid_slab == NULL)
3112 goto out_free_file_slab;
3113 deleg_slab = kmem_cache_create("nfsd4_delegations",
3114 sizeof(struct nfs4_delegation), 0, 0, NULL);
3115 if (deleg_slab == NULL)
3116 goto out_free_stateid_slab;
3119 out_free_stateid_slab:
3120 kmem_cache_destroy(stateid_slab);
3122 kmem_cache_destroy(file_slab);
3123 out_free_lockowner_slab:
3124 kmem_cache_destroy(lockowner_slab);
3125 out_free_openowner_slab:
3126 kmem_cache_destroy(openowner_slab);
3128 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3132 static void init_nfs4_replay(struct nfs4_replay *rp)
3134 rp->rp_status = nfserr_serverfault;
3136 rp->rp_buf = rp->rp_ibuf;
3137 mutex_init(&rp->rp_mutex);
3140 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3141 struct nfs4_stateowner *so)
3143 if (!nfsd4_has_session(cstate)) {
3144 mutex_lock(&so->so_replay.rp_mutex);
3145 cstate->replay_owner = so;
3146 atomic_inc(&so->so_count);
3150 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3152 struct nfs4_stateowner *so = cstate->replay_owner;
3155 cstate->replay_owner = NULL;
3156 mutex_unlock(&so->so_replay.rp_mutex);
3157 nfs4_put_stateowner(so);
3161 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3163 struct nfs4_stateowner *sop;
3165 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3169 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3170 if (!sop->so_owner.data) {
3171 kmem_cache_free(slab, sop);
3174 sop->so_owner.len = owner->len;
3176 INIT_LIST_HEAD(&sop->so_stateids);
3177 sop->so_client = clp;
3178 init_nfs4_replay(&sop->so_replay);
3179 atomic_set(&sop->so_count, 1);
3183 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3185 lockdep_assert_held(&clp->cl_lock);
3187 list_add(&oo->oo_owner.so_strhash,
3188 &clp->cl_ownerstr_hashtbl[strhashval]);
3189 list_add(&oo->oo_perclient, &clp->cl_openowners);
3192 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3194 unhash_openowner_locked(openowner(so));
3197 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3199 struct nfs4_openowner *oo = openowner(so);
3201 kmem_cache_free(openowner_slab, oo);
3204 static const struct nfs4_stateowner_operations openowner_ops = {
3205 .so_unhash = nfs4_unhash_openowner,
3206 .so_free = nfs4_free_openowner,
3209 static struct nfs4_openowner *
3210 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3211 struct nfsd4_compound_state *cstate)
3213 struct nfs4_client *clp = cstate->clp;
3214 struct nfs4_openowner *oo, *ret;
3216 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3219 oo->oo_owner.so_ops = &openowner_ops;
3220 oo->oo_owner.so_is_open_owner = 1;
3221 oo->oo_owner.so_seqid = open->op_seqid;
3223 if (nfsd4_has_session(cstate))
3224 oo->oo_flags |= NFS4_OO_CONFIRMED;
3226 oo->oo_last_closed_stid = NULL;
3227 INIT_LIST_HEAD(&oo->oo_close_lru);
3228 spin_lock(&clp->cl_lock);
3229 ret = find_openstateowner_str_locked(strhashval, open, clp);
3231 hash_openowner(oo, clp, strhashval);
3234 nfs4_free_openowner(&oo->oo_owner);
3235 spin_unlock(&clp->cl_lock);
3239 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3240 struct nfs4_openowner *oo = open->op_openowner;
3242 atomic_inc(&stp->st_stid.sc_count);
3243 stp->st_stid.sc_type = NFS4_OPEN_STID;
3244 INIT_LIST_HEAD(&stp->st_locks);
3245 stp->st_stateowner = &oo->oo_owner;
3246 atomic_inc(&stp->st_stateowner->so_count);
3248 stp->st_stid.sc_file = fp;
3249 stp->st_access_bmap = 0;
3250 stp->st_deny_bmap = 0;
3251 stp->st_openstp = NULL;
3252 spin_lock(&oo->oo_owner.so_client->cl_lock);
3253 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3254 spin_lock(&fp->fi_lock);
3255 list_add(&stp->st_perfile, &fp->fi_stateids);
3256 spin_unlock(&fp->fi_lock);
3257 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3261 * In the 4.0 case we need to keep the owners around a little while to handle
3262 * CLOSE replay. We still do need to release any file access that is held by
3263 * them before returning however.
3266 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3268 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3269 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3272 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3275 * We know that we hold one reference via nfsd4_close, and another
3276 * "persistent" reference for the client. If the refcount is higher
3277 * than 2, then there are still calls in progress that are using this
3278 * stateid. We can't put the sc_file reference until they are finished.
3279 * Wait for the refcount to drop to 2. Since it has been unhashed,
3280 * there should be no danger of the refcount going back up again at
3283 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3285 release_all_access(s);
3286 if (s->st_stid.sc_file) {
3287 put_nfs4_file(s->st_stid.sc_file);
3288 s->st_stid.sc_file = NULL;
3290 release_last_closed_stateid(oo);
3291 oo->oo_last_closed_stid = s;
3292 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3293 oo->oo_time = get_seconds();
3296 /* search file_hashtbl[] for file */
3297 static struct nfs4_file *
3298 find_file_locked(struct knfsd_fh *fh)
3300 unsigned int hashval = file_hashval(fh);
3301 struct nfs4_file *fp;
3303 lockdep_assert_held(&state_lock);
3305 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3306 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3314 static struct nfs4_file *
3315 find_file(struct knfsd_fh *fh)
3317 struct nfs4_file *fp;
3319 spin_lock(&state_lock);
3320 fp = find_file_locked(fh);
3321 spin_unlock(&state_lock);
3325 static struct nfs4_file *
3326 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3328 struct nfs4_file *fp;
3330 spin_lock(&state_lock);
3331 fp = find_file_locked(fh);
3333 nfsd4_init_file(new, fh);
3336 spin_unlock(&state_lock);
3342 * Called to check deny when READ with all zero stateid or
3343 * WRITE with all zero or all one stateid
3346 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3348 struct nfs4_file *fp;
3349 __be32 ret = nfs_ok;
3351 fp = find_file(¤t_fh->fh_handle);
3354 /* Check for conflicting share reservations */
3355 spin_lock(&fp->fi_lock);
3356 if (fp->fi_share_deny & deny_type)
3357 ret = nfserr_locked;
3358 spin_unlock(&fp->fi_lock);
3363 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3365 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3368 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3371 * We can't do this in nfsd_break_deleg_cb because it is
3372 * already holding inode->i_lock.
3374 * If the dl_time != 0, then we know that it has already been
3375 * queued for a lease break. Don't queue it again.
3377 spin_lock(&state_lock);
3378 if (dp->dl_time == 0) {
3379 dp->dl_time = get_seconds();
3380 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3382 spin_unlock(&state_lock);
3385 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3388 * We're assuming the state code never drops its reference
3389 * without first removing the lease. Since we're in this lease
3390 * callback (and since the lease code is serialized by the kernel
3391 * lock) we know the server hasn't removed the lease yet, we know
3392 * it's safe to take a reference.
3394 atomic_inc(&dp->dl_stid.sc_count);
3395 nfsd4_cb_recall(dp);
3398 /* Called from break_lease() with i_lock held. */
3399 static void nfsd_break_deleg_cb(struct file_lock *fl)
3401 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3402 struct nfs4_delegation *dp;
3405 WARN(1, "(%p)->fl_owner NULL\n", fl);
3408 if (fp->fi_had_conflict) {
3409 WARN(1, "duplicate break on %p\n", fp);
3413 * We don't want the locks code to timeout the lease for us;
3414 * we'll remove it ourself if a delegation isn't returned
3417 fl->fl_break_time = 0;
3419 spin_lock(&fp->fi_lock);
3420 fp->fi_had_conflict = true;
3422 * If there are no delegations on the list, then we can't count on this
3423 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3424 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3425 * true should keep any new delegations from being hashed.
3427 if (list_empty(&fp->fi_delegations))
3428 fl->fl_break_time = jiffies;
3430 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3431 nfsd_break_one_deleg(dp);
3432 spin_unlock(&fp->fi_lock);
3436 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3439 return lease_modify(onlist, arg);
3444 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3445 .lm_break = nfsd_break_deleg_cb,
3446 .lm_change = nfsd_change_deleg_cb,
3449 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3451 if (nfsd4_has_session(cstate))
3453 if (seqid == so->so_seqid - 1)
3454 return nfserr_replay_me;
3455 if (seqid == so->so_seqid)
3457 return nfserr_bad_seqid;
3460 static __be32 lookup_clientid(clientid_t *clid,
3461 struct nfsd4_compound_state *cstate,
3462 struct nfsd_net *nn)
3464 struct nfs4_client *found;
3467 found = cstate->clp;
3468 if (!same_clid(&found->cl_clientid, clid))
3469 return nfserr_stale_clientid;
3473 if (STALE_CLIENTID(clid, nn))
3474 return nfserr_stale_clientid;
3477 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3478 * cached already then we know this is for is for v4.0 and "sessions"
3481 WARN_ON_ONCE(cstate->session);
3482 spin_lock(&nn->client_lock);
3483 found = find_confirmed_client(clid, false, nn);
3485 spin_unlock(&nn->client_lock);
3486 return nfserr_expired;
3488 atomic_inc(&found->cl_refcount);
3489 spin_unlock(&nn->client_lock);
3491 /* Cache the nfs4_client in cstate! */
3492 cstate->clp = found;
3497 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3498 struct nfsd4_open *open, struct nfsd_net *nn)
3500 clientid_t *clientid = &open->op_clientid;
3501 struct nfs4_client *clp = NULL;
3502 unsigned int strhashval;
3503 struct nfs4_openowner *oo = NULL;
3506 if (STALE_CLIENTID(&open->op_clientid, nn))
3507 return nfserr_stale_clientid;
3509 * In case we need it later, after we've already created the
3510 * file and don't want to risk a further failure:
3512 open->op_file = nfsd4_alloc_file();
3513 if (open->op_file == NULL)
3514 return nfserr_jukebox;
3516 status = lookup_clientid(clientid, cstate, nn);
3521 strhashval = ownerstr_hashval(&open->op_owner);
3522 oo = find_openstateowner_str(strhashval, open, clp);
3523 open->op_openowner = oo;
3527 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3528 /* Replace unconfirmed owners without checking for replay. */
3529 release_openowner(oo);
3530 open->op_openowner = NULL;
3533 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3538 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3540 return nfserr_jukebox;
3541 open->op_openowner = oo;
3543 open->op_stp = nfs4_alloc_open_stateid(clp);
3545 return nfserr_jukebox;
3549 static inline __be32
3550 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3552 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3553 return nfserr_openmode;
3558 static int share_access_to_flags(u32 share_access)
3560 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3563 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3565 struct nfs4_stid *ret;
3567 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3570 return delegstateid(ret);
3573 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3575 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3576 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3580 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3581 struct nfs4_delegation **dp)
3584 __be32 status = nfserr_bad_stateid;
3585 struct nfs4_delegation *deleg;
3587 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3590 flags = share_access_to_flags(open->op_share_access);
3591 status = nfs4_check_delegmode(deleg, flags);
3593 nfs4_put_stid(&deleg->dl_stid);
3598 if (!nfsd4_is_deleg_cur(open))
3602 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3606 static struct nfs4_ol_stateid *
3607 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3609 struct nfs4_ol_stateid *local, *ret = NULL;
3610 struct nfs4_openowner *oo = open->op_openowner;
3612 spin_lock(&fp->fi_lock);
3613 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3614 /* ignore lock owners */
3615 if (local->st_stateowner->so_is_open_owner == 0)
3617 if (local->st_stateowner == &oo->oo_owner) {
3619 atomic_inc(&ret->st_stid.sc_count);
3623 spin_unlock(&fp->fi_lock);
3627 static inline int nfs4_access_to_access(u32 nfs4_access)
3631 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3632 flags |= NFSD_MAY_READ;
3633 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3634 flags |= NFSD_MAY_WRITE;
3638 static inline __be32
3639 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3640 struct nfsd4_open *open)
3642 struct iattr iattr = {
3643 .ia_valid = ATTR_SIZE,
3646 if (!open->op_truncate)
3648 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3649 return nfserr_inval;
3650 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3653 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3654 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3655 struct nfsd4_open *open)
3657 struct file *filp = NULL;
3659 int oflag = nfs4_access_to_omode(open->op_share_access);
3660 int access = nfs4_access_to_access(open->op_share_access);
3661 unsigned char old_access_bmap, old_deny_bmap;
3663 spin_lock(&fp->fi_lock);
3666 * Are we trying to set a deny mode that would conflict with
3669 status = nfs4_file_check_deny(fp, open->op_share_deny);
3670 if (status != nfs_ok) {
3671 spin_unlock(&fp->fi_lock);
3675 /* set access to the file */
3676 status = nfs4_file_get_access(fp, open->op_share_access);
3677 if (status != nfs_ok) {
3678 spin_unlock(&fp->fi_lock);
3682 /* Set access bits in stateid */
3683 old_access_bmap = stp->st_access_bmap;
3684 set_access(open->op_share_access, stp);
3686 /* Set new deny mask */
3687 old_deny_bmap = stp->st_deny_bmap;
3688 set_deny(open->op_share_deny, stp);
3689 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3691 if (!fp->fi_fds[oflag]) {
3692 spin_unlock(&fp->fi_lock);
3693 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3695 goto out_put_access;
3696 spin_lock(&fp->fi_lock);
3697 if (!fp->fi_fds[oflag]) {
3698 fp->fi_fds[oflag] = filp;
3702 spin_unlock(&fp->fi_lock);
3706 status = nfsd4_truncate(rqstp, cur_fh, open);
3708 goto out_put_access;
3712 stp->st_access_bmap = old_access_bmap;
3713 nfs4_file_put_access(fp, open->op_share_access);
3714 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3719 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)
3722 unsigned char old_deny_bmap;
3724 if (!test_access(open->op_share_access, stp))
3725 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3727 /* test and set deny mode */
3728 spin_lock(&fp->fi_lock);
3729 status = nfs4_file_check_deny(fp, open->op_share_deny);
3730 if (status == nfs_ok) {
3731 old_deny_bmap = stp->st_deny_bmap;
3732 set_deny(open->op_share_deny, stp);
3733 fp->fi_share_deny |=
3734 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3736 spin_unlock(&fp->fi_lock);
3738 if (status != nfs_ok)
3741 status = nfsd4_truncate(rqstp, cur_fh, open);
3742 if (status != nfs_ok)
3743 reset_union_bmap_deny(old_deny_bmap, stp);
3748 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3750 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3753 /* Should we give out recallable state?: */
3754 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3756 if (clp->cl_cb_state == NFSD4_CB_UP)
3759 * In the sessions case, since we don't have to establish a
3760 * separate connection for callbacks, we assume it's OK
3761 * until we hear otherwise:
3763 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3766 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3768 struct file_lock *fl;
3770 fl = locks_alloc_lock();
3773 locks_init_lock(fl);
3774 fl->fl_lmops = &nfsd_lease_mng_ops;
3775 fl->fl_flags = FL_DELEG;
3776 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3777 fl->fl_end = OFFSET_MAX;
3778 fl->fl_owner = (fl_owner_t)fp;
3779 fl->fl_pid = current->tgid;
3783 static int nfs4_setlease(struct nfs4_delegation *dp)
3785 struct nfs4_file *fp = dp->dl_stid.sc_file;
3786 struct file_lock *fl;
3790 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3793 filp = find_readable_file(fp);
3795 /* We should always have a readable file here */
3800 status = vfs_setlease(filp, fl->fl_type, &fl);
3802 locks_free_lock(fl);
3805 spin_lock(&state_lock);
3806 spin_lock(&fp->fi_lock);
3807 /* Did the lease get broken before we took the lock? */
3809 if (fp->fi_had_conflict)
3814 atomic_inc(&fp->fi_delegees);
3815 hash_delegation_locked(dp, fp);
3819 fp->fi_deleg_file = filp;
3820 atomic_set(&fp->fi_delegees, 1);
3821 hash_delegation_locked(dp, fp);
3822 spin_unlock(&fp->fi_lock);
3823 spin_unlock(&state_lock);
3826 spin_unlock(&fp->fi_lock);
3827 spin_unlock(&state_lock);
3833 static struct nfs4_delegation *
3834 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3835 struct nfs4_file *fp)
3838 struct nfs4_delegation *dp;
3840 if (fp->fi_had_conflict)
3841 return ERR_PTR(-EAGAIN);
3843 dp = alloc_init_deleg(clp, fh);
3845 return ERR_PTR(-ENOMEM);
3848 spin_lock(&state_lock);
3849 spin_lock(&fp->fi_lock);
3850 dp->dl_stid.sc_file = fp;
3851 if (!fp->fi_lease) {
3852 spin_unlock(&fp->fi_lock);
3853 spin_unlock(&state_lock);
3854 status = nfs4_setlease(dp);
3857 atomic_inc(&fp->fi_delegees);
3858 if (fp->fi_had_conflict) {
3862 hash_delegation_locked(dp, fp);
3865 spin_unlock(&fp->fi_lock);
3866 spin_unlock(&state_lock);
3869 nfs4_put_stid(&dp->dl_stid);
3870 return ERR_PTR(status);
3875 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3877 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3878 if (status == -EAGAIN)
3879 open->op_why_no_deleg = WND4_CONTENTION;
3881 open->op_why_no_deleg = WND4_RESOURCE;
3882 switch (open->op_deleg_want) {
3883 case NFS4_SHARE_WANT_READ_DELEG:
3884 case NFS4_SHARE_WANT_WRITE_DELEG:
3885 case NFS4_SHARE_WANT_ANY_DELEG:
3887 case NFS4_SHARE_WANT_CANCEL:
3888 open->op_why_no_deleg = WND4_CANCELLED;
3890 case NFS4_SHARE_WANT_NO_DELEG:
3897 * Attempt to hand out a delegation.
3899 * Note we don't support write delegations, and won't until the vfs has
3900 * proper support for them.
3903 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3904 struct nfs4_ol_stateid *stp)
3906 struct nfs4_delegation *dp;
3907 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3908 struct nfs4_client *clp = stp->st_stid.sc_client;
3912 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3913 open->op_recall = 0;
3914 switch (open->op_claim_type) {
3915 case NFS4_OPEN_CLAIM_PREVIOUS:
3917 open->op_recall = 1;
3918 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3921 case NFS4_OPEN_CLAIM_NULL:
3922 case NFS4_OPEN_CLAIM_FH:
3924 * Let's not give out any delegations till everyone's
3925 * had the chance to reclaim theirs....
3927 if (locks_in_grace(clp->net))
3929 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3932 * Also, if the file was opened for write or
3933 * create, there's a good chance the client's
3934 * about to write to it, resulting in an
3935 * immediate recall (since we don't support
3936 * write delegations):
3938 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3940 if (open->op_create == NFS4_OPEN_CREATE)
3946 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3950 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3952 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3953 STATEID_VAL(&dp->dl_stid.sc_stateid));
3954 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3955 nfs4_put_stid(&dp->dl_stid);
3958 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3959 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3960 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3961 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3962 open->op_recall = 1;
3965 /* 4.1 client asking for a delegation? */
3966 if (open->op_deleg_want)
3967 nfsd4_open_deleg_none_ext(open, status);
3971 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3972 struct nfs4_delegation *dp)
3974 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3975 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3976 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3977 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3978 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3979 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3980 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3981 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3983 /* Otherwise the client must be confused wanting a delegation
3984 * it already has, therefore we don't return
3985 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3990 * called with nfs4_lock_state() held.
3993 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3995 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3996 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3997 struct nfs4_file *fp = NULL;
3998 struct nfs4_ol_stateid *stp = NULL;
3999 struct nfs4_delegation *dp = NULL;
4003 * Lookup file; if found, lookup stateid and check open request,
4004 * and check for delegations in the process of being recalled.
4005 * If not found, create the nfs4_file struct
4007 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
4008 if (fp != open->op_file) {
4009 status = nfs4_check_deleg(cl, open, &dp);
4012 stp = nfsd4_find_existing_open(fp, open);
4014 open->op_file = NULL;
4015 status = nfserr_bad_stateid;
4016 if (nfsd4_is_deleg_cur(open))
4018 status = nfserr_jukebox;
4022 * OPEN the file, or upgrade an existing OPEN.
4023 * If truncate fails, the OPEN fails.
4026 /* Stateid was found, this is an OPEN upgrade */
4027 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4032 open->op_stp = NULL;
4033 init_open_stateid(stp, fp, open);
4034 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4036 release_open_stateid(stp);
4040 update_stateid(&stp->st_stid.sc_stateid);
4041 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4043 if (nfsd4_has_session(&resp->cstate)) {
4044 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4045 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4046 open->op_why_no_deleg = WND4_NOT_WANTED;
4052 * Attempt to hand out a delegation. No error return, because the
4053 * OPEN succeeds even if we fail.
4055 nfs4_open_delegation(current_fh, open, stp);
4059 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4060 STATEID_VAL(&stp->st_stid.sc_stateid));
4062 /* 4.1 client trying to upgrade/downgrade delegation? */
4063 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4064 open->op_deleg_want)
4065 nfsd4_deleg_xgrade_none_ext(open, dp);
4069 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4070 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4072 * To finish the open response, we just need to set the rflags.
4074 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4075 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4076 !nfsd4_has_session(&resp->cstate))
4077 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4079 nfs4_put_stid(&dp->dl_stid);
4081 nfs4_put_stid(&stp->st_stid);
4086 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4087 struct nfsd4_open *open, __be32 status)
4089 if (open->op_openowner) {
4090 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4092 nfsd4_cstate_assign_replay(cstate, so);
4093 nfs4_put_stateowner(so);
4096 nfsd4_free_file(open->op_file);
4098 nfs4_put_stid(&open->op_stp->st_stid);
4102 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4105 struct nfs4_client *clp;
4107 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4110 dprintk("process_renew(%08x/%08x): starting\n",
4111 clid->cl_boot, clid->cl_id);
4112 status = lookup_clientid(clid, cstate, nn);
4116 status = nfserr_cb_path_down;
4117 if (!list_empty(&clp->cl_delegations)
4118 && clp->cl_cb_state != NFSD4_CB_UP)
4122 nfs4_unlock_state();
4127 nfsd4_end_grace(struct nfsd_net *nn)
4129 /* do nothing if grace period already ended */
4130 if (nn->grace_ended)
4133 dprintk("NFSD: end of grace period\n");
4134 nn->grace_ended = true;
4135 nfsd4_record_grace_done(nn, nn->boot_time);
4136 locks_end_grace(&nn->nfsd4_manager);
4138 * Now that every NFSv4 client has had the chance to recover and
4139 * to see the (possibly new, possibly shorter) lease time, we
4140 * can safely set the next grace time to the current lease time:
4142 nn->nfsd4_grace = nn->nfsd4_lease;
4146 nfs4_laundromat(struct nfsd_net *nn)
4148 struct nfs4_client *clp;
4149 struct nfs4_openowner *oo;
4150 struct nfs4_delegation *dp;
4151 struct list_head *pos, *next, reaplist;
4152 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4153 time_t t, new_timeo = nn->nfsd4_lease;
4157 dprintk("NFSD: laundromat service - starting\n");
4158 nfsd4_end_grace(nn);
4159 INIT_LIST_HEAD(&reaplist);
4160 spin_lock(&nn->client_lock);
4161 list_for_each_safe(pos, next, &nn->client_lru) {
4162 clp = list_entry(pos, struct nfs4_client, cl_lru);
4163 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4164 t = clp->cl_time - cutoff;
4165 new_timeo = min(new_timeo, t);
4168 if (mark_client_expired_locked(clp)) {
4169 dprintk("NFSD: client in use (clientid %08x)\n",
4170 clp->cl_clientid.cl_id);
4173 unhash_client_locked(clp);
4174 list_add(&clp->cl_lru, &reaplist);
4176 spin_unlock(&nn->client_lock);
4177 list_for_each_safe(pos, next, &reaplist) {
4178 clp = list_entry(pos, struct nfs4_client, cl_lru);
4179 dprintk("NFSD: purging unused client (clientid %08x)\n",
4180 clp->cl_clientid.cl_id);
4181 list_del_init(&clp->cl_lru);
4184 spin_lock(&state_lock);
4185 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4186 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4187 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4189 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4190 t = dp->dl_time - cutoff;
4191 new_timeo = min(new_timeo, t);
4194 unhash_delegation_locked(dp);
4195 list_add(&dp->dl_recall_lru, &reaplist);
4197 spin_unlock(&state_lock);
4198 while (!list_empty(&reaplist)) {
4199 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4201 list_del_init(&dp->dl_recall_lru);
4202 revoke_delegation(dp);
4204 list_for_each_safe(pos, next, &nn->close_lru) {
4205 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4206 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4207 t = oo->oo_time - cutoff;
4208 new_timeo = min(new_timeo, t);
4211 release_last_closed_stateid(oo);
4213 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4214 nfs4_unlock_state();
4218 static struct workqueue_struct *laundry_wq;
4219 static void laundromat_main(struct work_struct *);
4222 laundromat_main(struct work_struct *laundry)
4225 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4227 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4230 t = nfs4_laundromat(nn);
4231 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4232 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4235 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4237 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4238 return nfserr_bad_stateid;
4243 access_permit_read(struct nfs4_ol_stateid *stp)
4245 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4246 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4247 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4251 access_permit_write(struct nfs4_ol_stateid *stp)
4253 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4254 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4258 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4260 __be32 status = nfserr_openmode;
4262 /* For lock stateid's, we test the parent open, not the lock: */
4263 if (stp->st_openstp)
4264 stp = stp->st_openstp;
4265 if ((flags & WR_STATE) && !access_permit_write(stp))
4267 if ((flags & RD_STATE) && !access_permit_read(stp))
4274 static inline __be32
4275 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4277 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4279 else if (locks_in_grace(net)) {
4280 /* Answer in remaining cases depends on existence of
4281 * conflicting state; so we must wait out the grace period. */
4282 return nfserr_grace;
4283 } else if (flags & WR_STATE)
4284 return nfs4_share_conflict(current_fh,
4285 NFS4_SHARE_DENY_WRITE);
4286 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4287 return nfs4_share_conflict(current_fh,
4288 NFS4_SHARE_DENY_READ);
4292 * Allow READ/WRITE during grace period on recovered state only for files
4293 * that are not able to provide mandatory locking.
4296 grace_disallows_io(struct net *net, struct inode *inode)
4298 return locks_in_grace(net) && mandatory_lock(inode);
4301 /* Returns true iff a is later than b: */
4302 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4304 return (s32)(a->si_generation - b->si_generation) > 0;
4307 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4310 * When sessions are used the stateid generation number is ignored
4313 if (has_session && in->si_generation == 0)
4316 if (in->si_generation == ref->si_generation)
4319 /* If the client sends us a stateid from the future, it's buggy: */
4320 if (stateid_generation_after(in, ref))
4321 return nfserr_bad_stateid;
4323 * However, we could see a stateid from the past, even from a
4324 * non-buggy client. For example, if the client sends a lock
4325 * while some IO is outstanding, the lock may bump si_generation
4326 * while the IO is still in flight. The client could avoid that
4327 * situation by waiting for responses on all the IO requests,
4328 * but better performance may result in retrying IO that
4329 * receives an old_stateid error if requests are rarely
4330 * reordered in flight:
4332 return nfserr_old_stateid;
4335 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4337 struct nfs4_stid *s;
4338 struct nfs4_ol_stateid *ols;
4339 __be32 status = nfserr_bad_stateid;
4341 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4343 /* Client debugging aid. */
4344 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4345 char addr_str[INET6_ADDRSTRLEN];
4346 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4348 pr_warn_ratelimited("NFSD: client %s testing state ID "
4349 "with incorrect client ID\n", addr_str);
4352 spin_lock(&cl->cl_lock);
4353 s = find_stateid_locked(cl, stateid);
4356 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4359 switch (s->sc_type) {
4360 case NFS4_DELEG_STID:
4363 case NFS4_REVOKED_DELEG_STID:
4364 status = nfserr_deleg_revoked;
4366 case NFS4_OPEN_STID:
4367 case NFS4_LOCK_STID:
4368 ols = openlockstateid(s);
4369 if (ols->st_stateowner->so_is_open_owner
4370 && !(openowner(ols->st_stateowner)->oo_flags
4371 & NFS4_OO_CONFIRMED))
4372 status = nfserr_bad_stateid;
4377 printk("unknown stateid type %x\n", s->sc_type);
4379 case NFS4_CLOSED_STID:
4380 case NFS4_CLOSED_DELEG_STID:
4381 status = nfserr_bad_stateid;
4384 spin_unlock(&cl->cl_lock);
4389 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4390 stateid_t *stateid, unsigned char typemask,
4391 struct nfs4_stid **s, struct nfsd_net *nn)
4395 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4396 return nfserr_bad_stateid;
4397 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4398 if (status == nfserr_stale_clientid) {
4399 if (cstate->session)
4400 return nfserr_bad_stateid;
4401 return nfserr_stale_stateid;
4405 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4407 return nfserr_bad_stateid;
4412 * Checks for stateid operations
4415 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4416 stateid_t *stateid, int flags, struct file **filpp)
4418 struct nfs4_stid *s;
4419 struct nfs4_ol_stateid *stp = NULL;
4420 struct nfs4_delegation *dp = NULL;
4421 struct svc_fh *current_fh = &cstate->current_fh;
4422 struct inode *ino = current_fh->fh_dentry->d_inode;
4423 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4424 struct file *file = NULL;
4430 if (grace_disallows_io(net, ino))
4431 return nfserr_grace;
4433 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4434 return check_special_stateids(net, current_fh, stateid, flags);
4438 status = nfsd4_lookup_stateid(cstate, stateid,
4439 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4443 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4446 switch (s->sc_type) {
4447 case NFS4_DELEG_STID:
4448 dp = delegstateid(s);
4449 status = nfs4_check_delegmode(dp, flags);
4453 file = dp->dl_stid.sc_file->fi_deleg_file;
4456 status = nfserr_serverfault;
4462 case NFS4_OPEN_STID:
4463 case NFS4_LOCK_STID:
4464 stp = openlockstateid(s);
4465 status = nfs4_check_fh(current_fh, stp);
4468 if (stp->st_stateowner->so_is_open_owner
4469 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4471 status = nfs4_check_openmode(stp, flags);
4475 struct nfs4_file *fp = stp->st_stid.sc_file;
4477 if (flags & RD_STATE)
4478 file = find_readable_file(fp);
4480 file = find_writeable_file(fp);
4484 status = nfserr_bad_stateid;
4493 nfs4_unlock_state();
4498 * Test if the stateid is valid
4501 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4502 struct nfsd4_test_stateid *test_stateid)
4504 struct nfsd4_test_stateid_id *stateid;
4505 struct nfs4_client *cl = cstate->session->se_client;
4508 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4509 stateid->ts_id_status =
4510 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4511 nfs4_unlock_state();
4517 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4518 struct nfsd4_free_stateid *free_stateid)
4520 stateid_t *stateid = &free_stateid->fr_stateid;
4521 struct nfs4_stid *s;
4522 struct nfs4_delegation *dp;
4523 struct nfs4_ol_stateid *stp;
4524 struct nfs4_client *cl = cstate->session->se_client;
4525 __be32 ret = nfserr_bad_stateid;
4528 spin_lock(&cl->cl_lock);
4529 s = find_stateid_locked(cl, stateid);
4532 switch (s->sc_type) {
4533 case NFS4_DELEG_STID:
4534 ret = nfserr_locks_held;
4536 case NFS4_OPEN_STID:
4537 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4540 ret = nfserr_locks_held;
4542 case NFS4_LOCK_STID:
4543 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4546 stp = openlockstateid(s);
4547 ret = nfserr_locks_held;
4548 if (check_for_locks(stp->st_stid.sc_file,
4549 lockowner(stp->st_stateowner)))
4551 unhash_lock_stateid(stp);
4552 spin_unlock(&cl->cl_lock);
4556 case NFS4_REVOKED_DELEG_STID:
4557 dp = delegstateid(s);
4558 list_del_init(&dp->dl_recall_lru);
4559 spin_unlock(&cl->cl_lock);
4563 /* Default falls through and returns nfserr_bad_stateid */
4566 spin_unlock(&cl->cl_lock);
4568 nfs4_unlock_state();
4575 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4576 RD_STATE : WR_STATE;
4579 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4581 struct svc_fh *current_fh = &cstate->current_fh;
4582 struct nfs4_stateowner *sop = stp->st_stateowner;
4585 status = nfsd4_check_seqid(cstate, sop, seqid);
4588 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4589 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4591 * "Closed" stateid's exist *only* to return
4592 * nfserr_replay_me from the previous step, and
4593 * revoked delegations are kept only for free_stateid.
4595 return nfserr_bad_stateid;
4596 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4599 return nfs4_check_fh(current_fh, stp);
4603 * Checks for sequence id mutating operations.
4606 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4607 stateid_t *stateid, char typemask,
4608 struct nfs4_ol_stateid **stpp,
4609 struct nfsd_net *nn)
4612 struct nfs4_stid *s;
4613 struct nfs4_ol_stateid *stp = NULL;
4615 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4616 seqid, STATEID_VAL(stateid));
4619 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4622 stp = openlockstateid(s);
4623 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4625 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4629 nfs4_put_stid(&stp->st_stid);
4633 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4634 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4637 struct nfs4_openowner *oo;
4638 struct nfs4_ol_stateid *stp;
4640 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4641 NFS4_OPEN_STID, &stp, nn);
4644 oo = openowner(stp->st_stateowner);
4645 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4646 nfs4_put_stid(&stp->st_stid);
4647 return nfserr_bad_stateid;
4654 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4655 struct nfsd4_open_confirm *oc)
4658 struct nfs4_openowner *oo;
4659 struct nfs4_ol_stateid *stp;
4660 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4662 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4663 cstate->current_fh.fh_dentry);
4665 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4671 status = nfs4_preprocess_seqid_op(cstate,
4672 oc->oc_seqid, &oc->oc_req_stateid,
4673 NFS4_OPEN_STID, &stp, nn);
4676 oo = openowner(stp->st_stateowner);
4677 status = nfserr_bad_stateid;
4678 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4680 oo->oo_flags |= NFS4_OO_CONFIRMED;
4681 update_stateid(&stp->st_stid.sc_stateid);
4682 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4683 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4684 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4686 nfsd4_client_record_create(oo->oo_owner.so_client);
4689 nfs4_put_stid(&stp->st_stid);
4691 nfsd4_bump_seqid(cstate, status);
4692 nfs4_unlock_state();
4696 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4698 if (!test_access(access, stp))
4700 nfs4_file_put_access(stp->st_stid.sc_file, access);
4701 clear_access(access, stp);
4704 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4706 switch (to_access) {
4707 case NFS4_SHARE_ACCESS_READ:
4708 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4709 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4711 case NFS4_SHARE_ACCESS_WRITE:
4712 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4713 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4715 case NFS4_SHARE_ACCESS_BOTH:
4723 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4724 struct nfsd4_compound_state *cstate,
4725 struct nfsd4_open_downgrade *od)
4728 struct nfs4_ol_stateid *stp;
4729 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4731 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4732 cstate->current_fh.fh_dentry);
4734 /* We don't yet support WANT bits: */
4735 if (od->od_deleg_want)
4736 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4740 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4741 &od->od_stateid, &stp, nn);
4744 status = nfserr_inval;
4745 if (!test_access(od->od_share_access, stp)) {
4746 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4747 stp->st_access_bmap, od->od_share_access);
4750 if (!test_deny(od->od_share_deny, stp)) {
4751 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4752 stp->st_deny_bmap, od->od_share_deny);
4755 nfs4_stateid_downgrade(stp, od->od_share_access);
4757 reset_union_bmap_deny(od->od_share_deny, stp);
4759 update_stateid(&stp->st_stid.sc_stateid);
4760 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4763 nfs4_put_stid(&stp->st_stid);
4765 nfsd4_bump_seqid(cstate, status);
4766 nfs4_unlock_state();
4770 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4772 struct nfs4_client *clp = s->st_stid.sc_client;
4773 LIST_HEAD(reaplist);
4775 s->st_stid.sc_type = NFS4_CLOSED_STID;
4776 spin_lock(&clp->cl_lock);
4777 unhash_open_stateid(s, &reaplist);
4779 if (clp->cl_minorversion) {
4780 put_ol_stateid_locked(s, &reaplist);
4781 spin_unlock(&clp->cl_lock);
4782 free_ol_stateid_reaplist(&reaplist);
4784 spin_unlock(&clp->cl_lock);
4785 free_ol_stateid_reaplist(&reaplist);
4786 move_to_close_lru(s, clp->net);
4791 * nfs4_unlock_state() called after encode
4794 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4795 struct nfsd4_close *close)
4798 struct nfs4_ol_stateid *stp;
4799 struct net *net = SVC_NET(rqstp);
4800 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4802 dprintk("NFSD: nfsd4_close on file %pd\n",
4803 cstate->current_fh.fh_dentry);
4806 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4808 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4810 nfsd4_bump_seqid(cstate, status);
4813 update_stateid(&stp->st_stid.sc_stateid);
4814 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4816 nfsd4_close_open_stateid(stp);
4818 /* put reference from nfs4_preprocess_seqid_op */
4819 nfs4_put_stid(&stp->st_stid);
4821 nfs4_unlock_state();
4826 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4827 struct nfsd4_delegreturn *dr)
4829 struct nfs4_delegation *dp;
4830 stateid_t *stateid = &dr->dr_stateid;
4831 struct nfs4_stid *s;
4833 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4835 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4839 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4842 dp = delegstateid(s);
4843 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4847 destroy_delegation(dp);
4849 nfs4_put_stid(&dp->dl_stid);
4851 nfs4_unlock_state();
4857 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4860 end_offset(u64 start, u64 len)
4865 return end >= start ? end: NFS4_MAX_UINT64;
4868 /* last octet in a range */
4870 last_byte_offset(u64 start, u64 len)
4876 return end > start ? end - 1: NFS4_MAX_UINT64;
4880 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4881 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4882 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4883 * locking, this prevents us from being completely protocol-compliant. The
4884 * real solution to this problem is to start using unsigned file offsets in
4885 * the VFS, but this is a very deep change!
4888 nfs4_transform_lock_offset(struct file_lock *lock)
4890 if (lock->fl_start < 0)
4891 lock->fl_start = OFFSET_MAX;
4892 if (lock->fl_end < 0)
4893 lock->fl_end = OFFSET_MAX;
4896 /* Hack!: For now, we're defining this just so we can use a pointer to it
4897 * as a unique cookie to identify our (NFSv4's) posix locks. */
4898 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4902 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4904 struct nfs4_lockowner *lo;
4906 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4907 lo = (struct nfs4_lockowner *) fl->fl_owner;
4908 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4909 lo->lo_owner.so_owner.len, GFP_KERNEL);
4910 if (!deny->ld_owner.data)
4911 /* We just don't care that much */
4913 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4914 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4917 deny->ld_owner.len = 0;
4918 deny->ld_owner.data = NULL;
4919 deny->ld_clientid.cl_boot = 0;
4920 deny->ld_clientid.cl_id = 0;
4922 deny->ld_start = fl->fl_start;
4923 deny->ld_length = NFS4_MAX_UINT64;
4924 if (fl->fl_end != NFS4_MAX_UINT64)
4925 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4926 deny->ld_type = NFS4_READ_LT;
4927 if (fl->fl_type != F_RDLCK)
4928 deny->ld_type = NFS4_WRITE_LT;
4931 static struct nfs4_lockowner *
4932 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4933 struct nfs4_client *clp)
4935 unsigned int strhashval = ownerstr_hashval(owner);
4936 struct nfs4_stateowner *so;
4938 lockdep_assert_held(&clp->cl_lock);
4940 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4942 if (so->so_is_open_owner)
4944 if (!same_owner_str(so, owner))
4946 atomic_inc(&so->so_count);
4947 return lockowner(so);
4952 static struct nfs4_lockowner *
4953 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4954 struct nfs4_client *clp)
4956 struct nfs4_lockowner *lo;
4958 spin_lock(&clp->cl_lock);
4959 lo = find_lockowner_str_locked(clid, owner, clp);
4960 spin_unlock(&clp->cl_lock);
4964 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4966 unhash_lockowner_locked(lockowner(sop));
4969 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4971 struct nfs4_lockowner *lo = lockowner(sop);
4973 kmem_cache_free(lockowner_slab, lo);
4976 static const struct nfs4_stateowner_operations lockowner_ops = {
4977 .so_unhash = nfs4_unhash_lockowner,
4978 .so_free = nfs4_free_lockowner,
4982 * Alloc a lock owner structure.
4983 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4986 * strhashval = ownerstr_hashval
4988 static struct nfs4_lockowner *
4989 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4990 struct nfs4_ol_stateid *open_stp,
4991 struct nfsd4_lock *lock)
4993 struct nfs4_lockowner *lo, *ret;
4995 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4998 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4999 lo->lo_owner.so_is_open_owner = 0;
5000 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5001 lo->lo_owner.so_ops = &lockowner_ops;
5002 spin_lock(&clp->cl_lock);
5003 ret = find_lockowner_str_locked(&clp->cl_clientid,
5004 &lock->lk_new_owner, clp);
5006 list_add(&lo->lo_owner.so_strhash,
5007 &clp->cl_ownerstr_hashtbl[strhashval]);
5010 nfs4_free_lockowner(&lo->lo_owner);
5011 spin_unlock(&clp->cl_lock);
5016 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5017 struct nfs4_file *fp, struct inode *inode,
5018 struct nfs4_ol_stateid *open_stp)
5020 struct nfs4_client *clp = lo->lo_owner.so_client;
5022 lockdep_assert_held(&clp->cl_lock);
5024 atomic_inc(&stp->st_stid.sc_count);
5025 stp->st_stid.sc_type = NFS4_LOCK_STID;
5026 stp->st_stateowner = &lo->lo_owner;
5027 atomic_inc(&lo->lo_owner.so_count);
5029 stp->st_stid.sc_file = fp;
5030 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5031 stp->st_access_bmap = 0;
5032 stp->st_deny_bmap = open_stp->st_deny_bmap;
5033 stp->st_openstp = open_stp;
5034 list_add(&stp->st_locks, &open_stp->st_locks);
5035 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5036 spin_lock(&fp->fi_lock);
5037 list_add(&stp->st_perfile, &fp->fi_stateids);
5038 spin_unlock(&fp->fi_lock);
5041 static struct nfs4_ol_stateid *
5042 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5044 struct nfs4_ol_stateid *lst;
5045 struct nfs4_client *clp = lo->lo_owner.so_client;
5047 lockdep_assert_held(&clp->cl_lock);
5049 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5050 if (lst->st_stid.sc_file == fp) {
5051 atomic_inc(&lst->st_stid.sc_count);
5058 static struct nfs4_ol_stateid *
5059 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5060 struct inode *inode, struct nfs4_ol_stateid *ost,
5063 struct nfs4_stid *ns = NULL;
5064 struct nfs4_ol_stateid *lst;
5065 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5066 struct nfs4_client *clp = oo->oo_owner.so_client;
5068 spin_lock(&clp->cl_lock);
5069 lst = find_lock_stateid(lo, fi);
5071 spin_unlock(&clp->cl_lock);
5072 ns = nfs4_alloc_stid(clp, stateid_slab);
5076 spin_lock(&clp->cl_lock);
5077 lst = find_lock_stateid(lo, fi);
5079 lst = openlockstateid(ns);
5080 init_lock_stateid(lst, lo, fi, inode, ost);
5085 spin_unlock(&clp->cl_lock);
5092 check_lock_length(u64 offset, u64 length)
5094 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5095 LOFF_OVERFLOW(offset, length)));
5098 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5100 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5102 lockdep_assert_held(&fp->fi_lock);
5104 if (test_access(access, lock_stp))
5106 __nfs4_file_get_access(fp, access);
5107 set_access(access, lock_stp);
5111 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5112 struct nfs4_ol_stateid *ost,
5113 struct nfsd4_lock *lock,
5114 struct nfs4_ol_stateid **lst, bool *new)
5117 struct nfs4_file *fi = ost->st_stid.sc_file;
5118 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5119 struct nfs4_client *cl = oo->oo_owner.so_client;
5120 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5121 struct nfs4_lockowner *lo;
5122 unsigned int strhashval;
5124 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5126 strhashval = ownerstr_hashval(&lock->v.new.owner);
5127 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5129 return nfserr_jukebox;
5131 /* with an existing lockowner, seqids must be the same */
5132 status = nfserr_bad_seqid;
5133 if (!cstate->minorversion &&
5134 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5138 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5140 status = nfserr_jukebox;
5145 nfs4_put_stateowner(&lo->lo_owner);
5153 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5154 struct nfsd4_lock *lock)
5156 struct nfs4_openowner *open_sop = NULL;
5157 struct nfs4_lockowner *lock_sop = NULL;
5158 struct nfs4_ol_stateid *lock_stp = NULL;
5159 struct nfs4_ol_stateid *open_stp = NULL;
5160 struct nfs4_file *fp;
5161 struct file *filp = NULL;
5162 struct file_lock *file_lock = NULL;
5163 struct file_lock *conflock = NULL;
5168 struct net *net = SVC_NET(rqstp);
5169 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5171 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5172 (long long) lock->lk_offset,
5173 (long long) lock->lk_length);
5175 if (check_lock_length(lock->lk_offset, lock->lk_length))
5176 return nfserr_inval;
5178 if ((status = fh_verify(rqstp, &cstate->current_fh,
5179 S_IFREG, NFSD_MAY_LOCK))) {
5180 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5186 if (lock->lk_is_new) {
5187 if (nfsd4_has_session(cstate))
5188 /* See rfc 5661 18.10.3: given clientid is ignored: */
5189 memcpy(&lock->v.new.clientid,
5190 &cstate->session->se_client->cl_clientid,
5191 sizeof(clientid_t));
5193 status = nfserr_stale_clientid;
5194 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5197 /* validate and update open stateid and open seqid */
5198 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5199 lock->lk_new_open_seqid,
5200 &lock->lk_new_open_stateid,
5204 open_sop = openowner(open_stp->st_stateowner);
5205 status = nfserr_bad_stateid;
5206 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5207 &lock->v.new.clientid))
5209 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5212 status = nfs4_preprocess_seqid_op(cstate,
5213 lock->lk_old_lock_seqid,
5214 &lock->lk_old_lock_stateid,
5215 NFS4_LOCK_STID, &lock_stp, nn);
5219 lock_sop = lockowner(lock_stp->st_stateowner);
5221 lkflg = setlkflg(lock->lk_type);
5222 status = nfs4_check_openmode(lock_stp, lkflg);
5226 status = nfserr_grace;
5227 if (locks_in_grace(net) && !lock->lk_reclaim)
5229 status = nfserr_no_grace;
5230 if (!locks_in_grace(net) && lock->lk_reclaim)
5233 file_lock = locks_alloc_lock();
5235 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5236 status = nfserr_jukebox;
5240 fp = lock_stp->st_stid.sc_file;
5241 locks_init_lock(file_lock);
5242 switch (lock->lk_type) {
5245 spin_lock(&fp->fi_lock);
5246 filp = find_readable_file_locked(fp);
5248 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5249 spin_unlock(&fp->fi_lock);
5250 file_lock->fl_type = F_RDLCK;
5253 case NFS4_WRITEW_LT:
5254 spin_lock(&fp->fi_lock);
5255 filp = find_writeable_file_locked(fp);
5257 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5258 spin_unlock(&fp->fi_lock);
5259 file_lock->fl_type = F_WRLCK;
5262 status = nfserr_inval;
5266 status = nfserr_openmode;
5269 file_lock->fl_owner = (fl_owner_t)lock_sop;
5270 file_lock->fl_pid = current->tgid;
5271 file_lock->fl_file = filp;
5272 file_lock->fl_flags = FL_POSIX;
5273 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5274 file_lock->fl_start = lock->lk_offset;
5275 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5276 nfs4_transform_lock_offset(file_lock);
5278 conflock = locks_alloc_lock();
5280 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5281 status = nfserr_jukebox;
5285 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5287 case 0: /* success! */
5288 update_stateid(&lock_stp->st_stid.sc_stateid);
5289 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5293 case (EAGAIN): /* conflock holds conflicting lock */
5294 status = nfserr_denied;
5295 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5296 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5299 status = nfserr_deadlock;
5302 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5303 status = nfserrno(err);
5310 /* Bump seqid manually if the 4.0 replay owner is openowner */
5311 if (cstate->replay_owner &&
5312 cstate->replay_owner != &lock_sop->lo_owner &&
5313 seqid_mutating_err(ntohl(status)))
5314 lock_sop->lo_owner.so_seqid++;
5317 * If this is a new, never-before-used stateid, and we are
5318 * returning an error, then just go ahead and release it.
5321 release_lock_stateid(lock_stp);
5323 nfs4_put_stid(&lock_stp->st_stid);
5326 nfs4_put_stid(&open_stp->st_stid);
5327 nfsd4_bump_seqid(cstate, status);
5328 nfs4_unlock_state();
5330 locks_free_lock(file_lock);
5332 locks_free_lock(conflock);
5337 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5338 * so we do a temporary open here just to get an open file to pass to
5339 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5342 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5345 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5347 err = nfserrno(vfs_test_lock(file, lock));
5357 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5358 struct nfsd4_lockt *lockt)
5360 struct file_lock *file_lock = NULL;
5361 struct nfs4_lockowner *lo = NULL;
5363 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5365 if (locks_in_grace(SVC_NET(rqstp)))
5366 return nfserr_grace;
5368 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5369 return nfserr_inval;
5373 if (!nfsd4_has_session(cstate)) {
5374 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5379 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5382 file_lock = locks_alloc_lock();
5384 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5385 status = nfserr_jukebox;
5388 locks_init_lock(file_lock);
5389 switch (lockt->lt_type) {
5392 file_lock->fl_type = F_RDLCK;
5395 case NFS4_WRITEW_LT:
5396 file_lock->fl_type = F_WRLCK;
5399 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5400 status = nfserr_inval;
5404 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5407 file_lock->fl_owner = (fl_owner_t)lo;
5408 file_lock->fl_pid = current->tgid;
5409 file_lock->fl_flags = FL_POSIX;
5411 file_lock->fl_start = lockt->lt_offset;
5412 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5414 nfs4_transform_lock_offset(file_lock);
5416 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5420 if (file_lock->fl_type != F_UNLCK) {
5421 status = nfserr_denied;
5422 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5426 nfs4_put_stateowner(&lo->lo_owner);
5427 nfs4_unlock_state();
5429 locks_free_lock(file_lock);
5434 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5435 struct nfsd4_locku *locku)
5437 struct nfs4_ol_stateid *stp;
5438 struct file *filp = NULL;
5439 struct file_lock *file_lock = NULL;
5442 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5444 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5445 (long long) locku->lu_offset,
5446 (long long) locku->lu_length);
5448 if (check_lock_length(locku->lu_offset, locku->lu_length))
5449 return nfserr_inval;
5453 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5454 &locku->lu_stateid, NFS4_LOCK_STID,
5458 filp = find_any_file(stp->st_stid.sc_file);
5460 status = nfserr_lock_range;
5463 file_lock = locks_alloc_lock();
5465 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5466 status = nfserr_jukebox;
5469 locks_init_lock(file_lock);
5470 file_lock->fl_type = F_UNLCK;
5471 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5472 file_lock->fl_pid = current->tgid;
5473 file_lock->fl_file = filp;
5474 file_lock->fl_flags = FL_POSIX;
5475 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5476 file_lock->fl_start = locku->lu_offset;
5478 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5480 nfs4_transform_lock_offset(file_lock);
5482 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5484 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5487 update_stateid(&stp->st_stid.sc_stateid);
5488 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5492 nfs4_put_stid(&stp->st_stid);
5494 nfsd4_bump_seqid(cstate, status);
5495 nfs4_unlock_state();
5497 locks_free_lock(file_lock);
5501 status = nfserrno(err);
5507 * true: locks held by lockowner
5508 * false: no locks held by lockowner
5511 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5513 struct file_lock **flpp;
5515 struct file *filp = find_any_file(fp);
5516 struct inode *inode;
5519 /* Any valid lock stateid should have some sort of access */
5524 inode = file_inode(filp);
5526 spin_lock(&inode->i_lock);
5527 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5528 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5533 spin_unlock(&inode->i_lock);
5539 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5540 struct nfsd4_compound_state *cstate,
5541 struct nfsd4_release_lockowner *rlockowner)
5543 clientid_t *clid = &rlockowner->rl_clientid;
5544 struct nfs4_stateowner *sop;
5545 struct nfs4_lockowner *lo = NULL;
5546 struct nfs4_ol_stateid *stp;
5547 struct xdr_netobj *owner = &rlockowner->rl_owner;
5548 unsigned int hashval = ownerstr_hashval(owner);
5550 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5551 struct nfs4_client *clp;
5553 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5554 clid->cl_boot, clid->cl_id);
5558 status = lookup_clientid(clid, cstate, nn);
5563 /* Find the matching lock stateowner */
5564 spin_lock(&clp->cl_lock);
5565 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5568 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5571 /* see if there are still any locks associated with it */
5572 lo = lockowner(sop);
5573 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5574 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5575 status = nfserr_locks_held;
5576 spin_unlock(&clp->cl_lock);
5581 atomic_inc(&sop->so_count);
5584 spin_unlock(&clp->cl_lock);
5586 release_lockowner(lo);
5588 nfs4_unlock_state();
5592 static inline struct nfs4_client_reclaim *
5595 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5599 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5601 struct nfs4_client_reclaim *crp;
5603 crp = nfsd4_find_reclaim_client(name, nn);
5604 return (crp && crp->cr_clp);
5608 * failure => all reset bets are off, nfserr_no_grace...
5610 struct nfs4_client_reclaim *
5611 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5613 unsigned int strhashval;
5614 struct nfs4_client_reclaim *crp;
5616 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5617 crp = alloc_reclaim();
5619 strhashval = clientstr_hashval(name);
5620 INIT_LIST_HEAD(&crp->cr_strhash);
5621 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5622 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5624 nn->reclaim_str_hashtbl_size++;
5630 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5632 list_del(&crp->cr_strhash);
5634 nn->reclaim_str_hashtbl_size--;
5638 nfs4_release_reclaim(struct nfsd_net *nn)
5640 struct nfs4_client_reclaim *crp = NULL;
5643 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5644 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5645 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5646 struct nfs4_client_reclaim, cr_strhash);
5647 nfs4_remove_reclaim_record(crp, nn);
5650 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5654 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5655 struct nfs4_client_reclaim *
5656 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5658 unsigned int strhashval;
5659 struct nfs4_client_reclaim *crp = NULL;
5661 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5663 strhashval = clientstr_hashval(recdir);
5664 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5665 if (same_name(crp->cr_recdir, recdir)) {
5673 * Called from OPEN. Look for clientid in reclaim list.
5676 nfs4_check_open_reclaim(clientid_t *clid,
5677 struct nfsd4_compound_state *cstate,
5678 struct nfsd_net *nn)
5682 /* find clientid in conf_id_hashtbl */
5683 status = lookup_clientid(clid, cstate, nn);
5685 return nfserr_reclaim_bad;
5687 if (nfsd4_client_record_check(cstate->clp))
5688 return nfserr_reclaim_bad;
5693 #ifdef CONFIG_NFSD_FAULT_INJECTION
5695 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5698 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
5700 spin_lock(&nn->client_lock);
5701 ret = mark_client_expired_locked(clp);
5702 spin_unlock(&nn->client_lock);
5709 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5711 char buf[INET6_ADDRSTRLEN];
5712 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5713 printk(KERN_INFO "NFS Client: %s\n", buf);
5717 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5720 char buf[INET6_ADDRSTRLEN];
5721 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5722 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5725 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5726 void (*func)(struct nfs4_ol_stateid *))
5728 struct nfs4_openowner *oop;
5729 struct nfs4_ol_stateid *stp, *st_next;
5730 struct nfs4_ol_stateid *lst, *lst_next;
5733 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5734 list_for_each_entry_safe(stp, st_next,
5735 &oop->oo_owner.so_stateids, st_perstateowner) {
5736 list_for_each_entry_safe(lst, lst_next,
5737 &stp->st_locks, st_locks) {
5749 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5751 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5754 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5756 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5757 nfsd_print_count(clp, count, "locked files");
5761 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5763 struct nfs4_openowner *oop, *next;
5766 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5776 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5778 return nfsd_foreach_client_open(clp, max, release_openowner);
5781 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5783 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5784 nfsd_print_count(clp, count, "open files");
5788 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5789 struct list_head *victims)
5791 struct nfs4_delegation *dp, *next;
5794 lockdep_assert_held(&state_lock);
5795 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5798 * It's not safe to mess with delegations that have a
5799 * non-zero dl_time. They might have already been broken
5800 * and could be processed by the laundromat outside of
5801 * the state_lock. Just leave them be.
5803 if (dp->dl_time != 0)
5806 unhash_delegation_locked(dp);
5807 list_add(&dp->dl_recall_lru, victims);
5815 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5817 struct nfs4_delegation *dp, *next;
5821 spin_lock(&state_lock);
5822 count = nfsd_find_all_delegations(clp, max, &victims);
5823 spin_unlock(&state_lock);
5825 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5826 list_del_init(&dp->dl_recall_lru);
5827 revoke_delegation(dp);
5833 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5835 struct nfs4_delegation *dp;
5839 spin_lock(&state_lock);
5840 count = nfsd_find_all_delegations(clp, max, &victims);
5841 while (!list_empty(&victims)) {
5842 dp = list_first_entry(&victims, struct nfs4_delegation,
5844 list_del_init(&dp->dl_recall_lru);
5846 nfsd_break_one_deleg(dp);
5848 spin_unlock(&state_lock);
5853 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5857 spin_lock(&state_lock);
5858 count = nfsd_find_all_delegations(clp, max, NULL);
5859 spin_unlock(&state_lock);
5861 nfsd_print_count(clp, count, "delegations");
5865 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5867 struct nfs4_client *clp, *next;
5869 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5871 if (!nfsd_netns_ready(nn))
5874 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5875 count += func(clp, max - count);
5876 if ((max != 0) && (count >= max))
5883 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5885 struct nfs4_client *clp;
5886 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5888 if (!nfsd_netns_ready(nn))
5891 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5892 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5898 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5901 * Since the lifetime of a delegation isn't limited to that of an open, a
5902 * client may quite reasonably hang on to a delegation as long as it has
5903 * the inode cached. This becomes an obvious problem the first time a
5904 * client's inode cache approaches the size of the server's total memory.
5906 * For now we avoid this problem by imposing a hard limit on the number
5907 * of delegations, which varies according to the server's memory size.
5910 set_max_delegations(void)
5913 * Allow at most 4 delegations per megabyte of RAM. Quick
5914 * estimates suggest that in the worst case (where every delegation
5915 * is for a different inode), a delegation could take about 1.5K,
5916 * giving a worst case usage of about 6% of memory.
5918 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5921 static int nfs4_state_create_net(struct net *net)
5923 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5926 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5927 CLIENT_HASH_SIZE, GFP_KERNEL);
5928 if (!nn->conf_id_hashtbl)
5930 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5931 CLIENT_HASH_SIZE, GFP_KERNEL);
5932 if (!nn->unconf_id_hashtbl)
5934 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5935 SESSION_HASH_SIZE, GFP_KERNEL);
5936 if (!nn->sessionid_hashtbl)
5939 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5940 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5941 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5943 for (i = 0; i < SESSION_HASH_SIZE; i++)
5944 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5945 nn->conf_name_tree = RB_ROOT;
5946 nn->unconf_name_tree = RB_ROOT;
5947 INIT_LIST_HEAD(&nn->client_lru);
5948 INIT_LIST_HEAD(&nn->close_lru);
5949 INIT_LIST_HEAD(&nn->del_recall_lru);
5950 spin_lock_init(&nn->client_lock);
5952 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5958 kfree(nn->unconf_id_hashtbl);
5960 kfree(nn->conf_id_hashtbl);
5966 nfs4_state_destroy_net(struct net *net)
5969 struct nfs4_client *clp = NULL;
5970 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5972 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5973 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5974 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5975 destroy_client(clp);
5979 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5980 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5981 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5982 destroy_client(clp);
5986 kfree(nn->sessionid_hashtbl);
5987 kfree(nn->unconf_id_hashtbl);
5988 kfree(nn->conf_id_hashtbl);
5993 nfs4_state_start_net(struct net *net)
5995 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5998 ret = nfs4_state_create_net(net);
6001 nfsd4_client_tracking_init(net);
6002 nn->boot_time = get_seconds();
6003 locks_start_grace(net, &nn->nfsd4_manager);
6004 nn->grace_ended = false;
6005 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6006 nn->nfsd4_grace, net);
6007 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6011 /* initialization to perform when the nfsd service is started: */
6014 nfs4_state_start(void)
6018 ret = set_callback_cred();
6021 laundry_wq = create_singlethread_workqueue("nfsd4");
6022 if (laundry_wq == NULL) {
6026 ret = nfsd4_create_callback_queue();
6028 goto out_free_laundry;
6030 set_max_delegations();
6035 destroy_workqueue(laundry_wq);
6041 nfs4_state_shutdown_net(struct net *net)
6043 struct nfs4_delegation *dp = NULL;
6044 struct list_head *pos, *next, reaplist;
6045 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6047 cancel_delayed_work_sync(&nn->laundromat_work);
6048 locks_end_grace(&nn->nfsd4_manager);
6051 INIT_LIST_HEAD(&reaplist);
6052 spin_lock(&state_lock);
6053 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6054 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6055 unhash_delegation_locked(dp);
6056 list_add(&dp->dl_recall_lru, &reaplist);
6058 spin_unlock(&state_lock);
6059 list_for_each_safe(pos, next, &reaplist) {
6060 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6061 list_del_init(&dp->dl_recall_lru);
6062 nfs4_put_stid(&dp->dl_stid);
6065 nfsd4_client_tracking_exit(net);
6066 nfs4_state_destroy_net(net);
6067 nfs4_unlock_state();
6071 nfs4_state_shutdown(void)
6073 destroy_workqueue(laundry_wq);
6074 nfsd4_destroy_callback_queue();
6078 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6080 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6081 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6085 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6087 if (cstate->minorversion) {
6088 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6089 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6094 clear_current_stateid(struct nfsd4_compound_state *cstate)
6096 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6100 * functions to set current state id
6103 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6105 put_stateid(cstate, &odp->od_stateid);
6109 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6111 put_stateid(cstate, &open->op_stateid);
6115 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6117 put_stateid(cstate, &close->cl_stateid);
6121 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6123 put_stateid(cstate, &lock->lk_resp_stateid);
6127 * functions to consume current state id
6131 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6133 get_stateid(cstate, &odp->od_stateid);
6137 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6139 get_stateid(cstate, &drp->dr_stateid);
6143 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6145 get_stateid(cstate, &fsp->fr_stateid);
6149 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6151 get_stateid(cstate, &setattr->sa_stateid);
6155 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6157 get_stateid(cstate, &close->cl_stateid);
6161 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6163 get_stateid(cstate, &locku->lu_stateid);
6167 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6169 get_stateid(cstate, &read->rd_stateid);
6173 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6175 get_stateid(cstate, &write->wr_stateid);