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 mark_client_expired(struct nfs4_client *clp)
142 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
145 spin_lock(&nn->client_lock);
146 ret = mark_client_expired_locked(clp);
147 spin_unlock(&nn->client_lock);
151 static __be32 get_client_locked(struct nfs4_client *clp)
153 if (is_client_expired(clp))
154 return nfserr_expired;
155 atomic_inc(&clp->cl_refcount);
159 /* must be called under the client_lock */
161 renew_client_locked(struct nfs4_client *clp)
163 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
165 if (is_client_expired(clp)) {
167 printk("%s: client (clientid %08x/%08x) already expired\n",
169 clp->cl_clientid.cl_boot,
170 clp->cl_clientid.cl_id);
174 dprintk("renewing client (clientid %08x/%08x)\n",
175 clp->cl_clientid.cl_boot,
176 clp->cl_clientid.cl_id);
177 list_move_tail(&clp->cl_lru, &nn->client_lru);
178 clp->cl_time = get_seconds();
182 renew_client(struct nfs4_client *clp)
184 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
186 spin_lock(&nn->client_lock);
187 renew_client_locked(clp);
188 spin_unlock(&nn->client_lock);
191 static void put_client_renew_locked(struct nfs4_client *clp)
193 if (!atomic_dec_and_test(&clp->cl_refcount))
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
199 static void put_client_renew(struct nfs4_client *clp)
201 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
203 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
205 if (!is_client_expired(clp))
206 renew_client_locked(clp);
207 spin_unlock(&nn->client_lock);
210 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
214 if (is_session_dead(ses))
215 return nfserr_badsession;
216 status = get_client_locked(ses->se_client);
219 atomic_inc(&ses->se_ref);
223 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
225 struct nfs4_client *clp = ses->se_client;
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 spin_lock(&nn->client_lock);
1441 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1442 spin_lock(&clp->cl_lock);
1443 list_add(&new->se_perclnt, &clp->cl_sessions);
1444 spin_unlock(&clp->cl_lock);
1445 spin_unlock(&nn->client_lock);
1447 if (cses->flags & SESSION4_BACK_CHAN) {
1448 struct sockaddr *sa = svc_addr(rqstp);
1450 * This is a little silly; with sessions there's no real
1451 * use for the callback address. Use the peer address
1452 * as a reasonable default for now, but consider fixing
1453 * the rpc client not to require an address in the
1456 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1457 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1461 /* caller must hold client_lock */
1462 static struct nfsd4_session *
1463 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1465 struct nfsd4_session *elem;
1467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
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 list_del(&ses->se_hash);
1506 spin_lock(&ses->se_client->cl_lock);
1507 list_del(&ses->se_perclnt);
1508 spin_unlock(&ses->se_client->cl_lock);
1511 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1513 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1515 if (clid->cl_boot == nn->boot_time)
1517 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1518 clid->cl_boot, clid->cl_id, nn->boot_time);
1523 * XXX Should we use a slab cache ?
1524 * This type of memory management is somewhat inefficient, but we use it
1525 * anyway since SETCLIENTID is not a common operation.
1527 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1529 struct nfs4_client *clp;
1532 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1535 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1536 if (clp->cl_name.data == NULL)
1538 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1539 OWNER_HASH_SIZE, GFP_KERNEL);
1540 if (!clp->cl_ownerstr_hashtbl)
1541 goto err_no_hashtbl;
1542 for (i = 0; i < OWNER_HASH_SIZE; i++)
1543 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1544 clp->cl_name.len = name.len;
1545 INIT_LIST_HEAD(&clp->cl_sessions);
1546 idr_init(&clp->cl_stateids);
1547 atomic_set(&clp->cl_refcount, 0);
1548 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1549 INIT_LIST_HEAD(&clp->cl_idhash);
1550 INIT_LIST_HEAD(&clp->cl_openowners);
1551 INIT_LIST_HEAD(&clp->cl_delegations);
1552 INIT_LIST_HEAD(&clp->cl_lru);
1553 INIT_LIST_HEAD(&clp->cl_callbacks);
1554 INIT_LIST_HEAD(&clp->cl_revoked);
1555 spin_lock_init(&clp->cl_lock);
1556 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1559 kfree(clp->cl_name.data);
1566 free_client(struct nfs4_client *clp)
1568 while (!list_empty(&clp->cl_sessions)) {
1569 struct nfsd4_session *ses;
1570 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1572 list_del(&ses->se_perclnt);
1573 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1576 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1577 free_svc_cred(&clp->cl_cred);
1578 kfree(clp->cl_ownerstr_hashtbl);
1579 kfree(clp->cl_name.data);
1580 idr_destroy(&clp->cl_stateids);
1584 /* must be called under the client_lock */
1586 unhash_client_locked(struct nfs4_client *clp)
1588 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1589 struct nfsd4_session *ses;
1591 /* Mark the client as expired! */
1593 /* Make it invisible */
1594 if (!list_empty(&clp->cl_idhash)) {
1595 list_del_init(&clp->cl_idhash);
1596 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1597 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1599 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1601 list_del_init(&clp->cl_lru);
1602 spin_lock(&clp->cl_lock);
1603 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1604 list_del_init(&ses->se_hash);
1605 spin_unlock(&clp->cl_lock);
1609 unhash_client(struct nfs4_client *clp)
1611 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1613 spin_lock(&nn->client_lock);
1614 unhash_client_locked(clp);
1615 spin_unlock(&nn->client_lock);
1619 __destroy_client(struct nfs4_client *clp)
1621 struct nfs4_openowner *oo;
1622 struct nfs4_delegation *dp;
1623 struct list_head reaplist;
1625 INIT_LIST_HEAD(&reaplist);
1626 spin_lock(&state_lock);
1627 while (!list_empty(&clp->cl_delegations)) {
1628 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1629 unhash_delegation_locked(dp);
1630 list_add(&dp->dl_recall_lru, &reaplist);
1632 spin_unlock(&state_lock);
1633 while (!list_empty(&reaplist)) {
1634 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1635 list_del_init(&dp->dl_recall_lru);
1636 nfs4_put_stid(&dp->dl_stid);
1638 while (!list_empty(&clp->cl_revoked)) {
1639 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1640 list_del_init(&dp->dl_recall_lru);
1641 nfs4_put_stid(&dp->dl_stid);
1643 while (!list_empty(&clp->cl_openowners)) {
1644 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1645 atomic_inc(&oo->oo_owner.so_count);
1646 release_openowner(oo);
1648 nfsd4_shutdown_callback(clp);
1649 if (clp->cl_cb_conn.cb_xprt)
1650 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1655 destroy_client(struct nfs4_client *clp)
1658 __destroy_client(clp);
1661 static void expire_client(struct nfs4_client *clp)
1664 nfsd4_client_record_remove(clp);
1665 __destroy_client(clp);
1668 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1670 memcpy(target->cl_verifier.data, source->data,
1671 sizeof(target->cl_verifier.data));
1674 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1676 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1677 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1680 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1682 if (source->cr_principal) {
1683 target->cr_principal =
1684 kstrdup(source->cr_principal, GFP_KERNEL);
1685 if (target->cr_principal == NULL)
1688 target->cr_principal = NULL;
1689 target->cr_flavor = source->cr_flavor;
1690 target->cr_uid = source->cr_uid;
1691 target->cr_gid = source->cr_gid;
1692 target->cr_group_info = source->cr_group_info;
1693 get_group_info(target->cr_group_info);
1694 target->cr_gss_mech = source->cr_gss_mech;
1695 if (source->cr_gss_mech)
1696 gss_mech_get(source->cr_gss_mech);
1701 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1705 res = o1->len - o2->len;
1708 return (long long)memcmp(o1->data, o2->data, o1->len);
1711 static int same_name(const char *n1, const char *n2)
1713 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1717 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1719 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1723 same_clid(clientid_t *cl1, clientid_t *cl2)
1725 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1728 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1732 if (g1->ngroups != g2->ngroups)
1734 for (i=0; i<g1->ngroups; i++)
1735 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1741 * RFC 3530 language requires clid_inuse be returned when the
1742 * "principal" associated with a requests differs from that previously
1743 * used. We use uid, gid's, and gss principal string as our best
1744 * approximation. We also don't want to allow non-gss use of a client
1745 * established using gss: in theory cr_principal should catch that
1746 * change, but in practice cr_principal can be null even in the gss case
1747 * since gssd doesn't always pass down a principal string.
1749 static bool is_gss_cred(struct svc_cred *cr)
1751 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1752 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1757 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1759 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1760 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1761 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1762 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1764 if (cr1->cr_principal == cr2->cr_principal)
1766 if (!cr1->cr_principal || !cr2->cr_principal)
1768 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1771 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1773 struct svc_cred *cr = &rqstp->rq_cred;
1776 if (!cr->cr_gss_mech)
1778 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1779 return service == RPC_GSS_SVC_INTEGRITY ||
1780 service == RPC_GSS_SVC_PRIVACY;
1783 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1785 struct svc_cred *cr = &rqstp->rq_cred;
1787 if (!cl->cl_mach_cred)
1789 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1791 if (!svc_rqst_integrity_protected(rqstp))
1793 if (!cr->cr_principal)
1795 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1798 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1800 static u32 current_clientid = 1;
1802 clp->cl_clientid.cl_boot = nn->boot_time;
1803 clp->cl_clientid.cl_id = current_clientid++;
1806 static void gen_confirm(struct nfs4_client *clp)
1812 * This is opaque to client, so no need to byte-swap. Use
1813 * __force to keep sparse happy
1815 verf[0] = (__force __be32)get_seconds();
1816 verf[1] = (__force __be32)i++;
1817 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1820 static struct nfs4_stid *
1821 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1823 struct nfs4_stid *ret;
1825 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1826 if (!ret || !ret->sc_type)
1831 static struct nfs4_stid *
1832 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1834 struct nfs4_stid *s;
1836 spin_lock(&cl->cl_lock);
1837 s = find_stateid_locked(cl, t);
1839 if (typemask & s->sc_type)
1840 atomic_inc(&s->sc_count);
1844 spin_unlock(&cl->cl_lock);
1848 static struct nfs4_client *create_client(struct xdr_netobj name,
1849 struct svc_rqst *rqstp, nfs4_verifier *verf)
1851 struct nfs4_client *clp;
1852 struct sockaddr *sa = svc_addr(rqstp);
1854 struct net *net = SVC_NET(rqstp);
1856 clp = alloc_client(name);
1860 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1865 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1866 clp->cl_time = get_seconds();
1867 clear_bit(0, &clp->cl_cb_slot_busy);
1868 copy_verf(clp, verf);
1869 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1871 clp->cl_cb_session = NULL;
1877 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1879 struct rb_node **new = &(root->rb_node), *parent = NULL;
1880 struct nfs4_client *clp;
1883 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1886 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1887 new = &((*new)->rb_left);
1889 new = &((*new)->rb_right);
1892 rb_link_node(&new_clp->cl_namenode, parent, new);
1893 rb_insert_color(&new_clp->cl_namenode, root);
1896 static struct nfs4_client *
1897 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1900 struct rb_node *node = root->rb_node;
1901 struct nfs4_client *clp;
1904 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1905 cmp = compare_blob(&clp->cl_name, name);
1907 node = node->rb_left;
1909 node = node->rb_right;
1917 add_to_unconfirmed(struct nfs4_client *clp)
1919 unsigned int idhashval;
1920 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1922 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1923 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1924 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1925 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1930 move_to_confirmed(struct nfs4_client *clp)
1932 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1933 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1935 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1936 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1937 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1938 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1939 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1943 static struct nfs4_client *
1944 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1946 struct nfs4_client *clp;
1947 unsigned int idhashval = clientid_hashval(clid->cl_id);
1949 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1950 if (same_clid(&clp->cl_clientid, clid)) {
1951 if ((bool)clp->cl_minorversion != sessions)
1960 static struct nfs4_client *
1961 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1963 struct list_head *tbl = nn->conf_id_hashtbl;
1965 return find_client_in_id_table(tbl, clid, sessions);
1968 static struct nfs4_client *
1969 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1971 struct list_head *tbl = nn->unconf_id_hashtbl;
1973 return find_client_in_id_table(tbl, clid, sessions);
1976 static bool clp_used_exchangeid(struct nfs4_client *clp)
1978 return clp->cl_exchange_flags != 0;
1981 static struct nfs4_client *
1982 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1984 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1987 static struct nfs4_client *
1988 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1990 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1994 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1996 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1997 struct sockaddr *sa = svc_addr(rqstp);
1998 u32 scopeid = rpc_get_scope_id(sa);
1999 unsigned short expected_family;
2001 /* Currently, we only support tcp and tcp6 for the callback channel */
2002 if (se->se_callback_netid_len == 3 &&
2003 !memcmp(se->se_callback_netid_val, "tcp", 3))
2004 expected_family = AF_INET;
2005 else if (se->se_callback_netid_len == 4 &&
2006 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2007 expected_family = AF_INET6;
2011 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2012 se->se_callback_addr_len,
2013 (struct sockaddr *)&conn->cb_addr,
2014 sizeof(conn->cb_addr));
2016 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2019 if (conn->cb_addr.ss_family == AF_INET6)
2020 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2022 conn->cb_prog = se->se_callback_prog;
2023 conn->cb_ident = se->se_callback_ident;
2024 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2027 conn->cb_addr.ss_family = AF_UNSPEC;
2028 conn->cb_addrlen = 0;
2029 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2030 "will not receive delegations\n",
2031 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2037 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2040 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2042 struct xdr_buf *buf = resp->xdr.buf;
2043 struct nfsd4_slot *slot = resp->cstate.slot;
2046 dprintk("--> %s slot %p\n", __func__, slot);
2048 slot->sl_opcnt = resp->opcnt;
2049 slot->sl_status = resp->cstate.status;
2051 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2052 if (nfsd4_not_cached(resp)) {
2053 slot->sl_datalen = 0;
2056 base = resp->cstate.data_offset;
2057 slot->sl_datalen = buf->len - base;
2058 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2059 WARN("%s: sessions DRC could not cache compound\n", __func__);
2064 * Encode the replay sequence operation from the slot values.
2065 * If cachethis is FALSE encode the uncached rep error on the next
2066 * operation which sets resp->p and increments resp->opcnt for
2067 * nfs4svc_encode_compoundres.
2071 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2072 struct nfsd4_compoundres *resp)
2074 struct nfsd4_op *op;
2075 struct nfsd4_slot *slot = resp->cstate.slot;
2077 /* Encode the replayed sequence operation */
2078 op = &args->ops[resp->opcnt - 1];
2079 nfsd4_encode_operation(resp, op);
2081 /* Return nfserr_retry_uncached_rep in next operation. */
2082 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2083 op = &args->ops[resp->opcnt++];
2084 op->status = nfserr_retry_uncached_rep;
2085 nfsd4_encode_operation(resp, op);
2091 * The sequence operation is not cached because we can use the slot and
2095 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2096 struct nfsd4_sequence *seq)
2098 struct nfsd4_slot *slot = resp->cstate.slot;
2099 struct xdr_stream *xdr = &resp->xdr;
2103 dprintk("--> %s slot %p\n", __func__, slot);
2105 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2109 p = xdr_reserve_space(xdr, slot->sl_datalen);
2112 return nfserr_serverfault;
2114 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2115 xdr_commit_encode(xdr);
2117 resp->opcnt = slot->sl_opcnt;
2118 return slot->sl_status;
2122 * Set the exchange_id flags returned by the server.
2125 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2127 /* pNFS is not supported */
2128 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2130 /* Referrals are supported, Migration is not. */
2131 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2133 /* set the wire flags to return to client. */
2134 clid->flags = new->cl_exchange_flags;
2137 static bool client_has_state(struct nfs4_client *clp)
2140 * Note clp->cl_openowners check isn't quite right: there's no
2141 * need to count owners without stateid's.
2143 * Also note we should probably be using this in 4.0 case too.
2145 return !list_empty(&clp->cl_openowners)
2146 || !list_empty(&clp->cl_delegations)
2147 || !list_empty(&clp->cl_sessions);
2151 nfsd4_exchange_id(struct svc_rqst *rqstp,
2152 struct nfsd4_compound_state *cstate,
2153 struct nfsd4_exchange_id *exid)
2155 struct nfs4_client *unconf, *conf, *new;
2157 char addr_str[INET6_ADDRSTRLEN];
2158 nfs4_verifier verf = exid->verifier;
2159 struct sockaddr *sa = svc_addr(rqstp);
2160 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2161 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2163 rpc_ntop(sa, addr_str, sizeof(addr_str));
2164 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2165 "ip_addr=%s flags %x, spa_how %d\n",
2166 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2167 addr_str, exid->flags, exid->spa_how);
2169 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2170 return nfserr_inval;
2172 switch (exid->spa_how) {
2174 if (!svc_rqst_integrity_protected(rqstp))
2175 return nfserr_inval;
2178 default: /* checked by xdr code */
2181 return nfserr_encr_alg_unsupp;
2184 new = create_client(exid->clname, rqstp, &verf);
2186 return nfserr_jukebox;
2188 /* Cases below refer to rfc 5661 section 18.35.4: */
2190 conf = find_confirmed_client_by_name(&exid->clname, nn);
2192 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2193 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2196 if (!clp_used_exchangeid(conf)) { /* buggy client */
2197 status = nfserr_inval;
2200 if (!mach_creds_match(conf, rqstp)) {
2201 status = nfserr_wrong_cred;
2204 if (!creds_match) { /* case 9 */
2205 status = nfserr_perm;
2208 if (!verfs_match) { /* case 8 */
2209 status = nfserr_not_same;
2213 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2216 if (!creds_match) { /* case 3 */
2217 if (client_has_state(conf)) {
2218 status = nfserr_clid_inuse;
2221 expire_client(conf);
2224 if (verfs_match) { /* case 2 */
2225 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2228 /* case 5, client reboot */
2232 if (update) { /* case 7 */
2233 status = nfserr_noent;
2237 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2238 if (unconf) /* case 4, possible retry or client restart */
2239 expire_client(unconf);
2241 /* case 1 (normal case) */
2243 new->cl_minorversion = cstate->minorversion;
2244 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2247 add_to_unconfirmed(new);
2251 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2252 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2254 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2255 nfsd4_set_ex_flags(conf, exid);
2257 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2258 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2262 nfs4_unlock_state();
2269 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2271 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2274 /* The slot is in use, and no response has been sent. */
2276 if (seqid == slot_seqid)
2277 return nfserr_jukebox;
2279 return nfserr_seq_misordered;
2281 /* Note unsigned 32-bit arithmetic handles wraparound: */
2282 if (likely(seqid == slot_seqid + 1))
2284 if (seqid == slot_seqid)
2285 return nfserr_replay_cache;
2286 return nfserr_seq_misordered;
2290 * Cache the create session result into the create session single DRC
2291 * slot cache by saving the xdr structure. sl_seqid has been set.
2292 * Do this for solo or embedded create session operations.
2295 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2296 struct nfsd4_clid_slot *slot, __be32 nfserr)
2298 slot->sl_status = nfserr;
2299 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2303 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2304 struct nfsd4_clid_slot *slot)
2306 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2307 return slot->sl_status;
2310 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2311 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2312 1 + /* MIN tag is length with zero, only length */ \
2313 3 + /* version, opcount, opcode */ \
2314 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2315 /* seqid, slotID, slotID, cache */ \
2316 4 ) * sizeof(__be32))
2318 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2319 2 + /* verifier: AUTH_NULL, length 0 */\
2321 1 + /* MIN tag is length with zero, only length */ \
2322 3 + /* opcount, opcode, opstatus*/ \
2323 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2324 /* seqid, slotID, slotID, slotID, status */ \
2325 5 ) * sizeof(__be32))
2327 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2329 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2331 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2332 return nfserr_toosmall;
2333 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2334 return nfserr_toosmall;
2335 ca->headerpadsz = 0;
2336 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2337 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2338 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2339 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2340 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2341 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2343 * Note decreasing slot size below client's request may make it
2344 * difficult for client to function correctly, whereas
2345 * decreasing the number of slots will (just?) affect
2346 * performance. When short on memory we therefore prefer to
2347 * decrease number of slots instead of their size. Clients that
2348 * request larger slots than they need will get poor results:
2350 ca->maxreqs = nfsd4_get_drc_mem(ca);
2352 return nfserr_jukebox;
2357 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2358 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2359 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2360 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2362 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2364 ca->headerpadsz = 0;
2367 * These RPC_MAX_HEADER macros are overkill, especially since we
2368 * don't even do gss on the backchannel yet. But this is still
2369 * less than 1k. Tighten up this estimate in the unlikely event
2370 * it turns out to be a problem for some client:
2372 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2373 return nfserr_toosmall;
2374 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2375 return nfserr_toosmall;
2376 ca->maxresp_cached = 0;
2378 return nfserr_toosmall;
2383 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2385 switch (cbs->flavor) {
2391 * GSS case: the spec doesn't allow us to return this
2392 * error. But it also doesn't allow us not to support
2394 * I'd rather this fail hard than return some error the
2395 * client might think it can already handle:
2397 return nfserr_encr_alg_unsupp;
2402 nfsd4_create_session(struct svc_rqst *rqstp,
2403 struct nfsd4_compound_state *cstate,
2404 struct nfsd4_create_session *cr_ses)
2406 struct sockaddr *sa = svc_addr(rqstp);
2407 struct nfs4_client *conf, *unconf;
2408 struct nfsd4_session *new;
2409 struct nfsd4_conn *conn;
2410 struct nfsd4_clid_slot *cs_slot = NULL;
2412 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2414 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2415 return nfserr_inval;
2416 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2419 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2422 status = check_backchannel_attrs(&cr_ses->back_channel);
2424 goto out_release_drc_mem;
2425 status = nfserr_jukebox;
2426 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2428 goto out_release_drc_mem;
2429 conn = alloc_conn_from_crses(rqstp, cr_ses);
2431 goto out_free_session;
2434 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2435 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2436 WARN_ON_ONCE(conf && unconf);
2439 status = nfserr_wrong_cred;
2440 if (!mach_creds_match(conf, rqstp))
2442 cs_slot = &conf->cl_cs_slot;
2443 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2444 if (status == nfserr_replay_cache) {
2445 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2447 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2448 status = nfserr_seq_misordered;
2451 } else if (unconf) {
2452 struct nfs4_client *old;
2453 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2454 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2455 status = nfserr_clid_inuse;
2458 status = nfserr_wrong_cred;
2459 if (!mach_creds_match(unconf, rqstp))
2461 cs_slot = &unconf->cl_cs_slot;
2462 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2464 /* an unconfirmed replay returns misordered */
2465 status = nfserr_seq_misordered;
2468 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2470 status = mark_client_expired(old);
2475 move_to_confirmed(unconf);
2478 status = nfserr_stale_clientid;
2483 * We do not support RDMA or persistent sessions
2485 cr_ses->flags &= ~SESSION4_PERSIST;
2486 cr_ses->flags &= ~SESSION4_RDMA;
2488 init_session(rqstp, new, conf, cr_ses);
2489 nfsd4_init_conn(rqstp, conn, new);
2491 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2492 NFS4_MAX_SESSIONID_LEN);
2493 cs_slot->sl_seqid++;
2494 cr_ses->seqid = cs_slot->sl_seqid;
2496 /* cache solo and embedded create sessions under the state lock */
2497 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2498 nfs4_unlock_state();
2501 nfs4_unlock_state();
2504 __free_session(new);
2505 out_release_drc_mem:
2506 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2510 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2513 case NFS4_CDFC4_FORE:
2514 case NFS4_CDFC4_BACK:
2516 case NFS4_CDFC4_FORE_OR_BOTH:
2517 case NFS4_CDFC4_BACK_OR_BOTH:
2518 *dir = NFS4_CDFC4_BOTH;
2521 return nfserr_inval;
2524 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2526 struct nfsd4_session *session = cstate->session;
2527 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2530 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2533 spin_lock(&nn->client_lock);
2534 session->se_cb_prog = bc->bc_cb_program;
2535 session->se_cb_sec = bc->bc_cb_sec;
2536 spin_unlock(&nn->client_lock);
2538 nfsd4_probe_callback(session->se_client);
2543 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2544 struct nfsd4_compound_state *cstate,
2545 struct nfsd4_bind_conn_to_session *bcts)
2548 struct nfsd4_conn *conn;
2549 struct nfsd4_session *session;
2550 struct net *net = SVC_NET(rqstp);
2551 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2553 if (!nfsd4_last_compound_op(rqstp))
2554 return nfserr_not_only_op;
2556 spin_lock(&nn->client_lock);
2557 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2558 spin_unlock(&nn->client_lock);
2560 goto out_no_session;
2561 status = nfserr_wrong_cred;
2562 if (!mach_creds_match(session->se_client, rqstp))
2564 status = nfsd4_map_bcts_dir(&bcts->dir);
2567 conn = alloc_conn(rqstp, bcts->dir);
2568 status = nfserr_jukebox;
2571 nfsd4_init_conn(rqstp, conn, session);
2574 nfsd4_put_session(session);
2576 nfs4_unlock_state();
2580 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2584 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2588 nfsd4_destroy_session(struct svc_rqst *r,
2589 struct nfsd4_compound_state *cstate,
2590 struct nfsd4_destroy_session *sessionid)
2592 struct nfsd4_session *ses;
2594 int ref_held_by_me = 0;
2595 struct net *net = SVC_NET(r);
2596 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2599 status = nfserr_not_only_op;
2600 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2601 if (!nfsd4_last_compound_op(r))
2605 dump_sessionid(__func__, &sessionid->sessionid);
2606 spin_lock(&nn->client_lock);
2607 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2609 goto out_client_lock;
2610 status = nfserr_wrong_cred;
2611 if (!mach_creds_match(ses->se_client, r))
2612 goto out_put_session;
2613 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2615 goto out_put_session;
2616 unhash_session(ses);
2617 spin_unlock(&nn->client_lock);
2619 nfsd4_probe_callback_sync(ses->se_client);
2621 spin_lock(&nn->client_lock);
2624 nfsd4_put_session_locked(ses);
2626 spin_unlock(&nn->client_lock);
2628 nfs4_unlock_state();
2632 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2634 struct nfsd4_conn *c;
2636 list_for_each_entry(c, &s->se_conns, cn_persession) {
2637 if (c->cn_xprt == xpt) {
2644 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2646 struct nfs4_client *clp = ses->se_client;
2647 struct nfsd4_conn *c;
2648 __be32 status = nfs_ok;
2651 spin_lock(&clp->cl_lock);
2652 c = __nfsd4_find_conn(new->cn_xprt, ses);
2655 status = nfserr_conn_not_bound_to_session;
2656 if (clp->cl_mach_cred)
2658 __nfsd4_hash_conn(new, ses);
2659 spin_unlock(&clp->cl_lock);
2660 ret = nfsd4_register_conn(new);
2662 /* oops; xprt is already down: */
2663 nfsd4_conn_lost(&new->cn_xpt_user);
2666 spin_unlock(&clp->cl_lock);
2671 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2673 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2675 return args->opcnt > session->se_fchannel.maxops;
2678 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2679 struct nfsd4_session *session)
2681 struct xdr_buf *xb = &rqstp->rq_arg;
2683 return xb->len > session->se_fchannel.maxreq_sz;
2687 nfsd4_sequence(struct svc_rqst *rqstp,
2688 struct nfsd4_compound_state *cstate,
2689 struct nfsd4_sequence *seq)
2691 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2692 struct xdr_stream *xdr = &resp->xdr;
2693 struct nfsd4_session *session;
2694 struct nfs4_client *clp;
2695 struct nfsd4_slot *slot;
2696 struct nfsd4_conn *conn;
2699 struct net *net = SVC_NET(rqstp);
2700 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2702 if (resp->opcnt != 1)
2703 return nfserr_sequence_pos;
2706 * Will be either used or freed by nfsd4_sequence_check_conn
2709 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2711 return nfserr_jukebox;
2713 spin_lock(&nn->client_lock);
2714 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2716 goto out_no_session;
2717 clp = session->se_client;
2719 status = nfserr_too_many_ops;
2720 if (nfsd4_session_too_many_ops(rqstp, session))
2721 goto out_put_session;
2723 status = nfserr_req_too_big;
2724 if (nfsd4_request_too_big(rqstp, session))
2725 goto out_put_session;
2727 status = nfserr_badslot;
2728 if (seq->slotid >= session->se_fchannel.maxreqs)
2729 goto out_put_session;
2731 slot = session->se_slots[seq->slotid];
2732 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2734 /* We do not negotiate the number of slots yet, so set the
2735 * maxslots to the session maxreqs which is used to encode
2736 * sr_highest_slotid and the sr_target_slot id to maxslots */
2737 seq->maxslots = session->se_fchannel.maxreqs;
2739 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2740 slot->sl_flags & NFSD4_SLOT_INUSE);
2741 if (status == nfserr_replay_cache) {
2742 status = nfserr_seq_misordered;
2743 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2744 goto out_put_session;
2745 cstate->slot = slot;
2746 cstate->session = session;
2748 /* Return the cached reply status and set cstate->status
2749 * for nfsd4_proc_compound processing */
2750 status = nfsd4_replay_cache_entry(resp, seq);
2751 cstate->status = nfserr_replay_cache;
2755 goto out_put_session;
2757 status = nfsd4_sequence_check_conn(conn, session);
2760 goto out_put_session;
2762 buflen = (seq->cachethis) ?
2763 session->se_fchannel.maxresp_cached :
2764 session->se_fchannel.maxresp_sz;
2765 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2767 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2768 goto out_put_session;
2769 svc_reserve(rqstp, buflen);
2772 /* Success! bump slot seqid */
2773 slot->sl_seqid = seq->seqid;
2774 slot->sl_flags |= NFSD4_SLOT_INUSE;
2776 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2778 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2780 cstate->slot = slot;
2781 cstate->session = session;
2785 switch (clp->cl_cb_state) {
2787 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2789 case NFSD4_CB_FAULT:
2790 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2793 seq->status_flags = 0;
2795 if (!list_empty(&clp->cl_revoked))
2796 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2800 spin_unlock(&nn->client_lock);
2803 nfsd4_put_session_locked(session);
2804 goto out_no_session;
2808 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2810 struct nfsd4_compound_state *cs = &resp->cstate;
2812 if (nfsd4_has_session(cs)) {
2813 if (cs->status != nfserr_replay_cache) {
2814 nfsd4_store_cache_entry(resp);
2815 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2817 /* Drop session reference that was taken in nfsd4_sequence() */
2818 nfsd4_put_session(cs->session);
2820 put_client_renew(cs->clp);
2824 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2826 struct nfs4_client *conf, *unconf, *clp;
2828 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2831 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2832 conf = find_confirmed_client(&dc->clientid, true, nn);
2833 WARN_ON_ONCE(conf && unconf);
2838 if (client_has_state(conf)) {
2839 status = nfserr_clientid_busy;
2845 status = nfserr_stale_clientid;
2848 if (!mach_creds_match(clp, rqstp)) {
2849 status = nfserr_wrong_cred;
2854 nfs4_unlock_state();
2859 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2863 if (rc->rca_one_fs) {
2864 if (!cstate->current_fh.fh_dentry)
2865 return nfserr_nofilehandle;
2867 * We don't take advantage of the rca_one_fs case.
2868 * That's OK, it's optional, we can safely ignore it.
2874 status = nfserr_complete_already;
2875 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2876 &cstate->session->se_client->cl_flags))
2879 status = nfserr_stale_clientid;
2880 if (is_client_expired(cstate->session->se_client))
2882 * The following error isn't really legal.
2883 * But we only get here if the client just explicitly
2884 * destroyed the client. Surely it no longer cares what
2885 * error it gets back on an operation for the dead
2891 nfsd4_client_record_create(cstate->session->se_client);
2893 nfs4_unlock_state();
2898 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2899 struct nfsd4_setclientid *setclid)
2901 struct xdr_netobj clname = setclid->se_name;
2902 nfs4_verifier clverifier = setclid->se_verf;
2903 struct nfs4_client *conf, *unconf, *new;
2905 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2907 new = create_client(clname, rqstp, &clverifier);
2909 return nfserr_jukebox;
2910 /* Cases below refer to rfc 3530 section 14.2.33: */
2912 conf = find_confirmed_client_by_name(&clname, nn);
2915 status = nfserr_clid_inuse;
2916 if (clp_used_exchangeid(conf))
2918 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2919 char addr_str[INET6_ADDRSTRLEN];
2920 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2922 dprintk("NFSD: setclientid: string in use by client "
2923 "at %s\n", addr_str);
2927 unconf = find_unconfirmed_client_by_name(&clname, nn);
2929 expire_client(unconf);
2930 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2931 /* case 1: probable callback update */
2932 copy_clid(new, conf);
2933 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2935 new->cl_minorversion = 0;
2936 gen_callback(new, setclid, rqstp);
2937 add_to_unconfirmed(new);
2938 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2939 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2940 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2944 nfs4_unlock_state();
2952 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2953 struct nfsd4_compound_state *cstate,
2954 struct nfsd4_setclientid_confirm *setclientid_confirm)
2956 struct nfs4_client *conf, *unconf;
2957 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2958 clientid_t * clid = &setclientid_confirm->sc_clientid;
2960 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2962 if (STALE_CLIENTID(clid, nn))
2963 return nfserr_stale_clientid;
2966 conf = find_confirmed_client(clid, false, nn);
2967 unconf = find_unconfirmed_client(clid, false, nn);
2969 * We try hard to give out unique clientid's, so if we get an
2970 * attempt to confirm the same clientid with a different cred,
2971 * there's a bug somewhere. Let's charitably assume it's our
2974 status = nfserr_serverfault;
2975 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2977 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2979 /* cases below refer to rfc 3530 section 14.2.34: */
2980 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2981 if (conf && !unconf) /* case 2: probable retransmit */
2983 else /* case 4: client hasn't noticed we rebooted yet? */
2984 status = nfserr_stale_clientid;
2988 if (conf) { /* case 1: callback update */
2989 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2990 nfsd4_probe_callback(conf);
2991 expire_client(unconf);
2992 } else { /* case 3: normal case; new or rebooted client */
2993 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2995 status = mark_client_expired(conf);
2998 expire_client(conf);
3000 move_to_confirmed(unconf);
3001 nfsd4_probe_callback(unconf);
3004 nfs4_unlock_state();
3008 static struct nfs4_file *nfsd4_alloc_file(void)
3010 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3013 /* OPEN Share state helper functions */
3014 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3016 unsigned int hashval = file_hashval(fh);
3018 lockdep_assert_held(&state_lock);
3020 atomic_set(&fp->fi_ref, 1);
3021 spin_lock_init(&fp->fi_lock);
3022 INIT_LIST_HEAD(&fp->fi_stateids);
3023 INIT_LIST_HEAD(&fp->fi_delegations);
3024 fh_copy_shallow(&fp->fi_fhandle, fh);
3025 fp->fi_had_conflict = false;
3026 fp->fi_lease = NULL;
3027 fp->fi_share_deny = 0;
3028 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3029 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3030 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3034 nfsd4_free_slabs(void)
3036 kmem_cache_destroy(openowner_slab);
3037 kmem_cache_destroy(lockowner_slab);
3038 kmem_cache_destroy(file_slab);
3039 kmem_cache_destroy(stateid_slab);
3040 kmem_cache_destroy(deleg_slab);
3044 nfsd4_init_slabs(void)
3046 openowner_slab = kmem_cache_create("nfsd4_openowners",
3047 sizeof(struct nfs4_openowner), 0, 0, NULL);
3048 if (openowner_slab == NULL)
3050 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3051 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3052 if (lockowner_slab == NULL)
3053 goto out_free_openowner_slab;
3054 file_slab = kmem_cache_create("nfsd4_files",
3055 sizeof(struct nfs4_file), 0, 0, NULL);
3056 if (file_slab == NULL)
3057 goto out_free_lockowner_slab;
3058 stateid_slab = kmem_cache_create("nfsd4_stateids",
3059 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3060 if (stateid_slab == NULL)
3061 goto out_free_file_slab;
3062 deleg_slab = kmem_cache_create("nfsd4_delegations",
3063 sizeof(struct nfs4_delegation), 0, 0, NULL);
3064 if (deleg_slab == NULL)
3065 goto out_free_stateid_slab;
3068 out_free_stateid_slab:
3069 kmem_cache_destroy(stateid_slab);
3071 kmem_cache_destroy(file_slab);
3072 out_free_lockowner_slab:
3073 kmem_cache_destroy(lockowner_slab);
3074 out_free_openowner_slab:
3075 kmem_cache_destroy(openowner_slab);
3077 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3081 static void init_nfs4_replay(struct nfs4_replay *rp)
3083 rp->rp_status = nfserr_serverfault;
3085 rp->rp_buf = rp->rp_ibuf;
3086 mutex_init(&rp->rp_mutex);
3089 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3090 struct nfs4_stateowner *so)
3092 if (!nfsd4_has_session(cstate)) {
3093 mutex_lock(&so->so_replay.rp_mutex);
3094 cstate->replay_owner = so;
3095 atomic_inc(&so->so_count);
3099 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3101 struct nfs4_stateowner *so = cstate->replay_owner;
3104 cstate->replay_owner = NULL;
3105 mutex_unlock(&so->so_replay.rp_mutex);
3106 nfs4_put_stateowner(so);
3110 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3112 struct nfs4_stateowner *sop;
3114 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3118 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3119 if (!sop->so_owner.data) {
3120 kmem_cache_free(slab, sop);
3123 sop->so_owner.len = owner->len;
3125 INIT_LIST_HEAD(&sop->so_stateids);
3126 sop->so_client = clp;
3127 init_nfs4_replay(&sop->so_replay);
3128 atomic_set(&sop->so_count, 1);
3132 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3134 lockdep_assert_held(&clp->cl_lock);
3136 list_add(&oo->oo_owner.so_strhash,
3137 &clp->cl_ownerstr_hashtbl[strhashval]);
3138 list_add(&oo->oo_perclient, &clp->cl_openowners);
3141 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3143 unhash_openowner_locked(openowner(so));
3146 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3148 struct nfs4_openowner *oo = openowner(so);
3150 kmem_cache_free(openowner_slab, oo);
3153 static const struct nfs4_stateowner_operations openowner_ops = {
3154 .so_unhash = nfs4_unhash_openowner,
3155 .so_free = nfs4_free_openowner,
3158 static struct nfs4_openowner *
3159 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3160 struct nfsd4_compound_state *cstate)
3162 struct nfs4_client *clp = cstate->clp;
3163 struct nfs4_openowner *oo, *ret;
3165 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3168 oo->oo_owner.so_ops = &openowner_ops;
3169 oo->oo_owner.so_is_open_owner = 1;
3170 oo->oo_owner.so_seqid = open->op_seqid;
3172 if (nfsd4_has_session(cstate))
3173 oo->oo_flags |= NFS4_OO_CONFIRMED;
3175 oo->oo_last_closed_stid = NULL;
3176 INIT_LIST_HEAD(&oo->oo_close_lru);
3177 spin_lock(&clp->cl_lock);
3178 ret = find_openstateowner_str_locked(strhashval, open, clp);
3180 hash_openowner(oo, clp, strhashval);
3183 nfs4_free_openowner(&oo->oo_owner);
3184 spin_unlock(&clp->cl_lock);
3188 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3189 struct nfs4_openowner *oo = open->op_openowner;
3191 atomic_inc(&stp->st_stid.sc_count);
3192 stp->st_stid.sc_type = NFS4_OPEN_STID;
3193 INIT_LIST_HEAD(&stp->st_locks);
3194 stp->st_stateowner = &oo->oo_owner;
3195 atomic_inc(&stp->st_stateowner->so_count);
3197 stp->st_stid.sc_file = fp;
3198 stp->st_access_bmap = 0;
3199 stp->st_deny_bmap = 0;
3200 stp->st_openstp = NULL;
3201 spin_lock(&oo->oo_owner.so_client->cl_lock);
3202 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3203 spin_lock(&fp->fi_lock);
3204 list_add(&stp->st_perfile, &fp->fi_stateids);
3205 spin_unlock(&fp->fi_lock);
3206 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3210 * In the 4.0 case we need to keep the owners around a little while to handle
3211 * CLOSE replay. We still do need to release any file access that is held by
3212 * them before returning however.
3215 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3217 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3218 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3221 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3224 * We know that we hold one reference via nfsd4_close, and another
3225 * "persistent" reference for the client. If the refcount is higher
3226 * than 2, then there are still calls in progress that are using this
3227 * stateid. We can't put the sc_file reference until they are finished.
3228 * Wait for the refcount to drop to 2. Since it has been unhashed,
3229 * there should be no danger of the refcount going back up again at
3232 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3234 release_all_access(s);
3235 if (s->st_stid.sc_file) {
3236 put_nfs4_file(s->st_stid.sc_file);
3237 s->st_stid.sc_file = NULL;
3239 release_last_closed_stateid(oo);
3240 oo->oo_last_closed_stid = s;
3241 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3242 oo->oo_time = get_seconds();
3245 /* search file_hashtbl[] for file */
3246 static struct nfs4_file *
3247 find_file_locked(struct knfsd_fh *fh)
3249 unsigned int hashval = file_hashval(fh);
3250 struct nfs4_file *fp;
3252 lockdep_assert_held(&state_lock);
3254 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3255 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3263 static struct nfs4_file *
3264 find_file(struct knfsd_fh *fh)
3266 struct nfs4_file *fp;
3268 spin_lock(&state_lock);
3269 fp = find_file_locked(fh);
3270 spin_unlock(&state_lock);
3274 static struct nfs4_file *
3275 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3277 struct nfs4_file *fp;
3279 spin_lock(&state_lock);
3280 fp = find_file_locked(fh);
3282 nfsd4_init_file(new, fh);
3285 spin_unlock(&state_lock);
3291 * Called to check deny when READ with all zero stateid or
3292 * WRITE with all zero or all one stateid
3295 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3297 struct nfs4_file *fp;
3298 __be32 ret = nfs_ok;
3300 fp = find_file(¤t_fh->fh_handle);
3303 /* Check for conflicting share reservations */
3304 spin_lock(&fp->fi_lock);
3305 if (fp->fi_share_deny & deny_type)
3306 ret = nfserr_locked;
3307 spin_unlock(&fp->fi_lock);
3312 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3314 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3317 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3320 * We can't do this in nfsd_break_deleg_cb because it is
3321 * already holding inode->i_lock.
3323 * If the dl_time != 0, then we know that it has already been
3324 * queued for a lease break. Don't queue it again.
3326 spin_lock(&state_lock);
3327 if (dp->dl_time == 0) {
3328 dp->dl_time = get_seconds();
3329 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3331 spin_unlock(&state_lock);
3334 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3337 * We're assuming the state code never drops its reference
3338 * without first removing the lease. Since we're in this lease
3339 * callback (and since the lease code is serialized by the kernel
3340 * lock) we know the server hasn't removed the lease yet, we know
3341 * it's safe to take a reference.
3343 atomic_inc(&dp->dl_stid.sc_count);
3344 nfsd4_cb_recall(dp);
3347 /* Called from break_lease() with i_lock held. */
3348 static void nfsd_break_deleg_cb(struct file_lock *fl)
3350 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3351 struct nfs4_delegation *dp;
3354 WARN(1, "(%p)->fl_owner NULL\n", fl);
3357 if (fp->fi_had_conflict) {
3358 WARN(1, "duplicate break on %p\n", fp);
3362 * We don't want the locks code to timeout the lease for us;
3363 * we'll remove it ourself if a delegation isn't returned
3366 fl->fl_break_time = 0;
3368 spin_lock(&fp->fi_lock);
3369 fp->fi_had_conflict = true;
3371 * If there are no delegations on the list, then we can't count on this
3372 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3373 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3374 * true should keep any new delegations from being hashed.
3376 if (list_empty(&fp->fi_delegations))
3377 fl->fl_break_time = jiffies;
3379 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3380 nfsd_break_one_deleg(dp);
3381 spin_unlock(&fp->fi_lock);
3385 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3388 return lease_modify(onlist, arg);
3393 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3394 .lm_break = nfsd_break_deleg_cb,
3395 .lm_change = nfsd_change_deleg_cb,
3398 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3400 if (nfsd4_has_session(cstate))
3402 if (seqid == so->so_seqid - 1)
3403 return nfserr_replay_me;
3404 if (seqid == so->so_seqid)
3406 return nfserr_bad_seqid;
3409 static __be32 lookup_clientid(clientid_t *clid,
3410 struct nfsd4_compound_state *cstate,
3411 struct nfsd_net *nn)
3413 struct nfs4_client *found;
3416 found = cstate->clp;
3417 if (!same_clid(&found->cl_clientid, clid))
3418 return nfserr_stale_clientid;
3422 if (STALE_CLIENTID(clid, nn))
3423 return nfserr_stale_clientid;
3426 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3427 * cached already then we know this is for is for v4.0 and "sessions"
3430 WARN_ON_ONCE(cstate->session);
3431 found = find_confirmed_client(clid, false, nn);
3433 return nfserr_expired;
3435 /* Cache the nfs4_client in cstate! */
3436 cstate->clp = found;
3437 atomic_inc(&found->cl_refcount);
3442 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3443 struct nfsd4_open *open, struct nfsd_net *nn)
3445 clientid_t *clientid = &open->op_clientid;
3446 struct nfs4_client *clp = NULL;
3447 unsigned int strhashval;
3448 struct nfs4_openowner *oo = NULL;
3451 if (STALE_CLIENTID(&open->op_clientid, nn))
3452 return nfserr_stale_clientid;
3454 * In case we need it later, after we've already created the
3455 * file and don't want to risk a further failure:
3457 open->op_file = nfsd4_alloc_file();
3458 if (open->op_file == NULL)
3459 return nfserr_jukebox;
3461 status = lookup_clientid(clientid, cstate, nn);
3466 strhashval = ownerstr_hashval(&open->op_owner);
3467 oo = find_openstateowner_str(strhashval, open, clp);
3468 open->op_openowner = oo;
3472 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3473 /* Replace unconfirmed owners without checking for replay. */
3474 release_openowner(oo);
3475 open->op_openowner = NULL;
3478 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3483 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3485 return nfserr_jukebox;
3486 open->op_openowner = oo;
3488 open->op_stp = nfs4_alloc_open_stateid(clp);
3490 return nfserr_jukebox;
3494 static inline __be32
3495 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3497 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3498 return nfserr_openmode;
3503 static int share_access_to_flags(u32 share_access)
3505 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3508 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3510 struct nfs4_stid *ret;
3512 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3515 return delegstateid(ret);
3518 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3520 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3521 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3525 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3526 struct nfs4_delegation **dp)
3529 __be32 status = nfserr_bad_stateid;
3530 struct nfs4_delegation *deleg;
3532 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3535 flags = share_access_to_flags(open->op_share_access);
3536 status = nfs4_check_delegmode(deleg, flags);
3538 nfs4_put_stid(&deleg->dl_stid);
3543 if (!nfsd4_is_deleg_cur(open))
3547 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3551 static struct nfs4_ol_stateid *
3552 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3554 struct nfs4_ol_stateid *local, *ret = NULL;
3555 struct nfs4_openowner *oo = open->op_openowner;
3557 spin_lock(&fp->fi_lock);
3558 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3559 /* ignore lock owners */
3560 if (local->st_stateowner->so_is_open_owner == 0)
3562 if (local->st_stateowner == &oo->oo_owner) {
3564 atomic_inc(&ret->st_stid.sc_count);
3568 spin_unlock(&fp->fi_lock);
3572 static inline int nfs4_access_to_access(u32 nfs4_access)
3576 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3577 flags |= NFSD_MAY_READ;
3578 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3579 flags |= NFSD_MAY_WRITE;
3583 static inline __be32
3584 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3585 struct nfsd4_open *open)
3587 struct iattr iattr = {
3588 .ia_valid = ATTR_SIZE,
3591 if (!open->op_truncate)
3593 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3594 return nfserr_inval;
3595 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3598 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3599 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3600 struct nfsd4_open *open)
3602 struct file *filp = NULL;
3604 int oflag = nfs4_access_to_omode(open->op_share_access);
3605 int access = nfs4_access_to_access(open->op_share_access);
3606 unsigned char old_access_bmap, old_deny_bmap;
3608 spin_lock(&fp->fi_lock);
3611 * Are we trying to set a deny mode that would conflict with
3614 status = nfs4_file_check_deny(fp, open->op_share_deny);
3615 if (status != nfs_ok) {
3616 spin_unlock(&fp->fi_lock);
3620 /* set access to the file */
3621 status = nfs4_file_get_access(fp, open->op_share_access);
3622 if (status != nfs_ok) {
3623 spin_unlock(&fp->fi_lock);
3627 /* Set access bits in stateid */
3628 old_access_bmap = stp->st_access_bmap;
3629 set_access(open->op_share_access, stp);
3631 /* Set new deny mask */
3632 old_deny_bmap = stp->st_deny_bmap;
3633 set_deny(open->op_share_deny, stp);
3634 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3636 if (!fp->fi_fds[oflag]) {
3637 spin_unlock(&fp->fi_lock);
3638 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3640 goto out_put_access;
3641 spin_lock(&fp->fi_lock);
3642 if (!fp->fi_fds[oflag]) {
3643 fp->fi_fds[oflag] = filp;
3647 spin_unlock(&fp->fi_lock);
3651 status = nfsd4_truncate(rqstp, cur_fh, open);
3653 goto out_put_access;
3657 stp->st_access_bmap = old_access_bmap;
3658 nfs4_file_put_access(fp, open->op_share_access);
3659 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3664 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)
3667 unsigned char old_deny_bmap;
3669 if (!test_access(open->op_share_access, stp))
3670 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3672 /* test and set deny mode */
3673 spin_lock(&fp->fi_lock);
3674 status = nfs4_file_check_deny(fp, open->op_share_deny);
3675 if (status == nfs_ok) {
3676 old_deny_bmap = stp->st_deny_bmap;
3677 set_deny(open->op_share_deny, stp);
3678 fp->fi_share_deny |=
3679 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3681 spin_unlock(&fp->fi_lock);
3683 if (status != nfs_ok)
3686 status = nfsd4_truncate(rqstp, cur_fh, open);
3687 if (status != nfs_ok)
3688 reset_union_bmap_deny(old_deny_bmap, stp);
3693 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3695 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3698 /* Should we give out recallable state?: */
3699 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3701 if (clp->cl_cb_state == NFSD4_CB_UP)
3704 * In the sessions case, since we don't have to establish a
3705 * separate connection for callbacks, we assume it's OK
3706 * until we hear otherwise:
3708 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3711 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3713 struct file_lock *fl;
3715 fl = locks_alloc_lock();
3718 locks_init_lock(fl);
3719 fl->fl_lmops = &nfsd_lease_mng_ops;
3720 fl->fl_flags = FL_DELEG;
3721 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3722 fl->fl_end = OFFSET_MAX;
3723 fl->fl_owner = (fl_owner_t)fp;
3724 fl->fl_pid = current->tgid;
3728 static int nfs4_setlease(struct nfs4_delegation *dp)
3730 struct nfs4_file *fp = dp->dl_stid.sc_file;
3731 struct file_lock *fl;
3735 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3738 filp = find_readable_file(fp);
3740 /* We should always have a readable file here */
3745 status = vfs_setlease(filp, fl->fl_type, &fl);
3747 locks_free_lock(fl);
3750 spin_lock(&state_lock);
3751 spin_lock(&fp->fi_lock);
3752 /* Did the lease get broken before we took the lock? */
3754 if (fp->fi_had_conflict)
3759 atomic_inc(&fp->fi_delegees);
3760 hash_delegation_locked(dp, fp);
3764 fp->fi_deleg_file = filp;
3765 atomic_set(&fp->fi_delegees, 1);
3766 hash_delegation_locked(dp, fp);
3767 spin_unlock(&fp->fi_lock);
3768 spin_unlock(&state_lock);
3771 spin_unlock(&fp->fi_lock);
3772 spin_unlock(&state_lock);
3778 static struct nfs4_delegation *
3779 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3780 struct nfs4_file *fp)
3783 struct nfs4_delegation *dp;
3785 if (fp->fi_had_conflict)
3786 return ERR_PTR(-EAGAIN);
3788 dp = alloc_init_deleg(clp, fh);
3790 return ERR_PTR(-ENOMEM);
3793 spin_lock(&state_lock);
3794 spin_lock(&fp->fi_lock);
3795 dp->dl_stid.sc_file = fp;
3796 if (!fp->fi_lease) {
3797 spin_unlock(&fp->fi_lock);
3798 spin_unlock(&state_lock);
3799 status = nfs4_setlease(dp);
3802 atomic_inc(&fp->fi_delegees);
3803 if (fp->fi_had_conflict) {
3807 hash_delegation_locked(dp, fp);
3810 spin_unlock(&fp->fi_lock);
3811 spin_unlock(&state_lock);
3814 nfs4_put_stid(&dp->dl_stid);
3815 return ERR_PTR(status);
3820 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3822 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3823 if (status == -EAGAIN)
3824 open->op_why_no_deleg = WND4_CONTENTION;
3826 open->op_why_no_deleg = WND4_RESOURCE;
3827 switch (open->op_deleg_want) {
3828 case NFS4_SHARE_WANT_READ_DELEG:
3829 case NFS4_SHARE_WANT_WRITE_DELEG:
3830 case NFS4_SHARE_WANT_ANY_DELEG:
3832 case NFS4_SHARE_WANT_CANCEL:
3833 open->op_why_no_deleg = WND4_CANCELLED;
3835 case NFS4_SHARE_WANT_NO_DELEG:
3842 * Attempt to hand out a delegation.
3844 * Note we don't support write delegations, and won't until the vfs has
3845 * proper support for them.
3848 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3849 struct nfs4_ol_stateid *stp)
3851 struct nfs4_delegation *dp;
3852 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3853 struct nfs4_client *clp = stp->st_stid.sc_client;
3857 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3858 open->op_recall = 0;
3859 switch (open->op_claim_type) {
3860 case NFS4_OPEN_CLAIM_PREVIOUS:
3862 open->op_recall = 1;
3863 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3866 case NFS4_OPEN_CLAIM_NULL:
3867 case NFS4_OPEN_CLAIM_FH:
3869 * Let's not give out any delegations till everyone's
3870 * had the chance to reclaim theirs....
3872 if (locks_in_grace(clp->net))
3874 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3877 * Also, if the file was opened for write or
3878 * create, there's a good chance the client's
3879 * about to write to it, resulting in an
3880 * immediate recall (since we don't support
3881 * write delegations):
3883 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3885 if (open->op_create == NFS4_OPEN_CREATE)
3891 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3895 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3897 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3898 STATEID_VAL(&dp->dl_stid.sc_stateid));
3899 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3900 nfs4_put_stid(&dp->dl_stid);
3903 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3904 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3905 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3906 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3907 open->op_recall = 1;
3910 /* 4.1 client asking for a delegation? */
3911 if (open->op_deleg_want)
3912 nfsd4_open_deleg_none_ext(open, status);
3916 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3917 struct nfs4_delegation *dp)
3919 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3920 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3921 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3922 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3923 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3924 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3925 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3926 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3928 /* Otherwise the client must be confused wanting a delegation
3929 * it already has, therefore we don't return
3930 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3935 * called with nfs4_lock_state() held.
3938 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3940 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3941 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3942 struct nfs4_file *fp = NULL;
3943 struct nfs4_ol_stateid *stp = NULL;
3944 struct nfs4_delegation *dp = NULL;
3948 * Lookup file; if found, lookup stateid and check open request,
3949 * and check for delegations in the process of being recalled.
3950 * If not found, create the nfs4_file struct
3952 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3953 if (fp != open->op_file) {
3954 status = nfs4_check_deleg(cl, open, &dp);
3957 stp = nfsd4_find_existing_open(fp, open);
3959 open->op_file = NULL;
3960 status = nfserr_bad_stateid;
3961 if (nfsd4_is_deleg_cur(open))
3963 status = nfserr_jukebox;
3967 * OPEN the file, or upgrade an existing OPEN.
3968 * If truncate fails, the OPEN fails.
3971 /* Stateid was found, this is an OPEN upgrade */
3972 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3977 open->op_stp = NULL;
3978 init_open_stateid(stp, fp, open);
3979 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3981 release_open_stateid(stp);
3985 update_stateid(&stp->st_stid.sc_stateid);
3986 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3988 if (nfsd4_has_session(&resp->cstate)) {
3989 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3990 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3991 open->op_why_no_deleg = WND4_NOT_WANTED;
3997 * Attempt to hand out a delegation. No error return, because the
3998 * OPEN succeeds even if we fail.
4000 nfs4_open_delegation(current_fh, open, stp);
4004 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4005 STATEID_VAL(&stp->st_stid.sc_stateid));
4007 /* 4.1 client trying to upgrade/downgrade delegation? */
4008 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4009 open->op_deleg_want)
4010 nfsd4_deleg_xgrade_none_ext(open, dp);
4014 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4015 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4017 * To finish the open response, we just need to set the rflags.
4019 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4020 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4021 !nfsd4_has_session(&resp->cstate))
4022 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4024 nfs4_put_stid(&dp->dl_stid);
4026 nfs4_put_stid(&stp->st_stid);
4031 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4032 struct nfsd4_open *open, __be32 status)
4034 if (open->op_openowner) {
4035 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4037 nfsd4_cstate_assign_replay(cstate, so);
4038 nfs4_put_stateowner(so);
4041 nfsd4_free_file(open->op_file);
4043 nfs4_put_stid(&open->op_stp->st_stid);
4047 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4050 struct nfs4_client *clp;
4052 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4055 dprintk("process_renew(%08x/%08x): starting\n",
4056 clid->cl_boot, clid->cl_id);
4057 status = lookup_clientid(clid, cstate, nn);
4061 status = nfserr_cb_path_down;
4062 if (!list_empty(&clp->cl_delegations)
4063 && clp->cl_cb_state != NFSD4_CB_UP)
4067 nfs4_unlock_state();
4072 nfsd4_end_grace(struct nfsd_net *nn)
4074 /* do nothing if grace period already ended */
4075 if (nn->grace_ended)
4078 dprintk("NFSD: end of grace period\n");
4079 nn->grace_ended = true;
4080 nfsd4_record_grace_done(nn, nn->boot_time);
4081 locks_end_grace(&nn->nfsd4_manager);
4083 * Now that every NFSv4 client has had the chance to recover and
4084 * to see the (possibly new, possibly shorter) lease time, we
4085 * can safely set the next grace time to the current lease time:
4087 nn->nfsd4_grace = nn->nfsd4_lease;
4091 nfs4_laundromat(struct nfsd_net *nn)
4093 struct nfs4_client *clp;
4094 struct nfs4_openowner *oo;
4095 struct nfs4_delegation *dp;
4096 struct list_head *pos, *next, reaplist;
4097 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4098 time_t t, new_timeo = nn->nfsd4_lease;
4102 dprintk("NFSD: laundromat service - starting\n");
4103 nfsd4_end_grace(nn);
4104 INIT_LIST_HEAD(&reaplist);
4105 spin_lock(&nn->client_lock);
4106 list_for_each_safe(pos, next, &nn->client_lru) {
4107 clp = list_entry(pos, struct nfs4_client, cl_lru);
4108 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4109 t = clp->cl_time - cutoff;
4110 new_timeo = min(new_timeo, t);
4113 if (mark_client_expired_locked(clp)) {
4114 dprintk("NFSD: client in use (clientid %08x)\n",
4115 clp->cl_clientid.cl_id);
4118 unhash_client_locked(clp);
4119 list_add(&clp->cl_lru, &reaplist);
4121 spin_unlock(&nn->client_lock);
4122 list_for_each_safe(pos, next, &reaplist) {
4123 clp = list_entry(pos, struct nfs4_client, cl_lru);
4124 dprintk("NFSD: purging unused client (clientid %08x)\n",
4125 clp->cl_clientid.cl_id);
4126 list_del_init(&clp->cl_lru);
4129 spin_lock(&state_lock);
4130 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4131 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4132 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4134 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4135 t = dp->dl_time - cutoff;
4136 new_timeo = min(new_timeo, t);
4139 unhash_delegation_locked(dp);
4140 list_add(&dp->dl_recall_lru, &reaplist);
4142 spin_unlock(&state_lock);
4143 while (!list_empty(&reaplist)) {
4144 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4146 list_del_init(&dp->dl_recall_lru);
4147 revoke_delegation(dp);
4149 list_for_each_safe(pos, next, &nn->close_lru) {
4150 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4151 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4152 t = oo->oo_time - cutoff;
4153 new_timeo = min(new_timeo, t);
4156 release_last_closed_stateid(oo);
4158 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4159 nfs4_unlock_state();
4163 static struct workqueue_struct *laundry_wq;
4164 static void laundromat_main(struct work_struct *);
4167 laundromat_main(struct work_struct *laundry)
4170 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4172 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4175 t = nfs4_laundromat(nn);
4176 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4177 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4180 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4182 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4183 return nfserr_bad_stateid;
4188 access_permit_read(struct nfs4_ol_stateid *stp)
4190 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4191 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4192 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4196 access_permit_write(struct nfs4_ol_stateid *stp)
4198 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4199 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4203 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4205 __be32 status = nfserr_openmode;
4207 /* For lock stateid's, we test the parent open, not the lock: */
4208 if (stp->st_openstp)
4209 stp = stp->st_openstp;
4210 if ((flags & WR_STATE) && !access_permit_write(stp))
4212 if ((flags & RD_STATE) && !access_permit_read(stp))
4219 static inline __be32
4220 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4222 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4224 else if (locks_in_grace(net)) {
4225 /* Answer in remaining cases depends on existence of
4226 * conflicting state; so we must wait out the grace period. */
4227 return nfserr_grace;
4228 } else if (flags & WR_STATE)
4229 return nfs4_share_conflict(current_fh,
4230 NFS4_SHARE_DENY_WRITE);
4231 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4232 return nfs4_share_conflict(current_fh,
4233 NFS4_SHARE_DENY_READ);
4237 * Allow READ/WRITE during grace period on recovered state only for files
4238 * that are not able to provide mandatory locking.
4241 grace_disallows_io(struct net *net, struct inode *inode)
4243 return locks_in_grace(net) && mandatory_lock(inode);
4246 /* Returns true iff a is later than b: */
4247 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4249 return (s32)(a->si_generation - b->si_generation) > 0;
4252 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4255 * When sessions are used the stateid generation number is ignored
4258 if (has_session && in->si_generation == 0)
4261 if (in->si_generation == ref->si_generation)
4264 /* If the client sends us a stateid from the future, it's buggy: */
4265 if (stateid_generation_after(in, ref))
4266 return nfserr_bad_stateid;
4268 * However, we could see a stateid from the past, even from a
4269 * non-buggy client. For example, if the client sends a lock
4270 * while some IO is outstanding, the lock may bump si_generation
4271 * while the IO is still in flight. The client could avoid that
4272 * situation by waiting for responses on all the IO requests,
4273 * but better performance may result in retrying IO that
4274 * receives an old_stateid error if requests are rarely
4275 * reordered in flight:
4277 return nfserr_old_stateid;
4280 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4282 struct nfs4_stid *s;
4283 struct nfs4_ol_stateid *ols;
4284 __be32 status = nfserr_bad_stateid;
4286 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4288 /* Client debugging aid. */
4289 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4290 char addr_str[INET6_ADDRSTRLEN];
4291 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4293 pr_warn_ratelimited("NFSD: client %s testing state ID "
4294 "with incorrect client ID\n", addr_str);
4297 spin_lock(&cl->cl_lock);
4298 s = find_stateid_locked(cl, stateid);
4301 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4304 switch (s->sc_type) {
4305 case NFS4_DELEG_STID:
4308 case NFS4_REVOKED_DELEG_STID:
4309 status = nfserr_deleg_revoked;
4311 case NFS4_OPEN_STID:
4312 case NFS4_LOCK_STID:
4313 ols = openlockstateid(s);
4314 if (ols->st_stateowner->so_is_open_owner
4315 && !(openowner(ols->st_stateowner)->oo_flags
4316 & NFS4_OO_CONFIRMED))
4317 status = nfserr_bad_stateid;
4322 printk("unknown stateid type %x\n", s->sc_type);
4324 case NFS4_CLOSED_STID:
4325 case NFS4_CLOSED_DELEG_STID:
4326 status = nfserr_bad_stateid;
4329 spin_unlock(&cl->cl_lock);
4334 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4335 stateid_t *stateid, unsigned char typemask,
4336 struct nfs4_stid **s, struct nfsd_net *nn)
4340 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4341 return nfserr_bad_stateid;
4342 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4343 if (status == nfserr_stale_clientid) {
4344 if (cstate->session)
4345 return nfserr_bad_stateid;
4346 return nfserr_stale_stateid;
4350 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4352 return nfserr_bad_stateid;
4357 * Checks for stateid operations
4360 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4361 stateid_t *stateid, int flags, struct file **filpp)
4363 struct nfs4_stid *s;
4364 struct nfs4_ol_stateid *stp = NULL;
4365 struct nfs4_delegation *dp = NULL;
4366 struct svc_fh *current_fh = &cstate->current_fh;
4367 struct inode *ino = current_fh->fh_dentry->d_inode;
4368 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4369 struct file *file = NULL;
4375 if (grace_disallows_io(net, ino))
4376 return nfserr_grace;
4378 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4379 return check_special_stateids(net, current_fh, stateid, flags);
4383 status = nfsd4_lookup_stateid(cstate, stateid,
4384 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4388 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4391 switch (s->sc_type) {
4392 case NFS4_DELEG_STID:
4393 dp = delegstateid(s);
4394 status = nfs4_check_delegmode(dp, flags);
4398 file = dp->dl_stid.sc_file->fi_deleg_file;
4401 status = nfserr_serverfault;
4407 case NFS4_OPEN_STID:
4408 case NFS4_LOCK_STID:
4409 stp = openlockstateid(s);
4410 status = nfs4_check_fh(current_fh, stp);
4413 if (stp->st_stateowner->so_is_open_owner
4414 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4416 status = nfs4_check_openmode(stp, flags);
4420 struct nfs4_file *fp = stp->st_stid.sc_file;
4422 if (flags & RD_STATE)
4423 file = find_readable_file(fp);
4425 file = find_writeable_file(fp);
4429 status = nfserr_bad_stateid;
4438 nfs4_unlock_state();
4443 * Test if the stateid is valid
4446 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4447 struct nfsd4_test_stateid *test_stateid)
4449 struct nfsd4_test_stateid_id *stateid;
4450 struct nfs4_client *cl = cstate->session->se_client;
4453 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4454 stateid->ts_id_status =
4455 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4456 nfs4_unlock_state();
4462 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4463 struct nfsd4_free_stateid *free_stateid)
4465 stateid_t *stateid = &free_stateid->fr_stateid;
4466 struct nfs4_stid *s;
4467 struct nfs4_delegation *dp;
4468 struct nfs4_ol_stateid *stp;
4469 struct nfs4_client *cl = cstate->session->se_client;
4470 __be32 ret = nfserr_bad_stateid;
4473 spin_lock(&cl->cl_lock);
4474 s = find_stateid_locked(cl, stateid);
4477 switch (s->sc_type) {
4478 case NFS4_DELEG_STID:
4479 ret = nfserr_locks_held;
4481 case NFS4_OPEN_STID:
4482 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4485 ret = nfserr_locks_held;
4487 case NFS4_LOCK_STID:
4488 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4491 stp = openlockstateid(s);
4492 ret = nfserr_locks_held;
4493 if (check_for_locks(stp->st_stid.sc_file,
4494 lockowner(stp->st_stateowner)))
4496 unhash_lock_stateid(stp);
4497 spin_unlock(&cl->cl_lock);
4501 case NFS4_REVOKED_DELEG_STID:
4502 dp = delegstateid(s);
4503 list_del_init(&dp->dl_recall_lru);
4504 spin_unlock(&cl->cl_lock);
4508 /* Default falls through and returns nfserr_bad_stateid */
4511 spin_unlock(&cl->cl_lock);
4513 nfs4_unlock_state();
4520 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4521 RD_STATE : WR_STATE;
4524 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4526 struct svc_fh *current_fh = &cstate->current_fh;
4527 struct nfs4_stateowner *sop = stp->st_stateowner;
4530 status = nfsd4_check_seqid(cstate, sop, seqid);
4533 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4534 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4536 * "Closed" stateid's exist *only* to return
4537 * nfserr_replay_me from the previous step, and
4538 * revoked delegations are kept only for free_stateid.
4540 return nfserr_bad_stateid;
4541 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4544 return nfs4_check_fh(current_fh, stp);
4548 * Checks for sequence id mutating operations.
4551 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4552 stateid_t *stateid, char typemask,
4553 struct nfs4_ol_stateid **stpp,
4554 struct nfsd_net *nn)
4557 struct nfs4_stid *s;
4558 struct nfs4_ol_stateid *stp = NULL;
4560 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4561 seqid, STATEID_VAL(stateid));
4564 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4567 stp = openlockstateid(s);
4568 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4570 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4574 nfs4_put_stid(&stp->st_stid);
4578 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4579 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4582 struct nfs4_openowner *oo;
4583 struct nfs4_ol_stateid *stp;
4585 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4586 NFS4_OPEN_STID, &stp, nn);
4589 oo = openowner(stp->st_stateowner);
4590 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4591 nfs4_put_stid(&stp->st_stid);
4592 return nfserr_bad_stateid;
4599 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4600 struct nfsd4_open_confirm *oc)
4603 struct nfs4_openowner *oo;
4604 struct nfs4_ol_stateid *stp;
4605 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4607 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4608 cstate->current_fh.fh_dentry);
4610 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4616 status = nfs4_preprocess_seqid_op(cstate,
4617 oc->oc_seqid, &oc->oc_req_stateid,
4618 NFS4_OPEN_STID, &stp, nn);
4621 oo = openowner(stp->st_stateowner);
4622 status = nfserr_bad_stateid;
4623 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4625 oo->oo_flags |= NFS4_OO_CONFIRMED;
4626 update_stateid(&stp->st_stid.sc_stateid);
4627 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4628 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4629 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4631 nfsd4_client_record_create(oo->oo_owner.so_client);
4634 nfs4_put_stid(&stp->st_stid);
4636 nfsd4_bump_seqid(cstate, status);
4637 nfs4_unlock_state();
4641 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4643 if (!test_access(access, stp))
4645 nfs4_file_put_access(stp->st_stid.sc_file, access);
4646 clear_access(access, stp);
4649 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4651 switch (to_access) {
4652 case NFS4_SHARE_ACCESS_READ:
4653 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4654 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4656 case NFS4_SHARE_ACCESS_WRITE:
4657 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4658 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4660 case NFS4_SHARE_ACCESS_BOTH:
4668 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4669 struct nfsd4_compound_state *cstate,
4670 struct nfsd4_open_downgrade *od)
4673 struct nfs4_ol_stateid *stp;
4674 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4676 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4677 cstate->current_fh.fh_dentry);
4679 /* We don't yet support WANT bits: */
4680 if (od->od_deleg_want)
4681 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4685 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4686 &od->od_stateid, &stp, nn);
4689 status = nfserr_inval;
4690 if (!test_access(od->od_share_access, stp)) {
4691 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4692 stp->st_access_bmap, od->od_share_access);
4695 if (!test_deny(od->od_share_deny, stp)) {
4696 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4697 stp->st_deny_bmap, od->od_share_deny);
4700 nfs4_stateid_downgrade(stp, od->od_share_access);
4702 reset_union_bmap_deny(od->od_share_deny, stp);
4704 update_stateid(&stp->st_stid.sc_stateid);
4705 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4708 nfs4_put_stid(&stp->st_stid);
4710 nfsd4_bump_seqid(cstate, status);
4711 nfs4_unlock_state();
4715 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4717 struct nfs4_client *clp = s->st_stid.sc_client;
4718 LIST_HEAD(reaplist);
4720 s->st_stid.sc_type = NFS4_CLOSED_STID;
4721 spin_lock(&clp->cl_lock);
4722 unhash_open_stateid(s, &reaplist);
4724 if (clp->cl_minorversion) {
4725 put_ol_stateid_locked(s, &reaplist);
4726 spin_unlock(&clp->cl_lock);
4727 free_ol_stateid_reaplist(&reaplist);
4729 spin_unlock(&clp->cl_lock);
4730 free_ol_stateid_reaplist(&reaplist);
4731 move_to_close_lru(s, clp->net);
4736 * nfs4_unlock_state() called after encode
4739 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4740 struct nfsd4_close *close)
4743 struct nfs4_ol_stateid *stp;
4744 struct net *net = SVC_NET(rqstp);
4745 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4747 dprintk("NFSD: nfsd4_close on file %pd\n",
4748 cstate->current_fh.fh_dentry);
4751 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4753 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4755 nfsd4_bump_seqid(cstate, status);
4758 update_stateid(&stp->st_stid.sc_stateid);
4759 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4761 nfsd4_close_open_stateid(stp);
4763 /* put reference from nfs4_preprocess_seqid_op */
4764 nfs4_put_stid(&stp->st_stid);
4766 nfs4_unlock_state();
4771 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4772 struct nfsd4_delegreturn *dr)
4774 struct nfs4_delegation *dp;
4775 stateid_t *stateid = &dr->dr_stateid;
4776 struct nfs4_stid *s;
4778 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4780 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4784 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4787 dp = delegstateid(s);
4788 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4792 destroy_delegation(dp);
4794 nfs4_put_stid(&dp->dl_stid);
4796 nfs4_unlock_state();
4802 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4805 end_offset(u64 start, u64 len)
4810 return end >= start ? end: NFS4_MAX_UINT64;
4813 /* last octet in a range */
4815 last_byte_offset(u64 start, u64 len)
4821 return end > start ? end - 1: NFS4_MAX_UINT64;
4825 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4826 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4827 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4828 * locking, this prevents us from being completely protocol-compliant. The
4829 * real solution to this problem is to start using unsigned file offsets in
4830 * the VFS, but this is a very deep change!
4833 nfs4_transform_lock_offset(struct file_lock *lock)
4835 if (lock->fl_start < 0)
4836 lock->fl_start = OFFSET_MAX;
4837 if (lock->fl_end < 0)
4838 lock->fl_end = OFFSET_MAX;
4841 /* Hack!: For now, we're defining this just so we can use a pointer to it
4842 * as a unique cookie to identify our (NFSv4's) posix locks. */
4843 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4847 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4849 struct nfs4_lockowner *lo;
4851 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4852 lo = (struct nfs4_lockowner *) fl->fl_owner;
4853 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4854 lo->lo_owner.so_owner.len, GFP_KERNEL);
4855 if (!deny->ld_owner.data)
4856 /* We just don't care that much */
4858 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4859 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4862 deny->ld_owner.len = 0;
4863 deny->ld_owner.data = NULL;
4864 deny->ld_clientid.cl_boot = 0;
4865 deny->ld_clientid.cl_id = 0;
4867 deny->ld_start = fl->fl_start;
4868 deny->ld_length = NFS4_MAX_UINT64;
4869 if (fl->fl_end != NFS4_MAX_UINT64)
4870 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4871 deny->ld_type = NFS4_READ_LT;
4872 if (fl->fl_type != F_RDLCK)
4873 deny->ld_type = NFS4_WRITE_LT;
4876 static struct nfs4_lockowner *
4877 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4878 struct nfs4_client *clp)
4880 unsigned int strhashval = ownerstr_hashval(owner);
4881 struct nfs4_stateowner *so;
4883 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4885 if (so->so_is_open_owner)
4887 if (!same_owner_str(so, owner))
4889 atomic_inc(&so->so_count);
4890 return lockowner(so);
4895 static struct nfs4_lockowner *
4896 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4897 struct nfs4_client *clp)
4899 struct nfs4_lockowner *lo;
4901 spin_lock(&clp->cl_lock);
4902 lo = find_lockowner_str_locked(clid, owner, clp);
4903 spin_unlock(&clp->cl_lock);
4907 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4909 unhash_lockowner_locked(lockowner(sop));
4912 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4914 struct nfs4_lockowner *lo = lockowner(sop);
4916 kmem_cache_free(lockowner_slab, lo);
4919 static const struct nfs4_stateowner_operations lockowner_ops = {
4920 .so_unhash = nfs4_unhash_lockowner,
4921 .so_free = nfs4_free_lockowner,
4925 * Alloc a lock owner structure.
4926 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4929 * strhashval = ownerstr_hashval
4931 static struct nfs4_lockowner *
4932 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4933 struct nfs4_ol_stateid *open_stp,
4934 struct nfsd4_lock *lock)
4936 struct nfs4_lockowner *lo, *ret;
4938 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4941 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4942 lo->lo_owner.so_is_open_owner = 0;
4943 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4944 lo->lo_owner.so_ops = &lockowner_ops;
4945 spin_lock(&clp->cl_lock);
4946 ret = find_lockowner_str_locked(&clp->cl_clientid,
4947 &lock->lk_new_owner, clp);
4949 list_add(&lo->lo_owner.so_strhash,
4950 &clp->cl_ownerstr_hashtbl[strhashval]);
4953 nfs4_free_lockowner(&lo->lo_owner);
4954 spin_unlock(&clp->cl_lock);
4959 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4960 struct nfs4_file *fp, struct inode *inode,
4961 struct nfs4_ol_stateid *open_stp)
4963 struct nfs4_client *clp = lo->lo_owner.so_client;
4965 lockdep_assert_held(&clp->cl_lock);
4967 atomic_inc(&stp->st_stid.sc_count);
4968 stp->st_stid.sc_type = NFS4_LOCK_STID;
4969 stp->st_stateowner = &lo->lo_owner;
4970 atomic_inc(&lo->lo_owner.so_count);
4972 stp->st_stid.sc_file = fp;
4973 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4974 stp->st_access_bmap = 0;
4975 stp->st_deny_bmap = open_stp->st_deny_bmap;
4976 stp->st_openstp = open_stp;
4977 list_add(&stp->st_locks, &open_stp->st_locks);
4978 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4979 spin_lock(&fp->fi_lock);
4980 list_add(&stp->st_perfile, &fp->fi_stateids);
4981 spin_unlock(&fp->fi_lock);
4984 static struct nfs4_ol_stateid *
4985 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4987 struct nfs4_ol_stateid *lst;
4988 struct nfs4_client *clp = lo->lo_owner.so_client;
4990 lockdep_assert_held(&clp->cl_lock);
4992 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4993 if (lst->st_stid.sc_file == fp) {
4994 atomic_inc(&lst->st_stid.sc_count);
5001 static struct nfs4_ol_stateid *
5002 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5003 struct inode *inode, struct nfs4_ol_stateid *ost,
5006 struct nfs4_stid *ns = NULL;
5007 struct nfs4_ol_stateid *lst;
5008 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5009 struct nfs4_client *clp = oo->oo_owner.so_client;
5011 spin_lock(&clp->cl_lock);
5012 lst = find_lock_stateid(lo, fi);
5014 spin_unlock(&clp->cl_lock);
5015 ns = nfs4_alloc_stid(clp, stateid_slab);
5019 spin_lock(&clp->cl_lock);
5020 lst = find_lock_stateid(lo, fi);
5022 lst = openlockstateid(ns);
5023 init_lock_stateid(lst, lo, fi, inode, ost);
5028 spin_unlock(&clp->cl_lock);
5035 check_lock_length(u64 offset, u64 length)
5037 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5038 LOFF_OVERFLOW(offset, length)));
5041 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5043 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5045 lockdep_assert_held(&fp->fi_lock);
5047 if (test_access(access, lock_stp))
5049 __nfs4_file_get_access(fp, access);
5050 set_access(access, lock_stp);
5054 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5055 struct nfs4_ol_stateid *ost,
5056 struct nfsd4_lock *lock,
5057 struct nfs4_ol_stateid **lst, bool *new)
5060 struct nfs4_file *fi = ost->st_stid.sc_file;
5061 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5062 struct nfs4_client *cl = oo->oo_owner.so_client;
5063 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5064 struct nfs4_lockowner *lo;
5065 unsigned int strhashval;
5067 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5069 strhashval = ownerstr_hashval(&lock->v.new.owner);
5070 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5072 return nfserr_jukebox;
5074 /* with an existing lockowner, seqids must be the same */
5075 status = nfserr_bad_seqid;
5076 if (!cstate->minorversion &&
5077 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5081 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5083 status = nfserr_jukebox;
5088 nfs4_put_stateowner(&lo->lo_owner);
5096 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5097 struct nfsd4_lock *lock)
5099 struct nfs4_openowner *open_sop = NULL;
5100 struct nfs4_lockowner *lock_sop = NULL;
5101 struct nfs4_ol_stateid *lock_stp = NULL;
5102 struct nfs4_ol_stateid *open_stp = NULL;
5103 struct nfs4_file *fp;
5104 struct file *filp = NULL;
5105 struct file_lock *file_lock = NULL;
5106 struct file_lock *conflock = NULL;
5111 struct net *net = SVC_NET(rqstp);
5112 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5114 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5115 (long long) lock->lk_offset,
5116 (long long) lock->lk_length);
5118 if (check_lock_length(lock->lk_offset, lock->lk_length))
5119 return nfserr_inval;
5121 if ((status = fh_verify(rqstp, &cstate->current_fh,
5122 S_IFREG, NFSD_MAY_LOCK))) {
5123 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5129 if (lock->lk_is_new) {
5130 if (nfsd4_has_session(cstate))
5131 /* See rfc 5661 18.10.3: given clientid is ignored: */
5132 memcpy(&lock->v.new.clientid,
5133 &cstate->session->se_client->cl_clientid,
5134 sizeof(clientid_t));
5136 status = nfserr_stale_clientid;
5137 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5140 /* validate and update open stateid and open seqid */
5141 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5142 lock->lk_new_open_seqid,
5143 &lock->lk_new_open_stateid,
5147 open_sop = openowner(open_stp->st_stateowner);
5148 status = nfserr_bad_stateid;
5149 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5150 &lock->v.new.clientid))
5152 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5155 status = nfs4_preprocess_seqid_op(cstate,
5156 lock->lk_old_lock_seqid,
5157 &lock->lk_old_lock_stateid,
5158 NFS4_LOCK_STID, &lock_stp, nn);
5162 lock_sop = lockowner(lock_stp->st_stateowner);
5164 lkflg = setlkflg(lock->lk_type);
5165 status = nfs4_check_openmode(lock_stp, lkflg);
5169 status = nfserr_grace;
5170 if (locks_in_grace(net) && !lock->lk_reclaim)
5172 status = nfserr_no_grace;
5173 if (!locks_in_grace(net) && lock->lk_reclaim)
5176 file_lock = locks_alloc_lock();
5178 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5179 status = nfserr_jukebox;
5183 fp = lock_stp->st_stid.sc_file;
5184 locks_init_lock(file_lock);
5185 switch (lock->lk_type) {
5188 spin_lock(&fp->fi_lock);
5189 filp = find_readable_file_locked(fp);
5191 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5192 spin_unlock(&fp->fi_lock);
5193 file_lock->fl_type = F_RDLCK;
5196 case NFS4_WRITEW_LT:
5197 spin_lock(&fp->fi_lock);
5198 filp = find_writeable_file_locked(fp);
5200 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5201 spin_unlock(&fp->fi_lock);
5202 file_lock->fl_type = F_WRLCK;
5205 status = nfserr_inval;
5209 status = nfserr_openmode;
5212 file_lock->fl_owner = (fl_owner_t)lock_sop;
5213 file_lock->fl_pid = current->tgid;
5214 file_lock->fl_file = filp;
5215 file_lock->fl_flags = FL_POSIX;
5216 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5217 file_lock->fl_start = lock->lk_offset;
5218 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5219 nfs4_transform_lock_offset(file_lock);
5221 conflock = locks_alloc_lock();
5223 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5224 status = nfserr_jukebox;
5228 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5230 case 0: /* success! */
5231 update_stateid(&lock_stp->st_stid.sc_stateid);
5232 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5236 case (EAGAIN): /* conflock holds conflicting lock */
5237 status = nfserr_denied;
5238 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5239 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5242 status = nfserr_deadlock;
5245 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5246 status = nfserrno(err);
5253 /* Bump seqid manually if the 4.0 replay owner is openowner */
5254 if (cstate->replay_owner &&
5255 cstate->replay_owner != &lock_sop->lo_owner &&
5256 seqid_mutating_err(ntohl(status)))
5257 lock_sop->lo_owner.so_seqid++;
5260 * If this is a new, never-before-used stateid, and we are
5261 * returning an error, then just go ahead and release it.
5264 release_lock_stateid(lock_stp);
5266 nfs4_put_stid(&lock_stp->st_stid);
5269 nfs4_put_stid(&open_stp->st_stid);
5270 nfsd4_bump_seqid(cstate, status);
5271 nfs4_unlock_state();
5273 locks_free_lock(file_lock);
5275 locks_free_lock(conflock);
5280 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5281 * so we do a temporary open here just to get an open file to pass to
5282 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5285 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5288 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5290 err = nfserrno(vfs_test_lock(file, lock));
5300 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5301 struct nfsd4_lockt *lockt)
5303 struct file_lock *file_lock = NULL;
5304 struct nfs4_lockowner *lo = NULL;
5306 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5308 if (locks_in_grace(SVC_NET(rqstp)))
5309 return nfserr_grace;
5311 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5312 return nfserr_inval;
5316 if (!nfsd4_has_session(cstate)) {
5317 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5322 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5325 file_lock = locks_alloc_lock();
5327 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5328 status = nfserr_jukebox;
5331 locks_init_lock(file_lock);
5332 switch (lockt->lt_type) {
5335 file_lock->fl_type = F_RDLCK;
5338 case NFS4_WRITEW_LT:
5339 file_lock->fl_type = F_WRLCK;
5342 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5343 status = nfserr_inval;
5347 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5350 file_lock->fl_owner = (fl_owner_t)lo;
5351 file_lock->fl_pid = current->tgid;
5352 file_lock->fl_flags = FL_POSIX;
5354 file_lock->fl_start = lockt->lt_offset;
5355 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5357 nfs4_transform_lock_offset(file_lock);
5359 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5363 if (file_lock->fl_type != F_UNLCK) {
5364 status = nfserr_denied;
5365 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5369 nfs4_put_stateowner(&lo->lo_owner);
5370 nfs4_unlock_state();
5372 locks_free_lock(file_lock);
5377 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5378 struct nfsd4_locku *locku)
5380 struct nfs4_ol_stateid *stp;
5381 struct file *filp = NULL;
5382 struct file_lock *file_lock = NULL;
5385 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5387 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5388 (long long) locku->lu_offset,
5389 (long long) locku->lu_length);
5391 if (check_lock_length(locku->lu_offset, locku->lu_length))
5392 return nfserr_inval;
5396 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5397 &locku->lu_stateid, NFS4_LOCK_STID,
5401 filp = find_any_file(stp->st_stid.sc_file);
5403 status = nfserr_lock_range;
5406 file_lock = locks_alloc_lock();
5408 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5409 status = nfserr_jukebox;
5412 locks_init_lock(file_lock);
5413 file_lock->fl_type = F_UNLCK;
5414 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5415 file_lock->fl_pid = current->tgid;
5416 file_lock->fl_file = filp;
5417 file_lock->fl_flags = FL_POSIX;
5418 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5419 file_lock->fl_start = locku->lu_offset;
5421 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5423 nfs4_transform_lock_offset(file_lock);
5425 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5427 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5430 update_stateid(&stp->st_stid.sc_stateid);
5431 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5435 nfs4_put_stid(&stp->st_stid);
5437 nfsd4_bump_seqid(cstate, status);
5438 nfs4_unlock_state();
5440 locks_free_lock(file_lock);
5444 status = nfserrno(err);
5450 * true: locks held by lockowner
5451 * false: no locks held by lockowner
5454 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5456 struct file_lock **flpp;
5458 struct file *filp = find_any_file(fp);
5459 struct inode *inode;
5462 /* Any valid lock stateid should have some sort of access */
5467 inode = file_inode(filp);
5469 spin_lock(&inode->i_lock);
5470 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5471 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5476 spin_unlock(&inode->i_lock);
5482 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5483 struct nfsd4_compound_state *cstate,
5484 struct nfsd4_release_lockowner *rlockowner)
5486 clientid_t *clid = &rlockowner->rl_clientid;
5487 struct nfs4_stateowner *sop;
5488 struct nfs4_lockowner *lo = NULL;
5489 struct nfs4_ol_stateid *stp;
5490 struct xdr_netobj *owner = &rlockowner->rl_owner;
5491 unsigned int hashval = ownerstr_hashval(owner);
5493 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5494 struct nfs4_client *clp;
5496 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5497 clid->cl_boot, clid->cl_id);
5501 status = lookup_clientid(clid, cstate, nn);
5506 /* Find the matching lock stateowner */
5507 spin_lock(&clp->cl_lock);
5508 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5511 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5514 /* see if there are still any locks associated with it */
5515 lo = lockowner(sop);
5516 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5517 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5518 status = nfserr_locks_held;
5519 spin_unlock(&clp->cl_lock);
5524 atomic_inc(&sop->so_count);
5527 spin_unlock(&clp->cl_lock);
5529 release_lockowner(lo);
5531 nfs4_unlock_state();
5535 static inline struct nfs4_client_reclaim *
5538 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5542 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5544 struct nfs4_client_reclaim *crp;
5546 crp = nfsd4_find_reclaim_client(name, nn);
5547 return (crp && crp->cr_clp);
5551 * failure => all reset bets are off, nfserr_no_grace...
5553 struct nfs4_client_reclaim *
5554 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5556 unsigned int strhashval;
5557 struct nfs4_client_reclaim *crp;
5559 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5560 crp = alloc_reclaim();
5562 strhashval = clientstr_hashval(name);
5563 INIT_LIST_HEAD(&crp->cr_strhash);
5564 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5565 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5567 nn->reclaim_str_hashtbl_size++;
5573 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5575 list_del(&crp->cr_strhash);
5577 nn->reclaim_str_hashtbl_size--;
5581 nfs4_release_reclaim(struct nfsd_net *nn)
5583 struct nfs4_client_reclaim *crp = NULL;
5586 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5587 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5588 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5589 struct nfs4_client_reclaim, cr_strhash);
5590 nfs4_remove_reclaim_record(crp, nn);
5593 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5597 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5598 struct nfs4_client_reclaim *
5599 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5601 unsigned int strhashval;
5602 struct nfs4_client_reclaim *crp = NULL;
5604 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5606 strhashval = clientstr_hashval(recdir);
5607 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5608 if (same_name(crp->cr_recdir, recdir)) {
5616 * Called from OPEN. Look for clientid in reclaim list.
5619 nfs4_check_open_reclaim(clientid_t *clid,
5620 struct nfsd4_compound_state *cstate,
5621 struct nfsd_net *nn)
5625 /* find clientid in conf_id_hashtbl */
5626 status = lookup_clientid(clid, cstate, nn);
5628 return nfserr_reclaim_bad;
5630 if (nfsd4_client_record_check(cstate->clp))
5631 return nfserr_reclaim_bad;
5636 #ifdef CONFIG_NFSD_FAULT_INJECTION
5638 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5640 if (mark_client_expired(clp))
5646 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5648 char buf[INET6_ADDRSTRLEN];
5649 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5650 printk(KERN_INFO "NFS Client: %s\n", buf);
5654 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5657 char buf[INET6_ADDRSTRLEN];
5658 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5659 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5662 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5663 void (*func)(struct nfs4_ol_stateid *))
5665 struct nfs4_openowner *oop;
5666 struct nfs4_ol_stateid *stp, *st_next;
5667 struct nfs4_ol_stateid *lst, *lst_next;
5670 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5671 list_for_each_entry_safe(stp, st_next,
5672 &oop->oo_owner.so_stateids, st_perstateowner) {
5673 list_for_each_entry_safe(lst, lst_next,
5674 &stp->st_locks, st_locks) {
5686 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5688 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5691 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5693 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5694 nfsd_print_count(clp, count, "locked files");
5698 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5700 struct nfs4_openowner *oop, *next;
5703 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5713 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5715 return nfsd_foreach_client_open(clp, max, release_openowner);
5718 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5720 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5721 nfsd_print_count(clp, count, "open files");
5725 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5726 struct list_head *victims)
5728 struct nfs4_delegation *dp, *next;
5731 lockdep_assert_held(&state_lock);
5732 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5735 * It's not safe to mess with delegations that have a
5736 * non-zero dl_time. They might have already been broken
5737 * and could be processed by the laundromat outside of
5738 * the state_lock. Just leave them be.
5740 if (dp->dl_time != 0)
5743 unhash_delegation_locked(dp);
5744 list_add(&dp->dl_recall_lru, victims);
5752 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5754 struct nfs4_delegation *dp, *next;
5758 spin_lock(&state_lock);
5759 count = nfsd_find_all_delegations(clp, max, &victims);
5760 spin_unlock(&state_lock);
5762 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5763 list_del_init(&dp->dl_recall_lru);
5764 revoke_delegation(dp);
5770 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5772 struct nfs4_delegation *dp;
5776 spin_lock(&state_lock);
5777 count = nfsd_find_all_delegations(clp, max, &victims);
5778 while (!list_empty(&victims)) {
5779 dp = list_first_entry(&victims, struct nfs4_delegation,
5781 list_del_init(&dp->dl_recall_lru);
5783 nfsd_break_one_deleg(dp);
5785 spin_unlock(&state_lock);
5790 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5794 spin_lock(&state_lock);
5795 count = nfsd_find_all_delegations(clp, max, NULL);
5796 spin_unlock(&state_lock);
5798 nfsd_print_count(clp, count, "delegations");
5802 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5804 struct nfs4_client *clp, *next;
5806 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5808 if (!nfsd_netns_ready(nn))
5811 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5812 count += func(clp, max - count);
5813 if ((max != 0) && (count >= max))
5820 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5822 struct nfs4_client *clp;
5823 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5825 if (!nfsd_netns_ready(nn))
5828 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5829 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5835 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5838 * Since the lifetime of a delegation isn't limited to that of an open, a
5839 * client may quite reasonably hang on to a delegation as long as it has
5840 * the inode cached. This becomes an obvious problem the first time a
5841 * client's inode cache approaches the size of the server's total memory.
5843 * For now we avoid this problem by imposing a hard limit on the number
5844 * of delegations, which varies according to the server's memory size.
5847 set_max_delegations(void)
5850 * Allow at most 4 delegations per megabyte of RAM. Quick
5851 * estimates suggest that in the worst case (where every delegation
5852 * is for a different inode), a delegation could take about 1.5K,
5853 * giving a worst case usage of about 6% of memory.
5855 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5858 static int nfs4_state_create_net(struct net *net)
5860 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5863 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5864 CLIENT_HASH_SIZE, GFP_KERNEL);
5865 if (!nn->conf_id_hashtbl)
5867 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5868 CLIENT_HASH_SIZE, GFP_KERNEL);
5869 if (!nn->unconf_id_hashtbl)
5871 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5872 SESSION_HASH_SIZE, GFP_KERNEL);
5873 if (!nn->sessionid_hashtbl)
5876 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5877 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5878 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5880 for (i = 0; i < SESSION_HASH_SIZE; i++)
5881 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5882 nn->conf_name_tree = RB_ROOT;
5883 nn->unconf_name_tree = RB_ROOT;
5884 INIT_LIST_HEAD(&nn->client_lru);
5885 INIT_LIST_HEAD(&nn->close_lru);
5886 INIT_LIST_HEAD(&nn->del_recall_lru);
5887 spin_lock_init(&nn->client_lock);
5889 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5895 kfree(nn->unconf_id_hashtbl);
5897 kfree(nn->conf_id_hashtbl);
5903 nfs4_state_destroy_net(struct net *net)
5906 struct nfs4_client *clp = NULL;
5907 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5909 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5910 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5911 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5912 destroy_client(clp);
5916 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5917 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5918 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5919 destroy_client(clp);
5923 kfree(nn->sessionid_hashtbl);
5924 kfree(nn->unconf_id_hashtbl);
5925 kfree(nn->conf_id_hashtbl);
5930 nfs4_state_start_net(struct net *net)
5932 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5935 ret = nfs4_state_create_net(net);
5938 nfsd4_client_tracking_init(net);
5939 nn->boot_time = get_seconds();
5940 locks_start_grace(net, &nn->nfsd4_manager);
5941 nn->grace_ended = false;
5942 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5943 nn->nfsd4_grace, net);
5944 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5948 /* initialization to perform when the nfsd service is started: */
5951 nfs4_state_start(void)
5955 ret = set_callback_cred();
5958 laundry_wq = create_singlethread_workqueue("nfsd4");
5959 if (laundry_wq == NULL) {
5963 ret = nfsd4_create_callback_queue();
5965 goto out_free_laundry;
5967 set_max_delegations();
5972 destroy_workqueue(laundry_wq);
5978 nfs4_state_shutdown_net(struct net *net)
5980 struct nfs4_delegation *dp = NULL;
5981 struct list_head *pos, *next, reaplist;
5982 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5984 cancel_delayed_work_sync(&nn->laundromat_work);
5985 locks_end_grace(&nn->nfsd4_manager);
5988 INIT_LIST_HEAD(&reaplist);
5989 spin_lock(&state_lock);
5990 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5991 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5992 unhash_delegation_locked(dp);
5993 list_add(&dp->dl_recall_lru, &reaplist);
5995 spin_unlock(&state_lock);
5996 list_for_each_safe(pos, next, &reaplist) {
5997 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5998 list_del_init(&dp->dl_recall_lru);
5999 nfs4_put_stid(&dp->dl_stid);
6002 nfsd4_client_tracking_exit(net);
6003 nfs4_state_destroy_net(net);
6004 nfs4_unlock_state();
6008 nfs4_state_shutdown(void)
6010 destroy_workqueue(laundry_wq);
6011 nfsd4_destroy_callback_queue();
6015 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6017 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6018 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6022 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6024 if (cstate->minorversion) {
6025 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6026 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6031 clear_current_stateid(struct nfsd4_compound_state *cstate)
6033 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6037 * functions to set current state id
6040 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6042 put_stateid(cstate, &odp->od_stateid);
6046 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6048 put_stateid(cstate, &open->op_stateid);
6052 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6054 put_stateid(cstate, &close->cl_stateid);
6058 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6060 put_stateid(cstate, &lock->lk_resp_stateid);
6064 * functions to consume current state id
6068 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6070 get_stateid(cstate, &odp->od_stateid);
6074 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6076 get_stateid(cstate, &drp->dr_stateid);
6080 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6082 get_stateid(cstate, &fsp->fr_stateid);
6086 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6088 get_stateid(cstate, &setattr->sa_stateid);
6092 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6094 get_stateid(cstate, &close->cl_stateid);
6098 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6100 get_stateid(cstate, &locku->lu_stateid);
6104 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6106 get_stateid(cstate, &read->rd_stateid);
6110 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6112 get_stateid(cstate, &write->wr_stateid);