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>
46 #include "current_stateid.h"
50 #define NFSDDBG_FACILITY NFSDDBG_PROC
52 #define all_ones {{~0,~0},~0}
53 static const stateid_t one_stateid = {
55 .si_opaque = all_ones,
57 static const stateid_t zero_stateid = {
60 static const stateid_t currentstateid = {
64 static u64 current_sessionid = 1;
66 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
67 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
68 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
70 /* forward declarations */
71 static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
75 /* Currently used for almost all code touching nfsv4 state: */
76 static DEFINE_MUTEX(client_mutex);
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(recall_lock);
85 static struct kmem_cache *openowner_slab = NULL;
86 static struct kmem_cache *lockowner_slab = NULL;
87 static struct kmem_cache *file_slab = NULL;
88 static struct kmem_cache *stateid_slab = NULL;
89 static struct kmem_cache *deleg_slab = NULL;
94 mutex_lock(&client_mutex);
97 static void free_session(struct nfsd4_session *);
99 void nfsd4_put_session(struct nfsd4_session *ses)
101 atomic_dec(&ses->se_ref);
104 static bool is_session_dead(struct nfsd4_session *ses)
106 return ses->se_flags & NFS4_SESSION_DEAD;
109 static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
111 if (atomic_read(&ses->se_ref))
112 return nfserr_jukebox;
113 ses->se_flags |= NFS4_SESSION_DEAD;
117 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
119 if (is_session_dead(ses))
120 return nfserr_badsession;
121 atomic_inc(&ses->se_ref);
126 nfs4_unlock_state(void)
128 mutex_unlock(&client_mutex);
131 static bool is_client_expired(struct nfs4_client *clp)
133 return clp->cl_time == 0;
136 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
138 if (atomic_read(&clp->cl_refcount))
139 return nfserr_jukebox;
144 static __be32 mark_client_expired(struct nfs4_client *clp)
146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
149 spin_lock(&nn->client_lock);
150 ret = mark_client_expired_locked(clp);
151 spin_unlock(&nn->client_lock);
155 static __be32 get_client_locked(struct nfs4_client *clp)
157 if (is_client_expired(clp))
158 return nfserr_expired;
159 atomic_inc(&clp->cl_refcount);
163 /* must be called under the client_lock */
165 renew_client_locked(struct nfs4_client *clp)
167 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
169 if (is_client_expired(clp)) {
171 printk("%s: client (clientid %08x/%08x) already expired\n",
173 clp->cl_clientid.cl_boot,
174 clp->cl_clientid.cl_id);
178 dprintk("renewing client (clientid %08x/%08x)\n",
179 clp->cl_clientid.cl_boot,
180 clp->cl_clientid.cl_id);
181 list_move_tail(&clp->cl_lru, &nn->client_lru);
182 clp->cl_time = get_seconds();
186 renew_client(struct nfs4_client *clp)
188 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
190 spin_lock(&nn->client_lock);
191 renew_client_locked(clp);
192 spin_unlock(&nn->client_lock);
195 void put_client_renew_locked(struct nfs4_client *clp)
197 if (!atomic_dec_and_test(&clp->cl_refcount))
199 if (!is_client_expired(clp))
200 renew_client_locked(clp);
203 void put_client_renew(struct nfs4_client *clp)
205 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
207 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
209 if (!is_client_expired(clp))
210 renew_client_locked(clp);
211 spin_unlock(&nn->client_lock);
216 opaque_hashval(const void *ptr, int nbytes)
218 unsigned char *cptr = (unsigned char *) ptr;
228 static struct list_head del_recall_lru;
230 static void nfsd4_free_file(struct nfs4_file *f)
232 kmem_cache_free(file_slab, f);
236 put_nfs4_file(struct nfs4_file *fi)
238 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
239 hlist_del(&fi->fi_hash);
240 spin_unlock(&recall_lock);
247 get_nfs4_file(struct nfs4_file *fi)
249 atomic_inc(&fi->fi_ref);
252 static int num_delegations;
253 unsigned long max_delegations;
256 * Open owner state (share locks)
259 /* hash tables for lock and open owners */
260 #define OWNER_HASH_BITS 8
261 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
262 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
264 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
268 ret = opaque_hashval(ownername->data, ownername->len);
270 return ret & OWNER_HASH_MASK;
273 /* hash table for nfs4_file */
274 #define FILE_HASH_BITS 8
275 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
277 static unsigned int file_hashval(struct inode *ino)
279 /* XXX: why are we hashing on inode pointer, anyway? */
280 return hash_ptr(ino, FILE_HASH_BITS);
283 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
285 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
287 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
288 atomic_inc(&fp->fi_access[oflag]);
291 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
293 if (oflag == O_RDWR) {
294 __nfs4_file_get_access(fp, O_RDONLY);
295 __nfs4_file_get_access(fp, O_WRONLY);
297 __nfs4_file_get_access(fp, oflag);
300 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
302 if (fp->fi_fds[oflag]) {
303 fput(fp->fi_fds[oflag]);
304 fp->fi_fds[oflag] = NULL;
308 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
310 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
311 nfs4_file_put_fd(fp, oflag);
313 * It's also safe to get rid of the RDWR open *if*
314 * we no longer have need of the other kind of access
315 * or if we already have the other kind of open:
317 if (fp->fi_fds[1-oflag]
318 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
319 nfs4_file_put_fd(fp, O_RDWR);
323 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
325 if (oflag == O_RDWR) {
326 __nfs4_file_put_access(fp, O_RDONLY);
327 __nfs4_file_put_access(fp, O_WRONLY);
329 __nfs4_file_put_access(fp, oflag);
332 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
335 struct idr *stateids = &cl->cl_stateids;
336 static int min_stateid = 0;
337 struct nfs4_stid *stid;
340 stid = kmem_cache_alloc(slab, GFP_KERNEL);
344 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
347 stid->sc_client = cl;
349 stid->sc_stateid.si_opaque.so_id = new_id;
350 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
351 /* Will be incremented before return to client: */
352 stid->sc_stateid.si_generation = 0;
355 * It shouldn't be a problem to reuse an opaque stateid value.
356 * I don't think it is for 4.1. But with 4.0 I worry that, for
357 * example, a stray write retransmission could be accepted by
358 * the server when it should have been rejected. Therefore,
359 * adopt a trick from the sctp code to attempt to maximize the
360 * amount of time until an id is reused, by ensuring they always
361 * "increase" (mod INT_MAX):
364 min_stateid = new_id+1;
365 if (min_stateid == INT_MAX)
373 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
375 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
378 static struct nfs4_delegation *
379 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
381 struct nfs4_delegation *dp;
382 struct nfs4_file *fp = stp->st_file;
384 dprintk("NFSD alloc_init_deleg\n");
386 * Major work on the lease subsystem (for example, to support
387 * calbacks on stat) will be required before we can support
388 * write delegations properly.
390 if (type != NFS4_OPEN_DELEGATE_READ)
392 if (fp->fi_had_conflict)
394 if (num_delegations > max_delegations)
396 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
399 dp->dl_stid.sc_type = NFS4_DELEG_STID;
401 * delegation seqid's are never incremented. The 4.1 special
402 * meaning of seqid 0 isn't meaningful, really, but let's avoid
403 * 0 anyway just for consistency and use 1:
405 dp->dl_stid.sc_stateid.si_generation = 1;
407 INIT_LIST_HEAD(&dp->dl_perfile);
408 INIT_LIST_HEAD(&dp->dl_perclnt);
409 INIT_LIST_HEAD(&dp->dl_recall_lru);
413 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
415 atomic_set(&dp->dl_count, 1);
416 nfsd4_init_callback(&dp->dl_recall);
420 static void remove_stid(struct nfs4_stid *s)
422 struct idr *stateids = &s->sc_client->cl_stateids;
424 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
428 nfs4_put_delegation(struct nfs4_delegation *dp)
430 if (atomic_dec_and_test(&dp->dl_count)) {
431 kmem_cache_free(deleg_slab, dp);
436 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
438 if (atomic_dec_and_test(&fp->fi_delegees)) {
439 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
441 fput(fp->fi_deleg_file);
442 fp->fi_deleg_file = NULL;
446 static void unhash_stid(struct nfs4_stid *s)
451 /* Called under the state lock. */
453 unhash_delegation(struct nfs4_delegation *dp)
455 unhash_stid(&dp->dl_stid);
456 list_del_init(&dp->dl_perclnt);
457 spin_lock(&recall_lock);
458 list_del_init(&dp->dl_perfile);
459 list_del_init(&dp->dl_recall_lru);
460 spin_unlock(&recall_lock);
461 nfs4_put_deleg_lease(dp->dl_file);
462 put_nfs4_file(dp->dl_file);
464 remove_stid(&dp->dl_stid);
465 nfs4_put_delegation(dp);
472 static unsigned int clientid_hashval(u32 id)
474 return id & CLIENT_HASH_MASK;
477 static unsigned int clientstr_hashval(const char *name)
479 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
483 * We store the NONE, READ, WRITE, and BOTH bits separately in the
484 * st_{access,deny}_bmap field of the stateid, in order to track not
485 * only what share bits are currently in force, but also what
486 * combinations of share bits previous opens have used. This allows us
487 * to enforce the recommendation of rfc 3530 14.2.19 that the server
488 * return an error if the client attempt to downgrade to a combination
489 * of share bits not explicable by closing some of its previous opens.
491 * XXX: This enforcement is actually incomplete, since we don't keep
492 * track of access/deny bit combinations; so, e.g., we allow:
494 * OPEN allow read, deny write
495 * OPEN allow both, deny none
496 * DOWNGRADE allow read, deny none
498 * which we should reject.
501 bmap_to_share_mode(unsigned long bmap) {
503 unsigned int access = 0;
505 for (i = 1; i < 4; i++) {
506 if (test_bit(i, &bmap))
513 test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
514 unsigned int access, deny;
516 access = bmap_to_share_mode(stp->st_access_bmap);
517 deny = bmap_to_share_mode(stp->st_deny_bmap);
518 if ((access & open->op_share_deny) || (deny & open->op_share_access))
523 /* set share access for a given stateid */
525 set_access(u32 access, struct nfs4_ol_stateid *stp)
527 __set_bit(access, &stp->st_access_bmap);
530 /* clear share access for a given stateid */
532 clear_access(u32 access, struct nfs4_ol_stateid *stp)
534 __clear_bit(access, &stp->st_access_bmap);
537 /* test whether a given stateid has access */
539 test_access(u32 access, struct nfs4_ol_stateid *stp)
541 return test_bit(access, &stp->st_access_bmap);
544 /* set share deny for a given stateid */
546 set_deny(u32 access, struct nfs4_ol_stateid *stp)
548 __set_bit(access, &stp->st_deny_bmap);
551 /* clear share deny for a given stateid */
553 clear_deny(u32 access, struct nfs4_ol_stateid *stp)
555 __clear_bit(access, &stp->st_deny_bmap);
558 /* test whether a given stateid is denying specific access */
560 test_deny(u32 access, struct nfs4_ol_stateid *stp)
562 return test_bit(access, &stp->st_deny_bmap);
565 static int nfs4_access_to_omode(u32 access)
567 switch (access & NFS4_SHARE_ACCESS_BOTH) {
568 case NFS4_SHARE_ACCESS_READ:
570 case NFS4_SHARE_ACCESS_WRITE:
572 case NFS4_SHARE_ACCESS_BOTH:
579 /* release all access and file references for a given stateid */
581 release_all_access(struct nfs4_ol_stateid *stp)
585 for (i = 1; i < 4; i++) {
586 if (test_access(i, stp))
587 nfs4_file_put_access(stp->st_file,
588 nfs4_access_to_omode(i));
589 clear_access(i, stp);
593 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
595 list_del(&stp->st_perfile);
596 list_del(&stp->st_perstateowner);
599 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
601 release_all_access(stp);
602 put_nfs4_file(stp->st_file);
606 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
608 remove_stid(&stp->st_stid);
609 kmem_cache_free(stateid_slab, stp);
612 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
616 unhash_generic_stateid(stp);
617 unhash_stid(&stp->st_stid);
618 file = find_any_file(stp->st_file);
620 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
621 close_generic_stateid(stp);
622 free_generic_stateid(stp);
625 static void unhash_lockowner(struct nfs4_lockowner *lo)
627 struct nfs4_ol_stateid *stp;
629 list_del(&lo->lo_owner.so_strhash);
630 list_del(&lo->lo_perstateid);
631 list_del(&lo->lo_owner_ino_hash);
632 while (!list_empty(&lo->lo_owner.so_stateids)) {
633 stp = list_first_entry(&lo->lo_owner.so_stateids,
634 struct nfs4_ol_stateid, st_perstateowner);
635 release_lock_stateid(stp);
639 static void release_lockowner(struct nfs4_lockowner *lo)
641 unhash_lockowner(lo);
642 nfs4_free_lockowner(lo);
646 release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
648 struct nfs4_lockowner *lo;
650 while (!list_empty(&open_stp->st_lockowners)) {
651 lo = list_entry(open_stp->st_lockowners.next,
652 struct nfs4_lockowner, lo_perstateid);
653 release_lockowner(lo);
657 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
659 unhash_generic_stateid(stp);
660 release_stateid_lockowners(stp);
661 close_generic_stateid(stp);
664 static void release_open_stateid(struct nfs4_ol_stateid *stp)
666 unhash_open_stateid(stp);
667 unhash_stid(&stp->st_stid);
668 free_generic_stateid(stp);
671 static void unhash_openowner(struct nfs4_openowner *oo)
673 struct nfs4_ol_stateid *stp;
675 list_del(&oo->oo_owner.so_strhash);
676 list_del(&oo->oo_perclient);
677 while (!list_empty(&oo->oo_owner.so_stateids)) {
678 stp = list_first_entry(&oo->oo_owner.so_stateids,
679 struct nfs4_ol_stateid, st_perstateowner);
680 release_open_stateid(stp);
684 static void release_last_closed_stateid(struct nfs4_openowner *oo)
686 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
689 unhash_stid(&s->st_stid);
690 free_generic_stateid(s);
691 oo->oo_last_closed_stid = NULL;
695 static void release_openowner(struct nfs4_openowner *oo)
697 unhash_openowner(oo);
698 list_del(&oo->oo_close_lru);
699 release_last_closed_stateid(oo);
700 nfs4_free_openowner(oo);
704 hash_sessionid(struct nfs4_sessionid *sessionid)
706 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
708 return sid->sequence % SESSION_HASH_SIZE;
713 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
715 u32 *ptr = (u32 *)(&sessionid->data[0]);
716 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
720 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
727 gen_sessionid(struct nfsd4_session *ses)
729 struct nfs4_client *clp = ses->se_client;
730 struct nfsd4_sessionid *sid;
732 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
733 sid->clientid = clp->cl_clientid;
734 sid->sequence = current_sessionid++;
739 * The protocol defines ca_maxresponssize_cached to include the size of
740 * the rpc header, but all we need to cache is the data starting after
741 * the end of the initial SEQUENCE operation--the rest we regenerate
742 * each time. Therefore we can advertise a ca_maxresponssize_cached
743 * value that is the number of bytes in our cache plus a few additional
744 * bytes. In order to stay on the safe side, and not promise more than
745 * we can cache, those additional bytes must be the minimum possible: 24
746 * bytes of rpc header (xid through accept state, with AUTH_NULL
747 * verifier), 12 for the compound header (with zero-length tag), and 44
748 * for the SEQUENCE op response:
750 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
753 free_session_slots(struct nfsd4_session *ses)
757 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
758 kfree(ses->se_slots[i]);
762 * We don't actually need to cache the rpc and session headers, so we
763 * can allocate a little less for each slot:
765 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
767 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
770 static int nfsd4_sanitize_slot_size(u32 size)
772 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
773 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
779 * XXX: If we run out of reserved DRC memory we could (up to a point)
780 * re-negotiate active sessions and reduce their slot usage to make
781 * room for new connections. For now we just fail the create session.
783 static int nfsd4_get_drc_mem(int slotsize, u32 num)
787 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
789 spin_lock(&nfsd_drc_lock);
790 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
791 nfsd_drc_max_mem - nfsd_drc_mem_used);
792 num = min_t(int, num, avail / slotsize);
793 nfsd_drc_mem_used += num * slotsize;
794 spin_unlock(&nfsd_drc_lock);
799 static void nfsd4_put_drc_mem(int slotsize, int num)
801 spin_lock(&nfsd_drc_lock);
802 nfsd_drc_mem_used -= slotsize * num;
803 spin_unlock(&nfsd_drc_lock);
806 static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
808 struct nfsd4_session *new;
811 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
812 + sizeof(struct nfsd4_session) > PAGE_SIZE);
813 mem = numslots * sizeof(struct nfsd4_slot *);
815 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
818 /* allocate each struct nfsd4_slot and data cache in one piece */
819 for (i = 0; i < numslots; i++) {
820 mem = sizeof(struct nfsd4_slot) + slotsize;
821 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
822 if (!new->se_slots[i])
828 kfree(new->se_slots[i]);
833 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
834 struct nfsd4_channel_attrs *req,
835 int numslots, int slotsize,
838 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
840 new->maxreqs = numslots;
841 new->maxresp_cached = min_t(u32, req->maxresp_cached,
842 slotsize + NFSD_MIN_HDR_SEQ_SZ);
843 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
844 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
845 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
848 static void free_conn(struct nfsd4_conn *c)
850 svc_xprt_put(c->cn_xprt);
854 static void nfsd4_conn_lost(struct svc_xpt_user *u)
856 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
857 struct nfs4_client *clp = c->cn_session->se_client;
859 spin_lock(&clp->cl_lock);
860 if (!list_empty(&c->cn_persession)) {
861 list_del(&c->cn_persession);
864 nfsd4_probe_callback(clp);
865 spin_unlock(&clp->cl_lock);
868 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
870 struct nfsd4_conn *conn;
872 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
875 svc_xprt_get(rqstp->rq_xprt);
876 conn->cn_xprt = rqstp->rq_xprt;
877 conn->cn_flags = flags;
878 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
882 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
884 conn->cn_session = ses;
885 list_add(&conn->cn_persession, &ses->se_conns);
888 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
890 struct nfs4_client *clp = ses->se_client;
892 spin_lock(&clp->cl_lock);
893 __nfsd4_hash_conn(conn, ses);
894 spin_unlock(&clp->cl_lock);
897 static int nfsd4_register_conn(struct nfsd4_conn *conn)
899 conn->cn_xpt_user.callback = nfsd4_conn_lost;
900 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
903 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
907 nfsd4_hash_conn(conn, ses);
908 ret = nfsd4_register_conn(conn);
910 /* oops; xprt is already down: */
911 nfsd4_conn_lost(&conn->cn_xpt_user);
912 if (conn->cn_flags & NFS4_CDFC4_BACK) {
913 /* callback channel may be back up */
914 nfsd4_probe_callback(ses->se_client);
918 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
920 u32 dir = NFS4_CDFC4_FORE;
922 if (cses->flags & SESSION4_BACK_CHAN)
923 dir |= NFS4_CDFC4_BACK;
924 return alloc_conn(rqstp, dir);
927 /* must be called under client_lock */
928 static void nfsd4_del_conns(struct nfsd4_session *s)
930 struct nfs4_client *clp = s->se_client;
931 struct nfsd4_conn *c;
933 spin_lock(&clp->cl_lock);
934 while (!list_empty(&s->se_conns)) {
935 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
936 list_del_init(&c->cn_persession);
937 spin_unlock(&clp->cl_lock);
939 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
942 spin_lock(&clp->cl_lock);
944 spin_unlock(&clp->cl_lock);
947 static void __free_session(struct nfsd4_session *ses)
949 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
950 free_session_slots(ses);
954 static void free_session(struct nfsd4_session *ses)
956 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
958 lockdep_assert_held(&nn->client_lock);
959 nfsd4_del_conns(ses);
963 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
966 struct nfsd4_session *new;
967 int numslots, slotsize;
969 * Note decreasing slot size below client's request may
970 * make it difficult for client to function correctly, whereas
971 * decreasing the number of slots will (just?) affect
972 * performance. When short on memory we therefore prefer to
973 * decrease number of slots instead of their size.
975 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
976 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
980 new = __alloc_session(slotsize, numslots);
982 nfsd4_put_drc_mem(slotsize, numslots);
985 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
989 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
992 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
994 new->se_client = clp;
997 INIT_LIST_HEAD(&new->se_conns);
999 new->se_cb_seq_nr = 1;
1000 new->se_flags = cses->flags;
1001 new->se_cb_prog = cses->callback_prog;
1002 new->se_cb_sec = cses->cb_sec;
1003 atomic_set(&new->se_ref, 0);
1004 idx = hash_sessionid(&new->se_sessionid);
1005 spin_lock(&nn->client_lock);
1006 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1007 spin_lock(&clp->cl_lock);
1008 list_add(&new->se_perclnt, &clp->cl_sessions);
1009 spin_unlock(&clp->cl_lock);
1010 spin_unlock(&nn->client_lock);
1012 if (cses->flags & SESSION4_BACK_CHAN) {
1013 struct sockaddr *sa = svc_addr(rqstp);
1015 * This is a little silly; with sessions there's no real
1016 * use for the callback address. Use the peer address
1017 * as a reasonable default for now, but consider fixing
1018 * the rpc client not to require an address in the
1021 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1022 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1026 /* caller must hold client_lock */
1027 static struct nfsd4_session *
1028 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1030 struct nfsd4_session *elem;
1032 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1034 dump_sessionid(__func__, sessionid);
1035 idx = hash_sessionid(sessionid);
1036 /* Search in the appropriate list */
1037 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1038 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1039 NFS4_MAX_SESSIONID_LEN)) {
1044 dprintk("%s: session not found\n", __func__);
1048 /* caller must hold client_lock */
1050 unhash_session(struct nfsd4_session *ses)
1052 list_del(&ses->se_hash);
1053 spin_lock(&ses->se_client->cl_lock);
1054 list_del(&ses->se_perclnt);
1055 spin_unlock(&ses->se_client->cl_lock);
1058 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1060 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1062 if (clid->cl_boot == nn->boot_time)
1064 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1065 clid->cl_boot, clid->cl_id, nn->boot_time);
1070 * XXX Should we use a slab cache ?
1071 * This type of memory management is somewhat inefficient, but we use it
1072 * anyway since SETCLIENTID is not a common operation.
1074 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1076 struct nfs4_client *clp;
1078 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1081 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1082 if (clp->cl_name.data == NULL) {
1086 clp->cl_name.len = name.len;
1091 free_client(struct nfs4_client *clp)
1093 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1095 lockdep_assert_held(&nn->client_lock);
1096 while (!list_empty(&clp->cl_sessions)) {
1097 struct nfsd4_session *ses;
1098 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1100 list_del(&ses->se_perclnt);
1101 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1104 free_svc_cred(&clp->cl_cred);
1105 kfree(clp->cl_name.data);
1106 idr_destroy(&clp->cl_stateids);
1110 /* must be called under the client_lock */
1112 unhash_client_locked(struct nfs4_client *clp)
1114 struct nfsd4_session *ses;
1116 list_del(&clp->cl_lru);
1117 spin_lock(&clp->cl_lock);
1118 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1119 list_del_init(&ses->se_hash);
1120 spin_unlock(&clp->cl_lock);
1124 destroy_client(struct nfs4_client *clp)
1126 struct nfs4_openowner *oo;
1127 struct nfs4_delegation *dp;
1128 struct list_head reaplist;
1129 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1131 INIT_LIST_HEAD(&reaplist);
1132 spin_lock(&recall_lock);
1133 while (!list_empty(&clp->cl_delegations)) {
1134 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1135 list_del_init(&dp->dl_perclnt);
1136 list_move(&dp->dl_recall_lru, &reaplist);
1138 spin_unlock(&recall_lock);
1139 while (!list_empty(&reaplist)) {
1140 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1141 unhash_delegation(dp);
1143 while (!list_empty(&clp->cl_openowners)) {
1144 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1145 release_openowner(oo);
1147 nfsd4_shutdown_callback(clp);
1148 if (clp->cl_cb_conn.cb_xprt)
1149 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1150 list_del(&clp->cl_idhash);
1151 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1152 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1154 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1155 spin_lock(&nn->client_lock);
1156 unhash_client_locked(clp);
1157 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1159 spin_unlock(&nn->client_lock);
1162 static void expire_client(struct nfs4_client *clp)
1164 nfsd4_client_record_remove(clp);
1165 destroy_client(clp);
1168 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1170 memcpy(target->cl_verifier.data, source->data,
1171 sizeof(target->cl_verifier.data));
1174 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1176 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1177 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1180 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1182 if (source->cr_principal) {
1183 target->cr_principal =
1184 kstrdup(source->cr_principal, GFP_KERNEL);
1185 if (target->cr_principal == NULL)
1188 target->cr_principal = NULL;
1189 target->cr_flavor = source->cr_flavor;
1190 target->cr_uid = source->cr_uid;
1191 target->cr_gid = source->cr_gid;
1192 target->cr_group_info = source->cr_group_info;
1193 get_group_info(target->cr_group_info);
1198 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1202 res = o1->len - o2->len;
1205 return (long long)memcmp(o1->data, o2->data, o1->len);
1208 static int same_name(const char *n1, const char *n2)
1210 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1214 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1216 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1220 same_clid(clientid_t *cl1, clientid_t *cl2)
1222 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1225 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1229 if (g1->ngroups != g2->ngroups)
1231 for (i=0; i<g1->ngroups; i++)
1232 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1238 * RFC 3530 language requires clid_inuse be returned when the
1239 * "principal" associated with a requests differs from that previously
1240 * used. We use uid, gid's, and gss principal string as our best
1241 * approximation. We also don't want to allow non-gss use of a client
1242 * established using gss: in theory cr_principal should catch that
1243 * change, but in practice cr_principal can be null even in the gss case
1244 * since gssd doesn't always pass down a principal string.
1246 static bool is_gss_cred(struct svc_cred *cr)
1248 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1249 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1254 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1256 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1257 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1258 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1259 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1261 if (cr1->cr_principal == cr2->cr_principal)
1263 if (!cr1->cr_principal || !cr2->cr_principal)
1265 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1268 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1270 static u32 current_clientid = 1;
1272 clp->cl_clientid.cl_boot = nn->boot_time;
1273 clp->cl_clientid.cl_id = current_clientid++;
1276 static void gen_confirm(struct nfs4_client *clp)
1281 verf[0] = (__be32)get_seconds();
1282 verf[1] = (__be32)i++;
1283 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1286 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1288 struct nfs4_stid *ret;
1290 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1291 if (!ret || !ret->sc_type)
1296 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1298 struct nfs4_stid *s;
1300 s = find_stateid(cl, t);
1303 if (typemask & s->sc_type)
1308 static struct nfs4_client *create_client(struct xdr_netobj name,
1309 struct svc_rqst *rqstp, nfs4_verifier *verf)
1311 struct nfs4_client *clp;
1312 struct sockaddr *sa = svc_addr(rqstp);
1314 struct net *net = SVC_NET(rqstp);
1315 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1317 clp = alloc_client(name);
1321 INIT_LIST_HEAD(&clp->cl_sessions);
1322 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1324 spin_lock(&nn->client_lock);
1326 spin_unlock(&nn->client_lock);
1329 idr_init(&clp->cl_stateids);
1330 atomic_set(&clp->cl_refcount, 0);
1331 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1332 INIT_LIST_HEAD(&clp->cl_idhash);
1333 INIT_LIST_HEAD(&clp->cl_openowners);
1334 INIT_LIST_HEAD(&clp->cl_delegations);
1335 INIT_LIST_HEAD(&clp->cl_lru);
1336 INIT_LIST_HEAD(&clp->cl_callbacks);
1337 spin_lock_init(&clp->cl_lock);
1338 nfsd4_init_callback(&clp->cl_cb_null);
1339 clp->cl_time = get_seconds();
1340 clear_bit(0, &clp->cl_cb_slot_busy);
1341 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1342 copy_verf(clp, verf);
1343 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1345 clp->cl_cb_session = NULL;
1351 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1353 struct rb_node **new = &(root->rb_node), *parent = NULL;
1354 struct nfs4_client *clp;
1357 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1360 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1361 new = &((*new)->rb_left);
1363 new = &((*new)->rb_right);
1366 rb_link_node(&new_clp->cl_namenode, parent, new);
1367 rb_insert_color(&new_clp->cl_namenode, root);
1370 static struct nfs4_client *
1371 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1374 struct rb_node *node = root->rb_node;
1375 struct nfs4_client *clp;
1378 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1379 cmp = compare_blob(&clp->cl_name, name);
1381 node = node->rb_left;
1383 node = node->rb_right;
1391 add_to_unconfirmed(struct nfs4_client *clp)
1393 unsigned int idhashval;
1394 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1396 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1397 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1398 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1399 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1404 move_to_confirmed(struct nfs4_client *clp)
1406 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1407 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1409 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1410 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1411 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1412 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1413 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1417 static struct nfs4_client *
1418 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1420 struct nfs4_client *clp;
1421 unsigned int idhashval = clientid_hashval(clid->cl_id);
1423 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1424 if (same_clid(&clp->cl_clientid, clid)) {
1425 if ((bool)clp->cl_minorversion != sessions)
1434 static struct nfs4_client *
1435 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1437 struct list_head *tbl = nn->conf_id_hashtbl;
1439 return find_client_in_id_table(tbl, clid, sessions);
1442 static struct nfs4_client *
1443 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1445 struct list_head *tbl = nn->unconf_id_hashtbl;
1447 return find_client_in_id_table(tbl, clid, sessions);
1450 static bool clp_used_exchangeid(struct nfs4_client *clp)
1452 return clp->cl_exchange_flags != 0;
1455 static struct nfs4_client *
1456 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1458 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1461 static struct nfs4_client *
1462 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1464 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1468 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1470 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1471 struct sockaddr *sa = svc_addr(rqstp);
1472 u32 scopeid = rpc_get_scope_id(sa);
1473 unsigned short expected_family;
1475 /* Currently, we only support tcp and tcp6 for the callback channel */
1476 if (se->se_callback_netid_len == 3 &&
1477 !memcmp(se->se_callback_netid_val, "tcp", 3))
1478 expected_family = AF_INET;
1479 else if (se->se_callback_netid_len == 4 &&
1480 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1481 expected_family = AF_INET6;
1485 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1486 se->se_callback_addr_len,
1487 (struct sockaddr *)&conn->cb_addr,
1488 sizeof(conn->cb_addr));
1490 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1493 if (conn->cb_addr.ss_family == AF_INET6)
1494 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1496 conn->cb_prog = se->se_callback_prog;
1497 conn->cb_ident = se->se_callback_ident;
1498 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1501 conn->cb_addr.ss_family = AF_UNSPEC;
1502 conn->cb_addrlen = 0;
1503 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1504 "will not receive delegations\n",
1505 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1511 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1514 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1516 struct nfsd4_slot *slot = resp->cstate.slot;
1519 dprintk("--> %s slot %p\n", __func__, slot);
1521 slot->sl_opcnt = resp->opcnt;
1522 slot->sl_status = resp->cstate.status;
1524 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1525 if (nfsd4_not_cached(resp)) {
1526 slot->sl_datalen = 0;
1529 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1530 base = (char *)resp->cstate.datap -
1531 (char *)resp->xbuf->head[0].iov_base;
1532 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1534 WARN("%s: sessions DRC could not cache compound\n", __func__);
1539 * Encode the replay sequence operation from the slot values.
1540 * If cachethis is FALSE encode the uncached rep error on the next
1541 * operation which sets resp->p and increments resp->opcnt for
1542 * nfs4svc_encode_compoundres.
1546 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1547 struct nfsd4_compoundres *resp)
1549 struct nfsd4_op *op;
1550 struct nfsd4_slot *slot = resp->cstate.slot;
1552 /* Encode the replayed sequence operation */
1553 op = &args->ops[resp->opcnt - 1];
1554 nfsd4_encode_operation(resp, op);
1556 /* Return nfserr_retry_uncached_rep in next operation. */
1557 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1558 op = &args->ops[resp->opcnt++];
1559 op->status = nfserr_retry_uncached_rep;
1560 nfsd4_encode_operation(resp, op);
1566 * The sequence operation is not cached because we can use the slot and
1570 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1571 struct nfsd4_sequence *seq)
1573 struct nfsd4_slot *slot = resp->cstate.slot;
1576 dprintk("--> %s slot %p\n", __func__, slot);
1578 /* Either returns 0 or nfserr_retry_uncached */
1579 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1580 if (status == nfserr_retry_uncached_rep)
1583 /* The sequence operation has been encoded, cstate->datap set. */
1584 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1586 resp->opcnt = slot->sl_opcnt;
1587 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1588 status = slot->sl_status;
1594 * Set the exchange_id flags returned by the server.
1597 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1599 /* pNFS is not supported */
1600 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1602 /* Referrals are supported, Migration is not. */
1603 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1605 /* set the wire flags to return to client. */
1606 clid->flags = new->cl_exchange_flags;
1609 static bool client_has_state(struct nfs4_client *clp)
1612 * Note clp->cl_openowners check isn't quite right: there's no
1613 * need to count owners without stateid's.
1615 * Also note we should probably be using this in 4.0 case too.
1617 return !list_empty(&clp->cl_openowners)
1618 || !list_empty(&clp->cl_delegations)
1619 || !list_empty(&clp->cl_sessions);
1623 nfsd4_exchange_id(struct svc_rqst *rqstp,
1624 struct nfsd4_compound_state *cstate,
1625 struct nfsd4_exchange_id *exid)
1627 struct nfs4_client *unconf, *conf, *new;
1629 char addr_str[INET6_ADDRSTRLEN];
1630 nfs4_verifier verf = exid->verifier;
1631 struct sockaddr *sa = svc_addr(rqstp);
1632 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1633 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1635 rpc_ntop(sa, addr_str, sizeof(addr_str));
1636 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1637 "ip_addr=%s flags %x, spa_how %d\n",
1638 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1639 addr_str, exid->flags, exid->spa_how);
1641 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1642 return nfserr_inval;
1644 /* Currently only support SP4_NONE */
1645 switch (exid->spa_how) {
1648 default: /* checked by xdr code */
1652 return nfserr_serverfault; /* no excuse :-/ */
1655 /* Cases below refer to rfc 5661 section 18.35.4: */
1657 conf = find_confirmed_client_by_name(&exid->clname, nn);
1659 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1660 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1663 if (!clp_used_exchangeid(conf)) { /* buggy client */
1664 status = nfserr_inval;
1667 if (!creds_match) { /* case 9 */
1668 status = nfserr_perm;
1671 if (!verfs_match) { /* case 8 */
1672 status = nfserr_not_same;
1676 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1680 if (!creds_match) { /* case 3 */
1681 if (client_has_state(conf)) {
1682 status = nfserr_clid_inuse;
1685 expire_client(conf);
1688 if (verfs_match) { /* case 2 */
1689 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
1693 /* case 5, client reboot */
1697 if (update) { /* case 7 */
1698 status = nfserr_noent;
1702 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
1703 if (unconf) /* case 4, possible retry or client restart */
1704 expire_client(unconf);
1706 /* case 1 (normal case) */
1708 new = create_client(exid->clname, rqstp, &verf);
1710 status = nfserr_jukebox;
1713 new->cl_minorversion = 1;
1716 add_to_unconfirmed(new);
1718 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1719 exid->clientid.cl_id = new->cl_clientid.cl_id;
1721 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
1722 nfsd4_set_ex_flags(new, exid);
1724 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1725 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1729 nfs4_unlock_state();
1734 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1736 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1739 /* The slot is in use, and no response has been sent. */
1741 if (seqid == slot_seqid)
1742 return nfserr_jukebox;
1744 return nfserr_seq_misordered;
1746 /* Note unsigned 32-bit arithmetic handles wraparound: */
1747 if (likely(seqid == slot_seqid + 1))
1749 if (seqid == slot_seqid)
1750 return nfserr_replay_cache;
1751 return nfserr_seq_misordered;
1755 * Cache the create session result into the create session single DRC
1756 * slot cache by saving the xdr structure. sl_seqid has been set.
1757 * Do this for solo or embedded create session operations.
1760 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1761 struct nfsd4_clid_slot *slot, __be32 nfserr)
1763 slot->sl_status = nfserr;
1764 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1768 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1769 struct nfsd4_clid_slot *slot)
1771 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1772 return slot->sl_status;
1775 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1776 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1777 1 + /* MIN tag is length with zero, only length */ \
1778 3 + /* version, opcount, opcode */ \
1779 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1780 /* seqid, slotID, slotID, cache */ \
1781 4 ) * sizeof(__be32))
1783 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1784 2 + /* verifier: AUTH_NULL, length 0 */\
1786 1 + /* MIN tag is length with zero, only length */ \
1787 3 + /* opcount, opcode, opstatus*/ \
1788 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1789 /* seqid, slotID, slotID, slotID, status */ \
1790 5 ) * sizeof(__be32))
1792 static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1794 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1795 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1799 nfsd4_create_session(struct svc_rqst *rqstp,
1800 struct nfsd4_compound_state *cstate,
1801 struct nfsd4_create_session *cr_ses)
1803 struct sockaddr *sa = svc_addr(rqstp);
1804 struct nfs4_client *conf, *unconf;
1805 struct nfsd4_session *new;
1806 struct nfsd4_conn *conn;
1807 struct nfsd4_clid_slot *cs_slot = NULL;
1809 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1811 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1812 return nfserr_inval;
1813 if (check_forechannel_attrs(cr_ses->fore_channel))
1814 return nfserr_toosmall;
1815 new = alloc_session(&cr_ses->fore_channel, nn);
1817 return nfserr_jukebox;
1818 status = nfserr_jukebox;
1819 conn = alloc_conn_from_crses(rqstp, cr_ses);
1821 goto out_free_session;
1824 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
1825 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
1826 WARN_ON_ONCE(conf && unconf);
1829 cs_slot = &conf->cl_cs_slot;
1830 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1831 if (status == nfserr_replay_cache) {
1832 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1834 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1835 status = nfserr_seq_misordered;
1838 } else if (unconf) {
1839 struct nfs4_client *old;
1840 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1841 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1842 status = nfserr_clid_inuse;
1845 cs_slot = &unconf->cl_cs_slot;
1846 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1848 /* an unconfirmed replay returns misordered */
1849 status = nfserr_seq_misordered;
1852 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
1854 status = mark_client_expired(old);
1859 move_to_confirmed(unconf);
1862 status = nfserr_stale_clientid;
1867 * We do not support RDMA or persistent sessions
1869 cr_ses->flags &= ~SESSION4_PERSIST;
1870 cr_ses->flags &= ~SESSION4_RDMA;
1872 init_session(rqstp, new, conf, cr_ses);
1873 nfsd4_init_conn(rqstp, conn, new);
1875 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1876 NFS4_MAX_SESSIONID_LEN);
1877 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1878 sizeof(struct nfsd4_channel_attrs));
1879 cs_slot->sl_seqid++;
1880 cr_ses->seqid = cs_slot->sl_seqid;
1882 /* cache solo and embedded create sessions under the state lock */
1883 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1884 nfs4_unlock_state();
1887 nfs4_unlock_state();
1890 __free_session(new);
1894 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1897 case NFS4_CDFC4_FORE:
1898 case NFS4_CDFC4_BACK:
1900 case NFS4_CDFC4_FORE_OR_BOTH:
1901 case NFS4_CDFC4_BACK_OR_BOTH:
1902 *dir = NFS4_CDFC4_BOTH;
1905 return nfserr_inval;
1908 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1910 struct nfsd4_session *session = cstate->session;
1911 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1913 spin_lock(&nn->client_lock);
1914 session->se_cb_prog = bc->bc_cb_program;
1915 session->se_cb_sec = bc->bc_cb_sec;
1916 spin_unlock(&nn->client_lock);
1918 nfsd4_probe_callback(session->se_client);
1923 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1924 struct nfsd4_compound_state *cstate,
1925 struct nfsd4_bind_conn_to_session *bcts)
1928 struct nfsd4_conn *conn;
1929 struct nfsd4_session *session;
1930 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1932 if (!nfsd4_last_compound_op(rqstp))
1933 return nfserr_not_only_op;
1935 spin_lock(&nn->client_lock);
1936 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
1937 spin_unlock(&nn->client_lock);
1938 status = nfserr_badsession;
1941 status = nfsd4_map_bcts_dir(&bcts->dir);
1944 conn = alloc_conn(rqstp, bcts->dir);
1945 status = nfserr_jukebox;
1948 nfsd4_init_conn(rqstp, conn, session);
1951 nfs4_unlock_state();
1955 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1959 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1963 nfsd4_destroy_session(struct svc_rqst *r,
1964 struct nfsd4_compound_state *cstate,
1965 struct nfsd4_destroy_session *sessionid)
1967 struct nfsd4_session *ses;
1969 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
1972 status = nfserr_not_only_op;
1973 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1974 if (!nfsd4_last_compound_op(r))
1977 dump_sessionid(__func__, &sessionid->sessionid);
1978 spin_lock(&nn->client_lock);
1979 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
1980 status = nfserr_badsession;
1982 goto out_client_lock;
1983 status = mark_session_dead_locked(ses);
1985 goto out_client_lock;
1986 unhash_session(ses);
1987 spin_unlock(&nn->client_lock);
1989 nfsd4_probe_callback_sync(ses->se_client);
1991 spin_lock(&nn->client_lock);
1995 spin_unlock(&nn->client_lock);
1997 nfs4_unlock_state();
2001 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2003 struct nfsd4_conn *c;
2005 list_for_each_entry(c, &s->se_conns, cn_persession) {
2006 if (c->cn_xprt == xpt) {
2013 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2015 struct nfs4_client *clp = ses->se_client;
2016 struct nfsd4_conn *c;
2019 spin_lock(&clp->cl_lock);
2020 c = __nfsd4_find_conn(new->cn_xprt, ses);
2022 spin_unlock(&clp->cl_lock);
2026 __nfsd4_hash_conn(new, ses);
2027 spin_unlock(&clp->cl_lock);
2028 ret = nfsd4_register_conn(new);
2030 /* oops; xprt is already down: */
2031 nfsd4_conn_lost(&new->cn_xpt_user);
2035 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2037 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2039 return args->opcnt > session->se_fchannel.maxops;
2042 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2043 struct nfsd4_session *session)
2045 struct xdr_buf *xb = &rqstp->rq_arg;
2047 return xb->len > session->se_fchannel.maxreq_sz;
2051 nfsd4_sequence(struct svc_rqst *rqstp,
2052 struct nfsd4_compound_state *cstate,
2053 struct nfsd4_sequence *seq)
2055 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2056 struct nfsd4_session *session;
2057 struct nfs4_client *clp;
2058 struct nfsd4_slot *slot;
2059 struct nfsd4_conn *conn;
2061 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2063 if (resp->opcnt != 1)
2064 return nfserr_sequence_pos;
2067 * Will be either used or freed by nfsd4_sequence_check_conn
2070 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2072 return nfserr_jukebox;
2074 spin_lock(&nn->client_lock);
2075 status = nfserr_badsession;
2076 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
2078 goto out_no_session;
2079 clp = session->se_client;
2080 status = get_client_locked(clp);
2082 goto out_no_session;
2083 status = nfsd4_get_session_locked(session);
2085 goto out_put_client;
2087 status = nfserr_too_many_ops;
2088 if (nfsd4_session_too_many_ops(rqstp, session))
2089 goto out_put_session;
2091 status = nfserr_req_too_big;
2092 if (nfsd4_request_too_big(rqstp, session))
2093 goto out_put_session;
2095 status = nfserr_badslot;
2096 if (seq->slotid >= session->se_fchannel.maxreqs)
2097 goto out_put_session;
2099 slot = session->se_slots[seq->slotid];
2100 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2102 /* We do not negotiate the number of slots yet, so set the
2103 * maxslots to the session maxreqs which is used to encode
2104 * sr_highest_slotid and the sr_target_slot id to maxslots */
2105 seq->maxslots = session->se_fchannel.maxreqs;
2107 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2108 slot->sl_flags & NFSD4_SLOT_INUSE);
2109 if (status == nfserr_replay_cache) {
2110 status = nfserr_seq_misordered;
2111 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2112 goto out_put_session;
2113 cstate->slot = slot;
2114 cstate->session = session;
2115 /* Return the cached reply status and set cstate->status
2116 * for nfsd4_proc_compound processing */
2117 status = nfsd4_replay_cache_entry(resp, seq);
2118 cstate->status = nfserr_replay_cache;
2122 goto out_put_session;
2124 nfsd4_sequence_check_conn(conn, session);
2127 /* Success! bump slot seqid */
2128 slot->sl_seqid = seq->seqid;
2129 slot->sl_flags |= NFSD4_SLOT_INUSE;
2131 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2133 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2135 cstate->slot = slot;
2136 cstate->session = session;
2139 switch (clp->cl_cb_state) {
2141 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2143 case NFSD4_CB_FAULT:
2144 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2147 seq->status_flags = 0;
2151 spin_unlock(&nn->client_lock);
2154 nfsd4_put_session(session);
2156 put_client_renew_locked(clp);
2157 goto out_no_session;
2161 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2163 struct nfs4_client *conf, *unconf, *clp;
2165 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2168 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2169 conf = find_confirmed_client(&dc->clientid, true, nn);
2170 WARN_ON_ONCE(conf && unconf);
2175 if (client_has_state(conf)) {
2176 status = nfserr_clientid_busy;
2182 status = nfserr_stale_clientid;
2188 nfs4_unlock_state();
2193 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2197 if (rc->rca_one_fs) {
2198 if (!cstate->current_fh.fh_dentry)
2199 return nfserr_nofilehandle;
2201 * We don't take advantage of the rca_one_fs case.
2202 * That's OK, it's optional, we can safely ignore it.
2208 status = nfserr_complete_already;
2209 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2210 &cstate->session->se_client->cl_flags))
2213 status = nfserr_stale_clientid;
2214 if (is_client_expired(cstate->session->se_client))
2216 * The following error isn't really legal.
2217 * But we only get here if the client just explicitly
2218 * destroyed the client. Surely it no longer cares what
2219 * error it gets back on an operation for the dead
2225 nfsd4_client_record_create(cstate->session->se_client);
2227 nfs4_unlock_state();
2232 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2233 struct nfsd4_setclientid *setclid)
2235 struct xdr_netobj clname = setclid->se_name;
2236 nfs4_verifier clverifier = setclid->se_verf;
2237 struct nfs4_client *conf, *unconf, *new;
2239 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2241 /* Cases below refer to rfc 3530 section 14.2.33: */
2243 conf = find_confirmed_client_by_name(&clname, nn);
2246 status = nfserr_clid_inuse;
2247 if (clp_used_exchangeid(conf))
2249 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2250 char addr_str[INET6_ADDRSTRLEN];
2251 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2253 dprintk("NFSD: setclientid: string in use by client "
2254 "at %s\n", addr_str);
2258 unconf = find_unconfirmed_client_by_name(&clname, nn);
2260 expire_client(unconf);
2261 status = nfserr_jukebox;
2262 new = create_client(clname, rqstp, &clverifier);
2265 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2266 /* case 1: probable callback update */
2267 copy_clid(new, conf);
2268 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2270 new->cl_minorversion = 0;
2271 gen_callback(new, setclid, rqstp);
2272 add_to_unconfirmed(new);
2273 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2274 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2275 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2278 nfs4_unlock_state();
2284 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2285 struct nfsd4_compound_state *cstate,
2286 struct nfsd4_setclientid_confirm *setclientid_confirm)
2288 struct nfs4_client *conf, *unconf;
2289 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2290 clientid_t * clid = &setclientid_confirm->sc_clientid;
2292 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2294 if (STALE_CLIENTID(clid, nn))
2295 return nfserr_stale_clientid;
2298 conf = find_confirmed_client(clid, false, nn);
2299 unconf = find_unconfirmed_client(clid, false, nn);
2301 * We try hard to give out unique clientid's, so if we get an
2302 * attempt to confirm the same clientid with a different cred,
2303 * there's a bug somewhere. Let's charitably assume it's our
2306 status = nfserr_serverfault;
2307 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2309 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2311 /* cases below refer to rfc 3530 section 14.2.34: */
2312 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2313 if (conf && !unconf) /* case 2: probable retransmit */
2315 else /* case 4: client hasn't noticed we rebooted yet? */
2316 status = nfserr_stale_clientid;
2320 if (conf) { /* case 1: callback update */
2321 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2322 nfsd4_probe_callback(conf);
2323 expire_client(unconf);
2324 } else { /* case 3: normal case; new or rebooted client */
2325 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2327 status = mark_client_expired(conf);
2330 expire_client(conf);
2332 move_to_confirmed(unconf);
2333 nfsd4_probe_callback(unconf);
2336 nfs4_unlock_state();
2340 static struct nfs4_file *nfsd4_alloc_file(void)
2342 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2345 /* OPEN Share state helper functions */
2346 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2348 unsigned int hashval = file_hashval(ino);
2350 atomic_set(&fp->fi_ref, 1);
2351 INIT_LIST_HEAD(&fp->fi_stateids);
2352 INIT_LIST_HEAD(&fp->fi_delegations);
2353 fp->fi_inode = igrab(ino);
2354 fp->fi_had_conflict = false;
2355 fp->fi_lease = NULL;
2356 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2357 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2358 spin_lock(&recall_lock);
2359 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
2360 spin_unlock(&recall_lock);
2364 nfsd4_free_slab(struct kmem_cache **slab)
2368 kmem_cache_destroy(*slab);
2373 nfsd4_free_slabs(void)
2375 nfsd4_free_slab(&openowner_slab);
2376 nfsd4_free_slab(&lockowner_slab);
2377 nfsd4_free_slab(&file_slab);
2378 nfsd4_free_slab(&stateid_slab);
2379 nfsd4_free_slab(&deleg_slab);
2383 nfsd4_init_slabs(void)
2385 openowner_slab = kmem_cache_create("nfsd4_openowners",
2386 sizeof(struct nfs4_openowner), 0, 0, NULL);
2387 if (openowner_slab == NULL)
2389 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2390 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2391 if (lockowner_slab == NULL)
2393 file_slab = kmem_cache_create("nfsd4_files",
2394 sizeof(struct nfs4_file), 0, 0, NULL);
2395 if (file_slab == NULL)
2397 stateid_slab = kmem_cache_create("nfsd4_stateids",
2398 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2399 if (stateid_slab == NULL)
2401 deleg_slab = kmem_cache_create("nfsd4_delegations",
2402 sizeof(struct nfs4_delegation), 0, 0, NULL);
2403 if (deleg_slab == NULL)
2408 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2412 void nfs4_free_openowner(struct nfs4_openowner *oo)
2414 kfree(oo->oo_owner.so_owner.data);
2415 kmem_cache_free(openowner_slab, oo);
2418 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2420 kfree(lo->lo_owner.so_owner.data);
2421 kmem_cache_free(lockowner_slab, lo);
2424 static void init_nfs4_replay(struct nfs4_replay *rp)
2426 rp->rp_status = nfserr_serverfault;
2428 rp->rp_buf = rp->rp_ibuf;
2431 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2433 struct nfs4_stateowner *sop;
2435 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2439 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2440 if (!sop->so_owner.data) {
2441 kmem_cache_free(slab, sop);
2444 sop->so_owner.len = owner->len;
2446 INIT_LIST_HEAD(&sop->so_stateids);
2447 sop->so_client = clp;
2448 init_nfs4_replay(&sop->so_replay);
2452 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2454 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2456 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2457 list_add(&oo->oo_perclient, &clp->cl_openowners);
2460 static struct nfs4_openowner *
2461 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2462 struct nfs4_openowner *oo;
2464 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2467 oo->oo_owner.so_is_open_owner = 1;
2468 oo->oo_owner.so_seqid = open->op_seqid;
2469 oo->oo_flags = NFS4_OO_NEW;
2471 oo->oo_last_closed_stid = NULL;
2472 INIT_LIST_HEAD(&oo->oo_close_lru);
2473 hash_openowner(oo, clp, strhashval);
2477 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2478 struct nfs4_openowner *oo = open->op_openowner;
2480 stp->st_stid.sc_type = NFS4_OPEN_STID;
2481 INIT_LIST_HEAD(&stp->st_lockowners);
2482 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2483 list_add(&stp->st_perfile, &fp->fi_stateids);
2484 stp->st_stateowner = &oo->oo_owner;
2487 stp->st_access_bmap = 0;
2488 stp->st_deny_bmap = 0;
2489 set_access(open->op_share_access, stp);
2490 set_deny(open->op_share_deny, stp);
2491 stp->st_openstp = NULL;
2495 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2497 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2499 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2501 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2502 oo->oo_time = get_seconds();
2506 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2509 return (sop->so_owner.len == owner->len) &&
2510 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2511 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2514 static struct nfs4_openowner *
2515 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2516 bool sessions, struct nfsd_net *nn)
2518 struct nfs4_stateowner *so;
2519 struct nfs4_openowner *oo;
2520 struct nfs4_client *clp;
2522 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
2523 if (!so->so_is_open_owner)
2525 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2527 clp = oo->oo_owner.so_client;
2528 if ((bool)clp->cl_minorversion != sessions)
2530 renew_client(oo->oo_owner.so_client);
2537 /* search file_hashtbl[] for file */
2538 static struct nfs4_file *
2539 find_file(struct inode *ino)
2541 unsigned int hashval = file_hashval(ino);
2542 struct nfs4_file *fp;
2544 spin_lock(&recall_lock);
2545 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2546 if (fp->fi_inode == ino) {
2548 spin_unlock(&recall_lock);
2552 spin_unlock(&recall_lock);
2557 * Called to check deny when READ with all zero stateid or
2558 * WRITE with all zero or all one stateid
2561 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2563 struct inode *ino = current_fh->fh_dentry->d_inode;
2564 struct nfs4_file *fp;
2565 struct nfs4_ol_stateid *stp;
2568 fp = find_file(ino);
2571 ret = nfserr_locked;
2572 /* Search for conflicting share reservations */
2573 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2574 if (test_deny(deny_type, stp) ||
2575 test_deny(NFS4_SHARE_DENY_BOTH, stp))
2584 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2586 /* We're assuming the state code never drops its reference
2587 * without first removing the lease. Since we're in this lease
2588 * callback (and since the lease code is serialized by the kernel
2589 * lock) we know the server hasn't removed the lease yet, we know
2590 * it's safe to take a reference: */
2591 atomic_inc(&dp->dl_count);
2593 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2595 /* only place dl_time is set. protected by lock_flocks*/
2596 dp->dl_time = get_seconds();
2598 nfsd4_cb_recall(dp);
2601 /* Called from break_lease() with lock_flocks() held. */
2602 static void nfsd_break_deleg_cb(struct file_lock *fl)
2604 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2605 struct nfs4_delegation *dp;
2608 WARN(1, "(%p)->fl_owner NULL\n", fl);
2611 if (fp->fi_had_conflict) {
2612 WARN(1, "duplicate break on %p\n", fp);
2616 * We don't want the locks code to timeout the lease for us;
2617 * we'll remove it ourself if a delegation isn't returned
2620 fl->fl_break_time = 0;
2622 spin_lock(&recall_lock);
2623 fp->fi_had_conflict = true;
2624 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2625 nfsd_break_one_deleg(dp);
2626 spin_unlock(&recall_lock);
2630 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2633 return lease_modify(onlist, arg);
2638 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2639 .lm_break = nfsd_break_deleg_cb,
2640 .lm_change = nfsd_change_deleg_cb,
2643 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2645 if (nfsd4_has_session(cstate))
2647 if (seqid == so->so_seqid - 1)
2648 return nfserr_replay_me;
2649 if (seqid == so->so_seqid)
2651 return nfserr_bad_seqid;
2655 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2656 struct nfsd4_open *open, struct nfsd_net *nn)
2658 clientid_t *clientid = &open->op_clientid;
2659 struct nfs4_client *clp = NULL;
2660 unsigned int strhashval;
2661 struct nfs4_openowner *oo = NULL;
2664 if (STALE_CLIENTID(&open->op_clientid, nn))
2665 return nfserr_stale_clientid;
2667 * In case we need it later, after we've already created the
2668 * file and don't want to risk a further failure:
2670 open->op_file = nfsd4_alloc_file();
2671 if (open->op_file == NULL)
2672 return nfserr_jukebox;
2674 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
2675 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
2676 open->op_openowner = oo;
2678 clp = find_confirmed_client(clientid, cstate->minorversion,
2681 return nfserr_expired;
2684 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
2685 /* Replace unconfirmed owners without checking for replay. */
2686 clp = oo->oo_owner.so_client;
2687 release_openowner(oo);
2688 open->op_openowner = NULL;
2691 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2694 clp = oo->oo_owner.so_client;
2697 oo = alloc_init_open_stateowner(strhashval, clp, open);
2699 return nfserr_jukebox;
2700 open->op_openowner = oo;
2702 open->op_stp = nfs4_alloc_stateid(clp);
2704 return nfserr_jukebox;
2708 static inline __be32
2709 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2711 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2712 return nfserr_openmode;
2717 static int share_access_to_flags(u32 share_access)
2719 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2722 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2724 struct nfs4_stid *ret;
2726 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2729 return delegstateid(ret);
2732 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2734 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2735 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2739 nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2740 struct nfs4_delegation **dp)
2743 __be32 status = nfserr_bad_stateid;
2745 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2748 flags = share_access_to_flags(open->op_share_access);
2749 status = nfs4_check_delegmode(*dp, flags);
2753 if (!nfsd4_is_deleg_cur(open))
2757 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2762 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2764 struct nfs4_ol_stateid *local;
2765 struct nfs4_openowner *oo = open->op_openowner;
2767 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2768 /* ignore lock owners */
2769 if (local->st_stateowner->so_is_open_owner == 0)
2771 /* remember if we have seen this open owner */
2772 if (local->st_stateowner == &oo->oo_owner)
2774 /* check for conflicting share reservations */
2775 if (!test_share(local, open))
2776 return nfserr_share_denied;
2781 static inline int nfs4_access_to_access(u32 nfs4_access)
2785 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2786 flags |= NFSD_MAY_READ;
2787 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2788 flags |= NFSD_MAY_WRITE;
2792 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2793 struct svc_fh *cur_fh, struct nfsd4_open *open)
2796 int oflag = nfs4_access_to_omode(open->op_share_access);
2797 int access = nfs4_access_to_access(open->op_share_access);
2799 if (!fp->fi_fds[oflag]) {
2800 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2801 &fp->fi_fds[oflag]);
2805 nfs4_file_get_access(fp, oflag);
2810 static inline __be32
2811 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2812 struct nfsd4_open *open)
2814 struct iattr iattr = {
2815 .ia_valid = ATTR_SIZE,
2818 if (!open->op_truncate)
2820 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2821 return nfserr_inval;
2822 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2826 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)
2828 u32 op_share_access = open->op_share_access;
2832 new_access = !test_access(op_share_access, stp);
2834 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2838 status = nfsd4_truncate(rqstp, cur_fh, open);
2841 int oflag = nfs4_access_to_omode(op_share_access);
2842 nfs4_file_put_access(fp, oflag);
2846 /* remember the open */
2847 set_access(op_share_access, stp);
2848 set_deny(open->op_share_deny, stp);
2855 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
2857 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2860 /* Should we give out recallable state?: */
2861 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2863 if (clp->cl_cb_state == NFSD4_CB_UP)
2866 * In the sessions case, since we don't have to establish a
2867 * separate connection for callbacks, we assume it's OK
2868 * until we hear otherwise:
2870 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2873 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2875 struct file_lock *fl;
2877 fl = locks_alloc_lock();
2880 locks_init_lock(fl);
2881 fl->fl_lmops = &nfsd_lease_mng_ops;
2882 fl->fl_flags = FL_LEASE;
2883 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2884 fl->fl_end = OFFSET_MAX;
2885 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2886 fl->fl_pid = current->tgid;
2890 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2892 struct nfs4_file *fp = dp->dl_file;
2893 struct file_lock *fl;
2896 fl = nfs4_alloc_init_lease(dp, flag);
2899 fl->fl_file = find_readable_file(fp);
2900 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2901 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2903 list_del_init(&dp->dl_perclnt);
2904 locks_free_lock(fl);
2908 fp->fi_deleg_file = get_file(fl->fl_file);
2909 atomic_set(&fp->fi_delegees, 1);
2910 list_add(&dp->dl_perfile, &fp->fi_delegations);
2914 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2916 struct nfs4_file *fp = dp->dl_file;
2919 return nfs4_setlease(dp, flag);
2920 spin_lock(&recall_lock);
2921 if (fp->fi_had_conflict) {
2922 spin_unlock(&recall_lock);
2925 atomic_inc(&fp->fi_delegees);
2926 list_add(&dp->dl_perfile, &fp->fi_delegations);
2927 spin_unlock(&recall_lock);
2928 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2932 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2934 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2935 if (status == -EAGAIN)
2936 open->op_why_no_deleg = WND4_CONTENTION;
2938 open->op_why_no_deleg = WND4_RESOURCE;
2939 switch (open->op_deleg_want) {
2940 case NFS4_SHARE_WANT_READ_DELEG:
2941 case NFS4_SHARE_WANT_WRITE_DELEG:
2942 case NFS4_SHARE_WANT_ANY_DELEG:
2944 case NFS4_SHARE_WANT_CANCEL:
2945 open->op_why_no_deleg = WND4_CANCELLED;
2947 case NFS4_SHARE_WANT_NO_DELEG:
2954 * Attempt to hand out a delegation.
2957 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2958 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2960 struct nfs4_delegation *dp;
2961 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2963 int status = 0, flag = 0;
2965 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2966 flag = NFS4_OPEN_DELEGATE_NONE;
2967 open->op_recall = 0;
2968 switch (open->op_claim_type) {
2969 case NFS4_OPEN_CLAIM_PREVIOUS:
2971 open->op_recall = 1;
2972 flag = open->op_delegate_type;
2973 if (flag == NFS4_OPEN_DELEGATE_NONE)
2976 case NFS4_OPEN_CLAIM_NULL:
2977 /* Let's not give out any delegations till everyone's
2978 * had the chance to reclaim theirs.... */
2979 if (locks_in_grace(net))
2981 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
2983 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2984 flag = NFS4_OPEN_DELEGATE_WRITE;
2986 flag = NFS4_OPEN_DELEGATE_READ;
2992 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2995 status = nfs4_set_delegation(dp, flag);
2999 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3001 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3002 STATEID_VAL(&dp->dl_stid.sc_stateid));
3004 open->op_delegate_type = flag;
3005 if (flag == NFS4_OPEN_DELEGATE_NONE) {
3006 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3007 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3008 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3010 /* 4.1 client asking for a delegation? */
3011 if (open->op_deleg_want)
3012 nfsd4_open_deleg_none_ext(open, status);
3016 unhash_stid(&dp->dl_stid);
3017 nfs4_put_delegation(dp);
3019 flag = NFS4_OPEN_DELEGATE_NONE;
3023 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3024 struct nfs4_delegation *dp)
3026 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3027 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3028 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3029 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3030 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3031 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3032 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3033 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3035 /* Otherwise the client must be confused wanting a delegation
3036 * it already has, therefore we don't return
3037 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3042 * called with nfs4_lock_state() held.
3045 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3047 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3048 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3049 struct nfs4_file *fp = NULL;
3050 struct inode *ino = current_fh->fh_dentry->d_inode;
3051 struct nfs4_ol_stateid *stp = NULL;
3052 struct nfs4_delegation *dp = NULL;
3056 * Lookup file; if found, lookup stateid and check open request,
3057 * and check for delegations in the process of being recalled.
3058 * If not found, create the nfs4_file struct
3060 fp = find_file(ino);
3062 if ((status = nfs4_check_open(fp, open, &stp)))
3064 status = nfs4_check_deleg(cl, fp, open, &dp);
3068 status = nfserr_bad_stateid;
3069 if (nfsd4_is_deleg_cur(open))
3071 status = nfserr_jukebox;
3073 open->op_file = NULL;
3074 nfsd4_init_file(fp, ino);
3078 * OPEN the file, or upgrade an existing OPEN.
3079 * If truncate fails, the OPEN fails.
3082 /* Stateid was found, this is an OPEN upgrade */
3083 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3087 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
3090 status = nfsd4_truncate(rqstp, current_fh, open);
3094 open->op_stp = NULL;
3095 init_open_stateid(stp, fp, open);
3097 update_stateid(&stp->st_stid.sc_stateid);
3098 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3100 if (nfsd4_has_session(&resp->cstate)) {
3101 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3103 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3104 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3105 open->op_why_no_deleg = WND4_NOT_WANTED;
3111 * Attempt to hand out a delegation. No error return, because the
3112 * OPEN succeeds even if we fail.
3114 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3118 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3119 STATEID_VAL(&stp->st_stid.sc_stateid));
3121 /* 4.1 client trying to upgrade/downgrade delegation? */
3122 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3123 open->op_deleg_want)
3124 nfsd4_deleg_xgrade_none_ext(open, dp);
3128 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3129 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3131 * To finish the open response, we just need to set the rflags.
3133 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3134 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3135 !nfsd4_has_session(&resp->cstate))
3136 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3141 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3143 if (open->op_openowner) {
3144 struct nfs4_openowner *oo = open->op_openowner;
3146 if (!list_empty(&oo->oo_owner.so_stateids))
3147 list_del_init(&oo->oo_close_lru);
3148 if (oo->oo_flags & NFS4_OO_NEW) {
3150 release_openowner(oo);
3151 open->op_openowner = NULL;
3153 oo->oo_flags &= ~NFS4_OO_NEW;
3157 nfsd4_free_file(open->op_file);
3159 free_generic_stateid(open->op_stp);
3162 static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3164 struct nfs4_client *found;
3166 if (STALE_CLIENTID(clid, nn))
3167 return nfserr_stale_clientid;
3168 found = find_confirmed_client(clid, session, nn);
3171 return found ? nfs_ok : nfserr_expired;
3175 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3178 struct nfs4_client *clp;
3180 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3183 dprintk("process_renew(%08x/%08x): starting\n",
3184 clid->cl_boot, clid->cl_id);
3185 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3188 status = nfserr_cb_path_down;
3189 if (!list_empty(&clp->cl_delegations)
3190 && clp->cl_cb_state != NFSD4_CB_UP)
3194 nfs4_unlock_state();
3199 nfsd4_end_grace(struct nfsd_net *nn)
3201 /* do nothing if grace period already ended */
3202 if (nn->grace_ended)
3205 dprintk("NFSD: end of grace period\n");
3206 nn->grace_ended = true;
3207 nfsd4_record_grace_done(nn, nn->boot_time);
3208 locks_end_grace(&nn->nfsd4_manager);
3210 * Now that every NFSv4 client has had the chance to recover and
3211 * to see the (possibly new, possibly shorter) lease time, we
3212 * can safely set the next grace time to the current lease time:
3214 nn->nfsd4_grace = nn->nfsd4_lease;
3218 nfs4_laundromat(struct nfsd_net *nn)
3220 struct nfs4_client *clp;
3221 struct nfs4_openowner *oo;
3222 struct nfs4_delegation *dp;
3223 struct list_head *pos, *next, reaplist;
3224 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3225 time_t t, clientid_val = nn->nfsd4_lease;
3226 time_t u, test_val = nn->nfsd4_lease;
3230 dprintk("NFSD: laundromat service - starting\n");
3231 nfsd4_end_grace(nn);
3232 INIT_LIST_HEAD(&reaplist);
3233 spin_lock(&nn->client_lock);
3234 list_for_each_safe(pos, next, &nn->client_lru) {
3235 clp = list_entry(pos, struct nfs4_client, cl_lru);
3236 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3237 t = clp->cl_time - cutoff;
3238 if (clientid_val > t)
3242 if (mark_client_expired_locked(clp)) {
3243 dprintk("NFSD: client in use (clientid %08x)\n",
3244 clp->cl_clientid.cl_id);
3247 list_move(&clp->cl_lru, &reaplist);
3249 spin_unlock(&nn->client_lock);
3250 list_for_each_safe(pos, next, &reaplist) {
3251 clp = list_entry(pos, struct nfs4_client, cl_lru);
3252 dprintk("NFSD: purging unused client (clientid %08x)\n",
3253 clp->cl_clientid.cl_id);
3256 spin_lock(&recall_lock);
3257 list_for_each_safe(pos, next, &del_recall_lru) {
3258 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3259 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3261 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3262 u = dp->dl_time - cutoff;
3267 list_move(&dp->dl_recall_lru, &reaplist);
3269 spin_unlock(&recall_lock);
3270 list_for_each_safe(pos, next, &reaplist) {
3271 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3272 unhash_delegation(dp);
3274 test_val = nn->nfsd4_lease;
3275 list_for_each_safe(pos, next, &nn->close_lru) {
3276 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3277 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3278 u = oo->oo_time - cutoff;
3283 release_openowner(oo);
3285 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3286 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3287 nfs4_unlock_state();
3288 return clientid_val;
3291 static struct workqueue_struct *laundry_wq;
3292 static void laundromat_main(struct work_struct *);
3295 laundromat_main(struct work_struct *laundry)
3298 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3300 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3303 t = nfs4_laundromat(nn);
3304 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3305 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3308 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3310 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3311 return nfserr_bad_stateid;
3316 access_permit_read(struct nfs4_ol_stateid *stp)
3318 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3319 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3320 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3324 access_permit_write(struct nfs4_ol_stateid *stp)
3326 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3327 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3331 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3333 __be32 status = nfserr_openmode;
3335 /* For lock stateid's, we test the parent open, not the lock: */
3336 if (stp->st_openstp)
3337 stp = stp->st_openstp;
3338 if ((flags & WR_STATE) && !access_permit_write(stp))
3340 if ((flags & RD_STATE) && !access_permit_read(stp))
3347 static inline __be32
3348 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3350 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3352 else if (locks_in_grace(net)) {
3353 /* Answer in remaining cases depends on existence of
3354 * conflicting state; so we must wait out the grace period. */
3355 return nfserr_grace;
3356 } else if (flags & WR_STATE)
3357 return nfs4_share_conflict(current_fh,
3358 NFS4_SHARE_DENY_WRITE);
3359 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3360 return nfs4_share_conflict(current_fh,
3361 NFS4_SHARE_DENY_READ);
3365 * Allow READ/WRITE during grace period on recovered state only for files
3366 * that are not able to provide mandatory locking.
3369 grace_disallows_io(struct net *net, struct inode *inode)
3371 return locks_in_grace(net) && mandatory_lock(inode);
3374 /* Returns true iff a is later than b: */
3375 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3377 return (s32)a->si_generation - (s32)b->si_generation > 0;
3380 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3383 * When sessions are used the stateid generation number is ignored
3386 if (has_session && in->si_generation == 0)
3389 if (in->si_generation == ref->si_generation)
3392 /* If the client sends us a stateid from the future, it's buggy: */
3393 if (stateid_generation_after(in, ref))
3394 return nfserr_bad_stateid;
3396 * However, we could see a stateid from the past, even from a
3397 * non-buggy client. For example, if the client sends a lock
3398 * while some IO is outstanding, the lock may bump si_generation
3399 * while the IO is still in flight. The client could avoid that
3400 * situation by waiting for responses on all the IO requests,
3401 * but better performance may result in retrying IO that
3402 * receives an old_stateid error if requests are rarely
3403 * reordered in flight:
3405 return nfserr_old_stateid;
3408 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3410 struct nfs4_stid *s;
3411 struct nfs4_ol_stateid *ols;
3414 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3415 return nfserr_bad_stateid;
3416 /* Client debugging aid. */
3417 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3418 char addr_str[INET6_ADDRSTRLEN];
3419 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3421 pr_warn_ratelimited("NFSD: client %s testing state ID "
3422 "with incorrect client ID\n", addr_str);
3423 return nfserr_bad_stateid;
3425 s = find_stateid(cl, stateid);
3427 return nfserr_bad_stateid;
3428 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
3431 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3433 ols = openlockstateid(s);
3434 if (ols->st_stateowner->so_is_open_owner
3435 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3436 return nfserr_bad_stateid;
3440 static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3441 struct nfs4_stid **s, bool sessions,
3442 struct nfsd_net *nn)
3444 struct nfs4_client *cl;
3447 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3448 return nfserr_bad_stateid;
3449 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3451 if (status == nfserr_stale_clientid)
3452 return nfserr_stale_stateid;
3455 *s = find_stateid_by_type(cl, stateid, typemask);
3457 return nfserr_bad_stateid;
3462 * Checks for stateid operations
3465 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
3466 stateid_t *stateid, int flags, struct file **filpp)
3468 struct nfs4_stid *s;
3469 struct nfs4_ol_stateid *stp = NULL;
3470 struct nfs4_delegation *dp = NULL;
3471 struct svc_fh *current_fh = &cstate->current_fh;
3472 struct inode *ino = current_fh->fh_dentry->d_inode;
3473 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3479 if (grace_disallows_io(net, ino))
3480 return nfserr_grace;
3482 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3483 return check_special_stateids(net, current_fh, stateid, flags);
3485 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3486 &s, cstate->minorversion, nn);
3489 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3492 switch (s->sc_type) {
3493 case NFS4_DELEG_STID:
3494 dp = delegstateid(s);
3495 status = nfs4_check_delegmode(dp, flags);
3499 *filpp = dp->dl_file->fi_deleg_file;
3502 status = nfserr_serverfault;
3507 case NFS4_OPEN_STID:
3508 case NFS4_LOCK_STID:
3509 stp = openlockstateid(s);
3510 status = nfs4_check_fh(current_fh, stp);
3513 if (stp->st_stateowner->so_is_open_owner
3514 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3516 status = nfs4_check_openmode(stp, flags);
3520 if (flags & RD_STATE)
3521 *filpp = find_readable_file(stp->st_file);
3523 *filpp = find_writeable_file(stp->st_file);
3527 return nfserr_bad_stateid;
3535 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3537 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3538 return nfserr_locks_held;
3539 release_lock_stateid(stp);
3544 * Test if the stateid is valid
3547 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3548 struct nfsd4_test_stateid *test_stateid)
3550 struct nfsd4_test_stateid_id *stateid;
3551 struct nfs4_client *cl = cstate->session->se_client;
3554 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3555 stateid->ts_id_status =
3556 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
3557 nfs4_unlock_state();
3563 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3564 struct nfsd4_free_stateid *free_stateid)
3566 stateid_t *stateid = &free_stateid->fr_stateid;
3567 struct nfs4_stid *s;
3568 struct nfs4_client *cl = cstate->session->se_client;
3569 __be32 ret = nfserr_bad_stateid;
3572 s = find_stateid(cl, stateid);
3575 switch (s->sc_type) {
3576 case NFS4_DELEG_STID:
3577 ret = nfserr_locks_held;
3579 case NFS4_OPEN_STID:
3580 case NFS4_LOCK_STID:
3581 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3584 if (s->sc_type == NFS4_LOCK_STID)
3585 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3587 ret = nfserr_locks_held;
3590 ret = nfserr_bad_stateid;
3593 nfs4_unlock_state();
3600 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3601 RD_STATE : WR_STATE;
3604 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3606 struct svc_fh *current_fh = &cstate->current_fh;
3607 struct nfs4_stateowner *sop = stp->st_stateowner;
3610 status = nfsd4_check_seqid(cstate, sop, seqid);
3613 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3615 * "Closed" stateid's exist *only* to return
3616 * nfserr_replay_me from the previous step.
3618 return nfserr_bad_stateid;
3619 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3622 return nfs4_check_fh(current_fh, stp);
3626 * Checks for sequence id mutating operations.
3629 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3630 stateid_t *stateid, char typemask,
3631 struct nfs4_ol_stateid **stpp,
3632 struct nfsd_net *nn)
3635 struct nfs4_stid *s;
3637 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3638 seqid, STATEID_VAL(stateid));
3641 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3642 cstate->minorversion, nn);
3645 *stpp = openlockstateid(s);
3646 cstate->replay_owner = (*stpp)->st_stateowner;
3648 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3651 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3652 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
3655 struct nfs4_openowner *oo;
3657 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3658 NFS4_OPEN_STID, stpp, nn);
3661 oo = openowner((*stpp)->st_stateowner);
3662 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3663 return nfserr_bad_stateid;
3668 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3669 struct nfsd4_open_confirm *oc)
3672 struct nfs4_openowner *oo;
3673 struct nfs4_ol_stateid *stp;
3674 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3676 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3677 (int)cstate->current_fh.fh_dentry->d_name.len,
3678 cstate->current_fh.fh_dentry->d_name.name);
3680 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3686 status = nfs4_preprocess_seqid_op(cstate,
3687 oc->oc_seqid, &oc->oc_req_stateid,
3688 NFS4_OPEN_STID, &stp, nn);
3691 oo = openowner(stp->st_stateowner);
3692 status = nfserr_bad_stateid;
3693 if (oo->oo_flags & NFS4_OO_CONFIRMED)
3695 oo->oo_flags |= NFS4_OO_CONFIRMED;
3696 update_stateid(&stp->st_stid.sc_stateid);
3697 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3698 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3699 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3701 nfsd4_client_record_create(oo->oo_owner.so_client);
3704 if (!cstate->replay_owner)
3705 nfs4_unlock_state();
3709 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
3711 if (!test_access(access, stp))
3713 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3714 clear_access(access, stp);
3717 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3719 switch (to_access) {
3720 case NFS4_SHARE_ACCESS_READ:
3721 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3722 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3724 case NFS4_SHARE_ACCESS_WRITE:
3725 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3726 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3728 case NFS4_SHARE_ACCESS_BOTH:
3736 reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
3739 for (i = 0; i < 4; i++) {
3740 if ((i & deny) != i)
3746 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3747 struct nfsd4_compound_state *cstate,
3748 struct nfsd4_open_downgrade *od)
3751 struct nfs4_ol_stateid *stp;
3752 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3754 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3755 (int)cstate->current_fh.fh_dentry->d_name.len,
3756 cstate->current_fh.fh_dentry->d_name.name);
3758 /* We don't yet support WANT bits: */
3759 if (od->od_deleg_want)
3760 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3764 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3765 &od->od_stateid, &stp, nn);
3768 status = nfserr_inval;
3769 if (!test_access(od->od_share_access, stp)) {
3770 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
3771 stp->st_access_bmap, od->od_share_access);
3774 if (!test_deny(od->od_share_deny, stp)) {
3775 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3776 stp->st_deny_bmap, od->od_share_deny);
3779 nfs4_stateid_downgrade(stp, od->od_share_access);
3781 reset_union_bmap_deny(od->od_share_deny, stp);
3783 update_stateid(&stp->st_stid.sc_stateid);
3784 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3787 if (!cstate->replay_owner)
3788 nfs4_unlock_state();
3792 void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3794 struct nfs4_openowner *oo;
3795 struct nfs4_ol_stateid *s;
3797 if (!so->so_is_open_owner)
3800 s = oo->oo_last_closed_stid;
3803 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3804 /* Release the last_closed_stid on the next seqid bump: */
3805 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3808 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3809 release_last_closed_stateid(oo);
3812 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3814 unhash_open_stateid(s);
3815 s->st_stid.sc_type = NFS4_CLOSED_STID;
3819 * nfs4_unlock_state() called after encode
3822 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3823 struct nfsd4_close *close)
3826 struct nfs4_openowner *oo;
3827 struct nfs4_ol_stateid *stp;
3828 struct net *net = SVC_NET(rqstp);
3829 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3831 dprintk("NFSD: nfsd4_close on file %.*s\n",
3832 (int)cstate->current_fh.fh_dentry->d_name.len,
3833 cstate->current_fh.fh_dentry->d_name.name);
3836 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3838 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3842 oo = openowner(stp->st_stateowner);
3844 update_stateid(&stp->st_stid.sc_stateid);
3845 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3847 nfsd4_close_open_stateid(stp);
3848 release_last_closed_stateid(oo);
3849 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3850 oo->oo_last_closed_stid = stp;
3852 if (list_empty(&oo->oo_owner.so_stateids)) {
3853 if (cstate->minorversion) {
3854 release_openowner(oo);
3855 cstate->replay_owner = NULL;
3858 * In the 4.0 case we need to keep the owners around a
3859 * little while to handle CLOSE replay.
3861 if (list_empty(&oo->oo_owner.so_stateids))
3862 move_to_close_lru(oo, SVC_NET(rqstp));
3866 if (!cstate->replay_owner)
3867 nfs4_unlock_state();
3872 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3873 struct nfsd4_delegreturn *dr)
3875 struct nfs4_delegation *dp;
3876 stateid_t *stateid = &dr->dr_stateid;
3877 struct nfs4_stid *s;
3879 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3881 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3885 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3886 cstate->minorversion, nn);
3889 dp = delegstateid(s);
3890 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3894 unhash_delegation(dp);
3896 nfs4_unlock_state();
3902 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3904 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
3907 end_offset(u64 start, u64 len)
3912 return end >= start ? end: NFS4_MAX_UINT64;
3915 /* last octet in a range */
3917 last_byte_offset(u64 start, u64 len)
3923 return end > start ? end - 1: NFS4_MAX_UINT64;
3926 static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
3928 return (file_hashval(inode) + cl_id
3929 + opaque_hashval(ownername->data, ownername->len))
3930 & LOCKOWNER_INO_HASH_MASK;
3934 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3935 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3936 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3937 * locking, this prevents us from being completely protocol-compliant. The
3938 * real solution to this problem is to start using unsigned file offsets in
3939 * the VFS, but this is a very deep change!
3942 nfs4_transform_lock_offset(struct file_lock *lock)
3944 if (lock->fl_start < 0)
3945 lock->fl_start = OFFSET_MAX;
3946 if (lock->fl_end < 0)
3947 lock->fl_end = OFFSET_MAX;
3950 /* Hack!: For now, we're defining this just so we can use a pointer to it
3951 * as a unique cookie to identify our (NFSv4's) posix locks. */
3952 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3956 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3958 struct nfs4_lockowner *lo;
3960 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3961 lo = (struct nfs4_lockowner *) fl->fl_owner;
3962 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3963 lo->lo_owner.so_owner.len, GFP_KERNEL);
3964 if (!deny->ld_owner.data)
3965 /* We just don't care that much */
3967 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3968 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3971 deny->ld_owner.len = 0;
3972 deny->ld_owner.data = NULL;
3973 deny->ld_clientid.cl_boot = 0;
3974 deny->ld_clientid.cl_id = 0;
3976 deny->ld_start = fl->fl_start;
3977 deny->ld_length = NFS4_MAX_UINT64;
3978 if (fl->fl_end != NFS4_MAX_UINT64)
3979 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3980 deny->ld_type = NFS4_READ_LT;
3981 if (fl->fl_type != F_RDLCK)
3982 deny->ld_type = NFS4_WRITE_LT;
3985 static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3987 struct nfs4_ol_stateid *lst;
3989 if (!same_owner_str(&lo->lo_owner, owner, clid))
3991 lst = list_first_entry(&lo->lo_owner.so_stateids,
3992 struct nfs4_ol_stateid, st_perstateowner);
3993 return lst->st_file->fi_inode == inode;
3996 static struct nfs4_lockowner *
3997 find_lockowner_str(struct inode *inode, clientid_t *clid,
3998 struct xdr_netobj *owner, struct nfsd_net *nn)
4000 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
4001 struct nfs4_lockowner *lo;
4003 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
4004 if (same_lockowner_ino(lo, inode, clid, owner))
4010 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
4012 struct inode *inode = open_stp->st_file->fi_inode;
4013 unsigned int inohash = lockowner_ino_hashval(inode,
4014 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
4015 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4017 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4018 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
4019 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
4023 * Alloc a lock owner structure.
4024 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4027 * strhashval = ownerstr_hashval
4030 static struct nfs4_lockowner *
4031 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4032 struct nfs4_lockowner *lo;
4034 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4037 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4038 lo->lo_owner.so_is_open_owner = 0;
4039 /* It is the openowner seqid that will be incremented in encode in the
4040 * case of new lockowners; so increment the lock seqid manually: */
4041 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4042 hash_lockowner(lo, strhashval, clp, open_stp);
4046 static struct nfs4_ol_stateid *
4047 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
4049 struct nfs4_ol_stateid *stp;
4050 struct nfs4_client *clp = lo->lo_owner.so_client;
4052 stp = nfs4_alloc_stateid(clp);
4055 stp->st_stid.sc_type = NFS4_LOCK_STID;
4056 list_add(&stp->st_perfile, &fp->fi_stateids);
4057 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4058 stp->st_stateowner = &lo->lo_owner;
4061 stp->st_access_bmap = 0;
4062 stp->st_deny_bmap = open_stp->st_deny_bmap;
4063 stp->st_openstp = open_stp;
4068 check_lock_length(u64 offset, u64 length)
4070 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4071 LOFF_OVERFLOW(offset, length)));
4074 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4076 struct nfs4_file *fp = lock_stp->st_file;
4077 int oflag = nfs4_access_to_omode(access);
4079 if (test_access(access, lock_stp))
4081 nfs4_file_get_access(fp, oflag);
4082 set_access(access, lock_stp);
4085 static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
4087 struct nfs4_file *fi = ost->st_file;
4088 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4089 struct nfs4_client *cl = oo->oo_owner.so_client;
4090 struct nfs4_lockowner *lo;
4091 unsigned int strhashval;
4092 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4094 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4095 &lock->v.new.owner, nn);
4097 if (!cstate->minorversion)
4098 return nfserr_bad_seqid;
4099 /* XXX: a lockowner always has exactly one stateid: */
4100 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4101 struct nfs4_ol_stateid, st_perstateowner);
4104 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4105 &lock->v.new.owner);
4106 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4108 return nfserr_jukebox;
4109 *lst = alloc_init_lock_stateid(lo, fi, ost);
4111 release_lockowner(lo);
4112 return nfserr_jukebox;
4122 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4123 struct nfsd4_lock *lock)
4125 struct nfs4_openowner *open_sop = NULL;
4126 struct nfs4_lockowner *lock_sop = NULL;
4127 struct nfs4_ol_stateid *lock_stp;
4128 struct file *filp = NULL;
4129 struct file_lock *file_lock = NULL;
4130 struct file_lock *conflock = NULL;
4132 bool new_state = false;
4135 struct net *net = SVC_NET(rqstp);
4136 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4138 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4139 (long long) lock->lk_offset,
4140 (long long) lock->lk_length);
4142 if (check_lock_length(lock->lk_offset, lock->lk_length))
4143 return nfserr_inval;
4145 if ((status = fh_verify(rqstp, &cstate->current_fh,
4146 S_IFREG, NFSD_MAY_LOCK))) {
4147 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4153 if (lock->lk_is_new) {
4154 struct nfs4_ol_stateid *open_stp = NULL;
4156 if (nfsd4_has_session(cstate))
4157 /* See rfc 5661 18.10.3: given clientid is ignored: */
4158 memcpy(&lock->v.new.clientid,
4159 &cstate->session->se_client->cl_clientid,
4160 sizeof(clientid_t));
4162 status = nfserr_stale_clientid;
4163 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4166 /* validate and update open stateid and open seqid */
4167 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4168 lock->lk_new_open_seqid,
4169 &lock->lk_new_open_stateid,
4173 open_sop = openowner(open_stp->st_stateowner);
4174 status = nfserr_bad_stateid;
4175 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4176 &lock->v.new.clientid))
4178 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4179 &lock_stp, &new_state);
4181 status = nfs4_preprocess_seqid_op(cstate,
4182 lock->lk_old_lock_seqid,
4183 &lock->lk_old_lock_stateid,
4184 NFS4_LOCK_STID, &lock_stp, nn);
4187 lock_sop = lockowner(lock_stp->st_stateowner);
4189 lkflg = setlkflg(lock->lk_type);
4190 status = nfs4_check_openmode(lock_stp, lkflg);
4194 status = nfserr_grace;
4195 if (locks_in_grace(net) && !lock->lk_reclaim)
4197 status = nfserr_no_grace;
4198 if (!locks_in_grace(net) && lock->lk_reclaim)
4201 file_lock = locks_alloc_lock();
4203 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4204 status = nfserr_jukebox;
4208 locks_init_lock(file_lock);
4209 switch (lock->lk_type) {
4212 filp = find_readable_file(lock_stp->st_file);
4214 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4215 file_lock->fl_type = F_RDLCK;
4218 case NFS4_WRITEW_LT:
4219 filp = find_writeable_file(lock_stp->st_file);
4221 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4222 file_lock->fl_type = F_WRLCK;
4225 status = nfserr_inval;
4229 status = nfserr_openmode;
4232 file_lock->fl_owner = (fl_owner_t)lock_sop;
4233 file_lock->fl_pid = current->tgid;
4234 file_lock->fl_file = filp;
4235 file_lock->fl_flags = FL_POSIX;
4236 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4237 file_lock->fl_start = lock->lk_offset;
4238 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4239 nfs4_transform_lock_offset(file_lock);
4241 conflock = locks_alloc_lock();
4243 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4244 status = nfserr_jukebox;
4248 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4250 case 0: /* success! */
4251 update_stateid(&lock_stp->st_stid.sc_stateid);
4252 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4256 case (EAGAIN): /* conflock holds conflicting lock */
4257 status = nfserr_denied;
4258 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4259 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4262 status = nfserr_deadlock;
4265 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4266 status = nfserrno(err);
4270 if (status && new_state)
4271 release_lockowner(lock_sop);
4272 if (!cstate->replay_owner)
4273 nfs4_unlock_state();
4275 locks_free_lock(file_lock);
4277 locks_free_lock(conflock);
4282 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4283 * so we do a temporary open here just to get an open file to pass to
4284 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4287 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4290 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4292 err = nfserrno(vfs_test_lock(file, lock));
4302 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4303 struct nfsd4_lockt *lockt)
4305 struct inode *inode;
4306 struct file_lock *file_lock = NULL;
4307 struct nfs4_lockowner *lo;
4309 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4311 if (locks_in_grace(SVC_NET(rqstp)))
4312 return nfserr_grace;
4314 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4315 return nfserr_inval;
4319 if (!nfsd4_has_session(cstate)) {
4320 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4325 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4328 inode = cstate->current_fh.fh_dentry->d_inode;
4329 file_lock = locks_alloc_lock();
4331 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4332 status = nfserr_jukebox;
4335 locks_init_lock(file_lock);
4336 switch (lockt->lt_type) {
4339 file_lock->fl_type = F_RDLCK;
4342 case NFS4_WRITEW_LT:
4343 file_lock->fl_type = F_WRLCK;
4346 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4347 status = nfserr_inval;
4351 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
4353 file_lock->fl_owner = (fl_owner_t)lo;
4354 file_lock->fl_pid = current->tgid;
4355 file_lock->fl_flags = FL_POSIX;
4357 file_lock->fl_start = lockt->lt_offset;
4358 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4360 nfs4_transform_lock_offset(file_lock);
4362 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
4366 if (file_lock->fl_type != F_UNLCK) {
4367 status = nfserr_denied;
4368 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
4371 nfs4_unlock_state();
4373 locks_free_lock(file_lock);
4378 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4379 struct nfsd4_locku *locku)
4381 struct nfs4_ol_stateid *stp;
4382 struct file *filp = NULL;
4383 struct file_lock *file_lock = NULL;
4386 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4388 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4389 (long long) locku->lu_offset,
4390 (long long) locku->lu_length);
4392 if (check_lock_length(locku->lu_offset, locku->lu_length))
4393 return nfserr_inval;
4397 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4398 &locku->lu_stateid, NFS4_LOCK_STID,
4402 filp = find_any_file(stp->st_file);
4404 status = nfserr_lock_range;
4407 file_lock = locks_alloc_lock();
4409 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4410 status = nfserr_jukebox;
4413 locks_init_lock(file_lock);
4414 file_lock->fl_type = F_UNLCK;
4415 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4416 file_lock->fl_pid = current->tgid;
4417 file_lock->fl_file = filp;
4418 file_lock->fl_flags = FL_POSIX;
4419 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4420 file_lock->fl_start = locku->lu_offset;
4422 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4424 nfs4_transform_lock_offset(file_lock);
4427 * Try to unlock the file in the VFS.
4429 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
4431 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4435 * OK, unlock succeeded; the only thing left to do is update the stateid.
4437 update_stateid(&stp->st_stid.sc_stateid);
4438 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4441 if (!cstate->replay_owner)
4442 nfs4_unlock_state();
4444 locks_free_lock(file_lock);
4448 status = nfserrno(err);
4454 * 1: locks held by lockowner
4455 * 0: no locks held by lockowner
4458 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4460 struct file_lock **flpp;
4461 struct inode *inode = filp->fi_inode;
4465 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4466 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4477 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4478 struct nfsd4_compound_state *cstate,
4479 struct nfsd4_release_lockowner *rlockowner)
4481 clientid_t *clid = &rlockowner->rl_clientid;
4482 struct nfs4_stateowner *sop;
4483 struct nfs4_lockowner *lo;
4484 struct nfs4_ol_stateid *stp;
4485 struct xdr_netobj *owner = &rlockowner->rl_owner;
4486 struct list_head matches;
4487 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
4489 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4491 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4492 clid->cl_boot, clid->cl_id);
4496 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4500 status = nfserr_locks_held;
4501 INIT_LIST_HEAD(&matches);
4503 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4504 if (sop->so_is_open_owner)
4506 if (!same_owner_str(sop, owner, clid))
4508 list_for_each_entry(stp, &sop->so_stateids,
4510 lo = lockowner(sop);
4511 if (check_for_locks(stp->st_file, lo))
4513 list_add(&lo->lo_list, &matches);
4516 /* Clients probably won't expect us to return with some (but not all)
4517 * of the lockowner state released; so don't release any until all
4518 * have been checked. */
4520 while (!list_empty(&matches)) {
4521 lo = list_entry(matches.next, struct nfs4_lockowner,
4523 /* unhash_stateowner deletes so_perclient only
4524 * for openowners. */
4525 list_del(&lo->lo_list);
4526 release_lockowner(lo);
4529 nfs4_unlock_state();
4533 static inline struct nfs4_client_reclaim *
4536 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4540 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
4542 struct nfs4_client_reclaim *crp;
4544 crp = nfsd4_find_reclaim_client(name, nn);
4545 return (crp && crp->cr_clp);
4549 * failure => all reset bets are off, nfserr_no_grace...
4551 struct nfs4_client_reclaim *
4552 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
4554 unsigned int strhashval;
4555 struct nfs4_client_reclaim *crp;
4557 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4558 crp = alloc_reclaim();
4560 strhashval = clientstr_hashval(name);
4561 INIT_LIST_HEAD(&crp->cr_strhash);
4562 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
4563 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4565 nn->reclaim_str_hashtbl_size++;
4571 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
4573 list_del(&crp->cr_strhash);
4575 nn->reclaim_str_hashtbl_size--;
4579 nfs4_release_reclaim(struct nfsd_net *nn)
4581 struct nfs4_client_reclaim *crp = NULL;
4584 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4585 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4586 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
4587 struct nfs4_client_reclaim, cr_strhash);
4588 nfs4_remove_reclaim_record(crp, nn);
4591 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
4595 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4596 struct nfs4_client_reclaim *
4597 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
4599 unsigned int strhashval;
4600 struct nfs4_client_reclaim *crp = NULL;
4602 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
4604 strhashval = clientstr_hashval(recdir);
4605 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
4606 if (same_name(crp->cr_recdir, recdir)) {
4614 * Called from OPEN. Look for clientid in reclaim list.
4617 nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
4619 struct nfs4_client *clp;
4621 /* find clientid in conf_id_hashtbl */
4622 clp = find_confirmed_client(clid, sessions, nn);
4624 return nfserr_reclaim_bad;
4626 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
4629 #ifdef CONFIG_NFSD_FAULT_INJECTION
4631 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4633 if (mark_client_expired(clp))
4639 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4641 char buf[INET6_ADDRSTRLEN];
4642 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4643 printk(KERN_INFO "NFS Client: %s\n", buf);
4647 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4650 char buf[INET6_ADDRSTRLEN];
4651 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4652 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4655 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4657 struct nfs4_openowner *oop;
4658 struct nfs4_lockowner *lop, *lo_next;
4659 struct nfs4_ol_stateid *stp, *st_next;
4662 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4663 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4664 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4676 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4678 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4681 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4683 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4684 nfsd_print_count(clp, count, "locked files");
4688 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4690 struct nfs4_openowner *oop, *next;
4693 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4703 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4705 return nfsd_foreach_client_open(clp, max, release_openowner);
4708 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4710 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4711 nfsd_print_count(clp, count, "open files");
4715 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4716 struct list_head *victims)
4718 struct nfs4_delegation *dp, *next;
4721 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4723 list_move(&dp->dl_recall_lru, victims);
4730 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4732 struct nfs4_delegation *dp, *next;
4736 spin_lock(&recall_lock);
4737 count = nfsd_find_all_delegations(clp, max, &victims);
4738 spin_unlock(&recall_lock);
4740 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4741 unhash_delegation(dp);
4746 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4748 struct nfs4_delegation *dp, *next;
4752 spin_lock(&recall_lock);
4753 count = nfsd_find_all_delegations(clp, max, &victims);
4754 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4755 nfsd_break_one_deleg(dp);
4756 spin_unlock(&recall_lock);
4761 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4765 spin_lock(&recall_lock);
4766 count = nfsd_find_all_delegations(clp, max, NULL);
4767 spin_unlock(&recall_lock);
4769 nfsd_print_count(clp, count, "delegations");
4773 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
4775 struct nfs4_client *clp, *next;
4777 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4779 if (!nfsd_netns_ready(nn))
4782 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
4783 count += func(clp, max - count);
4784 if ((max != 0) && (count >= max))
4791 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4793 struct nfs4_client *clp;
4794 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4796 if (!nfsd_netns_ready(nn))
4799 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4800 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4806 #endif /* CONFIG_NFSD_FAULT_INJECTION */
4808 /* initialization to perform at module load time: */
4811 nfs4_state_init(void)
4813 INIT_LIST_HEAD(&del_recall_lru);
4817 * Since the lifetime of a delegation isn't limited to that of an open, a
4818 * client may quite reasonably hang on to a delegation as long as it has
4819 * the inode cached. This becomes an obvious problem the first time a
4820 * client's inode cache approaches the size of the server's total memory.
4822 * For now we avoid this problem by imposing a hard limit on the number
4823 * of delegations, which varies according to the server's memory size.
4826 set_max_delegations(void)
4829 * Allow at most 4 delegations per megabyte of RAM. Quick
4830 * estimates suggest that in the worst case (where every delegation
4831 * is for a different inode), a delegation could take about 1.5K,
4832 * giving a worst case usage of about 6% of memory.
4834 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4837 static int nfs4_state_create_net(struct net *net)
4839 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4842 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4843 CLIENT_HASH_SIZE, GFP_KERNEL);
4844 if (!nn->conf_id_hashtbl)
4846 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4847 CLIENT_HASH_SIZE, GFP_KERNEL);
4848 if (!nn->unconf_id_hashtbl)
4850 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4851 OWNER_HASH_SIZE, GFP_KERNEL);
4852 if (!nn->ownerstr_hashtbl)
4854 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4855 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4856 if (!nn->lockowner_ino_hashtbl)
4857 goto err_lockowner_ino;
4858 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4859 SESSION_HASH_SIZE, GFP_KERNEL);
4860 if (!nn->sessionid_hashtbl)
4863 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4864 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
4865 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
4867 for (i = 0; i < OWNER_HASH_SIZE; i++)
4868 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
4869 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4870 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
4871 for (i = 0; i < SESSION_HASH_SIZE; i++)
4872 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
4873 nn->conf_name_tree = RB_ROOT;
4874 nn->unconf_name_tree = RB_ROOT;
4875 INIT_LIST_HEAD(&nn->client_lru);
4876 INIT_LIST_HEAD(&nn->close_lru);
4877 spin_lock_init(&nn->client_lock);
4879 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
4885 kfree(nn->lockowner_ino_hashtbl);
4887 kfree(nn->ownerstr_hashtbl);
4889 kfree(nn->unconf_id_hashtbl);
4891 kfree(nn->conf_id_hashtbl);
4897 nfs4_state_destroy_net(struct net *net)
4900 struct nfs4_client *clp = NULL;
4901 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4902 struct rb_node *node, *tmp;
4904 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4905 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4906 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4907 destroy_client(clp);
4911 node = rb_first(&nn->unconf_name_tree);
4912 while (node != NULL) {
4914 node = rb_next(tmp);
4915 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4916 rb_erase(tmp, &nn->unconf_name_tree);
4917 destroy_client(clp);
4920 kfree(nn->sessionid_hashtbl);
4921 kfree(nn->lockowner_ino_hashtbl);
4922 kfree(nn->ownerstr_hashtbl);
4923 kfree(nn->unconf_id_hashtbl);
4924 kfree(nn->conf_id_hashtbl);
4929 nfs4_state_start_net(struct net *net)
4931 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4934 ret = nfs4_state_create_net(net);
4937 nfsd4_client_tracking_init(net);
4938 nn->boot_time = get_seconds();
4939 locks_start_grace(net, &nn->nfsd4_manager);
4940 nn->grace_ended = false;
4941 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
4942 nn->nfsd4_grace, net);
4943 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
4947 /* initialization to perform when the nfsd service is started: */
4950 nfs4_state_start(void)
4954 ret = set_callback_cred();
4957 laundry_wq = create_singlethread_workqueue("nfsd4");
4958 if (laundry_wq == NULL) {
4962 ret = nfsd4_create_callback_queue();
4964 goto out_free_laundry;
4966 set_max_delegations();
4971 destroy_workqueue(laundry_wq);
4976 /* should be called with the state lock held */
4978 nfs4_state_shutdown_net(struct net *net)
4980 struct nfs4_delegation *dp = NULL;
4981 struct list_head *pos, *next, reaplist;
4982 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4984 cancel_delayed_work_sync(&nn->laundromat_work);
4985 locks_end_grace(&nn->nfsd4_manager);
4987 INIT_LIST_HEAD(&reaplist);
4988 spin_lock(&recall_lock);
4989 list_for_each_safe(pos, next, &del_recall_lru) {
4990 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4991 if (dp->dl_stid.sc_client->net != net)
4993 list_move(&dp->dl_recall_lru, &reaplist);
4995 spin_unlock(&recall_lock);
4996 list_for_each_safe(pos, next, &reaplist) {
4997 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4998 unhash_delegation(dp);
5001 nfsd4_client_tracking_exit(net);
5002 nfs4_state_destroy_net(net);
5006 nfs4_state_shutdown(void)
5008 destroy_workqueue(laundry_wq);
5009 nfsd4_destroy_callback_queue();
5013 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5015 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5016 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5020 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5022 if (cstate->minorversion) {
5023 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5024 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5029 clear_current_stateid(struct nfsd4_compound_state *cstate)
5031 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5035 * functions to set current state id
5038 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5040 put_stateid(cstate, &odp->od_stateid);
5044 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5046 put_stateid(cstate, &open->op_stateid);
5050 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5052 put_stateid(cstate, &close->cl_stateid);
5056 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5058 put_stateid(cstate, &lock->lk_resp_stateid);
5062 * functions to consume current state id
5066 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5068 get_stateid(cstate, &odp->od_stateid);
5072 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5074 get_stateid(cstate, &drp->dr_stateid);
5078 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5080 get_stateid(cstate, &fsp->fr_stateid);
5084 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5086 get_stateid(cstate, &setattr->sa_stateid);
5090 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5092 get_stateid(cstate, &close->cl_stateid);
5096 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5098 get_stateid(cstate, &locku->lu_stateid);
5102 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5104 get_stateid(cstate, &read->rd_stateid);
5108 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5110 get_stateid(cstate, &write->wr_stateid);