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 kref *);
99 /* Must be called under the client_lock */
100 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
102 kref_put(&ses->se_ref, free_session);
105 static void nfsd4_get_session(struct nfsd4_session *ses)
107 kref_get(&ses->se_ref);
111 nfs4_unlock_state(void)
113 mutex_unlock(&client_mutex);
116 static bool is_client_expired(struct nfs4_client *clp)
118 return clp->cl_time == 0;
121 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
123 if (atomic_read(&clp->cl_refcount))
124 return nfserr_jukebox;
129 static __be32 mark_client_expired(struct nfs4_client *clp)
131 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
134 spin_lock(&nn->client_lock);
135 ret = mark_client_expired_locked(clp);
136 spin_unlock(&nn->client_lock);
140 static __be32 get_client_locked(struct nfs4_client *clp)
142 if (is_client_expired(clp))
143 return nfserr_expired;
144 atomic_inc(&clp->cl_refcount);
148 /* must be called under the client_lock */
150 renew_client_locked(struct nfs4_client *clp)
152 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
154 if (is_client_expired(clp)) {
156 printk("%s: client (clientid %08x/%08x) already expired\n",
158 clp->cl_clientid.cl_boot,
159 clp->cl_clientid.cl_id);
163 dprintk("renewing client (clientid %08x/%08x)\n",
164 clp->cl_clientid.cl_boot,
165 clp->cl_clientid.cl_id);
166 list_move_tail(&clp->cl_lru, &nn->client_lru);
167 clp->cl_time = get_seconds();
171 renew_client(struct nfs4_client *clp)
173 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
175 spin_lock(&nn->client_lock);
176 renew_client_locked(clp);
177 spin_unlock(&nn->client_lock);
180 void put_client_renew_locked(struct nfs4_client *clp)
182 if (!atomic_dec_and_test(&clp->cl_refcount))
184 if (!is_client_expired(clp))
185 renew_client_locked(clp);
188 void put_client_renew(struct nfs4_client *clp)
190 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
194 if (!is_client_expired(clp))
195 renew_client_locked(clp);
196 spin_unlock(&nn->client_lock);
201 opaque_hashval(const void *ptr, int nbytes)
203 unsigned char *cptr = (unsigned char *) ptr;
213 static struct list_head del_recall_lru;
215 static void nfsd4_free_file(struct nfs4_file *f)
217 kmem_cache_free(file_slab, f);
221 put_nfs4_file(struct nfs4_file *fi)
223 if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
224 list_del(&fi->fi_hash);
225 spin_unlock(&recall_lock);
232 get_nfs4_file(struct nfs4_file *fi)
234 atomic_inc(&fi->fi_ref);
237 static int num_delegations;
238 unsigned long max_delegations;
241 * Open owner state (share locks)
244 /* hash tables for lock and open owners */
245 #define OWNER_HASH_BITS 8
246 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
247 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
249 static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
253 ret = opaque_hashval(ownername->data, ownername->len);
255 return ret & OWNER_HASH_MASK;
258 /* hash table for nfs4_file */
259 #define FILE_HASH_BITS 8
260 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
262 static unsigned int file_hashval(struct inode *ino)
264 /* XXX: why are we hashing on inode pointer, anyway? */
265 return hash_ptr(ino, FILE_HASH_BITS);
268 static struct list_head file_hashtbl[FILE_HASH_SIZE];
270 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
272 WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
273 atomic_inc(&fp->fi_access[oflag]);
276 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
278 if (oflag == O_RDWR) {
279 __nfs4_file_get_access(fp, O_RDONLY);
280 __nfs4_file_get_access(fp, O_WRONLY);
282 __nfs4_file_get_access(fp, oflag);
285 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
287 if (fp->fi_fds[oflag]) {
288 fput(fp->fi_fds[oflag]);
289 fp->fi_fds[oflag] = NULL;
293 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
295 if (atomic_dec_and_test(&fp->fi_access[oflag])) {
296 nfs4_file_put_fd(fp, oflag);
298 * It's also safe to get rid of the RDWR open *if*
299 * we no longer have need of the other kind of access
300 * or if we already have the other kind of open:
302 if (fp->fi_fds[1-oflag]
303 || atomic_read(&fp->fi_access[1 - oflag]) == 0)
304 nfs4_file_put_fd(fp, O_RDWR);
308 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
310 if (oflag == O_RDWR) {
311 __nfs4_file_put_access(fp, O_RDONLY);
312 __nfs4_file_put_access(fp, O_WRONLY);
314 __nfs4_file_put_access(fp, oflag);
317 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
320 struct idr *stateids = &cl->cl_stateids;
321 static int min_stateid = 0;
322 struct nfs4_stid *stid;
325 stid = kmem_cache_alloc(slab, GFP_KERNEL);
329 new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
332 stid->sc_client = cl;
334 stid->sc_stateid.si_opaque.so_id = new_id;
335 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
336 /* Will be incremented before return to client: */
337 stid->sc_stateid.si_generation = 0;
340 * It shouldn't be a problem to reuse an opaque stateid value.
341 * I don't think it is for 4.1. But with 4.0 I worry that, for
342 * example, a stray write retransmission could be accepted by
343 * the server when it should have been rejected. Therefore,
344 * adopt a trick from the sctp code to attempt to maximize the
345 * amount of time until an id is reused, by ensuring they always
346 * "increase" (mod INT_MAX):
349 min_stateid = new_id+1;
350 if (min_stateid == INT_MAX)
358 static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
360 return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
363 static struct nfs4_delegation *
364 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
366 struct nfs4_delegation *dp;
367 struct nfs4_file *fp = stp->st_file;
369 dprintk("NFSD alloc_init_deleg\n");
371 * Major work on the lease subsystem (for example, to support
372 * calbacks on stat) will be required before we can support
373 * write delegations properly.
375 if (type != NFS4_OPEN_DELEGATE_READ)
377 if (fp->fi_had_conflict)
379 if (num_delegations > max_delegations)
381 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
384 dp->dl_stid.sc_type = NFS4_DELEG_STID;
386 * delegation seqid's are never incremented. The 4.1 special
387 * meaning of seqid 0 isn't meaningful, really, but let's avoid
388 * 0 anyway just for consistency and use 1:
390 dp->dl_stid.sc_stateid.si_generation = 1;
392 INIT_LIST_HEAD(&dp->dl_perfile);
393 INIT_LIST_HEAD(&dp->dl_perclnt);
394 INIT_LIST_HEAD(&dp->dl_recall_lru);
398 fh_copy_shallow(&dp->dl_fh, ¤t_fh->fh_handle);
400 atomic_set(&dp->dl_count, 1);
401 nfsd4_init_callback(&dp->dl_recall);
405 static void free_stid(struct nfs4_stid *s, struct kmem_cache *slab)
407 struct idr *stateids = &s->sc_client->cl_stateids;
409 idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
410 kmem_cache_free(slab, s);
414 nfs4_put_delegation(struct nfs4_delegation *dp)
416 if (atomic_dec_and_test(&dp->dl_count)) {
417 dprintk("NFSD: freeing dp %p\n",dp);
418 put_nfs4_file(dp->dl_file);
419 free_stid(&dp->dl_stid, deleg_slab);
424 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
426 if (atomic_dec_and_test(&fp->fi_delegees)) {
427 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
429 fput(fp->fi_deleg_file);
430 fp->fi_deleg_file = NULL;
434 static void unhash_stid(struct nfs4_stid *s)
439 /* Called under the state lock. */
441 unhash_delegation(struct nfs4_delegation *dp)
443 unhash_stid(&dp->dl_stid);
444 list_del_init(&dp->dl_perclnt);
445 spin_lock(&recall_lock);
446 list_del_init(&dp->dl_perfile);
447 list_del_init(&dp->dl_recall_lru);
448 spin_unlock(&recall_lock);
449 nfs4_put_deleg_lease(dp->dl_file);
450 nfs4_put_delegation(dp);
457 static unsigned int clientid_hashval(u32 id)
459 return id & CLIENT_HASH_MASK;
462 static unsigned int clientstr_hashval(const char *name)
464 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
468 * We store the NONE, READ, WRITE, and BOTH bits separately in the
469 * st_{access,deny}_bmap field of the stateid, in order to track not
470 * only what share bits are currently in force, but also what
471 * combinations of share bits previous opens have used. This allows us
472 * to enforce the recommendation of rfc 3530 14.2.19 that the server
473 * return an error if the client attempt to downgrade to a combination
474 * of share bits not explicable by closing some of its previous opens.
476 * XXX: This enforcement is actually incomplete, since we don't keep
477 * track of access/deny bit combinations; so, e.g., we allow:
479 * OPEN allow read, deny write
480 * OPEN allow both, deny none
481 * DOWNGRADE allow read, deny none
483 * which we should reject.
486 bmap_to_share_mode(unsigned long bmap) {
488 unsigned int access = 0;
490 for (i = 1; i < 4; i++) {
491 if (test_bit(i, &bmap))
498 test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
499 unsigned int access, deny;
501 access = bmap_to_share_mode(stp->st_access_bmap);
502 deny = bmap_to_share_mode(stp->st_deny_bmap);
503 if ((access & open->op_share_deny) || (deny & open->op_share_access))
508 /* set share access for a given stateid */
510 set_access(u32 access, struct nfs4_ol_stateid *stp)
512 __set_bit(access, &stp->st_access_bmap);
515 /* clear share access for a given stateid */
517 clear_access(u32 access, struct nfs4_ol_stateid *stp)
519 __clear_bit(access, &stp->st_access_bmap);
522 /* test whether a given stateid has access */
524 test_access(u32 access, struct nfs4_ol_stateid *stp)
526 return test_bit(access, &stp->st_access_bmap);
529 /* set share deny for a given stateid */
531 set_deny(u32 access, struct nfs4_ol_stateid *stp)
533 __set_bit(access, &stp->st_deny_bmap);
536 /* clear share deny for a given stateid */
538 clear_deny(u32 access, struct nfs4_ol_stateid *stp)
540 __clear_bit(access, &stp->st_deny_bmap);
543 /* test whether a given stateid is denying specific access */
545 test_deny(u32 access, struct nfs4_ol_stateid *stp)
547 return test_bit(access, &stp->st_deny_bmap);
550 static int nfs4_access_to_omode(u32 access)
552 switch (access & NFS4_SHARE_ACCESS_BOTH) {
553 case NFS4_SHARE_ACCESS_READ:
555 case NFS4_SHARE_ACCESS_WRITE:
557 case NFS4_SHARE_ACCESS_BOTH:
564 /* release all access and file references for a given stateid */
566 release_all_access(struct nfs4_ol_stateid *stp)
570 for (i = 1; i < 4; i++) {
571 if (test_access(i, stp))
572 nfs4_file_put_access(stp->st_file,
573 nfs4_access_to_omode(i));
574 clear_access(i, stp);
578 static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
580 list_del(&stp->st_perfile);
581 list_del(&stp->st_perstateowner);
584 static void close_generic_stateid(struct nfs4_ol_stateid *stp)
586 release_all_access(stp);
587 put_nfs4_file(stp->st_file);
591 static void free_generic_stateid(struct nfs4_ol_stateid *stp)
593 free_stid(&stp->st_stid, stateid_slab);
596 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
600 unhash_generic_stateid(stp);
601 unhash_stid(&stp->st_stid);
602 file = find_any_file(stp->st_file);
604 locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
605 close_generic_stateid(stp);
606 free_generic_stateid(stp);
609 static void unhash_lockowner(struct nfs4_lockowner *lo)
611 struct nfs4_ol_stateid *stp;
613 list_del(&lo->lo_owner.so_strhash);
614 list_del(&lo->lo_perstateid);
615 list_del(&lo->lo_owner_ino_hash);
616 while (!list_empty(&lo->lo_owner.so_stateids)) {
617 stp = list_first_entry(&lo->lo_owner.so_stateids,
618 struct nfs4_ol_stateid, st_perstateowner);
619 release_lock_stateid(stp);
623 static void release_lockowner(struct nfs4_lockowner *lo)
625 unhash_lockowner(lo);
626 nfs4_free_lockowner(lo);
630 release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
632 struct nfs4_lockowner *lo;
634 while (!list_empty(&open_stp->st_lockowners)) {
635 lo = list_entry(open_stp->st_lockowners.next,
636 struct nfs4_lockowner, lo_perstateid);
637 release_lockowner(lo);
641 static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
643 unhash_generic_stateid(stp);
644 release_stateid_lockowners(stp);
645 close_generic_stateid(stp);
648 static void release_open_stateid(struct nfs4_ol_stateid *stp)
650 unhash_open_stateid(stp);
651 unhash_stid(&stp->st_stid);
652 free_generic_stateid(stp);
655 static void unhash_openowner(struct nfs4_openowner *oo)
657 struct nfs4_ol_stateid *stp;
659 list_del(&oo->oo_owner.so_strhash);
660 list_del(&oo->oo_perclient);
661 while (!list_empty(&oo->oo_owner.so_stateids)) {
662 stp = list_first_entry(&oo->oo_owner.so_stateids,
663 struct nfs4_ol_stateid, st_perstateowner);
664 release_open_stateid(stp);
668 static void release_last_closed_stateid(struct nfs4_openowner *oo)
670 struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
673 unhash_stid(&s->st_stid);
674 free_generic_stateid(s);
675 oo->oo_last_closed_stid = NULL;
679 static void release_openowner(struct nfs4_openowner *oo)
681 unhash_openowner(oo);
682 list_del(&oo->oo_close_lru);
683 release_last_closed_stateid(oo);
684 nfs4_free_openowner(oo);
688 hash_sessionid(struct nfs4_sessionid *sessionid)
690 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
692 return sid->sequence % SESSION_HASH_SIZE;
697 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
699 u32 *ptr = (u32 *)(&sessionid->data[0]);
700 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
704 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
711 gen_sessionid(struct nfsd4_session *ses)
713 struct nfs4_client *clp = ses->se_client;
714 struct nfsd4_sessionid *sid;
716 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
717 sid->clientid = clp->cl_clientid;
718 sid->sequence = current_sessionid++;
723 * The protocol defines ca_maxresponssize_cached to include the size of
724 * the rpc header, but all we need to cache is the data starting after
725 * the end of the initial SEQUENCE operation--the rest we regenerate
726 * each time. Therefore we can advertise a ca_maxresponssize_cached
727 * value that is the number of bytes in our cache plus a few additional
728 * bytes. In order to stay on the safe side, and not promise more than
729 * we can cache, those additional bytes must be the minimum possible: 24
730 * bytes of rpc header (xid through accept state, with AUTH_NULL
731 * verifier), 12 for the compound header (with zero-length tag), and 44
732 * for the SEQUENCE op response:
734 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
737 free_session_slots(struct nfsd4_session *ses)
741 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
742 kfree(ses->se_slots[i]);
746 * We don't actually need to cache the rpc and session headers, so we
747 * can allocate a little less for each slot:
749 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
751 return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
754 static int nfsd4_sanitize_slot_size(u32 size)
756 size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
757 size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
763 * XXX: If we run out of reserved DRC memory we could (up to a point)
764 * re-negotiate active sessions and reduce their slot usage to make
765 * room for new connections. For now we just fail the create session.
767 static int nfsd4_get_drc_mem(int slotsize, u32 num)
771 num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
773 spin_lock(&nfsd_drc_lock);
774 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
775 nfsd_drc_max_mem - nfsd_drc_mem_used);
776 num = min_t(int, num, avail / slotsize);
777 nfsd_drc_mem_used += num * slotsize;
778 spin_unlock(&nfsd_drc_lock);
783 static void nfsd4_put_drc_mem(int slotsize, int num)
785 spin_lock(&nfsd_drc_lock);
786 nfsd_drc_mem_used -= slotsize * num;
787 spin_unlock(&nfsd_drc_lock);
790 static struct nfsd4_session *__alloc_session(int slotsize, int numslots)
792 struct nfsd4_session *new;
795 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
796 + sizeof(struct nfsd4_session) > PAGE_SIZE);
797 mem = numslots * sizeof(struct nfsd4_slot *);
799 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
802 /* allocate each struct nfsd4_slot and data cache in one piece */
803 for (i = 0; i < numslots; i++) {
804 mem = sizeof(struct nfsd4_slot) + slotsize;
805 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
806 if (!new->se_slots[i])
812 kfree(new->se_slots[i]);
817 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new,
818 struct nfsd4_channel_attrs *req,
819 int numslots, int slotsize,
822 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
824 new->maxreqs = numslots;
825 new->maxresp_cached = min_t(u32, req->maxresp_cached,
826 slotsize + NFSD_MIN_HDR_SEQ_SZ);
827 new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
828 new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
829 new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
832 static void free_conn(struct nfsd4_conn *c)
834 svc_xprt_put(c->cn_xprt);
838 static void nfsd4_conn_lost(struct svc_xpt_user *u)
840 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
841 struct nfs4_client *clp = c->cn_session->se_client;
843 spin_lock(&clp->cl_lock);
844 if (!list_empty(&c->cn_persession)) {
845 list_del(&c->cn_persession);
848 nfsd4_probe_callback(clp);
849 spin_unlock(&clp->cl_lock);
852 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
854 struct nfsd4_conn *conn;
856 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
859 svc_xprt_get(rqstp->rq_xprt);
860 conn->cn_xprt = rqstp->rq_xprt;
861 conn->cn_flags = flags;
862 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
866 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
868 conn->cn_session = ses;
869 list_add(&conn->cn_persession, &ses->se_conns);
872 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
874 struct nfs4_client *clp = ses->se_client;
876 spin_lock(&clp->cl_lock);
877 __nfsd4_hash_conn(conn, ses);
878 spin_unlock(&clp->cl_lock);
881 static int nfsd4_register_conn(struct nfsd4_conn *conn)
883 conn->cn_xpt_user.callback = nfsd4_conn_lost;
884 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
887 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
891 nfsd4_hash_conn(conn, ses);
892 ret = nfsd4_register_conn(conn);
894 /* oops; xprt is already down: */
895 nfsd4_conn_lost(&conn->cn_xpt_user);
896 if (conn->cn_flags & NFS4_CDFC4_BACK) {
897 /* callback channel may be back up */
898 nfsd4_probe_callback(ses->se_client);
902 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
904 u32 dir = NFS4_CDFC4_FORE;
906 if (cses->flags & SESSION4_BACK_CHAN)
907 dir |= NFS4_CDFC4_BACK;
908 return alloc_conn(rqstp, dir);
911 /* must be called under client_lock */
912 static void nfsd4_del_conns(struct nfsd4_session *s)
914 struct nfs4_client *clp = s->se_client;
915 struct nfsd4_conn *c;
917 spin_lock(&clp->cl_lock);
918 while (!list_empty(&s->se_conns)) {
919 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
920 list_del_init(&c->cn_persession);
921 spin_unlock(&clp->cl_lock);
923 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
926 spin_lock(&clp->cl_lock);
928 spin_unlock(&clp->cl_lock);
931 static void __free_session(struct nfsd4_session *ses)
933 nfsd4_put_drc_mem(slot_bytes(&ses->se_fchannel), ses->se_fchannel.maxreqs);
934 free_session_slots(ses);
938 static void free_session(struct kref *kref)
940 struct nfsd4_session *ses;
943 ses = container_of(kref, struct nfsd4_session, se_ref);
944 nn = net_generic(ses->se_client->net, nfsd_net_id);
946 lockdep_assert_held(&nn->client_lock);
947 nfsd4_del_conns(ses);
951 void nfsd4_put_session(struct nfsd4_session *ses)
953 struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
955 spin_lock(&nn->client_lock);
956 nfsd4_put_session_locked(ses);
957 spin_unlock(&nn->client_lock);
960 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fchan,
963 struct nfsd4_session *new;
964 int numslots, slotsize;
966 * Note decreasing slot size below client's request may
967 * make it difficult for client to function correctly, whereas
968 * decreasing the number of slots will (just?) affect
969 * performance. When short on memory we therefore prefer to
970 * decrease number of slots instead of their size.
972 slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
973 numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
977 new = __alloc_session(slotsize, numslots);
979 nfsd4_put_drc_mem(slotsize, numslots);
982 init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize, nn);
986 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
989 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
991 new->se_client = clp;
994 INIT_LIST_HEAD(&new->se_conns);
996 new->se_cb_seq_nr = 1;
997 new->se_flags = cses->flags;
998 new->se_cb_prog = cses->callback_prog;
999 new->se_cb_sec = cses->cb_sec;
1000 kref_init(&new->se_ref);
1001 idx = hash_sessionid(&new->se_sessionid);
1002 spin_lock(&nn->client_lock);
1003 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1004 spin_lock(&clp->cl_lock);
1005 list_add(&new->se_perclnt, &clp->cl_sessions);
1006 spin_unlock(&clp->cl_lock);
1007 spin_unlock(&nn->client_lock);
1009 if (cses->flags & SESSION4_BACK_CHAN) {
1010 struct sockaddr *sa = svc_addr(rqstp);
1012 * This is a little silly; with sessions there's no real
1013 * use for the callback address. Use the peer address
1014 * as a reasonable default for now, but consider fixing
1015 * the rpc client not to require an address in the
1018 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1019 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1023 /* caller must hold client_lock */
1024 static struct nfsd4_session *
1025 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1027 struct nfsd4_session *elem;
1029 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1031 dump_sessionid(__func__, sessionid);
1032 idx = hash_sessionid(sessionid);
1033 /* Search in the appropriate list */
1034 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1035 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1036 NFS4_MAX_SESSIONID_LEN)) {
1041 dprintk("%s: session not found\n", __func__);
1045 /* caller must hold client_lock */
1047 unhash_session(struct nfsd4_session *ses)
1049 list_del(&ses->se_hash);
1050 spin_lock(&ses->se_client->cl_lock);
1051 list_del(&ses->se_perclnt);
1052 spin_unlock(&ses->se_client->cl_lock);
1055 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1057 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1059 if (clid->cl_boot == nn->boot_time)
1061 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1062 clid->cl_boot, clid->cl_id, nn->boot_time);
1067 * XXX Should we use a slab cache ?
1068 * This type of memory management is somewhat inefficient, but we use it
1069 * anyway since SETCLIENTID is not a common operation.
1071 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1073 struct nfs4_client *clp;
1075 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1078 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1079 if (clp->cl_name.data == NULL) {
1083 clp->cl_name.len = name.len;
1088 free_client(struct nfs4_client *clp)
1090 struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
1092 lockdep_assert_held(&nn->client_lock);
1093 while (!list_empty(&clp->cl_sessions)) {
1094 struct nfsd4_session *ses;
1095 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1097 list_del(&ses->se_perclnt);
1098 nfsd4_put_session_locked(ses);
1100 free_svc_cred(&clp->cl_cred);
1101 kfree(clp->cl_name.data);
1102 idr_destroy(&clp->cl_stateids);
1106 /* must be called under the client_lock */
1108 unhash_client_locked(struct nfs4_client *clp)
1110 struct nfsd4_session *ses;
1112 list_del(&clp->cl_lru);
1113 spin_lock(&clp->cl_lock);
1114 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1115 list_del_init(&ses->se_hash);
1116 spin_unlock(&clp->cl_lock);
1120 destroy_client(struct nfs4_client *clp)
1122 struct nfs4_openowner *oo;
1123 struct nfs4_delegation *dp;
1124 struct list_head reaplist;
1125 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1127 INIT_LIST_HEAD(&reaplist);
1128 spin_lock(&recall_lock);
1129 while (!list_empty(&clp->cl_delegations)) {
1130 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1131 list_del_init(&dp->dl_perclnt);
1132 list_move(&dp->dl_recall_lru, &reaplist);
1134 spin_unlock(&recall_lock);
1135 while (!list_empty(&reaplist)) {
1136 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1137 unhash_delegation(dp);
1139 while (!list_empty(&clp->cl_openowners)) {
1140 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1141 release_openowner(oo);
1143 nfsd4_shutdown_callback(clp);
1144 if (clp->cl_cb_conn.cb_xprt)
1145 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1146 list_del(&clp->cl_idhash);
1147 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1148 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1150 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1151 spin_lock(&nn->client_lock);
1152 unhash_client_locked(clp);
1153 WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
1155 spin_unlock(&nn->client_lock);
1158 static void expire_client(struct nfs4_client *clp)
1160 nfsd4_client_record_remove(clp);
1161 destroy_client(clp);
1164 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1166 memcpy(target->cl_verifier.data, source->data,
1167 sizeof(target->cl_verifier.data));
1170 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1172 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1173 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1176 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1178 if (source->cr_principal) {
1179 target->cr_principal =
1180 kstrdup(source->cr_principal, GFP_KERNEL);
1181 if (target->cr_principal == NULL)
1184 target->cr_principal = NULL;
1185 target->cr_flavor = source->cr_flavor;
1186 target->cr_uid = source->cr_uid;
1187 target->cr_gid = source->cr_gid;
1188 target->cr_group_info = source->cr_group_info;
1189 get_group_info(target->cr_group_info);
1194 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1198 res = o1->len - o2->len;
1201 return (long long)memcmp(o1->data, o2->data, o1->len);
1204 static int same_name(const char *n1, const char *n2)
1206 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1210 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1212 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1216 same_clid(clientid_t *cl1, clientid_t *cl2)
1218 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1221 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1225 if (g1->ngroups != g2->ngroups)
1227 for (i=0; i<g1->ngroups; i++)
1228 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1234 * RFC 3530 language requires clid_inuse be returned when the
1235 * "principal" associated with a requests differs from that previously
1236 * used. We use uid, gid's, and gss principal string as our best
1237 * approximation. We also don't want to allow non-gss use of a client
1238 * established using gss: in theory cr_principal should catch that
1239 * change, but in practice cr_principal can be null even in the gss case
1240 * since gssd doesn't always pass down a principal string.
1242 static bool is_gss_cred(struct svc_cred *cr)
1244 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1245 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1250 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1252 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1253 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1254 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1255 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1257 if (cr1->cr_principal == cr2->cr_principal)
1259 if (!cr1->cr_principal || !cr2->cr_principal)
1261 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1264 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1266 static u32 current_clientid = 1;
1268 clp->cl_clientid.cl_boot = nn->boot_time;
1269 clp->cl_clientid.cl_id = current_clientid++;
1272 static void gen_confirm(struct nfs4_client *clp)
1277 verf[0] = (__be32)get_seconds();
1278 verf[1] = (__be32)i++;
1279 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1282 static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
1284 struct nfs4_stid *ret;
1286 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1287 if (!ret || !ret->sc_type)
1292 static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1294 struct nfs4_stid *s;
1296 s = find_stateid(cl, t);
1299 if (typemask & s->sc_type)
1304 static struct nfs4_client *create_client(struct xdr_netobj name,
1305 struct svc_rqst *rqstp, nfs4_verifier *verf)
1307 struct nfs4_client *clp;
1308 struct sockaddr *sa = svc_addr(rqstp);
1310 struct net *net = SVC_NET(rqstp);
1311 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1313 clp = alloc_client(name);
1317 INIT_LIST_HEAD(&clp->cl_sessions);
1318 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1320 spin_lock(&nn->client_lock);
1322 spin_unlock(&nn->client_lock);
1325 idr_init(&clp->cl_stateids);
1326 atomic_set(&clp->cl_refcount, 0);
1327 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1328 INIT_LIST_HEAD(&clp->cl_idhash);
1329 INIT_LIST_HEAD(&clp->cl_openowners);
1330 INIT_LIST_HEAD(&clp->cl_delegations);
1331 INIT_LIST_HEAD(&clp->cl_lru);
1332 INIT_LIST_HEAD(&clp->cl_callbacks);
1333 spin_lock_init(&clp->cl_lock);
1334 nfsd4_init_callback(&clp->cl_cb_null);
1335 clp->cl_time = get_seconds();
1336 clear_bit(0, &clp->cl_cb_slot_busy);
1337 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1338 copy_verf(clp, verf);
1339 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1341 clp->cl_cb_session = NULL;
1347 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1349 struct rb_node **new = &(root->rb_node), *parent = NULL;
1350 struct nfs4_client *clp;
1353 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1356 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1357 new = &((*new)->rb_left);
1359 new = &((*new)->rb_right);
1362 rb_link_node(&new_clp->cl_namenode, parent, new);
1363 rb_insert_color(&new_clp->cl_namenode, root);
1366 static struct nfs4_client *
1367 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1370 struct rb_node *node = root->rb_node;
1371 struct nfs4_client *clp;
1374 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1375 cmp = compare_blob(&clp->cl_name, name);
1377 node = node->rb_left;
1379 node = node->rb_right;
1387 add_to_unconfirmed(struct nfs4_client *clp)
1389 unsigned int idhashval;
1390 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1392 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1393 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1394 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1395 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1400 move_to_confirmed(struct nfs4_client *clp)
1402 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1403 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1405 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1406 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1407 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1408 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1409 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1413 static struct nfs4_client *
1414 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1416 struct nfs4_client *clp;
1417 unsigned int idhashval = clientid_hashval(clid->cl_id);
1419 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1420 if (same_clid(&clp->cl_clientid, clid)) {
1421 if ((bool)clp->cl_minorversion != sessions)
1430 static struct nfs4_client *
1431 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1433 struct list_head *tbl = nn->conf_id_hashtbl;
1435 return find_client_in_id_table(tbl, clid, sessions);
1438 static struct nfs4_client *
1439 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1441 struct list_head *tbl = nn->unconf_id_hashtbl;
1443 return find_client_in_id_table(tbl, clid, sessions);
1446 static bool clp_used_exchangeid(struct nfs4_client *clp)
1448 return clp->cl_exchange_flags != 0;
1451 static struct nfs4_client *
1452 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1454 return find_clp_in_name_tree(name, &nn->conf_name_tree);
1457 static struct nfs4_client *
1458 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
1460 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
1464 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1466 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1467 struct sockaddr *sa = svc_addr(rqstp);
1468 u32 scopeid = rpc_get_scope_id(sa);
1469 unsigned short expected_family;
1471 /* Currently, we only support tcp and tcp6 for the callback channel */
1472 if (se->se_callback_netid_len == 3 &&
1473 !memcmp(se->se_callback_netid_val, "tcp", 3))
1474 expected_family = AF_INET;
1475 else if (se->se_callback_netid_len == 4 &&
1476 !memcmp(se->se_callback_netid_val, "tcp6", 4))
1477 expected_family = AF_INET6;
1481 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
1482 se->se_callback_addr_len,
1483 (struct sockaddr *)&conn->cb_addr,
1484 sizeof(conn->cb_addr));
1486 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1489 if (conn->cb_addr.ss_family == AF_INET6)
1490 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1492 conn->cb_prog = se->se_callback_prog;
1493 conn->cb_ident = se->se_callback_ident;
1494 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1497 conn->cb_addr.ss_family = AF_UNSPEC;
1498 conn->cb_addrlen = 0;
1499 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1500 "will not receive delegations\n",
1501 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1507 * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1510 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1512 struct nfsd4_slot *slot = resp->cstate.slot;
1515 dprintk("--> %s slot %p\n", __func__, slot);
1517 slot->sl_opcnt = resp->opcnt;
1518 slot->sl_status = resp->cstate.status;
1520 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1521 if (nfsd4_not_cached(resp)) {
1522 slot->sl_datalen = 0;
1525 slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1526 base = (char *)resp->cstate.datap -
1527 (char *)resp->xbuf->head[0].iov_base;
1528 if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1530 WARN("%s: sessions DRC could not cache compound\n", __func__);
1535 * Encode the replay sequence operation from the slot values.
1536 * If cachethis is FALSE encode the uncached rep error on the next
1537 * operation which sets resp->p and increments resp->opcnt for
1538 * nfs4svc_encode_compoundres.
1542 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1543 struct nfsd4_compoundres *resp)
1545 struct nfsd4_op *op;
1546 struct nfsd4_slot *slot = resp->cstate.slot;
1548 /* Encode the replayed sequence operation */
1549 op = &args->ops[resp->opcnt - 1];
1550 nfsd4_encode_operation(resp, op);
1552 /* Return nfserr_retry_uncached_rep in next operation. */
1553 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1554 op = &args->ops[resp->opcnt++];
1555 op->status = nfserr_retry_uncached_rep;
1556 nfsd4_encode_operation(resp, op);
1562 * The sequence operation is not cached because we can use the slot and
1566 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1567 struct nfsd4_sequence *seq)
1569 struct nfsd4_slot *slot = resp->cstate.slot;
1572 dprintk("--> %s slot %p\n", __func__, slot);
1574 /* Either returns 0 or nfserr_retry_uncached */
1575 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1576 if (status == nfserr_retry_uncached_rep)
1579 /* The sequence operation has been encoded, cstate->datap set. */
1580 memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1582 resp->opcnt = slot->sl_opcnt;
1583 resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1584 status = slot->sl_status;
1590 * Set the exchange_id flags returned by the server.
1593 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1595 /* pNFS is not supported */
1596 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1598 /* Referrals are supported, Migration is not. */
1599 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1601 /* set the wire flags to return to client. */
1602 clid->flags = new->cl_exchange_flags;
1605 static bool client_has_state(struct nfs4_client *clp)
1608 * Note clp->cl_openowners check isn't quite right: there's no
1609 * need to count owners without stateid's.
1611 * Also note we should probably be using this in 4.0 case too.
1613 return !list_empty(&clp->cl_openowners)
1614 || !list_empty(&clp->cl_delegations)
1615 || !list_empty(&clp->cl_sessions);
1619 nfsd4_exchange_id(struct svc_rqst *rqstp,
1620 struct nfsd4_compound_state *cstate,
1621 struct nfsd4_exchange_id *exid)
1623 struct nfs4_client *unconf, *conf, *new;
1625 char addr_str[INET6_ADDRSTRLEN];
1626 nfs4_verifier verf = exid->verifier;
1627 struct sockaddr *sa = svc_addr(rqstp);
1628 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
1629 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1631 rpc_ntop(sa, addr_str, sizeof(addr_str));
1632 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1633 "ip_addr=%s flags %x, spa_how %d\n",
1634 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1635 addr_str, exid->flags, exid->spa_how);
1637 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
1638 return nfserr_inval;
1640 /* Currently only support SP4_NONE */
1641 switch (exid->spa_how) {
1644 default: /* checked by xdr code */
1648 return nfserr_serverfault; /* no excuse :-/ */
1651 /* Cases below refer to rfc 5661 section 18.35.4: */
1653 conf = find_confirmed_client_by_name(&exid->clname, nn);
1655 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
1656 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
1659 if (!clp_used_exchangeid(conf)) { /* buggy client */
1660 status = nfserr_inval;
1663 if (!creds_match) { /* case 9 */
1664 status = nfserr_perm;
1667 if (!verfs_match) { /* case 8 */
1668 status = nfserr_not_same;
1672 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1676 if (!creds_match) { /* case 3 */
1677 if (client_has_state(conf)) {
1678 status = nfserr_clid_inuse;
1681 expire_client(conf);
1684 if (verfs_match) { /* case 2 */
1685 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
1689 /* case 5, client reboot */
1693 if (update) { /* case 7 */
1694 status = nfserr_noent;
1698 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
1699 if (unconf) /* case 4, possible retry or client restart */
1700 expire_client(unconf);
1702 /* case 1 (normal case) */
1704 new = create_client(exid->clname, rqstp, &verf);
1706 status = nfserr_jukebox;
1709 new->cl_minorversion = 1;
1712 add_to_unconfirmed(new);
1714 exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1715 exid->clientid.cl_id = new->cl_clientid.cl_id;
1717 exid->seqid = new->cl_cs_slot.sl_seqid + 1;
1718 nfsd4_set_ex_flags(new, exid);
1720 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1721 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1725 nfs4_unlock_state();
1730 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1732 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1735 /* The slot is in use, and no response has been sent. */
1737 if (seqid == slot_seqid)
1738 return nfserr_jukebox;
1740 return nfserr_seq_misordered;
1742 /* Note unsigned 32-bit arithmetic handles wraparound: */
1743 if (likely(seqid == slot_seqid + 1))
1745 if (seqid == slot_seqid)
1746 return nfserr_replay_cache;
1747 return nfserr_seq_misordered;
1751 * Cache the create session result into the create session single DRC
1752 * slot cache by saving the xdr structure. sl_seqid has been set.
1753 * Do this for solo or embedded create session operations.
1756 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1757 struct nfsd4_clid_slot *slot, __be32 nfserr)
1759 slot->sl_status = nfserr;
1760 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1764 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1765 struct nfsd4_clid_slot *slot)
1767 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1768 return slot->sl_status;
1771 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
1772 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
1773 1 + /* MIN tag is length with zero, only length */ \
1774 3 + /* version, opcount, opcode */ \
1775 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1776 /* seqid, slotID, slotID, cache */ \
1777 4 ) * sizeof(__be32))
1779 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
1780 2 + /* verifier: AUTH_NULL, length 0 */\
1782 1 + /* MIN tag is length with zero, only length */ \
1783 3 + /* opcount, opcode, opstatus*/ \
1784 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
1785 /* seqid, slotID, slotID, slotID, status */ \
1786 5 ) * sizeof(__be32))
1788 static bool check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
1790 return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
1791 || fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
1795 nfsd4_create_session(struct svc_rqst *rqstp,
1796 struct nfsd4_compound_state *cstate,
1797 struct nfsd4_create_session *cr_ses)
1799 struct sockaddr *sa = svc_addr(rqstp);
1800 struct nfs4_client *conf, *unconf;
1801 struct nfsd4_session *new;
1802 struct nfsd4_conn *conn;
1803 struct nfsd4_clid_slot *cs_slot = NULL;
1805 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1807 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1808 return nfserr_inval;
1809 if (check_forechannel_attrs(cr_ses->fore_channel))
1810 return nfserr_toosmall;
1811 new = alloc_session(&cr_ses->fore_channel, nn);
1813 return nfserr_jukebox;
1814 status = nfserr_jukebox;
1815 conn = alloc_conn_from_crses(rqstp, cr_ses);
1817 goto out_free_session;
1820 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
1821 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
1822 WARN_ON_ONCE(conf && unconf);
1825 cs_slot = &conf->cl_cs_slot;
1826 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1827 if (status == nfserr_replay_cache) {
1828 status = nfsd4_replay_create_session(cr_ses, cs_slot);
1830 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1831 status = nfserr_seq_misordered;
1834 } else if (unconf) {
1835 struct nfs4_client *old;
1836 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1837 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1838 status = nfserr_clid_inuse;
1841 cs_slot = &unconf->cl_cs_slot;
1842 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1844 /* an unconfirmed replay returns misordered */
1845 status = nfserr_seq_misordered;
1848 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
1850 status = mark_client_expired(old);
1855 move_to_confirmed(unconf);
1858 status = nfserr_stale_clientid;
1863 * We do not support RDMA or persistent sessions
1865 cr_ses->flags &= ~SESSION4_PERSIST;
1866 cr_ses->flags &= ~SESSION4_RDMA;
1868 init_session(rqstp, new, conf, cr_ses);
1869 nfsd4_init_conn(rqstp, conn, new);
1871 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1872 NFS4_MAX_SESSIONID_LEN);
1873 memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1874 sizeof(struct nfsd4_channel_attrs));
1875 cs_slot->sl_seqid++;
1876 cr_ses->seqid = cs_slot->sl_seqid;
1878 /* cache solo and embedded create sessions under the state lock */
1879 nfsd4_cache_create_session(cr_ses, cs_slot, status);
1880 nfs4_unlock_state();
1883 nfs4_unlock_state();
1886 __free_session(new);
1890 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1893 case NFS4_CDFC4_FORE:
1894 case NFS4_CDFC4_BACK:
1896 case NFS4_CDFC4_FORE_OR_BOTH:
1897 case NFS4_CDFC4_BACK_OR_BOTH:
1898 *dir = NFS4_CDFC4_BOTH;
1901 return nfserr_inval;
1904 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
1906 struct nfsd4_session *session = cstate->session;
1907 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1909 spin_lock(&nn->client_lock);
1910 session->se_cb_prog = bc->bc_cb_program;
1911 session->se_cb_sec = bc->bc_cb_sec;
1912 spin_unlock(&nn->client_lock);
1914 nfsd4_probe_callback(session->se_client);
1919 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1920 struct nfsd4_compound_state *cstate,
1921 struct nfsd4_bind_conn_to_session *bcts)
1924 struct nfsd4_conn *conn;
1925 struct nfsd4_session *session;
1926 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1928 if (!nfsd4_last_compound_op(rqstp))
1929 return nfserr_not_only_op;
1931 spin_lock(&nn->client_lock);
1932 session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
1933 spin_unlock(&nn->client_lock);
1934 status = nfserr_badsession;
1937 status = nfsd4_map_bcts_dir(&bcts->dir);
1940 conn = alloc_conn(rqstp, bcts->dir);
1941 status = nfserr_jukebox;
1944 nfsd4_init_conn(rqstp, conn, session);
1947 nfs4_unlock_state();
1951 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1955 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1959 nfsd4_destroy_session(struct svc_rqst *r,
1960 struct nfsd4_compound_state *cstate,
1961 struct nfsd4_destroy_session *sessionid)
1963 struct nfsd4_session *ses;
1965 struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
1968 status = nfserr_not_only_op;
1969 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1970 if (!nfsd4_last_compound_op(r))
1973 dump_sessionid(__func__, &sessionid->sessionid);
1974 spin_lock(&nn->client_lock);
1975 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
1976 status = nfserr_badsession;
1978 goto out_client_lock;
1980 unhash_session(ses);
1981 spin_unlock(&nn->client_lock);
1983 nfsd4_probe_callback_sync(ses->se_client);
1985 spin_lock(&nn->client_lock);
1986 nfsd4_del_conns(ses);
1987 nfsd4_put_session_locked(ses);
1990 spin_unlock(&nn->client_lock);
1992 nfs4_unlock_state();
1996 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1998 struct nfsd4_conn *c;
2000 list_for_each_entry(c, &s->se_conns, cn_persession) {
2001 if (c->cn_xprt == xpt) {
2008 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2010 struct nfs4_client *clp = ses->se_client;
2011 struct nfsd4_conn *c;
2014 spin_lock(&clp->cl_lock);
2015 c = __nfsd4_find_conn(new->cn_xprt, ses);
2017 spin_unlock(&clp->cl_lock);
2021 __nfsd4_hash_conn(new, ses);
2022 spin_unlock(&clp->cl_lock);
2023 ret = nfsd4_register_conn(new);
2025 /* oops; xprt is already down: */
2026 nfsd4_conn_lost(&new->cn_xpt_user);
2030 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2032 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2034 return args->opcnt > session->se_fchannel.maxops;
2037 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2038 struct nfsd4_session *session)
2040 struct xdr_buf *xb = &rqstp->rq_arg;
2042 return xb->len > session->se_fchannel.maxreq_sz;
2046 nfsd4_sequence(struct svc_rqst *rqstp,
2047 struct nfsd4_compound_state *cstate,
2048 struct nfsd4_sequence *seq)
2050 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2051 struct nfsd4_session *session;
2052 struct nfs4_client *clp;
2053 struct nfsd4_slot *slot;
2054 struct nfsd4_conn *conn;
2056 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2058 if (resp->opcnt != 1)
2059 return nfserr_sequence_pos;
2062 * Will be either used or freed by nfsd4_sequence_check_conn
2065 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2067 return nfserr_jukebox;
2069 spin_lock(&nn->client_lock);
2070 status = nfserr_badsession;
2071 session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
2073 goto out_no_session;
2074 clp = session->se_client;
2075 status = get_client_locked(clp);
2077 goto out_no_session;
2079 status = nfserr_too_many_ops;
2080 if (nfsd4_session_too_many_ops(rqstp, session))
2081 goto out_put_client;
2083 status = nfserr_req_too_big;
2084 if (nfsd4_request_too_big(rqstp, session))
2085 goto out_put_client;
2087 status = nfserr_badslot;
2088 if (seq->slotid >= session->se_fchannel.maxreqs)
2089 goto out_put_client;
2091 slot = session->se_slots[seq->slotid];
2092 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2094 /* We do not negotiate the number of slots yet, so set the
2095 * maxslots to the session maxreqs which is used to encode
2096 * sr_highest_slotid and the sr_target_slot id to maxslots */
2097 seq->maxslots = session->se_fchannel.maxreqs;
2099 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2100 slot->sl_flags & NFSD4_SLOT_INUSE);
2101 if (status == nfserr_replay_cache) {
2102 status = nfserr_seq_misordered;
2103 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2104 goto out_put_client;
2105 cstate->slot = slot;
2106 cstate->session = session;
2107 /* Return the cached reply status and set cstate->status
2108 * for nfsd4_proc_compound processing */
2109 status = nfsd4_replay_cache_entry(resp, seq);
2110 cstate->status = nfserr_replay_cache;
2114 goto out_put_client;
2116 nfsd4_sequence_check_conn(conn, session);
2119 /* Success! bump slot seqid */
2120 slot->sl_seqid = seq->seqid;
2121 slot->sl_flags |= NFSD4_SLOT_INUSE;
2123 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2125 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2127 cstate->slot = slot;
2128 cstate->session = session;
2131 nfsd4_get_session(cstate->session);
2132 switch (clp->cl_cb_state) {
2134 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2136 case NFSD4_CB_FAULT:
2137 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2140 seq->status_flags = 0;
2144 spin_unlock(&nn->client_lock);
2147 put_client_renew_locked(clp);
2148 goto out_no_session;
2152 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2154 struct nfs4_client *conf, *unconf, *clp;
2156 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2159 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2160 conf = find_confirmed_client(&dc->clientid, true, nn);
2161 WARN_ON_ONCE(conf && unconf);
2166 if (client_has_state(conf)) {
2167 status = nfserr_clientid_busy;
2173 status = nfserr_stale_clientid;
2179 nfs4_unlock_state();
2184 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2188 if (rc->rca_one_fs) {
2189 if (!cstate->current_fh.fh_dentry)
2190 return nfserr_nofilehandle;
2192 * We don't take advantage of the rca_one_fs case.
2193 * That's OK, it's optional, we can safely ignore it.
2199 status = nfserr_complete_already;
2200 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2201 &cstate->session->se_client->cl_flags))
2204 status = nfserr_stale_clientid;
2205 if (is_client_expired(cstate->session->se_client))
2207 * The following error isn't really legal.
2208 * But we only get here if the client just explicitly
2209 * destroyed the client. Surely it no longer cares what
2210 * error it gets back on an operation for the dead
2216 nfsd4_client_record_create(cstate->session->se_client);
2218 nfs4_unlock_state();
2223 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2224 struct nfsd4_setclientid *setclid)
2226 struct xdr_netobj clname = setclid->se_name;
2227 nfs4_verifier clverifier = setclid->se_verf;
2228 struct nfs4_client *conf, *unconf, *new;
2230 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2232 /* Cases below refer to rfc 3530 section 14.2.33: */
2234 conf = find_confirmed_client_by_name(&clname, nn);
2237 status = nfserr_clid_inuse;
2238 if (clp_used_exchangeid(conf))
2240 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2241 char addr_str[INET6_ADDRSTRLEN];
2242 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2244 dprintk("NFSD: setclientid: string in use by client "
2245 "at %s\n", addr_str);
2249 unconf = find_unconfirmed_client_by_name(&clname, nn);
2251 expire_client(unconf);
2252 status = nfserr_jukebox;
2253 new = create_client(clname, rqstp, &clverifier);
2256 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2257 /* case 1: probable callback update */
2258 copy_clid(new, conf);
2259 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2261 new->cl_minorversion = 0;
2262 gen_callback(new, setclid, rqstp);
2263 add_to_unconfirmed(new);
2264 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2265 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2266 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2269 nfs4_unlock_state();
2275 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2276 struct nfsd4_compound_state *cstate,
2277 struct nfsd4_setclientid_confirm *setclientid_confirm)
2279 struct nfs4_client *conf, *unconf;
2280 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2281 clientid_t * clid = &setclientid_confirm->sc_clientid;
2283 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2285 if (STALE_CLIENTID(clid, nn))
2286 return nfserr_stale_clientid;
2289 conf = find_confirmed_client(clid, false, nn);
2290 unconf = find_unconfirmed_client(clid, false, nn);
2292 * We try hard to give out unique clientid's, so if we get an
2293 * attempt to confirm the same clientid with a different cred,
2294 * there's a bug somewhere. Let's charitably assume it's our
2297 status = nfserr_serverfault;
2298 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
2300 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
2302 /* cases below refer to rfc 3530 section 14.2.34: */
2303 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
2304 if (conf && !unconf) /* case 2: probable retransmit */
2306 else /* case 4: client hasn't noticed we rebooted yet? */
2307 status = nfserr_stale_clientid;
2311 if (conf) { /* case 1: callback update */
2312 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2313 nfsd4_probe_callback(conf);
2314 expire_client(unconf);
2315 } else { /* case 3: normal case; new or rebooted client */
2316 conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
2318 status = mark_client_expired(conf);
2321 expire_client(conf);
2323 move_to_confirmed(unconf);
2324 nfsd4_probe_callback(unconf);
2327 nfs4_unlock_state();
2331 static struct nfs4_file *nfsd4_alloc_file(void)
2333 return kmem_cache_alloc(file_slab, GFP_KERNEL);
2336 /* OPEN Share state helper functions */
2337 static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
2339 unsigned int hashval = file_hashval(ino);
2341 atomic_set(&fp->fi_ref, 1);
2342 INIT_LIST_HEAD(&fp->fi_hash);
2343 INIT_LIST_HEAD(&fp->fi_stateids);
2344 INIT_LIST_HEAD(&fp->fi_delegations);
2345 fp->fi_inode = igrab(ino);
2346 fp->fi_had_conflict = false;
2347 fp->fi_lease = NULL;
2348 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2349 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2350 spin_lock(&recall_lock);
2351 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2352 spin_unlock(&recall_lock);
2356 nfsd4_free_slab(struct kmem_cache **slab)
2360 kmem_cache_destroy(*slab);
2365 nfsd4_free_slabs(void)
2367 nfsd4_free_slab(&openowner_slab);
2368 nfsd4_free_slab(&lockowner_slab);
2369 nfsd4_free_slab(&file_slab);
2370 nfsd4_free_slab(&stateid_slab);
2371 nfsd4_free_slab(&deleg_slab);
2375 nfsd4_init_slabs(void)
2377 openowner_slab = kmem_cache_create("nfsd4_openowners",
2378 sizeof(struct nfs4_openowner), 0, 0, NULL);
2379 if (openowner_slab == NULL)
2381 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2382 sizeof(struct nfs4_lockowner), 0, 0, NULL);
2383 if (lockowner_slab == NULL)
2385 file_slab = kmem_cache_create("nfsd4_files",
2386 sizeof(struct nfs4_file), 0, 0, NULL);
2387 if (file_slab == NULL)
2389 stateid_slab = kmem_cache_create("nfsd4_stateids",
2390 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
2391 if (stateid_slab == NULL)
2393 deleg_slab = kmem_cache_create("nfsd4_delegations",
2394 sizeof(struct nfs4_delegation), 0, 0, NULL);
2395 if (deleg_slab == NULL)
2400 dprintk("nfsd4: out of memory while initializing nfsv4\n");
2404 void nfs4_free_openowner(struct nfs4_openowner *oo)
2406 kfree(oo->oo_owner.so_owner.data);
2407 kmem_cache_free(openowner_slab, oo);
2410 void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2412 kfree(lo->lo_owner.so_owner.data);
2413 kmem_cache_free(lockowner_slab, lo);
2416 static void init_nfs4_replay(struct nfs4_replay *rp)
2418 rp->rp_status = nfserr_serverfault;
2420 rp->rp_buf = rp->rp_ibuf;
2423 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
2425 struct nfs4_stateowner *sop;
2427 sop = kmem_cache_alloc(slab, GFP_KERNEL);
2431 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2432 if (!sop->so_owner.data) {
2433 kmem_cache_free(slab, sop);
2436 sop->so_owner.len = owner->len;
2438 INIT_LIST_HEAD(&sop->so_stateids);
2439 sop->so_client = clp;
2440 init_nfs4_replay(&sop->so_replay);
2444 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2446 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2448 list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
2449 list_add(&oo->oo_perclient, &clp->cl_openowners);
2452 static struct nfs4_openowner *
2453 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2454 struct nfs4_openowner *oo;
2456 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2459 oo->oo_owner.so_is_open_owner = 1;
2460 oo->oo_owner.so_seqid = open->op_seqid;
2461 oo->oo_flags = NFS4_OO_NEW;
2463 oo->oo_last_closed_stid = NULL;
2464 INIT_LIST_HEAD(&oo->oo_close_lru);
2465 hash_openowner(oo, clp, strhashval);
2469 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2470 struct nfs4_openowner *oo = open->op_openowner;
2472 stp->st_stid.sc_type = NFS4_OPEN_STID;
2473 INIT_LIST_HEAD(&stp->st_lockowners);
2474 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
2475 list_add(&stp->st_perfile, &fp->fi_stateids);
2476 stp->st_stateowner = &oo->oo_owner;
2479 stp->st_access_bmap = 0;
2480 stp->st_deny_bmap = 0;
2481 set_access(open->op_share_access, stp);
2482 set_deny(open->op_share_deny, stp);
2483 stp->st_openstp = NULL;
2487 move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
2489 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2491 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
2493 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
2494 oo->oo_time = get_seconds();
2498 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2501 return (sop->so_owner.len == owner->len) &&
2502 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2503 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2506 static struct nfs4_openowner *
2507 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
2508 bool sessions, struct nfsd_net *nn)
2510 struct nfs4_stateowner *so;
2511 struct nfs4_openowner *oo;
2512 struct nfs4_client *clp;
2514 list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
2515 if (!so->so_is_open_owner)
2517 if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2519 clp = oo->oo_owner.so_client;
2520 if ((bool)clp->cl_minorversion != sessions)
2522 renew_client(oo->oo_owner.so_client);
2529 /* search file_hashtbl[] for file */
2530 static struct nfs4_file *
2531 find_file(struct inode *ino)
2533 unsigned int hashval = file_hashval(ino);
2534 struct nfs4_file *fp;
2536 spin_lock(&recall_lock);
2537 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2538 if (fp->fi_inode == ino) {
2540 spin_unlock(&recall_lock);
2544 spin_unlock(&recall_lock);
2549 * Called to check deny when READ with all zero stateid or
2550 * WRITE with all zero or all one stateid
2553 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2555 struct inode *ino = current_fh->fh_dentry->d_inode;
2556 struct nfs4_file *fp;
2557 struct nfs4_ol_stateid *stp;
2560 fp = find_file(ino);
2563 ret = nfserr_locked;
2564 /* Search for conflicting share reservations */
2565 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2566 if (test_deny(deny_type, stp) ||
2567 test_deny(NFS4_SHARE_DENY_BOTH, stp))
2576 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2578 /* We're assuming the state code never drops its reference
2579 * without first removing the lease. Since we're in this lease
2580 * callback (and since the lease code is serialized by the kernel
2581 * lock) we know the server hasn't removed the lease yet, we know
2582 * it's safe to take a reference: */
2583 atomic_inc(&dp->dl_count);
2585 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2587 /* only place dl_time is set. protected by lock_flocks*/
2588 dp->dl_time = get_seconds();
2590 nfsd4_cb_recall(dp);
2593 /* Called from break_lease() with lock_flocks() held. */
2594 static void nfsd_break_deleg_cb(struct file_lock *fl)
2596 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2597 struct nfs4_delegation *dp;
2600 WARN(1, "(%p)->fl_owner NULL\n", fl);
2603 if (fp->fi_had_conflict) {
2604 WARN(1, "duplicate break on %p\n", fp);
2608 * We don't want the locks code to timeout the lease for us;
2609 * we'll remove it ourself if a delegation isn't returned
2612 fl->fl_break_time = 0;
2614 spin_lock(&recall_lock);
2615 fp->fi_had_conflict = true;
2616 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2617 nfsd_break_one_deleg(dp);
2618 spin_unlock(&recall_lock);
2622 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2625 return lease_modify(onlist, arg);
2630 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2631 .lm_break = nfsd_break_deleg_cb,
2632 .lm_change = nfsd_change_deleg_cb,
2635 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
2637 if (nfsd4_has_session(cstate))
2639 if (seqid == so->so_seqid - 1)
2640 return nfserr_replay_me;
2641 if (seqid == so->so_seqid)
2643 return nfserr_bad_seqid;
2647 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2648 struct nfsd4_open *open, struct nfsd_net *nn)
2650 clientid_t *clientid = &open->op_clientid;
2651 struct nfs4_client *clp = NULL;
2652 unsigned int strhashval;
2653 struct nfs4_openowner *oo = NULL;
2656 if (STALE_CLIENTID(&open->op_clientid, nn))
2657 return nfserr_stale_clientid;
2659 * In case we need it later, after we've already created the
2660 * file and don't want to risk a further failure:
2662 open->op_file = nfsd4_alloc_file();
2663 if (open->op_file == NULL)
2664 return nfserr_jukebox;
2666 strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
2667 oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
2668 open->op_openowner = oo;
2670 clp = find_confirmed_client(clientid, cstate->minorversion,
2673 return nfserr_expired;
2676 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
2677 /* Replace unconfirmed owners without checking for replay. */
2678 clp = oo->oo_owner.so_client;
2679 release_openowner(oo);
2680 open->op_openowner = NULL;
2683 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
2686 clp = oo->oo_owner.so_client;
2689 oo = alloc_init_open_stateowner(strhashval, clp, open);
2691 return nfserr_jukebox;
2692 open->op_openowner = oo;
2694 open->op_stp = nfs4_alloc_stateid(clp);
2696 return nfserr_jukebox;
2700 static inline __be32
2701 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2703 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2704 return nfserr_openmode;
2709 static int share_access_to_flags(u32 share_access)
2711 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2714 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2716 struct nfs4_stid *ret;
2718 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2721 return delegstateid(ret);
2724 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
2726 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
2727 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
2731 nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2732 struct nfs4_delegation **dp)
2735 __be32 status = nfserr_bad_stateid;
2737 *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2740 flags = share_access_to_flags(open->op_share_access);
2741 status = nfs4_check_delegmode(*dp, flags);
2745 if (!nfsd4_is_deleg_cur(open))
2749 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2754 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
2756 struct nfs4_ol_stateid *local;
2757 struct nfs4_openowner *oo = open->op_openowner;
2759 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2760 /* ignore lock owners */
2761 if (local->st_stateowner->so_is_open_owner == 0)
2763 /* remember if we have seen this open owner */
2764 if (local->st_stateowner == &oo->oo_owner)
2766 /* check for conflicting share reservations */
2767 if (!test_share(local, open))
2768 return nfserr_share_denied;
2773 static inline int nfs4_access_to_access(u32 nfs4_access)
2777 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2778 flags |= NFSD_MAY_READ;
2779 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2780 flags |= NFSD_MAY_WRITE;
2784 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2785 struct svc_fh *cur_fh, struct nfsd4_open *open)
2788 int oflag = nfs4_access_to_omode(open->op_share_access);
2789 int access = nfs4_access_to_access(open->op_share_access);
2791 if (!fp->fi_fds[oflag]) {
2792 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2793 &fp->fi_fds[oflag]);
2797 nfs4_file_get_access(fp, oflag);
2802 static inline __be32
2803 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2804 struct nfsd4_open *open)
2806 struct iattr iattr = {
2807 .ia_valid = ATTR_SIZE,
2810 if (!open->op_truncate)
2812 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2813 return nfserr_inval;
2814 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2818 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)
2820 u32 op_share_access = open->op_share_access;
2824 new_access = !test_access(op_share_access, stp);
2826 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2830 status = nfsd4_truncate(rqstp, cur_fh, open);
2833 int oflag = nfs4_access_to_omode(op_share_access);
2834 nfs4_file_put_access(fp, oflag);
2838 /* remember the open */
2839 set_access(op_share_access, stp);
2840 set_deny(open->op_share_deny, stp);
2847 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
2849 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2852 /* Should we give out recallable state?: */
2853 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2855 if (clp->cl_cb_state == NFSD4_CB_UP)
2858 * In the sessions case, since we don't have to establish a
2859 * separate connection for callbacks, we assume it's OK
2860 * until we hear otherwise:
2862 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2865 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2867 struct file_lock *fl;
2869 fl = locks_alloc_lock();
2872 locks_init_lock(fl);
2873 fl->fl_lmops = &nfsd_lease_mng_ops;
2874 fl->fl_flags = FL_LEASE;
2875 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2876 fl->fl_end = OFFSET_MAX;
2877 fl->fl_owner = (fl_owner_t)(dp->dl_file);
2878 fl->fl_pid = current->tgid;
2882 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2884 struct nfs4_file *fp = dp->dl_file;
2885 struct file_lock *fl;
2888 fl = nfs4_alloc_init_lease(dp, flag);
2891 fl->fl_file = find_readable_file(fp);
2892 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2893 status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2895 list_del_init(&dp->dl_perclnt);
2896 locks_free_lock(fl);
2900 fp->fi_deleg_file = get_file(fl->fl_file);
2901 atomic_set(&fp->fi_delegees, 1);
2902 list_add(&dp->dl_perfile, &fp->fi_delegations);
2906 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2908 struct nfs4_file *fp = dp->dl_file;
2911 return nfs4_setlease(dp, flag);
2912 spin_lock(&recall_lock);
2913 if (fp->fi_had_conflict) {
2914 spin_unlock(&recall_lock);
2917 atomic_inc(&fp->fi_delegees);
2918 list_add(&dp->dl_perfile, &fp->fi_delegations);
2919 spin_unlock(&recall_lock);
2920 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2924 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
2926 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2927 if (status == -EAGAIN)
2928 open->op_why_no_deleg = WND4_CONTENTION;
2930 open->op_why_no_deleg = WND4_RESOURCE;
2931 switch (open->op_deleg_want) {
2932 case NFS4_SHARE_WANT_READ_DELEG:
2933 case NFS4_SHARE_WANT_WRITE_DELEG:
2934 case NFS4_SHARE_WANT_ANY_DELEG:
2936 case NFS4_SHARE_WANT_CANCEL:
2937 open->op_why_no_deleg = WND4_CANCELLED;
2939 case NFS4_SHARE_WANT_NO_DELEG:
2946 * Attempt to hand out a delegation.
2949 nfs4_open_delegation(struct net *net, struct svc_fh *fh,
2950 struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
2952 struct nfs4_delegation *dp;
2953 struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
2955 int status = 0, flag = 0;
2957 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
2958 flag = NFS4_OPEN_DELEGATE_NONE;
2959 open->op_recall = 0;
2960 switch (open->op_claim_type) {
2961 case NFS4_OPEN_CLAIM_PREVIOUS:
2963 open->op_recall = 1;
2964 flag = open->op_delegate_type;
2965 if (flag == NFS4_OPEN_DELEGATE_NONE)
2968 case NFS4_OPEN_CLAIM_NULL:
2969 /* Let's not give out any delegations till everyone's
2970 * had the chance to reclaim theirs.... */
2971 if (locks_in_grace(net))
2973 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
2975 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2976 flag = NFS4_OPEN_DELEGATE_WRITE;
2978 flag = NFS4_OPEN_DELEGATE_READ;
2984 dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2987 status = nfs4_set_delegation(dp, flag);
2991 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
2993 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2994 STATEID_VAL(&dp->dl_stid.sc_stateid));
2996 open->op_delegate_type = flag;
2997 if (flag == NFS4_OPEN_DELEGATE_NONE) {
2998 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2999 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
3000 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3002 /* 4.1 client asking for a delegation? */
3003 if (open->op_deleg_want)
3004 nfsd4_open_deleg_none_ext(open, status);
3008 unhash_stid(&dp->dl_stid);
3009 nfs4_put_delegation(dp);
3011 flag = NFS4_OPEN_DELEGATE_NONE;
3015 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
3016 struct nfs4_delegation *dp)
3018 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3019 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3020 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3021 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3022 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3023 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3024 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3025 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3027 /* Otherwise the client must be confused wanting a delegation
3028 * it already has, therefore we don't return
3029 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3034 * called with nfs4_lock_state() held.
3037 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
3039 struct nfsd4_compoundres *resp = rqstp->rq_resp;
3040 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
3041 struct nfs4_file *fp = NULL;
3042 struct inode *ino = current_fh->fh_dentry->d_inode;
3043 struct nfs4_ol_stateid *stp = NULL;
3044 struct nfs4_delegation *dp = NULL;
3048 * Lookup file; if found, lookup stateid and check open request,
3049 * and check for delegations in the process of being recalled.
3050 * If not found, create the nfs4_file struct
3052 fp = find_file(ino);
3054 if ((status = nfs4_check_open(fp, open, &stp)))
3056 status = nfs4_check_deleg(cl, fp, open, &dp);
3060 status = nfserr_bad_stateid;
3061 if (nfsd4_is_deleg_cur(open))
3063 status = nfserr_jukebox;
3065 open->op_file = NULL;
3066 nfsd4_init_file(fp, ino);
3070 * OPEN the file, or upgrade an existing OPEN.
3071 * If truncate fails, the OPEN fails.
3074 /* Stateid was found, this is an OPEN upgrade */
3075 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
3079 status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
3082 status = nfsd4_truncate(rqstp, current_fh, open);
3086 open->op_stp = NULL;
3087 init_open_stateid(stp, fp, open);
3089 update_stateid(&stp->st_stid.sc_stateid);
3090 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3092 if (nfsd4_has_session(&resp->cstate)) {
3093 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3095 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3096 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3097 open->op_why_no_deleg = WND4_NOT_WANTED;
3103 * Attempt to hand out a delegation. No error return, because the
3104 * OPEN succeeds even if we fail.
3106 nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
3110 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3111 STATEID_VAL(&stp->st_stid.sc_stateid));
3113 /* 4.1 client trying to upgrade/downgrade delegation? */
3114 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3115 open->op_deleg_want)
3116 nfsd4_deleg_xgrade_none_ext(open, dp);
3120 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
3121 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
3123 * To finish the open response, we just need to set the rflags.
3125 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3126 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
3127 !nfsd4_has_session(&resp->cstate))
3128 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
3133 void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3135 if (open->op_openowner) {
3136 struct nfs4_openowner *oo = open->op_openowner;
3138 if (!list_empty(&oo->oo_owner.so_stateids))
3139 list_del_init(&oo->oo_close_lru);
3140 if (oo->oo_flags & NFS4_OO_NEW) {
3142 release_openowner(oo);
3143 open->op_openowner = NULL;
3145 oo->oo_flags &= ~NFS4_OO_NEW;
3149 nfsd4_free_file(open->op_file);
3151 free_generic_stateid(open->op_stp);
3154 static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
3156 struct nfs4_client *found;
3158 if (STALE_CLIENTID(clid, nn))
3159 return nfserr_stale_clientid;
3160 found = find_confirmed_client(clid, session, nn);
3163 return found ? nfs_ok : nfserr_expired;
3167 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3170 struct nfs4_client *clp;
3172 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3175 dprintk("process_renew(%08x/%08x): starting\n",
3176 clid->cl_boot, clid->cl_id);
3177 status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
3180 status = nfserr_cb_path_down;
3181 if (!list_empty(&clp->cl_delegations)
3182 && clp->cl_cb_state != NFSD4_CB_UP)
3186 nfs4_unlock_state();
3191 nfsd4_end_grace(struct nfsd_net *nn)
3193 /* do nothing if grace period already ended */
3194 if (nn->grace_ended)
3197 dprintk("NFSD: end of grace period\n");
3198 nn->grace_ended = true;
3199 nfsd4_record_grace_done(nn, nn->boot_time);
3200 locks_end_grace(&nn->nfsd4_manager);
3202 * Now that every NFSv4 client has had the chance to recover and
3203 * to see the (possibly new, possibly shorter) lease time, we
3204 * can safely set the next grace time to the current lease time:
3206 nn->nfsd4_grace = nn->nfsd4_lease;
3210 nfs4_laundromat(struct nfsd_net *nn)
3212 struct nfs4_client *clp;
3213 struct nfs4_openowner *oo;
3214 struct nfs4_delegation *dp;
3215 struct list_head *pos, *next, reaplist;
3216 time_t cutoff = get_seconds() - nn->nfsd4_lease;
3217 time_t t, clientid_val = nn->nfsd4_lease;
3218 time_t u, test_val = nn->nfsd4_lease;
3222 dprintk("NFSD: laundromat service - starting\n");
3223 nfsd4_end_grace(nn);
3224 INIT_LIST_HEAD(&reaplist);
3225 spin_lock(&nn->client_lock);
3226 list_for_each_safe(pos, next, &nn->client_lru) {
3227 clp = list_entry(pos, struct nfs4_client, cl_lru);
3228 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
3229 t = clp->cl_time - cutoff;
3230 if (clientid_val > t)
3234 if (mark_client_expired_locked(clp)) {
3235 dprintk("NFSD: client in use (clientid %08x)\n",
3236 clp->cl_clientid.cl_id);
3239 list_move(&clp->cl_lru, &reaplist);
3241 spin_unlock(&nn->client_lock);
3242 list_for_each_safe(pos, next, &reaplist) {
3243 clp = list_entry(pos, struct nfs4_client, cl_lru);
3244 dprintk("NFSD: purging unused client (clientid %08x)\n",
3245 clp->cl_clientid.cl_id);
3248 spin_lock(&recall_lock);
3249 list_for_each_safe(pos, next, &del_recall_lru) {
3250 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3251 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
3253 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
3254 u = dp->dl_time - cutoff;
3259 list_move(&dp->dl_recall_lru, &reaplist);
3261 spin_unlock(&recall_lock);
3262 list_for_each_safe(pos, next, &reaplist) {
3263 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3264 unhash_delegation(dp);
3266 test_val = nn->nfsd4_lease;
3267 list_for_each_safe(pos, next, &nn->close_lru) {
3268 oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3269 if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3270 u = oo->oo_time - cutoff;
3275 release_openowner(oo);
3277 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
3278 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
3279 nfs4_unlock_state();
3280 return clientid_val;
3283 static struct workqueue_struct *laundry_wq;
3284 static void laundromat_main(struct work_struct *);
3287 laundromat_main(struct work_struct *laundry)
3290 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
3292 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
3295 t = nfs4_laundromat(nn);
3296 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3297 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
3300 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3302 if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3303 return nfserr_bad_stateid;
3308 access_permit_read(struct nfs4_ol_stateid *stp)
3310 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
3311 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
3312 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
3316 access_permit_write(struct nfs4_ol_stateid *stp)
3318 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
3319 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
3323 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
3325 __be32 status = nfserr_openmode;
3327 /* For lock stateid's, we test the parent open, not the lock: */
3328 if (stp->st_openstp)
3329 stp = stp->st_openstp;
3330 if ((flags & WR_STATE) && !access_permit_write(stp))
3332 if ((flags & RD_STATE) && !access_permit_read(stp))
3339 static inline __be32
3340 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
3342 if (ONE_STATEID(stateid) && (flags & RD_STATE))
3344 else if (locks_in_grace(net)) {
3345 /* Answer in remaining cases depends on existence of
3346 * conflicting state; so we must wait out the grace period. */
3347 return nfserr_grace;
3348 } else if (flags & WR_STATE)
3349 return nfs4_share_conflict(current_fh,
3350 NFS4_SHARE_DENY_WRITE);
3351 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3352 return nfs4_share_conflict(current_fh,
3353 NFS4_SHARE_DENY_READ);
3357 * Allow READ/WRITE during grace period on recovered state only for files
3358 * that are not able to provide mandatory locking.
3361 grace_disallows_io(struct net *net, struct inode *inode)
3363 return locks_in_grace(net) && mandatory_lock(inode);
3366 /* Returns true iff a is later than b: */
3367 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
3369 return (s32)a->si_generation - (s32)b->si_generation > 0;
3372 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
3375 * When sessions are used the stateid generation number is ignored
3378 if (has_session && in->si_generation == 0)
3381 if (in->si_generation == ref->si_generation)
3384 /* If the client sends us a stateid from the future, it's buggy: */
3385 if (stateid_generation_after(in, ref))
3386 return nfserr_bad_stateid;
3388 * However, we could see a stateid from the past, even from a
3389 * non-buggy client. For example, if the client sends a lock
3390 * while some IO is outstanding, the lock may bump si_generation
3391 * while the IO is still in flight. The client could avoid that
3392 * situation by waiting for responses on all the IO requests,
3393 * but better performance may result in retrying IO that
3394 * receives an old_stateid error if requests are rarely
3395 * reordered in flight:
3397 return nfserr_old_stateid;
3400 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
3402 struct nfs4_stid *s;
3403 struct nfs4_ol_stateid *ols;
3406 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3407 return nfserr_bad_stateid;
3408 /* Client debugging aid. */
3409 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
3410 char addr_str[INET6_ADDRSTRLEN];
3411 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
3413 pr_warn_ratelimited("NFSD: client %s testing state ID "
3414 "with incorrect client ID\n", addr_str);
3415 return nfserr_bad_stateid;
3417 s = find_stateid(cl, stateid);
3419 return nfserr_bad_stateid;
3420 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
3423 if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
3425 ols = openlockstateid(s);
3426 if (ols->st_stateowner->so_is_open_owner
3427 && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3428 return nfserr_bad_stateid;
3432 static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
3433 struct nfs4_stid **s, bool sessions,
3434 struct nfsd_net *nn)
3436 struct nfs4_client *cl;
3439 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3440 return nfserr_bad_stateid;
3441 status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
3443 if (status == nfserr_stale_clientid)
3444 return nfserr_stale_stateid;
3447 *s = find_stateid_by_type(cl, stateid, typemask);
3449 return nfserr_bad_stateid;
3454 * Checks for stateid operations
3457 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
3458 stateid_t *stateid, int flags, struct file **filpp)
3460 struct nfs4_stid *s;
3461 struct nfs4_ol_stateid *stp = NULL;
3462 struct nfs4_delegation *dp = NULL;
3463 struct svc_fh *current_fh = &cstate->current_fh;
3464 struct inode *ino = current_fh->fh_dentry->d_inode;
3465 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3471 if (grace_disallows_io(net, ino))
3472 return nfserr_grace;
3474 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3475 return check_special_stateids(net, current_fh, stateid, flags);
3477 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
3478 &s, cstate->minorversion, nn);
3481 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
3484 switch (s->sc_type) {
3485 case NFS4_DELEG_STID:
3486 dp = delegstateid(s);
3487 status = nfs4_check_delegmode(dp, flags);
3491 *filpp = dp->dl_file->fi_deleg_file;
3494 status = nfserr_serverfault;
3499 case NFS4_OPEN_STID:
3500 case NFS4_LOCK_STID:
3501 stp = openlockstateid(s);
3502 status = nfs4_check_fh(current_fh, stp);
3505 if (stp->st_stateowner->so_is_open_owner
3506 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
3508 status = nfs4_check_openmode(stp, flags);
3512 if (flags & RD_STATE)
3513 *filpp = find_readable_file(stp->st_file);
3515 *filpp = find_writeable_file(stp->st_file);
3519 return nfserr_bad_stateid;
3527 nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3529 if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3530 return nfserr_locks_held;
3531 release_lock_stateid(stp);
3536 * Test if the stateid is valid
3539 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3540 struct nfsd4_test_stateid *test_stateid)
3542 struct nfsd4_test_stateid_id *stateid;
3543 struct nfs4_client *cl = cstate->session->se_client;
3546 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
3547 stateid->ts_id_status =
3548 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
3549 nfs4_unlock_state();
3555 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3556 struct nfsd4_free_stateid *free_stateid)
3558 stateid_t *stateid = &free_stateid->fr_stateid;
3559 struct nfs4_stid *s;
3560 struct nfs4_client *cl = cstate->session->se_client;
3561 __be32 ret = nfserr_bad_stateid;
3564 s = find_stateid(cl, stateid);
3567 switch (s->sc_type) {
3568 case NFS4_DELEG_STID:
3569 ret = nfserr_locks_held;
3571 case NFS4_OPEN_STID:
3572 case NFS4_LOCK_STID:
3573 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
3576 if (s->sc_type == NFS4_LOCK_STID)
3577 ret = nfsd4_free_lock_stateid(openlockstateid(s));
3579 ret = nfserr_locks_held;
3582 ret = nfserr_bad_stateid;
3585 nfs4_unlock_state();
3592 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3593 RD_STATE : WR_STATE;
3596 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3598 struct svc_fh *current_fh = &cstate->current_fh;
3599 struct nfs4_stateowner *sop = stp->st_stateowner;
3602 status = nfsd4_check_seqid(cstate, sop, seqid);
3605 if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3607 * "Closed" stateid's exist *only* to return
3608 * nfserr_replay_me from the previous step.
3610 return nfserr_bad_stateid;
3611 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3614 return nfs4_check_fh(current_fh, stp);
3618 * Checks for sequence id mutating operations.
3621 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3622 stateid_t *stateid, char typemask,
3623 struct nfs4_ol_stateid **stpp,
3624 struct nfsd_net *nn)
3627 struct nfs4_stid *s;
3629 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3630 seqid, STATEID_VAL(stateid));
3633 status = nfsd4_lookup_stateid(stateid, typemask, &s,
3634 cstate->minorversion, nn);
3637 *stpp = openlockstateid(s);
3638 cstate->replay_owner = (*stpp)->st_stateowner;
3640 return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
3643 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3644 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
3647 struct nfs4_openowner *oo;
3649 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
3650 NFS4_OPEN_STID, stpp, nn);
3653 oo = openowner((*stpp)->st_stateowner);
3654 if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3655 return nfserr_bad_stateid;
3660 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3661 struct nfsd4_open_confirm *oc)
3664 struct nfs4_openowner *oo;
3665 struct nfs4_ol_stateid *stp;
3666 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3668 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3669 (int)cstate->current_fh.fh_dentry->d_name.len,
3670 cstate->current_fh.fh_dentry->d_name.name);
3672 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3678 status = nfs4_preprocess_seqid_op(cstate,
3679 oc->oc_seqid, &oc->oc_req_stateid,
3680 NFS4_OPEN_STID, &stp, nn);
3683 oo = openowner(stp->st_stateowner);
3684 status = nfserr_bad_stateid;
3685 if (oo->oo_flags & NFS4_OO_CONFIRMED)
3687 oo->oo_flags |= NFS4_OO_CONFIRMED;
3688 update_stateid(&stp->st_stid.sc_stateid);
3689 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3690 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3691 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3693 nfsd4_client_record_create(oo->oo_owner.so_client);
3696 if (!cstate->replay_owner)
3697 nfs4_unlock_state();
3701 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
3703 if (!test_access(access, stp))
3705 nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
3706 clear_access(access, stp);
3709 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
3711 switch (to_access) {
3712 case NFS4_SHARE_ACCESS_READ:
3713 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
3714 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3716 case NFS4_SHARE_ACCESS_WRITE:
3717 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
3718 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
3720 case NFS4_SHARE_ACCESS_BOTH:
3728 reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
3731 for (i = 0; i < 4; i++) {
3732 if ((i & deny) != i)
3738 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3739 struct nfsd4_compound_state *cstate,
3740 struct nfsd4_open_downgrade *od)
3743 struct nfs4_ol_stateid *stp;
3744 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3746 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3747 (int)cstate->current_fh.fh_dentry->d_name.len,
3748 cstate->current_fh.fh_dentry->d_name.name);
3750 /* We don't yet support WANT bits: */
3751 if (od->od_deleg_want)
3752 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
3756 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3757 &od->od_stateid, &stp, nn);
3760 status = nfserr_inval;
3761 if (!test_access(od->od_share_access, stp)) {
3762 dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
3763 stp->st_access_bmap, od->od_share_access);
3766 if (!test_deny(od->od_share_deny, stp)) {
3767 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3768 stp->st_deny_bmap, od->od_share_deny);
3771 nfs4_stateid_downgrade(stp, od->od_share_access);
3773 reset_union_bmap_deny(od->od_share_deny, stp);
3775 update_stateid(&stp->st_stid.sc_stateid);
3776 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3779 if (!cstate->replay_owner)
3780 nfs4_unlock_state();
3784 void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
3786 struct nfs4_openowner *oo;
3787 struct nfs4_ol_stateid *s;
3789 if (!so->so_is_open_owner)
3792 s = oo->oo_last_closed_stid;
3795 if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
3796 /* Release the last_closed_stid on the next seqid bump: */
3797 oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
3800 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3801 release_last_closed_stateid(oo);
3804 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3806 unhash_open_stateid(s);
3807 s->st_stid.sc_type = NFS4_CLOSED_STID;
3811 * nfs4_unlock_state() called after encode
3814 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3815 struct nfsd4_close *close)
3818 struct nfs4_openowner *oo;
3819 struct nfs4_ol_stateid *stp;
3820 struct net *net = SVC_NET(rqstp);
3821 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3823 dprintk("NFSD: nfsd4_close on file %.*s\n",
3824 (int)cstate->current_fh.fh_dentry->d_name.len,
3825 cstate->current_fh.fh_dentry->d_name.name);
3828 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3830 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3834 oo = openowner(stp->st_stateowner);
3836 update_stateid(&stp->st_stid.sc_stateid);
3837 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
3839 nfsd4_close_open_stateid(stp);
3840 release_last_closed_stateid(oo);
3841 oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3842 oo->oo_last_closed_stid = stp;
3844 if (list_empty(&oo->oo_owner.so_stateids)) {
3845 if (cstate->minorversion) {
3846 release_openowner(oo);
3847 cstate->replay_owner = NULL;
3850 * In the 4.0 case we need to keep the owners around a
3851 * little while to handle CLOSE replay.
3853 if (list_empty(&oo->oo_owner.so_stateids))
3854 move_to_close_lru(oo, SVC_NET(rqstp));
3858 if (!cstate->replay_owner)
3859 nfs4_unlock_state();
3864 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3865 struct nfsd4_delegreturn *dr)
3867 struct nfs4_delegation *dp;
3868 stateid_t *stateid = &dr->dr_stateid;
3869 struct nfs4_stid *s;
3871 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3873 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3877 status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
3878 cstate->minorversion, nn);
3881 dp = delegstateid(s);
3882 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3886 unhash_delegation(dp);
3888 nfs4_unlock_state();
3894 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
3896 #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
3899 end_offset(u64 start, u64 len)
3904 return end >= start ? end: NFS4_MAX_UINT64;
3907 /* last octet in a range */
3909 last_byte_offset(u64 start, u64 len)
3915 return end > start ? end - 1: NFS4_MAX_UINT64;
3918 static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
3920 return (file_hashval(inode) + cl_id
3921 + opaque_hashval(ownername->data, ownername->len))
3922 & LOCKOWNER_INO_HASH_MASK;
3926 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3927 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3928 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
3929 * locking, this prevents us from being completely protocol-compliant. The
3930 * real solution to this problem is to start using unsigned file offsets in
3931 * the VFS, but this is a very deep change!
3934 nfs4_transform_lock_offset(struct file_lock *lock)
3936 if (lock->fl_start < 0)
3937 lock->fl_start = OFFSET_MAX;
3938 if (lock->fl_end < 0)
3939 lock->fl_end = OFFSET_MAX;
3942 /* Hack!: For now, we're defining this just so we can use a pointer to it
3943 * as a unique cookie to identify our (NFSv4's) posix locks. */
3944 static const struct lock_manager_operations nfsd_posix_mng_ops = {
3948 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3950 struct nfs4_lockowner *lo;
3952 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3953 lo = (struct nfs4_lockowner *) fl->fl_owner;
3954 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3955 lo->lo_owner.so_owner.len, GFP_KERNEL);
3956 if (!deny->ld_owner.data)
3957 /* We just don't care that much */
3959 deny->ld_owner.len = lo->lo_owner.so_owner.len;
3960 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3963 deny->ld_owner.len = 0;
3964 deny->ld_owner.data = NULL;
3965 deny->ld_clientid.cl_boot = 0;
3966 deny->ld_clientid.cl_id = 0;
3968 deny->ld_start = fl->fl_start;
3969 deny->ld_length = NFS4_MAX_UINT64;
3970 if (fl->fl_end != NFS4_MAX_UINT64)
3971 deny->ld_length = fl->fl_end - fl->fl_start + 1;
3972 deny->ld_type = NFS4_READ_LT;
3973 if (fl->fl_type != F_RDLCK)
3974 deny->ld_type = NFS4_WRITE_LT;
3977 static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3979 struct nfs4_ol_stateid *lst;
3981 if (!same_owner_str(&lo->lo_owner, owner, clid))
3983 lst = list_first_entry(&lo->lo_owner.so_stateids,
3984 struct nfs4_ol_stateid, st_perstateowner);
3985 return lst->st_file->fi_inode == inode;
3988 static struct nfs4_lockowner *
3989 find_lockowner_str(struct inode *inode, clientid_t *clid,
3990 struct xdr_netobj *owner, struct nfsd_net *nn)
3992 unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
3993 struct nfs4_lockowner *lo;
3995 list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
3996 if (same_lockowner_ino(lo, inode, clid, owner))
4002 static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
4004 struct inode *inode = open_stp->st_file->fi_inode;
4005 unsigned int inohash = lockowner_ino_hashval(inode,
4006 clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
4007 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
4009 list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
4010 list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
4011 list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
4015 * Alloc a lock owner structure.
4016 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
4019 * strhashval = ownerstr_hashval
4022 static struct nfs4_lockowner *
4023 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
4024 struct nfs4_lockowner *lo;
4026 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
4029 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
4030 lo->lo_owner.so_is_open_owner = 0;
4031 /* It is the openowner seqid that will be incremented in encode in the
4032 * case of new lockowners; so increment the lock seqid manually: */
4033 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
4034 hash_lockowner(lo, strhashval, clp, open_stp);
4038 static struct nfs4_ol_stateid *
4039 alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
4041 struct nfs4_ol_stateid *stp;
4042 struct nfs4_client *clp = lo->lo_owner.so_client;
4044 stp = nfs4_alloc_stateid(clp);
4047 stp->st_stid.sc_type = NFS4_LOCK_STID;
4048 list_add(&stp->st_perfile, &fp->fi_stateids);
4049 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
4050 stp->st_stateowner = &lo->lo_owner;
4053 stp->st_access_bmap = 0;
4054 stp->st_deny_bmap = open_stp->st_deny_bmap;
4055 stp->st_openstp = open_stp;
4060 check_lock_length(u64 offset, u64 length)
4062 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
4063 LOFF_OVERFLOW(offset, length)));
4066 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
4068 struct nfs4_file *fp = lock_stp->st_file;
4069 int oflag = nfs4_access_to_omode(access);
4071 if (test_access(access, lock_stp))
4073 nfs4_file_get_access(fp, oflag);
4074 set_access(access, lock_stp);
4077 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)
4079 struct nfs4_file *fi = ost->st_file;
4080 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
4081 struct nfs4_client *cl = oo->oo_owner.so_client;
4082 struct nfs4_lockowner *lo;
4083 unsigned int strhashval;
4084 struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
4086 lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
4087 &lock->v.new.owner, nn);
4089 if (!cstate->minorversion)
4090 return nfserr_bad_seqid;
4091 /* XXX: a lockowner always has exactly one stateid: */
4092 *lst = list_first_entry(&lo->lo_owner.so_stateids,
4093 struct nfs4_ol_stateid, st_perstateowner);
4096 strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
4097 &lock->v.new.owner);
4098 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
4100 return nfserr_jukebox;
4101 *lst = alloc_init_lock_stateid(lo, fi, ost);
4103 release_lockowner(lo);
4104 return nfserr_jukebox;
4114 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4115 struct nfsd4_lock *lock)
4117 struct nfs4_openowner *open_sop = NULL;
4118 struct nfs4_lockowner *lock_sop = NULL;
4119 struct nfs4_ol_stateid *lock_stp;
4120 struct file *filp = NULL;
4121 struct file_lock *file_lock = NULL;
4122 struct file_lock *conflock = NULL;
4124 bool new_state = false;
4127 struct net *net = SVC_NET(rqstp);
4128 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4130 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
4131 (long long) lock->lk_offset,
4132 (long long) lock->lk_length);
4134 if (check_lock_length(lock->lk_offset, lock->lk_length))
4135 return nfserr_inval;
4137 if ((status = fh_verify(rqstp, &cstate->current_fh,
4138 S_IFREG, NFSD_MAY_LOCK))) {
4139 dprintk("NFSD: nfsd4_lock: permission denied!\n");
4145 if (lock->lk_is_new) {
4146 struct nfs4_ol_stateid *open_stp = NULL;
4148 if (nfsd4_has_session(cstate))
4149 /* See rfc 5661 18.10.3: given clientid is ignored: */
4150 memcpy(&lock->v.new.clientid,
4151 &cstate->session->se_client->cl_clientid,
4152 sizeof(clientid_t));
4154 status = nfserr_stale_clientid;
4155 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
4158 /* validate and update open stateid and open seqid */
4159 status = nfs4_preprocess_confirmed_seqid_op(cstate,
4160 lock->lk_new_open_seqid,
4161 &lock->lk_new_open_stateid,
4165 open_sop = openowner(open_stp->st_stateowner);
4166 status = nfserr_bad_stateid;
4167 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4168 &lock->v.new.clientid))
4170 status = lookup_or_create_lock_state(cstate, open_stp, lock,
4171 &lock_stp, &new_state);
4173 status = nfs4_preprocess_seqid_op(cstate,
4174 lock->lk_old_lock_seqid,
4175 &lock->lk_old_lock_stateid,
4176 NFS4_LOCK_STID, &lock_stp, nn);
4179 lock_sop = lockowner(lock_stp->st_stateowner);
4181 lkflg = setlkflg(lock->lk_type);
4182 status = nfs4_check_openmode(lock_stp, lkflg);
4186 status = nfserr_grace;
4187 if (locks_in_grace(net) && !lock->lk_reclaim)
4189 status = nfserr_no_grace;
4190 if (!locks_in_grace(net) && lock->lk_reclaim)
4193 file_lock = locks_alloc_lock();
4195 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4196 status = nfserr_jukebox;
4200 locks_init_lock(file_lock);
4201 switch (lock->lk_type) {
4204 filp = find_readable_file(lock_stp->st_file);
4206 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
4207 file_lock->fl_type = F_RDLCK;
4210 case NFS4_WRITEW_LT:
4211 filp = find_writeable_file(lock_stp->st_file);
4213 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
4214 file_lock->fl_type = F_WRLCK;
4217 status = nfserr_inval;
4221 status = nfserr_openmode;
4224 file_lock->fl_owner = (fl_owner_t)lock_sop;
4225 file_lock->fl_pid = current->tgid;
4226 file_lock->fl_file = filp;
4227 file_lock->fl_flags = FL_POSIX;
4228 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4229 file_lock->fl_start = lock->lk_offset;
4230 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
4231 nfs4_transform_lock_offset(file_lock);
4233 conflock = locks_alloc_lock();
4235 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4236 status = nfserr_jukebox;
4240 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
4242 case 0: /* success! */
4243 update_stateid(&lock_stp->st_stid.sc_stateid);
4244 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
4248 case (EAGAIN): /* conflock holds conflicting lock */
4249 status = nfserr_denied;
4250 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4251 nfs4_set_lock_denied(conflock, &lock->lk_denied);
4254 status = nfserr_deadlock;
4257 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
4258 status = nfserrno(err);
4262 if (status && new_state)
4263 release_lockowner(lock_sop);
4264 if (!cstate->replay_owner)
4265 nfs4_unlock_state();
4267 locks_free_lock(file_lock);
4269 locks_free_lock(conflock);
4274 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
4275 * so we do a temporary open here just to get an open file to pass to
4276 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
4279 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
4282 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
4284 err = nfserrno(vfs_test_lock(file, lock));
4294 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4295 struct nfsd4_lockt *lockt)
4297 struct inode *inode;
4298 struct file_lock *file_lock = NULL;
4299 struct nfs4_lockowner *lo;
4301 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4303 if (locks_in_grace(SVC_NET(rqstp)))
4304 return nfserr_grace;
4306 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
4307 return nfserr_inval;
4311 if (!nfsd4_has_session(cstate)) {
4312 status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
4317 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4320 inode = cstate->current_fh.fh_dentry->d_inode;
4321 file_lock = locks_alloc_lock();
4323 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4324 status = nfserr_jukebox;
4327 locks_init_lock(file_lock);
4328 switch (lockt->lt_type) {
4331 file_lock->fl_type = F_RDLCK;
4334 case NFS4_WRITEW_LT:
4335 file_lock->fl_type = F_WRLCK;
4338 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4339 status = nfserr_inval;
4343 lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
4345 file_lock->fl_owner = (fl_owner_t)lo;
4346 file_lock->fl_pid = current->tgid;
4347 file_lock->fl_flags = FL_POSIX;
4349 file_lock->fl_start = lockt->lt_offset;
4350 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4352 nfs4_transform_lock_offset(file_lock);
4354 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
4358 if (file_lock->fl_type != F_UNLCK) {
4359 status = nfserr_denied;
4360 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
4363 nfs4_unlock_state();
4365 locks_free_lock(file_lock);
4370 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4371 struct nfsd4_locku *locku)
4373 struct nfs4_ol_stateid *stp;
4374 struct file *filp = NULL;
4375 struct file_lock *file_lock = NULL;
4378 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4380 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4381 (long long) locku->lu_offset,
4382 (long long) locku->lu_length);
4384 if (check_lock_length(locku->lu_offset, locku->lu_length))
4385 return nfserr_inval;
4389 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
4390 &locku->lu_stateid, NFS4_LOCK_STID,
4394 filp = find_any_file(stp->st_file);
4396 status = nfserr_lock_range;
4399 file_lock = locks_alloc_lock();
4401 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
4402 status = nfserr_jukebox;
4405 locks_init_lock(file_lock);
4406 file_lock->fl_type = F_UNLCK;
4407 file_lock->fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
4408 file_lock->fl_pid = current->tgid;
4409 file_lock->fl_file = filp;
4410 file_lock->fl_flags = FL_POSIX;
4411 file_lock->fl_lmops = &nfsd_posix_mng_ops;
4412 file_lock->fl_start = locku->lu_offset;
4414 file_lock->fl_end = last_byte_offset(locku->lu_offset,
4416 nfs4_transform_lock_offset(file_lock);
4419 * Try to unlock the file in the VFS.
4421 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
4423 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4427 * OK, unlock succeeded; the only thing left to do is update the stateid.
4429 update_stateid(&stp->st_stid.sc_stateid);
4430 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4433 if (!cstate->replay_owner)
4434 nfs4_unlock_state();
4436 locks_free_lock(file_lock);
4440 status = nfserrno(err);
4446 * 1: locks held by lockowner
4447 * 0: no locks held by lockowner
4450 check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
4452 struct file_lock **flpp;
4453 struct inode *inode = filp->fi_inode;
4457 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4458 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4469 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4470 struct nfsd4_compound_state *cstate,
4471 struct nfsd4_release_lockowner *rlockowner)
4473 clientid_t *clid = &rlockowner->rl_clientid;
4474 struct nfs4_stateowner *sop;
4475 struct nfs4_lockowner *lo;
4476 struct nfs4_ol_stateid *stp;
4477 struct xdr_netobj *owner = &rlockowner->rl_owner;
4478 struct list_head matches;
4479 unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
4481 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4483 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4484 clid->cl_boot, clid->cl_id);
4488 status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
4492 status = nfserr_locks_held;
4493 INIT_LIST_HEAD(&matches);
4495 list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
4496 if (sop->so_is_open_owner)
4498 if (!same_owner_str(sop, owner, clid))
4500 list_for_each_entry(stp, &sop->so_stateids,
4502 lo = lockowner(sop);
4503 if (check_for_locks(stp->st_file, lo))
4505 list_add(&lo->lo_list, &matches);
4508 /* Clients probably won't expect us to return with some (but not all)
4509 * of the lockowner state released; so don't release any until all
4510 * have been checked. */
4512 while (!list_empty(&matches)) {
4513 lo = list_entry(matches.next, struct nfs4_lockowner,
4515 /* unhash_stateowner deletes so_perclient only
4516 * for openowners. */
4517 list_del(&lo->lo_list);
4518 release_lockowner(lo);
4521 nfs4_unlock_state();
4525 static inline struct nfs4_client_reclaim *
4528 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4532 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
4534 struct nfs4_client_reclaim *crp;
4536 crp = nfsd4_find_reclaim_client(name, nn);
4537 return (crp && crp->cr_clp);
4541 * failure => all reset bets are off, nfserr_no_grace...
4543 struct nfs4_client_reclaim *
4544 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
4546 unsigned int strhashval;
4547 struct nfs4_client_reclaim *crp;
4549 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4550 crp = alloc_reclaim();
4552 strhashval = clientstr_hashval(name);
4553 INIT_LIST_HEAD(&crp->cr_strhash);
4554 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
4555 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4557 nn->reclaim_str_hashtbl_size++;
4563 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
4565 list_del(&crp->cr_strhash);
4567 nn->reclaim_str_hashtbl_size--;
4571 nfs4_release_reclaim(struct nfsd_net *nn)
4573 struct nfs4_client_reclaim *crp = NULL;
4576 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4577 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
4578 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
4579 struct nfs4_client_reclaim, cr_strhash);
4580 nfs4_remove_reclaim_record(crp, nn);
4583 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
4587 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4588 struct nfs4_client_reclaim *
4589 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
4591 unsigned int strhashval;
4592 struct nfs4_client_reclaim *crp = NULL;
4594 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
4596 strhashval = clientstr_hashval(recdir);
4597 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
4598 if (same_name(crp->cr_recdir, recdir)) {
4606 * Called from OPEN. Look for clientid in reclaim list.
4609 nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
4611 struct nfs4_client *clp;
4613 /* find clientid in conf_id_hashtbl */
4614 clp = find_confirmed_client(clid, sessions, nn);
4616 return nfserr_reclaim_bad;
4618 return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
4621 #ifdef CONFIG_NFSD_FAULT_INJECTION
4623 u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
4625 if (mark_client_expired(clp))
4631 u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
4633 char buf[INET6_ADDRSTRLEN];
4634 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4635 printk(KERN_INFO "NFS Client: %s\n", buf);
4639 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
4642 char buf[INET6_ADDRSTRLEN];
4643 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
4644 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
4647 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
4649 struct nfs4_openowner *oop;
4650 struct nfs4_lockowner *lop, *lo_next;
4651 struct nfs4_ol_stateid *stp, *st_next;
4654 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
4655 list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
4656 list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
4668 u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
4670 return nfsd_foreach_client_lock(clp, max, release_lockowner);
4673 u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
4675 u64 count = nfsd_foreach_client_lock(clp, max, NULL);
4676 nfsd_print_count(clp, count, "locked files");
4680 static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
4682 struct nfs4_openowner *oop, *next;
4685 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
4695 u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
4697 return nfsd_foreach_client_open(clp, max, release_openowner);
4700 u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
4702 u64 count = nfsd_foreach_client_open(clp, max, NULL);
4703 nfsd_print_count(clp, count, "open files");
4707 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
4708 struct list_head *victims)
4710 struct nfs4_delegation *dp, *next;
4713 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
4715 list_move(&dp->dl_recall_lru, victims);
4722 u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
4724 struct nfs4_delegation *dp, *next;
4728 spin_lock(&recall_lock);
4729 count = nfsd_find_all_delegations(clp, max, &victims);
4730 spin_unlock(&recall_lock);
4732 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4733 unhash_delegation(dp);
4738 u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
4740 struct nfs4_delegation *dp, *next;
4744 spin_lock(&recall_lock);
4745 count = nfsd_find_all_delegations(clp, max, &victims);
4746 list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
4747 nfsd_break_one_deleg(dp);
4748 spin_unlock(&recall_lock);
4753 u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
4757 spin_lock(&recall_lock);
4758 count = nfsd_find_all_delegations(clp, max, NULL);
4759 spin_unlock(&recall_lock);
4761 nfsd_print_count(clp, count, "delegations");
4765 u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
4767 struct nfs4_client *clp, *next;
4769 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4771 if (!nfsd_netns_ready(nn))
4774 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
4775 count += func(clp, max - count);
4776 if ((max != 0) && (count >= max))
4783 struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
4785 struct nfs4_client *clp;
4786 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
4788 if (!nfsd_netns_ready(nn))
4791 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
4792 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
4798 #endif /* CONFIG_NFSD_FAULT_INJECTION */
4800 /* initialization to perform at module load time: */
4803 nfs4_state_init(void)
4807 for (i = 0; i < FILE_HASH_SIZE; i++) {
4808 INIT_LIST_HEAD(&file_hashtbl[i]);
4810 INIT_LIST_HEAD(&del_recall_lru);
4814 * Since the lifetime of a delegation isn't limited to that of an open, a
4815 * client may quite reasonably hang on to a delegation as long as it has
4816 * the inode cached. This becomes an obvious problem the first time a
4817 * client's inode cache approaches the size of the server's total memory.
4819 * For now we avoid this problem by imposing a hard limit on the number
4820 * of delegations, which varies according to the server's memory size.
4823 set_max_delegations(void)
4826 * Allow at most 4 delegations per megabyte of RAM. Quick
4827 * estimates suggest that in the worst case (where every delegation
4828 * is for a different inode), a delegation could take about 1.5K,
4829 * giving a worst case usage of about 6% of memory.
4831 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4834 static int nfs4_state_create_net(struct net *net)
4836 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4839 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4840 CLIENT_HASH_SIZE, GFP_KERNEL);
4841 if (!nn->conf_id_hashtbl)
4843 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
4844 CLIENT_HASH_SIZE, GFP_KERNEL);
4845 if (!nn->unconf_id_hashtbl)
4847 nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
4848 OWNER_HASH_SIZE, GFP_KERNEL);
4849 if (!nn->ownerstr_hashtbl)
4851 nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
4852 LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
4853 if (!nn->lockowner_ino_hashtbl)
4854 goto err_lockowner_ino;
4855 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
4856 SESSION_HASH_SIZE, GFP_KERNEL);
4857 if (!nn->sessionid_hashtbl)
4860 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4861 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
4862 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
4864 for (i = 0; i < OWNER_HASH_SIZE; i++)
4865 INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
4866 for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4867 INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
4868 for (i = 0; i < SESSION_HASH_SIZE; i++)
4869 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
4870 nn->conf_name_tree = RB_ROOT;
4871 nn->unconf_name_tree = RB_ROOT;
4872 INIT_LIST_HEAD(&nn->client_lru);
4873 INIT_LIST_HEAD(&nn->close_lru);
4874 spin_lock_init(&nn->client_lock);
4876 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
4882 kfree(nn->lockowner_ino_hashtbl);
4884 kfree(nn->ownerstr_hashtbl);
4886 kfree(nn->unconf_id_hashtbl);
4888 kfree(nn->conf_id_hashtbl);
4894 nfs4_state_destroy_net(struct net *net)
4897 struct nfs4_client *clp = NULL;
4898 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4899 struct rb_node *node, *tmp;
4901 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4902 while (!list_empty(&nn->conf_id_hashtbl[i])) {
4903 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4904 destroy_client(clp);
4908 node = rb_first(&nn->unconf_name_tree);
4909 while (node != NULL) {
4911 node = rb_next(tmp);
4912 clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
4913 rb_erase(tmp, &nn->unconf_name_tree);
4914 destroy_client(clp);
4917 kfree(nn->sessionid_hashtbl);
4918 kfree(nn->lockowner_ino_hashtbl);
4919 kfree(nn->ownerstr_hashtbl);
4920 kfree(nn->unconf_id_hashtbl);
4921 kfree(nn->conf_id_hashtbl);
4926 nfs4_state_start_net(struct net *net)
4928 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4931 ret = nfs4_state_create_net(net);
4934 nfsd4_client_tracking_init(net);
4935 nn->boot_time = get_seconds();
4936 locks_start_grace(net, &nn->nfsd4_manager);
4937 nn->grace_ended = false;
4938 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
4939 nn->nfsd4_grace, net);
4940 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
4944 /* initialization to perform when the nfsd service is started: */
4947 nfs4_state_start(void)
4951 ret = set_callback_cred();
4954 laundry_wq = create_singlethread_workqueue("nfsd4");
4955 if (laundry_wq == NULL) {
4959 ret = nfsd4_create_callback_queue();
4961 goto out_free_laundry;
4963 set_max_delegations();
4968 destroy_workqueue(laundry_wq);
4973 /* should be called with the state lock held */
4975 nfs4_state_shutdown_net(struct net *net)
4977 struct nfs4_delegation *dp = NULL;
4978 struct list_head *pos, *next, reaplist;
4979 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4981 cancel_delayed_work_sync(&nn->laundromat_work);
4982 locks_end_grace(&nn->nfsd4_manager);
4984 INIT_LIST_HEAD(&reaplist);
4985 spin_lock(&recall_lock);
4986 list_for_each_safe(pos, next, &del_recall_lru) {
4987 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4988 if (dp->dl_stid.sc_client->net != net)
4990 list_move(&dp->dl_recall_lru, &reaplist);
4992 spin_unlock(&recall_lock);
4993 list_for_each_safe(pos, next, &reaplist) {
4994 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4995 unhash_delegation(dp);
4998 nfsd4_client_tracking_exit(net);
4999 nfs4_state_destroy_net(net);
5003 nfs4_state_shutdown(void)
5005 destroy_workqueue(laundry_wq);
5006 nfsd4_destroy_callback_queue();
5010 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5012 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
5013 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
5017 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
5019 if (cstate->minorversion) {
5020 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
5021 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5026 clear_current_stateid(struct nfsd4_compound_state *cstate)
5028 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
5032 * functions to set current state id
5035 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5037 put_stateid(cstate, &odp->od_stateid);
5041 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
5043 put_stateid(cstate, &open->op_stateid);
5047 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5049 put_stateid(cstate, &close->cl_stateid);
5053 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
5055 put_stateid(cstate, &lock->lk_resp_stateid);
5059 * functions to consume current state id
5063 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
5065 get_stateid(cstate, &odp->od_stateid);
5069 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
5071 get_stateid(cstate, &drp->dr_stateid);
5075 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
5077 get_stateid(cstate, &fsp->fr_stateid);
5081 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
5083 get_stateid(cstate, &setattr->sa_stateid);
5087 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
5089 get_stateid(cstate, &close->cl_stateid);
5093 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
5095 get_stateid(cstate, &locku->lu_stateid);
5099 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
5101 get_stateid(cstate, &read->rd_stateid);
5105 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
5107 get_stateid(cstate, &write->wr_stateid);