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_generic_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);
986 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
988 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
990 spin_lock(&oo->oo_owner.so_client->cl_lock);
991 list_del(&stp->st_locks);
992 unhash_generic_stateid(stp);
993 unhash_stid(&stp->st_stid);
994 spin_unlock(&oo->oo_owner.so_client->cl_lock);
995 nfs4_put_stid(&stp->st_stid);
998 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1000 struct nfs4_client *clp = lo->lo_owner.so_client;
1002 lockdep_assert_held(&clp->cl_lock);
1004 list_del_init(&lo->lo_owner.so_strhash);
1007 static void release_lockowner_stateids(struct nfs4_lockowner *lo)
1009 struct nfs4_client *clp = lo->lo_owner.so_client;
1010 struct nfs4_ol_stateid *stp;
1012 lockdep_assert_held(&clp->cl_lock);
1014 while (!list_empty(&lo->lo_owner.so_stateids)) {
1015 stp = list_first_entry(&lo->lo_owner.so_stateids,
1016 struct nfs4_ol_stateid, st_perstateowner);
1017 spin_unlock(&clp->cl_lock);
1018 release_lock_stateid(stp);
1019 spin_lock(&clp->cl_lock);
1023 static void release_lockowner(struct nfs4_lockowner *lo)
1025 struct nfs4_client *clp = lo->lo_owner.so_client;
1027 spin_lock(&clp->cl_lock);
1028 unhash_lockowner_locked(lo);
1029 release_lockowner_stateids(lo);
1030 spin_unlock(&clp->cl_lock);
1031 nfs4_put_stateowner(&lo->lo_owner);
1034 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
1035 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1036 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
1038 struct nfs4_ol_stateid *stp;
1040 while (!list_empty(&open_stp->st_locks)) {
1041 stp = list_entry(open_stp->st_locks.next,
1042 struct nfs4_ol_stateid, st_locks);
1043 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
1044 release_lock_stateid(stp);
1045 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
1049 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
1051 spin_lock(&stp->st_stateowner->so_client->cl_lock);
1052 unhash_generic_stateid(stp);
1053 release_open_stateid_locks(stp);
1054 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
1057 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1059 unhash_open_stateid(stp);
1060 nfs4_put_stid(&stp->st_stid);
1063 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1065 struct nfs4_client *clp = oo->oo_owner.so_client;
1067 lockdep_assert_held(&clp->cl_lock);
1069 list_del_init(&oo->oo_owner.so_strhash);
1070 list_del_init(&oo->oo_perclient);
1073 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1075 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1078 list_del_init(&oo->oo_close_lru);
1079 oo->oo_last_closed_stid = NULL;
1080 nfs4_put_stid(&s->st_stid);
1084 static void release_openowner_stateids(struct nfs4_openowner *oo)
1086 struct nfs4_ol_stateid *stp;
1087 struct nfs4_client *clp = oo->oo_owner.so_client;
1089 lockdep_assert_held(&clp->cl_lock);
1091 while (!list_empty(&oo->oo_owner.so_stateids)) {
1092 stp = list_first_entry(&oo->oo_owner.so_stateids,
1093 struct nfs4_ol_stateid, st_perstateowner);
1094 spin_unlock(&clp->cl_lock);
1095 release_open_stateid(stp);
1096 spin_lock(&clp->cl_lock);
1100 static void release_openowner(struct nfs4_openowner *oo)
1102 struct nfs4_client *clp = oo->oo_owner.so_client;
1104 spin_lock(&clp->cl_lock);
1105 unhash_openowner_locked(oo);
1106 release_openowner_stateids(oo);
1107 spin_unlock(&clp->cl_lock);
1108 release_last_closed_stateid(oo);
1109 nfs4_put_stateowner(&oo->oo_owner);
1113 hash_sessionid(struct nfs4_sessionid *sessionid)
1115 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1117 return sid->sequence % SESSION_HASH_SIZE;
1122 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1124 u32 *ptr = (u32 *)(&sessionid->data[0]);
1125 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1129 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1135 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1136 * won't be used for replay.
1138 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1140 struct nfs4_stateowner *so = cstate->replay_owner;
1142 if (nfserr == nfserr_replay_me)
1145 if (!seqid_mutating_err(ntohl(nfserr))) {
1146 nfsd4_cstate_clear_replay(cstate);
1151 if (so->so_is_open_owner)
1152 release_last_closed_stateid(openowner(so));
1158 gen_sessionid(struct nfsd4_session *ses)
1160 struct nfs4_client *clp = ses->se_client;
1161 struct nfsd4_sessionid *sid;
1163 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1164 sid->clientid = clp->cl_clientid;
1165 sid->sequence = current_sessionid++;
1170 * The protocol defines ca_maxresponssize_cached to include the size of
1171 * the rpc header, but all we need to cache is the data starting after
1172 * the end of the initial SEQUENCE operation--the rest we regenerate
1173 * each time. Therefore we can advertise a ca_maxresponssize_cached
1174 * value that is the number of bytes in our cache plus a few additional
1175 * bytes. In order to stay on the safe side, and not promise more than
1176 * we can cache, those additional bytes must be the minimum possible: 24
1177 * bytes of rpc header (xid through accept state, with AUTH_NULL
1178 * verifier), 12 for the compound header (with zero-length tag), and 44
1179 * for the SEQUENCE op response:
1181 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1184 free_session_slots(struct nfsd4_session *ses)
1188 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1189 kfree(ses->se_slots[i]);
1193 * We don't actually need to cache the rpc and session headers, so we
1194 * can allocate a little less for each slot:
1196 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1200 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1203 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1204 return size + sizeof(struct nfsd4_slot);
1208 * XXX: If we run out of reserved DRC memory we could (up to a point)
1209 * re-negotiate active sessions and reduce their slot usage to make
1210 * room for new connections. For now we just fail the create session.
1212 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1214 u32 slotsize = slot_bytes(ca);
1215 u32 num = ca->maxreqs;
1218 spin_lock(&nfsd_drc_lock);
1219 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1220 nfsd_drc_max_mem - nfsd_drc_mem_used);
1221 num = min_t(int, num, avail / slotsize);
1222 nfsd_drc_mem_used += num * slotsize;
1223 spin_unlock(&nfsd_drc_lock);
1228 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1230 int slotsize = slot_bytes(ca);
1232 spin_lock(&nfsd_drc_lock);
1233 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1234 spin_unlock(&nfsd_drc_lock);
1237 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1238 struct nfsd4_channel_attrs *battrs)
1240 int numslots = fattrs->maxreqs;
1241 int slotsize = slot_bytes(fattrs);
1242 struct nfsd4_session *new;
1245 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1246 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1247 mem = numslots * sizeof(struct nfsd4_slot *);
1249 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1252 /* allocate each struct nfsd4_slot and data cache in one piece */
1253 for (i = 0; i < numslots; i++) {
1254 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1255 if (!new->se_slots[i])
1259 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1260 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1265 kfree(new->se_slots[i]);
1270 static void free_conn(struct nfsd4_conn *c)
1272 svc_xprt_put(c->cn_xprt);
1276 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1278 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1279 struct nfs4_client *clp = c->cn_session->se_client;
1281 spin_lock(&clp->cl_lock);
1282 if (!list_empty(&c->cn_persession)) {
1283 list_del(&c->cn_persession);
1286 nfsd4_probe_callback(clp);
1287 spin_unlock(&clp->cl_lock);
1290 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1292 struct nfsd4_conn *conn;
1294 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1297 svc_xprt_get(rqstp->rq_xprt);
1298 conn->cn_xprt = rqstp->rq_xprt;
1299 conn->cn_flags = flags;
1300 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1304 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1306 conn->cn_session = ses;
1307 list_add(&conn->cn_persession, &ses->se_conns);
1310 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1312 struct nfs4_client *clp = ses->se_client;
1314 spin_lock(&clp->cl_lock);
1315 __nfsd4_hash_conn(conn, ses);
1316 spin_unlock(&clp->cl_lock);
1319 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1321 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1322 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1325 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1329 nfsd4_hash_conn(conn, ses);
1330 ret = nfsd4_register_conn(conn);
1332 /* oops; xprt is already down: */
1333 nfsd4_conn_lost(&conn->cn_xpt_user);
1334 /* We may have gained or lost a callback channel: */
1335 nfsd4_probe_callback_sync(ses->se_client);
1338 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1340 u32 dir = NFS4_CDFC4_FORE;
1342 if (cses->flags & SESSION4_BACK_CHAN)
1343 dir |= NFS4_CDFC4_BACK;
1344 return alloc_conn(rqstp, dir);
1347 /* must be called under client_lock */
1348 static void nfsd4_del_conns(struct nfsd4_session *s)
1350 struct nfs4_client *clp = s->se_client;
1351 struct nfsd4_conn *c;
1353 spin_lock(&clp->cl_lock);
1354 while (!list_empty(&s->se_conns)) {
1355 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1356 list_del_init(&c->cn_persession);
1357 spin_unlock(&clp->cl_lock);
1359 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1362 spin_lock(&clp->cl_lock);
1364 spin_unlock(&clp->cl_lock);
1367 static void __free_session(struct nfsd4_session *ses)
1369 free_session_slots(ses);
1373 static void free_session(struct nfsd4_session *ses)
1375 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1377 lockdep_assert_held(&nn->client_lock);
1378 nfsd4_del_conns(ses);
1379 nfsd4_put_drc_mem(&ses->se_fchannel);
1380 __free_session(ses);
1383 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1386 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1388 new->se_client = clp;
1391 INIT_LIST_HEAD(&new->se_conns);
1393 new->se_cb_seq_nr = 1;
1394 new->se_flags = cses->flags;
1395 new->se_cb_prog = cses->callback_prog;
1396 new->se_cb_sec = cses->cb_sec;
1397 atomic_set(&new->se_ref, 0);
1398 idx = hash_sessionid(&new->se_sessionid);
1399 spin_lock(&nn->client_lock);
1400 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1401 spin_lock(&clp->cl_lock);
1402 list_add(&new->se_perclnt, &clp->cl_sessions);
1403 spin_unlock(&clp->cl_lock);
1404 spin_unlock(&nn->client_lock);
1406 if (cses->flags & SESSION4_BACK_CHAN) {
1407 struct sockaddr *sa = svc_addr(rqstp);
1409 * This is a little silly; with sessions there's no real
1410 * use for the callback address. Use the peer address
1411 * as a reasonable default for now, but consider fixing
1412 * the rpc client not to require an address in the
1415 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1416 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1420 /* caller must hold client_lock */
1421 static struct nfsd4_session *
1422 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1424 struct nfsd4_session *elem;
1426 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1428 dump_sessionid(__func__, sessionid);
1429 idx = hash_sessionid(sessionid);
1430 /* Search in the appropriate list */
1431 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1432 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1433 NFS4_MAX_SESSIONID_LEN)) {
1438 dprintk("%s: session not found\n", __func__);
1442 static struct nfsd4_session *
1443 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1446 struct nfsd4_session *session;
1447 __be32 status = nfserr_badsession;
1449 session = __find_in_sessionid_hashtbl(sessionid, net);
1452 status = nfsd4_get_session_locked(session);
1460 /* caller must hold client_lock */
1462 unhash_session(struct nfsd4_session *ses)
1464 list_del(&ses->se_hash);
1465 spin_lock(&ses->se_client->cl_lock);
1466 list_del(&ses->se_perclnt);
1467 spin_unlock(&ses->se_client->cl_lock);
1470 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1472 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1474 if (clid->cl_boot == nn->boot_time)
1476 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1477 clid->cl_boot, clid->cl_id, nn->boot_time);
1482 * XXX Should we use a slab cache ?
1483 * This type of memory management is somewhat inefficient, but we use it
1484 * anyway since SETCLIENTID is not a common operation.
1486 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1488 struct nfs4_client *clp;
1491 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1494 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1495 if (clp->cl_name.data == NULL)
1497 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1498 OWNER_HASH_SIZE, GFP_KERNEL);
1499 if (!clp->cl_ownerstr_hashtbl)
1500 goto err_no_hashtbl;
1501 for (i = 0; i < OWNER_HASH_SIZE; i++)
1502 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1503 clp->cl_name.len = name.len;
1504 INIT_LIST_HEAD(&clp->cl_sessions);
1505 idr_init(&clp->cl_stateids);
1506 atomic_set(&clp->cl_refcount, 0);
1507 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1508 INIT_LIST_HEAD(&clp->cl_idhash);
1509 INIT_LIST_HEAD(&clp->cl_openowners);
1510 INIT_LIST_HEAD(&clp->cl_delegations);
1511 INIT_LIST_HEAD(&clp->cl_lru);
1512 INIT_LIST_HEAD(&clp->cl_callbacks);
1513 INIT_LIST_HEAD(&clp->cl_revoked);
1514 spin_lock_init(&clp->cl_lock);
1515 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1518 kfree(clp->cl_name.data);
1525 free_client(struct nfs4_client *clp)
1527 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1529 lockdep_assert_held(&nn->client_lock);
1530 while (!list_empty(&clp->cl_sessions)) {
1531 struct nfsd4_session *ses;
1532 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1534 list_del(&ses->se_perclnt);
1535 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1538 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1539 free_svc_cred(&clp->cl_cred);
1540 kfree(clp->cl_ownerstr_hashtbl);
1541 kfree(clp->cl_name.data);
1542 idr_destroy(&clp->cl_stateids);
1546 /* must be called under the client_lock */
1548 unhash_client_locked(struct nfs4_client *clp)
1550 struct nfsd4_session *ses;
1552 list_del(&clp->cl_lru);
1553 spin_lock(&clp->cl_lock);
1554 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1555 list_del_init(&ses->se_hash);
1556 spin_unlock(&clp->cl_lock);
1560 destroy_client(struct nfs4_client *clp)
1562 struct nfs4_openowner *oo;
1563 struct nfs4_delegation *dp;
1564 struct list_head reaplist;
1565 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1567 INIT_LIST_HEAD(&reaplist);
1568 spin_lock(&state_lock);
1569 while (!list_empty(&clp->cl_delegations)) {
1570 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1571 unhash_delegation_locked(dp);
1572 list_add(&dp->dl_recall_lru, &reaplist);
1574 spin_unlock(&state_lock);
1575 while (!list_empty(&reaplist)) {
1576 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1577 list_del_init(&dp->dl_recall_lru);
1578 nfs4_put_stid(&dp->dl_stid);
1580 while (!list_empty(&clp->cl_revoked)) {
1581 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1582 list_del_init(&dp->dl_recall_lru);
1583 nfs4_put_stid(&dp->dl_stid);
1585 while (!list_empty(&clp->cl_openowners)) {
1586 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1587 atomic_inc(&oo->oo_owner.so_count);
1588 release_openowner(oo);
1590 nfsd4_shutdown_callback(clp);
1591 if (clp->cl_cb_conn.cb_xprt)
1592 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1593 list_del(&clp->cl_idhash);
1594 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1595 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1597 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1598 spin_lock(&nn->client_lock);
1599 unhash_client_locked(clp);
1600 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1602 spin_unlock(&nn->client_lock);
1605 static void expire_client(struct nfs4_client *clp)
1607 nfsd4_client_record_remove(clp);
1608 destroy_client(clp);
1611 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1613 memcpy(target->cl_verifier.data, source->data,
1614 sizeof(target->cl_verifier.data));
1617 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1619 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1620 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1623 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1625 if (source->cr_principal) {
1626 target->cr_principal =
1627 kstrdup(source->cr_principal, GFP_KERNEL);
1628 if (target->cr_principal == NULL)
1631 target->cr_principal = NULL;
1632 target->cr_flavor = source->cr_flavor;
1633 target->cr_uid = source->cr_uid;
1634 target->cr_gid = source->cr_gid;
1635 target->cr_group_info = source->cr_group_info;
1636 get_group_info(target->cr_group_info);
1637 target->cr_gss_mech = source->cr_gss_mech;
1638 if (source->cr_gss_mech)
1639 gss_mech_get(source->cr_gss_mech);
1644 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1648 res = o1->len - o2->len;
1651 return (long long)memcmp(o1->data, o2->data, o1->len);
1654 static int same_name(const char *n1, const char *n2)
1656 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1660 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1662 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1666 same_clid(clientid_t *cl1, clientid_t *cl2)
1668 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1671 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1675 if (g1->ngroups != g2->ngroups)
1677 for (i=0; i<g1->ngroups; i++)
1678 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1684 * RFC 3530 language requires clid_inuse be returned when the
1685 * "principal" associated with a requests differs from that previously
1686 * used. We use uid, gid's, and gss principal string as our best
1687 * approximation. We also don't want to allow non-gss use of a client
1688 * established using gss: in theory cr_principal should catch that
1689 * change, but in practice cr_principal can be null even in the gss case
1690 * since gssd doesn't always pass down a principal string.
1692 static bool is_gss_cred(struct svc_cred *cr)
1694 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1695 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1700 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1702 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1703 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1704 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1705 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1707 if (cr1->cr_principal == cr2->cr_principal)
1709 if (!cr1->cr_principal || !cr2->cr_principal)
1711 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1714 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1716 struct svc_cred *cr = &rqstp->rq_cred;
1719 if (!cr->cr_gss_mech)
1721 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1722 return service == RPC_GSS_SVC_INTEGRITY ||
1723 service == RPC_GSS_SVC_PRIVACY;
1726 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1728 struct svc_cred *cr = &rqstp->rq_cred;
1730 if (!cl->cl_mach_cred)
1732 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1734 if (!svc_rqst_integrity_protected(rqstp))
1736 if (!cr->cr_principal)
1738 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1741 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1743 static u32 current_clientid = 1;
1745 clp->cl_clientid.cl_boot = nn->boot_time;
1746 clp->cl_clientid.cl_id = current_clientid++;
1749 static void gen_confirm(struct nfs4_client *clp)
1755 * This is opaque to client, so no need to byte-swap. Use
1756 * __force to keep sparse happy
1758 verf[0] = (__force __be32)get_seconds();
1759 verf[1] = (__force __be32)i++;
1760 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1763 static struct nfs4_stid *
1764 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1766 struct nfs4_stid *ret;
1768 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1769 if (!ret || !ret->sc_type)
1774 static struct nfs4_stid *
1775 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1777 struct nfs4_stid *s;
1779 spin_lock(&cl->cl_lock);
1780 s = find_stateid_locked(cl, t);
1782 if (typemask & s->sc_type)
1783 atomic_inc(&s->sc_count);
1787 spin_unlock(&cl->cl_lock);
1791 static struct nfs4_client *create_client(struct xdr_netobj name,
1792 struct svc_rqst *rqstp, nfs4_verifier *verf)
1794 struct nfs4_client *clp;
1795 struct sockaddr *sa = svc_addr(rqstp);
1797 struct net *net = SVC_NET(rqstp);
1798 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1800 clp = alloc_client(name);
1804 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1806 spin_lock(&nn->client_lock);
1808 spin_unlock(&nn->client_lock);
1811 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1812 clp->cl_time = get_seconds();
1813 clear_bit(0, &clp->cl_cb_slot_busy);
1814 copy_verf(clp, verf);
1815 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1817 clp->cl_cb_session = NULL;
1823 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1825 struct rb_node **new = &(root->rb_node), *parent = NULL;
1826 struct nfs4_client *clp;
1829 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1832 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1833 new = &((*new)->rb_left);
1835 new = &((*new)->rb_right);
1838 rb_link_node(&new_clp->cl_namenode, parent, new);
1839 rb_insert_color(&new_clp->cl_namenode, root);
1842 static struct nfs4_client *
1843 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1846 struct rb_node *node = root->rb_node;
1847 struct nfs4_client *clp;
1850 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1851 cmp = compare_blob(&clp->cl_name, name);
1853 node = node->rb_left;
1855 node = node->rb_right;
1863 add_to_unconfirmed(struct nfs4_client *clp)
1865 unsigned int idhashval;
1866 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1868 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1869 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1870 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1871 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1876 move_to_confirmed(struct nfs4_client *clp)
1878 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1879 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1881 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1882 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1883 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1884 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1885 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1889 static struct nfs4_client *
1890 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1892 struct nfs4_client *clp;
1893 unsigned int idhashval = clientid_hashval(clid->cl_id);
1895 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1896 if (same_clid(&clp->cl_clientid, clid)) {
1897 if ((bool)clp->cl_minorversion != sessions)
1906 static struct nfs4_client *
1907 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1909 struct list_head *tbl = nn->conf_id_hashtbl;
1911 return find_client_in_id_table(tbl, clid, sessions);
1914 static struct nfs4_client *
1915 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1917 struct list_head *tbl = nn->unconf_id_hashtbl;
1919 return find_client_in_id_table(tbl, clid, sessions);
1922 static bool clp_used_exchangeid(struct nfs4_client *clp)
1924 return clp->cl_exchange_flags != 0;
1927 static struct nfs4_client *
1928 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1930 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1933 static struct nfs4_client *
1934 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1936 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1940 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1942 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1943 struct sockaddr *sa = svc_addr(rqstp);
1944 u32 scopeid = rpc_get_scope_id(sa);
1945 unsigned short expected_family;
1947 /* Currently, we only support tcp and tcp6 for the callback channel */
1948 if (se->se_callback_netid_len == 3 &&
1949 !memcmp(se->se_callback_netid_val, "tcp", 3))
1950 expected_family = AF_INET;
1951 else if (se->se_callback_netid_len == 4 &&
1952 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1953 expected_family = AF_INET6;
1957 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1958 se->se_callback_addr_len,
1959 (struct sockaddr *)&conn->cb_addr,
1960 sizeof(conn->cb_addr));
1962 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1965 if (conn->cb_addr.ss_family == AF_INET6)
1966 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1968 conn->cb_prog = se->se_callback_prog;
1969 conn->cb_ident = se->se_callback_ident;
1970 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1973 conn->cb_addr.ss_family = AF_UNSPEC;
1974 conn->cb_addrlen = 0;
1975 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1976 "will not receive delegations\n",
1977 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1983 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
1986 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1988 struct xdr_buf *buf = resp->xdr.buf;
1989 struct nfsd4_slot *slot = resp->cstate.slot;
1992 dprintk("--> %s slot %p\n", __func__, slot);
1994 slot->sl_opcnt = resp->opcnt;
1995 slot->sl_status = resp->cstate.status;
1997 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1998 if (nfsd4_not_cached(resp)) {
1999 slot->sl_datalen = 0;
2002 base = resp->cstate.data_offset;
2003 slot->sl_datalen = buf->len - base;
2004 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2005 WARN("%s: sessions DRC could not cache compound\n", __func__);
2010 * Encode the replay sequence operation from the slot values.
2011 * If cachethis is FALSE encode the uncached rep error on the next
2012 * operation which sets resp->p and increments resp->opcnt for
2013 * nfs4svc_encode_compoundres.
2017 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2018 struct nfsd4_compoundres *resp)
2020 struct nfsd4_op *op;
2021 struct nfsd4_slot *slot = resp->cstate.slot;
2023 /* Encode the replayed sequence operation */
2024 op = &args->ops[resp->opcnt - 1];
2025 nfsd4_encode_operation(resp, op);
2027 /* Return nfserr_retry_uncached_rep in next operation. */
2028 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2029 op = &args->ops[resp->opcnt++];
2030 op->status = nfserr_retry_uncached_rep;
2031 nfsd4_encode_operation(resp, op);
2037 * The sequence operation is not cached because we can use the slot and
2041 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2042 struct nfsd4_sequence *seq)
2044 struct nfsd4_slot *slot = resp->cstate.slot;
2045 struct xdr_stream *xdr = &resp->xdr;
2049 dprintk("--> %s slot %p\n", __func__, slot);
2051 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2055 p = xdr_reserve_space(xdr, slot->sl_datalen);
2058 return nfserr_serverfault;
2060 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2061 xdr_commit_encode(xdr);
2063 resp->opcnt = slot->sl_opcnt;
2064 return slot->sl_status;
2068 * Set the exchange_id flags returned by the server.
2071 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2073 /* pNFS is not supported */
2074 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2076 /* Referrals are supported, Migration is not. */
2077 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2079 /* set the wire flags to return to client. */
2080 clid->flags = new->cl_exchange_flags;
2083 static bool client_has_state(struct nfs4_client *clp)
2086 * Note clp->cl_openowners check isn't quite right: there's no
2087 * need to count owners without stateid's.
2089 * Also note we should probably be using this in 4.0 case too.
2091 return !list_empty(&clp->cl_openowners)
2092 || !list_empty(&clp->cl_delegations)
2093 || !list_empty(&clp->cl_sessions);
2097 nfsd4_exchange_id(struct svc_rqst *rqstp,
2098 struct nfsd4_compound_state *cstate,
2099 struct nfsd4_exchange_id *exid)
2101 struct nfs4_client *unconf, *conf, *new;
2103 char addr_str[INET6_ADDRSTRLEN];
2104 nfs4_verifier verf = exid->verifier;
2105 struct sockaddr *sa = svc_addr(rqstp);
2106 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2107 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2109 rpc_ntop(sa, addr_str, sizeof(addr_str));
2110 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2111 "ip_addr=%s flags %x, spa_how %d\n",
2112 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2113 addr_str, exid->flags, exid->spa_how);
2115 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2116 return nfserr_inval;
2118 switch (exid->spa_how) {
2120 if (!svc_rqst_integrity_protected(rqstp))
2121 return nfserr_inval;
2124 default: /* checked by xdr code */
2127 return nfserr_encr_alg_unsupp;
2130 /* Cases below refer to rfc 5661 section 18.35.4: */
2132 conf = find_confirmed_client_by_name(&exid->clname, nn);
2134 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2135 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2138 if (!clp_used_exchangeid(conf)) { /* buggy client */
2139 status = nfserr_inval;
2142 if (!mach_creds_match(conf, rqstp)) {
2143 status = nfserr_wrong_cred;
2146 if (!creds_match) { /* case 9 */
2147 status = nfserr_perm;
2150 if (!verfs_match) { /* case 8 */
2151 status = nfserr_not_same;
2155 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2159 if (!creds_match) { /* case 3 */
2160 if (client_has_state(conf)) {
2161 status = nfserr_clid_inuse;
2164 expire_client(conf);
2167 if (verfs_match) { /* case 2 */
2168 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2172 /* case 5, client reboot */
2176 if (update) { /* case 7 */
2177 status = nfserr_noent;
2181 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2182 if (unconf) /* case 4, possible retry or client restart */
2183 expire_client(unconf);
2185 /* case 1 (normal case) */
2187 new = create_client(exid->clname, rqstp, &verf);
2189 status = nfserr_jukebox;
2192 new->cl_minorversion = cstate->minorversion;
2193 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2196 add_to_unconfirmed(new);
2198 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2199 exid->clientid.cl_id = new->cl_clientid.cl_id;
2201 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2202 nfsd4_set_ex_flags(new, exid);
2204 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2205 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2209 nfs4_unlock_state();
2214 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2216 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2219 /* The slot is in use, and no response has been sent. */
2221 if (seqid == slot_seqid)
2222 return nfserr_jukebox;
2224 return nfserr_seq_misordered;
2226 /* Note unsigned 32-bit arithmetic handles wraparound: */
2227 if (likely(seqid == slot_seqid + 1))
2229 if (seqid == slot_seqid)
2230 return nfserr_replay_cache;
2231 return nfserr_seq_misordered;
2235 * Cache the create session result into the create session single DRC
2236 * slot cache by saving the xdr structure. sl_seqid has been set.
2237 * Do this for solo or embedded create session operations.
2240 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2241 struct nfsd4_clid_slot *slot, __be32 nfserr)
2243 slot->sl_status = nfserr;
2244 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2248 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2249 struct nfsd4_clid_slot *slot)
2251 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2252 return slot->sl_status;
2255 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2256 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2257 1 + /* MIN tag is length with zero, only length */ \
2258 3 + /* version, opcount, opcode */ \
2259 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2260 /* seqid, slotID, slotID, cache */ \
2261 4 ) * sizeof(__be32))
2263 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2264 2 + /* verifier: AUTH_NULL, length 0 */\
2266 1 + /* MIN tag is length with zero, only length */ \
2267 3 + /* opcount, opcode, opstatus*/ \
2268 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2269 /* seqid, slotID, slotID, slotID, status */ \
2270 5 ) * sizeof(__be32))
2272 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2274 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2276 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2277 return nfserr_toosmall;
2278 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2279 return nfserr_toosmall;
2280 ca->headerpadsz = 0;
2281 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2282 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2283 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2284 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2285 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2286 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2288 * Note decreasing slot size below client's request may make it
2289 * difficult for client to function correctly, whereas
2290 * decreasing the number of slots will (just?) affect
2291 * performance. When short on memory we therefore prefer to
2292 * decrease number of slots instead of their size. Clients that
2293 * request larger slots than they need will get poor results:
2295 ca->maxreqs = nfsd4_get_drc_mem(ca);
2297 return nfserr_jukebox;
2302 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2303 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2304 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2305 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2307 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2309 ca->headerpadsz = 0;
2312 * These RPC_MAX_HEADER macros are overkill, especially since we
2313 * don't even do gss on the backchannel yet. But this is still
2314 * less than 1k. Tighten up this estimate in the unlikely event
2315 * it turns out to be a problem for some client:
2317 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2318 return nfserr_toosmall;
2319 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2320 return nfserr_toosmall;
2321 ca->maxresp_cached = 0;
2323 return nfserr_toosmall;
2328 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2330 switch (cbs->flavor) {
2336 * GSS case: the spec doesn't allow us to return this
2337 * error. But it also doesn't allow us not to support
2339 * I'd rather this fail hard than return some error the
2340 * client might think it can already handle:
2342 return nfserr_encr_alg_unsupp;
2347 nfsd4_create_session(struct svc_rqst *rqstp,
2348 struct nfsd4_compound_state *cstate,
2349 struct nfsd4_create_session *cr_ses)
2351 struct sockaddr *sa = svc_addr(rqstp);
2352 struct nfs4_client *conf, *unconf;
2353 struct nfsd4_session *new;
2354 struct nfsd4_conn *conn;
2355 struct nfsd4_clid_slot *cs_slot = NULL;
2357 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2359 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2360 return nfserr_inval;
2361 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2364 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2367 status = check_backchannel_attrs(&cr_ses->back_channel);
2369 goto out_release_drc_mem;
2370 status = nfserr_jukebox;
2371 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2373 goto out_release_drc_mem;
2374 conn = alloc_conn_from_crses(rqstp, cr_ses);
2376 goto out_free_session;
2379 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2380 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2381 WARN_ON_ONCE(conf && unconf);
2384 status = nfserr_wrong_cred;
2385 if (!mach_creds_match(conf, rqstp))
2387 cs_slot = &conf->cl_cs_slot;
2388 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2389 if (status == nfserr_replay_cache) {
2390 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2392 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2393 status = nfserr_seq_misordered;
2396 } else if (unconf) {
2397 struct nfs4_client *old;
2398 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2399 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2400 status = nfserr_clid_inuse;
2403 status = nfserr_wrong_cred;
2404 if (!mach_creds_match(unconf, rqstp))
2406 cs_slot = &unconf->cl_cs_slot;
2407 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2409 /* an unconfirmed replay returns misordered */
2410 status = nfserr_seq_misordered;
2413 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2415 status = mark_client_expired(old);
2420 move_to_confirmed(unconf);
2423 status = nfserr_stale_clientid;
2428 * We do not support RDMA or persistent sessions
2430 cr_ses->flags &= ~SESSION4_PERSIST;
2431 cr_ses->flags &= ~SESSION4_RDMA;
2433 init_session(rqstp, new, conf, cr_ses);
2434 nfsd4_init_conn(rqstp, conn, new);
2436 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2437 NFS4_MAX_SESSIONID_LEN);
2438 cs_slot->sl_seqid++;
2439 cr_ses->seqid = cs_slot->sl_seqid;
2441 /* cache solo and embedded create sessions under the state lock */
2442 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2443 nfs4_unlock_state();
2446 nfs4_unlock_state();
2449 __free_session(new);
2450 out_release_drc_mem:
2451 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2455 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2458 case NFS4_CDFC4_FORE:
2459 case NFS4_CDFC4_BACK:
2461 case NFS4_CDFC4_FORE_OR_BOTH:
2462 case NFS4_CDFC4_BACK_OR_BOTH:
2463 *dir = NFS4_CDFC4_BOTH;
2466 return nfserr_inval;
2469 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2471 struct nfsd4_session *session = cstate->session;
2472 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2475 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2478 spin_lock(&nn->client_lock);
2479 session->se_cb_prog = bc->bc_cb_program;
2480 session->se_cb_sec = bc->bc_cb_sec;
2481 spin_unlock(&nn->client_lock);
2483 nfsd4_probe_callback(session->se_client);
2488 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2489 struct nfsd4_compound_state *cstate,
2490 struct nfsd4_bind_conn_to_session *bcts)
2493 struct nfsd4_conn *conn;
2494 struct nfsd4_session *session;
2495 struct net *net = SVC_NET(rqstp);
2496 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2498 if (!nfsd4_last_compound_op(rqstp))
2499 return nfserr_not_only_op;
2501 spin_lock(&nn->client_lock);
2502 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2503 spin_unlock(&nn->client_lock);
2505 goto out_no_session;
2506 status = nfserr_wrong_cred;
2507 if (!mach_creds_match(session->se_client, rqstp))
2509 status = nfsd4_map_bcts_dir(&bcts->dir);
2512 conn = alloc_conn(rqstp, bcts->dir);
2513 status = nfserr_jukebox;
2516 nfsd4_init_conn(rqstp, conn, session);
2519 nfsd4_put_session(session);
2521 nfs4_unlock_state();
2525 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2529 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2533 nfsd4_destroy_session(struct svc_rqst *r,
2534 struct nfsd4_compound_state *cstate,
2535 struct nfsd4_destroy_session *sessionid)
2537 struct nfsd4_session *ses;
2539 int ref_held_by_me = 0;
2540 struct net *net = SVC_NET(r);
2541 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2544 status = nfserr_not_only_op;
2545 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2546 if (!nfsd4_last_compound_op(r))
2550 dump_sessionid(__func__, &sessionid->sessionid);
2551 spin_lock(&nn->client_lock);
2552 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2554 goto out_client_lock;
2555 status = nfserr_wrong_cred;
2556 if (!mach_creds_match(ses->se_client, r))
2557 goto out_put_session;
2558 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2560 goto out_put_session;
2561 unhash_session(ses);
2562 spin_unlock(&nn->client_lock);
2564 nfsd4_probe_callback_sync(ses->se_client);
2566 spin_lock(&nn->client_lock);
2569 nfsd4_put_session_locked(ses);
2571 spin_unlock(&nn->client_lock);
2573 nfs4_unlock_state();
2577 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2579 struct nfsd4_conn *c;
2581 list_for_each_entry(c, &s->se_conns, cn_persession) {
2582 if (c->cn_xprt == xpt) {
2589 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2591 struct nfs4_client *clp = ses->se_client;
2592 struct nfsd4_conn *c;
2593 __be32 status = nfs_ok;
2596 spin_lock(&clp->cl_lock);
2597 c = __nfsd4_find_conn(new->cn_xprt, ses);
2600 status = nfserr_conn_not_bound_to_session;
2601 if (clp->cl_mach_cred)
2603 __nfsd4_hash_conn(new, ses);
2604 spin_unlock(&clp->cl_lock);
2605 ret = nfsd4_register_conn(new);
2607 /* oops; xprt is already down: */
2608 nfsd4_conn_lost(&new->cn_xpt_user);
2611 spin_unlock(&clp->cl_lock);
2616 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2618 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2620 return args->opcnt > session->se_fchannel.maxops;
2623 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2624 struct nfsd4_session *session)
2626 struct xdr_buf *xb = &rqstp->rq_arg;
2628 return xb->len > session->se_fchannel.maxreq_sz;
2632 nfsd4_sequence(struct svc_rqst *rqstp,
2633 struct nfsd4_compound_state *cstate,
2634 struct nfsd4_sequence *seq)
2636 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2637 struct xdr_stream *xdr = &resp->xdr;
2638 struct nfsd4_session *session;
2639 struct nfs4_client *clp;
2640 struct nfsd4_slot *slot;
2641 struct nfsd4_conn *conn;
2644 struct net *net = SVC_NET(rqstp);
2645 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2647 if (resp->opcnt != 1)
2648 return nfserr_sequence_pos;
2651 * Will be either used or freed by nfsd4_sequence_check_conn
2654 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2656 return nfserr_jukebox;
2658 spin_lock(&nn->client_lock);
2659 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2661 goto out_no_session;
2662 clp = session->se_client;
2664 status = nfserr_too_many_ops;
2665 if (nfsd4_session_too_many_ops(rqstp, session))
2666 goto out_put_session;
2668 status = nfserr_req_too_big;
2669 if (nfsd4_request_too_big(rqstp, session))
2670 goto out_put_session;
2672 status = nfserr_badslot;
2673 if (seq->slotid >= session->se_fchannel.maxreqs)
2674 goto out_put_session;
2676 slot = session->se_slots[seq->slotid];
2677 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2679 /* We do not negotiate the number of slots yet, so set the
2680 * maxslots to the session maxreqs which is used to encode
2681 * sr_highest_slotid and the sr_target_slot id to maxslots */
2682 seq->maxslots = session->se_fchannel.maxreqs;
2684 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2685 slot->sl_flags & NFSD4_SLOT_INUSE);
2686 if (status == nfserr_replay_cache) {
2687 status = nfserr_seq_misordered;
2688 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2689 goto out_put_session;
2690 cstate->slot = slot;
2691 cstate->session = session;
2693 /* Return the cached reply status and set cstate->status
2694 * for nfsd4_proc_compound processing */
2695 status = nfsd4_replay_cache_entry(resp, seq);
2696 cstate->status = nfserr_replay_cache;
2700 goto out_put_session;
2702 status = nfsd4_sequence_check_conn(conn, session);
2705 goto out_put_session;
2707 buflen = (seq->cachethis) ?
2708 session->se_fchannel.maxresp_cached :
2709 session->se_fchannel.maxresp_sz;
2710 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2712 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2713 goto out_put_session;
2714 svc_reserve(rqstp, buflen);
2717 /* Success! bump slot seqid */
2718 slot->sl_seqid = seq->seqid;
2719 slot->sl_flags |= NFSD4_SLOT_INUSE;
2721 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2723 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2725 cstate->slot = slot;
2726 cstate->session = session;
2730 switch (clp->cl_cb_state) {
2732 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2734 case NFSD4_CB_FAULT:
2735 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2738 seq->status_flags = 0;
2740 if (!list_empty(&clp->cl_revoked))
2741 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2745 spin_unlock(&nn->client_lock);
2748 nfsd4_put_session_locked(session);
2749 goto out_no_session;
2753 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2755 struct nfsd4_compound_state *cs = &resp->cstate;
2757 if (nfsd4_has_session(cs)) {
2758 if (cs->status != nfserr_replay_cache) {
2759 nfsd4_store_cache_entry(resp);
2760 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2762 /* Drop session reference that was taken in nfsd4_sequence() */
2763 nfsd4_put_session(cs->session);
2765 put_client_renew(cs->clp);
2769 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2771 struct nfs4_client *conf, *unconf, *clp;
2773 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2776 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2777 conf = find_confirmed_client(&dc->clientid, true, nn);
2778 WARN_ON_ONCE(conf && unconf);
2783 if (client_has_state(conf)) {
2784 status = nfserr_clientid_busy;
2790 status = nfserr_stale_clientid;
2793 if (!mach_creds_match(clp, rqstp)) {
2794 status = nfserr_wrong_cred;
2799 nfs4_unlock_state();
2804 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2808 if (rc->rca_one_fs) {
2809 if (!cstate->current_fh.fh_dentry)
2810 return nfserr_nofilehandle;
2812 * We don't take advantage of the rca_one_fs case.
2813 * That's OK, it's optional, we can safely ignore it.
2819 status = nfserr_complete_already;
2820 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2821 &cstate->session->se_client->cl_flags))
2824 status = nfserr_stale_clientid;
2825 if (is_client_expired(cstate->session->se_client))
2827 * The following error isn't really legal.
2828 * But we only get here if the client just explicitly
2829 * destroyed the client. Surely it no longer cares what
2830 * error it gets back on an operation for the dead
2836 nfsd4_client_record_create(cstate->session->se_client);
2838 nfs4_unlock_state();
2843 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2844 struct nfsd4_setclientid *setclid)
2846 struct xdr_netobj clname = setclid->se_name;
2847 nfs4_verifier clverifier = setclid->se_verf;
2848 struct nfs4_client *conf, *unconf, *new;
2850 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2852 /* Cases below refer to rfc 3530 section 14.2.33: */
2854 conf = find_confirmed_client_by_name(&clname, nn);
2857 status = nfserr_clid_inuse;
2858 if (clp_used_exchangeid(conf))
2860 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2861 char addr_str[INET6_ADDRSTRLEN];
2862 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2864 dprintk("NFSD: setclientid: string in use by client "
2865 "at %s\n", addr_str);
2869 unconf = find_unconfirmed_client_by_name(&clname, nn);
2871 expire_client(unconf);
2872 status = nfserr_jukebox;
2873 new = create_client(clname, rqstp, &clverifier);
2876 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2877 /* case 1: probable callback update */
2878 copy_clid(new, conf);
2879 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2881 new->cl_minorversion = 0;
2882 gen_callback(new, setclid, rqstp);
2883 add_to_unconfirmed(new);
2884 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2885 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2886 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2889 nfs4_unlock_state();
2895 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2896 struct nfsd4_compound_state *cstate,
2897 struct nfsd4_setclientid_confirm *setclientid_confirm)
2899 struct nfs4_client *conf, *unconf;
2900 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2901 clientid_t * clid = &setclientid_confirm->sc_clientid;
2903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2905 if (STALE_CLIENTID(clid, nn))
2906 return nfserr_stale_clientid;
2909 conf = find_confirmed_client(clid, false, nn);
2910 unconf = find_unconfirmed_client(clid, false, nn);
2912 * We try hard to give out unique clientid's, so if we get an
2913 * attempt to confirm the same clientid with a different cred,
2914 * there's a bug somewhere. Let's charitably assume it's our
2917 status = nfserr_serverfault;
2918 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2920 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2922 /* cases below refer to rfc 3530 section 14.2.34: */
2923 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2924 if (conf && !unconf) /* case 2: probable retransmit */
2926 else /* case 4: client hasn't noticed we rebooted yet? */
2927 status = nfserr_stale_clientid;
2931 if (conf) { /* case 1: callback update */
2932 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2933 nfsd4_probe_callback(conf);
2934 expire_client(unconf);
2935 } else { /* case 3: normal case; new or rebooted client */
2936 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2938 status = mark_client_expired(conf);
2941 expire_client(conf);
2943 move_to_confirmed(unconf);
2944 nfsd4_probe_callback(unconf);
2947 nfs4_unlock_state();
2951 static struct nfs4_file *nfsd4_alloc_file(void)
2953 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2956 /* OPEN Share state helper functions */
2957 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2959 unsigned int hashval = file_hashval(fh);
2961 lockdep_assert_held(&state_lock);
2963 atomic_set(&fp->fi_ref, 1);
2964 spin_lock_init(&fp->fi_lock);
2965 INIT_LIST_HEAD(&fp->fi_stateids);
2966 INIT_LIST_HEAD(&fp->fi_delegations);
2967 fh_copy_shallow(&fp->fi_fhandle, fh);
2968 fp->fi_had_conflict = false;
2969 fp->fi_lease = NULL;
2970 fp->fi_share_deny = 0;
2971 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2972 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2973 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2977 nfsd4_free_slabs(void)
2979 kmem_cache_destroy(openowner_slab);
2980 kmem_cache_destroy(lockowner_slab);
2981 kmem_cache_destroy(file_slab);
2982 kmem_cache_destroy(stateid_slab);
2983 kmem_cache_destroy(deleg_slab);
2987 nfsd4_init_slabs(void)
2989 openowner_slab = kmem_cache_create("nfsd4_openowners",
2990 sizeof(struct nfs4_openowner), 0, 0, NULL);
2991 if (openowner_slab == NULL)
2993 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2994 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2995 if (lockowner_slab == NULL)
2996 goto out_free_openowner_slab;
2997 file_slab = kmem_cache_create("nfsd4_files",
2998 sizeof(struct nfs4_file), 0, 0, NULL);
2999 if (file_slab == NULL)
3000 goto out_free_lockowner_slab;
3001 stateid_slab = kmem_cache_create("nfsd4_stateids",
3002 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3003 if (stateid_slab == NULL)
3004 goto out_free_file_slab;
3005 deleg_slab = kmem_cache_create("nfsd4_delegations",
3006 sizeof(struct nfs4_delegation), 0, 0, NULL);
3007 if (deleg_slab == NULL)
3008 goto out_free_stateid_slab;
3011 out_free_stateid_slab:
3012 kmem_cache_destroy(stateid_slab);
3014 kmem_cache_destroy(file_slab);
3015 out_free_lockowner_slab:
3016 kmem_cache_destroy(lockowner_slab);
3017 out_free_openowner_slab:
3018 kmem_cache_destroy(openowner_slab);
3020 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3024 static void init_nfs4_replay(struct nfs4_replay *rp)
3026 rp->rp_status = nfserr_serverfault;
3028 rp->rp_buf = rp->rp_ibuf;
3029 mutex_init(&rp->rp_mutex);
3032 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3033 struct nfs4_stateowner *so)
3035 if (!nfsd4_has_session(cstate)) {
3036 mutex_lock(&so->so_replay.rp_mutex);
3037 cstate->replay_owner = so;
3038 atomic_inc(&so->so_count);
3042 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3044 struct nfs4_stateowner *so = cstate->replay_owner;
3047 cstate->replay_owner = NULL;
3048 mutex_unlock(&so->so_replay.rp_mutex);
3049 nfs4_put_stateowner(so);
3053 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3055 struct nfs4_stateowner *sop;
3057 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3061 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3062 if (!sop->so_owner.data) {
3063 kmem_cache_free(slab, sop);
3066 sop->so_owner.len = owner->len;
3068 INIT_LIST_HEAD(&sop->so_stateids);
3069 sop->so_client = clp;
3070 init_nfs4_replay(&sop->so_replay);
3071 atomic_set(&sop->so_count, 1);
3075 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3077 lockdep_assert_held(&clp->cl_lock);
3079 list_add(&oo->oo_owner.so_strhash,
3080 &clp->cl_ownerstr_hashtbl[strhashval]);
3081 list_add(&oo->oo_perclient, &clp->cl_openowners);
3084 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3086 unhash_openowner_locked(openowner(so));
3089 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3091 struct nfs4_openowner *oo = openowner(so);
3093 kmem_cache_free(openowner_slab, oo);
3096 static const struct nfs4_stateowner_operations openowner_ops = {
3097 .so_unhash = nfs4_unhash_openowner,
3098 .so_free = nfs4_free_openowner,
3101 static struct nfs4_openowner *
3102 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3103 struct nfsd4_compound_state *cstate)
3105 struct nfs4_client *clp = cstate->clp;
3106 struct nfs4_openowner *oo, *ret;
3108 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3111 oo->oo_owner.so_ops = &openowner_ops;
3112 oo->oo_owner.so_is_open_owner = 1;
3113 oo->oo_owner.so_seqid = open->op_seqid;
3115 if (nfsd4_has_session(cstate))
3116 oo->oo_flags |= NFS4_OO_CONFIRMED;
3118 oo->oo_last_closed_stid = NULL;
3119 INIT_LIST_HEAD(&oo->oo_close_lru);
3120 spin_lock(&clp->cl_lock);
3121 ret = find_openstateowner_str_locked(strhashval, open, clp);
3123 hash_openowner(oo, clp, strhashval);
3126 nfs4_free_openowner(&oo->oo_owner);
3127 spin_unlock(&clp->cl_lock);
3131 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3132 struct nfs4_openowner *oo = open->op_openowner;
3134 atomic_inc(&stp->st_stid.sc_count);
3135 stp->st_stid.sc_type = NFS4_OPEN_STID;
3136 INIT_LIST_HEAD(&stp->st_locks);
3137 stp->st_stateowner = &oo->oo_owner;
3138 atomic_inc(&stp->st_stateowner->so_count);
3140 stp->st_stid.sc_file = fp;
3141 stp->st_access_bmap = 0;
3142 stp->st_deny_bmap = 0;
3143 stp->st_openstp = NULL;
3144 spin_lock(&oo->oo_owner.so_client->cl_lock);
3145 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3146 spin_lock(&fp->fi_lock);
3147 list_add(&stp->st_perfile, &fp->fi_stateids);
3148 spin_unlock(&fp->fi_lock);
3149 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3153 * In the 4.0 case we need to keep the owners around a little while to handle
3154 * CLOSE replay. We still do need to release any file access that is held by
3155 * them before returning however.
3158 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3160 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3161 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3164 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3167 * We know that we hold one reference via nfsd4_close, and another
3168 * "persistent" reference for the client. If the refcount is higher
3169 * than 2, then there are still calls in progress that are using this
3170 * stateid. We can't put the sc_file reference until they are finished.
3171 * Wait for the refcount to drop to 2. Since it has been unhashed,
3172 * there should be no danger of the refcount going back up again at
3175 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3177 release_all_access(s);
3178 if (s->st_stid.sc_file) {
3179 put_nfs4_file(s->st_stid.sc_file);
3180 s->st_stid.sc_file = NULL;
3182 release_last_closed_stateid(oo);
3183 oo->oo_last_closed_stid = s;
3184 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3185 oo->oo_time = get_seconds();
3188 /* search file_hashtbl[] for file */
3189 static struct nfs4_file *
3190 find_file_locked(struct knfsd_fh *fh)
3192 unsigned int hashval = file_hashval(fh);
3193 struct nfs4_file *fp;
3195 lockdep_assert_held(&state_lock);
3197 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3198 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3206 static struct nfs4_file *
3207 find_file(struct knfsd_fh *fh)
3209 struct nfs4_file *fp;
3211 spin_lock(&state_lock);
3212 fp = find_file_locked(fh);
3213 spin_unlock(&state_lock);
3217 static struct nfs4_file *
3218 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3220 struct nfs4_file *fp;
3222 spin_lock(&state_lock);
3223 fp = find_file_locked(fh);
3225 nfsd4_init_file(new, fh);
3228 spin_unlock(&state_lock);
3234 * Called to check deny when READ with all zero stateid or
3235 * WRITE with all zero or all one stateid
3238 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3240 struct nfs4_file *fp;
3241 __be32 ret = nfs_ok;
3243 fp = find_file(¤t_fh->fh_handle);
3246 /* Check for conflicting share reservations */
3247 spin_lock(&fp->fi_lock);
3248 if (fp->fi_share_deny & deny_type)
3249 ret = nfserr_locked;
3250 spin_unlock(&fp->fi_lock);
3255 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3257 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3260 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3263 * We can't do this in nfsd_break_deleg_cb because it is
3264 * already holding inode->i_lock.
3266 * If the dl_time != 0, then we know that it has already been
3267 * queued for a lease break. Don't queue it again.
3269 spin_lock(&state_lock);
3270 if (dp->dl_time == 0) {
3271 dp->dl_time = get_seconds();
3272 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3274 spin_unlock(&state_lock);
3277 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3280 * We're assuming the state code never drops its reference
3281 * without first removing the lease. Since we're in this lease
3282 * callback (and since the lease code is serialized by the kernel
3283 * lock) we know the server hasn't removed the lease yet, we know
3284 * it's safe to take a reference.
3286 atomic_inc(&dp->dl_stid.sc_count);
3287 nfsd4_cb_recall(dp);
3290 /* Called from break_lease() with i_lock held. */
3291 static void nfsd_break_deleg_cb(struct file_lock *fl)
3293 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3294 struct nfs4_delegation *dp;
3297 WARN(1, "(%p)->fl_owner NULL\n", fl);
3300 if (fp->fi_had_conflict) {
3301 WARN(1, "duplicate break on %p\n", fp);
3305 * We don't want the locks code to timeout the lease for us;
3306 * we'll remove it ourself if a delegation isn't returned
3309 fl->fl_break_time = 0;
3311 spin_lock(&fp->fi_lock);
3312 fp->fi_had_conflict = true;
3314 * If there are no delegations on the list, then we can't count on this
3315 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3316 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3317 * true should keep any new delegations from being hashed.
3319 if (list_empty(&fp->fi_delegations))
3320 fl->fl_break_time = jiffies;
3322 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3323 nfsd_break_one_deleg(dp);
3324 spin_unlock(&fp->fi_lock);
3328 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3331 return lease_modify(onlist, arg);
3336 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3337 .lm_break = nfsd_break_deleg_cb,
3338 .lm_change = nfsd_change_deleg_cb,
3341 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3343 if (nfsd4_has_session(cstate))
3345 if (seqid == so->so_seqid - 1)
3346 return nfserr_replay_me;
3347 if (seqid == so->so_seqid)
3349 return nfserr_bad_seqid;
3352 static __be32 lookup_clientid(clientid_t *clid,
3353 struct nfsd4_compound_state *cstate,
3354 struct nfsd_net *nn)
3356 struct nfs4_client *found;
3359 found = cstate->clp;
3360 if (!same_clid(&found->cl_clientid, clid))
3361 return nfserr_stale_clientid;
3365 if (STALE_CLIENTID(clid, nn))
3366 return nfserr_stale_clientid;
3369 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3370 * cached already then we know this is for is for v4.0 and "sessions"
3373 WARN_ON_ONCE(cstate->session);
3374 found = find_confirmed_client(clid, false, nn);
3376 return nfserr_expired;
3378 /* Cache the nfs4_client in cstate! */
3379 cstate->clp = found;
3380 atomic_inc(&found->cl_refcount);
3385 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3386 struct nfsd4_open *open, struct nfsd_net *nn)
3388 clientid_t *clientid = &open->op_clientid;
3389 struct nfs4_client *clp = NULL;
3390 unsigned int strhashval;
3391 struct nfs4_openowner *oo = NULL;
3394 if (STALE_CLIENTID(&open->op_clientid, nn))
3395 return nfserr_stale_clientid;
3397 * In case we need it later, after we've already created the
3398 * file and don't want to risk a further failure:
3400 open->op_file = nfsd4_alloc_file();
3401 if (open->op_file == NULL)
3402 return nfserr_jukebox;
3404 status = lookup_clientid(clientid, cstate, nn);
3409 strhashval = ownerstr_hashval(&open->op_owner);
3410 oo = find_openstateowner_str(strhashval, open, clp);
3411 open->op_openowner = oo;
3415 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3416 /* Replace unconfirmed owners without checking for replay. */
3417 release_openowner(oo);
3418 open->op_openowner = NULL;
3421 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3426 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3428 return nfserr_jukebox;
3429 open->op_openowner = oo;
3431 open->op_stp = nfs4_alloc_open_stateid(clp);
3433 return nfserr_jukebox;
3437 static inline __be32
3438 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3440 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3441 return nfserr_openmode;
3446 static int share_access_to_flags(u32 share_access)
3448 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3451 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3453 struct nfs4_stid *ret;
3455 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3458 return delegstateid(ret);
3461 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3463 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3464 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3468 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3469 struct nfs4_delegation **dp)
3472 __be32 status = nfserr_bad_stateid;
3473 struct nfs4_delegation *deleg;
3475 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3478 flags = share_access_to_flags(open->op_share_access);
3479 status = nfs4_check_delegmode(deleg, flags);
3481 nfs4_put_stid(&deleg->dl_stid);
3486 if (!nfsd4_is_deleg_cur(open))
3490 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3494 static struct nfs4_ol_stateid *
3495 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3497 struct nfs4_ol_stateid *local, *ret = NULL;
3498 struct nfs4_openowner *oo = open->op_openowner;
3500 spin_lock(&fp->fi_lock);
3501 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3502 /* ignore lock owners */
3503 if (local->st_stateowner->so_is_open_owner == 0)
3505 if (local->st_stateowner == &oo->oo_owner) {
3507 atomic_inc(&ret->st_stid.sc_count);
3511 spin_unlock(&fp->fi_lock);
3515 static inline int nfs4_access_to_access(u32 nfs4_access)
3519 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3520 flags |= NFSD_MAY_READ;
3521 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3522 flags |= NFSD_MAY_WRITE;
3526 static inline __be32
3527 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3528 struct nfsd4_open *open)
3530 struct iattr iattr = {
3531 .ia_valid = ATTR_SIZE,
3534 if (!open->op_truncate)
3536 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3537 return nfserr_inval;
3538 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3541 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3542 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3543 struct nfsd4_open *open)
3545 struct file *filp = NULL;
3547 int oflag = nfs4_access_to_omode(open->op_share_access);
3548 int access = nfs4_access_to_access(open->op_share_access);
3549 unsigned char old_access_bmap, old_deny_bmap;
3551 spin_lock(&fp->fi_lock);
3554 * Are we trying to set a deny mode that would conflict with
3557 status = nfs4_file_check_deny(fp, open->op_share_deny);
3558 if (status != nfs_ok) {
3559 spin_unlock(&fp->fi_lock);
3563 /* set access to the file */
3564 status = nfs4_file_get_access(fp, open->op_share_access);
3565 if (status != nfs_ok) {
3566 spin_unlock(&fp->fi_lock);
3570 /* Set access bits in stateid */
3571 old_access_bmap = stp->st_access_bmap;
3572 set_access(open->op_share_access, stp);
3574 /* Set new deny mask */
3575 old_deny_bmap = stp->st_deny_bmap;
3576 set_deny(open->op_share_deny, stp);
3577 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3579 if (!fp->fi_fds[oflag]) {
3580 spin_unlock(&fp->fi_lock);
3581 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3583 goto out_put_access;
3584 spin_lock(&fp->fi_lock);
3585 if (!fp->fi_fds[oflag]) {
3586 fp->fi_fds[oflag] = filp;
3590 spin_unlock(&fp->fi_lock);
3594 status = nfsd4_truncate(rqstp, cur_fh, open);
3596 goto out_put_access;
3600 stp->st_access_bmap = old_access_bmap;
3601 nfs4_file_put_access(fp, open->op_share_access);
3602 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3607 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)
3610 unsigned char old_deny_bmap;
3612 if (!test_access(open->op_share_access, stp))
3613 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3615 /* test and set deny mode */
3616 spin_lock(&fp->fi_lock);
3617 status = nfs4_file_check_deny(fp, open->op_share_deny);
3618 if (status == nfs_ok) {
3619 old_deny_bmap = stp->st_deny_bmap;
3620 set_deny(open->op_share_deny, stp);
3621 fp->fi_share_deny |=
3622 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3624 spin_unlock(&fp->fi_lock);
3626 if (status != nfs_ok)
3629 status = nfsd4_truncate(rqstp, cur_fh, open);
3630 if (status != nfs_ok)
3631 reset_union_bmap_deny(old_deny_bmap, stp);
3636 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3638 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3641 /* Should we give out recallable state?: */
3642 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3644 if (clp->cl_cb_state == NFSD4_CB_UP)
3647 * In the sessions case, since we don't have to establish a
3648 * separate connection for callbacks, we assume it's OK
3649 * until we hear otherwise:
3651 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3654 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3656 struct file_lock *fl;
3658 fl = locks_alloc_lock();
3661 locks_init_lock(fl);
3662 fl->fl_lmops = &nfsd_lease_mng_ops;
3663 fl->fl_flags = FL_DELEG;
3664 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3665 fl->fl_end = OFFSET_MAX;
3666 fl->fl_owner = (fl_owner_t)fp;
3667 fl->fl_pid = current->tgid;
3671 static int nfs4_setlease(struct nfs4_delegation *dp)
3673 struct nfs4_file *fp = dp->dl_stid.sc_file;
3674 struct file_lock *fl;
3678 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3681 filp = find_readable_file(fp);
3683 /* We should always have a readable file here */
3688 status = vfs_setlease(filp, fl->fl_type, &fl);
3690 locks_free_lock(fl);
3693 spin_lock(&state_lock);
3694 spin_lock(&fp->fi_lock);
3695 /* Did the lease get broken before we took the lock? */
3697 if (fp->fi_had_conflict)
3702 atomic_inc(&fp->fi_delegees);
3703 hash_delegation_locked(dp, fp);
3707 fp->fi_deleg_file = filp;
3708 atomic_set(&fp->fi_delegees, 1);
3709 hash_delegation_locked(dp, fp);
3710 spin_unlock(&fp->fi_lock);
3711 spin_unlock(&state_lock);
3714 spin_unlock(&fp->fi_lock);
3715 spin_unlock(&state_lock);
3721 static struct nfs4_delegation *
3722 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3723 struct nfs4_file *fp)
3726 struct nfs4_delegation *dp;
3728 if (fp->fi_had_conflict)
3729 return ERR_PTR(-EAGAIN);
3731 dp = alloc_init_deleg(clp, fh);
3733 return ERR_PTR(-ENOMEM);
3736 spin_lock(&state_lock);
3737 spin_lock(&fp->fi_lock);
3738 dp->dl_stid.sc_file = fp;
3739 if (!fp->fi_lease) {
3740 spin_unlock(&fp->fi_lock);
3741 spin_unlock(&state_lock);
3742 status = nfs4_setlease(dp);
3745 atomic_inc(&fp->fi_delegees);
3746 if (fp->fi_had_conflict) {
3750 hash_delegation_locked(dp, fp);
3753 spin_unlock(&fp->fi_lock);
3754 spin_unlock(&state_lock);
3757 nfs4_put_stid(&dp->dl_stid);
3758 return ERR_PTR(status);
3763 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3765 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3766 if (status == -EAGAIN)
3767 open->op_why_no_deleg = WND4_CONTENTION;
3769 open->op_why_no_deleg = WND4_RESOURCE;
3770 switch (open->op_deleg_want) {
3771 case NFS4_SHARE_WANT_READ_DELEG:
3772 case NFS4_SHARE_WANT_WRITE_DELEG:
3773 case NFS4_SHARE_WANT_ANY_DELEG:
3775 case NFS4_SHARE_WANT_CANCEL:
3776 open->op_why_no_deleg = WND4_CANCELLED;
3778 case NFS4_SHARE_WANT_NO_DELEG:
3785 * Attempt to hand out a delegation.
3787 * Note we don't support write delegations, and won't until the vfs has
3788 * proper support for them.
3791 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3792 struct nfs4_ol_stateid *stp)
3794 struct nfs4_delegation *dp;
3795 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3796 struct nfs4_client *clp = stp->st_stid.sc_client;
3800 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3801 open->op_recall = 0;
3802 switch (open->op_claim_type) {
3803 case NFS4_OPEN_CLAIM_PREVIOUS:
3805 open->op_recall = 1;
3806 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3809 case NFS4_OPEN_CLAIM_NULL:
3810 case NFS4_OPEN_CLAIM_FH:
3812 * Let's not give out any delegations till everyone's
3813 * had the chance to reclaim theirs....
3815 if (locks_in_grace(clp->net))
3817 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3820 * Also, if the file was opened for write or
3821 * create, there's a good chance the client's
3822 * about to write to it, resulting in an
3823 * immediate recall (since we don't support
3824 * write delegations):
3826 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3828 if (open->op_create == NFS4_OPEN_CREATE)
3834 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3838 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3840 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3841 STATEID_VAL(&dp->dl_stid.sc_stateid));
3842 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3843 nfs4_put_stid(&dp->dl_stid);
3846 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3847 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3848 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3849 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3850 open->op_recall = 1;
3853 /* 4.1 client asking for a delegation? */
3854 if (open->op_deleg_want)
3855 nfsd4_open_deleg_none_ext(open, status);
3859 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3860 struct nfs4_delegation *dp)
3862 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3863 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3864 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3865 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3866 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3867 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3868 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3869 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3871 /* Otherwise the client must be confused wanting a delegation
3872 * it already has, therefore we don't return
3873 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3878 * called with nfs4_lock_state() held.
3881 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3883 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3884 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3885 struct nfs4_file *fp = NULL;
3886 struct nfs4_ol_stateid *stp = NULL;
3887 struct nfs4_delegation *dp = NULL;
3891 * Lookup file; if found, lookup stateid and check open request,
3892 * and check for delegations in the process of being recalled.
3893 * If not found, create the nfs4_file struct
3895 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3896 if (fp != open->op_file) {
3897 status = nfs4_check_deleg(cl, open, &dp);
3900 stp = nfsd4_find_existing_open(fp, open);
3902 open->op_file = NULL;
3903 status = nfserr_bad_stateid;
3904 if (nfsd4_is_deleg_cur(open))
3906 status = nfserr_jukebox;
3910 * OPEN the file, or upgrade an existing OPEN.
3911 * If truncate fails, the OPEN fails.
3914 /* Stateid was found, this is an OPEN upgrade */
3915 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3920 open->op_stp = NULL;
3921 init_open_stateid(stp, fp, open);
3922 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3924 release_open_stateid(stp);
3928 update_stateid(&stp->st_stid.sc_stateid);
3929 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3931 if (nfsd4_has_session(&resp->cstate)) {
3932 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3933 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3934 open->op_why_no_deleg = WND4_NOT_WANTED;
3940 * Attempt to hand out a delegation. No error return, because the
3941 * OPEN succeeds even if we fail.
3943 nfs4_open_delegation(current_fh, open, stp);
3947 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3948 STATEID_VAL(&stp->st_stid.sc_stateid));
3950 /* 4.1 client trying to upgrade/downgrade delegation? */
3951 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3952 open->op_deleg_want)
3953 nfsd4_deleg_xgrade_none_ext(open, dp);
3957 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3958 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3960 * To finish the open response, we just need to set the rflags.
3962 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3963 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3964 !nfsd4_has_session(&resp->cstate))
3965 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3967 nfs4_put_stid(&dp->dl_stid);
3969 nfs4_put_stid(&stp->st_stid);
3974 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3975 struct nfsd4_open *open, __be32 status)
3977 if (open->op_openowner) {
3978 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
3980 nfsd4_cstate_assign_replay(cstate, so);
3981 nfs4_put_stateowner(so);
3984 nfsd4_free_file(open->op_file);
3986 nfs4_put_stid(&open->op_stp->st_stid);
3990 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3993 struct nfs4_client *clp;
3995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3998 dprintk("process_renew(%08x/%08x): starting\n",
3999 clid->cl_boot, clid->cl_id);
4000 status = lookup_clientid(clid, cstate, nn);
4004 status = nfserr_cb_path_down;
4005 if (!list_empty(&clp->cl_delegations)
4006 && clp->cl_cb_state != NFSD4_CB_UP)
4010 nfs4_unlock_state();
4015 nfsd4_end_grace(struct nfsd_net *nn)
4017 /* do nothing if grace period already ended */
4018 if (nn->grace_ended)
4021 dprintk("NFSD: end of grace period\n");
4022 nn->grace_ended = true;
4023 nfsd4_record_grace_done(nn, nn->boot_time);
4024 locks_end_grace(&nn->nfsd4_manager);
4026 * Now that every NFSv4 client has had the chance to recover and
4027 * to see the (possibly new, possibly shorter) lease time, we
4028 * can safely set the next grace time to the current lease time:
4030 nn->nfsd4_grace = nn->nfsd4_lease;
4034 nfs4_laundromat(struct nfsd_net *nn)
4036 struct nfs4_client *clp;
4037 struct nfs4_openowner *oo;
4038 struct nfs4_delegation *dp;
4039 struct list_head *pos, *next, reaplist;
4040 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4041 time_t t, new_timeo = nn->nfsd4_lease;
4045 dprintk("NFSD: laundromat service - starting\n");
4046 nfsd4_end_grace(nn);
4047 INIT_LIST_HEAD(&reaplist);
4048 spin_lock(&nn->client_lock);
4049 list_for_each_safe(pos, next, &nn->client_lru) {
4050 clp = list_entry(pos, struct nfs4_client, cl_lru);
4051 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4052 t = clp->cl_time - cutoff;
4053 new_timeo = min(new_timeo, t);
4056 if (mark_client_expired_locked(clp)) {
4057 dprintk("NFSD: client in use (clientid %08x)\n",
4058 clp->cl_clientid.cl_id);
4061 list_move(&clp->cl_lru, &reaplist);
4063 spin_unlock(&nn->client_lock);
4064 list_for_each_safe(pos, next, &reaplist) {
4065 clp = list_entry(pos, struct nfs4_client, cl_lru);
4066 dprintk("NFSD: purging unused client (clientid %08x)\n",
4067 clp->cl_clientid.cl_id);
4070 spin_lock(&state_lock);
4071 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4072 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4073 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4075 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4076 t = dp->dl_time - cutoff;
4077 new_timeo = min(new_timeo, t);
4080 unhash_delegation_locked(dp);
4081 list_add(&dp->dl_recall_lru, &reaplist);
4083 spin_unlock(&state_lock);
4084 while (!list_empty(&reaplist)) {
4085 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4087 list_del_init(&dp->dl_recall_lru);
4088 revoke_delegation(dp);
4090 list_for_each_safe(pos, next, &nn->close_lru) {
4091 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4092 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4093 t = oo->oo_time - cutoff;
4094 new_timeo = min(new_timeo, t);
4097 release_last_closed_stateid(oo);
4099 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4100 nfs4_unlock_state();
4104 static struct workqueue_struct *laundry_wq;
4105 static void laundromat_main(struct work_struct *);
4108 laundromat_main(struct work_struct *laundry)
4111 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4113 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4116 t = nfs4_laundromat(nn);
4117 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4118 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4121 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4123 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4124 return nfserr_bad_stateid;
4129 access_permit_read(struct nfs4_ol_stateid *stp)
4131 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4132 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4133 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4137 access_permit_write(struct nfs4_ol_stateid *stp)
4139 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4140 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4144 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4146 __be32 status = nfserr_openmode;
4148 /* For lock stateid's, we test the parent open, not the lock: */
4149 if (stp->st_openstp)
4150 stp = stp->st_openstp;
4151 if ((flags & WR_STATE) && !access_permit_write(stp))
4153 if ((flags & RD_STATE) && !access_permit_read(stp))
4160 static inline __be32
4161 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4163 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4165 else if (locks_in_grace(net)) {
4166 /* Answer in remaining cases depends on existence of
4167 * conflicting state; so we must wait out the grace period. */
4168 return nfserr_grace;
4169 } else if (flags & WR_STATE)
4170 return nfs4_share_conflict(current_fh,
4171 NFS4_SHARE_DENY_WRITE);
4172 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4173 return nfs4_share_conflict(current_fh,
4174 NFS4_SHARE_DENY_READ);
4178 * Allow READ/WRITE during grace period on recovered state only for files
4179 * that are not able to provide mandatory locking.
4182 grace_disallows_io(struct net *net, struct inode *inode)
4184 return locks_in_grace(net) && mandatory_lock(inode);
4187 /* Returns true iff a is later than b: */
4188 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4190 return (s32)(a->si_generation - b->si_generation) > 0;
4193 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4196 * When sessions are used the stateid generation number is ignored
4199 if (has_session && in->si_generation == 0)
4202 if (in->si_generation == ref->si_generation)
4205 /* If the client sends us a stateid from the future, it's buggy: */
4206 if (stateid_generation_after(in, ref))
4207 return nfserr_bad_stateid;
4209 * However, we could see a stateid from the past, even from a
4210 * non-buggy client. For example, if the client sends a lock
4211 * while some IO is outstanding, the lock may bump si_generation
4212 * while the IO is still in flight. The client could avoid that
4213 * situation by waiting for responses on all the IO requests,
4214 * but better performance may result in retrying IO that
4215 * receives an old_stateid error if requests are rarely
4216 * reordered in flight:
4218 return nfserr_old_stateid;
4221 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4223 struct nfs4_stid *s;
4224 struct nfs4_ol_stateid *ols;
4225 __be32 status = nfserr_bad_stateid;
4227 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4229 /* Client debugging aid. */
4230 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4231 char addr_str[INET6_ADDRSTRLEN];
4232 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4234 pr_warn_ratelimited("NFSD: client %s testing state ID "
4235 "with incorrect client ID\n", addr_str);
4238 spin_lock(&cl->cl_lock);
4239 s = find_stateid_locked(cl, stateid);
4242 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4245 switch (s->sc_type) {
4246 case NFS4_DELEG_STID:
4249 case NFS4_REVOKED_DELEG_STID:
4250 status = nfserr_deleg_revoked;
4252 case NFS4_OPEN_STID:
4253 case NFS4_LOCK_STID:
4254 ols = openlockstateid(s);
4255 if (ols->st_stateowner->so_is_open_owner
4256 && !(openowner(ols->st_stateowner)->oo_flags
4257 & NFS4_OO_CONFIRMED))
4258 status = nfserr_bad_stateid;
4263 printk("unknown stateid type %x\n", s->sc_type);
4265 case NFS4_CLOSED_STID:
4266 case NFS4_CLOSED_DELEG_STID:
4267 status = nfserr_bad_stateid;
4270 spin_unlock(&cl->cl_lock);
4275 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4276 stateid_t *stateid, unsigned char typemask,
4277 struct nfs4_stid **s, struct nfsd_net *nn)
4281 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4282 return nfserr_bad_stateid;
4283 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4284 if (status == nfserr_stale_clientid) {
4285 if (cstate->session)
4286 return nfserr_bad_stateid;
4287 return nfserr_stale_stateid;
4291 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4293 return nfserr_bad_stateid;
4298 * Checks for stateid operations
4301 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4302 stateid_t *stateid, int flags, struct file **filpp)
4304 struct nfs4_stid *s;
4305 struct nfs4_ol_stateid *stp = NULL;
4306 struct nfs4_delegation *dp = NULL;
4307 struct svc_fh *current_fh = &cstate->current_fh;
4308 struct inode *ino = current_fh->fh_dentry->d_inode;
4309 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4310 struct file *file = NULL;
4316 if (grace_disallows_io(net, ino))
4317 return nfserr_grace;
4319 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4320 return check_special_stateids(net, current_fh, stateid, flags);
4324 status = nfsd4_lookup_stateid(cstate, stateid,
4325 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4329 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4332 switch (s->sc_type) {
4333 case NFS4_DELEG_STID:
4334 dp = delegstateid(s);
4335 status = nfs4_check_delegmode(dp, flags);
4339 file = dp->dl_stid.sc_file->fi_deleg_file;
4342 status = nfserr_serverfault;
4348 case NFS4_OPEN_STID:
4349 case NFS4_LOCK_STID:
4350 stp = openlockstateid(s);
4351 status = nfs4_check_fh(current_fh, stp);
4354 if (stp->st_stateowner->so_is_open_owner
4355 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4357 status = nfs4_check_openmode(stp, flags);
4361 struct nfs4_file *fp = stp->st_stid.sc_file;
4363 if (flags & RD_STATE)
4364 file = find_readable_file(fp);
4366 file = find_writeable_file(fp);
4370 status = nfserr_bad_stateid;
4379 nfs4_unlock_state();
4384 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4386 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4388 if (check_for_locks(stp->st_stid.sc_file, lo))
4389 return nfserr_locks_held;
4390 release_lock_stateid(stp);
4395 * Test if the stateid is valid
4398 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4399 struct nfsd4_test_stateid *test_stateid)
4401 struct nfsd4_test_stateid_id *stateid;
4402 struct nfs4_client *cl = cstate->session->se_client;
4405 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4406 stateid->ts_id_status =
4407 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4408 nfs4_unlock_state();
4414 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4415 struct nfsd4_free_stateid *free_stateid)
4417 stateid_t *stateid = &free_stateid->fr_stateid;
4418 struct nfs4_stid *s;
4419 struct nfs4_delegation *dp;
4420 struct nfs4_client *cl = cstate->session->se_client;
4421 __be32 ret = nfserr_bad_stateid;
4424 spin_lock(&cl->cl_lock);
4425 s = find_stateid_locked(cl, stateid);
4428 switch (s->sc_type) {
4429 case NFS4_DELEG_STID:
4430 ret = nfserr_locks_held;
4432 case NFS4_OPEN_STID:
4433 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4436 ret = nfserr_locks_held;
4438 case NFS4_LOCK_STID:
4439 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4442 spin_unlock(&cl->cl_lock);
4443 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4445 case NFS4_REVOKED_DELEG_STID:
4446 dp = delegstateid(s);
4447 list_del_init(&dp->dl_recall_lru);
4448 spin_unlock(&cl->cl_lock);
4452 /* Default falls through and returns nfserr_bad_stateid */
4455 spin_unlock(&cl->cl_lock);
4457 nfs4_unlock_state();
4464 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4465 RD_STATE : WR_STATE;
4468 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4470 struct svc_fh *current_fh = &cstate->current_fh;
4471 struct nfs4_stateowner *sop = stp->st_stateowner;
4474 status = nfsd4_check_seqid(cstate, sop, seqid);
4477 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4478 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4480 * "Closed" stateid's exist *only* to return
4481 * nfserr_replay_me from the previous step, and
4482 * revoked delegations are kept only for free_stateid.
4484 return nfserr_bad_stateid;
4485 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4488 return nfs4_check_fh(current_fh, stp);
4492 * Checks for sequence id mutating operations.
4495 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4496 stateid_t *stateid, char typemask,
4497 struct nfs4_ol_stateid **stpp,
4498 struct nfsd_net *nn)
4501 struct nfs4_stid *s;
4502 struct nfs4_ol_stateid *stp = NULL;
4504 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4505 seqid, STATEID_VAL(stateid));
4508 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4511 stp = openlockstateid(s);
4512 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4514 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4518 nfs4_put_stid(&stp->st_stid);
4522 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4523 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4526 struct nfs4_openowner *oo;
4527 struct nfs4_ol_stateid *stp;
4529 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4530 NFS4_OPEN_STID, &stp, nn);
4533 oo = openowner(stp->st_stateowner);
4534 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4535 nfs4_put_stid(&stp->st_stid);
4536 return nfserr_bad_stateid;
4543 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4544 struct nfsd4_open_confirm *oc)
4547 struct nfs4_openowner *oo;
4548 struct nfs4_ol_stateid *stp;
4549 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4551 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4552 cstate->current_fh.fh_dentry);
4554 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4560 status = nfs4_preprocess_seqid_op(cstate,
4561 oc->oc_seqid, &oc->oc_req_stateid,
4562 NFS4_OPEN_STID, &stp, nn);
4565 oo = openowner(stp->st_stateowner);
4566 status = nfserr_bad_stateid;
4567 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4569 oo->oo_flags |= NFS4_OO_CONFIRMED;
4570 update_stateid(&stp->st_stid.sc_stateid);
4571 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4572 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4573 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4575 nfsd4_client_record_create(oo->oo_owner.so_client);
4578 nfs4_put_stid(&stp->st_stid);
4580 nfsd4_bump_seqid(cstate, status);
4581 nfs4_unlock_state();
4585 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4587 if (!test_access(access, stp))
4589 nfs4_file_put_access(stp->st_stid.sc_file, access);
4590 clear_access(access, stp);
4593 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4595 switch (to_access) {
4596 case NFS4_SHARE_ACCESS_READ:
4597 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4598 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4600 case NFS4_SHARE_ACCESS_WRITE:
4601 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4602 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4604 case NFS4_SHARE_ACCESS_BOTH:
4612 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4613 struct nfsd4_compound_state *cstate,
4614 struct nfsd4_open_downgrade *od)
4617 struct nfs4_ol_stateid *stp;
4618 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4620 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4621 cstate->current_fh.fh_dentry);
4623 /* We don't yet support WANT bits: */
4624 if (od->od_deleg_want)
4625 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4629 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4630 &od->od_stateid, &stp, nn);
4633 status = nfserr_inval;
4634 if (!test_access(od->od_share_access, stp)) {
4635 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4636 stp->st_access_bmap, od->od_share_access);
4639 if (!test_deny(od->od_share_deny, stp)) {
4640 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4641 stp->st_deny_bmap, od->od_share_deny);
4644 nfs4_stateid_downgrade(stp, od->od_share_access);
4646 reset_union_bmap_deny(od->od_share_deny, stp);
4648 update_stateid(&stp->st_stid.sc_stateid);
4649 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4652 nfs4_put_stid(&stp->st_stid);
4654 nfsd4_bump_seqid(cstate, status);
4655 nfs4_unlock_state();
4659 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4661 struct nfs4_client *clp = s->st_stid.sc_client;
4663 s->st_stid.sc_type = NFS4_CLOSED_STID;
4664 unhash_open_stateid(s);
4666 if (clp->cl_minorversion)
4667 nfs4_put_stid(&s->st_stid);
4669 move_to_close_lru(s, clp->net);
4673 * nfs4_unlock_state() called after encode
4676 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4677 struct nfsd4_close *close)
4680 struct nfs4_ol_stateid *stp;
4681 struct net *net = SVC_NET(rqstp);
4682 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4684 dprintk("NFSD: nfsd4_close on file %pd\n",
4685 cstate->current_fh.fh_dentry);
4688 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4690 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4692 nfsd4_bump_seqid(cstate, status);
4695 update_stateid(&stp->st_stid.sc_stateid);
4696 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4698 nfsd4_close_open_stateid(stp);
4700 /* put reference from nfs4_preprocess_seqid_op */
4701 nfs4_put_stid(&stp->st_stid);
4703 nfs4_unlock_state();
4708 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4709 struct nfsd4_delegreturn *dr)
4711 struct nfs4_delegation *dp;
4712 stateid_t *stateid = &dr->dr_stateid;
4713 struct nfs4_stid *s;
4715 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4717 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4721 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4724 dp = delegstateid(s);
4725 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4729 destroy_delegation(dp);
4731 nfs4_put_stid(&dp->dl_stid);
4733 nfs4_unlock_state();
4739 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4742 end_offset(u64 start, u64 len)
4747 return end >= start ? end: NFS4_MAX_UINT64;
4750 /* last octet in a range */
4752 last_byte_offset(u64 start, u64 len)
4758 return end > start ? end - 1: NFS4_MAX_UINT64;
4762 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4763 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4764 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4765 * locking, this prevents us from being completely protocol-compliant. The
4766 * real solution to this problem is to start using unsigned file offsets in
4767 * the VFS, but this is a very deep change!
4770 nfs4_transform_lock_offset(struct file_lock *lock)
4772 if (lock->fl_start < 0)
4773 lock->fl_start = OFFSET_MAX;
4774 if (lock->fl_end < 0)
4775 lock->fl_end = OFFSET_MAX;
4778 /* Hack!: For now, we're defining this just so we can use a pointer to it
4779 * as a unique cookie to identify our (NFSv4's) posix locks. */
4780 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4784 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4786 struct nfs4_lockowner *lo;
4788 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4789 lo = (struct nfs4_lockowner *) fl->fl_owner;
4790 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4791 lo->lo_owner.so_owner.len, GFP_KERNEL);
4792 if (!deny->ld_owner.data)
4793 /* We just don't care that much */
4795 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4796 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4799 deny->ld_owner.len = 0;
4800 deny->ld_owner.data = NULL;
4801 deny->ld_clientid.cl_boot = 0;
4802 deny->ld_clientid.cl_id = 0;
4804 deny->ld_start = fl->fl_start;
4805 deny->ld_length = NFS4_MAX_UINT64;
4806 if (fl->fl_end != NFS4_MAX_UINT64)
4807 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4808 deny->ld_type = NFS4_READ_LT;
4809 if (fl->fl_type != F_RDLCK)
4810 deny->ld_type = NFS4_WRITE_LT;
4813 static struct nfs4_lockowner *
4814 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4815 struct nfs4_client *clp)
4817 unsigned int strhashval = ownerstr_hashval(owner);
4818 struct nfs4_stateowner *so;
4820 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4822 if (so->so_is_open_owner)
4824 if (!same_owner_str(so, owner))
4826 atomic_inc(&so->so_count);
4827 return lockowner(so);
4832 static struct nfs4_lockowner *
4833 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4834 struct nfs4_client *clp)
4836 struct nfs4_lockowner *lo;
4838 spin_lock(&clp->cl_lock);
4839 lo = find_lockowner_str_locked(clid, owner, clp);
4840 spin_unlock(&clp->cl_lock);
4844 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4846 unhash_lockowner_locked(lockowner(sop));
4849 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4851 struct nfs4_lockowner *lo = lockowner(sop);
4853 kmem_cache_free(lockowner_slab, lo);
4856 static const struct nfs4_stateowner_operations lockowner_ops = {
4857 .so_unhash = nfs4_unhash_lockowner,
4858 .so_free = nfs4_free_lockowner,
4862 * Alloc a lock owner structure.
4863 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4866 * strhashval = ownerstr_hashval
4868 static struct nfs4_lockowner *
4869 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4870 struct nfs4_ol_stateid *open_stp,
4871 struct nfsd4_lock *lock)
4873 struct nfs4_lockowner *lo, *ret;
4875 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4878 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4879 lo->lo_owner.so_is_open_owner = 0;
4880 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4881 lo->lo_owner.so_ops = &lockowner_ops;
4882 spin_lock(&clp->cl_lock);
4883 ret = find_lockowner_str_locked(&clp->cl_clientid,
4884 &lock->lk_new_owner, clp);
4886 list_add(&lo->lo_owner.so_strhash,
4887 &clp->cl_ownerstr_hashtbl[strhashval]);
4890 nfs4_free_lockowner(&lo->lo_owner);
4891 spin_unlock(&clp->cl_lock);
4896 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4897 struct nfs4_file *fp, struct inode *inode,
4898 struct nfs4_ol_stateid *open_stp)
4900 struct nfs4_client *clp = lo->lo_owner.so_client;
4902 lockdep_assert_held(&clp->cl_lock);
4904 atomic_inc(&stp->st_stid.sc_count);
4905 stp->st_stid.sc_type = NFS4_LOCK_STID;
4906 stp->st_stateowner = &lo->lo_owner;
4907 atomic_inc(&lo->lo_owner.so_count);
4909 stp->st_stid.sc_file = fp;
4910 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4911 stp->st_access_bmap = 0;
4912 stp->st_deny_bmap = open_stp->st_deny_bmap;
4913 stp->st_openstp = open_stp;
4914 list_add(&stp->st_locks, &open_stp->st_locks);
4915 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4916 spin_lock(&fp->fi_lock);
4917 list_add(&stp->st_perfile, &fp->fi_stateids);
4918 spin_unlock(&fp->fi_lock);
4921 static struct nfs4_ol_stateid *
4922 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4924 struct nfs4_ol_stateid *lst;
4925 struct nfs4_client *clp = lo->lo_owner.so_client;
4927 lockdep_assert_held(&clp->cl_lock);
4929 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4930 if (lst->st_stid.sc_file == fp) {
4931 atomic_inc(&lst->st_stid.sc_count);
4938 static struct nfs4_ol_stateid *
4939 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4940 struct inode *inode, struct nfs4_ol_stateid *ost,
4943 struct nfs4_stid *ns = NULL;
4944 struct nfs4_ol_stateid *lst;
4945 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4946 struct nfs4_client *clp = oo->oo_owner.so_client;
4948 spin_lock(&clp->cl_lock);
4949 lst = find_lock_stateid(lo, fi);
4951 spin_unlock(&clp->cl_lock);
4952 ns = nfs4_alloc_stid(clp, stateid_slab);
4956 spin_lock(&clp->cl_lock);
4957 lst = find_lock_stateid(lo, fi);
4959 lst = openlockstateid(ns);
4960 init_lock_stateid(lst, lo, fi, inode, ost);
4965 spin_unlock(&clp->cl_lock);
4972 check_lock_length(u64 offset, u64 length)
4974 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4975 LOFF_OVERFLOW(offset, length)));
4978 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4980 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4982 lockdep_assert_held(&fp->fi_lock);
4984 if (test_access(access, lock_stp))
4986 __nfs4_file_get_access(fp, access);
4987 set_access(access, lock_stp);
4991 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
4992 struct nfs4_ol_stateid *ost,
4993 struct nfsd4_lock *lock,
4994 struct nfs4_ol_stateid **lst, bool *new)
4997 struct nfs4_file *fi = ost->st_stid.sc_file;
4998 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4999 struct nfs4_client *cl = oo->oo_owner.so_client;
5000 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5001 struct nfs4_lockowner *lo;
5002 unsigned int strhashval;
5004 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5006 strhashval = ownerstr_hashval(&lock->v.new.owner);
5007 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5009 return nfserr_jukebox;
5011 /* with an existing lockowner, seqids must be the same */
5012 status = nfserr_bad_seqid;
5013 if (!cstate->minorversion &&
5014 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5018 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5020 status = nfserr_jukebox;
5025 nfs4_put_stateowner(&lo->lo_owner);
5033 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5034 struct nfsd4_lock *lock)
5036 struct nfs4_openowner *open_sop = NULL;
5037 struct nfs4_lockowner *lock_sop = NULL;
5038 struct nfs4_ol_stateid *lock_stp = NULL;
5039 struct nfs4_ol_stateid *open_stp = NULL;
5040 struct nfs4_file *fp;
5041 struct file *filp = NULL;
5042 struct file_lock *file_lock = NULL;
5043 struct file_lock *conflock = NULL;
5048 struct net *net = SVC_NET(rqstp);
5049 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5051 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5052 (long long) lock->lk_offset,
5053 (long long) lock->lk_length);
5055 if (check_lock_length(lock->lk_offset, lock->lk_length))
5056 return nfserr_inval;
5058 if ((status = fh_verify(rqstp, &cstate->current_fh,
5059 S_IFREG, NFSD_MAY_LOCK))) {
5060 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5066 if (lock->lk_is_new) {
5067 if (nfsd4_has_session(cstate))
5068 /* See rfc 5661 18.10.3: given clientid is ignored: */
5069 memcpy(&lock->v.new.clientid,
5070 &cstate->session->se_client->cl_clientid,
5071 sizeof(clientid_t));
5073 status = nfserr_stale_clientid;
5074 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5077 /* validate and update open stateid and open seqid */
5078 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5079 lock->lk_new_open_seqid,
5080 &lock->lk_new_open_stateid,
5084 open_sop = openowner(open_stp->st_stateowner);
5085 status = nfserr_bad_stateid;
5086 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5087 &lock->v.new.clientid))
5089 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5092 status = nfs4_preprocess_seqid_op(cstate,
5093 lock->lk_old_lock_seqid,
5094 &lock->lk_old_lock_stateid,
5095 NFS4_LOCK_STID, &lock_stp, nn);
5099 lock_sop = lockowner(lock_stp->st_stateowner);
5101 lkflg = setlkflg(lock->lk_type);
5102 status = nfs4_check_openmode(lock_stp, lkflg);
5106 status = nfserr_grace;
5107 if (locks_in_grace(net) && !lock->lk_reclaim)
5109 status = nfserr_no_grace;
5110 if (!locks_in_grace(net) && lock->lk_reclaim)
5113 file_lock = locks_alloc_lock();
5115 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5116 status = nfserr_jukebox;
5120 fp = lock_stp->st_stid.sc_file;
5121 locks_init_lock(file_lock);
5122 switch (lock->lk_type) {
5125 spin_lock(&fp->fi_lock);
5126 filp = find_readable_file_locked(fp);
5128 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5129 spin_unlock(&fp->fi_lock);
5130 file_lock->fl_type = F_RDLCK;
5133 case NFS4_WRITEW_LT:
5134 spin_lock(&fp->fi_lock);
5135 filp = find_writeable_file_locked(fp);
5137 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5138 spin_unlock(&fp->fi_lock);
5139 file_lock->fl_type = F_WRLCK;
5142 status = nfserr_inval;
5146 status = nfserr_openmode;
5149 file_lock->fl_owner = (fl_owner_t)lock_sop;
5150 file_lock->fl_pid = current->tgid;
5151 file_lock->fl_file = filp;
5152 file_lock->fl_flags = FL_POSIX;
5153 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5154 file_lock->fl_start = lock->lk_offset;
5155 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5156 nfs4_transform_lock_offset(file_lock);
5158 conflock = locks_alloc_lock();
5160 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5161 status = nfserr_jukebox;
5165 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5167 case 0: /* success! */
5168 update_stateid(&lock_stp->st_stid.sc_stateid);
5169 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5173 case (EAGAIN): /* conflock holds conflicting lock */
5174 status = nfserr_denied;
5175 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5176 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5179 status = nfserr_deadlock;
5182 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5183 status = nfserrno(err);
5190 /* Bump seqid manually if the 4.0 replay owner is openowner */
5191 if (cstate->replay_owner &&
5192 cstate->replay_owner != &lock_sop->lo_owner &&
5193 seqid_mutating_err(ntohl(status)))
5194 lock_sop->lo_owner.so_seqid++;
5197 * If this is a new, never-before-used stateid, and we are
5198 * returning an error, then just go ahead and release it.
5201 release_lock_stateid(lock_stp);
5203 nfs4_put_stid(&lock_stp->st_stid);
5206 nfs4_put_stid(&open_stp->st_stid);
5207 nfsd4_bump_seqid(cstate, status);
5208 nfs4_unlock_state();
5210 locks_free_lock(file_lock);
5212 locks_free_lock(conflock);
5217 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5218 * so we do a temporary open here just to get an open file to pass to
5219 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5222 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5225 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5227 err = nfserrno(vfs_test_lock(file, lock));
5237 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5238 struct nfsd4_lockt *lockt)
5240 struct file_lock *file_lock = NULL;
5241 struct nfs4_lockowner *lo = NULL;
5243 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5245 if (locks_in_grace(SVC_NET(rqstp)))
5246 return nfserr_grace;
5248 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5249 return nfserr_inval;
5253 if (!nfsd4_has_session(cstate)) {
5254 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5259 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5262 file_lock = locks_alloc_lock();
5264 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5265 status = nfserr_jukebox;
5268 locks_init_lock(file_lock);
5269 switch (lockt->lt_type) {
5272 file_lock->fl_type = F_RDLCK;
5275 case NFS4_WRITEW_LT:
5276 file_lock->fl_type = F_WRLCK;
5279 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5280 status = nfserr_inval;
5284 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5287 file_lock->fl_owner = (fl_owner_t)lo;
5288 file_lock->fl_pid = current->tgid;
5289 file_lock->fl_flags = FL_POSIX;
5291 file_lock->fl_start = lockt->lt_offset;
5292 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5294 nfs4_transform_lock_offset(file_lock);
5296 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5300 if (file_lock->fl_type != F_UNLCK) {
5301 status = nfserr_denied;
5302 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5306 nfs4_put_stateowner(&lo->lo_owner);
5307 nfs4_unlock_state();
5309 locks_free_lock(file_lock);
5314 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5315 struct nfsd4_locku *locku)
5317 struct nfs4_ol_stateid *stp;
5318 struct file *filp = NULL;
5319 struct file_lock *file_lock = NULL;
5322 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5324 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5325 (long long) locku->lu_offset,
5326 (long long) locku->lu_length);
5328 if (check_lock_length(locku->lu_offset, locku->lu_length))
5329 return nfserr_inval;
5333 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5334 &locku->lu_stateid, NFS4_LOCK_STID,
5338 filp = find_any_file(stp->st_stid.sc_file);
5340 status = nfserr_lock_range;
5343 file_lock = locks_alloc_lock();
5345 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5346 status = nfserr_jukebox;
5349 locks_init_lock(file_lock);
5350 file_lock->fl_type = F_UNLCK;
5351 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5352 file_lock->fl_pid = current->tgid;
5353 file_lock->fl_file = filp;
5354 file_lock->fl_flags = FL_POSIX;
5355 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5356 file_lock->fl_start = locku->lu_offset;
5358 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5360 nfs4_transform_lock_offset(file_lock);
5362 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5364 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5367 update_stateid(&stp->st_stid.sc_stateid);
5368 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5372 nfs4_put_stid(&stp->st_stid);
5374 nfsd4_bump_seqid(cstate, status);
5375 nfs4_unlock_state();
5377 locks_free_lock(file_lock);
5381 status = nfserrno(err);
5387 * true: locks held by lockowner
5388 * false: no locks held by lockowner
5391 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5393 struct file_lock **flpp;
5395 struct file *filp = find_any_file(fp);
5396 struct inode *inode;
5399 /* Any valid lock stateid should have some sort of access */
5404 inode = file_inode(filp);
5406 spin_lock(&inode->i_lock);
5407 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5408 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5413 spin_unlock(&inode->i_lock);
5419 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5420 struct nfsd4_compound_state *cstate,
5421 struct nfsd4_release_lockowner *rlockowner)
5423 clientid_t *clid = &rlockowner->rl_clientid;
5424 struct nfs4_stateowner *sop;
5425 struct nfs4_lockowner *lo = NULL;
5426 struct nfs4_ol_stateid *stp;
5427 struct xdr_netobj *owner = &rlockowner->rl_owner;
5428 unsigned int hashval = ownerstr_hashval(owner);
5430 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5431 struct nfs4_client *clp;
5433 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5434 clid->cl_boot, clid->cl_id);
5438 status = lookup_clientid(clid, cstate, nn);
5443 /* Find the matching lock stateowner */
5444 spin_lock(&clp->cl_lock);
5445 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5448 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5451 /* see if there are still any locks associated with it */
5452 lo = lockowner(sop);
5453 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5454 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5455 status = nfserr_locks_held;
5456 spin_unlock(&clp->cl_lock);
5461 atomic_inc(&sop->so_count);
5464 spin_unlock(&clp->cl_lock);
5466 release_lockowner(lo);
5468 nfs4_unlock_state();
5472 static inline struct nfs4_client_reclaim *
5475 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5479 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5481 struct nfs4_client_reclaim *crp;
5483 crp = nfsd4_find_reclaim_client(name, nn);
5484 return (crp && crp->cr_clp);
5488 * failure => all reset bets are off, nfserr_no_grace...
5490 struct nfs4_client_reclaim *
5491 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5493 unsigned int strhashval;
5494 struct nfs4_client_reclaim *crp;
5496 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5497 crp = alloc_reclaim();
5499 strhashval = clientstr_hashval(name);
5500 INIT_LIST_HEAD(&crp->cr_strhash);
5501 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5502 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5504 nn->reclaim_str_hashtbl_size++;
5510 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5512 list_del(&crp->cr_strhash);
5514 nn->reclaim_str_hashtbl_size--;
5518 nfs4_release_reclaim(struct nfsd_net *nn)
5520 struct nfs4_client_reclaim *crp = NULL;
5523 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5524 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5525 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5526 struct nfs4_client_reclaim, cr_strhash);
5527 nfs4_remove_reclaim_record(crp, nn);
5530 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5534 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5535 struct nfs4_client_reclaim *
5536 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5538 unsigned int strhashval;
5539 struct nfs4_client_reclaim *crp = NULL;
5541 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5543 strhashval = clientstr_hashval(recdir);
5544 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5545 if (same_name(crp->cr_recdir, recdir)) {
5553 * Called from OPEN. Look for clientid in reclaim list.
5556 nfs4_check_open_reclaim(clientid_t *clid,
5557 struct nfsd4_compound_state *cstate,
5558 struct nfsd_net *nn)
5562 /* find clientid in conf_id_hashtbl */
5563 status = lookup_clientid(clid, cstate, nn);
5565 return nfserr_reclaim_bad;
5567 if (nfsd4_client_record_check(cstate->clp))
5568 return nfserr_reclaim_bad;
5573 #ifdef CONFIG_NFSD_FAULT_INJECTION
5575 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5577 if (mark_client_expired(clp))
5583 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5585 char buf[INET6_ADDRSTRLEN];
5586 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5587 printk(KERN_INFO "NFS Client: %s\n", buf);
5591 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5594 char buf[INET6_ADDRSTRLEN];
5595 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5596 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5599 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5600 void (*func)(struct nfs4_ol_stateid *))
5602 struct nfs4_openowner *oop;
5603 struct nfs4_ol_stateid *stp, *st_next;
5604 struct nfs4_ol_stateid *lst, *lst_next;
5607 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5608 list_for_each_entry_safe(stp, st_next,
5609 &oop->oo_owner.so_stateids, st_perstateowner) {
5610 list_for_each_entry_safe(lst, lst_next,
5611 &stp->st_locks, st_locks) {
5623 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5625 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5628 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5630 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5631 nfsd_print_count(clp, count, "locked files");
5635 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5637 struct nfs4_openowner *oop, *next;
5640 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5650 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5652 return nfsd_foreach_client_open(clp, max, release_openowner);
5655 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5657 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5658 nfsd_print_count(clp, count, "open files");
5662 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5663 struct list_head *victims)
5665 struct nfs4_delegation *dp, *next;
5668 lockdep_assert_held(&state_lock);
5669 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5672 * It's not safe to mess with delegations that have a
5673 * non-zero dl_time. They might have already been broken
5674 * and could be processed by the laundromat outside of
5675 * the state_lock. Just leave them be.
5677 if (dp->dl_time != 0)
5680 unhash_delegation_locked(dp);
5681 list_add(&dp->dl_recall_lru, victims);
5689 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5691 struct nfs4_delegation *dp, *next;
5695 spin_lock(&state_lock);
5696 count = nfsd_find_all_delegations(clp, max, &victims);
5697 spin_unlock(&state_lock);
5699 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5700 list_del_init(&dp->dl_recall_lru);
5701 revoke_delegation(dp);
5707 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5709 struct nfs4_delegation *dp;
5713 spin_lock(&state_lock);
5714 count = nfsd_find_all_delegations(clp, max, &victims);
5715 while (!list_empty(&victims)) {
5716 dp = list_first_entry(&victims, struct nfs4_delegation,
5718 list_del_init(&dp->dl_recall_lru);
5720 nfsd_break_one_deleg(dp);
5722 spin_unlock(&state_lock);
5727 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5731 spin_lock(&state_lock);
5732 count = nfsd_find_all_delegations(clp, max, NULL);
5733 spin_unlock(&state_lock);
5735 nfsd_print_count(clp, count, "delegations");
5739 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5741 struct nfs4_client *clp, *next;
5743 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5745 if (!nfsd_netns_ready(nn))
5748 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5749 count += func(clp, max - count);
5750 if ((max != 0) && (count >= max))
5757 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5759 struct nfs4_client *clp;
5760 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5762 if (!nfsd_netns_ready(nn))
5765 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5766 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5772 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5775 * Since the lifetime of a delegation isn't limited to that of an open, a
5776 * client may quite reasonably hang on to a delegation as long as it has
5777 * the inode cached. This becomes an obvious problem the first time a
5778 * client's inode cache approaches the size of the server's total memory.
5780 * For now we avoid this problem by imposing a hard limit on the number
5781 * of delegations, which varies according to the server's memory size.
5784 set_max_delegations(void)
5787 * Allow at most 4 delegations per megabyte of RAM. Quick
5788 * estimates suggest that in the worst case (where every delegation
5789 * is for a different inode), a delegation could take about 1.5K,
5790 * giving a worst case usage of about 6% of memory.
5792 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5795 static int nfs4_state_create_net(struct net *net)
5797 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5800 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5801 CLIENT_HASH_SIZE, GFP_KERNEL);
5802 if (!nn->conf_id_hashtbl)
5804 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5805 CLIENT_HASH_SIZE, GFP_KERNEL);
5806 if (!nn->unconf_id_hashtbl)
5808 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5809 SESSION_HASH_SIZE, GFP_KERNEL);
5810 if (!nn->sessionid_hashtbl)
5813 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5814 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5815 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5817 for (i = 0; i < SESSION_HASH_SIZE; i++)
5818 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5819 nn->conf_name_tree = RB_ROOT;
5820 nn->unconf_name_tree = RB_ROOT;
5821 INIT_LIST_HEAD(&nn->client_lru);
5822 INIT_LIST_HEAD(&nn->close_lru);
5823 INIT_LIST_HEAD(&nn->del_recall_lru);
5824 spin_lock_init(&nn->client_lock);
5826 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5832 kfree(nn->unconf_id_hashtbl);
5834 kfree(nn->conf_id_hashtbl);
5840 nfs4_state_destroy_net(struct net *net)
5843 struct nfs4_client *clp = NULL;
5844 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5846 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5847 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5848 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5849 destroy_client(clp);
5853 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5854 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5855 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5856 destroy_client(clp);
5860 kfree(nn->sessionid_hashtbl);
5861 kfree(nn->unconf_id_hashtbl);
5862 kfree(nn->conf_id_hashtbl);
5867 nfs4_state_start_net(struct net *net)
5869 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5872 ret = nfs4_state_create_net(net);
5875 nfsd4_client_tracking_init(net);
5876 nn->boot_time = get_seconds();
5877 locks_start_grace(net, &nn->nfsd4_manager);
5878 nn->grace_ended = false;
5879 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5880 nn->nfsd4_grace, net);
5881 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5885 /* initialization to perform when the nfsd service is started: */
5888 nfs4_state_start(void)
5892 ret = set_callback_cred();
5895 laundry_wq = create_singlethread_workqueue("nfsd4");
5896 if (laundry_wq == NULL) {
5900 ret = nfsd4_create_callback_queue();
5902 goto out_free_laundry;
5904 set_max_delegations();
5909 destroy_workqueue(laundry_wq);
5915 nfs4_state_shutdown_net(struct net *net)
5917 struct nfs4_delegation *dp = NULL;
5918 struct list_head *pos, *next, reaplist;
5919 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5921 cancel_delayed_work_sync(&nn->laundromat_work);
5922 locks_end_grace(&nn->nfsd4_manager);
5925 INIT_LIST_HEAD(&reaplist);
5926 spin_lock(&state_lock);
5927 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5928 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5929 unhash_delegation_locked(dp);
5930 list_add(&dp->dl_recall_lru, &reaplist);
5932 spin_unlock(&state_lock);
5933 list_for_each_safe(pos, next, &reaplist) {
5934 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5935 list_del_init(&dp->dl_recall_lru);
5936 nfs4_put_stid(&dp->dl_stid);
5939 nfsd4_client_tracking_exit(net);
5940 nfs4_state_destroy_net(net);
5941 nfs4_unlock_state();
5945 nfs4_state_shutdown(void)
5947 destroy_workqueue(laundry_wq);
5948 nfsd4_destroy_callback_queue();
5952 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5954 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5955 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5959 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5961 if (cstate->minorversion) {
5962 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5963 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5968 clear_current_stateid(struct nfsd4_compound_state *cstate)
5970 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5974 * functions to set current state id
5977 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5979 put_stateid(cstate, &odp->od_stateid);
5983 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5985 put_stateid(cstate, &open->op_stateid);
5989 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5991 put_stateid(cstate, &close->cl_stateid);
5995 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5997 put_stateid(cstate, &lock->lk_resp_stateid);
6001 * functions to consume current state id
6005 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6007 get_stateid(cstate, &odp->od_stateid);
6011 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6013 get_stateid(cstate, &drp->dr_stateid);
6017 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6019 get_stateid(cstate, &fsp->fr_stateid);
6023 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6025 get_stateid(cstate, &setattr->sa_stateid);
6029 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6031 get_stateid(cstate, &close->cl_stateid);
6035 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6037 get_stateid(cstate, &locku->lu_stateid);
6041 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6043 get_stateid(cstate, &read->rd_stateid);
6047 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6049 get_stateid(cstate, &write->wr_stateid);