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);
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
83 static DEFINE_SPINLOCK(state_lock);
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
89 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91 static struct kmem_cache *openowner_slab;
92 static struct kmem_cache *lockowner_slab;
93 static struct kmem_cache *file_slab;
94 static struct kmem_cache *stateid_slab;
95 static struct kmem_cache *deleg_slab;
97 static void free_session(struct nfsd4_session *);
99 static bool is_session_dead(struct nfsd4_session *ses)
101 return ses->se_flags & NFS4_SESSION_DEAD;
104 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
106 if (atomic_read(&ses->se_ref) > ref_held_by_me)
107 return nfserr_jukebox;
108 ses->se_flags |= NFS4_SESSION_DEAD;
112 static bool is_client_expired(struct nfs4_client *clp)
114 return clp->cl_time == 0;
117 static __be32 get_client_locked(struct nfs4_client *clp)
119 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
121 lockdep_assert_held(&nn->client_lock);
123 if (is_client_expired(clp))
124 return nfserr_expired;
125 atomic_inc(&clp->cl_refcount);
129 /* must be called under the client_lock */
131 renew_client_locked(struct nfs4_client *clp)
133 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
135 if (is_client_expired(clp)) {
137 printk("%s: client (clientid %08x/%08x) already expired\n",
139 clp->cl_clientid.cl_boot,
140 clp->cl_clientid.cl_id);
144 dprintk("renewing client (clientid %08x/%08x)\n",
145 clp->cl_clientid.cl_boot,
146 clp->cl_clientid.cl_id);
147 list_move_tail(&clp->cl_lru, &nn->client_lru);
148 clp->cl_time = get_seconds();
152 renew_client(struct nfs4_client *clp)
154 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
156 spin_lock(&nn->client_lock);
157 renew_client_locked(clp);
158 spin_unlock(&nn->client_lock);
161 static void put_client_renew_locked(struct nfs4_client *clp)
163 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
165 lockdep_assert_held(&nn->client_lock);
167 if (!atomic_dec_and_test(&clp->cl_refcount))
169 if (!is_client_expired(clp))
170 renew_client_locked(clp);
173 static void put_client_renew(struct nfs4_client *clp)
175 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
177 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
179 if (!is_client_expired(clp))
180 renew_client_locked(clp);
181 spin_unlock(&nn->client_lock);
184 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
188 if (is_session_dead(ses))
189 return nfserr_badsession;
190 status = get_client_locked(ses->se_client);
193 atomic_inc(&ses->se_ref);
197 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
199 struct nfs4_client *clp = ses->se_client;
200 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
202 lockdep_assert_held(&nn->client_lock);
204 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
206 put_client_renew_locked(clp);
209 static void nfsd4_put_session(struct nfsd4_session *ses)
211 struct nfs4_client *clp = ses->se_client;
212 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
214 spin_lock(&nn->client_lock);
215 nfsd4_put_session_locked(ses);
216 spin_unlock(&nn->client_lock);
219 static inline struct nfs4_stateowner *
220 nfs4_get_stateowner(struct nfs4_stateowner *sop)
222 atomic_inc(&sop->so_count);
227 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
229 return (sop->so_owner.len == owner->len) &&
230 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
233 static struct nfs4_openowner *
234 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
235 struct nfs4_client *clp)
237 struct nfs4_stateowner *so;
239 lockdep_assert_held(&clp->cl_lock);
241 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
243 if (!so->so_is_open_owner)
245 if (same_owner_str(so, &open->op_owner))
246 return openowner(nfs4_get_stateowner(so));
251 static struct nfs4_openowner *
252 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
253 struct nfs4_client *clp)
255 struct nfs4_openowner *oo;
257 spin_lock(&clp->cl_lock);
258 oo = find_openstateowner_str_locked(hashval, open, clp);
259 spin_unlock(&clp->cl_lock);
264 opaque_hashval(const void *ptr, int nbytes)
266 unsigned char *cptr = (unsigned char *) ptr;
276 static void nfsd4_free_file(struct nfs4_file *f)
278 kmem_cache_free(file_slab, f);
282 put_nfs4_file(struct nfs4_file *fi)
284 might_lock(&state_lock);
286 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
287 hlist_del(&fi->fi_hash);
288 spin_unlock(&state_lock);
294 get_nfs4_file(struct nfs4_file *fi)
296 atomic_inc(&fi->fi_ref);
300 __nfs4_get_fd(struct nfs4_file *f, int oflag)
302 if (f->fi_fds[oflag])
303 return get_file(f->fi_fds[oflag]);
308 find_writeable_file_locked(struct nfs4_file *f)
312 lockdep_assert_held(&f->fi_lock);
314 ret = __nfs4_get_fd(f, O_WRONLY);
316 ret = __nfs4_get_fd(f, O_RDWR);
321 find_writeable_file(struct nfs4_file *f)
325 spin_lock(&f->fi_lock);
326 ret = find_writeable_file_locked(f);
327 spin_unlock(&f->fi_lock);
332 static struct file *find_readable_file_locked(struct nfs4_file *f)
336 lockdep_assert_held(&f->fi_lock);
338 ret = __nfs4_get_fd(f, O_RDONLY);
340 ret = __nfs4_get_fd(f, O_RDWR);
345 find_readable_file(struct nfs4_file *f)
349 spin_lock(&f->fi_lock);
350 ret = find_readable_file_locked(f);
351 spin_unlock(&f->fi_lock);
357 find_any_file(struct nfs4_file *f)
361 spin_lock(&f->fi_lock);
362 ret = __nfs4_get_fd(f, O_RDWR);
364 ret = __nfs4_get_fd(f, O_WRONLY);
366 ret = __nfs4_get_fd(f, O_RDONLY);
368 spin_unlock(&f->fi_lock);
372 static atomic_long_t num_delegations;
373 unsigned long max_delegations;
376 * Open owner state (share locks)
379 /* hash tables for lock and open owners */
380 #define OWNER_HASH_BITS 8
381 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
382 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
384 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
388 ret = opaque_hashval(ownername->data, ownername->len);
389 return ret & OWNER_HASH_MASK;
392 /* hash table for nfs4_file */
393 #define FILE_HASH_BITS 8
394 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
396 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
398 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
401 static unsigned int file_hashval(struct knfsd_fh *fh)
403 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
406 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
408 return fh1->fh_size == fh2->fh_size &&
409 !memcmp(fh1->fh_base.fh_pad,
414 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
417 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
419 lockdep_assert_held(&fp->fi_lock);
421 if (access & NFS4_SHARE_ACCESS_WRITE)
422 atomic_inc(&fp->fi_access[O_WRONLY]);
423 if (access & NFS4_SHARE_ACCESS_READ)
424 atomic_inc(&fp->fi_access[O_RDONLY]);
428 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
430 lockdep_assert_held(&fp->fi_lock);
432 /* Does this access mode make sense? */
433 if (access & ~NFS4_SHARE_ACCESS_BOTH)
436 /* Does it conflict with a deny mode already set? */
437 if ((access & fp->fi_share_deny) != 0)
438 return nfserr_share_denied;
440 __nfs4_file_get_access(fp, access);
444 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
446 /* Common case is that there is no deny mode. */
448 /* Does this deny mode make sense? */
449 if (deny & ~NFS4_SHARE_DENY_BOTH)
452 if ((deny & NFS4_SHARE_DENY_READ) &&
453 atomic_read(&fp->fi_access[O_RDONLY]))
454 return nfserr_share_denied;
456 if ((deny & NFS4_SHARE_DENY_WRITE) &&
457 atomic_read(&fp->fi_access[O_WRONLY]))
458 return nfserr_share_denied;
463 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
465 might_lock(&fp->fi_lock);
467 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
468 struct file *f1 = NULL;
469 struct file *f2 = NULL;
471 swap(f1, fp->fi_fds[oflag]);
472 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
473 swap(f2, fp->fi_fds[O_RDWR]);
474 spin_unlock(&fp->fi_lock);
482 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
484 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
486 if (access & NFS4_SHARE_ACCESS_WRITE)
487 __nfs4_file_put_access(fp, O_WRONLY);
488 if (access & NFS4_SHARE_ACCESS_READ)
489 __nfs4_file_put_access(fp, O_RDONLY);
492 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
493 struct kmem_cache *slab)
495 struct nfs4_stid *stid;
498 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
502 idr_preload(GFP_KERNEL);
503 spin_lock(&cl->cl_lock);
504 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
505 spin_unlock(&cl->cl_lock);
509 stid->sc_client = cl;
510 stid->sc_stateid.si_opaque.so_id = new_id;
511 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
512 /* Will be incremented before return to client: */
513 atomic_set(&stid->sc_count, 1);
516 * It shouldn't be a problem to reuse an opaque stateid value.
517 * I don't think it is for 4.1. But with 4.0 I worry that, for
518 * example, a stray write retransmission could be accepted by
519 * the server when it should have been rejected. Therefore,
520 * adopt a trick from the sctp code to attempt to maximize the
521 * amount of time until an id is reused, by ensuring they always
522 * "increase" (mod INT_MAX):
526 kmem_cache_free(slab, stid);
530 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
532 struct nfs4_stid *stid;
533 struct nfs4_ol_stateid *stp;
535 stid = nfs4_alloc_stid(clp, stateid_slab);
539 stp = openlockstateid(stid);
540 stp->st_stid.sc_free = nfs4_free_ol_stateid;
544 static void nfs4_free_deleg(struct nfs4_stid *stid)
546 kmem_cache_free(deleg_slab, stid);
547 atomic_long_dec(&num_delegations);
551 * When we recall a delegation, we should be careful not to hand it
552 * out again straight away.
553 * To ensure this we keep a pair of bloom filters ('new' and 'old')
554 * in which the filehandles of recalled delegations are "stored".
555 * If a filehandle appear in either filter, a delegation is blocked.
556 * When a delegation is recalled, the filehandle is stored in the "new"
558 * Every 30 seconds we swap the filters and clear the "new" one,
559 * unless both are empty of course.
561 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
562 * low 3 bytes as hash-table indices.
564 * 'blocked_delegations_lock', which is always taken in block_delegations(),
565 * is used to manage concurrent access. Testing does not need the lock
566 * except when swapping the two filters.
568 static DEFINE_SPINLOCK(blocked_delegations_lock);
569 static struct bloom_pair {
570 int entries, old_entries;
572 int new; /* index into 'set' */
573 DECLARE_BITMAP(set[2], 256);
574 } blocked_delegations;
576 static int delegation_blocked(struct knfsd_fh *fh)
579 struct bloom_pair *bd = &blocked_delegations;
581 if (bd->entries == 0)
583 if (seconds_since_boot() - bd->swap_time > 30) {
584 spin_lock(&blocked_delegations_lock);
585 if (seconds_since_boot() - bd->swap_time > 30) {
586 bd->entries -= bd->old_entries;
587 bd->old_entries = bd->entries;
588 memset(bd->set[bd->new], 0,
591 bd->swap_time = seconds_since_boot();
593 spin_unlock(&blocked_delegations_lock);
595 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
596 if (test_bit(hash&255, bd->set[0]) &&
597 test_bit((hash>>8)&255, bd->set[0]) &&
598 test_bit((hash>>16)&255, bd->set[0]))
601 if (test_bit(hash&255, bd->set[1]) &&
602 test_bit((hash>>8)&255, bd->set[1]) &&
603 test_bit((hash>>16)&255, bd->set[1]))
609 static void block_delegations(struct knfsd_fh *fh)
612 struct bloom_pair *bd = &blocked_delegations;
614 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
616 spin_lock(&blocked_delegations_lock);
617 __set_bit(hash&255, bd->set[bd->new]);
618 __set_bit((hash>>8)&255, bd->set[bd->new]);
619 __set_bit((hash>>16)&255, bd->set[bd->new]);
620 if (bd->entries == 0)
621 bd->swap_time = seconds_since_boot();
623 spin_unlock(&blocked_delegations_lock);
626 static struct nfs4_delegation *
627 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
629 struct nfs4_delegation *dp;
632 dprintk("NFSD alloc_init_deleg\n");
633 n = atomic_long_inc_return(&num_delegations);
634 if (n < 0 || n > max_delegations)
636 if (delegation_blocked(¤t_fh->fh_handle))
638 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
642 dp->dl_stid.sc_free = nfs4_free_deleg;
644 * delegation seqid's are never incremented. The 4.1 special
645 * meaning of seqid 0 isn't meaningful, really, but let's avoid
646 * 0 anyway just for consistency and use 1:
648 dp->dl_stid.sc_stateid.si_generation = 1;
649 INIT_LIST_HEAD(&dp->dl_perfile);
650 INIT_LIST_HEAD(&dp->dl_perclnt);
651 INIT_LIST_HEAD(&dp->dl_recall_lru);
652 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
653 INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
656 atomic_long_dec(&num_delegations);
661 nfs4_put_stid(struct nfs4_stid *s)
663 struct nfs4_file *fp = s->sc_file;
664 struct nfs4_client *clp = s->sc_client;
666 might_lock(&clp->cl_lock);
668 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
669 wake_up_all(&close_wq);
672 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
673 spin_unlock(&clp->cl_lock);
679 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
681 struct file *filp = NULL;
683 spin_lock(&fp->fi_lock);
684 if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
685 swap(filp, fp->fi_deleg_file);
686 spin_unlock(&fp->fi_lock);
689 vfs_setlease(filp, F_UNLCK, NULL, NULL);
694 static void unhash_stid(struct nfs4_stid *s)
700 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
702 lockdep_assert_held(&state_lock);
703 lockdep_assert_held(&fp->fi_lock);
705 atomic_inc(&dp->dl_stid.sc_count);
706 dp->dl_stid.sc_type = NFS4_DELEG_STID;
707 list_add(&dp->dl_perfile, &fp->fi_delegations);
708 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
712 unhash_delegation_locked(struct nfs4_delegation *dp)
714 struct nfs4_file *fp = dp->dl_stid.sc_file;
716 lockdep_assert_held(&state_lock);
718 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
719 /* Ensure that deleg break won't try to requeue it */
721 spin_lock(&fp->fi_lock);
722 list_del_init(&dp->dl_perclnt);
723 list_del_init(&dp->dl_recall_lru);
724 list_del_init(&dp->dl_perfile);
725 spin_unlock(&fp->fi_lock);
728 static void destroy_delegation(struct nfs4_delegation *dp)
730 spin_lock(&state_lock);
731 unhash_delegation_locked(dp);
732 spin_unlock(&state_lock);
733 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
734 nfs4_put_stid(&dp->dl_stid);
737 static void revoke_delegation(struct nfs4_delegation *dp)
739 struct nfs4_client *clp = dp->dl_stid.sc_client;
741 WARN_ON(!list_empty(&dp->dl_recall_lru));
743 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
745 if (clp->cl_minorversion == 0)
746 nfs4_put_stid(&dp->dl_stid);
748 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
749 spin_lock(&clp->cl_lock);
750 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
751 spin_unlock(&clp->cl_lock);
759 static unsigned int clientid_hashval(u32 id)
761 return id & CLIENT_HASH_MASK;
764 static unsigned int clientstr_hashval(const char *name)
766 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
770 * We store the NONE, READ, WRITE, and BOTH bits separately in the
771 * st_{access,deny}_bmap field of the stateid, in order to track not
772 * only what share bits are currently in force, but also what
773 * combinations of share bits previous opens have used. This allows us
774 * to enforce the recommendation of rfc 3530 14.2.19 that the server
775 * return an error if the client attempt to downgrade to a combination
776 * of share bits not explicable by closing some of its previous opens.
778 * XXX: This enforcement is actually incomplete, since we don't keep
779 * track of access/deny bit combinations; so, e.g., we allow:
781 * OPEN allow read, deny write
782 * OPEN allow both, deny none
783 * DOWNGRADE allow read, deny none
785 * which we should reject.
788 bmap_to_share_mode(unsigned long bmap) {
790 unsigned int access = 0;
792 for (i = 1; i < 4; i++) {
793 if (test_bit(i, &bmap))
799 /* set share access for a given stateid */
801 set_access(u32 access, struct nfs4_ol_stateid *stp)
803 unsigned char mask = 1 << access;
805 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
806 stp->st_access_bmap |= mask;
809 /* clear share access for a given stateid */
811 clear_access(u32 access, struct nfs4_ol_stateid *stp)
813 unsigned char mask = 1 << access;
815 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
816 stp->st_access_bmap &= ~mask;
819 /* test whether a given stateid has access */
821 test_access(u32 access, struct nfs4_ol_stateid *stp)
823 unsigned char mask = 1 << access;
825 return (bool)(stp->st_access_bmap & mask);
828 /* set share deny for a given stateid */
830 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
832 unsigned char mask = 1 << deny;
834 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
835 stp->st_deny_bmap |= mask;
838 /* clear share deny for a given stateid */
840 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
842 unsigned char mask = 1 << deny;
844 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
845 stp->st_deny_bmap &= ~mask;
848 /* test whether a given stateid is denying specific access */
850 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
852 unsigned char mask = 1 << deny;
854 return (bool)(stp->st_deny_bmap & mask);
857 static int nfs4_access_to_omode(u32 access)
859 switch (access & NFS4_SHARE_ACCESS_BOTH) {
860 case NFS4_SHARE_ACCESS_READ:
862 case NFS4_SHARE_ACCESS_WRITE:
864 case NFS4_SHARE_ACCESS_BOTH:
872 * A stateid that had a deny mode associated with it is being released
873 * or downgraded. Recalculate the deny mode on the file.
876 recalculate_deny_mode(struct nfs4_file *fp)
878 struct nfs4_ol_stateid *stp;
880 spin_lock(&fp->fi_lock);
881 fp->fi_share_deny = 0;
882 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
883 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
884 spin_unlock(&fp->fi_lock);
888 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
893 for (i = 1; i < 4; i++) {
894 if ((i & deny) != i) {
900 /* Recalculate per-file deny mode if there was a change */
902 recalculate_deny_mode(stp->st_stid.sc_file);
905 /* release all access and file references for a given stateid */
907 release_all_access(struct nfs4_ol_stateid *stp)
910 struct nfs4_file *fp = stp->st_stid.sc_file;
912 if (fp && stp->st_deny_bmap != 0)
913 recalculate_deny_mode(fp);
915 for (i = 1; i < 4; i++) {
916 if (test_access(i, stp))
917 nfs4_file_put_access(stp->st_stid.sc_file, i);
918 clear_access(i, stp);
922 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
924 struct nfs4_client *clp = sop->so_client;
926 might_lock(&clp->cl_lock);
928 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
930 sop->so_ops->so_unhash(sop);
931 spin_unlock(&clp->cl_lock);
932 kfree(sop->so_owner.data);
933 sop->so_ops->so_free(sop);
936 static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
938 struct nfs4_file *fp = stp->st_stid.sc_file;
940 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
942 spin_lock(&fp->fi_lock);
943 list_del(&stp->st_perfile);
944 spin_unlock(&fp->fi_lock);
945 list_del(&stp->st_perstateowner);
948 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
950 struct nfs4_ol_stateid *stp = openlockstateid(stid);
952 release_all_access(stp);
953 if (stp->st_stateowner)
954 nfs4_put_stateowner(stp->st_stateowner);
955 kmem_cache_free(stateid_slab, stid);
958 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
960 struct nfs4_ol_stateid *stp = openlockstateid(stid);
961 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
964 file = find_any_file(stp->st_stid.sc_file);
966 filp_close(file, (fl_owner_t)lo);
967 nfs4_free_ol_stateid(stid);
971 * Put the persistent reference to an already unhashed generic stateid, while
972 * holding the cl_lock. If it's the last reference, then put it onto the
973 * reaplist for later destruction.
975 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
976 struct list_head *reaplist)
978 struct nfs4_stid *s = &stp->st_stid;
979 struct nfs4_client *clp = s->sc_client;
981 lockdep_assert_held(&clp->cl_lock);
983 WARN_ON_ONCE(!list_empty(&stp->st_locks));
985 if (!atomic_dec_and_test(&s->sc_count)) {
986 wake_up_all(&close_wq);
990 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
991 list_add(&stp->st_locks, reaplist);
994 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
996 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
998 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1000 list_del_init(&stp->st_locks);
1001 unhash_ol_stateid(stp);
1002 unhash_stid(&stp->st_stid);
1005 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1007 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1009 spin_lock(&oo->oo_owner.so_client->cl_lock);
1010 unhash_lock_stateid(stp);
1011 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1012 nfs4_put_stid(&stp->st_stid);
1015 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1017 struct nfs4_client *clp = lo->lo_owner.so_client;
1019 lockdep_assert_held(&clp->cl_lock);
1021 list_del_init(&lo->lo_owner.so_strhash);
1025 * Free a list of generic stateids that were collected earlier after being
1029 free_ol_stateid_reaplist(struct list_head *reaplist)
1031 struct nfs4_ol_stateid *stp;
1032 struct nfs4_file *fp;
1036 while (!list_empty(reaplist)) {
1037 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1039 list_del(&stp->st_locks);
1040 fp = stp->st_stid.sc_file;
1041 stp->st_stid.sc_free(&stp->st_stid);
1047 static void release_lockowner(struct nfs4_lockowner *lo)
1049 struct nfs4_client *clp = lo->lo_owner.so_client;
1050 struct nfs4_ol_stateid *stp;
1051 struct list_head reaplist;
1053 INIT_LIST_HEAD(&reaplist);
1055 spin_lock(&clp->cl_lock);
1056 unhash_lockowner_locked(lo);
1057 while (!list_empty(&lo->lo_owner.so_stateids)) {
1058 stp = list_first_entry(&lo->lo_owner.so_stateids,
1059 struct nfs4_ol_stateid, st_perstateowner);
1060 unhash_lock_stateid(stp);
1061 put_ol_stateid_locked(stp, &reaplist);
1063 spin_unlock(&clp->cl_lock);
1064 free_ol_stateid_reaplist(&reaplist);
1065 nfs4_put_stateowner(&lo->lo_owner);
1068 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1069 struct list_head *reaplist)
1071 struct nfs4_ol_stateid *stp;
1073 while (!list_empty(&open_stp->st_locks)) {
1074 stp = list_entry(open_stp->st_locks.next,
1075 struct nfs4_ol_stateid, st_locks);
1076 unhash_lock_stateid(stp);
1077 put_ol_stateid_locked(stp, reaplist);
1081 static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1082 struct list_head *reaplist)
1084 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1086 unhash_ol_stateid(stp);
1087 release_open_stateid_locks(stp, reaplist);
1090 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1092 LIST_HEAD(reaplist);
1094 spin_lock(&stp->st_stid.sc_client->cl_lock);
1095 unhash_open_stateid(stp, &reaplist);
1096 put_ol_stateid_locked(stp, &reaplist);
1097 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1098 free_ol_stateid_reaplist(&reaplist);
1101 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1103 struct nfs4_client *clp = oo->oo_owner.so_client;
1105 lockdep_assert_held(&clp->cl_lock);
1107 list_del_init(&oo->oo_owner.so_strhash);
1108 list_del_init(&oo->oo_perclient);
1111 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1113 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1115 struct nfs4_ol_stateid *s;
1117 spin_lock(&nn->client_lock);
1118 s = oo->oo_last_closed_stid;
1120 list_del_init(&oo->oo_close_lru);
1121 oo->oo_last_closed_stid = NULL;
1123 spin_unlock(&nn->client_lock);
1125 nfs4_put_stid(&s->st_stid);
1128 static void release_openowner(struct nfs4_openowner *oo)
1130 struct nfs4_ol_stateid *stp;
1131 struct nfs4_client *clp = oo->oo_owner.so_client;
1132 struct list_head reaplist;
1134 INIT_LIST_HEAD(&reaplist);
1136 spin_lock(&clp->cl_lock);
1137 unhash_openowner_locked(oo);
1138 while (!list_empty(&oo->oo_owner.so_stateids)) {
1139 stp = list_first_entry(&oo->oo_owner.so_stateids,
1140 struct nfs4_ol_stateid, st_perstateowner);
1141 unhash_open_stateid(stp, &reaplist);
1142 put_ol_stateid_locked(stp, &reaplist);
1144 spin_unlock(&clp->cl_lock);
1145 free_ol_stateid_reaplist(&reaplist);
1146 release_last_closed_stateid(oo);
1147 nfs4_put_stateowner(&oo->oo_owner);
1151 hash_sessionid(struct nfs4_sessionid *sessionid)
1153 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1155 return sid->sequence % SESSION_HASH_SIZE;
1160 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1162 u32 *ptr = (u32 *)(&sessionid->data[0]);
1163 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1167 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1173 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1174 * won't be used for replay.
1176 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1178 struct nfs4_stateowner *so = cstate->replay_owner;
1180 if (nfserr == nfserr_replay_me)
1183 if (!seqid_mutating_err(ntohl(nfserr))) {
1184 nfsd4_cstate_clear_replay(cstate);
1189 if (so->so_is_open_owner)
1190 release_last_closed_stateid(openowner(so));
1196 gen_sessionid(struct nfsd4_session *ses)
1198 struct nfs4_client *clp = ses->se_client;
1199 struct nfsd4_sessionid *sid;
1201 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1202 sid->clientid = clp->cl_clientid;
1203 sid->sequence = current_sessionid++;
1208 * The protocol defines ca_maxresponssize_cached to include the size of
1209 * the rpc header, but all we need to cache is the data starting after
1210 * the end of the initial SEQUENCE operation--the rest we regenerate
1211 * each time. Therefore we can advertise a ca_maxresponssize_cached
1212 * value that is the number of bytes in our cache plus a few additional
1213 * bytes. In order to stay on the safe side, and not promise more than
1214 * we can cache, those additional bytes must be the minimum possible: 24
1215 * bytes of rpc header (xid through accept state, with AUTH_NULL
1216 * verifier), 12 for the compound header (with zero-length tag), and 44
1217 * for the SEQUENCE op response:
1219 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1222 free_session_slots(struct nfsd4_session *ses)
1226 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1227 kfree(ses->se_slots[i]);
1231 * We don't actually need to cache the rpc and session headers, so we
1232 * can allocate a little less for each slot:
1234 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1238 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1241 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1242 return size + sizeof(struct nfsd4_slot);
1246 * XXX: If we run out of reserved DRC memory we could (up to a point)
1247 * re-negotiate active sessions and reduce their slot usage to make
1248 * room for new connections. For now we just fail the create session.
1250 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1252 u32 slotsize = slot_bytes(ca);
1253 u32 num = ca->maxreqs;
1256 spin_lock(&nfsd_drc_lock);
1257 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1258 nfsd_drc_max_mem - nfsd_drc_mem_used);
1259 num = min_t(int, num, avail / slotsize);
1260 nfsd_drc_mem_used += num * slotsize;
1261 spin_unlock(&nfsd_drc_lock);
1266 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1268 int slotsize = slot_bytes(ca);
1270 spin_lock(&nfsd_drc_lock);
1271 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1272 spin_unlock(&nfsd_drc_lock);
1275 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1276 struct nfsd4_channel_attrs *battrs)
1278 int numslots = fattrs->maxreqs;
1279 int slotsize = slot_bytes(fattrs);
1280 struct nfsd4_session *new;
1283 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1284 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1285 mem = numslots * sizeof(struct nfsd4_slot *);
1287 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1290 /* allocate each struct nfsd4_slot and data cache in one piece */
1291 for (i = 0; i < numslots; i++) {
1292 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1293 if (!new->se_slots[i])
1297 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1298 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1303 kfree(new->se_slots[i]);
1308 static void free_conn(struct nfsd4_conn *c)
1310 svc_xprt_put(c->cn_xprt);
1314 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1316 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1317 struct nfs4_client *clp = c->cn_session->se_client;
1319 spin_lock(&clp->cl_lock);
1320 if (!list_empty(&c->cn_persession)) {
1321 list_del(&c->cn_persession);
1324 nfsd4_probe_callback(clp);
1325 spin_unlock(&clp->cl_lock);
1328 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1330 struct nfsd4_conn *conn;
1332 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1335 svc_xprt_get(rqstp->rq_xprt);
1336 conn->cn_xprt = rqstp->rq_xprt;
1337 conn->cn_flags = flags;
1338 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1342 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1344 conn->cn_session = ses;
1345 list_add(&conn->cn_persession, &ses->se_conns);
1348 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1350 struct nfs4_client *clp = ses->se_client;
1352 spin_lock(&clp->cl_lock);
1353 __nfsd4_hash_conn(conn, ses);
1354 spin_unlock(&clp->cl_lock);
1357 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1359 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1360 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1363 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1367 nfsd4_hash_conn(conn, ses);
1368 ret = nfsd4_register_conn(conn);
1370 /* oops; xprt is already down: */
1371 nfsd4_conn_lost(&conn->cn_xpt_user);
1372 /* We may have gained or lost a callback channel: */
1373 nfsd4_probe_callback_sync(ses->se_client);
1376 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1378 u32 dir = NFS4_CDFC4_FORE;
1380 if (cses->flags & SESSION4_BACK_CHAN)
1381 dir |= NFS4_CDFC4_BACK;
1382 return alloc_conn(rqstp, dir);
1385 /* must be called under client_lock */
1386 static void nfsd4_del_conns(struct nfsd4_session *s)
1388 struct nfs4_client *clp = s->se_client;
1389 struct nfsd4_conn *c;
1391 spin_lock(&clp->cl_lock);
1392 while (!list_empty(&s->se_conns)) {
1393 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1394 list_del_init(&c->cn_persession);
1395 spin_unlock(&clp->cl_lock);
1397 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1400 spin_lock(&clp->cl_lock);
1402 spin_unlock(&clp->cl_lock);
1405 static void __free_session(struct nfsd4_session *ses)
1407 free_session_slots(ses);
1411 static void free_session(struct nfsd4_session *ses)
1413 nfsd4_del_conns(ses);
1414 nfsd4_put_drc_mem(&ses->se_fchannel);
1415 __free_session(ses);
1418 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1421 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1423 new->se_client = clp;
1426 INIT_LIST_HEAD(&new->se_conns);
1428 new->se_cb_seq_nr = 1;
1429 new->se_flags = cses->flags;
1430 new->se_cb_prog = cses->callback_prog;
1431 new->se_cb_sec = cses->cb_sec;
1432 atomic_set(&new->se_ref, 0);
1433 idx = hash_sessionid(&new->se_sessionid);
1434 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1435 spin_lock(&clp->cl_lock);
1436 list_add(&new->se_perclnt, &clp->cl_sessions);
1437 spin_unlock(&clp->cl_lock);
1439 if (cses->flags & SESSION4_BACK_CHAN) {
1440 struct sockaddr *sa = svc_addr(rqstp);
1442 * This is a little silly; with sessions there's no real
1443 * use for the callback address. Use the peer address
1444 * as a reasonable default for now, but consider fixing
1445 * the rpc client not to require an address in the
1448 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1449 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1453 /* caller must hold client_lock */
1454 static struct nfsd4_session *
1455 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1457 struct nfsd4_session *elem;
1459 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1461 lockdep_assert_held(&nn->client_lock);
1463 dump_sessionid(__func__, sessionid);
1464 idx = hash_sessionid(sessionid);
1465 /* Search in the appropriate list */
1466 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1467 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1468 NFS4_MAX_SESSIONID_LEN)) {
1473 dprintk("%s: session not found\n", __func__);
1477 static struct nfsd4_session *
1478 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1481 struct nfsd4_session *session;
1482 __be32 status = nfserr_badsession;
1484 session = __find_in_sessionid_hashtbl(sessionid, net);
1487 status = nfsd4_get_session_locked(session);
1495 /* caller must hold client_lock */
1497 unhash_session(struct nfsd4_session *ses)
1499 struct nfs4_client *clp = ses->se_client;
1500 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1502 lockdep_assert_held(&nn->client_lock);
1504 list_del(&ses->se_hash);
1505 spin_lock(&ses->se_client->cl_lock);
1506 list_del(&ses->se_perclnt);
1507 spin_unlock(&ses->se_client->cl_lock);
1510 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1512 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1514 if (clid->cl_boot == nn->boot_time)
1516 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1517 clid->cl_boot, clid->cl_id, nn->boot_time);
1522 * XXX Should we use a slab cache ?
1523 * This type of memory management is somewhat inefficient, but we use it
1524 * anyway since SETCLIENTID is not a common operation.
1526 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1528 struct nfs4_client *clp;
1531 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1534 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1535 if (clp->cl_name.data == NULL)
1537 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1538 OWNER_HASH_SIZE, GFP_KERNEL);
1539 if (!clp->cl_ownerstr_hashtbl)
1540 goto err_no_hashtbl;
1541 for (i = 0; i < OWNER_HASH_SIZE; i++)
1542 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1543 clp->cl_name.len = name.len;
1544 INIT_LIST_HEAD(&clp->cl_sessions);
1545 idr_init(&clp->cl_stateids);
1546 atomic_set(&clp->cl_refcount, 0);
1547 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1548 INIT_LIST_HEAD(&clp->cl_idhash);
1549 INIT_LIST_HEAD(&clp->cl_openowners);
1550 INIT_LIST_HEAD(&clp->cl_delegations);
1551 INIT_LIST_HEAD(&clp->cl_lru);
1552 INIT_LIST_HEAD(&clp->cl_callbacks);
1553 INIT_LIST_HEAD(&clp->cl_revoked);
1554 spin_lock_init(&clp->cl_lock);
1555 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1558 kfree(clp->cl_name.data);
1565 free_client(struct nfs4_client *clp)
1567 while (!list_empty(&clp->cl_sessions)) {
1568 struct nfsd4_session *ses;
1569 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1571 list_del(&ses->se_perclnt);
1572 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1575 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1576 free_svc_cred(&clp->cl_cred);
1577 kfree(clp->cl_ownerstr_hashtbl);
1578 kfree(clp->cl_name.data);
1579 idr_destroy(&clp->cl_stateids);
1583 /* must be called under the client_lock */
1585 unhash_client_locked(struct nfs4_client *clp)
1587 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1588 struct nfsd4_session *ses;
1590 lockdep_assert_held(&nn->client_lock);
1592 /* Mark the client as expired! */
1594 /* Make it invisible */
1595 if (!list_empty(&clp->cl_idhash)) {
1596 list_del_init(&clp->cl_idhash);
1597 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1598 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1600 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1602 list_del_init(&clp->cl_lru);
1603 spin_lock(&clp->cl_lock);
1604 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1605 list_del_init(&ses->se_hash);
1606 spin_unlock(&clp->cl_lock);
1610 unhash_client(struct nfs4_client *clp)
1612 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1614 spin_lock(&nn->client_lock);
1615 unhash_client_locked(clp);
1616 spin_unlock(&nn->client_lock);
1619 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1621 if (atomic_read(&clp->cl_refcount))
1622 return nfserr_jukebox;
1623 unhash_client_locked(clp);
1628 __destroy_client(struct nfs4_client *clp)
1630 struct nfs4_openowner *oo;
1631 struct nfs4_delegation *dp;
1632 struct list_head reaplist;
1634 INIT_LIST_HEAD(&reaplist);
1635 spin_lock(&state_lock);
1636 while (!list_empty(&clp->cl_delegations)) {
1637 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1638 unhash_delegation_locked(dp);
1639 list_add(&dp->dl_recall_lru, &reaplist);
1641 spin_unlock(&state_lock);
1642 while (!list_empty(&reaplist)) {
1643 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1644 list_del_init(&dp->dl_recall_lru);
1645 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1646 nfs4_put_stid(&dp->dl_stid);
1648 while (!list_empty(&clp->cl_revoked)) {
1649 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1650 list_del_init(&dp->dl_recall_lru);
1651 nfs4_put_stid(&dp->dl_stid);
1653 while (!list_empty(&clp->cl_openowners)) {
1654 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1655 nfs4_get_stateowner(&oo->oo_owner);
1656 release_openowner(oo);
1658 nfsd4_shutdown_callback(clp);
1659 if (clp->cl_cb_conn.cb_xprt)
1660 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1665 destroy_client(struct nfs4_client *clp)
1668 __destroy_client(clp);
1671 static void expire_client(struct nfs4_client *clp)
1674 nfsd4_client_record_remove(clp);
1675 __destroy_client(clp);
1678 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1680 memcpy(target->cl_verifier.data, source->data,
1681 sizeof(target->cl_verifier.data));
1684 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1686 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1687 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1690 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1692 if (source->cr_principal) {
1693 target->cr_principal =
1694 kstrdup(source->cr_principal, GFP_KERNEL);
1695 if (target->cr_principal == NULL)
1698 target->cr_principal = NULL;
1699 target->cr_flavor = source->cr_flavor;
1700 target->cr_uid = source->cr_uid;
1701 target->cr_gid = source->cr_gid;
1702 target->cr_group_info = source->cr_group_info;
1703 get_group_info(target->cr_group_info);
1704 target->cr_gss_mech = source->cr_gss_mech;
1705 if (source->cr_gss_mech)
1706 gss_mech_get(source->cr_gss_mech);
1711 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1715 res = o1->len - o2->len;
1718 return (long long)memcmp(o1->data, o2->data, o1->len);
1721 static int same_name(const char *n1, const char *n2)
1723 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1727 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1729 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1733 same_clid(clientid_t *cl1, clientid_t *cl2)
1735 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1738 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1742 if (g1->ngroups != g2->ngroups)
1744 for (i=0; i<g1->ngroups; i++)
1745 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1751 * RFC 3530 language requires clid_inuse be returned when the
1752 * "principal" associated with a requests differs from that previously
1753 * used. We use uid, gid's, and gss principal string as our best
1754 * approximation. We also don't want to allow non-gss use of a client
1755 * established using gss: in theory cr_principal should catch that
1756 * change, but in practice cr_principal can be null even in the gss case
1757 * since gssd doesn't always pass down a principal string.
1759 static bool is_gss_cred(struct svc_cred *cr)
1761 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1762 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1767 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1769 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1770 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1771 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1772 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1774 if (cr1->cr_principal == cr2->cr_principal)
1776 if (!cr1->cr_principal || !cr2->cr_principal)
1778 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1781 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1783 struct svc_cred *cr = &rqstp->rq_cred;
1786 if (!cr->cr_gss_mech)
1788 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1789 return service == RPC_GSS_SVC_INTEGRITY ||
1790 service == RPC_GSS_SVC_PRIVACY;
1793 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1795 struct svc_cred *cr = &rqstp->rq_cred;
1797 if (!cl->cl_mach_cred)
1799 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1801 if (!svc_rqst_integrity_protected(rqstp))
1803 if (!cr->cr_principal)
1805 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1808 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1813 * This is opaque to client, so no need to byte-swap. Use
1814 * __force to keep sparse happy
1816 verf[0] = (__force __be32)get_seconds();
1817 verf[1] = (__force __be32)nn->clientid_counter;
1818 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1821 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1823 clp->cl_clientid.cl_boot = nn->boot_time;
1824 clp->cl_clientid.cl_id = nn->clientid_counter++;
1825 gen_confirm(clp, nn);
1828 static struct nfs4_stid *
1829 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1831 struct nfs4_stid *ret;
1833 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1834 if (!ret || !ret->sc_type)
1839 static struct nfs4_stid *
1840 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1842 struct nfs4_stid *s;
1844 spin_lock(&cl->cl_lock);
1845 s = find_stateid_locked(cl, t);
1847 if (typemask & s->sc_type)
1848 atomic_inc(&s->sc_count);
1852 spin_unlock(&cl->cl_lock);
1856 static struct nfs4_client *create_client(struct xdr_netobj name,
1857 struct svc_rqst *rqstp, nfs4_verifier *verf)
1859 struct nfs4_client *clp;
1860 struct sockaddr *sa = svc_addr(rqstp);
1862 struct net *net = SVC_NET(rqstp);
1864 clp = alloc_client(name);
1868 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1873 INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
1874 clp->cl_time = get_seconds();
1875 clear_bit(0, &clp->cl_cb_slot_busy);
1876 copy_verf(clp, verf);
1877 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1878 clp->cl_cb_session = NULL;
1884 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1886 struct rb_node **new = &(root->rb_node), *parent = NULL;
1887 struct nfs4_client *clp;
1890 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1893 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1894 new = &((*new)->rb_left);
1896 new = &((*new)->rb_right);
1899 rb_link_node(&new_clp->cl_namenode, parent, new);
1900 rb_insert_color(&new_clp->cl_namenode, root);
1903 static struct nfs4_client *
1904 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1907 struct rb_node *node = root->rb_node;
1908 struct nfs4_client *clp;
1911 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1912 cmp = compare_blob(&clp->cl_name, name);
1914 node = node->rb_left;
1916 node = node->rb_right;
1924 add_to_unconfirmed(struct nfs4_client *clp)
1926 unsigned int idhashval;
1927 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1929 lockdep_assert_held(&nn->client_lock);
1931 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1932 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1933 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1934 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1935 renew_client_locked(clp);
1939 move_to_confirmed(struct nfs4_client *clp)
1941 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1942 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1944 lockdep_assert_held(&nn->client_lock);
1946 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1947 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1948 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1949 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1950 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1951 renew_client_locked(clp);
1954 static struct nfs4_client *
1955 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1957 struct nfs4_client *clp;
1958 unsigned int idhashval = clientid_hashval(clid->cl_id);
1960 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1961 if (same_clid(&clp->cl_clientid, clid)) {
1962 if ((bool)clp->cl_minorversion != sessions)
1964 renew_client_locked(clp);
1971 static struct nfs4_client *
1972 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1974 struct list_head *tbl = nn->conf_id_hashtbl;
1976 lockdep_assert_held(&nn->client_lock);
1977 return find_client_in_id_table(tbl, clid, sessions);
1980 static struct nfs4_client *
1981 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1983 struct list_head *tbl = nn->unconf_id_hashtbl;
1985 lockdep_assert_held(&nn->client_lock);
1986 return find_client_in_id_table(tbl, clid, sessions);
1989 static bool clp_used_exchangeid(struct nfs4_client *clp)
1991 return clp->cl_exchange_flags != 0;
1994 static struct nfs4_client *
1995 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1997 lockdep_assert_held(&nn->client_lock);
1998 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2001 static struct nfs4_client *
2002 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2004 lockdep_assert_held(&nn->client_lock);
2005 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2009 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2011 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2012 struct sockaddr *sa = svc_addr(rqstp);
2013 u32 scopeid = rpc_get_scope_id(sa);
2014 unsigned short expected_family;
2016 /* Currently, we only support tcp and tcp6 for the callback channel */
2017 if (se->se_callback_netid_len == 3 &&
2018 !memcmp(se->se_callback_netid_val, "tcp", 3))
2019 expected_family = AF_INET;
2020 else if (se->se_callback_netid_len == 4 &&
2021 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2022 expected_family = AF_INET6;
2026 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2027 se->se_callback_addr_len,
2028 (struct sockaddr *)&conn->cb_addr,
2029 sizeof(conn->cb_addr));
2031 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2034 if (conn->cb_addr.ss_family == AF_INET6)
2035 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2037 conn->cb_prog = se->se_callback_prog;
2038 conn->cb_ident = se->se_callback_ident;
2039 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2042 conn->cb_addr.ss_family = AF_UNSPEC;
2043 conn->cb_addrlen = 0;
2044 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2045 "will not receive delegations\n",
2046 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2052 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2055 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2057 struct xdr_buf *buf = resp->xdr.buf;
2058 struct nfsd4_slot *slot = resp->cstate.slot;
2061 dprintk("--> %s slot %p\n", __func__, slot);
2063 slot->sl_opcnt = resp->opcnt;
2064 slot->sl_status = resp->cstate.status;
2066 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2067 if (nfsd4_not_cached(resp)) {
2068 slot->sl_datalen = 0;
2071 base = resp->cstate.data_offset;
2072 slot->sl_datalen = buf->len - base;
2073 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2074 WARN("%s: sessions DRC could not cache compound\n", __func__);
2079 * Encode the replay sequence operation from the slot values.
2080 * If cachethis is FALSE encode the uncached rep error on the next
2081 * operation which sets resp->p and increments resp->opcnt for
2082 * nfs4svc_encode_compoundres.
2086 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2087 struct nfsd4_compoundres *resp)
2089 struct nfsd4_op *op;
2090 struct nfsd4_slot *slot = resp->cstate.slot;
2092 /* Encode the replayed sequence operation */
2093 op = &args->ops[resp->opcnt - 1];
2094 nfsd4_encode_operation(resp, op);
2096 /* Return nfserr_retry_uncached_rep in next operation. */
2097 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2098 op = &args->ops[resp->opcnt++];
2099 op->status = nfserr_retry_uncached_rep;
2100 nfsd4_encode_operation(resp, op);
2106 * The sequence operation is not cached because we can use the slot and
2110 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2111 struct nfsd4_sequence *seq)
2113 struct nfsd4_slot *slot = resp->cstate.slot;
2114 struct xdr_stream *xdr = &resp->xdr;
2118 dprintk("--> %s slot %p\n", __func__, slot);
2120 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2124 p = xdr_reserve_space(xdr, slot->sl_datalen);
2127 return nfserr_serverfault;
2129 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2130 xdr_commit_encode(xdr);
2132 resp->opcnt = slot->sl_opcnt;
2133 return slot->sl_status;
2137 * Set the exchange_id flags returned by the server.
2140 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2142 /* pNFS is not supported */
2143 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2145 /* Referrals are supported, Migration is not. */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2148 /* set the wire flags to return to client. */
2149 clid->flags = new->cl_exchange_flags;
2152 static bool client_has_state(struct nfs4_client *clp)
2155 * Note clp->cl_openowners check isn't quite right: there's no
2156 * need to count owners without stateid's.
2158 * Also note we should probably be using this in 4.0 case too.
2160 return !list_empty(&clp->cl_openowners)
2161 || !list_empty(&clp->cl_delegations)
2162 || !list_empty(&clp->cl_sessions);
2166 nfsd4_exchange_id(struct svc_rqst *rqstp,
2167 struct nfsd4_compound_state *cstate,
2168 struct nfsd4_exchange_id *exid)
2170 struct nfs4_client *conf, *new;
2171 struct nfs4_client *unconf = NULL;
2173 char addr_str[INET6_ADDRSTRLEN];
2174 nfs4_verifier verf = exid->verifier;
2175 struct sockaddr *sa = svc_addr(rqstp);
2176 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2177 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2179 rpc_ntop(sa, addr_str, sizeof(addr_str));
2180 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2181 "ip_addr=%s flags %x, spa_how %d\n",
2182 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2183 addr_str, exid->flags, exid->spa_how);
2185 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2186 return nfserr_inval;
2188 switch (exid->spa_how) {
2190 if (!svc_rqst_integrity_protected(rqstp))
2191 return nfserr_inval;
2194 default: /* checked by xdr code */
2197 return nfserr_encr_alg_unsupp;
2200 new = create_client(exid->clname, rqstp, &verf);
2202 return nfserr_jukebox;
2204 /* Cases below refer to rfc 5661 section 18.35.4: */
2205 spin_lock(&nn->client_lock);
2206 conf = find_confirmed_client_by_name(&exid->clname, nn);
2208 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2209 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2212 if (!clp_used_exchangeid(conf)) { /* buggy client */
2213 status = nfserr_inval;
2216 if (!mach_creds_match(conf, rqstp)) {
2217 status = nfserr_wrong_cred;
2220 if (!creds_match) { /* case 9 */
2221 status = nfserr_perm;
2224 if (!verfs_match) { /* case 8 */
2225 status = nfserr_not_same;
2229 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2232 if (!creds_match) { /* case 3 */
2233 if (client_has_state(conf)) {
2234 status = nfserr_clid_inuse;
2239 if (verfs_match) { /* case 2 */
2240 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2243 /* case 5, client reboot */
2248 if (update) { /* case 7 */
2249 status = nfserr_noent;
2253 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2254 if (unconf) /* case 4, possible retry or client restart */
2255 unhash_client_locked(unconf);
2257 /* case 1 (normal case) */
2260 status = mark_client_expired_locked(conf);
2264 new->cl_minorversion = cstate->minorversion;
2265 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2268 add_to_unconfirmed(new);
2271 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2272 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2274 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2275 nfsd4_set_ex_flags(conf, exid);
2277 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2278 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2282 spin_unlock(&nn->client_lock);
2286 expire_client(unconf);
2291 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2293 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2296 /* The slot is in use, and no response has been sent. */
2298 if (seqid == slot_seqid)
2299 return nfserr_jukebox;
2301 return nfserr_seq_misordered;
2303 /* Note unsigned 32-bit arithmetic handles wraparound: */
2304 if (likely(seqid == slot_seqid + 1))
2306 if (seqid == slot_seqid)
2307 return nfserr_replay_cache;
2308 return nfserr_seq_misordered;
2312 * Cache the create session result into the create session single DRC
2313 * slot cache by saving the xdr structure. sl_seqid has been set.
2314 * Do this for solo or embedded create session operations.
2317 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2318 struct nfsd4_clid_slot *slot, __be32 nfserr)
2320 slot->sl_status = nfserr;
2321 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2325 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2326 struct nfsd4_clid_slot *slot)
2328 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2329 return slot->sl_status;
2332 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2333 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2334 1 + /* MIN tag is length with zero, only length */ \
2335 3 + /* version, opcount, opcode */ \
2336 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2337 /* seqid, slotID, slotID, cache */ \
2338 4 ) * sizeof(__be32))
2340 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2341 2 + /* verifier: AUTH_NULL, length 0 */\
2343 1 + /* MIN tag is length with zero, only length */ \
2344 3 + /* opcount, opcode, opstatus*/ \
2345 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2346 /* seqid, slotID, slotID, slotID, status */ \
2347 5 ) * sizeof(__be32))
2349 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2351 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2353 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2354 return nfserr_toosmall;
2355 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2356 return nfserr_toosmall;
2357 ca->headerpadsz = 0;
2358 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2359 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2360 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2361 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2362 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2363 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2365 * Note decreasing slot size below client's request may make it
2366 * difficult for client to function correctly, whereas
2367 * decreasing the number of slots will (just?) affect
2368 * performance. When short on memory we therefore prefer to
2369 * decrease number of slots instead of their size. Clients that
2370 * request larger slots than they need will get poor results:
2372 ca->maxreqs = nfsd4_get_drc_mem(ca);
2374 return nfserr_jukebox;
2379 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2380 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2381 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2382 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2384 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2386 ca->headerpadsz = 0;
2389 * These RPC_MAX_HEADER macros are overkill, especially since we
2390 * don't even do gss on the backchannel yet. But this is still
2391 * less than 1k. Tighten up this estimate in the unlikely event
2392 * it turns out to be a problem for some client:
2394 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2395 return nfserr_toosmall;
2396 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2397 return nfserr_toosmall;
2398 ca->maxresp_cached = 0;
2400 return nfserr_toosmall;
2405 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2407 switch (cbs->flavor) {
2413 * GSS case: the spec doesn't allow us to return this
2414 * error. But it also doesn't allow us not to support
2416 * I'd rather this fail hard than return some error the
2417 * client might think it can already handle:
2419 return nfserr_encr_alg_unsupp;
2424 nfsd4_create_session(struct svc_rqst *rqstp,
2425 struct nfsd4_compound_state *cstate,
2426 struct nfsd4_create_session *cr_ses)
2428 struct sockaddr *sa = svc_addr(rqstp);
2429 struct nfs4_client *conf, *unconf;
2430 struct nfs4_client *old = NULL;
2431 struct nfsd4_session *new;
2432 struct nfsd4_conn *conn;
2433 struct nfsd4_clid_slot *cs_slot = NULL;
2435 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2437 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2438 return nfserr_inval;
2439 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2442 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2445 status = check_backchannel_attrs(&cr_ses->back_channel);
2447 goto out_release_drc_mem;
2448 status = nfserr_jukebox;
2449 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2451 goto out_release_drc_mem;
2452 conn = alloc_conn_from_crses(rqstp, cr_ses);
2454 goto out_free_session;
2456 spin_lock(&nn->client_lock);
2457 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2458 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2459 WARN_ON_ONCE(conf && unconf);
2462 status = nfserr_wrong_cred;
2463 if (!mach_creds_match(conf, rqstp))
2465 cs_slot = &conf->cl_cs_slot;
2466 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2467 if (status == nfserr_replay_cache) {
2468 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2470 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2471 status = nfserr_seq_misordered;
2474 } else if (unconf) {
2475 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2476 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2477 status = nfserr_clid_inuse;
2480 status = nfserr_wrong_cred;
2481 if (!mach_creds_match(unconf, rqstp))
2483 cs_slot = &unconf->cl_cs_slot;
2484 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2486 /* an unconfirmed replay returns misordered */
2487 status = nfserr_seq_misordered;
2490 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2492 status = mark_client_expired_locked(old);
2498 move_to_confirmed(unconf);
2501 status = nfserr_stale_clientid;
2506 * We do not support RDMA or persistent sessions
2508 cr_ses->flags &= ~SESSION4_PERSIST;
2509 cr_ses->flags &= ~SESSION4_RDMA;
2511 init_session(rqstp, new, conf, cr_ses);
2512 nfsd4_get_session_locked(new);
2514 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2515 NFS4_MAX_SESSIONID_LEN);
2516 cs_slot->sl_seqid++;
2517 cr_ses->seqid = cs_slot->sl_seqid;
2519 /* cache solo and embedded create sessions under the client_lock */
2520 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2521 spin_unlock(&nn->client_lock);
2522 /* init connection and backchannel */
2523 nfsd4_init_conn(rqstp, conn, new);
2524 nfsd4_put_session(new);
2529 spin_unlock(&nn->client_lock);
2534 __free_session(new);
2535 out_release_drc_mem:
2536 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2540 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2543 case NFS4_CDFC4_FORE:
2544 case NFS4_CDFC4_BACK:
2546 case NFS4_CDFC4_FORE_OR_BOTH:
2547 case NFS4_CDFC4_BACK_OR_BOTH:
2548 *dir = NFS4_CDFC4_BOTH;
2551 return nfserr_inval;
2554 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2556 struct nfsd4_session *session = cstate->session;
2557 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2560 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2563 spin_lock(&nn->client_lock);
2564 session->se_cb_prog = bc->bc_cb_program;
2565 session->se_cb_sec = bc->bc_cb_sec;
2566 spin_unlock(&nn->client_lock);
2568 nfsd4_probe_callback(session->se_client);
2573 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2574 struct nfsd4_compound_state *cstate,
2575 struct nfsd4_bind_conn_to_session *bcts)
2578 struct nfsd4_conn *conn;
2579 struct nfsd4_session *session;
2580 struct net *net = SVC_NET(rqstp);
2581 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2583 if (!nfsd4_last_compound_op(rqstp))
2584 return nfserr_not_only_op;
2585 spin_lock(&nn->client_lock);
2586 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2587 spin_unlock(&nn->client_lock);
2589 goto out_no_session;
2590 status = nfserr_wrong_cred;
2591 if (!mach_creds_match(session->se_client, rqstp))
2593 status = nfsd4_map_bcts_dir(&bcts->dir);
2596 conn = alloc_conn(rqstp, bcts->dir);
2597 status = nfserr_jukebox;
2600 nfsd4_init_conn(rqstp, conn, session);
2603 nfsd4_put_session(session);
2608 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2612 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2616 nfsd4_destroy_session(struct svc_rqst *r,
2617 struct nfsd4_compound_state *cstate,
2618 struct nfsd4_destroy_session *sessionid)
2620 struct nfsd4_session *ses;
2622 int ref_held_by_me = 0;
2623 struct net *net = SVC_NET(r);
2624 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2626 status = nfserr_not_only_op;
2627 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2628 if (!nfsd4_last_compound_op(r))
2632 dump_sessionid(__func__, &sessionid->sessionid);
2633 spin_lock(&nn->client_lock);
2634 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2636 goto out_client_lock;
2637 status = nfserr_wrong_cred;
2638 if (!mach_creds_match(ses->se_client, r))
2639 goto out_put_session;
2640 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2642 goto out_put_session;
2643 unhash_session(ses);
2644 spin_unlock(&nn->client_lock);
2646 nfsd4_probe_callback_sync(ses->se_client);
2648 spin_lock(&nn->client_lock);
2651 nfsd4_put_session_locked(ses);
2653 spin_unlock(&nn->client_lock);
2658 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2660 struct nfsd4_conn *c;
2662 list_for_each_entry(c, &s->se_conns, cn_persession) {
2663 if (c->cn_xprt == xpt) {
2670 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2672 struct nfs4_client *clp = ses->se_client;
2673 struct nfsd4_conn *c;
2674 __be32 status = nfs_ok;
2677 spin_lock(&clp->cl_lock);
2678 c = __nfsd4_find_conn(new->cn_xprt, ses);
2681 status = nfserr_conn_not_bound_to_session;
2682 if (clp->cl_mach_cred)
2684 __nfsd4_hash_conn(new, ses);
2685 spin_unlock(&clp->cl_lock);
2686 ret = nfsd4_register_conn(new);
2688 /* oops; xprt is already down: */
2689 nfsd4_conn_lost(&new->cn_xpt_user);
2692 spin_unlock(&clp->cl_lock);
2697 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2699 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2701 return args->opcnt > session->se_fchannel.maxops;
2704 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2705 struct nfsd4_session *session)
2707 struct xdr_buf *xb = &rqstp->rq_arg;
2709 return xb->len > session->se_fchannel.maxreq_sz;
2713 nfsd4_sequence(struct svc_rqst *rqstp,
2714 struct nfsd4_compound_state *cstate,
2715 struct nfsd4_sequence *seq)
2717 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2718 struct xdr_stream *xdr = &resp->xdr;
2719 struct nfsd4_session *session;
2720 struct nfs4_client *clp;
2721 struct nfsd4_slot *slot;
2722 struct nfsd4_conn *conn;
2725 struct net *net = SVC_NET(rqstp);
2726 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2728 if (resp->opcnt != 1)
2729 return nfserr_sequence_pos;
2732 * Will be either used or freed by nfsd4_sequence_check_conn
2735 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2737 return nfserr_jukebox;
2739 spin_lock(&nn->client_lock);
2740 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2742 goto out_no_session;
2743 clp = session->se_client;
2745 status = nfserr_too_many_ops;
2746 if (nfsd4_session_too_many_ops(rqstp, session))
2747 goto out_put_session;
2749 status = nfserr_req_too_big;
2750 if (nfsd4_request_too_big(rqstp, session))
2751 goto out_put_session;
2753 status = nfserr_badslot;
2754 if (seq->slotid >= session->se_fchannel.maxreqs)
2755 goto out_put_session;
2757 slot = session->se_slots[seq->slotid];
2758 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2760 /* We do not negotiate the number of slots yet, so set the
2761 * maxslots to the session maxreqs which is used to encode
2762 * sr_highest_slotid and the sr_target_slot id to maxslots */
2763 seq->maxslots = session->se_fchannel.maxreqs;
2765 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2766 slot->sl_flags & NFSD4_SLOT_INUSE);
2767 if (status == nfserr_replay_cache) {
2768 status = nfserr_seq_misordered;
2769 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2770 goto out_put_session;
2771 cstate->slot = slot;
2772 cstate->session = session;
2774 /* Return the cached reply status and set cstate->status
2775 * for nfsd4_proc_compound processing */
2776 status = nfsd4_replay_cache_entry(resp, seq);
2777 cstate->status = nfserr_replay_cache;
2781 goto out_put_session;
2783 status = nfsd4_sequence_check_conn(conn, session);
2786 goto out_put_session;
2788 buflen = (seq->cachethis) ?
2789 session->se_fchannel.maxresp_cached :
2790 session->se_fchannel.maxresp_sz;
2791 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2793 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2794 goto out_put_session;
2795 svc_reserve(rqstp, buflen);
2798 /* Success! bump slot seqid */
2799 slot->sl_seqid = seq->seqid;
2800 slot->sl_flags |= NFSD4_SLOT_INUSE;
2802 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2804 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2806 cstate->slot = slot;
2807 cstate->session = session;
2811 switch (clp->cl_cb_state) {
2813 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2815 case NFSD4_CB_FAULT:
2816 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2819 seq->status_flags = 0;
2821 if (!list_empty(&clp->cl_revoked))
2822 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2826 spin_unlock(&nn->client_lock);
2829 nfsd4_put_session_locked(session);
2830 goto out_no_session;
2834 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2836 struct nfsd4_compound_state *cs = &resp->cstate;
2838 if (nfsd4_has_session(cs)) {
2839 if (cs->status != nfserr_replay_cache) {
2840 nfsd4_store_cache_entry(resp);
2841 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2843 /* Drop session reference that was taken in nfsd4_sequence() */
2844 nfsd4_put_session(cs->session);
2846 put_client_renew(cs->clp);
2850 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2852 struct nfs4_client *conf, *unconf;
2853 struct nfs4_client *clp = NULL;
2855 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2857 spin_lock(&nn->client_lock);
2858 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2859 conf = find_confirmed_client(&dc->clientid, true, nn);
2860 WARN_ON_ONCE(conf && unconf);
2863 if (client_has_state(conf)) {
2864 status = nfserr_clientid_busy;
2867 status = mark_client_expired_locked(conf);
2874 status = nfserr_stale_clientid;
2877 if (!mach_creds_match(clp, rqstp)) {
2879 status = nfserr_wrong_cred;
2882 unhash_client_locked(clp);
2884 spin_unlock(&nn->client_lock);
2891 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2895 if (rc->rca_one_fs) {
2896 if (!cstate->current_fh.fh_dentry)
2897 return nfserr_nofilehandle;
2899 * We don't take advantage of the rca_one_fs case.
2900 * That's OK, it's optional, we can safely ignore it.
2905 status = nfserr_complete_already;
2906 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2907 &cstate->session->se_client->cl_flags))
2910 status = nfserr_stale_clientid;
2911 if (is_client_expired(cstate->session->se_client))
2913 * The following error isn't really legal.
2914 * But we only get here if the client just explicitly
2915 * destroyed the client. Surely it no longer cares what
2916 * error it gets back on an operation for the dead
2922 nfsd4_client_record_create(cstate->session->se_client);
2928 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2929 struct nfsd4_setclientid *setclid)
2931 struct xdr_netobj clname = setclid->se_name;
2932 nfs4_verifier clverifier = setclid->se_verf;
2933 struct nfs4_client *conf, *new;
2934 struct nfs4_client *unconf = NULL;
2936 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2938 new = create_client(clname, rqstp, &clverifier);
2940 return nfserr_jukebox;
2941 /* Cases below refer to rfc 3530 section 14.2.33: */
2942 spin_lock(&nn->client_lock);
2943 conf = find_confirmed_client_by_name(&clname, nn);
2946 status = nfserr_clid_inuse;
2947 if (clp_used_exchangeid(conf))
2949 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2950 char addr_str[INET6_ADDRSTRLEN];
2951 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2953 dprintk("NFSD: setclientid: string in use by client "
2954 "at %s\n", addr_str);
2958 unconf = find_unconfirmed_client_by_name(&clname, nn);
2960 unhash_client_locked(unconf);
2961 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2962 /* case 1: probable callback update */
2963 copy_clid(new, conf);
2964 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2966 new->cl_minorversion = 0;
2967 gen_callback(new, setclid, rqstp);
2968 add_to_unconfirmed(new);
2969 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2970 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2971 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2975 spin_unlock(&nn->client_lock);
2979 expire_client(unconf);
2985 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2986 struct nfsd4_compound_state *cstate,
2987 struct nfsd4_setclientid_confirm *setclientid_confirm)
2989 struct nfs4_client *conf, *unconf;
2990 struct nfs4_client *old = NULL;
2991 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2992 clientid_t * clid = &setclientid_confirm->sc_clientid;
2994 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2996 if (STALE_CLIENTID(clid, nn))
2997 return nfserr_stale_clientid;
2999 spin_lock(&nn->client_lock);
3000 conf = find_confirmed_client(clid, false, nn);
3001 unconf = find_unconfirmed_client(clid, false, nn);
3003 * We try hard to give out unique clientid's, so if we get an
3004 * attempt to confirm the same clientid with a different cred,
3005 * there's a bug somewhere. Let's charitably assume it's our
3008 status = nfserr_serverfault;
3009 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3011 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3013 /* cases below refer to rfc 3530 section 14.2.34: */
3014 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3015 if (conf && !unconf) /* case 2: probable retransmit */
3017 else /* case 4: client hasn't noticed we rebooted yet? */
3018 status = nfserr_stale_clientid;
3022 if (conf) { /* case 1: callback update */
3024 unhash_client_locked(old);
3025 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3026 } else { /* case 3: normal case; new or rebooted client */
3027 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3029 status = mark_client_expired_locked(old);
3035 move_to_confirmed(unconf);
3038 get_client_locked(conf);
3039 spin_unlock(&nn->client_lock);
3040 nfsd4_probe_callback(conf);
3041 spin_lock(&nn->client_lock);
3042 put_client_renew_locked(conf);
3044 spin_unlock(&nn->client_lock);
3050 static struct nfs4_file *nfsd4_alloc_file(void)
3052 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3055 /* OPEN Share state helper functions */
3056 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3058 unsigned int hashval = file_hashval(fh);
3060 lockdep_assert_held(&state_lock);
3062 atomic_set(&fp->fi_ref, 1);
3063 spin_lock_init(&fp->fi_lock);
3064 INIT_LIST_HEAD(&fp->fi_stateids);
3065 INIT_LIST_HEAD(&fp->fi_delegations);
3066 fh_copy_shallow(&fp->fi_fhandle, fh);
3067 fp->fi_deleg_file = NULL;
3068 fp->fi_had_conflict = false;
3069 fp->fi_share_deny = 0;
3070 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3071 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3072 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3076 nfsd4_free_slabs(void)
3078 kmem_cache_destroy(openowner_slab);
3079 kmem_cache_destroy(lockowner_slab);
3080 kmem_cache_destroy(file_slab);
3081 kmem_cache_destroy(stateid_slab);
3082 kmem_cache_destroy(deleg_slab);
3086 nfsd4_init_slabs(void)
3088 openowner_slab = kmem_cache_create("nfsd4_openowners",
3089 sizeof(struct nfs4_openowner), 0, 0, NULL);
3090 if (openowner_slab == NULL)
3092 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3093 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3094 if (lockowner_slab == NULL)
3095 goto out_free_openowner_slab;
3096 file_slab = kmem_cache_create("nfsd4_files",
3097 sizeof(struct nfs4_file), 0, 0, NULL);
3098 if (file_slab == NULL)
3099 goto out_free_lockowner_slab;
3100 stateid_slab = kmem_cache_create("nfsd4_stateids",
3101 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3102 if (stateid_slab == NULL)
3103 goto out_free_file_slab;
3104 deleg_slab = kmem_cache_create("nfsd4_delegations",
3105 sizeof(struct nfs4_delegation), 0, 0, NULL);
3106 if (deleg_slab == NULL)
3107 goto out_free_stateid_slab;
3110 out_free_stateid_slab:
3111 kmem_cache_destroy(stateid_slab);
3113 kmem_cache_destroy(file_slab);
3114 out_free_lockowner_slab:
3115 kmem_cache_destroy(lockowner_slab);
3116 out_free_openowner_slab:
3117 kmem_cache_destroy(openowner_slab);
3119 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3123 static void init_nfs4_replay(struct nfs4_replay *rp)
3125 rp->rp_status = nfserr_serverfault;
3127 rp->rp_buf = rp->rp_ibuf;
3128 mutex_init(&rp->rp_mutex);
3131 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3132 struct nfs4_stateowner *so)
3134 if (!nfsd4_has_session(cstate)) {
3135 mutex_lock(&so->so_replay.rp_mutex);
3136 cstate->replay_owner = nfs4_get_stateowner(so);
3140 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3142 struct nfs4_stateowner *so = cstate->replay_owner;
3145 cstate->replay_owner = NULL;
3146 mutex_unlock(&so->so_replay.rp_mutex);
3147 nfs4_put_stateowner(so);
3151 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3153 struct nfs4_stateowner *sop;
3155 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3159 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3160 if (!sop->so_owner.data) {
3161 kmem_cache_free(slab, sop);
3164 sop->so_owner.len = owner->len;
3166 INIT_LIST_HEAD(&sop->so_stateids);
3167 sop->so_client = clp;
3168 init_nfs4_replay(&sop->so_replay);
3169 atomic_set(&sop->so_count, 1);
3173 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3175 lockdep_assert_held(&clp->cl_lock);
3177 list_add(&oo->oo_owner.so_strhash,
3178 &clp->cl_ownerstr_hashtbl[strhashval]);
3179 list_add(&oo->oo_perclient, &clp->cl_openowners);
3182 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3184 unhash_openowner_locked(openowner(so));
3187 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3189 struct nfs4_openowner *oo = openowner(so);
3191 kmem_cache_free(openowner_slab, oo);
3194 static const struct nfs4_stateowner_operations openowner_ops = {
3195 .so_unhash = nfs4_unhash_openowner,
3196 .so_free = nfs4_free_openowner,
3199 static struct nfs4_openowner *
3200 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3201 struct nfsd4_compound_state *cstate)
3203 struct nfs4_client *clp = cstate->clp;
3204 struct nfs4_openowner *oo, *ret;
3206 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3209 oo->oo_owner.so_ops = &openowner_ops;
3210 oo->oo_owner.so_is_open_owner = 1;
3211 oo->oo_owner.so_seqid = open->op_seqid;
3213 if (nfsd4_has_session(cstate))
3214 oo->oo_flags |= NFS4_OO_CONFIRMED;
3216 oo->oo_last_closed_stid = NULL;
3217 INIT_LIST_HEAD(&oo->oo_close_lru);
3218 spin_lock(&clp->cl_lock);
3219 ret = find_openstateowner_str_locked(strhashval, open, clp);
3221 hash_openowner(oo, clp, strhashval);
3224 nfs4_free_openowner(&oo->oo_owner);
3225 spin_unlock(&clp->cl_lock);
3229 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3230 struct nfs4_openowner *oo = open->op_openowner;
3232 atomic_inc(&stp->st_stid.sc_count);
3233 stp->st_stid.sc_type = NFS4_OPEN_STID;
3234 INIT_LIST_HEAD(&stp->st_locks);
3235 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3237 stp->st_stid.sc_file = fp;
3238 stp->st_access_bmap = 0;
3239 stp->st_deny_bmap = 0;
3240 stp->st_openstp = NULL;
3241 spin_lock(&oo->oo_owner.so_client->cl_lock);
3242 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3243 spin_lock(&fp->fi_lock);
3244 list_add(&stp->st_perfile, &fp->fi_stateids);
3245 spin_unlock(&fp->fi_lock);
3246 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3250 * In the 4.0 case we need to keep the owners around a little while to handle
3251 * CLOSE replay. We still do need to release any file access that is held by
3252 * them before returning however.
3255 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3257 struct nfs4_ol_stateid *last;
3258 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3259 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3262 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3265 * We know that we hold one reference via nfsd4_close, and another
3266 * "persistent" reference for the client. If the refcount is higher
3267 * than 2, then there are still calls in progress that are using this
3268 * stateid. We can't put the sc_file reference until they are finished.
3269 * Wait for the refcount to drop to 2. Since it has been unhashed,
3270 * there should be no danger of the refcount going back up again at
3273 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3275 release_all_access(s);
3276 if (s->st_stid.sc_file) {
3277 put_nfs4_file(s->st_stid.sc_file);
3278 s->st_stid.sc_file = NULL;
3281 spin_lock(&nn->client_lock);
3282 last = oo->oo_last_closed_stid;
3283 oo->oo_last_closed_stid = s;
3284 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3285 oo->oo_time = get_seconds();
3286 spin_unlock(&nn->client_lock);
3288 nfs4_put_stid(&last->st_stid);
3291 /* search file_hashtbl[] for file */
3292 static struct nfs4_file *
3293 find_file_locked(struct knfsd_fh *fh)
3295 unsigned int hashval = file_hashval(fh);
3296 struct nfs4_file *fp;
3298 lockdep_assert_held(&state_lock);
3300 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3301 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3309 static struct nfs4_file *
3310 find_file(struct knfsd_fh *fh)
3312 struct nfs4_file *fp;
3314 spin_lock(&state_lock);
3315 fp = find_file_locked(fh);
3316 spin_unlock(&state_lock);
3320 static struct nfs4_file *
3321 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3323 struct nfs4_file *fp;
3325 spin_lock(&state_lock);
3326 fp = find_file_locked(fh);
3328 nfsd4_init_file(new, fh);
3331 spin_unlock(&state_lock);
3337 * Called to check deny when READ with all zero stateid or
3338 * WRITE with all zero or all one stateid
3341 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3343 struct nfs4_file *fp;
3344 __be32 ret = nfs_ok;
3346 fp = find_file(¤t_fh->fh_handle);
3349 /* Check for conflicting share reservations */
3350 spin_lock(&fp->fi_lock);
3351 if (fp->fi_share_deny & deny_type)
3352 ret = nfserr_locked;
3353 spin_unlock(&fp->fi_lock);
3358 void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
3360 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3363 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3366 * We can't do this in nfsd_break_deleg_cb because it is
3367 * already holding inode->i_lock.
3369 * If the dl_time != 0, then we know that it has already been
3370 * queued for a lease break. Don't queue it again.
3372 spin_lock(&state_lock);
3373 if (dp->dl_time == 0) {
3374 dp->dl_time = get_seconds();
3375 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3377 spin_unlock(&state_lock);
3380 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3383 * We're assuming the state code never drops its reference
3384 * without first removing the lease. Since we're in this lease
3385 * callback (and since the lease code is serialized by the kernel
3386 * lock) we know the server hasn't removed the lease yet, we know
3387 * it's safe to take a reference.
3389 atomic_inc(&dp->dl_stid.sc_count);
3390 nfsd4_cb_recall(dp);
3393 /* Called from break_lease() with i_lock held. */
3394 static void nfsd_break_deleg_cb(struct file_lock *fl)
3396 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3397 struct nfs4_delegation *dp;
3400 WARN(1, "(%p)->fl_owner NULL\n", fl);
3403 if (fp->fi_had_conflict) {
3404 WARN(1, "duplicate break on %p\n", fp);
3408 * We don't want the locks code to timeout the lease for us;
3409 * we'll remove it ourself if a delegation isn't returned
3412 fl->fl_break_time = 0;
3414 spin_lock(&fp->fi_lock);
3415 fp->fi_had_conflict = true;
3417 * If there are no delegations on the list, then we can't count on this
3418 * lease ever being cleaned up. Set the fl_break_time to jiffies so that
3419 * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
3420 * true should keep any new delegations from being hashed.
3422 if (list_empty(&fp->fi_delegations))
3423 fl->fl_break_time = jiffies;
3425 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3426 nfsd_break_one_deleg(dp);
3427 spin_unlock(&fp->fi_lock);
3431 nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
3434 return lease_modify(onlist, arg, dispose);
3439 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3440 .lm_break = nfsd_break_deleg_cb,
3441 .lm_change = nfsd_change_deleg_cb,
3444 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3446 if (nfsd4_has_session(cstate))
3448 if (seqid == so->so_seqid - 1)
3449 return nfserr_replay_me;
3450 if (seqid == so->so_seqid)
3452 return nfserr_bad_seqid;
3455 static __be32 lookup_clientid(clientid_t *clid,
3456 struct nfsd4_compound_state *cstate,
3457 struct nfsd_net *nn)
3459 struct nfs4_client *found;
3462 found = cstate->clp;
3463 if (!same_clid(&found->cl_clientid, clid))
3464 return nfserr_stale_clientid;
3468 if (STALE_CLIENTID(clid, nn))
3469 return nfserr_stale_clientid;
3472 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3473 * cached already then we know this is for is for v4.0 and "sessions"
3476 WARN_ON_ONCE(cstate->session);
3477 spin_lock(&nn->client_lock);
3478 found = find_confirmed_client(clid, false, nn);
3480 spin_unlock(&nn->client_lock);
3481 return nfserr_expired;
3483 atomic_inc(&found->cl_refcount);
3484 spin_unlock(&nn->client_lock);
3486 /* Cache the nfs4_client in cstate! */
3487 cstate->clp = found;
3492 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3493 struct nfsd4_open *open, struct nfsd_net *nn)
3495 clientid_t *clientid = &open->op_clientid;
3496 struct nfs4_client *clp = NULL;
3497 unsigned int strhashval;
3498 struct nfs4_openowner *oo = NULL;
3501 if (STALE_CLIENTID(&open->op_clientid, nn))
3502 return nfserr_stale_clientid;
3504 * In case we need it later, after we've already created the
3505 * file and don't want to risk a further failure:
3507 open->op_file = nfsd4_alloc_file();
3508 if (open->op_file == NULL)
3509 return nfserr_jukebox;
3511 status = lookup_clientid(clientid, cstate, nn);
3516 strhashval = ownerstr_hashval(&open->op_owner);
3517 oo = find_openstateowner_str(strhashval, open, clp);
3518 open->op_openowner = oo;
3522 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3523 /* Replace unconfirmed owners without checking for replay. */
3524 release_openowner(oo);
3525 open->op_openowner = NULL;
3528 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3533 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3535 return nfserr_jukebox;
3536 open->op_openowner = oo;
3538 open->op_stp = nfs4_alloc_open_stateid(clp);
3540 return nfserr_jukebox;
3544 static inline __be32
3545 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3547 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3548 return nfserr_openmode;
3553 static int share_access_to_flags(u32 share_access)
3555 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3558 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3560 struct nfs4_stid *ret;
3562 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3565 return delegstateid(ret);
3568 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3570 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3571 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3575 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3576 struct nfs4_delegation **dp)
3579 __be32 status = nfserr_bad_stateid;
3580 struct nfs4_delegation *deleg;
3582 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3585 flags = share_access_to_flags(open->op_share_access);
3586 status = nfs4_check_delegmode(deleg, flags);
3588 nfs4_put_stid(&deleg->dl_stid);
3593 if (!nfsd4_is_deleg_cur(open))
3597 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3601 static struct nfs4_ol_stateid *
3602 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3604 struct nfs4_ol_stateid *local, *ret = NULL;
3605 struct nfs4_openowner *oo = open->op_openowner;
3607 spin_lock(&fp->fi_lock);
3608 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3609 /* ignore lock owners */
3610 if (local->st_stateowner->so_is_open_owner == 0)
3612 if (local->st_stateowner == &oo->oo_owner) {
3614 atomic_inc(&ret->st_stid.sc_count);
3618 spin_unlock(&fp->fi_lock);
3622 static inline int nfs4_access_to_access(u32 nfs4_access)
3626 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3627 flags |= NFSD_MAY_READ;
3628 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3629 flags |= NFSD_MAY_WRITE;
3633 static inline __be32
3634 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3635 struct nfsd4_open *open)
3637 struct iattr iattr = {
3638 .ia_valid = ATTR_SIZE,
3641 if (!open->op_truncate)
3643 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3644 return nfserr_inval;
3645 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3648 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3649 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3650 struct nfsd4_open *open)
3652 struct file *filp = NULL;
3654 int oflag = nfs4_access_to_omode(open->op_share_access);
3655 int access = nfs4_access_to_access(open->op_share_access);
3656 unsigned char old_access_bmap, old_deny_bmap;
3658 spin_lock(&fp->fi_lock);
3661 * Are we trying to set a deny mode that would conflict with
3664 status = nfs4_file_check_deny(fp, open->op_share_deny);
3665 if (status != nfs_ok) {
3666 spin_unlock(&fp->fi_lock);
3670 /* set access to the file */
3671 status = nfs4_file_get_access(fp, open->op_share_access);
3672 if (status != nfs_ok) {
3673 spin_unlock(&fp->fi_lock);
3677 /* Set access bits in stateid */
3678 old_access_bmap = stp->st_access_bmap;
3679 set_access(open->op_share_access, stp);
3681 /* Set new deny mask */
3682 old_deny_bmap = stp->st_deny_bmap;
3683 set_deny(open->op_share_deny, stp);
3684 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3686 if (!fp->fi_fds[oflag]) {
3687 spin_unlock(&fp->fi_lock);
3688 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3690 goto out_put_access;
3691 spin_lock(&fp->fi_lock);
3692 if (!fp->fi_fds[oflag]) {
3693 fp->fi_fds[oflag] = filp;
3697 spin_unlock(&fp->fi_lock);
3701 status = nfsd4_truncate(rqstp, cur_fh, open);
3703 goto out_put_access;
3707 stp->st_access_bmap = old_access_bmap;
3708 nfs4_file_put_access(fp, open->op_share_access);
3709 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3714 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)
3717 unsigned char old_deny_bmap;
3719 if (!test_access(open->op_share_access, stp))
3720 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3722 /* test and set deny mode */
3723 spin_lock(&fp->fi_lock);
3724 status = nfs4_file_check_deny(fp, open->op_share_deny);
3725 if (status == nfs_ok) {
3726 old_deny_bmap = stp->st_deny_bmap;
3727 set_deny(open->op_share_deny, stp);
3728 fp->fi_share_deny |=
3729 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3731 spin_unlock(&fp->fi_lock);
3733 if (status != nfs_ok)
3736 status = nfsd4_truncate(rqstp, cur_fh, open);
3737 if (status != nfs_ok)
3738 reset_union_bmap_deny(old_deny_bmap, stp);
3743 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3745 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3748 /* Should we give out recallable state?: */
3749 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3751 if (clp->cl_cb_state == NFSD4_CB_UP)
3754 * In the sessions case, since we don't have to establish a
3755 * separate connection for callbacks, we assume it's OK
3756 * until we hear otherwise:
3758 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3761 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3763 struct file_lock *fl;
3765 fl = locks_alloc_lock();
3768 fl->fl_lmops = &nfsd_lease_mng_ops;
3769 fl->fl_flags = FL_DELEG;
3770 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3771 fl->fl_end = OFFSET_MAX;
3772 fl->fl_owner = (fl_owner_t)fp;
3773 fl->fl_pid = current->tgid;
3777 static int nfs4_setlease(struct nfs4_delegation *dp)
3779 struct nfs4_file *fp = dp->dl_stid.sc_file;
3780 struct file_lock *fl, *ret;
3784 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3787 filp = find_readable_file(fp);
3789 /* We should always have a readable file here */
3795 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3797 locks_free_lock(fl);
3800 spin_lock(&state_lock);
3801 spin_lock(&fp->fi_lock);
3802 /* Did the lease get broken before we took the lock? */
3804 if (fp->fi_had_conflict)
3807 if (fp->fi_deleg_file) {
3809 atomic_inc(&fp->fi_delegees);
3810 hash_delegation_locked(dp, fp);
3813 fp->fi_deleg_file = filp;
3814 atomic_set(&fp->fi_delegees, 1);
3815 hash_delegation_locked(dp, fp);
3816 spin_unlock(&fp->fi_lock);
3817 spin_unlock(&state_lock);
3820 spin_unlock(&fp->fi_lock);
3821 spin_unlock(&state_lock);
3827 static struct nfs4_delegation *
3828 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3829 struct nfs4_file *fp)
3832 struct nfs4_delegation *dp;
3834 if (fp->fi_had_conflict)
3835 return ERR_PTR(-EAGAIN);
3837 dp = alloc_init_deleg(clp, fh);
3839 return ERR_PTR(-ENOMEM);
3842 spin_lock(&state_lock);
3843 spin_lock(&fp->fi_lock);
3844 dp->dl_stid.sc_file = fp;
3845 if (!fp->fi_deleg_file) {
3846 spin_unlock(&fp->fi_lock);
3847 spin_unlock(&state_lock);
3848 status = nfs4_setlease(dp);
3851 atomic_inc(&fp->fi_delegees);
3852 if (fp->fi_had_conflict) {
3856 hash_delegation_locked(dp, fp);
3859 spin_unlock(&fp->fi_lock);
3860 spin_unlock(&state_lock);
3863 nfs4_put_stid(&dp->dl_stid);
3864 return ERR_PTR(status);
3869 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3871 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3872 if (status == -EAGAIN)
3873 open->op_why_no_deleg = WND4_CONTENTION;
3875 open->op_why_no_deleg = WND4_RESOURCE;
3876 switch (open->op_deleg_want) {
3877 case NFS4_SHARE_WANT_READ_DELEG:
3878 case NFS4_SHARE_WANT_WRITE_DELEG:
3879 case NFS4_SHARE_WANT_ANY_DELEG:
3881 case NFS4_SHARE_WANT_CANCEL:
3882 open->op_why_no_deleg = WND4_CANCELLED;
3884 case NFS4_SHARE_WANT_NO_DELEG:
3891 * Attempt to hand out a delegation.
3893 * Note we don't support write delegations, and won't until the vfs has
3894 * proper support for them.
3897 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3898 struct nfs4_ol_stateid *stp)
3900 struct nfs4_delegation *dp;
3901 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3902 struct nfs4_client *clp = stp->st_stid.sc_client;
3906 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3907 open->op_recall = 0;
3908 switch (open->op_claim_type) {
3909 case NFS4_OPEN_CLAIM_PREVIOUS:
3911 open->op_recall = 1;
3912 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3915 case NFS4_OPEN_CLAIM_NULL:
3916 case NFS4_OPEN_CLAIM_FH:
3918 * Let's not give out any delegations till everyone's
3919 * had the chance to reclaim theirs....
3921 if (locks_in_grace(clp->net))
3923 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3926 * Also, if the file was opened for write or
3927 * create, there's a good chance the client's
3928 * about to write to it, resulting in an
3929 * immediate recall (since we don't support
3930 * write delegations):
3932 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3934 if (open->op_create == NFS4_OPEN_CREATE)
3940 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3944 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3946 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3947 STATEID_VAL(&dp->dl_stid.sc_stateid));
3948 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3949 nfs4_put_stid(&dp->dl_stid);
3952 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3953 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3954 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3955 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3956 open->op_recall = 1;
3959 /* 4.1 client asking for a delegation? */
3960 if (open->op_deleg_want)
3961 nfsd4_open_deleg_none_ext(open, status);
3965 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3966 struct nfs4_delegation *dp)
3968 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3969 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3970 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3971 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3972 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3973 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3974 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3975 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3977 /* Otherwise the client must be confused wanting a delegation
3978 * it already has, therefore we don't return
3979 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3984 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3986 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3987 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3988 struct nfs4_file *fp = NULL;
3989 struct nfs4_ol_stateid *stp = NULL;
3990 struct nfs4_delegation *dp = NULL;
3994 * Lookup file; if found, lookup stateid and check open request,
3995 * and check for delegations in the process of being recalled.
3996 * If not found, create the nfs4_file struct
3998 fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle);
3999 if (fp != open->op_file) {
4000 status = nfs4_check_deleg(cl, open, &dp);
4003 stp = nfsd4_find_existing_open(fp, open);
4005 open->op_file = NULL;
4006 status = nfserr_bad_stateid;
4007 if (nfsd4_is_deleg_cur(open))
4009 status = nfserr_jukebox;
4013 * OPEN the file, or upgrade an existing OPEN.
4014 * If truncate fails, the OPEN fails.
4017 /* Stateid was found, this is an OPEN upgrade */
4018 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4023 open->op_stp = NULL;
4024 init_open_stateid(stp, fp, open);
4025 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4027 release_open_stateid(stp);
4031 update_stateid(&stp->st_stid.sc_stateid);
4032 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4034 if (nfsd4_has_session(&resp->cstate)) {
4035 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4036 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4037 open->op_why_no_deleg = WND4_NOT_WANTED;
4043 * Attempt to hand out a delegation. No error return, because the
4044 * OPEN succeeds even if we fail.
4046 nfs4_open_delegation(current_fh, open, stp);
4050 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4051 STATEID_VAL(&stp->st_stid.sc_stateid));
4053 /* 4.1 client trying to upgrade/downgrade delegation? */
4054 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4055 open->op_deleg_want)
4056 nfsd4_deleg_xgrade_none_ext(open, dp);
4060 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4061 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4063 * To finish the open response, we just need to set the rflags.
4065 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4066 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4067 !nfsd4_has_session(&resp->cstate))
4068 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4070 nfs4_put_stid(&dp->dl_stid);
4072 nfs4_put_stid(&stp->st_stid);
4077 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4078 struct nfsd4_open *open, __be32 status)
4080 if (open->op_openowner) {
4081 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4083 nfsd4_cstate_assign_replay(cstate, so);
4084 nfs4_put_stateowner(so);
4087 nfsd4_free_file(open->op_file);
4089 nfs4_put_stid(&open->op_stp->st_stid);
4093 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4096 struct nfs4_client *clp;
4098 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4100 dprintk("process_renew(%08x/%08x): starting\n",
4101 clid->cl_boot, clid->cl_id);
4102 status = lookup_clientid(clid, cstate, nn);
4106 status = nfserr_cb_path_down;
4107 if (!list_empty(&clp->cl_delegations)
4108 && clp->cl_cb_state != NFSD4_CB_UP)
4116 nfsd4_end_grace(struct nfsd_net *nn)
4118 /* do nothing if grace period already ended */
4119 if (nn->grace_ended)
4122 dprintk("NFSD: end of grace period\n");
4123 nn->grace_ended = true;
4124 nfsd4_record_grace_done(nn, nn->boot_time);
4125 locks_end_grace(&nn->nfsd4_manager);
4127 * Now that every NFSv4 client has had the chance to recover and
4128 * to see the (possibly new, possibly shorter) lease time, we
4129 * can safely set the next grace time to the current lease time:
4131 nn->nfsd4_grace = nn->nfsd4_lease;
4135 nfs4_laundromat(struct nfsd_net *nn)
4137 struct nfs4_client *clp;
4138 struct nfs4_openowner *oo;
4139 struct nfs4_delegation *dp;
4140 struct nfs4_ol_stateid *stp;
4141 struct list_head *pos, *next, reaplist;
4142 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4143 time_t t, new_timeo = nn->nfsd4_lease;
4145 dprintk("NFSD: laundromat service - starting\n");
4146 nfsd4_end_grace(nn);
4147 INIT_LIST_HEAD(&reaplist);
4148 spin_lock(&nn->client_lock);
4149 list_for_each_safe(pos, next, &nn->client_lru) {
4150 clp = list_entry(pos, struct nfs4_client, cl_lru);
4151 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4152 t = clp->cl_time - cutoff;
4153 new_timeo = min(new_timeo, t);
4156 if (mark_client_expired_locked(clp)) {
4157 dprintk("NFSD: client in use (clientid %08x)\n",
4158 clp->cl_clientid.cl_id);
4161 list_add(&clp->cl_lru, &reaplist);
4163 spin_unlock(&nn->client_lock);
4164 list_for_each_safe(pos, next, &reaplist) {
4165 clp = list_entry(pos, struct nfs4_client, cl_lru);
4166 dprintk("NFSD: purging unused client (clientid %08x)\n",
4167 clp->cl_clientid.cl_id);
4168 list_del_init(&clp->cl_lru);
4171 spin_lock(&state_lock);
4172 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4173 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4174 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4176 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4177 t = dp->dl_time - cutoff;
4178 new_timeo = min(new_timeo, t);
4181 unhash_delegation_locked(dp);
4182 list_add(&dp->dl_recall_lru, &reaplist);
4184 spin_unlock(&state_lock);
4185 while (!list_empty(&reaplist)) {
4186 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4188 list_del_init(&dp->dl_recall_lru);
4189 revoke_delegation(dp);
4192 spin_lock(&nn->client_lock);
4193 while (!list_empty(&nn->close_lru)) {
4194 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4196 if (time_after((unsigned long)oo->oo_time,
4197 (unsigned long)cutoff)) {
4198 t = oo->oo_time - cutoff;
4199 new_timeo = min(new_timeo, t);
4202 list_del_init(&oo->oo_close_lru);
4203 stp = oo->oo_last_closed_stid;
4204 oo->oo_last_closed_stid = NULL;
4205 spin_unlock(&nn->client_lock);
4206 nfs4_put_stid(&stp->st_stid);
4207 spin_lock(&nn->client_lock);
4209 spin_unlock(&nn->client_lock);
4211 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4215 static struct workqueue_struct *laundry_wq;
4216 static void laundromat_main(struct work_struct *);
4219 laundromat_main(struct work_struct *laundry)
4222 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4224 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4227 t = nfs4_laundromat(nn);
4228 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4229 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4232 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4234 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4235 return nfserr_bad_stateid;
4240 access_permit_read(struct nfs4_ol_stateid *stp)
4242 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4243 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4244 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4248 access_permit_write(struct nfs4_ol_stateid *stp)
4250 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4251 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4255 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4257 __be32 status = nfserr_openmode;
4259 /* For lock stateid's, we test the parent open, not the lock: */
4260 if (stp->st_openstp)
4261 stp = stp->st_openstp;
4262 if ((flags & WR_STATE) && !access_permit_write(stp))
4264 if ((flags & RD_STATE) && !access_permit_read(stp))
4271 static inline __be32
4272 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4274 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4276 else if (locks_in_grace(net)) {
4277 /* Answer in remaining cases depends on existence of
4278 * conflicting state; so we must wait out the grace period. */
4279 return nfserr_grace;
4280 } else if (flags & WR_STATE)
4281 return nfs4_share_conflict(current_fh,
4282 NFS4_SHARE_DENY_WRITE);
4283 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4284 return nfs4_share_conflict(current_fh,
4285 NFS4_SHARE_DENY_READ);
4289 * Allow READ/WRITE during grace period on recovered state only for files
4290 * that are not able to provide mandatory locking.
4293 grace_disallows_io(struct net *net, struct inode *inode)
4295 return locks_in_grace(net) && mandatory_lock(inode);
4298 /* Returns true iff a is later than b: */
4299 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4301 return (s32)(a->si_generation - b->si_generation) > 0;
4304 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4307 * When sessions are used the stateid generation number is ignored
4310 if (has_session && in->si_generation == 0)
4313 if (in->si_generation == ref->si_generation)
4316 /* If the client sends us a stateid from the future, it's buggy: */
4317 if (stateid_generation_after(in, ref))
4318 return nfserr_bad_stateid;
4320 * However, we could see a stateid from the past, even from a
4321 * non-buggy client. For example, if the client sends a lock
4322 * while some IO is outstanding, the lock may bump si_generation
4323 * while the IO is still in flight. The client could avoid that
4324 * situation by waiting for responses on all the IO requests,
4325 * but better performance may result in retrying IO that
4326 * receives an old_stateid error if requests are rarely
4327 * reordered in flight:
4329 return nfserr_old_stateid;
4332 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4334 struct nfs4_stid *s;
4335 struct nfs4_ol_stateid *ols;
4336 __be32 status = nfserr_bad_stateid;
4338 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4340 /* Client debugging aid. */
4341 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4342 char addr_str[INET6_ADDRSTRLEN];
4343 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4345 pr_warn_ratelimited("NFSD: client %s testing state ID "
4346 "with incorrect client ID\n", addr_str);
4349 spin_lock(&cl->cl_lock);
4350 s = find_stateid_locked(cl, stateid);
4353 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4356 switch (s->sc_type) {
4357 case NFS4_DELEG_STID:
4360 case NFS4_REVOKED_DELEG_STID:
4361 status = nfserr_deleg_revoked;
4363 case NFS4_OPEN_STID:
4364 case NFS4_LOCK_STID:
4365 ols = openlockstateid(s);
4366 if (ols->st_stateowner->so_is_open_owner
4367 && !(openowner(ols->st_stateowner)->oo_flags
4368 & NFS4_OO_CONFIRMED))
4369 status = nfserr_bad_stateid;
4374 printk("unknown stateid type %x\n", s->sc_type);
4376 case NFS4_CLOSED_STID:
4377 case NFS4_CLOSED_DELEG_STID:
4378 status = nfserr_bad_stateid;
4381 spin_unlock(&cl->cl_lock);
4386 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4387 stateid_t *stateid, unsigned char typemask,
4388 struct nfs4_stid **s, struct nfsd_net *nn)
4392 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4393 return nfserr_bad_stateid;
4394 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4395 if (status == nfserr_stale_clientid) {
4396 if (cstate->session)
4397 return nfserr_bad_stateid;
4398 return nfserr_stale_stateid;
4402 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4404 return nfserr_bad_stateid;
4409 * Checks for stateid operations
4412 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4413 stateid_t *stateid, int flags, struct file **filpp)
4415 struct nfs4_stid *s;
4416 struct nfs4_ol_stateid *stp = NULL;
4417 struct nfs4_delegation *dp = NULL;
4418 struct svc_fh *current_fh = &cstate->current_fh;
4419 struct inode *ino = current_fh->fh_dentry->d_inode;
4420 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4421 struct file *file = NULL;
4427 if (grace_disallows_io(net, ino))
4428 return nfserr_grace;
4430 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4431 return check_special_stateids(net, current_fh, stateid, flags);
4433 status = nfsd4_lookup_stateid(cstate, stateid,
4434 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4438 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4441 switch (s->sc_type) {
4442 case NFS4_DELEG_STID:
4443 dp = delegstateid(s);
4444 status = nfs4_check_delegmode(dp, flags);
4448 file = dp->dl_stid.sc_file->fi_deleg_file;
4451 status = nfserr_serverfault;
4457 case NFS4_OPEN_STID:
4458 case NFS4_LOCK_STID:
4459 stp = openlockstateid(s);
4460 status = nfs4_check_fh(current_fh, stp);
4463 if (stp->st_stateowner->so_is_open_owner
4464 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4466 status = nfs4_check_openmode(stp, flags);
4470 struct nfs4_file *fp = stp->st_stid.sc_file;
4472 if (flags & RD_STATE)
4473 file = find_readable_file(fp);
4475 file = find_writeable_file(fp);
4479 status = nfserr_bad_stateid;
4491 * Test if the stateid is valid
4494 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4495 struct nfsd4_test_stateid *test_stateid)
4497 struct nfsd4_test_stateid_id *stateid;
4498 struct nfs4_client *cl = cstate->session->se_client;
4500 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4501 stateid->ts_id_status =
4502 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4508 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4509 struct nfsd4_free_stateid *free_stateid)
4511 stateid_t *stateid = &free_stateid->fr_stateid;
4512 struct nfs4_stid *s;
4513 struct nfs4_delegation *dp;
4514 struct nfs4_ol_stateid *stp;
4515 struct nfs4_client *cl = cstate->session->se_client;
4516 __be32 ret = nfserr_bad_stateid;
4518 spin_lock(&cl->cl_lock);
4519 s = find_stateid_locked(cl, stateid);
4522 switch (s->sc_type) {
4523 case NFS4_DELEG_STID:
4524 ret = nfserr_locks_held;
4526 case NFS4_OPEN_STID:
4527 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4530 ret = nfserr_locks_held;
4532 case NFS4_LOCK_STID:
4533 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4536 stp = openlockstateid(s);
4537 ret = nfserr_locks_held;
4538 if (check_for_locks(stp->st_stid.sc_file,
4539 lockowner(stp->st_stateowner)))
4541 unhash_lock_stateid(stp);
4542 spin_unlock(&cl->cl_lock);
4546 case NFS4_REVOKED_DELEG_STID:
4547 dp = delegstateid(s);
4548 list_del_init(&dp->dl_recall_lru);
4549 spin_unlock(&cl->cl_lock);
4553 /* Default falls through and returns nfserr_bad_stateid */
4556 spin_unlock(&cl->cl_lock);
4564 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4565 RD_STATE : WR_STATE;
4568 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4570 struct svc_fh *current_fh = &cstate->current_fh;
4571 struct nfs4_stateowner *sop = stp->st_stateowner;
4574 status = nfsd4_check_seqid(cstate, sop, seqid);
4577 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4578 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4580 * "Closed" stateid's exist *only* to return
4581 * nfserr_replay_me from the previous step, and
4582 * revoked delegations are kept only for free_stateid.
4584 return nfserr_bad_stateid;
4585 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4588 return nfs4_check_fh(current_fh, stp);
4592 * Checks for sequence id mutating operations.
4595 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4596 stateid_t *stateid, char typemask,
4597 struct nfs4_ol_stateid **stpp,
4598 struct nfsd_net *nn)
4601 struct nfs4_stid *s;
4602 struct nfs4_ol_stateid *stp = NULL;
4604 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4605 seqid, STATEID_VAL(stateid));
4608 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4611 stp = openlockstateid(s);
4612 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4614 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4618 nfs4_put_stid(&stp->st_stid);
4622 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4623 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4626 struct nfs4_openowner *oo;
4627 struct nfs4_ol_stateid *stp;
4629 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4630 NFS4_OPEN_STID, &stp, nn);
4633 oo = openowner(stp->st_stateowner);
4634 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4635 nfs4_put_stid(&stp->st_stid);
4636 return nfserr_bad_stateid;
4643 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4644 struct nfsd4_open_confirm *oc)
4647 struct nfs4_openowner *oo;
4648 struct nfs4_ol_stateid *stp;
4649 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4651 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4652 cstate->current_fh.fh_dentry);
4654 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4658 status = nfs4_preprocess_seqid_op(cstate,
4659 oc->oc_seqid, &oc->oc_req_stateid,
4660 NFS4_OPEN_STID, &stp, nn);
4663 oo = openowner(stp->st_stateowner);
4664 status = nfserr_bad_stateid;
4665 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4667 oo->oo_flags |= NFS4_OO_CONFIRMED;
4668 update_stateid(&stp->st_stid.sc_stateid);
4669 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4670 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4671 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4673 nfsd4_client_record_create(oo->oo_owner.so_client);
4676 nfs4_put_stid(&stp->st_stid);
4678 nfsd4_bump_seqid(cstate, status);
4682 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4684 if (!test_access(access, stp))
4686 nfs4_file_put_access(stp->st_stid.sc_file, access);
4687 clear_access(access, stp);
4690 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4692 switch (to_access) {
4693 case NFS4_SHARE_ACCESS_READ:
4694 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4695 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4697 case NFS4_SHARE_ACCESS_WRITE:
4698 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4699 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4701 case NFS4_SHARE_ACCESS_BOTH:
4709 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4710 struct nfsd4_compound_state *cstate,
4711 struct nfsd4_open_downgrade *od)
4714 struct nfs4_ol_stateid *stp;
4715 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4717 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4718 cstate->current_fh.fh_dentry);
4720 /* We don't yet support WANT bits: */
4721 if (od->od_deleg_want)
4722 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4725 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4726 &od->od_stateid, &stp, nn);
4729 status = nfserr_inval;
4730 if (!test_access(od->od_share_access, stp)) {
4731 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4732 stp->st_access_bmap, od->od_share_access);
4735 if (!test_deny(od->od_share_deny, stp)) {
4736 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4737 stp->st_deny_bmap, od->od_share_deny);
4740 nfs4_stateid_downgrade(stp, od->od_share_access);
4742 reset_union_bmap_deny(od->od_share_deny, stp);
4744 update_stateid(&stp->st_stid.sc_stateid);
4745 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4748 nfs4_put_stid(&stp->st_stid);
4750 nfsd4_bump_seqid(cstate, status);
4754 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4756 struct nfs4_client *clp = s->st_stid.sc_client;
4757 LIST_HEAD(reaplist);
4759 s->st_stid.sc_type = NFS4_CLOSED_STID;
4760 spin_lock(&clp->cl_lock);
4761 unhash_open_stateid(s, &reaplist);
4763 if (clp->cl_minorversion) {
4764 put_ol_stateid_locked(s, &reaplist);
4765 spin_unlock(&clp->cl_lock);
4766 free_ol_stateid_reaplist(&reaplist);
4768 spin_unlock(&clp->cl_lock);
4769 free_ol_stateid_reaplist(&reaplist);
4770 move_to_close_lru(s, clp->net);
4775 * nfs4_unlock_state() called after encode
4778 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4779 struct nfsd4_close *close)
4782 struct nfs4_ol_stateid *stp;
4783 struct net *net = SVC_NET(rqstp);
4784 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4786 dprintk("NFSD: nfsd4_close on file %pd\n",
4787 cstate->current_fh.fh_dentry);
4789 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4791 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4793 nfsd4_bump_seqid(cstate, status);
4796 update_stateid(&stp->st_stid.sc_stateid);
4797 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4799 nfsd4_close_open_stateid(stp);
4801 /* put reference from nfs4_preprocess_seqid_op */
4802 nfs4_put_stid(&stp->st_stid);
4808 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4809 struct nfsd4_delegreturn *dr)
4811 struct nfs4_delegation *dp;
4812 stateid_t *stateid = &dr->dr_stateid;
4813 struct nfs4_stid *s;
4815 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4817 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4820 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4823 dp = delegstateid(s);
4824 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4828 destroy_delegation(dp);
4830 nfs4_put_stid(&dp->dl_stid);
4836 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4839 end_offset(u64 start, u64 len)
4844 return end >= start ? end: NFS4_MAX_UINT64;
4847 /* last octet in a range */
4849 last_byte_offset(u64 start, u64 len)
4855 return end > start ? end - 1: NFS4_MAX_UINT64;
4859 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4860 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4861 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4862 * locking, this prevents us from being completely protocol-compliant. The
4863 * real solution to this problem is to start using unsigned file offsets in
4864 * the VFS, but this is a very deep change!
4867 nfs4_transform_lock_offset(struct file_lock *lock)
4869 if (lock->fl_start < 0)
4870 lock->fl_start = OFFSET_MAX;
4871 if (lock->fl_end < 0)
4872 lock->fl_end = OFFSET_MAX;
4875 static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4877 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4878 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4881 static void nfsd4_fl_put_owner(struct file_lock *fl)
4883 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4886 nfs4_put_stateowner(&lo->lo_owner);
4887 fl->fl_owner = NULL;
4891 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4892 .lm_get_owner = nfsd4_fl_get_owner,
4893 .lm_put_owner = nfsd4_fl_put_owner,
4897 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4899 struct nfs4_lockowner *lo;
4901 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4902 lo = (struct nfs4_lockowner *) fl->fl_owner;
4903 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4904 lo->lo_owner.so_owner.len, GFP_KERNEL);
4905 if (!deny->ld_owner.data)
4906 /* We just don't care that much */
4908 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4909 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4912 deny->ld_owner.len = 0;
4913 deny->ld_owner.data = NULL;
4914 deny->ld_clientid.cl_boot = 0;
4915 deny->ld_clientid.cl_id = 0;
4917 deny->ld_start = fl->fl_start;
4918 deny->ld_length = NFS4_MAX_UINT64;
4919 if (fl->fl_end != NFS4_MAX_UINT64)
4920 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4921 deny->ld_type = NFS4_READ_LT;
4922 if (fl->fl_type != F_RDLCK)
4923 deny->ld_type = NFS4_WRITE_LT;
4926 static struct nfs4_lockowner *
4927 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4928 struct nfs4_client *clp)
4930 unsigned int strhashval = ownerstr_hashval(owner);
4931 struct nfs4_stateowner *so;
4933 lockdep_assert_held(&clp->cl_lock);
4935 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4937 if (so->so_is_open_owner)
4939 if (same_owner_str(so, owner))
4940 return lockowner(nfs4_get_stateowner(so));
4945 static struct nfs4_lockowner *
4946 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
4947 struct nfs4_client *clp)
4949 struct nfs4_lockowner *lo;
4951 spin_lock(&clp->cl_lock);
4952 lo = find_lockowner_str_locked(clid, owner, clp);
4953 spin_unlock(&clp->cl_lock);
4957 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
4959 unhash_lockowner_locked(lockowner(sop));
4962 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
4964 struct nfs4_lockowner *lo = lockowner(sop);
4966 kmem_cache_free(lockowner_slab, lo);
4969 static const struct nfs4_stateowner_operations lockowner_ops = {
4970 .so_unhash = nfs4_unhash_lockowner,
4971 .so_free = nfs4_free_lockowner,
4975 * Alloc a lock owner structure.
4976 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4979 * strhashval = ownerstr_hashval
4981 static struct nfs4_lockowner *
4982 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
4983 struct nfs4_ol_stateid *open_stp,
4984 struct nfsd4_lock *lock)
4986 struct nfs4_lockowner *lo, *ret;
4988 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4991 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4992 lo->lo_owner.so_is_open_owner = 0;
4993 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
4994 lo->lo_owner.so_ops = &lockowner_ops;
4995 spin_lock(&clp->cl_lock);
4996 ret = find_lockowner_str_locked(&clp->cl_clientid,
4997 &lock->lk_new_owner, clp);
4999 list_add(&lo->lo_owner.so_strhash,
5000 &clp->cl_ownerstr_hashtbl[strhashval]);
5003 nfs4_free_lockowner(&lo->lo_owner);
5004 spin_unlock(&clp->cl_lock);
5009 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5010 struct nfs4_file *fp, struct inode *inode,
5011 struct nfs4_ol_stateid *open_stp)
5013 struct nfs4_client *clp = lo->lo_owner.so_client;
5015 lockdep_assert_held(&clp->cl_lock);
5017 atomic_inc(&stp->st_stid.sc_count);
5018 stp->st_stid.sc_type = NFS4_LOCK_STID;
5019 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5021 stp->st_stid.sc_file = fp;
5022 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5023 stp->st_access_bmap = 0;
5024 stp->st_deny_bmap = open_stp->st_deny_bmap;
5025 stp->st_openstp = open_stp;
5026 list_add(&stp->st_locks, &open_stp->st_locks);
5027 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5028 spin_lock(&fp->fi_lock);
5029 list_add(&stp->st_perfile, &fp->fi_stateids);
5030 spin_unlock(&fp->fi_lock);
5033 static struct nfs4_ol_stateid *
5034 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5036 struct nfs4_ol_stateid *lst;
5037 struct nfs4_client *clp = lo->lo_owner.so_client;
5039 lockdep_assert_held(&clp->cl_lock);
5041 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5042 if (lst->st_stid.sc_file == fp) {
5043 atomic_inc(&lst->st_stid.sc_count);
5050 static struct nfs4_ol_stateid *
5051 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5052 struct inode *inode, struct nfs4_ol_stateid *ost,
5055 struct nfs4_stid *ns = NULL;
5056 struct nfs4_ol_stateid *lst;
5057 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5058 struct nfs4_client *clp = oo->oo_owner.so_client;
5060 spin_lock(&clp->cl_lock);
5061 lst = find_lock_stateid(lo, fi);
5063 spin_unlock(&clp->cl_lock);
5064 ns = nfs4_alloc_stid(clp, stateid_slab);
5068 spin_lock(&clp->cl_lock);
5069 lst = find_lock_stateid(lo, fi);
5071 lst = openlockstateid(ns);
5072 init_lock_stateid(lst, lo, fi, inode, ost);
5077 spin_unlock(&clp->cl_lock);
5084 check_lock_length(u64 offset, u64 length)
5086 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5087 LOFF_OVERFLOW(offset, length)));
5090 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5092 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5094 lockdep_assert_held(&fp->fi_lock);
5096 if (test_access(access, lock_stp))
5098 __nfs4_file_get_access(fp, access);
5099 set_access(access, lock_stp);
5103 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5104 struct nfs4_ol_stateid *ost,
5105 struct nfsd4_lock *lock,
5106 struct nfs4_ol_stateid **lst, bool *new)
5109 struct nfs4_file *fi = ost->st_stid.sc_file;
5110 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5111 struct nfs4_client *cl = oo->oo_owner.so_client;
5112 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5113 struct nfs4_lockowner *lo;
5114 unsigned int strhashval;
5116 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5118 strhashval = ownerstr_hashval(&lock->v.new.owner);
5119 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5121 return nfserr_jukebox;
5123 /* with an existing lockowner, seqids must be the same */
5124 status = nfserr_bad_seqid;
5125 if (!cstate->minorversion &&
5126 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5130 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5132 status = nfserr_jukebox;
5137 nfs4_put_stateowner(&lo->lo_owner);
5145 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5146 struct nfsd4_lock *lock)
5148 struct nfs4_openowner *open_sop = NULL;
5149 struct nfs4_lockowner *lock_sop = NULL;
5150 struct nfs4_ol_stateid *lock_stp = NULL;
5151 struct nfs4_ol_stateid *open_stp = NULL;
5152 struct nfs4_file *fp;
5153 struct file *filp = NULL;
5154 struct file_lock *file_lock = NULL;
5155 struct file_lock *conflock = NULL;
5160 struct net *net = SVC_NET(rqstp);
5161 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5163 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5164 (long long) lock->lk_offset,
5165 (long long) lock->lk_length);
5167 if (check_lock_length(lock->lk_offset, lock->lk_length))
5168 return nfserr_inval;
5170 if ((status = fh_verify(rqstp, &cstate->current_fh,
5171 S_IFREG, NFSD_MAY_LOCK))) {
5172 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5176 if (lock->lk_is_new) {
5177 if (nfsd4_has_session(cstate))
5178 /* See rfc 5661 18.10.3: given clientid is ignored: */
5179 memcpy(&lock->v.new.clientid,
5180 &cstate->session->se_client->cl_clientid,
5181 sizeof(clientid_t));
5183 status = nfserr_stale_clientid;
5184 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5187 /* validate and update open stateid and open seqid */
5188 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5189 lock->lk_new_open_seqid,
5190 &lock->lk_new_open_stateid,
5194 open_sop = openowner(open_stp->st_stateowner);
5195 status = nfserr_bad_stateid;
5196 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5197 &lock->v.new.clientid))
5199 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5202 status = nfs4_preprocess_seqid_op(cstate,
5203 lock->lk_old_lock_seqid,
5204 &lock->lk_old_lock_stateid,
5205 NFS4_LOCK_STID, &lock_stp, nn);
5209 lock_sop = lockowner(lock_stp->st_stateowner);
5211 lkflg = setlkflg(lock->lk_type);
5212 status = nfs4_check_openmode(lock_stp, lkflg);
5216 status = nfserr_grace;
5217 if (locks_in_grace(net) && !lock->lk_reclaim)
5219 status = nfserr_no_grace;
5220 if (!locks_in_grace(net) && lock->lk_reclaim)
5223 file_lock = locks_alloc_lock();
5225 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5226 status = nfserr_jukebox;
5230 fp = lock_stp->st_stid.sc_file;
5231 switch (lock->lk_type) {
5234 spin_lock(&fp->fi_lock);
5235 filp = find_readable_file_locked(fp);
5237 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5238 spin_unlock(&fp->fi_lock);
5239 file_lock->fl_type = F_RDLCK;
5242 case NFS4_WRITEW_LT:
5243 spin_lock(&fp->fi_lock);
5244 filp = find_writeable_file_locked(fp);
5246 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5247 spin_unlock(&fp->fi_lock);
5248 file_lock->fl_type = F_WRLCK;
5251 status = nfserr_inval;
5255 status = nfserr_openmode;
5259 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5260 file_lock->fl_pid = current->tgid;
5261 file_lock->fl_file = filp;
5262 file_lock->fl_flags = FL_POSIX;
5263 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5264 file_lock->fl_start = lock->lk_offset;
5265 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5266 nfs4_transform_lock_offset(file_lock);
5268 conflock = locks_alloc_lock();
5270 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5271 status = nfserr_jukebox;
5275 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5277 case 0: /* success! */
5278 update_stateid(&lock_stp->st_stid.sc_stateid);
5279 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5283 case (EAGAIN): /* conflock holds conflicting lock */
5284 status = nfserr_denied;
5285 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5286 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5289 status = nfserr_deadlock;
5292 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5293 status = nfserrno(err);
5300 /* Bump seqid manually if the 4.0 replay owner is openowner */
5301 if (cstate->replay_owner &&
5302 cstate->replay_owner != &lock_sop->lo_owner &&
5303 seqid_mutating_err(ntohl(status)))
5304 lock_sop->lo_owner.so_seqid++;
5307 * If this is a new, never-before-used stateid, and we are
5308 * returning an error, then just go ahead and release it.
5311 release_lock_stateid(lock_stp);
5313 nfs4_put_stid(&lock_stp->st_stid);
5316 nfs4_put_stid(&open_stp->st_stid);
5317 nfsd4_bump_seqid(cstate, status);
5319 locks_free_lock(file_lock);
5321 locks_free_lock(conflock);
5326 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5327 * so we do a temporary open here just to get an open file to pass to
5328 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5331 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5334 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5336 err = nfserrno(vfs_test_lock(file, lock));
5346 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5347 struct nfsd4_lockt *lockt)
5349 struct file_lock *file_lock = NULL;
5350 struct nfs4_lockowner *lo = NULL;
5352 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5354 if (locks_in_grace(SVC_NET(rqstp)))
5355 return nfserr_grace;
5357 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5358 return nfserr_inval;
5360 if (!nfsd4_has_session(cstate)) {
5361 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5366 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5369 file_lock = locks_alloc_lock();
5371 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5372 status = nfserr_jukebox;
5376 switch (lockt->lt_type) {
5379 file_lock->fl_type = F_RDLCK;
5382 case NFS4_WRITEW_LT:
5383 file_lock->fl_type = F_WRLCK;
5386 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5387 status = nfserr_inval;
5391 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5394 file_lock->fl_owner = (fl_owner_t)lo;
5395 file_lock->fl_pid = current->tgid;
5396 file_lock->fl_flags = FL_POSIX;
5398 file_lock->fl_start = lockt->lt_offset;
5399 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5401 nfs4_transform_lock_offset(file_lock);
5403 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5407 if (file_lock->fl_type != F_UNLCK) {
5408 status = nfserr_denied;
5409 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5413 nfs4_put_stateowner(&lo->lo_owner);
5415 locks_free_lock(file_lock);
5420 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5421 struct nfsd4_locku *locku)
5423 struct nfs4_ol_stateid *stp;
5424 struct file *filp = NULL;
5425 struct file_lock *file_lock = NULL;
5428 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5430 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5431 (long long) locku->lu_offset,
5432 (long long) locku->lu_length);
5434 if (check_lock_length(locku->lu_offset, locku->lu_length))
5435 return nfserr_inval;
5437 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5438 &locku->lu_stateid, NFS4_LOCK_STID,
5442 filp = find_any_file(stp->st_stid.sc_file);
5444 status = nfserr_lock_range;
5447 file_lock = locks_alloc_lock();
5449 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5450 status = nfserr_jukebox;
5454 file_lock->fl_type = F_UNLCK;
5455 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5456 file_lock->fl_pid = current->tgid;
5457 file_lock->fl_file = filp;
5458 file_lock->fl_flags = FL_POSIX;
5459 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5460 file_lock->fl_start = locku->lu_offset;
5462 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5464 nfs4_transform_lock_offset(file_lock);
5466 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5468 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5471 update_stateid(&stp->st_stid.sc_stateid);
5472 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5476 nfs4_put_stid(&stp->st_stid);
5478 nfsd4_bump_seqid(cstate, status);
5480 locks_free_lock(file_lock);
5484 status = nfserrno(err);
5490 * true: locks held by lockowner
5491 * false: no locks held by lockowner
5494 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5496 struct file_lock **flpp;
5498 struct file *filp = find_any_file(fp);
5499 struct inode *inode;
5502 /* Any valid lock stateid should have some sort of access */
5507 inode = file_inode(filp);
5509 spin_lock(&inode->i_lock);
5510 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5511 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5516 spin_unlock(&inode->i_lock);
5522 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5523 struct nfsd4_compound_state *cstate,
5524 struct nfsd4_release_lockowner *rlockowner)
5526 clientid_t *clid = &rlockowner->rl_clientid;
5527 struct nfs4_stateowner *sop;
5528 struct nfs4_lockowner *lo = NULL;
5529 struct nfs4_ol_stateid *stp;
5530 struct xdr_netobj *owner = &rlockowner->rl_owner;
5531 unsigned int hashval = ownerstr_hashval(owner);
5533 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5534 struct nfs4_client *clp;
5536 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5537 clid->cl_boot, clid->cl_id);
5539 status = lookup_clientid(clid, cstate, nn);
5544 /* Find the matching lock stateowner */
5545 spin_lock(&clp->cl_lock);
5546 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5549 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5552 /* see if there are still any locks associated with it */
5553 lo = lockowner(sop);
5554 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5555 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5556 status = nfserr_locks_held;
5557 spin_unlock(&clp->cl_lock);
5562 nfs4_get_stateowner(sop);
5565 spin_unlock(&clp->cl_lock);
5567 release_lockowner(lo);
5571 static inline struct nfs4_client_reclaim *
5574 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5578 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5580 struct nfs4_client_reclaim *crp;
5582 crp = nfsd4_find_reclaim_client(name, nn);
5583 return (crp && crp->cr_clp);
5587 * failure => all reset bets are off, nfserr_no_grace...
5589 struct nfs4_client_reclaim *
5590 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5592 unsigned int strhashval;
5593 struct nfs4_client_reclaim *crp;
5595 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5596 crp = alloc_reclaim();
5598 strhashval = clientstr_hashval(name);
5599 INIT_LIST_HEAD(&crp->cr_strhash);
5600 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5601 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5603 nn->reclaim_str_hashtbl_size++;
5609 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5611 list_del(&crp->cr_strhash);
5613 nn->reclaim_str_hashtbl_size--;
5617 nfs4_release_reclaim(struct nfsd_net *nn)
5619 struct nfs4_client_reclaim *crp = NULL;
5622 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5623 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5624 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5625 struct nfs4_client_reclaim, cr_strhash);
5626 nfs4_remove_reclaim_record(crp, nn);
5629 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5633 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5634 struct nfs4_client_reclaim *
5635 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5637 unsigned int strhashval;
5638 struct nfs4_client_reclaim *crp = NULL;
5640 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5642 strhashval = clientstr_hashval(recdir);
5643 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5644 if (same_name(crp->cr_recdir, recdir)) {
5652 * Called from OPEN. Look for clientid in reclaim list.
5655 nfs4_check_open_reclaim(clientid_t *clid,
5656 struct nfsd4_compound_state *cstate,
5657 struct nfsd_net *nn)
5661 /* find clientid in conf_id_hashtbl */
5662 status = lookup_clientid(clid, cstate, nn);
5664 return nfserr_reclaim_bad;
5666 if (nfsd4_client_record_check(cstate->clp))
5667 return nfserr_reclaim_bad;
5672 #ifdef CONFIG_NFSD_FAULT_INJECTION
5674 put_client(struct nfs4_client *clp)
5676 atomic_dec(&clp->cl_refcount);
5679 static struct nfs4_client *
5680 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5682 struct nfs4_client *clp;
5683 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5686 if (!nfsd_netns_ready(nn))
5689 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5690 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5697 nfsd_inject_print_clients(void)
5699 struct nfs4_client *clp;
5701 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5703 char buf[INET6_ADDRSTRLEN];
5705 if (!nfsd_netns_ready(nn))
5708 spin_lock(&nn->client_lock);
5709 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5710 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5711 pr_info("NFS Client: %s\n", buf);
5714 spin_unlock(&nn->client_lock);
5720 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5723 struct nfs4_client *clp;
5724 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5727 if (!nfsd_netns_ready(nn))
5730 spin_lock(&nn->client_lock);
5731 clp = nfsd_find_client(addr, addr_size);
5733 if (mark_client_expired_locked(clp) == nfs_ok)
5738 spin_unlock(&nn->client_lock);
5747 nfsd_inject_forget_clients(u64 max)
5750 struct nfs4_client *clp, *next;
5751 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5753 LIST_HEAD(reaplist);
5755 if (!nfsd_netns_ready(nn))
5758 spin_lock(&nn->client_lock);
5759 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5760 if (mark_client_expired_locked(clp) == nfs_ok) {
5761 list_add(&clp->cl_lru, &reaplist);
5762 if (max != 0 && ++count >= max)
5766 spin_unlock(&nn->client_lock);
5768 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5774 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5777 char buf[INET6_ADDRSTRLEN];
5778 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5779 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5783 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5784 struct list_head *collect)
5786 struct nfs4_client *clp = lst->st_stid.sc_client;
5787 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5793 lockdep_assert_held(&nn->client_lock);
5794 atomic_inc(&clp->cl_refcount);
5795 list_add(&lst->st_locks, collect);
5798 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5799 struct list_head *collect,
5800 void (*func)(struct nfs4_ol_stateid *))
5802 struct nfs4_openowner *oop;
5803 struct nfs4_ol_stateid *stp, *st_next;
5804 struct nfs4_ol_stateid *lst, *lst_next;
5807 spin_lock(&clp->cl_lock);
5808 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5809 list_for_each_entry_safe(stp, st_next,
5810 &oop->oo_owner.so_stateids, st_perstateowner) {
5811 list_for_each_entry_safe(lst, lst_next,
5812 &stp->st_locks, st_locks) {
5815 nfsd_inject_add_lock_to_list(lst,
5820 * Despite the fact that these functions deal
5821 * with 64-bit integers for "count", we must
5822 * ensure that it doesn't blow up the
5823 * clp->cl_refcount. Throw a warning if we
5824 * start to approach INT_MAX here.
5826 WARN_ON_ONCE(count == (INT_MAX / 2));
5833 spin_unlock(&clp->cl_lock);
5839 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5842 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
5846 nfsd_print_client_locks(struct nfs4_client *clp)
5848 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
5849 nfsd_print_count(clp, count, "locked files");
5854 nfsd_inject_print_locks(void)
5856 struct nfs4_client *clp;
5858 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5861 if (!nfsd_netns_ready(nn))
5864 spin_lock(&nn->client_lock);
5865 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5866 count += nfsd_print_client_locks(clp);
5867 spin_unlock(&nn->client_lock);
5873 nfsd_reap_locks(struct list_head *reaplist)
5875 struct nfs4_client *clp;
5876 struct nfs4_ol_stateid *stp, *next;
5878 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5879 list_del_init(&stp->st_locks);
5880 clp = stp->st_stid.sc_client;
5881 nfs4_put_stid(&stp->st_stid);
5887 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
5889 unsigned int count = 0;
5890 struct nfs4_client *clp;
5891 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5893 LIST_HEAD(reaplist);
5895 if (!nfsd_netns_ready(nn))
5898 spin_lock(&nn->client_lock);
5899 clp = nfsd_find_client(addr, addr_size);
5901 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5902 spin_unlock(&nn->client_lock);
5903 nfsd_reap_locks(&reaplist);
5908 nfsd_inject_forget_locks(u64 max)
5911 struct nfs4_client *clp;
5912 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5914 LIST_HEAD(reaplist);
5916 if (!nfsd_netns_ready(nn))
5919 spin_lock(&nn->client_lock);
5920 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5921 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5922 if (max != 0 && count >= max)
5925 spin_unlock(&nn->client_lock);
5926 nfsd_reap_locks(&reaplist);
5931 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5932 struct list_head *collect,
5933 void (*func)(struct nfs4_openowner *))
5935 struct nfs4_openowner *oop, *next;
5936 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5940 lockdep_assert_held(&nn->client_lock);
5942 spin_lock(&clp->cl_lock);
5943 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
5947 atomic_inc(&clp->cl_refcount);
5948 list_add(&oop->oo_perclient, collect);
5953 * Despite the fact that these functions deal with
5954 * 64-bit integers for "count", we must ensure that
5955 * it doesn't blow up the clp->cl_refcount. Throw a
5956 * warning if we start to approach INT_MAX here.
5958 WARN_ON_ONCE(count == (INT_MAX / 2));
5962 spin_unlock(&clp->cl_lock);
5968 nfsd_print_client_openowners(struct nfs4_client *clp)
5970 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
5972 nfsd_print_count(clp, count, "openowners");
5977 nfsd_collect_client_openowners(struct nfs4_client *clp,
5978 struct list_head *collect, u64 max)
5980 return nfsd_foreach_client_openowner(clp, max, collect,
5981 unhash_openowner_locked);
5985 nfsd_inject_print_openowners(void)
5987 struct nfs4_client *clp;
5989 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5992 if (!nfsd_netns_ready(nn))
5995 spin_lock(&nn->client_lock);
5996 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5997 count += nfsd_print_client_openowners(clp);
5998 spin_unlock(&nn->client_lock);
6004 nfsd_reap_openowners(struct list_head *reaplist)
6006 struct nfs4_client *clp;
6007 struct nfs4_openowner *oop, *next;
6009 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6010 list_del_init(&oop->oo_perclient);
6011 clp = oop->oo_owner.so_client;
6012 release_openowner(oop);
6018 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6021 unsigned int count = 0;
6022 struct nfs4_client *clp;
6023 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6025 LIST_HEAD(reaplist);
6027 if (!nfsd_netns_ready(nn))
6030 spin_lock(&nn->client_lock);
6031 clp = nfsd_find_client(addr, addr_size);
6033 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6034 spin_unlock(&nn->client_lock);
6035 nfsd_reap_openowners(&reaplist);
6040 nfsd_inject_forget_openowners(u64 max)
6043 struct nfs4_client *clp;
6044 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6046 LIST_HEAD(reaplist);
6048 if (!nfsd_netns_ready(nn))
6051 spin_lock(&nn->client_lock);
6052 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6053 count += nfsd_collect_client_openowners(clp, &reaplist,
6055 if (max != 0 && count >= max)
6058 spin_unlock(&nn->client_lock);
6059 nfsd_reap_openowners(&reaplist);
6063 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6064 struct list_head *victims)
6066 struct nfs4_delegation *dp, *next;
6067 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6071 lockdep_assert_held(&nn->client_lock);
6073 spin_lock(&state_lock);
6074 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6077 * It's not safe to mess with delegations that have a
6078 * non-zero dl_time. They might have already been broken
6079 * and could be processed by the laundromat outside of
6080 * the state_lock. Just leave them be.
6082 if (dp->dl_time != 0)
6085 atomic_inc(&clp->cl_refcount);
6086 unhash_delegation_locked(dp);
6087 list_add(&dp->dl_recall_lru, victims);
6091 * Despite the fact that these functions deal with
6092 * 64-bit integers for "count", we must ensure that
6093 * it doesn't blow up the clp->cl_refcount. Throw a
6094 * warning if we start to approach INT_MAX here.
6096 WARN_ON_ONCE(count == (INT_MAX / 2));
6100 spin_unlock(&state_lock);
6105 nfsd_print_client_delegations(struct nfs4_client *clp)
6107 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6109 nfsd_print_count(clp, count, "delegations");
6114 nfsd_inject_print_delegations(void)
6116 struct nfs4_client *clp;
6118 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6121 if (!nfsd_netns_ready(nn))
6124 spin_lock(&nn->client_lock);
6125 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6126 count += nfsd_print_client_delegations(clp);
6127 spin_unlock(&nn->client_lock);
6133 nfsd_forget_delegations(struct list_head *reaplist)
6135 struct nfs4_client *clp;
6136 struct nfs4_delegation *dp, *next;
6138 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6139 list_del_init(&dp->dl_recall_lru);
6140 clp = dp->dl_stid.sc_client;
6141 revoke_delegation(dp);
6147 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6151 struct nfs4_client *clp;
6152 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6154 LIST_HEAD(reaplist);
6156 if (!nfsd_netns_ready(nn))
6159 spin_lock(&nn->client_lock);
6160 clp = nfsd_find_client(addr, addr_size);
6162 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6163 spin_unlock(&nn->client_lock);
6165 nfsd_forget_delegations(&reaplist);
6170 nfsd_inject_forget_delegations(u64 max)
6173 struct nfs4_client *clp;
6174 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6176 LIST_HEAD(reaplist);
6178 if (!nfsd_netns_ready(nn))
6181 spin_lock(&nn->client_lock);
6182 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6183 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6184 if (max != 0 && count >= max)
6187 spin_unlock(&nn->client_lock);
6188 nfsd_forget_delegations(&reaplist);
6193 nfsd_recall_delegations(struct list_head *reaplist)
6195 struct nfs4_client *clp;
6196 struct nfs4_delegation *dp, *next;
6198 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6199 list_del_init(&dp->dl_recall_lru);
6200 clp = dp->dl_stid.sc_client;
6202 * We skipped all entries that had a zero dl_time before,
6203 * so we can now reset the dl_time back to 0. If a delegation
6204 * break comes in now, then it won't make any difference since
6205 * we're recalling it either way.
6207 spin_lock(&state_lock);
6209 spin_unlock(&state_lock);
6210 nfsd_break_one_deleg(dp);
6216 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6220 struct nfs4_client *clp;
6221 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6223 LIST_HEAD(reaplist);
6225 if (!nfsd_netns_ready(nn))
6228 spin_lock(&nn->client_lock);
6229 clp = nfsd_find_client(addr, addr_size);
6231 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6232 spin_unlock(&nn->client_lock);
6234 nfsd_recall_delegations(&reaplist);
6239 nfsd_inject_recall_delegations(u64 max)
6242 struct nfs4_client *clp, *next;
6243 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6245 LIST_HEAD(reaplist);
6247 if (!nfsd_netns_ready(nn))
6250 spin_lock(&nn->client_lock);
6251 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6252 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6253 if (max != 0 && ++count >= max)
6256 spin_unlock(&nn->client_lock);
6257 nfsd_recall_delegations(&reaplist);
6260 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6263 * Since the lifetime of a delegation isn't limited to that of an open, a
6264 * client may quite reasonably hang on to a delegation as long as it has
6265 * the inode cached. This becomes an obvious problem the first time a
6266 * client's inode cache approaches the size of the server's total memory.
6268 * For now we avoid this problem by imposing a hard limit on the number
6269 * of delegations, which varies according to the server's memory size.
6272 set_max_delegations(void)
6275 * Allow at most 4 delegations per megabyte of RAM. Quick
6276 * estimates suggest that in the worst case (where every delegation
6277 * is for a different inode), a delegation could take about 1.5K,
6278 * giving a worst case usage of about 6% of memory.
6280 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6283 static int nfs4_state_create_net(struct net *net)
6285 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6288 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6289 CLIENT_HASH_SIZE, GFP_KERNEL);
6290 if (!nn->conf_id_hashtbl)
6292 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6293 CLIENT_HASH_SIZE, GFP_KERNEL);
6294 if (!nn->unconf_id_hashtbl)
6296 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6297 SESSION_HASH_SIZE, GFP_KERNEL);
6298 if (!nn->sessionid_hashtbl)
6301 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6302 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6303 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6305 for (i = 0; i < SESSION_HASH_SIZE; i++)
6306 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6307 nn->conf_name_tree = RB_ROOT;
6308 nn->unconf_name_tree = RB_ROOT;
6309 INIT_LIST_HEAD(&nn->client_lru);
6310 INIT_LIST_HEAD(&nn->close_lru);
6311 INIT_LIST_HEAD(&nn->del_recall_lru);
6312 spin_lock_init(&nn->client_lock);
6314 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6320 kfree(nn->unconf_id_hashtbl);
6322 kfree(nn->conf_id_hashtbl);
6328 nfs4_state_destroy_net(struct net *net)
6331 struct nfs4_client *clp = NULL;
6332 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6334 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6335 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6336 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6337 destroy_client(clp);
6341 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6342 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6343 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6344 destroy_client(clp);
6348 kfree(nn->sessionid_hashtbl);
6349 kfree(nn->unconf_id_hashtbl);
6350 kfree(nn->conf_id_hashtbl);
6355 nfs4_state_start_net(struct net *net)
6357 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6360 ret = nfs4_state_create_net(net);
6363 nfsd4_client_tracking_init(net);
6364 nn->boot_time = get_seconds();
6365 locks_start_grace(net, &nn->nfsd4_manager);
6366 nn->grace_ended = false;
6367 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6368 nn->nfsd4_grace, net);
6369 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6373 /* initialization to perform when the nfsd service is started: */
6376 nfs4_state_start(void)
6380 ret = set_callback_cred();
6383 laundry_wq = create_singlethread_workqueue("nfsd4");
6384 if (laundry_wq == NULL) {
6388 ret = nfsd4_create_callback_queue();
6390 goto out_free_laundry;
6392 set_max_delegations();
6397 destroy_workqueue(laundry_wq);
6403 nfs4_state_shutdown_net(struct net *net)
6405 struct nfs4_delegation *dp = NULL;
6406 struct list_head *pos, *next, reaplist;
6407 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6409 cancel_delayed_work_sync(&nn->laundromat_work);
6410 locks_end_grace(&nn->nfsd4_manager);
6412 INIT_LIST_HEAD(&reaplist);
6413 spin_lock(&state_lock);
6414 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6415 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6416 unhash_delegation_locked(dp);
6417 list_add(&dp->dl_recall_lru, &reaplist);
6419 spin_unlock(&state_lock);
6420 list_for_each_safe(pos, next, &reaplist) {
6421 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6422 list_del_init(&dp->dl_recall_lru);
6423 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6424 nfs4_put_stid(&dp->dl_stid);
6427 nfsd4_client_tracking_exit(net);
6428 nfs4_state_destroy_net(net);
6432 nfs4_state_shutdown(void)
6434 destroy_workqueue(laundry_wq);
6435 nfsd4_destroy_callback_queue();
6439 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6441 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6442 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6446 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6448 if (cstate->minorversion) {
6449 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6450 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6455 clear_current_stateid(struct nfsd4_compound_state *cstate)
6457 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6461 * functions to set current state id
6464 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6466 put_stateid(cstate, &odp->od_stateid);
6470 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6472 put_stateid(cstate, &open->op_stateid);
6476 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6478 put_stateid(cstate, &close->cl_stateid);
6482 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6484 put_stateid(cstate, &lock->lk_resp_stateid);
6488 * functions to consume current state id
6492 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6494 get_stateid(cstate, &odp->od_stateid);
6498 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6500 get_stateid(cstate, &drp->dr_stateid);
6504 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6506 get_stateid(cstate, &fsp->fr_stateid);
6510 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6512 get_stateid(cstate, &setattr->sa_stateid);
6516 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6518 get_stateid(cstate, &close->cl_stateid);
6522 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6524 get_stateid(cstate, &locku->lu_stateid);
6528 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6530 get_stateid(cstate, &read->rd_stateid);
6534 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6536 get_stateid(cstate, &write->wr_stateid);