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 unhash_lock_stateid(struct nfs4_ol_stateid *stp)
988 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
990 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
992 list_del_init(&stp->st_locks);
993 unhash_generic_stateid(stp);
994 unhash_stid(&stp->st_stid);
997 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
999 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1001 spin_lock(&oo->oo_owner.so_client->cl_lock);
1002 unhash_lock_stateid(stp);
1003 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1004 nfs4_put_stid(&stp->st_stid);
1007 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1009 struct nfs4_client *clp = lo->lo_owner.so_client;
1011 lockdep_assert_held(&clp->cl_lock);
1013 list_del_init(&lo->lo_owner.so_strhash);
1016 static void release_lockowner(struct nfs4_lockowner *lo)
1018 struct nfs4_client *clp = lo->lo_owner.so_client;
1019 struct nfs4_ol_stateid *stp;
1020 struct list_head reaplist;
1022 INIT_LIST_HEAD(&reaplist);
1024 spin_lock(&clp->cl_lock);
1025 unhash_lockowner_locked(lo);
1026 while (!list_empty(&lo->lo_owner.so_stateids)) {
1027 stp = list_first_entry(&lo->lo_owner.so_stateids,
1028 struct nfs4_ol_stateid, st_perstateowner);
1029 unhash_lock_stateid(stp);
1031 * We now know that no new references can be added to the
1032 * stateid. If ours is the last one, finish the unhashing
1033 * and put it on the list to be reaped.
1035 if (atomic_dec_and_test(&stp->st_stid.sc_count)) {
1036 idr_remove(&clp->cl_stateids,
1037 stp->st_stid.sc_stateid.si_opaque.so_id);
1038 list_add(&stp->st_locks, &reaplist);
1041 spin_unlock(&clp->cl_lock);
1042 while (!list_empty(&reaplist)) {
1043 stp = list_first_entry(&reaplist, struct nfs4_ol_stateid,
1045 list_del(&stp->st_locks);
1046 stp->st_stid.sc_free(&stp->st_stid);
1048 nfs4_put_stateowner(&lo->lo_owner);
1051 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp)
1052 __releases(&open_stp->st_stateowner->so_client->cl_lock)
1053 __acquires(&open_stp->st_stateowner->so_client->cl_lock)
1055 struct nfs4_ol_stateid *stp;
1057 while (!list_empty(&open_stp->st_locks)) {
1058 stp = list_entry(open_stp->st_locks.next,
1059 struct nfs4_ol_stateid, st_locks);
1060 spin_unlock(&open_stp->st_stateowner->so_client->cl_lock);
1061 release_lock_stateid(stp);
1062 spin_lock(&open_stp->st_stateowner->so_client->cl_lock);
1066 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
1068 spin_lock(&stp->st_stateowner->so_client->cl_lock);
1069 unhash_generic_stateid(stp);
1070 release_open_stateid_locks(stp);
1071 spin_unlock(&stp->st_stateowner->so_client->cl_lock);
1074 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1076 unhash_open_stateid(stp);
1077 nfs4_put_stid(&stp->st_stid);
1080 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1082 struct nfs4_client *clp = oo->oo_owner.so_client;
1084 lockdep_assert_held(&clp->cl_lock);
1086 list_del_init(&oo->oo_owner.so_strhash);
1087 list_del_init(&oo->oo_perclient);
1090 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1092 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
1095 list_del_init(&oo->oo_close_lru);
1096 oo->oo_last_closed_stid = NULL;
1097 nfs4_put_stid(&s->st_stid);
1101 static void release_openowner_stateids(struct nfs4_openowner *oo)
1103 struct nfs4_ol_stateid *stp;
1104 struct nfs4_client *clp = oo->oo_owner.so_client;
1106 lockdep_assert_held(&clp->cl_lock);
1108 while (!list_empty(&oo->oo_owner.so_stateids)) {
1109 stp = list_first_entry(&oo->oo_owner.so_stateids,
1110 struct nfs4_ol_stateid, st_perstateowner);
1111 spin_unlock(&clp->cl_lock);
1112 release_open_stateid(stp);
1113 spin_lock(&clp->cl_lock);
1117 static void release_openowner(struct nfs4_openowner *oo)
1119 struct nfs4_client *clp = oo->oo_owner.so_client;
1121 spin_lock(&clp->cl_lock);
1122 unhash_openowner_locked(oo);
1123 release_openowner_stateids(oo);
1124 spin_unlock(&clp->cl_lock);
1125 release_last_closed_stateid(oo);
1126 nfs4_put_stateowner(&oo->oo_owner);
1130 hash_sessionid(struct nfs4_sessionid *sessionid)
1132 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1134 return sid->sequence % SESSION_HASH_SIZE;
1139 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1141 u32 *ptr = (u32 *)(&sessionid->data[0]);
1142 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1146 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1152 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1153 * won't be used for replay.
1155 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1157 struct nfs4_stateowner *so = cstate->replay_owner;
1159 if (nfserr == nfserr_replay_me)
1162 if (!seqid_mutating_err(ntohl(nfserr))) {
1163 nfsd4_cstate_clear_replay(cstate);
1168 if (so->so_is_open_owner)
1169 release_last_closed_stateid(openowner(so));
1175 gen_sessionid(struct nfsd4_session *ses)
1177 struct nfs4_client *clp = ses->se_client;
1178 struct nfsd4_sessionid *sid;
1180 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1181 sid->clientid = clp->cl_clientid;
1182 sid->sequence = current_sessionid++;
1187 * The protocol defines ca_maxresponssize_cached to include the size of
1188 * the rpc header, but all we need to cache is the data starting after
1189 * the end of the initial SEQUENCE operation--the rest we regenerate
1190 * each time. Therefore we can advertise a ca_maxresponssize_cached
1191 * value that is the number of bytes in our cache plus a few additional
1192 * bytes. In order to stay on the safe side, and not promise more than
1193 * we can cache, those additional bytes must be the minimum possible: 24
1194 * bytes of rpc header (xid through accept state, with AUTH_NULL
1195 * verifier), 12 for the compound header (with zero-length tag), and 44
1196 * for the SEQUENCE op response:
1198 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1201 free_session_slots(struct nfsd4_session *ses)
1205 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1206 kfree(ses->se_slots[i]);
1210 * We don't actually need to cache the rpc and session headers, so we
1211 * can allocate a little less for each slot:
1213 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1217 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1220 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1221 return size + sizeof(struct nfsd4_slot);
1225 * XXX: If we run out of reserved DRC memory we could (up to a point)
1226 * re-negotiate active sessions and reduce their slot usage to make
1227 * room for new connections. For now we just fail the create session.
1229 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1231 u32 slotsize = slot_bytes(ca);
1232 u32 num = ca->maxreqs;
1235 spin_lock(&nfsd_drc_lock);
1236 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1237 nfsd_drc_max_mem - nfsd_drc_mem_used);
1238 num = min_t(int, num, avail / slotsize);
1239 nfsd_drc_mem_used += num * slotsize;
1240 spin_unlock(&nfsd_drc_lock);
1245 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1247 int slotsize = slot_bytes(ca);
1249 spin_lock(&nfsd_drc_lock);
1250 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1251 spin_unlock(&nfsd_drc_lock);
1254 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1255 struct nfsd4_channel_attrs *battrs)
1257 int numslots = fattrs->maxreqs;
1258 int slotsize = slot_bytes(fattrs);
1259 struct nfsd4_session *new;
1262 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1263 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1264 mem = numslots * sizeof(struct nfsd4_slot *);
1266 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1269 /* allocate each struct nfsd4_slot and data cache in one piece */
1270 for (i = 0; i < numslots; i++) {
1271 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1272 if (!new->se_slots[i])
1276 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1277 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1282 kfree(new->se_slots[i]);
1287 static void free_conn(struct nfsd4_conn *c)
1289 svc_xprt_put(c->cn_xprt);
1293 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1295 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1296 struct nfs4_client *clp = c->cn_session->se_client;
1298 spin_lock(&clp->cl_lock);
1299 if (!list_empty(&c->cn_persession)) {
1300 list_del(&c->cn_persession);
1303 nfsd4_probe_callback(clp);
1304 spin_unlock(&clp->cl_lock);
1307 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1309 struct nfsd4_conn *conn;
1311 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1314 svc_xprt_get(rqstp->rq_xprt);
1315 conn->cn_xprt = rqstp->rq_xprt;
1316 conn->cn_flags = flags;
1317 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1321 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1323 conn->cn_session = ses;
1324 list_add(&conn->cn_persession, &ses->se_conns);
1327 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1329 struct nfs4_client *clp = ses->se_client;
1331 spin_lock(&clp->cl_lock);
1332 __nfsd4_hash_conn(conn, ses);
1333 spin_unlock(&clp->cl_lock);
1336 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1338 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1339 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1342 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1346 nfsd4_hash_conn(conn, ses);
1347 ret = nfsd4_register_conn(conn);
1349 /* oops; xprt is already down: */
1350 nfsd4_conn_lost(&conn->cn_xpt_user);
1351 /* We may have gained or lost a callback channel: */
1352 nfsd4_probe_callback_sync(ses->se_client);
1355 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1357 u32 dir = NFS4_CDFC4_FORE;
1359 if (cses->flags & SESSION4_BACK_CHAN)
1360 dir |= NFS4_CDFC4_BACK;
1361 return alloc_conn(rqstp, dir);
1364 /* must be called under client_lock */
1365 static void nfsd4_del_conns(struct nfsd4_session *s)
1367 struct nfs4_client *clp = s->se_client;
1368 struct nfsd4_conn *c;
1370 spin_lock(&clp->cl_lock);
1371 while (!list_empty(&s->se_conns)) {
1372 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1373 list_del_init(&c->cn_persession);
1374 spin_unlock(&clp->cl_lock);
1376 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1379 spin_lock(&clp->cl_lock);
1381 spin_unlock(&clp->cl_lock);
1384 static void __free_session(struct nfsd4_session *ses)
1386 free_session_slots(ses);
1390 static void free_session(struct nfsd4_session *ses)
1392 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
1394 lockdep_assert_held(&nn->client_lock);
1395 nfsd4_del_conns(ses);
1396 nfsd4_put_drc_mem(&ses->se_fchannel);
1397 __free_session(ses);
1400 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1403 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1405 new->se_client = clp;
1408 INIT_LIST_HEAD(&new->se_conns);
1410 new->se_cb_seq_nr = 1;
1411 new->se_flags = cses->flags;
1412 new->se_cb_prog = cses->callback_prog;
1413 new->se_cb_sec = cses->cb_sec;
1414 atomic_set(&new->se_ref, 0);
1415 idx = hash_sessionid(&new->se_sessionid);
1416 spin_lock(&nn->client_lock);
1417 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1418 spin_lock(&clp->cl_lock);
1419 list_add(&new->se_perclnt, &clp->cl_sessions);
1420 spin_unlock(&clp->cl_lock);
1421 spin_unlock(&nn->client_lock);
1423 if (cses->flags & SESSION4_BACK_CHAN) {
1424 struct sockaddr *sa = svc_addr(rqstp);
1426 * This is a little silly; with sessions there's no real
1427 * use for the callback address. Use the peer address
1428 * as a reasonable default for now, but consider fixing
1429 * the rpc client not to require an address in the
1432 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1433 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1437 /* caller must hold client_lock */
1438 static struct nfsd4_session *
1439 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1441 struct nfsd4_session *elem;
1443 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1445 dump_sessionid(__func__, sessionid);
1446 idx = hash_sessionid(sessionid);
1447 /* Search in the appropriate list */
1448 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1449 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1450 NFS4_MAX_SESSIONID_LEN)) {
1455 dprintk("%s: session not found\n", __func__);
1459 static struct nfsd4_session *
1460 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1463 struct nfsd4_session *session;
1464 __be32 status = nfserr_badsession;
1466 session = __find_in_sessionid_hashtbl(sessionid, net);
1469 status = nfsd4_get_session_locked(session);
1477 /* caller must hold client_lock */
1479 unhash_session(struct nfsd4_session *ses)
1481 list_del(&ses->se_hash);
1482 spin_lock(&ses->se_client->cl_lock);
1483 list_del(&ses->se_perclnt);
1484 spin_unlock(&ses->se_client->cl_lock);
1487 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1489 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1491 if (clid->cl_boot == nn->boot_time)
1493 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1494 clid->cl_boot, clid->cl_id, nn->boot_time);
1499 * XXX Should we use a slab cache ?
1500 * This type of memory management is somewhat inefficient, but we use it
1501 * anyway since SETCLIENTID is not a common operation.
1503 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1505 struct nfs4_client *clp;
1508 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1511 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1512 if (clp->cl_name.data == NULL)
1514 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1515 OWNER_HASH_SIZE, GFP_KERNEL);
1516 if (!clp->cl_ownerstr_hashtbl)
1517 goto err_no_hashtbl;
1518 for (i = 0; i < OWNER_HASH_SIZE; i++)
1519 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1520 clp->cl_name.len = name.len;
1521 INIT_LIST_HEAD(&clp->cl_sessions);
1522 idr_init(&clp->cl_stateids);
1523 atomic_set(&clp->cl_refcount, 0);
1524 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1525 INIT_LIST_HEAD(&clp->cl_idhash);
1526 INIT_LIST_HEAD(&clp->cl_openowners);
1527 INIT_LIST_HEAD(&clp->cl_delegations);
1528 INIT_LIST_HEAD(&clp->cl_lru);
1529 INIT_LIST_HEAD(&clp->cl_callbacks);
1530 INIT_LIST_HEAD(&clp->cl_revoked);
1531 spin_lock_init(&clp->cl_lock);
1532 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1535 kfree(clp->cl_name.data);
1542 free_client(struct nfs4_client *clp)
1544 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1546 lockdep_assert_held(&nn->client_lock);
1547 while (!list_empty(&clp->cl_sessions)) {
1548 struct nfsd4_session *ses;
1549 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1551 list_del(&ses->se_perclnt);
1552 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1555 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1556 free_svc_cred(&clp->cl_cred);
1557 kfree(clp->cl_ownerstr_hashtbl);
1558 kfree(clp->cl_name.data);
1559 idr_destroy(&clp->cl_stateids);
1563 /* must be called under the client_lock */
1565 unhash_client_locked(struct nfs4_client *clp)
1567 struct nfsd4_session *ses;
1569 list_del(&clp->cl_lru);
1570 spin_lock(&clp->cl_lock);
1571 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1572 list_del_init(&ses->se_hash);
1573 spin_unlock(&clp->cl_lock);
1577 destroy_client(struct nfs4_client *clp)
1579 struct nfs4_openowner *oo;
1580 struct nfs4_delegation *dp;
1581 struct list_head reaplist;
1582 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1584 INIT_LIST_HEAD(&reaplist);
1585 spin_lock(&state_lock);
1586 while (!list_empty(&clp->cl_delegations)) {
1587 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1588 unhash_delegation_locked(dp);
1589 list_add(&dp->dl_recall_lru, &reaplist);
1591 spin_unlock(&state_lock);
1592 while (!list_empty(&reaplist)) {
1593 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1594 list_del_init(&dp->dl_recall_lru);
1595 nfs4_put_stid(&dp->dl_stid);
1597 while (!list_empty(&clp->cl_revoked)) {
1598 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1599 list_del_init(&dp->dl_recall_lru);
1600 nfs4_put_stid(&dp->dl_stid);
1602 while (!list_empty(&clp->cl_openowners)) {
1603 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1604 atomic_inc(&oo->oo_owner.so_count);
1605 release_openowner(oo);
1607 nfsd4_shutdown_callback(clp);
1608 if (clp->cl_cb_conn.cb_xprt)
1609 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1610 list_del(&clp->cl_idhash);
1611 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1612 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1614 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1615 spin_lock(&nn->client_lock);
1616 unhash_client_locked(clp);
1617 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1619 spin_unlock(&nn->client_lock);
1622 static void expire_client(struct nfs4_client *clp)
1624 nfsd4_client_record_remove(clp);
1625 destroy_client(clp);
1628 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1630 memcpy(target->cl_verifier.data, source->data,
1631 sizeof(target->cl_verifier.data));
1634 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1636 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1637 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1640 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1642 if (source->cr_principal) {
1643 target->cr_principal =
1644 kstrdup(source->cr_principal, GFP_KERNEL);
1645 if (target->cr_principal == NULL)
1648 target->cr_principal = NULL;
1649 target->cr_flavor = source->cr_flavor;
1650 target->cr_uid = source->cr_uid;
1651 target->cr_gid = source->cr_gid;
1652 target->cr_group_info = source->cr_group_info;
1653 get_group_info(target->cr_group_info);
1654 target->cr_gss_mech = source->cr_gss_mech;
1655 if (source->cr_gss_mech)
1656 gss_mech_get(source->cr_gss_mech);
1661 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1665 res = o1->len - o2->len;
1668 return (long long)memcmp(o1->data, o2->data, o1->len);
1671 static int same_name(const char *n1, const char *n2)
1673 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1677 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1679 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1683 same_clid(clientid_t *cl1, clientid_t *cl2)
1685 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1688 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1692 if (g1->ngroups != g2->ngroups)
1694 for (i=0; i<g1->ngroups; i++)
1695 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1701 * RFC 3530 language requires clid_inuse be returned when the
1702 * "principal" associated with a requests differs from that previously
1703 * used. We use uid, gid's, and gss principal string as our best
1704 * approximation. We also don't want to allow non-gss use of a client
1705 * established using gss: in theory cr_principal should catch that
1706 * change, but in practice cr_principal can be null even in the gss case
1707 * since gssd doesn't always pass down a principal string.
1709 static bool is_gss_cred(struct svc_cred *cr)
1711 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1712 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1717 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1719 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1720 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1721 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1722 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1724 if (cr1->cr_principal == cr2->cr_principal)
1726 if (!cr1->cr_principal || !cr2->cr_principal)
1728 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1731 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1733 struct svc_cred *cr = &rqstp->rq_cred;
1736 if (!cr->cr_gss_mech)
1738 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1739 return service == RPC_GSS_SVC_INTEGRITY ||
1740 service == RPC_GSS_SVC_PRIVACY;
1743 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1745 struct svc_cred *cr = &rqstp->rq_cred;
1747 if (!cl->cl_mach_cred)
1749 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1751 if (!svc_rqst_integrity_protected(rqstp))
1753 if (!cr->cr_principal)
1755 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1758 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1760 static u32 current_clientid = 1;
1762 clp->cl_clientid.cl_boot = nn->boot_time;
1763 clp->cl_clientid.cl_id = current_clientid++;
1766 static void gen_confirm(struct nfs4_client *clp)
1772 * This is opaque to client, so no need to byte-swap. Use
1773 * __force to keep sparse happy
1775 verf[0] = (__force __be32)get_seconds();
1776 verf[1] = (__force __be32)i++;
1777 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1780 static struct nfs4_stid *
1781 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1783 struct nfs4_stid *ret;
1785 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1786 if (!ret || !ret->sc_type)
1791 static struct nfs4_stid *
1792 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1794 struct nfs4_stid *s;
1796 spin_lock(&cl->cl_lock);
1797 s = find_stateid_locked(cl, t);
1799 if (typemask & s->sc_type)
1800 atomic_inc(&s->sc_count);
1804 spin_unlock(&cl->cl_lock);
1808 static struct nfs4_client *create_client(struct xdr_netobj name,
1809 struct svc_rqst *rqstp, nfs4_verifier *verf)
1811 struct nfs4_client *clp;
1812 struct sockaddr *sa = svc_addr(rqstp);
1814 struct net *net = SVC_NET(rqstp);
1815 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1817 clp = alloc_client(name);
1821 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1823 spin_lock(&nn->client_lock);
1825 spin_unlock(&nn->client_lock);
1828 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1829 clp->cl_time = get_seconds();
1830 clear_bit(0, &clp->cl_cb_slot_busy);
1831 copy_verf(clp, verf);
1832 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1834 clp->cl_cb_session = NULL;
1840 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1842 struct rb_node **new = &(root->rb_node), *parent = NULL;
1843 struct nfs4_client *clp;
1846 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1849 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1850 new = &((*new)->rb_left);
1852 new = &((*new)->rb_right);
1855 rb_link_node(&new_clp->cl_namenode, parent, new);
1856 rb_insert_color(&new_clp->cl_namenode, root);
1859 static struct nfs4_client *
1860 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1863 struct rb_node *node = root->rb_node;
1864 struct nfs4_client *clp;
1867 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1868 cmp = compare_blob(&clp->cl_name, name);
1870 node = node->rb_left;
1872 node = node->rb_right;
1880 add_to_unconfirmed(struct nfs4_client *clp)
1882 unsigned int idhashval;
1883 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1885 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1886 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1887 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1888 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1893 move_to_confirmed(struct nfs4_client *clp)
1895 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1896 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1898 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1899 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1900 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1901 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1902 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1906 static struct nfs4_client *
1907 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1909 struct nfs4_client *clp;
1910 unsigned int idhashval = clientid_hashval(clid->cl_id);
1912 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1913 if (same_clid(&clp->cl_clientid, clid)) {
1914 if ((bool)clp->cl_minorversion != sessions)
1923 static struct nfs4_client *
1924 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1926 struct list_head *tbl = nn->conf_id_hashtbl;
1928 return find_client_in_id_table(tbl, clid, sessions);
1931 static struct nfs4_client *
1932 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1934 struct list_head *tbl = nn->unconf_id_hashtbl;
1936 return find_client_in_id_table(tbl, clid, sessions);
1939 static bool clp_used_exchangeid(struct nfs4_client *clp)
1941 return clp->cl_exchange_flags != 0;
1944 static struct nfs4_client *
1945 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1947 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1950 static struct nfs4_client *
1951 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1953 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1957 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1959 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1960 struct sockaddr *sa = svc_addr(rqstp);
1961 u32 scopeid = rpc_get_scope_id(sa);
1962 unsigned short expected_family;
1964 /* Currently, we only support tcp and tcp6 for the callback channel */
1965 if (se->se_callback_netid_len == 3 &&
1966 !memcmp(se->se_callback_netid_val, "tcp", 3))
1967 expected_family = AF_INET;
1968 else if (se->se_callback_netid_len == 4 &&
1969 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1970 expected_family = AF_INET6;
1974 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1975 se->se_callback_addr_len,
1976 (struct sockaddr *)&conn->cb_addr,
1977 sizeof(conn->cb_addr));
1979 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1982 if (conn->cb_addr.ss_family == AF_INET6)
1983 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1985 conn->cb_prog = se->se_callback_prog;
1986 conn->cb_ident = se->se_callback_ident;
1987 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1990 conn->cb_addr.ss_family = AF_UNSPEC;
1991 conn->cb_addrlen = 0;
1992 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1993 "will not receive delegations\n",
1994 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2000 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2003 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2005 struct xdr_buf *buf = resp->xdr.buf;
2006 struct nfsd4_slot *slot = resp->cstate.slot;
2009 dprintk("--> %s slot %p\n", __func__, slot);
2011 slot->sl_opcnt = resp->opcnt;
2012 slot->sl_status = resp->cstate.status;
2014 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2015 if (nfsd4_not_cached(resp)) {
2016 slot->sl_datalen = 0;
2019 base = resp->cstate.data_offset;
2020 slot->sl_datalen = buf->len - base;
2021 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2022 WARN("%s: sessions DRC could not cache compound\n", __func__);
2027 * Encode the replay sequence operation from the slot values.
2028 * If cachethis is FALSE encode the uncached rep error on the next
2029 * operation which sets resp->p and increments resp->opcnt for
2030 * nfs4svc_encode_compoundres.
2034 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2035 struct nfsd4_compoundres *resp)
2037 struct nfsd4_op *op;
2038 struct nfsd4_slot *slot = resp->cstate.slot;
2040 /* Encode the replayed sequence operation */
2041 op = &args->ops[resp->opcnt - 1];
2042 nfsd4_encode_operation(resp, op);
2044 /* Return nfserr_retry_uncached_rep in next operation. */
2045 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2046 op = &args->ops[resp->opcnt++];
2047 op->status = nfserr_retry_uncached_rep;
2048 nfsd4_encode_operation(resp, op);
2054 * The sequence operation is not cached because we can use the slot and
2058 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2059 struct nfsd4_sequence *seq)
2061 struct nfsd4_slot *slot = resp->cstate.slot;
2062 struct xdr_stream *xdr = &resp->xdr;
2066 dprintk("--> %s slot %p\n", __func__, slot);
2068 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2072 p = xdr_reserve_space(xdr, slot->sl_datalen);
2075 return nfserr_serverfault;
2077 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2078 xdr_commit_encode(xdr);
2080 resp->opcnt = slot->sl_opcnt;
2081 return slot->sl_status;
2085 * Set the exchange_id flags returned by the server.
2088 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2090 /* pNFS is not supported */
2091 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2093 /* Referrals are supported, Migration is not. */
2094 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2096 /* set the wire flags to return to client. */
2097 clid->flags = new->cl_exchange_flags;
2100 static bool client_has_state(struct nfs4_client *clp)
2103 * Note clp->cl_openowners check isn't quite right: there's no
2104 * need to count owners without stateid's.
2106 * Also note we should probably be using this in 4.0 case too.
2108 return !list_empty(&clp->cl_openowners)
2109 || !list_empty(&clp->cl_delegations)
2110 || !list_empty(&clp->cl_sessions);
2114 nfsd4_exchange_id(struct svc_rqst *rqstp,
2115 struct nfsd4_compound_state *cstate,
2116 struct nfsd4_exchange_id *exid)
2118 struct nfs4_client *unconf, *conf, *new;
2120 char addr_str[INET6_ADDRSTRLEN];
2121 nfs4_verifier verf = exid->verifier;
2122 struct sockaddr *sa = svc_addr(rqstp);
2123 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2124 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2126 rpc_ntop(sa, addr_str, sizeof(addr_str));
2127 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2128 "ip_addr=%s flags %x, spa_how %d\n",
2129 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2130 addr_str, exid->flags, exid->spa_how);
2132 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2133 return nfserr_inval;
2135 switch (exid->spa_how) {
2137 if (!svc_rqst_integrity_protected(rqstp))
2138 return nfserr_inval;
2141 default: /* checked by xdr code */
2144 return nfserr_encr_alg_unsupp;
2147 /* Cases below refer to rfc 5661 section 18.35.4: */
2149 conf = find_confirmed_client_by_name(&exid->clname, nn);
2151 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2152 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2155 if (!clp_used_exchangeid(conf)) { /* buggy client */
2156 status = nfserr_inval;
2159 if (!mach_creds_match(conf, rqstp)) {
2160 status = nfserr_wrong_cred;
2163 if (!creds_match) { /* case 9 */
2164 status = nfserr_perm;
2167 if (!verfs_match) { /* case 8 */
2168 status = nfserr_not_same;
2172 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2176 if (!creds_match) { /* case 3 */
2177 if (client_has_state(conf)) {
2178 status = nfserr_clid_inuse;
2181 expire_client(conf);
2184 if (verfs_match) { /* case 2 */
2185 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2189 /* case 5, client reboot */
2193 if (update) { /* case 7 */
2194 status = nfserr_noent;
2198 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2199 if (unconf) /* case 4, possible retry or client restart */
2200 expire_client(unconf);
2202 /* case 1 (normal case) */
2204 new = create_client(exid->clname, rqstp, &verf);
2206 status = nfserr_jukebox;
2209 new->cl_minorversion = cstate->minorversion;
2210 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2213 add_to_unconfirmed(new);
2215 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
2216 exid->clientid.cl_id = new->cl_clientid.cl_id;
2218 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
2219 nfsd4_set_ex_flags(new, exid);
2221 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2222 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
2226 nfs4_unlock_state();
2231 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2233 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2236 /* The slot is in use, and no response has been sent. */
2238 if (seqid == slot_seqid)
2239 return nfserr_jukebox;
2241 return nfserr_seq_misordered;
2243 /* Note unsigned 32-bit arithmetic handles wraparound: */
2244 if (likely(seqid == slot_seqid + 1))
2246 if (seqid == slot_seqid)
2247 return nfserr_replay_cache;
2248 return nfserr_seq_misordered;
2252 * Cache the create session result into the create session single DRC
2253 * slot cache by saving the xdr structure. sl_seqid has been set.
2254 * Do this for solo or embedded create session operations.
2257 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2258 struct nfsd4_clid_slot *slot, __be32 nfserr)
2260 slot->sl_status = nfserr;
2261 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2265 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2266 struct nfsd4_clid_slot *slot)
2268 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2269 return slot->sl_status;
2272 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2273 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2274 1 + /* MIN tag is length with zero, only length */ \
2275 3 + /* version, opcount, opcode */ \
2276 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2277 /* seqid, slotID, slotID, cache */ \
2278 4 ) * sizeof(__be32))
2280 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2281 2 + /* verifier: AUTH_NULL, length 0 */\
2283 1 + /* MIN tag is length with zero, only length */ \
2284 3 + /* opcount, opcode, opstatus*/ \
2285 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2286 /* seqid, slotID, slotID, slotID, status */ \
2287 5 ) * sizeof(__be32))
2289 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2291 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2293 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2294 return nfserr_toosmall;
2295 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2296 return nfserr_toosmall;
2297 ca->headerpadsz = 0;
2298 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2299 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2300 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2301 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2302 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2303 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2305 * Note decreasing slot size below client's request may make it
2306 * difficult for client to function correctly, whereas
2307 * decreasing the number of slots will (just?) affect
2308 * performance. When short on memory we therefore prefer to
2309 * decrease number of slots instead of their size. Clients that
2310 * request larger slots than they need will get poor results:
2312 ca->maxreqs = nfsd4_get_drc_mem(ca);
2314 return nfserr_jukebox;
2319 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2320 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2321 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2322 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2324 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2326 ca->headerpadsz = 0;
2329 * These RPC_MAX_HEADER macros are overkill, especially since we
2330 * don't even do gss on the backchannel yet. But this is still
2331 * less than 1k. Tighten up this estimate in the unlikely event
2332 * it turns out to be a problem for some client:
2334 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2335 return nfserr_toosmall;
2336 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2337 return nfserr_toosmall;
2338 ca->maxresp_cached = 0;
2340 return nfserr_toosmall;
2345 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2347 switch (cbs->flavor) {
2353 * GSS case: the spec doesn't allow us to return this
2354 * error. But it also doesn't allow us not to support
2356 * I'd rather this fail hard than return some error the
2357 * client might think it can already handle:
2359 return nfserr_encr_alg_unsupp;
2364 nfsd4_create_session(struct svc_rqst *rqstp,
2365 struct nfsd4_compound_state *cstate,
2366 struct nfsd4_create_session *cr_ses)
2368 struct sockaddr *sa = svc_addr(rqstp);
2369 struct nfs4_client *conf, *unconf;
2370 struct nfsd4_session *new;
2371 struct nfsd4_conn *conn;
2372 struct nfsd4_clid_slot *cs_slot = NULL;
2374 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2376 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2377 return nfserr_inval;
2378 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2381 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2384 status = check_backchannel_attrs(&cr_ses->back_channel);
2386 goto out_release_drc_mem;
2387 status = nfserr_jukebox;
2388 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2390 goto out_release_drc_mem;
2391 conn = alloc_conn_from_crses(rqstp, cr_ses);
2393 goto out_free_session;
2396 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2397 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2398 WARN_ON_ONCE(conf && unconf);
2401 status = nfserr_wrong_cred;
2402 if (!mach_creds_match(conf, rqstp))
2404 cs_slot = &conf->cl_cs_slot;
2405 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2406 if (status == nfserr_replay_cache) {
2407 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2409 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2410 status = nfserr_seq_misordered;
2413 } else if (unconf) {
2414 struct nfs4_client *old;
2415 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2416 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2417 status = nfserr_clid_inuse;
2420 status = nfserr_wrong_cred;
2421 if (!mach_creds_match(unconf, rqstp))
2423 cs_slot = &unconf->cl_cs_slot;
2424 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2426 /* an unconfirmed replay returns misordered */
2427 status = nfserr_seq_misordered;
2430 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2432 status = mark_client_expired(old);
2437 move_to_confirmed(unconf);
2440 status = nfserr_stale_clientid;
2445 * We do not support RDMA or persistent sessions
2447 cr_ses->flags &= ~SESSION4_PERSIST;
2448 cr_ses->flags &= ~SESSION4_RDMA;
2450 init_session(rqstp, new, conf, cr_ses);
2451 nfsd4_init_conn(rqstp, conn, new);
2453 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2454 NFS4_MAX_SESSIONID_LEN);
2455 cs_slot->sl_seqid++;
2456 cr_ses->seqid = cs_slot->sl_seqid;
2458 /* cache solo and embedded create sessions under the state lock */
2459 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2460 nfs4_unlock_state();
2463 nfs4_unlock_state();
2466 __free_session(new);
2467 out_release_drc_mem:
2468 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2472 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2475 case NFS4_CDFC4_FORE:
2476 case NFS4_CDFC4_BACK:
2478 case NFS4_CDFC4_FORE_OR_BOTH:
2479 case NFS4_CDFC4_BACK_OR_BOTH:
2480 *dir = NFS4_CDFC4_BOTH;
2483 return nfserr_inval;
2486 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2488 struct nfsd4_session *session = cstate->session;
2489 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2492 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2495 spin_lock(&nn->client_lock);
2496 session->se_cb_prog = bc->bc_cb_program;
2497 session->se_cb_sec = bc->bc_cb_sec;
2498 spin_unlock(&nn->client_lock);
2500 nfsd4_probe_callback(session->se_client);
2505 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2506 struct nfsd4_compound_state *cstate,
2507 struct nfsd4_bind_conn_to_session *bcts)
2510 struct nfsd4_conn *conn;
2511 struct nfsd4_session *session;
2512 struct net *net = SVC_NET(rqstp);
2513 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2515 if (!nfsd4_last_compound_op(rqstp))
2516 return nfserr_not_only_op;
2518 spin_lock(&nn->client_lock);
2519 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2520 spin_unlock(&nn->client_lock);
2522 goto out_no_session;
2523 status = nfserr_wrong_cred;
2524 if (!mach_creds_match(session->se_client, rqstp))
2526 status = nfsd4_map_bcts_dir(&bcts->dir);
2529 conn = alloc_conn(rqstp, bcts->dir);
2530 status = nfserr_jukebox;
2533 nfsd4_init_conn(rqstp, conn, session);
2536 nfsd4_put_session(session);
2538 nfs4_unlock_state();
2542 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2546 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2550 nfsd4_destroy_session(struct svc_rqst *r,
2551 struct nfsd4_compound_state *cstate,
2552 struct nfsd4_destroy_session *sessionid)
2554 struct nfsd4_session *ses;
2556 int ref_held_by_me = 0;
2557 struct net *net = SVC_NET(r);
2558 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2561 status = nfserr_not_only_op;
2562 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2563 if (!nfsd4_last_compound_op(r))
2567 dump_sessionid(__func__, &sessionid->sessionid);
2568 spin_lock(&nn->client_lock);
2569 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2571 goto out_client_lock;
2572 status = nfserr_wrong_cred;
2573 if (!mach_creds_match(ses->se_client, r))
2574 goto out_put_session;
2575 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2577 goto out_put_session;
2578 unhash_session(ses);
2579 spin_unlock(&nn->client_lock);
2581 nfsd4_probe_callback_sync(ses->se_client);
2583 spin_lock(&nn->client_lock);
2586 nfsd4_put_session_locked(ses);
2588 spin_unlock(&nn->client_lock);
2590 nfs4_unlock_state();
2594 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2596 struct nfsd4_conn *c;
2598 list_for_each_entry(c, &s->se_conns, cn_persession) {
2599 if (c->cn_xprt == xpt) {
2606 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2608 struct nfs4_client *clp = ses->se_client;
2609 struct nfsd4_conn *c;
2610 __be32 status = nfs_ok;
2613 spin_lock(&clp->cl_lock);
2614 c = __nfsd4_find_conn(new->cn_xprt, ses);
2617 status = nfserr_conn_not_bound_to_session;
2618 if (clp->cl_mach_cred)
2620 __nfsd4_hash_conn(new, ses);
2621 spin_unlock(&clp->cl_lock);
2622 ret = nfsd4_register_conn(new);
2624 /* oops; xprt is already down: */
2625 nfsd4_conn_lost(&new->cn_xpt_user);
2628 spin_unlock(&clp->cl_lock);
2633 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2635 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2637 return args->opcnt > session->se_fchannel.maxops;
2640 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2641 struct nfsd4_session *session)
2643 struct xdr_buf *xb = &rqstp->rq_arg;
2645 return xb->len > session->se_fchannel.maxreq_sz;
2649 nfsd4_sequence(struct svc_rqst *rqstp,
2650 struct nfsd4_compound_state *cstate,
2651 struct nfsd4_sequence *seq)
2653 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2654 struct xdr_stream *xdr = &resp->xdr;
2655 struct nfsd4_session *session;
2656 struct nfs4_client *clp;
2657 struct nfsd4_slot *slot;
2658 struct nfsd4_conn *conn;
2661 struct net *net = SVC_NET(rqstp);
2662 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2664 if (resp->opcnt != 1)
2665 return nfserr_sequence_pos;
2668 * Will be either used or freed by nfsd4_sequence_check_conn
2671 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2673 return nfserr_jukebox;
2675 spin_lock(&nn->client_lock);
2676 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2678 goto out_no_session;
2679 clp = session->se_client;
2681 status = nfserr_too_many_ops;
2682 if (nfsd4_session_too_many_ops(rqstp, session))
2683 goto out_put_session;
2685 status = nfserr_req_too_big;
2686 if (nfsd4_request_too_big(rqstp, session))
2687 goto out_put_session;
2689 status = nfserr_badslot;
2690 if (seq->slotid >= session->se_fchannel.maxreqs)
2691 goto out_put_session;
2693 slot = session->se_slots[seq->slotid];
2694 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2696 /* We do not negotiate the number of slots yet, so set the
2697 * maxslots to the session maxreqs which is used to encode
2698 * sr_highest_slotid and the sr_target_slot id to maxslots */
2699 seq->maxslots = session->se_fchannel.maxreqs;
2701 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2702 slot->sl_flags & NFSD4_SLOT_INUSE);
2703 if (status == nfserr_replay_cache) {
2704 status = nfserr_seq_misordered;
2705 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2706 goto out_put_session;
2707 cstate->slot = slot;
2708 cstate->session = session;
2710 /* Return the cached reply status and set cstate->status
2711 * for nfsd4_proc_compound processing */
2712 status = nfsd4_replay_cache_entry(resp, seq);
2713 cstate->status = nfserr_replay_cache;
2717 goto out_put_session;
2719 status = nfsd4_sequence_check_conn(conn, session);
2722 goto out_put_session;
2724 buflen = (seq->cachethis) ?
2725 session->se_fchannel.maxresp_cached :
2726 session->se_fchannel.maxresp_sz;
2727 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2729 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2730 goto out_put_session;
2731 svc_reserve(rqstp, buflen);
2734 /* Success! bump slot seqid */
2735 slot->sl_seqid = seq->seqid;
2736 slot->sl_flags |= NFSD4_SLOT_INUSE;
2738 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2740 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2742 cstate->slot = slot;
2743 cstate->session = session;
2747 switch (clp->cl_cb_state) {
2749 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2751 case NFSD4_CB_FAULT:
2752 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2755 seq->status_flags = 0;
2757 if (!list_empty(&clp->cl_revoked))
2758 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2762 spin_unlock(&nn->client_lock);
2765 nfsd4_put_session_locked(session);
2766 goto out_no_session;
2770 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2772 struct nfsd4_compound_state *cs = &resp->cstate;
2774 if (nfsd4_has_session(cs)) {
2775 if (cs->status != nfserr_replay_cache) {
2776 nfsd4_store_cache_entry(resp);
2777 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2779 /* Drop session reference that was taken in nfsd4_sequence() */
2780 nfsd4_put_session(cs->session);
2782 put_client_renew(cs->clp);
2786 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2788 struct nfs4_client *conf, *unconf, *clp;
2790 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2793 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2794 conf = find_confirmed_client(&dc->clientid, true, nn);
2795 WARN_ON_ONCE(conf && unconf);
2800 if (client_has_state(conf)) {
2801 status = nfserr_clientid_busy;
2807 status = nfserr_stale_clientid;
2810 if (!mach_creds_match(clp, rqstp)) {
2811 status = nfserr_wrong_cred;
2816 nfs4_unlock_state();
2821 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2825 if (rc->rca_one_fs) {
2826 if (!cstate->current_fh.fh_dentry)
2827 return nfserr_nofilehandle;
2829 * We don't take advantage of the rca_one_fs case.
2830 * That's OK, it's optional, we can safely ignore it.
2836 status = nfserr_complete_already;
2837 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2838 &cstate->session->se_client->cl_flags))
2841 status = nfserr_stale_clientid;
2842 if (is_client_expired(cstate->session->se_client))
2844 * The following error isn't really legal.
2845 * But we only get here if the client just explicitly
2846 * destroyed the client. Surely it no longer cares what
2847 * error it gets back on an operation for the dead
2853 nfsd4_client_record_create(cstate->session->se_client);
2855 nfs4_unlock_state();
2860 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2861 struct nfsd4_setclientid *setclid)
2863 struct xdr_netobj clname = setclid->se_name;
2864 nfs4_verifier clverifier = setclid->se_verf;
2865 struct nfs4_client *conf, *unconf, *new;
2867 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2869 /* Cases below refer to rfc 3530 section 14.2.33: */
2871 conf = find_confirmed_client_by_name(&clname, nn);
2874 status = nfserr_clid_inuse;
2875 if (clp_used_exchangeid(conf))
2877 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2878 char addr_str[INET6_ADDRSTRLEN];
2879 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2881 dprintk("NFSD: setclientid: string in use by client "
2882 "at %s\n", addr_str);
2886 unconf = find_unconfirmed_client_by_name(&clname, nn);
2888 expire_client(unconf);
2889 status = nfserr_jukebox;
2890 new = create_client(clname, rqstp, &clverifier);
2893 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2894 /* case 1: probable callback update */
2895 copy_clid(new, conf);
2896 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2898 new->cl_minorversion = 0;
2899 gen_callback(new, setclid, rqstp);
2900 add_to_unconfirmed(new);
2901 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2902 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2903 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2906 nfs4_unlock_state();
2912 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2913 struct nfsd4_compound_state *cstate,
2914 struct nfsd4_setclientid_confirm *setclientid_confirm)
2916 struct nfs4_client *conf, *unconf;
2917 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2918 clientid_t * clid = &setclientid_confirm->sc_clientid;
2920 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2922 if (STALE_CLIENTID(clid, nn))
2923 return nfserr_stale_clientid;
2926 conf = find_confirmed_client(clid, false, nn);
2927 unconf = find_unconfirmed_client(clid, false, nn);
2929 * We try hard to give out unique clientid's, so if we get an
2930 * attempt to confirm the same clientid with a different cred,
2931 * there's a bug somewhere. Let's charitably assume it's our
2934 status = nfserr_serverfault;
2935 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2937 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2939 /* cases below refer to rfc 3530 section 14.2.34: */
2940 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2941 if (conf && !unconf) /* case 2: probable retransmit */
2943 else /* case 4: client hasn't noticed we rebooted yet? */
2944 status = nfserr_stale_clientid;
2948 if (conf) { /* case 1: callback update */
2949 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2950 nfsd4_probe_callback(conf);
2951 expire_client(unconf);
2952 } else { /* case 3: normal case; new or rebooted client */
2953 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2955 status = mark_client_expired(conf);
2958 expire_client(conf);
2960 move_to_confirmed(unconf);
2961 nfsd4_probe_callback(unconf);
2964 nfs4_unlock_state();
2968 static struct nfs4_file *nfsd4_alloc_file(void)
2970 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2973 /* OPEN Share state helper functions */
2974 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
2976 unsigned int hashval = file_hashval(fh);
2978 lockdep_assert_held(&state_lock);
2980 atomic_set(&fp->fi_ref, 1);
2981 spin_lock_init(&fp->fi_lock);
2982 INIT_LIST_HEAD(&fp->fi_stateids);
2983 INIT_LIST_HEAD(&fp->fi_delegations);
2984 fh_copy_shallow(&fp->fi_fhandle, fh);
2985 fp->fi_had_conflict = false;
2986 fp->fi_lease = NULL;
2987 fp->fi_share_deny = 0;
2988 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2989 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2990 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2994 nfsd4_free_slabs(void)
2996 kmem_cache_destroy(openowner_slab);
2997 kmem_cache_destroy(lockowner_slab);
2998 kmem_cache_destroy(file_slab);
2999 kmem_cache_destroy(stateid_slab);
3000 kmem_cache_destroy(deleg_slab);
3004 nfsd4_init_slabs(void)
3006 openowner_slab = kmem_cache_create("nfsd4_openowners",
3007 sizeof(struct nfs4_openowner), 0, 0, NULL);
3008 if (openowner_slab == NULL)
3010 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3011 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3012 if (lockowner_slab == NULL)
3013 goto out_free_openowner_slab;
3014 file_slab = kmem_cache_create("nfsd4_files",
3015 sizeof(struct nfs4_file), 0, 0, NULL);
3016 if (file_slab == NULL)
3017 goto out_free_lockowner_slab;
3018 stateid_slab = kmem_cache_create("nfsd4_stateids",
3019 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3020 if (stateid_slab == NULL)
3021 goto out_free_file_slab;
3022 deleg_slab = kmem_cache_create("nfsd4_delegations",
3023 sizeof(struct nfs4_delegation), 0, 0, NULL);
3024 if (deleg_slab == NULL)
3025 goto out_free_stateid_slab;
3028 out_free_stateid_slab:
3029 kmem_cache_destroy(stateid_slab);
3031 kmem_cache_destroy(file_slab);
3032 out_free_lockowner_slab:
3033 kmem_cache_destroy(lockowner_slab);
3034 out_free_openowner_slab:
3035 kmem_cache_destroy(openowner_slab);
3037 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3041 static void init_nfs4_replay(struct nfs4_replay *rp)
3043 rp->rp_status = nfserr_serverfault;
3045 rp->rp_buf = rp->rp_ibuf;
3046 mutex_init(&rp->rp_mutex);
3049 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3050 struct nfs4_stateowner *so)
3052 if (!nfsd4_has_session(cstate)) {
3053 mutex_lock(&so->so_replay.rp_mutex);
3054 cstate->replay_owner = so;
3055 atomic_inc(&so->so_count);
3059 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3061 struct nfs4_stateowner *so = cstate->replay_owner;
3064 cstate->replay_owner = NULL;
3065 mutex_unlock(&so->so_replay.rp_mutex);
3066 nfs4_put_stateowner(so);
3070 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3072 struct nfs4_stateowner *sop;
3074 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3078 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3079 if (!sop->so_owner.data) {
3080 kmem_cache_free(slab, sop);
3083 sop->so_owner.len = owner->len;
3085 INIT_LIST_HEAD(&sop->so_stateids);
3086 sop->so_client = clp;
3087 init_nfs4_replay(&sop->so_replay);
3088 atomic_set(&sop->so_count, 1);
3092 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3094 lockdep_assert_held(&clp->cl_lock);
3096 list_add(&oo->oo_owner.so_strhash,
3097 &clp->cl_ownerstr_hashtbl[strhashval]);
3098 list_add(&oo->oo_perclient, &clp->cl_openowners);
3101 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3103 unhash_openowner_locked(openowner(so));
3106 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3108 struct nfs4_openowner *oo = openowner(so);
3110 kmem_cache_free(openowner_slab, oo);
3113 static const struct nfs4_stateowner_operations openowner_ops = {
3114 .so_unhash = nfs4_unhash_openowner,
3115 .so_free = nfs4_free_openowner,
3118 static struct nfs4_openowner *
3119 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3120 struct nfsd4_compound_state *cstate)
3122 struct nfs4_client *clp = cstate->clp;
3123 struct nfs4_openowner *oo, *ret;
3125 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3128 oo->oo_owner.so_ops = &openowner_ops;
3129 oo->oo_owner.so_is_open_owner = 1;
3130 oo->oo_owner.so_seqid = open->op_seqid;
3132 if (nfsd4_has_session(cstate))
3133 oo->oo_flags |= NFS4_OO_CONFIRMED;
3135 oo->oo_last_closed_stid = NULL;
3136 INIT_LIST_HEAD(&oo->oo_close_lru);
3137 spin_lock(&clp->cl_lock);
3138 ret = find_openstateowner_str_locked(strhashval, open, clp);
3140 hash_openowner(oo, clp, strhashval);
3143 nfs4_free_openowner(&oo->oo_owner);
3144 spin_unlock(&clp->cl_lock);
3148 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3149 struct nfs4_openowner *oo = open->op_openowner;
3151 atomic_inc(&stp->st_stid.sc_count);
3152 stp->st_stid.sc_type = NFS4_OPEN_STID;
3153 INIT_LIST_HEAD(&stp->st_locks);
3154 stp->st_stateowner = &oo->oo_owner;
3155 atomic_inc(&stp->st_stateowner->so_count);
3157 stp->st_stid.sc_file = fp;
3158 stp->st_access_bmap = 0;
3159 stp->st_deny_bmap = 0;
3160 stp->st_openstp = NULL;
3161 spin_lock(&oo->oo_owner.so_client->cl_lock);
3162 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3163 spin_lock(&fp->fi_lock);
3164 list_add(&stp->st_perfile, &fp->fi_stateids);
3165 spin_unlock(&fp->fi_lock);
3166 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3170 * In the 4.0 case we need to keep the owners around a little while to handle
3171 * CLOSE replay. We still do need to release any file access that is held by
3172 * them before returning however.
3175 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3177 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3178 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3181 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3184 * We know that we hold one reference via nfsd4_close, and another
3185 * "persistent" reference for the client. If the refcount is higher
3186 * than 2, then there are still calls in progress that are using this
3187 * stateid. We can't put the sc_file reference until they are finished.
3188 * Wait for the refcount to drop to 2. Since it has been unhashed,
3189 * there should be no danger of the refcount going back up again at
3192 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3194 release_all_access(s);
3195 if (s->st_stid.sc_file) {
3196 put_nfs4_file(s->st_stid.sc_file);
3197 s->st_stid.sc_file = NULL;
3199 release_last_closed_stateid(oo);
3200 oo->oo_last_closed_stid = s;
3201 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3202 oo->oo_time = get_seconds();
3205 /* search file_hashtbl[] for file */
3206 static struct nfs4_file *
3207 find_file_locked(struct knfsd_fh *fh)
3209 unsigned int hashval = file_hashval(fh);
3210 struct nfs4_file *fp;
3212 lockdep_assert_held(&state_lock);
3214 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3215 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3223 static struct nfs4_file *
3224 find_file(struct knfsd_fh *fh)
3226 struct nfs4_file *fp;
3228 spin_lock(&state_lock);
3229 fp = find_file_locked(fh);
3230 spin_unlock(&state_lock);
3234 static struct nfs4_file *
3235 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3237 struct nfs4_file *fp;
3239 spin_lock(&state_lock);
3240 fp = find_file_locked(fh);
3242 nfsd4_init_file(new, fh);
3245 spin_unlock(&state_lock);
3251 * Called to check deny when READ with all zero stateid or
3252 * WRITE with all zero or all one stateid
3255 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3257 struct nfs4_file *fp;
3258 __be32 ret = nfs_ok;
3260 fp = find_file(¤t_fh->fh_handle);
3263 /* Check for conflicting share reservations */
3264 spin_lock(&fp->fi_lock);
3265 if (fp->fi_share_deny & deny_type)
3266 ret = nfserr_locked;
3267 spin_unlock(&fp->fi_lock);
3272 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3274 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3277 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3280 * We can't do this in nfsd_break_deleg_cb because it is
3281 * already holding inode->i_lock.
3283 * If the dl_time != 0, then we know that it has already been
3284 * queued for a lease break. Don't queue it again.
3286 spin_lock(&state_lock);
3287 if (dp->dl_time == 0) {
3288 dp->dl_time = get_seconds();
3289 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3291 spin_unlock(&state_lock);
3294 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3297 * We're assuming the state code never drops its reference
3298 * without first removing the lease. Since we're in this lease
3299 * callback (and since the lease code is serialized by the kernel
3300 * lock) we know the server hasn't removed the lease yet, we know
3301 * it's safe to take a reference.
3303 atomic_inc(&dp->dl_stid.sc_count);
3304 nfsd4_cb_recall(dp);
3307 /* Called from break_lease() with i_lock held. */
3308 static void nfsd_break_deleg_cb(struct file_lock *fl)
3310 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3311 struct nfs4_delegation *dp;
3314 WARN(1, "(%p)->fl_owner NULL\n", fl);
3317 if (fp->fi_had_conflict) {
3318 WARN(1, "duplicate break on %p\n", fp);
3322 * We don't want the locks code to timeout the lease for us;
3323 * we'll remove it ourself if a delegation isn't returned
3326 fl->fl_break_time = 0;
3328 spin_lock(&fp->fi_lock);
3329 fp->fi_had_conflict = true;
3331 * If there are no delegations on the list, then we can't count on this
3332 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3333 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3334 * true should keep any new delegations from being hashed.
3336 if (list_empty(&fp->fi_delegations))
3337 fl->fl_break_time = jiffies;
3339 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3340 nfsd_break_one_deleg(dp);
3341 spin_unlock(&fp->fi_lock);
3345 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
3348 return lease_modify(onlist, arg);
3353 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3354 .lm_break = nfsd_break_deleg_cb,
3355 .lm_change = nfsd_change_deleg_cb,
3358 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3360 if (nfsd4_has_session(cstate))
3362 if (seqid == so->so_seqid - 1)
3363 return nfserr_replay_me;
3364 if (seqid == so->so_seqid)
3366 return nfserr_bad_seqid;
3369 static __be32 lookup_clientid(clientid_t *clid,
3370 struct nfsd4_compound_state *cstate,
3371 struct nfsd_net *nn)
3373 struct nfs4_client *found;
3376 found = cstate->clp;
3377 if (!same_clid(&found->cl_clientid, clid))
3378 return nfserr_stale_clientid;
3382 if (STALE_CLIENTID(clid, nn))
3383 return nfserr_stale_clientid;
3386 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3387 * cached already then we know this is for is for v4.0 and "sessions"
3390 WARN_ON_ONCE(cstate->session);
3391 found = find_confirmed_client(clid, false, nn);
3393 return nfserr_expired;
3395 /* Cache the nfs4_client in cstate! */
3396 cstate->clp = found;
3397 atomic_inc(&found->cl_refcount);
3402 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3403 struct nfsd4_open *open, struct nfsd_net *nn)
3405 clientid_t *clientid = &open->op_clientid;
3406 struct nfs4_client *clp = NULL;
3407 unsigned int strhashval;
3408 struct nfs4_openowner *oo = NULL;
3411 if (STALE_CLIENTID(&open->op_clientid, nn))
3412 return nfserr_stale_clientid;
3414 * In case we need it later, after we've already created the
3415 * file and don't want to risk a further failure:
3417 open->op_file = nfsd4_alloc_file();
3418 if (open->op_file == NULL)
3419 return nfserr_jukebox;
3421 status = lookup_clientid(clientid, cstate, nn);
3426 strhashval = ownerstr_hashval(&open->op_owner);
3427 oo = find_openstateowner_str(strhashval, open, clp);
3428 open->op_openowner = oo;
3432 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3433 /* Replace unconfirmed owners without checking for replay. */
3434 release_openowner(oo);
3435 open->op_openowner = NULL;
3438 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3443 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3445 return nfserr_jukebox;
3446 open->op_openowner = oo;
3448 open->op_stp = nfs4_alloc_open_stateid(clp);
3450 return nfserr_jukebox;
3454 static inline __be32
3455 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3457 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3458 return nfserr_openmode;
3463 static int share_access_to_flags(u32 share_access)
3465 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3468 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3470 struct nfs4_stid *ret;
3472 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3475 return delegstateid(ret);
3478 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3480 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3481 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3485 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3486 struct nfs4_delegation **dp)
3489 __be32 status = nfserr_bad_stateid;
3490 struct nfs4_delegation *deleg;
3492 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3495 flags = share_access_to_flags(open->op_share_access);
3496 status = nfs4_check_delegmode(deleg, flags);
3498 nfs4_put_stid(&deleg->dl_stid);
3503 if (!nfsd4_is_deleg_cur(open))
3507 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3511 static struct nfs4_ol_stateid *
3512 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3514 struct nfs4_ol_stateid *local, *ret = NULL;
3515 struct nfs4_openowner *oo = open->op_openowner;
3517 spin_lock(&fp->fi_lock);
3518 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3519 /* ignore lock owners */
3520 if (local->st_stateowner->so_is_open_owner == 0)
3522 if (local->st_stateowner == &oo->oo_owner) {
3524 atomic_inc(&ret->st_stid.sc_count);
3528 spin_unlock(&fp->fi_lock);
3532 static inline int nfs4_access_to_access(u32 nfs4_access)
3536 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3537 flags |= NFSD_MAY_READ;
3538 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3539 flags |= NFSD_MAY_WRITE;
3543 static inline __be32
3544 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3545 struct nfsd4_open *open)
3547 struct iattr iattr = {
3548 .ia_valid = ATTR_SIZE,
3551 if (!open->op_truncate)
3553 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3554 return nfserr_inval;
3555 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3558 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3559 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3560 struct nfsd4_open *open)
3562 struct file *filp = NULL;
3564 int oflag = nfs4_access_to_omode(open->op_share_access);
3565 int access = nfs4_access_to_access(open->op_share_access);
3566 unsigned char old_access_bmap, old_deny_bmap;
3568 spin_lock(&fp->fi_lock);
3571 * Are we trying to set a deny mode that would conflict with
3574 status = nfs4_file_check_deny(fp, open->op_share_deny);
3575 if (status != nfs_ok) {
3576 spin_unlock(&fp->fi_lock);
3580 /* set access to the file */
3581 status = nfs4_file_get_access(fp, open->op_share_access);
3582 if (status != nfs_ok) {
3583 spin_unlock(&fp->fi_lock);
3587 /* Set access bits in stateid */
3588 old_access_bmap = stp->st_access_bmap;
3589 set_access(open->op_share_access, stp);
3591 /* Set new deny mask */
3592 old_deny_bmap = stp->st_deny_bmap;
3593 set_deny(open->op_share_deny, stp);
3594 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3596 if (!fp->fi_fds[oflag]) {
3597 spin_unlock(&fp->fi_lock);
3598 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3600 goto out_put_access;
3601 spin_lock(&fp->fi_lock);
3602 if (!fp->fi_fds[oflag]) {
3603 fp->fi_fds[oflag] = filp;
3607 spin_unlock(&fp->fi_lock);
3611 status = nfsd4_truncate(rqstp, cur_fh, open);
3613 goto out_put_access;
3617 stp->st_access_bmap = old_access_bmap;
3618 nfs4_file_put_access(fp, open->op_share_access);
3619 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3624 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)
3627 unsigned char old_deny_bmap;
3629 if (!test_access(open->op_share_access, stp))
3630 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3632 /* test and set deny mode */
3633 spin_lock(&fp->fi_lock);
3634 status = nfs4_file_check_deny(fp, open->op_share_deny);
3635 if (status == nfs_ok) {
3636 old_deny_bmap = stp->st_deny_bmap;
3637 set_deny(open->op_share_deny, stp);
3638 fp->fi_share_deny |=
3639 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3641 spin_unlock(&fp->fi_lock);
3643 if (status != nfs_ok)
3646 status = nfsd4_truncate(rqstp, cur_fh, open);
3647 if (status != nfs_ok)
3648 reset_union_bmap_deny(old_deny_bmap, stp);
3653 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3655 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3658 /* Should we give out recallable state?: */
3659 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3661 if (clp->cl_cb_state == NFSD4_CB_UP)
3664 * In the sessions case, since we don't have to establish a
3665 * separate connection for callbacks, we assume it's OK
3666 * until we hear otherwise:
3668 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3671 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3673 struct file_lock *fl;
3675 fl = locks_alloc_lock();
3678 locks_init_lock(fl);
3679 fl->fl_lmops = &nfsd_lease_mng_ops;
3680 fl->fl_flags = FL_DELEG;
3681 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3682 fl->fl_end = OFFSET_MAX;
3683 fl->fl_owner = (fl_owner_t)fp;
3684 fl->fl_pid = current->tgid;
3688 static int nfs4_setlease(struct nfs4_delegation *dp)
3690 struct nfs4_file *fp = dp->dl_stid.sc_file;
3691 struct file_lock *fl;
3695 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3698 filp = find_readable_file(fp);
3700 /* We should always have a readable file here */
3705 status = vfs_setlease(filp, fl->fl_type, &fl);
3707 locks_free_lock(fl);
3710 spin_lock(&state_lock);
3711 spin_lock(&fp->fi_lock);
3712 /* Did the lease get broken before we took the lock? */
3714 if (fp->fi_had_conflict)
3719 atomic_inc(&fp->fi_delegees);
3720 hash_delegation_locked(dp, fp);
3724 fp->fi_deleg_file = filp;
3725 atomic_set(&fp->fi_delegees, 1);
3726 hash_delegation_locked(dp, fp);
3727 spin_unlock(&fp->fi_lock);
3728 spin_unlock(&state_lock);
3731 spin_unlock(&fp->fi_lock);
3732 spin_unlock(&state_lock);
3738 static struct nfs4_delegation *
3739 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3740 struct nfs4_file *fp)
3743 struct nfs4_delegation *dp;
3745 if (fp->fi_had_conflict)
3746 return ERR_PTR(-EAGAIN);
3748 dp = alloc_init_deleg(clp, fh);
3750 return ERR_PTR(-ENOMEM);
3753 spin_lock(&state_lock);
3754 spin_lock(&fp->fi_lock);
3755 dp->dl_stid.sc_file = fp;
3756 if (!fp->fi_lease) {
3757 spin_unlock(&fp->fi_lock);
3758 spin_unlock(&state_lock);
3759 status = nfs4_setlease(dp);
3762 atomic_inc(&fp->fi_delegees);
3763 if (fp->fi_had_conflict) {
3767 hash_delegation_locked(dp, fp);
3770 spin_unlock(&fp->fi_lock);
3771 spin_unlock(&state_lock);
3774 nfs4_put_stid(&dp->dl_stid);
3775 return ERR_PTR(status);
3780 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3782 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3783 if (status == -EAGAIN)
3784 open->op_why_no_deleg = WND4_CONTENTION;
3786 open->op_why_no_deleg = WND4_RESOURCE;
3787 switch (open->op_deleg_want) {
3788 case NFS4_SHARE_WANT_READ_DELEG:
3789 case NFS4_SHARE_WANT_WRITE_DELEG:
3790 case NFS4_SHARE_WANT_ANY_DELEG:
3792 case NFS4_SHARE_WANT_CANCEL:
3793 open->op_why_no_deleg = WND4_CANCELLED;
3795 case NFS4_SHARE_WANT_NO_DELEG:
3802 * Attempt to hand out a delegation.
3804 * Note we don't support write delegations, and won't until the vfs has
3805 * proper support for them.
3808 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3809 struct nfs4_ol_stateid *stp)
3811 struct nfs4_delegation *dp;
3812 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3813 struct nfs4_client *clp = stp->st_stid.sc_client;
3817 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3818 open->op_recall = 0;
3819 switch (open->op_claim_type) {
3820 case NFS4_OPEN_CLAIM_PREVIOUS:
3822 open->op_recall = 1;
3823 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3826 case NFS4_OPEN_CLAIM_NULL:
3827 case NFS4_OPEN_CLAIM_FH:
3829 * Let's not give out any delegations till everyone's
3830 * had the chance to reclaim theirs....
3832 if (locks_in_grace(clp->net))
3834 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3837 * Also, if the file was opened for write or
3838 * create, there's a good chance the client's
3839 * about to write to it, resulting in an
3840 * immediate recall (since we don't support
3841 * write delegations):
3843 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3845 if (open->op_create == NFS4_OPEN_CREATE)
3851 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3855 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3857 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3858 STATEID_VAL(&dp->dl_stid.sc_stateid));
3859 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3860 nfs4_put_stid(&dp->dl_stid);
3863 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3864 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3865 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3866 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3867 open->op_recall = 1;
3870 /* 4.1 client asking for a delegation? */
3871 if (open->op_deleg_want)
3872 nfsd4_open_deleg_none_ext(open, status);
3876 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3877 struct nfs4_delegation *dp)
3879 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3880 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3881 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3882 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3883 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3884 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3885 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3886 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3888 /* Otherwise the client must be confused wanting a delegation
3889 * it already has, therefore we don't return
3890 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3895 * called with nfs4_lock_state() held.
3898 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3900 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3901 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3902 struct nfs4_file *fp = NULL;
3903 struct nfs4_ol_stateid *stp = NULL;
3904 struct nfs4_delegation *dp = NULL;
3908 * Lookup file; if found, lookup stateid and check open request,
3909 * and check for delegations in the process of being recalled.
3910 * If not found, create the nfs4_file struct
3912 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3913 if (fp != open->op_file) {
3914 status = nfs4_check_deleg(cl, open, &dp);
3917 stp = nfsd4_find_existing_open(fp, open);
3919 open->op_file = NULL;
3920 status = nfserr_bad_stateid;
3921 if (nfsd4_is_deleg_cur(open))
3923 status = nfserr_jukebox;
3927 * OPEN the file, or upgrade an existing OPEN.
3928 * If truncate fails, the OPEN fails.
3931 /* Stateid was found, this is an OPEN upgrade */
3932 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3937 open->op_stp = NULL;
3938 init_open_stateid(stp, fp, open);
3939 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
3941 release_open_stateid(stp);
3945 update_stateid(&stp->st_stid.sc_stateid);
3946 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3948 if (nfsd4_has_session(&resp->cstate)) {
3949 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3950 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3951 open->op_why_no_deleg = WND4_NOT_WANTED;
3957 * Attempt to hand out a delegation. No error return, because the
3958 * OPEN succeeds even if we fail.
3960 nfs4_open_delegation(current_fh, open, stp);
3964 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3965 STATEID_VAL(&stp->st_stid.sc_stateid));
3967 /* 4.1 client trying to upgrade/downgrade delegation? */
3968 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3969 open->op_deleg_want)
3970 nfsd4_deleg_xgrade_none_ext(open, dp);
3974 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3975 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3977 * To finish the open response, we just need to set the rflags.
3979 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3980 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3981 !nfsd4_has_session(&resp->cstate))
3982 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3984 nfs4_put_stid(&dp->dl_stid);
3986 nfs4_put_stid(&stp->st_stid);
3991 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
3992 struct nfsd4_open *open, __be32 status)
3994 if (open->op_openowner) {
3995 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
3997 nfsd4_cstate_assign_replay(cstate, so);
3998 nfs4_put_stateowner(so);
4001 nfsd4_free_file(open->op_file);
4003 nfs4_put_stid(&open->op_stp->st_stid);
4007 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4010 struct nfs4_client *clp;
4012 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4015 dprintk("process_renew(%08x/%08x): starting\n",
4016 clid->cl_boot, clid->cl_id);
4017 status = lookup_clientid(clid, cstate, nn);
4021 status = nfserr_cb_path_down;
4022 if (!list_empty(&clp->cl_delegations)
4023 && clp->cl_cb_state != NFSD4_CB_UP)
4027 nfs4_unlock_state();
4032 nfsd4_end_grace(struct nfsd_net *nn)
4034 /* do nothing if grace period already ended */
4035 if (nn->grace_ended)
4038 dprintk("NFSD: end of grace period\n");
4039 nn->grace_ended = true;
4040 nfsd4_record_grace_done(nn, nn->boot_time);
4041 locks_end_grace(&nn->nfsd4_manager);
4043 * Now that every NFSv4 client has had the chance to recover and
4044 * to see the (possibly new, possibly shorter) lease time, we
4045 * can safely set the next grace time to the current lease time:
4047 nn->nfsd4_grace = nn->nfsd4_lease;
4051 nfs4_laundromat(struct nfsd_net *nn)
4053 struct nfs4_client *clp;
4054 struct nfs4_openowner *oo;
4055 struct nfs4_delegation *dp;
4056 struct list_head *pos, *next, reaplist;
4057 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4058 time_t t, new_timeo = nn->nfsd4_lease;
4062 dprintk("NFSD: laundromat service - starting\n");
4063 nfsd4_end_grace(nn);
4064 INIT_LIST_HEAD(&reaplist);
4065 spin_lock(&nn->client_lock);
4066 list_for_each_safe(pos, next, &nn->client_lru) {
4067 clp = list_entry(pos, struct nfs4_client, cl_lru);
4068 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4069 t = clp->cl_time - cutoff;
4070 new_timeo = min(new_timeo, t);
4073 if (mark_client_expired_locked(clp)) {
4074 dprintk("NFSD: client in use (clientid %08x)\n",
4075 clp->cl_clientid.cl_id);
4078 list_move(&clp->cl_lru, &reaplist);
4080 spin_unlock(&nn->client_lock);
4081 list_for_each_safe(pos, next, &reaplist) {
4082 clp = list_entry(pos, struct nfs4_client, cl_lru);
4083 dprintk("NFSD: purging unused client (clientid %08x)\n",
4084 clp->cl_clientid.cl_id);
4087 spin_lock(&state_lock);
4088 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4089 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4090 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4092 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4093 t = dp->dl_time - cutoff;
4094 new_timeo = min(new_timeo, t);
4097 unhash_delegation_locked(dp);
4098 list_add(&dp->dl_recall_lru, &reaplist);
4100 spin_unlock(&state_lock);
4101 while (!list_empty(&reaplist)) {
4102 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4104 list_del_init(&dp->dl_recall_lru);
4105 revoke_delegation(dp);
4107 list_for_each_safe(pos, next, &nn->close_lru) {
4108 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
4109 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
4110 t = oo->oo_time - cutoff;
4111 new_timeo = min(new_timeo, t);
4114 release_last_closed_stateid(oo);
4116 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4117 nfs4_unlock_state();
4121 static struct workqueue_struct *laundry_wq;
4122 static void laundromat_main(struct work_struct *);
4125 laundromat_main(struct work_struct *laundry)
4128 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4130 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4133 t = nfs4_laundromat(nn);
4134 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4135 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4138 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4140 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4141 return nfserr_bad_stateid;
4146 access_permit_read(struct nfs4_ol_stateid *stp)
4148 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4149 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4150 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4154 access_permit_write(struct nfs4_ol_stateid *stp)
4156 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4157 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4161 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4163 __be32 status = nfserr_openmode;
4165 /* For lock stateid's, we test the parent open, not the lock: */
4166 if (stp->st_openstp)
4167 stp = stp->st_openstp;
4168 if ((flags & WR_STATE) && !access_permit_write(stp))
4170 if ((flags & RD_STATE) && !access_permit_read(stp))
4177 static inline __be32
4178 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4180 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4182 else if (locks_in_grace(net)) {
4183 /* Answer in remaining cases depends on existence of
4184 * conflicting state; so we must wait out the grace period. */
4185 return nfserr_grace;
4186 } else if (flags & WR_STATE)
4187 return nfs4_share_conflict(current_fh,
4188 NFS4_SHARE_DENY_WRITE);
4189 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4190 return nfs4_share_conflict(current_fh,
4191 NFS4_SHARE_DENY_READ);
4195 * Allow READ/WRITE during grace period on recovered state only for files
4196 * that are not able to provide mandatory locking.
4199 grace_disallows_io(struct net *net, struct inode *inode)
4201 return locks_in_grace(net) && mandatory_lock(inode);
4204 /* Returns true iff a is later than b: */
4205 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4207 return (s32)(a->si_generation - b->si_generation) > 0;
4210 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4213 * When sessions are used the stateid generation number is ignored
4216 if (has_session && in->si_generation == 0)
4219 if (in->si_generation == ref->si_generation)
4222 /* If the client sends us a stateid from the future, it's buggy: */
4223 if (stateid_generation_after(in, ref))
4224 return nfserr_bad_stateid;
4226 * However, we could see a stateid from the past, even from a
4227 * non-buggy client. For example, if the client sends a lock
4228 * while some IO is outstanding, the lock may bump si_generation
4229 * while the IO is still in flight. The client could avoid that
4230 * situation by waiting for responses on all the IO requests,
4231 * but better performance may result in retrying IO that
4232 * receives an old_stateid error if requests are rarely
4233 * reordered in flight:
4235 return nfserr_old_stateid;
4238 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4240 struct nfs4_stid *s;
4241 struct nfs4_ol_stateid *ols;
4242 __be32 status = nfserr_bad_stateid;
4244 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4246 /* Client debugging aid. */
4247 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4248 char addr_str[INET6_ADDRSTRLEN];
4249 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4251 pr_warn_ratelimited("NFSD: client %s testing state ID "
4252 "with incorrect client ID\n", addr_str);
4255 spin_lock(&cl->cl_lock);
4256 s = find_stateid_locked(cl, stateid);
4259 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4262 switch (s->sc_type) {
4263 case NFS4_DELEG_STID:
4266 case NFS4_REVOKED_DELEG_STID:
4267 status = nfserr_deleg_revoked;
4269 case NFS4_OPEN_STID:
4270 case NFS4_LOCK_STID:
4271 ols = openlockstateid(s);
4272 if (ols->st_stateowner->so_is_open_owner
4273 && !(openowner(ols->st_stateowner)->oo_flags
4274 & NFS4_OO_CONFIRMED))
4275 status = nfserr_bad_stateid;
4280 printk("unknown stateid type %x\n", s->sc_type);
4282 case NFS4_CLOSED_STID:
4283 case NFS4_CLOSED_DELEG_STID:
4284 status = nfserr_bad_stateid;
4287 spin_unlock(&cl->cl_lock);
4292 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4293 stateid_t *stateid, unsigned char typemask,
4294 struct nfs4_stid **s, struct nfsd_net *nn)
4298 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4299 return nfserr_bad_stateid;
4300 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4301 if (status == nfserr_stale_clientid) {
4302 if (cstate->session)
4303 return nfserr_bad_stateid;
4304 return nfserr_stale_stateid;
4308 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4310 return nfserr_bad_stateid;
4315 * Checks for stateid operations
4318 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4319 stateid_t *stateid, int flags, struct file **filpp)
4321 struct nfs4_stid *s;
4322 struct nfs4_ol_stateid *stp = NULL;
4323 struct nfs4_delegation *dp = NULL;
4324 struct svc_fh *current_fh = &cstate->current_fh;
4325 struct inode *ino = current_fh->fh_dentry->d_inode;
4326 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4327 struct file *file = NULL;
4333 if (grace_disallows_io(net, ino))
4334 return nfserr_grace;
4336 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4337 return check_special_stateids(net, current_fh, stateid, flags);
4341 status = nfsd4_lookup_stateid(cstate, stateid,
4342 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4346 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4349 switch (s->sc_type) {
4350 case NFS4_DELEG_STID:
4351 dp = delegstateid(s);
4352 status = nfs4_check_delegmode(dp, flags);
4356 file = dp->dl_stid.sc_file->fi_deleg_file;
4359 status = nfserr_serverfault;
4365 case NFS4_OPEN_STID:
4366 case NFS4_LOCK_STID:
4367 stp = openlockstateid(s);
4368 status = nfs4_check_fh(current_fh, stp);
4371 if (stp->st_stateowner->so_is_open_owner
4372 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4374 status = nfs4_check_openmode(stp, flags);
4378 struct nfs4_file *fp = stp->st_stid.sc_file;
4380 if (flags & RD_STATE)
4381 file = find_readable_file(fp);
4383 file = find_writeable_file(fp);
4387 status = nfserr_bad_stateid;
4396 nfs4_unlock_state();
4401 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
4403 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
4405 if (check_for_locks(stp->st_stid.sc_file, lo))
4406 return nfserr_locks_held;
4407 release_lock_stateid(stp);
4412 * Test if the stateid is valid
4415 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4416 struct nfsd4_test_stateid *test_stateid)
4418 struct nfsd4_test_stateid_id *stateid;
4419 struct nfs4_client *cl = cstate->session->se_client;
4422 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4423 stateid->ts_id_status =
4424 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4425 nfs4_unlock_state();
4431 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4432 struct nfsd4_free_stateid *free_stateid)
4434 stateid_t *stateid = &free_stateid->fr_stateid;
4435 struct nfs4_stid *s;
4436 struct nfs4_delegation *dp;
4437 struct nfs4_client *cl = cstate->session->se_client;
4438 __be32 ret = nfserr_bad_stateid;
4441 spin_lock(&cl->cl_lock);
4442 s = find_stateid_locked(cl, stateid);
4445 switch (s->sc_type) {
4446 case NFS4_DELEG_STID:
4447 ret = nfserr_locks_held;
4449 case NFS4_OPEN_STID:
4450 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4453 ret = nfserr_locks_held;
4455 case NFS4_LOCK_STID:
4456 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4459 spin_unlock(&cl->cl_lock);
4460 ret = nfsd4_free_lock_stateid(openlockstateid(s));
4462 case NFS4_REVOKED_DELEG_STID:
4463 dp = delegstateid(s);
4464 list_del_init(&dp->dl_recall_lru);
4465 spin_unlock(&cl->cl_lock);
4469 /* Default falls through and returns nfserr_bad_stateid */
4472 spin_unlock(&cl->cl_lock);
4474 nfs4_unlock_state();
4481 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4482 RD_STATE : WR_STATE;
4485 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4487 struct svc_fh *current_fh = &cstate->current_fh;
4488 struct nfs4_stateowner *sop = stp->st_stateowner;
4491 status = nfsd4_check_seqid(cstate, sop, seqid);
4494 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4495 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4497 * "Closed" stateid's exist *only* to return
4498 * nfserr_replay_me from the previous step, and
4499 * revoked delegations are kept only for free_stateid.
4501 return nfserr_bad_stateid;
4502 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4505 return nfs4_check_fh(current_fh, stp);
4509 * Checks for sequence id mutating operations.
4512 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4513 stateid_t *stateid, char typemask,
4514 struct nfs4_ol_stateid **stpp,
4515 struct nfsd_net *nn)
4518 struct nfs4_stid *s;
4519 struct nfs4_ol_stateid *stp = NULL;
4521 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4522 seqid, STATEID_VAL(stateid));
4525 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4528 stp = openlockstateid(s);
4529 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4531 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4535 nfs4_put_stid(&stp->st_stid);
4539 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4540 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4543 struct nfs4_openowner *oo;
4544 struct nfs4_ol_stateid *stp;
4546 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4547 NFS4_OPEN_STID, &stp, nn);
4550 oo = openowner(stp->st_stateowner);
4551 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4552 nfs4_put_stid(&stp->st_stid);
4553 return nfserr_bad_stateid;
4560 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4561 struct nfsd4_open_confirm *oc)
4564 struct nfs4_openowner *oo;
4565 struct nfs4_ol_stateid *stp;
4566 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4568 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4569 cstate->current_fh.fh_dentry);
4571 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4577 status = nfs4_preprocess_seqid_op(cstate,
4578 oc->oc_seqid, &oc->oc_req_stateid,
4579 NFS4_OPEN_STID, &stp, nn);
4582 oo = openowner(stp->st_stateowner);
4583 status = nfserr_bad_stateid;
4584 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4586 oo->oo_flags |= NFS4_OO_CONFIRMED;
4587 update_stateid(&stp->st_stid.sc_stateid);
4588 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4589 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4590 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4592 nfsd4_client_record_create(oo->oo_owner.so_client);
4595 nfs4_put_stid(&stp->st_stid);
4597 nfsd4_bump_seqid(cstate, status);
4598 nfs4_unlock_state();
4602 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4604 if (!test_access(access, stp))
4606 nfs4_file_put_access(stp->st_stid.sc_file, access);
4607 clear_access(access, stp);
4610 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4612 switch (to_access) {
4613 case NFS4_SHARE_ACCESS_READ:
4614 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4615 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4617 case NFS4_SHARE_ACCESS_WRITE:
4618 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4619 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4621 case NFS4_SHARE_ACCESS_BOTH:
4629 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4630 struct nfsd4_compound_state *cstate,
4631 struct nfsd4_open_downgrade *od)
4634 struct nfs4_ol_stateid *stp;
4635 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4637 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4638 cstate->current_fh.fh_dentry);
4640 /* We don't yet support WANT bits: */
4641 if (od->od_deleg_want)
4642 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4646 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4647 &od->od_stateid, &stp, nn);
4650 status = nfserr_inval;
4651 if (!test_access(od->od_share_access, stp)) {
4652 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4653 stp->st_access_bmap, od->od_share_access);
4656 if (!test_deny(od->od_share_deny, stp)) {
4657 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4658 stp->st_deny_bmap, od->od_share_deny);
4661 nfs4_stateid_downgrade(stp, od->od_share_access);
4663 reset_union_bmap_deny(od->od_share_deny, stp);
4665 update_stateid(&stp->st_stid.sc_stateid);
4666 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4669 nfs4_put_stid(&stp->st_stid);
4671 nfsd4_bump_seqid(cstate, status);
4672 nfs4_unlock_state();
4676 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4678 struct nfs4_client *clp = s->st_stid.sc_client;
4680 s->st_stid.sc_type = NFS4_CLOSED_STID;
4681 unhash_open_stateid(s);
4683 if (clp->cl_minorversion)
4684 nfs4_put_stid(&s->st_stid);
4686 move_to_close_lru(s, clp->net);
4690 * nfs4_unlock_state() called after encode
4693 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4694 struct nfsd4_close *close)
4697 struct nfs4_ol_stateid *stp;
4698 struct net *net = SVC_NET(rqstp);
4699 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4701 dprintk("NFSD: nfsd4_close on file %pd\n",
4702 cstate->current_fh.fh_dentry);
4705 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4707 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4709 nfsd4_bump_seqid(cstate, status);
4712 update_stateid(&stp->st_stid.sc_stateid);
4713 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4715 nfsd4_close_open_stateid(stp);
4717 /* put reference from nfs4_preprocess_seqid_op */
4718 nfs4_put_stid(&stp->st_stid);
4720 nfs4_unlock_state();
4725 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4726 struct nfsd4_delegreturn *dr)
4728 struct nfs4_delegation *dp;
4729 stateid_t *stateid = &dr->dr_stateid;
4730 struct nfs4_stid *s;
4732 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4734 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4738 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4741 dp = delegstateid(s);
4742 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4746 destroy_delegation(dp);
4748 nfs4_put_stid(&dp->dl_stid);
4750 nfs4_unlock_state();
4756 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4759 end_offset(u64 start, u64 len)
4764 return end >= start ? end: NFS4_MAX_UINT64;
4767 /* last octet in a range */
4769 last_byte_offset(u64 start, u64 len)
4775 return end > start ? end - 1: NFS4_MAX_UINT64;
4779 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4780 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4781 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4782 * locking, this prevents us from being completely protocol-compliant. The
4783 * real solution to this problem is to start using unsigned file offsets in
4784 * the VFS, but this is a very deep change!
4787 nfs4_transform_lock_offset(struct file_lock *lock)
4789 if (lock->fl_start < 0)
4790 lock->fl_start = OFFSET_MAX;
4791 if (lock->fl_end < 0)
4792 lock->fl_end = OFFSET_MAX;
4795 /* Hack!: For now, we're defining this just so we can use a pointer to it
4796 * as a unique cookie to identify our (NFSv4's) posix locks. */
4797 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4801 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4803 struct nfs4_lockowner *lo;
4805 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4806 lo = (struct nfs4_lockowner *) fl->fl_owner;
4807 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4808 lo->lo_owner.so_owner.len, GFP_KERNEL);
4809 if (!deny->ld_owner.data)
4810 /* We just don't care that much */
4812 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4813 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4816 deny->ld_owner.len = 0;
4817 deny->ld_owner.data = NULL;
4818 deny->ld_clientid.cl_boot = 0;
4819 deny->ld_clientid.cl_id = 0;
4821 deny->ld_start = fl->fl_start;
4822 deny->ld_length = NFS4_MAX_UINT64;
4823 if (fl->fl_end != NFS4_MAX_UINT64)
4824 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4825 deny->ld_type = NFS4_READ_LT;
4826 if (fl->fl_type != F_RDLCK)
4827 deny->ld_type = NFS4_WRITE_LT;
4830 static struct nfs4_lockowner *
4831 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4832 struct nfs4_client *clp)
4834 unsigned int strhashval = ownerstr_hashval(owner);
4835 struct nfs4_stateowner *so;
4837 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4839 if (so->so_is_open_owner)
4841 if (!same_owner_str(so, owner))
4843 atomic_inc(&so->so_count);
4844 return lockowner(so);
4849 static struct nfs4_lockowner *
4850 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4851 struct nfs4_client *clp)
4853 struct nfs4_lockowner *lo;
4855 spin_lock(&clp->cl_lock);
4856 lo = find_lockowner_str_locked(clid, owner, clp);
4857 spin_unlock(&clp->cl_lock);
4861 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4863 unhash_lockowner_locked(lockowner(sop));
4866 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4868 struct nfs4_lockowner *lo = lockowner(sop);
4870 kmem_cache_free(lockowner_slab, lo);
4873 static const struct nfs4_stateowner_operations lockowner_ops = {
4874 .so_unhash = nfs4_unhash_lockowner,
4875 .so_free = nfs4_free_lockowner,
4879 * Alloc a lock owner structure.
4880 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4883 * strhashval = ownerstr_hashval
4885 static struct nfs4_lockowner *
4886 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4887 struct nfs4_ol_stateid *open_stp,
4888 struct nfsd4_lock *lock)
4890 struct nfs4_lockowner *lo, *ret;
4892 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4895 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4896 lo->lo_owner.so_is_open_owner = 0;
4897 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4898 lo->lo_owner.so_ops = &lockowner_ops;
4899 spin_lock(&clp->cl_lock);
4900 ret = find_lockowner_str_locked(&clp->cl_clientid,
4901 &lock->lk_new_owner, clp);
4903 list_add(&lo->lo_owner.so_strhash,
4904 &clp->cl_ownerstr_hashtbl[strhashval]);
4907 nfs4_free_lockowner(&lo->lo_owner);
4908 spin_unlock(&clp->cl_lock);
4913 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
4914 struct nfs4_file *fp, struct inode *inode,
4915 struct nfs4_ol_stateid *open_stp)
4917 struct nfs4_client *clp = lo->lo_owner.so_client;
4919 lockdep_assert_held(&clp->cl_lock);
4921 atomic_inc(&stp->st_stid.sc_count);
4922 stp->st_stid.sc_type = NFS4_LOCK_STID;
4923 stp->st_stateowner = &lo->lo_owner;
4924 atomic_inc(&lo->lo_owner.so_count);
4926 stp->st_stid.sc_file = fp;
4927 stp->st_stid.sc_free = nfs4_free_lock_stateid;
4928 stp->st_access_bmap = 0;
4929 stp->st_deny_bmap = open_stp->st_deny_bmap;
4930 stp->st_openstp = open_stp;
4931 list_add(&stp->st_locks, &open_stp->st_locks);
4932 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4933 spin_lock(&fp->fi_lock);
4934 list_add(&stp->st_perfile, &fp->fi_stateids);
4935 spin_unlock(&fp->fi_lock);
4938 static struct nfs4_ol_stateid *
4939 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
4941 struct nfs4_ol_stateid *lst;
4942 struct nfs4_client *clp = lo->lo_owner.so_client;
4944 lockdep_assert_held(&clp->cl_lock);
4946 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
4947 if (lst->st_stid.sc_file == fp) {
4948 atomic_inc(&lst->st_stid.sc_count);
4955 static struct nfs4_ol_stateid *
4956 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
4957 struct inode *inode, struct nfs4_ol_stateid *ost,
4960 struct nfs4_stid *ns = NULL;
4961 struct nfs4_ol_stateid *lst;
4962 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4963 struct nfs4_client *clp = oo->oo_owner.so_client;
4965 spin_lock(&clp->cl_lock);
4966 lst = find_lock_stateid(lo, fi);
4968 spin_unlock(&clp->cl_lock);
4969 ns = nfs4_alloc_stid(clp, stateid_slab);
4973 spin_lock(&clp->cl_lock);
4974 lst = find_lock_stateid(lo, fi);
4976 lst = openlockstateid(ns);
4977 init_lock_stateid(lst, lo, fi, inode, ost);
4982 spin_unlock(&clp->cl_lock);
4989 check_lock_length(u64 offset, u64 length)
4991 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4992 LOFF_OVERFLOW(offset, length)));
4995 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4997 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
4999 lockdep_assert_held(&fp->fi_lock);
5001 if (test_access(access, lock_stp))
5003 __nfs4_file_get_access(fp, access);
5004 set_access(access, lock_stp);
5008 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5009 struct nfs4_ol_stateid *ost,
5010 struct nfsd4_lock *lock,
5011 struct nfs4_ol_stateid **lst, bool *new)
5014 struct nfs4_file *fi = ost->st_stid.sc_file;
5015 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5016 struct nfs4_client *cl = oo->oo_owner.so_client;
5017 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5018 struct nfs4_lockowner *lo;
5019 unsigned int strhashval;
5021 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5023 strhashval = ownerstr_hashval(&lock->v.new.owner);
5024 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5026 return nfserr_jukebox;
5028 /* with an existing lockowner, seqids must be the same */
5029 status = nfserr_bad_seqid;
5030 if (!cstate->minorversion &&
5031 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5035 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5037 status = nfserr_jukebox;
5042 nfs4_put_stateowner(&lo->lo_owner);
5050 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5051 struct nfsd4_lock *lock)
5053 struct nfs4_openowner *open_sop = NULL;
5054 struct nfs4_lockowner *lock_sop = NULL;
5055 struct nfs4_ol_stateid *lock_stp = NULL;
5056 struct nfs4_ol_stateid *open_stp = NULL;
5057 struct nfs4_file *fp;
5058 struct file *filp = NULL;
5059 struct file_lock *file_lock = NULL;
5060 struct file_lock *conflock = NULL;
5065 struct net *net = SVC_NET(rqstp);
5066 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5068 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5069 (long long) lock->lk_offset,
5070 (long long) lock->lk_length);
5072 if (check_lock_length(lock->lk_offset, lock->lk_length))
5073 return nfserr_inval;
5075 if ((status = fh_verify(rqstp, &cstate->current_fh,
5076 S_IFREG, NFSD_MAY_LOCK))) {
5077 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5083 if (lock->lk_is_new) {
5084 if (nfsd4_has_session(cstate))
5085 /* See rfc 5661 18.10.3: given clientid is ignored: */
5086 memcpy(&lock->v.new.clientid,
5087 &cstate->session->se_client->cl_clientid,
5088 sizeof(clientid_t));
5090 status = nfserr_stale_clientid;
5091 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5094 /* validate and update open stateid and open seqid */
5095 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5096 lock->lk_new_open_seqid,
5097 &lock->lk_new_open_stateid,
5101 open_sop = openowner(open_stp->st_stateowner);
5102 status = nfserr_bad_stateid;
5103 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5104 &lock->v.new.clientid))
5106 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5109 status = nfs4_preprocess_seqid_op(cstate,
5110 lock->lk_old_lock_seqid,
5111 &lock->lk_old_lock_stateid,
5112 NFS4_LOCK_STID, &lock_stp, nn);
5116 lock_sop = lockowner(lock_stp->st_stateowner);
5118 lkflg = setlkflg(lock->lk_type);
5119 status = nfs4_check_openmode(lock_stp, lkflg);
5123 status = nfserr_grace;
5124 if (locks_in_grace(net) && !lock->lk_reclaim)
5126 status = nfserr_no_grace;
5127 if (!locks_in_grace(net) && lock->lk_reclaim)
5130 file_lock = locks_alloc_lock();
5132 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5133 status = nfserr_jukebox;
5137 fp = lock_stp->st_stid.sc_file;
5138 locks_init_lock(file_lock);
5139 switch (lock->lk_type) {
5142 spin_lock(&fp->fi_lock);
5143 filp = find_readable_file_locked(fp);
5145 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5146 spin_unlock(&fp->fi_lock);
5147 file_lock->fl_type = F_RDLCK;
5150 case NFS4_WRITEW_LT:
5151 spin_lock(&fp->fi_lock);
5152 filp = find_writeable_file_locked(fp);
5154 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5155 spin_unlock(&fp->fi_lock);
5156 file_lock->fl_type = F_WRLCK;
5159 status = nfserr_inval;
5163 status = nfserr_openmode;
5166 file_lock->fl_owner = (fl_owner_t)lock_sop;
5167 file_lock->fl_pid = current->tgid;
5168 file_lock->fl_file = filp;
5169 file_lock->fl_flags = FL_POSIX;
5170 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5171 file_lock->fl_start = lock->lk_offset;
5172 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5173 nfs4_transform_lock_offset(file_lock);
5175 conflock = locks_alloc_lock();
5177 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5178 status = nfserr_jukebox;
5182 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5184 case 0: /* success! */
5185 update_stateid(&lock_stp->st_stid.sc_stateid);
5186 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5190 case (EAGAIN): /* conflock holds conflicting lock */
5191 status = nfserr_denied;
5192 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5193 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5196 status = nfserr_deadlock;
5199 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5200 status = nfserrno(err);
5207 /* Bump seqid manually if the 4.0 replay owner is openowner */
5208 if (cstate->replay_owner &&
5209 cstate->replay_owner != &lock_sop->lo_owner &&
5210 seqid_mutating_err(ntohl(status)))
5211 lock_sop->lo_owner.so_seqid++;
5214 * If this is a new, never-before-used stateid, and we are
5215 * returning an error, then just go ahead and release it.
5218 release_lock_stateid(lock_stp);
5220 nfs4_put_stid(&lock_stp->st_stid);
5223 nfs4_put_stid(&open_stp->st_stid);
5224 nfsd4_bump_seqid(cstate, status);
5225 nfs4_unlock_state();
5227 locks_free_lock(file_lock);
5229 locks_free_lock(conflock);
5234 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5235 * so we do a temporary open here just to get an open file to pass to
5236 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5239 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5242 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5244 err = nfserrno(vfs_test_lock(file, lock));
5254 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5255 struct nfsd4_lockt *lockt)
5257 struct file_lock *file_lock = NULL;
5258 struct nfs4_lockowner *lo = NULL;
5260 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5262 if (locks_in_grace(SVC_NET(rqstp)))
5263 return nfserr_grace;
5265 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5266 return nfserr_inval;
5270 if (!nfsd4_has_session(cstate)) {
5271 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5276 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5279 file_lock = locks_alloc_lock();
5281 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5282 status = nfserr_jukebox;
5285 locks_init_lock(file_lock);
5286 switch (lockt->lt_type) {
5289 file_lock->fl_type = F_RDLCK;
5292 case NFS4_WRITEW_LT:
5293 file_lock->fl_type = F_WRLCK;
5296 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5297 status = nfserr_inval;
5301 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5304 file_lock->fl_owner = (fl_owner_t)lo;
5305 file_lock->fl_pid = current->tgid;
5306 file_lock->fl_flags = FL_POSIX;
5308 file_lock->fl_start = lockt->lt_offset;
5309 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5311 nfs4_transform_lock_offset(file_lock);
5313 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5317 if (file_lock->fl_type != F_UNLCK) {
5318 status = nfserr_denied;
5319 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5323 nfs4_put_stateowner(&lo->lo_owner);
5324 nfs4_unlock_state();
5326 locks_free_lock(file_lock);
5331 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5332 struct nfsd4_locku *locku)
5334 struct nfs4_ol_stateid *stp;
5335 struct file *filp = NULL;
5336 struct file_lock *file_lock = NULL;
5339 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5341 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5342 (long long) locku->lu_offset,
5343 (long long) locku->lu_length);
5345 if (check_lock_length(locku->lu_offset, locku->lu_length))
5346 return nfserr_inval;
5350 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5351 &locku->lu_stateid, NFS4_LOCK_STID,
5355 filp = find_any_file(stp->st_stid.sc_file);
5357 status = nfserr_lock_range;
5360 file_lock = locks_alloc_lock();
5362 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5363 status = nfserr_jukebox;
5366 locks_init_lock(file_lock);
5367 file_lock->fl_type = F_UNLCK;
5368 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
5369 file_lock->fl_pid = current->tgid;
5370 file_lock->fl_file = filp;
5371 file_lock->fl_flags = FL_POSIX;
5372 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5373 file_lock->fl_start = locku->lu_offset;
5375 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5377 nfs4_transform_lock_offset(file_lock);
5379 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5381 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5384 update_stateid(&stp->st_stid.sc_stateid);
5385 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5389 nfs4_put_stid(&stp->st_stid);
5391 nfsd4_bump_seqid(cstate, status);
5392 nfs4_unlock_state();
5394 locks_free_lock(file_lock);
5398 status = nfserrno(err);
5404 * true: locks held by lockowner
5405 * false: no locks held by lockowner
5408 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5410 struct file_lock **flpp;
5412 struct file *filp = find_any_file(fp);
5413 struct inode *inode;
5416 /* Any valid lock stateid should have some sort of access */
5421 inode = file_inode(filp);
5423 spin_lock(&inode->i_lock);
5424 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5425 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5430 spin_unlock(&inode->i_lock);
5436 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5437 struct nfsd4_compound_state *cstate,
5438 struct nfsd4_release_lockowner *rlockowner)
5440 clientid_t *clid = &rlockowner->rl_clientid;
5441 struct nfs4_stateowner *sop;
5442 struct nfs4_lockowner *lo = NULL;
5443 struct nfs4_ol_stateid *stp;
5444 struct xdr_netobj *owner = &rlockowner->rl_owner;
5445 unsigned int hashval = ownerstr_hashval(owner);
5447 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5448 struct nfs4_client *clp;
5450 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5451 clid->cl_boot, clid->cl_id);
5455 status = lookup_clientid(clid, cstate, nn);
5460 /* Find the matching lock stateowner */
5461 spin_lock(&clp->cl_lock);
5462 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5465 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5468 /* see if there are still any locks associated with it */
5469 lo = lockowner(sop);
5470 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5471 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5472 status = nfserr_locks_held;
5473 spin_unlock(&clp->cl_lock);
5478 atomic_inc(&sop->so_count);
5481 spin_unlock(&clp->cl_lock);
5483 release_lockowner(lo);
5485 nfs4_unlock_state();
5489 static inline struct nfs4_client_reclaim *
5492 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5496 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5498 struct nfs4_client_reclaim *crp;
5500 crp = nfsd4_find_reclaim_client(name, nn);
5501 return (crp && crp->cr_clp);
5505 * failure => all reset bets are off, nfserr_no_grace...
5507 struct nfs4_client_reclaim *
5508 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5510 unsigned int strhashval;
5511 struct nfs4_client_reclaim *crp;
5513 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5514 crp = alloc_reclaim();
5516 strhashval = clientstr_hashval(name);
5517 INIT_LIST_HEAD(&crp->cr_strhash);
5518 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5519 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5521 nn->reclaim_str_hashtbl_size++;
5527 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5529 list_del(&crp->cr_strhash);
5531 nn->reclaim_str_hashtbl_size--;
5535 nfs4_release_reclaim(struct nfsd_net *nn)
5537 struct nfs4_client_reclaim *crp = NULL;
5540 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5541 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5542 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5543 struct nfs4_client_reclaim, cr_strhash);
5544 nfs4_remove_reclaim_record(crp, nn);
5547 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5551 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5552 struct nfs4_client_reclaim *
5553 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5555 unsigned int strhashval;
5556 struct nfs4_client_reclaim *crp = NULL;
5558 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5560 strhashval = clientstr_hashval(recdir);
5561 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5562 if (same_name(crp->cr_recdir, recdir)) {
5570 * Called from OPEN. Look for clientid in reclaim list.
5573 nfs4_check_open_reclaim(clientid_t *clid,
5574 struct nfsd4_compound_state *cstate,
5575 struct nfsd_net *nn)
5579 /* find clientid in conf_id_hashtbl */
5580 status = lookup_clientid(clid, cstate, nn);
5582 return nfserr_reclaim_bad;
5584 if (nfsd4_client_record_check(cstate->clp))
5585 return nfserr_reclaim_bad;
5590 #ifdef CONFIG_NFSD_FAULT_INJECTION
5592 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
5594 if (mark_client_expired(clp))
5600 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
5602 char buf[INET6_ADDRSTRLEN];
5603 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5604 printk(KERN_INFO "NFS Client: %s\n", buf);
5608 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5611 char buf[INET6_ADDRSTRLEN];
5612 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5613 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5616 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5617 void (*func)(struct nfs4_ol_stateid *))
5619 struct nfs4_openowner *oop;
5620 struct nfs4_ol_stateid *stp, *st_next;
5621 struct nfs4_ol_stateid *lst, *lst_next;
5624 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5625 list_for_each_entry_safe(stp, st_next,
5626 &oop->oo_owner.so_stateids, st_perstateowner) {
5627 list_for_each_entry_safe(lst, lst_next,
5628 &stp->st_locks, st_locks) {
5640 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
5642 return nfsd_foreach_client_lock(clp, max, release_lock_stateid);
5645 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
5647 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
5648 nfsd_print_count(clp, count, "locked files");
5652 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
5654 struct nfs4_openowner *oop, *next;
5657 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5667 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
5669 return nfsd_foreach_client_open(clp, max, release_openowner);
5672 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
5674 u64 count = nfsd_foreach_client_open(clp, max, NULL);
5675 nfsd_print_count(clp, count, "open files");
5679 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
5680 struct list_head *victims)
5682 struct nfs4_delegation *dp, *next;
5685 lockdep_assert_held(&state_lock);
5686 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
5689 * It's not safe to mess with delegations that have a
5690 * non-zero dl_time. They might have already been broken
5691 * and could be processed by the laundromat outside of
5692 * the state_lock. Just leave them be.
5694 if (dp->dl_time != 0)
5697 unhash_delegation_locked(dp);
5698 list_add(&dp->dl_recall_lru, victims);
5706 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
5708 struct nfs4_delegation *dp, *next;
5712 spin_lock(&state_lock);
5713 count = nfsd_find_all_delegations(clp, max, &victims);
5714 spin_unlock(&state_lock);
5716 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru) {
5717 list_del_init(&dp->dl_recall_lru);
5718 revoke_delegation(dp);
5724 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
5726 struct nfs4_delegation *dp;
5730 spin_lock(&state_lock);
5731 count = nfsd_find_all_delegations(clp, max, &victims);
5732 while (!list_empty(&victims)) {
5733 dp = list_first_entry(&victims, struct nfs4_delegation,
5735 list_del_init(&dp->dl_recall_lru);
5737 nfsd_break_one_deleg(dp);
5739 spin_unlock(&state_lock);
5744 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
5748 spin_lock(&state_lock);
5749 count = nfsd_find_all_delegations(clp, max, NULL);
5750 spin_unlock(&state_lock);
5752 nfsd_print_count(clp, count, "delegations");
5756 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
5758 struct nfs4_client *clp, *next;
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_safe(clp, next, &nn->client_lru, cl_lru) {
5766 count += func(clp, max - count);
5767 if ((max != 0) && (count >= max))
5774 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5776 struct nfs4_client *clp;
5777 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
5779 if (!nfsd_netns_ready(nn))
5782 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5783 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5789 #endif /* CONFIG_NFSD_FAULT_INJECTION */
5792 * Since the lifetime of a delegation isn't limited to that of an open, a
5793 * client may quite reasonably hang on to a delegation as long as it has
5794 * the inode cached. This becomes an obvious problem the first time a
5795 * client's inode cache approaches the size of the server's total memory.
5797 * For now we avoid this problem by imposing a hard limit on the number
5798 * of delegations, which varies according to the server's memory size.
5801 set_max_delegations(void)
5804 * Allow at most 4 delegations per megabyte of RAM. Quick
5805 * estimates suggest that in the worst case (where every delegation
5806 * is for a different inode), a delegation could take about 1.5K,
5807 * giving a worst case usage of about 6% of memory.
5809 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
5812 static int nfs4_state_create_net(struct net *net)
5814 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5817 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5818 CLIENT_HASH_SIZE, GFP_KERNEL);
5819 if (!nn->conf_id_hashtbl)
5821 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
5822 CLIENT_HASH_SIZE, GFP_KERNEL);
5823 if (!nn->unconf_id_hashtbl)
5825 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
5826 SESSION_HASH_SIZE, GFP_KERNEL);
5827 if (!nn->sessionid_hashtbl)
5830 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5831 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
5832 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
5834 for (i = 0; i < SESSION_HASH_SIZE; i++)
5835 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
5836 nn->conf_name_tree = RB_ROOT;
5837 nn->unconf_name_tree = RB_ROOT;
5838 INIT_LIST_HEAD(&nn->client_lru);
5839 INIT_LIST_HEAD(&nn->close_lru);
5840 INIT_LIST_HEAD(&nn->del_recall_lru);
5841 spin_lock_init(&nn->client_lock);
5843 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
5849 kfree(nn->unconf_id_hashtbl);
5851 kfree(nn->conf_id_hashtbl);
5857 nfs4_state_destroy_net(struct net *net)
5860 struct nfs4_client *clp = NULL;
5861 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5863 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5864 while (!list_empty(&nn->conf_id_hashtbl[i])) {
5865 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5866 destroy_client(clp);
5870 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5871 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
5872 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
5873 destroy_client(clp);
5877 kfree(nn->sessionid_hashtbl);
5878 kfree(nn->unconf_id_hashtbl);
5879 kfree(nn->conf_id_hashtbl);
5884 nfs4_state_start_net(struct net *net)
5886 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5889 ret = nfs4_state_create_net(net);
5892 nfsd4_client_tracking_init(net);
5893 nn->boot_time = get_seconds();
5894 locks_start_grace(net, &nn->nfsd4_manager);
5895 nn->grace_ended = false;
5896 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
5897 nn->nfsd4_grace, net);
5898 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
5902 /* initialization to perform when the nfsd service is started: */
5905 nfs4_state_start(void)
5909 ret = set_callback_cred();
5912 laundry_wq = create_singlethread_workqueue("nfsd4");
5913 if (laundry_wq == NULL) {
5917 ret = nfsd4_create_callback_queue();
5919 goto out_free_laundry;
5921 set_max_delegations();
5926 destroy_workqueue(laundry_wq);
5932 nfs4_state_shutdown_net(struct net *net)
5934 struct nfs4_delegation *dp = NULL;
5935 struct list_head *pos, *next, reaplist;
5936 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5938 cancel_delayed_work_sync(&nn->laundromat_work);
5939 locks_end_grace(&nn->nfsd4_manager);
5942 INIT_LIST_HEAD(&reaplist);
5943 spin_lock(&state_lock);
5944 list_for_each_safe(pos, next, &nn->del_recall_lru) {
5945 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5946 unhash_delegation_locked(dp);
5947 list_add(&dp->dl_recall_lru, &reaplist);
5949 spin_unlock(&state_lock);
5950 list_for_each_safe(pos, next, &reaplist) {
5951 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5952 list_del_init(&dp->dl_recall_lru);
5953 nfs4_put_stid(&dp->dl_stid);
5956 nfsd4_client_tracking_exit(net);
5957 nfs4_state_destroy_net(net);
5958 nfs4_unlock_state();
5962 nfs4_state_shutdown(void)
5964 destroy_workqueue(laundry_wq);
5965 nfsd4_destroy_callback_queue();
5969 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5971 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5972 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5976 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5978 if (cstate->minorversion) {
5979 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5980 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5985 clear_current_stateid(struct nfsd4_compound_state *cstate)
5987 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5991 * functions to set current state id
5994 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5996 put_stateid(cstate, &odp->od_stateid);
6000 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6002 put_stateid(cstate, &open->op_stateid);
6006 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6008 put_stateid(cstate, &close->cl_stateid);
6012 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6014 put_stateid(cstate, &lock->lk_resp_stateid);
6018 * functions to consume current state id
6022 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6024 get_stateid(cstate, &odp->od_stateid);
6028 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6030 get_stateid(cstate, &drp->dr_stateid);
6034 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6036 get_stateid(cstate, &fsp->fr_stateid);
6040 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6042 get_stateid(cstate, &setattr->sa_stateid);
6046 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6048 get_stateid(cstate, &close->cl_stateid);
6052 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6054 get_stateid(cstate, &locku->lu_stateid);
6058 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6060 get_stateid(cstate, &read->rd_stateid);
6064 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6066 get_stateid(cstate, &write->wr_stateid);