xref: /openbmc/linux/fs/nfsd/nfs4state.c (revision 45f56da8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds *  Copyright (c) 2001 The Regents of the University of Michigan.
31da177e4SLinus Torvalds *  All rights reserved.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds *  Kendrick Smith <kmsmith@umich.edu>
61da177e4SLinus Torvalds *  Andy Adamson <kandros@umich.edu>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds *  Redistribution and use in source and binary forms, with or without
91da177e4SLinus Torvalds *  modification, are permitted provided that the following conditions
101da177e4SLinus Torvalds *  are met:
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds *  1. Redistributions of source code must retain the above copyright
131da177e4SLinus Torvalds *     notice, this list of conditions and the following disclaimer.
141da177e4SLinus Torvalds *  2. Redistributions in binary form must reproduce the above copyright
151da177e4SLinus Torvalds *     notice, this list of conditions and the following disclaimer in the
161da177e4SLinus Torvalds *     documentation and/or other materials provided with the distribution.
171da177e4SLinus Torvalds *  3. Neither the name of the University nor the names of its
181da177e4SLinus Torvalds *     contributors may be used to endorse or promote products derived
191da177e4SLinus Torvalds *     from this software without specific prior written permission.
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
221da177e4SLinus Torvalds *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
231da177e4SLinus Torvalds *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
241da177e4SLinus Torvalds *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251da177e4SLinus Torvalds *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
261da177e4SLinus Torvalds *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
271da177e4SLinus Torvalds *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
281da177e4SLinus Torvalds *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
291da177e4SLinus Torvalds *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
301da177e4SLinus Torvalds *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
311da177e4SLinus Torvalds *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321da177e4SLinus Torvalds *
331da177e4SLinus Torvalds */
341da177e4SLinus Torvalds 
35aceaf78dSDave Hansen #include <linux/file.h>
36b89f4321SArnd Bergmann #include <linux/fs.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
380964a3d3SNeilBrown #include <linux/namei.h>
39c2f1a551SMeelap Shah #include <linux/swap.h>
4017456804SBryan Schumaker #include <linux/pagemap.h>
417df302f7SChuck Lever #include <linux/ratelimit.h>
4268e76ad0SOlga Kornievskaia #include <linux/sunrpc/svcauth_gss.h>
435976687aSJeff Layton #include <linux/sunrpc/addr.h>
4487545899SDaniel Borkmann #include <linux/jhash.h>
45169319f1SJ. Bruce Fields #include <linux/string_helpers.h>
469a74af21SBoaz Harrosh #include "xdr4.h"
4706b332a5SJ. Bruce Fields #include "xdr4cb.h"
480a3adadeSJ. Bruce Fields #include "vfs.h"
49bfa4b365SJ. Bruce Fields #include "current_stateid.h"
501da177e4SLinus Torvalds 
515e1533c7SStanislav Kinsbursky #include "netns.h"
529cf514ccSChristoph Hellwig #include "pnfs.h"
53fd4f83fdSJeff Layton #include "filecache.h"
54dd5e3fbcSChuck Lever #include "trace.h"
555e1533c7SStanislav Kinsbursky 
561da177e4SLinus Torvalds #define NFSDDBG_FACILITY                NFSDDBG_PROC
571da177e4SLinus Torvalds 
58f32f3c2dSJ. Bruce Fields #define all_ones {{~0,~0},~0}
59f32f3c2dSJ. Bruce Fields static const stateid_t one_stateid = {
60f32f3c2dSJ. Bruce Fields 	.si_generation = ~0,
61f32f3c2dSJ. Bruce Fields 	.si_opaque = all_ones,
62f32f3c2dSJ. Bruce Fields };
63f32f3c2dSJ. Bruce Fields static const stateid_t zero_stateid = {
64f32f3c2dSJ. Bruce Fields 	/* all fields zero */
65f32f3c2dSJ. Bruce Fields };
6619ff0f28STigran Mkrtchyan static const stateid_t currentstateid = {
6719ff0f28STigran Mkrtchyan 	.si_generation = 1,
6819ff0f28STigran Mkrtchyan };
69fb500a7cSTrond Myklebust static const stateid_t close_stateid = {
70fb500a7cSTrond Myklebust 	.si_generation = 0xffffffffU,
71fb500a7cSTrond Myklebust };
72f32f3c2dSJ. Bruce Fields 
73ec6b5d7bSAndy Adamson static u64 current_sessionid = 1;
74fd39ca9aSNeilBrown 
75f32f3c2dSJ. Bruce Fields #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76f32f3c2dSJ. Bruce Fields #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
7719ff0f28STigran Mkrtchyan #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
78ae254dacSAndrew Elble #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /* forward declarations */
81f9c00c3aSJeff Layton static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
826011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
83362063a5SScott Mayhew void nfsd4_end_grace(struct nfsd_net *nn);
84624322f1SOlga Kornievskaia static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
851da177e4SLinus Torvalds 
868b671b80SJ. Bruce Fields /* Locking: */
878b671b80SJ. Bruce Fields 
888b671b80SJ. Bruce Fields /*
898b671b80SJ. Bruce Fields  * Currently used for the del_recall_lru and file hash table.  In an
908b671b80SJ. Bruce Fields  * effort to decrease the scope of the client_mutex, this spinlock may
918b671b80SJ. Bruce Fields  * eventually cover more:
928b671b80SJ. Bruce Fields  */
93cdc97505SBenny Halevy static DEFINE_SPINLOCK(state_lock);
948b671b80SJ. Bruce Fields 
954f34bd05SAndrew Elble enum nfsd4_st_mutex_lock_subclass {
964f34bd05SAndrew Elble 	OPEN_STATEID_MUTEX = 0,
974f34bd05SAndrew Elble 	LOCK_STATEID_MUTEX = 1,
984f34bd05SAndrew Elble };
994f34bd05SAndrew Elble 
100b401be22SJeff Layton /*
101b401be22SJeff Layton  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102b401be22SJeff Layton  * the refcount on the open stateid to drop.
103b401be22SJeff Layton  */
104b401be22SJeff Layton static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105b401be22SJeff Layton 
10689c905beSJ. Bruce Fields /*
10789c905beSJ. Bruce Fields  * A waitqueue where a writer to clients/#/ctl destroying a client can
10889c905beSJ. Bruce Fields  * wait for cl_rpc_users to drop to 0 and then for the client to be
10989c905beSJ. Bruce Fields  * unhashed.
11089c905beSJ. Bruce Fields  */
11189c905beSJ. Bruce Fields static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
11289c905beSJ. Bruce Fields 
1139258a2d5SJeff Layton static struct kmem_cache *client_slab;
114abf1135bSChristoph Hellwig static struct kmem_cache *openowner_slab;
115abf1135bSChristoph Hellwig static struct kmem_cache *lockowner_slab;
116abf1135bSChristoph Hellwig static struct kmem_cache *file_slab;
117abf1135bSChristoph Hellwig static struct kmem_cache *stateid_slab;
118abf1135bSChristoph Hellwig static struct kmem_cache *deleg_slab;
1198287f009SSachin Bhamare static struct kmem_cache *odstate_slab;
120e60d4398SNeilBrown 
12166b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *);
122508dc6e1SBenny Halevy 
123c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
12476d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
1250162ac2bSChristoph Hellwig 
12666b2b9b2SJ. Bruce Fields static bool is_session_dead(struct nfsd4_session *ses)
127508dc6e1SBenny Halevy {
12866b2b9b2SJ. Bruce Fields 	return ses->se_flags & NFS4_SESSION_DEAD;
12966b2b9b2SJ. Bruce Fields }
13066b2b9b2SJ. Bruce Fields 
131f0f51f5cSJ. Bruce Fields static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
132f0f51f5cSJ. Bruce Fields {
133f0f51f5cSJ. Bruce Fields 	if (atomic_read(&ses->se_ref) > ref_held_by_me)
13466b2b9b2SJ. Bruce Fields 		return nfserr_jukebox;
13566b2b9b2SJ. Bruce Fields 	ses->se_flags |= NFS4_SESSION_DEAD;
13666b2b9b2SJ. Bruce Fields 	return nfs_ok;
13766b2b9b2SJ. Bruce Fields }
13866b2b9b2SJ. Bruce Fields 
139221a6876SJ. Bruce Fields static bool is_client_expired(struct nfs4_client *clp)
140221a6876SJ. Bruce Fields {
141221a6876SJ. Bruce Fields 	return clp->cl_time == 0;
142221a6876SJ. Bruce Fields }
143221a6876SJ. Bruce Fields 
144221a6876SJ. Bruce Fields static __be32 get_client_locked(struct nfs4_client *clp)
145221a6876SJ. Bruce Fields {
1460a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1470a880a28STrond Myklebust 
1480a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
1490a880a28STrond Myklebust 
150221a6876SJ. Bruce Fields 	if (is_client_expired(clp))
151221a6876SJ. Bruce Fields 		return nfserr_expired;
15214ed14ccSJ. Bruce Fields 	atomic_inc(&clp->cl_rpc_users);
153221a6876SJ. Bruce Fields 	return nfs_ok;
154221a6876SJ. Bruce Fields }
155221a6876SJ. Bruce Fields 
156221a6876SJ. Bruce Fields /* must be called under the client_lock */
157221a6876SJ. Bruce Fields static inline void
158221a6876SJ. Bruce Fields renew_client_locked(struct nfs4_client *clp)
159221a6876SJ. Bruce Fields {
160221a6876SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161221a6876SJ. Bruce Fields 
162221a6876SJ. Bruce Fields 	if (is_client_expired(clp)) {
163221a6876SJ. Bruce Fields 		WARN_ON(1);
164221a6876SJ. Bruce Fields 		printk("%s: client (clientid %08x/%08x) already expired\n",
165221a6876SJ. Bruce Fields 			__func__,
166221a6876SJ. Bruce Fields 			clp->cl_clientid.cl_boot,
167221a6876SJ. Bruce Fields 			clp->cl_clientid.cl_id);
168221a6876SJ. Bruce Fields 		return;
169221a6876SJ. Bruce Fields 	}
170221a6876SJ. Bruce Fields 
171221a6876SJ. Bruce Fields 	list_move_tail(&clp->cl_lru, &nn->client_lru);
17220b7d86fSArnd Bergmann 	clp->cl_time = ktime_get_boottime_seconds();
173221a6876SJ. Bruce Fields }
174221a6876SJ. Bruce Fields 
175ba138435SFengguang Wu static void put_client_renew_locked(struct nfs4_client *clp)
176221a6876SJ. Bruce Fields {
1770a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1780a880a28STrond Myklebust 
1790a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
1800a880a28STrond Myklebust 
18114ed14ccSJ. Bruce Fields 	if (!atomic_dec_and_test(&clp->cl_rpc_users))
182221a6876SJ. Bruce Fields 		return;
183221a6876SJ. Bruce Fields 	if (!is_client_expired(clp))
184221a6876SJ. Bruce Fields 		renew_client_locked(clp);
18589c905beSJ. Bruce Fields 	else
18689c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
187221a6876SJ. Bruce Fields }
188221a6876SJ. Bruce Fields 
1894b24ca7dSJeff Layton static void put_client_renew(struct nfs4_client *clp)
1904b24ca7dSJeff Layton {
1914b24ca7dSJeff Layton 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1924b24ca7dSJeff Layton 
19314ed14ccSJ. Bruce Fields 	if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
194d6c249b4SJeff Layton 		return;
195d6c249b4SJeff Layton 	if (!is_client_expired(clp))
196d6c249b4SJeff Layton 		renew_client_locked(clp);
19789c905beSJ. Bruce Fields 	else
19889c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
1994b24ca7dSJeff Layton 	spin_unlock(&nn->client_lock);
2004b24ca7dSJeff Layton }
2014b24ca7dSJeff Layton 
202d4e19e70STrond Myklebust static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203d4e19e70STrond Myklebust {
204d4e19e70STrond Myklebust 	__be32 status;
205d4e19e70STrond Myklebust 
206d4e19e70STrond Myklebust 	if (is_session_dead(ses))
207d4e19e70STrond Myklebust 		return nfserr_badsession;
208d4e19e70STrond Myklebust 	status = get_client_locked(ses->se_client);
209d4e19e70STrond Myklebust 	if (status)
210d4e19e70STrond Myklebust 		return status;
211d4e19e70STrond Myklebust 	atomic_inc(&ses->se_ref);
212d4e19e70STrond Myklebust 	return nfs_ok;
213d4e19e70STrond Myklebust }
214d4e19e70STrond Myklebust 
215d4e19e70STrond Myklebust static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216d4e19e70STrond Myklebust {
217d4e19e70STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
2180a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2190a880a28STrond Myklebust 
2200a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
221d4e19e70STrond Myklebust 
222d4e19e70STrond Myklebust 	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223d4e19e70STrond Myklebust 		free_session(ses);
224d4e19e70STrond Myklebust 	put_client_renew_locked(clp);
225d4e19e70STrond Myklebust }
226d4e19e70STrond Myklebust 
227d4e19e70STrond Myklebust static void nfsd4_put_session(struct nfsd4_session *ses)
228d4e19e70STrond Myklebust {
229d4e19e70STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
230d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231d4e19e70STrond Myklebust 
232d4e19e70STrond Myklebust 	spin_lock(&nn->client_lock);
233d4e19e70STrond Myklebust 	nfsd4_put_session_locked(ses);
234d4e19e70STrond Myklebust 	spin_unlock(&nn->client_lock);
235d4e19e70STrond Myklebust }
236d4e19e70STrond Myklebust 
23776d348faSJeff Layton static struct nfsd4_blocked_lock *
23876d348faSJeff Layton find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
23976d348faSJeff Layton 			struct nfsd_net *nn)
24076d348faSJeff Layton {
24176d348faSJeff Layton 	struct nfsd4_blocked_lock *cur, *found = NULL;
24276d348faSJeff Layton 
2430cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
24476d348faSJeff Layton 	list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
24576d348faSJeff Layton 		if (fh_match(fh, &cur->nbl_fh)) {
24676d348faSJeff Layton 			list_del_init(&cur->nbl_list);
2477919d0a2SJeff Layton 			list_del_init(&cur->nbl_lru);
24876d348faSJeff Layton 			found = cur;
24976d348faSJeff Layton 			break;
25076d348faSJeff Layton 		}
25176d348faSJeff Layton 	}
2520cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
25376d348faSJeff Layton 	if (found)
254cb03f94fSNeilBrown 		locks_delete_block(&found->nbl_lock);
25576d348faSJeff Layton 	return found;
25676d348faSJeff Layton }
25776d348faSJeff Layton 
25876d348faSJeff Layton static struct nfsd4_blocked_lock *
25976d348faSJeff Layton find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
26076d348faSJeff Layton 			struct nfsd_net *nn)
26176d348faSJeff Layton {
26276d348faSJeff Layton 	struct nfsd4_blocked_lock *nbl;
26376d348faSJeff Layton 
26476d348faSJeff Layton 	nbl = find_blocked_lock(lo, fh, nn);
26576d348faSJeff Layton 	if (!nbl) {
26676d348faSJeff Layton 		nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
26776d348faSJeff Layton 		if (nbl) {
268e1e8399eSVasily Averin 			INIT_LIST_HEAD(&nbl->nbl_list);
269e1e8399eSVasily Averin 			INIT_LIST_HEAD(&nbl->nbl_lru);
27076d348faSJeff Layton 			fh_copy_shallow(&nbl->nbl_fh, fh);
27176d348faSJeff Layton 			locks_init_lock(&nbl->nbl_lock);
27276d348faSJeff Layton 			nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
27376d348faSJeff Layton 					&nfsd4_cb_notify_lock_ops,
27476d348faSJeff Layton 					NFSPROC4_CLNT_CB_NOTIFY_LOCK);
27576d348faSJeff Layton 		}
27676d348faSJeff Layton 	}
27776d348faSJeff Layton 	return nbl;
27876d348faSJeff Layton }
27976d348faSJeff Layton 
28076d348faSJeff Layton static void
28176d348faSJeff Layton free_blocked_lock(struct nfsd4_blocked_lock *nbl)
28276d348faSJeff Layton {
2836aaafc43SJeff Layton 	locks_delete_block(&nbl->nbl_lock);
28476d348faSJeff Layton 	locks_release_private(&nbl->nbl_lock);
28576d348faSJeff Layton 	kfree(nbl);
28676d348faSJeff Layton }
28776d348faSJeff Layton 
28868ef3bc3SJeff Layton static void
28968ef3bc3SJeff Layton remove_blocked_locks(struct nfs4_lockowner *lo)
29068ef3bc3SJeff Layton {
29168ef3bc3SJeff Layton 	struct nfs4_client *clp = lo->lo_owner.so_client;
29268ef3bc3SJeff Layton 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
29368ef3bc3SJeff Layton 	struct nfsd4_blocked_lock *nbl;
29468ef3bc3SJeff Layton 	LIST_HEAD(reaplist);
29568ef3bc3SJeff Layton 
29668ef3bc3SJeff Layton 	/* Dequeue all blocked locks */
29768ef3bc3SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
29868ef3bc3SJeff Layton 	while (!list_empty(&lo->lo_blocked)) {
29968ef3bc3SJeff Layton 		nbl = list_first_entry(&lo->lo_blocked,
30068ef3bc3SJeff Layton 					struct nfsd4_blocked_lock,
30168ef3bc3SJeff Layton 					nbl_list);
30268ef3bc3SJeff Layton 		list_del_init(&nbl->nbl_list);
30368ef3bc3SJeff Layton 		list_move(&nbl->nbl_lru, &reaplist);
30468ef3bc3SJeff Layton 	}
30568ef3bc3SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
30668ef3bc3SJeff Layton 
30768ef3bc3SJeff Layton 	/* Now free them */
30868ef3bc3SJeff Layton 	while (!list_empty(&reaplist)) {
30968ef3bc3SJeff Layton 		nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
31068ef3bc3SJeff Layton 					nbl_lru);
31168ef3bc3SJeff Layton 		list_del_init(&nbl->nbl_lru);
31268ef3bc3SJeff Layton 		free_blocked_lock(nbl);
31368ef3bc3SJeff Layton 	}
31468ef3bc3SJeff Layton }
31568ef3bc3SJeff Layton 
316f456458eSJeff Layton static void
317f456458eSJeff Layton nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
318f456458eSJeff Layton {
319f456458eSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
320f456458eSJeff Layton 						struct nfsd4_blocked_lock, nbl_cb);
321f456458eSJeff Layton 	locks_delete_block(&nbl->nbl_lock);
322f456458eSJeff Layton }
323f456458eSJeff Layton 
32476d348faSJeff Layton static int
32576d348faSJeff Layton nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
32676d348faSJeff Layton {
32776d348faSJeff Layton 	/*
32876d348faSJeff Layton 	 * Since this is just an optimization, we don't try very hard if it
32976d348faSJeff Layton 	 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
33076d348faSJeff Layton 	 * just quit trying on anything else.
33176d348faSJeff Layton 	 */
33276d348faSJeff Layton 	switch (task->tk_status) {
33376d348faSJeff Layton 	case -NFS4ERR_DELAY:
33476d348faSJeff Layton 		rpc_delay(task, 1 * HZ);
33576d348faSJeff Layton 		return 0;
33676d348faSJeff Layton 	default:
33776d348faSJeff Layton 		return 1;
33876d348faSJeff Layton 	}
33976d348faSJeff Layton }
34076d348faSJeff Layton 
34176d348faSJeff Layton static void
34276d348faSJeff Layton nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
34376d348faSJeff Layton {
34476d348faSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
34576d348faSJeff Layton 						struct nfsd4_blocked_lock, nbl_cb);
34676d348faSJeff Layton 
34776d348faSJeff Layton 	free_blocked_lock(nbl);
34876d348faSJeff Layton }
34976d348faSJeff Layton 
35076d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
351f456458eSJeff Layton 	.prepare	= nfsd4_cb_notify_lock_prepare,
35276d348faSJeff Layton 	.done		= nfsd4_cb_notify_lock_done,
35376d348faSJeff Layton 	.release	= nfsd4_cb_notify_lock_release,
35476d348faSJeff Layton };
35576d348faSJeff Layton 
356b5971afaSKinglong Mee static inline struct nfs4_stateowner *
357b5971afaSKinglong Mee nfs4_get_stateowner(struct nfs4_stateowner *sop)
358b5971afaSKinglong Mee {
359b5971afaSKinglong Mee 	atomic_inc(&sop->so_count);
360b5971afaSKinglong Mee 	return sop;
361b5971afaSKinglong Mee }
362b5971afaSKinglong Mee 
3637ffb5880STrond Myklebust static int
364d4f0489fSTrond Myklebust same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
3657ffb5880STrond Myklebust {
3667ffb5880STrond Myklebust 	return (sop->so_owner.len == owner->len) &&
367d4f0489fSTrond Myklebust 		0 == memcmp(sop->so_owner.data, owner->data, owner->len);
3687ffb5880STrond Myklebust }
3697ffb5880STrond Myklebust 
3707ffb5880STrond Myklebust static struct nfs4_openowner *
3717ffb5880STrond Myklebust find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
372d4f0489fSTrond Myklebust 			struct nfs4_client *clp)
3737ffb5880STrond Myklebust {
3747ffb5880STrond Myklebust 	struct nfs4_stateowner *so;
3757ffb5880STrond Myklebust 
376d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
3777ffb5880STrond Myklebust 
378d4f0489fSTrond Myklebust 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
379d4f0489fSTrond Myklebust 			    so_strhash) {
3807ffb5880STrond Myklebust 		if (!so->so_is_open_owner)
3817ffb5880STrond Myklebust 			continue;
382b5971afaSKinglong Mee 		if (same_owner_str(so, &open->op_owner))
383b5971afaSKinglong Mee 			return openowner(nfs4_get_stateowner(so));
3847ffb5880STrond Myklebust 	}
3857ffb5880STrond Myklebust 	return NULL;
3867ffb5880STrond Myklebust }
3877ffb5880STrond Myklebust 
3887ffb5880STrond Myklebust static struct nfs4_openowner *
3897ffb5880STrond Myklebust find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
390d4f0489fSTrond Myklebust 			struct nfs4_client *clp)
3917ffb5880STrond Myklebust {
3927ffb5880STrond Myklebust 	struct nfs4_openowner *oo;
3937ffb5880STrond Myklebust 
394d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
395d4f0489fSTrond Myklebust 	oo = find_openstateowner_str_locked(hashval, open, clp);
396d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
3977ffb5880STrond Myklebust 	return oo;
3987ffb5880STrond Myklebust }
3997ffb5880STrond Myklebust 
4001da177e4SLinus Torvalds static inline u32
4011da177e4SLinus Torvalds opaque_hashval(const void *ptr, int nbytes)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	unsigned char *cptr = (unsigned char *) ptr;
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	u32 x = 0;
4061da177e4SLinus Torvalds 	while (nbytes--) {
4071da177e4SLinus Torvalds 		x *= 37;
4081da177e4SLinus Torvalds 		x += *cptr++;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 	return x;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4135b095e99SJeff Layton static void nfsd4_free_file_rcu(struct rcu_head *rcu)
41432513b40SJ. Bruce Fields {
4155b095e99SJeff Layton 	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
4165b095e99SJeff Layton 
4175b095e99SJeff Layton 	kmem_cache_free(file_slab, fp);
41832513b40SJ. Bruce Fields }
41932513b40SJ. Bruce Fields 
420e6ba76e1SChristoph Hellwig void
42113cd2184SNeilBrown put_nfs4_file(struct nfs4_file *fi)
42213cd2184SNeilBrown {
42302e1215fSJeff Layton 	might_lock(&state_lock);
42402e1215fSJeff Layton 
425818a34ebSElena Reshetova 	if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
4265b095e99SJeff Layton 		hlist_del_rcu(&fi->fi_hash);
427cdc97505SBenny Halevy 		spin_unlock(&state_lock);
4288287f009SSachin Bhamare 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
4295b095e99SJeff Layton 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
4305b095e99SJeff Layton 		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
4318b671b80SJ. Bruce Fields 	}
43213cd2184SNeilBrown }
43313cd2184SNeilBrown 
434eb82dd39SJeff Layton static struct nfsd_file *
435de18643dSTrond Myklebust __nfs4_get_fd(struct nfs4_file *f, int oflag)
436de18643dSTrond Myklebust {
437de18643dSTrond Myklebust 	if (f->fi_fds[oflag])
438eb82dd39SJeff Layton 		return nfsd_file_get(f->fi_fds[oflag]);
439de18643dSTrond Myklebust 	return NULL;
440de18643dSTrond Myklebust }
441de18643dSTrond Myklebust 
442eb82dd39SJeff Layton static struct nfsd_file *
443de18643dSTrond Myklebust find_writeable_file_locked(struct nfs4_file *f)
444de18643dSTrond Myklebust {
445eb82dd39SJeff Layton 	struct nfsd_file *ret;
446de18643dSTrond Myklebust 
447de18643dSTrond Myklebust 	lockdep_assert_held(&f->fi_lock);
448de18643dSTrond Myklebust 
449de18643dSTrond Myklebust 	ret = __nfs4_get_fd(f, O_WRONLY);
450de18643dSTrond Myklebust 	if (!ret)
451de18643dSTrond Myklebust 		ret = __nfs4_get_fd(f, O_RDWR);
452de18643dSTrond Myklebust 	return ret;
453de18643dSTrond Myklebust }
454de18643dSTrond Myklebust 
455eb82dd39SJeff Layton static struct nfsd_file *
456de18643dSTrond Myklebust find_writeable_file(struct nfs4_file *f)
457de18643dSTrond Myklebust {
458eb82dd39SJeff Layton 	struct nfsd_file *ret;
459de18643dSTrond Myklebust 
460de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
461de18643dSTrond Myklebust 	ret = find_writeable_file_locked(f);
462de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
463de18643dSTrond Myklebust 
464de18643dSTrond Myklebust 	return ret;
465de18643dSTrond Myklebust }
466de18643dSTrond Myklebust 
467eb82dd39SJeff Layton static struct nfsd_file *
468eb82dd39SJeff Layton find_readable_file_locked(struct nfs4_file *f)
469de18643dSTrond Myklebust {
470eb82dd39SJeff Layton 	struct nfsd_file *ret;
471de18643dSTrond Myklebust 
472de18643dSTrond Myklebust 	lockdep_assert_held(&f->fi_lock);
473de18643dSTrond Myklebust 
474de18643dSTrond Myklebust 	ret = __nfs4_get_fd(f, O_RDONLY);
475de18643dSTrond Myklebust 	if (!ret)
476de18643dSTrond Myklebust 		ret = __nfs4_get_fd(f, O_RDWR);
477de18643dSTrond Myklebust 	return ret;
478de18643dSTrond Myklebust }
479de18643dSTrond Myklebust 
480eb82dd39SJeff Layton static struct nfsd_file *
481de18643dSTrond Myklebust find_readable_file(struct nfs4_file *f)
482de18643dSTrond Myklebust {
483eb82dd39SJeff Layton 	struct nfsd_file *ret;
484de18643dSTrond Myklebust 
485de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
486de18643dSTrond Myklebust 	ret = find_readable_file_locked(f);
487de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
488de18643dSTrond Myklebust 
489de18643dSTrond Myklebust 	return ret;
490de18643dSTrond Myklebust }
491de18643dSTrond Myklebust 
492eb82dd39SJeff Layton struct nfsd_file *
493de18643dSTrond Myklebust find_any_file(struct nfs4_file *f)
494de18643dSTrond Myklebust {
495eb82dd39SJeff Layton 	struct nfsd_file *ret;
496de18643dSTrond Myklebust 
497a451b123STrond Myklebust 	if (!f)
498a451b123STrond Myklebust 		return NULL;
499de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
500de18643dSTrond Myklebust 	ret = __nfs4_get_fd(f, O_RDWR);
501de18643dSTrond Myklebust 	if (!ret) {
502de18643dSTrond Myklebust 		ret = __nfs4_get_fd(f, O_WRONLY);
503de18643dSTrond Myklebust 		if (!ret)
504de18643dSTrond Myklebust 			ret = __nfs4_get_fd(f, O_RDONLY);
505de18643dSTrond Myklebust 	}
506de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
507de18643dSTrond Myklebust 	return ret;
508de18643dSTrond Myklebust }
509de18643dSTrond Myklebust 
51002a3508dSTrond Myklebust static atomic_long_t num_delegations;
511697ce9beSZhang Yanfei unsigned long max_delegations;
512ef0f3390SNeilBrown 
513ef0f3390SNeilBrown /*
514ef0f3390SNeilBrown  * Open owner state (share locks)
515ef0f3390SNeilBrown  */
516ef0f3390SNeilBrown 
51716bfdaafSJ. Bruce Fields /* hash tables for lock and open owners */
51816bfdaafSJ. Bruce Fields #define OWNER_HASH_BITS              8
51916bfdaafSJ. Bruce Fields #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
52016bfdaafSJ. Bruce Fields #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
521ef0f3390SNeilBrown 
522d4f0489fSTrond Myklebust static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
523ddc04c41SJ. Bruce Fields {
524ddc04c41SJ. Bruce Fields 	unsigned int ret;
525ddc04c41SJ. Bruce Fields 
526ddc04c41SJ. Bruce Fields 	ret = opaque_hashval(ownername->data, ownername->len);
52716bfdaafSJ. Bruce Fields 	return ret & OWNER_HASH_MASK;
528ddc04c41SJ. Bruce Fields }
529ef0f3390SNeilBrown 
530ef0f3390SNeilBrown /* hash table for nfs4_file */
531ef0f3390SNeilBrown #define FILE_HASH_BITS                   8
532ef0f3390SNeilBrown #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
53335079582SShan Wei 
534ca943217STrond Myklebust static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
535ddc04c41SJ. Bruce Fields {
536ca943217STrond Myklebust 	return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
537ca943217STrond Myklebust }
538ca943217STrond Myklebust 
539ca943217STrond Myklebust static unsigned int file_hashval(struct knfsd_fh *fh)
540ca943217STrond Myklebust {
541ca943217STrond Myklebust 	return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
542ca943217STrond Myklebust }
543ca943217STrond Myklebust 
54489876f8cSJeff Layton static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
545ef0f3390SNeilBrown 
54612659651SJeff Layton static void
54712659651SJeff Layton __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
5483477565eSJ. Bruce Fields {
5497214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
5507214e860SJeff Layton 
55112659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_WRITE)
55212659651SJeff Layton 		atomic_inc(&fp->fi_access[O_WRONLY]);
55312659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_READ)
55412659651SJeff Layton 		atomic_inc(&fp->fi_access[O_RDONLY]);
5553477565eSJ. Bruce Fields }
5563477565eSJ. Bruce Fields 
55712659651SJeff Layton static __be32
55812659651SJeff Layton nfs4_file_get_access(struct nfs4_file *fp, u32 access)
559998db52cSJ. Bruce Fields {
5607214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
5617214e860SJeff Layton 
56212659651SJeff Layton 	/* Does this access mode make sense? */
56312659651SJeff Layton 	if (access & ~NFS4_SHARE_ACCESS_BOTH)
56412659651SJeff Layton 		return nfserr_inval;
56512659651SJeff Layton 
566baeb4ff0SJeff Layton 	/* Does it conflict with a deny mode already set? */
567baeb4ff0SJeff Layton 	if ((access & fp->fi_share_deny) != 0)
568baeb4ff0SJeff Layton 		return nfserr_share_denied;
569baeb4ff0SJeff Layton 
57012659651SJeff Layton 	__nfs4_file_get_access(fp, access);
57112659651SJeff Layton 	return nfs_ok;
572998db52cSJ. Bruce Fields }
573998db52cSJ. Bruce Fields 
574baeb4ff0SJeff Layton static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
575baeb4ff0SJeff Layton {
576baeb4ff0SJeff Layton 	/* Common case is that there is no deny mode. */
577baeb4ff0SJeff Layton 	if (deny) {
578baeb4ff0SJeff Layton 		/* Does this deny mode make sense? */
579baeb4ff0SJeff Layton 		if (deny & ~NFS4_SHARE_DENY_BOTH)
580baeb4ff0SJeff Layton 			return nfserr_inval;
581baeb4ff0SJeff Layton 
582baeb4ff0SJeff Layton 		if ((deny & NFS4_SHARE_DENY_READ) &&
583baeb4ff0SJeff Layton 		    atomic_read(&fp->fi_access[O_RDONLY]))
584baeb4ff0SJeff Layton 			return nfserr_share_denied;
585baeb4ff0SJeff Layton 
586baeb4ff0SJeff Layton 		if ((deny & NFS4_SHARE_DENY_WRITE) &&
587baeb4ff0SJeff Layton 		    atomic_read(&fp->fi_access[O_WRONLY]))
588baeb4ff0SJeff Layton 			return nfserr_share_denied;
589baeb4ff0SJeff Layton 	}
590baeb4ff0SJeff Layton 	return nfs_ok;
591baeb4ff0SJeff Layton }
592baeb4ff0SJeff Layton 
593998db52cSJ. Bruce Fields static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
594f9d7562fSJ. Bruce Fields {
595de18643dSTrond Myklebust 	might_lock(&fp->fi_lock);
596de18643dSTrond Myklebust 
597de18643dSTrond Myklebust 	if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
598fd4f83fdSJeff Layton 		struct nfsd_file *f1 = NULL;
599fd4f83fdSJeff Layton 		struct nfsd_file *f2 = NULL;
600de18643dSTrond Myklebust 
6016d338b51SJeff Layton 		swap(f1, fp->fi_fds[oflag]);
6020c7c3e67SJ. Bruce Fields 		if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
6036d338b51SJeff Layton 			swap(f2, fp->fi_fds[O_RDWR]);
604de18643dSTrond Myklebust 		spin_unlock(&fp->fi_lock);
605de18643dSTrond Myklebust 		if (f1)
606fd4f83fdSJeff Layton 			nfsd_file_put(f1);
607de18643dSTrond Myklebust 		if (f2)
608fd4f83fdSJeff Layton 			nfsd_file_put(f2);
609f9d7562fSJ. Bruce Fields 	}
610f9d7562fSJ. Bruce Fields }
611f9d7562fSJ. Bruce Fields 
61212659651SJeff Layton static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
613998db52cSJ. Bruce Fields {
61412659651SJeff Layton 	WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
61512659651SJeff Layton 
61612659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_WRITE)
617998db52cSJ. Bruce Fields 		__nfs4_file_put_access(fp, O_WRONLY);
61812659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_READ)
61912659651SJeff Layton 		__nfs4_file_put_access(fp, O_RDONLY);
620998db52cSJ. Bruce Fields }
621998db52cSJ. Bruce Fields 
6228287f009SSachin Bhamare /*
6238287f009SSachin Bhamare  * Allocate a new open/delegation state counter. This is needed for
6248287f009SSachin Bhamare  * pNFS for proper return on close semantics.
6258287f009SSachin Bhamare  *
6268287f009SSachin Bhamare  * Note that we only allocate it for pNFS-enabled exports, otherwise
6278287f009SSachin Bhamare  * all pointers to struct nfs4_clnt_odstate are always NULL.
6288287f009SSachin Bhamare  */
6298287f009SSachin Bhamare static struct nfs4_clnt_odstate *
6308287f009SSachin Bhamare alloc_clnt_odstate(struct nfs4_client *clp)
6318287f009SSachin Bhamare {
6328287f009SSachin Bhamare 	struct nfs4_clnt_odstate *co;
6338287f009SSachin Bhamare 
6348287f009SSachin Bhamare 	co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
6358287f009SSachin Bhamare 	if (co) {
6368287f009SSachin Bhamare 		co->co_client = clp;
637cff7cb2eSElena Reshetova 		refcount_set(&co->co_odcount, 1);
6388287f009SSachin Bhamare 	}
6398287f009SSachin Bhamare 	return co;
6408287f009SSachin Bhamare }
6418287f009SSachin Bhamare 
6428287f009SSachin Bhamare static void
6438287f009SSachin Bhamare hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
6448287f009SSachin Bhamare {
6458287f009SSachin Bhamare 	struct nfs4_file *fp = co->co_file;
6468287f009SSachin Bhamare 
6478287f009SSachin Bhamare 	lockdep_assert_held(&fp->fi_lock);
6488287f009SSachin Bhamare 	list_add(&co->co_perfile, &fp->fi_clnt_odstate);
6498287f009SSachin Bhamare }
6508287f009SSachin Bhamare 
6518287f009SSachin Bhamare static inline void
6528287f009SSachin Bhamare get_clnt_odstate(struct nfs4_clnt_odstate *co)
6538287f009SSachin Bhamare {
6548287f009SSachin Bhamare 	if (co)
655cff7cb2eSElena Reshetova 		refcount_inc(&co->co_odcount);
6568287f009SSachin Bhamare }
6578287f009SSachin Bhamare 
6588287f009SSachin Bhamare static void
6598287f009SSachin Bhamare put_clnt_odstate(struct nfs4_clnt_odstate *co)
6608287f009SSachin Bhamare {
6618287f009SSachin Bhamare 	struct nfs4_file *fp;
6628287f009SSachin Bhamare 
6638287f009SSachin Bhamare 	if (!co)
6648287f009SSachin Bhamare 		return;
6658287f009SSachin Bhamare 
6668287f009SSachin Bhamare 	fp = co->co_file;
667cff7cb2eSElena Reshetova 	if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
6688287f009SSachin Bhamare 		list_del(&co->co_perfile);
6698287f009SSachin Bhamare 		spin_unlock(&fp->fi_lock);
6708287f009SSachin Bhamare 
6718287f009SSachin Bhamare 		nfsd4_return_all_file_layouts(co->co_client, fp);
6728287f009SSachin Bhamare 		kmem_cache_free(odstate_slab, co);
6738287f009SSachin Bhamare 	}
6748287f009SSachin Bhamare }
6758287f009SSachin Bhamare 
6768287f009SSachin Bhamare static struct nfs4_clnt_odstate *
6778287f009SSachin Bhamare find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
6788287f009SSachin Bhamare {
6798287f009SSachin Bhamare 	struct nfs4_clnt_odstate *co;
6808287f009SSachin Bhamare 	struct nfs4_client *cl;
6818287f009SSachin Bhamare 
6828287f009SSachin Bhamare 	if (!new)
6838287f009SSachin Bhamare 		return NULL;
6848287f009SSachin Bhamare 
6858287f009SSachin Bhamare 	cl = new->co_client;
6868287f009SSachin Bhamare 
6878287f009SSachin Bhamare 	spin_lock(&fp->fi_lock);
6888287f009SSachin Bhamare 	list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
6898287f009SSachin Bhamare 		if (co->co_client == cl) {
6908287f009SSachin Bhamare 			get_clnt_odstate(co);
6918287f009SSachin Bhamare 			goto out;
6928287f009SSachin Bhamare 		}
6938287f009SSachin Bhamare 	}
6948287f009SSachin Bhamare 	co = new;
6958287f009SSachin Bhamare 	co->co_file = fp;
6968287f009SSachin Bhamare 	hash_clnt_odstate_locked(new);
6978287f009SSachin Bhamare out:
6988287f009SSachin Bhamare 	spin_unlock(&fp->fi_lock);
6998287f009SSachin Bhamare 	return co;
7008287f009SSachin Bhamare }
7018287f009SSachin Bhamare 
702d19fb70dSKinglong Mee struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
703d19fb70dSKinglong Mee 				  void (*sc_free)(struct nfs4_stid *))
704996e0938SJ. Bruce Fields {
7053abdb607SJ. Bruce Fields 	struct nfs4_stid *stid;
7063abdb607SJ. Bruce Fields 	int new_id;
7073abdb607SJ. Bruce Fields 
708f8338834STrond Myklebust 	stid = kmem_cache_zalloc(slab, GFP_KERNEL);
7093abdb607SJ. Bruce Fields 	if (!stid)
7103abdb607SJ. Bruce Fields 		return NULL;
711996e0938SJ. Bruce Fields 
7124770d722SJeff Layton 	idr_preload(GFP_KERNEL);
7134770d722SJeff Layton 	spin_lock(&cl->cl_lock);
71478599c42SJ. Bruce Fields 	/* Reserving 0 for start of file in nfsdfs "states" file: */
71578599c42SJ. Bruce Fields 	new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
7164770d722SJeff Layton 	spin_unlock(&cl->cl_lock);
7174770d722SJeff Layton 	idr_preload_end();
718ebd6c707STejun Heo 	if (new_id < 0)
7193abdb607SJ. Bruce Fields 		goto out_free;
720d19fb70dSKinglong Mee 
721d19fb70dSKinglong Mee 	stid->sc_free = sc_free;
7223abdb607SJ. Bruce Fields 	stid->sc_client = cl;
7233abdb607SJ. Bruce Fields 	stid->sc_stateid.si_opaque.so_id = new_id;
7243abdb607SJ. Bruce Fields 	stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
7253abdb607SJ. Bruce Fields 	/* Will be incremented before return to client: */
726a15dfcd5SElena Reshetova 	refcount_set(&stid->sc_count, 1);
7279767feb2SJeff Layton 	spin_lock_init(&stid->sc_lock);
728624322f1SOlga Kornievskaia 	INIT_LIST_HEAD(&stid->sc_cp_list);
7293abdb607SJ. Bruce Fields 
730996e0938SJ. Bruce Fields 	/*
7313abdb607SJ. Bruce Fields 	 * It shouldn't be a problem to reuse an opaque stateid value.
7323abdb607SJ. Bruce Fields 	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
7333abdb607SJ. Bruce Fields 	 * example, a stray write retransmission could be accepted by
7343abdb607SJ. Bruce Fields 	 * the server when it should have been rejected.  Therefore,
7353abdb607SJ. Bruce Fields 	 * adopt a trick from the sctp code to attempt to maximize the
7363abdb607SJ. Bruce Fields 	 * amount of time until an id is reused, by ensuring they always
7373abdb607SJ. Bruce Fields 	 * "increase" (mod INT_MAX):
738996e0938SJ. Bruce Fields 	 */
7393abdb607SJ. Bruce Fields 	return stid;
7403abdb607SJ. Bruce Fields out_free:
7412c44a234SWei Yongjun 	kmem_cache_free(slab, stid);
7423abdb607SJ. Bruce Fields 	return NULL;
7432a74aba7SJ. Bruce Fields }
7442a74aba7SJ. Bruce Fields 
745e0639dc5SOlga Kornievskaia /*
746e0639dc5SOlga Kornievskaia  * Create a unique stateid_t to represent each COPY.
747e0639dc5SOlga Kornievskaia  */
748624322f1SOlga Kornievskaia static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
749624322f1SOlga Kornievskaia 			      unsigned char sc_type)
750e0639dc5SOlga Kornievskaia {
751e0639dc5SOlga Kornievskaia 	int new_id;
752e0639dc5SOlga Kornievskaia 
7539cc76801SArnd Bergmann 	stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
754624322f1SOlga Kornievskaia 	stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
755624322f1SOlga Kornievskaia 	stid->sc_type = sc_type;
756624322f1SOlga Kornievskaia 
757e0639dc5SOlga Kornievskaia 	idr_preload(GFP_KERNEL);
758e0639dc5SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
759624322f1SOlga Kornievskaia 	new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
760624322f1SOlga Kornievskaia 	stid->stid.si_opaque.so_id = new_id;
761e0639dc5SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
762e0639dc5SOlga Kornievskaia 	idr_preload_end();
763e0639dc5SOlga Kornievskaia 	if (new_id < 0)
764e0639dc5SOlga Kornievskaia 		return 0;
765e0639dc5SOlga Kornievskaia 	return 1;
766e0639dc5SOlga Kornievskaia }
767e0639dc5SOlga Kornievskaia 
768624322f1SOlga Kornievskaia int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
769624322f1SOlga Kornievskaia {
770624322f1SOlga Kornievskaia 	return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
771624322f1SOlga Kornievskaia }
772624322f1SOlga Kornievskaia 
773624322f1SOlga Kornievskaia struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
774624322f1SOlga Kornievskaia 						     struct nfs4_stid *p_stid)
775624322f1SOlga Kornievskaia {
776624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
777624322f1SOlga Kornievskaia 
778624322f1SOlga Kornievskaia 	cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
779624322f1SOlga Kornievskaia 	if (!cps)
780624322f1SOlga Kornievskaia 		return NULL;
78120b7d86fSArnd Bergmann 	cps->cpntf_time = ktime_get_boottime_seconds();
782624322f1SOlga Kornievskaia 	refcount_set(&cps->cp_stateid.sc_count, 1);
783624322f1SOlga Kornievskaia 	if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
784624322f1SOlga Kornievskaia 		goto out_free;
785624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
786624322f1SOlga Kornievskaia 	list_add(&cps->cp_list, &p_stid->sc_cp_list);
787624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
788624322f1SOlga Kornievskaia 	return cps;
789624322f1SOlga Kornievskaia out_free:
790624322f1SOlga Kornievskaia 	kfree(cps);
791624322f1SOlga Kornievskaia 	return NULL;
792624322f1SOlga Kornievskaia }
793624322f1SOlga Kornievskaia 
794624322f1SOlga Kornievskaia void nfs4_free_copy_state(struct nfsd4_copy *copy)
795e0639dc5SOlga Kornievskaia {
796e0639dc5SOlga Kornievskaia 	struct nfsd_net *nn;
797e0639dc5SOlga Kornievskaia 
798624322f1SOlga Kornievskaia 	WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
799e0639dc5SOlga Kornievskaia 	nn = net_generic(copy->cp_clp->net, nfsd_net_id);
800e0639dc5SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
801624322f1SOlga Kornievskaia 	idr_remove(&nn->s2s_cp_stateids,
802624322f1SOlga Kornievskaia 		   copy->cp_stateid.stid.si_opaque.so_id);
803624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
804624322f1SOlga Kornievskaia }
805624322f1SOlga Kornievskaia 
806624322f1SOlga Kornievskaia static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
807624322f1SOlga Kornievskaia {
808624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
809624322f1SOlga Kornievskaia 	struct nfsd_net *nn;
810624322f1SOlga Kornievskaia 
811624322f1SOlga Kornievskaia 	nn = net_generic(net, nfsd_net_id);
812624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
813624322f1SOlga Kornievskaia 	while (!list_empty(&stid->sc_cp_list)) {
814624322f1SOlga Kornievskaia 		cps = list_first_entry(&stid->sc_cp_list,
815624322f1SOlga Kornievskaia 				       struct nfs4_cpntf_state, cp_list);
816624322f1SOlga Kornievskaia 		_free_cpntf_state_locked(nn, cps);
817624322f1SOlga Kornievskaia 	}
818e0639dc5SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
819e0639dc5SOlga Kornievskaia }
820e0639dc5SOlga Kornievskaia 
821b49e084dSJeff Layton static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
8224cdc951bSJ. Bruce Fields {
8236011695dSTrond Myklebust 	struct nfs4_stid *stid;
8246011695dSTrond Myklebust 
825d19fb70dSKinglong Mee 	stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
8266011695dSTrond Myklebust 	if (!stid)
8276011695dSTrond Myklebust 		return NULL;
8286011695dSTrond Myklebust 
829d19fb70dSKinglong Mee 	return openlockstateid(stid);
8306011695dSTrond Myklebust }
8316011695dSTrond Myklebust 
8326011695dSTrond Myklebust static void nfs4_free_deleg(struct nfs4_stid *stid)
8336011695dSTrond Myklebust {
8346011695dSTrond Myklebust 	kmem_cache_free(deleg_slab, stid);
8356011695dSTrond Myklebust 	atomic_long_dec(&num_delegations);
8364cdc951bSJ. Bruce Fields }
8374cdc951bSJ. Bruce Fields 
8386282cd56SNeilBrown /*
8396282cd56SNeilBrown  * When we recall a delegation, we should be careful not to hand it
8406282cd56SNeilBrown  * out again straight away.
8416282cd56SNeilBrown  * To ensure this we keep a pair of bloom filters ('new' and 'old')
8426282cd56SNeilBrown  * in which the filehandles of recalled delegations are "stored".
8436282cd56SNeilBrown  * If a filehandle appear in either filter, a delegation is blocked.
8446282cd56SNeilBrown  * When a delegation is recalled, the filehandle is stored in the "new"
8456282cd56SNeilBrown  * filter.
8466282cd56SNeilBrown  * Every 30 seconds we swap the filters and clear the "new" one,
8476282cd56SNeilBrown  * unless both are empty of course.
8486282cd56SNeilBrown  *
8496282cd56SNeilBrown  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
8506282cd56SNeilBrown  * low 3 bytes as hash-table indices.
8516282cd56SNeilBrown  *
852f54fe962SJeff Layton  * 'blocked_delegations_lock', which is always taken in block_delegations(),
8536282cd56SNeilBrown  * is used to manage concurrent access.  Testing does not need the lock
8546282cd56SNeilBrown  * except when swapping the two filters.
8556282cd56SNeilBrown  */
856f54fe962SJeff Layton static DEFINE_SPINLOCK(blocked_delegations_lock);
8576282cd56SNeilBrown static struct bloom_pair {
8586282cd56SNeilBrown 	int	entries, old_entries;
859b3f255efSArnd Bergmann 	time64_t swap_time;
8606282cd56SNeilBrown 	int	new; /* index into 'set' */
8616282cd56SNeilBrown 	DECLARE_BITMAP(set[2], 256);
8626282cd56SNeilBrown } blocked_delegations;
8636282cd56SNeilBrown 
8646282cd56SNeilBrown static int delegation_blocked(struct knfsd_fh *fh)
8656282cd56SNeilBrown {
8666282cd56SNeilBrown 	u32 hash;
8676282cd56SNeilBrown 	struct bloom_pair *bd = &blocked_delegations;
8686282cd56SNeilBrown 
8696282cd56SNeilBrown 	if (bd->entries == 0)
8706282cd56SNeilBrown 		return 0;
871b3f255efSArnd Bergmann 	if (ktime_get_seconds() - bd->swap_time > 30) {
872f54fe962SJeff Layton 		spin_lock(&blocked_delegations_lock);
873b3f255efSArnd Bergmann 		if (ktime_get_seconds() - bd->swap_time > 30) {
8746282cd56SNeilBrown 			bd->entries -= bd->old_entries;
8756282cd56SNeilBrown 			bd->old_entries = bd->entries;
8766282cd56SNeilBrown 			memset(bd->set[bd->new], 0,
8776282cd56SNeilBrown 			       sizeof(bd->set[0]));
8786282cd56SNeilBrown 			bd->new = 1-bd->new;
879b3f255efSArnd Bergmann 			bd->swap_time = ktime_get_seconds();
8806282cd56SNeilBrown 		}
881f54fe962SJeff Layton 		spin_unlock(&blocked_delegations_lock);
8826282cd56SNeilBrown 	}
88387545899SDaniel Borkmann 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
8846282cd56SNeilBrown 	if (test_bit(hash&255, bd->set[0]) &&
8856282cd56SNeilBrown 	    test_bit((hash>>8)&255, bd->set[0]) &&
8866282cd56SNeilBrown 	    test_bit((hash>>16)&255, bd->set[0]))
8876282cd56SNeilBrown 		return 1;
8886282cd56SNeilBrown 
8896282cd56SNeilBrown 	if (test_bit(hash&255, bd->set[1]) &&
8906282cd56SNeilBrown 	    test_bit((hash>>8)&255, bd->set[1]) &&
8916282cd56SNeilBrown 	    test_bit((hash>>16)&255, bd->set[1]))
8926282cd56SNeilBrown 		return 1;
8936282cd56SNeilBrown 
8946282cd56SNeilBrown 	return 0;
8956282cd56SNeilBrown }
8966282cd56SNeilBrown 
8976282cd56SNeilBrown static void block_delegations(struct knfsd_fh *fh)
8986282cd56SNeilBrown {
8996282cd56SNeilBrown 	u32 hash;
9006282cd56SNeilBrown 	struct bloom_pair *bd = &blocked_delegations;
9016282cd56SNeilBrown 
90287545899SDaniel Borkmann 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
9036282cd56SNeilBrown 
904f54fe962SJeff Layton 	spin_lock(&blocked_delegations_lock);
9056282cd56SNeilBrown 	__set_bit(hash&255, bd->set[bd->new]);
9066282cd56SNeilBrown 	__set_bit((hash>>8)&255, bd->set[bd->new]);
9076282cd56SNeilBrown 	__set_bit((hash>>16)&255, bd->set[bd->new]);
9086282cd56SNeilBrown 	if (bd->entries == 0)
909b3f255efSArnd Bergmann 		bd->swap_time = ktime_get_seconds();
9106282cd56SNeilBrown 	bd->entries += 1;
911f54fe962SJeff Layton 	spin_unlock(&blocked_delegations_lock);
9126282cd56SNeilBrown }
9136282cd56SNeilBrown 
9141da177e4SLinus Torvalds static struct nfs4_delegation *
91586d29b10SJ. Bruce Fields alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
91686d29b10SJ. Bruce Fields 		 struct svc_fh *current_fh,
9178287f009SSachin Bhamare 		 struct nfs4_clnt_odstate *odstate)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
92002a3508dSTrond Myklebust 	long n;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	dprintk("NFSD alloc_init_deleg\n");
92302a3508dSTrond Myklebust 	n = atomic_long_inc_return(&num_delegations);
92402a3508dSTrond Myklebust 	if (n < 0 || n > max_delegations)
92502a3508dSTrond Myklebust 		goto out_dec;
9266282cd56SNeilBrown 	if (delegation_blocked(&current_fh->fh_handle))
92702a3508dSTrond Myklebust 		goto out_dec;
928d19fb70dSKinglong Mee 	dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
9295b2d21c1SNeilBrown 	if (dp == NULL)
93002a3508dSTrond Myklebust 		goto out_dec;
9316011695dSTrond Myklebust 
9322a74aba7SJ. Bruce Fields 	/*
9332a74aba7SJ. Bruce Fields 	 * delegation seqid's are never incremented.  The 4.1 special
9346136d2b4SJ. Bruce Fields 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
9356136d2b4SJ. Bruce Fields 	 * 0 anyway just for consistency and use 1:
9362a74aba7SJ. Bruce Fields 	 */
9372a74aba7SJ. Bruce Fields 	dp->dl_stid.sc_stateid.si_generation = 1;
938ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perfile);
939ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perclnt);
9401da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dp->dl_recall_lru);
9418287f009SSachin Bhamare 	dp->dl_clnt_odstate = odstate;
9428287f009SSachin Bhamare 	get_clnt_odstate(odstate);
94399c41515SJ. Bruce Fields 	dp->dl_type = NFS4_OPEN_DELEGATE_READ;
944f0b5de1bSChristoph Hellwig 	dp->dl_retries = 1;
945f0b5de1bSChristoph Hellwig 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
9460162ac2bSChristoph Hellwig 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
94786d29b10SJ. Bruce Fields 	get_nfs4_file(fp);
94886d29b10SJ. Bruce Fields 	dp->dl_stid.sc_file = fp;
9491da177e4SLinus Torvalds 	return dp;
95002a3508dSTrond Myklebust out_dec:
95102a3508dSTrond Myklebust 	atomic_long_dec(&num_delegations);
95202a3508dSTrond Myklebust 	return NULL;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds void
9566011695dSTrond Myklebust nfs4_put_stid(struct nfs4_stid *s)
9571da177e4SLinus Torvalds {
95811b9164aSTrond Myklebust 	struct nfs4_file *fp = s->sc_file;
9596011695dSTrond Myklebust 	struct nfs4_client *clp = s->sc_client;
9606011695dSTrond Myklebust 
9614770d722SJeff Layton 	might_lock(&clp->cl_lock);
9624770d722SJeff Layton 
963a15dfcd5SElena Reshetova 	if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
964b401be22SJeff Layton 		wake_up_all(&close_wq);
9656011695dSTrond Myklebust 		return;
966b401be22SJeff Layton 	}
9676011695dSTrond Myklebust 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
968624322f1SOlga Kornievskaia 	nfs4_free_cpntf_statelist(clp->net, s);
9694770d722SJeff Layton 	spin_unlock(&clp->cl_lock);
9706011695dSTrond Myklebust 	s->sc_free(s);
97111b9164aSTrond Myklebust 	if (fp)
97211b9164aSTrond Myklebust 		put_nfs4_file(fp);
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
9759767feb2SJeff Layton void
9769767feb2SJeff Layton nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
9779767feb2SJeff Layton {
9789767feb2SJeff Layton 	stateid_t *src = &stid->sc_stateid;
9799767feb2SJeff Layton 
9809767feb2SJeff Layton 	spin_lock(&stid->sc_lock);
9819767feb2SJeff Layton 	if (unlikely(++src->si_generation == 0))
9829767feb2SJeff Layton 		src->si_generation = 1;
9839767feb2SJeff Layton 	memcpy(dst, src, sizeof(*dst));
9849767feb2SJeff Layton 	spin_unlock(&stid->sc_lock);
9859767feb2SJeff Layton }
9869767feb2SJeff Layton 
987353601e7SJ. Bruce Fields static void put_deleg_file(struct nfs4_file *fp)
9881da177e4SLinus Torvalds {
989eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
990353601e7SJ. Bruce Fields 
991353601e7SJ. Bruce Fields 	spin_lock(&fp->fi_lock);
992353601e7SJ. Bruce Fields 	if (--fp->fi_delegees == 0)
993eb82dd39SJeff Layton 		swap(nf, fp->fi_deleg_file);
994353601e7SJ. Bruce Fields 	spin_unlock(&fp->fi_lock);
995353601e7SJ. Bruce Fields 
996eb82dd39SJeff Layton 	if (nf)
997eb82dd39SJeff Layton 		nfsd_file_put(nf);
998353601e7SJ. Bruce Fields }
999353601e7SJ. Bruce Fields 
1000353601e7SJ. Bruce Fields static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1001353601e7SJ. Bruce Fields {
1002cba7b3d1SJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1003eb82dd39SJeff Layton 	struct nfsd_file *nf = fp->fi_deleg_file;
1004417c6629SJeff Layton 
1005b8232d33SJ. Bruce Fields 	WARN_ON_ONCE(!fp->fi_delegees);
1006b8232d33SJ. Bruce Fields 
1007eb82dd39SJeff Layton 	vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1008353601e7SJ. Bruce Fields 	put_deleg_file(fp);
10091da177e4SLinus Torvalds }
10101da177e4SLinus Torvalds 
10110af6e690SJ. Bruce Fields static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
10120af6e690SJ. Bruce Fields {
10130af6e690SJ. Bruce Fields 	put_clnt_odstate(dp->dl_clnt_odstate);
1014353601e7SJ. Bruce Fields 	nfs4_unlock_deleg_lease(dp);
10150af6e690SJ. Bruce Fields 	nfs4_put_stid(&dp->dl_stid);
10160af6e690SJ. Bruce Fields }
10170af6e690SJ. Bruce Fields 
1018cd61c522SChristoph Hellwig void nfs4_unhash_stid(struct nfs4_stid *s)
10196136d2b4SJ. Bruce Fields {
10203abdb607SJ. Bruce Fields 	s->sc_type = 0;
10216136d2b4SJ. Bruce Fields }
10226136d2b4SJ. Bruce Fields 
102334ed9872SAndrew Elble /**
102468b18f52SJ. Bruce Fields  * nfs4_delegation_exists - Discover if this delegation already exists
102534ed9872SAndrew Elble  * @clp:     a pointer to the nfs4_client we're granting a delegation to
102634ed9872SAndrew Elble  * @fp:      a pointer to the nfs4_file we're granting a delegation on
102734ed9872SAndrew Elble  *
102834ed9872SAndrew Elble  * Return:
102968b18f52SJ. Bruce Fields  *      On success: true iff an existing delegation is found
103034ed9872SAndrew Elble  */
103134ed9872SAndrew Elble 
103268b18f52SJ. Bruce Fields static bool
103368b18f52SJ. Bruce Fields nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1034931ee56cSBenny Halevy {
103534ed9872SAndrew Elble 	struct nfs4_delegation *searchdp = NULL;
103634ed9872SAndrew Elble 	struct nfs4_client *searchclp = NULL;
103734ed9872SAndrew Elble 
1038cdc97505SBenny Halevy 	lockdep_assert_held(&state_lock);
1039417c6629SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
1040931ee56cSBenny Halevy 
104134ed9872SAndrew Elble 	list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
104234ed9872SAndrew Elble 		searchclp = searchdp->dl_stid.sc_client;
104334ed9872SAndrew Elble 		if (clp == searchclp) {
104451d87bc2SFengguang Wu 			return true;
104534ed9872SAndrew Elble 		}
104634ed9872SAndrew Elble 	}
104751d87bc2SFengguang Wu 	return false;
104834ed9872SAndrew Elble }
104934ed9872SAndrew Elble 
105034ed9872SAndrew Elble /**
105134ed9872SAndrew Elble  * hash_delegation_locked - Add a delegation to the appropriate lists
105234ed9872SAndrew Elble  * @dp:     a pointer to the nfs4_delegation we are adding.
105334ed9872SAndrew Elble  * @fp:     a pointer to the nfs4_file we're granting a delegation on
105434ed9872SAndrew Elble  *
105534ed9872SAndrew Elble  * Return:
105634ed9872SAndrew Elble  *      On success: NULL if the delegation was successfully hashed.
105734ed9872SAndrew Elble  *
105834ed9872SAndrew Elble  *      On error: -EAGAIN if one was previously granted to this
105934ed9872SAndrew Elble  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
106034ed9872SAndrew Elble  *
106134ed9872SAndrew Elble  */
106234ed9872SAndrew Elble 
106334ed9872SAndrew Elble static int
106434ed9872SAndrew Elble hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
106534ed9872SAndrew Elble {
106634ed9872SAndrew Elble 	struct nfs4_client *clp = dp->dl_stid.sc_client;
106734ed9872SAndrew Elble 
106834ed9872SAndrew Elble 	lockdep_assert_held(&state_lock);
106934ed9872SAndrew Elble 	lockdep_assert_held(&fp->fi_lock);
107034ed9872SAndrew Elble 
107168b18f52SJ. Bruce Fields 	if (nfs4_delegation_exists(clp, fp))
107268b18f52SJ. Bruce Fields 		return -EAGAIN;
1073a15dfcd5SElena Reshetova 	refcount_inc(&dp->dl_stid.sc_count);
10743fb87d13SBenny Halevy 	dp->dl_stid.sc_type = NFS4_DELEG_STID;
1075931ee56cSBenny Halevy 	list_add(&dp->dl_perfile, &fp->fi_delegations);
107634ed9872SAndrew Elble 	list_add(&dp->dl_perclnt, &clp->cl_delegations);
107734ed9872SAndrew Elble 	return 0;
1078931ee56cSBenny Halevy }
1079931ee56cSBenny Halevy 
10803fcbbd24SJeff Layton static bool
108142690676SJeff Layton unhash_delegation_locked(struct nfs4_delegation *dp)
10821da177e4SLinus Torvalds {
108311b9164aSTrond Myklebust 	struct nfs4_file *fp = dp->dl_stid.sc_file;
108402e1215fSJeff Layton 
108542690676SJeff Layton 	lockdep_assert_held(&state_lock);
108642690676SJeff Layton 
10873fcbbd24SJeff Layton 	if (list_empty(&dp->dl_perfile))
10883fcbbd24SJeff Layton 		return false;
10893fcbbd24SJeff Layton 
1090b0fc29d6STrond Myklebust 	dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1091d55a166cSJeff Layton 	/* Ensure that deleg break won't try to requeue it */
1092d55a166cSJeff Layton 	++dp->dl_time;
1093417c6629SJeff Layton 	spin_lock(&fp->fi_lock);
1094931ee56cSBenny Halevy 	list_del_init(&dp->dl_perclnt);
10951da177e4SLinus Torvalds 	list_del_init(&dp->dl_recall_lru);
109602e1215fSJeff Layton 	list_del_init(&dp->dl_perfile);
109702e1215fSJeff Layton 	spin_unlock(&fp->fi_lock);
10983fcbbd24SJeff Layton 	return true;
1099cbf7a75bSJ. Bruce Fields }
11003bd64a5bSJ. Bruce Fields 
11013bd64a5bSJ. Bruce Fields static void destroy_delegation(struct nfs4_delegation *dp)
11023bd64a5bSJ. Bruce Fields {
11033fcbbd24SJeff Layton 	bool unhashed;
11043fcbbd24SJeff Layton 
110542690676SJeff Layton 	spin_lock(&state_lock);
11063fcbbd24SJeff Layton 	unhashed = unhash_delegation_locked(dp);
110742690676SJeff Layton 	spin_unlock(&state_lock);
11080af6e690SJ. Bruce Fields 	if (unhashed)
11090af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
11103fcbbd24SJeff Layton }
11113bd64a5bSJ. Bruce Fields 
11123bd64a5bSJ. Bruce Fields static void revoke_delegation(struct nfs4_delegation *dp)
11133bd64a5bSJ. Bruce Fields {
11143bd64a5bSJ. Bruce Fields 	struct nfs4_client *clp = dp->dl_stid.sc_client;
11153bd64a5bSJ. Bruce Fields 
11162d4a532dSJeff Layton 	WARN_ON(!list_empty(&dp->dl_recall_lru));
11172d4a532dSJeff Layton 
11180af6e690SJ. Bruce Fields 	if (clp->cl_minorversion) {
11193bd64a5bSJ. Bruce Fields 		dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
11200af6e690SJ. Bruce Fields 		refcount_inc(&dp->dl_stid.sc_count);
11212d4a532dSJeff Layton 		spin_lock(&clp->cl_lock);
11222d4a532dSJeff Layton 		list_add(&dp->dl_recall_lru, &clp->cl_revoked);
11232d4a532dSJeff Layton 		spin_unlock(&clp->cl_lock);
11243bd64a5bSJ. Bruce Fields 	}
11250af6e690SJ. Bruce Fields 	destroy_unhashed_deleg(dp);
11263bd64a5bSJ. Bruce Fields }
11273bd64a5bSJ. Bruce Fields 
11281da177e4SLinus Torvalds /*
11291da177e4SLinus Torvalds  * SETCLIENTID state
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds 
1132ddc04c41SJ. Bruce Fields static unsigned int clientid_hashval(u32 id)
1133ddc04c41SJ. Bruce Fields {
1134ddc04c41SJ. Bruce Fields 	return id & CLIENT_HASH_MASK;
1135ddc04c41SJ. Bruce Fields }
1136ddc04c41SJ. Bruce Fields 
11376b189105SScott Mayhew static unsigned int clientstr_hashval(struct xdr_netobj name)
1138ddc04c41SJ. Bruce Fields {
11396b189105SScott Mayhew 	return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1140ddc04c41SJ. Bruce Fields }
1141ddc04c41SJ. Bruce Fields 
11421da177e4SLinus Torvalds /*
1143f9d7562fSJ. Bruce Fields  * We store the NONE, READ, WRITE, and BOTH bits separately in the
1144f9d7562fSJ. Bruce Fields  * st_{access,deny}_bmap field of the stateid, in order to track not
1145f9d7562fSJ. Bruce Fields  * only what share bits are currently in force, but also what
1146f9d7562fSJ. Bruce Fields  * combinations of share bits previous opens have used.  This allows us
1147f9d7562fSJ. Bruce Fields  * to enforce the recommendation of rfc 3530 14.2.19 that the server
1148f9d7562fSJ. Bruce Fields  * return an error if the client attempt to downgrade to a combination
1149f9d7562fSJ. Bruce Fields  * of share bits not explicable by closing some of its previous opens.
1150f9d7562fSJ. Bruce Fields  *
1151f9d7562fSJ. Bruce Fields  * XXX: This enforcement is actually incomplete, since we don't keep
1152f9d7562fSJ. Bruce Fields  * track of access/deny bit combinations; so, e.g., we allow:
1153f9d7562fSJ. Bruce Fields  *
1154f9d7562fSJ. Bruce Fields  *	OPEN allow read, deny write
1155f9d7562fSJ. Bruce Fields  *	OPEN allow both, deny none
1156f9d7562fSJ. Bruce Fields  *	DOWNGRADE allow read, deny none
1157f9d7562fSJ. Bruce Fields  *
1158f9d7562fSJ. Bruce Fields  * which we should reject.
1159f9d7562fSJ. Bruce Fields  */
11605ae037e5SJeff Layton static unsigned int
11615ae037e5SJeff Layton bmap_to_share_mode(unsigned long bmap) {
1162f9d7562fSJ. Bruce Fields 	int i;
11635ae037e5SJeff Layton 	unsigned int access = 0;
1164f9d7562fSJ. Bruce Fields 
1165f9d7562fSJ. Bruce Fields 	for (i = 1; i < 4; i++) {
1166f9d7562fSJ. Bruce Fields 		if (test_bit(i, &bmap))
11675ae037e5SJeff Layton 			access |= i;
1168f9d7562fSJ. Bruce Fields 	}
11695ae037e5SJeff Layton 	return access;
1170f9d7562fSJ. Bruce Fields }
1171f9d7562fSJ. Bruce Fields 
117282c5ff1bSJeff Layton /* set share access for a given stateid */
117382c5ff1bSJeff Layton static inline void
117482c5ff1bSJeff Layton set_access(u32 access, struct nfs4_ol_stateid *stp)
117582c5ff1bSJeff Layton {
1176c11c591fSJeff Layton 	unsigned char mask = 1 << access;
1177c11c591fSJeff Layton 
1178c11c591fSJeff Layton 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1179c11c591fSJeff Layton 	stp->st_access_bmap |= mask;
118082c5ff1bSJeff Layton }
118182c5ff1bSJeff Layton 
118282c5ff1bSJeff Layton /* clear share access for a given stateid */
118382c5ff1bSJeff Layton static inline void
118482c5ff1bSJeff Layton clear_access(u32 access, struct nfs4_ol_stateid *stp)
118582c5ff1bSJeff Layton {
1186c11c591fSJeff Layton 	unsigned char mask = 1 << access;
1187c11c591fSJeff Layton 
1188c11c591fSJeff Layton 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1189c11c591fSJeff Layton 	stp->st_access_bmap &= ~mask;
119082c5ff1bSJeff Layton }
119182c5ff1bSJeff Layton 
119282c5ff1bSJeff Layton /* test whether a given stateid has access */
119382c5ff1bSJeff Layton static inline bool
119482c5ff1bSJeff Layton test_access(u32 access, struct nfs4_ol_stateid *stp)
119582c5ff1bSJeff Layton {
1196c11c591fSJeff Layton 	unsigned char mask = 1 << access;
1197c11c591fSJeff Layton 
1198c11c591fSJeff Layton 	return (bool)(stp->st_access_bmap & mask);
119982c5ff1bSJeff Layton }
120082c5ff1bSJeff Layton 
1201ce0fc43cSJeff Layton /* set share deny for a given stateid */
1202ce0fc43cSJeff Layton static inline void
1203c11c591fSJeff Layton set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1204ce0fc43cSJeff Layton {
1205c11c591fSJeff Layton 	unsigned char mask = 1 << deny;
1206c11c591fSJeff Layton 
1207c11c591fSJeff Layton 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1208c11c591fSJeff Layton 	stp->st_deny_bmap |= mask;
1209ce0fc43cSJeff Layton }
1210ce0fc43cSJeff Layton 
1211ce0fc43cSJeff Layton /* clear share deny for a given stateid */
1212ce0fc43cSJeff Layton static inline void
1213c11c591fSJeff Layton clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1214ce0fc43cSJeff Layton {
1215c11c591fSJeff Layton 	unsigned char mask = 1 << deny;
1216c11c591fSJeff Layton 
1217c11c591fSJeff Layton 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1218c11c591fSJeff Layton 	stp->st_deny_bmap &= ~mask;
1219ce0fc43cSJeff Layton }
1220ce0fc43cSJeff Layton 
1221ce0fc43cSJeff Layton /* test whether a given stateid is denying specific access */
1222ce0fc43cSJeff Layton static inline bool
1223c11c591fSJeff Layton test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1224ce0fc43cSJeff Layton {
1225c11c591fSJeff Layton 	unsigned char mask = 1 << deny;
1226c11c591fSJeff Layton 
1227c11c591fSJeff Layton 	return (bool)(stp->st_deny_bmap & mask);
1228f9d7562fSJ. Bruce Fields }
1229f9d7562fSJ. Bruce Fields 
1230f9d7562fSJ. Bruce Fields static int nfs4_access_to_omode(u32 access)
1231f9d7562fSJ. Bruce Fields {
12328f34a430SJ. Bruce Fields 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
1233f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
1234f9d7562fSJ. Bruce Fields 		return O_RDONLY;
1235f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
1236f9d7562fSJ. Bruce Fields 		return O_WRONLY;
1237f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
1238f9d7562fSJ. Bruce Fields 		return O_RDWR;
1239f9d7562fSJ. Bruce Fields 	}
1240063b0fb9SJ. Bruce Fields 	WARN_ON_ONCE(1);
1241063b0fb9SJ. Bruce Fields 	return O_RDONLY;
1242f9d7562fSJ. Bruce Fields }
1243f9d7562fSJ. Bruce Fields 
1244baeb4ff0SJeff Layton /*
1245baeb4ff0SJeff Layton  * A stateid that had a deny mode associated with it is being released
1246baeb4ff0SJeff Layton  * or downgraded. Recalculate the deny mode on the file.
1247baeb4ff0SJeff Layton  */
1248baeb4ff0SJeff Layton static void
1249baeb4ff0SJeff Layton recalculate_deny_mode(struct nfs4_file *fp)
1250baeb4ff0SJeff Layton {
1251baeb4ff0SJeff Layton 	struct nfs4_ol_stateid *stp;
1252baeb4ff0SJeff Layton 
1253baeb4ff0SJeff Layton 	spin_lock(&fp->fi_lock);
1254baeb4ff0SJeff Layton 	fp->fi_share_deny = 0;
1255baeb4ff0SJeff Layton 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1256baeb4ff0SJeff Layton 		fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1257baeb4ff0SJeff Layton 	spin_unlock(&fp->fi_lock);
1258baeb4ff0SJeff Layton }
1259baeb4ff0SJeff Layton 
1260baeb4ff0SJeff Layton static void
1261baeb4ff0SJeff Layton reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1262baeb4ff0SJeff Layton {
1263baeb4ff0SJeff Layton 	int i;
1264baeb4ff0SJeff Layton 	bool change = false;
1265baeb4ff0SJeff Layton 
1266baeb4ff0SJeff Layton 	for (i = 1; i < 4; i++) {
1267baeb4ff0SJeff Layton 		if ((i & deny) != i) {
1268baeb4ff0SJeff Layton 			change = true;
1269baeb4ff0SJeff Layton 			clear_deny(i, stp);
1270baeb4ff0SJeff Layton 		}
1271baeb4ff0SJeff Layton 	}
1272baeb4ff0SJeff Layton 
1273baeb4ff0SJeff Layton 	/* Recalculate per-file deny mode if there was a change */
1274baeb4ff0SJeff Layton 	if (change)
127511b9164aSTrond Myklebust 		recalculate_deny_mode(stp->st_stid.sc_file);
1276baeb4ff0SJeff Layton }
1277baeb4ff0SJeff Layton 
127882c5ff1bSJeff Layton /* release all access and file references for a given stateid */
127982c5ff1bSJeff Layton static void
128082c5ff1bSJeff Layton release_all_access(struct nfs4_ol_stateid *stp)
128182c5ff1bSJeff Layton {
128282c5ff1bSJeff Layton 	int i;
128311b9164aSTrond Myklebust 	struct nfs4_file *fp = stp->st_stid.sc_file;
1284baeb4ff0SJeff Layton 
1285baeb4ff0SJeff Layton 	if (fp && stp->st_deny_bmap != 0)
1286baeb4ff0SJeff Layton 		recalculate_deny_mode(fp);
128782c5ff1bSJeff Layton 
128882c5ff1bSJeff Layton 	for (i = 1; i < 4; i++) {
128982c5ff1bSJeff Layton 		if (test_access(i, stp))
129011b9164aSTrond Myklebust 			nfs4_file_put_access(stp->st_stid.sc_file, i);
129182c5ff1bSJeff Layton 		clear_access(i, stp);
129282c5ff1bSJeff Layton 	}
129382c5ff1bSJeff Layton }
129482c5ff1bSJeff Layton 
1295d50ffdedSKinglong Mee static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1296d50ffdedSKinglong Mee {
1297d50ffdedSKinglong Mee 	kfree(sop->so_owner.data);
1298d50ffdedSKinglong Mee 	sop->so_ops->so_free(sop);
1299d50ffdedSKinglong Mee }
1300d50ffdedSKinglong Mee 
13016b180f0bSJeff Layton static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
13026b180f0bSJeff Layton {
1303a819ecc1SJeff Layton 	struct nfs4_client *clp = sop->so_client;
1304a819ecc1SJeff Layton 
1305a819ecc1SJeff Layton 	might_lock(&clp->cl_lock);
1306a819ecc1SJeff Layton 
1307a819ecc1SJeff Layton 	if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
13086b180f0bSJeff Layton 		return;
13098f4b54c5SJeff Layton 	sop->so_ops->so_unhash(sop);
1310a819ecc1SJeff Layton 	spin_unlock(&clp->cl_lock);
1311d50ffdedSKinglong Mee 	nfs4_free_stateowner(sop);
13126b180f0bSJeff Layton }
13136b180f0bSJeff Layton 
1314a451b123STrond Myklebust static bool
1315a451b123STrond Myklebust nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1316a451b123STrond Myklebust {
1317a451b123STrond Myklebust 	return list_empty(&stp->st_perfile);
1318a451b123STrond Myklebust }
1319a451b123STrond Myklebust 
1320e8568739SJeff Layton static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1321529d7b2aSJ. Bruce Fields {
132211b9164aSTrond Myklebust 	struct nfs4_file *fp = stp->st_stid.sc_file;
13231d31a253STrond Myklebust 
13241c755dc1SJeff Layton 	lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
13251c755dc1SJeff Layton 
1326e8568739SJeff Layton 	if (list_empty(&stp->st_perfile))
1327e8568739SJeff Layton 		return false;
1328e8568739SJeff Layton 
13291d31a253STrond Myklebust 	spin_lock(&fp->fi_lock);
1330e8568739SJeff Layton 	list_del_init(&stp->st_perfile);
13311d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
1332529d7b2aSJ. Bruce Fields 	list_del(&stp->st_perstateowner);
1333e8568739SJeff Layton 	return true;
1334529d7b2aSJ. Bruce Fields }
1335529d7b2aSJ. Bruce Fields 
13366011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1337529d7b2aSJ. Bruce Fields {
13386011695dSTrond Myklebust 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
13394665e2baSJ. Bruce Fields 
13408287f009SSachin Bhamare 	put_clnt_odstate(stp->st_clnt_odstate);
13416011695dSTrond Myklebust 	release_all_access(stp);
1342d3134b10SJeff Layton 	if (stp->st_stateowner)
1343d3134b10SJeff Layton 		nfs4_put_stateowner(stp->st_stateowner);
13446011695dSTrond Myklebust 	kmem_cache_free(stateid_slab, stid);
1345529d7b2aSJ. Bruce Fields }
1346529d7b2aSJ. Bruce Fields 
1347b49e084dSJeff Layton static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1348529d7b2aSJ. Bruce Fields {
1349b49e084dSJeff Layton 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1350b49e084dSJeff Layton 	struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1351eb82dd39SJeff Layton 	struct nfsd_file *nf;
1352529d7b2aSJ. Bruce Fields 
1353eb82dd39SJeff Layton 	nf = find_any_file(stp->st_stid.sc_file);
1354eb82dd39SJeff Layton 	if (nf) {
1355eb82dd39SJeff Layton 		get_file(nf->nf_file);
1356eb82dd39SJeff Layton 		filp_close(nf->nf_file, (fl_owner_t)lo);
1357eb82dd39SJeff Layton 		nfsd_file_put(nf);
1358eb82dd39SJeff Layton 	}
1359b49e084dSJeff Layton 	nfs4_free_ol_stateid(stid);
1360b49e084dSJeff Layton }
1361b49e084dSJeff Layton 
13622c41beb0SJeff Layton /*
13632c41beb0SJeff Layton  * Put the persistent reference to an already unhashed generic stateid, while
13642c41beb0SJeff Layton  * holding the cl_lock. If it's the last reference, then put it onto the
13652c41beb0SJeff Layton  * reaplist for later destruction.
13662c41beb0SJeff Layton  */
13672c41beb0SJeff Layton static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
13682c41beb0SJeff Layton 				       struct list_head *reaplist)
13692c41beb0SJeff Layton {
13702c41beb0SJeff Layton 	struct nfs4_stid *s = &stp->st_stid;
13712c41beb0SJeff Layton 	struct nfs4_client *clp = s->sc_client;
13722c41beb0SJeff Layton 
13732c41beb0SJeff Layton 	lockdep_assert_held(&clp->cl_lock);
13742c41beb0SJeff Layton 
13752c41beb0SJeff Layton 	WARN_ON_ONCE(!list_empty(&stp->st_locks));
13762c41beb0SJeff Layton 
1377a15dfcd5SElena Reshetova 	if (!refcount_dec_and_test(&s->sc_count)) {
13782c41beb0SJeff Layton 		wake_up_all(&close_wq);
13792c41beb0SJeff Layton 		return;
13802c41beb0SJeff Layton 	}
13812c41beb0SJeff Layton 
13822c41beb0SJeff Layton 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
13832c41beb0SJeff Layton 	list_add(&stp->st_locks, reaplist);
13842c41beb0SJeff Layton }
13852c41beb0SJeff Layton 
1386e8568739SJeff Layton static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
13873c1c995cSJeff Layton {
1388f46c445bSChuck Lever 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
13893c1c995cSJeff Layton 
1390a451b123STrond Myklebust 	if (!unhash_ol_stateid(stp))
1391a451b123STrond Myklebust 		return false;
13923c1c995cSJeff Layton 	list_del_init(&stp->st_locks);
1393cd61c522SChristoph Hellwig 	nfs4_unhash_stid(&stp->st_stid);
1394a451b123STrond Myklebust 	return true;
13953c1c995cSJeff Layton }
13963c1c995cSJeff Layton 
13975adfd885SJeff Layton static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1398b49e084dSJeff Layton {
1399f46c445bSChuck Lever 	struct nfs4_client *clp = stp->st_stid.sc_client;
1400e8568739SJeff Layton 	bool unhashed;
14011c755dc1SJeff Layton 
1402f46c445bSChuck Lever 	spin_lock(&clp->cl_lock);
1403e8568739SJeff Layton 	unhashed = unhash_lock_stateid(stp);
1404f46c445bSChuck Lever 	spin_unlock(&clp->cl_lock);
1405e8568739SJeff Layton 	if (unhashed)
14066011695dSTrond Myklebust 		nfs4_put_stid(&stp->st_stid);
1407529d7b2aSJ. Bruce Fields }
1408529d7b2aSJ. Bruce Fields 
1409c58c6610STrond Myklebust static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1410529d7b2aSJ. Bruce Fields {
1411d4f0489fSTrond Myklebust 	struct nfs4_client *clp = lo->lo_owner.so_client;
1412c58c6610STrond Myklebust 
1413d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
1414c58c6610STrond Myklebust 
14158f4b54c5SJeff Layton 	list_del_init(&lo->lo_owner.so_strhash);
14168f4b54c5SJeff Layton }
14178f4b54c5SJeff Layton 
14182c41beb0SJeff Layton /*
14192c41beb0SJeff Layton  * Free a list of generic stateids that were collected earlier after being
14202c41beb0SJeff Layton  * fully unhashed.
14212c41beb0SJeff Layton  */
14222c41beb0SJeff Layton static void
14232c41beb0SJeff Layton free_ol_stateid_reaplist(struct list_head *reaplist)
14242c41beb0SJeff Layton {
14252c41beb0SJeff Layton 	struct nfs4_ol_stateid *stp;
1426fb94d766SKinglong Mee 	struct nfs4_file *fp;
14272c41beb0SJeff Layton 
14282c41beb0SJeff Layton 	might_sleep();
14292c41beb0SJeff Layton 
14302c41beb0SJeff Layton 	while (!list_empty(reaplist)) {
14312c41beb0SJeff Layton 		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
14322c41beb0SJeff Layton 				       st_locks);
14332c41beb0SJeff Layton 		list_del(&stp->st_locks);
1434fb94d766SKinglong Mee 		fp = stp->st_stid.sc_file;
14352c41beb0SJeff Layton 		stp->st_stid.sc_free(&stp->st_stid);
1436fb94d766SKinglong Mee 		if (fp)
1437fb94d766SKinglong Mee 			put_nfs4_file(fp);
14382c41beb0SJeff Layton 	}
14392c41beb0SJeff Layton }
14402c41beb0SJeff Layton 
1441d83017f9SJeff Layton static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1442d83017f9SJeff Layton 				       struct list_head *reaplist)
14433c87b9b7STrond Myklebust {
14443c87b9b7STrond Myklebust 	struct nfs4_ol_stateid *stp;
14453c87b9b7STrond Myklebust 
1446e8568739SJeff Layton 	lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1447e8568739SJeff Layton 
14483c87b9b7STrond Myklebust 	while (!list_empty(&open_stp->st_locks)) {
14493c87b9b7STrond Myklebust 		stp = list_entry(open_stp->st_locks.next,
14503c87b9b7STrond Myklebust 				struct nfs4_ol_stateid, st_locks);
1451e8568739SJeff Layton 		WARN_ON(!unhash_lock_stateid(stp));
1452d83017f9SJeff Layton 		put_ol_stateid_locked(stp, reaplist);
1453529d7b2aSJ. Bruce Fields 	}
1454529d7b2aSJ. Bruce Fields }
1455529d7b2aSJ. Bruce Fields 
1456e8568739SJeff Layton static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1457d83017f9SJeff Layton 				struct list_head *reaplist)
14582283963fSJ. Bruce Fields {
14592c41beb0SJeff Layton 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
14602c41beb0SJeff Layton 
1461a451b123STrond Myklebust 	if (!unhash_ol_stateid(stp))
1462a451b123STrond Myklebust 		return false;
1463d83017f9SJeff Layton 	release_open_stateid_locks(stp, reaplist);
1464a451b123STrond Myklebust 	return true;
146538c387b5SJ. Bruce Fields }
146638c387b5SJ. Bruce Fields 
146738c387b5SJ. Bruce Fields static void release_open_stateid(struct nfs4_ol_stateid *stp)
146838c387b5SJ. Bruce Fields {
14692c41beb0SJeff Layton 	LIST_HEAD(reaplist);
14702c41beb0SJeff Layton 
14712c41beb0SJeff Layton 	spin_lock(&stp->st_stid.sc_client->cl_lock);
1472e8568739SJeff Layton 	if (unhash_open_stateid(stp, &reaplist))
14732c41beb0SJeff Layton 		put_ol_stateid_locked(stp, &reaplist);
14742c41beb0SJeff Layton 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
14752c41beb0SJeff Layton 	free_ol_stateid_reaplist(&reaplist);
14762283963fSJ. Bruce Fields }
14772283963fSJ. Bruce Fields 
14787ffb5880STrond Myklebust static void unhash_openowner_locked(struct nfs4_openowner *oo)
1479f1d110caSJ. Bruce Fields {
1480d4f0489fSTrond Myklebust 	struct nfs4_client *clp = oo->oo_owner.so_client;
14817ffb5880STrond Myklebust 
1482d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
14837ffb5880STrond Myklebust 
14848f4b54c5SJeff Layton 	list_del_init(&oo->oo_owner.so_strhash);
14858f4b54c5SJeff Layton 	list_del_init(&oo->oo_perclient);
1486f1d110caSJ. Bruce Fields }
1487f1d110caSJ. Bruce Fields 
1488f7a4d872SJ. Bruce Fields static void release_last_closed_stateid(struct nfs4_openowner *oo)
1489f7a4d872SJ. Bruce Fields {
1490217526e7SJeff Layton 	struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1491217526e7SJeff Layton 					  nfsd_net_id);
1492217526e7SJeff Layton 	struct nfs4_ol_stateid *s;
1493f7a4d872SJ. Bruce Fields 
1494217526e7SJeff Layton 	spin_lock(&nn->client_lock);
1495217526e7SJeff Layton 	s = oo->oo_last_closed_stid;
1496f7a4d872SJ. Bruce Fields 	if (s) {
1497d3134b10SJeff Layton 		list_del_init(&oo->oo_close_lru);
1498f7a4d872SJ. Bruce Fields 		oo->oo_last_closed_stid = NULL;
1499f7a4d872SJ. Bruce Fields 	}
1500217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
1501217526e7SJeff Layton 	if (s)
1502217526e7SJeff Layton 		nfs4_put_stid(&s->st_stid);
1503f7a4d872SJ. Bruce Fields }
1504f7a4d872SJ. Bruce Fields 
15052c41beb0SJeff Layton static void release_openowner(struct nfs4_openowner *oo)
15068f4b54c5SJeff Layton {
15078f4b54c5SJeff Layton 	struct nfs4_ol_stateid *stp;
1508d4f0489fSTrond Myklebust 	struct nfs4_client *clp = oo->oo_owner.so_client;
15092c41beb0SJeff Layton 	struct list_head reaplist;
15107ffb5880STrond Myklebust 
15112c41beb0SJeff Layton 	INIT_LIST_HEAD(&reaplist);
15127ffb5880STrond Myklebust 
1513d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
15147ffb5880STrond Myklebust 	unhash_openowner_locked(oo);
15152c41beb0SJeff Layton 	while (!list_empty(&oo->oo_owner.so_stateids)) {
15162c41beb0SJeff Layton 		stp = list_first_entry(&oo->oo_owner.so_stateids,
15172c41beb0SJeff Layton 				struct nfs4_ol_stateid, st_perstateowner);
1518e8568739SJeff Layton 		if (unhash_open_stateid(stp, &reaplist))
15192c41beb0SJeff Layton 			put_ol_stateid_locked(stp, &reaplist);
15202c41beb0SJeff Layton 	}
1521d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
15222c41beb0SJeff Layton 	free_ol_stateid_reaplist(&reaplist);
1523f7a4d872SJ. Bruce Fields 	release_last_closed_stateid(oo);
15246b180f0bSJeff Layton 	nfs4_put_stateowner(&oo->oo_owner);
1525f1d110caSJ. Bruce Fields }
1526f1d110caSJ. Bruce Fields 
15275282fd72SMarc Eshel static inline int
15285282fd72SMarc Eshel hash_sessionid(struct nfs4_sessionid *sessionid)
15295282fd72SMarc Eshel {
15305282fd72SMarc Eshel 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
15315282fd72SMarc Eshel 
15325282fd72SMarc Eshel 	return sid->sequence % SESSION_HASH_SIZE;
15335282fd72SMarc Eshel }
15345282fd72SMarc Eshel 
1535135dd002SMark Salter #ifdef CONFIG_SUNRPC_DEBUG
15365282fd72SMarc Eshel static inline void
15375282fd72SMarc Eshel dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
15385282fd72SMarc Eshel {
15395282fd72SMarc Eshel 	u32 *ptr = (u32 *)(&sessionid->data[0]);
15405282fd72SMarc Eshel 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
15415282fd72SMarc Eshel }
15428f199b82STrond Myklebust #else
15438f199b82STrond Myklebust static inline void
15448f199b82STrond Myklebust dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
15458f199b82STrond Myklebust {
15468f199b82STrond Myklebust }
15478f199b82STrond Myklebust #endif
15488f199b82STrond Myklebust 
15499411b1d4SJ. Bruce Fields /*
15509411b1d4SJ. Bruce Fields  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
15519411b1d4SJ. Bruce Fields  * won't be used for replay.
15529411b1d4SJ. Bruce Fields  */
15539411b1d4SJ. Bruce Fields void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
15549411b1d4SJ. Bruce Fields {
15559411b1d4SJ. Bruce Fields 	struct nfs4_stateowner *so = cstate->replay_owner;
15569411b1d4SJ. Bruce Fields 
15579411b1d4SJ. Bruce Fields 	if (nfserr == nfserr_replay_me)
15589411b1d4SJ. Bruce Fields 		return;
15599411b1d4SJ. Bruce Fields 
15609411b1d4SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(nfserr))) {
156158fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
15629411b1d4SJ. Bruce Fields 		return;
15639411b1d4SJ. Bruce Fields 	}
15649411b1d4SJ. Bruce Fields 	if (!so)
15659411b1d4SJ. Bruce Fields 		return;
15669411b1d4SJ. Bruce Fields 	if (so->so_is_open_owner)
15679411b1d4SJ. Bruce Fields 		release_last_closed_stateid(openowner(so));
15689411b1d4SJ. Bruce Fields 	so->so_seqid++;
15699411b1d4SJ. Bruce Fields 	return;
15709411b1d4SJ. Bruce Fields }
15715282fd72SMarc Eshel 
1572ec6b5d7bSAndy Adamson static void
1573ec6b5d7bSAndy Adamson gen_sessionid(struct nfsd4_session *ses)
1574ec6b5d7bSAndy Adamson {
1575ec6b5d7bSAndy Adamson 	struct nfs4_client *clp = ses->se_client;
1576ec6b5d7bSAndy Adamson 	struct nfsd4_sessionid *sid;
1577ec6b5d7bSAndy Adamson 
1578ec6b5d7bSAndy Adamson 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1579ec6b5d7bSAndy Adamson 	sid->clientid = clp->cl_clientid;
1580ec6b5d7bSAndy Adamson 	sid->sequence = current_sessionid++;
1581ec6b5d7bSAndy Adamson 	sid->reserved = 0;
1582ec6b5d7bSAndy Adamson }
1583ec6b5d7bSAndy Adamson 
1584ec6b5d7bSAndy Adamson /*
1585a649637cSAndy Adamson  * The protocol defines ca_maxresponssize_cached to include the size of
1586a649637cSAndy Adamson  * the rpc header, but all we need to cache is the data starting after
1587a649637cSAndy Adamson  * the end of the initial SEQUENCE operation--the rest we regenerate
1588a649637cSAndy Adamson  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1589a649637cSAndy Adamson  * value that is the number of bytes in our cache plus a few additional
1590a649637cSAndy Adamson  * bytes.  In order to stay on the safe side, and not promise more than
1591a649637cSAndy Adamson  * we can cache, those additional bytes must be the minimum possible: 24
1592a649637cSAndy Adamson  * bytes of rpc header (xid through accept state, with AUTH_NULL
1593a649637cSAndy Adamson  * verifier), 12 for the compound header (with zero-length tag), and 44
1594a649637cSAndy Adamson  * for the SEQUENCE op response:
1595ec6b5d7bSAndy Adamson  */
1596a649637cSAndy Adamson #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1597a649637cSAndy Adamson 
1598557ce264SAndy Adamson static void
1599557ce264SAndy Adamson free_session_slots(struct nfsd4_session *ses)
1600557ce264SAndy Adamson {
1601557ce264SAndy Adamson 	int i;
1602557ce264SAndy Adamson 
160353da6a53SJ. Bruce Fields 	for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
160453da6a53SJ. Bruce Fields 		free_svc_cred(&ses->se_slots[i]->sl_cred);
1605557ce264SAndy Adamson 		kfree(ses->se_slots[i]);
1606557ce264SAndy Adamson 	}
160753da6a53SJ. Bruce Fields }
1608557ce264SAndy Adamson 
1609efe0cb6dSJ. Bruce Fields /*
1610efe0cb6dSJ. Bruce Fields  * We don't actually need to cache the rpc and session headers, so we
1611efe0cb6dSJ. Bruce Fields  * can allocate a little less for each slot:
1612efe0cb6dSJ. Bruce Fields  */
161355c760cfSJ. Bruce Fields static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1614efe0cb6dSJ. Bruce Fields {
161555c760cfSJ. Bruce Fields 	u32 size;
1616efe0cb6dSJ. Bruce Fields 
161755c760cfSJ. Bruce Fields 	if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
161855c760cfSJ. Bruce Fields 		size = 0;
161955c760cfSJ. Bruce Fields 	else
162055c760cfSJ. Bruce Fields 		size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
162155c760cfSJ. Bruce Fields 	return size + sizeof(struct nfsd4_slot);
1622557ce264SAndy Adamson }
1623557ce264SAndy Adamson 
16245b6feee9SJ. Bruce Fields /*
16255b6feee9SJ. Bruce Fields  * XXX: If we run out of reserved DRC memory we could (up to a point)
16265b6feee9SJ. Bruce Fields  * re-negotiate active sessions and reduce their slot usage to make
162742b2aa86SJustin P. Mattock  * room for new connections. For now we just fail the create session.
16285b6feee9SJ. Bruce Fields  */
16292030ca56SNeilBrown static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
16305b6feee9SJ. Bruce Fields {
163155c760cfSJ. Bruce Fields 	u32 slotsize = slot_bytes(ca);
163255c760cfSJ. Bruce Fields 	u32 num = ca->maxreqs;
1633c54f24e3SJ. Bruce Fields 	unsigned long avail, total_avail;
16342030ca56SNeilBrown 	unsigned int scale_factor;
16355b6feee9SJ. Bruce Fields 
16365b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
16377f49fd5dSNeilBrown 	if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1638c54f24e3SJ. Bruce Fields 		total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
16397f49fd5dSNeilBrown 	else
16407f49fd5dSNeilBrown 		/* We have handed out more space than we chose in
16417f49fd5dSNeilBrown 		 * set_max_drc() to allow.  That isn't really a
16427f49fd5dSNeilBrown 		 * problem as long as that doesn't make us think we
16437f49fd5dSNeilBrown 		 * have lots more due to integer overflow.
16447f49fd5dSNeilBrown 		 */
16457f49fd5dSNeilBrown 		total_avail = 0;
1646c54f24e3SJ. Bruce Fields 	avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1647de766e57SJ. Bruce Fields 	/*
16482030ca56SNeilBrown 	 * Never use more than a fraction of the remaining memory,
16497f49fd5dSNeilBrown 	 * unless it's the only way to give this client a slot.
16502030ca56SNeilBrown 	 * The chosen fraction is either 1/8 or 1/number of threads,
16512030ca56SNeilBrown 	 * whichever is smaller.  This ensures there are adequate
16522030ca56SNeilBrown 	 * slots to support multiple clients per thread.
16537f49fd5dSNeilBrown 	 * Give the client one slot even if that would require
16547f49fd5dSNeilBrown 	 * over-allocation--it is better than failure.
1655de766e57SJ. Bruce Fields 	 */
16562030ca56SNeilBrown 	scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
16572030ca56SNeilBrown 
16582030ca56SNeilBrown 	avail = clamp_t(unsigned long, avail, slotsize,
16592030ca56SNeilBrown 			total_avail/scale_factor);
16605b6feee9SJ. Bruce Fields 	num = min_t(int, num, avail / slotsize);
16617f49fd5dSNeilBrown 	num = max_t(int, num, 1);
16625b6feee9SJ. Bruce Fields 	nfsd_drc_mem_used += num * slotsize;
16635b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
16645b6feee9SJ. Bruce Fields 
16655b6feee9SJ. Bruce Fields 	return num;
16665b6feee9SJ. Bruce Fields }
16675b6feee9SJ. Bruce Fields 
166855c760cfSJ. Bruce Fields static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
16695b6feee9SJ. Bruce Fields {
167055c760cfSJ. Bruce Fields 	int slotsize = slot_bytes(ca);
167155c760cfSJ. Bruce Fields 
16725b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
167355c760cfSJ. Bruce Fields 	nfsd_drc_mem_used -= slotsize * ca->maxreqs;
16745b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
16755b6feee9SJ. Bruce Fields }
16765b6feee9SJ. Bruce Fields 
167760810e54SKinglong Mee static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
167860810e54SKinglong Mee 					   struct nfsd4_channel_attrs *battrs)
16795b6feee9SJ. Bruce Fields {
168060810e54SKinglong Mee 	int numslots = fattrs->maxreqs;
168160810e54SKinglong Mee 	int slotsize = slot_bytes(fattrs);
16825b6feee9SJ. Bruce Fields 	struct nfsd4_session *new;
16835b6feee9SJ. Bruce Fields 	int mem, i;
1684ec6b5d7bSAndy Adamson 
1685c23753daSJ. Bruce Fields 	BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1686ec6b5d7bSAndy Adamson 			+ sizeof(struct nfsd4_session) > PAGE_SIZE);
16875b6feee9SJ. Bruce Fields 	mem = numslots * sizeof(struct nfsd4_slot *);
1688ec6b5d7bSAndy Adamson 
16895b6feee9SJ. Bruce Fields 	new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
16906c18ba9fSAlexandros Batsakis 	if (!new)
16915b6feee9SJ. Bruce Fields 		return NULL;
1692ec6b5d7bSAndy Adamson 	/* allocate each struct nfsd4_slot and data cache in one piece */
16935b6feee9SJ. Bruce Fields 	for (i = 0; i < numslots; i++) {
169455c760cfSJ. Bruce Fields 		new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
16955b6feee9SJ. Bruce Fields 		if (!new->se_slots[i])
1696ec6b5d7bSAndy Adamson 			goto out_free;
1697ec6b5d7bSAndy Adamson 	}
169860810e54SKinglong Mee 
169960810e54SKinglong Mee 	memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
170060810e54SKinglong Mee 	memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
170160810e54SKinglong Mee 
17025b6feee9SJ. Bruce Fields 	return new;
17035b6feee9SJ. Bruce Fields out_free:
17045b6feee9SJ. Bruce Fields 	while (i--)
17055b6feee9SJ. Bruce Fields 		kfree(new->se_slots[i]);
17065b6feee9SJ. Bruce Fields 	kfree(new);
17075b6feee9SJ. Bruce Fields 	return NULL;
17085b6feee9SJ. Bruce Fields }
17095b6feee9SJ. Bruce Fields 
171019cf5c02SJ. Bruce Fields static void free_conn(struct nfsd4_conn *c)
171119cf5c02SJ. Bruce Fields {
171219cf5c02SJ. Bruce Fields 	svc_xprt_put(c->cn_xprt);
171319cf5c02SJ. Bruce Fields 	kfree(c);
171419cf5c02SJ. Bruce Fields }
171519cf5c02SJ. Bruce Fields 
171619cf5c02SJ. Bruce Fields static void nfsd4_conn_lost(struct svc_xpt_user *u)
171719cf5c02SJ. Bruce Fields {
171819cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
171919cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = c->cn_session->se_client;
172019cf5c02SJ. Bruce Fields 
172119cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
172219cf5c02SJ. Bruce Fields 	if (!list_empty(&c->cn_persession)) {
172319cf5c02SJ. Bruce Fields 		list_del(&c->cn_persession);
172419cf5c02SJ. Bruce Fields 		free_conn(c);
172519cf5c02SJ. Bruce Fields 	}
1726eea49806SJ. Bruce Fields 	nfsd4_probe_callback(clp);
17272e4b7239SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
172819cf5c02SJ. Bruce Fields }
172919cf5c02SJ. Bruce Fields 
1730d29c374cSJ. Bruce Fields static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1731c7662518SJ. Bruce Fields {
1732c7662518SJ. Bruce Fields 	struct nfsd4_conn *conn;
1733c7662518SJ. Bruce Fields 
1734c7662518SJ. Bruce Fields 	conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1735c7662518SJ. Bruce Fields 	if (!conn)
1736db90681dSJ. Bruce Fields 		return NULL;
1737c7662518SJ. Bruce Fields 	svc_xprt_get(rqstp->rq_xprt);
1738c7662518SJ. Bruce Fields 	conn->cn_xprt = rqstp->rq_xprt;
1739d29c374cSJ. Bruce Fields 	conn->cn_flags = flags;
1740db90681dSJ. Bruce Fields 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1741db90681dSJ. Bruce Fields 	return conn;
1742db90681dSJ. Bruce Fields }
1743db90681dSJ. Bruce Fields 
1744328ead28SJ. Bruce Fields static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1745328ead28SJ. Bruce Fields {
1746328ead28SJ. Bruce Fields 	conn->cn_session = ses;
1747328ead28SJ. Bruce Fields 	list_add(&conn->cn_persession, &ses->se_conns);
1748328ead28SJ. Bruce Fields }
1749328ead28SJ. Bruce Fields 
1750db90681dSJ. Bruce Fields static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1751db90681dSJ. Bruce Fields {
1752db90681dSJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
1753c7662518SJ. Bruce Fields 
1754c7662518SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1755328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(conn, ses);
1756c7662518SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
1757db90681dSJ. Bruce Fields }
1758c7662518SJ. Bruce Fields 
175921b75b01SJ. Bruce Fields static int nfsd4_register_conn(struct nfsd4_conn *conn)
1760db90681dSJ. Bruce Fields {
176119cf5c02SJ. Bruce Fields 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
176221b75b01SJ. Bruce Fields 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1763db90681dSJ. Bruce Fields }
1764db90681dSJ. Bruce Fields 
1765e1ff371fSJ. Bruce Fields static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1766db90681dSJ. Bruce Fields {
176721b75b01SJ. Bruce Fields 	int ret;
1768db90681dSJ. Bruce Fields 
1769db90681dSJ. Bruce Fields 	nfsd4_hash_conn(conn, ses);
177021b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(conn);
177121b75b01SJ. Bruce Fields 	if (ret)
177221b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
177321b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&conn->cn_xpt_user);
177457a37144SJ. Bruce Fields 	/* We may have gained or lost a callback channel: */
177557a37144SJ. Bruce Fields 	nfsd4_probe_callback_sync(ses->se_client);
1776c7662518SJ. Bruce Fields }
1777c7662518SJ. Bruce Fields 
1778e1ff371fSJ. Bruce Fields static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
17791d1bc8f2SJ. Bruce Fields {
17801d1bc8f2SJ. Bruce Fields 	u32 dir = NFS4_CDFC4_FORE;
17811d1bc8f2SJ. Bruce Fields 
1782e1ff371fSJ. Bruce Fields 	if (cses->flags & SESSION4_BACK_CHAN)
17831d1bc8f2SJ. Bruce Fields 		dir |= NFS4_CDFC4_BACK;
1784e1ff371fSJ. Bruce Fields 	return alloc_conn(rqstp, dir);
17851d1bc8f2SJ. Bruce Fields }
17861d1bc8f2SJ. Bruce Fields 
17871d1bc8f2SJ. Bruce Fields /* must be called under client_lock */
178819cf5c02SJ. Bruce Fields static void nfsd4_del_conns(struct nfsd4_session *s)
1789c7662518SJ. Bruce Fields {
179019cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = s->se_client;
179119cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c;
179219cf5c02SJ. Bruce Fields 
179319cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
179419cf5c02SJ. Bruce Fields 	while (!list_empty(&s->se_conns)) {
179519cf5c02SJ. Bruce Fields 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
179619cf5c02SJ. Bruce Fields 		list_del_init(&c->cn_persession);
179719cf5c02SJ. Bruce Fields 		spin_unlock(&clp->cl_lock);
179819cf5c02SJ. Bruce Fields 
179919cf5c02SJ. Bruce Fields 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
180019cf5c02SJ. Bruce Fields 		free_conn(c);
180119cf5c02SJ. Bruce Fields 
180219cf5c02SJ. Bruce Fields 		spin_lock(&clp->cl_lock);
180319cf5c02SJ. Bruce Fields 	}
180419cf5c02SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
1805c7662518SJ. Bruce Fields }
1806c7662518SJ. Bruce Fields 
18071377b69eSJ. Bruce Fields static void __free_session(struct nfsd4_session *ses)
18081377b69eSJ. Bruce Fields {
18091377b69eSJ. Bruce Fields 	free_session_slots(ses);
18101377b69eSJ. Bruce Fields 	kfree(ses);
18111377b69eSJ. Bruce Fields }
18121377b69eSJ. Bruce Fields 
181366b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *ses)
1814508dc6e1SBenny Halevy {
1815c7662518SJ. Bruce Fields 	nfsd4_del_conns(ses);
181655c760cfSJ. Bruce Fields 	nfsd4_put_drc_mem(&ses->se_fchannel);
1817c7662518SJ. Bruce Fields 	__free_session(ses);
1818a827bcb2SJ. Bruce Fields }
1819ec6b5d7bSAndy Adamson 
1820135ae827SFengguang Wu static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1821a827bcb2SJ. Bruce Fields {
1822a827bcb2SJ. Bruce Fields 	int idx;
18231872de0eSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1824a827bcb2SJ. Bruce Fields 
1825ec6b5d7bSAndy Adamson 	new->se_client = clp;
1826ec6b5d7bSAndy Adamson 	gen_sessionid(new);
1827ec6b5d7bSAndy Adamson 
1828c7662518SJ. Bruce Fields 	INIT_LIST_HEAD(&new->se_conns);
1829c7662518SJ. Bruce Fields 
1830ac7c46f2SJ. Bruce Fields 	new->se_cb_seq_nr = 1;
1831ec6b5d7bSAndy Adamson 	new->se_flags = cses->flags;
18328b5ce5cdSJ. Bruce Fields 	new->se_cb_prog = cses->callback_prog;
1833c6bb3ca2SJ. Bruce Fields 	new->se_cb_sec = cses->cb_sec;
183466b2b9b2SJ. Bruce Fields 	atomic_set(&new->se_ref, 0);
18355b6feee9SJ. Bruce Fields 	idx = hash_sessionid(&new->se_sessionid);
18361872de0eSStanislav Kinsbursky 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
18374c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1838ec6b5d7bSAndy Adamson 	list_add(&new->se_perclnt, &clp->cl_sessions);
18394c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
184060810e54SKinglong Mee 
1841b0d2e42cSChuck Lever 	{
1842edd76786SJ. Bruce Fields 		struct sockaddr *sa = svc_addr(rqstp);
1843dcbeaa68SJ. Bruce Fields 		/*
1844dcbeaa68SJ. Bruce Fields 		 * This is a little silly; with sessions there's no real
1845dcbeaa68SJ. Bruce Fields 		 * use for the callback address.  Use the peer address
1846dcbeaa68SJ. Bruce Fields 		 * as a reasonable default for now, but consider fixing
1847dcbeaa68SJ. Bruce Fields 		 * the rpc client not to require an address in the
1848dcbeaa68SJ. Bruce Fields 		 * future:
1849dcbeaa68SJ. Bruce Fields 		 */
1850edd76786SJ. Bruce Fields 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1851edd76786SJ. Bruce Fields 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1852edd76786SJ. Bruce Fields 	}
1853ec6b5d7bSAndy Adamson }
1854ec6b5d7bSAndy Adamson 
18559089f1b4SBenny Halevy /* caller must hold client_lock */
18565282fd72SMarc Eshel static struct nfsd4_session *
1857d4e19e70STrond Myklebust __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
18585282fd72SMarc Eshel {
18595282fd72SMarc Eshel 	struct nfsd4_session *elem;
18605282fd72SMarc Eshel 	int idx;
18611872de0eSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
18625282fd72SMarc Eshel 
18630a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
18640a880a28STrond Myklebust 
18655282fd72SMarc Eshel 	dump_sessionid(__func__, sessionid);
18665282fd72SMarc Eshel 	idx = hash_sessionid(sessionid);
18675282fd72SMarc Eshel 	/* Search in the appropriate list */
18681872de0eSStanislav Kinsbursky 	list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
18695282fd72SMarc Eshel 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
18705282fd72SMarc Eshel 			    NFS4_MAX_SESSIONID_LEN)) {
18715282fd72SMarc Eshel 			return elem;
18725282fd72SMarc Eshel 		}
18735282fd72SMarc Eshel 	}
18745282fd72SMarc Eshel 
18755282fd72SMarc Eshel 	dprintk("%s: session not found\n", __func__);
18765282fd72SMarc Eshel 	return NULL;
18775282fd72SMarc Eshel }
18785282fd72SMarc Eshel 
1879d4e19e70STrond Myklebust static struct nfsd4_session *
1880d4e19e70STrond Myklebust find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1881d4e19e70STrond Myklebust 		__be32 *ret)
1882d4e19e70STrond Myklebust {
1883d4e19e70STrond Myklebust 	struct nfsd4_session *session;
1884d4e19e70STrond Myklebust 	__be32 status = nfserr_badsession;
1885d4e19e70STrond Myklebust 
1886d4e19e70STrond Myklebust 	session = __find_in_sessionid_hashtbl(sessionid, net);
1887d4e19e70STrond Myklebust 	if (!session)
1888d4e19e70STrond Myklebust 		goto out;
1889d4e19e70STrond Myklebust 	status = nfsd4_get_session_locked(session);
1890d4e19e70STrond Myklebust 	if (status)
1891d4e19e70STrond Myklebust 		session = NULL;
1892d4e19e70STrond Myklebust out:
1893d4e19e70STrond Myklebust 	*ret = status;
1894d4e19e70STrond Myklebust 	return session;
1895d4e19e70STrond Myklebust }
1896d4e19e70STrond Myklebust 
18979089f1b4SBenny Halevy /* caller must hold client_lock */
18987116ed6bSAndy Adamson static void
18995282fd72SMarc Eshel unhash_session(struct nfsd4_session *ses)
19007116ed6bSAndy Adamson {
19010a880a28STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
19020a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
19030a880a28STrond Myklebust 
19040a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
19050a880a28STrond Myklebust 
19067116ed6bSAndy Adamson 	list_del(&ses->se_hash);
19074c649378SJ. Bruce Fields 	spin_lock(&ses->se_client->cl_lock);
19087116ed6bSAndy Adamson 	list_del(&ses->se_perclnt);
19094c649378SJ. Bruce Fields 	spin_unlock(&ses->se_client->cl_lock);
19105282fd72SMarc Eshel }
19115282fd72SMarc Eshel 
19121da177e4SLinus Torvalds /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
19131da177e4SLinus Torvalds static int
19142c142baaSStanislav Kinsbursky STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
19151da177e4SLinus Torvalds {
1916bbc7f33aSJ. Bruce Fields 	/*
1917bbc7f33aSJ. Bruce Fields 	 * We're assuming the clid was not given out from a boot
1918bbc7f33aSJ. Bruce Fields 	 * precisely 2^32 (about 136 years) before this one.  That seems
1919bbc7f33aSJ. Bruce Fields 	 * a safe assumption:
1920bbc7f33aSJ. Bruce Fields 	 */
1921bbc7f33aSJ. Bruce Fields 	if (clid->cl_boot == (u32)nn->boot_time)
19221da177e4SLinus Torvalds 		return 0;
1923dd5e3fbcSChuck Lever 	trace_nfsd_clid_stale(clid);
19241da177e4SLinus Torvalds 	return 1;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds /*
19281da177e4SLinus Torvalds  * XXX Should we use a slab cache ?
19291da177e4SLinus Torvalds  * This type of memory management is somewhat inefficient, but we use it
19301da177e4SLinus Torvalds  * anyway since SETCLIENTID is not a common operation.
19311da177e4SLinus Torvalds  */
193235bba9a3SJ. Bruce Fields static struct nfs4_client *alloc_client(struct xdr_netobj name)
19331da177e4SLinus Torvalds {
19341da177e4SLinus Torvalds 	struct nfs4_client *clp;
1935d4f0489fSTrond Myklebust 	int i;
19361da177e4SLinus Torvalds 
19379258a2d5SJeff Layton 	clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
193835bba9a3SJ. Bruce Fields 	if (clp == NULL)
193935bba9a3SJ. Bruce Fields 		return NULL;
19406f4859b8SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1941d4f0489fSTrond Myklebust 	if (clp->cl_name.data == NULL)
1942d4f0489fSTrond Myklebust 		goto err_no_name;
19436da2ec56SKees Cook 	clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
19446da2ec56SKees Cook 						 sizeof(struct list_head),
19456da2ec56SKees Cook 						 GFP_KERNEL);
1946d4f0489fSTrond Myklebust 	if (!clp->cl_ownerstr_hashtbl)
1947d4f0489fSTrond Myklebust 		goto err_no_hashtbl;
1948d4f0489fSTrond Myklebust 	for (i = 0; i < OWNER_HASH_SIZE; i++)
1949d4f0489fSTrond Myklebust 		INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
19505694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_sessions);
19515694c93eSTrond Myklebust 	idr_init(&clp->cl_stateids);
195214ed14ccSJ. Bruce Fields 	atomic_set(&clp->cl_rpc_users, 0);
19535694c93eSTrond Myklebust 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
19545694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_idhash);
19555694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_openowners);
19565694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_delegations);
19575694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_lru);
19585694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_revoked);
19599cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
19609cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&clp->cl_lo_states);
19619cf514ccSChristoph Hellwig #endif
1962e0639dc5SOlga Kornievskaia 	INIT_LIST_HEAD(&clp->async_copies);
1963e0639dc5SOlga Kornievskaia 	spin_lock_init(&clp->async_lock);
19645694c93eSTrond Myklebust 	spin_lock_init(&clp->cl_lock);
19655694c93eSTrond Myklebust 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
19661da177e4SLinus Torvalds 	return clp;
1967d4f0489fSTrond Myklebust err_no_hashtbl:
1968d4f0489fSTrond Myklebust 	kfree(clp->cl_name.data);
1969d4f0489fSTrond Myklebust err_no_name:
19709258a2d5SJeff Layton 	kmem_cache_free(client_slab, clp);
1971d4f0489fSTrond Myklebust 	return NULL;
19721da177e4SLinus Torvalds }
19731da177e4SLinus Torvalds 
197459f8e91bSJ. Bruce Fields static void __free_client(struct kref *k)
197559f8e91bSJ. Bruce Fields {
1976e8a79fb1SJ. Bruce Fields 	struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1977e8a79fb1SJ. Bruce Fields 	struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
197859f8e91bSJ. Bruce Fields 
197959f8e91bSJ. Bruce Fields 	free_svc_cred(&clp->cl_cred);
198059f8e91bSJ. Bruce Fields 	kfree(clp->cl_ownerstr_hashtbl);
198159f8e91bSJ. Bruce Fields 	kfree(clp->cl_name.data);
198279123444SJ. Bruce Fields 	kfree(clp->cl_nii_domain.data);
198379123444SJ. Bruce Fields 	kfree(clp->cl_nii_name.data);
198459f8e91bSJ. Bruce Fields 	idr_destroy(&clp->cl_stateids);
198559f8e91bSJ. Bruce Fields 	kmem_cache_free(client_slab, clp);
198659f8e91bSJ. Bruce Fields }
198759f8e91bSJ. Bruce Fields 
1988297e57a2SYueHaibing static void drop_client(struct nfs4_client *clp)
198959f8e91bSJ. Bruce Fields {
1990e8a79fb1SJ. Bruce Fields 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
199159f8e91bSJ. Bruce Fields }
199259f8e91bSJ. Bruce Fields 
19934dd86e15STrond Myklebust static void
19941da177e4SLinus Torvalds free_client(struct nfs4_client *clp)
19951da177e4SLinus Torvalds {
1996792c95ddSJ. Bruce Fields 	while (!list_empty(&clp->cl_sessions)) {
1997792c95ddSJ. Bruce Fields 		struct nfsd4_session *ses;
1998792c95ddSJ. Bruce Fields 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1999792c95ddSJ. Bruce Fields 				se_perclnt);
2000792c95ddSJ. Bruce Fields 		list_del(&ses->se_perclnt);
200166b2b9b2SJ. Bruce Fields 		WARN_ON_ONCE(atomic_read(&ses->se_ref));
200266b2b9b2SJ. Bruce Fields 		free_session(ses);
2003792c95ddSJ. Bruce Fields 	}
20044cb57e30STrond Myklebust 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
200589c905beSJ. Bruce Fields 	if (clp->cl_nfsd_dentry) {
2006e8a79fb1SJ. Bruce Fields 		nfsd_client_rmdir(clp->cl_nfsd_dentry);
200789c905beSJ. Bruce Fields 		clp->cl_nfsd_dentry = NULL;
200889c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
200989c905beSJ. Bruce Fields 	}
201059f8e91bSJ. Bruce Fields 	drop_client(clp);
20111da177e4SLinus Torvalds }
20121da177e4SLinus Torvalds 
201384d38ac9SBenny Halevy /* must be called under the client_lock */
20144beb345bSTrond Myklebust static void
201584d38ac9SBenny Halevy unhash_client_locked(struct nfs4_client *clp)
201684d38ac9SBenny Halevy {
20174beb345bSTrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2018792c95ddSJ. Bruce Fields 	struct nfsd4_session *ses;
2019792c95ddSJ. Bruce Fields 
20200a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
20210a880a28STrond Myklebust 
20224beb345bSTrond Myklebust 	/* Mark the client as expired! */
20234beb345bSTrond Myklebust 	clp->cl_time = 0;
20244beb345bSTrond Myklebust 	/* Make it invisible */
20254beb345bSTrond Myklebust 	if (!list_empty(&clp->cl_idhash)) {
20264beb345bSTrond Myklebust 		list_del_init(&clp->cl_idhash);
20274beb345bSTrond Myklebust 		if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
20284beb345bSTrond Myklebust 			rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
20294beb345bSTrond Myklebust 		else
20304beb345bSTrond Myklebust 			rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
20314beb345bSTrond Myklebust 	}
20324beb345bSTrond Myklebust 	list_del_init(&clp->cl_lru);
20334c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
2034792c95ddSJ. Bruce Fields 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2035792c95ddSJ. Bruce Fields 		list_del_init(&ses->se_hash);
20364c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
203784d38ac9SBenny Halevy }
203884d38ac9SBenny Halevy 
20391da177e4SLinus Torvalds static void
20404beb345bSTrond Myklebust unhash_client(struct nfs4_client *clp)
20414beb345bSTrond Myklebust {
20424beb345bSTrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
20434beb345bSTrond Myklebust 
20444beb345bSTrond Myklebust 	spin_lock(&nn->client_lock);
20454beb345bSTrond Myklebust 	unhash_client_locked(clp);
20464beb345bSTrond Myklebust 	spin_unlock(&nn->client_lock);
20474beb345bSTrond Myklebust }
20484beb345bSTrond Myklebust 
204997403d95SJeff Layton static __be32 mark_client_expired_locked(struct nfs4_client *clp)
205097403d95SJeff Layton {
205114ed14ccSJ. Bruce Fields 	if (atomic_read(&clp->cl_rpc_users))
205297403d95SJeff Layton 		return nfserr_jukebox;
205397403d95SJeff Layton 	unhash_client_locked(clp);
205497403d95SJeff Layton 	return nfs_ok;
205597403d95SJeff Layton }
205697403d95SJeff Layton 
20574beb345bSTrond Myklebust static void
20584beb345bSTrond Myklebust __destroy_client(struct nfs4_client *clp)
20591da177e4SLinus Torvalds {
206068ef3bc3SJeff Layton 	int i;
2061fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
20621da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
20631da177e4SLinus Torvalds 	struct list_head reaplist;
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
2066cdc97505SBenny Halevy 	spin_lock(&state_lock);
2067ea1da636SNeilBrown 	while (!list_empty(&clp->cl_delegations)) {
2068ea1da636SNeilBrown 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
20693fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
207042690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
20711da177e4SLinus Torvalds 	}
2072cdc97505SBenny Halevy 	spin_unlock(&state_lock);
20731da177e4SLinus Torvalds 	while (!list_empty(&reaplist)) {
20741da177e4SLinus Torvalds 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
207542690676SJeff Layton 		list_del_init(&dp->dl_recall_lru);
20760af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
20771da177e4SLinus Torvalds 	}
20782d4a532dSJeff Layton 	while (!list_empty(&clp->cl_revoked)) {
2079c876486bSAndrew Elble 		dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
20802d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
20816011695dSTrond Myklebust 		nfs4_put_stid(&dp->dl_stid);
2082956c4feeSBenny Halevy 	}
2083ea1da636SNeilBrown 	while (!list_empty(&clp->cl_openowners)) {
2084fe0750e5SJ. Bruce Fields 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2085b5971afaSKinglong Mee 		nfs4_get_stateowner(&oo->oo_owner);
2086fe0750e5SJ. Bruce Fields 		release_openowner(oo);
20871da177e4SLinus Torvalds 	}
208868ef3bc3SJeff Layton 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
208968ef3bc3SJeff Layton 		struct nfs4_stateowner *so, *tmp;
209068ef3bc3SJeff Layton 
209168ef3bc3SJeff Layton 		list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
209268ef3bc3SJeff Layton 					 so_strhash) {
209368ef3bc3SJeff Layton 			/* Should be no openowners at this point */
209468ef3bc3SJeff Layton 			WARN_ON_ONCE(so->so_is_open_owner);
209568ef3bc3SJeff Layton 			remove_blocked_locks(lockowner(so));
209668ef3bc3SJeff Layton 		}
209768ef3bc3SJeff Layton 	}
20989cf514ccSChristoph Hellwig 	nfsd4_return_all_client_layouts(clp);
2099e0639dc5SOlga Kornievskaia 	nfsd4_shutdown_copy(clp);
21006ff8da08SJ. Bruce Fields 	nfsd4_shutdown_callback(clp);
21012bf23875SJ. Bruce Fields 	if (clp->cl_cb_conn.cb_xprt)
21022bf23875SJ. Bruce Fields 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2103b12a05cbSJ. Bruce Fields 	free_client(clp);
210489c905beSJ. Bruce Fields 	wake_up_all(&expiry_wq);
21051da177e4SLinus Torvalds }
21061da177e4SLinus Torvalds 
21074beb345bSTrond Myklebust static void
21084beb345bSTrond Myklebust destroy_client(struct nfs4_client *clp)
21094beb345bSTrond Myklebust {
21104beb345bSTrond Myklebust 	unhash_client(clp);
21114beb345bSTrond Myklebust 	__destroy_client(clp);
21124beb345bSTrond Myklebust }
21134beb345bSTrond Myklebust 
2114362063a5SScott Mayhew static void inc_reclaim_complete(struct nfs4_client *clp)
2115362063a5SScott Mayhew {
2116362063a5SScott Mayhew 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2117362063a5SScott Mayhew 
2118362063a5SScott Mayhew 	if (!nn->track_reclaim_completes)
2119362063a5SScott Mayhew 		return;
2120362063a5SScott Mayhew 	if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2121362063a5SScott Mayhew 		return;
2122362063a5SScott Mayhew 	if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2123362063a5SScott Mayhew 			nn->reclaim_str_hashtbl_size) {
2124362063a5SScott Mayhew 		printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2125362063a5SScott Mayhew 				clp->net->ns.inum);
2126362063a5SScott Mayhew 		nfsd4_end_grace(nn);
2127362063a5SScott Mayhew 	}
2128362063a5SScott Mayhew }
2129362063a5SScott Mayhew 
21300d22f68fSJ. Bruce Fields static void expire_client(struct nfs4_client *clp)
21310d22f68fSJ. Bruce Fields {
21324beb345bSTrond Myklebust 	unhash_client(clp);
21330d22f68fSJ. Bruce Fields 	nfsd4_client_record_remove(clp);
21344beb345bSTrond Myklebust 	__destroy_client(clp);
21350d22f68fSJ. Bruce Fields }
21360d22f68fSJ. Bruce Fields 
213735bba9a3SJ. Bruce Fields static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
213835bba9a3SJ. Bruce Fields {
213935bba9a3SJ. Bruce Fields 	memcpy(target->cl_verifier.data, source->data,
214035bba9a3SJ. Bruce Fields 			sizeof(target->cl_verifier.data));
21411da177e4SLinus Torvalds }
21421da177e4SLinus Torvalds 
214335bba9a3SJ. Bruce Fields static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
214435bba9a3SJ. Bruce Fields {
21451da177e4SLinus Torvalds 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
21461da177e4SLinus Torvalds 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
21471da177e4SLinus Torvalds }
21481da177e4SLinus Torvalds 
214950043859SJ. Bruce Fields static int copy_cred(struct svc_cred *target, struct svc_cred *source)
215050043859SJ. Bruce Fields {
21512f10fdcbSNeilBrown 	target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
21522f10fdcbSNeilBrown 	target->cr_raw_principal = kstrdup(source->cr_raw_principal,
21532f10fdcbSNeilBrown 								GFP_KERNEL);
21549abdda5dSChuck Lever 	target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
21552f10fdcbSNeilBrown 	if ((source->cr_principal && !target->cr_principal) ||
21569abdda5dSChuck Lever 	    (source->cr_raw_principal && !target->cr_raw_principal) ||
21579abdda5dSChuck Lever 	    (source->cr_targ_princ && !target->cr_targ_princ))
21582f10fdcbSNeilBrown 		return -ENOMEM;
215950043859SJ. Bruce Fields 
2160d5497fc6SJ. Bruce Fields 	target->cr_flavor = source->cr_flavor;
21611da177e4SLinus Torvalds 	target->cr_uid = source->cr_uid;
21621da177e4SLinus Torvalds 	target->cr_gid = source->cr_gid;
21631da177e4SLinus Torvalds 	target->cr_group_info = source->cr_group_info;
21641da177e4SLinus Torvalds 	get_group_info(target->cr_group_info);
21650dc1531aSJ. Bruce Fields 	target->cr_gss_mech = source->cr_gss_mech;
21660dc1531aSJ. Bruce Fields 	if (source->cr_gss_mech)
21670dc1531aSJ. Bruce Fields 		gss_mech_get(source->cr_gss_mech);
216803a4e1f6SJ. Bruce Fields 	return 0;
21691da177e4SLinus Torvalds }
21701da177e4SLinus Torvalds 
2171ef17af2aSRasmus Villemoes static int
2172ac55fdc4SJeff Layton compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2173ac55fdc4SJeff Layton {
2174ef17af2aSRasmus Villemoes 	if (o1->len < o2->len)
2175ef17af2aSRasmus Villemoes 		return -1;
2176ef17af2aSRasmus Villemoes 	if (o1->len > o2->len)
2177ef17af2aSRasmus Villemoes 		return 1;
2178ef17af2aSRasmus Villemoes 	return memcmp(o1->data, o2->data, o1->len);
2179ac55fdc4SJeff Layton }
2180ac55fdc4SJeff Layton 
21811da177e4SLinus Torvalds static int
2182599e0a22SJ. Bruce Fields same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2183599e0a22SJ. Bruce Fields {
2184599e0a22SJ. Bruce Fields 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
21851da177e4SLinus Torvalds }
21861da177e4SLinus Torvalds 
21871da177e4SLinus Torvalds static int
2188599e0a22SJ. Bruce Fields same_clid(clientid_t *cl1, clientid_t *cl2)
2189599e0a22SJ. Bruce Fields {
2190599e0a22SJ. Bruce Fields 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
21911da177e4SLinus Torvalds }
21921da177e4SLinus Torvalds 
21938fbba96eSJ. Bruce Fields static bool groups_equal(struct group_info *g1, struct group_info *g2)
21948fbba96eSJ. Bruce Fields {
21958fbba96eSJ. Bruce Fields 	int i;
21968fbba96eSJ. Bruce Fields 
21978fbba96eSJ. Bruce Fields 	if (g1->ngroups != g2->ngroups)
21988fbba96eSJ. Bruce Fields 		return false;
21998fbba96eSJ. Bruce Fields 	for (i=0; i<g1->ngroups; i++)
220081243eacSAlexey Dobriyan 		if (!gid_eq(g1->gid[i], g2->gid[i]))
22018fbba96eSJ. Bruce Fields 			return false;
22028fbba96eSJ. Bruce Fields 	return true;
22038fbba96eSJ. Bruce Fields }
22048fbba96eSJ. Bruce Fields 
220568eb3508SJ. Bruce Fields /*
220668eb3508SJ. Bruce Fields  * RFC 3530 language requires clid_inuse be returned when the
220768eb3508SJ. Bruce Fields  * "principal" associated with a requests differs from that previously
220868eb3508SJ. Bruce Fields  * used.  We use uid, gid's, and gss principal string as our best
220968eb3508SJ. Bruce Fields  * approximation.  We also don't want to allow non-gss use of a client
221068eb3508SJ. Bruce Fields  * established using gss: in theory cr_principal should catch that
221168eb3508SJ. Bruce Fields  * change, but in practice cr_principal can be null even in the gss case
221268eb3508SJ. Bruce Fields  * since gssd doesn't always pass down a principal string.
221368eb3508SJ. Bruce Fields  */
221468eb3508SJ. Bruce Fields static bool is_gss_cred(struct svc_cred *cr)
221568eb3508SJ. Bruce Fields {
221668eb3508SJ. Bruce Fields 	/* Is cr_flavor one of the gss "pseudoflavors"?: */
221768eb3508SJ. Bruce Fields 	return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
221868eb3508SJ. Bruce Fields }
221968eb3508SJ. Bruce Fields 
222068eb3508SJ. Bruce Fields 
22215559b50aSVivek Trivedi static bool
2222599e0a22SJ. Bruce Fields same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2223599e0a22SJ. Bruce Fields {
222468eb3508SJ. Bruce Fields 	if ((is_gss_cred(cr1) != is_gss_cred(cr2))
22256fab8779SEric W. Biederman 		|| (!uid_eq(cr1->cr_uid, cr2->cr_uid))
22266fab8779SEric W. Biederman 		|| (!gid_eq(cr1->cr_gid, cr2->cr_gid))
22278fbba96eSJ. Bruce Fields 		|| !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
22288fbba96eSJ. Bruce Fields 		return false;
22299abdda5dSChuck Lever 	/* XXX: check that cr_targ_princ fields match ? */
22308fbba96eSJ. Bruce Fields 	if (cr1->cr_principal == cr2->cr_principal)
22318fbba96eSJ. Bruce Fields 		return true;
22328fbba96eSJ. Bruce Fields 	if (!cr1->cr_principal || !cr2->cr_principal)
22338fbba96eSJ. Bruce Fields 		return false;
22345559b50aSVivek Trivedi 	return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
22351da177e4SLinus Torvalds }
22361da177e4SLinus Torvalds 
223757266a6eSJ. Bruce Fields static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
223857266a6eSJ. Bruce Fields {
223957266a6eSJ. Bruce Fields 	struct svc_cred *cr = &rqstp->rq_cred;
224057266a6eSJ. Bruce Fields 	u32 service;
224157266a6eSJ. Bruce Fields 
2242c4720591SJ. Bruce Fields 	if (!cr->cr_gss_mech)
2243c4720591SJ. Bruce Fields 		return false;
224457266a6eSJ. Bruce Fields 	service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
224557266a6eSJ. Bruce Fields 	return service == RPC_GSS_SVC_INTEGRITY ||
224657266a6eSJ. Bruce Fields 	       service == RPC_GSS_SVC_PRIVACY;
224757266a6eSJ. Bruce Fields }
224857266a6eSJ. Bruce Fields 
2249dedeb13fSAndrew Elble bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
225057266a6eSJ. Bruce Fields {
225157266a6eSJ. Bruce Fields 	struct svc_cred *cr = &rqstp->rq_cred;
225257266a6eSJ. Bruce Fields 
225357266a6eSJ. Bruce Fields 	if (!cl->cl_mach_cred)
225457266a6eSJ. Bruce Fields 		return true;
225557266a6eSJ. Bruce Fields 	if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
225657266a6eSJ. Bruce Fields 		return false;
225757266a6eSJ. Bruce Fields 	if (!svc_rqst_integrity_protected(rqstp))
225857266a6eSJ. Bruce Fields 		return false;
2259414ca017SJ. Bruce Fields 	if (cl->cl_cred.cr_raw_principal)
2260414ca017SJ. Bruce Fields 		return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2261414ca017SJ. Bruce Fields 						cr->cr_raw_principal);
226257266a6eSJ. Bruce Fields 	if (!cr->cr_principal)
226357266a6eSJ. Bruce Fields 		return false;
226457266a6eSJ. Bruce Fields 	return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
226557266a6eSJ. Bruce Fields }
226657266a6eSJ. Bruce Fields 
2267294ac32eSJeff Layton static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2268deda2faaSJ. Bruce Fields {
2269ab4684d1SChuck Lever 	__be32 verf[2];
22701da177e4SLinus Torvalds 
2271f419992cSJeff Layton 	/*
2272f419992cSJeff Layton 	 * This is opaque to client, so no need to byte-swap. Use
2273f419992cSJeff Layton 	 * __force to keep sparse happy
2274f419992cSJeff Layton 	 */
22759104ae49SArnd Bergmann 	verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
227619311aa8SKinglong Mee 	verf[1] = (__force __be32)nn->clverifier_counter++;
2277ab4684d1SChuck Lever 	memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
22781da177e4SLinus Torvalds }
22791da177e4SLinus Torvalds 
2280294ac32eSJeff Layton static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2281294ac32eSJeff Layton {
22829cc76801SArnd Bergmann 	clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2283294ac32eSJeff Layton 	clp->cl_clientid.cl_id = nn->clientid_counter++;
2284294ac32eSJeff Layton 	gen_confirm(clp, nn);
2285294ac32eSJeff Layton }
2286294ac32eSJeff Layton 
22874770d722SJeff Layton static struct nfs4_stid *
22884770d722SJeff Layton find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
22894581d140SJ. Bruce Fields {
22903abdb607SJ. Bruce Fields 	struct nfs4_stid *ret;
22913abdb607SJ. Bruce Fields 
22923abdb607SJ. Bruce Fields 	ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
22933abdb607SJ. Bruce Fields 	if (!ret || !ret->sc_type)
22943abdb607SJ. Bruce Fields 		return NULL;
22953abdb607SJ. Bruce Fields 	return ret;
22964581d140SJ. Bruce Fields }
22974d71ab87SJ. Bruce Fields 
22984770d722SJeff Layton static struct nfs4_stid *
22994770d722SJeff Layton find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2300f459e453SJ. Bruce Fields {
2301f459e453SJ. Bruce Fields 	struct nfs4_stid *s;
2302f459e453SJ. Bruce Fields 
23034770d722SJeff Layton 	spin_lock(&cl->cl_lock);
23044770d722SJeff Layton 	s = find_stateid_locked(cl, t);
23052d3f9668STrond Myklebust 	if (s != NULL) {
23062d3f9668STrond Myklebust 		if (typemask & s->sc_type)
2307a15dfcd5SElena Reshetova 			refcount_inc(&s->sc_count);
23082d3f9668STrond Myklebust 		else
23094770d722SJeff Layton 			s = NULL;
23102d3f9668STrond Myklebust 	}
23114770d722SJeff Layton 	spin_unlock(&cl->cl_lock);
23124d71ab87SJ. Bruce Fields 	return s;
23134581d140SJ. Bruce Fields }
23144581d140SJ. Bruce Fields 
2315a204f25eSJ. Bruce Fields static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2316a204f25eSJ. Bruce Fields {
2317a204f25eSJ. Bruce Fields 	struct nfsdfs_client *nc;
2318a204f25eSJ. Bruce Fields 	nc = get_nfsdfs_client(inode);
2319a204f25eSJ. Bruce Fields 	if (!nc)
2320a204f25eSJ. Bruce Fields 		return NULL;
2321a204f25eSJ. Bruce Fields 	return container_of(nc, struct nfs4_client, cl_nfsdfs);
2322a204f25eSJ. Bruce Fields }
2323a204f25eSJ. Bruce Fields 
2324169319f1SJ. Bruce Fields static void seq_quote_mem(struct seq_file *m, char *data, int len)
2325169319f1SJ. Bruce Fields {
2326169319f1SJ. Bruce Fields 	seq_printf(m, "\"");
2327169319f1SJ. Bruce Fields 	seq_escape_mem_ascii(m, data, len);
2328169319f1SJ. Bruce Fields 	seq_printf(m, "\"");
2329169319f1SJ. Bruce Fields }
2330169319f1SJ. Bruce Fields 
233197ad4031SJ. Bruce Fields static int client_info_show(struct seq_file *m, void *v)
233297ad4031SJ. Bruce Fields {
233397ad4031SJ. Bruce Fields 	struct inode *inode = m->private;
233497ad4031SJ. Bruce Fields 	struct nfs4_client *clp;
233597ad4031SJ. Bruce Fields 	u64 clid;
233697ad4031SJ. Bruce Fields 
2337a204f25eSJ. Bruce Fields 	clp = get_nfsdfs_clp(inode);
2338a204f25eSJ. Bruce Fields 	if (!clp)
233997ad4031SJ. Bruce Fields 		return -ENXIO;
234097ad4031SJ. Bruce Fields 	memcpy(&clid, &clp->cl_clientid, sizeof(clid));
234197ad4031SJ. Bruce Fields 	seq_printf(m, "clientid: 0x%llx\n", clid);
2342169319f1SJ. Bruce Fields 	seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2343169319f1SJ. Bruce Fields 	seq_printf(m, "name: ");
2344169319f1SJ. Bruce Fields 	seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2345169319f1SJ. Bruce Fields 	seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
234679123444SJ. Bruce Fields 	if (clp->cl_nii_domain.data) {
234779123444SJ. Bruce Fields 		seq_printf(m, "Implementation domain: ");
234879123444SJ. Bruce Fields 		seq_quote_mem(m, clp->cl_nii_domain.data,
234979123444SJ. Bruce Fields 					clp->cl_nii_domain.len);
235079123444SJ. Bruce Fields 		seq_printf(m, "\nImplementation name: ");
235179123444SJ. Bruce Fields 		seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2352e29f4703SArnd Bergmann 		seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
235379123444SJ. Bruce Fields 			clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
235479123444SJ. Bruce Fields 	}
235597ad4031SJ. Bruce Fields 	drop_client(clp);
235697ad4031SJ. Bruce Fields 
235797ad4031SJ. Bruce Fields 	return 0;
235897ad4031SJ. Bruce Fields }
235997ad4031SJ. Bruce Fields 
236097ad4031SJ. Bruce Fields static int client_info_open(struct inode *inode, struct file *file)
236197ad4031SJ. Bruce Fields {
236297ad4031SJ. Bruce Fields 	return single_open(file, client_info_show, inode);
236397ad4031SJ. Bruce Fields }
236497ad4031SJ. Bruce Fields 
236597ad4031SJ. Bruce Fields static const struct file_operations client_info_fops = {
236697ad4031SJ. Bruce Fields 	.open		= client_info_open,
236797ad4031SJ. Bruce Fields 	.read		= seq_read,
236897ad4031SJ. Bruce Fields 	.llseek		= seq_lseek,
236997ad4031SJ. Bruce Fields 	.release	= single_release,
237097ad4031SJ. Bruce Fields };
237197ad4031SJ. Bruce Fields 
237278599c42SJ. Bruce Fields static void *states_start(struct seq_file *s, loff_t *pos)
237378599c42SJ. Bruce Fields 	__acquires(&clp->cl_lock)
237478599c42SJ. Bruce Fields {
237578599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
237678599c42SJ. Bruce Fields 	unsigned long id = *pos;
237778599c42SJ. Bruce Fields 	void *ret;
237878599c42SJ. Bruce Fields 
237978599c42SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
238078599c42SJ. Bruce Fields 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
238178599c42SJ. Bruce Fields 	*pos = id;
238278599c42SJ. Bruce Fields 	return ret;
238378599c42SJ. Bruce Fields }
238478599c42SJ. Bruce Fields 
238578599c42SJ. Bruce Fields static void *states_next(struct seq_file *s, void *v, loff_t *pos)
238678599c42SJ. Bruce Fields {
238778599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
238878599c42SJ. Bruce Fields 	unsigned long id = *pos;
238978599c42SJ. Bruce Fields 	void *ret;
239078599c42SJ. Bruce Fields 
239178599c42SJ. Bruce Fields 	id = *pos;
239278599c42SJ. Bruce Fields 	id++;
239378599c42SJ. Bruce Fields 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
239478599c42SJ. Bruce Fields 	*pos = id;
239578599c42SJ. Bruce Fields 	return ret;
239678599c42SJ. Bruce Fields }
239778599c42SJ. Bruce Fields 
239878599c42SJ. Bruce Fields static void states_stop(struct seq_file *s, void *v)
239978599c42SJ. Bruce Fields 	__releases(&clp->cl_lock)
240078599c42SJ. Bruce Fields {
240178599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
240278599c42SJ. Bruce Fields 
240378599c42SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
240478599c42SJ. Bruce Fields }
240578599c42SJ. Bruce Fields 
2406fd4f83fdSJeff Layton static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
240778599c42SJ. Bruce Fields {
2408fd4f83fdSJeff Layton 	struct inode *inode = f->nf_inode;
240978599c42SJ. Bruce Fields 
241078599c42SJ. Bruce Fields 	seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
241178599c42SJ. Bruce Fields 					MAJOR(inode->i_sb->s_dev),
241278599c42SJ. Bruce Fields 					 MINOR(inode->i_sb->s_dev),
241378599c42SJ. Bruce Fields 					 inode->i_ino);
241478599c42SJ. Bruce Fields }
241578599c42SJ. Bruce Fields 
241678599c42SJ. Bruce Fields static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
241778599c42SJ. Bruce Fields {
241878599c42SJ. Bruce Fields 	seq_printf(s, "owner: ");
241978599c42SJ. Bruce Fields 	seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
242078599c42SJ. Bruce Fields }
242178599c42SJ. Bruce Fields 
242278599c42SJ. Bruce Fields static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
242378599c42SJ. Bruce Fields {
242478599c42SJ. Bruce Fields 	struct nfs4_ol_stateid *ols;
242578599c42SJ. Bruce Fields 	struct nfs4_file *nf;
2426fd4f83fdSJeff Layton 	struct nfsd_file *file;
242778599c42SJ. Bruce Fields 	struct nfs4_stateowner *oo;
242878599c42SJ. Bruce Fields 	unsigned int access, deny;
242978599c42SJ. Bruce Fields 
243078599c42SJ. Bruce Fields 	if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
243178599c42SJ. Bruce Fields 		return 0; /* XXX: or SEQ_SKIP? */
243278599c42SJ. Bruce Fields 	ols = openlockstateid(st);
243378599c42SJ. Bruce Fields 	oo = ols->st_stateowner;
243478599c42SJ. Bruce Fields 	nf = st->sc_file;
243578599c42SJ. Bruce Fields 	file = find_any_file(nf);
243678599c42SJ. Bruce Fields 
243778599c42SJ. Bruce Fields 	seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid);
243878599c42SJ. Bruce Fields 
243978599c42SJ. Bruce Fields 	access = bmap_to_share_mode(ols->st_access_bmap);
244078599c42SJ. Bruce Fields 	deny   = bmap_to_share_mode(ols->st_deny_bmap);
244178599c42SJ. Bruce Fields 
2442c4b77edbSJ. Bruce Fields 	seq_printf(s, "access: %s%s, ",
244378599c42SJ. Bruce Fields 		access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
244478599c42SJ. Bruce Fields 		access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2445c4b77edbSJ. Bruce Fields 	seq_printf(s, "deny: %s%s, ",
244678599c42SJ. Bruce Fields 		deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
244778599c42SJ. Bruce Fields 		deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
244878599c42SJ. Bruce Fields 
244978599c42SJ. Bruce Fields 	nfs4_show_superblock(s, file);
245078599c42SJ. Bruce Fields 	seq_printf(s, ", ");
245178599c42SJ. Bruce Fields 	nfs4_show_owner(s, oo);
245278599c42SJ. Bruce Fields 	seq_printf(s, " }\n");
2453fd4f83fdSJeff Layton 	nfsd_file_put(file);
245478599c42SJ. Bruce Fields 
245578599c42SJ. Bruce Fields 	return 0;
245678599c42SJ. Bruce Fields }
245778599c42SJ. Bruce Fields 
245816d36e09SJ. Bruce Fields static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
245916d36e09SJ. Bruce Fields {
246016d36e09SJ. Bruce Fields 	struct nfs4_ol_stateid *ols;
246116d36e09SJ. Bruce Fields 	struct nfs4_file *nf;
2462fd4f83fdSJeff Layton 	struct nfsd_file *file;
246316d36e09SJ. Bruce Fields 	struct nfs4_stateowner *oo;
246416d36e09SJ. Bruce Fields 
246516d36e09SJ. Bruce Fields 	ols = openlockstateid(st);
246616d36e09SJ. Bruce Fields 	oo = ols->st_stateowner;
246716d36e09SJ. Bruce Fields 	nf = st->sc_file;
246816d36e09SJ. Bruce Fields 	file = find_any_file(nf);
246916d36e09SJ. Bruce Fields 
247016d36e09SJ. Bruce Fields 	seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid);
247116d36e09SJ. Bruce Fields 
247216d36e09SJ. Bruce Fields 	/*
247316d36e09SJ. Bruce Fields 	 * Note: a lock stateid isn't really the same thing as a lock,
247416d36e09SJ. Bruce Fields 	 * it's the locking state held by one owner on a file, and there
247516d36e09SJ. Bruce Fields 	 * may be multiple (or no) lock ranges associated with it.
247616d36e09SJ. Bruce Fields 	 * (Same for the matter is true of open stateids.)
247716d36e09SJ. Bruce Fields 	 */
247816d36e09SJ. Bruce Fields 
247916d36e09SJ. Bruce Fields 	nfs4_show_superblock(s, file);
248016d36e09SJ. Bruce Fields 	/* XXX: open stateid? */
248116d36e09SJ. Bruce Fields 	seq_printf(s, ", ");
248216d36e09SJ. Bruce Fields 	nfs4_show_owner(s, oo);
248316d36e09SJ. Bruce Fields 	seq_printf(s, " }\n");
2484fd4f83fdSJeff Layton 	nfsd_file_put(file);
248516d36e09SJ. Bruce Fields 
248616d36e09SJ. Bruce Fields 	return 0;
248716d36e09SJ. Bruce Fields }
248816d36e09SJ. Bruce Fields 
248916d36e09SJ. Bruce Fields static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
249016d36e09SJ. Bruce Fields {
249116d36e09SJ. Bruce Fields 	struct nfs4_delegation *ds;
249216d36e09SJ. Bruce Fields 	struct nfs4_file *nf;
2493eb82dd39SJeff Layton 	struct nfsd_file *file;
249416d36e09SJ. Bruce Fields 
249516d36e09SJ. Bruce Fields 	ds = delegstateid(st);
249616d36e09SJ. Bruce Fields 	nf = st->sc_file;
249716d36e09SJ. Bruce Fields 	file = nf->fi_deleg_file;
249816d36e09SJ. Bruce Fields 
249916d36e09SJ. Bruce Fields 	seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid);
250016d36e09SJ. Bruce Fields 
250116d36e09SJ. Bruce Fields 	/* Kinda dead code as long as we only support read delegs: */
250216d36e09SJ. Bruce Fields 	seq_printf(s, "access: %s, ",
250316d36e09SJ. Bruce Fields 		ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
250416d36e09SJ. Bruce Fields 
250516d36e09SJ. Bruce Fields 	/* XXX: lease time, whether it's being recalled. */
250616d36e09SJ. Bruce Fields 
250716d36e09SJ. Bruce Fields 	nfs4_show_superblock(s, file);
250816d36e09SJ. Bruce Fields 	seq_printf(s, " }\n");
250916d36e09SJ. Bruce Fields 
251016d36e09SJ. Bruce Fields 	return 0;
251116d36e09SJ. Bruce Fields }
251216d36e09SJ. Bruce Fields 
25130c4b62b0SJ. Bruce Fields static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
25140c4b62b0SJ. Bruce Fields {
25150c4b62b0SJ. Bruce Fields 	struct nfs4_layout_stateid *ls;
2516eb82dd39SJeff Layton 	struct nfsd_file *file;
25170c4b62b0SJ. Bruce Fields 
25180c4b62b0SJ. Bruce Fields 	ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
25190c4b62b0SJ. Bruce Fields 	file = ls->ls_file;
25200c4b62b0SJ. Bruce Fields 
25210c4b62b0SJ. Bruce Fields 	seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid);
25220c4b62b0SJ. Bruce Fields 
25230c4b62b0SJ. Bruce Fields 	/* XXX: What else would be useful? */
25240c4b62b0SJ. Bruce Fields 
25250c4b62b0SJ. Bruce Fields 	nfs4_show_superblock(s, file);
25260c4b62b0SJ. Bruce Fields 	seq_printf(s, " }\n");
25270c4b62b0SJ. Bruce Fields 
25280c4b62b0SJ. Bruce Fields 	return 0;
25290c4b62b0SJ. Bruce Fields }
25300c4b62b0SJ. Bruce Fields 
253178599c42SJ. Bruce Fields static int states_show(struct seq_file *s, void *v)
253278599c42SJ. Bruce Fields {
253378599c42SJ. Bruce Fields 	struct nfs4_stid *st = v;
253478599c42SJ. Bruce Fields 
253578599c42SJ. Bruce Fields 	switch (st->sc_type) {
253678599c42SJ. Bruce Fields 	case NFS4_OPEN_STID:
253778599c42SJ. Bruce Fields 		return nfs4_show_open(s, st);
253816d36e09SJ. Bruce Fields 	case NFS4_LOCK_STID:
253916d36e09SJ. Bruce Fields 		return nfs4_show_lock(s, st);
254016d36e09SJ. Bruce Fields 	case NFS4_DELEG_STID:
254116d36e09SJ. Bruce Fields 		return nfs4_show_deleg(s, st);
25420c4b62b0SJ. Bruce Fields 	case NFS4_LAYOUT_STID:
25430c4b62b0SJ. Bruce Fields 		return nfs4_show_layout(s, st);
254478599c42SJ. Bruce Fields 	default:
254578599c42SJ. Bruce Fields 		return 0; /* XXX: or SEQ_SKIP? */
254678599c42SJ. Bruce Fields 	}
254716d36e09SJ. Bruce Fields 	/* XXX: copy stateids? */
254878599c42SJ. Bruce Fields }
254978599c42SJ. Bruce Fields 
255078599c42SJ. Bruce Fields static struct seq_operations states_seq_ops = {
255178599c42SJ. Bruce Fields 	.start = states_start,
255278599c42SJ. Bruce Fields 	.next = states_next,
255378599c42SJ. Bruce Fields 	.stop = states_stop,
255478599c42SJ. Bruce Fields 	.show = states_show
255578599c42SJ. Bruce Fields };
255678599c42SJ. Bruce Fields 
255778599c42SJ. Bruce Fields static int client_states_open(struct inode *inode, struct file *file)
255878599c42SJ. Bruce Fields {
255978599c42SJ. Bruce Fields 	struct seq_file *s;
256078599c42SJ. Bruce Fields 	struct nfs4_client *clp;
256178599c42SJ. Bruce Fields 	int ret;
256278599c42SJ. Bruce Fields 
2563a204f25eSJ. Bruce Fields 	clp = get_nfsdfs_clp(inode);
2564a204f25eSJ. Bruce Fields 	if (!clp)
256578599c42SJ. Bruce Fields 		return -ENXIO;
256678599c42SJ. Bruce Fields 
256778599c42SJ. Bruce Fields 	ret = seq_open(file, &states_seq_ops);
256878599c42SJ. Bruce Fields 	if (ret)
256978599c42SJ. Bruce Fields 		return ret;
257078599c42SJ. Bruce Fields 	s = file->private_data;
257178599c42SJ. Bruce Fields 	s->private = clp;
257278599c42SJ. Bruce Fields 	return 0;
257378599c42SJ. Bruce Fields }
257478599c42SJ. Bruce Fields 
257578599c42SJ. Bruce Fields static int client_opens_release(struct inode *inode, struct file *file)
257678599c42SJ. Bruce Fields {
257778599c42SJ. Bruce Fields 	struct seq_file *m = file->private_data;
257878599c42SJ. Bruce Fields 	struct nfs4_client *clp = m->private;
257978599c42SJ. Bruce Fields 
258078599c42SJ. Bruce Fields 	/* XXX: alternatively, we could get/drop in seq start/stop */
258178599c42SJ. Bruce Fields 	drop_client(clp);
258278599c42SJ. Bruce Fields 	return 0;
258378599c42SJ. Bruce Fields }
258478599c42SJ. Bruce Fields 
258578599c42SJ. Bruce Fields static const struct file_operations client_states_fops = {
258678599c42SJ. Bruce Fields 	.open		= client_states_open,
258778599c42SJ. Bruce Fields 	.read		= seq_read,
258878599c42SJ. Bruce Fields 	.llseek		= seq_lseek,
258978599c42SJ. Bruce Fields 	.release	= client_opens_release,
259078599c42SJ. Bruce Fields };
259178599c42SJ. Bruce Fields 
259289c905beSJ. Bruce Fields /*
259389c905beSJ. Bruce Fields  * Normally we refuse to destroy clients that are in use, but here the
259489c905beSJ. Bruce Fields  * administrator is telling us to just do it.  We also want to wait
259589c905beSJ. Bruce Fields  * so the caller has a guarantee that the client's locks are gone by
259689c905beSJ. Bruce Fields  * the time the write returns:
259789c905beSJ. Bruce Fields  */
2598297e57a2SYueHaibing static void force_expire_client(struct nfs4_client *clp)
259989c905beSJ. Bruce Fields {
260089c905beSJ. Bruce Fields 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
260189c905beSJ. Bruce Fields 	bool already_expired;
260289c905beSJ. Bruce Fields 
260389c905beSJ. Bruce Fields 	spin_lock(&clp->cl_lock);
260489c905beSJ. Bruce Fields 	clp->cl_time = 0;
260589c905beSJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
260689c905beSJ. Bruce Fields 
260789c905beSJ. Bruce Fields 	wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
260889c905beSJ. Bruce Fields 	spin_lock(&nn->client_lock);
260989c905beSJ. Bruce Fields 	already_expired = list_empty(&clp->cl_lru);
261089c905beSJ. Bruce Fields 	if (!already_expired)
261189c905beSJ. Bruce Fields 		unhash_client_locked(clp);
261289c905beSJ. Bruce Fields 	spin_unlock(&nn->client_lock);
261389c905beSJ. Bruce Fields 
261489c905beSJ. Bruce Fields 	if (!already_expired)
261589c905beSJ. Bruce Fields 		expire_client(clp);
261689c905beSJ. Bruce Fields 	else
261789c905beSJ. Bruce Fields 		wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
261889c905beSJ. Bruce Fields }
261989c905beSJ. Bruce Fields 
262089c905beSJ. Bruce Fields static ssize_t client_ctl_write(struct file *file, const char __user *buf,
262189c905beSJ. Bruce Fields 				   size_t size, loff_t *pos)
262289c905beSJ. Bruce Fields {
262389c905beSJ. Bruce Fields 	char *data;
262489c905beSJ. Bruce Fields 	struct nfs4_client *clp;
262589c905beSJ. Bruce Fields 
262689c905beSJ. Bruce Fields 	data = simple_transaction_get(file, buf, size);
262789c905beSJ. Bruce Fields 	if (IS_ERR(data))
262889c905beSJ. Bruce Fields 		return PTR_ERR(data);
262989c905beSJ. Bruce Fields 	if (size != 7 || 0 != memcmp(data, "expire\n", 7))
263089c905beSJ. Bruce Fields 		return -EINVAL;
263189c905beSJ. Bruce Fields 	clp = get_nfsdfs_clp(file_inode(file));
263289c905beSJ. Bruce Fields 	if (!clp)
263389c905beSJ. Bruce Fields 		return -ENXIO;
263489c905beSJ. Bruce Fields 	force_expire_client(clp);
263589c905beSJ. Bruce Fields 	drop_client(clp);
263689c905beSJ. Bruce Fields 	return 7;
263789c905beSJ. Bruce Fields }
263889c905beSJ. Bruce Fields 
263989c905beSJ. Bruce Fields static const struct file_operations client_ctl_fops = {
264089c905beSJ. Bruce Fields 	.write		= client_ctl_write,
264189c905beSJ. Bruce Fields 	.release	= simple_transaction_release,
264289c905beSJ. Bruce Fields };
264389c905beSJ. Bruce Fields 
264497ad4031SJ. Bruce Fields static const struct tree_descr client_files[] = {
264597ad4031SJ. Bruce Fields 	[0] = {"info", &client_info_fops, S_IRUSR},
264678599c42SJ. Bruce Fields 	[1] = {"states", &client_states_fops, S_IRUSR},
26476cbfad5fSPetr Vorel 	[2] = {"ctl", &client_ctl_fops, S_IWUSR},
264878599c42SJ. Bruce Fields 	[3] = {""},
264997ad4031SJ. Bruce Fields };
265097ad4031SJ. Bruce Fields 
26512216d449SJeff Layton static struct nfs4_client *create_client(struct xdr_netobj name,
2652b09333c4SRicardo Labiaga 		struct svc_rqst *rqstp, nfs4_verifier *verf)
2653b09333c4SRicardo Labiaga {
2654b09333c4SRicardo Labiaga 	struct nfs4_client *clp;
2655b09333c4SRicardo Labiaga 	struct sockaddr *sa = svc_addr(rqstp);
265603a4e1f6SJ. Bruce Fields 	int ret;
2657c212cecfSStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
2658e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2659b09333c4SRicardo Labiaga 
2660b09333c4SRicardo Labiaga 	clp = alloc_client(name);
2661b09333c4SRicardo Labiaga 	if (clp == NULL)
2662b09333c4SRicardo Labiaga 		return NULL;
2663b09333c4SRicardo Labiaga 
266403a4e1f6SJ. Bruce Fields 	ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
266503a4e1f6SJ. Bruce Fields 	if (ret) {
2666b09333c4SRicardo Labiaga 		free_client(clp);
2667b09333c4SRicardo Labiaga 		return NULL;
2668b09333c4SRicardo Labiaga 	}
2669e8a79fb1SJ. Bruce Fields 	gen_clid(clp, nn);
2670e8a79fb1SJ. Bruce Fields 	kref_init(&clp->cl_nfsdfs.cl_ref);
26710162ac2bSChristoph Hellwig 	nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
267220b7d86fSArnd Bergmann 	clp->cl_time = ktime_get_boottime_seconds();
2673b09333c4SRicardo Labiaga 	clear_bit(0, &clp->cl_cb_slot_busy);
2674b09333c4SRicardo Labiaga 	copy_verf(clp, verf);
26753bade247SJ. Bruce Fields 	memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2676edd76786SJ. Bruce Fields 	clp->cl_cb_session = NULL;
2677c212cecfSStanislav Kinsbursky 	clp->net = net;
2678e8a79fb1SJ. Bruce Fields 	clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
267997ad4031SJ. Bruce Fields 			clp->cl_clientid.cl_id - nn->clientid_base,
268097ad4031SJ. Bruce Fields 			client_files);
2681e8a79fb1SJ. Bruce Fields 	if (!clp->cl_nfsd_dentry) {
2682e8a79fb1SJ. Bruce Fields 		free_client(clp);
2683e8a79fb1SJ. Bruce Fields 		return NULL;
2684e8a79fb1SJ. Bruce Fields 	}
2685b09333c4SRicardo Labiaga 	return clp;
2686b09333c4SRicardo Labiaga }
2687b09333c4SRicardo Labiaga 
2688fd39ca9aSNeilBrown static void
2689ac55fdc4SJeff Layton add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2690ac55fdc4SJeff Layton {
2691ac55fdc4SJeff Layton 	struct rb_node **new = &(root->rb_node), *parent = NULL;
2692ac55fdc4SJeff Layton 	struct nfs4_client *clp;
2693ac55fdc4SJeff Layton 
2694ac55fdc4SJeff Layton 	while (*new) {
2695ac55fdc4SJeff Layton 		clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2696ac55fdc4SJeff Layton 		parent = *new;
2697ac55fdc4SJeff Layton 
2698ac55fdc4SJeff Layton 		if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2699ac55fdc4SJeff Layton 			new = &((*new)->rb_left);
2700ac55fdc4SJeff Layton 		else
2701ac55fdc4SJeff Layton 			new = &((*new)->rb_right);
2702ac55fdc4SJeff Layton 	}
2703ac55fdc4SJeff Layton 
2704ac55fdc4SJeff Layton 	rb_link_node(&new_clp->cl_namenode, parent, new);
2705ac55fdc4SJeff Layton 	rb_insert_color(&new_clp->cl_namenode, root);
2706ac55fdc4SJeff Layton }
2707ac55fdc4SJeff Layton 
2708ac55fdc4SJeff Layton static struct nfs4_client *
2709ac55fdc4SJeff Layton find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2710ac55fdc4SJeff Layton {
2711ef17af2aSRasmus Villemoes 	int cmp;
2712ac55fdc4SJeff Layton 	struct rb_node *node = root->rb_node;
2713ac55fdc4SJeff Layton 	struct nfs4_client *clp;
2714ac55fdc4SJeff Layton 
2715ac55fdc4SJeff Layton 	while (node) {
2716ac55fdc4SJeff Layton 		clp = rb_entry(node, struct nfs4_client, cl_namenode);
2717ac55fdc4SJeff Layton 		cmp = compare_blob(&clp->cl_name, name);
2718ac55fdc4SJeff Layton 		if (cmp > 0)
2719ac55fdc4SJeff Layton 			node = node->rb_left;
2720ac55fdc4SJeff Layton 		else if (cmp < 0)
2721ac55fdc4SJeff Layton 			node = node->rb_right;
2722ac55fdc4SJeff Layton 		else
2723ac55fdc4SJeff Layton 			return clp;
2724ac55fdc4SJeff Layton 	}
2725ac55fdc4SJeff Layton 	return NULL;
2726ac55fdc4SJeff Layton }
2727ac55fdc4SJeff Layton 
2728ac55fdc4SJeff Layton static void
2729ac55fdc4SJeff Layton add_to_unconfirmed(struct nfs4_client *clp)
27301da177e4SLinus Torvalds {
27311da177e4SLinus Torvalds 	unsigned int idhashval;
27320a7ec377SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
27331da177e4SLinus Torvalds 
27340a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
27350a880a28STrond Myklebust 
2736ac55fdc4SJeff Layton 	clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2737a99454aaSStanislav Kinsbursky 	add_clp_to_name_tree(clp, &nn->unconf_name_tree);
27381da177e4SLinus Torvalds 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
27390a7ec377SStanislav Kinsbursky 	list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
27403dbacee6STrond Myklebust 	renew_client_locked(clp);
27411da177e4SLinus Torvalds }
27421da177e4SLinus Torvalds 
2743fd39ca9aSNeilBrown static void
27441da177e4SLinus Torvalds move_to_confirmed(struct nfs4_client *clp)
27451da177e4SLinus Torvalds {
27461da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
27478daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
27481da177e4SLinus Torvalds 
27490a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
27500a880a28STrond Myklebust 
27511da177e4SLinus Torvalds 	dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
27528daae4dcSStanislav Kinsbursky 	list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2753a99454aaSStanislav Kinsbursky 	rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2754382a62e7SStanislav Kinsbursky 	add_clp_to_name_tree(clp, &nn->conf_name_tree);
2755ac55fdc4SJeff Layton 	set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
27563dbacee6STrond Myklebust 	renew_client_locked(clp);
27571da177e4SLinus Torvalds }
27581da177e4SLinus Torvalds 
27591da177e4SLinus Torvalds static struct nfs4_client *
2760bfa85e83SJ. Bruce Fields find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
27611da177e4SLinus Torvalds {
27621da177e4SLinus Torvalds 	struct nfs4_client *clp;
27631da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clid->cl_id);
27641da177e4SLinus Torvalds 
2765bfa85e83SJ. Bruce Fields 	list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2766a50d2ad1SJ. Bruce Fields 		if (same_clid(&clp->cl_clientid, clid)) {
2767d15c077eSJ. Bruce Fields 			if ((bool)clp->cl_minorversion != sessions)
2768d15c077eSJ. Bruce Fields 				return NULL;
27693dbacee6STrond Myklebust 			renew_client_locked(clp);
27701da177e4SLinus Torvalds 			return clp;
27711da177e4SLinus Torvalds 		}
2772a50d2ad1SJ. Bruce Fields 	}
27731da177e4SLinus Torvalds 	return NULL;
27741da177e4SLinus Torvalds }
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds static struct nfs4_client *
2777bfa85e83SJ. Bruce Fields find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2778bfa85e83SJ. Bruce Fields {
2779bfa85e83SJ. Bruce Fields 	struct list_head *tbl = nn->conf_id_hashtbl;
2780bfa85e83SJ. Bruce Fields 
27810a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
2782bfa85e83SJ. Bruce Fields 	return find_client_in_id_table(tbl, clid, sessions);
2783bfa85e83SJ. Bruce Fields }
2784bfa85e83SJ. Bruce Fields 
2785bfa85e83SJ. Bruce Fields static struct nfs4_client *
27860a7ec377SStanislav Kinsbursky find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
27871da177e4SLinus Torvalds {
2788bfa85e83SJ. Bruce Fields 	struct list_head *tbl = nn->unconf_id_hashtbl;
27891da177e4SLinus Torvalds 
27900a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
2791bfa85e83SJ. Bruce Fields 	return find_client_in_id_table(tbl, clid, sessions);
27921da177e4SLinus Torvalds }
27931da177e4SLinus Torvalds 
27946e5f15c9SJ. Bruce Fields static bool clp_used_exchangeid(struct nfs4_client *clp)
2795a1bcecd2SAndy Adamson {
27966e5f15c9SJ. Bruce Fields 	return clp->cl_exchange_flags != 0;
2797a1bcecd2SAndy Adamson }
2798a1bcecd2SAndy Adamson 
279928ce6054SNeilBrown static struct nfs4_client *
2800382a62e7SStanislav Kinsbursky find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
280128ce6054SNeilBrown {
28020a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
2803382a62e7SStanislav Kinsbursky 	return find_clp_in_name_tree(name, &nn->conf_name_tree);
280428ce6054SNeilBrown }
280528ce6054SNeilBrown 
280628ce6054SNeilBrown static struct nfs4_client *
2807a99454aaSStanislav Kinsbursky find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
280828ce6054SNeilBrown {
28090a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
2810a99454aaSStanislav Kinsbursky 	return find_clp_in_name_tree(name, &nn->unconf_name_tree);
281128ce6054SNeilBrown }
281228ce6054SNeilBrown 
2813fd39ca9aSNeilBrown static void
28146f3d772fSTakuma Umeya gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
28151da177e4SLinus Torvalds {
281607263f1eSJ. Bruce Fields 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
28176f3d772fSTakuma Umeya 	struct sockaddr	*sa = svc_addr(rqstp);
28186f3d772fSTakuma Umeya 	u32 scopeid = rpc_get_scope_id(sa);
28197077ecbaSJeff Layton 	unsigned short expected_family;
28201da177e4SLinus Torvalds 
28217077ecbaSJeff Layton 	/* Currently, we only support tcp and tcp6 for the callback channel */
28227077ecbaSJeff Layton 	if (se->se_callback_netid_len == 3 &&
28237077ecbaSJeff Layton 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
28247077ecbaSJeff Layton 		expected_family = AF_INET;
28257077ecbaSJeff Layton 	else if (se->se_callback_netid_len == 4 &&
28267077ecbaSJeff Layton 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
28277077ecbaSJeff Layton 		expected_family = AF_INET6;
28287077ecbaSJeff Layton 	else
28291da177e4SLinus Torvalds 		goto out_err;
28301da177e4SLinus Torvalds 
2831c212cecfSStanislav Kinsbursky 	conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2832aa9a4ec7SJeff Layton 					    se->se_callback_addr_len,
283307263f1eSJ. Bruce Fields 					    (struct sockaddr *)&conn->cb_addr,
283407263f1eSJ. Bruce Fields 					    sizeof(conn->cb_addr));
2835aa9a4ec7SJeff Layton 
283607263f1eSJ. Bruce Fields 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
28371da177e4SLinus Torvalds 		goto out_err;
2838aa9a4ec7SJeff Layton 
283907263f1eSJ. Bruce Fields 	if (conn->cb_addr.ss_family == AF_INET6)
284007263f1eSJ. Bruce Fields 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2841fbf4665fSJeff Layton 
284207263f1eSJ. Bruce Fields 	conn->cb_prog = se->se_callback_prog;
284307263f1eSJ. Bruce Fields 	conn->cb_ident = se->se_callback_ident;
2844849a1cf1SMi Jinlong 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
28451eace0d1SChuck Lever 	trace_nfsd_cb_args(clp, conn);
28461da177e4SLinus Torvalds 	return;
28471da177e4SLinus Torvalds out_err:
284807263f1eSJ. Bruce Fields 	conn->cb_addr.ss_family = AF_UNSPEC;
284907263f1eSJ. Bruce Fields 	conn->cb_addrlen = 0;
28501eace0d1SChuck Lever 	trace_nfsd_cb_nodelegs(clp);
28511da177e4SLinus Torvalds 	return;
28521da177e4SLinus Torvalds }
28531da177e4SLinus Torvalds 
2854074fe897SAndy Adamson /*
2855067e1aceSJ. Bruce Fields  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2856074fe897SAndy Adamson  */
2857b607664eSTrond Myklebust static void
2858074fe897SAndy Adamson nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2859074fe897SAndy Adamson {
2860f5236013SJ. Bruce Fields 	struct xdr_buf *buf = resp->xdr.buf;
2861557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
2862557ce264SAndy Adamson 	unsigned int base;
2863074fe897SAndy Adamson 
2864557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
2865074fe897SAndy Adamson 
2866085def3aSJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2867557ce264SAndy Adamson 	slot->sl_opcnt = resp->opcnt;
2868557ce264SAndy Adamson 	slot->sl_status = resp->cstate.status;
286953da6a53SJ. Bruce Fields 	free_svc_cred(&slot->sl_cred);
287053da6a53SJ. Bruce Fields 	copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2871bf864a31SAndy Adamson 
2872085def3aSJ. Bruce Fields 	if (!nfsd4_cache_this(resp)) {
2873085def3aSJ. Bruce Fields 		slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2874bf864a31SAndy Adamson 		return;
2875bf864a31SAndy Adamson 	}
2876085def3aSJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_CACHED;
2877085def3aSJ. Bruce Fields 
2878f5236013SJ. Bruce Fields 	base = resp->cstate.data_offset;
2879f5236013SJ. Bruce Fields 	slot->sl_datalen = buf->len - base;
2880f5236013SJ. Bruce Fields 	if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2881d3f03403SDan Carpenter 		WARN(1, "%s: sessions DRC could not cache compound\n",
2882d3f03403SDan Carpenter 		     __func__);
2883557ce264SAndy Adamson 	return;
2884074fe897SAndy Adamson }
2885074fe897SAndy Adamson 
2886074fe897SAndy Adamson /*
2887abfabf8cSAndy Adamson  * Encode the replay sequence operation from the slot values.
2888abfabf8cSAndy Adamson  * If cachethis is FALSE encode the uncached rep error on the next
2889abfabf8cSAndy Adamson  * operation which sets resp->p and increments resp->opcnt for
2890abfabf8cSAndy Adamson  * nfs4svc_encode_compoundres.
2891abfabf8cSAndy Adamson  *
2892074fe897SAndy Adamson  */
2893abfabf8cSAndy Adamson static __be32
2894abfabf8cSAndy Adamson nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2895abfabf8cSAndy Adamson 			  struct nfsd4_compoundres *resp)
2896074fe897SAndy Adamson {
2897abfabf8cSAndy Adamson 	struct nfsd4_op *op;
2898abfabf8cSAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
2899074fe897SAndy Adamson 
2900abfabf8cSAndy Adamson 	/* Encode the replayed sequence operation */
2901abfabf8cSAndy Adamson 	op = &args->ops[resp->opcnt - 1];
2902abfabf8cSAndy Adamson 	nfsd4_encode_operation(resp, op);
2903abfabf8cSAndy Adamson 
2904085def3aSJ. Bruce Fields 	if (slot->sl_flags & NFSD4_SLOT_CACHED)
2905085def3aSJ. Bruce Fields 		return op->status;
2906085def3aSJ. Bruce Fields 	if (args->opcnt == 1) {
2907085def3aSJ. Bruce Fields 		/*
2908085def3aSJ. Bruce Fields 		 * The original operation wasn't a solo sequence--we
2909085def3aSJ. Bruce Fields 		 * always cache those--so this retry must not match the
2910085def3aSJ. Bruce Fields 		 * original:
2911085def3aSJ. Bruce Fields 		 */
2912085def3aSJ. Bruce Fields 		op->status = nfserr_seq_false_retry;
2913085def3aSJ. Bruce Fields 	} else {
2914abfabf8cSAndy Adamson 		op = &args->ops[resp->opcnt++];
2915abfabf8cSAndy Adamson 		op->status = nfserr_retry_uncached_rep;
2916abfabf8cSAndy Adamson 		nfsd4_encode_operation(resp, op);
2917074fe897SAndy Adamson 	}
2918abfabf8cSAndy Adamson 	return op->status;
2919074fe897SAndy Adamson }
2920074fe897SAndy Adamson 
2921074fe897SAndy Adamson /*
2922557ce264SAndy Adamson  * The sequence operation is not cached because we can use the slot and
2923557ce264SAndy Adamson  * session values.
2924074fe897SAndy Adamson  */
29253ca2eb98SJ. Bruce Fields static __be32
2926bf864a31SAndy Adamson nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2927bf864a31SAndy Adamson 			 struct nfsd4_sequence *seq)
2928074fe897SAndy Adamson {
2929557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
2930f5236013SJ. Bruce Fields 	struct xdr_stream *xdr = &resp->xdr;
2931f5236013SJ. Bruce Fields 	__be32 *p;
2932074fe897SAndy Adamson 	__be32 status;
2933074fe897SAndy Adamson 
2934557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
2935074fe897SAndy Adamson 
2936abfabf8cSAndy Adamson 	status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
29370da7b19cSJ. Bruce Fields 	if (status)
2938abfabf8cSAndy Adamson 		return status;
2939074fe897SAndy Adamson 
2940f5236013SJ. Bruce Fields 	p = xdr_reserve_space(xdr, slot->sl_datalen);
2941f5236013SJ. Bruce Fields 	if (!p) {
2942f5236013SJ. Bruce Fields 		WARN_ON_ONCE(1);
2943f5236013SJ. Bruce Fields 		return nfserr_serverfault;
2944f5236013SJ. Bruce Fields 	}
2945f5236013SJ. Bruce Fields 	xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2946f5236013SJ. Bruce Fields 	xdr_commit_encode(xdr);
2947074fe897SAndy Adamson 
2948557ce264SAndy Adamson 	resp->opcnt = slot->sl_opcnt;
2949f5236013SJ. Bruce Fields 	return slot->sl_status;
2950074fe897SAndy Adamson }
2951074fe897SAndy Adamson 
29520733d213SAndy Adamson /*
29530733d213SAndy Adamson  * Set the exchange_id flags returned by the server.
29540733d213SAndy Adamson  */
29550733d213SAndy Adamson static void
29560733d213SAndy Adamson nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
29570733d213SAndy Adamson {
29589cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
29599cf514ccSChristoph Hellwig 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
29609cf514ccSChristoph Hellwig #else
29610733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
29629cf514ccSChristoph Hellwig #endif
29630733d213SAndy Adamson 
29640733d213SAndy Adamson 	/* Referrals are supported, Migration is not. */
29650733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
29660733d213SAndy Adamson 
29670733d213SAndy Adamson 	/* set the wire flags to return to client. */
29680733d213SAndy Adamson 	clid->flags = new->cl_exchange_flags;
29690733d213SAndy Adamson }
29700733d213SAndy Adamson 
29714eaea134SJ. Bruce Fields static bool client_has_openowners(struct nfs4_client *clp)
29724eaea134SJ. Bruce Fields {
29734eaea134SJ. Bruce Fields 	struct nfs4_openowner *oo;
29744eaea134SJ. Bruce Fields 
29754eaea134SJ. Bruce Fields 	list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
29764eaea134SJ. Bruce Fields 		if (!list_empty(&oo->oo_owner.so_stateids))
29774eaea134SJ. Bruce Fields 			return true;
29784eaea134SJ. Bruce Fields 	}
29794eaea134SJ. Bruce Fields 	return false;
29804eaea134SJ. Bruce Fields }
29814eaea134SJ. Bruce Fields 
2982631fc9eaSJ. Bruce Fields static bool client_has_state(struct nfs4_client *clp)
2983631fc9eaSJ. Bruce Fields {
29844eaea134SJ. Bruce Fields 	return client_has_openowners(clp)
298547e970beSKinglong Mee #ifdef CONFIG_NFSD_PNFS
298647e970beSKinglong Mee 		|| !list_empty(&clp->cl_lo_states)
298747e970beSKinglong Mee #endif
29886eccece9SJ. Bruce Fields 		|| !list_empty(&clp->cl_delegations)
2989e0639dc5SOlga Kornievskaia 		|| !list_empty(&clp->cl_sessions)
2990e0639dc5SOlga Kornievskaia 		|| !list_empty(&clp->async_copies);
2991631fc9eaSJ. Bruce Fields }
2992631fc9eaSJ. Bruce Fields 
299379123444SJ. Bruce Fields static __be32 copy_impl_id(struct nfs4_client *clp,
299479123444SJ. Bruce Fields 				struct nfsd4_exchange_id *exid)
299579123444SJ. Bruce Fields {
299679123444SJ. Bruce Fields 	if (!exid->nii_domain.data)
299779123444SJ. Bruce Fields 		return 0;
299879123444SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
299979123444SJ. Bruce Fields 	if (!clp->cl_nii_domain.data)
300079123444SJ. Bruce Fields 		return nfserr_jukebox;
300179123444SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
300279123444SJ. Bruce Fields 	if (!clp->cl_nii_name.data)
300379123444SJ. Bruce Fields 		return nfserr_jukebox;
3004e29f4703SArnd Bergmann 	clp->cl_nii_time = exid->nii_time;
300579123444SJ. Bruce Fields 	return 0;
300679123444SJ. Bruce Fields }
300779123444SJ. Bruce Fields 
3008b37ad28bSAl Viro __be32
3009eb69853dSChristoph Hellwig nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3010eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3011069b6ad4SAndy Adamson {
3012eb69853dSChristoph Hellwig 	struct nfsd4_exchange_id *exid = &u->exchange_id;
30133dbacee6STrond Myklebust 	struct nfs4_client *conf, *new;
30143dbacee6STrond Myklebust 	struct nfs4_client *unconf = NULL;
301557b7b43bSJ. Bruce Fields 	__be32 status;
3016363168b4SJeff Layton 	char			addr_str[INET6_ADDRSTRLEN];
30170733d213SAndy Adamson 	nfs4_verifier		verf = exid->verifier;
3018363168b4SJeff Layton 	struct sockaddr		*sa = svc_addr(rqstp);
301983e08fd4SJ. Bruce Fields 	bool	update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3020c212cecfSStanislav Kinsbursky 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
30210733d213SAndy Adamson 
3022363168b4SJeff Layton 	rpc_ntop(sa, addr_str, sizeof(addr_str));
30230733d213SAndy Adamson 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3024363168b4SJeff Layton 		"ip_addr=%s flags %x, spa_how %d\n",
30250733d213SAndy Adamson 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
3026363168b4SJeff Layton 		addr_str, exid->flags, exid->spa_how);
30270733d213SAndy Adamson 
3028a084daf5SJ. Bruce Fields 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
30290733d213SAndy Adamson 		return nfserr_inval;
30300733d213SAndy Adamson 
303150c7b948SJ. Bruce Fields 	new = create_client(exid->clname, rqstp, &verf);
303250c7b948SJ. Bruce Fields 	if (new == NULL)
303350c7b948SJ. Bruce Fields 		return nfserr_jukebox;
303479123444SJ. Bruce Fields 	status = copy_impl_id(new, exid);
303579123444SJ. Bruce Fields 	if (status)
303679123444SJ. Bruce Fields 		goto out_nolock;
303750c7b948SJ. Bruce Fields 
30380733d213SAndy Adamson 	switch (exid->spa_how) {
303957266a6eSJ. Bruce Fields 	case SP4_MACH_CRED:
3040ed941643SAndrew Elble 		exid->spo_must_enforce[0] = 0;
3041ed941643SAndrew Elble 		exid->spo_must_enforce[1] = (
3042ed941643SAndrew Elble 			1 << (OP_BIND_CONN_TO_SESSION - 32) |
3043ed941643SAndrew Elble 			1 << (OP_EXCHANGE_ID - 32) |
3044ed941643SAndrew Elble 			1 << (OP_CREATE_SESSION - 32) |
3045ed941643SAndrew Elble 			1 << (OP_DESTROY_SESSION - 32) |
3046ed941643SAndrew Elble 			1 << (OP_DESTROY_CLIENTID - 32));
3047ed941643SAndrew Elble 
3048ed941643SAndrew Elble 		exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3049ed941643SAndrew Elble 					1 << (OP_OPEN_DOWNGRADE) |
3050ed941643SAndrew Elble 					1 << (OP_LOCKU) |
3051ed941643SAndrew Elble 					1 << (OP_DELEGRETURN));
3052ed941643SAndrew Elble 
3053ed941643SAndrew Elble 		exid->spo_must_allow[1] &= (
3054ed941643SAndrew Elble 					1 << (OP_TEST_STATEID - 32) |
3055ed941643SAndrew Elble 					1 << (OP_FREE_STATEID - 32));
305650c7b948SJ. Bruce Fields 		if (!svc_rqst_integrity_protected(rqstp)) {
305750c7b948SJ. Bruce Fields 			status = nfserr_inval;
305850c7b948SJ. Bruce Fields 			goto out_nolock;
305950c7b948SJ. Bruce Fields 		}
3060920dd9bbSJ. Bruce Fields 		/*
3061920dd9bbSJ. Bruce Fields 		 * Sometimes userspace doesn't give us a principal.
3062920dd9bbSJ. Bruce Fields 		 * Which is a bug, really.  Anyway, we can't enforce
3063920dd9bbSJ. Bruce Fields 		 * MACH_CRED in that case, better to give up now:
3064920dd9bbSJ. Bruce Fields 		 */
3065414ca017SJ. Bruce Fields 		if (!new->cl_cred.cr_principal &&
3066414ca017SJ. Bruce Fields 					!new->cl_cred.cr_raw_principal) {
3067920dd9bbSJ. Bruce Fields 			status = nfserr_serverfault;
3068920dd9bbSJ. Bruce Fields 			goto out_nolock;
3069920dd9bbSJ. Bruce Fields 		}
307050c7b948SJ. Bruce Fields 		new->cl_mach_cred = true;
30710733d213SAndy Adamson 	case SP4_NONE:
30720733d213SAndy Adamson 		break;
3073063b0fb9SJ. Bruce Fields 	default:				/* checked by xdr code */
3074063b0fb9SJ. Bruce Fields 		WARN_ON_ONCE(1);
30750a4c9265SGustavo A. R. Silva 		/* fall through */
30760733d213SAndy Adamson 	case SP4_SSV:
30778edf4b02SKinglong Mee 		status = nfserr_encr_alg_unsupp;
30788edf4b02SKinglong Mee 		goto out_nolock;
30790733d213SAndy Adamson 	}
30800733d213SAndy Adamson 
30812dbb269dSJ. Bruce Fields 	/* Cases below refer to rfc 5661 section 18.35.4: */
30823dbacee6STrond Myklebust 	spin_lock(&nn->client_lock);
3083382a62e7SStanislav Kinsbursky 	conf = find_confirmed_client_by_name(&exid->clname, nn);
30840733d213SAndy Adamson 	if (conf) {
308583e08fd4SJ. Bruce Fields 		bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
308683e08fd4SJ. Bruce Fields 		bool verfs_match = same_verf(&verf, &conf->cl_verifier);
308783e08fd4SJ. Bruce Fields 
3088136e658dSJ. Bruce Fields 		if (update) {
3089136e658dSJ. Bruce Fields 			if (!clp_used_exchangeid(conf)) { /* buggy client */
30902dbb269dSJ. Bruce Fields 				status = nfserr_inval;
3091e203d506SJ. Bruce Fields 				goto out;
3092e203d506SJ. Bruce Fields 			}
3093dedeb13fSAndrew Elble 			if (!nfsd4_mach_creds_match(conf, rqstp)) {
309457266a6eSJ. Bruce Fields 				status = nfserr_wrong_cred;
309557266a6eSJ. Bruce Fields 				goto out;
309657266a6eSJ. Bruce Fields 			}
30972dbb269dSJ. Bruce Fields 			if (!creds_match) { /* case 9 */
30980733d213SAndy Adamson 				status = nfserr_perm;
30990733d213SAndy Adamson 				goto out;
31000733d213SAndy Adamson 			}
31012dbb269dSJ. Bruce Fields 			if (!verfs_match) { /* case 8 */
31020733d213SAndy Adamson 				status = nfserr_not_same;
31030733d213SAndy Adamson 				goto out;
31040733d213SAndy Adamson 			}
3105136e658dSJ. Bruce Fields 			/* case 6 */
31060733d213SAndy Adamson 			exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
31070733d213SAndy Adamson 			goto out_copy;
31086ddbbbfeSMike Sager 		}
3109136e658dSJ. Bruce Fields 		if (!creds_match) { /* case 3 */
3110631fc9eaSJ. Bruce Fields 			if (client_has_state(conf)) {
3111136e658dSJ. Bruce Fields 				status = nfserr_clid_inuse;
3112136e658dSJ. Bruce Fields 				goto out;
3113136e658dSJ. Bruce Fields 			}
3114b9831b59SJ. Bruce Fields 			goto out_new;
3115631fc9eaSJ. Bruce Fields 		}
3116136e658dSJ. Bruce Fields 		if (verfs_match) { /* case 2 */
31170f1ba0efSJ. Bruce Fields 			conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3118136e658dSJ. Bruce Fields 			goto out_copy;
3119136e658dSJ. Bruce Fields 		}
31202dbb269dSJ. Bruce Fields 		/* case 5, client reboot */
31213dbacee6STrond Myklebust 		conf = NULL;
31220733d213SAndy Adamson 		goto out_new;
31230733d213SAndy Adamson 	}
31246ddbbbfeSMike Sager 
31252dbb269dSJ. Bruce Fields 	if (update) { /* case 7 */
31260733d213SAndy Adamson 		status = nfserr_noent;
31270733d213SAndy Adamson 		goto out;
31280733d213SAndy Adamson 	}
31290733d213SAndy Adamson 
3130a99454aaSStanislav Kinsbursky 	unconf  = find_unconfirmed_client_by_name(&exid->clname, nn);
31312dbb269dSJ. Bruce Fields 	if (unconf) /* case 4, possible retry or client restart */
31323dbacee6STrond Myklebust 		unhash_client_locked(unconf);
31330733d213SAndy Adamson 
31342dbb269dSJ. Bruce Fields 	/* case 1 (normal case) */
31350733d213SAndy Adamson out_new:
3136fd699b8aSJeff Layton 	if (conf) {
3137fd699b8aSJeff Layton 		status = mark_client_expired_locked(conf);
3138fd699b8aSJeff Layton 		if (status)
3139fd699b8aSJeff Layton 			goto out;
3140fd699b8aSJeff Layton 	}
31414f540e29SJ. Bruce Fields 	new->cl_minorversion = cstate->minorversion;
3142ed941643SAndrew Elble 	new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3143ed941643SAndrew Elble 	new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
31440733d213SAndy Adamson 
3145ac55fdc4SJeff Layton 	add_to_unconfirmed(new);
31463dbacee6STrond Myklebust 	swap(new, conf);
31470733d213SAndy Adamson out_copy:
31485cc40fd7STrond Myklebust 	exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
31495cc40fd7STrond Myklebust 	exid->clientid.cl_id = conf->cl_clientid.cl_id;
31500733d213SAndy Adamson 
31515cc40fd7STrond Myklebust 	exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
31525cc40fd7STrond Myklebust 	nfsd4_set_ex_flags(conf, exid);
31530733d213SAndy Adamson 
31540733d213SAndy Adamson 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
31555cc40fd7STrond Myklebust 		conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
31560733d213SAndy Adamson 	status = nfs_ok;
31570733d213SAndy Adamson 
31580733d213SAndy Adamson out:
31593dbacee6STrond Myklebust 	spin_unlock(&nn->client_lock);
316050c7b948SJ. Bruce Fields out_nolock:
31615cc40fd7STrond Myklebust 	if (new)
31623dbacee6STrond Myklebust 		expire_client(new);
31633dbacee6STrond Myklebust 	if (unconf)
31643dbacee6STrond Myklebust 		expire_client(unconf);
31650733d213SAndy Adamson 	return status;
3166069b6ad4SAndy Adamson }
3167069b6ad4SAndy Adamson 
316857b7b43bSJ. Bruce Fields static __be32
316988e588d5SAndy Adamson check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3170b85d4c01SBenny Halevy {
317188e588d5SAndy Adamson 	dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
317288e588d5SAndy Adamson 		slot_seqid);
3173b85d4c01SBenny Halevy 
3174b85d4c01SBenny Halevy 	/* The slot is in use, and no response has been sent. */
317588e588d5SAndy Adamson 	if (slot_inuse) {
317688e588d5SAndy Adamson 		if (seqid == slot_seqid)
3177b85d4c01SBenny Halevy 			return nfserr_jukebox;
3178b85d4c01SBenny Halevy 		else
3179b85d4c01SBenny Halevy 			return nfserr_seq_misordered;
3180b85d4c01SBenny Halevy 	}
3181f6d82485SJ. Bruce Fields 	/* Note unsigned 32-bit arithmetic handles wraparound: */
318288e588d5SAndy Adamson 	if (likely(seqid == slot_seqid + 1))
3183b85d4c01SBenny Halevy 		return nfs_ok;
318488e588d5SAndy Adamson 	if (seqid == slot_seqid)
3185b85d4c01SBenny Halevy 		return nfserr_replay_cache;
3186b85d4c01SBenny Halevy 	return nfserr_seq_misordered;
3187b85d4c01SBenny Halevy }
3188b85d4c01SBenny Halevy 
318949557cc7SAndy Adamson /*
319049557cc7SAndy Adamson  * Cache the create session result into the create session single DRC
319149557cc7SAndy Adamson  * slot cache by saving the xdr structure. sl_seqid has been set.
319249557cc7SAndy Adamson  * Do this for solo or embedded create session operations.
319349557cc7SAndy Adamson  */
319449557cc7SAndy Adamson static void
319549557cc7SAndy Adamson nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
319657b7b43bSJ. Bruce Fields 			   struct nfsd4_clid_slot *slot, __be32 nfserr)
319749557cc7SAndy Adamson {
319849557cc7SAndy Adamson 	slot->sl_status = nfserr;
319949557cc7SAndy Adamson 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
320049557cc7SAndy Adamson }
320149557cc7SAndy Adamson 
320249557cc7SAndy Adamson static __be32
320349557cc7SAndy Adamson nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
320449557cc7SAndy Adamson 			    struct nfsd4_clid_slot *slot)
320549557cc7SAndy Adamson {
320649557cc7SAndy Adamson 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
320749557cc7SAndy Adamson 	return slot->sl_status;
320849557cc7SAndy Adamson }
320949557cc7SAndy Adamson 
32101b74c25bSMi Jinlong #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
32111b74c25bSMi Jinlong 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
32121b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
32131b74c25bSMi Jinlong 			3 +	/* version, opcount, opcode */ \
32141b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
32151b74c25bSMi Jinlong 				/* seqid, slotID, slotID, cache */ \
32161b74c25bSMi Jinlong 			4 ) * sizeof(__be32))
32171b74c25bSMi Jinlong 
32181b74c25bSMi Jinlong #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
32191b74c25bSMi Jinlong 			2 +	/* verifier: AUTH_NULL, length 0 */\
32201b74c25bSMi Jinlong 			1 +	/* status */ \
32211b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
32221b74c25bSMi Jinlong 			3 +	/* opcount, opcode, opstatus*/ \
32231b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
32241b74c25bSMi Jinlong 				/* seqid, slotID, slotID, slotID, status */ \
32251b74c25bSMi Jinlong 			5 ) * sizeof(__be32))
32261b74c25bSMi Jinlong 
322755c760cfSJ. Bruce Fields static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
32281b74c25bSMi Jinlong {
322955c760cfSJ. Bruce Fields 	u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
323055c760cfSJ. Bruce Fields 
3231373cd409SJ. Bruce Fields 	if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3232373cd409SJ. Bruce Fields 		return nfserr_toosmall;
3233373cd409SJ. Bruce Fields 	if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3234373cd409SJ. Bruce Fields 		return nfserr_toosmall;
323555c760cfSJ. Bruce Fields 	ca->headerpadsz = 0;
323655c760cfSJ. Bruce Fields 	ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
323755c760cfSJ. Bruce Fields 	ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
323855c760cfSJ. Bruce Fields 	ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
323955c760cfSJ. Bruce Fields 	ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
324055c760cfSJ. Bruce Fields 			NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
324155c760cfSJ. Bruce Fields 	ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
324255c760cfSJ. Bruce Fields 	/*
324355c760cfSJ. Bruce Fields 	 * Note decreasing slot size below client's request may make it
324455c760cfSJ. Bruce Fields 	 * difficult for client to function correctly, whereas
324555c760cfSJ. Bruce Fields 	 * decreasing the number of slots will (just?) affect
324655c760cfSJ. Bruce Fields 	 * performance.  When short on memory we therefore prefer to
324755c760cfSJ. Bruce Fields 	 * decrease number of slots instead of their size.  Clients that
324855c760cfSJ. Bruce Fields 	 * request larger slots than they need will get poor results:
32497f49fd5dSNeilBrown 	 * Note that we always allow at least one slot, because our
32507f49fd5dSNeilBrown 	 * accounting is soft and provides no guarantees either way.
325155c760cfSJ. Bruce Fields 	 */
32522030ca56SNeilBrown 	ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
325355c760cfSJ. Bruce Fields 
3254373cd409SJ. Bruce Fields 	return nfs_ok;
32551b74c25bSMi Jinlong }
32561b74c25bSMi Jinlong 
32574500632fSChuck Lever /*
32584500632fSChuck Lever  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
32594500632fSChuck Lever  * These are based on similar macros in linux/sunrpc/msg_prot.h .
32604500632fSChuck Lever  */
32614500632fSChuck Lever #define RPC_MAX_HEADER_WITH_AUTH_SYS \
32624500632fSChuck Lever 	(RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
32634500632fSChuck Lever 
32644500632fSChuck Lever #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
32654500632fSChuck Lever 	(RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
32664500632fSChuck Lever 
32678a891633SKinglong Mee #define NFSD_CB_MAX_REQ_SZ	((NFS4_enc_cb_recall_sz + \
32684500632fSChuck Lever 				 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
32698a891633SKinglong Mee #define NFSD_CB_MAX_RESP_SZ	((NFS4_dec_cb_recall_sz + \
32704500632fSChuck Lever 				 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
32714500632fSChuck Lever 				 sizeof(__be32))
32728a891633SKinglong Mee 
327306b332a5SJ. Bruce Fields static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
327406b332a5SJ. Bruce Fields {
327506b332a5SJ. Bruce Fields 	ca->headerpadsz = 0;
327606b332a5SJ. Bruce Fields 
32778a891633SKinglong Mee 	if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
327806b332a5SJ. Bruce Fields 		return nfserr_toosmall;
32798a891633SKinglong Mee 	if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
328006b332a5SJ. Bruce Fields 		return nfserr_toosmall;
328106b332a5SJ. Bruce Fields 	ca->maxresp_cached = 0;
328206b332a5SJ. Bruce Fields 	if (ca->maxops < 2)
328306b332a5SJ. Bruce Fields 		return nfserr_toosmall;
328406b332a5SJ. Bruce Fields 
328506b332a5SJ. Bruce Fields 	return nfs_ok;
3286069b6ad4SAndy Adamson }
3287069b6ad4SAndy Adamson 
3288b78724b7SJ. Bruce Fields static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3289b78724b7SJ. Bruce Fields {
3290b78724b7SJ. Bruce Fields 	switch (cbs->flavor) {
3291b78724b7SJ. Bruce Fields 	case RPC_AUTH_NULL:
3292b78724b7SJ. Bruce Fields 	case RPC_AUTH_UNIX:
3293b78724b7SJ. Bruce Fields 		return nfs_ok;
3294b78724b7SJ. Bruce Fields 	default:
3295b78724b7SJ. Bruce Fields 		/*
3296b78724b7SJ. Bruce Fields 		 * GSS case: the spec doesn't allow us to return this
3297b78724b7SJ. Bruce Fields 		 * error.  But it also doesn't allow us not to support
3298b78724b7SJ. Bruce Fields 		 * GSS.
3299b78724b7SJ. Bruce Fields 		 * I'd rather this fail hard than return some error the
3300b78724b7SJ. Bruce Fields 		 * client might think it can already handle:
3301b78724b7SJ. Bruce Fields 		 */
3302b78724b7SJ. Bruce Fields 		return nfserr_encr_alg_unsupp;
3303b78724b7SJ. Bruce Fields 	}
3304b78724b7SJ. Bruce Fields }
3305b78724b7SJ. Bruce Fields 
3306069b6ad4SAndy Adamson __be32
3307069b6ad4SAndy Adamson nfsd4_create_session(struct svc_rqst *rqstp,
3308eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3309069b6ad4SAndy Adamson {
3310eb69853dSChristoph Hellwig 	struct nfsd4_create_session *cr_ses = &u->create_session;
3311363168b4SJeff Layton 	struct sockaddr *sa = svc_addr(rqstp);
3312ec6b5d7bSAndy Adamson 	struct nfs4_client *conf, *unconf;
3313d20c11d8SJeff Layton 	struct nfs4_client *old = NULL;
3314ac7c46f2SJ. Bruce Fields 	struct nfsd4_session *new;
331581f0b2a4SJ. Bruce Fields 	struct nfsd4_conn *conn;
331649557cc7SAndy Adamson 	struct nfsd4_clid_slot *cs_slot = NULL;
331757b7b43bSJ. Bruce Fields 	__be32 status = 0;
33188daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3319ec6b5d7bSAndy Adamson 
3320a62573dcSMi Jinlong 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3321a62573dcSMi Jinlong 		return nfserr_inval;
3322b78724b7SJ. Bruce Fields 	status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3323b78724b7SJ. Bruce Fields 	if (status)
3324b78724b7SJ. Bruce Fields 		return status;
332555c760cfSJ. Bruce Fields 	status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3326373cd409SJ. Bruce Fields 	if (status)
3327373cd409SJ. Bruce Fields 		return status;
332806b332a5SJ. Bruce Fields 	status = check_backchannel_attrs(&cr_ses->back_channel);
332906b332a5SJ. Bruce Fields 	if (status)
3330f403e450SKinglong Mee 		goto out_release_drc_mem;
333181f0b2a4SJ. Bruce Fields 	status = nfserr_jukebox;
333260810e54SKinglong Mee 	new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
333355c760cfSJ. Bruce Fields 	if (!new)
333455c760cfSJ. Bruce Fields 		goto out_release_drc_mem;
333581f0b2a4SJ. Bruce Fields 	conn = alloc_conn_from_crses(rqstp, cr_ses);
333681f0b2a4SJ. Bruce Fields 	if (!conn)
333781f0b2a4SJ. Bruce Fields 		goto out_free_session;
3338a62573dcSMi Jinlong 
3339d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
33400a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
33418daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(&cr_ses->clientid, true, nn);
334278389046SJ. Bruce Fields 	WARN_ON_ONCE(conf && unconf);
3343ec6b5d7bSAndy Adamson 
3344ec6b5d7bSAndy Adamson 	if (conf) {
334557266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
3346dedeb13fSAndrew Elble 		if (!nfsd4_mach_creds_match(conf, rqstp))
334757266a6eSJ. Bruce Fields 			goto out_free_conn;
334849557cc7SAndy Adamson 		cs_slot = &conf->cl_cs_slot;
334949557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3350f5e22bb6SKinglong Mee 		if (status) {
3351f5e22bb6SKinglong Mee 			if (status == nfserr_replay_cache)
335249557cc7SAndy Adamson 				status = nfsd4_replay_create_session(cr_ses, cs_slot);
335381f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3354ec6b5d7bSAndy Adamson 		}
3355ec6b5d7bSAndy Adamson 	} else if (unconf) {
3356ec6b5d7bSAndy Adamson 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3357363168b4SJeff Layton 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3358ec6b5d7bSAndy Adamson 			status = nfserr_clid_inuse;
335981f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3360ec6b5d7bSAndy Adamson 		}
336157266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
3362dedeb13fSAndrew Elble 		if (!nfsd4_mach_creds_match(unconf, rqstp))
336357266a6eSJ. Bruce Fields 			goto out_free_conn;
336449557cc7SAndy Adamson 		cs_slot = &unconf->cl_cs_slot;
336549557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
336638eb76a5SAndy Adamson 		if (status) {
336738eb76a5SAndy Adamson 			/* an unconfirmed replay returns misordered */
3368ec6b5d7bSAndy Adamson 			status = nfserr_seq_misordered;
336981f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3370ec6b5d7bSAndy Adamson 		}
3371382a62e7SStanislav Kinsbursky 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3372221a6876SJ. Bruce Fields 		if (old) {
3373d20c11d8SJeff Layton 			status = mark_client_expired_locked(old);
33747abea1e8SJeff Layton 			if (status) {
33757abea1e8SJeff Layton 				old = NULL;
3376221a6876SJ. Bruce Fields 				goto out_free_conn;
3377221a6876SJ. Bruce Fields 			}
33787abea1e8SJeff Layton 		}
33798f9d3d3bSJ. Bruce Fields 		move_to_confirmed(unconf);
3380ec6b5d7bSAndy Adamson 		conf = unconf;
3381ec6b5d7bSAndy Adamson 	} else {
3382ec6b5d7bSAndy Adamson 		status = nfserr_stale_clientid;
338381f0b2a4SJ. Bruce Fields 		goto out_free_conn;
3384ec6b5d7bSAndy Adamson 	}
338581f0b2a4SJ. Bruce Fields 	status = nfs_ok;
33864ce85c8cSChuck Lever 	/* Persistent sessions are not supported */
3387408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_PERSIST;
33884ce85c8cSChuck Lever 	/* Upshifting from TCP to RDMA is not supported */
3389408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_RDMA;
3390408b79bcSJ. Bruce Fields 
339181f0b2a4SJ. Bruce Fields 	init_session(rqstp, new, conf, cr_ses);
3392d20c11d8SJeff Layton 	nfsd4_get_session_locked(new);
339381f0b2a4SJ. Bruce Fields 
3394ac7c46f2SJ. Bruce Fields 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3395ec6b5d7bSAndy Adamson 	       NFS4_MAX_SESSIONID_LEN);
339686c3e16cSJ. Bruce Fields 	cs_slot->sl_seqid++;
339749557cc7SAndy Adamson 	cr_ses->seqid = cs_slot->sl_seqid;
3398ec6b5d7bSAndy Adamson 
3399d20c11d8SJeff Layton 	/* cache solo and embedded create sessions under the client_lock */
340049557cc7SAndy Adamson 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
3401d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
3402d20c11d8SJeff Layton 	/* init connection and backchannel */
3403d20c11d8SJeff Layton 	nfsd4_init_conn(rqstp, conn, new);
3404d20c11d8SJeff Layton 	nfsd4_put_session(new);
3405d20c11d8SJeff Layton 	if (old)
3406d20c11d8SJeff Layton 		expire_client(old);
3407ec6b5d7bSAndy Adamson 	return status;
340881f0b2a4SJ. Bruce Fields out_free_conn:
3409d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
341081f0b2a4SJ. Bruce Fields 	free_conn(conn);
3411d20c11d8SJeff Layton 	if (old)
3412d20c11d8SJeff Layton 		expire_client(old);
341381f0b2a4SJ. Bruce Fields out_free_session:
341481f0b2a4SJ. Bruce Fields 	__free_session(new);
341555c760cfSJ. Bruce Fields out_release_drc_mem:
341655c760cfSJ. Bruce Fields 	nfsd4_put_drc_mem(&cr_ses->fore_channel);
34171ca50792SJ. Bruce Fields 	return status;
3418069b6ad4SAndy Adamson }
3419069b6ad4SAndy Adamson 
34201d1bc8f2SJ. Bruce Fields static __be32 nfsd4_map_bcts_dir(u32 *dir)
34211d1bc8f2SJ. Bruce Fields {
34221d1bc8f2SJ. Bruce Fields 	switch (*dir) {
34231d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE:
34241d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK:
34251d1bc8f2SJ. Bruce Fields 		return nfs_ok;
34261d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE_OR_BOTH:
34271d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK_OR_BOTH:
34281d1bc8f2SJ. Bruce Fields 		*dir = NFS4_CDFC4_BOTH;
34291d1bc8f2SJ. Bruce Fields 		return nfs_ok;
3430fc5fc5d7Szhengbin 	}
34311d1bc8f2SJ. Bruce Fields 	return nfserr_inval;
34321d1bc8f2SJ. Bruce Fields }
34331d1bc8f2SJ. Bruce Fields 
3434eb69853dSChristoph Hellwig __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3435eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
3436eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3437cb73a9f4SJ. Bruce Fields {
3438eb69853dSChristoph Hellwig 	struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3439cb73a9f4SJ. Bruce Fields 	struct nfsd4_session *session = cstate->session;
3440c9a49628SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3441b78724b7SJ. Bruce Fields 	__be32 status;
3442cb73a9f4SJ. Bruce Fields 
3443b78724b7SJ. Bruce Fields 	status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3444b78724b7SJ. Bruce Fields 	if (status)
3445b78724b7SJ. Bruce Fields 		return status;
3446c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3447cb73a9f4SJ. Bruce Fields 	session->se_cb_prog = bc->bc_cb_program;
3448cb73a9f4SJ. Bruce Fields 	session->se_cb_sec = bc->bc_cb_sec;
3449c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
3450cb73a9f4SJ. Bruce Fields 
3451cb73a9f4SJ. Bruce Fields 	nfsd4_probe_callback(session->se_client);
3452cb73a9f4SJ. Bruce Fields 
3453cb73a9f4SJ. Bruce Fields 	return nfs_ok;
3454cb73a9f4SJ. Bruce Fields }
3455cb73a9f4SJ. Bruce Fields 
34561d1bc8f2SJ. Bruce Fields __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
34571d1bc8f2SJ. Bruce Fields 		     struct nfsd4_compound_state *cstate,
3458eb69853dSChristoph Hellwig 		     union nfsd4_op_u *u)
34591d1bc8f2SJ. Bruce Fields {
3460eb69853dSChristoph Hellwig 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
34611d1bc8f2SJ. Bruce Fields 	__be32 status;
34623ba63671SJ. Bruce Fields 	struct nfsd4_conn *conn;
34634f6e6c17SJ. Bruce Fields 	struct nfsd4_session *session;
3464d4e19e70STrond Myklebust 	struct net *net = SVC_NET(rqstp);
3465d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
34661d1bc8f2SJ. Bruce Fields 
34671d1bc8f2SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
34681d1bc8f2SJ. Bruce Fields 		return nfserr_not_only_op;
3469c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3470d4e19e70STrond Myklebust 	session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3471c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
34724f6e6c17SJ. Bruce Fields 	if (!session)
3473d4e19e70STrond Myklebust 		goto out_no_session;
347457266a6eSJ. Bruce Fields 	status = nfserr_wrong_cred;
3475dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(session->se_client, rqstp))
347657266a6eSJ. Bruce Fields 		goto out;
34771d1bc8f2SJ. Bruce Fields 	status = nfsd4_map_bcts_dir(&bcts->dir);
34783ba63671SJ. Bruce Fields 	if (status)
34794f6e6c17SJ. Bruce Fields 		goto out;
34803ba63671SJ. Bruce Fields 	conn = alloc_conn(rqstp, bcts->dir);
34814f6e6c17SJ. Bruce Fields 	status = nfserr_jukebox;
34823ba63671SJ. Bruce Fields 	if (!conn)
34834f6e6c17SJ. Bruce Fields 		goto out;
34844f6e6c17SJ. Bruce Fields 	nfsd4_init_conn(rqstp, conn, session);
34854f6e6c17SJ. Bruce Fields 	status = nfs_ok;
34864f6e6c17SJ. Bruce Fields out:
3487d4e19e70STrond Myklebust 	nfsd4_put_session(session);
3488d4e19e70STrond Myklebust out_no_session:
34894f6e6c17SJ. Bruce Fields 	return status;
34901d1bc8f2SJ. Bruce Fields }
34911d1bc8f2SJ. Bruce Fields 
3492665d5072SJ. Bruce Fields static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
34935d4cec2fSJ. Bruce Fields {
3494665d5072SJ. Bruce Fields 	if (!cstate->session)
349551d87bc2SFengguang Wu 		return false;
3496665d5072SJ. Bruce Fields 	return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
34975d4cec2fSJ. Bruce Fields }
34985d4cec2fSJ. Bruce Fields 
3499069b6ad4SAndy Adamson __be32
3500eb69853dSChristoph Hellwig nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3501eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3502069b6ad4SAndy Adamson {
3503ca0552f4SJ. Bruce Fields 	struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3504e10e0cfcSBenny Halevy 	struct nfsd4_session *ses;
3505abcdff09SJ. Bruce Fields 	__be32 status;
3506f0f51f5cSJ. Bruce Fields 	int ref_held_by_me = 0;
3507d4e19e70STrond Myklebust 	struct net *net = SVC_NET(r);
3508d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3509e10e0cfcSBenny Halevy 
3510abcdff09SJ. Bruce Fields 	status = nfserr_not_only_op;
3511ca0552f4SJ. Bruce Fields 	if (nfsd4_compound_in_session(cstate, sessionid)) {
351257716355SJ. Bruce Fields 		if (!nfsd4_last_compound_op(r))
3513abcdff09SJ. Bruce Fields 			goto out;
3514f0f51f5cSJ. Bruce Fields 		ref_held_by_me++;
351557716355SJ. Bruce Fields 	}
3516ca0552f4SJ. Bruce Fields 	dump_sessionid(__func__, sessionid);
3517c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3518ca0552f4SJ. Bruce Fields 	ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3519abcdff09SJ. Bruce Fields 	if (!ses)
3520abcdff09SJ. Bruce Fields 		goto out_client_lock;
352157266a6eSJ. Bruce Fields 	status = nfserr_wrong_cred;
3522dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(ses->se_client, r))
3523d4e19e70STrond Myklebust 		goto out_put_session;
3524f0f51f5cSJ. Bruce Fields 	status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
352566b2b9b2SJ. Bruce Fields 	if (status)
3526f0f51f5cSJ. Bruce Fields 		goto out_put_session;
3527e10e0cfcSBenny Halevy 	unhash_session(ses);
3528c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
3529e10e0cfcSBenny Halevy 
353084f5f7ccSJ. Bruce Fields 	nfsd4_probe_callback_sync(ses->se_client);
353119cf5c02SJ. Bruce Fields 
3532c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3533e10e0cfcSBenny Halevy 	status = nfs_ok;
3534f0f51f5cSJ. Bruce Fields out_put_session:
3535d4e19e70STrond Myklebust 	nfsd4_put_session_locked(ses);
3536abcdff09SJ. Bruce Fields out_client_lock:
3537abcdff09SJ. Bruce Fields 	spin_unlock(&nn->client_lock);
3538e10e0cfcSBenny Halevy out:
3539e10e0cfcSBenny Halevy 	return status;
3540069b6ad4SAndy Adamson }
3541069b6ad4SAndy Adamson 
3542a663bdd8SJ. Bruce Fields static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3543328ead28SJ. Bruce Fields {
3544328ead28SJ. Bruce Fields 	struct nfsd4_conn *c;
3545328ead28SJ. Bruce Fields 
3546328ead28SJ. Bruce Fields 	list_for_each_entry(c, &s->se_conns, cn_persession) {
3547a663bdd8SJ. Bruce Fields 		if (c->cn_xprt == xpt) {
3548328ead28SJ. Bruce Fields 			return c;
3549328ead28SJ. Bruce Fields 		}
3550328ead28SJ. Bruce Fields 	}
3551328ead28SJ. Bruce Fields 	return NULL;
3552328ead28SJ. Bruce Fields }
3553328ead28SJ. Bruce Fields 
355457266a6eSJ. Bruce Fields static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3555328ead28SJ. Bruce Fields {
3556328ead28SJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
3557a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *c;
355857266a6eSJ. Bruce Fields 	__be32 status = nfs_ok;
355921b75b01SJ. Bruce Fields 	int ret;
3560328ead28SJ. Bruce Fields 
3561328ead28SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
3562a663bdd8SJ. Bruce Fields 	c = __nfsd4_find_conn(new->cn_xprt, ses);
356357266a6eSJ. Bruce Fields 	if (c)
356457266a6eSJ. Bruce Fields 		goto out_free;
356557266a6eSJ. Bruce Fields 	status = nfserr_conn_not_bound_to_session;
356657266a6eSJ. Bruce Fields 	if (clp->cl_mach_cred)
356757266a6eSJ. Bruce Fields 		goto out_free;
3568328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(new, ses);
3569328ead28SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
357021b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(new);
357121b75b01SJ. Bruce Fields 	if (ret)
357221b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
357321b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&new->cn_xpt_user);
357457266a6eSJ. Bruce Fields 	return nfs_ok;
357557266a6eSJ. Bruce Fields out_free:
357657266a6eSJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
357757266a6eSJ. Bruce Fields 	free_conn(new);
357857266a6eSJ. Bruce Fields 	return status;
3579328ead28SJ. Bruce Fields }
3580328ead28SJ. Bruce Fields 
3581868b89c3SMi Jinlong static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3582868b89c3SMi Jinlong {
3583868b89c3SMi Jinlong 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
3584868b89c3SMi Jinlong 
3585868b89c3SMi Jinlong 	return args->opcnt > session->se_fchannel.maxops;
3586868b89c3SMi Jinlong }
3587868b89c3SMi Jinlong 
3588ae82a8d0SMi Jinlong static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3589ae82a8d0SMi Jinlong 				  struct nfsd4_session *session)
3590ae82a8d0SMi Jinlong {
3591ae82a8d0SMi Jinlong 	struct xdr_buf *xb = &rqstp->rq_arg;
3592ae82a8d0SMi Jinlong 
3593ae82a8d0SMi Jinlong 	return xb->len > session->se_fchannel.maxreq_sz;
3594ae82a8d0SMi Jinlong }
3595ae82a8d0SMi Jinlong 
359653da6a53SJ. Bruce Fields static bool replay_matches_cache(struct svc_rqst *rqstp,
359753da6a53SJ. Bruce Fields 		 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
359853da6a53SJ. Bruce Fields {
359953da6a53SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
360053da6a53SJ. Bruce Fields 
360153da6a53SJ. Bruce Fields 	if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
360253da6a53SJ. Bruce Fields 	    (bool)seq->cachethis)
360353da6a53SJ. Bruce Fields 		return false;
360453da6a53SJ. Bruce Fields 	/*
36056e73e92bSScott Mayhew 	 * If there's an error then the reply can have fewer ops than
36066e73e92bSScott Mayhew 	 * the call.
360753da6a53SJ. Bruce Fields 	 */
36086e73e92bSScott Mayhew 	if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
36096e73e92bSScott Mayhew 		return false;
36106e73e92bSScott Mayhew 	/*
36116e73e92bSScott Mayhew 	 * But if we cached a reply with *more* ops than the call you're
36126e73e92bSScott Mayhew 	 * sending us now, then this new call is clearly not really a
36136e73e92bSScott Mayhew 	 * replay of the old one:
36146e73e92bSScott Mayhew 	 */
36156e73e92bSScott Mayhew 	if (slot->sl_opcnt > argp->opcnt)
361653da6a53SJ. Bruce Fields 		return false;
361753da6a53SJ. Bruce Fields 	/* This is the only check explicitly called by spec: */
361853da6a53SJ. Bruce Fields 	if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
361953da6a53SJ. Bruce Fields 		return false;
362053da6a53SJ. Bruce Fields 	/*
362153da6a53SJ. Bruce Fields 	 * There may be more comparisons we could actually do, but the
362253da6a53SJ. Bruce Fields 	 * spec doesn't require us to catch every case where the calls
362353da6a53SJ. Bruce Fields 	 * don't match (that would require caching the call as well as
362453da6a53SJ. Bruce Fields 	 * the reply), so we don't bother.
362553da6a53SJ. Bruce Fields 	 */
362653da6a53SJ. Bruce Fields 	return true;
362753da6a53SJ. Bruce Fields }
362853da6a53SJ. Bruce Fields 
3629069b6ad4SAndy Adamson __be32
3630eb69853dSChristoph Hellwig nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3631eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3632069b6ad4SAndy Adamson {
3633eb69853dSChristoph Hellwig 	struct nfsd4_sequence *seq = &u->sequence;
3634f9bb94c4SAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
363547ee5298SJ. Bruce Fields 	struct xdr_stream *xdr = &resp->xdr;
3636b85d4c01SBenny Halevy 	struct nfsd4_session *session;
3637221a6876SJ. Bruce Fields 	struct nfs4_client *clp;
3638b85d4c01SBenny Halevy 	struct nfsd4_slot *slot;
3639a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *conn;
364057b7b43bSJ. Bruce Fields 	__be32 status;
364147ee5298SJ. Bruce Fields 	int buflen;
3642d4e19e70STrond Myklebust 	struct net *net = SVC_NET(rqstp);
3643d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3644b85d4c01SBenny Halevy 
3645f9bb94c4SAndy Adamson 	if (resp->opcnt != 1)
3646f9bb94c4SAndy Adamson 		return nfserr_sequence_pos;
3647f9bb94c4SAndy Adamson 
3648a663bdd8SJ. Bruce Fields 	/*
3649a663bdd8SJ. Bruce Fields 	 * Will be either used or freed by nfsd4_sequence_check_conn
3650a663bdd8SJ. Bruce Fields 	 * below.
3651a663bdd8SJ. Bruce Fields 	 */
3652a663bdd8SJ. Bruce Fields 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3653a663bdd8SJ. Bruce Fields 	if (!conn)
3654a663bdd8SJ. Bruce Fields 		return nfserr_jukebox;
3655a663bdd8SJ. Bruce Fields 
3656c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3657d4e19e70STrond Myklebust 	session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3658b85d4c01SBenny Halevy 	if (!session)
3659221a6876SJ. Bruce Fields 		goto out_no_session;
3660221a6876SJ. Bruce Fields 	clp = session->se_client;
3661b85d4c01SBenny Halevy 
3662868b89c3SMi Jinlong 	status = nfserr_too_many_ops;
3663868b89c3SMi Jinlong 	if (nfsd4_session_too_many_ops(rqstp, session))
366466b2b9b2SJ. Bruce Fields 		goto out_put_session;
3665868b89c3SMi Jinlong 
3666ae82a8d0SMi Jinlong 	status = nfserr_req_too_big;
3667ae82a8d0SMi Jinlong 	if (nfsd4_request_too_big(rqstp, session))
366866b2b9b2SJ. Bruce Fields 		goto out_put_session;
3669ae82a8d0SMi Jinlong 
3670b85d4c01SBenny Halevy 	status = nfserr_badslot;
36716c18ba9fSAlexandros Batsakis 	if (seq->slotid >= session->se_fchannel.maxreqs)
367266b2b9b2SJ. Bruce Fields 		goto out_put_session;
3673b85d4c01SBenny Halevy 
3674557ce264SAndy Adamson 	slot = session->se_slots[seq->slotid];
3675b85d4c01SBenny Halevy 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
3676b85d4c01SBenny Halevy 
3677a8dfdaebSAndy Adamson 	/* We do not negotiate the number of slots yet, so set the
3678a8dfdaebSAndy Adamson 	 * maxslots to the session maxreqs which is used to encode
3679a8dfdaebSAndy Adamson 	 * sr_highest_slotid and the sr_target_slot id to maxslots */
3680a8dfdaebSAndy Adamson 	seq->maxslots = session->se_fchannel.maxreqs;
3681a8dfdaebSAndy Adamson 
368273e79482SJ. Bruce Fields 	status = check_slot_seqid(seq->seqid, slot->sl_seqid,
368373e79482SJ. Bruce Fields 					slot->sl_flags & NFSD4_SLOT_INUSE);
3684b85d4c01SBenny Halevy 	if (status == nfserr_replay_cache) {
3685bf5c43c8SJ. Bruce Fields 		status = nfserr_seq_misordered;
3686bf5c43c8SJ. Bruce Fields 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
368766b2b9b2SJ. Bruce Fields 			goto out_put_session;
368853da6a53SJ. Bruce Fields 		status = nfserr_seq_false_retry;
368953da6a53SJ. Bruce Fields 		if (!replay_matches_cache(rqstp, seq, slot))
369053da6a53SJ. Bruce Fields 			goto out_put_session;
3691b85d4c01SBenny Halevy 		cstate->slot = slot;
3692b85d4c01SBenny Halevy 		cstate->session = session;
36934b24ca7dSJeff Layton 		cstate->clp = clp;
3694da3846a2SAndy Adamson 		/* Return the cached reply status and set cstate->status
3695557ce264SAndy Adamson 		 * for nfsd4_proc_compound processing */
3696bf864a31SAndy Adamson 		status = nfsd4_replay_cache_entry(resp, seq);
3697da3846a2SAndy Adamson 		cstate->status = nfserr_replay_cache;
3698aaf84eb9SBenny Halevy 		goto out;
3699b85d4c01SBenny Halevy 	}
3700b85d4c01SBenny Halevy 	if (status)
370166b2b9b2SJ. Bruce Fields 		goto out_put_session;
3702b85d4c01SBenny Halevy 
370357266a6eSJ. Bruce Fields 	status = nfsd4_sequence_check_conn(conn, session);
3704a663bdd8SJ. Bruce Fields 	conn = NULL;
370557266a6eSJ. Bruce Fields 	if (status)
370657266a6eSJ. Bruce Fields 		goto out_put_session;
3707328ead28SJ. Bruce Fields 
370847ee5298SJ. Bruce Fields 	buflen = (seq->cachethis) ?
370947ee5298SJ. Bruce Fields 			session->se_fchannel.maxresp_cached :
371047ee5298SJ. Bruce Fields 			session->se_fchannel.maxresp_sz;
371147ee5298SJ. Bruce Fields 	status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
371247ee5298SJ. Bruce Fields 				    nfserr_rep_too_big;
3713a5cddc88SJ. Bruce Fields 	if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
371447ee5298SJ. Bruce Fields 		goto out_put_session;
371532aaa62eSJ. Bruce Fields 	svc_reserve(rqstp, buflen);
371647ee5298SJ. Bruce Fields 
371747ee5298SJ. Bruce Fields 	status = nfs_ok;
3718b85d4c01SBenny Halevy 	/* Success! bump slot seqid */
3719b85d4c01SBenny Halevy 	slot->sl_seqid = seq->seqid;
3720bf5c43c8SJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INUSE;
372173e79482SJ. Bruce Fields 	if (seq->cachethis)
372273e79482SJ. Bruce Fields 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3723bf5c43c8SJ. Bruce Fields 	else
3724bf5c43c8SJ. Bruce Fields 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3725b85d4c01SBenny Halevy 
3726b85d4c01SBenny Halevy 	cstate->slot = slot;
3727b85d4c01SBenny Halevy 	cstate->session = session;
37284b24ca7dSJeff Layton 	cstate->clp = clp;
3729b85d4c01SBenny Halevy 
3730b85d4c01SBenny Halevy out:
37315423732aSBenny Halevy 	switch (clp->cl_cb_state) {
37325423732aSBenny Halevy 	case NFSD4_CB_DOWN:
3733fc0c3dd1SBenny Halevy 		seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
37345423732aSBenny Halevy 		break;
37355423732aSBenny Halevy 	case NFSD4_CB_FAULT:
3736fc0c3dd1SBenny Halevy 		seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
37375423732aSBenny Halevy 		break;
3738fc0c3dd1SBenny Halevy 	default:
3739fc0c3dd1SBenny Halevy 		seq->status_flags = 0;
37405423732aSBenny Halevy 	}
37413bd64a5bSJ. Bruce Fields 	if (!list_empty(&clp->cl_revoked))
37423bd64a5bSJ. Bruce Fields 		seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3743221a6876SJ. Bruce Fields out_no_session:
37443f42d2c4SKinglong Mee 	if (conn)
37453f42d2c4SKinglong Mee 		free_conn(conn);
3746c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
3747b85d4c01SBenny Halevy 	return status;
374866b2b9b2SJ. Bruce Fields out_put_session:
3749d4e19e70STrond Myklebust 	nfsd4_put_session_locked(session);
3750221a6876SJ. Bruce Fields 	goto out_no_session;
3751069b6ad4SAndy Adamson }
3752069b6ad4SAndy Adamson 
3753b607664eSTrond Myklebust void
3754b607664eSTrond Myklebust nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3755b607664eSTrond Myklebust {
3756b607664eSTrond Myklebust 	struct nfsd4_compound_state *cs = &resp->cstate;
3757b607664eSTrond Myklebust 
3758b607664eSTrond Myklebust 	if (nfsd4_has_session(cs)) {
3759b607664eSTrond Myklebust 		if (cs->status != nfserr_replay_cache) {
3760b607664eSTrond Myklebust 			nfsd4_store_cache_entry(resp);
3761b607664eSTrond Myklebust 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3762b607664eSTrond Myklebust 		}
3763d4e19e70STrond Myklebust 		/* Drop session reference that was taken in nfsd4_sequence() */
3764b607664eSTrond Myklebust 		nfsd4_put_session(cs->session);
37654b24ca7dSJeff Layton 	} else if (cs->clp)
37664b24ca7dSJeff Layton 		put_client_renew(cs->clp);
3767b607664eSTrond Myklebust }
3768b607664eSTrond Myklebust 
3769345c2842SMi Jinlong __be32
3770eb69853dSChristoph Hellwig nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3771eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
3772eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3773345c2842SMi Jinlong {
3774eb69853dSChristoph Hellwig 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
37756b10ad19STrond Myklebust 	struct nfs4_client *conf, *unconf;
37766b10ad19STrond Myklebust 	struct nfs4_client *clp = NULL;
377757b7b43bSJ. Bruce Fields 	__be32 status = 0;
37788daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3779345c2842SMi Jinlong 
37806b10ad19STrond Myklebust 	spin_lock(&nn->client_lock);
37810a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(&dc->clientid, true, nn);
37828daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(&dc->clientid, true, nn);
378378389046SJ. Bruce Fields 	WARN_ON_ONCE(conf && unconf);
3784345c2842SMi Jinlong 
3785345c2842SMi Jinlong 	if (conf) {
3786c0293b01SJ. Bruce Fields 		if (client_has_state(conf)) {
3787345c2842SMi Jinlong 			status = nfserr_clientid_busy;
3788345c2842SMi Jinlong 			goto out;
3789345c2842SMi Jinlong 		}
3790fd699b8aSJeff Layton 		status = mark_client_expired_locked(conf);
3791fd699b8aSJeff Layton 		if (status)
3792fd699b8aSJeff Layton 			goto out;
37936b10ad19STrond Myklebust 		clp = conf;
3794345c2842SMi Jinlong 	} else if (unconf)
3795345c2842SMi Jinlong 		clp = unconf;
3796345c2842SMi Jinlong 	else {
3797345c2842SMi Jinlong 		status = nfserr_stale_clientid;
3798345c2842SMi Jinlong 		goto out;
3799345c2842SMi Jinlong 	}
3800dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(clp, rqstp)) {
38016b10ad19STrond Myklebust 		clp = NULL;
380257266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
380357266a6eSJ. Bruce Fields 		goto out;
380457266a6eSJ. Bruce Fields 	}
38056b10ad19STrond Myklebust 	unhash_client_locked(clp);
3806345c2842SMi Jinlong out:
38076b10ad19STrond Myklebust 	spin_unlock(&nn->client_lock);
38086b10ad19STrond Myklebust 	if (clp)
38096b10ad19STrond Myklebust 		expire_client(clp);
3810345c2842SMi Jinlong 	return status;
3811345c2842SMi Jinlong }
3812345c2842SMi Jinlong 
3813069b6ad4SAndy Adamson __be32
3814eb69853dSChristoph Hellwig nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3815eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
38164dc6ec00SJ. Bruce Fields {
3817eb69853dSChristoph Hellwig 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
381857b7b43bSJ. Bruce Fields 	__be32 status = 0;
3819bcecf1ccSMi Jinlong 
38204dc6ec00SJ. Bruce Fields 	if (rc->rca_one_fs) {
38214dc6ec00SJ. Bruce Fields 		if (!cstate->current_fh.fh_dentry)
38224dc6ec00SJ. Bruce Fields 			return nfserr_nofilehandle;
38234dc6ec00SJ. Bruce Fields 		/*
38244dc6ec00SJ. Bruce Fields 		 * We don't take advantage of the rca_one_fs case.
38254dc6ec00SJ. Bruce Fields 		 * That's OK, it's optional, we can safely ignore it.
38264dc6ec00SJ. Bruce Fields 		 */
38274dc6ec00SJ. Bruce Fields 		return nfs_ok;
38284dc6ec00SJ. Bruce Fields 	}
3829bcecf1ccSMi Jinlong 
3830bcecf1ccSMi Jinlong 	status = nfserr_complete_already;
3831a52d726bSJeff Layton 	if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3832a52d726bSJeff Layton 			     &cstate->session->se_client->cl_flags))
3833bcecf1ccSMi Jinlong 		goto out;
3834bcecf1ccSMi Jinlong 
3835bcecf1ccSMi Jinlong 	status = nfserr_stale_clientid;
3836bcecf1ccSMi Jinlong 	if (is_client_expired(cstate->session->se_client))
38374dc6ec00SJ. Bruce Fields 		/*
38384dc6ec00SJ. Bruce Fields 		 * The following error isn't really legal.
38394dc6ec00SJ. Bruce Fields 		 * But we only get here if the client just explicitly
38404dc6ec00SJ. Bruce Fields 		 * destroyed the client.  Surely it no longer cares what
38414dc6ec00SJ. Bruce Fields 		 * error it gets back on an operation for the dead
38424dc6ec00SJ. Bruce Fields 		 * client.
38434dc6ec00SJ. Bruce Fields 		 */
3844bcecf1ccSMi Jinlong 		goto out;
3845bcecf1ccSMi Jinlong 
3846bcecf1ccSMi Jinlong 	status = nfs_ok;
38472a4317c5SJeff Layton 	nfsd4_client_record_create(cstate->session->se_client);
3848362063a5SScott Mayhew 	inc_reclaim_complete(cstate->session->se_client);
3849bcecf1ccSMi Jinlong out:
3850bcecf1ccSMi Jinlong 	return status;
38514dc6ec00SJ. Bruce Fields }
38524dc6ec00SJ. Bruce Fields 
38534dc6ec00SJ. Bruce Fields __be32
3854b591480bSJ.Bruce Fields nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3855eb69853dSChristoph Hellwig 		  union nfsd4_op_u *u)
38561da177e4SLinus Torvalds {
3857eb69853dSChristoph Hellwig 	struct nfsd4_setclientid *setclid = &u->setclientid;
3858a084daf5SJ. Bruce Fields 	struct xdr_netobj 	clname = setclid->se_name;
38591da177e4SLinus Torvalds 	nfs4_verifier		clverifier = setclid->se_verf;
38603dbacee6STrond Myklebust 	struct nfs4_client	*conf, *new;
38613dbacee6STrond Myklebust 	struct nfs4_client	*unconf = NULL;
3862b37ad28bSAl Viro 	__be32 			status;
3863c212cecfSStanislav Kinsbursky 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3864a55370a3SNeilBrown 
38655cc40fd7STrond Myklebust 	new = create_client(clname, rqstp, &clverifier);
38665cc40fd7STrond Myklebust 	if (new == NULL)
38675cc40fd7STrond Myklebust 		return nfserr_jukebox;
386863db4632SJ. Bruce Fields 	/* Cases below refer to rfc 3530 section 14.2.33: */
38693dbacee6STrond Myklebust 	spin_lock(&nn->client_lock);
3870382a62e7SStanislav Kinsbursky 	conf = find_confirmed_client_by_name(&clname, nn);
38712b634821SJ. Bruce Fields 	if (conf && client_has_state(conf)) {
387263db4632SJ. Bruce Fields 		/* case 0: */
38731da177e4SLinus Torvalds 		status = nfserr_clid_inuse;
3874e203d506SJ. Bruce Fields 		if (clp_used_exchangeid(conf))
3875e203d506SJ. Bruce Fields 			goto out;
3876026722c2SJ. Bruce Fields 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3877dd5e3fbcSChuck Lever 			trace_nfsd_clid_inuse_err(conf);
38781da177e4SLinus Torvalds 			goto out;
38791da177e4SLinus Torvalds 		}
38801da177e4SLinus Torvalds 	}
3881a99454aaSStanislav Kinsbursky 	unconf = find_unconfirmed_client_by_name(&clname, nn);
38821da177e4SLinus Torvalds 	if (unconf)
38833dbacee6STrond Myklebust 		unhash_client_locked(unconf);
388445f56da8SChuck Lever 	/* We need to handle only case 1: probable callback update */
388541eb1670SKinglong Mee 	if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
38861da177e4SLinus Torvalds 		copy_clid(new, conf);
388741eb1670SKinglong Mee 		gen_confirm(new, nn);
388845f56da8SChuck Lever 	}
38898323c3b2SJ. Bruce Fields 	new->cl_minorversion = 0;
38906f3d772fSTakuma Umeya 	gen_callback(new, setclid, rqstp);
3891ac55fdc4SJeff Layton 	add_to_unconfirmed(new);
38921da177e4SLinus Torvalds 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
38931da177e4SLinus Torvalds 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
38941da177e4SLinus Torvalds 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
38955cc40fd7STrond Myklebust 	new = NULL;
38961da177e4SLinus Torvalds 	status = nfs_ok;
38971da177e4SLinus Torvalds out:
38983dbacee6STrond Myklebust 	spin_unlock(&nn->client_lock);
38995cc40fd7STrond Myklebust 	if (new)
39005cc40fd7STrond Myklebust 		free_client(new);
39013dbacee6STrond Myklebust 	if (unconf)
39023dbacee6STrond Myklebust 		expire_client(unconf);
39031da177e4SLinus Torvalds 	return status;
39041da177e4SLinus Torvalds }
39051da177e4SLinus Torvalds 
39061da177e4SLinus Torvalds 
3907b37ad28bSAl Viro __be32
3908b591480bSJ.Bruce Fields nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
3909b591480bSJ.Bruce Fields 			struct nfsd4_compound_state *cstate,
3910eb69853dSChristoph Hellwig 			union nfsd4_op_u *u)
39111da177e4SLinus Torvalds {
3912eb69853dSChristoph Hellwig 	struct nfsd4_setclientid_confirm *setclientid_confirm =
3913eb69853dSChristoph Hellwig 			&u->setclientid_confirm;
391421ab45a4SNeilBrown 	struct nfs4_client *conf, *unconf;
3915d20c11d8SJeff Layton 	struct nfs4_client *old = NULL;
39161da177e4SLinus Torvalds 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
39171da177e4SLinus Torvalds 	clientid_t * clid = &setclientid_confirm->sc_clientid;
3918b37ad28bSAl Viro 	__be32 status;
39197f2210faSStanislav Kinsbursky 	struct nfsd_net	*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
39201da177e4SLinus Torvalds 
39212c142baaSStanislav Kinsbursky 	if (STALE_CLIENTID(clid, nn))
39221da177e4SLinus Torvalds 		return nfserr_stale_clientid;
392321ab45a4SNeilBrown 
3924d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
39258daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(clid, false, nn);
39260a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(clid, false, nn);
3927a186e767SJ. Bruce Fields 	/*
39288695b90aSJ. Bruce Fields 	 * We try hard to give out unique clientid's, so if we get an
39298695b90aSJ. Bruce Fields 	 * attempt to confirm the same clientid with a different cred,
3930f984a7ceSJ. Bruce Fields 	 * the client may be buggy; this should never happen.
3931f984a7ceSJ. Bruce Fields 	 *
3932f984a7ceSJ. Bruce Fields 	 * Nevertheless, RFC 7530 recommends INUSE for this case:
3933a186e767SJ. Bruce Fields 	 */
3934f984a7ceSJ. Bruce Fields 	status = nfserr_clid_inuse;
39358695b90aSJ. Bruce Fields 	if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
39368695b90aSJ. Bruce Fields 		goto out;
39378695b90aSJ. Bruce Fields 	if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
39388695b90aSJ. Bruce Fields 		goto out;
393963db4632SJ. Bruce Fields 	/* cases below refer to rfc 3530 section 14.2.34: */
394090d700b7SJ. Bruce Fields 	if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
39417d22fc11SJ. Bruce Fields 		if (conf && same_verf(&confirm, &conf->cl_confirm)) {
39427d22fc11SJ. Bruce Fields 			/* case 2: probable retransmit */
394390d700b7SJ. Bruce Fields 			status = nfs_ok;
39447d22fc11SJ. Bruce Fields 		} else /* case 4: client hasn't noticed we rebooted yet? */
394590d700b7SJ. Bruce Fields 			status = nfserr_stale_clientid;
394690d700b7SJ. Bruce Fields 		goto out;
394790d700b7SJ. Bruce Fields 	}
394890d700b7SJ. Bruce Fields 	status = nfs_ok;
394990d700b7SJ. Bruce Fields 	if (conf) { /* case 1: callback update */
3950d20c11d8SJeff Layton 		old = unconf;
3951d20c11d8SJeff Layton 		unhash_client_locked(old);
39525a3c9d71SJ. Bruce Fields 		nfsd4_change_callback(conf, &unconf->cl_cb_conn);
395390d700b7SJ. Bruce Fields 	} else { /* case 3: normal case; new or rebooted client */
3954d20c11d8SJeff Layton 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3955d20c11d8SJeff Layton 		if (old) {
39562b634821SJ. Bruce Fields 			status = nfserr_clid_inuse;
39572b634821SJ. Bruce Fields 			if (client_has_state(old)
39582b634821SJ. Bruce Fields 					&& !same_creds(&unconf->cl_cred,
39592b634821SJ. Bruce Fields 							&old->cl_cred))
39602b634821SJ. Bruce Fields 				goto out;
3961d20c11d8SJeff Layton 			status = mark_client_expired_locked(old);
39627abea1e8SJeff Layton 			if (status) {
39637abea1e8SJeff Layton 				old = NULL;
3964221a6876SJ. Bruce Fields 				goto out;
3965221a6876SJ. Bruce Fields 			}
39667abea1e8SJeff Layton 		}
39671da177e4SLinus Torvalds 		move_to_confirmed(unconf);
3968d20c11d8SJeff Layton 		conf = unconf;
396908e8987cSNeilBrown 	}
3970d20c11d8SJeff Layton 	get_client_locked(conf);
3971d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
3972d20c11d8SJeff Layton 	nfsd4_probe_callback(conf);
3973d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
3974d20c11d8SJeff Layton 	put_client_renew_locked(conf);
39751da177e4SLinus Torvalds out:
3976d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
3977d20c11d8SJeff Layton 	if (old)
3978d20c11d8SJeff Layton 		expire_client(old);
39791da177e4SLinus Torvalds 	return status;
39801da177e4SLinus Torvalds }
39811da177e4SLinus Torvalds 
398232513b40SJ. Bruce Fields static struct nfs4_file *nfsd4_alloc_file(void)
39831da177e4SLinus Torvalds {
398432513b40SJ. Bruce Fields 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
398532513b40SJ. Bruce Fields }
398632513b40SJ. Bruce Fields 
398732513b40SJ. Bruce Fields /* OPEN Share state helper functions */
39885b095e99SJeff Layton static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
39895b095e99SJeff Layton 				struct nfs4_file *fp)
399032513b40SJ. Bruce Fields {
3991950e0118STrond Myklebust 	lockdep_assert_held(&state_lock);
3992950e0118STrond Myklebust 
3993818a34ebSElena Reshetova 	refcount_set(&fp->fi_ref, 1);
39941d31a253STrond Myklebust 	spin_lock_init(&fp->fi_lock);
39958beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_stateids);
39968beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_delegations);
39978287f009SSachin Bhamare 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
3998e2cf80d7STrond Myklebust 	fh_copy_shallow(&fp->fi_fhandle, fh);
39990c637be8SJeff Layton 	fp->fi_deleg_file = NULL;
400047f9940cSMeelap Shah 	fp->fi_had_conflict = false;
4001baeb4ff0SJeff Layton 	fp->fi_share_deny = 0;
4002f9d7562fSJ. Bruce Fields 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4003f9d7562fSJ. Bruce Fields 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
40049cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
40059cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&fp->fi_lo_states);
4006c5c707f9SChristoph Hellwig 	atomic_set(&fp->fi_lo_recalls, 0);
40079cf514ccSChristoph Hellwig #endif
40085b095e99SJeff Layton 	hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
40091da177e4SLinus Torvalds }
40101da177e4SLinus Torvalds 
4011e8ff2a84SJ. Bruce Fields void
4012e60d4398SNeilBrown nfsd4_free_slabs(void)
4013e60d4398SNeilBrown {
40149258a2d5SJeff Layton 	kmem_cache_destroy(client_slab);
4015abf1135bSChristoph Hellwig 	kmem_cache_destroy(openowner_slab);
4016abf1135bSChristoph Hellwig 	kmem_cache_destroy(lockowner_slab);
4017abf1135bSChristoph Hellwig 	kmem_cache_destroy(file_slab);
4018abf1135bSChristoph Hellwig 	kmem_cache_destroy(stateid_slab);
4019abf1135bSChristoph Hellwig 	kmem_cache_destroy(deleg_slab);
40209258a2d5SJeff Layton 	kmem_cache_destroy(odstate_slab);
4021e60d4398SNeilBrown }
40221da177e4SLinus Torvalds 
402372083396SBryan Schumaker int
40241da177e4SLinus Torvalds nfsd4_init_slabs(void)
40251da177e4SLinus Torvalds {
40269258a2d5SJeff Layton 	client_slab = kmem_cache_create("nfsd4_clients",
40279258a2d5SJeff Layton 			sizeof(struct nfs4_client), 0, 0, NULL);
40289258a2d5SJeff Layton 	if (client_slab == NULL)
40299258a2d5SJeff Layton 		goto out;
4030fe0750e5SJ. Bruce Fields 	openowner_slab = kmem_cache_create("nfsd4_openowners",
4031fe0750e5SJ. Bruce Fields 			sizeof(struct nfs4_openowner), 0, 0, NULL);
4032fe0750e5SJ. Bruce Fields 	if (openowner_slab == NULL)
40339258a2d5SJeff Layton 		goto out_free_client_slab;
4034fe0750e5SJ. Bruce Fields 	lockowner_slab = kmem_cache_create("nfsd4_lockowners",
40353c40794bSYanchuan Nian 			sizeof(struct nfs4_lockowner), 0, 0, NULL);
4036fe0750e5SJ. Bruce Fields 	if (lockowner_slab == NULL)
4037abf1135bSChristoph Hellwig 		goto out_free_openowner_slab;
4038e60d4398SNeilBrown 	file_slab = kmem_cache_create("nfsd4_files",
403920c2df83SPaul Mundt 			sizeof(struct nfs4_file), 0, 0, NULL);
4040e60d4398SNeilBrown 	if (file_slab == NULL)
4041abf1135bSChristoph Hellwig 		goto out_free_lockowner_slab;
40425ac049acSNeilBrown 	stateid_slab = kmem_cache_create("nfsd4_stateids",
4043dcef0413SJ. Bruce Fields 			sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
40445ac049acSNeilBrown 	if (stateid_slab == NULL)
4045abf1135bSChristoph Hellwig 		goto out_free_file_slab;
40465b2d21c1SNeilBrown 	deleg_slab = kmem_cache_create("nfsd4_delegations",
404720c2df83SPaul Mundt 			sizeof(struct nfs4_delegation), 0, 0, NULL);
40485b2d21c1SNeilBrown 	if (deleg_slab == NULL)
4049abf1135bSChristoph Hellwig 		goto out_free_stateid_slab;
40508287f009SSachin Bhamare 	odstate_slab = kmem_cache_create("nfsd4_odstate",
40518287f009SSachin Bhamare 			sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
40528287f009SSachin Bhamare 	if (odstate_slab == NULL)
40538287f009SSachin Bhamare 		goto out_free_deleg_slab;
4054e60d4398SNeilBrown 	return 0;
4055abf1135bSChristoph Hellwig 
40568287f009SSachin Bhamare out_free_deleg_slab:
40578287f009SSachin Bhamare 	kmem_cache_destroy(deleg_slab);
4058abf1135bSChristoph Hellwig out_free_stateid_slab:
4059abf1135bSChristoph Hellwig 	kmem_cache_destroy(stateid_slab);
4060abf1135bSChristoph Hellwig out_free_file_slab:
4061abf1135bSChristoph Hellwig 	kmem_cache_destroy(file_slab);
4062abf1135bSChristoph Hellwig out_free_lockowner_slab:
4063abf1135bSChristoph Hellwig 	kmem_cache_destroy(lockowner_slab);
4064abf1135bSChristoph Hellwig out_free_openowner_slab:
4065abf1135bSChristoph Hellwig 	kmem_cache_destroy(openowner_slab);
40669258a2d5SJeff Layton out_free_client_slab:
40679258a2d5SJeff Layton 	kmem_cache_destroy(client_slab);
4068abf1135bSChristoph Hellwig out:
40691da177e4SLinus Torvalds 	return -ENOMEM;
40701da177e4SLinus Torvalds }
40711da177e4SLinus Torvalds 
4072ff194bd9SJ. Bruce Fields static void init_nfs4_replay(struct nfs4_replay *rp)
4073ff194bd9SJ. Bruce Fields {
4074ff194bd9SJ. Bruce Fields 	rp->rp_status = nfserr_serverfault;
4075ff194bd9SJ. Bruce Fields 	rp->rp_buflen = 0;
4076ff194bd9SJ. Bruce Fields 	rp->rp_buf = rp->rp_ibuf;
407758fb12e6SJeff Layton 	mutex_init(&rp->rp_mutex);
407858fb12e6SJeff Layton }
407958fb12e6SJeff Layton 
408058fb12e6SJeff Layton static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
408158fb12e6SJeff Layton 		struct nfs4_stateowner *so)
408258fb12e6SJeff Layton {
408358fb12e6SJeff Layton 	if (!nfsd4_has_session(cstate)) {
408458fb12e6SJeff Layton 		mutex_lock(&so->so_replay.rp_mutex);
4085b5971afaSKinglong Mee 		cstate->replay_owner = nfs4_get_stateowner(so);
408658fb12e6SJeff Layton 	}
408758fb12e6SJeff Layton }
408858fb12e6SJeff Layton 
408958fb12e6SJeff Layton void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
409058fb12e6SJeff Layton {
409158fb12e6SJeff Layton 	struct nfs4_stateowner *so = cstate->replay_owner;
409258fb12e6SJeff Layton 
409358fb12e6SJeff Layton 	if (so != NULL) {
409458fb12e6SJeff Layton 		cstate->replay_owner = NULL;
409558fb12e6SJeff Layton 		mutex_unlock(&so->so_replay.rp_mutex);
409658fb12e6SJeff Layton 		nfs4_put_stateowner(so);
409758fb12e6SJeff Layton 	}
4098ff194bd9SJ. Bruce Fields }
4099ff194bd9SJ. Bruce Fields 
4100fe0750e5SJ. Bruce Fields static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
41011da177e4SLinus Torvalds {
41021da177e4SLinus Torvalds 	struct nfs4_stateowner *sop;
41031da177e4SLinus Torvalds 
4104fe0750e5SJ. Bruce Fields 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
4105ff194bd9SJ. Bruce Fields 	if (!sop)
4106ff194bd9SJ. Bruce Fields 		return NULL;
4107ff194bd9SJ. Bruce Fields 
41086f4859b8SJ. Bruce Fields 	xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4109ff194bd9SJ. Bruce Fields 	if (!sop->so_owner.data) {
4110fe0750e5SJ. Bruce Fields 		kmem_cache_free(slab, sop);
4111ff194bd9SJ. Bruce Fields 		return NULL;
4112ff194bd9SJ. Bruce Fields 	}
4113ff194bd9SJ. Bruce Fields 
4114ff194bd9SJ. Bruce Fields 	INIT_LIST_HEAD(&sop->so_stateids);
4115ff194bd9SJ. Bruce Fields 	sop->so_client = clp;
4116ff194bd9SJ. Bruce Fields 	init_nfs4_replay(&sop->so_replay);
41176b180f0bSJeff Layton 	atomic_set(&sop->so_count, 1);
41181da177e4SLinus Torvalds 	return sop;
41191da177e4SLinus Torvalds }
4120ff194bd9SJ. Bruce Fields 
4121fe0750e5SJ. Bruce Fields static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4122ff194bd9SJ. Bruce Fields {
4123d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
41249b531137SStanislav Kinsbursky 
4125d4f0489fSTrond Myklebust 	list_add(&oo->oo_owner.so_strhash,
4126d4f0489fSTrond Myklebust 		 &clp->cl_ownerstr_hashtbl[strhashval]);
4127fe0750e5SJ. Bruce Fields 	list_add(&oo->oo_perclient, &clp->cl_openowners);
41281da177e4SLinus Torvalds }
41291da177e4SLinus Torvalds 
41308f4b54c5SJeff Layton static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
41318f4b54c5SJeff Layton {
4132d4f0489fSTrond Myklebust 	unhash_openowner_locked(openowner(so));
41338f4b54c5SJeff Layton }
41348f4b54c5SJeff Layton 
41356b180f0bSJeff Layton static void nfs4_free_openowner(struct nfs4_stateowner *so)
41366b180f0bSJeff Layton {
41376b180f0bSJeff Layton 	struct nfs4_openowner *oo = openowner(so);
41386b180f0bSJeff Layton 
41396b180f0bSJeff Layton 	kmem_cache_free(openowner_slab, oo);
41406b180f0bSJeff Layton }
41416b180f0bSJeff Layton 
41426b180f0bSJeff Layton static const struct nfs4_stateowner_operations openowner_ops = {
41438f4b54c5SJeff Layton 	.so_unhash =	nfs4_unhash_openowner,
41446b180f0bSJeff Layton 	.so_free =	nfs4_free_openowner,
41456b180f0bSJeff Layton };
41466b180f0bSJeff Layton 
41477fc0564eSAndrew Elble static struct nfs4_ol_stateid *
41487fc0564eSAndrew Elble nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
41497fc0564eSAndrew Elble {
41507fc0564eSAndrew Elble 	struct nfs4_ol_stateid *local, *ret = NULL;
41517fc0564eSAndrew Elble 	struct nfs4_openowner *oo = open->op_openowner;
41527fc0564eSAndrew Elble 
41537fc0564eSAndrew Elble 	lockdep_assert_held(&fp->fi_lock);
41547fc0564eSAndrew Elble 
41557fc0564eSAndrew Elble 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
41567fc0564eSAndrew Elble 		/* ignore lock owners */
41577fc0564eSAndrew Elble 		if (local->st_stateowner->so_is_open_owner == 0)
41587fc0564eSAndrew Elble 			continue;
415915ca08d3STrond Myklebust 		if (local->st_stateowner != &oo->oo_owner)
416015ca08d3STrond Myklebust 			continue;
416115ca08d3STrond Myklebust 		if (local->st_stid.sc_type == NFS4_OPEN_STID) {
41627fc0564eSAndrew Elble 			ret = local;
4163a15dfcd5SElena Reshetova 			refcount_inc(&ret->st_stid.sc_count);
41647fc0564eSAndrew Elble 			break;
41657fc0564eSAndrew Elble 		}
41667fc0564eSAndrew Elble 	}
41677fc0564eSAndrew Elble 	return ret;
41687fc0564eSAndrew Elble }
41697fc0564eSAndrew Elble 
417015ca08d3STrond Myklebust static __be32
417115ca08d3STrond Myklebust nfsd4_verify_open_stid(struct nfs4_stid *s)
417215ca08d3STrond Myklebust {
417315ca08d3STrond Myklebust 	__be32 ret = nfs_ok;
417415ca08d3STrond Myklebust 
417515ca08d3STrond Myklebust 	switch (s->sc_type) {
417615ca08d3STrond Myklebust 	default:
417715ca08d3STrond Myklebust 		break;
41784f176417STrond Myklebust 	case 0:
417915ca08d3STrond Myklebust 	case NFS4_CLOSED_STID:
418015ca08d3STrond Myklebust 	case NFS4_CLOSED_DELEG_STID:
418115ca08d3STrond Myklebust 		ret = nfserr_bad_stateid;
418215ca08d3STrond Myklebust 		break;
418315ca08d3STrond Myklebust 	case NFS4_REVOKED_DELEG_STID:
418415ca08d3STrond Myklebust 		ret = nfserr_deleg_revoked;
418515ca08d3STrond Myklebust 	}
418615ca08d3STrond Myklebust 	return ret;
418715ca08d3STrond Myklebust }
418815ca08d3STrond Myklebust 
418915ca08d3STrond Myklebust /* Lock the stateid st_mutex, and deal with races with CLOSE */
419015ca08d3STrond Myklebust static __be32
419115ca08d3STrond Myklebust nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
419215ca08d3STrond Myklebust {
419315ca08d3STrond Myklebust 	__be32 ret;
419415ca08d3STrond Myklebust 
41954f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
419615ca08d3STrond Myklebust 	ret = nfsd4_verify_open_stid(&stp->st_stid);
419715ca08d3STrond Myklebust 	if (ret != nfs_ok)
419815ca08d3STrond Myklebust 		mutex_unlock(&stp->st_mutex);
419915ca08d3STrond Myklebust 	return ret;
420015ca08d3STrond Myklebust }
420115ca08d3STrond Myklebust 
420215ca08d3STrond Myklebust static struct nfs4_ol_stateid *
420315ca08d3STrond Myklebust nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
420415ca08d3STrond Myklebust {
420515ca08d3STrond Myklebust 	struct nfs4_ol_stateid *stp;
420615ca08d3STrond Myklebust 	for (;;) {
420715ca08d3STrond Myklebust 		spin_lock(&fp->fi_lock);
420815ca08d3STrond Myklebust 		stp = nfsd4_find_existing_open(fp, open);
420915ca08d3STrond Myklebust 		spin_unlock(&fp->fi_lock);
421015ca08d3STrond Myklebust 		if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
421115ca08d3STrond Myklebust 			break;
421215ca08d3STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
421315ca08d3STrond Myklebust 	}
421415ca08d3STrond Myklebust 	return stp;
421515ca08d3STrond Myklebust }
421615ca08d3STrond Myklebust 
4217fe0750e5SJ. Bruce Fields static struct nfs4_openowner *
421813d6f66bSTrond Myklebust alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4219db24b3b4SJeff Layton 			   struct nfsd4_compound_state *cstate)
4220db24b3b4SJeff Layton {
422113d6f66bSTrond Myklebust 	struct nfs4_client *clp = cstate->clp;
42227ffb5880STrond Myklebust 	struct nfs4_openowner *oo, *ret;
42231da177e4SLinus Torvalds 
4224fe0750e5SJ. Bruce Fields 	oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4225fe0750e5SJ. Bruce Fields 	if (!oo)
42261da177e4SLinus Torvalds 		return NULL;
42276b180f0bSJeff Layton 	oo->oo_owner.so_ops = &openowner_ops;
4228fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_is_open_owner = 1;
4229fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_seqid = open->op_seqid;
4230d3134b10SJeff Layton 	oo->oo_flags = 0;
4231db24b3b4SJeff Layton 	if (nfsd4_has_session(cstate))
4232db24b3b4SJeff Layton 		oo->oo_flags |= NFS4_OO_CONFIRMED;
4233fe0750e5SJ. Bruce Fields 	oo->oo_time = 0;
423438c387b5SJ. Bruce Fields 	oo->oo_last_closed_stid = NULL;
4235fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&oo->oo_close_lru);
4236d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
4237d4f0489fSTrond Myklebust 	ret = find_openstateowner_str_locked(strhashval, open, clp);
42387ffb5880STrond Myklebust 	if (ret == NULL) {
4239fe0750e5SJ. Bruce Fields 		hash_openowner(oo, clp, strhashval);
42407ffb5880STrond Myklebust 		ret = oo;
42417ffb5880STrond Myklebust 	} else
4242d50ffdedSKinglong Mee 		nfs4_free_stateowner(&oo->oo_owner);
4243d50ffdedSKinglong Mee 
4244d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
4245c5952338SJeff Layton 	return ret;
42461da177e4SLinus Torvalds }
42471da177e4SLinus Torvalds 
42487fc0564eSAndrew Elble static struct nfs4_ol_stateid *
42498c7245abSOleg Drokin init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
42507fc0564eSAndrew Elble {
42511da177e4SLinus Torvalds 
42527fc0564eSAndrew Elble 	struct nfs4_openowner *oo = open->op_openowner;
42537fc0564eSAndrew Elble 	struct nfs4_ol_stateid *retstp = NULL;
42548c7245abSOleg Drokin 	struct nfs4_ol_stateid *stp;
42557fc0564eSAndrew Elble 
42568c7245abSOleg Drokin 	stp = open->op_stp;
42575cc1fb2aSOleg Drokin 	/* We are moving these outside of the spinlocks to avoid the warnings */
42585cc1fb2aSOleg Drokin 	mutex_init(&stp->st_mutex);
42594f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
42605cc1fb2aSOleg Drokin 
426115ca08d3STrond Myklebust retry:
42627fc0564eSAndrew Elble 	spin_lock(&oo->oo_owner.so_client->cl_lock);
42637fc0564eSAndrew Elble 	spin_lock(&fp->fi_lock);
42647fc0564eSAndrew Elble 
42657fc0564eSAndrew Elble 	retstp = nfsd4_find_existing_open(fp, open);
42667fc0564eSAndrew Elble 	if (retstp)
42677fc0564eSAndrew Elble 		goto out_unlock;
42688c7245abSOleg Drokin 
42698c7245abSOleg Drokin 	open->op_stp = NULL;
4270a15dfcd5SElena Reshetova 	refcount_inc(&stp->st_stid.sc_count);
42713abdb607SJ. Bruce Fields 	stp->st_stid.sc_type = NFS4_OPEN_STID;
42723c87b9b7STrond Myklebust 	INIT_LIST_HEAD(&stp->st_locks);
4273b5971afaSKinglong Mee 	stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
427413cd2184SNeilBrown 	get_nfs4_file(fp);
427511b9164aSTrond Myklebust 	stp->st_stid.sc_file = fp;
42761da177e4SLinus Torvalds 	stp->st_access_bmap = 0;
42771da177e4SLinus Torvalds 	stp->st_deny_bmap = 0;
42784c4cd222SNeilBrown 	stp->st_openstp = NULL;
42791c755dc1SJeff Layton 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
42801d31a253STrond Myklebust 	list_add(&stp->st_perfile, &fp->fi_stateids);
42817fc0564eSAndrew Elble 
42827fc0564eSAndrew Elble out_unlock:
42831d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
42841c755dc1SJeff Layton 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
42855cc1fb2aSOleg Drokin 	if (retstp) {
428615ca08d3STrond Myklebust 		/* Handle races with CLOSE */
428715ca08d3STrond Myklebust 		if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
428815ca08d3STrond Myklebust 			nfs4_put_stid(&retstp->st_stid);
428915ca08d3STrond Myklebust 			goto retry;
429015ca08d3STrond Myklebust 		}
42918c7245abSOleg Drokin 		/* To keep mutex tracking happy */
42925cc1fb2aSOleg Drokin 		mutex_unlock(&stp->st_mutex);
42938c7245abSOleg Drokin 		stp = retstp;
42945cc1fb2aSOleg Drokin 	}
42958c7245abSOleg Drokin 	return stp;
42961da177e4SLinus Torvalds }
42971da177e4SLinus Torvalds 
4298d3134b10SJeff Layton /*
4299d3134b10SJeff Layton  * In the 4.0 case we need to keep the owners around a little while to handle
4300d3134b10SJeff Layton  * CLOSE replay. We still do need to release any file access that is held by
4301d3134b10SJeff Layton  * them before returning however.
4302d3134b10SJeff Layton  */
43031da177e4SLinus Torvalds static void
4304d3134b10SJeff Layton move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
43051da177e4SLinus Torvalds {
4306217526e7SJeff Layton 	struct nfs4_ol_stateid *last;
4307d3134b10SJeff Layton 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
4308d3134b10SJeff Layton 	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4309d3134b10SJeff Layton 						nfsd_net_id);
431073758fedSStanislav Kinsbursky 
4311fe0750e5SJ. Bruce Fields 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
43121da177e4SLinus Torvalds 
4313b401be22SJeff Layton 	/*
4314b401be22SJeff Layton 	 * We know that we hold one reference via nfsd4_close, and another
4315b401be22SJeff Layton 	 * "persistent" reference for the client. If the refcount is higher
4316b401be22SJeff Layton 	 * than 2, then there are still calls in progress that are using this
4317b401be22SJeff Layton 	 * stateid. We can't put the sc_file reference until they are finished.
4318b401be22SJeff Layton 	 * Wait for the refcount to drop to 2. Since it has been unhashed,
4319b401be22SJeff Layton 	 * there should be no danger of the refcount going back up again at
4320b401be22SJeff Layton 	 * this point.
4321b401be22SJeff Layton 	 */
4322a15dfcd5SElena Reshetova 	wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4323b401be22SJeff Layton 
4324d3134b10SJeff Layton 	release_all_access(s);
4325d3134b10SJeff Layton 	if (s->st_stid.sc_file) {
4326d3134b10SJeff Layton 		put_nfs4_file(s->st_stid.sc_file);
4327d3134b10SJeff Layton 		s->st_stid.sc_file = NULL;
4328d3134b10SJeff Layton 	}
4329217526e7SJeff Layton 
4330217526e7SJeff Layton 	spin_lock(&nn->client_lock);
4331217526e7SJeff Layton 	last = oo->oo_last_closed_stid;
4332d3134b10SJeff Layton 	oo->oo_last_closed_stid = s;
433373758fedSStanislav Kinsbursky 	list_move_tail(&oo->oo_close_lru, &nn->close_lru);
433420b7d86fSArnd Bergmann 	oo->oo_time = ktime_get_boottime_seconds();
4335217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
4336217526e7SJeff Layton 	if (last)
4337217526e7SJeff Layton 		nfs4_put_stid(&last->st_stid);
43381da177e4SLinus Torvalds }
43391da177e4SLinus Torvalds 
43401da177e4SLinus Torvalds /* search file_hashtbl[] for file */
43411da177e4SLinus Torvalds static struct nfs4_file *
43425b095e99SJeff Layton find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
43431da177e4SLinus Torvalds {
43441da177e4SLinus Torvalds 	struct nfs4_file *fp;
43451da177e4SLinus Torvalds 
434636a80491SMadhuparna Bhowmik 	hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
434736a80491SMadhuparna Bhowmik 				lockdep_is_held(&state_lock)) {
43484d94c2efSChristoph Hellwig 		if (fh_match(&fp->fi_fhandle, fh)) {
4349818a34ebSElena Reshetova 			if (refcount_inc_not_zero(&fp->fi_ref))
43501da177e4SLinus Torvalds 				return fp;
43511da177e4SLinus Torvalds 		}
435213cd2184SNeilBrown 	}
43531da177e4SLinus Torvalds 	return NULL;
43541da177e4SLinus Torvalds }
43551da177e4SLinus Torvalds 
4356e6ba76e1SChristoph Hellwig struct nfs4_file *
4357ca943217STrond Myklebust find_file(struct knfsd_fh *fh)
4358950e0118STrond Myklebust {
4359950e0118STrond Myklebust 	struct nfs4_file *fp;
43605b095e99SJeff Layton 	unsigned int hashval = file_hashval(fh);
4361950e0118STrond Myklebust 
43625b095e99SJeff Layton 	rcu_read_lock();
43635b095e99SJeff Layton 	fp = find_file_locked(fh, hashval);
43645b095e99SJeff Layton 	rcu_read_unlock();
4365950e0118STrond Myklebust 	return fp;
4366950e0118STrond Myklebust }
4367950e0118STrond Myklebust 
4368950e0118STrond Myklebust static struct nfs4_file *
4369f9c00c3aSJeff Layton find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4370950e0118STrond Myklebust {
4371950e0118STrond Myklebust 	struct nfs4_file *fp;
43725b095e99SJeff Layton 	unsigned int hashval = file_hashval(fh);
43735b095e99SJeff Layton 
43745b095e99SJeff Layton 	rcu_read_lock();
43755b095e99SJeff Layton 	fp = find_file_locked(fh, hashval);
43765b095e99SJeff Layton 	rcu_read_unlock();
43775b095e99SJeff Layton 	if (fp)
43785b095e99SJeff Layton 		return fp;
4379950e0118STrond Myklebust 
4380950e0118STrond Myklebust 	spin_lock(&state_lock);
43815b095e99SJeff Layton 	fp = find_file_locked(fh, hashval);
43825b095e99SJeff Layton 	if (likely(fp == NULL)) {
43835b095e99SJeff Layton 		nfsd4_init_file(fh, hashval, new);
4384950e0118STrond Myklebust 		fp = new;
4385950e0118STrond Myklebust 	}
4386950e0118STrond Myklebust 	spin_unlock(&state_lock);
4387950e0118STrond Myklebust 
4388950e0118STrond Myklebust 	return fp;
4389950e0118STrond Myklebust }
4390950e0118STrond Myklebust 
43914f83aa30SJ. Bruce Fields /*
43921da177e4SLinus Torvalds  * Called to check deny when READ with all zero stateid or
43931da177e4SLinus Torvalds  * WRITE with all zero or all one stateid
43941da177e4SLinus Torvalds  */
4395b37ad28bSAl Viro static __be32
43961da177e4SLinus Torvalds nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
43971da177e4SLinus Torvalds {
43981da177e4SLinus Torvalds 	struct nfs4_file *fp;
4399baeb4ff0SJeff Layton 	__be32 ret = nfs_ok;
44001da177e4SLinus Torvalds 
4401ca943217STrond Myklebust 	fp = find_file(&current_fh->fh_handle);
440213cd2184SNeilBrown 	if (!fp)
4403baeb4ff0SJeff Layton 		return ret;
4404baeb4ff0SJeff Layton 	/* Check for conflicting share reservations */
44051d31a253STrond Myklebust 	spin_lock(&fp->fi_lock);
4406baeb4ff0SJeff Layton 	if (fp->fi_share_deny & deny_type)
4407baeb4ff0SJeff Layton 		ret = nfserr_locked;
44081d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
440913cd2184SNeilBrown 	put_nfs4_file(fp);
441013cd2184SNeilBrown 	return ret;
44111da177e4SLinus Torvalds }
44121da177e4SLinus Torvalds 
44130162ac2bSChristoph Hellwig static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
44141da177e4SLinus Torvalds {
44150162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
441611b9164aSTrond Myklebust 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
441711b9164aSTrond Myklebust 					  nfsd_net_id);
4418e8c69d17SJ. Bruce Fields 
441911b9164aSTrond Myklebust 	block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4420f54fe962SJeff Layton 
442102e1215fSJeff Layton 	/*
442202e1215fSJeff Layton 	 * We can't do this in nfsd_break_deleg_cb because it is
4423f54fe962SJeff Layton 	 * already holding inode->i_lock.
4424f54fe962SJeff Layton 	 *
4425dff1399fSJeff Layton 	 * If the dl_time != 0, then we know that it has already been
4426dff1399fSJeff Layton 	 * queued for a lease break. Don't queue it again.
4427dff1399fSJeff Layton 	 */
4428f54fe962SJeff Layton 	spin_lock(&state_lock);
4429dff1399fSJeff Layton 	if (dp->dl_time == 0) {
443020b7d86fSArnd Bergmann 		dp->dl_time = ktime_get_boottime_seconds();
443102e1215fSJeff Layton 		list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
443202e1215fSJeff Layton 	}
443302e1215fSJeff Layton 	spin_unlock(&state_lock);
4434dff1399fSJeff Layton }
44351da177e4SLinus Torvalds 
44360162ac2bSChristoph Hellwig static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
44370162ac2bSChristoph Hellwig 		struct rpc_task *task)
44380162ac2bSChristoph Hellwig {
44390162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
44400162ac2bSChristoph Hellwig 
4441a457974fSAndrew Elble 	if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
4442a457974fSAndrew Elble 	        return 1;
4443a457974fSAndrew Elble 
44440162ac2bSChristoph Hellwig 	switch (task->tk_status) {
44450162ac2bSChristoph Hellwig 	case 0:
44460162ac2bSChristoph Hellwig 		return 1;
44471c73b9d2SScott Mayhew 	case -NFS4ERR_DELAY:
44481c73b9d2SScott Mayhew 		rpc_delay(task, 2 * HZ);
44491c73b9d2SScott Mayhew 		return 0;
44500162ac2bSChristoph Hellwig 	case -EBADHANDLE:
44510162ac2bSChristoph Hellwig 	case -NFS4ERR_BAD_STATEID:
44520162ac2bSChristoph Hellwig 		/*
44530162ac2bSChristoph Hellwig 		 * Race: client probably got cb_recall before open reply
44540162ac2bSChristoph Hellwig 		 * granting delegation.
44550162ac2bSChristoph Hellwig 		 */
44560162ac2bSChristoph Hellwig 		if (dp->dl_retries--) {
44570162ac2bSChristoph Hellwig 			rpc_delay(task, 2 * HZ);
44580162ac2bSChristoph Hellwig 			return 0;
44590162ac2bSChristoph Hellwig 		}
44600162ac2bSChristoph Hellwig 		/*FALLTHRU*/
44610162ac2bSChristoph Hellwig 	default:
44621c73b9d2SScott Mayhew 		return 1;
44630162ac2bSChristoph Hellwig 	}
44640162ac2bSChristoph Hellwig }
44650162ac2bSChristoph Hellwig 
44660162ac2bSChristoph Hellwig static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
44670162ac2bSChristoph Hellwig {
44680162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
44690162ac2bSChristoph Hellwig 
44700162ac2bSChristoph Hellwig 	nfs4_put_stid(&dp->dl_stid);
44710162ac2bSChristoph Hellwig }
44720162ac2bSChristoph Hellwig 
4473c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
44740162ac2bSChristoph Hellwig 	.prepare	= nfsd4_cb_recall_prepare,
44750162ac2bSChristoph Hellwig 	.done		= nfsd4_cb_recall_done,
44760162ac2bSChristoph Hellwig 	.release	= nfsd4_cb_recall_release,
44770162ac2bSChristoph Hellwig };
44780162ac2bSChristoph Hellwig 
447902e1215fSJeff Layton static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
448002e1215fSJeff Layton {
448102e1215fSJeff Layton 	/*
448202e1215fSJeff Layton 	 * We're assuming the state code never drops its reference
448302e1215fSJeff Layton 	 * without first removing the lease.  Since we're in this lease
44844a269efbSJ. Bruce Fields 	 * callback (and since the lease code is serialized by the
44854a269efbSJ. Bruce Fields 	 * i_lock) we know the server hasn't removed the lease yet, and
44864a269efbSJ. Bruce Fields 	 * we know it's safe to take a reference.
448702e1215fSJeff Layton 	 */
4488a15dfcd5SElena Reshetova 	refcount_inc(&dp->dl_stid.sc_count);
4489f0b5de1bSChristoph Hellwig 	nfsd4_run_cb(&dp->dl_recall);
44906b57d9c8SJ. Bruce Fields }
44916b57d9c8SJ. Bruce Fields 
44921c8c601aSJeff Layton /* Called from break_lease() with i_lock held. */
44934d01b7f5SJeff Layton static bool
44944d01b7f5SJeff Layton nfsd_break_deleg_cb(struct file_lock *fl)
44956b57d9c8SJ. Bruce Fields {
44964d01b7f5SJeff Layton 	bool ret = false;
4497653e514eSJ. Bruce Fields 	struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4498653e514eSJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_stid.sc_file;
44996b57d9c8SJ. Bruce Fields 
4500dd5e3fbcSChuck Lever 	trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4501dd5e3fbcSChuck Lever 
45020272e1fdSJ. Bruce Fields 	/*
45030272e1fdSJ. Bruce Fields 	 * We don't want the locks code to timeout the lease for us;
4504acfdf5c3SJ. Bruce Fields 	 * we'll remove it ourself if a delegation isn't returned
45056b57d9c8SJ. Bruce Fields 	 * in time:
45060272e1fdSJ. Bruce Fields 	 */
45070272e1fdSJ. Bruce Fields 	fl->fl_break_time = 0;
45081da177e4SLinus Torvalds 
450902e1215fSJeff Layton 	spin_lock(&fp->fi_lock);
4510417c6629SJeff Layton 	fp->fi_had_conflict = true;
45115d926e8cSJ. Bruce Fields 	nfsd_break_one_deleg(dp);
451202e1215fSJeff Layton 	spin_unlock(&fp->fi_lock);
45134d01b7f5SJeff Layton 	return ret;
45141da177e4SLinus Torvalds }
45151da177e4SLinus Torvalds 
4516c45198edSJeff Layton static int
45177448cc37SJeff Layton nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
45187448cc37SJeff Layton 		     struct list_head *dispose)
45191da177e4SLinus Torvalds {
45201da177e4SLinus Torvalds 	if (arg & F_UNLCK)
4521c45198edSJeff Layton 		return lease_modify(onlist, arg, dispose);
45221da177e4SLinus Torvalds 	else
45231da177e4SLinus Torvalds 		return -EAGAIN;
45241da177e4SLinus Torvalds }
45251da177e4SLinus Torvalds 
45267b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_lease_mng_ops = {
45278fb47a4fSJ. Bruce Fields 	.lm_break = nfsd_break_deleg_cb,
45288fb47a4fSJ. Bruce Fields 	.lm_change = nfsd_change_deleg_cb,
45291da177e4SLinus Torvalds };
45301da177e4SLinus Torvalds 
45317a8711c9SJ. Bruce Fields static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
45327a8711c9SJ. Bruce Fields {
45337a8711c9SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
45347a8711c9SJ. Bruce Fields 		return nfs_ok;
45357a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid - 1)
45367a8711c9SJ. Bruce Fields 		return nfserr_replay_me;
45377a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid)
45387a8711c9SJ. Bruce Fields 		return nfs_ok;
45397a8711c9SJ. Bruce Fields 	return nfserr_bad_seqid;
45407a8711c9SJ. Bruce Fields }
45411da177e4SLinus Torvalds 
45424b24ca7dSJeff Layton static __be32 lookup_clientid(clientid_t *clid,
45434b24ca7dSJeff Layton 		struct nfsd4_compound_state *cstate,
4544b7342204SOlga Kornievskaia 		struct nfsd_net *nn,
4545b7342204SOlga Kornievskaia 		bool sessions)
45464b24ca7dSJeff Layton {
45474b24ca7dSJeff Layton 	struct nfs4_client *found;
45484b24ca7dSJeff Layton 
45494b24ca7dSJeff Layton 	if (cstate->clp) {
45504b24ca7dSJeff Layton 		found = cstate->clp;
45514b24ca7dSJeff Layton 		if (!same_clid(&found->cl_clientid, clid))
45524b24ca7dSJeff Layton 			return nfserr_stale_clientid;
45534b24ca7dSJeff Layton 		return nfs_ok;
45544b24ca7dSJeff Layton 	}
45554b24ca7dSJeff Layton 
45564b24ca7dSJeff Layton 	if (STALE_CLIENTID(clid, nn))
45574b24ca7dSJeff Layton 		return nfserr_stale_clientid;
45584b24ca7dSJeff Layton 
45594b24ca7dSJeff Layton 	/*
45604b24ca7dSJeff Layton 	 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
45614b24ca7dSJeff Layton 	 * cached already then we know this is for is for v4.0 and "sessions"
45624b24ca7dSJeff Layton 	 * will be false.
45634b24ca7dSJeff Layton 	 */
45644b24ca7dSJeff Layton 	WARN_ON_ONCE(cstate->session);
45653e339f96STrond Myklebust 	spin_lock(&nn->client_lock);
4566b7342204SOlga Kornievskaia 	found = find_confirmed_client(clid, sessions, nn);
45673e339f96STrond Myklebust 	if (!found) {
45683e339f96STrond Myklebust 		spin_unlock(&nn->client_lock);
45694b24ca7dSJeff Layton 		return nfserr_expired;
45703e339f96STrond Myklebust 	}
457114ed14ccSJ. Bruce Fields 	atomic_inc(&found->cl_rpc_users);
45723e339f96STrond Myklebust 	spin_unlock(&nn->client_lock);
45734b24ca7dSJeff Layton 
45744b24ca7dSJeff Layton 	/* Cache the nfs4_client in cstate! */
45754b24ca7dSJeff Layton 	cstate->clp = found;
45764b24ca7dSJeff Layton 	return nfs_ok;
45774b24ca7dSJeff Layton }
45784b24ca7dSJeff Layton 
4579b37ad28bSAl Viro __be32
45806668958fSAndy Adamson nfsd4_process_open1(struct nfsd4_compound_state *cstate,
45813320fef1SStanislav Kinsbursky 		    struct nfsd4_open *open, struct nfsd_net *nn)
45821da177e4SLinus Torvalds {
45831da177e4SLinus Torvalds 	clientid_t *clientid = &open->op_clientid;
45841da177e4SLinus Torvalds 	struct nfs4_client *clp = NULL;
45851da177e4SLinus Torvalds 	unsigned int strhashval;
4586fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = NULL;
45874cdc951bSJ. Bruce Fields 	__be32 status;
45881da177e4SLinus Torvalds 
45892c142baaSStanislav Kinsbursky 	if (STALE_CLIENTID(&open->op_clientid, nn))
45901da177e4SLinus Torvalds 		return nfserr_stale_clientid;
459132513b40SJ. Bruce Fields 	/*
459232513b40SJ. Bruce Fields 	 * In case we need it later, after we've already created the
459332513b40SJ. Bruce Fields 	 * file and don't want to risk a further failure:
459432513b40SJ. Bruce Fields 	 */
459532513b40SJ. Bruce Fields 	open->op_file = nfsd4_alloc_file();
459632513b40SJ. Bruce Fields 	if (open->op_file == NULL)
459732513b40SJ. Bruce Fields 		return nfserr_jukebox;
45981da177e4SLinus Torvalds 
4599b7342204SOlga Kornievskaia 	status = lookup_clientid(clientid, cstate, nn, false);
460013d6f66bSTrond Myklebust 	if (status)
460113d6f66bSTrond Myklebust 		return status;
460213d6f66bSTrond Myklebust 	clp = cstate->clp;
46032d91e895STrond Myklebust 
4604d4f0489fSTrond Myklebust 	strhashval = ownerstr_hashval(&open->op_owner);
4605d4f0489fSTrond Myklebust 	oo = find_openstateowner_str(strhashval, open, clp);
46062d91e895STrond Myklebust 	open->op_openowner = oo;
46072d91e895STrond Myklebust 	if (!oo) {
4608bcf130f9SJ. Bruce Fields 		goto new_owner;
46090f442aa2SJ. Bruce Fields 	}
4610dad1c067SJ. Bruce Fields 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
46110f442aa2SJ. Bruce Fields 		/* Replace unconfirmed owners without checking for replay. */
4612fe0750e5SJ. Bruce Fields 		release_openowner(oo);
4613fe0750e5SJ. Bruce Fields 		open->op_openowner = NULL;
4614bcf130f9SJ. Bruce Fields 		goto new_owner;
46150f442aa2SJ. Bruce Fields 	}
46164cdc951bSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
46174cdc951bSJ. Bruce Fields 	if (status)
46184cdc951bSJ. Bruce Fields 		return status;
46194cdc951bSJ. Bruce Fields 	goto alloc_stateid;
4620bcf130f9SJ. Bruce Fields new_owner:
462113d6f66bSTrond Myklebust 	oo = alloc_init_open_stateowner(strhashval, open, cstate);
4622fe0750e5SJ. Bruce Fields 	if (oo == NULL)
46233e772463SJ. Bruce Fields 		return nfserr_jukebox;
4624fe0750e5SJ. Bruce Fields 	open->op_openowner = oo;
46254cdc951bSJ. Bruce Fields alloc_stateid:
4626b49e084dSJeff Layton 	open->op_stp = nfs4_alloc_open_stateid(clp);
46274cdc951bSJ. Bruce Fields 	if (!open->op_stp)
46284cdc951bSJ. Bruce Fields 		return nfserr_jukebox;
46298287f009SSachin Bhamare 
46308287f009SSachin Bhamare 	if (nfsd4_has_session(cstate) &&
46318287f009SSachin Bhamare 	    (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
46328287f009SSachin Bhamare 		open->op_odstate = alloc_clnt_odstate(clp);
46338287f009SSachin Bhamare 		if (!open->op_odstate)
46348287f009SSachin Bhamare 			return nfserr_jukebox;
46358287f009SSachin Bhamare 	}
46368287f009SSachin Bhamare 
46370f442aa2SJ. Bruce Fields 	return nfs_ok;
46381da177e4SLinus Torvalds }
46391da177e4SLinus Torvalds 
4640b37ad28bSAl Viro static inline __be32
46414a6e43e6SNeilBrown nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
46424a6e43e6SNeilBrown {
46434a6e43e6SNeilBrown 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
46444a6e43e6SNeilBrown 		return nfserr_openmode;
46454a6e43e6SNeilBrown 	else
46464a6e43e6SNeilBrown 		return nfs_ok;
46474a6e43e6SNeilBrown }
46484a6e43e6SNeilBrown 
4649c47d832bSDaniel Mack static int share_access_to_flags(u32 share_access)
465024a0111eSJ. Bruce Fields {
465124a0111eSJ. Bruce Fields 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
465224a0111eSJ. Bruce Fields }
465324a0111eSJ. Bruce Fields 
465438c2f4b1SJ. Bruce Fields static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4655f459e453SJ. Bruce Fields {
4656f459e453SJ. Bruce Fields 	struct nfs4_stid *ret;
4657f459e453SJ. Bruce Fields 
465895da1b3aSAndrew Elble 	ret = find_stateid_by_type(cl, s,
465995da1b3aSAndrew Elble 				NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4660f459e453SJ. Bruce Fields 	if (!ret)
4661f459e453SJ. Bruce Fields 		return NULL;
4662f459e453SJ. Bruce Fields 	return delegstateid(ret);
4663f459e453SJ. Bruce Fields }
4664f459e453SJ. Bruce Fields 
46658b289b2cSJ. Bruce Fields static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
46668b289b2cSJ. Bruce Fields {
46678b289b2cSJ. Bruce Fields 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
46688b289b2cSJ. Bruce Fields 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
46698b289b2cSJ. Bruce Fields }
46708b289b2cSJ. Bruce Fields 
4671b37ad28bSAl Viro static __be32
467241d22663SJ. Bruce Fields nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4673567d9829SNeilBrown 		struct nfs4_delegation **dp)
4674567d9829SNeilBrown {
4675567d9829SNeilBrown 	int flags;
4676b37ad28bSAl Viro 	__be32 status = nfserr_bad_stateid;
4677dcd94cc2STrond Myklebust 	struct nfs4_delegation *deleg;
4678567d9829SNeilBrown 
4679dcd94cc2STrond Myklebust 	deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4680dcd94cc2STrond Myklebust 	if (deleg == NULL)
4681c44c5eebSNeilBrown 		goto out;
468295da1b3aSAndrew Elble 	if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
468395da1b3aSAndrew Elble 		nfs4_put_stid(&deleg->dl_stid);
468495da1b3aSAndrew Elble 		if (cl->cl_minorversion)
468595da1b3aSAndrew Elble 			status = nfserr_deleg_revoked;
468695da1b3aSAndrew Elble 		goto out;
468795da1b3aSAndrew Elble 	}
468824a0111eSJ. Bruce Fields 	flags = share_access_to_flags(open->op_share_access);
4689dcd94cc2STrond Myklebust 	status = nfs4_check_delegmode(deleg, flags);
4690dcd94cc2STrond Myklebust 	if (status) {
4691dcd94cc2STrond Myklebust 		nfs4_put_stid(&deleg->dl_stid);
4692dcd94cc2STrond Myklebust 		goto out;
4693dcd94cc2STrond Myklebust 	}
4694dcd94cc2STrond Myklebust 	*dp = deleg;
4695c44c5eebSNeilBrown out:
46968b289b2cSJ. Bruce Fields 	if (!nfsd4_is_deleg_cur(open))
4697c44c5eebSNeilBrown 		return nfs_ok;
4698c44c5eebSNeilBrown 	if (status)
4699c44c5eebSNeilBrown 		return status;
4700dad1c067SJ. Bruce Fields 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4701c44c5eebSNeilBrown 	return nfs_ok;
4702567d9829SNeilBrown }
4703567d9829SNeilBrown 
470421fb4016SJ. Bruce Fields static inline int nfs4_access_to_access(u32 nfs4_access)
470521fb4016SJ. Bruce Fields {
470621fb4016SJ. Bruce Fields 	int flags = 0;
470721fb4016SJ. Bruce Fields 
470821fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
470921fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_READ;
471021fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
471121fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_WRITE;
471221fb4016SJ. Bruce Fields 	return flags;
471321fb4016SJ. Bruce Fields }
471421fb4016SJ. Bruce Fields 
4715b37ad28bSAl Viro static inline __be32
47161da177e4SLinus Torvalds nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
47171da177e4SLinus Torvalds 		struct nfsd4_open *open)
47181da177e4SLinus Torvalds {
47191da177e4SLinus Torvalds 	struct iattr iattr = {
47201da177e4SLinus Torvalds 		.ia_valid = ATTR_SIZE,
47211da177e4SLinus Torvalds 		.ia_size = 0,
47221da177e4SLinus Torvalds 	};
47231da177e4SLinus Torvalds 	if (!open->op_truncate)
47241da177e4SLinus Torvalds 		return 0;
47251da177e4SLinus Torvalds 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
47269246585aSAl Viro 		return nfserr_inval;
47272a1aa489SArnd Bergmann 	return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
47281da177e4SLinus Torvalds }
47291da177e4SLinus Torvalds 
47307e6a72e5SChristoph Hellwig static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
47316eb3a1d0SJeff Layton 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
47326eb3a1d0SJeff Layton 		struct nfsd4_open *open)
47337e6a72e5SChristoph Hellwig {
4734fd4f83fdSJeff Layton 	struct nfsd_file *nf = NULL;
47357e6a72e5SChristoph Hellwig 	__be32 status;
47367e6a72e5SChristoph Hellwig 	int oflag = nfs4_access_to_omode(open->op_share_access);
47377e6a72e5SChristoph Hellwig 	int access = nfs4_access_to_access(open->op_share_access);
4738baeb4ff0SJeff Layton 	unsigned char old_access_bmap, old_deny_bmap;
47397e6a72e5SChristoph Hellwig 
4740de18643dSTrond Myklebust 	spin_lock(&fp->fi_lock);
4741baeb4ff0SJeff Layton 
4742baeb4ff0SJeff Layton 	/*
4743baeb4ff0SJeff Layton 	 * Are we trying to set a deny mode that would conflict with
4744baeb4ff0SJeff Layton 	 * current access?
4745baeb4ff0SJeff Layton 	 */
4746baeb4ff0SJeff Layton 	status = nfs4_file_check_deny(fp, open->op_share_deny);
4747baeb4ff0SJeff Layton 	if (status != nfs_ok) {
4748baeb4ff0SJeff Layton 		spin_unlock(&fp->fi_lock);
4749baeb4ff0SJeff Layton 		goto out;
4750baeb4ff0SJeff Layton 	}
4751baeb4ff0SJeff Layton 
4752baeb4ff0SJeff Layton 	/* set access to the file */
4753baeb4ff0SJeff Layton 	status = nfs4_file_get_access(fp, open->op_share_access);
4754baeb4ff0SJeff Layton 	if (status != nfs_ok) {
4755baeb4ff0SJeff Layton 		spin_unlock(&fp->fi_lock);
4756baeb4ff0SJeff Layton 		goto out;
4757baeb4ff0SJeff Layton 	}
4758baeb4ff0SJeff Layton 
4759baeb4ff0SJeff Layton 	/* Set access bits in stateid */
4760baeb4ff0SJeff Layton 	old_access_bmap = stp->st_access_bmap;
4761baeb4ff0SJeff Layton 	set_access(open->op_share_access, stp);
4762baeb4ff0SJeff Layton 
4763baeb4ff0SJeff Layton 	/* Set new deny mask */
4764baeb4ff0SJeff Layton 	old_deny_bmap = stp->st_deny_bmap;
4765baeb4ff0SJeff Layton 	set_deny(open->op_share_deny, stp);
4766baeb4ff0SJeff Layton 	fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4767baeb4ff0SJeff Layton 
47687e6a72e5SChristoph Hellwig 	if (!fp->fi_fds[oflag]) {
4769de18643dSTrond Myklebust 		spin_unlock(&fp->fi_lock);
4770fd4f83fdSJeff Layton 		status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
47717e6a72e5SChristoph Hellwig 		if (status)
4772baeb4ff0SJeff Layton 			goto out_put_access;
4773de18643dSTrond Myklebust 		spin_lock(&fp->fi_lock);
4774de18643dSTrond Myklebust 		if (!fp->fi_fds[oflag]) {
4775fd4f83fdSJeff Layton 			fp->fi_fds[oflag] = nf;
4776fd4f83fdSJeff Layton 			nf = NULL;
4777de18643dSTrond Myklebust 		}
47787e6a72e5SChristoph Hellwig 	}
4779de18643dSTrond Myklebust 	spin_unlock(&fp->fi_lock);
4780fd4f83fdSJeff Layton 	if (nf)
4781fd4f83fdSJeff Layton 		nfsd_file_put(nf);
47827e6a72e5SChristoph Hellwig 
47837e6a72e5SChristoph Hellwig 	status = nfsd4_truncate(rqstp, cur_fh, open);
47847e6a72e5SChristoph Hellwig 	if (status)
47857e6a72e5SChristoph Hellwig 		goto out_put_access;
47867e6a72e5SChristoph Hellwig out:
47877e6a72e5SChristoph Hellwig 	return status;
4788baeb4ff0SJeff Layton out_put_access:
4789baeb4ff0SJeff Layton 	stp->st_access_bmap = old_access_bmap;
4790baeb4ff0SJeff Layton 	nfs4_file_put_access(fp, open->op_share_access);
4791baeb4ff0SJeff Layton 	reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4792baeb4ff0SJeff Layton 	goto out;
47937e6a72e5SChristoph Hellwig }
47947e6a72e5SChristoph Hellwig 
4795b37ad28bSAl Viro static __be32
4796dcef0413SJ. Bruce Fields 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)
47971da177e4SLinus Torvalds {
4798b37ad28bSAl Viro 	__be32 status;
47996ac75368SArnd Bergmann 	unsigned char old_deny_bmap = stp->st_deny_bmap;
48001da177e4SLinus Torvalds 
48016eb3a1d0SJeff Layton 	if (!test_access(open->op_share_access, stp))
4802baeb4ff0SJeff Layton 		return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
48037e6a72e5SChristoph Hellwig 
4804baeb4ff0SJeff Layton 	/* test and set deny mode */
4805baeb4ff0SJeff Layton 	spin_lock(&fp->fi_lock);
4806baeb4ff0SJeff Layton 	status = nfs4_file_check_deny(fp, open->op_share_deny);
4807baeb4ff0SJeff Layton 	if (status == nfs_ok) {
4808baeb4ff0SJeff Layton 		set_deny(open->op_share_deny, stp);
4809baeb4ff0SJeff Layton 		fp->fi_share_deny |=
4810baeb4ff0SJeff Layton 				(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
48111da177e4SLinus Torvalds 	}
4812baeb4ff0SJeff Layton 	spin_unlock(&fp->fi_lock);
48131da177e4SLinus Torvalds 
4814baeb4ff0SJeff Layton 	if (status != nfs_ok)
4815baeb4ff0SJeff Layton 		return status;
4816baeb4ff0SJeff Layton 
4817baeb4ff0SJeff Layton 	status = nfsd4_truncate(rqstp, cur_fh, open);
4818baeb4ff0SJeff Layton 	if (status != nfs_ok)
4819baeb4ff0SJeff Layton 		reset_union_bmap_deny(old_deny_bmap, stp);
4820baeb4ff0SJeff Layton 	return status;
4821baeb4ff0SJeff Layton }
48221da177e4SLinus Torvalds 
482314a24e99SJ. Bruce Fields /* Should we give out recallable state?: */
482414a24e99SJ. Bruce Fields static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
482514a24e99SJ. Bruce Fields {
482614a24e99SJ. Bruce Fields 	if (clp->cl_cb_state == NFSD4_CB_UP)
482714a24e99SJ. Bruce Fields 		return true;
482814a24e99SJ. Bruce Fields 	/*
482914a24e99SJ. Bruce Fields 	 * In the sessions case, since we don't have to establish a
483014a24e99SJ. Bruce Fields 	 * separate connection for callbacks, we assume it's OK
483114a24e99SJ. Bruce Fields 	 * until we hear otherwise:
483214a24e99SJ. Bruce Fields 	 */
483314a24e99SJ. Bruce Fields 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
483414a24e99SJ. Bruce Fields }
483514a24e99SJ. Bruce Fields 
4836653e514eSJ. Bruce Fields static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4837653e514eSJ. Bruce Fields 						int flag)
483822d38c4cSJ. Bruce Fields {
483922d38c4cSJ. Bruce Fields 	struct file_lock *fl;
484022d38c4cSJ. Bruce Fields 
484122d38c4cSJ. Bruce Fields 	fl = locks_alloc_lock();
484222d38c4cSJ. Bruce Fields 	if (!fl)
484322d38c4cSJ. Bruce Fields 		return NULL;
484422d38c4cSJ. Bruce Fields 	fl->fl_lmops = &nfsd_lease_mng_ops;
4845617588d5SJ. Bruce Fields 	fl->fl_flags = FL_DELEG;
484622d38c4cSJ. Bruce Fields 	fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
484722d38c4cSJ. Bruce Fields 	fl->fl_end = OFFSET_MAX;
4848653e514eSJ. Bruce Fields 	fl->fl_owner = (fl_owner_t)dp;
484922d38c4cSJ. Bruce Fields 	fl->fl_pid = current->tgid;
4850eb82dd39SJeff Layton 	fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
485122d38c4cSJ. Bruce Fields 	return fl;
485222d38c4cSJ. Bruce Fields }
485322d38c4cSJ. Bruce Fields 
48540b26693cSJeff Layton static struct nfs4_delegation *
48550b26693cSJeff Layton nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
48568287f009SSachin Bhamare 		    struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4857acfdf5c3SJ. Bruce Fields {
485868b18f52SJ. Bruce Fields 	int status = 0;
48590b26693cSJeff Layton 	struct nfs4_delegation *dp;
4860eb82dd39SJeff Layton 	struct nfsd_file *nf;
4861353601e7SJ. Bruce Fields 	struct file_lock *fl;
4862417c6629SJeff Layton 
4863353601e7SJ. Bruce Fields 	/*
4864353601e7SJ. Bruce Fields 	 * The fi_had_conflict and nfs_get_existing_delegation checks
4865353601e7SJ. Bruce Fields 	 * here are just optimizations; we'll need to recheck them at
4866353601e7SJ. Bruce Fields 	 * the end:
4867353601e7SJ. Bruce Fields 	 */
4868bf7bd3e9SJ. Bruce Fields 	if (fp->fi_had_conflict)
48690b26693cSJeff Layton 		return ERR_PTR(-EAGAIN);
48700b26693cSJeff Layton 
4871eb82dd39SJeff Layton 	nf = find_readable_file(fp);
4872eb82dd39SJeff Layton 	if (!nf) {
4873353601e7SJ. Bruce Fields 		/* We should always have a readable file here */
4874353601e7SJ. Bruce Fields 		WARN_ON_ONCE(1);
4875353601e7SJ. Bruce Fields 		return ERR_PTR(-EBADF);
4876353601e7SJ. Bruce Fields 	}
487734ed9872SAndrew Elble 	spin_lock(&state_lock);
487834ed9872SAndrew Elble 	spin_lock(&fp->fi_lock);
487968b18f52SJ. Bruce Fields 	if (nfs4_delegation_exists(clp, fp))
488068b18f52SJ. Bruce Fields 		status = -EAGAIN;
4881353601e7SJ. Bruce Fields 	else if (!fp->fi_deleg_file) {
4882eb82dd39SJeff Layton 		fp->fi_deleg_file = nf;
4883353601e7SJ. Bruce Fields 		/* increment early to prevent fi_deleg_file from being
4884353601e7SJ. Bruce Fields 		 * cleared */
4885353601e7SJ. Bruce Fields 		fp->fi_delegees = 1;
4886eb82dd39SJeff Layton 		nf = NULL;
4887353601e7SJ. Bruce Fields 	} else
4888353601e7SJ. Bruce Fields 		fp->fi_delegees++;
4889353601e7SJ. Bruce Fields 	spin_unlock(&fp->fi_lock);
4890353601e7SJ. Bruce Fields 	spin_unlock(&state_lock);
4891eb82dd39SJeff Layton 	if (nf)
4892eb82dd39SJeff Layton 		nfsd_file_put(nf);
4893353601e7SJ. Bruce Fields 	if (status)
4894353601e7SJ. Bruce Fields 		return ERR_PTR(status);
4895353601e7SJ. Bruce Fields 
4896353601e7SJ. Bruce Fields 	status = -ENOMEM;
4897353601e7SJ. Bruce Fields 	dp = alloc_init_deleg(clp, fp, fh, odstate);
4898353601e7SJ. Bruce Fields 	if (!dp)
4899353601e7SJ. Bruce Fields 		goto out_delegees;
4900353601e7SJ. Bruce Fields 
4901353601e7SJ. Bruce Fields 	fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
4902353601e7SJ. Bruce Fields 	if (!fl)
4903bd8d7250SAndrew Elble 		goto out_clnt_odstate;
4904353601e7SJ. Bruce Fields 
4905eb82dd39SJeff Layton 	status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
4906353601e7SJ. Bruce Fields 	if (fl)
4907353601e7SJ. Bruce Fields 		locks_free_lock(fl);
4908353601e7SJ. Bruce Fields 	if (status)
4909353601e7SJ. Bruce Fields 		goto out_clnt_odstate;
4910353601e7SJ. Bruce Fields 
4911353601e7SJ. Bruce Fields 	spin_lock(&state_lock);
4912353601e7SJ. Bruce Fields 	spin_lock(&fp->fi_lock);
4913353601e7SJ. Bruce Fields 	if (fp->fi_had_conflict)
4914353601e7SJ. Bruce Fields 		status = -EAGAIN;
4915353601e7SJ. Bruce Fields 	else
4916353601e7SJ. Bruce Fields 		status = hash_delegation_locked(dp, fp);
491734ed9872SAndrew Elble 	spin_unlock(&fp->fi_lock);
491834ed9872SAndrew Elble 	spin_unlock(&state_lock);
491934ed9872SAndrew Elble 
492034ed9872SAndrew Elble 	if (status)
4921692ad280SAndrew Elble 		goto out_unlock;
4922692ad280SAndrew Elble 
49230b26693cSJeff Layton 	return dp;
4924692ad280SAndrew Elble out_unlock:
4925eb82dd39SJeff Layton 	vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
4926353601e7SJ. Bruce Fields out_clnt_odstate:
4927353601e7SJ. Bruce Fields 	put_clnt_odstate(dp->dl_clnt_odstate);
4928353601e7SJ. Bruce Fields 	nfs4_put_stid(&dp->dl_stid);
4929353601e7SJ. Bruce Fields out_delegees:
4930353601e7SJ. Bruce Fields 	put_deleg_file(fp);
4931353601e7SJ. Bruce Fields 	return ERR_PTR(status);
4932edab9782SJ. Bruce Fields }
4933edab9782SJ. Bruce Fields 
49344aa8913cSBenny Halevy static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
49354aa8913cSBenny Halevy {
49364aa8913cSBenny Halevy 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
49374aa8913cSBenny Halevy 	if (status == -EAGAIN)
49384aa8913cSBenny Halevy 		open->op_why_no_deleg = WND4_CONTENTION;
49394aa8913cSBenny Halevy 	else {
49404aa8913cSBenny Halevy 		open->op_why_no_deleg = WND4_RESOURCE;
49414aa8913cSBenny Halevy 		switch (open->op_deleg_want) {
49424aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_READ_DELEG:
49434aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_WRITE_DELEG:
49444aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_ANY_DELEG:
49454aa8913cSBenny Halevy 			break;
49464aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_CANCEL:
49474aa8913cSBenny Halevy 			open->op_why_no_deleg = WND4_CANCELLED;
49484aa8913cSBenny Halevy 			break;
49494aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_NO_DELEG:
4950063b0fb9SJ. Bruce Fields 			WARN_ON_ONCE(1);
49514aa8913cSBenny Halevy 		}
49524aa8913cSBenny Halevy 	}
49534aa8913cSBenny Halevy }
49544aa8913cSBenny Halevy 
49551da177e4SLinus Torvalds /*
49561da177e4SLinus Torvalds  * Attempt to hand out a delegation.
495799c41515SJ. Bruce Fields  *
495899c41515SJ. Bruce Fields  * Note we don't support write delegations, and won't until the vfs has
495999c41515SJ. Bruce Fields  * proper support for them.
49601da177e4SLinus Torvalds  */
49611da177e4SLinus Torvalds static void
49624cf59221SJeff Layton nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
49634cf59221SJeff Layton 			struct nfs4_ol_stateid *stp)
49641da177e4SLinus Torvalds {
49651da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
49664cf59221SJeff Layton 	struct nfs4_openowner *oo = openowner(stp->st_stateowner);
49674cf59221SJeff Layton 	struct nfs4_client *clp = stp->st_stid.sc_client;
496814a24e99SJ. Bruce Fields 	int cb_up;
496999c41515SJ. Bruce Fields 	int status = 0;
49701da177e4SLinus Torvalds 
4971fe0750e5SJ. Bruce Fields 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
49727b190fecSNeilBrown 	open->op_recall = 0;
49737b190fecSNeilBrown 	switch (open->op_claim_type) {
49747b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_PREVIOUS:
49752bf23875SJ. Bruce Fields 			if (!cb_up)
49767b190fecSNeilBrown 				open->op_recall = 1;
497799c41515SJ. Bruce Fields 			if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
497899c41515SJ. Bruce Fields 				goto out_no_deleg;
49797b190fecSNeilBrown 			break;
49807b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_NULL:
4981ed47b062SMing Chen 		case NFS4_OPEN_CLAIM_FH:
498299c41515SJ. Bruce Fields 			/*
498399c41515SJ. Bruce Fields 			 * Let's not give out any delegations till everyone's
4984c87fb4a3SJ. Bruce Fields 			 * had the chance to reclaim theirs, *and* until
4985c87fb4a3SJ. Bruce Fields 			 * NLM locks have all been reclaimed:
498699c41515SJ. Bruce Fields 			 */
49874cf59221SJeff Layton 			if (locks_in_grace(clp->net))
498899c41515SJ. Bruce Fields 				goto out_no_deleg;
4989dad1c067SJ. Bruce Fields 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
499099c41515SJ. Bruce Fields 				goto out_no_deleg;
49919a0590aeSSteve Dickson 			/*
49929a0590aeSSteve Dickson 			 * Also, if the file was opened for write or
49939a0590aeSSteve Dickson 			 * create, there's a good chance the client's
49949a0590aeSSteve Dickson 			 * about to write to it, resulting in an
49959a0590aeSSteve Dickson 			 * immediate recall (since we don't support
49969a0590aeSSteve Dickson 			 * write delegations):
49979a0590aeSSteve Dickson 			 */
49981da177e4SLinus Torvalds 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
499999c41515SJ. Bruce Fields 				goto out_no_deleg;
500099c41515SJ. Bruce Fields 			if (open->op_create == NFS4_OPEN_CREATE)
500199c41515SJ. Bruce Fields 				goto out_no_deleg;
50027b190fecSNeilBrown 			break;
50037b190fecSNeilBrown 		default:
500499c41515SJ. Bruce Fields 			goto out_no_deleg;
50057b190fecSNeilBrown 	}
50068287f009SSachin Bhamare 	dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
50070b26693cSJeff Layton 	if (IS_ERR(dp))
5008dd239cc0SJ. Bruce Fields 		goto out_no_deleg;
50091da177e4SLinus Torvalds 
5010d5477a8dSJ. Bruce Fields 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
50111da177e4SLinus Torvalds 
5012dd5e3fbcSChuck Lever 	trace_nfsd_deleg_open(&dp->dl_stid.sc_stateid);
501399c41515SJ. Bruce Fields 	open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
501467cb1279STrond Myklebust 	nfs4_put_stid(&dp->dl_stid);
5015dd239cc0SJ. Bruce Fields 	return;
5016dd239cc0SJ. Bruce Fields out_no_deleg:
501799c41515SJ. Bruce Fields 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
50187b190fecSNeilBrown 	if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5019d08d32e6SJ. Bruce Fields 	    open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
50201da177e4SLinus Torvalds 		dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5021d08d32e6SJ. Bruce Fields 		open->op_recall = 1;
5022d08d32e6SJ. Bruce Fields 	}
5023dd239cc0SJ. Bruce Fields 
5024dd239cc0SJ. Bruce Fields 	/* 4.1 client asking for a delegation? */
5025dd239cc0SJ. Bruce Fields 	if (open->op_deleg_want)
5026dd239cc0SJ. Bruce Fields 		nfsd4_open_deleg_none_ext(open, status);
5027dd239cc0SJ. Bruce Fields 	return;
50281da177e4SLinus Torvalds }
50291da177e4SLinus Torvalds 
5030e27f49c3SBenny Halevy static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5031e27f49c3SBenny Halevy 					struct nfs4_delegation *dp)
5032e27f49c3SBenny Halevy {
5033e27f49c3SBenny Halevy 	if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5034e27f49c3SBenny Halevy 	    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5035e27f49c3SBenny Halevy 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5036e27f49c3SBenny Halevy 		open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5037e27f49c3SBenny Halevy 	} else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5038e27f49c3SBenny Halevy 		   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5039e27f49c3SBenny Halevy 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5040e27f49c3SBenny Halevy 		open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5041e27f49c3SBenny Halevy 	}
5042e27f49c3SBenny Halevy 	/* Otherwise the client must be confused wanting a delegation
5043e27f49c3SBenny Halevy 	 * it already has, therefore we don't return
5044e27f49c3SBenny Halevy 	 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5045e27f49c3SBenny Halevy 	 */
5046e27f49c3SBenny Halevy }
5047e27f49c3SBenny Halevy 
5048b37ad28bSAl Viro __be32
50491da177e4SLinus Torvalds nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
50501da177e4SLinus Torvalds {
50516668958fSAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
505238c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
50531da177e4SLinus Torvalds 	struct nfs4_file *fp = NULL;
5054dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp = NULL;
5055567d9829SNeilBrown 	struct nfs4_delegation *dp = NULL;
5056b37ad28bSAl Viro 	__be32 status;
5057d8a1a000STrond Myklebust 	bool new_stp = false;
50581da177e4SLinus Torvalds 
50591da177e4SLinus Torvalds 	/*
50601da177e4SLinus Torvalds 	 * Lookup file; if found, lookup stateid and check open request,
50611da177e4SLinus Torvalds 	 * and check for delegations in the process of being recalled.
50621da177e4SLinus Torvalds 	 * If not found, create the nfs4_file struct
50631da177e4SLinus Torvalds 	 */
5064f9c00c3aSJeff Layton 	fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
5065950e0118STrond Myklebust 	if (fp != open->op_file) {
506641d22663SJ. Bruce Fields 		status = nfs4_check_deleg(cl, open, &dp);
5067c44c5eebSNeilBrown 		if (status)
5068c44c5eebSNeilBrown 			goto out;
506915ca08d3STrond Myklebust 		stp = nfsd4_find_and_lock_existing_open(fp, open);
50701da177e4SLinus Torvalds 	} else {
5071950e0118STrond Myklebust 		open->op_file = NULL;
5072c44c5eebSNeilBrown 		status = nfserr_bad_stateid;
50738b289b2cSJ. Bruce Fields 		if (nfsd4_is_deleg_cur(open))
5074c44c5eebSNeilBrown 			goto out;
50751da177e4SLinus Torvalds 	}
50761da177e4SLinus Torvalds 
5077d8a1a000STrond Myklebust 	if (!stp) {
5078d8a1a000STrond Myklebust 		stp = init_open_stateid(fp, open);
5079d8a1a000STrond Myklebust 		if (!open->op_stp)
5080d8a1a000STrond Myklebust 			new_stp = true;
5081d8a1a000STrond Myklebust 	}
5082d8a1a000STrond Myklebust 
50831da177e4SLinus Torvalds 	/*
50841da177e4SLinus Torvalds 	 * OPEN the file, or upgrade an existing OPEN.
50851da177e4SLinus Torvalds 	 * If truncate fails, the OPEN fails.
5086d8a1a000STrond Myklebust 	 *
5087d8a1a000STrond Myklebust 	 * stp is already locked.
50881da177e4SLinus Torvalds 	 */
5089d8a1a000STrond Myklebust 	if (!new_stp) {
50901da177e4SLinus Torvalds 		/* Stateid was found, this is an OPEN upgrade */
5091f9d7562fSJ. Bruce Fields 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
509235a92fe8SJeff Layton 		if (status) {
5093feb9dad5SOleg Drokin 			mutex_unlock(&stp->st_mutex);
50941da177e4SLinus Torvalds 			goto out;
509535a92fe8SJeff Layton 		}
50961da177e4SLinus Torvalds 	} else {
50976eb3a1d0SJeff Layton 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
50986eb3a1d0SJeff Layton 		if (status) {
5099d8a1a000STrond Myklebust 			stp->st_stid.sc_type = NFS4_CLOSED_STID;
51006eb3a1d0SJeff Layton 			release_open_stateid(stp);
5101d8a1a000STrond Myklebust 			mutex_unlock(&stp->st_mutex);
51026eb3a1d0SJeff Layton 			goto out;
51036eb3a1d0SJeff Layton 		}
51048287f009SSachin Bhamare 
51058287f009SSachin Bhamare 		stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
51068287f009SSachin Bhamare 							open->op_odstate);
51078287f009SSachin Bhamare 		if (stp->st_clnt_odstate == open->op_odstate)
51088287f009SSachin Bhamare 			open->op_odstate = NULL;
51091da177e4SLinus Torvalds 	}
5110d8a1a000STrond Myklebust 
51119767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5112feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
51131da177e4SLinus Torvalds 
5114d24433cdSBenny Halevy 	if (nfsd4_has_session(&resp->cstate)) {
5115d24433cdSBenny Halevy 		if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5116d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5117d24433cdSBenny Halevy 			open->op_why_no_deleg = WND4_NOT_WANTED;
5118d24433cdSBenny Halevy 			goto nodeleg;
5119d24433cdSBenny Halevy 		}
5120d24433cdSBenny Halevy 	}
5121d24433cdSBenny Halevy 
51221da177e4SLinus Torvalds 	/*
51231da177e4SLinus Torvalds 	* Attempt to hand out a delegation. No error return, because the
51241da177e4SLinus Torvalds 	* OPEN succeeds even if we fail.
51251da177e4SLinus Torvalds 	*/
51264cf59221SJeff Layton 	nfs4_open_delegation(current_fh, open, stp);
5127d24433cdSBenny Halevy nodeleg:
51281da177e4SLinus Torvalds 	status = nfs_ok;
5129dd5e3fbcSChuck Lever 	trace_nfsd_deleg_none(&stp->st_stid.sc_stateid);
51301da177e4SLinus Torvalds out:
5131d24433cdSBenny Halevy 	/* 4.1 client trying to upgrade/downgrade delegation? */
5132d24433cdSBenny Halevy 	if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5133e27f49c3SBenny Halevy 	    open->op_deleg_want)
5134e27f49c3SBenny Halevy 		nfsd4_deleg_xgrade_none_ext(open, dp);
5135d24433cdSBenny Halevy 
513613cd2184SNeilBrown 	if (fp)
513713cd2184SNeilBrown 		put_nfs4_file(fp);
513837515177SNeilBrown 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
513987186022SKinglong Mee 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
51401da177e4SLinus Torvalds 	/*
51411da177e4SLinus Torvalds 	* To finish the open response, we just need to set the rflags.
51421da177e4SLinus Torvalds 	*/
51431da177e4SLinus Torvalds 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
514419e4c347SJeff Layton 	if (nfsd4_has_session(&resp->cstate))
514519e4c347SJeff Layton 		open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
514619e4c347SJeff Layton 	else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
51471da177e4SLinus Torvalds 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
514819e4c347SJeff Layton 
5149dcd94cc2STrond Myklebust 	if (dp)
5150dcd94cc2STrond Myklebust 		nfs4_put_stid(&dp->dl_stid);
5151d6f2bc5dSTrond Myklebust 	if (stp)
5152d6f2bc5dSTrond Myklebust 		nfs4_put_stid(&stp->st_stid);
51531da177e4SLinus Torvalds 
51541da177e4SLinus Torvalds 	return status;
51551da177e4SLinus Torvalds }
51561da177e4SLinus Torvalds 
515758fb12e6SJeff Layton void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
515842297899SJeff Layton 			      struct nfsd4_open *open)
5159d29b20cdSJ. Bruce Fields {
5160d29b20cdSJ. Bruce Fields 	if (open->op_openowner) {
5161d3134b10SJeff Layton 		struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5162d29b20cdSJ. Bruce Fields 
5163d3134b10SJeff Layton 		nfsd4_cstate_assign_replay(cstate, so);
5164d3134b10SJeff Layton 		nfs4_put_stateowner(so);
5165d29b20cdSJ. Bruce Fields 	}
516632513b40SJ. Bruce Fields 	if (open->op_file)
51675b095e99SJeff Layton 		kmem_cache_free(file_slab, open->op_file);
51684cdc951bSJ. Bruce Fields 	if (open->op_stp)
51696011695dSTrond Myklebust 		nfs4_put_stid(&open->op_stp->st_stid);
51708287f009SSachin Bhamare 	if (open->op_odstate)
51718287f009SSachin Bhamare 		kmem_cache_free(odstate_slab, open->op_odstate);
5172d29b20cdSJ. Bruce Fields }
5173d29b20cdSJ. Bruce Fields 
5174b37ad28bSAl Viro __be32
5175b591480bSJ.Bruce Fields nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5176eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
51771da177e4SLinus Torvalds {
5178eb69853dSChristoph Hellwig 	clientid_t *clid = &u->renew;
51791da177e4SLinus Torvalds 	struct nfs4_client *clp;
5180b37ad28bSAl Viro 	__be32 status;
51817f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
51821da177e4SLinus Torvalds 
5183dd5e3fbcSChuck Lever 	trace_nfsd_clid_renew(clid);
5184b7342204SOlga Kornievskaia 	status = lookup_clientid(clid, cstate, nn, false);
51859b2ef62bSJ. Bruce Fields 	if (status)
51861da177e4SLinus Torvalds 		goto out;
51874b24ca7dSJeff Layton 	clp = cstate->clp;
51881da177e4SLinus Torvalds 	status = nfserr_cb_path_down;
5189ea1da636SNeilBrown 	if (!list_empty(&clp->cl_delegations)
519077a3569dSJ. Bruce Fields 			&& clp->cl_cb_state != NFSD4_CB_UP)
51911da177e4SLinus Torvalds 		goto out;
51921da177e4SLinus Torvalds 	status = nfs_ok;
51931da177e4SLinus Torvalds out:
51941da177e4SLinus Torvalds 	return status;
51951da177e4SLinus Torvalds }
51961da177e4SLinus Torvalds 
51977f5ef2e9SJeff Layton void
519812760c66SStanislav Kinsbursky nfsd4_end_grace(struct nfsd_net *nn)
5199a76b4319SNeilBrown {
520033dcc481SJeff Layton 	/* do nothing if grace period already ended */
5201a51c84edSStanislav Kinsbursky 	if (nn->grace_ended)
520233dcc481SJeff Layton 		return;
520333dcc481SJeff Layton 
5204dd5e3fbcSChuck Lever 	trace_nfsd_grace_complete(nn);
5205a51c84edSStanislav Kinsbursky 	nn->grace_ended = true;
520670b28235SJ. Bruce Fields 	/*
520770b28235SJ. Bruce Fields 	 * If the server goes down again right now, an NFSv4
520870b28235SJ. Bruce Fields 	 * client will still be allowed to reclaim after it comes back up,
520970b28235SJ. Bruce Fields 	 * even if it hasn't yet had a chance to reclaim state this time.
521070b28235SJ. Bruce Fields 	 *
521170b28235SJ. Bruce Fields 	 */
5212919b8049SJeff Layton 	nfsd4_record_grace_done(nn);
521370b28235SJ. Bruce Fields 	/*
521470b28235SJ. Bruce Fields 	 * At this point, NFSv4 clients can still reclaim.  But if the
521570b28235SJ. Bruce Fields 	 * server crashes, any that have not yet reclaimed will be out
521670b28235SJ. Bruce Fields 	 * of luck on the next boot.
521770b28235SJ. Bruce Fields 	 *
521870b28235SJ. Bruce Fields 	 * (NFSv4.1+ clients are considered to have reclaimed once they
521970b28235SJ. Bruce Fields 	 * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
522070b28235SJ. Bruce Fields 	 * have reclaimed after their first OPEN.)
522170b28235SJ. Bruce Fields 	 */
52225e1533c7SStanislav Kinsbursky 	locks_end_grace(&nn->nfsd4_manager);
522370b28235SJ. Bruce Fields 	/*
522470b28235SJ. Bruce Fields 	 * At this point, and once lockd and/or any other containers
522570b28235SJ. Bruce Fields 	 * exit their grace period, further reclaims will fail and
522670b28235SJ. Bruce Fields 	 * regular locking can resume.
522770b28235SJ. Bruce Fields 	 */
5228a76b4319SNeilBrown }
5229a76b4319SNeilBrown 
523003f318caSJ. Bruce Fields /*
523103f318caSJ. Bruce Fields  * If we've waited a lease period but there are still clients trying to
523203f318caSJ. Bruce Fields  * reclaim, wait a little longer to give them a chance to finish.
523303f318caSJ. Bruce Fields  */
523403f318caSJ. Bruce Fields static bool clients_still_reclaiming(struct nfsd_net *nn)
523503f318caSJ. Bruce Fields {
523620b7d86fSArnd Bergmann 	time64_t double_grace_period_end = nn->boot_time +
523720b7d86fSArnd Bergmann 					   2 * nn->nfsd4_lease;
523803f318caSJ. Bruce Fields 
5239362063a5SScott Mayhew 	if (nn->track_reclaim_completes &&
5240362063a5SScott Mayhew 			atomic_read(&nn->nr_reclaim_complete) ==
5241362063a5SScott Mayhew 			nn->reclaim_str_hashtbl_size)
5242362063a5SScott Mayhew 		return false;
524303f318caSJ. Bruce Fields 	if (!nn->somebody_reclaimed)
524403f318caSJ. Bruce Fields 		return false;
524503f318caSJ. Bruce Fields 	nn->somebody_reclaimed = false;
524603f318caSJ. Bruce Fields 	/*
524703f318caSJ. Bruce Fields 	 * If we've given them *two* lease times to reclaim, and they're
524803f318caSJ. Bruce Fields 	 * still not done, give up:
524903f318caSJ. Bruce Fields 	 */
525020b7d86fSArnd Bergmann 	if (ktime_get_boottime_seconds() > double_grace_period_end)
525103f318caSJ. Bruce Fields 		return false;
525203f318caSJ. Bruce Fields 	return true;
525303f318caSJ. Bruce Fields }
525403f318caSJ. Bruce Fields 
525520b7d86fSArnd Bergmann static time64_t
525609121281SStanislav Kinsbursky nfs4_laundromat(struct nfsd_net *nn)
52571da177e4SLinus Torvalds {
52581da177e4SLinus Torvalds 	struct nfs4_client *clp;
5259fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
52601da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
5261217526e7SJeff Layton 	struct nfs4_ol_stateid *stp;
52627919d0a2SJeff Layton 	struct nfsd4_blocked_lock *nbl;
52631da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
526420b7d86fSArnd Bergmann 	time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
526520b7d86fSArnd Bergmann 	time64_t t, new_timeo = nn->nfsd4_lease;
5266624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
5267624322f1SOlga Kornievskaia 	copy_stateid_t *cps_t;
5268624322f1SOlga Kornievskaia 	int i;
52691da177e4SLinus Torvalds 
527003f318caSJ. Bruce Fields 	if (clients_still_reclaiming(nn)) {
527103f318caSJ. Bruce Fields 		new_timeo = 0;
527203f318caSJ. Bruce Fields 		goto out;
527303f318caSJ. Bruce Fields 	}
527412760c66SStanislav Kinsbursky 	nfsd4_end_grace(nn);
527536acb66bSBenny Halevy 	INIT_LIST_HEAD(&reaplist);
5276624322f1SOlga Kornievskaia 
5277624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
5278624322f1SOlga Kornievskaia 	idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5279624322f1SOlga Kornievskaia 		cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5280624322f1SOlga Kornievskaia 		if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
528120b7d86fSArnd Bergmann 				cps->cpntf_time > cutoff)
5282624322f1SOlga Kornievskaia 			_free_cpntf_state_locked(nn, cps);
5283624322f1SOlga Kornievskaia 	}
5284624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
5285624322f1SOlga Kornievskaia 
5286c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
52875ed58bb2SStanislav Kinsbursky 	list_for_each_safe(pos, next, &nn->client_lru) {
52881da177e4SLinus Torvalds 		clp = list_entry(pos, struct nfs4_client, cl_lru);
528920b7d86fSArnd Bergmann 		if (clp->cl_time > cutoff) {
52901da177e4SLinus Torvalds 			t = clp->cl_time - cutoff;
5291a832e7aeSJeff Layton 			new_timeo = min(new_timeo, t);
52921da177e4SLinus Torvalds 			break;
52931da177e4SLinus Torvalds 		}
5294221a6876SJ. Bruce Fields 		if (mark_client_expired_locked(clp)) {
5295dd5e3fbcSChuck Lever 			trace_nfsd_clid_expired(&clp->cl_clientid);
5296d7682988SBenny Halevy 			continue;
5297d7682988SBenny Halevy 		}
52984864af97STrond Myklebust 		list_add(&clp->cl_lru, &reaplist);
529936acb66bSBenny Halevy 	}
5300c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
530136acb66bSBenny Halevy 	list_for_each_safe(pos, next, &reaplist) {
530236acb66bSBenny Halevy 		clp = list_entry(pos, struct nfs4_client, cl_lru);
5303dd5e3fbcSChuck Lever 		trace_nfsd_clid_purged(&clp->cl_clientid);
53044864af97STrond Myklebust 		list_del_init(&clp->cl_lru);
53051da177e4SLinus Torvalds 		expire_client(clp);
53061da177e4SLinus Torvalds 	}
5307cdc97505SBenny Halevy 	spin_lock(&state_lock);
5308e8c69d17SJ. Bruce Fields 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
53091da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
531020b7d86fSArnd Bergmann 		if (dp->dl_time > cutoff) {
5311a832e7aeSJeff Layton 			t = dp->dl_time - cutoff;
5312a832e7aeSJeff Layton 			new_timeo = min(new_timeo, t);
53131da177e4SLinus Torvalds 			break;
53141da177e4SLinus Torvalds 		}
53153fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
531642690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
53171da177e4SLinus Torvalds 	}
5318cdc97505SBenny Halevy 	spin_unlock(&state_lock);
53192d4a532dSJeff Layton 	while (!list_empty(&reaplist)) {
53202d4a532dSJeff Layton 		dp = list_first_entry(&reaplist, struct nfs4_delegation,
53212d4a532dSJeff Layton 					dl_recall_lru);
53222d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
53233bd64a5bSJ. Bruce Fields 		revoke_delegation(dp);
53241da177e4SLinus Torvalds 	}
5325217526e7SJeff Layton 
5326217526e7SJeff Layton 	spin_lock(&nn->client_lock);
5327217526e7SJeff Layton 	while (!list_empty(&nn->close_lru)) {
5328217526e7SJeff Layton 		oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5329217526e7SJeff Layton 					oo_close_lru);
533020b7d86fSArnd Bergmann 		if (oo->oo_time > cutoff) {
5331a832e7aeSJeff Layton 			t = oo->oo_time - cutoff;
5332a832e7aeSJeff Layton 			new_timeo = min(new_timeo, t);
53331da177e4SLinus Torvalds 			break;
53341da177e4SLinus Torvalds 		}
5335217526e7SJeff Layton 		list_del_init(&oo->oo_close_lru);
5336217526e7SJeff Layton 		stp = oo->oo_last_closed_stid;
5337217526e7SJeff Layton 		oo->oo_last_closed_stid = NULL;
5338217526e7SJeff Layton 		spin_unlock(&nn->client_lock);
5339217526e7SJeff Layton 		nfs4_put_stid(&stp->st_stid);
5340217526e7SJeff Layton 		spin_lock(&nn->client_lock);
53411da177e4SLinus Torvalds 	}
5342217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
5343217526e7SJeff Layton 
53447919d0a2SJeff Layton 	/*
53457919d0a2SJeff Layton 	 * It's possible for a client to try and acquire an already held lock
53467919d0a2SJeff Layton 	 * that is being held for a long time, and then lose interest in it.
53477919d0a2SJeff Layton 	 * So, we clean out any un-revisited request after a lease period
53487919d0a2SJeff Layton 	 * under the assumption that the client is no longer interested.
53497919d0a2SJeff Layton 	 *
53507919d0a2SJeff Layton 	 * RFC5661, sec. 9.6 states that the client must not rely on getting
53517919d0a2SJeff Layton 	 * notifications and must continue to poll for locks, even when the
53527919d0a2SJeff Layton 	 * server supports them. Thus this shouldn't lead to clients blocking
53537919d0a2SJeff Layton 	 * indefinitely once the lock does become free.
53547919d0a2SJeff Layton 	 */
53557919d0a2SJeff Layton 	BUG_ON(!list_empty(&reaplist));
53560cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
53577919d0a2SJeff Layton 	while (!list_empty(&nn->blocked_locks_lru)) {
53587919d0a2SJeff Layton 		nbl = list_first_entry(&nn->blocked_locks_lru,
53597919d0a2SJeff Layton 					struct nfsd4_blocked_lock, nbl_lru);
536020b7d86fSArnd Bergmann 		if (nbl->nbl_time > cutoff) {
53617919d0a2SJeff Layton 			t = nbl->nbl_time - cutoff;
53627919d0a2SJeff Layton 			new_timeo = min(new_timeo, t);
53637919d0a2SJeff Layton 			break;
53647919d0a2SJeff Layton 		}
53657919d0a2SJeff Layton 		list_move(&nbl->nbl_lru, &reaplist);
53667919d0a2SJeff Layton 		list_del_init(&nbl->nbl_list);
53677919d0a2SJeff Layton 	}
53680cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
53697919d0a2SJeff Layton 
53707919d0a2SJeff Layton 	while (!list_empty(&reaplist)) {
537164ebe124SNaofumi Honda 		nbl = list_first_entry(&reaplist,
53727919d0a2SJeff Layton 					struct nfsd4_blocked_lock, nbl_lru);
53737919d0a2SJeff Layton 		list_del_init(&nbl->nbl_lru);
53747919d0a2SJeff Layton 		free_blocked_lock(nbl);
53757919d0a2SJeff Layton 	}
537603f318caSJ. Bruce Fields out:
537720b7d86fSArnd Bergmann 	new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5378a832e7aeSJeff Layton 	return new_timeo;
53791da177e4SLinus Torvalds }
53801da177e4SLinus Torvalds 
5381a254b246SHarvey Harrison static struct workqueue_struct *laundry_wq;
5382a254b246SHarvey Harrison static void laundromat_main(struct work_struct *);
5383a254b246SHarvey Harrison 
5384a254b246SHarvey Harrison static void
538509121281SStanislav Kinsbursky laundromat_main(struct work_struct *laundry)
53861da177e4SLinus Torvalds {
538720b7d86fSArnd Bergmann 	time64_t t;
53882e55f3abSGeliang Tang 	struct delayed_work *dwork = to_delayed_work(laundry);
538909121281SStanislav Kinsbursky 	struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
539009121281SStanislav Kinsbursky 					   laundromat_work);
53911da177e4SLinus Torvalds 
539209121281SStanislav Kinsbursky 	t = nfs4_laundromat(nn);
539309121281SStanislav Kinsbursky 	queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
53941da177e4SLinus Torvalds }
53951da177e4SLinus Torvalds 
53968fcd461dSJeff Layton static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5397f8816512SNeilBrown {
53988fcd461dSJeff Layton 	if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5399f7a4d872SJ. Bruce Fields 		return nfserr_bad_stateid;
5400f7a4d872SJ. Bruce Fields 	return nfs_ok;
54011da177e4SLinus Torvalds }
54021da177e4SLinus Torvalds 
54031da177e4SLinus Torvalds static inline int
540482c5ff1bSJeff Layton access_permit_read(struct nfs4_ol_stateid *stp)
54051da177e4SLinus Torvalds {
540682c5ff1bSJeff Layton 	return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
540782c5ff1bSJeff Layton 		test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
540882c5ff1bSJeff Layton 		test_access(NFS4_SHARE_ACCESS_WRITE, stp);
54091da177e4SLinus Torvalds }
54101da177e4SLinus Torvalds 
54111da177e4SLinus Torvalds static inline int
541282c5ff1bSJeff Layton access_permit_write(struct nfs4_ol_stateid *stp)
54131da177e4SLinus Torvalds {
541482c5ff1bSJeff Layton 	return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
541582c5ff1bSJeff Layton 		test_access(NFS4_SHARE_ACCESS_BOTH, stp);
54161da177e4SLinus Torvalds }
54171da177e4SLinus Torvalds 
54181da177e4SLinus Torvalds static
5419dcef0413SJ. Bruce Fields __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
54201da177e4SLinus Torvalds {
5421b37ad28bSAl Viro         __be32 status = nfserr_openmode;
54221da177e4SLinus Torvalds 
542302921914SJ. Bruce Fields 	/* For lock stateid's, we test the parent open, not the lock: */
542402921914SJ. Bruce Fields 	if (stp->st_openstp)
542502921914SJ. Bruce Fields 		stp = stp->st_openstp;
542682c5ff1bSJeff Layton 	if ((flags & WR_STATE) && !access_permit_write(stp))
54271da177e4SLinus Torvalds                 goto out;
542882c5ff1bSJeff Layton 	if ((flags & RD_STATE) && !access_permit_read(stp))
54291da177e4SLinus Torvalds                 goto out;
54301da177e4SLinus Torvalds 	status = nfs_ok;
54311da177e4SLinus Torvalds out:
54321da177e4SLinus Torvalds 	return status;
54331da177e4SLinus Torvalds }
54341da177e4SLinus Torvalds 
5435b37ad28bSAl Viro static inline __be32
54365ccb0066SStanislav Kinsbursky check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
54371da177e4SLinus Torvalds {
5438203a8c8eSJ. Bruce Fields 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
54391da177e4SLinus Torvalds 		return nfs_ok;
5440c87fb4a3SJ. Bruce Fields 	else if (opens_in_grace(net)) {
544125985edcSLucas De Marchi 		/* Answer in remaining cases depends on existence of
54421da177e4SLinus Torvalds 		 * conflicting state; so we must wait out the grace period. */
54431da177e4SLinus Torvalds 		return nfserr_grace;
54441da177e4SLinus Torvalds 	} else if (flags & WR_STATE)
54451da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
54461da177e4SLinus Torvalds 				NFS4_SHARE_DENY_WRITE);
54471da177e4SLinus Torvalds 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
54481da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
54491da177e4SLinus Torvalds 				NFS4_SHARE_DENY_READ);
54501da177e4SLinus Torvalds }
54511da177e4SLinus Torvalds 
54521da177e4SLinus Torvalds /*
54531da177e4SLinus Torvalds  * Allow READ/WRITE during grace period on recovered state only for files
54541da177e4SLinus Torvalds  * that are not able to provide mandatory locking.
54551da177e4SLinus Torvalds  */
54561da177e4SLinus Torvalds static inline int
54575ccb0066SStanislav Kinsbursky grace_disallows_io(struct net *net, struct inode *inode)
54581da177e4SLinus Torvalds {
5459c87fb4a3SJ. Bruce Fields 	return opens_in_grace(net) && mandatory_lock(inode);
54601da177e4SLinus Torvalds }
54611da177e4SLinus Torvalds 
546257b7b43bSJ. Bruce Fields static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
54630836f587SJ. Bruce Fields {
54646668958fSAndy Adamson 	/*
54656668958fSAndy Adamson 	 * When sessions are used the stateid generation number is ignored
54666668958fSAndy Adamson 	 * when it is zero.
54676668958fSAndy Adamson 	 */
546828dde241SJ. Bruce Fields 	if (has_session && in->si_generation == 0)
546981b82965SJ. Bruce Fields 		return nfs_ok;
547081b82965SJ. Bruce Fields 
547181b82965SJ. Bruce Fields 	if (in->si_generation == ref->si_generation)
547281b82965SJ. Bruce Fields 		return nfs_ok;
54736668958fSAndy Adamson 
54740836f587SJ. Bruce Fields 	/* If the client sends us a stateid from the future, it's buggy: */
547514b7f4a1SJeff Layton 	if (nfsd4_stateid_generation_after(in, ref))
54760836f587SJ. Bruce Fields 		return nfserr_bad_stateid;
54770836f587SJ. Bruce Fields 	/*
547881b82965SJ. Bruce Fields 	 * However, we could see a stateid from the past, even from a
547981b82965SJ. Bruce Fields 	 * non-buggy client.  For example, if the client sends a lock
548081b82965SJ. Bruce Fields 	 * while some IO is outstanding, the lock may bump si_generation
548181b82965SJ. Bruce Fields 	 * while the IO is still in flight.  The client could avoid that
548281b82965SJ. Bruce Fields 	 * situation by waiting for responses on all the IO requests,
548381b82965SJ. Bruce Fields 	 * but better performance may result in retrying IO that
548481b82965SJ. Bruce Fields 	 * receives an old_stateid error if requests are rarely
548581b82965SJ. Bruce Fields 	 * reordered in flight:
54860836f587SJ. Bruce Fields 	 */
54870836f587SJ. Bruce Fields 	return nfserr_old_stateid;
54880836f587SJ. Bruce Fields }
54890836f587SJ. Bruce Fields 
549003da3169STrond Myklebust static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
549103da3169STrond Myklebust {
549203da3169STrond Myklebust 	__be32 ret;
549303da3169STrond Myklebust 
549403da3169STrond Myklebust 	spin_lock(&s->sc_lock);
549503da3169STrond Myklebust 	ret = nfsd4_verify_open_stid(s);
549603da3169STrond Myklebust 	if (ret == nfs_ok)
549703da3169STrond Myklebust 		ret = check_stateid_generation(in, &s->sc_stateid, has_session);
549803da3169STrond Myklebust 	spin_unlock(&s->sc_lock);
549903da3169STrond Myklebust 	return ret;
550003da3169STrond Myklebust }
550103da3169STrond Myklebust 
5502ebe9cb3bSChristoph Hellwig static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5503ebe9cb3bSChristoph Hellwig {
5504ebe9cb3bSChristoph Hellwig 	if (ols->st_stateowner->so_is_open_owner &&
5505ebe9cb3bSChristoph Hellwig 	    !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5506ebe9cb3bSChristoph Hellwig 		return nfserr_bad_stateid;
5507ebe9cb3bSChristoph Hellwig 	return nfs_ok;
5508ebe9cb3bSChristoph Hellwig }
5509ebe9cb3bSChristoph Hellwig 
55107df302f7SChuck Lever static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
551117456804SBryan Schumaker {
551297b7e3b6SJ. Bruce Fields 	struct nfs4_stid *s;
55131af71cc8SJeff Layton 	__be32 status = nfserr_bad_stateid;
551417456804SBryan Schumaker 
5515ae254dacSAndrew Elble 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5516ae254dacSAndrew Elble 		CLOSE_STATEID(stateid))
55171af71cc8SJeff Layton 		return status;
5518663e36f0SJ. Bruce Fields 	if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
55191af71cc8SJeff Layton 		return status;
55201af71cc8SJeff Layton 	spin_lock(&cl->cl_lock);
55211af71cc8SJeff Layton 	s = find_stateid_locked(cl, stateid);
552297b7e3b6SJ. Bruce Fields 	if (!s)
55231af71cc8SJeff Layton 		goto out_unlock;
552403da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
552517456804SBryan Schumaker 	if (status)
55261af71cc8SJeff Layton 		goto out_unlock;
552723340032SJ. Bruce Fields 	switch (s->sc_type) {
552823340032SJ. Bruce Fields 	case NFS4_DELEG_STID:
55291af71cc8SJeff Layton 		status = nfs_ok;
55301af71cc8SJeff Layton 		break;
55313bd64a5bSJ. Bruce Fields 	case NFS4_REVOKED_DELEG_STID:
55321af71cc8SJeff Layton 		status = nfserr_deleg_revoked;
55331af71cc8SJeff Layton 		break;
553423340032SJ. Bruce Fields 	case NFS4_OPEN_STID:
553523340032SJ. Bruce Fields 	case NFS4_LOCK_STID:
5536ebe9cb3bSChristoph Hellwig 		status = nfsd4_check_openowner_confirmed(openlockstateid(s));
55371af71cc8SJeff Layton 		break;
553823340032SJ. Bruce Fields 	default:
553923340032SJ. Bruce Fields 		printk("unknown stateid type %x\n", s->sc_type);
5540b0fc29d6STrond Myklebust 		/* Fallthrough */
554123340032SJ. Bruce Fields 	case NFS4_CLOSED_STID:
5542b0fc29d6STrond Myklebust 	case NFS4_CLOSED_DELEG_STID:
55431af71cc8SJeff Layton 		status = nfserr_bad_stateid;
554423340032SJ. Bruce Fields 	}
55451af71cc8SJeff Layton out_unlock:
55461af71cc8SJeff Layton 	spin_unlock(&cl->cl_lock);
55471af71cc8SJeff Layton 	return status;
554817456804SBryan Schumaker }
554917456804SBryan Schumaker 
5550cd61c522SChristoph Hellwig __be32
55512dd6e458STrond Myklebust nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
55522dd6e458STrond Myklebust 		     stateid_t *stateid, unsigned char typemask,
55532dd6e458STrond Myklebust 		     struct nfs4_stid **s, struct nfsd_net *nn)
555438c2f4b1SJ. Bruce Fields {
55550eb6f20aSJ. Bruce Fields 	__be32 status;
555695da1b3aSAndrew Elble 	bool return_revoked = false;
555795da1b3aSAndrew Elble 
555895da1b3aSAndrew Elble 	/*
555995da1b3aSAndrew Elble 	 *  only return revoked delegations if explicitly asked.
556095da1b3aSAndrew Elble 	 *  otherwise we report revoked or bad_stateid status.
556195da1b3aSAndrew Elble 	 */
556295da1b3aSAndrew Elble 	if (typemask & NFS4_REVOKED_DELEG_STID)
556395da1b3aSAndrew Elble 		return_revoked = true;
556495da1b3aSAndrew Elble 	else if (typemask & NFS4_DELEG_STID)
556595da1b3aSAndrew Elble 		typemask |= NFS4_REVOKED_DELEG_STID;
556638c2f4b1SJ. Bruce Fields 
5567ae254dacSAndrew Elble 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5568ae254dacSAndrew Elble 		CLOSE_STATEID(stateid))
556938c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
5570b7342204SOlga Kornievskaia 	status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5571b7342204SOlga Kornievskaia 				 false);
5572a8a7c677STrond Myklebust 	if (status == nfserr_stale_clientid) {
55734b24ca7dSJeff Layton 		if (cstate->session)
5574a8a7c677STrond Myklebust 			return nfserr_bad_stateid;
557538c2f4b1SJ. Bruce Fields 		return nfserr_stale_stateid;
5576a8a7c677STrond Myklebust 	}
55770eb6f20aSJ. Bruce Fields 	if (status)
55780eb6f20aSJ. Bruce Fields 		return status;
55794b24ca7dSJeff Layton 	*s = find_stateid_by_type(cstate->clp, stateid, typemask);
558038c2f4b1SJ. Bruce Fields 	if (!*s)
558138c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
558295da1b3aSAndrew Elble 	if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
558395da1b3aSAndrew Elble 		nfs4_put_stid(*s);
558495da1b3aSAndrew Elble 		if (cstate->minorversion)
558595da1b3aSAndrew Elble 			return nfserr_deleg_revoked;
558695da1b3aSAndrew Elble 		return nfserr_bad_stateid;
558795da1b3aSAndrew Elble 	}
558838c2f4b1SJ. Bruce Fields 	return nfs_ok;
558938c2f4b1SJ. Bruce Fields }
559038c2f4b1SJ. Bruce Fields 
5591eb82dd39SJeff Layton static struct nfsd_file *
5592a0649b2dSChristoph Hellwig nfs4_find_file(struct nfs4_stid *s, int flags)
5593a0649b2dSChristoph Hellwig {
5594af90f707SChristoph Hellwig 	if (!s)
5595af90f707SChristoph Hellwig 		return NULL;
5596af90f707SChristoph Hellwig 
5597a0649b2dSChristoph Hellwig 	switch (s->sc_type) {
5598a0649b2dSChristoph Hellwig 	case NFS4_DELEG_STID:
5599a0649b2dSChristoph Hellwig 		if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5600a0649b2dSChristoph Hellwig 			return NULL;
5601eb82dd39SJeff Layton 		return nfsd_file_get(s->sc_file->fi_deleg_file);
5602a0649b2dSChristoph Hellwig 	case NFS4_OPEN_STID:
5603a0649b2dSChristoph Hellwig 	case NFS4_LOCK_STID:
5604a0649b2dSChristoph Hellwig 		if (flags & RD_STATE)
5605a0649b2dSChristoph Hellwig 			return find_readable_file(s->sc_file);
5606a0649b2dSChristoph Hellwig 		else
5607a0649b2dSChristoph Hellwig 			return find_writeable_file(s->sc_file);
5608a0649b2dSChristoph Hellwig 		break;
5609a0649b2dSChristoph Hellwig 	}
5610a0649b2dSChristoph Hellwig 
5611a0649b2dSChristoph Hellwig 	return NULL;
5612a0649b2dSChristoph Hellwig }
5613a0649b2dSChristoph Hellwig 
5614a0649b2dSChristoph Hellwig static __be32
5615d8836f77SJ. Bruce Fields nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5616a0649b2dSChristoph Hellwig {
5617a0649b2dSChristoph Hellwig 	__be32 status;
5618a0649b2dSChristoph Hellwig 
5619a0649b2dSChristoph Hellwig 	status = nfsd4_check_openowner_confirmed(ols);
5620a0649b2dSChristoph Hellwig 	if (status)
5621a0649b2dSChristoph Hellwig 		return status;
5622a0649b2dSChristoph Hellwig 	return nfs4_check_openmode(ols, flags);
5623a0649b2dSChristoph Hellwig }
5624a0649b2dSChristoph Hellwig 
5625af90f707SChristoph Hellwig static __be32
5626af90f707SChristoph Hellwig nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
56275c4583b2SJeff Layton 		struct nfsd_file **nfp, int flags)
5628af90f707SChristoph Hellwig {
5629af90f707SChristoph Hellwig 	int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5630eb82dd39SJeff Layton 	struct nfsd_file *nf;
5631af90f707SChristoph Hellwig 	__be32 status;
5632af90f707SChristoph Hellwig 
5633eb82dd39SJeff Layton 	nf = nfs4_find_file(s, flags);
5634eb82dd39SJeff Layton 	if (nf) {
5635af90f707SChristoph Hellwig 		status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5636af90f707SChristoph Hellwig 				acc | NFSD_MAY_OWNER_OVERRIDE);
56375c4583b2SJeff Layton 		if (status) {
56385c4583b2SJeff Layton 			nfsd_file_put(nf);
5639eb82dd39SJeff Layton 			goto out;
56405c4583b2SJeff Layton 		}
5641af90f707SChristoph Hellwig 	} else {
5642eb82dd39SJeff Layton 		status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5643af90f707SChristoph Hellwig 		if (status)
5644af90f707SChristoph Hellwig 			return status;
5645af90f707SChristoph Hellwig 	}
56465c4583b2SJeff Layton 	*nfp = nf;
5647eb82dd39SJeff Layton out:
5648eb82dd39SJeff Layton 	return status;
5649af90f707SChristoph Hellwig }
5650624322f1SOlga Kornievskaia static void
5651624322f1SOlga Kornievskaia _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5652624322f1SOlga Kornievskaia {
5653624322f1SOlga Kornievskaia 	WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5654624322f1SOlga Kornievskaia 	if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5655624322f1SOlga Kornievskaia 		return;
5656624322f1SOlga Kornievskaia 	list_del(&cps->cp_list);
5657624322f1SOlga Kornievskaia 	idr_remove(&nn->s2s_cp_stateids,
5658624322f1SOlga Kornievskaia 		   cps->cp_stateid.stid.si_opaque.so_id);
5659624322f1SOlga Kornievskaia 	kfree(cps);
5660624322f1SOlga Kornievskaia }
5661b7342204SOlga Kornievskaia /*
5662b7342204SOlga Kornievskaia  * A READ from an inter server to server COPY will have a
5663b7342204SOlga Kornievskaia  * copy stateid. Look up the copy notify stateid from the
5664b7342204SOlga Kornievskaia  * idr structure and take a reference on it.
5665b7342204SOlga Kornievskaia  */
5666ce0887acSOlga Kornievskaia __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5667ce0887acSOlga Kornievskaia 			  struct nfs4_client *clp,
5668b7342204SOlga Kornievskaia 			  struct nfs4_cpntf_state **cps)
5669b7342204SOlga Kornievskaia {
5670b7342204SOlga Kornievskaia 	copy_stateid_t *cps_t;
5671b7342204SOlga Kornievskaia 	struct nfs4_cpntf_state *state = NULL;
5672b7342204SOlga Kornievskaia 
5673b7342204SOlga Kornievskaia 	if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5674b7342204SOlga Kornievskaia 		return nfserr_bad_stateid;
5675b7342204SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
5676b7342204SOlga Kornievskaia 	cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5677b7342204SOlga Kornievskaia 	if (cps_t) {
5678b7342204SOlga Kornievskaia 		state = container_of(cps_t, struct nfs4_cpntf_state,
5679b7342204SOlga Kornievskaia 				     cp_stateid);
56805277a79eSDan Carpenter 		if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
56815277a79eSDan Carpenter 			state = NULL;
56825277a79eSDan Carpenter 			goto unlock;
56835277a79eSDan Carpenter 		}
5684ce0887acSOlga Kornievskaia 		if (!clp)
5685b7342204SOlga Kornievskaia 			refcount_inc(&state->cp_stateid.sc_count);
5686ce0887acSOlga Kornievskaia 		else
5687ce0887acSOlga Kornievskaia 			_free_cpntf_state_locked(nn, state);
5688b7342204SOlga Kornievskaia 	}
56895277a79eSDan Carpenter unlock:
5690b7342204SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
5691b7342204SOlga Kornievskaia 	if (!state)
5692b7342204SOlga Kornievskaia 		return nfserr_bad_stateid;
5693ce0887acSOlga Kornievskaia 	if (!clp && state)
5694b7342204SOlga Kornievskaia 		*cps = state;
5695b7342204SOlga Kornievskaia 	return 0;
5696b7342204SOlga Kornievskaia }
5697b7342204SOlga Kornievskaia 
5698b7342204SOlga Kornievskaia static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5699b7342204SOlga Kornievskaia 			       struct nfs4_stid **stid)
5700b7342204SOlga Kornievskaia {
5701b7342204SOlga Kornievskaia 	__be32 status;
5702b7342204SOlga Kornievskaia 	struct nfs4_cpntf_state *cps = NULL;
5703b7342204SOlga Kornievskaia 	struct nfsd4_compound_state cstate;
5704b7342204SOlga Kornievskaia 
5705ce0887acSOlga Kornievskaia 	status = manage_cpntf_state(nn, st, NULL, &cps);
5706b7342204SOlga Kornievskaia 	if (status)
5707b7342204SOlga Kornievskaia 		return status;
5708b7342204SOlga Kornievskaia 
570920b7d86fSArnd Bergmann 	cps->cpntf_time = ktime_get_boottime_seconds();
5710b7342204SOlga Kornievskaia 	memset(&cstate, 0, sizeof(cstate));
5711b7342204SOlga Kornievskaia 	status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5712b7342204SOlga Kornievskaia 	if (status)
5713b7342204SOlga Kornievskaia 		goto out;
5714b7342204SOlga Kornievskaia 	status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5715b7342204SOlga Kornievskaia 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5716b7342204SOlga Kornievskaia 				stid, nn);
5717b7342204SOlga Kornievskaia 	put_client_renew(cstate.clp);
5718b7342204SOlga Kornievskaia out:
5719b7342204SOlga Kornievskaia 	nfs4_put_cpntf_state(nn, cps);
5720b7342204SOlga Kornievskaia 	return status;
5721b7342204SOlga Kornievskaia }
5722624322f1SOlga Kornievskaia 
5723624322f1SOlga Kornievskaia void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5724624322f1SOlga Kornievskaia {
5725624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
5726624322f1SOlga Kornievskaia 	_free_cpntf_state_locked(nn, cps);
5727624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
5728624322f1SOlga Kornievskaia }
5729af90f707SChristoph Hellwig 
57301da177e4SLinus Torvalds /*
57311da177e4SLinus Torvalds  * Checks for stateid operations
57321da177e4SLinus Torvalds  */
5733b37ad28bSAl Viro __be32
5734af90f707SChristoph Hellwig nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5735aa0d6aedSAnna Schumaker 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5736624322f1SOlga Kornievskaia 		stateid_t *stateid, int flags, struct nfsd_file **nfp,
5737624322f1SOlga Kornievskaia 		struct nfs4_stid **cstid)
57381da177e4SLinus Torvalds {
5739a0649b2dSChristoph Hellwig 	struct inode *ino = d_inode(fhp->fh_dentry);
5740af90f707SChristoph Hellwig 	struct net *net = SVC_NET(rqstp);
57413320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5742af90f707SChristoph Hellwig 	struct nfs4_stid *s = NULL;
5743b37ad28bSAl Viro 	__be32 status;
57441da177e4SLinus Torvalds 
57455c4583b2SJeff Layton 	if (nfp)
57465c4583b2SJeff Layton 		*nfp = NULL;
57471da177e4SLinus Torvalds 
57485ccb0066SStanislav Kinsbursky 	if (grace_disallows_io(net, ino))
57491da177e4SLinus Torvalds 		return nfserr_grace;
57501da177e4SLinus Torvalds 
5751af90f707SChristoph Hellwig 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5752af90f707SChristoph Hellwig 		status = check_special_stateids(net, fhp, stateid, flags);
5753af90f707SChristoph Hellwig 		goto done;
5754af90f707SChristoph Hellwig 	}
57551da177e4SLinus Torvalds 
57562dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid,
5757db24b3b4SJeff Layton 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
57582dd6e458STrond Myklebust 				&s, nn);
5759b7342204SOlga Kornievskaia 	if (status == nfserr_bad_stateid)
5760b7342204SOlga Kornievskaia 		status = find_cpntf_state(nn, stateid, &s);
576138c2f4b1SJ. Bruce Fields 	if (status)
5762c2d1d6a8STrond Myklebust 		return status;
576303da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, s,
5764a0649b2dSChristoph Hellwig 			nfsd4_has_session(cstate));
57650c2a498fSJ. Bruce Fields 	if (status)
57660c2a498fSJ. Bruce Fields 		goto out;
5767a0649b2dSChristoph Hellwig 
5768f7a4d872SJ. Bruce Fields 	switch (s->sc_type) {
5769f7a4d872SJ. Bruce Fields 	case NFS4_DELEG_STID:
5770a0649b2dSChristoph Hellwig 		status = nfs4_check_delegmode(delegstateid(s), flags);
5771f7a4d872SJ. Bruce Fields 		break;
5772f7a4d872SJ. Bruce Fields 	case NFS4_OPEN_STID:
5773f7a4d872SJ. Bruce Fields 	case NFS4_LOCK_STID:
5774d8836f77SJ. Bruce Fields 		status = nfs4_check_olstateid(openlockstateid(s), flags);
5775f7a4d872SJ. Bruce Fields 		break;
5776f7a4d872SJ. Bruce Fields 	default:
577714bcab1aSTrond Myklebust 		status = nfserr_bad_stateid;
5778a0649b2dSChristoph Hellwig 		break;
57791da177e4SLinus Torvalds 	}
57808fcd461dSJeff Layton 	if (status)
57818fcd461dSJeff Layton 		goto out;
57828fcd461dSJeff Layton 	status = nfs4_check_fh(fhp, s);
5783a0649b2dSChristoph Hellwig 
5784af90f707SChristoph Hellwig done:
57855c4583b2SJeff Layton 	if (status == nfs_ok && nfp)
57865c4583b2SJeff Layton 		status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
57871da177e4SLinus Torvalds out:
5788624322f1SOlga Kornievskaia 	if (s) {
5789624322f1SOlga Kornievskaia 		if (!status && cstid)
5790624322f1SOlga Kornievskaia 			*cstid = s;
5791624322f1SOlga Kornievskaia 		else
5792fd911011STrond Myklebust 			nfs4_put_stid(s);
5793624322f1SOlga Kornievskaia 	}
57941da177e4SLinus Torvalds 	return status;
57951da177e4SLinus Torvalds }
57961da177e4SLinus Torvalds 
5797e1ca12dfSBryan Schumaker /*
579817456804SBryan Schumaker  * Test if the stateid is valid
579917456804SBryan Schumaker  */
580017456804SBryan Schumaker __be32
580117456804SBryan Schumaker nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5802eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
580317456804SBryan Schumaker {
5804eb69853dSChristoph Hellwig 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
580503cfb420SBryan Schumaker 	struct nfsd4_test_stateid_id *stateid;
580603cfb420SBryan Schumaker 	struct nfs4_client *cl = cstate->session->se_client;
580703cfb420SBryan Schumaker 
580803cfb420SBryan Schumaker 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
58097df302f7SChuck Lever 		stateid->ts_id_status =
58107df302f7SChuck Lever 			nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
581103cfb420SBryan Schumaker 
581217456804SBryan Schumaker 	return nfs_ok;
581317456804SBryan Schumaker }
581417456804SBryan Schumaker 
581542691398SChuck Lever static __be32
581642691398SChuck Lever nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
581742691398SChuck Lever {
581842691398SChuck Lever 	struct nfs4_ol_stateid *stp = openlockstateid(s);
581942691398SChuck Lever 	__be32 ret;
582042691398SChuck Lever 
5821659aefb6STrond Myklebust 	ret = nfsd4_lock_ol_stateid(stp);
5822659aefb6STrond Myklebust 	if (ret)
5823659aefb6STrond Myklebust 		goto out_put_stid;
582442691398SChuck Lever 
582542691398SChuck Lever 	ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
582642691398SChuck Lever 	if (ret)
582742691398SChuck Lever 		goto out;
582842691398SChuck Lever 
582942691398SChuck Lever 	ret = nfserr_locks_held;
583042691398SChuck Lever 	if (check_for_locks(stp->st_stid.sc_file,
583142691398SChuck Lever 			    lockowner(stp->st_stateowner)))
583242691398SChuck Lever 		goto out;
583342691398SChuck Lever 
583442691398SChuck Lever 	release_lock_stateid(stp);
583542691398SChuck Lever 	ret = nfs_ok;
583642691398SChuck Lever 
583742691398SChuck Lever out:
583842691398SChuck Lever 	mutex_unlock(&stp->st_mutex);
5839659aefb6STrond Myklebust out_put_stid:
584042691398SChuck Lever 	nfs4_put_stid(s);
584142691398SChuck Lever 	return ret;
584242691398SChuck Lever }
584342691398SChuck Lever 
5844e1ca12dfSBryan Schumaker __be32
5845e1ca12dfSBryan Schumaker nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5846eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
5847e1ca12dfSBryan Schumaker {
5848eb69853dSChristoph Hellwig 	struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5849e1ca12dfSBryan Schumaker 	stateid_t *stateid = &free_stateid->fr_stateid;
58502da1cec7SJ. Bruce Fields 	struct nfs4_stid *s;
58513bd64a5bSJ. Bruce Fields 	struct nfs4_delegation *dp;
585238c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl = cstate->session->se_client;
58532da1cec7SJ. Bruce Fields 	__be32 ret = nfserr_bad_stateid;
5854e1ca12dfSBryan Schumaker 
58551af71cc8SJeff Layton 	spin_lock(&cl->cl_lock);
58561af71cc8SJeff Layton 	s = find_stateid_locked(cl, stateid);
58572da1cec7SJ. Bruce Fields 	if (!s)
58581af71cc8SJeff Layton 		goto out_unlock;
585903da3169STrond Myklebust 	spin_lock(&s->sc_lock);
58602da1cec7SJ. Bruce Fields 	switch (s->sc_type) {
58612da1cec7SJ. Bruce Fields 	case NFS4_DELEG_STID:
5862e1ca12dfSBryan Schumaker 		ret = nfserr_locks_held;
58631af71cc8SJeff Layton 		break;
58642da1cec7SJ. Bruce Fields 	case NFS4_OPEN_STID:
58651af71cc8SJeff Layton 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
58661af71cc8SJeff Layton 		if (ret)
58671af71cc8SJeff Layton 			break;
58681af71cc8SJeff Layton 		ret = nfserr_locks_held;
58691af71cc8SJeff Layton 		break;
58702da1cec7SJ. Bruce Fields 	case NFS4_LOCK_STID:
587103da3169STrond Myklebust 		spin_unlock(&s->sc_lock);
5872a15dfcd5SElena Reshetova 		refcount_inc(&s->sc_count);
58731af71cc8SJeff Layton 		spin_unlock(&cl->cl_lock);
587442691398SChuck Lever 		ret = nfsd4_free_lock_stateid(stateid, s);
58751af71cc8SJeff Layton 		goto out;
58763bd64a5bSJ. Bruce Fields 	case NFS4_REVOKED_DELEG_STID:
587703da3169STrond Myklebust 		spin_unlock(&s->sc_lock);
58783bd64a5bSJ. Bruce Fields 		dp = delegstateid(s);
58792d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
58802d4a532dSJeff Layton 		spin_unlock(&cl->cl_lock);
58816011695dSTrond Myklebust 		nfs4_put_stid(s);
58823bd64a5bSJ. Bruce Fields 		ret = nfs_ok;
58831af71cc8SJeff Layton 		goto out;
58841af71cc8SJeff Layton 	/* Default falls through and returns nfserr_bad_stateid */
5885e1ca12dfSBryan Schumaker 	}
588603da3169STrond Myklebust 	spin_unlock(&s->sc_lock);
58871af71cc8SJeff Layton out_unlock:
58881af71cc8SJeff Layton 	spin_unlock(&cl->cl_lock);
5889e1ca12dfSBryan Schumaker out:
5890e1ca12dfSBryan Schumaker 	return ret;
5891e1ca12dfSBryan Schumaker }
5892e1ca12dfSBryan Schumaker 
58934c4cd222SNeilBrown static inline int
58944c4cd222SNeilBrown setlkflg (int type)
58954c4cd222SNeilBrown {
58964c4cd222SNeilBrown 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
58974c4cd222SNeilBrown 		RD_STATE : WR_STATE;
58984c4cd222SNeilBrown }
58991da177e4SLinus Torvalds 
5900dcef0413SJ. Bruce Fields static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
5901c0a5d93eSJ. Bruce Fields {
5902c0a5d93eSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
5903c0a5d93eSJ. Bruce Fields 	struct nfs4_stateowner *sop = stp->st_stateowner;
5904c0a5d93eSJ. Bruce Fields 	__be32 status;
5905c0a5d93eSJ. Bruce Fields 
5906c0a5d93eSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, sop, seqid);
5907c0a5d93eSJ. Bruce Fields 	if (status)
5908c0a5d93eSJ. Bruce Fields 		return status;
59099271d7e5STrond Myklebust 	status = nfsd4_lock_ol_stateid(stp);
59109271d7e5STrond Myklebust 	if (status != nfs_ok)
59119271d7e5STrond Myklebust 		return status;
5912f7a4d872SJ. Bruce Fields 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
591335a92fe8SJeff Layton 	if (status == nfs_ok)
591435a92fe8SJeff Layton 		status = nfs4_check_fh(current_fh, &stp->st_stid);
591535a92fe8SJeff Layton 	if (status != nfs_ok)
5916feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
5917f7a4d872SJ. Bruce Fields 	return status;
5918c0a5d93eSJ. Bruce Fields }
5919c0a5d93eSJ. Bruce Fields 
59201da177e4SLinus Torvalds /*
59211da177e4SLinus Torvalds  * Checks for sequence id mutating operations.
59221da177e4SLinus Torvalds  */
5923b37ad28bSAl Viro static __be32
5924dd453dfdSBenny Halevy nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
59252288d0e3SJ. Bruce Fields 			 stateid_t *stateid, char typemask,
59263320fef1SStanislav Kinsbursky 			 struct nfs4_ol_stateid **stpp,
59273320fef1SStanislav Kinsbursky 			 struct nfsd_net *nn)
59281da177e4SLinus Torvalds {
59290836f587SJ. Bruce Fields 	__be32 status;
593038c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
5931e17f99b7STrond Myklebust 	struct nfs4_ol_stateid *stp = NULL;
59321da177e4SLinus Torvalds 
5933dd5e3fbcSChuck Lever 	trace_nfsd_preprocess(seqid, stateid);
59341da177e4SLinus Torvalds 
59351da177e4SLinus Torvalds 	*stpp = NULL;
59362dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
5937c0a5d93eSJ. Bruce Fields 	if (status)
5938c0a5d93eSJ. Bruce Fields 		return status;
5939e17f99b7STrond Myklebust 	stp = openlockstateid(s);
594058fb12e6SJeff Layton 	nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
59411da177e4SLinus Torvalds 
5942e17f99b7STrond Myklebust 	status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
5943fd911011STrond Myklebust 	if (!status)
5944e17f99b7STrond Myklebust 		*stpp = stp;
5945fd911011STrond Myklebust 	else
5946fd911011STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
5947e17f99b7STrond Myklebust 	return status;
59481da177e4SLinus Torvalds }
59491da177e4SLinus Torvalds 
59503320fef1SStanislav Kinsbursky static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
59513320fef1SStanislav Kinsbursky 						 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
5952c0a5d93eSJ. Bruce Fields {
5953c0a5d93eSJ. Bruce Fields 	__be32 status;
5954c0a5d93eSJ. Bruce Fields 	struct nfs4_openowner *oo;
59554cbfc9f7STrond Myklebust 	struct nfs4_ol_stateid *stp;
59561da177e4SLinus Torvalds 
5957c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
59584cbfc9f7STrond Myklebust 						NFS4_OPEN_STID, &stp, nn);
59590836f587SJ. Bruce Fields 	if (status)
59600836f587SJ. Bruce Fields 		return status;
59614cbfc9f7STrond Myklebust 	oo = openowner(stp->st_stateowner);
59624cbfc9f7STrond Myklebust 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
5963feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
59644cbfc9f7STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
5965c0a5d93eSJ. Bruce Fields 		return nfserr_bad_stateid;
59664cbfc9f7STrond Myklebust 	}
59674cbfc9f7STrond Myklebust 	*stpp = stp;
59683a4f98bbSNeilBrown 	return nfs_ok;
59691da177e4SLinus Torvalds }
59701da177e4SLinus Torvalds 
5971b37ad28bSAl Viro __be32
5972ca364317SJ.Bruce Fields nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5973eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
59741da177e4SLinus Torvalds {
5975eb69853dSChristoph Hellwig 	struct nfsd4_open_confirm *oc = &u->open_confirm;
5976b37ad28bSAl Viro 	__be32 status;
5977fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
5978dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
59793320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
59801da177e4SLinus Torvalds 
5981a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5982a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
59831da177e4SLinus Torvalds 
5984ca364317SJ.Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
5985a8cddc5dSJ. Bruce Fields 	if (status)
5986a8cddc5dSJ. Bruce Fields 		return status;
59871da177e4SLinus Torvalds 
59889072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate,
5989ca364317SJ.Bruce Fields 					oc->oc_seqid, &oc->oc_req_stateid,
59903320fef1SStanislav Kinsbursky 					NFS4_OPEN_STID, &stp, nn);
59919072d5c6SJ. Bruce Fields 	if (status)
59921da177e4SLinus Torvalds 		goto out;
5993fe0750e5SJ. Bruce Fields 	oo = openowner(stp->st_stateowner);
599468b66e82SJ. Bruce Fields 	status = nfserr_bad_stateid;
599535a92fe8SJeff Layton 	if (oo->oo_flags & NFS4_OO_CONFIRMED) {
5996feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
59972585fc79STrond Myklebust 		goto put_stateid;
599835a92fe8SJeff Layton 	}
5999dad1c067SJ. Bruce Fields 	oo->oo_flags |= NFS4_OO_CONFIRMED;
60009767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6001feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
6002dd5e3fbcSChuck Lever 	trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
60032a4317c5SJeff Layton 	nfsd4_client_record_create(oo->oo_owner.so_client);
600468b66e82SJ. Bruce Fields 	status = nfs_ok;
60052585fc79STrond Myklebust put_stateid:
60062585fc79STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
60071da177e4SLinus Torvalds out:
60089411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
60091da177e4SLinus Torvalds 	return status;
60101da177e4SLinus Torvalds }
60111da177e4SLinus Torvalds 
60126409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
60131da177e4SLinus Torvalds {
601482c5ff1bSJeff Layton 	if (!test_access(access, stp))
60156409a5a6SJ. Bruce Fields 		return;
601611b9164aSTrond Myklebust 	nfs4_file_put_access(stp->st_stid.sc_file, access);
601782c5ff1bSJeff Layton 	clear_access(access, stp);
6018f197c271SJ. Bruce Fields }
60196409a5a6SJ. Bruce Fields 
60206409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
60216409a5a6SJ. Bruce Fields {
60226409a5a6SJ. Bruce Fields 	switch (to_access) {
60236409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
60246409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
60256409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
60266409a5a6SJ. Bruce Fields 		break;
60276409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
60286409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
60296409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
60306409a5a6SJ. Bruce Fields 		break;
60316409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
60326409a5a6SJ. Bruce Fields 		break;
60336409a5a6SJ. Bruce Fields 	default:
6034063b0fb9SJ. Bruce Fields 		WARN_ON_ONCE(1);
60351da177e4SLinus Torvalds 	}
60361da177e4SLinus Torvalds }
60371da177e4SLinus Torvalds 
6038b37ad28bSAl Viro __be32
6039ca364317SJ.Bruce Fields nfsd4_open_downgrade(struct svc_rqst *rqstp,
6040eb69853dSChristoph Hellwig 		     struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
60411da177e4SLinus Torvalds {
6042eb69853dSChristoph Hellwig 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
6043b37ad28bSAl Viro 	__be32 status;
6044dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
60453320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
60461da177e4SLinus Torvalds 
6047a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6048a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
60491da177e4SLinus Torvalds 
6050c30e92dfSJ. Bruce Fields 	/* We don't yet support WANT bits: */
60512c8bd7e0SBenny Halevy 	if (od->od_deleg_want)
60522c8bd7e0SBenny Halevy 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
60532c8bd7e0SBenny Halevy 			od->od_deleg_want);
60541da177e4SLinus Torvalds 
6055c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
60563320fef1SStanislav Kinsbursky 					&od->od_stateid, &stp, nn);
60579072d5c6SJ. Bruce Fields 	if (status)
60581da177e4SLinus Torvalds 		goto out;
60591da177e4SLinus Torvalds 	status = nfserr_inval;
606082c5ff1bSJeff Layton 	if (!test_access(od->od_share_access, stp)) {
6061c11c591fSJeff Layton 		dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
60621da177e4SLinus Torvalds 			stp->st_access_bmap, od->od_share_access);
60630667b1e9STrond Myklebust 		goto put_stateid;
60641da177e4SLinus Torvalds 	}
6065ce0fc43cSJeff Layton 	if (!test_deny(od->od_share_deny, stp)) {
6066c11c591fSJeff Layton 		dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
60671da177e4SLinus Torvalds 			stp->st_deny_bmap, od->od_share_deny);
60680667b1e9STrond Myklebust 		goto put_stateid;
60691da177e4SLinus Torvalds 	}
60706409a5a6SJ. Bruce Fields 	nfs4_stateid_downgrade(stp, od->od_share_access);
6071ce0fc43cSJeff Layton 	reset_union_bmap_deny(od->od_share_deny, stp);
60729767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
60731da177e4SLinus Torvalds 	status = nfs_ok;
60740667b1e9STrond Myklebust put_stateid:
6075feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
60760667b1e9STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
60771da177e4SLinus Torvalds out:
60789411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
60791da177e4SLinus Torvalds 	return status;
60801da177e4SLinus Torvalds }
60811da177e4SLinus Torvalds 
6082f7a4d872SJ. Bruce Fields static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6083f7a4d872SJ. Bruce Fields {
6084acf9295bSTrond Myklebust 	struct nfs4_client *clp = s->st_stid.sc_client;
6085e8568739SJeff Layton 	bool unhashed;
6086d83017f9SJeff Layton 	LIST_HEAD(reaplist);
6087acf9295bSTrond Myklebust 
60882c41beb0SJeff Layton 	spin_lock(&clp->cl_lock);
6089e8568739SJeff Layton 	unhashed = unhash_open_stateid(s, &reaplist);
6090acf9295bSTrond Myklebust 
6091d83017f9SJeff Layton 	if (clp->cl_minorversion) {
6092e8568739SJeff Layton 		if (unhashed)
6093d83017f9SJeff Layton 			put_ol_stateid_locked(s, &reaplist);
6094d83017f9SJeff Layton 		spin_unlock(&clp->cl_lock);
6095d83017f9SJeff Layton 		free_ol_stateid_reaplist(&reaplist);
6096d83017f9SJeff Layton 	} else {
6097d83017f9SJeff Layton 		spin_unlock(&clp->cl_lock);
6098d83017f9SJeff Layton 		free_ol_stateid_reaplist(&reaplist);
6099e8568739SJeff Layton 		if (unhashed)
6100d3134b10SJeff Layton 			move_to_close_lru(s, clp->net);
610138c387b5SJ. Bruce Fields 	}
6102d83017f9SJeff Layton }
610338c387b5SJ. Bruce Fields 
61041da177e4SLinus Torvalds /*
61051da177e4SLinus Torvalds  * nfs4_unlock_state() called after encode
61061da177e4SLinus Torvalds  */
6107b37ad28bSAl Viro __be32
6108ca364317SJ.Bruce Fields nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6109eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
61101da177e4SLinus Torvalds {
6111eb69853dSChristoph Hellwig 	struct nfsd4_close *close = &u->close;
6112b37ad28bSAl Viro 	__be32 status;
6113dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
61143320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
61153320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
61161da177e4SLinus Torvalds 
6117a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_close on file %pd\n",
6118a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
61191da177e4SLinus Torvalds 
6120f7a4d872SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6121f7a4d872SJ. Bruce Fields 					&close->cl_stateid,
6122f7a4d872SJ. Bruce Fields 					NFS4_OPEN_STID|NFS4_CLOSED_STID,
61233320fef1SStanislav Kinsbursky 					&stp, nn);
61249411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
61259072d5c6SJ. Bruce Fields 	if (status)
61261da177e4SLinus Torvalds 		goto out;
612715ca08d3STrond Myklebust 
612815ca08d3STrond Myklebust 	stp->st_stid.sc_type = NFS4_CLOSED_STID;
6129bd2decacSJeff Layton 
6130bd2decacSJeff Layton 	/*
6131bd2decacSJeff Layton 	 * Technically we don't _really_ have to increment or copy it, since
6132bd2decacSJeff Layton 	 * it should just be gone after this operation and we clobber the
6133bd2decacSJeff Layton 	 * copied value below, but we continue to do so here just to ensure
6134bd2decacSJeff Layton 	 * that racing ops see that there was a state change.
6135bd2decacSJeff Layton 	 */
61369767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
61371da177e4SLinus Torvalds 
6138f7a4d872SJ. Bruce Fields 	nfsd4_close_open_stateid(stp);
613915ca08d3STrond Myklebust 	mutex_unlock(&stp->st_mutex);
61408a0b589dSTrond Myklebust 
6141bd2decacSJeff Layton 	/* v4.1+ suggests that we send a special stateid in here, since the
6142bd2decacSJeff Layton 	 * clients should just ignore this anyway. Since this is not useful
6143bd2decacSJeff Layton 	 * for v4.0 clients either, we set it to the special close_stateid
6144bd2decacSJeff Layton 	 * universally.
6145bd2decacSJeff Layton 	 *
6146bd2decacSJeff Layton 	 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6147bd2decacSJeff Layton 	 */
6148bd2decacSJeff Layton 	memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6149fb500a7cSTrond Myklebust 
61508a0b589dSTrond Myklebust 	/* put reference from nfs4_preprocess_seqid_op */
61518a0b589dSTrond Myklebust 	nfs4_put_stid(&stp->st_stid);
61521da177e4SLinus Torvalds out:
61531da177e4SLinus Torvalds 	return status;
61541da177e4SLinus Torvalds }
61551da177e4SLinus Torvalds 
6156b37ad28bSAl Viro __be32
6157ca364317SJ.Bruce Fields nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6158eb69853dSChristoph Hellwig 		  union nfsd4_op_u *u)
61591da177e4SLinus Torvalds {
6160eb69853dSChristoph Hellwig 	struct nfsd4_delegreturn *dr = &u->delegreturn;
6161203a8c8eSJ. Bruce Fields 	struct nfs4_delegation *dp;
6162203a8c8eSJ. Bruce Fields 	stateid_t *stateid = &dr->dr_stateid;
616338c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
6164b37ad28bSAl Viro 	__be32 status;
61653320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
61661da177e4SLinus Torvalds 
6167ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6168203a8c8eSJ. Bruce Fields 		return status;
61691da177e4SLinus Torvalds 
61702dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
617138c2f4b1SJ. Bruce Fields 	if (status)
6172203a8c8eSJ. Bruce Fields 		goto out;
617338c2f4b1SJ. Bruce Fields 	dp = delegstateid(s);
617403da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6175203a8c8eSJ. Bruce Fields 	if (status)
6176fd911011STrond Myklebust 		goto put_stateid;
6177203a8c8eSJ. Bruce Fields 
61783bd64a5bSJ. Bruce Fields 	destroy_delegation(dp);
6179fd911011STrond Myklebust put_stateid:
6180fd911011STrond Myklebust 	nfs4_put_stid(&dp->dl_stid);
61811da177e4SLinus Torvalds out:
61821da177e4SLinus Torvalds 	return status;
61831da177e4SLinus Torvalds }
61841da177e4SLinus Torvalds 
618587df4de8SBenny Halevy static inline u64
618687df4de8SBenny Halevy end_offset(u64 start, u64 len)
618787df4de8SBenny Halevy {
618887df4de8SBenny Halevy 	u64 end;
618987df4de8SBenny Halevy 
619087df4de8SBenny Halevy 	end = start + len;
619187df4de8SBenny Halevy 	return end >= start ? end: NFS4_MAX_UINT64;
619287df4de8SBenny Halevy }
619387df4de8SBenny Halevy 
619487df4de8SBenny Halevy /* last octet in a range */
619587df4de8SBenny Halevy static inline u64
619687df4de8SBenny Halevy last_byte_offset(u64 start, u64 len)
619787df4de8SBenny Halevy {
619887df4de8SBenny Halevy 	u64 end;
619987df4de8SBenny Halevy 
6200063b0fb9SJ. Bruce Fields 	WARN_ON_ONCE(!len);
620187df4de8SBenny Halevy 	end = start + len;
620287df4de8SBenny Halevy 	return end > start ? end - 1: NFS4_MAX_UINT64;
620387df4de8SBenny Halevy }
620487df4de8SBenny Halevy 
62051da177e4SLinus Torvalds /*
62061da177e4SLinus Torvalds  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
62071da177e4SLinus Torvalds  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
62081da177e4SLinus Torvalds  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
62091da177e4SLinus Torvalds  * locking, this prevents us from being completely protocol-compliant.  The
62101da177e4SLinus Torvalds  * real solution to this problem is to start using unsigned file offsets in
62111da177e4SLinus Torvalds  * the VFS, but this is a very deep change!
62121da177e4SLinus Torvalds  */
62131da177e4SLinus Torvalds static inline void
62141da177e4SLinus Torvalds nfs4_transform_lock_offset(struct file_lock *lock)
62151da177e4SLinus Torvalds {
62161da177e4SLinus Torvalds 	if (lock->fl_start < 0)
62171da177e4SLinus Torvalds 		lock->fl_start = OFFSET_MAX;
62181da177e4SLinus Torvalds 	if (lock->fl_end < 0)
62191da177e4SLinus Torvalds 		lock->fl_end = OFFSET_MAX;
62201da177e4SLinus Torvalds }
62211da177e4SLinus Torvalds 
6222cae80b30SJeff Layton static fl_owner_t
6223cae80b30SJeff Layton nfsd4_fl_get_owner(fl_owner_t owner)
6224aef9583bSKinglong Mee {
6225cae80b30SJeff Layton 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6226cae80b30SJeff Layton 
6227cae80b30SJeff Layton 	nfs4_get_stateowner(&lo->lo_owner);
6228cae80b30SJeff Layton 	return owner;
6229aef9583bSKinglong Mee }
6230aef9583bSKinglong Mee 
6231cae80b30SJeff Layton static void
6232cae80b30SJeff Layton nfsd4_fl_put_owner(fl_owner_t owner)
6233aef9583bSKinglong Mee {
6234cae80b30SJeff Layton 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6235aef9583bSKinglong Mee 
6236cae80b30SJeff Layton 	if (lo)
6237aef9583bSKinglong Mee 		nfs4_put_stateowner(&lo->lo_owner);
6238aef9583bSKinglong Mee }
6239aef9583bSKinglong Mee 
624076d348faSJeff Layton static void
624176d348faSJeff Layton nfsd4_lm_notify(struct file_lock *fl)
624276d348faSJeff Layton {
624376d348faSJeff Layton 	struct nfs4_lockowner		*lo = (struct nfs4_lockowner *)fl->fl_owner;
624476d348faSJeff Layton 	struct net			*net = lo->lo_owner.so_client->net;
624576d348faSJeff Layton 	struct nfsd_net			*nn = net_generic(net, nfsd_net_id);
624676d348faSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(fl,
624776d348faSJeff Layton 						struct nfsd4_blocked_lock, nbl_lock);
624876d348faSJeff Layton 	bool queue = false;
624976d348faSJeff Layton 
62507919d0a2SJeff Layton 	/* An empty list means that something else is going to be using it */
62510cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
625276d348faSJeff Layton 	if (!list_empty(&nbl->nbl_list)) {
625376d348faSJeff Layton 		list_del_init(&nbl->nbl_list);
62547919d0a2SJeff Layton 		list_del_init(&nbl->nbl_lru);
625576d348faSJeff Layton 		queue = true;
625676d348faSJeff Layton 	}
62570cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
625876d348faSJeff Layton 
625976d348faSJeff Layton 	if (queue)
626076d348faSJeff Layton 		nfsd4_run_cb(&nbl->nbl_cb);
626176d348faSJeff Layton }
626276d348faSJeff Layton 
62637b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_posix_mng_ops  = {
626476d348faSJeff Layton 	.lm_notify = nfsd4_lm_notify,
6265aef9583bSKinglong Mee 	.lm_get_owner = nfsd4_fl_get_owner,
6266aef9583bSKinglong Mee 	.lm_put_owner = nfsd4_fl_put_owner,
6267d5b9026aSNeilBrown };
62681da177e4SLinus Torvalds 
62691da177e4SLinus Torvalds static inline void
62701da177e4SLinus Torvalds nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
62711da177e4SLinus Torvalds {
6272fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
62731da177e4SLinus Torvalds 
6274d5b9026aSNeilBrown 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6275fe0750e5SJ. Bruce Fields 		lo = (struct nfs4_lockowner *) fl->fl_owner;
62766f4859b8SJ. Bruce Fields 		xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
62776f4859b8SJ. Bruce Fields 						GFP_KERNEL);
62787c13f344SJ. Bruce Fields 		if (!deny->ld_owner.data)
62797c13f344SJ. Bruce Fields 			/* We just don't care that much */
62807c13f344SJ. Bruce Fields 			goto nevermind;
6281fe0750e5SJ. Bruce Fields 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6282d5b9026aSNeilBrown 	} else {
62837c13f344SJ. Bruce Fields nevermind:
62847c13f344SJ. Bruce Fields 		deny->ld_owner.len = 0;
62857c13f344SJ. Bruce Fields 		deny->ld_owner.data = NULL;
6286d5b9026aSNeilBrown 		deny->ld_clientid.cl_boot = 0;
6287d5b9026aSNeilBrown 		deny->ld_clientid.cl_id = 0;
62881da177e4SLinus Torvalds 	}
62891da177e4SLinus Torvalds 	deny->ld_start = fl->fl_start;
629087df4de8SBenny Halevy 	deny->ld_length = NFS4_MAX_UINT64;
629187df4de8SBenny Halevy 	if (fl->fl_end != NFS4_MAX_UINT64)
62921da177e4SLinus Torvalds 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
62931da177e4SLinus Torvalds 	deny->ld_type = NFS4_READ_LT;
62941da177e4SLinus Torvalds 	if (fl->fl_type != F_RDLCK)
62951da177e4SLinus Torvalds 		deny->ld_type = NFS4_WRITE_LT;
62961da177e4SLinus Torvalds }
62971da177e4SLinus Torvalds 
6298fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
6299c8623999SKinglong Mee find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
63001da177e4SLinus Torvalds {
6301d4f0489fSTrond Myklebust 	unsigned int strhashval = ownerstr_hashval(owner);
6302b3c32bcdSTrond Myklebust 	struct nfs4_stateowner *so;
63031da177e4SLinus Torvalds 
63040a880a28STrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
63050a880a28STrond Myklebust 
6306d4f0489fSTrond Myklebust 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6307d4f0489fSTrond Myklebust 			    so_strhash) {
6308b3c32bcdSTrond Myklebust 		if (so->so_is_open_owner)
6309b3c32bcdSTrond Myklebust 			continue;
6310b5971afaSKinglong Mee 		if (same_owner_str(so, owner))
6311b5971afaSKinglong Mee 			return lockowner(nfs4_get_stateowner(so));
63121da177e4SLinus Torvalds 	}
63131da177e4SLinus Torvalds 	return NULL;
63141da177e4SLinus Torvalds }
63151da177e4SLinus Torvalds 
6316c58c6610STrond Myklebust static struct nfs4_lockowner *
6317c8623999SKinglong Mee find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6318c58c6610STrond Myklebust {
6319c58c6610STrond Myklebust 	struct nfs4_lockowner *lo;
6320c58c6610STrond Myklebust 
6321d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
6322c8623999SKinglong Mee 	lo = find_lockowner_str_locked(clp, owner);
6323d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
6324c58c6610STrond Myklebust 	return lo;
6325c58c6610STrond Myklebust }
6326c58c6610STrond Myklebust 
63278f4b54c5SJeff Layton static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
63288f4b54c5SJeff Layton {
6329c58c6610STrond Myklebust 	unhash_lockowner_locked(lockowner(sop));
63308f4b54c5SJeff Layton }
63318f4b54c5SJeff Layton 
63326b180f0bSJeff Layton static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
63336b180f0bSJeff Layton {
63346b180f0bSJeff Layton 	struct nfs4_lockowner *lo = lockowner(sop);
63356b180f0bSJeff Layton 
63366b180f0bSJeff Layton 	kmem_cache_free(lockowner_slab, lo);
63376b180f0bSJeff Layton }
63386b180f0bSJeff Layton 
63396b180f0bSJeff Layton static const struct nfs4_stateowner_operations lockowner_ops = {
63408f4b54c5SJeff Layton 	.so_unhash =	nfs4_unhash_lockowner,
63416b180f0bSJeff Layton 	.so_free =	nfs4_free_lockowner,
63426b180f0bSJeff Layton };
63436b180f0bSJeff Layton 
63441da177e4SLinus Torvalds /*
63451da177e4SLinus Torvalds  * Alloc a lock owner structure.
63461da177e4SLinus Torvalds  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
634725985edcSLucas De Marchi  * occurred.
63481da177e4SLinus Torvalds  *
634916bfdaafSJ. Bruce Fields  * strhashval = ownerstr_hashval
63501da177e4SLinus Torvalds  */
6351fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
6352c58c6610STrond Myklebust alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6353c58c6610STrond Myklebust 			   struct nfs4_ol_stateid *open_stp,
6354c58c6610STrond Myklebust 			   struct nfsd4_lock *lock)
6355c58c6610STrond Myklebust {
6356c58c6610STrond Myklebust 	struct nfs4_lockowner *lo, *ret;
63571da177e4SLinus Torvalds 
6358fe0750e5SJ. Bruce Fields 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6359fe0750e5SJ. Bruce Fields 	if (!lo)
63601da177e4SLinus Torvalds 		return NULL;
636176d348faSJeff Layton 	INIT_LIST_HEAD(&lo->lo_blocked);
6362fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6363fe0750e5SJ. Bruce Fields 	lo->lo_owner.so_is_open_owner = 0;
63645db1c03fSJeff Layton 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
63656b180f0bSJeff Layton 	lo->lo_owner.so_ops = &lockowner_ops;
6366d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
6367c8623999SKinglong Mee 	ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6368c58c6610STrond Myklebust 	if (ret == NULL) {
6369c58c6610STrond Myklebust 		list_add(&lo->lo_owner.so_strhash,
6370d4f0489fSTrond Myklebust 			 &clp->cl_ownerstr_hashtbl[strhashval]);
6371c58c6610STrond Myklebust 		ret = lo;
6372c58c6610STrond Myklebust 	} else
6373d50ffdedSKinglong Mee 		nfs4_free_stateowner(&lo->lo_owner);
6374d50ffdedSKinglong Mee 
6375d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
6376340f0ba1SJ. Bruce Fields 	return ret;
63771da177e4SLinus Torvalds }
63781da177e4SLinus Torvalds 
6379fd1fd685STrond Myklebust static struct nfs4_ol_stateid *
6380a451b123STrond Myklebust find_lock_stateid(const struct nfs4_lockowner *lo,
6381a451b123STrond Myklebust 		  const struct nfs4_ol_stateid *ost)
6382fd1fd685STrond Myklebust {
6383fd1fd685STrond Myklebust 	struct nfs4_ol_stateid *lst;
6384fd1fd685STrond Myklebust 
6385a451b123STrond Myklebust 	lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6386fd1fd685STrond Myklebust 
6387a451b123STrond Myklebust 	/* If ost is not hashed, ost->st_locks will not be valid */
6388a451b123STrond Myklebust 	if (!nfs4_ol_stateid_unhashed(ost))
6389a451b123STrond Myklebust 		list_for_each_entry(lst, &ost->st_locks, st_locks) {
6390a451b123STrond Myklebust 			if (lst->st_stateowner == &lo->lo_owner) {
6391fd1fd685STrond Myklebust 				refcount_inc(&lst->st_stid.sc_count);
6392fd1fd685STrond Myklebust 				return lst;
6393fd1fd685STrond Myklebust 			}
6394fd1fd685STrond Myklebust 		}
6395fd1fd685STrond Myklebust 	return NULL;
6396fd1fd685STrond Myklebust }
6397fd1fd685STrond Myklebust 
6398beeca19cSTrond Myklebust static struct nfs4_ol_stateid *
6399356a95ecSJeff Layton init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6400356a95ecSJeff Layton 		  struct nfs4_file *fp, struct inode *inode,
6401f9c00c3aSJeff Layton 		  struct nfs4_ol_stateid *open_stp)
64021da177e4SLinus Torvalds {
6403d3b313a4SJ. Bruce Fields 	struct nfs4_client *clp = lo->lo_owner.so_client;
6404beeca19cSTrond Myklebust 	struct nfs4_ol_stateid *retstp;
64051da177e4SLinus Torvalds 
6406beeca19cSTrond Myklebust 	mutex_init(&stp->st_mutex);
64074f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6408beeca19cSTrond Myklebust retry:
6409beeca19cSTrond Myklebust 	spin_lock(&clp->cl_lock);
6410a451b123STrond Myklebust 	if (nfs4_ol_stateid_unhashed(open_stp))
6411a451b123STrond Myklebust 		goto out_close;
6412a451b123STrond Myklebust 	retstp = find_lock_stateid(lo, open_stp);
6413beeca19cSTrond Myklebust 	if (retstp)
6414a451b123STrond Myklebust 		goto out_found;
6415a15dfcd5SElena Reshetova 	refcount_inc(&stp->st_stid.sc_count);
64163abdb607SJ. Bruce Fields 	stp->st_stid.sc_type = NFS4_LOCK_STID;
6417b5971afaSKinglong Mee 	stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
641813cd2184SNeilBrown 	get_nfs4_file(fp);
641911b9164aSTrond Myklebust 	stp->st_stid.sc_file = fp;
64200997b173SJ. Bruce Fields 	stp->st_access_bmap = 0;
64211da177e4SLinus Torvalds 	stp->st_deny_bmap = open_stp->st_deny_bmap;
64224c4cd222SNeilBrown 	stp->st_openstp = open_stp;
6423a451b123STrond Myklebust 	spin_lock(&fp->fi_lock);
64243c87b9b7STrond Myklebust 	list_add(&stp->st_locks, &open_stp->st_locks);
64251c755dc1SJeff Layton 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
64261d31a253STrond Myklebust 	list_add(&stp->st_perfile, &fp->fi_stateids);
64271d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
6428beeca19cSTrond Myklebust 	spin_unlock(&clp->cl_lock);
6429a451b123STrond Myklebust 	return stp;
6430a451b123STrond Myklebust out_found:
6431a451b123STrond Myklebust 	spin_unlock(&clp->cl_lock);
6432beeca19cSTrond Myklebust 	if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6433beeca19cSTrond Myklebust 		nfs4_put_stid(&retstp->st_stid);
6434beeca19cSTrond Myklebust 		goto retry;
6435beeca19cSTrond Myklebust 	}
6436beeca19cSTrond Myklebust 	/* To keep mutex tracking happy */
6437beeca19cSTrond Myklebust 	mutex_unlock(&stp->st_mutex);
6438a451b123STrond Myklebust 	return retstp;
6439a451b123STrond Myklebust out_close:
6440a451b123STrond Myklebust 	spin_unlock(&clp->cl_lock);
6441a451b123STrond Myklebust 	mutex_unlock(&stp->st_mutex);
6442a451b123STrond Myklebust 	return NULL;
64431da177e4SLinus Torvalds }
64441da177e4SLinus Torvalds 
6445c53530daSJeff Layton static struct nfs4_ol_stateid *
6446356a95ecSJeff Layton find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6447356a95ecSJeff Layton 			    struct inode *inode, struct nfs4_ol_stateid *ost,
6448356a95ecSJeff Layton 			    bool *new)
6449356a95ecSJeff Layton {
6450356a95ecSJeff Layton 	struct nfs4_stid *ns = NULL;
6451356a95ecSJeff Layton 	struct nfs4_ol_stateid *lst;
6452356a95ecSJeff Layton 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6453356a95ecSJeff Layton 	struct nfs4_client *clp = oo->oo_owner.so_client;
6454356a95ecSJeff Layton 
6455beeca19cSTrond Myklebust 	*new = false;
6456356a95ecSJeff Layton 	spin_lock(&clp->cl_lock);
6457a451b123STrond Myklebust 	lst = find_lock_stateid(lo, ost);
6458356a95ecSJeff Layton 	spin_unlock(&clp->cl_lock);
6459beeca19cSTrond Myklebust 	if (lst != NULL) {
6460beeca19cSTrond Myklebust 		if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6461beeca19cSTrond Myklebust 			goto out;
6462beeca19cSTrond Myklebust 		nfs4_put_stid(&lst->st_stid);
6463beeca19cSTrond Myklebust 	}
6464d19fb70dSKinglong Mee 	ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6465356a95ecSJeff Layton 	if (ns == NULL)
6466356a95ecSJeff Layton 		return NULL;
6467356a95ecSJeff Layton 
6468beeca19cSTrond Myklebust 	lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6469beeca19cSTrond Myklebust 	if (lst == openlockstateid(ns))
6470356a95ecSJeff Layton 		*new = true;
6471beeca19cSTrond Myklebust 	else
6472356a95ecSJeff Layton 		nfs4_put_stid(ns);
6473beeca19cSTrond Myklebust out:
6474356a95ecSJeff Layton 	return lst;
6475356a95ecSJeff Layton }
6476c53530daSJeff Layton 
6477fd39ca9aSNeilBrown static int
64781da177e4SLinus Torvalds check_lock_length(u64 offset, u64 length)
64791da177e4SLinus Torvalds {
648087df4de8SBenny Halevy 	return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6481e7969315SKinglong Mee 		(length > ~offset)));
64821da177e4SLinus Torvalds }
64831da177e4SLinus Torvalds 
6484dcef0413SJ. Bruce Fields static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
64850997b173SJ. Bruce Fields {
648611b9164aSTrond Myklebust 	struct nfs4_file *fp = lock_stp->st_stid.sc_file;
64870997b173SJ. Bruce Fields 
64887214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
64897214e860SJeff Layton 
649082c5ff1bSJeff Layton 	if (test_access(access, lock_stp))
64910997b173SJ. Bruce Fields 		return;
649212659651SJeff Layton 	__nfs4_file_get_access(fp, access);
649382c5ff1bSJeff Layton 	set_access(access, lock_stp);
64940997b173SJ. Bruce Fields }
64950997b173SJ. Bruce Fields 
6496356a95ecSJeff Layton static __be32
6497356a95ecSJeff Layton lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6498356a95ecSJeff Layton 			    struct nfs4_ol_stateid *ost,
6499356a95ecSJeff Layton 			    struct nfsd4_lock *lock,
6500dd257933SJeff Layton 			    struct nfs4_ol_stateid **plst, bool *new)
650164a284d0SJ. Bruce Fields {
65025db1c03fSJeff Layton 	__be32 status;
650311b9164aSTrond Myklebust 	struct nfs4_file *fi = ost->st_stid.sc_file;
650464a284d0SJ. Bruce Fields 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
650564a284d0SJ. Bruce Fields 	struct nfs4_client *cl = oo->oo_owner.so_client;
65062b0143b5SDavid Howells 	struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
650764a284d0SJ. Bruce Fields 	struct nfs4_lockowner *lo;
6508dd257933SJeff Layton 	struct nfs4_ol_stateid *lst;
650964a284d0SJ. Bruce Fields 	unsigned int strhashval;
651064a284d0SJ. Bruce Fields 
6511c8623999SKinglong Mee 	lo = find_lockowner_str(cl, &lock->lk_new_owner);
6512c53530daSJeff Layton 	if (!lo) {
651376f6c9e1SKinglong Mee 		strhashval = ownerstr_hashval(&lock->lk_new_owner);
651464a284d0SJ. Bruce Fields 		lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
651564a284d0SJ. Bruce Fields 		if (lo == NULL)
651664a284d0SJ. Bruce Fields 			return nfserr_jukebox;
6517c53530daSJeff Layton 	} else {
6518c53530daSJeff Layton 		/* with an existing lockowner, seqids must be the same */
65195db1c03fSJeff Layton 		status = nfserr_bad_seqid;
6520c53530daSJeff Layton 		if (!cstate->minorversion &&
6521c53530daSJeff Layton 		    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
65225db1c03fSJeff Layton 			goto out;
6523c53530daSJeff Layton 	}
6524c53530daSJeff Layton 
6525dd257933SJeff Layton 	lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6526dd257933SJeff Layton 	if (lst == NULL) {
65275db1c03fSJeff Layton 		status = nfserr_jukebox;
65285db1c03fSJeff Layton 		goto out;
652964a284d0SJ. Bruce Fields 	}
6530dd257933SJeff Layton 
65315db1c03fSJeff Layton 	status = nfs_ok;
6532dd257933SJeff Layton 	*plst = lst;
65335db1c03fSJeff Layton out:
65345db1c03fSJeff Layton 	nfs4_put_stateowner(&lo->lo_owner);
65355db1c03fSJeff Layton 	return status;
653664a284d0SJ. Bruce Fields }
653764a284d0SJ. Bruce Fields 
65381da177e4SLinus Torvalds /*
65391da177e4SLinus Torvalds  *  LOCK operation
65401da177e4SLinus Torvalds  */
6541b37ad28bSAl Viro __be32
6542ca364317SJ.Bruce Fields nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6543eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
65441da177e4SLinus Torvalds {
6545eb69853dSChristoph Hellwig 	struct nfsd4_lock *lock = &u->lock;
6546fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *open_sop = NULL;
6547fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lock_sop = NULL;
65483d0fabd5STrond Myklebust 	struct nfs4_ol_stateid *lock_stp = NULL;
65490667b1e9STrond Myklebust 	struct nfs4_ol_stateid *open_stp = NULL;
65507214e860SJeff Layton 	struct nfs4_file *fp;
6551eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
655276d348faSJeff Layton 	struct nfsd4_blocked_lock *nbl = NULL;
655321179d81SJeff Layton 	struct file_lock *file_lock = NULL;
655421179d81SJeff Layton 	struct file_lock *conflock = NULL;
6555b37ad28bSAl Viro 	__be32 status = 0;
6556b34f27aaSJ. Bruce Fields 	int lkflg;
6557b8dd7b9aSAl Viro 	int err;
65585db1c03fSJeff Layton 	bool new = false;
655976d348faSJeff Layton 	unsigned char fl_type;
656076d348faSJeff Layton 	unsigned int fl_flags = FL_POSIX;
65613320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
65623320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
65631da177e4SLinus Torvalds 
65641da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
65651da177e4SLinus Torvalds 		(long long) lock->lk_offset,
65661da177e4SLinus Torvalds 		(long long) lock->lk_length);
65671da177e4SLinus Torvalds 
65681da177e4SLinus Torvalds 	if (check_lock_length(lock->lk_offset, lock->lk_length))
65691da177e4SLinus Torvalds 		 return nfserr_inval;
65701da177e4SLinus Torvalds 
6571ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh,
65728837abcaSMiklos Szeredi 				S_IFREG, NFSD_MAY_LOCK))) {
6573a6f6ef2fSAndy Adamson 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
6574a6f6ef2fSAndy Adamson 		return status;
6575a6f6ef2fSAndy Adamson 	}
6576a6f6ef2fSAndy Adamson 
65771da177e4SLinus Torvalds 	if (lock->lk_is_new) {
6578684e5638SJ. Bruce Fields 		if (nfsd4_has_session(cstate))
6579684e5638SJ. Bruce Fields 			/* See rfc 5661 18.10.3: given clientid is ignored: */
658076f6c9e1SKinglong Mee 			memcpy(&lock->lk_new_clientid,
6581684e5638SJ. Bruce Fields 				&cstate->session->se_client->cl_clientid,
6582684e5638SJ. Bruce Fields 				sizeof(clientid_t));
6583684e5638SJ. Bruce Fields 
65841da177e4SLinus Torvalds 		status = nfserr_stale_clientid;
65852c142baaSStanislav Kinsbursky 		if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
65861da177e4SLinus Torvalds 			goto out;
65871da177e4SLinus Torvalds 
65881da177e4SLinus Torvalds 		/* validate and update open stateid and open seqid */
6589c0a5d93eSJ. Bruce Fields 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
65901da177e4SLinus Torvalds 				        lock->lk_new_open_seqid,
65911da177e4SLinus Torvalds 		                        &lock->lk_new_open_stateid,
65923320fef1SStanislav Kinsbursky 					&open_stp, nn);
659337515177SNeilBrown 		if (status)
65941da177e4SLinus Torvalds 			goto out;
6595feb9dad5SOleg Drokin 		mutex_unlock(&open_stp->st_mutex);
6596fe0750e5SJ. Bruce Fields 		open_sop = openowner(open_stp->st_stateowner);
6597b34f27aaSJ. Bruce Fields 		status = nfserr_bad_stateid;
6598684e5638SJ. Bruce Fields 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
659976f6c9e1SKinglong Mee 						&lock->lk_new_clientid))
6600b34f27aaSJ. Bruce Fields 			goto out;
660164a284d0SJ. Bruce Fields 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
66025db1c03fSJeff Layton 							&lock_stp, &new);
66033d0fabd5STrond Myklebust 	} else {
6604dd453dfdSBenny Halevy 		status = nfs4_preprocess_seqid_op(cstate,
66051da177e4SLinus Torvalds 				       lock->lk_old_lock_seqid,
66061da177e4SLinus Torvalds 				       &lock->lk_old_lock_stateid,
66073320fef1SStanislav Kinsbursky 				       NFS4_LOCK_STID, &lock_stp, nn);
66083d0fabd5STrond Myklebust 	}
66091da177e4SLinus Torvalds 	if (status)
66101da177e4SLinus Torvalds 		goto out;
6611fe0750e5SJ. Bruce Fields 	lock_sop = lockowner(lock_stp->st_stateowner);
66121da177e4SLinus Torvalds 
6613b34f27aaSJ. Bruce Fields 	lkflg = setlkflg(lock->lk_type);
6614b34f27aaSJ. Bruce Fields 	status = nfs4_check_openmode(lock_stp, lkflg);
6615b34f27aaSJ. Bruce Fields 	if (status)
6616b34f27aaSJ. Bruce Fields 		goto out;
6617b34f27aaSJ. Bruce Fields 
66180dd395dcSNeilBrown 	status = nfserr_grace;
66193320fef1SStanislav Kinsbursky 	if (locks_in_grace(net) && !lock->lk_reclaim)
66200dd395dcSNeilBrown 		goto out;
66210dd395dcSNeilBrown 	status = nfserr_no_grace;
66223320fef1SStanislav Kinsbursky 	if (!locks_in_grace(net) && lock->lk_reclaim)
66230dd395dcSNeilBrown 		goto out;
66240dd395dcSNeilBrown 
662511b9164aSTrond Myklebust 	fp = lock_stp->st_stid.sc_file;
66261da177e4SLinus Torvalds 	switch (lock->lk_type) {
66271da177e4SLinus Torvalds 		case NFS4_READW_LT:
662876d348faSJeff Layton 			if (nfsd4_has_session(cstate))
662976d348faSJeff Layton 				fl_flags |= FL_SLEEP;
663076d348faSJeff Layton 			/* Fallthrough */
663176d348faSJeff Layton 		case NFS4_READ_LT:
66327214e860SJeff Layton 			spin_lock(&fp->fi_lock);
6633eb82dd39SJeff Layton 			nf = find_readable_file_locked(fp);
6634eb82dd39SJeff Layton 			if (nf)
66350997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
66367214e860SJeff Layton 			spin_unlock(&fp->fi_lock);
663776d348faSJeff Layton 			fl_type = F_RDLCK;
66381da177e4SLinus Torvalds 			break;
66391da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
664076d348faSJeff Layton 			if (nfsd4_has_session(cstate))
664176d348faSJeff Layton 				fl_flags |= FL_SLEEP;
664276d348faSJeff Layton 			/* Fallthrough */
664376d348faSJeff Layton 		case NFS4_WRITE_LT:
66447214e860SJeff Layton 			spin_lock(&fp->fi_lock);
6645eb82dd39SJeff Layton 			nf = find_writeable_file_locked(fp);
6646eb82dd39SJeff Layton 			if (nf)
66470997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
66487214e860SJeff Layton 			spin_unlock(&fp->fi_lock);
664976d348faSJeff Layton 			fl_type = F_WRLCK;
66501da177e4SLinus Torvalds 			break;
66511da177e4SLinus Torvalds 		default:
66521da177e4SLinus Torvalds 			status = nfserr_inval;
66531da177e4SLinus Torvalds 		goto out;
66541da177e4SLinus Torvalds 	}
665576d348faSJeff Layton 
6656eb82dd39SJeff Layton 	if (!nf) {
6657f9d7562fSJ. Bruce Fields 		status = nfserr_openmode;
6658f9d7562fSJ. Bruce Fields 		goto out;
6659f9d7562fSJ. Bruce Fields 	}
6660aef9583bSKinglong Mee 
666176d348faSJeff Layton 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
666276d348faSJeff Layton 	if (!nbl) {
666376d348faSJeff Layton 		dprintk("NFSD: %s: unable to allocate block!\n", __func__);
666476d348faSJeff Layton 		status = nfserr_jukebox;
666576d348faSJeff Layton 		goto out;
666676d348faSJeff Layton 	}
666776d348faSJeff Layton 
666876d348faSJeff Layton 	file_lock = &nbl->nbl_lock;
666976d348faSJeff Layton 	file_lock->fl_type = fl_type;
6670aef9583bSKinglong Mee 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
667121179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
6672eb82dd39SJeff Layton 	file_lock->fl_file = nf->nf_file;
667376d348faSJeff Layton 	file_lock->fl_flags = fl_flags;
667421179d81SJeff Layton 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
667521179d81SJeff Layton 	file_lock->fl_start = lock->lk_offset;
667621179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
667721179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
66781da177e4SLinus Torvalds 
667921179d81SJeff Layton 	conflock = locks_alloc_lock();
668021179d81SJeff Layton 	if (!conflock) {
668121179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
668221179d81SJeff Layton 		status = nfserr_jukebox;
668321179d81SJeff Layton 		goto out;
668421179d81SJeff Layton 	}
66851da177e4SLinus Torvalds 
668676d348faSJeff Layton 	if (fl_flags & FL_SLEEP) {
668720b7d86fSArnd Bergmann 		nbl->nbl_time = ktime_get_boottime_seconds();
66880cc11a61SJeff Layton 		spin_lock(&nn->blocked_locks_lock);
668976d348faSJeff Layton 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
66907919d0a2SJeff Layton 		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
66910cc11a61SJeff Layton 		spin_unlock(&nn->blocked_locks_lock);
669276d348faSJeff Layton 	}
669376d348faSJeff Layton 
6694eb82dd39SJeff Layton 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
669576d348faSJeff Layton 	switch (err) {
66961da177e4SLinus Torvalds 	case 0: /* success! */
66979767feb2SJeff Layton 		nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6698b8dd7b9aSAl Viro 		status = 0;
669903f318caSJ. Bruce Fields 		if (lock->lk_reclaim)
670003f318caSJ. Bruce Fields 			nn->somebody_reclaimed = true;
6701eb76b3fdSAndy Adamson 		break;
670276d348faSJeff Layton 	case FILE_LOCK_DEFERRED:
670376d348faSJeff Layton 		nbl = NULL;
670476d348faSJeff Layton 		/* Fallthrough */
670576d348faSJeff Layton 	case -EAGAIN:		/* conflock holds conflicting lock */
6706eb76b3fdSAndy Adamson 		status = nfserr_denied;
6707eb76b3fdSAndy Adamson 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
670821179d81SJeff Layton 		nfs4_set_lock_denied(conflock, &lock->lk_denied);
6709eb76b3fdSAndy Adamson 		break;
671076d348faSJeff Layton 	case -EDEADLK:
67111da177e4SLinus Torvalds 		status = nfserr_deadlock;
6712eb76b3fdSAndy Adamson 		break;
67131da177e4SLinus Torvalds 	default:
6714fd85b817SMarc Eshel 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
67153e772463SJ. Bruce Fields 		status = nfserrno(err);
6716eb76b3fdSAndy Adamson 		break;
67171da177e4SLinus Torvalds 	}
67181da177e4SLinus Torvalds out:
671976d348faSJeff Layton 	if (nbl) {
672076d348faSJeff Layton 		/* dequeue it if we queued it before */
672176d348faSJeff Layton 		if (fl_flags & FL_SLEEP) {
67220cc11a61SJeff Layton 			spin_lock(&nn->blocked_locks_lock);
672376d348faSJeff Layton 			list_del_init(&nbl->nbl_list);
67247919d0a2SJeff Layton 			list_del_init(&nbl->nbl_lru);
67250cc11a61SJeff Layton 			spin_unlock(&nn->blocked_locks_lock);
672676d348faSJeff Layton 		}
672776d348faSJeff Layton 		free_blocked_lock(nbl);
672876d348faSJeff Layton 	}
6729eb82dd39SJeff Layton 	if (nf)
6730eb82dd39SJeff Layton 		nfsd_file_put(nf);
67315db1c03fSJeff Layton 	if (lock_stp) {
67325db1c03fSJeff Layton 		/* Bump seqid manually if the 4.0 replay owner is openowner */
67335db1c03fSJeff Layton 		if (cstate->replay_owner &&
67345db1c03fSJeff Layton 		    cstate->replay_owner != &lock_sop->lo_owner &&
67355db1c03fSJeff Layton 		    seqid_mutating_err(ntohl(status)))
67365db1c03fSJeff Layton 			lock_sop->lo_owner.so_seqid++;
67375db1c03fSJeff Layton 
67385db1c03fSJeff Layton 		/*
67395db1c03fSJeff Layton 		 * If this is a new, never-before-used stateid, and we are
67405db1c03fSJeff Layton 		 * returning an error, then just go ahead and release it.
67415db1c03fSJeff Layton 		 */
674225020720SJ. Bruce Fields 		if (status && new)
67435db1c03fSJeff Layton 			release_lock_stateid(lock_stp);
6744beeca19cSTrond Myklebust 
6745beeca19cSTrond Myklebust 		mutex_unlock(&lock_stp->st_mutex);
67465db1c03fSJeff Layton 
67473d0fabd5STrond Myklebust 		nfs4_put_stid(&lock_stp->st_stid);
67485db1c03fSJeff Layton 	}
67490667b1e9STrond Myklebust 	if (open_stp)
67500667b1e9STrond Myklebust 		nfs4_put_stid(&open_stp->st_stid);
67519411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
675221179d81SJeff Layton 	if (conflock)
675321179d81SJeff Layton 		locks_free_lock(conflock);
67541da177e4SLinus Torvalds 	return status;
67551da177e4SLinus Torvalds }
67561da177e4SLinus Torvalds 
67571da177e4SLinus Torvalds /*
675855ef1274SJ. Bruce Fields  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
675955ef1274SJ. Bruce Fields  * so we do a temporary open here just to get an open file to pass to
676055ef1274SJ. Bruce Fields  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
676155ef1274SJ. Bruce Fields  * inode operation.)
676255ef1274SJ. Bruce Fields  */
676304da6e9dSAl Viro static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
676455ef1274SJ. Bruce Fields {
67656b556ca2SJeff Layton 	struct nfsd_file *nf;
67666b556ca2SJeff Layton 	__be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
676704da6e9dSAl Viro 	if (!err) {
67686b556ca2SJeff Layton 		err = nfserrno(vfs_test_lock(nf->nf_file, lock));
67696b556ca2SJeff Layton 		nfsd_file_put(nf);
677004da6e9dSAl Viro 	}
677155ef1274SJ. Bruce Fields 	return err;
677255ef1274SJ. Bruce Fields }
677355ef1274SJ. Bruce Fields 
677455ef1274SJ. Bruce Fields /*
67751da177e4SLinus Torvalds  * LOCKT operation
67761da177e4SLinus Torvalds  */
6777b37ad28bSAl Viro __be32
6778ca364317SJ.Bruce Fields nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6779eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
67801da177e4SLinus Torvalds {
6781eb69853dSChristoph Hellwig 	struct nfsd4_lockt *lockt = &u->lockt;
678221179d81SJeff Layton 	struct file_lock *file_lock = NULL;
67835db1c03fSJeff Layton 	struct nfs4_lockowner *lo = NULL;
6784b37ad28bSAl Viro 	__be32 status;
67857f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
67861da177e4SLinus Torvalds 
67875ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp)))
67881da177e4SLinus Torvalds 		return nfserr_grace;
67891da177e4SLinus Torvalds 
67901da177e4SLinus Torvalds 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
67911da177e4SLinus Torvalds 		 return nfserr_inval;
67921da177e4SLinus Torvalds 
67939b2ef62bSJ. Bruce Fields 	if (!nfsd4_has_session(cstate)) {
6794b7342204SOlga Kornievskaia 		status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6795b7342204SOlga Kornievskaia 					 false);
67969b2ef62bSJ. Bruce Fields 		if (status)
67971da177e4SLinus Torvalds 			goto out;
67989b2ef62bSJ. Bruce Fields 	}
67991da177e4SLinus Torvalds 
680075c096f7SJ. Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
68011da177e4SLinus Torvalds 		goto out;
68021da177e4SLinus Torvalds 
680321179d81SJeff Layton 	file_lock = locks_alloc_lock();
680421179d81SJeff Layton 	if (!file_lock) {
680521179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
680621179d81SJeff Layton 		status = nfserr_jukebox;
680721179d81SJeff Layton 		goto out;
680821179d81SJeff Layton 	}
68096cd90662SKinglong Mee 
68101da177e4SLinus Torvalds 	switch (lockt->lt_type) {
68111da177e4SLinus Torvalds 		case NFS4_READ_LT:
68121da177e4SLinus Torvalds 		case NFS4_READW_LT:
681321179d81SJeff Layton 			file_lock->fl_type = F_RDLCK;
68141da177e4SLinus Torvalds 			break;
68151da177e4SLinus Torvalds 		case NFS4_WRITE_LT:
68161da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
681721179d81SJeff Layton 			file_lock->fl_type = F_WRLCK;
68181da177e4SLinus Torvalds 			break;
68191da177e4SLinus Torvalds 		default:
68202fdada03SJ. Bruce Fields 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
68211da177e4SLinus Torvalds 			status = nfserr_inval;
68221da177e4SLinus Torvalds 			goto out;
68231da177e4SLinus Torvalds 	}
68241da177e4SLinus Torvalds 
6825c8623999SKinglong Mee 	lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6826fe0750e5SJ. Bruce Fields 	if (lo)
682721179d81SJeff Layton 		file_lock->fl_owner = (fl_owner_t)lo;
682821179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
682921179d81SJeff Layton 	file_lock->fl_flags = FL_POSIX;
68301da177e4SLinus Torvalds 
683121179d81SJeff Layton 	file_lock->fl_start = lockt->lt_offset;
683221179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
68331da177e4SLinus Torvalds 
683421179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
68351da177e4SLinus Torvalds 
683621179d81SJeff Layton 	status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
683704da6e9dSAl Viro 	if (status)
6838fd85b817SMarc Eshel 		goto out;
683904da6e9dSAl Viro 
684021179d81SJeff Layton 	if (file_lock->fl_type != F_UNLCK) {
68411da177e4SLinus Torvalds 		status = nfserr_denied;
684221179d81SJeff Layton 		nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
68431da177e4SLinus Torvalds 	}
68441da177e4SLinus Torvalds out:
68455db1c03fSJeff Layton 	if (lo)
68465db1c03fSJeff Layton 		nfs4_put_stateowner(&lo->lo_owner);
684721179d81SJeff Layton 	if (file_lock)
684821179d81SJeff Layton 		locks_free_lock(file_lock);
68491da177e4SLinus Torvalds 	return status;
68501da177e4SLinus Torvalds }
68511da177e4SLinus Torvalds 
6852b37ad28bSAl Viro __be32
6853ca364317SJ.Bruce Fields nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6854eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
68551da177e4SLinus Torvalds {
6856eb69853dSChristoph Hellwig 	struct nfsd4_locku *locku = &u->locku;
6857dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
6858eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
685921179d81SJeff Layton 	struct file_lock *file_lock = NULL;
6860b37ad28bSAl Viro 	__be32 status;
6861b8dd7b9aSAl Viro 	int err;
68623320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
68631da177e4SLinus Torvalds 
68641da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
68651da177e4SLinus Torvalds 		(long long) locku->lu_offset,
68661da177e4SLinus Torvalds 		(long long) locku->lu_length);
68671da177e4SLinus Torvalds 
68681da177e4SLinus Torvalds 	if (check_lock_length(locku->lu_offset, locku->lu_length))
68691da177e4SLinus Torvalds 		 return nfserr_inval;
68701da177e4SLinus Torvalds 
68719072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
68723320fef1SStanislav Kinsbursky 					&locku->lu_stateid, NFS4_LOCK_STID,
68733320fef1SStanislav Kinsbursky 					&stp, nn);
68749072d5c6SJ. Bruce Fields 	if (status)
68751da177e4SLinus Torvalds 		goto out;
6876eb82dd39SJeff Layton 	nf = find_any_file(stp->st_stid.sc_file);
6877eb82dd39SJeff Layton 	if (!nf) {
6878f9d7562fSJ. Bruce Fields 		status = nfserr_lock_range;
6879858cc573STrond Myklebust 		goto put_stateid;
6880f9d7562fSJ. Bruce Fields 	}
688121179d81SJeff Layton 	file_lock = locks_alloc_lock();
688221179d81SJeff Layton 	if (!file_lock) {
688321179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
688421179d81SJeff Layton 		status = nfserr_jukebox;
6885eb82dd39SJeff Layton 		goto put_file;
688621179d81SJeff Layton 	}
68876cd90662SKinglong Mee 
688821179d81SJeff Layton 	file_lock->fl_type = F_UNLCK;
6889aef9583bSKinglong Mee 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
689021179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
6891eb82dd39SJeff Layton 	file_lock->fl_file = nf->nf_file;
689221179d81SJeff Layton 	file_lock->fl_flags = FL_POSIX;
689321179d81SJeff Layton 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
689421179d81SJeff Layton 	file_lock->fl_start = locku->lu_offset;
68951da177e4SLinus Torvalds 
689621179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(locku->lu_offset,
689721179d81SJeff Layton 						locku->lu_length);
689821179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
68991da177e4SLinus Torvalds 
6900eb82dd39SJeff Layton 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
6901b8dd7b9aSAl Viro 	if (err) {
6902fd85b817SMarc Eshel 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
69031da177e4SLinus Torvalds 		goto out_nfserr;
69041da177e4SLinus Torvalds 	}
69059767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
6906eb82dd39SJeff Layton put_file:
6907eb82dd39SJeff Layton 	nfsd_file_put(nf);
6908858cc573STrond Myklebust put_stateid:
6909feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
6910858cc573STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
69111da177e4SLinus Torvalds out:
69129411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
691321179d81SJeff Layton 	if (file_lock)
691421179d81SJeff Layton 		locks_free_lock(file_lock);
69151da177e4SLinus Torvalds 	return status;
69161da177e4SLinus Torvalds 
69171da177e4SLinus Torvalds out_nfserr:
6918b8dd7b9aSAl Viro 	status = nfserrno(err);
6919eb82dd39SJeff Layton 	goto put_file;
69201da177e4SLinus Torvalds }
69211da177e4SLinus Torvalds 
69221da177e4SLinus Torvalds /*
69231da177e4SLinus Torvalds  * returns
6924f9c00c3aSJeff Layton  * 	true:  locks held by lockowner
6925f9c00c3aSJeff Layton  * 	false: no locks held by lockowner
69261da177e4SLinus Torvalds  */
6927f9c00c3aSJeff Layton static bool
6928f9c00c3aSJeff Layton check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
69291da177e4SLinus Torvalds {
6930bd61e0a9SJeff Layton 	struct file_lock *fl;
6931f9c00c3aSJeff Layton 	int status = false;
6932eb82dd39SJeff Layton 	struct nfsd_file *nf = find_any_file(fp);
6933f9c00c3aSJeff Layton 	struct inode *inode;
6934bd61e0a9SJeff Layton 	struct file_lock_context *flctx;
6935f9c00c3aSJeff Layton 
6936eb82dd39SJeff Layton 	if (!nf) {
6937f9c00c3aSJeff Layton 		/* Any valid lock stateid should have some sort of access */
6938f9c00c3aSJeff Layton 		WARN_ON_ONCE(1);
6939f9c00c3aSJeff Layton 		return status;
6940f9c00c3aSJeff Layton 	}
6941f9c00c3aSJeff Layton 
6942eb82dd39SJeff Layton 	inode = locks_inode(nf->nf_file);
6943bd61e0a9SJeff Layton 	flctx = inode->i_flctx;
69441da177e4SLinus Torvalds 
6945bd61e0a9SJeff Layton 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
69466109c850SJeff Layton 		spin_lock(&flctx->flc_lock);
6947bd61e0a9SJeff Layton 		list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
6948bd61e0a9SJeff Layton 			if (fl->fl_owner == (fl_owner_t)lowner) {
6949f9c00c3aSJeff Layton 				status = true;
6950f9c00c3aSJeff Layton 				break;
69511da177e4SLinus Torvalds 			}
6952796dadfdSJ. Bruce Fields 		}
69536109c850SJeff Layton 		spin_unlock(&flctx->flc_lock);
6954bd61e0a9SJeff Layton 	}
6955eb82dd39SJeff Layton 	nfsd_file_put(nf);
69561da177e4SLinus Torvalds 	return status;
69571da177e4SLinus Torvalds }
69581da177e4SLinus Torvalds 
6959b37ad28bSAl Viro __be32
6960b591480bSJ.Bruce Fields nfsd4_release_lockowner(struct svc_rqst *rqstp,
6961b591480bSJ.Bruce Fields 			struct nfsd4_compound_state *cstate,
6962eb69853dSChristoph Hellwig 			union nfsd4_op_u *u)
69631da177e4SLinus Torvalds {
6964eb69853dSChristoph Hellwig 	struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
69651da177e4SLinus Torvalds 	clientid_t *clid = &rlockowner->rl_clientid;
6966882e9d25SJeff Layton 	struct nfs4_stateowner *sop;
6967882e9d25SJeff Layton 	struct nfs4_lockowner *lo = NULL;
6968dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
69691da177e4SLinus Torvalds 	struct xdr_netobj *owner = &rlockowner->rl_owner;
6970d4f0489fSTrond Myklebust 	unsigned int hashval = ownerstr_hashval(owner);
6971b37ad28bSAl Viro 	__be32 status;
69727f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6973c58c6610STrond Myklebust 	struct nfs4_client *clp;
697488584818SChuck Lever 	LIST_HEAD (reaplist);
69751da177e4SLinus Torvalds 
69761da177e4SLinus Torvalds 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
69771da177e4SLinus Torvalds 		clid->cl_boot, clid->cl_id);
69781da177e4SLinus Torvalds 
6979b7342204SOlga Kornievskaia 	status = lookup_clientid(clid, cstate, nn, false);
69809b2ef62bSJ. Bruce Fields 	if (status)
698151f5e783STrond Myklebust 		return status;
69829b2ef62bSJ. Bruce Fields 
6983d4f0489fSTrond Myklebust 	clp = cstate->clp;
6984fd44907cSJeff Layton 	/* Find the matching lock stateowner */
6985d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
6986882e9d25SJeff Layton 	list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
6987d4f0489fSTrond Myklebust 			    so_strhash) {
6988882e9d25SJeff Layton 
6989882e9d25SJeff Layton 		if (sop->so_is_open_owner || !same_owner_str(sop, owner))
699016bfdaafSJ. Bruce Fields 			continue;
6991882e9d25SJeff Layton 
6992882e9d25SJeff Layton 		/* see if there are still any locks associated with it */
6993882e9d25SJeff Layton 		lo = lockowner(sop);
6994882e9d25SJeff Layton 		list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
6995882e9d25SJeff Layton 			if (check_for_locks(stp->st_stid.sc_file, lo)) {
6996882e9d25SJeff Layton 				status = nfserr_locks_held;
6997882e9d25SJeff Layton 				spin_unlock(&clp->cl_lock);
699851f5e783STrond Myklebust 				return status;
6999882e9d25SJeff Layton 			}
7000882e9d25SJeff Layton 		}
7001882e9d25SJeff Layton 
7002b5971afaSKinglong Mee 		nfs4_get_stateowner(sop);
7003fd44907cSJeff Layton 		break;
7004fd44907cSJeff Layton 	}
700588584818SChuck Lever 	if (!lo) {
7006d4f0489fSTrond Myklebust 		spin_unlock(&clp->cl_lock);
700788584818SChuck Lever 		return status;
700888584818SChuck Lever 	}
700988584818SChuck Lever 
701088584818SChuck Lever 	unhash_lockowner_locked(lo);
701188584818SChuck Lever 	while (!list_empty(&lo->lo_owner.so_stateids)) {
701288584818SChuck Lever 		stp = list_first_entry(&lo->lo_owner.so_stateids,
701388584818SChuck Lever 				       struct nfs4_ol_stateid,
701488584818SChuck Lever 				       st_perstateowner);
701588584818SChuck Lever 		WARN_ON(!unhash_lock_stateid(stp));
701688584818SChuck Lever 		put_ol_stateid_locked(stp, &reaplist);
701788584818SChuck Lever 	}
701888584818SChuck Lever 	spin_unlock(&clp->cl_lock);
701988584818SChuck Lever 	free_ol_stateid_reaplist(&reaplist);
702068ef3bc3SJeff Layton 	remove_blocked_locks(lo);
702188584818SChuck Lever 	nfs4_put_stateowner(&lo->lo_owner);
702288584818SChuck Lever 
70231da177e4SLinus Torvalds 	return status;
70241da177e4SLinus Torvalds }
70251da177e4SLinus Torvalds 
70261da177e4SLinus Torvalds static inline struct nfs4_client_reclaim *
7027a55370a3SNeilBrown alloc_reclaim(void)
70281da177e4SLinus Torvalds {
7029a55370a3SNeilBrown 	return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
70301da177e4SLinus Torvalds }
70311da177e4SLinus Torvalds 
70320ce0c2b5SJeff Layton bool
70336b189105SScott Mayhew nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7034c7b9a459SNeilBrown {
70350ce0c2b5SJeff Layton 	struct nfs4_client_reclaim *crp;
7036c7b9a459SNeilBrown 
703752e19c09SStanislav Kinsbursky 	crp = nfsd4_find_reclaim_client(name, nn);
70380ce0c2b5SJeff Layton 	return (crp && crp->cr_clp);
7039c7b9a459SNeilBrown }
7040c7b9a459SNeilBrown 
70411da177e4SLinus Torvalds /*
70421da177e4SLinus Torvalds  * failure => all reset bets are off, nfserr_no_grace...
70436b189105SScott Mayhew  *
70446b189105SScott Mayhew  * The caller is responsible for freeing name.data if NULL is returned (it
70456b189105SScott Mayhew  * will be freed in nfs4_remove_reclaim_record in the normal case).
70461da177e4SLinus Torvalds  */
7047772a9bbbSJeff Layton struct nfs4_client_reclaim *
70486ee95d1cSScott Mayhew nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
70496ee95d1cSScott Mayhew 		struct nfsd_net *nn)
70501da177e4SLinus Torvalds {
70511da177e4SLinus Torvalds 	unsigned int strhashval;
7052772a9bbbSJeff Layton 	struct nfs4_client_reclaim *crp;
70531da177e4SLinus Torvalds 
7054dd5e3fbcSChuck Lever 	trace_nfsd_clid_reclaim(nn, name.len, name.data);
7055a55370a3SNeilBrown 	crp = alloc_reclaim();
7056772a9bbbSJeff Layton 	if (crp) {
7057a55370a3SNeilBrown 		strhashval = clientstr_hashval(name);
70581da177e4SLinus Torvalds 		INIT_LIST_HEAD(&crp->cr_strhash);
705952e19c09SStanislav Kinsbursky 		list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
70606b189105SScott Mayhew 		crp->cr_name.data = name.data;
70616b189105SScott Mayhew 		crp->cr_name.len = name.len;
70626ee95d1cSScott Mayhew 		crp->cr_princhash.data = princhash.data;
70636ee95d1cSScott Mayhew 		crp->cr_princhash.len = princhash.len;
70640ce0c2b5SJeff Layton 		crp->cr_clp = NULL;
706552e19c09SStanislav Kinsbursky 		nn->reclaim_str_hashtbl_size++;
7066772a9bbbSJeff Layton 	}
7067772a9bbbSJeff Layton 	return crp;
70681da177e4SLinus Torvalds }
70691da177e4SLinus Torvalds 
70702a4317c5SJeff Layton void
707152e19c09SStanislav Kinsbursky nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7072ce30e539SJeff Layton {
7073ce30e539SJeff Layton 	list_del(&crp->cr_strhash);
70746b189105SScott Mayhew 	kfree(crp->cr_name.data);
70756ee95d1cSScott Mayhew 	kfree(crp->cr_princhash.data);
7076ce30e539SJeff Layton 	kfree(crp);
707752e19c09SStanislav Kinsbursky 	nn->reclaim_str_hashtbl_size--;
7078ce30e539SJeff Layton }
7079ce30e539SJeff Layton 
7080ce30e539SJeff Layton void
708152e19c09SStanislav Kinsbursky nfs4_release_reclaim(struct nfsd_net *nn)
70821da177e4SLinus Torvalds {
70831da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
70841da177e4SLinus Torvalds 	int i;
70851da177e4SLinus Torvalds 
70861da177e4SLinus Torvalds 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
708752e19c09SStanislav Kinsbursky 		while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
708852e19c09SStanislav Kinsbursky 			crp = list_entry(nn->reclaim_str_hashtbl[i].next,
70891da177e4SLinus Torvalds 			                struct nfs4_client_reclaim, cr_strhash);
709052e19c09SStanislav Kinsbursky 			nfs4_remove_reclaim_record(crp, nn);
70911da177e4SLinus Torvalds 		}
70921da177e4SLinus Torvalds 	}
7093063b0fb9SJ. Bruce Fields 	WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
70941da177e4SLinus Torvalds }
70951da177e4SLinus Torvalds 
70961da177e4SLinus Torvalds /*
70971da177e4SLinus Torvalds  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
70982a4317c5SJeff Layton struct nfs4_client_reclaim *
70996b189105SScott Mayhew nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
71001da177e4SLinus Torvalds {
71011da177e4SLinus Torvalds 	unsigned int strhashval;
71021da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
71031da177e4SLinus Torvalds 
7104dd5e3fbcSChuck Lever 	trace_nfsd_clid_find(nn, name.len, name.data);
71051da177e4SLinus Torvalds 
71066b189105SScott Mayhew 	strhashval = clientstr_hashval(name);
710752e19c09SStanislav Kinsbursky 	list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
71086b189105SScott Mayhew 		if (compare_blob(&crp->cr_name, &name) == 0) {
71091da177e4SLinus Torvalds 			return crp;
71101da177e4SLinus Torvalds 		}
71111da177e4SLinus Torvalds 	}
71121da177e4SLinus Torvalds 	return NULL;
71131da177e4SLinus Torvalds }
71141da177e4SLinus Torvalds 
71151da177e4SLinus Torvalds /*
71161da177e4SLinus Torvalds * Called from OPEN. Look for clientid in reclaim list.
71171da177e4SLinus Torvalds */
7118b37ad28bSAl Viro __be32
71190fe492dbSTrond Myklebust nfs4_check_open_reclaim(clientid_t *clid,
71200fe492dbSTrond Myklebust 		struct nfsd4_compound_state *cstate,
71210fe492dbSTrond Myklebust 		struct nfsd_net *nn)
71221da177e4SLinus Torvalds {
71230fe492dbSTrond Myklebust 	__be32 status;
7124a52d726bSJeff Layton 
7125a52d726bSJeff Layton 	/* find clientid in conf_id_hashtbl */
7126b7342204SOlga Kornievskaia 	status = lookup_clientid(clid, cstate, nn, false);
71270fe492dbSTrond Myklebust 	if (status)
7128a52d726bSJeff Layton 		return nfserr_reclaim_bad;
7129a52d726bSJeff Layton 
71303b3e7b72SJeff Layton 	if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
71313b3e7b72SJeff Layton 		return nfserr_no_grace;
71323b3e7b72SJeff Layton 
71330fe492dbSTrond Myklebust 	if (nfsd4_client_record_check(cstate->clp))
71340fe492dbSTrond Myklebust 		return nfserr_reclaim_bad;
71350fe492dbSTrond Myklebust 
71360fe492dbSTrond Myklebust 	return nfs_ok;
71371da177e4SLinus Torvalds }
71381da177e4SLinus Torvalds 
713965178db4SBryan Schumaker #ifdef CONFIG_NFSD_FAULT_INJECTION
7140016200c3SJeff Layton static inline void
7141016200c3SJeff Layton put_client(struct nfs4_client *clp)
7142016200c3SJeff Layton {
714314ed14ccSJ. Bruce Fields 	atomic_dec(&clp->cl_rpc_users);
7144016200c3SJeff Layton }
7145016200c3SJeff Layton 
7146285abdeeSJeff Layton static struct nfs4_client *
7147285abdeeSJeff Layton nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
7148285abdeeSJeff Layton {
7149285abdeeSJeff Layton 	struct nfs4_client *clp;
7150285abdeeSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7151285abdeeSJeff Layton 					  nfsd_net_id);
7152285abdeeSJeff Layton 
7153285abdeeSJeff Layton 	if (!nfsd_netns_ready(nn))
7154285abdeeSJeff Layton 		return NULL;
7155285abdeeSJeff Layton 
7156285abdeeSJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7157285abdeeSJeff Layton 		if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
7158285abdeeSJeff Layton 			return clp;
7159285abdeeSJeff Layton 	}
7160285abdeeSJeff Layton 	return NULL;
7161285abdeeSJeff Layton }
7162285abdeeSJeff Layton 
71637ec0e36fSJeff Layton u64
7164285abdeeSJeff Layton nfsd_inject_print_clients(void)
71657ec0e36fSJeff Layton {
71667ec0e36fSJeff Layton 	struct nfs4_client *clp;
71677ec0e36fSJeff Layton 	u64 count = 0;
71687ec0e36fSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
71697ec0e36fSJeff Layton 					  nfsd_net_id);
71707ec0e36fSJeff Layton 	char buf[INET6_ADDRSTRLEN];
71717ec0e36fSJeff Layton 
71727ec0e36fSJeff Layton 	if (!nfsd_netns_ready(nn))
71737ec0e36fSJeff Layton 		return 0;
71747ec0e36fSJeff Layton 
71757ec0e36fSJeff Layton 	spin_lock(&nn->client_lock);
71767ec0e36fSJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
71777ec0e36fSJeff Layton 		rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
71787ec0e36fSJeff Layton 		pr_info("NFS Client: %s\n", buf);
71797ec0e36fSJeff Layton 		++count;
71807ec0e36fSJeff Layton 	}
71817ec0e36fSJeff Layton 	spin_unlock(&nn->client_lock);
71827ec0e36fSJeff Layton 
71837ec0e36fSJeff Layton 	return count;
71847ec0e36fSJeff Layton }
718565178db4SBryan Schumaker 
7186a0926d15SJeff Layton u64
7187285abdeeSJeff Layton nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
7188a0926d15SJeff Layton {
7189a0926d15SJeff Layton 	u64 count = 0;
7190a0926d15SJeff Layton 	struct nfs4_client *clp;
7191a0926d15SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7192a0926d15SJeff Layton 					  nfsd_net_id);
7193a0926d15SJeff Layton 
7194a0926d15SJeff Layton 	if (!nfsd_netns_ready(nn))
7195a0926d15SJeff Layton 		return count;
7196a0926d15SJeff Layton 
7197a0926d15SJeff Layton 	spin_lock(&nn->client_lock);
7198a0926d15SJeff Layton 	clp = nfsd_find_client(addr, addr_size);
7199a0926d15SJeff Layton 	if (clp) {
7200a0926d15SJeff Layton 		if (mark_client_expired_locked(clp) == nfs_ok)
7201a0926d15SJeff Layton 			++count;
7202a0926d15SJeff Layton 		else
7203a0926d15SJeff Layton 			clp = NULL;
7204a0926d15SJeff Layton 	}
7205a0926d15SJeff Layton 	spin_unlock(&nn->client_lock);
7206a0926d15SJeff Layton 
7207a0926d15SJeff Layton 	if (clp)
7208a0926d15SJeff Layton 		expire_client(clp);
7209a0926d15SJeff Layton 
7210a0926d15SJeff Layton 	return count;
7211a0926d15SJeff Layton }
7212a0926d15SJeff Layton 
721369fc9edfSJeff Layton u64
7214285abdeeSJeff Layton nfsd_inject_forget_clients(u64 max)
721569fc9edfSJeff Layton {
721669fc9edfSJeff Layton 	u64 count = 0;
721769fc9edfSJeff Layton 	struct nfs4_client *clp, *next;
721869fc9edfSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
721969fc9edfSJeff Layton 						nfsd_net_id);
722069fc9edfSJeff Layton 	LIST_HEAD(reaplist);
722169fc9edfSJeff Layton 
722269fc9edfSJeff Layton 	if (!nfsd_netns_ready(nn))
722369fc9edfSJeff Layton 		return count;
722469fc9edfSJeff Layton 
722569fc9edfSJeff Layton 	spin_lock(&nn->client_lock);
722669fc9edfSJeff Layton 	list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
722769fc9edfSJeff Layton 		if (mark_client_expired_locked(clp) == nfs_ok) {
722869fc9edfSJeff Layton 			list_add(&clp->cl_lru, &reaplist);
722969fc9edfSJeff Layton 			if (max != 0 && ++count >= max)
723069fc9edfSJeff Layton 				break;
723169fc9edfSJeff Layton 		}
723269fc9edfSJeff Layton 	}
723369fc9edfSJeff Layton 	spin_unlock(&nn->client_lock);
723469fc9edfSJeff Layton 
723569fc9edfSJeff Layton 	list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
723669fc9edfSJeff Layton 		expire_client(clp);
723769fc9edfSJeff Layton 
723869fc9edfSJeff Layton 	return count;
723969fc9edfSJeff Layton }
724069fc9edfSJeff Layton 
7241184c1847SBryan Schumaker static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
7242184c1847SBryan Schumaker 			     const char *type)
7243184c1847SBryan Schumaker {
7244184c1847SBryan Schumaker 	char buf[INET6_ADDRSTRLEN];
72450a5c33e2SBryan Schumaker 	rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
7246184c1847SBryan Schumaker 	printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
7247184c1847SBryan Schumaker }
7248184c1847SBryan Schumaker 
7249016200c3SJeff Layton static void
7250016200c3SJeff Layton nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
7251016200c3SJeff Layton 			     struct list_head *collect)
7252016200c3SJeff Layton {
7253016200c3SJeff Layton 	struct nfs4_client *clp = lst->st_stid.sc_client;
7254016200c3SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7255016200c3SJeff Layton 					  nfsd_net_id);
7256016200c3SJeff Layton 
7257016200c3SJeff Layton 	if (!collect)
7258016200c3SJeff Layton 		return;
7259016200c3SJeff Layton 
7260016200c3SJeff Layton 	lockdep_assert_held(&nn->client_lock);
726114ed14ccSJ. Bruce Fields 	atomic_inc(&clp->cl_rpc_users);
7262016200c3SJeff Layton 	list_add(&lst->st_locks, collect);
7263016200c3SJeff Layton }
7264016200c3SJeff Layton 
72653c87b9b7STrond Myklebust static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
72663738d50eSJeff Layton 				    struct list_head *collect,
7267e8568739SJeff Layton 				    bool (*func)(struct nfs4_ol_stateid *))
7268fc29171fSBryan Schumaker {
7269fc29171fSBryan Schumaker 	struct nfs4_openowner *oop;
7270fc29171fSBryan Schumaker 	struct nfs4_ol_stateid *stp, *st_next;
72713c87b9b7STrond Myklebust 	struct nfs4_ol_stateid *lst, *lst_next;
7272fc29171fSBryan Schumaker 	u64 count = 0;
7273fc29171fSBryan Schumaker 
7274016200c3SJeff Layton 	spin_lock(&clp->cl_lock);
7275fc29171fSBryan Schumaker 	list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
72763c87b9b7STrond Myklebust 		list_for_each_entry_safe(stp, st_next,
72773c87b9b7STrond Myklebust 				&oop->oo_owner.so_stateids, st_perstateowner) {
72783c87b9b7STrond Myklebust 			list_for_each_entry_safe(lst, lst_next,
72793c87b9b7STrond Myklebust 					&stp->st_locks, st_locks) {
72803738d50eSJeff Layton 				if (func) {
7281e8568739SJeff Layton 					if (func(lst))
7282016200c3SJeff Layton 						nfsd_inject_add_lock_to_list(lst,
72833738d50eSJeff Layton 									collect);
72843738d50eSJeff Layton 				}
7285016200c3SJeff Layton 				++count;
7286016200c3SJeff Layton 				/*
7287016200c3SJeff Layton 				 * Despite the fact that these functions deal
7288016200c3SJeff Layton 				 * with 64-bit integers for "count", we must
7289016200c3SJeff Layton 				 * ensure that it doesn't blow up the
729014ed14ccSJ. Bruce Fields 				 * clp->cl_rpc_users. Throw a warning if we
7291016200c3SJeff Layton 				 * start to approach INT_MAX here.
7292016200c3SJeff Layton 				 */
7293016200c3SJeff Layton 				WARN_ON_ONCE(count == (INT_MAX / 2));
7294016200c3SJeff Layton 				if (count == max)
7295016200c3SJeff Layton 					goto out;
7296fc29171fSBryan Schumaker 			}
7297fc29171fSBryan Schumaker 		}
7298fc29171fSBryan Schumaker 	}
7299016200c3SJeff Layton out:
7300016200c3SJeff Layton 	spin_unlock(&clp->cl_lock);
7301fc29171fSBryan Schumaker 
7302fc29171fSBryan Schumaker 	return count;
7303fc29171fSBryan Schumaker }
7304fc29171fSBryan Schumaker 
7305016200c3SJeff Layton static u64
7306016200c3SJeff Layton nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
7307016200c3SJeff Layton 			  u64 max)
7308fc29171fSBryan Schumaker {
7309016200c3SJeff Layton 	return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
7310fc29171fSBryan Schumaker }
7311fc29171fSBryan Schumaker 
7312016200c3SJeff Layton static u64
7313016200c3SJeff Layton nfsd_print_client_locks(struct nfs4_client *clp)
7314184c1847SBryan Schumaker {
7315016200c3SJeff Layton 	u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
7316184c1847SBryan Schumaker 	nfsd_print_count(clp, count, "locked files");
7317184c1847SBryan Schumaker 	return count;
7318184c1847SBryan Schumaker }
7319184c1847SBryan Schumaker 
7320016200c3SJeff Layton u64
7321285abdeeSJeff Layton nfsd_inject_print_locks(void)
7322016200c3SJeff Layton {
7323016200c3SJeff Layton 	struct nfs4_client *clp;
7324016200c3SJeff Layton 	u64 count = 0;
7325016200c3SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7326016200c3SJeff Layton 						nfsd_net_id);
7327016200c3SJeff Layton 
7328016200c3SJeff Layton 	if (!nfsd_netns_ready(nn))
7329016200c3SJeff Layton 		return 0;
7330016200c3SJeff Layton 
7331016200c3SJeff Layton 	spin_lock(&nn->client_lock);
7332016200c3SJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
7333016200c3SJeff Layton 		count += nfsd_print_client_locks(clp);
7334016200c3SJeff Layton 	spin_unlock(&nn->client_lock);
7335016200c3SJeff Layton 
7336016200c3SJeff Layton 	return count;
7337016200c3SJeff Layton }
7338016200c3SJeff Layton 
7339016200c3SJeff Layton static void
7340016200c3SJeff Layton nfsd_reap_locks(struct list_head *reaplist)
7341016200c3SJeff Layton {
7342016200c3SJeff Layton 	struct nfs4_client *clp;
7343016200c3SJeff Layton 	struct nfs4_ol_stateid *stp, *next;
7344016200c3SJeff Layton 
7345016200c3SJeff Layton 	list_for_each_entry_safe(stp, next, reaplist, st_locks) {
7346016200c3SJeff Layton 		list_del_init(&stp->st_locks);
7347016200c3SJeff Layton 		clp = stp->st_stid.sc_client;
7348016200c3SJeff Layton 		nfs4_put_stid(&stp->st_stid);
7349016200c3SJeff Layton 		put_client(clp);
7350016200c3SJeff Layton 	}
7351016200c3SJeff Layton }
7352016200c3SJeff Layton 
7353016200c3SJeff Layton u64
7354285abdeeSJeff Layton nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
7355016200c3SJeff Layton {
7356016200c3SJeff Layton 	unsigned int count = 0;
7357016200c3SJeff Layton 	struct nfs4_client *clp;
7358016200c3SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7359016200c3SJeff Layton 						nfsd_net_id);
7360016200c3SJeff Layton 	LIST_HEAD(reaplist);
7361016200c3SJeff Layton 
7362016200c3SJeff Layton 	if (!nfsd_netns_ready(nn))
7363016200c3SJeff Layton 		return count;
7364016200c3SJeff Layton 
7365016200c3SJeff Layton 	spin_lock(&nn->client_lock);
7366016200c3SJeff Layton 	clp = nfsd_find_client(addr, addr_size);
7367016200c3SJeff Layton 	if (clp)
7368016200c3SJeff Layton 		count = nfsd_collect_client_locks(clp, &reaplist, 0);
7369016200c3SJeff Layton 	spin_unlock(&nn->client_lock);
7370016200c3SJeff Layton 	nfsd_reap_locks(&reaplist);
7371016200c3SJeff Layton 	return count;
7372016200c3SJeff Layton }
7373016200c3SJeff Layton 
7374016200c3SJeff Layton u64
7375285abdeeSJeff Layton nfsd_inject_forget_locks(u64 max)
7376016200c3SJeff Layton {
7377016200c3SJeff Layton 	u64 count = 0;
7378016200c3SJeff Layton 	struct nfs4_client *clp;
7379016200c3SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
7380016200c3SJeff Layton 						nfsd_net_id);
7381016200c3SJeff Layton 	LIST_HEAD(reaplist);
7382016200c3SJeff Layton 
7383016200c3SJeff Layton 	if (!nfsd_netns_ready(nn))
7384016200c3SJeff Layton 		return count;
7385016200c3SJeff Layton 
7386016200c3SJeff Layton 	spin_lock(&nn->client_lock);
7387016200c3SJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
7388016200c3SJeff Layton 		count += nfsd_collect_client_locks(clp, &reaplist, max - count);
7389016200c3SJeff Layton 		if (max != 0 && count >= max)
7390016200c3SJeff Layton 			break;
7391016200c3SJeff Layton 	}
7392016200c3SJeff Layton 	spin_unlock(&nn->client_lock);
7393016200c3SJeff Layton 	nfsd_reap_locks(&reaplist);
7394016200c3SJeff Layton 	return count;
7395016200c3SJeff Layton }
7396016200c3SJeff Layton 
739782e05efaSJeff Layton static u64
739882e05efaSJeff Layton nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
739982e05efaSJeff Layton 			      struct list_head *collect,
740082e05efaSJeff Layton 			      void (*func)(struct nfs4_openowner *))
74014dbdbda8SBryan Schumaker {
74024dbdbda8SBryan Schumaker 	struct nfs4_openowner *oop, *next;
740382e05efaSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
740482e05efaSJeff Layton 						nfsd_net_id);
74054dbdbda8SBryan Schumaker 	u64 count = 0;
74064dbdbda8SBryan Schumaker 
740782e05efaSJeff Layton 	lockdep_assert_held(&nn->client_lock);
740882e05efaSJeff Layton 
740982e05efaSJeff Layton 	spin_lock(&clp->cl_lock);
74104dbdbda8SBryan Schumaker 	list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
741182e05efaSJeff Layton 		if (func) {
74124dbdbda8SBryan Schumaker 			func(oop);
741382e05efaSJeff Layton 			if (collect) {
741414ed14ccSJ. Bruce Fields 				atomic_inc(&clp->cl_rpc_users);
741582e05efaSJeff Layton 				list_add(&oop->oo_perclient, collect);
741682e05efaSJeff Layton 			}
741782e05efaSJeff Layton 		}
741882e05efaSJeff Layton 		++count;
741982e05efaSJeff Layton 		/*
742082e05efaSJeff Layton 		 * Despite the fact that these functions deal with
742182e05efaSJeff Layton 		 * 64-bit integers for "count", we must ensure that
742214ed14ccSJ. Bruce Fields 		 * it doesn't blow up the clp->cl_rpc_users. Throw a
742382e05efaSJeff Layton 		 * warning if we start to approach INT_MAX here.
742482e05efaSJeff Layton 		 */
742582e05efaSJeff Layton 		WARN_ON_ONCE(count == (INT_MAX / 2));
742682e05efaSJeff Layton 		if (count == max)
74274dbdbda8SBryan Schumaker 			break;
74284dbdbda8SBryan Schumaker 	}
742982e05efaSJeff Layton 	spin_unlock(&clp->cl_lock);
74304dbdbda8SBryan Schumaker 
74314dbdbda8SBryan Schumaker 	return count;
74324dbdbda8SBryan Schumaker }
74334dbdbda8SBryan Schumaker 
743482e05efaSJeff Layton static u64
743582e05efaSJeff Layton nfsd_print_client_openowners(struct nfs4_client *clp)
74364dbdbda8SBryan Schumaker {
743782e05efaSJeff Layton 	u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
743882e05efaSJeff Layton 
743982e05efaSJeff Layton 	nfsd_print_count(clp, count, "openowners");
744082e05efaSJeff Layton 	return count;
74414dbdbda8SBryan Schumaker }
74424dbdbda8SBryan Schumaker 
744382e05efaSJeff Layton static u64
744482e05efaSJeff Layton nfsd_collect_client_openowners(struct nfs4_client *clp,
744582e05efaSJeff Layton 			       struct list_head *collect, u64 max)
7446184c1847SBryan Schumaker {
744782e05efaSJeff Layton 	return nfsd_foreach_client_openowner(clp, max, collect,
744882e05efaSJeff Layton 						unhash_openowner_locked);
744982e05efaSJeff Layton }
745082e05efaSJeff Layton 
745182e05efaSJeff Layton u64
7452285abdeeSJeff Layton nfsd_inject_print_openowners(void)
745382e05efaSJeff Layton {
745482e05efaSJeff Layton 	struct nfs4_client *clp;
745582e05efaSJeff Layton 	u64 count = 0;
745682e05efaSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
745782e05efaSJeff Layton 						nfsd_net_id);
745882e05efaSJeff Layton 
745982e05efaSJeff Layton 	if (!nfsd_netns_ready(nn))
746082e05efaSJeff Layton 		return 0;
746182e05efaSJeff Layton 
746282e05efaSJeff Layton 	spin_lock(&nn->client_lock);
746382e05efaSJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
746482e05efaSJeff Layton 		count += nfsd_print_client_openowners(clp);
746582e05efaSJeff Layton 	spin_unlock(&nn->client_lock);
746682e05efaSJeff Layton 
746782e05efaSJeff Layton 	return count;
746882e05efaSJeff Layton }
746982e05efaSJeff Layton 
747082e05efaSJeff Layton static void
747182e05efaSJeff Layton nfsd_reap_openowners(struct list_head *reaplist)
747282e05efaSJeff Layton {
747382e05efaSJeff Layton 	struct nfs4_client *clp;
747482e05efaSJeff Layton 	struct nfs4_openowner *oop, *next;
747582e05efaSJeff Layton 
747682e05efaSJeff Layton 	list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
747782e05efaSJeff Layton 		list_del_init(&oop->oo_perclient);
747882e05efaSJeff Layton 		clp = oop->oo_owner.so_client;
747982e05efaSJeff Layton 		release_openowner(oop);
748082e05efaSJeff Layton 		put_client(clp);
748182e05efaSJeff Layton 	}
748282e05efaSJeff Layton }
748382e05efaSJeff Layton 
748482e05efaSJeff Layton u64
7485285abdeeSJeff Layton nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
7486285abdeeSJeff Layton 				     size_t addr_size)
748782e05efaSJeff Layton {
748882e05efaSJeff Layton 	unsigned int count = 0;
748982e05efaSJeff Layton 	struct nfs4_client *clp;
749082e05efaSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
749182e05efaSJeff Layton 						nfsd_net_id);
749282e05efaSJeff Layton 	LIST_HEAD(reaplist);
749382e05efaSJeff Layton 
749482e05efaSJeff Layton 	if (!nfsd_netns_ready(nn))
749582e05efaSJeff Layton 		return count;
749682e05efaSJeff Layton 
749782e05efaSJeff Layton 	spin_lock(&nn->client_lock);
749882e05efaSJeff Layton 	clp = nfsd_find_client(addr, addr_size);
749982e05efaSJeff Layton 	if (clp)
750082e05efaSJeff Layton 		count = nfsd_collect_client_openowners(clp, &reaplist, 0);
750182e05efaSJeff Layton 	spin_unlock(&nn->client_lock);
750282e05efaSJeff Layton 	nfsd_reap_openowners(&reaplist);
750382e05efaSJeff Layton 	return count;
750482e05efaSJeff Layton }
750582e05efaSJeff Layton 
750682e05efaSJeff Layton u64
7507285abdeeSJeff Layton nfsd_inject_forget_openowners(u64 max)
750882e05efaSJeff Layton {
750982e05efaSJeff Layton 	u64 count = 0;
751082e05efaSJeff Layton 	struct nfs4_client *clp;
751182e05efaSJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
751282e05efaSJeff Layton 						nfsd_net_id);
751382e05efaSJeff Layton 	LIST_HEAD(reaplist);
751482e05efaSJeff Layton 
751582e05efaSJeff Layton 	if (!nfsd_netns_ready(nn))
751682e05efaSJeff Layton 		return count;
751782e05efaSJeff Layton 
751882e05efaSJeff Layton 	spin_lock(&nn->client_lock);
751982e05efaSJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
752082e05efaSJeff Layton 		count += nfsd_collect_client_openowners(clp, &reaplist,
752182e05efaSJeff Layton 							max - count);
752282e05efaSJeff Layton 		if (max != 0 && count >= max)
752382e05efaSJeff Layton 			break;
752482e05efaSJeff Layton 	}
752582e05efaSJeff Layton 	spin_unlock(&nn->client_lock);
752682e05efaSJeff Layton 	nfsd_reap_openowners(&reaplist);
7527184c1847SBryan Schumaker 	return count;
7528184c1847SBryan Schumaker }
7529184c1847SBryan Schumaker 
7530269de30fSBryan Schumaker static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
7531269de30fSBryan Schumaker 				     struct list_head *victims)
7532269de30fSBryan Schumaker {
7533269de30fSBryan Schumaker 	struct nfs4_delegation *dp, *next;
753498d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
753598d5c7c5SJeff Layton 						nfsd_net_id);
7536269de30fSBryan Schumaker 	u64 count = 0;
7537269de30fSBryan Schumaker 
753898d5c7c5SJeff Layton 	lockdep_assert_held(&nn->client_lock);
753998d5c7c5SJeff Layton 
754098d5c7c5SJeff Layton 	spin_lock(&state_lock);
7541269de30fSBryan Schumaker 	list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
7542dff1399fSJeff Layton 		if (victims) {
7543dff1399fSJeff Layton 			/*
7544dff1399fSJeff Layton 			 * It's not safe to mess with delegations that have a
7545dff1399fSJeff Layton 			 * non-zero dl_time. They might have already been broken
7546dff1399fSJeff Layton 			 * and could be processed by the laundromat outside of
7547dff1399fSJeff Layton 			 * the state_lock. Just leave them be.
7548dff1399fSJeff Layton 			 */
7549dff1399fSJeff Layton 			if (dp->dl_time != 0)
7550dff1399fSJeff Layton 				continue;
7551dff1399fSJeff Layton 
755214ed14ccSJ. Bruce Fields 			atomic_inc(&clp->cl_rpc_users);
75533fcbbd24SJeff Layton 			WARN_ON(!unhash_delegation_locked(dp));
755442690676SJeff Layton 			list_add(&dp->dl_recall_lru, victims);
7555dff1399fSJeff Layton 		}
755698d5c7c5SJeff Layton 		++count;
755798d5c7c5SJeff Layton 		/*
755898d5c7c5SJeff Layton 		 * Despite the fact that these functions deal with
755998d5c7c5SJeff Layton 		 * 64-bit integers for "count", we must ensure that
756014ed14ccSJ. Bruce Fields 		 * it doesn't blow up the clp->cl_rpc_users. Throw a
756198d5c7c5SJeff Layton 		 * warning if we start to approach INT_MAX here.
756298d5c7c5SJeff Layton 		 */
756398d5c7c5SJeff Layton 		WARN_ON_ONCE(count == (INT_MAX / 2));
756498d5c7c5SJeff Layton 		if (count == max)
7565269de30fSBryan Schumaker 			break;
7566269de30fSBryan Schumaker 	}
756798d5c7c5SJeff Layton 	spin_unlock(&state_lock);
7568269de30fSBryan Schumaker 	return count;
7569269de30fSBryan Schumaker }
7570269de30fSBryan Schumaker 
757198d5c7c5SJeff Layton static u64
757298d5c7c5SJeff Layton nfsd_print_client_delegations(struct nfs4_client *clp)
7573269de30fSBryan Schumaker {
757498d5c7c5SJeff Layton 	u64 count = nfsd_find_all_delegations(clp, 0, NULL);
7575184c1847SBryan Schumaker 
7576184c1847SBryan Schumaker 	nfsd_print_count(clp, count, "delegations");
7577184c1847SBryan Schumaker 	return count;
7578184c1847SBryan Schumaker }
7579184c1847SBryan Schumaker 
758098d5c7c5SJeff Layton u64
7581285abdeeSJeff Layton nfsd_inject_print_delegations(void)
758298d5c7c5SJeff Layton {
758398d5c7c5SJeff Layton 	struct nfs4_client *clp;
758498d5c7c5SJeff Layton 	u64 count = 0;
758598d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
758698d5c7c5SJeff Layton 						nfsd_net_id);
758798d5c7c5SJeff Layton 
758898d5c7c5SJeff Layton 	if (!nfsd_netns_ready(nn))
758998d5c7c5SJeff Layton 		return 0;
759098d5c7c5SJeff Layton 
759198d5c7c5SJeff Layton 	spin_lock(&nn->client_lock);
759298d5c7c5SJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru)
759398d5c7c5SJeff Layton 		count += nfsd_print_client_delegations(clp);
759498d5c7c5SJeff Layton 	spin_unlock(&nn->client_lock);
759598d5c7c5SJeff Layton 
759698d5c7c5SJeff Layton 	return count;
759798d5c7c5SJeff Layton }
759898d5c7c5SJeff Layton 
759998d5c7c5SJeff Layton static void
760098d5c7c5SJeff Layton nfsd_forget_delegations(struct list_head *reaplist)
760198d5c7c5SJeff Layton {
760298d5c7c5SJeff Layton 	struct nfs4_client *clp;
760398d5c7c5SJeff Layton 	struct nfs4_delegation *dp, *next;
760498d5c7c5SJeff Layton 
760598d5c7c5SJeff Layton 	list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
760698d5c7c5SJeff Layton 		list_del_init(&dp->dl_recall_lru);
760798d5c7c5SJeff Layton 		clp = dp->dl_stid.sc_client;
760898d5c7c5SJeff Layton 		revoke_delegation(dp);
760998d5c7c5SJeff Layton 		put_client(clp);
761098d5c7c5SJeff Layton 	}
761198d5c7c5SJeff Layton }
761298d5c7c5SJeff Layton 
761398d5c7c5SJeff Layton u64
7614285abdeeSJeff Layton nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
7615285abdeeSJeff Layton 				      size_t addr_size)
761698d5c7c5SJeff Layton {
761798d5c7c5SJeff Layton 	u64 count = 0;
761898d5c7c5SJeff Layton 	struct nfs4_client *clp;
761998d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
762098d5c7c5SJeff Layton 						nfsd_net_id);
762198d5c7c5SJeff Layton 	LIST_HEAD(reaplist);
762298d5c7c5SJeff Layton 
762398d5c7c5SJeff Layton 	if (!nfsd_netns_ready(nn))
762498d5c7c5SJeff Layton 		return count;
762598d5c7c5SJeff Layton 
762698d5c7c5SJeff Layton 	spin_lock(&nn->client_lock);
762798d5c7c5SJeff Layton 	clp = nfsd_find_client(addr, addr_size);
762898d5c7c5SJeff Layton 	if (clp)
762998d5c7c5SJeff Layton 		count = nfsd_find_all_delegations(clp, 0, &reaplist);
763098d5c7c5SJeff Layton 	spin_unlock(&nn->client_lock);
763198d5c7c5SJeff Layton 
763298d5c7c5SJeff Layton 	nfsd_forget_delegations(&reaplist);
763398d5c7c5SJeff Layton 	return count;
763498d5c7c5SJeff Layton }
763598d5c7c5SJeff Layton 
763698d5c7c5SJeff Layton u64
7637285abdeeSJeff Layton nfsd_inject_forget_delegations(u64 max)
763898d5c7c5SJeff Layton {
763998d5c7c5SJeff Layton 	u64 count = 0;
764098d5c7c5SJeff Layton 	struct nfs4_client *clp;
764198d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
764298d5c7c5SJeff Layton 						nfsd_net_id);
764398d5c7c5SJeff Layton 	LIST_HEAD(reaplist);
764498d5c7c5SJeff Layton 
764598d5c7c5SJeff Layton 	if (!nfsd_netns_ready(nn))
764698d5c7c5SJeff Layton 		return count;
764798d5c7c5SJeff Layton 
764898d5c7c5SJeff Layton 	spin_lock(&nn->client_lock);
764998d5c7c5SJeff Layton 	list_for_each_entry(clp, &nn->client_lru, cl_lru) {
765098d5c7c5SJeff Layton 		count += nfsd_find_all_delegations(clp, max - count, &reaplist);
765198d5c7c5SJeff Layton 		if (max != 0 && count >= max)
765298d5c7c5SJeff Layton 			break;
765398d5c7c5SJeff Layton 	}
765498d5c7c5SJeff Layton 	spin_unlock(&nn->client_lock);
765598d5c7c5SJeff Layton 	nfsd_forget_delegations(&reaplist);
765698d5c7c5SJeff Layton 	return count;
765798d5c7c5SJeff Layton }
765898d5c7c5SJeff Layton 
765998d5c7c5SJeff Layton static void
766098d5c7c5SJeff Layton nfsd_recall_delegations(struct list_head *reaplist)
766198d5c7c5SJeff Layton {
766298d5c7c5SJeff Layton 	struct nfs4_client *clp;
766398d5c7c5SJeff Layton 	struct nfs4_delegation *dp, *next;
766498d5c7c5SJeff Layton 
766598d5c7c5SJeff Layton 	list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
766698d5c7c5SJeff Layton 		list_del_init(&dp->dl_recall_lru);
766798d5c7c5SJeff Layton 		clp = dp->dl_stid.sc_client;
7668dd5e3fbcSChuck Lever 
7669dd5e3fbcSChuck Lever 		trace_nfsd_deleg_recall(&dp->dl_stid.sc_stateid);
7670dd5e3fbcSChuck Lever 
767198d5c7c5SJeff Layton 		/*
767298d5c7c5SJeff Layton 		 * We skipped all entries that had a zero dl_time before,
767398d5c7c5SJeff Layton 		 * so we can now reset the dl_time back to 0. If a delegation
767498d5c7c5SJeff Layton 		 * break comes in now, then it won't make any difference since
767598d5c7c5SJeff Layton 		 * we're recalling it either way.
767698d5c7c5SJeff Layton 		 */
767798d5c7c5SJeff Layton 		spin_lock(&state_lock);
767898d5c7c5SJeff Layton 		dp->dl_time = 0;
767998d5c7c5SJeff Layton 		spin_unlock(&state_lock);
768098d5c7c5SJeff Layton 		nfsd_break_one_deleg(dp);
768198d5c7c5SJeff Layton 		put_client(clp);
768298d5c7c5SJeff Layton 	}
768398d5c7c5SJeff Layton }
768498d5c7c5SJeff Layton 
768598d5c7c5SJeff Layton u64
7686285abdeeSJeff Layton nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
768798d5c7c5SJeff Layton 				      size_t addr_size)
768898d5c7c5SJeff Layton {
768998d5c7c5SJeff Layton 	u64 count = 0;
769098d5c7c5SJeff Layton 	struct nfs4_client *clp;
769198d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
769298d5c7c5SJeff Layton 						nfsd_net_id);
769398d5c7c5SJeff Layton 	LIST_HEAD(reaplist);
769498d5c7c5SJeff Layton 
769598d5c7c5SJeff Layton 	if (!nfsd_netns_ready(nn))
769698d5c7c5SJeff Layton 		return count;
769798d5c7c5SJeff Layton 
769898d5c7c5SJeff Layton 	spin_lock(&nn->client_lock);
769998d5c7c5SJeff Layton 	clp = nfsd_find_client(addr, addr_size);
770098d5c7c5SJeff Layton 	if (clp)
770198d5c7c5SJeff Layton 		count = nfsd_find_all_delegations(clp, 0, &reaplist);
770298d5c7c5SJeff Layton 	spin_unlock(&nn->client_lock);
770398d5c7c5SJeff Layton 
770498d5c7c5SJeff Layton 	nfsd_recall_delegations(&reaplist);
770598d5c7c5SJeff Layton 	return count;
770698d5c7c5SJeff Layton }
770798d5c7c5SJeff Layton 
770898d5c7c5SJeff Layton u64
7709285abdeeSJeff Layton nfsd_inject_recall_delegations(u64 max)
771098d5c7c5SJeff Layton {
771198d5c7c5SJeff Layton 	u64 count = 0;
771298d5c7c5SJeff Layton 	struct nfs4_client *clp, *next;
771398d5c7c5SJeff Layton 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
771498d5c7c5SJeff Layton 						nfsd_net_id);
771598d5c7c5SJeff Layton 	LIST_HEAD(reaplist);
771698d5c7c5SJeff Layton 
771798d5c7c5SJeff Layton 	if (!nfsd_netns_ready(nn))
771898d5c7c5SJeff Layton 		return count;
771998d5c7c5SJeff Layton 
772098d5c7c5SJeff Layton 	spin_lock(&nn->client_lock);
772198d5c7c5SJeff Layton 	list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
772298d5c7c5SJeff Layton 		count += nfsd_find_all_delegations(clp, max - count, &reaplist);
772398d5c7c5SJeff Layton 		if (max != 0 && ++count >= max)
772498d5c7c5SJeff Layton 			break;
772598d5c7c5SJeff Layton 	}
772698d5c7c5SJeff Layton 	spin_unlock(&nn->client_lock);
772798d5c7c5SJeff Layton 	nfsd_recall_delegations(&reaplist);
772898d5c7c5SJeff Layton 	return count;
772998d5c7c5SJeff Layton }
773065178db4SBryan Schumaker #endif /* CONFIG_NFSD_FAULT_INJECTION */
773165178db4SBryan Schumaker 
7732c2f1a551SMeelap Shah /*
7733c2f1a551SMeelap Shah  * Since the lifetime of a delegation isn't limited to that of an open, a
7734c2f1a551SMeelap Shah  * client may quite reasonably hang on to a delegation as long as it has
7735c2f1a551SMeelap Shah  * the inode cached.  This becomes an obvious problem the first time a
7736c2f1a551SMeelap Shah  * client's inode cache approaches the size of the server's total memory.
7737c2f1a551SMeelap Shah  *
7738c2f1a551SMeelap Shah  * For now we avoid this problem by imposing a hard limit on the number
7739c2f1a551SMeelap Shah  * of delegations, which varies according to the server's memory size.
7740c2f1a551SMeelap Shah  */
7741c2f1a551SMeelap Shah static void
7742c2f1a551SMeelap Shah set_max_delegations(void)
7743c2f1a551SMeelap Shah {
7744c2f1a551SMeelap Shah 	/*
7745c2f1a551SMeelap Shah 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
7746c2f1a551SMeelap Shah 	 * estimates suggest that in the worst case (where every delegation
7747c2f1a551SMeelap Shah 	 * is for a different inode), a delegation could take about 1.5K,
7748c2f1a551SMeelap Shah 	 * giving a worst case usage of about 6% of memory.
7749c2f1a551SMeelap Shah 	 */
7750c2f1a551SMeelap Shah 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7751c2f1a551SMeelap Shah }
7752c2f1a551SMeelap Shah 
7753d85ed443SStanislav Kinsbursky static int nfs4_state_create_net(struct net *net)
77548daae4dcSStanislav Kinsbursky {
77558daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
77568daae4dcSStanislav Kinsbursky 	int i;
77578daae4dcSStanislav Kinsbursky 
77586da2ec56SKees Cook 	nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
77596da2ec56SKees Cook 					    sizeof(struct list_head),
77606da2ec56SKees Cook 					    GFP_KERNEL);
77618daae4dcSStanislav Kinsbursky 	if (!nn->conf_id_hashtbl)
7762382a62e7SStanislav Kinsbursky 		goto err;
77636da2ec56SKees Cook 	nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
77646da2ec56SKees Cook 					      sizeof(struct list_head),
77656da2ec56SKees Cook 					      GFP_KERNEL);
77660a7ec377SStanislav Kinsbursky 	if (!nn->unconf_id_hashtbl)
77670a7ec377SStanislav Kinsbursky 		goto err_unconf_id;
77686da2ec56SKees Cook 	nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
77696da2ec56SKees Cook 					      sizeof(struct list_head),
77706da2ec56SKees Cook 					      GFP_KERNEL);
77711872de0eSStanislav Kinsbursky 	if (!nn->sessionid_hashtbl)
77721872de0eSStanislav Kinsbursky 		goto err_sessionid;
77738daae4dcSStanislav Kinsbursky 
7774382a62e7SStanislav Kinsbursky 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
77758daae4dcSStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
77760a7ec377SStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7777382a62e7SStanislav Kinsbursky 	}
77781872de0eSStanislav Kinsbursky 	for (i = 0; i < SESSION_HASH_SIZE; i++)
77791872de0eSStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7780382a62e7SStanislav Kinsbursky 	nn->conf_name_tree = RB_ROOT;
7781a99454aaSStanislav Kinsbursky 	nn->unconf_name_tree = RB_ROOT;
77829cc76801SArnd Bergmann 	nn->boot_time = ktime_get_real_seconds();
778381833de1SVasily Averin 	nn->grace_ended = false;
778481833de1SVasily Averin 	nn->nfsd4_manager.block_opens = true;
778581833de1SVasily Averin 	INIT_LIST_HEAD(&nn->nfsd4_manager.list);
77865ed58bb2SStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->client_lru);
778773758fedSStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->close_lru);
7788e8c69d17SJ. Bruce Fields 	INIT_LIST_HEAD(&nn->del_recall_lru);
7789c9a49628SStanislav Kinsbursky 	spin_lock_init(&nn->client_lock);
7790e0639dc5SOlga Kornievskaia 	spin_lock_init(&nn->s2s_cp_lock);
7791e0639dc5SOlga Kornievskaia 	idr_init(&nn->s2s_cp_stateids);
77928daae4dcSStanislav Kinsbursky 
77930cc11a61SJeff Layton 	spin_lock_init(&nn->blocked_locks_lock);
77940cc11a61SJeff Layton 	INIT_LIST_HEAD(&nn->blocked_locks_lru);
77950cc11a61SJeff Layton 
779609121281SStanislav Kinsbursky 	INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7797d85ed443SStanislav Kinsbursky 	get_net(net);
779809121281SStanislav Kinsbursky 
77998daae4dcSStanislav Kinsbursky 	return 0;
7800382a62e7SStanislav Kinsbursky 
78011872de0eSStanislav Kinsbursky err_sessionid:
78029b531137SStanislav Kinsbursky 	kfree(nn->unconf_id_hashtbl);
78030a7ec377SStanislav Kinsbursky err_unconf_id:
78040a7ec377SStanislav Kinsbursky 	kfree(nn->conf_id_hashtbl);
7805382a62e7SStanislav Kinsbursky err:
7806382a62e7SStanislav Kinsbursky 	return -ENOMEM;
78078daae4dcSStanislav Kinsbursky }
78088daae4dcSStanislav Kinsbursky 
78098daae4dcSStanislav Kinsbursky static void
78104dce0ac9SStanislav Kinsbursky nfs4_state_destroy_net(struct net *net)
78118daae4dcSStanislav Kinsbursky {
78128daae4dcSStanislav Kinsbursky 	int i;
78138daae4dcSStanislav Kinsbursky 	struct nfs4_client *clp = NULL;
78148daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
78158daae4dcSStanislav Kinsbursky 
78168daae4dcSStanislav Kinsbursky 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
78178daae4dcSStanislav Kinsbursky 		while (!list_empty(&nn->conf_id_hashtbl[i])) {
78188daae4dcSStanislav Kinsbursky 			clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
78198daae4dcSStanislav Kinsbursky 			destroy_client(clp);
78208daae4dcSStanislav Kinsbursky 		}
78218daae4dcSStanislav Kinsbursky 	}
7822a99454aaSStanislav Kinsbursky 
782368ef3bc3SJeff Layton 	WARN_ON(!list_empty(&nn->blocked_locks_lru));
782468ef3bc3SJeff Layton 
78252b905635SKinglong Mee 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
78262b905635SKinglong Mee 		while (!list_empty(&nn->unconf_id_hashtbl[i])) {
78272b905635SKinglong Mee 			clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7828a99454aaSStanislav Kinsbursky 			destroy_client(clp);
7829a99454aaSStanislav Kinsbursky 		}
78302b905635SKinglong Mee 	}
7831a99454aaSStanislav Kinsbursky 
78321872de0eSStanislav Kinsbursky 	kfree(nn->sessionid_hashtbl);
78330a7ec377SStanislav Kinsbursky 	kfree(nn->unconf_id_hashtbl);
78348daae4dcSStanislav Kinsbursky 	kfree(nn->conf_id_hashtbl);
78354dce0ac9SStanislav Kinsbursky 	put_net(net);
78368daae4dcSStanislav Kinsbursky }
78378daae4dcSStanislav Kinsbursky 
7838f252bc68SStanislav Kinsbursky int
7839d85ed443SStanislav Kinsbursky nfs4_state_start_net(struct net *net)
7840ac4d8ff2SNeilBrown {
78415e1533c7SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7842b5a1a81eSJ. Bruce Fields 	int ret;
7843b5a1a81eSJ. Bruce Fields 
7844d85ed443SStanislav Kinsbursky 	ret = nfs4_state_create_net(net);
78458daae4dcSStanislav Kinsbursky 	if (ret)
78468daae4dcSStanislav Kinsbursky 		return ret;
7847d4318acdSJeff Layton 	locks_start_grace(net, &nn->nfsd4_manager);
7848d4318acdSJeff Layton 	nfsd4_client_tracking_init(net);
7849362063a5SScott Mayhew 	if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7850362063a5SScott Mayhew 		goto skip_grace;
785120b7d86fSArnd Bergmann 	printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
78527e981a8aSVasily Averin 	       nn->nfsd4_grace, net->ns.inum);
7853dd5e3fbcSChuck Lever 	trace_nfsd_grace_start(nn);
78545284b44eSStanislav Kinsbursky 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7855d85ed443SStanislav Kinsbursky 	return 0;
7856362063a5SScott Mayhew 
7857362063a5SScott Mayhew skip_grace:
7858362063a5SScott Mayhew 	printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7859362063a5SScott Mayhew 			net->ns.inum);
7860362063a5SScott Mayhew 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7861362063a5SScott Mayhew 	nfsd4_end_grace(nn);
7862362063a5SScott Mayhew 	return 0;
7863a6d6b781SJeff Layton }
7864d85ed443SStanislav Kinsbursky 
7865d85ed443SStanislav Kinsbursky /* initialization to perform when the nfsd service is started: */
7866d85ed443SStanislav Kinsbursky 
7867d85ed443SStanislav Kinsbursky int
7868d85ed443SStanislav Kinsbursky nfs4_state_start(void)
7869d85ed443SStanislav Kinsbursky {
7870d85ed443SStanislav Kinsbursky 	int ret;
7871d85ed443SStanislav Kinsbursky 
787251a54568SJeff Layton 	laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7873a6d6b781SJeff Layton 	if (laundry_wq == NULL) {
7874a6d6b781SJeff Layton 		ret = -ENOMEM;
7875a26dd64fSChuck Lever 		goto out;
7876a6d6b781SJeff Layton 	}
7877b5a1a81eSJ. Bruce Fields 	ret = nfsd4_create_callback_queue();
7878b5a1a81eSJ. Bruce Fields 	if (ret)
7879b5a1a81eSJ. Bruce Fields 		goto out_free_laundry;
788009121281SStanislav Kinsbursky 
7881c2f1a551SMeelap Shah 	set_max_delegations();
7882b5a1a81eSJ. Bruce Fields 	return 0;
7883d85ed443SStanislav Kinsbursky 
7884b5a1a81eSJ. Bruce Fields out_free_laundry:
7885b5a1a81eSJ. Bruce Fields 	destroy_workqueue(laundry_wq);
7886a26dd64fSChuck Lever out:
7887b5a1a81eSJ. Bruce Fields 	return ret;
78881da177e4SLinus Torvalds }
78891da177e4SLinus Torvalds 
7890f252bc68SStanislav Kinsbursky void
78914dce0ac9SStanislav Kinsbursky nfs4_state_shutdown_net(struct net *net)
78921da177e4SLinus Torvalds {
78931da177e4SLinus Torvalds 	struct nfs4_delegation *dp = NULL;
78941da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
78954dce0ac9SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
78961da177e4SLinus Torvalds 
78974dce0ac9SStanislav Kinsbursky 	cancel_delayed_work_sync(&nn->laundromat_work);
78984dce0ac9SStanislav Kinsbursky 	locks_end_grace(&nn->nfsd4_manager);
7899ac55fdc4SJeff Layton 
79001da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
7901cdc97505SBenny Halevy 	spin_lock(&state_lock);
7902e8c69d17SJ. Bruce Fields 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
79031da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
79043fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
790542690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
79061da177e4SLinus Torvalds 	}
7907cdc97505SBenny Halevy 	spin_unlock(&state_lock);
79081da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &reaplist) {
79091da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
791042690676SJeff Layton 		list_del_init(&dp->dl_recall_lru);
79110af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
79121da177e4SLinus Torvalds 	}
79131da177e4SLinus Torvalds 
79143320fef1SStanislav Kinsbursky 	nfsd4_client_tracking_exit(net);
79154dce0ac9SStanislav Kinsbursky 	nfs4_state_destroy_net(net);
79161da177e4SLinus Torvalds }
79171da177e4SLinus Torvalds 
79181da177e4SLinus Torvalds void
79191da177e4SLinus Torvalds nfs4_state_shutdown(void)
79201da177e4SLinus Torvalds {
79215e8d5c29SNeilBrown 	destroy_workqueue(laundry_wq);
7922c3935e30SJ. Bruce Fields 	nfsd4_destroy_callback_queue();
79231da177e4SLinus Torvalds }
79248b70484cSTigran Mkrtchyan 
79258b70484cSTigran Mkrtchyan static void
79268b70484cSTigran Mkrtchyan get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
79278b70484cSTigran Mkrtchyan {
792851100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
792951100d2bSOlga Kornievskaia 	    CURRENT_STATEID(stateid))
793037c593c5STigran Mkrtchyan 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
79318b70484cSTigran Mkrtchyan }
79328b70484cSTigran Mkrtchyan 
79338b70484cSTigran Mkrtchyan static void
79348b70484cSTigran Mkrtchyan put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
79358b70484cSTigran Mkrtchyan {
793637c593c5STigran Mkrtchyan 	if (cstate->minorversion) {
793737c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
793851100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
793937c593c5STigran Mkrtchyan 	}
794037c593c5STigran Mkrtchyan }
794137c593c5STigran Mkrtchyan 
794237c593c5STigran Mkrtchyan void
794337c593c5STigran Mkrtchyan clear_current_stateid(struct nfsd4_compound_state *cstate)
794437c593c5STigran Mkrtchyan {
794551100d2bSOlga Kornievskaia 	CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
79468b70484cSTigran Mkrtchyan }
79478b70484cSTigran Mkrtchyan 
794862cd4a59STigran Mkrtchyan /*
794962cd4a59STigran Mkrtchyan  * functions to set current state id
795062cd4a59STigran Mkrtchyan  */
79518b70484cSTigran Mkrtchyan void
7952b60e9859SChristoph Hellwig nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7953b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
79549428fe1aSTigran Mkrtchyan {
7955b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->open_downgrade.od_stateid);
79569428fe1aSTigran Mkrtchyan }
79579428fe1aSTigran Mkrtchyan 
79589428fe1aSTigran Mkrtchyan void
7959b60e9859SChristoph Hellwig nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7960b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
79618b70484cSTigran Mkrtchyan {
7962b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->open.op_stateid);
79638b70484cSTigran Mkrtchyan }
79648b70484cSTigran Mkrtchyan 
79658b70484cSTigran Mkrtchyan void
7966b60e9859SChristoph Hellwig nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7967b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
796862cd4a59STigran Mkrtchyan {
7969b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->close.cl_stateid);
797062cd4a59STigran Mkrtchyan }
797162cd4a59STigran Mkrtchyan 
797262cd4a59STigran Mkrtchyan void
7973b60e9859SChristoph Hellwig nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7974b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
797562cd4a59STigran Mkrtchyan {
7976b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->lock.lk_resp_stateid);
797762cd4a59STigran Mkrtchyan }
797862cd4a59STigran Mkrtchyan 
797962cd4a59STigran Mkrtchyan /*
798062cd4a59STigran Mkrtchyan  * functions to consume current state id
798162cd4a59STigran Mkrtchyan  */
79821e97b519STigran Mkrtchyan 
79831e97b519STigran Mkrtchyan void
798457832e7bSChristoph Hellwig nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
798557832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
79869428fe1aSTigran Mkrtchyan {
798757832e7bSChristoph Hellwig 	get_stateid(cstate, &u->open_downgrade.od_stateid);
79889428fe1aSTigran Mkrtchyan }
79899428fe1aSTigran Mkrtchyan 
79909428fe1aSTigran Mkrtchyan void
799157832e7bSChristoph Hellwig nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
799257832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
79939428fe1aSTigran Mkrtchyan {
799457832e7bSChristoph Hellwig 	get_stateid(cstate, &u->delegreturn.dr_stateid);
79959428fe1aSTigran Mkrtchyan }
79969428fe1aSTigran Mkrtchyan 
79979428fe1aSTigran Mkrtchyan void
799857832e7bSChristoph Hellwig nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
799957832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
80001e97b519STigran Mkrtchyan {
800157832e7bSChristoph Hellwig 	get_stateid(cstate, &u->free_stateid.fr_stateid);
80021e97b519STigran Mkrtchyan }
80031e97b519STigran Mkrtchyan 
80041e97b519STigran Mkrtchyan void
800557832e7bSChristoph Hellwig nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
800657832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
80071e97b519STigran Mkrtchyan {
800857832e7bSChristoph Hellwig 	get_stateid(cstate, &u->setattr.sa_stateid);
80091e97b519STigran Mkrtchyan }
80101e97b519STigran Mkrtchyan 
801162cd4a59STigran Mkrtchyan void
801257832e7bSChristoph Hellwig nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
801357832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
80148b70484cSTigran Mkrtchyan {
801557832e7bSChristoph Hellwig 	get_stateid(cstate, &u->close.cl_stateid);
80168b70484cSTigran Mkrtchyan }
80178b70484cSTigran Mkrtchyan 
80188b70484cSTigran Mkrtchyan void
801957832e7bSChristoph Hellwig nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
802057832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
80218b70484cSTigran Mkrtchyan {
802257832e7bSChristoph Hellwig 	get_stateid(cstate, &u->locku.lu_stateid);
80238b70484cSTigran Mkrtchyan }
802430813e27STigran Mkrtchyan 
802530813e27STigran Mkrtchyan void
802657832e7bSChristoph Hellwig nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
802757832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
802830813e27STigran Mkrtchyan {
802957832e7bSChristoph Hellwig 	get_stateid(cstate, &u->read.rd_stateid);
803030813e27STigran Mkrtchyan }
803130813e27STigran Mkrtchyan 
803230813e27STigran Mkrtchyan void
803357832e7bSChristoph Hellwig nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
803457832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
803530813e27STigran Mkrtchyan {
803657832e7bSChristoph Hellwig 	get_stateid(cstate, &u->write.wr_stateid);
803730813e27STigran Mkrtchyan }
8038