xref: /openbmc/linux/fs/nfsd/nfs4state.c (revision 9d60e8ec)
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>
46472d155aSNeilBrown #include <linux/fsnotify.h>
47d47b295eSChuck Lever #include <linux/rhashtable.h>
48f4e44b39SDai Ngo #include <linux/nfs_ssc.h>
49d47b295eSChuck Lever 
509a74af21SBoaz Harrosh #include "xdr4.h"
5106b332a5SJ. Bruce Fields #include "xdr4cb.h"
520a3adadeSJ. Bruce Fields #include "vfs.h"
53bfa4b365SJ. Bruce Fields #include "current_stateid.h"
541da177e4SLinus Torvalds 
555e1533c7SStanislav Kinsbursky #include "netns.h"
569cf514ccSChristoph Hellwig #include "pnfs.h"
57fd4f83fdSJeff Layton #include "filecache.h"
58dd5e3fbcSChuck Lever #include "trace.h"
595e1533c7SStanislav Kinsbursky 
601da177e4SLinus Torvalds #define NFSDDBG_FACILITY                NFSDDBG_PROC
611da177e4SLinus Torvalds 
62f32f3c2dSJ. Bruce Fields #define all_ones {{~0,~0},~0}
63f32f3c2dSJ. Bruce Fields static const stateid_t one_stateid = {
64f32f3c2dSJ. Bruce Fields 	.si_generation = ~0,
65f32f3c2dSJ. Bruce Fields 	.si_opaque = all_ones,
66f32f3c2dSJ. Bruce Fields };
67f32f3c2dSJ. Bruce Fields static const stateid_t zero_stateid = {
68f32f3c2dSJ. Bruce Fields 	/* all fields zero */
69f32f3c2dSJ. Bruce Fields };
7019ff0f28STigran Mkrtchyan static const stateid_t currentstateid = {
7119ff0f28STigran Mkrtchyan 	.si_generation = 1,
7219ff0f28STigran Mkrtchyan };
73fb500a7cSTrond Myklebust static const stateid_t close_stateid = {
74fb500a7cSTrond Myklebust 	.si_generation = 0xffffffffU,
75fb500a7cSTrond Myklebust };
76f32f3c2dSJ. Bruce Fields 
77ec6b5d7bSAndy Adamson static u64 current_sessionid = 1;
78fd39ca9aSNeilBrown 
79f32f3c2dSJ. Bruce Fields #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
80f32f3c2dSJ. Bruce Fields #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
8119ff0f28STigran Mkrtchyan #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
82ae254dacSAndrew Elble #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* forward declarations */
85f9c00c3aSJeff Layton static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
866011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
87362063a5SScott Mayhew void nfsd4_end_grace(struct nfsd_net *nn);
88624322f1SOlga Kornievskaia static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
893341678fSChuck Lever static void nfsd4_file_hash_remove(struct nfs4_file *fi);
901da177e4SLinus Torvalds 
918b671b80SJ. Bruce Fields /* Locking: */
928b671b80SJ. Bruce Fields 
938b671b80SJ. Bruce Fields /*
948b671b80SJ. Bruce Fields  * Currently used for the del_recall_lru and file hash table.  In an
958b671b80SJ. Bruce Fields  * effort to decrease the scope of the client_mutex, this spinlock may
968b671b80SJ. Bruce Fields  * eventually cover more:
978b671b80SJ. Bruce Fields  */
98cdc97505SBenny Halevy static DEFINE_SPINLOCK(state_lock);
998b671b80SJ. Bruce Fields 
1004f34bd05SAndrew Elble enum nfsd4_st_mutex_lock_subclass {
1014f34bd05SAndrew Elble 	OPEN_STATEID_MUTEX = 0,
1024f34bd05SAndrew Elble 	LOCK_STATEID_MUTEX = 1,
1034f34bd05SAndrew Elble };
1044f34bd05SAndrew Elble 
105b401be22SJeff Layton /*
106b401be22SJeff Layton  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
107b401be22SJeff Layton  * the refcount on the open stateid to drop.
108b401be22SJeff Layton  */
109b401be22SJeff Layton static DECLARE_WAIT_QUEUE_HEAD(close_wq);
110b401be22SJeff Layton 
11189c905beSJ. Bruce Fields /*
11289c905beSJ. Bruce Fields  * A waitqueue where a writer to clients/#/ctl destroying a client can
11389c905beSJ. Bruce Fields  * wait for cl_rpc_users to drop to 0 and then for the client to be
11489c905beSJ. Bruce Fields  * unhashed.
11589c905beSJ. Bruce Fields  */
11689c905beSJ. Bruce Fields static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
11789c905beSJ. Bruce Fields 
1189258a2d5SJeff Layton static struct kmem_cache *client_slab;
119abf1135bSChristoph Hellwig static struct kmem_cache *openowner_slab;
120abf1135bSChristoph Hellwig static struct kmem_cache *lockowner_slab;
121abf1135bSChristoph Hellwig static struct kmem_cache *file_slab;
122abf1135bSChristoph Hellwig static struct kmem_cache *stateid_slab;
123abf1135bSChristoph Hellwig static struct kmem_cache *deleg_slab;
1248287f009SSachin Bhamare static struct kmem_cache *odstate_slab;
125e60d4398SNeilBrown 
12666b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *);
127508dc6e1SBenny Halevy 
128c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
12976d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
1300162ac2bSChristoph Hellwig 
13166af2579SDai Ngo static struct workqueue_struct *laundry_wq;
13266af2579SDai Ngo 
nfsd4_create_laundry_wq(void)133d76cc46bSDai Ngo int nfsd4_create_laundry_wq(void)
134d76cc46bSDai Ngo {
135d76cc46bSDai Ngo 	int rc = 0;
136d76cc46bSDai Ngo 
137d76cc46bSDai Ngo 	laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
138d76cc46bSDai Ngo 	if (laundry_wq == NULL)
139d76cc46bSDai Ngo 		rc = -ENOMEM;
140d76cc46bSDai Ngo 	return rc;
141d76cc46bSDai Ngo }
142d76cc46bSDai Ngo 
nfsd4_destroy_laundry_wq(void)143d76cc46bSDai Ngo void nfsd4_destroy_laundry_wq(void)
144d76cc46bSDai Ngo {
145d76cc46bSDai Ngo 	destroy_workqueue(laundry_wq);
146d76cc46bSDai Ngo }
147d76cc46bSDai Ngo 
is_session_dead(struct nfsd4_session * ses)14866b2b9b2SJ. Bruce Fields static bool is_session_dead(struct nfsd4_session *ses)
149508dc6e1SBenny Halevy {
15066b2b9b2SJ. Bruce Fields 	return ses->se_flags & NFS4_SESSION_DEAD;
15166b2b9b2SJ. Bruce Fields }
15266b2b9b2SJ. Bruce Fields 
mark_session_dead_locked(struct nfsd4_session * ses,int ref_held_by_me)153f0f51f5cSJ. Bruce Fields static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
154f0f51f5cSJ. Bruce Fields {
155f0f51f5cSJ. Bruce Fields 	if (atomic_read(&ses->se_ref) > ref_held_by_me)
15666b2b9b2SJ. Bruce Fields 		return nfserr_jukebox;
15766b2b9b2SJ. Bruce Fields 	ses->se_flags |= NFS4_SESSION_DEAD;
15866b2b9b2SJ. Bruce Fields 	return nfs_ok;
15966b2b9b2SJ. Bruce Fields }
16066b2b9b2SJ. Bruce Fields 
is_client_expired(struct nfs4_client * clp)161221a6876SJ. Bruce Fields static bool is_client_expired(struct nfs4_client *clp)
162221a6876SJ. Bruce Fields {
163221a6876SJ. Bruce Fields 	return clp->cl_time == 0;
164221a6876SJ. Bruce Fields }
165221a6876SJ. Bruce Fields 
nfsd4_dec_courtesy_client_count(struct nfsd_net * nn,struct nfs4_client * clp)1663a4ea23dSDai Ngo static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
1673a4ea23dSDai Ngo 					struct nfs4_client *clp)
1683a4ea23dSDai Ngo {
1693a4ea23dSDai Ngo 	if (clp->cl_state != NFSD4_ACTIVE)
1703a4ea23dSDai Ngo 		atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
1713a4ea23dSDai Ngo }
1723a4ea23dSDai Ngo 
get_client_locked(struct nfs4_client * clp)173221a6876SJ. Bruce Fields static __be32 get_client_locked(struct nfs4_client *clp)
174221a6876SJ. Bruce Fields {
1750a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1760a880a28STrond Myklebust 
1770a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
1780a880a28STrond Myklebust 
179221a6876SJ. Bruce Fields 	if (is_client_expired(clp))
180221a6876SJ. Bruce Fields 		return nfserr_expired;
18114ed14ccSJ. Bruce Fields 	atomic_inc(&clp->cl_rpc_users);
1823a4ea23dSDai Ngo 	nfsd4_dec_courtesy_client_count(nn, clp);
18366af2579SDai Ngo 	clp->cl_state = NFSD4_ACTIVE;
184221a6876SJ. Bruce Fields 	return nfs_ok;
185221a6876SJ. Bruce Fields }
186221a6876SJ. Bruce Fields 
187221a6876SJ. Bruce Fields /* must be called under the client_lock */
188221a6876SJ. Bruce Fields static inline void
renew_client_locked(struct nfs4_client * clp)189221a6876SJ. Bruce Fields renew_client_locked(struct nfs4_client *clp)
190221a6876SJ. Bruce Fields {
191221a6876SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192221a6876SJ. Bruce Fields 
193221a6876SJ. Bruce Fields 	if (is_client_expired(clp)) {
194221a6876SJ. Bruce Fields 		WARN_ON(1);
195221a6876SJ. Bruce Fields 		printk("%s: client (clientid %08x/%08x) already expired\n",
196221a6876SJ. Bruce Fields 			__func__,
197221a6876SJ. Bruce Fields 			clp->cl_clientid.cl_boot,
198221a6876SJ. Bruce Fields 			clp->cl_clientid.cl_id);
199221a6876SJ. Bruce Fields 		return;
200221a6876SJ. Bruce Fields 	}
201221a6876SJ. Bruce Fields 
202221a6876SJ. Bruce Fields 	list_move_tail(&clp->cl_lru, &nn->client_lru);
20320b7d86fSArnd Bergmann 	clp->cl_time = ktime_get_boottime_seconds();
2043a4ea23dSDai Ngo 	nfsd4_dec_courtesy_client_count(nn, clp);
20566af2579SDai Ngo 	clp->cl_state = NFSD4_ACTIVE;
206221a6876SJ. Bruce Fields }
207221a6876SJ. Bruce Fields 
put_client_renew_locked(struct nfs4_client * clp)208ba138435SFengguang Wu static void put_client_renew_locked(struct nfs4_client *clp)
209221a6876SJ. Bruce Fields {
2100a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2110a880a28STrond Myklebust 
2120a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
2130a880a28STrond Myklebust 
21414ed14ccSJ. Bruce Fields 	if (!atomic_dec_and_test(&clp->cl_rpc_users))
215221a6876SJ. Bruce Fields 		return;
216221a6876SJ. Bruce Fields 	if (!is_client_expired(clp))
217221a6876SJ. Bruce Fields 		renew_client_locked(clp);
21889c905beSJ. Bruce Fields 	else
21989c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
220221a6876SJ. Bruce Fields }
221221a6876SJ. Bruce Fields 
put_client_renew(struct nfs4_client * clp)2224b24ca7dSJeff Layton static void put_client_renew(struct nfs4_client *clp)
2234b24ca7dSJeff Layton {
2244b24ca7dSJeff Layton 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2254b24ca7dSJeff Layton 
22614ed14ccSJ. Bruce Fields 	if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
227d6c249b4SJeff Layton 		return;
228d6c249b4SJeff Layton 	if (!is_client_expired(clp))
229d6c249b4SJeff Layton 		renew_client_locked(clp);
23089c905beSJ. Bruce Fields 	else
23189c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
2324b24ca7dSJeff Layton 	spin_unlock(&nn->client_lock);
2334b24ca7dSJeff Layton }
2344b24ca7dSJeff Layton 
nfsd4_get_session_locked(struct nfsd4_session * ses)235d4e19e70STrond Myklebust static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
236d4e19e70STrond Myklebust {
237d4e19e70STrond Myklebust 	__be32 status;
238d4e19e70STrond Myklebust 
239d4e19e70STrond Myklebust 	if (is_session_dead(ses))
240d4e19e70STrond Myklebust 		return nfserr_badsession;
241d4e19e70STrond Myklebust 	status = get_client_locked(ses->se_client);
242d4e19e70STrond Myklebust 	if (status)
243d4e19e70STrond Myklebust 		return status;
244d4e19e70STrond Myklebust 	atomic_inc(&ses->se_ref);
245d4e19e70STrond Myklebust 	return nfs_ok;
246d4e19e70STrond Myklebust }
247d4e19e70STrond Myklebust 
nfsd4_put_session_locked(struct nfsd4_session * ses)248d4e19e70STrond Myklebust static void nfsd4_put_session_locked(struct nfsd4_session *ses)
249d4e19e70STrond Myklebust {
250d4e19e70STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
2510a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2520a880a28STrond Myklebust 
2530a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
254d4e19e70STrond Myklebust 
255d4e19e70STrond Myklebust 	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
256d4e19e70STrond Myklebust 		free_session(ses);
257d4e19e70STrond Myklebust 	put_client_renew_locked(clp);
258d4e19e70STrond Myklebust }
259d4e19e70STrond Myklebust 
nfsd4_put_session(struct nfsd4_session * ses)260d4e19e70STrond Myklebust static void nfsd4_put_session(struct nfsd4_session *ses)
261d4e19e70STrond Myklebust {
262d4e19e70STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
263d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
264d4e19e70STrond Myklebust 
265d4e19e70STrond Myklebust 	spin_lock(&nn->client_lock);
266d4e19e70STrond Myklebust 	nfsd4_put_session_locked(ses);
267d4e19e70STrond Myklebust 	spin_unlock(&nn->client_lock);
268d4e19e70STrond Myklebust }
269d4e19e70STrond Myklebust 
27076d348faSJeff Layton static struct nfsd4_blocked_lock *
find_blocked_lock(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)27176d348faSJeff Layton find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
27276d348faSJeff Layton 			struct nfsd_net *nn)
27376d348faSJeff Layton {
27476d348faSJeff Layton 	struct nfsd4_blocked_lock *cur, *found = NULL;
27576d348faSJeff Layton 
2760cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
27776d348faSJeff Layton 	list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
27876d348faSJeff Layton 		if (fh_match(fh, &cur->nbl_fh)) {
27976d348faSJeff Layton 			list_del_init(&cur->nbl_list);
28047446d74SVasily Averin 			WARN_ON(list_empty(&cur->nbl_lru));
2817919d0a2SJeff Layton 			list_del_init(&cur->nbl_lru);
28276d348faSJeff Layton 			found = cur;
28376d348faSJeff Layton 			break;
28476d348faSJeff Layton 		}
28576d348faSJeff Layton 	}
2860cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
28776d348faSJeff Layton 	if (found)
288cb03f94fSNeilBrown 		locks_delete_block(&found->nbl_lock);
28976d348faSJeff Layton 	return found;
29076d348faSJeff Layton }
29176d348faSJeff Layton 
29276d348faSJeff Layton static struct nfsd4_blocked_lock *
find_or_allocate_block(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)29376d348faSJeff Layton find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
29476d348faSJeff Layton 			struct nfsd_net *nn)
29576d348faSJeff Layton {
29676d348faSJeff Layton 	struct nfsd4_blocked_lock *nbl;
29776d348faSJeff Layton 
29876d348faSJeff Layton 	nbl = find_blocked_lock(lo, fh, nn);
29976d348faSJeff Layton 	if (!nbl) {
30076d348faSJeff Layton 		nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
30176d348faSJeff Layton 		if (nbl) {
302e1e8399eSVasily Averin 			INIT_LIST_HEAD(&nbl->nbl_list);
303e1e8399eSVasily Averin 			INIT_LIST_HEAD(&nbl->nbl_lru);
30476d348faSJeff Layton 			fh_copy_shallow(&nbl->nbl_fh, fh);
30576d348faSJeff Layton 			locks_init_lock(&nbl->nbl_lock);
30647446d74SVasily Averin 			kref_init(&nbl->nbl_kref);
30776d348faSJeff Layton 			nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
30876d348faSJeff Layton 					&nfsd4_cb_notify_lock_ops,
30976d348faSJeff Layton 					NFSPROC4_CLNT_CB_NOTIFY_LOCK);
31076d348faSJeff Layton 		}
31176d348faSJeff Layton 	}
31276d348faSJeff Layton 	return nbl;
31376d348faSJeff Layton }
31476d348faSJeff Layton 
31576d348faSJeff Layton static void
free_nbl(struct kref * kref)31647446d74SVasily Averin free_nbl(struct kref *kref)
31747446d74SVasily Averin {
31847446d74SVasily Averin 	struct nfsd4_blocked_lock *nbl;
31947446d74SVasily Averin 
32047446d74SVasily Averin 	nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
32147446d74SVasily Averin 	kfree(nbl);
32247446d74SVasily Averin }
32347446d74SVasily Averin 
32447446d74SVasily Averin static void
free_blocked_lock(struct nfsd4_blocked_lock * nbl)32576d348faSJeff Layton free_blocked_lock(struct nfsd4_blocked_lock *nbl)
32676d348faSJeff Layton {
3276aaafc43SJeff Layton 	locks_delete_block(&nbl->nbl_lock);
32876d348faSJeff Layton 	locks_release_private(&nbl->nbl_lock);
32947446d74SVasily Averin 	kref_put(&nbl->nbl_kref, free_nbl);
33076d348faSJeff Layton }
33176d348faSJeff Layton 
33268ef3bc3SJeff Layton static void
remove_blocked_locks(struct nfs4_lockowner * lo)33368ef3bc3SJeff Layton remove_blocked_locks(struct nfs4_lockowner *lo)
33468ef3bc3SJeff Layton {
33568ef3bc3SJeff Layton 	struct nfs4_client *clp = lo->lo_owner.so_client;
33668ef3bc3SJeff Layton 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
33768ef3bc3SJeff Layton 	struct nfsd4_blocked_lock *nbl;
33868ef3bc3SJeff Layton 	LIST_HEAD(reaplist);
33968ef3bc3SJeff Layton 
34068ef3bc3SJeff Layton 	/* Dequeue all blocked locks */
34168ef3bc3SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
34268ef3bc3SJeff Layton 	while (!list_empty(&lo->lo_blocked)) {
34368ef3bc3SJeff Layton 		nbl = list_first_entry(&lo->lo_blocked,
34468ef3bc3SJeff Layton 					struct nfsd4_blocked_lock,
34568ef3bc3SJeff Layton 					nbl_list);
34668ef3bc3SJeff Layton 		list_del_init(&nbl->nbl_list);
34747446d74SVasily Averin 		WARN_ON(list_empty(&nbl->nbl_lru));
34868ef3bc3SJeff Layton 		list_move(&nbl->nbl_lru, &reaplist);
34968ef3bc3SJeff Layton 	}
35068ef3bc3SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
35168ef3bc3SJeff Layton 
35268ef3bc3SJeff Layton 	/* Now free them */
35368ef3bc3SJeff Layton 	while (!list_empty(&reaplist)) {
35468ef3bc3SJeff Layton 		nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
35568ef3bc3SJeff Layton 					nbl_lru);
35668ef3bc3SJeff Layton 		list_del_init(&nbl->nbl_lru);
35768ef3bc3SJeff Layton 		free_blocked_lock(nbl);
35868ef3bc3SJeff Layton 	}
35968ef3bc3SJeff Layton }
36068ef3bc3SJeff Layton 
361f456458eSJeff Layton static void
nfsd4_cb_notify_lock_prepare(struct nfsd4_callback * cb)362f456458eSJeff Layton nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
363f456458eSJeff Layton {
364f456458eSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
365f456458eSJeff Layton 						struct nfsd4_blocked_lock, nbl_cb);
366f456458eSJeff Layton 	locks_delete_block(&nbl->nbl_lock);
367f456458eSJeff Layton }
368f456458eSJeff Layton 
36976d348faSJeff Layton static int
nfsd4_cb_notify_lock_done(struct nfsd4_callback * cb,struct rpc_task * task)37076d348faSJeff Layton nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
37176d348faSJeff Layton {
3721035d654SChuck Lever 	trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
3731035d654SChuck Lever 
37476d348faSJeff Layton 	/*
37576d348faSJeff Layton 	 * Since this is just an optimization, we don't try very hard if it
37676d348faSJeff Layton 	 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
37776d348faSJeff Layton 	 * just quit trying on anything else.
37876d348faSJeff Layton 	 */
37976d348faSJeff Layton 	switch (task->tk_status) {
38076d348faSJeff Layton 	case -NFS4ERR_DELAY:
38176d348faSJeff Layton 		rpc_delay(task, 1 * HZ);
38276d348faSJeff Layton 		return 0;
38376d348faSJeff Layton 	default:
38476d348faSJeff Layton 		return 1;
38576d348faSJeff Layton 	}
38676d348faSJeff Layton }
38776d348faSJeff Layton 
38876d348faSJeff Layton static void
nfsd4_cb_notify_lock_release(struct nfsd4_callback * cb)38976d348faSJeff Layton nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
39076d348faSJeff Layton {
39176d348faSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
39276d348faSJeff Layton 						struct nfsd4_blocked_lock, nbl_cb);
39376d348faSJeff Layton 
39476d348faSJeff Layton 	free_blocked_lock(nbl);
39576d348faSJeff Layton }
39676d348faSJeff Layton 
39776d348faSJeff Layton static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
398f456458eSJeff Layton 	.prepare	= nfsd4_cb_notify_lock_prepare,
39976d348faSJeff Layton 	.done		= nfsd4_cb_notify_lock_done,
40076d348faSJeff Layton 	.release	= nfsd4_cb_notify_lock_release,
40176d348faSJeff Layton };
40276d348faSJeff Layton 
403ebd9d2c2SJ. Bruce Fields /*
404ebd9d2c2SJ. Bruce Fields  * We store the NONE, READ, WRITE, and BOTH bits separately in the
405ebd9d2c2SJ. Bruce Fields  * st_{access,deny}_bmap field of the stateid, in order to track not
406ebd9d2c2SJ. Bruce Fields  * only what share bits are currently in force, but also what
407ebd9d2c2SJ. Bruce Fields  * combinations of share bits previous opens have used.  This allows us
4083dcd1d8aSJ. Bruce Fields  * to enforce the recommendation in
4093dcd1d8aSJ. Bruce Fields  * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
4103dcd1d8aSJ. Bruce Fields  * the server return an error if the client attempt to downgrade to a
4113dcd1d8aSJ. Bruce Fields  * combination of share bits not explicable by closing some of its
4123dcd1d8aSJ. Bruce Fields  * previous opens.
413ebd9d2c2SJ. Bruce Fields  *
4143dcd1d8aSJ. Bruce Fields  * This enforcement is arguably incomplete, since we don't keep
415ebd9d2c2SJ. Bruce Fields  * track of access/deny bit combinations; so, e.g., we allow:
416ebd9d2c2SJ. Bruce Fields  *
417ebd9d2c2SJ. Bruce Fields  *	OPEN allow read, deny write
418ebd9d2c2SJ. Bruce Fields  *	OPEN allow both, deny none
419ebd9d2c2SJ. Bruce Fields  *	DOWNGRADE allow read, deny none
420ebd9d2c2SJ. Bruce Fields  *
421ebd9d2c2SJ. Bruce Fields  * which we should reject.
4223dcd1d8aSJ. Bruce Fields  *
4233dcd1d8aSJ. Bruce Fields  * But you could also argue that our current code is already overkill,
4243dcd1d8aSJ. Bruce Fields  * since it only exists to return NFS4ERR_INVAL on incorrect client
4253dcd1d8aSJ. Bruce Fields  * behavior.
426ebd9d2c2SJ. Bruce Fields  */
427ebd9d2c2SJ. Bruce Fields static unsigned int
bmap_to_share_mode(unsigned long bmap)428ebd9d2c2SJ. Bruce Fields bmap_to_share_mode(unsigned long bmap)
429ebd9d2c2SJ. Bruce Fields {
430ebd9d2c2SJ. Bruce Fields 	int i;
431ebd9d2c2SJ. Bruce Fields 	unsigned int access = 0;
432ebd9d2c2SJ. Bruce Fields 
433ebd9d2c2SJ. Bruce Fields 	for (i = 1; i < 4; i++) {
434ebd9d2c2SJ. Bruce Fields 		if (test_bit(i, &bmap))
435ebd9d2c2SJ. Bruce Fields 			access |= i;
436ebd9d2c2SJ. Bruce Fields 	}
437ebd9d2c2SJ. Bruce Fields 	return access;
438ebd9d2c2SJ. Bruce Fields }
439ebd9d2c2SJ. Bruce Fields 
440ebd9d2c2SJ. Bruce Fields /* set share access for a given stateid */
441ebd9d2c2SJ. Bruce Fields static inline void
set_access(u32 access,struct nfs4_ol_stateid * stp)442ebd9d2c2SJ. Bruce Fields set_access(u32 access, struct nfs4_ol_stateid *stp)
443ebd9d2c2SJ. Bruce Fields {
444ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << access;
445ebd9d2c2SJ. Bruce Fields 
446ebd9d2c2SJ. Bruce Fields 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
447ebd9d2c2SJ. Bruce Fields 	stp->st_access_bmap |= mask;
448ebd9d2c2SJ. Bruce Fields }
449ebd9d2c2SJ. Bruce Fields 
450ebd9d2c2SJ. Bruce Fields /* clear share access for a given stateid */
451ebd9d2c2SJ. Bruce Fields static inline void
clear_access(u32 access,struct nfs4_ol_stateid * stp)452ebd9d2c2SJ. Bruce Fields clear_access(u32 access, struct nfs4_ol_stateid *stp)
453ebd9d2c2SJ. Bruce Fields {
454ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << access;
455ebd9d2c2SJ. Bruce Fields 
456ebd9d2c2SJ. Bruce Fields 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
457ebd9d2c2SJ. Bruce Fields 	stp->st_access_bmap &= ~mask;
458ebd9d2c2SJ. Bruce Fields }
459ebd9d2c2SJ. Bruce Fields 
460ebd9d2c2SJ. Bruce Fields /* test whether a given stateid has access */
461ebd9d2c2SJ. Bruce Fields static inline bool
test_access(u32 access,struct nfs4_ol_stateid * stp)462ebd9d2c2SJ. Bruce Fields test_access(u32 access, struct nfs4_ol_stateid *stp)
463ebd9d2c2SJ. Bruce Fields {
464ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << access;
465ebd9d2c2SJ. Bruce Fields 
466ebd9d2c2SJ. Bruce Fields 	return (bool)(stp->st_access_bmap & mask);
467ebd9d2c2SJ. Bruce Fields }
468ebd9d2c2SJ. Bruce Fields 
469ebd9d2c2SJ. Bruce Fields /* set share deny for a given stateid */
470ebd9d2c2SJ. Bruce Fields static inline void
set_deny(u32 deny,struct nfs4_ol_stateid * stp)471ebd9d2c2SJ. Bruce Fields set_deny(u32 deny, struct nfs4_ol_stateid *stp)
472ebd9d2c2SJ. Bruce Fields {
473ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << deny;
474ebd9d2c2SJ. Bruce Fields 
475ebd9d2c2SJ. Bruce Fields 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
476ebd9d2c2SJ. Bruce Fields 	stp->st_deny_bmap |= mask;
477ebd9d2c2SJ. Bruce Fields }
478ebd9d2c2SJ. Bruce Fields 
479ebd9d2c2SJ. Bruce Fields /* clear share deny for a given stateid */
480ebd9d2c2SJ. Bruce Fields static inline void
clear_deny(u32 deny,struct nfs4_ol_stateid * stp)481ebd9d2c2SJ. Bruce Fields clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
482ebd9d2c2SJ. Bruce Fields {
483ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << deny;
484ebd9d2c2SJ. Bruce Fields 
485ebd9d2c2SJ. Bruce Fields 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
486ebd9d2c2SJ. Bruce Fields 	stp->st_deny_bmap &= ~mask;
487ebd9d2c2SJ. Bruce Fields }
488ebd9d2c2SJ. Bruce Fields 
489ebd9d2c2SJ. Bruce Fields /* test whether a given stateid is denying specific access */
490ebd9d2c2SJ. Bruce Fields static inline bool
test_deny(u32 deny,struct nfs4_ol_stateid * stp)491ebd9d2c2SJ. Bruce Fields test_deny(u32 deny, struct nfs4_ol_stateid *stp)
492ebd9d2c2SJ. Bruce Fields {
493ebd9d2c2SJ. Bruce Fields 	unsigned char mask = 1 << deny;
494ebd9d2c2SJ. Bruce Fields 
495ebd9d2c2SJ. Bruce Fields 	return (bool)(stp->st_deny_bmap & mask);
496ebd9d2c2SJ. Bruce Fields }
497ebd9d2c2SJ. Bruce Fields 
nfs4_access_to_omode(u32 access)498ebd9d2c2SJ. Bruce Fields static int nfs4_access_to_omode(u32 access)
499ebd9d2c2SJ. Bruce Fields {
500ebd9d2c2SJ. Bruce Fields 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
501ebd9d2c2SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
502ebd9d2c2SJ. Bruce Fields 		return O_RDONLY;
503ebd9d2c2SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
504ebd9d2c2SJ. Bruce Fields 		return O_WRONLY;
505ebd9d2c2SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
506ebd9d2c2SJ. Bruce Fields 		return O_RDWR;
507ebd9d2c2SJ. Bruce Fields 	}
508ebd9d2c2SJ. Bruce Fields 	WARN_ON_ONCE(1);
509ebd9d2c2SJ. Bruce Fields 	return O_RDONLY;
510ebd9d2c2SJ. Bruce Fields }
511ebd9d2c2SJ. Bruce Fields 
512ebd9d2c2SJ. Bruce Fields static inline int
access_permit_read(struct nfs4_ol_stateid * stp)513ebd9d2c2SJ. Bruce Fields access_permit_read(struct nfs4_ol_stateid *stp)
514ebd9d2c2SJ. Bruce Fields {
515ebd9d2c2SJ. Bruce Fields 	return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
516ebd9d2c2SJ. Bruce Fields 		test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
517ebd9d2c2SJ. Bruce Fields 		test_access(NFS4_SHARE_ACCESS_WRITE, stp);
518ebd9d2c2SJ. Bruce Fields }
519ebd9d2c2SJ. Bruce Fields 
520ebd9d2c2SJ. Bruce Fields static inline int
access_permit_write(struct nfs4_ol_stateid * stp)521ebd9d2c2SJ. Bruce Fields access_permit_write(struct nfs4_ol_stateid *stp)
522ebd9d2c2SJ. Bruce Fields {
523ebd9d2c2SJ. Bruce Fields 	return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
524ebd9d2c2SJ. Bruce Fields 		test_access(NFS4_SHARE_ACCESS_BOTH, stp);
525ebd9d2c2SJ. Bruce Fields }
526ebd9d2c2SJ. Bruce Fields 
527b5971afaSKinglong Mee static inline struct nfs4_stateowner *
nfs4_get_stateowner(struct nfs4_stateowner * sop)528b5971afaSKinglong Mee nfs4_get_stateowner(struct nfs4_stateowner *sop)
529b5971afaSKinglong Mee {
530b5971afaSKinglong Mee 	atomic_inc(&sop->so_count);
531b5971afaSKinglong Mee 	return sop;
532b5971afaSKinglong Mee }
533b5971afaSKinglong Mee 
5347ffb5880STrond Myklebust static int
same_owner_str(struct nfs4_stateowner * sop,struct xdr_netobj * owner)535d4f0489fSTrond Myklebust same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
5367ffb5880STrond Myklebust {
5377ffb5880STrond Myklebust 	return (sop->so_owner.len == owner->len) &&
538d4f0489fSTrond Myklebust 		0 == memcmp(sop->so_owner.data, owner->data, owner->len);
5397ffb5880STrond Myklebust }
5407ffb5880STrond Myklebust 
5417ffb5880STrond Myklebust static struct nfs4_openowner *
find_openstateowner_str_locked(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)5427ffb5880STrond Myklebust find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
543d4f0489fSTrond Myklebust 			struct nfs4_client *clp)
5447ffb5880STrond Myklebust {
5457ffb5880STrond Myklebust 	struct nfs4_stateowner *so;
5467ffb5880STrond Myklebust 
547d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
5487ffb5880STrond Myklebust 
549d4f0489fSTrond Myklebust 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
550d4f0489fSTrond Myklebust 			    so_strhash) {
5517ffb5880STrond Myklebust 		if (!so->so_is_open_owner)
5527ffb5880STrond Myklebust 			continue;
553b5971afaSKinglong Mee 		if (same_owner_str(so, &open->op_owner))
554b5971afaSKinglong Mee 			return openowner(nfs4_get_stateowner(so));
5557ffb5880STrond Myklebust 	}
5567ffb5880STrond Myklebust 	return NULL;
5577ffb5880STrond Myklebust }
5587ffb5880STrond Myklebust 
5597ffb5880STrond Myklebust static struct nfs4_openowner *
find_openstateowner_str(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)5607ffb5880STrond Myklebust find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
561d4f0489fSTrond Myklebust 			struct nfs4_client *clp)
5627ffb5880STrond Myklebust {
5637ffb5880STrond Myklebust 	struct nfs4_openowner *oo;
5647ffb5880STrond Myklebust 
565d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
566d4f0489fSTrond Myklebust 	oo = find_openstateowner_str_locked(hashval, open, clp);
567d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
5687ffb5880STrond Myklebust 	return oo;
5697ffb5880STrond Myklebust }
5707ffb5880STrond Myklebust 
5711da177e4SLinus Torvalds static inline u32
opaque_hashval(const void * ptr,int nbytes)5721da177e4SLinus Torvalds opaque_hashval(const void *ptr, int nbytes)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	unsigned char *cptr = (unsigned char *) ptr;
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds 	u32 x = 0;
5771da177e4SLinus Torvalds 	while (nbytes--) {
5781da177e4SLinus Torvalds 		x *= 37;
5791da177e4SLinus Torvalds 		x += *cptr++;
5801da177e4SLinus Torvalds 	}
5811da177e4SLinus Torvalds 	return x;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
nfsd4_free_file_rcu(struct rcu_head * rcu)5845b095e99SJeff Layton static void nfsd4_free_file_rcu(struct rcu_head *rcu)
58532513b40SJ. Bruce Fields {
5865b095e99SJeff Layton 	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
5875b095e99SJeff Layton 
5885b095e99SJeff Layton 	kmem_cache_free(file_slab, fp);
58932513b40SJ. Bruce Fields }
59032513b40SJ. Bruce Fields 
591e6ba76e1SChristoph Hellwig void
put_nfs4_file(struct nfs4_file * fi)59213cd2184SNeilBrown put_nfs4_file(struct nfs4_file *fi)
59313cd2184SNeilBrown {
594d47b295eSChuck Lever 	if (refcount_dec_and_test(&fi->fi_ref)) {
5953341678fSChuck Lever 		nfsd4_file_hash_remove(fi);
5968287f009SSachin Bhamare 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
5975b095e99SJeff Layton 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
5985b095e99SJeff Layton 		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
5998b671b80SJ. Bruce Fields 	}
60013cd2184SNeilBrown }
60113cd2184SNeilBrown 
602eb82dd39SJeff Layton static struct nfsd_file *
find_writeable_file_locked(struct nfs4_file * f)603de18643dSTrond Myklebust find_writeable_file_locked(struct nfs4_file *f)
604de18643dSTrond Myklebust {
605eb82dd39SJeff Layton 	struct nfsd_file *ret;
606de18643dSTrond Myklebust 
607de18643dSTrond Myklebust 	lockdep_assert_held(&f->fi_lock);
608de18643dSTrond Myklebust 
609edd2f552SJeff Layton 	ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
610de18643dSTrond Myklebust 	if (!ret)
611edd2f552SJeff Layton 		ret = nfsd_file_get(f->fi_fds[O_RDWR]);
612de18643dSTrond Myklebust 	return ret;
613de18643dSTrond Myklebust }
614de18643dSTrond Myklebust 
615eb82dd39SJeff Layton static struct nfsd_file *
find_writeable_file(struct nfs4_file * f)616de18643dSTrond Myklebust find_writeable_file(struct nfs4_file *f)
617de18643dSTrond Myklebust {
618eb82dd39SJeff Layton 	struct nfsd_file *ret;
619de18643dSTrond Myklebust 
620de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
621de18643dSTrond Myklebust 	ret = find_writeable_file_locked(f);
622de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
623de18643dSTrond Myklebust 
624de18643dSTrond Myklebust 	return ret;
625de18643dSTrond Myklebust }
626de18643dSTrond Myklebust 
627eb82dd39SJeff Layton static struct nfsd_file *
find_readable_file_locked(struct nfs4_file * f)628eb82dd39SJeff Layton find_readable_file_locked(struct nfs4_file *f)
629de18643dSTrond Myklebust {
630eb82dd39SJeff Layton 	struct nfsd_file *ret;
631de18643dSTrond Myklebust 
632de18643dSTrond Myklebust 	lockdep_assert_held(&f->fi_lock);
633de18643dSTrond Myklebust 
634edd2f552SJeff Layton 	ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
635de18643dSTrond Myklebust 	if (!ret)
636edd2f552SJeff Layton 		ret = nfsd_file_get(f->fi_fds[O_RDWR]);
637de18643dSTrond Myklebust 	return ret;
638de18643dSTrond Myklebust }
639de18643dSTrond Myklebust 
640eb82dd39SJeff Layton static struct nfsd_file *
find_readable_file(struct nfs4_file * f)641de18643dSTrond Myklebust find_readable_file(struct nfs4_file *f)
642de18643dSTrond Myklebust {
643eb82dd39SJeff Layton 	struct nfsd_file *ret;
644de18643dSTrond Myklebust 
645de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
646de18643dSTrond Myklebust 	ret = find_readable_file_locked(f);
647de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
648de18643dSTrond Myklebust 
649de18643dSTrond Myklebust 	return ret;
650de18643dSTrond Myklebust }
651de18643dSTrond Myklebust 
6521d3dd1d5SDai Ngo static struct nfsd_file *
find_rw_file(struct nfs4_file * f)6531d3dd1d5SDai Ngo find_rw_file(struct nfs4_file *f)
6541d3dd1d5SDai Ngo {
6551d3dd1d5SDai Ngo 	struct nfsd_file *ret;
6561d3dd1d5SDai Ngo 
6571d3dd1d5SDai Ngo 	spin_lock(&f->fi_lock);
6581d3dd1d5SDai Ngo 	ret = nfsd_file_get(f->fi_fds[O_RDWR]);
6591d3dd1d5SDai Ngo 	spin_unlock(&f->fi_lock);
6601d3dd1d5SDai Ngo 
6611d3dd1d5SDai Ngo 	return ret;
6621d3dd1d5SDai Ngo }
6631d3dd1d5SDai Ngo 
664eb82dd39SJeff Layton struct nfsd_file *
find_any_file(struct nfs4_file * f)665de18643dSTrond Myklebust find_any_file(struct nfs4_file *f)
666de18643dSTrond Myklebust {
667eb82dd39SJeff Layton 	struct nfsd_file *ret;
668de18643dSTrond Myklebust 
669a451b123STrond Myklebust 	if (!f)
670a451b123STrond Myklebust 		return NULL;
671de18643dSTrond Myklebust 	spin_lock(&f->fi_lock);
672edd2f552SJeff Layton 	ret = nfsd_file_get(f->fi_fds[O_RDWR]);
673de18643dSTrond Myklebust 	if (!ret) {
674edd2f552SJeff Layton 		ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
675de18643dSTrond Myklebust 		if (!ret)
676edd2f552SJeff Layton 			ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
677de18643dSTrond Myklebust 	}
678de18643dSTrond Myklebust 	spin_unlock(&f->fi_lock);
679de18643dSTrond Myklebust 	return ret;
680de18643dSTrond Myklebust }
681de18643dSTrond Myklebust 
find_any_file_locked(struct nfs4_file * f)682e0aa6510SJeff Layton static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
6839affa435SJ. Bruce Fields {
684e0aa6510SJeff Layton 	lockdep_assert_held(&f->fi_lock);
6859affa435SJ. Bruce Fields 
686e0aa6510SJeff Layton 	if (f->fi_fds[O_RDWR])
687e0aa6510SJeff Layton 		return f->fi_fds[O_RDWR];
688e0aa6510SJeff Layton 	if (f->fi_fds[O_WRONLY])
689e0aa6510SJeff Layton 		return f->fi_fds[O_WRONLY];
690e0aa6510SJeff Layton 	if (f->fi_fds[O_RDONLY])
691e0aa6510SJeff Layton 		return f->fi_fds[O_RDONLY];
692e0aa6510SJeff Layton 	return NULL;
693e0aa6510SJeff Layton }
694e0aa6510SJeff Layton 
69502a3508dSTrond Myklebust static atomic_long_t num_delegations;
696697ce9beSZhang Yanfei unsigned long max_delegations;
697ef0f3390SNeilBrown 
698ef0f3390SNeilBrown /*
699ef0f3390SNeilBrown  * Open owner state (share locks)
700ef0f3390SNeilBrown  */
701ef0f3390SNeilBrown 
70216bfdaafSJ. Bruce Fields /* hash tables for lock and open owners */
70316bfdaafSJ. Bruce Fields #define OWNER_HASH_BITS              8
70416bfdaafSJ. Bruce Fields #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
70516bfdaafSJ. Bruce Fields #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
706ef0f3390SNeilBrown 
ownerstr_hashval(struct xdr_netobj * ownername)707d4f0489fSTrond Myklebust static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
708ddc04c41SJ. Bruce Fields {
709ddc04c41SJ. Bruce Fields 	unsigned int ret;
710ddc04c41SJ. Bruce Fields 
711ddc04c41SJ. Bruce Fields 	ret = opaque_hashval(ownername->data, ownername->len);
71216bfdaafSJ. Bruce Fields 	return ret & OWNER_HASH_MASK;
713ddc04c41SJ. Bruce Fields }
714ef0f3390SNeilBrown 
715d47b295eSChuck Lever static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
71635079582SShan Wei 
717d47b295eSChuck Lever static const struct rhashtable_params nfs4_file_rhash_params = {
718d47b295eSChuck Lever 	.key_len		= sizeof_field(struct nfs4_file, fi_inode),
719d47b295eSChuck Lever 	.key_offset		= offsetof(struct nfs4_file, fi_inode),
720d47b295eSChuck Lever 	.head_offset		= offsetof(struct nfs4_file, fi_rlist),
721ca943217STrond Myklebust 
722d47b295eSChuck Lever 	/*
723d47b295eSChuck Lever 	 * Start with a single page hash table to reduce resizing churn
724d47b295eSChuck Lever 	 * on light workloads.
725d47b295eSChuck Lever 	 */
726d47b295eSChuck Lever 	.min_size		= 256,
727d47b295eSChuck Lever 	.automatic_shrinking	= true,
728d47b295eSChuck Lever };
729ef0f3390SNeilBrown 
7303d694271SDai Ngo /*
7313d694271SDai Ngo  * Check if courtesy clients have conflicting access and resolve it if possible
7323d694271SDai Ngo  *
7333d694271SDai Ngo  * access:  is op_share_access if share_access is true.
7343d694271SDai Ngo  *	    Check if access mode, op_share_access, would conflict with
7353d694271SDai Ngo  *	    the current deny mode of the file 'fp'.
7363d694271SDai Ngo  * access:  is op_share_deny if share_access is false.
7373d694271SDai Ngo  *	    Check if the deny mode, op_share_deny, would conflict with
7383d694271SDai Ngo  *	    current access of the file 'fp'.
7393d694271SDai Ngo  * stp:     skip checking this entry.
7403d694271SDai Ngo  * new_stp: normal open, not open upgrade.
7413d694271SDai Ngo  *
7423d694271SDai Ngo  * Function returns:
7433d694271SDai Ngo  *	false - access/deny mode conflict with normal client.
7443d694271SDai Ngo  *	true  - no conflict or conflict with courtesy client(s) is resolved.
7453d694271SDai Ngo  */
7463d694271SDai Ngo static bool
nfs4_resolve_deny_conflicts_locked(struct nfs4_file * fp,bool new_stp,struct nfs4_ol_stateid * stp,u32 access,bool share_access)7473d694271SDai Ngo nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
7483d694271SDai Ngo 		struct nfs4_ol_stateid *stp, u32 access, bool share_access)
7493d694271SDai Ngo {
7503d694271SDai Ngo 	struct nfs4_ol_stateid *st;
7513d694271SDai Ngo 	bool resolvable = true;
7523d694271SDai Ngo 	unsigned char bmap;
7533d694271SDai Ngo 	struct nfsd_net *nn;
7543d694271SDai Ngo 	struct nfs4_client *clp;
7553d694271SDai Ngo 
7563d694271SDai Ngo 	lockdep_assert_held(&fp->fi_lock);
7573d694271SDai Ngo 	list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
7583d694271SDai Ngo 		/* ignore lock stateid */
7593d694271SDai Ngo 		if (st->st_openstp)
7603d694271SDai Ngo 			continue;
7613d694271SDai Ngo 		if (st == stp && new_stp)
7623d694271SDai Ngo 			continue;
7633d694271SDai Ngo 		/* check file access against deny mode or vice versa */
7643d694271SDai Ngo 		bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
7653d694271SDai Ngo 		if (!(access & bmap_to_share_mode(bmap)))
7663d694271SDai Ngo 			continue;
7673d694271SDai Ngo 		clp = st->st_stid.sc_client;
7683d694271SDai Ngo 		if (try_to_expire_client(clp))
7693d694271SDai Ngo 			continue;
7703d694271SDai Ngo 		resolvable = false;
7713d694271SDai Ngo 		break;
7723d694271SDai Ngo 	}
7733d694271SDai Ngo 	if (resolvable) {
7743d694271SDai Ngo 		clp = stp->st_stid.sc_client;
7753d694271SDai Ngo 		nn = net_generic(clp->net, nfsd_net_id);
7763d694271SDai Ngo 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
7773d694271SDai Ngo 	}
7783d694271SDai Ngo 	return resolvable;
7793d694271SDai Ngo }
7803d694271SDai Ngo 
78112659651SJeff Layton static void
__nfs4_file_get_access(struct nfs4_file * fp,u32 access)78212659651SJeff Layton __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
7833477565eSJ. Bruce Fields {
7847214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
7857214e860SJeff Layton 
78612659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_WRITE)
78712659651SJeff Layton 		atomic_inc(&fp->fi_access[O_WRONLY]);
78812659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_READ)
78912659651SJeff Layton 		atomic_inc(&fp->fi_access[O_RDONLY]);
7903477565eSJ. Bruce Fields }
7913477565eSJ. Bruce Fields 
79212659651SJeff Layton static __be32
nfs4_file_get_access(struct nfs4_file * fp,u32 access)79312659651SJeff Layton nfs4_file_get_access(struct nfs4_file *fp, u32 access)
794998db52cSJ. Bruce Fields {
7957214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
7967214e860SJeff Layton 
79712659651SJeff Layton 	/* Does this access mode make sense? */
79812659651SJeff Layton 	if (access & ~NFS4_SHARE_ACCESS_BOTH)
79912659651SJeff Layton 		return nfserr_inval;
80012659651SJeff Layton 
801baeb4ff0SJeff Layton 	/* Does it conflict with a deny mode already set? */
802baeb4ff0SJeff Layton 	if ((access & fp->fi_share_deny) != 0)
803baeb4ff0SJeff Layton 		return nfserr_share_denied;
804baeb4ff0SJeff Layton 
80512659651SJeff Layton 	__nfs4_file_get_access(fp, access);
80612659651SJeff Layton 	return nfs_ok;
807998db52cSJ. Bruce Fields }
808998db52cSJ. Bruce Fields 
nfs4_file_check_deny(struct nfs4_file * fp,u32 deny)809baeb4ff0SJeff Layton static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
810baeb4ff0SJeff Layton {
811baeb4ff0SJeff Layton 	/* Common case is that there is no deny mode. */
812baeb4ff0SJeff Layton 	if (deny) {
813baeb4ff0SJeff Layton 		/* Does this deny mode make sense? */
814baeb4ff0SJeff Layton 		if (deny & ~NFS4_SHARE_DENY_BOTH)
815baeb4ff0SJeff Layton 			return nfserr_inval;
816baeb4ff0SJeff Layton 
817baeb4ff0SJeff Layton 		if ((deny & NFS4_SHARE_DENY_READ) &&
818baeb4ff0SJeff Layton 		    atomic_read(&fp->fi_access[O_RDONLY]))
819baeb4ff0SJeff Layton 			return nfserr_share_denied;
820baeb4ff0SJeff Layton 
821baeb4ff0SJeff Layton 		if ((deny & NFS4_SHARE_DENY_WRITE) &&
822baeb4ff0SJeff Layton 		    atomic_read(&fp->fi_access[O_WRONLY]))
823baeb4ff0SJeff Layton 			return nfserr_share_denied;
824baeb4ff0SJeff Layton 	}
825baeb4ff0SJeff Layton 	return nfs_ok;
826baeb4ff0SJeff Layton }
827baeb4ff0SJeff Layton 
__nfs4_file_put_access(struct nfs4_file * fp,int oflag)828998db52cSJ. Bruce Fields static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
829f9d7562fSJ. Bruce Fields {
830de18643dSTrond Myklebust 	might_lock(&fp->fi_lock);
831de18643dSTrond Myklebust 
832de18643dSTrond Myklebust 	if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
833fd4f83fdSJeff Layton 		struct nfsd_file *f1 = NULL;
834fd4f83fdSJeff Layton 		struct nfsd_file *f2 = NULL;
835de18643dSTrond Myklebust 
8366d338b51SJeff Layton 		swap(f1, fp->fi_fds[oflag]);
8370c7c3e67SJ. Bruce Fields 		if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
8386d338b51SJeff Layton 			swap(f2, fp->fi_fds[O_RDWR]);
839de18643dSTrond Myklebust 		spin_unlock(&fp->fi_lock);
840de18643dSTrond Myklebust 		if (f1)
841dcf3f809SChuck Lever 			nfsd_file_put(f1);
842de18643dSTrond Myklebust 		if (f2)
843dcf3f809SChuck Lever 			nfsd_file_put(f2);
844f9d7562fSJ. Bruce Fields 	}
845f9d7562fSJ. Bruce Fields }
846f9d7562fSJ. Bruce Fields 
nfs4_file_put_access(struct nfs4_file * fp,u32 access)84712659651SJeff Layton static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
848998db52cSJ. Bruce Fields {
84912659651SJeff Layton 	WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
85012659651SJeff Layton 
85112659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_WRITE)
852998db52cSJ. Bruce Fields 		__nfs4_file_put_access(fp, O_WRONLY);
85312659651SJeff Layton 	if (access & NFS4_SHARE_ACCESS_READ)
85412659651SJeff Layton 		__nfs4_file_put_access(fp, O_RDONLY);
855998db52cSJ. Bruce Fields }
856998db52cSJ. Bruce Fields 
8578287f009SSachin Bhamare /*
8588287f009SSachin Bhamare  * Allocate a new open/delegation state counter. This is needed for
8598287f009SSachin Bhamare  * pNFS for proper return on close semantics.
8608287f009SSachin Bhamare  *
8618287f009SSachin Bhamare  * Note that we only allocate it for pNFS-enabled exports, otherwise
8628287f009SSachin Bhamare  * all pointers to struct nfs4_clnt_odstate are always NULL.
8638287f009SSachin Bhamare  */
8648287f009SSachin Bhamare static struct nfs4_clnt_odstate *
alloc_clnt_odstate(struct nfs4_client * clp)8658287f009SSachin Bhamare alloc_clnt_odstate(struct nfs4_client *clp)
8668287f009SSachin Bhamare {
8678287f009SSachin Bhamare 	struct nfs4_clnt_odstate *co;
8688287f009SSachin Bhamare 
8698287f009SSachin Bhamare 	co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
8708287f009SSachin Bhamare 	if (co) {
8718287f009SSachin Bhamare 		co->co_client = clp;
872cff7cb2eSElena Reshetova 		refcount_set(&co->co_odcount, 1);
8738287f009SSachin Bhamare 	}
8748287f009SSachin Bhamare 	return co;
8758287f009SSachin Bhamare }
8768287f009SSachin Bhamare 
8778287f009SSachin Bhamare static void
hash_clnt_odstate_locked(struct nfs4_clnt_odstate * co)8788287f009SSachin Bhamare hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
8798287f009SSachin Bhamare {
8808287f009SSachin Bhamare 	struct nfs4_file *fp = co->co_file;
8818287f009SSachin Bhamare 
8828287f009SSachin Bhamare 	lockdep_assert_held(&fp->fi_lock);
8838287f009SSachin Bhamare 	list_add(&co->co_perfile, &fp->fi_clnt_odstate);
8848287f009SSachin Bhamare }
8858287f009SSachin Bhamare 
8868287f009SSachin Bhamare static inline void
get_clnt_odstate(struct nfs4_clnt_odstate * co)8878287f009SSachin Bhamare get_clnt_odstate(struct nfs4_clnt_odstate *co)
8888287f009SSachin Bhamare {
8898287f009SSachin Bhamare 	if (co)
890cff7cb2eSElena Reshetova 		refcount_inc(&co->co_odcount);
8918287f009SSachin Bhamare }
8928287f009SSachin Bhamare 
8938287f009SSachin Bhamare static void
put_clnt_odstate(struct nfs4_clnt_odstate * co)8948287f009SSachin Bhamare put_clnt_odstate(struct nfs4_clnt_odstate *co)
8958287f009SSachin Bhamare {
8968287f009SSachin Bhamare 	struct nfs4_file *fp;
8978287f009SSachin Bhamare 
8988287f009SSachin Bhamare 	if (!co)
8998287f009SSachin Bhamare 		return;
9008287f009SSachin Bhamare 
9018287f009SSachin Bhamare 	fp = co->co_file;
902cff7cb2eSElena Reshetova 	if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
9038287f009SSachin Bhamare 		list_del(&co->co_perfile);
9048287f009SSachin Bhamare 		spin_unlock(&fp->fi_lock);
9058287f009SSachin Bhamare 
9068287f009SSachin Bhamare 		nfsd4_return_all_file_layouts(co->co_client, fp);
9078287f009SSachin Bhamare 		kmem_cache_free(odstate_slab, co);
9088287f009SSachin Bhamare 	}
9098287f009SSachin Bhamare }
9108287f009SSachin Bhamare 
9118287f009SSachin Bhamare static struct nfs4_clnt_odstate *
find_or_hash_clnt_odstate(struct nfs4_file * fp,struct nfs4_clnt_odstate * new)9128287f009SSachin Bhamare find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
9138287f009SSachin Bhamare {
9148287f009SSachin Bhamare 	struct nfs4_clnt_odstate *co;
9158287f009SSachin Bhamare 	struct nfs4_client *cl;
9168287f009SSachin Bhamare 
9178287f009SSachin Bhamare 	if (!new)
9188287f009SSachin Bhamare 		return NULL;
9198287f009SSachin Bhamare 
9208287f009SSachin Bhamare 	cl = new->co_client;
9218287f009SSachin Bhamare 
9228287f009SSachin Bhamare 	spin_lock(&fp->fi_lock);
9238287f009SSachin Bhamare 	list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
9248287f009SSachin Bhamare 		if (co->co_client == cl) {
9258287f009SSachin Bhamare 			get_clnt_odstate(co);
9268287f009SSachin Bhamare 			goto out;
9278287f009SSachin Bhamare 		}
9288287f009SSachin Bhamare 	}
9298287f009SSachin Bhamare 	co = new;
9308287f009SSachin Bhamare 	co->co_file = fp;
9318287f009SSachin Bhamare 	hash_clnt_odstate_locked(new);
9328287f009SSachin Bhamare out:
9338287f009SSachin Bhamare 	spin_unlock(&fp->fi_lock);
9348287f009SSachin Bhamare 	return co;
9358287f009SSachin Bhamare }
9368287f009SSachin Bhamare 
nfs4_alloc_stid(struct nfs4_client * cl,struct kmem_cache * slab,void (* sc_free)(struct nfs4_stid *))937d19fb70dSKinglong Mee struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
938d19fb70dSKinglong Mee 				  void (*sc_free)(struct nfs4_stid *))
939996e0938SJ. Bruce Fields {
9403abdb607SJ. Bruce Fields 	struct nfs4_stid *stid;
9413abdb607SJ. Bruce Fields 	int new_id;
9423abdb607SJ. Bruce Fields 
943f8338834STrond Myklebust 	stid = kmem_cache_zalloc(slab, GFP_KERNEL);
9443abdb607SJ. Bruce Fields 	if (!stid)
9453abdb607SJ. Bruce Fields 		return NULL;
946996e0938SJ. Bruce Fields 
9474770d722SJeff Layton 	idr_preload(GFP_KERNEL);
9484770d722SJeff Layton 	spin_lock(&cl->cl_lock);
94978599c42SJ. Bruce Fields 	/* Reserving 0 for start of file in nfsdfs "states" file: */
95078599c42SJ. Bruce Fields 	new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
9514770d722SJeff Layton 	spin_unlock(&cl->cl_lock);
9524770d722SJeff Layton 	idr_preload_end();
953ebd6c707STejun Heo 	if (new_id < 0)
9543abdb607SJ. Bruce Fields 		goto out_free;
955d19fb70dSKinglong Mee 
956d19fb70dSKinglong Mee 	stid->sc_free = sc_free;
9573abdb607SJ. Bruce Fields 	stid->sc_client = cl;
9583abdb607SJ. Bruce Fields 	stid->sc_stateid.si_opaque.so_id = new_id;
9593abdb607SJ. Bruce Fields 	stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
9603abdb607SJ. Bruce Fields 	/* Will be incremented before return to client: */
961a15dfcd5SElena Reshetova 	refcount_set(&stid->sc_count, 1);
9629767feb2SJeff Layton 	spin_lock_init(&stid->sc_lock);
963624322f1SOlga Kornievskaia 	INIT_LIST_HEAD(&stid->sc_cp_list);
9643abdb607SJ. Bruce Fields 
965996e0938SJ. Bruce Fields 	/*
9663abdb607SJ. Bruce Fields 	 * It shouldn't be a problem to reuse an opaque stateid value.
9673abdb607SJ. Bruce Fields 	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
9683abdb607SJ. Bruce Fields 	 * example, a stray write retransmission could be accepted by
9693abdb607SJ. Bruce Fields 	 * the server when it should have been rejected.  Therefore,
9703abdb607SJ. Bruce Fields 	 * adopt a trick from the sctp code to attempt to maximize the
9713abdb607SJ. Bruce Fields 	 * amount of time until an id is reused, by ensuring they always
9723abdb607SJ. Bruce Fields 	 * "increase" (mod INT_MAX):
973996e0938SJ. Bruce Fields 	 */
9743abdb607SJ. Bruce Fields 	return stid;
9753abdb607SJ. Bruce Fields out_free:
9762c44a234SWei Yongjun 	kmem_cache_free(slab, stid);
9773abdb607SJ. Bruce Fields 	return NULL;
9782a74aba7SJ. Bruce Fields }
9792a74aba7SJ. Bruce Fields 
980e0639dc5SOlga Kornievskaia /*
981e0639dc5SOlga Kornievskaia  * Create a unique stateid_t to represent each COPY.
982e0639dc5SOlga Kornievskaia  */
nfs4_init_cp_state(struct nfsd_net * nn,copy_stateid_t * stid,unsigned char cs_type)983624322f1SOlga Kornievskaia static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
984781fde1aSChuck Lever 			      unsigned char cs_type)
985e0639dc5SOlga Kornievskaia {
986e0639dc5SOlga Kornievskaia 	int new_id;
987e0639dc5SOlga Kornievskaia 
988781fde1aSChuck Lever 	stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
989781fde1aSChuck Lever 	stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
990624322f1SOlga Kornievskaia 
991e0639dc5SOlga Kornievskaia 	idr_preload(GFP_KERNEL);
992e0639dc5SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
993624322f1SOlga Kornievskaia 	new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
994781fde1aSChuck Lever 	stid->cs_stid.si_opaque.so_id = new_id;
995781fde1aSChuck Lever 	stid->cs_stid.si_generation = 1;
996e0639dc5SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
997e0639dc5SOlga Kornievskaia 	idr_preload_end();
998e0639dc5SOlga Kornievskaia 	if (new_id < 0)
999e0639dc5SOlga Kornievskaia 		return 0;
100081e72297SDai Ngo 	stid->cs_type = cs_type;
1001e0639dc5SOlga Kornievskaia 	return 1;
1002e0639dc5SOlga Kornievskaia }
1003e0639dc5SOlga Kornievskaia 
nfs4_init_copy_state(struct nfsd_net * nn,struct nfsd4_copy * copy)1004624322f1SOlga Kornievskaia int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
1005624322f1SOlga Kornievskaia {
1006624322f1SOlga Kornievskaia 	return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
1007624322f1SOlga Kornievskaia }
1008624322f1SOlga Kornievskaia 
nfs4_alloc_init_cpntf_state(struct nfsd_net * nn,struct nfs4_stid * p_stid)1009624322f1SOlga Kornievskaia struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
1010624322f1SOlga Kornievskaia 						     struct nfs4_stid *p_stid)
1011624322f1SOlga Kornievskaia {
1012624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
1013624322f1SOlga Kornievskaia 
1014624322f1SOlga Kornievskaia 	cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
1015624322f1SOlga Kornievskaia 	if (!cps)
1016624322f1SOlga Kornievskaia 		return NULL;
101720b7d86fSArnd Bergmann 	cps->cpntf_time = ktime_get_boottime_seconds();
1018781fde1aSChuck Lever 	refcount_set(&cps->cp_stateid.cs_count, 1);
1019624322f1SOlga Kornievskaia 	if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
1020624322f1SOlga Kornievskaia 		goto out_free;
1021624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
1022624322f1SOlga Kornievskaia 	list_add(&cps->cp_list, &p_stid->sc_cp_list);
1023624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
1024624322f1SOlga Kornievskaia 	return cps;
1025624322f1SOlga Kornievskaia out_free:
1026624322f1SOlga Kornievskaia 	kfree(cps);
1027624322f1SOlga Kornievskaia 	return NULL;
1028624322f1SOlga Kornievskaia }
1029624322f1SOlga Kornievskaia 
nfs4_free_copy_state(struct nfsd4_copy * copy)1030624322f1SOlga Kornievskaia void nfs4_free_copy_state(struct nfsd4_copy *copy)
1031e0639dc5SOlga Kornievskaia {
1032e0639dc5SOlga Kornievskaia 	struct nfsd_net *nn;
1033e0639dc5SOlga Kornievskaia 
103481e72297SDai Ngo 	if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
103581e72297SDai Ngo 		return;
1036e0639dc5SOlga Kornievskaia 	nn = net_generic(copy->cp_clp->net, nfsd_net_id);
1037e0639dc5SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
1038624322f1SOlga Kornievskaia 	idr_remove(&nn->s2s_cp_stateids,
1039781fde1aSChuck Lever 		   copy->cp_stateid.cs_stid.si_opaque.so_id);
1040624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
1041624322f1SOlga Kornievskaia }
1042624322f1SOlga Kornievskaia 
nfs4_free_cpntf_statelist(struct net * net,struct nfs4_stid * stid)1043624322f1SOlga Kornievskaia static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
1044624322f1SOlga Kornievskaia {
1045624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
1046624322f1SOlga Kornievskaia 	struct nfsd_net *nn;
1047624322f1SOlga Kornievskaia 
1048624322f1SOlga Kornievskaia 	nn = net_generic(net, nfsd_net_id);
1049624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
1050624322f1SOlga Kornievskaia 	while (!list_empty(&stid->sc_cp_list)) {
1051624322f1SOlga Kornievskaia 		cps = list_first_entry(&stid->sc_cp_list,
1052624322f1SOlga Kornievskaia 				       struct nfs4_cpntf_state, cp_list);
1053624322f1SOlga Kornievskaia 		_free_cpntf_state_locked(nn, cps);
1054624322f1SOlga Kornievskaia 	}
1055e0639dc5SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
1056e0639dc5SOlga Kornievskaia }
1057e0639dc5SOlga Kornievskaia 
nfs4_alloc_open_stateid(struct nfs4_client * clp)1058b49e084dSJeff Layton static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
10594cdc951bSJ. Bruce Fields {
10606011695dSTrond Myklebust 	struct nfs4_stid *stid;
10616011695dSTrond Myklebust 
1062d19fb70dSKinglong Mee 	stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
10636011695dSTrond Myklebust 	if (!stid)
10646011695dSTrond Myklebust 		return NULL;
10656011695dSTrond Myklebust 
1066d19fb70dSKinglong Mee 	return openlockstateid(stid);
10676011695dSTrond Myklebust }
10686011695dSTrond Myklebust 
nfs4_free_deleg(struct nfs4_stid * stid)10696011695dSTrond Myklebust static void nfs4_free_deleg(struct nfs4_stid *stid)
10706011695dSTrond Myklebust {
1071895ddf5eSJeff Layton 	struct nfs4_delegation *dp = delegstateid(stid);
1072895ddf5eSJeff Layton 
1073895ddf5eSJeff Layton 	WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
1074895ddf5eSJeff Layton 	WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
1075895ddf5eSJeff Layton 	WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
1076895ddf5eSJeff Layton 	WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
10776011695dSTrond Myklebust 	kmem_cache_free(deleg_slab, stid);
10786011695dSTrond Myklebust 	atomic_long_dec(&num_delegations);
10794cdc951bSJ. Bruce Fields }
10804cdc951bSJ. Bruce Fields 
10816282cd56SNeilBrown /*
10826282cd56SNeilBrown  * When we recall a delegation, we should be careful not to hand it
10836282cd56SNeilBrown  * out again straight away.
10846282cd56SNeilBrown  * To ensure this we keep a pair of bloom filters ('new' and 'old')
10856282cd56SNeilBrown  * in which the filehandles of recalled delegations are "stored".
10866282cd56SNeilBrown  * If a filehandle appear in either filter, a delegation is blocked.
10876282cd56SNeilBrown  * When a delegation is recalled, the filehandle is stored in the "new"
10886282cd56SNeilBrown  * filter.
10896282cd56SNeilBrown  * Every 30 seconds we swap the filters and clear the "new" one,
10906282cd56SNeilBrown  * unless both are empty of course.
10916282cd56SNeilBrown  *
10926282cd56SNeilBrown  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
10936282cd56SNeilBrown  * low 3 bytes as hash-table indices.
10946282cd56SNeilBrown  *
1095f54fe962SJeff Layton  * 'blocked_delegations_lock', which is always taken in block_delegations(),
10966282cd56SNeilBrown  * is used to manage concurrent access.  Testing does not need the lock
10976282cd56SNeilBrown  * except when swapping the two filters.
10986282cd56SNeilBrown  */
1099f54fe962SJeff Layton static DEFINE_SPINLOCK(blocked_delegations_lock);
11006282cd56SNeilBrown static struct bloom_pair {
11016282cd56SNeilBrown 	int	entries, old_entries;
1102b3f255efSArnd Bergmann 	time64_t swap_time;
11036282cd56SNeilBrown 	int	new; /* index into 'set' */
11046282cd56SNeilBrown 	DECLARE_BITMAP(set[2], 256);
11056282cd56SNeilBrown } blocked_delegations;
11066282cd56SNeilBrown 
delegation_blocked(struct knfsd_fh * fh)11076282cd56SNeilBrown static int delegation_blocked(struct knfsd_fh *fh)
11086282cd56SNeilBrown {
11096282cd56SNeilBrown 	u32 hash;
11106282cd56SNeilBrown 	struct bloom_pair *bd = &blocked_delegations;
11116282cd56SNeilBrown 
11126282cd56SNeilBrown 	if (bd->entries == 0)
11136282cd56SNeilBrown 		return 0;
1114b3f255efSArnd Bergmann 	if (ktime_get_seconds() - bd->swap_time > 30) {
1115f54fe962SJeff Layton 		spin_lock(&blocked_delegations_lock);
1116b3f255efSArnd Bergmann 		if (ktime_get_seconds() - bd->swap_time > 30) {
11176282cd56SNeilBrown 			bd->entries -= bd->old_entries;
11186282cd56SNeilBrown 			bd->old_entries = bd->entries;
11196282cd56SNeilBrown 			memset(bd->set[bd->new], 0,
11206282cd56SNeilBrown 			       sizeof(bd->set[0]));
11216282cd56SNeilBrown 			bd->new = 1-bd->new;
1122b3f255efSArnd Bergmann 			bd->swap_time = ktime_get_seconds();
11236282cd56SNeilBrown 		}
1124f54fe962SJeff Layton 		spin_unlock(&blocked_delegations_lock);
11256282cd56SNeilBrown 	}
1126d8b26071SNeilBrown 	hash = jhash(&fh->fh_raw, fh->fh_size, 0);
11276282cd56SNeilBrown 	if (test_bit(hash&255, bd->set[0]) &&
11286282cd56SNeilBrown 	    test_bit((hash>>8)&255, bd->set[0]) &&
11296282cd56SNeilBrown 	    test_bit((hash>>16)&255, bd->set[0]))
11306282cd56SNeilBrown 		return 1;
11316282cd56SNeilBrown 
11326282cd56SNeilBrown 	if (test_bit(hash&255, bd->set[1]) &&
11336282cd56SNeilBrown 	    test_bit((hash>>8)&255, bd->set[1]) &&
11346282cd56SNeilBrown 	    test_bit((hash>>16)&255, bd->set[1]))
11356282cd56SNeilBrown 		return 1;
11366282cd56SNeilBrown 
11376282cd56SNeilBrown 	return 0;
11386282cd56SNeilBrown }
11396282cd56SNeilBrown 
block_delegations(struct knfsd_fh * fh)11406282cd56SNeilBrown static void block_delegations(struct knfsd_fh *fh)
11416282cd56SNeilBrown {
11426282cd56SNeilBrown 	u32 hash;
11436282cd56SNeilBrown 	struct bloom_pair *bd = &blocked_delegations;
11446282cd56SNeilBrown 
1145d8b26071SNeilBrown 	hash = jhash(&fh->fh_raw, fh->fh_size, 0);
11466282cd56SNeilBrown 
1147f54fe962SJeff Layton 	spin_lock(&blocked_delegations_lock);
11486282cd56SNeilBrown 	__set_bit(hash&255, bd->set[bd->new]);
11496282cd56SNeilBrown 	__set_bit((hash>>8)&255, bd->set[bd->new]);
11506282cd56SNeilBrown 	__set_bit((hash>>16)&255, bd->set[bd->new]);
11516282cd56SNeilBrown 	if (bd->entries == 0)
1152b3f255efSArnd Bergmann 		bd->swap_time = ktime_get_seconds();
11536282cd56SNeilBrown 	bd->entries += 1;
1154f54fe962SJeff Layton 	spin_unlock(&blocked_delegations_lock);
11556282cd56SNeilBrown }
11566282cd56SNeilBrown 
11571da177e4SLinus Torvalds static struct nfs4_delegation *
alloc_init_deleg(struct nfs4_client * clp,struct nfs4_file * fp,struct nfs4_clnt_odstate * odstate,u32 dl_type)115886d29b10SJ. Bruce Fields alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
11591d3dd1d5SDai Ngo 		 struct nfs4_clnt_odstate *odstate, u32 dl_type)
11601da177e4SLinus Torvalds {
11611da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
116202a3508dSTrond Myklebust 	long n;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	dprintk("NFSD alloc_init_deleg\n");
116502a3508dSTrond Myklebust 	n = atomic_long_inc_return(&num_delegations);
116602a3508dSTrond Myklebust 	if (n < 0 || n > max_delegations)
116702a3508dSTrond Myklebust 		goto out_dec;
1168bbf936edSJeff Layton 	if (delegation_blocked(&fp->fi_fhandle))
116902a3508dSTrond Myklebust 		goto out_dec;
1170d19fb70dSKinglong Mee 	dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
11715b2d21c1SNeilBrown 	if (dp == NULL)
117202a3508dSTrond Myklebust 		goto out_dec;
11736011695dSTrond Myklebust 
11742a74aba7SJ. Bruce Fields 	/*
11752a74aba7SJ. Bruce Fields 	 * delegation seqid's are never incremented.  The 4.1 special
11766136d2b4SJ. Bruce Fields 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
11776136d2b4SJ. Bruce Fields 	 * 0 anyway just for consistency and use 1:
11782a74aba7SJ. Bruce Fields 	 */
11792a74aba7SJ. Bruce Fields 	dp->dl_stid.sc_stateid.si_generation = 1;
1180ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perfile);
1181ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perclnt);
11821da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dp->dl_recall_lru);
11838287f009SSachin Bhamare 	dp->dl_clnt_odstate = odstate;
11848287f009SSachin Bhamare 	get_clnt_odstate(odstate);
11851d3dd1d5SDai Ngo 	dp->dl_type = dl_type;
1186f0b5de1bSChristoph Hellwig 	dp->dl_retries = 1;
118766af2579SDai Ngo 	dp->dl_recalled = false;
1188f0b5de1bSChristoph Hellwig 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
11890162ac2bSChristoph Hellwig 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
119086d29b10SJ. Bruce Fields 	get_nfs4_file(fp);
119186d29b10SJ. Bruce Fields 	dp->dl_stid.sc_file = fp;
11921da177e4SLinus Torvalds 	return dp;
119302a3508dSTrond Myklebust out_dec:
119402a3508dSTrond Myklebust 	atomic_long_dec(&num_delegations);
119502a3508dSTrond Myklebust 	return NULL;
11961da177e4SLinus Torvalds }
11971da177e4SLinus Torvalds 
11981da177e4SLinus Torvalds void
nfs4_put_stid(struct nfs4_stid * s)11996011695dSTrond Myklebust nfs4_put_stid(struct nfs4_stid *s)
12001da177e4SLinus Torvalds {
120111b9164aSTrond Myklebust 	struct nfs4_file *fp = s->sc_file;
12026011695dSTrond Myklebust 	struct nfs4_client *clp = s->sc_client;
12036011695dSTrond Myklebust 
12044770d722SJeff Layton 	might_lock(&clp->cl_lock);
12054770d722SJeff Layton 
1206a15dfcd5SElena Reshetova 	if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
1207b401be22SJeff Layton 		wake_up_all(&close_wq);
12086011695dSTrond Myklebust 		return;
1209b401be22SJeff Layton 	}
12106011695dSTrond Myklebust 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1211624322f1SOlga Kornievskaia 	nfs4_free_cpntf_statelist(clp->net, s);
12124770d722SJeff Layton 	spin_unlock(&clp->cl_lock);
12136011695dSTrond Myklebust 	s->sc_free(s);
121411b9164aSTrond Myklebust 	if (fp)
121511b9164aSTrond Myklebust 		put_nfs4_file(fp);
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12189767feb2SJeff Layton void
nfs4_inc_and_copy_stateid(stateid_t * dst,struct nfs4_stid * stid)12199767feb2SJeff Layton nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
12209767feb2SJeff Layton {
12219767feb2SJeff Layton 	stateid_t *src = &stid->sc_stateid;
12229767feb2SJeff Layton 
12239767feb2SJeff Layton 	spin_lock(&stid->sc_lock);
12249767feb2SJeff Layton 	if (unlikely(++src->si_generation == 0))
12259767feb2SJeff Layton 		src->si_generation = 1;
12269767feb2SJeff Layton 	memcpy(dst, src, sizeof(*dst));
12279767feb2SJeff Layton 	spin_unlock(&stid->sc_lock);
12289767feb2SJeff Layton }
12299767feb2SJeff Layton 
put_deleg_file(struct nfs4_file * fp)1230353601e7SJ. Bruce Fields static void put_deleg_file(struct nfs4_file *fp)
12311da177e4SLinus Torvalds {
1232eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
1233353601e7SJ. Bruce Fields 
1234353601e7SJ. Bruce Fields 	spin_lock(&fp->fi_lock);
1235353601e7SJ. Bruce Fields 	if (--fp->fi_delegees == 0)
1236eb82dd39SJeff Layton 		swap(nf, fp->fi_deleg_file);
1237353601e7SJ. Bruce Fields 	spin_unlock(&fp->fi_lock);
1238353601e7SJ. Bruce Fields 
1239eb82dd39SJeff Layton 	if (nf)
1240eb82dd39SJeff Layton 		nfsd_file_put(nf);
1241353601e7SJ. Bruce Fields }
1242353601e7SJ. Bruce Fields 
nfs4_unlock_deleg_lease(struct nfs4_delegation * dp)1243353601e7SJ. Bruce Fields static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1244353601e7SJ. Bruce Fields {
1245cba7b3d1SJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1246eb82dd39SJeff Layton 	struct nfsd_file *nf = fp->fi_deleg_file;
1247417c6629SJeff Layton 
1248b8232d33SJ. Bruce Fields 	WARN_ON_ONCE(!fp->fi_delegees);
1249b8232d33SJ. Bruce Fields 
1250eb82dd39SJeff Layton 	vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1251353601e7SJ. Bruce Fields 	put_deleg_file(fp);
12521da177e4SLinus Torvalds }
12531da177e4SLinus Torvalds 
destroy_unhashed_deleg(struct nfs4_delegation * dp)12540af6e690SJ. Bruce Fields static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
12550af6e690SJ. Bruce Fields {
12560af6e690SJ. Bruce Fields 	put_clnt_odstate(dp->dl_clnt_odstate);
1257353601e7SJ. Bruce Fields 	nfs4_unlock_deleg_lease(dp);
12580af6e690SJ. Bruce Fields 	nfs4_put_stid(&dp->dl_stid);
12590af6e690SJ. Bruce Fields }
12600af6e690SJ. Bruce Fields 
nfs4_unhash_stid(struct nfs4_stid * s)1261cd61c522SChristoph Hellwig void nfs4_unhash_stid(struct nfs4_stid *s)
12626136d2b4SJ. Bruce Fields {
12633abdb607SJ. Bruce Fields 	s->sc_type = 0;
12646136d2b4SJ. Bruce Fields }
12656136d2b4SJ. Bruce Fields 
126634ed9872SAndrew Elble /**
126768b18f52SJ. Bruce Fields  * nfs4_delegation_exists - Discover if this delegation already exists
126834ed9872SAndrew Elble  * @clp:     a pointer to the nfs4_client we're granting a delegation to
126934ed9872SAndrew Elble  * @fp:      a pointer to the nfs4_file we're granting a delegation on
127034ed9872SAndrew Elble  *
127134ed9872SAndrew Elble  * Return:
127268b18f52SJ. Bruce Fields  *      On success: true iff an existing delegation is found
127334ed9872SAndrew Elble  */
127434ed9872SAndrew Elble 
127568b18f52SJ. Bruce Fields static bool
nfs4_delegation_exists(struct nfs4_client * clp,struct nfs4_file * fp)127668b18f52SJ. Bruce Fields nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1277931ee56cSBenny Halevy {
127834ed9872SAndrew Elble 	struct nfs4_delegation *searchdp = NULL;
127934ed9872SAndrew Elble 	struct nfs4_client *searchclp = NULL;
128034ed9872SAndrew Elble 
1281cdc97505SBenny Halevy 	lockdep_assert_held(&state_lock);
1282417c6629SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
1283931ee56cSBenny Halevy 
128434ed9872SAndrew Elble 	list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
128534ed9872SAndrew Elble 		searchclp = searchdp->dl_stid.sc_client;
128634ed9872SAndrew Elble 		if (clp == searchclp) {
128751d87bc2SFengguang Wu 			return true;
128834ed9872SAndrew Elble 		}
128934ed9872SAndrew Elble 	}
129051d87bc2SFengguang Wu 	return false;
129134ed9872SAndrew Elble }
129234ed9872SAndrew Elble 
129334ed9872SAndrew Elble /**
129434ed9872SAndrew Elble  * hash_delegation_locked - Add a delegation to the appropriate lists
129534ed9872SAndrew Elble  * @dp:     a pointer to the nfs4_delegation we are adding.
129634ed9872SAndrew Elble  * @fp:     a pointer to the nfs4_file we're granting a delegation on
129734ed9872SAndrew Elble  *
129834ed9872SAndrew Elble  * Return:
129934ed9872SAndrew Elble  *      On success: NULL if the delegation was successfully hashed.
130034ed9872SAndrew Elble  *
130134ed9872SAndrew Elble  *      On error: -EAGAIN if one was previously granted to this
130234ed9872SAndrew Elble  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
130334ed9872SAndrew Elble  *
130434ed9872SAndrew Elble  */
130534ed9872SAndrew Elble 
130634ed9872SAndrew Elble static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)130734ed9872SAndrew Elble hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
130834ed9872SAndrew Elble {
130934ed9872SAndrew Elble 	struct nfs4_client *clp = dp->dl_stid.sc_client;
131034ed9872SAndrew Elble 
131134ed9872SAndrew Elble 	lockdep_assert_held(&state_lock);
131234ed9872SAndrew Elble 	lockdep_assert_held(&fp->fi_lock);
131334ed9872SAndrew Elble 
131468b18f52SJ. Bruce Fields 	if (nfs4_delegation_exists(clp, fp))
131568b18f52SJ. Bruce Fields 		return -EAGAIN;
1316a15dfcd5SElena Reshetova 	refcount_inc(&dp->dl_stid.sc_count);
13173fb87d13SBenny Halevy 	dp->dl_stid.sc_type = NFS4_DELEG_STID;
1318931ee56cSBenny Halevy 	list_add(&dp->dl_perfile, &fp->fi_delegations);
131934ed9872SAndrew Elble 	list_add(&dp->dl_perclnt, &clp->cl_delegations);
132034ed9872SAndrew Elble 	return 0;
1321931ee56cSBenny Halevy }
1322931ee56cSBenny Halevy 
delegation_hashed(struct nfs4_delegation * dp)1323548ec080SJ. Bruce Fields static bool delegation_hashed(struct nfs4_delegation *dp)
1324548ec080SJ. Bruce Fields {
1325548ec080SJ. Bruce Fields 	return !(list_empty(&dp->dl_perfile));
1326548ec080SJ. Bruce Fields }
1327548ec080SJ. Bruce Fields 
13283fcbbd24SJeff Layton static bool
unhash_delegation_locked(struct nfs4_delegation * dp)132942690676SJeff Layton unhash_delegation_locked(struct nfs4_delegation *dp)
13301da177e4SLinus Torvalds {
133111b9164aSTrond Myklebust 	struct nfs4_file *fp = dp->dl_stid.sc_file;
133202e1215fSJeff Layton 
133342690676SJeff Layton 	lockdep_assert_held(&state_lock);
133442690676SJeff Layton 
1335548ec080SJ. Bruce Fields 	if (!delegation_hashed(dp))
13363fcbbd24SJeff Layton 		return false;
13373fcbbd24SJeff Layton 
1338b0fc29d6STrond Myklebust 	dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1339d55a166cSJeff Layton 	/* Ensure that deleg break won't try to requeue it */
1340d55a166cSJeff Layton 	++dp->dl_time;
1341417c6629SJeff Layton 	spin_lock(&fp->fi_lock);
1342931ee56cSBenny Halevy 	list_del_init(&dp->dl_perclnt);
13431da177e4SLinus Torvalds 	list_del_init(&dp->dl_recall_lru);
134402e1215fSJeff Layton 	list_del_init(&dp->dl_perfile);
134502e1215fSJeff Layton 	spin_unlock(&fp->fi_lock);
13463fcbbd24SJeff Layton 	return true;
1347cbf7a75bSJ. Bruce Fields }
13483bd64a5bSJ. Bruce Fields 
destroy_delegation(struct nfs4_delegation * dp)13493bd64a5bSJ. Bruce Fields static void destroy_delegation(struct nfs4_delegation *dp)
13503bd64a5bSJ. Bruce Fields {
13513fcbbd24SJeff Layton 	bool unhashed;
13523fcbbd24SJeff Layton 
135342690676SJeff Layton 	spin_lock(&state_lock);
13543fcbbd24SJeff Layton 	unhashed = unhash_delegation_locked(dp);
135542690676SJeff Layton 	spin_unlock(&state_lock);
13560af6e690SJ. Bruce Fields 	if (unhashed)
13570af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
13583fcbbd24SJeff Layton }
13593bd64a5bSJ. Bruce Fields 
revoke_delegation(struct nfs4_delegation * dp)13603bd64a5bSJ. Bruce Fields static void revoke_delegation(struct nfs4_delegation *dp)
13613bd64a5bSJ. Bruce Fields {
13623bd64a5bSJ. Bruce Fields 	struct nfs4_client *clp = dp->dl_stid.sc_client;
13633bd64a5bSJ. Bruce Fields 
13642d4a532dSJeff Layton 	WARN_ON(!list_empty(&dp->dl_recall_lru));
13652d4a532dSJeff Layton 
1366a1c74569SChuck Lever 	trace_nfsd_stid_revoke(&dp->dl_stid);
1367a1c74569SChuck Lever 
13680af6e690SJ. Bruce Fields 	if (clp->cl_minorversion) {
13693b816601SBenjamin Coddington 		spin_lock(&clp->cl_lock);
13703bd64a5bSJ. Bruce Fields 		dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
13710af6e690SJ. Bruce Fields 		refcount_inc(&dp->dl_stid.sc_count);
13722d4a532dSJeff Layton 		list_add(&dp->dl_recall_lru, &clp->cl_revoked);
13732d4a532dSJeff Layton 		spin_unlock(&clp->cl_lock);
13743bd64a5bSJ. Bruce Fields 	}
13750af6e690SJ. Bruce Fields 	destroy_unhashed_deleg(dp);
13763bd64a5bSJ. Bruce Fields }
13773bd64a5bSJ. Bruce Fields 
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds  * SETCLIENTID state
13801da177e4SLinus Torvalds  */
13811da177e4SLinus Torvalds 
clientid_hashval(u32 id)1382ddc04c41SJ. Bruce Fields static unsigned int clientid_hashval(u32 id)
1383ddc04c41SJ. Bruce Fields {
1384ddc04c41SJ. Bruce Fields 	return id & CLIENT_HASH_MASK;
1385ddc04c41SJ. Bruce Fields }
1386ddc04c41SJ. Bruce Fields 
clientstr_hashval(struct xdr_netobj name)13876b189105SScott Mayhew static unsigned int clientstr_hashval(struct xdr_netobj name)
1388ddc04c41SJ. Bruce Fields {
13896b189105SScott Mayhew 	return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1390ddc04c41SJ. Bruce Fields }
1391ddc04c41SJ. Bruce Fields 
13921da177e4SLinus Torvalds /*
1393baeb4ff0SJeff Layton  * A stateid that had a deny mode associated with it is being released
1394baeb4ff0SJeff Layton  * or downgraded. Recalculate the deny mode on the file.
1395baeb4ff0SJeff Layton  */
1396baeb4ff0SJeff Layton static void
recalculate_deny_mode(struct nfs4_file * fp)1397baeb4ff0SJeff Layton recalculate_deny_mode(struct nfs4_file *fp)
1398baeb4ff0SJeff Layton {
1399baeb4ff0SJeff Layton 	struct nfs4_ol_stateid *stp;
1400baeb4ff0SJeff Layton 
1401baeb4ff0SJeff Layton 	spin_lock(&fp->fi_lock);
1402baeb4ff0SJeff Layton 	fp->fi_share_deny = 0;
1403baeb4ff0SJeff Layton 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1404baeb4ff0SJeff Layton 		fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1405baeb4ff0SJeff Layton 	spin_unlock(&fp->fi_lock);
1406baeb4ff0SJeff Layton }
1407baeb4ff0SJeff Layton 
1408baeb4ff0SJeff Layton static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1409baeb4ff0SJeff Layton reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1410baeb4ff0SJeff Layton {
1411baeb4ff0SJeff Layton 	int i;
1412baeb4ff0SJeff Layton 	bool change = false;
1413baeb4ff0SJeff Layton 
1414baeb4ff0SJeff Layton 	for (i = 1; i < 4; i++) {
1415baeb4ff0SJeff Layton 		if ((i & deny) != i) {
1416baeb4ff0SJeff Layton 			change = true;
1417baeb4ff0SJeff Layton 			clear_deny(i, stp);
1418baeb4ff0SJeff Layton 		}
1419baeb4ff0SJeff Layton 	}
1420baeb4ff0SJeff Layton 
1421baeb4ff0SJeff Layton 	/* Recalculate per-file deny mode if there was a change */
1422baeb4ff0SJeff Layton 	if (change)
142311b9164aSTrond Myklebust 		recalculate_deny_mode(stp->st_stid.sc_file);
1424baeb4ff0SJeff Layton }
1425baeb4ff0SJeff Layton 
142682c5ff1bSJeff Layton /* release all access and file references for a given stateid */
142782c5ff1bSJeff Layton static void
release_all_access(struct nfs4_ol_stateid * stp)142882c5ff1bSJeff Layton release_all_access(struct nfs4_ol_stateid *stp)
142982c5ff1bSJeff Layton {
143082c5ff1bSJeff Layton 	int i;
143111b9164aSTrond Myklebust 	struct nfs4_file *fp = stp->st_stid.sc_file;
1432baeb4ff0SJeff Layton 
1433baeb4ff0SJeff Layton 	if (fp && stp->st_deny_bmap != 0)
1434baeb4ff0SJeff Layton 		recalculate_deny_mode(fp);
143582c5ff1bSJeff Layton 
143682c5ff1bSJeff Layton 	for (i = 1; i < 4; i++) {
143782c5ff1bSJeff Layton 		if (test_access(i, stp))
143811b9164aSTrond Myklebust 			nfs4_file_put_access(stp->st_stid.sc_file, i);
143982c5ff1bSJeff Layton 		clear_access(i, stp);
144082c5ff1bSJeff Layton 	}
144182c5ff1bSJeff Layton }
144282c5ff1bSJeff Layton 
nfs4_free_stateowner(struct nfs4_stateowner * sop)1443d50ffdedSKinglong Mee static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1444d50ffdedSKinglong Mee {
1445d50ffdedSKinglong Mee 	kfree(sop->so_owner.data);
1446d50ffdedSKinglong Mee 	sop->so_ops->so_free(sop);
1447d50ffdedSKinglong Mee }
1448d50ffdedSKinglong Mee 
nfs4_put_stateowner(struct nfs4_stateowner * sop)14496b180f0bSJeff Layton static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
14506b180f0bSJeff Layton {
1451a819ecc1SJeff Layton 	struct nfs4_client *clp = sop->so_client;
1452a819ecc1SJeff Layton 
1453a819ecc1SJeff Layton 	might_lock(&clp->cl_lock);
1454a819ecc1SJeff Layton 
1455a819ecc1SJeff Layton 	if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
14566b180f0bSJeff Layton 		return;
14578f4b54c5SJeff Layton 	sop->so_ops->so_unhash(sop);
1458a819ecc1SJeff Layton 	spin_unlock(&clp->cl_lock);
1459d50ffdedSKinglong Mee 	nfs4_free_stateowner(sop);
14606b180f0bSJeff Layton }
14616b180f0bSJeff Layton 
1462a451b123STrond Myklebust static bool
nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid * stp)1463a451b123STrond Myklebust nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1464a451b123STrond Myklebust {
1465a451b123STrond Myklebust 	return list_empty(&stp->st_perfile);
1466a451b123STrond Myklebust }
1467a451b123STrond Myklebust 
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1468e8568739SJeff Layton static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1469529d7b2aSJ. Bruce Fields {
147011b9164aSTrond Myklebust 	struct nfs4_file *fp = stp->st_stid.sc_file;
14711d31a253STrond Myklebust 
14721c755dc1SJeff Layton 	lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
14731c755dc1SJeff Layton 
1474e8568739SJeff Layton 	if (list_empty(&stp->st_perfile))
1475e8568739SJeff Layton 		return false;
1476e8568739SJeff Layton 
14771d31a253STrond Myklebust 	spin_lock(&fp->fi_lock);
1478e8568739SJeff Layton 	list_del_init(&stp->st_perfile);
14791d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
1480529d7b2aSJ. Bruce Fields 	list_del(&stp->st_perstateowner);
1481e8568739SJeff Layton 	return true;
1482529d7b2aSJ. Bruce Fields }
1483529d7b2aSJ. Bruce Fields 
nfs4_free_ol_stateid(struct nfs4_stid * stid)14846011695dSTrond Myklebust static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1485529d7b2aSJ. Bruce Fields {
14866011695dSTrond Myklebust 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
14874665e2baSJ. Bruce Fields 
14888287f009SSachin Bhamare 	put_clnt_odstate(stp->st_clnt_odstate);
14896011695dSTrond Myklebust 	release_all_access(stp);
1490d3134b10SJeff Layton 	if (stp->st_stateowner)
1491d3134b10SJeff Layton 		nfs4_put_stateowner(stp->st_stateowner);
1492019805feSDai Ngo 	WARN_ON(!list_empty(&stid->sc_cp_list));
14936011695dSTrond Myklebust 	kmem_cache_free(stateid_slab, stid);
1494529d7b2aSJ. Bruce Fields }
1495529d7b2aSJ. Bruce Fields 
nfs4_free_lock_stateid(struct nfs4_stid * stid)1496b49e084dSJeff Layton static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1497529d7b2aSJ. Bruce Fields {
1498b49e084dSJeff Layton 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1499b49e084dSJeff Layton 	struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1500eb82dd39SJeff Layton 	struct nfsd_file *nf;
1501529d7b2aSJ. Bruce Fields 
1502eb82dd39SJeff Layton 	nf = find_any_file(stp->st_stid.sc_file);
1503eb82dd39SJeff Layton 	if (nf) {
1504eb82dd39SJeff Layton 		get_file(nf->nf_file);
1505eb82dd39SJeff Layton 		filp_close(nf->nf_file, (fl_owner_t)lo);
1506eb82dd39SJeff Layton 		nfsd_file_put(nf);
1507eb82dd39SJeff Layton 	}
1508b49e084dSJeff Layton 	nfs4_free_ol_stateid(stid);
1509b49e084dSJeff Layton }
1510b49e084dSJeff Layton 
15112c41beb0SJeff Layton /*
15122c41beb0SJeff Layton  * Put the persistent reference to an already unhashed generic stateid, while
15132c41beb0SJeff Layton  * holding the cl_lock. If it's the last reference, then put it onto the
15142c41beb0SJeff Layton  * reaplist for later destruction.
15152c41beb0SJeff Layton  */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)15162c41beb0SJeff Layton static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
15172c41beb0SJeff Layton 				       struct list_head *reaplist)
15182c41beb0SJeff Layton {
15192c41beb0SJeff Layton 	struct nfs4_stid *s = &stp->st_stid;
15202c41beb0SJeff Layton 	struct nfs4_client *clp = s->sc_client;
15212c41beb0SJeff Layton 
15222c41beb0SJeff Layton 	lockdep_assert_held(&clp->cl_lock);
15232c41beb0SJeff Layton 
15242c41beb0SJeff Layton 	WARN_ON_ONCE(!list_empty(&stp->st_locks));
15252c41beb0SJeff Layton 
1526a15dfcd5SElena Reshetova 	if (!refcount_dec_and_test(&s->sc_count)) {
15272c41beb0SJeff Layton 		wake_up_all(&close_wq);
15282c41beb0SJeff Layton 		return;
15292c41beb0SJeff Layton 	}
15302c41beb0SJeff Layton 
15312c41beb0SJeff Layton 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
15322c41beb0SJeff Layton 	list_add(&stp->st_locks, reaplist);
15332c41beb0SJeff Layton }
15342c41beb0SJeff Layton 
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1535e8568739SJeff Layton static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
15363c1c995cSJeff Layton {
1537f46c445bSChuck Lever 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
15383c1c995cSJeff Layton 
1539a451b123STrond Myklebust 	if (!unhash_ol_stateid(stp))
1540a451b123STrond Myklebust 		return false;
15413c1c995cSJeff Layton 	list_del_init(&stp->st_locks);
1542cd61c522SChristoph Hellwig 	nfs4_unhash_stid(&stp->st_stid);
1543a451b123STrond Myklebust 	return true;
15443c1c995cSJeff Layton }
15453c1c995cSJeff Layton 
release_lock_stateid(struct nfs4_ol_stateid * stp)15465adfd885SJeff Layton static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1547b49e084dSJeff Layton {
1548f46c445bSChuck Lever 	struct nfs4_client *clp = stp->st_stid.sc_client;
1549e8568739SJeff Layton 	bool unhashed;
15501c755dc1SJeff Layton 
1551f46c445bSChuck Lever 	spin_lock(&clp->cl_lock);
1552e8568739SJeff Layton 	unhashed = unhash_lock_stateid(stp);
1553f46c445bSChuck Lever 	spin_unlock(&clp->cl_lock);
1554e8568739SJeff Layton 	if (unhashed)
15556011695dSTrond Myklebust 		nfs4_put_stid(&stp->st_stid);
1556529d7b2aSJ. Bruce Fields }
1557529d7b2aSJ. Bruce Fields 
unhash_lockowner_locked(struct nfs4_lockowner * lo)1558c58c6610STrond Myklebust static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1559529d7b2aSJ. Bruce Fields {
1560d4f0489fSTrond Myklebust 	struct nfs4_client *clp = lo->lo_owner.so_client;
1561c58c6610STrond Myklebust 
1562d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
1563c58c6610STrond Myklebust 
15648f4b54c5SJeff Layton 	list_del_init(&lo->lo_owner.so_strhash);
15658f4b54c5SJeff Layton }
15668f4b54c5SJeff Layton 
15672c41beb0SJeff Layton /*
15682c41beb0SJeff Layton  * Free a list of generic stateids that were collected earlier after being
15692c41beb0SJeff Layton  * fully unhashed.
15702c41beb0SJeff Layton  */
15712c41beb0SJeff Layton static void
free_ol_stateid_reaplist(struct list_head * reaplist)15722c41beb0SJeff Layton free_ol_stateid_reaplist(struct list_head *reaplist)
15732c41beb0SJeff Layton {
15742c41beb0SJeff Layton 	struct nfs4_ol_stateid *stp;
1575fb94d766SKinglong Mee 	struct nfs4_file *fp;
15762c41beb0SJeff Layton 
15772c41beb0SJeff Layton 	might_sleep();
15782c41beb0SJeff Layton 
15792c41beb0SJeff Layton 	while (!list_empty(reaplist)) {
15802c41beb0SJeff Layton 		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
15812c41beb0SJeff Layton 				       st_locks);
15822c41beb0SJeff Layton 		list_del(&stp->st_locks);
1583fb94d766SKinglong Mee 		fp = stp->st_stid.sc_file;
15842c41beb0SJeff Layton 		stp->st_stid.sc_free(&stp->st_stid);
1585fb94d766SKinglong Mee 		if (fp)
1586fb94d766SKinglong Mee 			put_nfs4_file(fp);
15872c41beb0SJeff Layton 	}
15882c41beb0SJeff Layton }
15892c41beb0SJeff Layton 
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1590d83017f9SJeff Layton static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1591d83017f9SJeff Layton 				       struct list_head *reaplist)
15923c87b9b7STrond Myklebust {
15933c87b9b7STrond Myklebust 	struct nfs4_ol_stateid *stp;
15943c87b9b7STrond Myklebust 
1595e8568739SJeff Layton 	lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1596e8568739SJeff Layton 
15973c87b9b7STrond Myklebust 	while (!list_empty(&open_stp->st_locks)) {
15983c87b9b7STrond Myklebust 		stp = list_entry(open_stp->st_locks.next,
15993c87b9b7STrond Myklebust 				struct nfs4_ol_stateid, st_locks);
1600e8568739SJeff Layton 		WARN_ON(!unhash_lock_stateid(stp));
1601d83017f9SJeff Layton 		put_ol_stateid_locked(stp, reaplist);
1602529d7b2aSJ. Bruce Fields 	}
1603529d7b2aSJ. Bruce Fields }
1604529d7b2aSJ. Bruce Fields 
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1605e8568739SJeff Layton static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1606d83017f9SJeff Layton 				struct list_head *reaplist)
16072283963fSJ. Bruce Fields {
16082c41beb0SJeff Layton 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
16092c41beb0SJeff Layton 
1610a451b123STrond Myklebust 	if (!unhash_ol_stateid(stp))
1611a451b123STrond Myklebust 		return false;
1612d83017f9SJeff Layton 	release_open_stateid_locks(stp, reaplist);
1613a451b123STrond Myklebust 	return true;
161438c387b5SJ. Bruce Fields }
161538c387b5SJ. Bruce Fields 
release_open_stateid(struct nfs4_ol_stateid * stp)161638c387b5SJ. Bruce Fields static void release_open_stateid(struct nfs4_ol_stateid *stp)
161738c387b5SJ. Bruce Fields {
16182c41beb0SJeff Layton 	LIST_HEAD(reaplist);
16192c41beb0SJeff Layton 
16202c41beb0SJeff Layton 	spin_lock(&stp->st_stid.sc_client->cl_lock);
1621e8568739SJeff Layton 	if (unhash_open_stateid(stp, &reaplist))
16222c41beb0SJeff Layton 		put_ol_stateid_locked(stp, &reaplist);
16232c41beb0SJeff Layton 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
16242c41beb0SJeff Layton 	free_ol_stateid_reaplist(&reaplist);
16252283963fSJ. Bruce Fields }
16262283963fSJ. Bruce Fields 
unhash_openowner_locked(struct nfs4_openowner * oo)16277ffb5880STrond Myklebust static void unhash_openowner_locked(struct nfs4_openowner *oo)
1628f1d110caSJ. Bruce Fields {
1629d4f0489fSTrond Myklebust 	struct nfs4_client *clp = oo->oo_owner.so_client;
16307ffb5880STrond Myklebust 
1631d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
16327ffb5880STrond Myklebust 
16338f4b54c5SJeff Layton 	list_del_init(&oo->oo_owner.so_strhash);
16348f4b54c5SJeff Layton 	list_del_init(&oo->oo_perclient);
1635f1d110caSJ. Bruce Fields }
1636f1d110caSJ. Bruce Fields 
release_last_closed_stateid(struct nfs4_openowner * oo)1637f7a4d872SJ. Bruce Fields static void release_last_closed_stateid(struct nfs4_openowner *oo)
1638f7a4d872SJ. Bruce Fields {
1639217526e7SJeff Layton 	struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1640217526e7SJeff Layton 					  nfsd_net_id);
1641217526e7SJeff Layton 	struct nfs4_ol_stateid *s;
1642f7a4d872SJ. Bruce Fields 
1643217526e7SJeff Layton 	spin_lock(&nn->client_lock);
1644217526e7SJeff Layton 	s = oo->oo_last_closed_stid;
1645f7a4d872SJ. Bruce Fields 	if (s) {
1646d3134b10SJeff Layton 		list_del_init(&oo->oo_close_lru);
1647f7a4d872SJ. Bruce Fields 		oo->oo_last_closed_stid = NULL;
1648f7a4d872SJ. Bruce Fields 	}
1649217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
1650217526e7SJeff Layton 	if (s)
1651217526e7SJeff Layton 		nfs4_put_stid(&s->st_stid);
1652f7a4d872SJ. Bruce Fields }
1653f7a4d872SJ. Bruce Fields 
release_openowner(struct nfs4_openowner * oo)16542c41beb0SJeff Layton static void release_openowner(struct nfs4_openowner *oo)
16558f4b54c5SJeff Layton {
16568f4b54c5SJeff Layton 	struct nfs4_ol_stateid *stp;
1657d4f0489fSTrond Myklebust 	struct nfs4_client *clp = oo->oo_owner.so_client;
16582c41beb0SJeff Layton 	struct list_head reaplist;
16597ffb5880STrond Myklebust 
16602c41beb0SJeff Layton 	INIT_LIST_HEAD(&reaplist);
16617ffb5880STrond Myklebust 
1662d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
16637ffb5880STrond Myklebust 	unhash_openowner_locked(oo);
16642c41beb0SJeff Layton 	while (!list_empty(&oo->oo_owner.so_stateids)) {
16652c41beb0SJeff Layton 		stp = list_first_entry(&oo->oo_owner.so_stateids,
16662c41beb0SJeff Layton 				struct nfs4_ol_stateid, st_perstateowner);
1667e8568739SJeff Layton 		if (unhash_open_stateid(stp, &reaplist))
16682c41beb0SJeff Layton 			put_ol_stateid_locked(stp, &reaplist);
16692c41beb0SJeff Layton 	}
1670d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
16712c41beb0SJeff Layton 	free_ol_stateid_reaplist(&reaplist);
1672f7a4d872SJ. Bruce Fields 	release_last_closed_stateid(oo);
16736b180f0bSJeff Layton 	nfs4_put_stateowner(&oo->oo_owner);
1674f1d110caSJ. Bruce Fields }
1675f1d110caSJ. Bruce Fields 
16765282fd72SMarc Eshel static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)16775282fd72SMarc Eshel hash_sessionid(struct nfs4_sessionid *sessionid)
16785282fd72SMarc Eshel {
16795282fd72SMarc Eshel 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
16805282fd72SMarc Eshel 
16815282fd72SMarc Eshel 	return sid->sequence % SESSION_HASH_SIZE;
16825282fd72SMarc Eshel }
16835282fd72SMarc Eshel 
1684135dd002SMark Salter #ifdef CONFIG_SUNRPC_DEBUG
16855282fd72SMarc Eshel static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)16865282fd72SMarc Eshel dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
16875282fd72SMarc Eshel {
16885282fd72SMarc Eshel 	u32 *ptr = (u32 *)(&sessionid->data[0]);
16895282fd72SMarc Eshel 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
16905282fd72SMarc Eshel }
16918f199b82STrond Myklebust #else
16928f199b82STrond Myklebust static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)16938f199b82STrond Myklebust dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
16948f199b82STrond Myklebust {
16958f199b82STrond Myklebust }
16968f199b82STrond Myklebust #endif
16978f199b82STrond Myklebust 
16989411b1d4SJ. Bruce Fields /*
16999411b1d4SJ. Bruce Fields  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
17009411b1d4SJ. Bruce Fields  * won't be used for replay.
17019411b1d4SJ. Bruce Fields  */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)17029411b1d4SJ. Bruce Fields void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
17039411b1d4SJ. Bruce Fields {
17049411b1d4SJ. Bruce Fields 	struct nfs4_stateowner *so = cstate->replay_owner;
17059411b1d4SJ. Bruce Fields 
17069411b1d4SJ. Bruce Fields 	if (nfserr == nfserr_replay_me)
17079411b1d4SJ. Bruce Fields 		return;
17089411b1d4SJ. Bruce Fields 
17099411b1d4SJ. Bruce Fields 	if (!seqid_mutating_err(ntohl(nfserr))) {
171058fb12e6SJeff Layton 		nfsd4_cstate_clear_replay(cstate);
17119411b1d4SJ. Bruce Fields 		return;
17129411b1d4SJ. Bruce Fields 	}
17139411b1d4SJ. Bruce Fields 	if (!so)
17149411b1d4SJ. Bruce Fields 		return;
17159411b1d4SJ. Bruce Fields 	if (so->so_is_open_owner)
17169411b1d4SJ. Bruce Fields 		release_last_closed_stateid(openowner(so));
17179411b1d4SJ. Bruce Fields 	so->so_seqid++;
17189411b1d4SJ. Bruce Fields 	return;
17199411b1d4SJ. Bruce Fields }
17205282fd72SMarc Eshel 
1721ec6b5d7bSAndy Adamson static void
gen_sessionid(struct nfsd4_session * ses)1722ec6b5d7bSAndy Adamson gen_sessionid(struct nfsd4_session *ses)
1723ec6b5d7bSAndy Adamson {
1724ec6b5d7bSAndy Adamson 	struct nfs4_client *clp = ses->se_client;
1725ec6b5d7bSAndy Adamson 	struct nfsd4_sessionid *sid;
1726ec6b5d7bSAndy Adamson 
1727ec6b5d7bSAndy Adamson 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1728ec6b5d7bSAndy Adamson 	sid->clientid = clp->cl_clientid;
1729ec6b5d7bSAndy Adamson 	sid->sequence = current_sessionid++;
1730ec6b5d7bSAndy Adamson 	sid->reserved = 0;
1731ec6b5d7bSAndy Adamson }
1732ec6b5d7bSAndy Adamson 
1733ec6b5d7bSAndy Adamson /*
1734a649637cSAndy Adamson  * The protocol defines ca_maxresponssize_cached to include the size of
1735a649637cSAndy Adamson  * the rpc header, but all we need to cache is the data starting after
1736a649637cSAndy Adamson  * the end of the initial SEQUENCE operation--the rest we regenerate
1737a649637cSAndy Adamson  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1738a649637cSAndy Adamson  * value that is the number of bytes in our cache plus a few additional
1739a649637cSAndy Adamson  * bytes.  In order to stay on the safe side, and not promise more than
1740a649637cSAndy Adamson  * we can cache, those additional bytes must be the minimum possible: 24
1741a649637cSAndy Adamson  * bytes of rpc header (xid through accept state, with AUTH_NULL
1742a649637cSAndy Adamson  * verifier), 12 for the compound header (with zero-length tag), and 44
1743a649637cSAndy Adamson  * for the SEQUENCE op response:
1744ec6b5d7bSAndy Adamson  */
1745a649637cSAndy Adamson #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1746a649637cSAndy Adamson 
1747557ce264SAndy Adamson static void
free_session_slots(struct nfsd4_session * ses)1748557ce264SAndy Adamson free_session_slots(struct nfsd4_session *ses)
1749557ce264SAndy Adamson {
1750557ce264SAndy Adamson 	int i;
1751557ce264SAndy Adamson 
175253da6a53SJ. Bruce Fields 	for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
175353da6a53SJ. Bruce Fields 		free_svc_cred(&ses->se_slots[i]->sl_cred);
1754557ce264SAndy Adamson 		kfree(ses->se_slots[i]);
1755557ce264SAndy Adamson 	}
175653da6a53SJ. Bruce Fields }
1757557ce264SAndy Adamson 
1758efe0cb6dSJ. Bruce Fields /*
1759efe0cb6dSJ. Bruce Fields  * We don't actually need to cache the rpc and session headers, so we
1760efe0cb6dSJ. Bruce Fields  * can allocate a little less for each slot:
1761efe0cb6dSJ. Bruce Fields  */
slot_bytes(struct nfsd4_channel_attrs * ca)176255c760cfSJ. Bruce Fields static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1763efe0cb6dSJ. Bruce Fields {
176455c760cfSJ. Bruce Fields 	u32 size;
1765efe0cb6dSJ. Bruce Fields 
176655c760cfSJ. Bruce Fields 	if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
176755c760cfSJ. Bruce Fields 		size = 0;
176855c760cfSJ. Bruce Fields 	else
176955c760cfSJ. Bruce Fields 		size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
177055c760cfSJ. Bruce Fields 	return size + sizeof(struct nfsd4_slot);
1771557ce264SAndy Adamson }
1772557ce264SAndy Adamson 
17735b6feee9SJ. Bruce Fields /*
17745b6feee9SJ. Bruce Fields  * XXX: If we run out of reserved DRC memory we could (up to a point)
17755b6feee9SJ. Bruce Fields  * re-negotiate active sessions and reduce their slot usage to make
177642b2aa86SJustin P. Mattock  * room for new connections. For now we just fail the create session.
17775b6feee9SJ. Bruce Fields  */
nfsd4_get_drc_mem(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)17782030ca56SNeilBrown static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
17795b6feee9SJ. Bruce Fields {
178055c760cfSJ. Bruce Fields 	u32 slotsize = slot_bytes(ca);
178155c760cfSJ. Bruce Fields 	u32 num = ca->maxreqs;
1782c54f24e3SJ. Bruce Fields 	unsigned long avail, total_avail;
17832030ca56SNeilBrown 	unsigned int scale_factor;
17845b6feee9SJ. Bruce Fields 
17855b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
17867f49fd5dSNeilBrown 	if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1787c54f24e3SJ. Bruce Fields 		total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
17887f49fd5dSNeilBrown 	else
17897f49fd5dSNeilBrown 		/* We have handed out more space than we chose in
17907f49fd5dSNeilBrown 		 * set_max_drc() to allow.  That isn't really a
17917f49fd5dSNeilBrown 		 * problem as long as that doesn't make us think we
17927f49fd5dSNeilBrown 		 * have lots more due to integer overflow.
17937f49fd5dSNeilBrown 		 */
17947f49fd5dSNeilBrown 		total_avail = 0;
1795c54f24e3SJ. Bruce Fields 	avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1796de766e57SJ. Bruce Fields 	/*
17972030ca56SNeilBrown 	 * Never use more than a fraction of the remaining memory,
17987f49fd5dSNeilBrown 	 * unless it's the only way to give this client a slot.
17992030ca56SNeilBrown 	 * The chosen fraction is either 1/8 or 1/number of threads,
18002030ca56SNeilBrown 	 * whichever is smaller.  This ensures there are adequate
18012030ca56SNeilBrown 	 * slots to support multiple clients per thread.
18027f49fd5dSNeilBrown 	 * Give the client one slot even if that would require
18037f49fd5dSNeilBrown 	 * over-allocation--it is better than failure.
1804de766e57SJ. Bruce Fields 	 */
18052030ca56SNeilBrown 	scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
18062030ca56SNeilBrown 
18072030ca56SNeilBrown 	avail = clamp_t(unsigned long, avail, slotsize,
18082030ca56SNeilBrown 			total_avail/scale_factor);
18095b6feee9SJ. Bruce Fields 	num = min_t(int, num, avail / slotsize);
18107f49fd5dSNeilBrown 	num = max_t(int, num, 1);
18115b6feee9SJ. Bruce Fields 	nfsd_drc_mem_used += num * slotsize;
18125b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
18135b6feee9SJ. Bruce Fields 
18145b6feee9SJ. Bruce Fields 	return num;
18155b6feee9SJ. Bruce Fields }
18165b6feee9SJ. Bruce Fields 
nfsd4_put_drc_mem(struct nfsd4_channel_attrs * ca)181755c760cfSJ. Bruce Fields static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
18185b6feee9SJ. Bruce Fields {
181955c760cfSJ. Bruce Fields 	int slotsize = slot_bytes(ca);
182055c760cfSJ. Bruce Fields 
18215b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
182255c760cfSJ. Bruce Fields 	nfsd_drc_mem_used -= slotsize * ca->maxreqs;
18235b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
18245b6feee9SJ. Bruce Fields }
18255b6feee9SJ. Bruce Fields 
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)182660810e54SKinglong Mee static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
182760810e54SKinglong Mee 					   struct nfsd4_channel_attrs *battrs)
18285b6feee9SJ. Bruce Fields {
182960810e54SKinglong Mee 	int numslots = fattrs->maxreqs;
183060810e54SKinglong Mee 	int slotsize = slot_bytes(fattrs);
18315b6feee9SJ. Bruce Fields 	struct nfsd4_session *new;
183285a0d0c9SXiu Jianfeng 	int i;
1833ec6b5d7bSAndy Adamson 
183485a0d0c9SXiu Jianfeng 	BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
183585a0d0c9SXiu Jianfeng 		     > PAGE_SIZE);
1836ec6b5d7bSAndy Adamson 
183785a0d0c9SXiu Jianfeng 	new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
18386c18ba9fSAlexandros Batsakis 	if (!new)
18395b6feee9SJ. Bruce Fields 		return NULL;
1840ec6b5d7bSAndy Adamson 	/* allocate each struct nfsd4_slot and data cache in one piece */
18415b6feee9SJ. Bruce Fields 	for (i = 0; i < numslots; i++) {
184255c760cfSJ. Bruce Fields 		new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
18435b6feee9SJ. Bruce Fields 		if (!new->se_slots[i])
1844ec6b5d7bSAndy Adamson 			goto out_free;
1845ec6b5d7bSAndy Adamson 	}
184660810e54SKinglong Mee 
184760810e54SKinglong Mee 	memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
184860810e54SKinglong Mee 	memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
184960810e54SKinglong Mee 
18505b6feee9SJ. Bruce Fields 	return new;
18515b6feee9SJ. Bruce Fields out_free:
18525b6feee9SJ. Bruce Fields 	while (i--)
18535b6feee9SJ. Bruce Fields 		kfree(new->se_slots[i]);
18545b6feee9SJ. Bruce Fields 	kfree(new);
18555b6feee9SJ. Bruce Fields 	return NULL;
18565b6feee9SJ. Bruce Fields }
18575b6feee9SJ. Bruce Fields 
free_conn(struct nfsd4_conn * c)185819cf5c02SJ. Bruce Fields static void free_conn(struct nfsd4_conn *c)
185919cf5c02SJ. Bruce Fields {
186019cf5c02SJ. Bruce Fields 	svc_xprt_put(c->cn_xprt);
186119cf5c02SJ. Bruce Fields 	kfree(c);
186219cf5c02SJ. Bruce Fields }
186319cf5c02SJ. Bruce Fields 
nfsd4_conn_lost(struct svc_xpt_user * u)186419cf5c02SJ. Bruce Fields static void nfsd4_conn_lost(struct svc_xpt_user *u)
186519cf5c02SJ. Bruce Fields {
186619cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
186719cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = c->cn_session->se_client;
186819cf5c02SJ. Bruce Fields 
1869806d65b6SChuck Lever 	trace_nfsd_cb_lost(clp);
1870806d65b6SChuck Lever 
187119cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
187219cf5c02SJ. Bruce Fields 	if (!list_empty(&c->cn_persession)) {
187319cf5c02SJ. Bruce Fields 		list_del(&c->cn_persession);
187419cf5c02SJ. Bruce Fields 		free_conn(c);
187519cf5c02SJ. Bruce Fields 	}
1876eea49806SJ. Bruce Fields 	nfsd4_probe_callback(clp);
18772e4b7239SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
187819cf5c02SJ. Bruce Fields }
187919cf5c02SJ. Bruce Fields 
alloc_conn(struct svc_rqst * rqstp,u32 flags)1880d29c374cSJ. Bruce Fields static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1881c7662518SJ. Bruce Fields {
1882c7662518SJ. Bruce Fields 	struct nfsd4_conn *conn;
1883c7662518SJ. Bruce Fields 
1884c7662518SJ. Bruce Fields 	conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1885c7662518SJ. Bruce Fields 	if (!conn)
1886db90681dSJ. Bruce Fields 		return NULL;
1887c7662518SJ. Bruce Fields 	svc_xprt_get(rqstp->rq_xprt);
1888c7662518SJ. Bruce Fields 	conn->cn_xprt = rqstp->rq_xprt;
1889d29c374cSJ. Bruce Fields 	conn->cn_flags = flags;
1890db90681dSJ. Bruce Fields 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1891db90681dSJ. Bruce Fields 	return conn;
1892db90681dSJ. Bruce Fields }
1893db90681dSJ. Bruce Fields 
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1894328ead28SJ. Bruce Fields static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1895328ead28SJ. Bruce Fields {
1896328ead28SJ. Bruce Fields 	conn->cn_session = ses;
1897328ead28SJ. Bruce Fields 	list_add(&conn->cn_persession, &ses->se_conns);
1898328ead28SJ. Bruce Fields }
1899328ead28SJ. Bruce Fields 
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1900db90681dSJ. Bruce Fields static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1901db90681dSJ. Bruce Fields {
1902db90681dSJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
1903c7662518SJ. Bruce Fields 
1904c7662518SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1905328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(conn, ses);
1906c7662518SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
1907db90681dSJ. Bruce Fields }
1908c7662518SJ. Bruce Fields 
nfsd4_register_conn(struct nfsd4_conn * conn)190921b75b01SJ. Bruce Fields static int nfsd4_register_conn(struct nfsd4_conn *conn)
1910db90681dSJ. Bruce Fields {
191119cf5c02SJ. Bruce Fields 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
191221b75b01SJ. Bruce Fields 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1913db90681dSJ. Bruce Fields }
1914db90681dSJ. Bruce Fields 
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)1915e1ff371fSJ. Bruce Fields static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1916db90681dSJ. Bruce Fields {
191721b75b01SJ. Bruce Fields 	int ret;
1918db90681dSJ. Bruce Fields 
1919db90681dSJ. Bruce Fields 	nfsd4_hash_conn(conn, ses);
192021b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(conn);
192121b75b01SJ. Bruce Fields 	if (ret)
192221b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
192321b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&conn->cn_xpt_user);
192457a37144SJ. Bruce Fields 	/* We may have gained or lost a callback channel: */
192557a37144SJ. Bruce Fields 	nfsd4_probe_callback_sync(ses->se_client);
1926c7662518SJ. Bruce Fields }
1927c7662518SJ. Bruce Fields 
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)1928e1ff371fSJ. Bruce Fields static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
19291d1bc8f2SJ. Bruce Fields {
19301d1bc8f2SJ. Bruce Fields 	u32 dir = NFS4_CDFC4_FORE;
19311d1bc8f2SJ. Bruce Fields 
1932e1ff371fSJ. Bruce Fields 	if (cses->flags & SESSION4_BACK_CHAN)
19331d1bc8f2SJ. Bruce Fields 		dir |= NFS4_CDFC4_BACK;
1934e1ff371fSJ. Bruce Fields 	return alloc_conn(rqstp, dir);
19351d1bc8f2SJ. Bruce Fields }
19361d1bc8f2SJ. Bruce Fields 
19371d1bc8f2SJ. Bruce Fields /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)193819cf5c02SJ. Bruce Fields static void nfsd4_del_conns(struct nfsd4_session *s)
1939c7662518SJ. Bruce Fields {
194019cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = s->se_client;
194119cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c;
194219cf5c02SJ. Bruce Fields 
194319cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
194419cf5c02SJ. Bruce Fields 	while (!list_empty(&s->se_conns)) {
194519cf5c02SJ. Bruce Fields 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
194619cf5c02SJ. Bruce Fields 		list_del_init(&c->cn_persession);
194719cf5c02SJ. Bruce Fields 		spin_unlock(&clp->cl_lock);
194819cf5c02SJ. Bruce Fields 
194919cf5c02SJ. Bruce Fields 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
195019cf5c02SJ. Bruce Fields 		free_conn(c);
195119cf5c02SJ. Bruce Fields 
195219cf5c02SJ. Bruce Fields 		spin_lock(&clp->cl_lock);
195319cf5c02SJ. Bruce Fields 	}
195419cf5c02SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
1955c7662518SJ. Bruce Fields }
1956c7662518SJ. Bruce Fields 
__free_session(struct nfsd4_session * ses)19571377b69eSJ. Bruce Fields static void __free_session(struct nfsd4_session *ses)
19581377b69eSJ. Bruce Fields {
19591377b69eSJ. Bruce Fields 	free_session_slots(ses);
19601377b69eSJ. Bruce Fields 	kfree(ses);
19611377b69eSJ. Bruce Fields }
19621377b69eSJ. Bruce Fields 
free_session(struct nfsd4_session * ses)196366b2b9b2SJ. Bruce Fields static void free_session(struct nfsd4_session *ses)
1964508dc6e1SBenny Halevy {
1965c7662518SJ. Bruce Fields 	nfsd4_del_conns(ses);
196655c760cfSJ. Bruce Fields 	nfsd4_put_drc_mem(&ses->se_fchannel);
1967c7662518SJ. Bruce Fields 	__free_session(ses);
1968a827bcb2SJ. Bruce Fields }
1969ec6b5d7bSAndy Adamson 
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)1970135ae827SFengguang Wu static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1971a827bcb2SJ. Bruce Fields {
1972a827bcb2SJ. Bruce Fields 	int idx;
19731872de0eSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1974a827bcb2SJ. Bruce Fields 
1975ec6b5d7bSAndy Adamson 	new->se_client = clp;
1976ec6b5d7bSAndy Adamson 	gen_sessionid(new);
1977ec6b5d7bSAndy Adamson 
1978c7662518SJ. Bruce Fields 	INIT_LIST_HEAD(&new->se_conns);
1979c7662518SJ. Bruce Fields 
1980ac7c46f2SJ. Bruce Fields 	new->se_cb_seq_nr = 1;
1981ec6b5d7bSAndy Adamson 	new->se_flags = cses->flags;
19828b5ce5cdSJ. Bruce Fields 	new->se_cb_prog = cses->callback_prog;
1983c6bb3ca2SJ. Bruce Fields 	new->se_cb_sec = cses->cb_sec;
198466b2b9b2SJ. Bruce Fields 	atomic_set(&new->se_ref, 0);
19855b6feee9SJ. Bruce Fields 	idx = hash_sessionid(&new->se_sessionid);
19861872de0eSStanislav Kinsbursky 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
19874c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1988ec6b5d7bSAndy Adamson 	list_add(&new->se_perclnt, &clp->cl_sessions);
19894c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
199060810e54SKinglong Mee 
1991b0d2e42cSChuck Lever 	{
1992edd76786SJ. Bruce Fields 		struct sockaddr *sa = svc_addr(rqstp);
1993dcbeaa68SJ. Bruce Fields 		/*
1994dcbeaa68SJ. Bruce Fields 		 * This is a little silly; with sessions there's no real
1995dcbeaa68SJ. Bruce Fields 		 * use for the callback address.  Use the peer address
1996dcbeaa68SJ. Bruce Fields 		 * as a reasonable default for now, but consider fixing
1997dcbeaa68SJ. Bruce Fields 		 * the rpc client not to require an address in the
1998dcbeaa68SJ. Bruce Fields 		 * future:
1999dcbeaa68SJ. Bruce Fields 		 */
2000edd76786SJ. Bruce Fields 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
2001edd76786SJ. Bruce Fields 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
2002edd76786SJ. Bruce Fields 	}
2003ec6b5d7bSAndy Adamson }
2004ec6b5d7bSAndy Adamson 
20059089f1b4SBenny Halevy /* caller must hold client_lock */
20065282fd72SMarc Eshel static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)2007d4e19e70STrond Myklebust __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
20085282fd72SMarc Eshel {
20095282fd72SMarc Eshel 	struct nfsd4_session *elem;
20105282fd72SMarc Eshel 	int idx;
20111872de0eSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
20125282fd72SMarc Eshel 
20130a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
20140a880a28STrond Myklebust 
20155282fd72SMarc Eshel 	dump_sessionid(__func__, sessionid);
20165282fd72SMarc Eshel 	idx = hash_sessionid(sessionid);
20175282fd72SMarc Eshel 	/* Search in the appropriate list */
20181872de0eSStanislav Kinsbursky 	list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
20195282fd72SMarc Eshel 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
20205282fd72SMarc Eshel 			    NFS4_MAX_SESSIONID_LEN)) {
20215282fd72SMarc Eshel 			return elem;
20225282fd72SMarc Eshel 		}
20235282fd72SMarc Eshel 	}
20245282fd72SMarc Eshel 
20255282fd72SMarc Eshel 	dprintk("%s: session not found\n", __func__);
20265282fd72SMarc Eshel 	return NULL;
20275282fd72SMarc Eshel }
20285282fd72SMarc Eshel 
2029d4e19e70STrond Myklebust static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)2030d4e19e70STrond Myklebust find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
2031d4e19e70STrond Myklebust 		__be32 *ret)
2032d4e19e70STrond Myklebust {
2033d4e19e70STrond Myklebust 	struct nfsd4_session *session;
2034d4e19e70STrond Myklebust 	__be32 status = nfserr_badsession;
2035d4e19e70STrond Myklebust 
2036d4e19e70STrond Myklebust 	session = __find_in_sessionid_hashtbl(sessionid, net);
2037d4e19e70STrond Myklebust 	if (!session)
2038d4e19e70STrond Myklebust 		goto out;
2039d4e19e70STrond Myklebust 	status = nfsd4_get_session_locked(session);
2040d4e19e70STrond Myklebust 	if (status)
2041d4e19e70STrond Myklebust 		session = NULL;
2042d4e19e70STrond Myklebust out:
2043d4e19e70STrond Myklebust 	*ret = status;
2044d4e19e70STrond Myklebust 	return session;
2045d4e19e70STrond Myklebust }
2046d4e19e70STrond Myklebust 
20479089f1b4SBenny Halevy /* caller must hold client_lock */
20487116ed6bSAndy Adamson static void
unhash_session(struct nfsd4_session * ses)20495282fd72SMarc Eshel unhash_session(struct nfsd4_session *ses)
20507116ed6bSAndy Adamson {
20510a880a28STrond Myklebust 	struct nfs4_client *clp = ses->se_client;
20520a880a28STrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
20530a880a28STrond Myklebust 
20540a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
20550a880a28STrond Myklebust 
20567116ed6bSAndy Adamson 	list_del(&ses->se_hash);
20574c649378SJ. Bruce Fields 	spin_lock(&ses->se_client->cl_lock);
20587116ed6bSAndy Adamson 	list_del(&ses->se_perclnt);
20594c649378SJ. Bruce Fields 	spin_unlock(&ses->se_client->cl_lock);
20605282fd72SMarc Eshel }
20615282fd72SMarc Eshel 
20621da177e4SLinus Torvalds /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
20631da177e4SLinus Torvalds static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)20642c142baaSStanislav Kinsbursky STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
20651da177e4SLinus Torvalds {
2066bbc7f33aSJ. Bruce Fields 	/*
2067bbc7f33aSJ. Bruce Fields 	 * We're assuming the clid was not given out from a boot
2068bbc7f33aSJ. Bruce Fields 	 * precisely 2^32 (about 136 years) before this one.  That seems
2069bbc7f33aSJ. Bruce Fields 	 * a safe assumption:
2070bbc7f33aSJ. Bruce Fields 	 */
2071bbc7f33aSJ. Bruce Fields 	if (clid->cl_boot == (u32)nn->boot_time)
20721da177e4SLinus Torvalds 		return 0;
2073dd5e3fbcSChuck Lever 	trace_nfsd_clid_stale(clid);
20741da177e4SLinus Torvalds 	return 1;
20751da177e4SLinus Torvalds }
20761da177e4SLinus Torvalds 
20771da177e4SLinus Torvalds /*
20781da177e4SLinus Torvalds  * XXX Should we use a slab cache ?
20791da177e4SLinus Torvalds  * This type of memory management is somewhat inefficient, but we use it
20801da177e4SLinus Torvalds  * anyway since SETCLIENTID is not a common operation.
20811da177e4SLinus Torvalds  */
alloc_client(struct xdr_netobj name,struct nfsd_net * nn)20820926c395SDai Ngo static struct nfs4_client *alloc_client(struct xdr_netobj name,
20830926c395SDai Ngo 				struct nfsd_net *nn)
20841da177e4SLinus Torvalds {
20851da177e4SLinus Torvalds 	struct nfs4_client *clp;
2086d4f0489fSTrond Myklebust 	int i;
20871da177e4SLinus Torvalds 
20884271c2c0SDai Ngo 	if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
20894271c2c0SDai Ngo 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
20904271c2c0SDai Ngo 		return NULL;
20914271c2c0SDai Ngo 	}
20929258a2d5SJeff Layton 	clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
209335bba9a3SJ. Bruce Fields 	if (clp == NULL)
209435bba9a3SJ. Bruce Fields 		return NULL;
20956f4859b8SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
2096d4f0489fSTrond Myklebust 	if (clp->cl_name.data == NULL)
2097d4f0489fSTrond Myklebust 		goto err_no_name;
20986da2ec56SKees Cook 	clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
20996da2ec56SKees Cook 						 sizeof(struct list_head),
21006da2ec56SKees Cook 						 GFP_KERNEL);
2101d4f0489fSTrond Myklebust 	if (!clp->cl_ownerstr_hashtbl)
2102d4f0489fSTrond Myklebust 		goto err_no_hashtbl;
2103d4f0489fSTrond Myklebust 	for (i = 0; i < OWNER_HASH_SIZE; i++)
2104d4f0489fSTrond Myklebust 		INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
21055694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_sessions);
21065694c93eSTrond Myklebust 	idr_init(&clp->cl_stateids);
210714ed14ccSJ. Bruce Fields 	atomic_set(&clp->cl_rpc_users, 0);
21085694c93eSTrond Myklebust 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
210966af2579SDai Ngo 	clp->cl_state = NFSD4_ACTIVE;
21100926c395SDai Ngo 	atomic_inc(&nn->nfs4_client_count);
211166af2579SDai Ngo 	atomic_set(&clp->cl_delegs_in_recall, 0);
21125694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_idhash);
21135694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_openowners);
21145694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_delegations);
21155694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_lru);
21165694c93eSTrond Myklebust 	INIT_LIST_HEAD(&clp->cl_revoked);
21179cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
21189cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&clp->cl_lo_states);
21199cf514ccSChristoph Hellwig #endif
2120e0639dc5SOlga Kornievskaia 	INIT_LIST_HEAD(&clp->async_copies);
2121e0639dc5SOlga Kornievskaia 	spin_lock_init(&clp->async_lock);
21225694c93eSTrond Myklebust 	spin_lock_init(&clp->cl_lock);
21235694c93eSTrond Myklebust 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
21241da177e4SLinus Torvalds 	return clp;
2125d4f0489fSTrond Myklebust err_no_hashtbl:
2126d4f0489fSTrond Myklebust 	kfree(clp->cl_name.data);
2127d4f0489fSTrond Myklebust err_no_name:
21289258a2d5SJeff Layton 	kmem_cache_free(client_slab, clp);
2129d4f0489fSTrond Myklebust 	return NULL;
21301da177e4SLinus Torvalds }
21311da177e4SLinus Torvalds 
__free_client(struct kref * k)213259f8e91bSJ. Bruce Fields static void __free_client(struct kref *k)
213359f8e91bSJ. Bruce Fields {
2134e8a79fb1SJ. Bruce Fields 	struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
2135e8a79fb1SJ. Bruce Fields 	struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
213659f8e91bSJ. Bruce Fields 
213759f8e91bSJ. Bruce Fields 	free_svc_cred(&clp->cl_cred);
213859f8e91bSJ. Bruce Fields 	kfree(clp->cl_ownerstr_hashtbl);
213959f8e91bSJ. Bruce Fields 	kfree(clp->cl_name.data);
214079123444SJ. Bruce Fields 	kfree(clp->cl_nii_domain.data);
214179123444SJ. Bruce Fields 	kfree(clp->cl_nii_name.data);
214259f8e91bSJ. Bruce Fields 	idr_destroy(&clp->cl_stateids);
214344df6f43SDai Ngo 	kfree(clp->cl_ra);
214459f8e91bSJ. Bruce Fields 	kmem_cache_free(client_slab, clp);
214559f8e91bSJ. Bruce Fields }
214659f8e91bSJ. Bruce Fields 
drop_client(struct nfs4_client * clp)2147297e57a2SYueHaibing static void drop_client(struct nfs4_client *clp)
214859f8e91bSJ. Bruce Fields {
2149e8a79fb1SJ. Bruce Fields 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
215059f8e91bSJ. Bruce Fields }
215159f8e91bSJ. Bruce Fields 
21524dd86e15STrond Myklebust static void
free_client(struct nfs4_client * clp)21531da177e4SLinus Torvalds free_client(struct nfs4_client *clp)
21541da177e4SLinus Torvalds {
2155792c95ddSJ. Bruce Fields 	while (!list_empty(&clp->cl_sessions)) {
2156792c95ddSJ. Bruce Fields 		struct nfsd4_session *ses;
2157792c95ddSJ. Bruce Fields 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2158792c95ddSJ. Bruce Fields 				se_perclnt);
2159792c95ddSJ. Bruce Fields 		list_del(&ses->se_perclnt);
216066b2b9b2SJ. Bruce Fields 		WARN_ON_ONCE(atomic_read(&ses->se_ref));
216166b2b9b2SJ. Bruce Fields 		free_session(ses);
2162792c95ddSJ. Bruce Fields 	}
21634cb57e30STrond Myklebust 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
216489c905beSJ. Bruce Fields 	if (clp->cl_nfsd_dentry) {
2165e8a79fb1SJ. Bruce Fields 		nfsd_client_rmdir(clp->cl_nfsd_dentry);
216689c905beSJ. Bruce Fields 		clp->cl_nfsd_dentry = NULL;
216789c905beSJ. Bruce Fields 		wake_up_all(&expiry_wq);
216889c905beSJ. Bruce Fields 	}
216959f8e91bSJ. Bruce Fields 	drop_client(clp);
21701da177e4SLinus Torvalds }
21711da177e4SLinus Torvalds 
217284d38ac9SBenny Halevy /* must be called under the client_lock */
21734beb345bSTrond Myklebust static void
unhash_client_locked(struct nfs4_client * clp)217484d38ac9SBenny Halevy unhash_client_locked(struct nfs4_client *clp)
217584d38ac9SBenny Halevy {
21764beb345bSTrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2177792c95ddSJ. Bruce Fields 	struct nfsd4_session *ses;
2178792c95ddSJ. Bruce Fields 
21790a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
21800a880a28STrond Myklebust 
21814beb345bSTrond Myklebust 	/* Mark the client as expired! */
21824beb345bSTrond Myklebust 	clp->cl_time = 0;
21834beb345bSTrond Myklebust 	/* Make it invisible */
21844beb345bSTrond Myklebust 	if (!list_empty(&clp->cl_idhash)) {
21854beb345bSTrond Myklebust 		list_del_init(&clp->cl_idhash);
21864beb345bSTrond Myklebust 		if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
21874beb345bSTrond Myklebust 			rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
21884beb345bSTrond Myklebust 		else
21894beb345bSTrond Myklebust 			rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
21904beb345bSTrond Myklebust 	}
21914beb345bSTrond Myklebust 	list_del_init(&clp->cl_lru);
21924c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
2193792c95ddSJ. Bruce Fields 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2194792c95ddSJ. Bruce Fields 		list_del_init(&ses->se_hash);
21954c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
219684d38ac9SBenny Halevy }
219784d38ac9SBenny Halevy 
21981da177e4SLinus Torvalds static void
unhash_client(struct nfs4_client * clp)21994beb345bSTrond Myklebust unhash_client(struct nfs4_client *clp)
22004beb345bSTrond Myklebust {
22014beb345bSTrond Myklebust 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
22024beb345bSTrond Myklebust 
22034beb345bSTrond Myklebust 	spin_lock(&nn->client_lock);
22044beb345bSTrond Myklebust 	unhash_client_locked(clp);
22054beb345bSTrond Myklebust 	spin_unlock(&nn->client_lock);
22064beb345bSTrond Myklebust }
22074beb345bSTrond Myklebust 
mark_client_expired_locked(struct nfs4_client * clp)220897403d95SJeff Layton static __be32 mark_client_expired_locked(struct nfs4_client *clp)
220997403d95SJeff Layton {
221014ed14ccSJ. Bruce Fields 	if (atomic_read(&clp->cl_rpc_users))
221197403d95SJeff Layton 		return nfserr_jukebox;
221297403d95SJeff Layton 	unhash_client_locked(clp);
221397403d95SJeff Layton 	return nfs_ok;
221497403d95SJeff Layton }
221597403d95SJeff Layton 
22164beb345bSTrond Myklebust static void
__destroy_client(struct nfs4_client * clp)22174beb345bSTrond Myklebust __destroy_client(struct nfs4_client *clp)
22181da177e4SLinus Torvalds {
22190926c395SDai Ngo 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
222068ef3bc3SJeff Layton 	int i;
2221fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
22221da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
22231da177e4SLinus Torvalds 	struct list_head reaplist;
22241da177e4SLinus Torvalds 
22251da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
2226cdc97505SBenny Halevy 	spin_lock(&state_lock);
2227ea1da636SNeilBrown 	while (!list_empty(&clp->cl_delegations)) {
2228ea1da636SNeilBrown 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
22293fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
223042690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
22311da177e4SLinus Torvalds 	}
2232cdc97505SBenny Halevy 	spin_unlock(&state_lock);
22331da177e4SLinus Torvalds 	while (!list_empty(&reaplist)) {
22341da177e4SLinus Torvalds 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
223542690676SJeff Layton 		list_del_init(&dp->dl_recall_lru);
22360af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
22371da177e4SLinus Torvalds 	}
22382d4a532dSJeff Layton 	while (!list_empty(&clp->cl_revoked)) {
2239c876486bSAndrew Elble 		dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
22402d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
22416011695dSTrond Myklebust 		nfs4_put_stid(&dp->dl_stid);
2242956c4feeSBenny Halevy 	}
2243ea1da636SNeilBrown 	while (!list_empty(&clp->cl_openowners)) {
2244fe0750e5SJ. Bruce Fields 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2245b5971afaSKinglong Mee 		nfs4_get_stateowner(&oo->oo_owner);
2246fe0750e5SJ. Bruce Fields 		release_openowner(oo);
22471da177e4SLinus Torvalds 	}
224868ef3bc3SJeff Layton 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
224968ef3bc3SJeff Layton 		struct nfs4_stateowner *so, *tmp;
225068ef3bc3SJeff Layton 
225168ef3bc3SJeff Layton 		list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
225268ef3bc3SJeff Layton 					 so_strhash) {
225368ef3bc3SJeff Layton 			/* Should be no openowners at this point */
225468ef3bc3SJeff Layton 			WARN_ON_ONCE(so->so_is_open_owner);
225568ef3bc3SJeff Layton 			remove_blocked_locks(lockowner(so));
225668ef3bc3SJeff Layton 		}
225768ef3bc3SJeff Layton 	}
22589cf514ccSChristoph Hellwig 	nfsd4_return_all_client_layouts(clp);
2259e0639dc5SOlga Kornievskaia 	nfsd4_shutdown_copy(clp);
22606ff8da08SJ. Bruce Fields 	nfsd4_shutdown_callback(clp);
22612bf23875SJ. Bruce Fields 	if (clp->cl_cb_conn.cb_xprt)
22622bf23875SJ. Bruce Fields 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
22630926c395SDai Ngo 	atomic_add_unless(&nn->nfs4_client_count, -1, 0);
22643a4ea23dSDai Ngo 	nfsd4_dec_courtesy_client_count(nn, clp);
2265b12a05cbSJ. Bruce Fields 	free_client(clp);
226689c905beSJ. Bruce Fields 	wake_up_all(&expiry_wq);
22671da177e4SLinus Torvalds }
22681da177e4SLinus Torvalds 
22694beb345bSTrond Myklebust static void
destroy_client(struct nfs4_client * clp)22704beb345bSTrond Myklebust destroy_client(struct nfs4_client *clp)
22714beb345bSTrond Myklebust {
22724beb345bSTrond Myklebust 	unhash_client(clp);
22734beb345bSTrond Myklebust 	__destroy_client(clp);
22744beb345bSTrond Myklebust }
22754beb345bSTrond Myklebust 
inc_reclaim_complete(struct nfs4_client * clp)2276362063a5SScott Mayhew static void inc_reclaim_complete(struct nfs4_client *clp)
2277362063a5SScott Mayhew {
2278362063a5SScott Mayhew 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2279362063a5SScott Mayhew 
2280362063a5SScott Mayhew 	if (!nn->track_reclaim_completes)
2281362063a5SScott Mayhew 		return;
2282362063a5SScott Mayhew 	if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2283362063a5SScott Mayhew 		return;
2284362063a5SScott Mayhew 	if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2285362063a5SScott Mayhew 			nn->reclaim_str_hashtbl_size) {
2286362063a5SScott Mayhew 		printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2287362063a5SScott Mayhew 				clp->net->ns.inum);
2288362063a5SScott Mayhew 		nfsd4_end_grace(nn);
2289362063a5SScott Mayhew 	}
2290362063a5SScott Mayhew }
2291362063a5SScott Mayhew 
expire_client(struct nfs4_client * clp)22920d22f68fSJ. Bruce Fields static void expire_client(struct nfs4_client *clp)
22930d22f68fSJ. Bruce Fields {
22944beb345bSTrond Myklebust 	unhash_client(clp);
22950d22f68fSJ. Bruce Fields 	nfsd4_client_record_remove(clp);
22964beb345bSTrond Myklebust 	__destroy_client(clp);
22970d22f68fSJ. Bruce Fields }
22980d22f68fSJ. Bruce Fields 
copy_verf(struct nfs4_client * target,nfs4_verifier * source)229935bba9a3SJ. Bruce Fields static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
230035bba9a3SJ. Bruce Fields {
230135bba9a3SJ. Bruce Fields 	memcpy(target->cl_verifier.data, source->data,
230235bba9a3SJ. Bruce Fields 			sizeof(target->cl_verifier.data));
23031da177e4SLinus Torvalds }
23041da177e4SLinus Torvalds 
copy_clid(struct nfs4_client * target,struct nfs4_client * source)230535bba9a3SJ. Bruce Fields static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
230635bba9a3SJ. Bruce Fields {
23071da177e4SLinus Torvalds 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
23081da177e4SLinus Torvalds 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
23091da177e4SLinus Torvalds }
23101da177e4SLinus Torvalds 
copy_cred(struct svc_cred * target,struct svc_cred * source)231150043859SJ. Bruce Fields static int copy_cred(struct svc_cred *target, struct svc_cred *source)
231250043859SJ. Bruce Fields {
23132f10fdcbSNeilBrown 	target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
23142f10fdcbSNeilBrown 	target->cr_raw_principal = kstrdup(source->cr_raw_principal,
23152f10fdcbSNeilBrown 								GFP_KERNEL);
23169abdda5dSChuck Lever 	target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
23172f10fdcbSNeilBrown 	if ((source->cr_principal && !target->cr_principal) ||
23189abdda5dSChuck Lever 	    (source->cr_raw_principal && !target->cr_raw_principal) ||
23199abdda5dSChuck Lever 	    (source->cr_targ_princ && !target->cr_targ_princ))
23202f10fdcbSNeilBrown 		return -ENOMEM;
232150043859SJ. Bruce Fields 
2322d5497fc6SJ. Bruce Fields 	target->cr_flavor = source->cr_flavor;
23231da177e4SLinus Torvalds 	target->cr_uid = source->cr_uid;
23241da177e4SLinus Torvalds 	target->cr_gid = source->cr_gid;
23251da177e4SLinus Torvalds 	target->cr_group_info = source->cr_group_info;
23261da177e4SLinus Torvalds 	get_group_info(target->cr_group_info);
23270dc1531aSJ. Bruce Fields 	target->cr_gss_mech = source->cr_gss_mech;
23280dc1531aSJ. Bruce Fields 	if (source->cr_gss_mech)
23290dc1531aSJ. Bruce Fields 		gss_mech_get(source->cr_gss_mech);
233003a4e1f6SJ. Bruce Fields 	return 0;
23311da177e4SLinus Torvalds }
23321da177e4SLinus Torvalds 
2333ef17af2aSRasmus Villemoes static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)2334ac55fdc4SJeff Layton compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2335ac55fdc4SJeff Layton {
2336ef17af2aSRasmus Villemoes 	if (o1->len < o2->len)
2337ef17af2aSRasmus Villemoes 		return -1;
2338ef17af2aSRasmus Villemoes 	if (o1->len > o2->len)
2339ef17af2aSRasmus Villemoes 		return 1;
2340ef17af2aSRasmus Villemoes 	return memcmp(o1->data, o2->data, o1->len);
2341ac55fdc4SJeff Layton }
2342ac55fdc4SJeff Layton 
23431da177e4SLinus Torvalds static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)2344599e0a22SJ. Bruce Fields same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2345599e0a22SJ. Bruce Fields {
2346599e0a22SJ. Bruce Fields 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
23471da177e4SLinus Torvalds }
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds static int
same_clid(clientid_t * cl1,clientid_t * cl2)2350599e0a22SJ. Bruce Fields same_clid(clientid_t *cl1, clientid_t *cl2)
2351599e0a22SJ. Bruce Fields {
2352599e0a22SJ. Bruce Fields 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
23531da177e4SLinus Torvalds }
23541da177e4SLinus Torvalds 
groups_equal(struct group_info * g1,struct group_info * g2)23558fbba96eSJ. Bruce Fields static bool groups_equal(struct group_info *g1, struct group_info *g2)
23568fbba96eSJ. Bruce Fields {
23578fbba96eSJ. Bruce Fields 	int i;
23588fbba96eSJ. Bruce Fields 
23598fbba96eSJ. Bruce Fields 	if (g1->ngroups != g2->ngroups)
23608fbba96eSJ. Bruce Fields 		return false;
23618fbba96eSJ. Bruce Fields 	for (i=0; i<g1->ngroups; i++)
236281243eacSAlexey Dobriyan 		if (!gid_eq(g1->gid[i], g2->gid[i]))
23638fbba96eSJ. Bruce Fields 			return false;
23648fbba96eSJ. Bruce Fields 	return true;
23658fbba96eSJ. Bruce Fields }
23668fbba96eSJ. Bruce Fields 
236768eb3508SJ. Bruce Fields /*
236868eb3508SJ. Bruce Fields  * RFC 3530 language requires clid_inuse be returned when the
236968eb3508SJ. Bruce Fields  * "principal" associated with a requests differs from that previously
237068eb3508SJ. Bruce Fields  * used.  We use uid, gid's, and gss principal string as our best
237168eb3508SJ. Bruce Fields  * approximation.  We also don't want to allow non-gss use of a client
237268eb3508SJ. Bruce Fields  * established using gss: in theory cr_principal should catch that
237368eb3508SJ. Bruce Fields  * change, but in practice cr_principal can be null even in the gss case
237468eb3508SJ. Bruce Fields  * since gssd doesn't always pass down a principal string.
237568eb3508SJ. Bruce Fields  */
is_gss_cred(struct svc_cred * cr)237668eb3508SJ. Bruce Fields static bool is_gss_cred(struct svc_cred *cr)
237768eb3508SJ. Bruce Fields {
237868eb3508SJ. Bruce Fields 	/* Is cr_flavor one of the gss "pseudoflavors"?: */
237968eb3508SJ. Bruce Fields 	return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
238068eb3508SJ. Bruce Fields }
238168eb3508SJ. Bruce Fields 
238268eb3508SJ. Bruce Fields 
23835559b50aSVivek Trivedi static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)2384599e0a22SJ. Bruce Fields same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2385599e0a22SJ. Bruce Fields {
238668eb3508SJ. Bruce Fields 	if ((is_gss_cred(cr1) != is_gss_cred(cr2))
23876fab8779SEric W. Biederman 		|| (!uid_eq(cr1->cr_uid, cr2->cr_uid))
23886fab8779SEric W. Biederman 		|| (!gid_eq(cr1->cr_gid, cr2->cr_gid))
23898fbba96eSJ. Bruce Fields 		|| !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
23908fbba96eSJ. Bruce Fields 		return false;
23919abdda5dSChuck Lever 	/* XXX: check that cr_targ_princ fields match ? */
23928fbba96eSJ. Bruce Fields 	if (cr1->cr_principal == cr2->cr_principal)
23938fbba96eSJ. Bruce Fields 		return true;
23948fbba96eSJ. Bruce Fields 	if (!cr1->cr_principal || !cr2->cr_principal)
23958fbba96eSJ. Bruce Fields 		return false;
23965559b50aSVivek Trivedi 	return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
23971da177e4SLinus Torvalds }
23981da177e4SLinus Torvalds 
svc_rqst_integrity_protected(struct svc_rqst * rqstp)239957266a6eSJ. Bruce Fields static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
240057266a6eSJ. Bruce Fields {
240157266a6eSJ. Bruce Fields 	struct svc_cred *cr = &rqstp->rq_cred;
240257266a6eSJ. Bruce Fields 	u32 service;
240357266a6eSJ. Bruce Fields 
2404c4720591SJ. Bruce Fields 	if (!cr->cr_gss_mech)
2405c4720591SJ. Bruce Fields 		return false;
240657266a6eSJ. Bruce Fields 	service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
240757266a6eSJ. Bruce Fields 	return service == RPC_GSS_SVC_INTEGRITY ||
240857266a6eSJ. Bruce Fields 	       service == RPC_GSS_SVC_PRIVACY;
240957266a6eSJ. Bruce Fields }
241057266a6eSJ. Bruce Fields 
nfsd4_mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)2411dedeb13fSAndrew Elble bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
241257266a6eSJ. Bruce Fields {
241357266a6eSJ. Bruce Fields 	struct svc_cred *cr = &rqstp->rq_cred;
241457266a6eSJ. Bruce Fields 
241557266a6eSJ. Bruce Fields 	if (!cl->cl_mach_cred)
241657266a6eSJ. Bruce Fields 		return true;
241757266a6eSJ. Bruce Fields 	if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
241857266a6eSJ. Bruce Fields 		return false;
241957266a6eSJ. Bruce Fields 	if (!svc_rqst_integrity_protected(rqstp))
242057266a6eSJ. Bruce Fields 		return false;
2421414ca017SJ. Bruce Fields 	if (cl->cl_cred.cr_raw_principal)
2422414ca017SJ. Bruce Fields 		return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2423414ca017SJ. Bruce Fields 						cr->cr_raw_principal);
242457266a6eSJ. Bruce Fields 	if (!cr->cr_principal)
242557266a6eSJ. Bruce Fields 		return false;
242657266a6eSJ. Bruce Fields 	return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
242757266a6eSJ. Bruce Fields }
242857266a6eSJ. Bruce Fields 
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)2429294ac32eSJeff Layton static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2430deda2faaSJ. Bruce Fields {
2431ab4684d1SChuck Lever 	__be32 verf[2];
24321da177e4SLinus Torvalds 
2433f419992cSJeff Layton 	/*
2434f419992cSJeff Layton 	 * This is opaque to client, so no need to byte-swap. Use
2435f419992cSJeff Layton 	 * __force to keep sparse happy
2436f419992cSJeff Layton 	 */
24379104ae49SArnd Bergmann 	verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
243819311aa8SKinglong Mee 	verf[1] = (__force __be32)nn->clverifier_counter++;
2439ab4684d1SChuck Lever 	memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds 
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)2442294ac32eSJeff Layton static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2443294ac32eSJeff Layton {
24449cc76801SArnd Bergmann 	clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2445294ac32eSJeff Layton 	clp->cl_clientid.cl_id = nn->clientid_counter++;
2446294ac32eSJeff Layton 	gen_confirm(clp, nn);
2447294ac32eSJeff Layton }
2448294ac32eSJeff Layton 
24494770d722SJeff Layton static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)24504770d722SJeff Layton find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
24514581d140SJ. Bruce Fields {
24523abdb607SJ. Bruce Fields 	struct nfs4_stid *ret;
24533abdb607SJ. Bruce Fields 
24543abdb607SJ. Bruce Fields 	ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
24553abdb607SJ. Bruce Fields 	if (!ret || !ret->sc_type)
24563abdb607SJ. Bruce Fields 		return NULL;
24573abdb607SJ. Bruce Fields 	return ret;
24584581d140SJ. Bruce Fields }
24594d71ab87SJ. Bruce Fields 
24604770d722SJeff Layton static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,char typemask)24614770d722SJeff Layton find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2462f459e453SJ. Bruce Fields {
2463f459e453SJ. Bruce Fields 	struct nfs4_stid *s;
2464f459e453SJ. Bruce Fields 
24654770d722SJeff Layton 	spin_lock(&cl->cl_lock);
24664770d722SJeff Layton 	s = find_stateid_locked(cl, t);
24672d3f9668STrond Myklebust 	if (s != NULL) {
24682d3f9668STrond Myklebust 		if (typemask & s->sc_type)
2469a15dfcd5SElena Reshetova 			refcount_inc(&s->sc_count);
24702d3f9668STrond Myklebust 		else
24714770d722SJeff Layton 			s = NULL;
24722d3f9668STrond Myklebust 	}
24734770d722SJeff Layton 	spin_unlock(&cl->cl_lock);
24744d71ab87SJ. Bruce Fields 	return s;
24754581d140SJ. Bruce Fields }
24764581d140SJ. Bruce Fields 
get_nfsdfs_clp(struct inode * inode)2477a204f25eSJ. Bruce Fields static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2478a204f25eSJ. Bruce Fields {
2479a204f25eSJ. Bruce Fields 	struct nfsdfs_client *nc;
2480a204f25eSJ. Bruce Fields 	nc = get_nfsdfs_client(inode);
2481a204f25eSJ. Bruce Fields 	if (!nc)
2482a204f25eSJ. Bruce Fields 		return NULL;
2483a204f25eSJ. Bruce Fields 	return container_of(nc, struct nfs4_client, cl_nfsdfs);
2484a204f25eSJ. Bruce Fields }
2485a204f25eSJ. Bruce Fields 
seq_quote_mem(struct seq_file * m,char * data,int len)2486169319f1SJ. Bruce Fields static void seq_quote_mem(struct seq_file *m, char *data, int len)
2487169319f1SJ. Bruce Fields {
2488169319f1SJ. Bruce Fields 	seq_printf(m, "\"");
2489c0546391SAndy Shevchenko 	seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
2490169319f1SJ. Bruce Fields 	seq_printf(m, "\"");
2491169319f1SJ. Bruce Fields }
2492169319f1SJ. Bruce Fields 
cb_state2str(int state)24933518c866SDave Wysochanski static const char *cb_state2str(int state)
24943518c866SDave Wysochanski {
24953518c866SDave Wysochanski 	switch (state) {
24963518c866SDave Wysochanski 	case NFSD4_CB_UP:
24973518c866SDave Wysochanski 		return "UP";
24983518c866SDave Wysochanski 	case NFSD4_CB_UNKNOWN:
24993518c866SDave Wysochanski 		return "UNKNOWN";
25003518c866SDave Wysochanski 	case NFSD4_CB_DOWN:
25013518c866SDave Wysochanski 		return "DOWN";
25023518c866SDave Wysochanski 	case NFSD4_CB_FAULT:
25033518c866SDave Wysochanski 		return "FAULT";
25043518c866SDave Wysochanski 	}
25053518c866SDave Wysochanski 	return "UNDEFINED";
25063518c866SDave Wysochanski }
25073518c866SDave Wysochanski 
client_info_show(struct seq_file * m,void * v)250897ad4031SJ. Bruce Fields static int client_info_show(struct seq_file *m, void *v)
250997ad4031SJ. Bruce Fields {
25101d7f6b30SChenXiaoSong 	struct inode *inode = file_inode(m->file);
251197ad4031SJ. Bruce Fields 	struct nfs4_client *clp;
251297ad4031SJ. Bruce Fields 	u64 clid;
251397ad4031SJ. Bruce Fields 
2514a204f25eSJ. Bruce Fields 	clp = get_nfsdfs_clp(inode);
2515a204f25eSJ. Bruce Fields 	if (!clp)
251697ad4031SJ. Bruce Fields 		return -ENXIO;
251797ad4031SJ. Bruce Fields 	memcpy(&clid, &clp->cl_clientid, sizeof(clid));
251897ad4031SJ. Bruce Fields 	seq_printf(m, "clientid: 0x%llx\n", clid);
2519169319f1SJ. Bruce Fields 	seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2520e9488d5aSDai Ngo 
2521e9488d5aSDai Ngo 	if (clp->cl_state == NFSD4_COURTESY)
2522e9488d5aSDai Ngo 		seq_puts(m, "status: courtesy\n");
2523e9488d5aSDai Ngo 	else if (clp->cl_state == NFSD4_EXPIRABLE)
2524e9488d5aSDai Ngo 		seq_puts(m, "status: expirable\n");
2525e9488d5aSDai Ngo 	else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2526472d155aSNeilBrown 		seq_puts(m, "status: confirmed\n");
2527472d155aSNeilBrown 	else
2528472d155aSNeilBrown 		seq_puts(m, "status: unconfirmed\n");
2529e9488d5aSDai Ngo 	seq_printf(m, "seconds from last renew: %lld\n",
2530e9488d5aSDai Ngo 		ktime_get_boottime_seconds() - clp->cl_time);
2531169319f1SJ. Bruce Fields 	seq_printf(m, "name: ");
2532169319f1SJ. Bruce Fields 	seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2533169319f1SJ. Bruce Fields 	seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
253479123444SJ. Bruce Fields 	if (clp->cl_nii_domain.data) {
253579123444SJ. Bruce Fields 		seq_printf(m, "Implementation domain: ");
253679123444SJ. Bruce Fields 		seq_quote_mem(m, clp->cl_nii_domain.data,
253779123444SJ. Bruce Fields 					clp->cl_nii_domain.len);
253879123444SJ. Bruce Fields 		seq_printf(m, "\nImplementation name: ");
253979123444SJ. Bruce Fields 		seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2540e29f4703SArnd Bergmann 		seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
254179123444SJ. Bruce Fields 			clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
254279123444SJ. Bruce Fields 	}
25433518c866SDave Wysochanski 	seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
25443518c866SDave Wysochanski 	seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr);
254597ad4031SJ. Bruce Fields 	drop_client(clp);
254697ad4031SJ. Bruce Fields 
254797ad4031SJ. Bruce Fields 	return 0;
254897ad4031SJ. Bruce Fields }
254997ad4031SJ. Bruce Fields 
25501d7f6b30SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(client_info);
255197ad4031SJ. Bruce Fields 
states_start(struct seq_file * s,loff_t * pos)255278599c42SJ. Bruce Fields static void *states_start(struct seq_file *s, loff_t *pos)
255378599c42SJ. Bruce Fields 	__acquires(&clp->cl_lock)
255478599c42SJ. Bruce Fields {
255578599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
255678599c42SJ. Bruce Fields 	unsigned long id = *pos;
255778599c42SJ. Bruce Fields 	void *ret;
255878599c42SJ. Bruce Fields 
255978599c42SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
256078599c42SJ. Bruce Fields 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
256178599c42SJ. Bruce Fields 	*pos = id;
256278599c42SJ. Bruce Fields 	return ret;
256378599c42SJ. Bruce Fields }
256478599c42SJ. Bruce Fields 
states_next(struct seq_file * s,void * v,loff_t * pos)256578599c42SJ. Bruce Fields static void *states_next(struct seq_file *s, void *v, loff_t *pos)
256678599c42SJ. Bruce Fields {
256778599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
256878599c42SJ. Bruce Fields 	unsigned long id = *pos;
256978599c42SJ. Bruce Fields 	void *ret;
257078599c42SJ. Bruce Fields 
257178599c42SJ. Bruce Fields 	id = *pos;
257278599c42SJ. Bruce Fields 	id++;
257378599c42SJ. Bruce Fields 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
257478599c42SJ. Bruce Fields 	*pos = id;
257578599c42SJ. Bruce Fields 	return ret;
257678599c42SJ. Bruce Fields }
257778599c42SJ. Bruce Fields 
states_stop(struct seq_file * s,void * v)257878599c42SJ. Bruce Fields static void states_stop(struct seq_file *s, void *v)
257978599c42SJ. Bruce Fields 	__releases(&clp->cl_lock)
258078599c42SJ. Bruce Fields {
258178599c42SJ. Bruce Fields 	struct nfs4_client *clp = s->private;
258278599c42SJ. Bruce Fields 
258378599c42SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
258478599c42SJ. Bruce Fields }
258578599c42SJ. Bruce Fields 
nfs4_show_fname(struct seq_file * s,struct nfsd_file * f)2586580da465SAchilles Gaikwad static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2587580da465SAchilles Gaikwad {
2588580da465SAchilles Gaikwad          seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2589580da465SAchilles Gaikwad }
2590580da465SAchilles Gaikwad 
nfs4_show_superblock(struct seq_file * s,struct nfsd_file * f)2591fd4f83fdSJeff Layton static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
259278599c42SJ. Bruce Fields {
2593427f5f83SChuck Lever 	struct inode *inode = file_inode(f->nf_file);
259478599c42SJ. Bruce Fields 
259578599c42SJ. Bruce Fields 	seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
259678599c42SJ. Bruce Fields 					MAJOR(inode->i_sb->s_dev),
259778599c42SJ. Bruce Fields 					 MINOR(inode->i_sb->s_dev),
259878599c42SJ. Bruce Fields 					 inode->i_ino);
259978599c42SJ. Bruce Fields }
260078599c42SJ. Bruce Fields 
nfs4_show_owner(struct seq_file * s,struct nfs4_stateowner * oo)260178599c42SJ. Bruce Fields static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
260278599c42SJ. Bruce Fields {
260378599c42SJ. Bruce Fields 	seq_printf(s, "owner: ");
260478599c42SJ. Bruce Fields 	seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
260578599c42SJ. Bruce Fields }
260678599c42SJ. Bruce Fields 
nfs4_show_stateid(struct seq_file * s,stateid_t * stid)2607ace7ade4SJ. Bruce Fields static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2608ace7ade4SJ. Bruce Fields {
2609ee590d25SJ. Bruce Fields 	seq_printf(s, "0x%.8x", stid->si_generation);
2610ee590d25SJ. Bruce Fields 	seq_printf(s, "%12phN", &stid->si_opaque);
2611ace7ade4SJ. Bruce Fields }
2612ace7ade4SJ. Bruce Fields 
nfs4_show_open(struct seq_file * s,struct nfs4_stid * st)261378599c42SJ. Bruce Fields static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
261478599c42SJ. Bruce Fields {
261578599c42SJ. Bruce Fields 	struct nfs4_ol_stateid *ols;
261678599c42SJ. Bruce Fields 	struct nfs4_file *nf;
2617fd4f83fdSJeff Layton 	struct nfsd_file *file;
261878599c42SJ. Bruce Fields 	struct nfs4_stateowner *oo;
261978599c42SJ. Bruce Fields 	unsigned int access, deny;
262078599c42SJ. Bruce Fields 
262178599c42SJ. Bruce Fields 	if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
262278599c42SJ. Bruce Fields 		return 0; /* XXX: or SEQ_SKIP? */
262378599c42SJ. Bruce Fields 	ols = openlockstateid(st);
262478599c42SJ. Bruce Fields 	oo = ols->st_stateowner;
262578599c42SJ. Bruce Fields 	nf = st->sc_file;
2626e0aa6510SJeff Layton 
2627e0aa6510SJeff Layton 	spin_lock(&nf->fi_lock);
2628e0aa6510SJeff Layton 	file = find_any_file_locked(nf);
26299affa435SJ. Bruce Fields 	if (!file)
2630e0aa6510SJeff Layton 		goto out;
263178599c42SJ. Bruce Fields 
2632ace7ade4SJ. Bruce Fields 	seq_printf(s, "- ");
2633ace7ade4SJ. Bruce Fields 	nfs4_show_stateid(s, &st->sc_stateid);
2634ace7ade4SJ. Bruce Fields 	seq_printf(s, ": { type: open, ");
263578599c42SJ. Bruce Fields 
263678599c42SJ. Bruce Fields 	access = bmap_to_share_mode(ols->st_access_bmap);
263778599c42SJ. Bruce Fields 	deny   = bmap_to_share_mode(ols->st_deny_bmap);
263878599c42SJ. Bruce Fields 
2639c4b77edbSJ. Bruce Fields 	seq_printf(s, "access: %s%s, ",
264078599c42SJ. Bruce Fields 		access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
264178599c42SJ. Bruce Fields 		access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2642c4b77edbSJ. Bruce Fields 	seq_printf(s, "deny: %s%s, ",
264378599c42SJ. Bruce Fields 		deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
264478599c42SJ. Bruce Fields 		deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
264578599c42SJ. Bruce Fields 
264678599c42SJ. Bruce Fields 	nfs4_show_superblock(s, file);
264778599c42SJ. Bruce Fields 	seq_printf(s, ", ");
2648580da465SAchilles Gaikwad 	nfs4_show_fname(s, file);
2649580da465SAchilles Gaikwad 	seq_printf(s, ", ");
265078599c42SJ. Bruce Fields 	nfs4_show_owner(s, oo);
265178599c42SJ. Bruce Fields 	seq_printf(s, " }\n");
2652e0aa6510SJeff Layton out:
2653e0aa6510SJeff Layton 	spin_unlock(&nf->fi_lock);
265478599c42SJ. Bruce Fields 	return 0;
265578599c42SJ. Bruce Fields }
265678599c42SJ. Bruce Fields 
nfs4_show_lock(struct seq_file * s,struct nfs4_stid * st)265716d36e09SJ. Bruce Fields static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
265816d36e09SJ. Bruce Fields {
265916d36e09SJ. Bruce Fields 	struct nfs4_ol_stateid *ols;
266016d36e09SJ. Bruce Fields 	struct nfs4_file *nf;
2661fd4f83fdSJeff Layton 	struct nfsd_file *file;
266216d36e09SJ. Bruce Fields 	struct nfs4_stateowner *oo;
266316d36e09SJ. Bruce Fields 
266416d36e09SJ. Bruce Fields 	ols = openlockstateid(st);
266516d36e09SJ. Bruce Fields 	oo = ols->st_stateowner;
266616d36e09SJ. Bruce Fields 	nf = st->sc_file;
2667e0aa6510SJeff Layton 	spin_lock(&nf->fi_lock);
2668e0aa6510SJeff Layton 	file = find_any_file_locked(nf);
26699affa435SJ. Bruce Fields 	if (!file)
2670e0aa6510SJeff Layton 		goto out;
267116d36e09SJ. Bruce Fields 
2672ace7ade4SJ. Bruce Fields 	seq_printf(s, "- ");
2673ace7ade4SJ. Bruce Fields 	nfs4_show_stateid(s, &st->sc_stateid);
2674ace7ade4SJ. Bruce Fields 	seq_printf(s, ": { type: lock, ");
267516d36e09SJ. Bruce Fields 
267616d36e09SJ. Bruce Fields 	/*
267716d36e09SJ. Bruce Fields 	 * Note: a lock stateid isn't really the same thing as a lock,
267816d36e09SJ. Bruce Fields 	 * it's the locking state held by one owner on a file, and there
267916d36e09SJ. Bruce Fields 	 * may be multiple (or no) lock ranges associated with it.
268016d36e09SJ. Bruce Fields 	 * (Same for the matter is true of open stateids.)
268116d36e09SJ. Bruce Fields 	 */
268216d36e09SJ. Bruce Fields 
268316d36e09SJ. Bruce Fields 	nfs4_show_superblock(s, file);
268416d36e09SJ. Bruce Fields 	/* XXX: open stateid? */
268516d36e09SJ. Bruce Fields 	seq_printf(s, ", ");
2686580da465SAchilles Gaikwad 	nfs4_show_fname(s, file);
2687580da465SAchilles Gaikwad 	seq_printf(s, ", ");
268816d36e09SJ. Bruce Fields 	nfs4_show_owner(s, oo);
268916d36e09SJ. Bruce Fields 	seq_printf(s, " }\n");
2690e0aa6510SJeff Layton out:
2691e0aa6510SJeff Layton 	spin_unlock(&nf->fi_lock);
269216d36e09SJ. Bruce Fields 	return 0;
269316d36e09SJ. Bruce Fields }
269416d36e09SJ. Bruce Fields 
nfs4_show_deleg(struct seq_file * s,struct nfs4_stid * st)269516d36e09SJ. Bruce Fields static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
269616d36e09SJ. Bruce Fields {
269716d36e09SJ. Bruce Fields 	struct nfs4_delegation *ds;
269816d36e09SJ. Bruce Fields 	struct nfs4_file *nf;
2699eb82dd39SJeff Layton 	struct nfsd_file *file;
270016d36e09SJ. Bruce Fields 
270116d36e09SJ. Bruce Fields 	ds = delegstateid(st);
270216d36e09SJ. Bruce Fields 	nf = st->sc_file;
2703e0aa6510SJeff Layton 	spin_lock(&nf->fi_lock);
270445ba66ccSJeff Layton 	file = nf->fi_deleg_file;
27059affa435SJ. Bruce Fields 	if (!file)
2706e0aa6510SJeff Layton 		goto out;
270716d36e09SJ. Bruce Fields 
2708ace7ade4SJ. Bruce Fields 	seq_printf(s, "- ");
2709ace7ade4SJ. Bruce Fields 	nfs4_show_stateid(s, &st->sc_stateid);
2710ace7ade4SJ. Bruce Fields 	seq_printf(s, ": { type: deleg, ");
271116d36e09SJ. Bruce Fields 
271216d36e09SJ. Bruce Fields 	/* Kinda dead code as long as we only support read delegs: */
271316d36e09SJ. Bruce Fields 	seq_printf(s, "access: %s, ",
271416d36e09SJ. Bruce Fields 		ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
271516d36e09SJ. Bruce Fields 
271616d36e09SJ. Bruce Fields 	/* XXX: lease time, whether it's being recalled. */
271716d36e09SJ. Bruce Fields 
271816d36e09SJ. Bruce Fields 	nfs4_show_superblock(s, file);
2719580da465SAchilles Gaikwad 	seq_printf(s, ", ");
2720580da465SAchilles Gaikwad 	nfs4_show_fname(s, file);
272116d36e09SJ. Bruce Fields 	seq_printf(s, " }\n");
2722e0aa6510SJeff Layton out:
2723e0aa6510SJeff Layton 	spin_unlock(&nf->fi_lock);
272416d36e09SJ. Bruce Fields 	return 0;
272516d36e09SJ. Bruce Fields }
272616d36e09SJ. Bruce Fields 
nfs4_show_layout(struct seq_file * s,struct nfs4_stid * st)27270c4b62b0SJ. Bruce Fields static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
27280c4b62b0SJ. Bruce Fields {
27290c4b62b0SJ. Bruce Fields 	struct nfs4_layout_stateid *ls;
2730eb82dd39SJeff Layton 	struct nfsd_file *file;
27310c4b62b0SJ. Bruce Fields 
27320c4b62b0SJ. Bruce Fields 	ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
27330c4b62b0SJ. Bruce Fields 	file = ls->ls_file;
27340c4b62b0SJ. Bruce Fields 
2735ace7ade4SJ. Bruce Fields 	seq_printf(s, "- ");
2736ace7ade4SJ. Bruce Fields 	nfs4_show_stateid(s, &st->sc_stateid);
2737ace7ade4SJ. Bruce Fields 	seq_printf(s, ": { type: layout, ");
27380c4b62b0SJ. Bruce Fields 
27390c4b62b0SJ. Bruce Fields 	/* XXX: What else would be useful? */
27400c4b62b0SJ. Bruce Fields 
27410c4b62b0SJ. Bruce Fields 	nfs4_show_superblock(s, file);
2742580da465SAchilles Gaikwad 	seq_printf(s, ", ");
2743580da465SAchilles Gaikwad 	nfs4_show_fname(s, file);
27440c4b62b0SJ. Bruce Fields 	seq_printf(s, " }\n");
27450c4b62b0SJ. Bruce Fields 
27460c4b62b0SJ. Bruce Fields 	return 0;
27470c4b62b0SJ. Bruce Fields }
27480c4b62b0SJ. Bruce Fields 
states_show(struct seq_file * s,void * v)274978599c42SJ. Bruce Fields static int states_show(struct seq_file *s, void *v)
275078599c42SJ. Bruce Fields {
275178599c42SJ. Bruce Fields 	struct nfs4_stid *st = v;
275278599c42SJ. Bruce Fields 
275378599c42SJ. Bruce Fields 	switch (st->sc_type) {
275478599c42SJ. Bruce Fields 	case NFS4_OPEN_STID:
275578599c42SJ. Bruce Fields 		return nfs4_show_open(s, st);
275616d36e09SJ. Bruce Fields 	case NFS4_LOCK_STID:
275716d36e09SJ. Bruce Fields 		return nfs4_show_lock(s, st);
275816d36e09SJ. Bruce Fields 	case NFS4_DELEG_STID:
275916d36e09SJ. Bruce Fields 		return nfs4_show_deleg(s, st);
27600c4b62b0SJ. Bruce Fields 	case NFS4_LAYOUT_STID:
27610c4b62b0SJ. Bruce Fields 		return nfs4_show_layout(s, st);
276278599c42SJ. Bruce Fields 	default:
276378599c42SJ. Bruce Fields 		return 0; /* XXX: or SEQ_SKIP? */
276478599c42SJ. Bruce Fields 	}
276516d36e09SJ. Bruce Fields 	/* XXX: copy stateids? */
276678599c42SJ. Bruce Fields }
276778599c42SJ. Bruce Fields 
276878599c42SJ. Bruce Fields static struct seq_operations states_seq_ops = {
276978599c42SJ. Bruce Fields 	.start = states_start,
277078599c42SJ. Bruce Fields 	.next = states_next,
277178599c42SJ. Bruce Fields 	.stop = states_stop,
277278599c42SJ. Bruce Fields 	.show = states_show
277378599c42SJ. Bruce Fields };
277478599c42SJ. Bruce Fields 
client_states_open(struct inode * inode,struct file * file)277578599c42SJ. Bruce Fields static int client_states_open(struct inode *inode, struct file *file)
277678599c42SJ. Bruce Fields {
277778599c42SJ. Bruce Fields 	struct seq_file *s;
277878599c42SJ. Bruce Fields 	struct nfs4_client *clp;
277978599c42SJ. Bruce Fields 	int ret;
278078599c42SJ. Bruce Fields 
2781a204f25eSJ. Bruce Fields 	clp = get_nfsdfs_clp(inode);
2782a204f25eSJ. Bruce Fields 	if (!clp)
278378599c42SJ. Bruce Fields 		return -ENXIO;
278478599c42SJ. Bruce Fields 
278578599c42SJ. Bruce Fields 	ret = seq_open(file, &states_seq_ops);
278678599c42SJ. Bruce Fields 	if (ret)
278778599c42SJ. Bruce Fields 		return ret;
278878599c42SJ. Bruce Fields 	s = file->private_data;
278978599c42SJ. Bruce Fields 	s->private = clp;
279078599c42SJ. Bruce Fields 	return 0;
279178599c42SJ. Bruce Fields }
279278599c42SJ. Bruce Fields 
client_opens_release(struct inode * inode,struct file * file)279378599c42SJ. Bruce Fields static int client_opens_release(struct inode *inode, struct file *file)
279478599c42SJ. Bruce Fields {
279578599c42SJ. Bruce Fields 	struct seq_file *m = file->private_data;
279678599c42SJ. Bruce Fields 	struct nfs4_client *clp = m->private;
279778599c42SJ. Bruce Fields 
279878599c42SJ. Bruce Fields 	/* XXX: alternatively, we could get/drop in seq start/stop */
279978599c42SJ. Bruce Fields 	drop_client(clp);
280047a9c946SMahmoud Adam 	return seq_release(inode, file);
280178599c42SJ. Bruce Fields }
280278599c42SJ. Bruce Fields 
280378599c42SJ. Bruce Fields static const struct file_operations client_states_fops = {
280478599c42SJ. Bruce Fields 	.open		= client_states_open,
280578599c42SJ. Bruce Fields 	.read		= seq_read,
280678599c42SJ. Bruce Fields 	.llseek		= seq_lseek,
280778599c42SJ. Bruce Fields 	.release	= client_opens_release,
280878599c42SJ. Bruce Fields };
280978599c42SJ. Bruce Fields 
281089c905beSJ. Bruce Fields /*
281189c905beSJ. Bruce Fields  * Normally we refuse to destroy clients that are in use, but here the
281289c905beSJ. Bruce Fields  * administrator is telling us to just do it.  We also want to wait
281389c905beSJ. Bruce Fields  * so the caller has a guarantee that the client's locks are gone by
281489c905beSJ. Bruce Fields  * the time the write returns:
281589c905beSJ. Bruce Fields  */
force_expire_client(struct nfs4_client * clp)2816297e57a2SYueHaibing static void force_expire_client(struct nfs4_client *clp)
281789c905beSJ. Bruce Fields {
281889c905beSJ. Bruce Fields 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
281989c905beSJ. Bruce Fields 	bool already_expired;
282089c905beSJ. Bruce Fields 
28212958d2eeSChuck Lever 	trace_nfsd_clid_admin_expired(&clp->cl_clientid);
28222958d2eeSChuck Lever 
2823f7104cc1SJ. Bruce Fields 	spin_lock(&nn->client_lock);
282489c905beSJ. Bruce Fields 	clp->cl_time = 0;
2825f7104cc1SJ. Bruce Fields 	spin_unlock(&nn->client_lock);
282689c905beSJ. Bruce Fields 
282789c905beSJ. Bruce Fields 	wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
282889c905beSJ. Bruce Fields 	spin_lock(&nn->client_lock);
282989c905beSJ. Bruce Fields 	already_expired = list_empty(&clp->cl_lru);
283089c905beSJ. Bruce Fields 	if (!already_expired)
283189c905beSJ. Bruce Fields 		unhash_client_locked(clp);
283289c905beSJ. Bruce Fields 	spin_unlock(&nn->client_lock);
283389c905beSJ. Bruce Fields 
283489c905beSJ. Bruce Fields 	if (!already_expired)
283589c905beSJ. Bruce Fields 		expire_client(clp);
283689c905beSJ. Bruce Fields 	else
283789c905beSJ. Bruce Fields 		wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
283889c905beSJ. Bruce Fields }
283989c905beSJ. Bruce Fields 
client_ctl_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)284089c905beSJ. Bruce Fields static ssize_t client_ctl_write(struct file *file, const char __user *buf,
284189c905beSJ. Bruce Fields 				   size_t size, loff_t *pos)
284289c905beSJ. Bruce Fields {
284389c905beSJ. Bruce Fields 	char *data;
284489c905beSJ. Bruce Fields 	struct nfs4_client *clp;
284589c905beSJ. Bruce Fields 
284689c905beSJ. Bruce Fields 	data = simple_transaction_get(file, buf, size);
284789c905beSJ. Bruce Fields 	if (IS_ERR(data))
284889c905beSJ. Bruce Fields 		return PTR_ERR(data);
284989c905beSJ. Bruce Fields 	if (size != 7 || 0 != memcmp(data, "expire\n", 7))
285089c905beSJ. Bruce Fields 		return -EINVAL;
285189c905beSJ. Bruce Fields 	clp = get_nfsdfs_clp(file_inode(file));
285289c905beSJ. Bruce Fields 	if (!clp)
285389c905beSJ. Bruce Fields 		return -ENXIO;
285489c905beSJ. Bruce Fields 	force_expire_client(clp);
285589c905beSJ. Bruce Fields 	drop_client(clp);
285689c905beSJ. Bruce Fields 	return 7;
285789c905beSJ. Bruce Fields }
285889c905beSJ. Bruce Fields 
285989c905beSJ. Bruce Fields static const struct file_operations client_ctl_fops = {
286089c905beSJ. Bruce Fields 	.write		= client_ctl_write,
286189c905beSJ. Bruce Fields 	.release	= simple_transaction_release,
286289c905beSJ. Bruce Fields };
286389c905beSJ. Bruce Fields 
286497ad4031SJ. Bruce Fields static const struct tree_descr client_files[] = {
286597ad4031SJ. Bruce Fields 	[0] = {"info", &client_info_fops, S_IRUSR},
286678599c42SJ. Bruce Fields 	[1] = {"states", &client_states_fops, S_IRUSR},
28676cbfad5fSPetr Vorel 	[2] = {"ctl", &client_ctl_fops, S_IWUSR},
286878599c42SJ. Bruce Fields 	[3] = {""},
286997ad4031SJ. Bruce Fields };
287097ad4031SJ. Bruce Fields 
287144df6f43SDai Ngo static int
nfsd4_cb_recall_any_done(struct nfsd4_callback * cb,struct rpc_task * task)287244df6f43SDai Ngo nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
287344df6f43SDai Ngo 				struct rpc_task *task)
287444df6f43SDai Ngo {
2875638593beSDai Ngo 	trace_nfsd_cb_recall_any_done(cb, task);
287644df6f43SDai Ngo 	switch (task->tk_status) {
287744df6f43SDai Ngo 	case -NFS4ERR_DELAY:
287844df6f43SDai Ngo 		rpc_delay(task, 2 * HZ);
287944df6f43SDai Ngo 		return 0;
288044df6f43SDai Ngo 	default:
288144df6f43SDai Ngo 		return 1;
288244df6f43SDai Ngo 	}
288344df6f43SDai Ngo }
288444df6f43SDai Ngo 
288544df6f43SDai Ngo static void
nfsd4_cb_recall_any_release(struct nfsd4_callback * cb)288644df6f43SDai Ngo nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
288744df6f43SDai Ngo {
288844df6f43SDai Ngo 	struct nfs4_client *clp = cb->cb_clp;
288944df6f43SDai Ngo 
289044df6f43SDai Ngo 	clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
2891*9d60e8ecSJeff Layton 	drop_client(clp);
289244df6f43SDai Ngo }
289344df6f43SDai Ngo 
289444df6f43SDai Ngo static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
289544df6f43SDai Ngo 	.done		= nfsd4_cb_recall_any_done,
289644df6f43SDai Ngo 	.release	= nfsd4_cb_recall_any_release,
289744df6f43SDai Ngo };
289844df6f43SDai Ngo 
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)28992216d449SJeff Layton static struct nfs4_client *create_client(struct xdr_netobj name,
2900b09333c4SRicardo Labiaga 		struct svc_rqst *rqstp, nfs4_verifier *verf)
2901b09333c4SRicardo Labiaga {
2902b09333c4SRicardo Labiaga 	struct nfs4_client *clp;
2903b09333c4SRicardo Labiaga 	struct sockaddr *sa = svc_addr(rqstp);
290403a4e1f6SJ. Bruce Fields 	int ret;
2905c212cecfSStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
2906e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2907472d155aSNeilBrown 	struct dentry *dentries[ARRAY_SIZE(client_files)];
2908b09333c4SRicardo Labiaga 
29090926c395SDai Ngo 	clp = alloc_client(name, nn);
2910b09333c4SRicardo Labiaga 	if (clp == NULL)
2911b09333c4SRicardo Labiaga 		return NULL;
2912b09333c4SRicardo Labiaga 
291303a4e1f6SJ. Bruce Fields 	ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
291403a4e1f6SJ. Bruce Fields 	if (ret) {
2915b09333c4SRicardo Labiaga 		free_client(clp);
2916b09333c4SRicardo Labiaga 		return NULL;
2917b09333c4SRicardo Labiaga 	}
2918e8a79fb1SJ. Bruce Fields 	gen_clid(clp, nn);
2919e8a79fb1SJ. Bruce Fields 	kref_init(&clp->cl_nfsdfs.cl_ref);
29200162ac2bSChristoph Hellwig 	nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
292120b7d86fSArnd Bergmann 	clp->cl_time = ktime_get_boottime_seconds();
2922b09333c4SRicardo Labiaga 	clear_bit(0, &clp->cl_cb_slot_busy);
2923b09333c4SRicardo Labiaga 	copy_verf(clp, verf);
29243bade247SJ. Bruce Fields 	memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2925edd76786SJ. Bruce Fields 	clp->cl_cb_session = NULL;
2926c212cecfSStanislav Kinsbursky 	clp->net = net;
2927472d155aSNeilBrown 	clp->cl_nfsd_dentry = nfsd_client_mkdir(
2928472d155aSNeilBrown 		nn, &clp->cl_nfsdfs,
292997ad4031SJ. Bruce Fields 		clp->cl_clientid.cl_id - nn->clientid_base,
2930472d155aSNeilBrown 		client_files, dentries);
2931472d155aSNeilBrown 	clp->cl_nfsd_info_dentry = dentries[0];
2932e8a79fb1SJ. Bruce Fields 	if (!clp->cl_nfsd_dentry) {
2933e8a79fb1SJ. Bruce Fields 		free_client(clp);
2934e8a79fb1SJ. Bruce Fields 		return NULL;
2935e8a79fb1SJ. Bruce Fields 	}
293644df6f43SDai Ngo 	clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
293744df6f43SDai Ngo 	if (!clp->cl_ra) {
293844df6f43SDai Ngo 		free_client(clp);
293944df6f43SDai Ngo 		return NULL;
294044df6f43SDai Ngo 	}
294144df6f43SDai Ngo 	clp->cl_ra_time = 0;
294244df6f43SDai Ngo 	nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
294344df6f43SDai Ngo 			NFSPROC4_CLNT_CB_RECALL_ANY);
2944b09333c4SRicardo Labiaga 	return clp;
2945b09333c4SRicardo Labiaga }
2946b09333c4SRicardo Labiaga 
2947fd39ca9aSNeilBrown static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)2948ac55fdc4SJeff Layton add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2949ac55fdc4SJeff Layton {
2950ac55fdc4SJeff Layton 	struct rb_node **new = &(root->rb_node), *parent = NULL;
2951ac55fdc4SJeff Layton 	struct nfs4_client *clp;
2952ac55fdc4SJeff Layton 
2953ac55fdc4SJeff Layton 	while (*new) {
2954ac55fdc4SJeff Layton 		clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2955ac55fdc4SJeff Layton 		parent = *new;
2956ac55fdc4SJeff Layton 
2957ac55fdc4SJeff Layton 		if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2958ac55fdc4SJeff Layton 			new = &((*new)->rb_left);
2959ac55fdc4SJeff Layton 		else
2960ac55fdc4SJeff Layton 			new = &((*new)->rb_right);
2961ac55fdc4SJeff Layton 	}
2962ac55fdc4SJeff Layton 
2963ac55fdc4SJeff Layton 	rb_link_node(&new_clp->cl_namenode, parent, new);
2964ac55fdc4SJeff Layton 	rb_insert_color(&new_clp->cl_namenode, root);
2965ac55fdc4SJeff Layton }
2966ac55fdc4SJeff Layton 
2967ac55fdc4SJeff Layton static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)2968ac55fdc4SJeff Layton find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2969ac55fdc4SJeff Layton {
2970ef17af2aSRasmus Villemoes 	int cmp;
2971ac55fdc4SJeff Layton 	struct rb_node *node = root->rb_node;
2972ac55fdc4SJeff Layton 	struct nfs4_client *clp;
2973ac55fdc4SJeff Layton 
2974ac55fdc4SJeff Layton 	while (node) {
2975ac55fdc4SJeff Layton 		clp = rb_entry(node, struct nfs4_client, cl_namenode);
2976ac55fdc4SJeff Layton 		cmp = compare_blob(&clp->cl_name, name);
2977ac55fdc4SJeff Layton 		if (cmp > 0)
2978ac55fdc4SJeff Layton 			node = node->rb_left;
2979ac55fdc4SJeff Layton 		else if (cmp < 0)
2980ac55fdc4SJeff Layton 			node = node->rb_right;
2981ac55fdc4SJeff Layton 		else
2982ac55fdc4SJeff Layton 			return clp;
2983ac55fdc4SJeff Layton 	}
2984ac55fdc4SJeff Layton 	return NULL;
2985ac55fdc4SJeff Layton }
2986ac55fdc4SJeff Layton 
2987ac55fdc4SJeff Layton static void
add_to_unconfirmed(struct nfs4_client * clp)2988ac55fdc4SJeff Layton add_to_unconfirmed(struct nfs4_client *clp)
29891da177e4SLinus Torvalds {
29901da177e4SLinus Torvalds 	unsigned int idhashval;
29910a7ec377SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
29921da177e4SLinus Torvalds 
29930a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
29940a880a28STrond Myklebust 
2995ac55fdc4SJeff Layton 	clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2996a99454aaSStanislav Kinsbursky 	add_clp_to_name_tree(clp, &nn->unconf_name_tree);
29971da177e4SLinus Torvalds 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
29980a7ec377SStanislav Kinsbursky 	list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
29993dbacee6STrond Myklebust 	renew_client_locked(clp);
30001da177e4SLinus Torvalds }
30011da177e4SLinus Torvalds 
3002fd39ca9aSNeilBrown static void
move_to_confirmed(struct nfs4_client * clp)30031da177e4SLinus Torvalds move_to_confirmed(struct nfs4_client *clp)
30041da177e4SLinus Torvalds {
30051da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
30068daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
30071da177e4SLinus Torvalds 
30080a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
30090a880a28STrond Myklebust 
30108daae4dcSStanislav Kinsbursky 	list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
3011a99454aaSStanislav Kinsbursky 	rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
3012382a62e7SStanislav Kinsbursky 	add_clp_to_name_tree(clp, &nn->conf_name_tree);
3013934bd07fSJ. Bruce Fields 	set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
30147e3b32acSChuck Lever 	trace_nfsd_clid_confirmed(&clp->cl_clientid);
30153dbacee6STrond Myklebust 	renew_client_locked(clp);
30161da177e4SLinus Torvalds }
30171da177e4SLinus Torvalds 
30181da177e4SLinus Torvalds static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)3019bfa85e83SJ. Bruce Fields find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
30201da177e4SLinus Torvalds {
30211da177e4SLinus Torvalds 	struct nfs4_client *clp;
30221da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clid->cl_id);
30231da177e4SLinus Torvalds 
3024bfa85e83SJ. Bruce Fields 	list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
3025a50d2ad1SJ. Bruce Fields 		if (same_clid(&clp->cl_clientid, clid)) {
3026d15c077eSJ. Bruce Fields 			if ((bool)clp->cl_minorversion != sessions)
3027d15c077eSJ. Bruce Fields 				return NULL;
30283dbacee6STrond Myklebust 			renew_client_locked(clp);
30291da177e4SLinus Torvalds 			return clp;
30301da177e4SLinus Torvalds 		}
3031a50d2ad1SJ. Bruce Fields 	}
30321da177e4SLinus Torvalds 	return NULL;
30331da177e4SLinus Torvalds }
30341da177e4SLinus Torvalds 
30351da177e4SLinus Torvalds static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)3036bfa85e83SJ. Bruce Fields find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
3037bfa85e83SJ. Bruce Fields {
3038bfa85e83SJ. Bruce Fields 	struct list_head *tbl = nn->conf_id_hashtbl;
3039bfa85e83SJ. Bruce Fields 
30400a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
3041bfa85e83SJ. Bruce Fields 	return find_client_in_id_table(tbl, clid, sessions);
3042bfa85e83SJ. Bruce Fields }
3043bfa85e83SJ. Bruce Fields 
3044bfa85e83SJ. Bruce Fields static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)30450a7ec377SStanislav Kinsbursky find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
30461da177e4SLinus Torvalds {
3047bfa85e83SJ. Bruce Fields 	struct list_head *tbl = nn->unconf_id_hashtbl;
30481da177e4SLinus Torvalds 
30490a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
3050bfa85e83SJ. Bruce Fields 	return find_client_in_id_table(tbl, clid, sessions);
30511da177e4SLinus Torvalds }
30521da177e4SLinus Torvalds 
clp_used_exchangeid(struct nfs4_client * clp)30536e5f15c9SJ. Bruce Fields static bool clp_used_exchangeid(struct nfs4_client *clp)
3054a1bcecd2SAndy Adamson {
30556e5f15c9SJ. Bruce Fields 	return clp->cl_exchange_flags != 0;
3056a1bcecd2SAndy Adamson }
3057a1bcecd2SAndy Adamson 
305828ce6054SNeilBrown static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3059382a62e7SStanislav Kinsbursky find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
306028ce6054SNeilBrown {
30610a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
3062382a62e7SStanislav Kinsbursky 	return find_clp_in_name_tree(name, &nn->conf_name_tree);
306328ce6054SNeilBrown }
306428ce6054SNeilBrown 
306528ce6054SNeilBrown static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)3066a99454aaSStanislav Kinsbursky find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
306728ce6054SNeilBrown {
30680a880a28STrond Myklebust 	lockdep_assert_held(&nn->client_lock);
3069a99454aaSStanislav Kinsbursky 	return find_clp_in_name_tree(name, &nn->unconf_name_tree);
307028ce6054SNeilBrown }
307128ce6054SNeilBrown 
3072fd39ca9aSNeilBrown static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)30736f3d772fSTakuma Umeya gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
30741da177e4SLinus Torvalds {
307507263f1eSJ. Bruce Fields 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
30766f3d772fSTakuma Umeya 	struct sockaddr	*sa = svc_addr(rqstp);
30776f3d772fSTakuma Umeya 	u32 scopeid = rpc_get_scope_id(sa);
30787077ecbaSJeff Layton 	unsigned short expected_family;
30791da177e4SLinus Torvalds 
30807077ecbaSJeff Layton 	/* Currently, we only support tcp and tcp6 for the callback channel */
30817077ecbaSJeff Layton 	if (se->se_callback_netid_len == 3 &&
30827077ecbaSJeff Layton 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
30837077ecbaSJeff Layton 		expected_family = AF_INET;
30847077ecbaSJeff Layton 	else if (se->se_callback_netid_len == 4 &&
30857077ecbaSJeff Layton 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
30867077ecbaSJeff Layton 		expected_family = AF_INET6;
30877077ecbaSJeff Layton 	else
30881da177e4SLinus Torvalds 		goto out_err;
30891da177e4SLinus Torvalds 
3090c212cecfSStanislav Kinsbursky 	conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
3091aa9a4ec7SJeff Layton 					    se->se_callback_addr_len,
309207263f1eSJ. Bruce Fields 					    (struct sockaddr *)&conn->cb_addr,
309307263f1eSJ. Bruce Fields 					    sizeof(conn->cb_addr));
3094aa9a4ec7SJeff Layton 
309507263f1eSJ. Bruce Fields 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
30961da177e4SLinus Torvalds 		goto out_err;
3097aa9a4ec7SJeff Layton 
309807263f1eSJ. Bruce Fields 	if (conn->cb_addr.ss_family == AF_INET6)
309907263f1eSJ. Bruce Fields 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
3100fbf4665fSJeff Layton 
310107263f1eSJ. Bruce Fields 	conn->cb_prog = se->se_callback_prog;
310207263f1eSJ. Bruce Fields 	conn->cb_ident = se->se_callback_ident;
3103849a1cf1SMi Jinlong 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
31041eace0d1SChuck Lever 	trace_nfsd_cb_args(clp, conn);
31051da177e4SLinus Torvalds 	return;
31061da177e4SLinus Torvalds out_err:
310707263f1eSJ. Bruce Fields 	conn->cb_addr.ss_family = AF_UNSPEC;
310807263f1eSJ. Bruce Fields 	conn->cb_addrlen = 0;
31091eace0d1SChuck Lever 	trace_nfsd_cb_nodelegs(clp);
31101da177e4SLinus Torvalds 	return;
31111da177e4SLinus Torvalds }
31121da177e4SLinus Torvalds 
3113074fe897SAndy Adamson /*
3114067e1aceSJ. Bruce Fields  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
3115074fe897SAndy Adamson  */
3116b607664eSTrond Myklebust static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)3117074fe897SAndy Adamson nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
3118074fe897SAndy Adamson {
3119bddfdbcdSChuck Lever 	struct xdr_buf *buf = resp->xdr->buf;
3120557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
3121557ce264SAndy Adamson 	unsigned int base;
3122074fe897SAndy Adamson 
3123557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
3124074fe897SAndy Adamson 
3125085def3aSJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
3126557ce264SAndy Adamson 	slot->sl_opcnt = resp->opcnt;
3127557ce264SAndy Adamson 	slot->sl_status = resp->cstate.status;
312853da6a53SJ. Bruce Fields 	free_svc_cred(&slot->sl_cred);
312953da6a53SJ. Bruce Fields 	copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
3130bf864a31SAndy Adamson 
3131085def3aSJ. Bruce Fields 	if (!nfsd4_cache_this(resp)) {
3132085def3aSJ. Bruce Fields 		slot->sl_flags &= ~NFSD4_SLOT_CACHED;
3133bf864a31SAndy Adamson 		return;
3134bf864a31SAndy Adamson 	}
3135085def3aSJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_CACHED;
3136085def3aSJ. Bruce Fields 
3137f5236013SJ. Bruce Fields 	base = resp->cstate.data_offset;
3138f5236013SJ. Bruce Fields 	slot->sl_datalen = buf->len - base;
3139f5236013SJ. Bruce Fields 	if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
3140d3f03403SDan Carpenter 		WARN(1, "%s: sessions DRC could not cache compound\n",
3141d3f03403SDan Carpenter 		     __func__);
3142557ce264SAndy Adamson 	return;
3143074fe897SAndy Adamson }
3144074fe897SAndy Adamson 
3145074fe897SAndy Adamson /*
3146abfabf8cSAndy Adamson  * Encode the replay sequence operation from the slot values.
3147abfabf8cSAndy Adamson  * If cachethis is FALSE encode the uncached rep error on the next
3148abfabf8cSAndy Adamson  * operation which sets resp->p and increments resp->opcnt for
3149abfabf8cSAndy Adamson  * nfs4svc_encode_compoundres.
3150abfabf8cSAndy Adamson  *
3151074fe897SAndy Adamson  */
3152abfabf8cSAndy Adamson static __be32
nfsd4_enc_sequence_replay(struct nfsd4_compoundargs * args,struct nfsd4_compoundres * resp)3153abfabf8cSAndy Adamson nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
3154abfabf8cSAndy Adamson 			  struct nfsd4_compoundres *resp)
3155074fe897SAndy Adamson {
3156abfabf8cSAndy Adamson 	struct nfsd4_op *op;
3157abfabf8cSAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
3158074fe897SAndy Adamson 
3159abfabf8cSAndy Adamson 	/* Encode the replayed sequence operation */
3160abfabf8cSAndy Adamson 	op = &args->ops[resp->opcnt - 1];
3161abfabf8cSAndy Adamson 	nfsd4_encode_operation(resp, op);
3162abfabf8cSAndy Adamson 
3163085def3aSJ. Bruce Fields 	if (slot->sl_flags & NFSD4_SLOT_CACHED)
3164085def3aSJ. Bruce Fields 		return op->status;
3165085def3aSJ. Bruce Fields 	if (args->opcnt == 1) {
3166085def3aSJ. Bruce Fields 		/*
3167085def3aSJ. Bruce Fields 		 * The original operation wasn't a solo sequence--we
3168085def3aSJ. Bruce Fields 		 * always cache those--so this retry must not match the
3169085def3aSJ. Bruce Fields 		 * original:
3170085def3aSJ. Bruce Fields 		 */
3171085def3aSJ. Bruce Fields 		op->status = nfserr_seq_false_retry;
3172085def3aSJ. Bruce Fields 	} else {
3173abfabf8cSAndy Adamson 		op = &args->ops[resp->opcnt++];
3174abfabf8cSAndy Adamson 		op->status = nfserr_retry_uncached_rep;
3175abfabf8cSAndy Adamson 		nfsd4_encode_operation(resp, op);
3176074fe897SAndy Adamson 	}
3177abfabf8cSAndy Adamson 	return op->status;
3178074fe897SAndy Adamson }
3179074fe897SAndy Adamson 
3180074fe897SAndy Adamson /*
3181557ce264SAndy Adamson  * The sequence operation is not cached because we can use the slot and
3182557ce264SAndy Adamson  * session values.
3183074fe897SAndy Adamson  */
31843ca2eb98SJ. Bruce Fields static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)3185bf864a31SAndy Adamson nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
3186bf864a31SAndy Adamson 			 struct nfsd4_sequence *seq)
3187074fe897SAndy Adamson {
3188557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
3189bddfdbcdSChuck Lever 	struct xdr_stream *xdr = resp->xdr;
3190f5236013SJ. Bruce Fields 	__be32 *p;
3191074fe897SAndy Adamson 	__be32 status;
3192074fe897SAndy Adamson 
3193557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
3194074fe897SAndy Adamson 
3195abfabf8cSAndy Adamson 	status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
31960da7b19cSJ. Bruce Fields 	if (status)
3197abfabf8cSAndy Adamson 		return status;
3198074fe897SAndy Adamson 
3199f5236013SJ. Bruce Fields 	p = xdr_reserve_space(xdr, slot->sl_datalen);
3200f5236013SJ. Bruce Fields 	if (!p) {
3201f5236013SJ. Bruce Fields 		WARN_ON_ONCE(1);
3202f5236013SJ. Bruce Fields 		return nfserr_serverfault;
3203f5236013SJ. Bruce Fields 	}
3204f5236013SJ. Bruce Fields 	xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
3205f5236013SJ. Bruce Fields 	xdr_commit_encode(xdr);
3206074fe897SAndy Adamson 
3207557ce264SAndy Adamson 	resp->opcnt = slot->sl_opcnt;
3208f5236013SJ. Bruce Fields 	return slot->sl_status;
3209074fe897SAndy Adamson }
3210074fe897SAndy Adamson 
32110733d213SAndy Adamson /*
32120733d213SAndy Adamson  * Set the exchange_id flags returned by the server.
32130733d213SAndy Adamson  */
32140733d213SAndy Adamson static void
nfsd4_set_ex_flags(struct nfs4_client * new,struct nfsd4_exchange_id * clid)32150733d213SAndy Adamson nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
32160733d213SAndy Adamson {
32179cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
32189cf514ccSChristoph Hellwig 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
32199cf514ccSChristoph Hellwig #else
32200733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
32219cf514ccSChristoph Hellwig #endif
32220733d213SAndy Adamson 
32230733d213SAndy Adamson 	/* Referrals are supported, Migration is not. */
32240733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
32250733d213SAndy Adamson 
32260733d213SAndy Adamson 	/* set the wire flags to return to client. */
32270733d213SAndy Adamson 	clid->flags = new->cl_exchange_flags;
32280733d213SAndy Adamson }
32290733d213SAndy Adamson 
client_has_openowners(struct nfs4_client * clp)32304eaea134SJ. Bruce Fields static bool client_has_openowners(struct nfs4_client *clp)
32314eaea134SJ. Bruce Fields {
32324eaea134SJ. Bruce Fields 	struct nfs4_openowner *oo;
32334eaea134SJ. Bruce Fields 
32344eaea134SJ. Bruce Fields 	list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
32354eaea134SJ. Bruce Fields 		if (!list_empty(&oo->oo_owner.so_stateids))
32364eaea134SJ. Bruce Fields 			return true;
32374eaea134SJ. Bruce Fields 	}
32384eaea134SJ. Bruce Fields 	return false;
32394eaea134SJ. Bruce Fields }
32404eaea134SJ. Bruce Fields 
client_has_state(struct nfs4_client * clp)3241631fc9eaSJ. Bruce Fields static bool client_has_state(struct nfs4_client *clp)
3242631fc9eaSJ. Bruce Fields {
32434eaea134SJ. Bruce Fields 	return client_has_openowners(clp)
324447e970beSKinglong Mee #ifdef CONFIG_NFSD_PNFS
324547e970beSKinglong Mee 		|| !list_empty(&clp->cl_lo_states)
324647e970beSKinglong Mee #endif
32476eccece9SJ. Bruce Fields 		|| !list_empty(&clp->cl_delegations)
3248e0639dc5SOlga Kornievskaia 		|| !list_empty(&clp->cl_sessions)
3249e0639dc5SOlga Kornievskaia 		|| !list_empty(&clp->async_copies);
3250631fc9eaSJ. Bruce Fields }
3251631fc9eaSJ. Bruce Fields 
copy_impl_id(struct nfs4_client * clp,struct nfsd4_exchange_id * exid)325279123444SJ. Bruce Fields static __be32 copy_impl_id(struct nfs4_client *clp,
325379123444SJ. Bruce Fields 				struct nfsd4_exchange_id *exid)
325479123444SJ. Bruce Fields {
325579123444SJ. Bruce Fields 	if (!exid->nii_domain.data)
325679123444SJ. Bruce Fields 		return 0;
325779123444SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
325879123444SJ. Bruce Fields 	if (!clp->cl_nii_domain.data)
325979123444SJ. Bruce Fields 		return nfserr_jukebox;
326079123444SJ. Bruce Fields 	xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
326179123444SJ. Bruce Fields 	if (!clp->cl_nii_name.data)
326279123444SJ. Bruce Fields 		return nfserr_jukebox;
3263e29f4703SArnd Bergmann 	clp->cl_nii_time = exid->nii_time;
326479123444SJ. Bruce Fields 	return 0;
326579123444SJ. Bruce Fields }
326679123444SJ. Bruce Fields 
3267b37ad28bSAl Viro __be32
nfsd4_exchange_id(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3268eb69853dSChristoph Hellwig nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3269eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3270069b6ad4SAndy Adamson {
3271eb69853dSChristoph Hellwig 	struct nfsd4_exchange_id *exid = &u->exchange_id;
32723dbacee6STrond Myklebust 	struct nfs4_client *conf, *new;
32733dbacee6STrond Myklebust 	struct nfs4_client *unconf = NULL;
327457b7b43bSJ. Bruce Fields 	__be32 status;
3275363168b4SJeff Layton 	char			addr_str[INET6_ADDRSTRLEN];
32760733d213SAndy Adamson 	nfs4_verifier		verf = exid->verifier;
3277363168b4SJeff Layton 	struct sockaddr		*sa = svc_addr(rqstp);
327883e08fd4SJ. Bruce Fields 	bool	update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3279c212cecfSStanislav Kinsbursky 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
32800733d213SAndy Adamson 
3281363168b4SJeff Layton 	rpc_ntop(sa, addr_str, sizeof(addr_str));
32820733d213SAndy Adamson 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3283523ec6edSChuck Lever 		"ip_addr=%s flags %x, spa_how %u\n",
32840733d213SAndy Adamson 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
3285363168b4SJeff Layton 		addr_str, exid->flags, exid->spa_how);
32860733d213SAndy Adamson 
3287a084daf5SJ. Bruce Fields 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
32880733d213SAndy Adamson 		return nfserr_inval;
32890733d213SAndy Adamson 
329050c7b948SJ. Bruce Fields 	new = create_client(exid->clname, rqstp, &verf);
329150c7b948SJ. Bruce Fields 	if (new == NULL)
329250c7b948SJ. Bruce Fields 		return nfserr_jukebox;
329379123444SJ. Bruce Fields 	status = copy_impl_id(new, exid);
329479123444SJ. Bruce Fields 	if (status)
329579123444SJ. Bruce Fields 		goto out_nolock;
329650c7b948SJ. Bruce Fields 
32970733d213SAndy Adamson 	switch (exid->spa_how) {
329857266a6eSJ. Bruce Fields 	case SP4_MACH_CRED:
3299ed941643SAndrew Elble 		exid->spo_must_enforce[0] = 0;
3300ed941643SAndrew Elble 		exid->spo_must_enforce[1] = (
3301ed941643SAndrew Elble 			1 << (OP_BIND_CONN_TO_SESSION - 32) |
3302ed941643SAndrew Elble 			1 << (OP_EXCHANGE_ID - 32) |
3303ed941643SAndrew Elble 			1 << (OP_CREATE_SESSION - 32) |
3304ed941643SAndrew Elble 			1 << (OP_DESTROY_SESSION - 32) |
3305ed941643SAndrew Elble 			1 << (OP_DESTROY_CLIENTID - 32));
3306ed941643SAndrew Elble 
3307ed941643SAndrew Elble 		exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3308ed941643SAndrew Elble 					1 << (OP_OPEN_DOWNGRADE) |
3309ed941643SAndrew Elble 					1 << (OP_LOCKU) |
3310ed941643SAndrew Elble 					1 << (OP_DELEGRETURN));
3311ed941643SAndrew Elble 
3312ed941643SAndrew Elble 		exid->spo_must_allow[1] &= (
3313ed941643SAndrew Elble 					1 << (OP_TEST_STATEID - 32) |
3314ed941643SAndrew Elble 					1 << (OP_FREE_STATEID - 32));
331550c7b948SJ. Bruce Fields 		if (!svc_rqst_integrity_protected(rqstp)) {
331650c7b948SJ. Bruce Fields 			status = nfserr_inval;
331750c7b948SJ. Bruce Fields 			goto out_nolock;
331850c7b948SJ. Bruce Fields 		}
3319920dd9bbSJ. Bruce Fields 		/*
3320920dd9bbSJ. Bruce Fields 		 * Sometimes userspace doesn't give us a principal.
3321920dd9bbSJ. Bruce Fields 		 * Which is a bug, really.  Anyway, we can't enforce
3322920dd9bbSJ. Bruce Fields 		 * MACH_CRED in that case, better to give up now:
3323920dd9bbSJ. Bruce Fields 		 */
3324414ca017SJ. Bruce Fields 		if (!new->cl_cred.cr_principal &&
3325414ca017SJ. Bruce Fields 					!new->cl_cred.cr_raw_principal) {
3326920dd9bbSJ. Bruce Fields 			status = nfserr_serverfault;
3327920dd9bbSJ. Bruce Fields 			goto out_nolock;
3328920dd9bbSJ. Bruce Fields 		}
332950c7b948SJ. Bruce Fields 		new->cl_mach_cred = true;
333076c50eb7SGustavo A. R. Silva 		break;
33310733d213SAndy Adamson 	case SP4_NONE:
33320733d213SAndy Adamson 		break;
3333063b0fb9SJ. Bruce Fields 	default:				/* checked by xdr code */
3334063b0fb9SJ. Bruce Fields 		WARN_ON_ONCE(1);
3335df561f66SGustavo A. R. Silva 		fallthrough;
33360733d213SAndy Adamson 	case SP4_SSV:
33378edf4b02SKinglong Mee 		status = nfserr_encr_alg_unsupp;
33388edf4b02SKinglong Mee 		goto out_nolock;
33390733d213SAndy Adamson 	}
33400733d213SAndy Adamson 
33412dbb269dSJ. Bruce Fields 	/* Cases below refer to rfc 5661 section 18.35.4: */
33423dbacee6STrond Myklebust 	spin_lock(&nn->client_lock);
3343382a62e7SStanislav Kinsbursky 	conf = find_confirmed_client_by_name(&exid->clname, nn);
33440733d213SAndy Adamson 	if (conf) {
334583e08fd4SJ. Bruce Fields 		bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
334683e08fd4SJ. Bruce Fields 		bool verfs_match = same_verf(&verf, &conf->cl_verifier);
334783e08fd4SJ. Bruce Fields 
3348136e658dSJ. Bruce Fields 		if (update) {
3349136e658dSJ. Bruce Fields 			if (!clp_used_exchangeid(conf)) { /* buggy client */
33502dbb269dSJ. Bruce Fields 				status = nfserr_inval;
3351e203d506SJ. Bruce Fields 				goto out;
3352e203d506SJ. Bruce Fields 			}
3353dedeb13fSAndrew Elble 			if (!nfsd4_mach_creds_match(conf, rqstp)) {
335457266a6eSJ. Bruce Fields 				status = nfserr_wrong_cred;
335557266a6eSJ. Bruce Fields 				goto out;
335657266a6eSJ. Bruce Fields 			}
33572dbb269dSJ. Bruce Fields 			if (!creds_match) { /* case 9 */
33580733d213SAndy Adamson 				status = nfserr_perm;
33590733d213SAndy Adamson 				goto out;
33600733d213SAndy Adamson 			}
33612dbb269dSJ. Bruce Fields 			if (!verfs_match) { /* case 8 */
33620733d213SAndy Adamson 				status = nfserr_not_same;
33630733d213SAndy Adamson 				goto out;
33640733d213SAndy Adamson 			}
3365136e658dSJ. Bruce Fields 			/* case 6 */
33660733d213SAndy Adamson 			exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3367e8f80c55SChuck Lever 			trace_nfsd_clid_confirmed_r(conf);
33680733d213SAndy Adamson 			goto out_copy;
33696ddbbbfeSMike Sager 		}
3370136e658dSJ. Bruce Fields 		if (!creds_match) { /* case 3 */
3371631fc9eaSJ. Bruce Fields 			if (client_has_state(conf)) {
3372136e658dSJ. Bruce Fields 				status = nfserr_clid_inuse;
337327787733SChuck Lever 				trace_nfsd_clid_cred_mismatch(conf, rqstp);
3374136e658dSJ. Bruce Fields 				goto out;
3375136e658dSJ. Bruce Fields 			}
3376b9831b59SJ. Bruce Fields 			goto out_new;
3377631fc9eaSJ. Bruce Fields 		}
3378136e658dSJ. Bruce Fields 		if (verfs_match) { /* case 2 */
33790f1ba0efSJ. Bruce Fields 			conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3380e8f80c55SChuck Lever 			trace_nfsd_clid_confirmed_r(conf);
3381136e658dSJ. Bruce Fields 			goto out_copy;
3382136e658dSJ. Bruce Fields 		}
33832dbb269dSJ. Bruce Fields 		/* case 5, client reboot */
3384744ea54cSChuck Lever 		trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
33853dbacee6STrond Myklebust 		conf = NULL;
33860733d213SAndy Adamson 		goto out_new;
33870733d213SAndy Adamson 	}
33886ddbbbfeSMike Sager 
33892dbb269dSJ. Bruce Fields 	if (update) { /* case 7 */
33900733d213SAndy Adamson 		status = nfserr_noent;
33910733d213SAndy Adamson 		goto out;
33920733d213SAndy Adamson 	}
33930733d213SAndy Adamson 
3394a99454aaSStanislav Kinsbursky 	unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
33952dbb269dSJ. Bruce Fields 	if (unconf) /* case 4, possible retry or client restart */
33963dbacee6STrond Myklebust 		unhash_client_locked(unconf);
33970733d213SAndy Adamson 
3398e8f80c55SChuck Lever 	/* case 1, new owner ID */
3399e8f80c55SChuck Lever 	trace_nfsd_clid_fresh(new);
3400e8f80c55SChuck Lever 
34010733d213SAndy Adamson out_new:
3402fd699b8aSJeff Layton 	if (conf) {
3403fd699b8aSJeff Layton 		status = mark_client_expired_locked(conf);
3404fd699b8aSJeff Layton 		if (status)
3405fd699b8aSJeff Layton 			goto out;
34062958d2eeSChuck Lever 		trace_nfsd_clid_replaced(&conf->cl_clientid);
3407fd699b8aSJeff Layton 	}
34084f540e29SJ. Bruce Fields 	new->cl_minorversion = cstate->minorversion;
3409ed941643SAndrew Elble 	new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3410ed941643SAndrew Elble 	new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
34110733d213SAndy Adamson 
3412ac55fdc4SJeff Layton 	add_to_unconfirmed(new);
34133dbacee6STrond Myklebust 	swap(new, conf);
34140733d213SAndy Adamson out_copy:
34155cc40fd7STrond Myklebust 	exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
34165cc40fd7STrond Myklebust 	exid->clientid.cl_id = conf->cl_clientid.cl_id;
34170733d213SAndy Adamson 
34185cc40fd7STrond Myklebust 	exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
34195cc40fd7STrond Myklebust 	nfsd4_set_ex_flags(conf, exid);
34200733d213SAndy Adamson 
34210733d213SAndy Adamson 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
34225cc40fd7STrond Myklebust 		conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
34230733d213SAndy Adamson 	status = nfs_ok;
34240733d213SAndy Adamson 
34250733d213SAndy Adamson out:
34263dbacee6STrond Myklebust 	spin_unlock(&nn->client_lock);
342750c7b948SJ. Bruce Fields out_nolock:
34285cc40fd7STrond Myklebust 	if (new)
34293dbacee6STrond Myklebust 		expire_client(new);
3430e8f80c55SChuck Lever 	if (unconf) {
3431e8f80c55SChuck Lever 		trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
34323dbacee6STrond Myklebust 		expire_client(unconf);
3433e8f80c55SChuck Lever 	}
34340733d213SAndy Adamson 	return status;
3435069b6ad4SAndy Adamson }
3436069b6ad4SAndy Adamson 
343757b7b43bSJ. Bruce Fields static __be32
check_slot_seqid(u32 seqid,u32 slot_seqid,int slot_inuse)343888e588d5SAndy Adamson check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3439b85d4c01SBenny Halevy {
344088e588d5SAndy Adamson 	dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
344188e588d5SAndy Adamson 		slot_seqid);
3442b85d4c01SBenny Halevy 
3443b85d4c01SBenny Halevy 	/* The slot is in use, and no response has been sent. */
344488e588d5SAndy Adamson 	if (slot_inuse) {
344588e588d5SAndy Adamson 		if (seqid == slot_seqid)
3446b85d4c01SBenny Halevy 			return nfserr_jukebox;
3447b85d4c01SBenny Halevy 		else
3448b85d4c01SBenny Halevy 			return nfserr_seq_misordered;
3449b85d4c01SBenny Halevy 	}
3450f6d82485SJ. Bruce Fields 	/* Note unsigned 32-bit arithmetic handles wraparound: */
345188e588d5SAndy Adamson 	if (likely(seqid == slot_seqid + 1))
3452b85d4c01SBenny Halevy 		return nfs_ok;
345388e588d5SAndy Adamson 	if (seqid == slot_seqid)
3454b85d4c01SBenny Halevy 		return nfserr_replay_cache;
3455b85d4c01SBenny Halevy 	return nfserr_seq_misordered;
3456b85d4c01SBenny Halevy }
3457b85d4c01SBenny Halevy 
345849557cc7SAndy Adamson /*
345949557cc7SAndy Adamson  * Cache the create session result into the create session single DRC
346049557cc7SAndy Adamson  * slot cache by saving the xdr structure. sl_seqid has been set.
346149557cc7SAndy Adamson  * Do this for solo or embedded create session operations.
346249557cc7SAndy Adamson  */
346349557cc7SAndy Adamson static void
nfsd4_cache_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot,__be32 nfserr)346449557cc7SAndy Adamson nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
346557b7b43bSJ. Bruce Fields 			   struct nfsd4_clid_slot *slot, __be32 nfserr)
346649557cc7SAndy Adamson {
346749557cc7SAndy Adamson 	slot->sl_status = nfserr;
346849557cc7SAndy Adamson 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
346949557cc7SAndy Adamson }
347049557cc7SAndy Adamson 
347149557cc7SAndy Adamson static __be32
nfsd4_replay_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot)347249557cc7SAndy Adamson nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
347349557cc7SAndy Adamson 			    struct nfsd4_clid_slot *slot)
347449557cc7SAndy Adamson {
347549557cc7SAndy Adamson 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
347649557cc7SAndy Adamson 	return slot->sl_status;
347749557cc7SAndy Adamson }
347849557cc7SAndy Adamson 
34791b74c25bSMi Jinlong #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
34801b74c25bSMi Jinlong 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
34811b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
34821b74c25bSMi Jinlong 			3 +	/* version, opcount, opcode */ \
34831b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
34841b74c25bSMi Jinlong 				/* seqid, slotID, slotID, cache */ \
34851b74c25bSMi Jinlong 			4 ) * sizeof(__be32))
34861b74c25bSMi Jinlong 
34871b74c25bSMi Jinlong #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
34881b74c25bSMi Jinlong 			2 +	/* verifier: AUTH_NULL, length 0 */\
34891b74c25bSMi Jinlong 			1 +	/* status */ \
34901b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
34911b74c25bSMi Jinlong 			3 +	/* opcount, opcode, opstatus*/ \
34921b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
34931b74c25bSMi Jinlong 				/* seqid, slotID, slotID, slotID, status */ \
34941b74c25bSMi Jinlong 			5 ) * sizeof(__be32))
34951b74c25bSMi Jinlong 
check_forechannel_attrs(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)349655c760cfSJ. Bruce Fields static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
34971b74c25bSMi Jinlong {
349855c760cfSJ. Bruce Fields 	u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
349955c760cfSJ. Bruce Fields 
3500373cd409SJ. Bruce Fields 	if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3501373cd409SJ. Bruce Fields 		return nfserr_toosmall;
3502373cd409SJ. Bruce Fields 	if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3503373cd409SJ. Bruce Fields 		return nfserr_toosmall;
350455c760cfSJ. Bruce Fields 	ca->headerpadsz = 0;
350555c760cfSJ. Bruce Fields 	ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
350655c760cfSJ. Bruce Fields 	ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
350755c760cfSJ. Bruce Fields 	ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
350855c760cfSJ. Bruce Fields 	ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
350955c760cfSJ. Bruce Fields 			NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
351055c760cfSJ. Bruce Fields 	ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
351155c760cfSJ. Bruce Fields 	/*
351255c760cfSJ. Bruce Fields 	 * Note decreasing slot size below client's request may make it
351355c760cfSJ. Bruce Fields 	 * difficult for client to function correctly, whereas
351455c760cfSJ. Bruce Fields 	 * decreasing the number of slots will (just?) affect
351555c760cfSJ. Bruce Fields 	 * performance.  When short on memory we therefore prefer to
351655c760cfSJ. Bruce Fields 	 * decrease number of slots instead of their size.  Clients that
351755c760cfSJ. Bruce Fields 	 * request larger slots than they need will get poor results:
35187f49fd5dSNeilBrown 	 * Note that we always allow at least one slot, because our
35197f49fd5dSNeilBrown 	 * accounting is soft and provides no guarantees either way.
352055c760cfSJ. Bruce Fields 	 */
35212030ca56SNeilBrown 	ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
352255c760cfSJ. Bruce Fields 
3523373cd409SJ. Bruce Fields 	return nfs_ok;
35241b74c25bSMi Jinlong }
35251b74c25bSMi Jinlong 
35264500632fSChuck Lever /*
35274500632fSChuck Lever  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
35284500632fSChuck Lever  * These are based on similar macros in linux/sunrpc/msg_prot.h .
35294500632fSChuck Lever  */
35304500632fSChuck Lever #define RPC_MAX_HEADER_WITH_AUTH_SYS \
35314500632fSChuck Lever 	(RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
35324500632fSChuck Lever 
35334500632fSChuck Lever #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
35344500632fSChuck Lever 	(RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
35354500632fSChuck Lever 
35368a891633SKinglong Mee #define NFSD_CB_MAX_REQ_SZ	((NFS4_enc_cb_recall_sz + \
35374500632fSChuck Lever 				 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
35388a891633SKinglong Mee #define NFSD_CB_MAX_RESP_SZ	((NFS4_dec_cb_recall_sz + \
35394500632fSChuck Lever 				 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
35404500632fSChuck Lever 				 sizeof(__be32))
35418a891633SKinglong Mee 
check_backchannel_attrs(struct nfsd4_channel_attrs * ca)354206b332a5SJ. Bruce Fields static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
354306b332a5SJ. Bruce Fields {
354406b332a5SJ. Bruce Fields 	ca->headerpadsz = 0;
354506b332a5SJ. Bruce Fields 
35468a891633SKinglong Mee 	if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
354706b332a5SJ. Bruce Fields 		return nfserr_toosmall;
35488a891633SKinglong Mee 	if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
354906b332a5SJ. Bruce Fields 		return nfserr_toosmall;
355006b332a5SJ. Bruce Fields 	ca->maxresp_cached = 0;
355106b332a5SJ. Bruce Fields 	if (ca->maxops < 2)
355206b332a5SJ. Bruce Fields 		return nfserr_toosmall;
355306b332a5SJ. Bruce Fields 
355406b332a5SJ. Bruce Fields 	return nfs_ok;
3555069b6ad4SAndy Adamson }
3556069b6ad4SAndy Adamson 
nfsd4_check_cb_sec(struct nfsd4_cb_sec * cbs)3557b78724b7SJ. Bruce Fields static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3558b78724b7SJ. Bruce Fields {
3559b78724b7SJ. Bruce Fields 	switch (cbs->flavor) {
3560b78724b7SJ. Bruce Fields 	case RPC_AUTH_NULL:
3561b78724b7SJ. Bruce Fields 	case RPC_AUTH_UNIX:
3562b78724b7SJ. Bruce Fields 		return nfs_ok;
3563b78724b7SJ. Bruce Fields 	default:
3564b78724b7SJ. Bruce Fields 		/*
3565b78724b7SJ. Bruce Fields 		 * GSS case: the spec doesn't allow us to return this
3566b78724b7SJ. Bruce Fields 		 * error.  But it also doesn't allow us not to support
3567b78724b7SJ. Bruce Fields 		 * GSS.
3568b78724b7SJ. Bruce Fields 		 * I'd rather this fail hard than return some error the
3569b78724b7SJ. Bruce Fields 		 * client might think it can already handle:
3570b78724b7SJ. Bruce Fields 		 */
3571b78724b7SJ. Bruce Fields 		return nfserr_encr_alg_unsupp;
3572b78724b7SJ. Bruce Fields 	}
3573b78724b7SJ. Bruce Fields }
3574b78724b7SJ. Bruce Fields 
3575069b6ad4SAndy Adamson __be32
nfsd4_create_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3576069b6ad4SAndy Adamson nfsd4_create_session(struct svc_rqst *rqstp,
3577eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3578069b6ad4SAndy Adamson {
3579eb69853dSChristoph Hellwig 	struct nfsd4_create_session *cr_ses = &u->create_session;
3580363168b4SJeff Layton 	struct sockaddr *sa = svc_addr(rqstp);
3581ec6b5d7bSAndy Adamson 	struct nfs4_client *conf, *unconf;
3582d20c11d8SJeff Layton 	struct nfs4_client *old = NULL;
3583ac7c46f2SJ. Bruce Fields 	struct nfsd4_session *new;
358481f0b2a4SJ. Bruce Fields 	struct nfsd4_conn *conn;
358549557cc7SAndy Adamson 	struct nfsd4_clid_slot *cs_slot = NULL;
358657b7b43bSJ. Bruce Fields 	__be32 status = 0;
35878daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3588ec6b5d7bSAndy Adamson 
3589a62573dcSMi Jinlong 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3590a62573dcSMi Jinlong 		return nfserr_inval;
3591b78724b7SJ. Bruce Fields 	status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3592b78724b7SJ. Bruce Fields 	if (status)
3593b78724b7SJ. Bruce Fields 		return status;
359455c760cfSJ. Bruce Fields 	status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3595373cd409SJ. Bruce Fields 	if (status)
3596373cd409SJ. Bruce Fields 		return status;
359706b332a5SJ. Bruce Fields 	status = check_backchannel_attrs(&cr_ses->back_channel);
359806b332a5SJ. Bruce Fields 	if (status)
3599f403e450SKinglong Mee 		goto out_release_drc_mem;
360081f0b2a4SJ. Bruce Fields 	status = nfserr_jukebox;
360160810e54SKinglong Mee 	new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
360255c760cfSJ. Bruce Fields 	if (!new)
360355c760cfSJ. Bruce Fields 		goto out_release_drc_mem;
360481f0b2a4SJ. Bruce Fields 	conn = alloc_conn_from_crses(rqstp, cr_ses);
360581f0b2a4SJ. Bruce Fields 	if (!conn)
360681f0b2a4SJ. Bruce Fields 		goto out_free_session;
3607a62573dcSMi Jinlong 
3608d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
36090a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
36108daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(&cr_ses->clientid, true, nn);
361178389046SJ. Bruce Fields 	WARN_ON_ONCE(conf && unconf);
3612ec6b5d7bSAndy Adamson 
3613ec6b5d7bSAndy Adamson 	if (conf) {
361457266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
3615dedeb13fSAndrew Elble 		if (!nfsd4_mach_creds_match(conf, rqstp))
361657266a6eSJ. Bruce Fields 			goto out_free_conn;
361749557cc7SAndy Adamson 		cs_slot = &conf->cl_cs_slot;
361849557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3619f5e22bb6SKinglong Mee 		if (status) {
3620f5e22bb6SKinglong Mee 			if (status == nfserr_replay_cache)
362149557cc7SAndy Adamson 				status = nfsd4_replay_create_session(cr_ses, cs_slot);
362281f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3623ec6b5d7bSAndy Adamson 		}
3624ec6b5d7bSAndy Adamson 	} else if (unconf) {
362527787733SChuck Lever 		status = nfserr_clid_inuse;
3626ec6b5d7bSAndy Adamson 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3627363168b4SJeff Layton 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
362827787733SChuck Lever 			trace_nfsd_clid_cred_mismatch(unconf, rqstp);
362981f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3630ec6b5d7bSAndy Adamson 		}
363157266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
3632dedeb13fSAndrew Elble 		if (!nfsd4_mach_creds_match(unconf, rqstp))
363357266a6eSJ. Bruce Fields 			goto out_free_conn;
363449557cc7SAndy Adamson 		cs_slot = &unconf->cl_cs_slot;
363549557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
363638eb76a5SAndy Adamson 		if (status) {
363738eb76a5SAndy Adamson 			/* an unconfirmed replay returns misordered */
3638ec6b5d7bSAndy Adamson 			status = nfserr_seq_misordered;
363981f0b2a4SJ. Bruce Fields 			goto out_free_conn;
3640ec6b5d7bSAndy Adamson 		}
3641382a62e7SStanislav Kinsbursky 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3642221a6876SJ. Bruce Fields 		if (old) {
3643d20c11d8SJeff Layton 			status = mark_client_expired_locked(old);
36447abea1e8SJeff Layton 			if (status) {
36457abea1e8SJeff Layton 				old = NULL;
3646221a6876SJ. Bruce Fields 				goto out_free_conn;
3647221a6876SJ. Bruce Fields 			}
36482958d2eeSChuck Lever 			trace_nfsd_clid_replaced(&old->cl_clientid);
36497abea1e8SJeff Layton 		}
36508f9d3d3bSJ. Bruce Fields 		move_to_confirmed(unconf);
3651ec6b5d7bSAndy Adamson 		conf = unconf;
3652ec6b5d7bSAndy Adamson 	} else {
3653ec6b5d7bSAndy Adamson 		status = nfserr_stale_clientid;
365481f0b2a4SJ. Bruce Fields 		goto out_free_conn;
3655ec6b5d7bSAndy Adamson 	}
365681f0b2a4SJ. Bruce Fields 	status = nfs_ok;
36574ce85c8cSChuck Lever 	/* Persistent sessions are not supported */
3658408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_PERSIST;
36594ce85c8cSChuck Lever 	/* Upshifting from TCP to RDMA is not supported */
3660408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_RDMA;
3661408b79bcSJ. Bruce Fields 
366281f0b2a4SJ. Bruce Fields 	init_session(rqstp, new, conf, cr_ses);
3663d20c11d8SJeff Layton 	nfsd4_get_session_locked(new);
366481f0b2a4SJ. Bruce Fields 
3665ac7c46f2SJ. Bruce Fields 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3666ec6b5d7bSAndy Adamson 	       NFS4_MAX_SESSIONID_LEN);
366786c3e16cSJ. Bruce Fields 	cs_slot->sl_seqid++;
366849557cc7SAndy Adamson 	cr_ses->seqid = cs_slot->sl_seqid;
3669ec6b5d7bSAndy Adamson 
3670d20c11d8SJeff Layton 	/* cache solo and embedded create sessions under the client_lock */
367149557cc7SAndy Adamson 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
3672d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
3673934bd07fSJ. Bruce Fields 	if (conf == unconf)
3674934bd07fSJ. Bruce Fields 		fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
3675d20c11d8SJeff Layton 	/* init connection and backchannel */
3676d20c11d8SJeff Layton 	nfsd4_init_conn(rqstp, conn, new);
3677d20c11d8SJeff Layton 	nfsd4_put_session(new);
3678d20c11d8SJeff Layton 	if (old)
3679d20c11d8SJeff Layton 		expire_client(old);
3680ec6b5d7bSAndy Adamson 	return status;
368181f0b2a4SJ. Bruce Fields out_free_conn:
3682d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
368381f0b2a4SJ. Bruce Fields 	free_conn(conn);
3684d20c11d8SJeff Layton 	if (old)
3685d20c11d8SJeff Layton 		expire_client(old);
368681f0b2a4SJ. Bruce Fields out_free_session:
368781f0b2a4SJ. Bruce Fields 	__free_session(new);
368855c760cfSJ. Bruce Fields out_release_drc_mem:
368955c760cfSJ. Bruce Fields 	nfsd4_put_drc_mem(&cr_ses->fore_channel);
36901ca50792SJ. Bruce Fields 	return status;
3691069b6ad4SAndy Adamson }
3692069b6ad4SAndy Adamson 
nfsd4_map_bcts_dir(u32 * dir)36931d1bc8f2SJ. Bruce Fields static __be32 nfsd4_map_bcts_dir(u32 *dir)
36941d1bc8f2SJ. Bruce Fields {
36951d1bc8f2SJ. Bruce Fields 	switch (*dir) {
36961d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE:
36971d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK:
36981d1bc8f2SJ. Bruce Fields 		return nfs_ok;
36991d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE_OR_BOTH:
37001d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK_OR_BOTH:
37011d1bc8f2SJ. Bruce Fields 		*dir = NFS4_CDFC4_BOTH;
37021d1bc8f2SJ. Bruce Fields 		return nfs_ok;
3703fc5fc5d7Szhengbin 	}
37041d1bc8f2SJ. Bruce Fields 	return nfserr_inval;
37051d1bc8f2SJ. Bruce Fields }
37061d1bc8f2SJ. Bruce Fields 
nfsd4_backchannel_ctl(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3707eb69853dSChristoph Hellwig __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3708eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
3709eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3710cb73a9f4SJ. Bruce Fields {
3711eb69853dSChristoph Hellwig 	struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3712cb73a9f4SJ. Bruce Fields 	struct nfsd4_session *session = cstate->session;
3713c9a49628SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3714b78724b7SJ. Bruce Fields 	__be32 status;
3715cb73a9f4SJ. Bruce Fields 
3716b78724b7SJ. Bruce Fields 	status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3717b78724b7SJ. Bruce Fields 	if (status)
3718b78724b7SJ. Bruce Fields 		return status;
3719c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3720cb73a9f4SJ. Bruce Fields 	session->se_cb_prog = bc->bc_cb_program;
3721cb73a9f4SJ. Bruce Fields 	session->se_cb_sec = bc->bc_cb_sec;
3722c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
3723cb73a9f4SJ. Bruce Fields 
3724cb73a9f4SJ. Bruce Fields 	nfsd4_probe_callback(session->se_client);
3725cb73a9f4SJ. Bruce Fields 
3726cb73a9f4SJ. Bruce Fields 	return nfs_ok;
3727cb73a9f4SJ. Bruce Fields }
3728cb73a9f4SJ. Bruce Fields 
__nfsd4_find_conn(struct svc_xprt * xpt,struct nfsd4_session * s)3729c2d715a1SJ. Bruce Fields static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3730c2d715a1SJ. Bruce Fields {
3731c2d715a1SJ. Bruce Fields 	struct nfsd4_conn *c;
3732c2d715a1SJ. Bruce Fields 
3733c2d715a1SJ. Bruce Fields 	list_for_each_entry(c, &s->se_conns, cn_persession) {
3734c2d715a1SJ. Bruce Fields 		if (c->cn_xprt == xpt) {
3735c2d715a1SJ. Bruce Fields 			return c;
3736c2d715a1SJ. Bruce Fields 		}
3737c2d715a1SJ. Bruce Fields 	}
3738c2d715a1SJ. Bruce Fields 	return NULL;
3739c2d715a1SJ. Bruce Fields }
3740c2d715a1SJ. Bruce Fields 
nfsd4_match_existing_connection(struct svc_rqst * rqst,struct nfsd4_session * session,u32 req,struct nfsd4_conn ** conn)3741c2d715a1SJ. Bruce Fields static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
374202579b2fSDai Ngo 		struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3743c2d715a1SJ. Bruce Fields {
3744c2d715a1SJ. Bruce Fields 	struct nfs4_client *clp = session->se_client;
3745c2d715a1SJ. Bruce Fields 	struct svc_xprt *xpt = rqst->rq_xprt;
3746c2d715a1SJ. Bruce Fields 	struct nfsd4_conn *c;
3747c2d715a1SJ. Bruce Fields 	__be32 status;
3748c2d715a1SJ. Bruce Fields 
3749c2d715a1SJ. Bruce Fields 	/* Following the last paragraph of RFC 5661 Section 18.34.3: */
3750c2d715a1SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
3751c2d715a1SJ. Bruce Fields 	c = __nfsd4_find_conn(xpt, session);
3752c2d715a1SJ. Bruce Fields 	if (!c)
3753c2d715a1SJ. Bruce Fields 		status = nfserr_noent;
3754c2d715a1SJ. Bruce Fields 	else if (req == c->cn_flags)
3755c2d715a1SJ. Bruce Fields 		status = nfs_ok;
3756c2d715a1SJ. Bruce Fields 	else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3757c2d715a1SJ. Bruce Fields 				c->cn_flags != NFS4_CDFC4_BACK)
3758c2d715a1SJ. Bruce Fields 		status = nfs_ok;
3759c2d715a1SJ. Bruce Fields 	else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3760c2d715a1SJ. Bruce Fields 				c->cn_flags != NFS4_CDFC4_FORE)
3761c2d715a1SJ. Bruce Fields 		status = nfs_ok;
3762c2d715a1SJ. Bruce Fields 	else
3763c2d715a1SJ. Bruce Fields 		status = nfserr_inval;
3764c2d715a1SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
376502579b2fSDai Ngo 	if (status == nfs_ok && conn)
376602579b2fSDai Ngo 		*conn = c;
3767c2d715a1SJ. Bruce Fields 	return status;
3768c2d715a1SJ. Bruce Fields }
3769c2d715a1SJ. Bruce Fields 
nfsd4_bind_conn_to_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)37701d1bc8f2SJ. Bruce Fields __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
37711d1bc8f2SJ. Bruce Fields 		     struct nfsd4_compound_state *cstate,
3772eb69853dSChristoph Hellwig 		     union nfsd4_op_u *u)
37731d1bc8f2SJ. Bruce Fields {
3774eb69853dSChristoph Hellwig 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
37751d1bc8f2SJ. Bruce Fields 	__be32 status;
37763ba63671SJ. Bruce Fields 	struct nfsd4_conn *conn;
37774f6e6c17SJ. Bruce Fields 	struct nfsd4_session *session;
3778d4e19e70STrond Myklebust 	struct net *net = SVC_NET(rqstp);
3779d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
37801d1bc8f2SJ. Bruce Fields 
37811d1bc8f2SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
37821d1bc8f2SJ. Bruce Fields 		return nfserr_not_only_op;
3783c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3784d4e19e70STrond Myklebust 	session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3785c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
37864f6e6c17SJ. Bruce Fields 	if (!session)
3787d4e19e70STrond Myklebust 		goto out_no_session;
378857266a6eSJ. Bruce Fields 	status = nfserr_wrong_cred;
3789dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(session->se_client, rqstp))
379057266a6eSJ. Bruce Fields 		goto out;
379102579b2fSDai Ngo 	status = nfsd4_match_existing_connection(rqstp, session,
379202579b2fSDai Ngo 			bcts->dir, &conn);
379302579b2fSDai Ngo 	if (status == nfs_ok) {
379402579b2fSDai Ngo 		if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
379502579b2fSDai Ngo 				bcts->dir == NFS4_CDFC4_BACK)
379602579b2fSDai Ngo 			conn->cn_flags |= NFS4_CDFC4_BACK;
379702579b2fSDai Ngo 		nfsd4_probe_callback(session->se_client);
379802579b2fSDai Ngo 		goto out;
379902579b2fSDai Ngo 	}
380002579b2fSDai Ngo 	if (status == nfserr_inval)
3801c2d715a1SJ. Bruce Fields 		goto out;
38021d1bc8f2SJ. Bruce Fields 	status = nfsd4_map_bcts_dir(&bcts->dir);
38033ba63671SJ. Bruce Fields 	if (status)
38044f6e6c17SJ. Bruce Fields 		goto out;
38053ba63671SJ. Bruce Fields 	conn = alloc_conn(rqstp, bcts->dir);
38064f6e6c17SJ. Bruce Fields 	status = nfserr_jukebox;
38073ba63671SJ. Bruce Fields 	if (!conn)
38084f6e6c17SJ. Bruce Fields 		goto out;
38094f6e6c17SJ. Bruce Fields 	nfsd4_init_conn(rqstp, conn, session);
38104f6e6c17SJ. Bruce Fields 	status = nfs_ok;
38114f6e6c17SJ. Bruce Fields out:
3812d4e19e70STrond Myklebust 	nfsd4_put_session(session);
3813d4e19e70STrond Myklebust out_no_session:
38144f6e6c17SJ. Bruce Fields 	return status;
38151d1bc8f2SJ. Bruce Fields }
38161d1bc8f2SJ. Bruce Fields 
nfsd4_compound_in_session(struct nfsd4_compound_state * cstate,struct nfs4_sessionid * sid)3817665d5072SJ. Bruce Fields static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
38185d4cec2fSJ. Bruce Fields {
3819665d5072SJ. Bruce Fields 	if (!cstate->session)
382051d87bc2SFengguang Wu 		return false;
3821665d5072SJ. Bruce Fields 	return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
38225d4cec2fSJ. Bruce Fields }
38235d4cec2fSJ. Bruce Fields 
3824069b6ad4SAndy Adamson __be32
nfsd4_destroy_session(struct svc_rqst * r,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3825eb69853dSChristoph Hellwig nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3826eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3827069b6ad4SAndy Adamson {
3828ca0552f4SJ. Bruce Fields 	struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3829e10e0cfcSBenny Halevy 	struct nfsd4_session *ses;
3830abcdff09SJ. Bruce Fields 	__be32 status;
3831f0f51f5cSJ. Bruce Fields 	int ref_held_by_me = 0;
3832d4e19e70STrond Myklebust 	struct net *net = SVC_NET(r);
3833d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3834e10e0cfcSBenny Halevy 
3835abcdff09SJ. Bruce Fields 	status = nfserr_not_only_op;
3836ca0552f4SJ. Bruce Fields 	if (nfsd4_compound_in_session(cstate, sessionid)) {
383757716355SJ. Bruce Fields 		if (!nfsd4_last_compound_op(r))
3838abcdff09SJ. Bruce Fields 			goto out;
3839f0f51f5cSJ. Bruce Fields 		ref_held_by_me++;
384057716355SJ. Bruce Fields 	}
3841ca0552f4SJ. Bruce Fields 	dump_sessionid(__func__, sessionid);
3842c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3843ca0552f4SJ. Bruce Fields 	ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3844abcdff09SJ. Bruce Fields 	if (!ses)
3845abcdff09SJ. Bruce Fields 		goto out_client_lock;
384657266a6eSJ. Bruce Fields 	status = nfserr_wrong_cred;
3847dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(ses->se_client, r))
3848d4e19e70STrond Myklebust 		goto out_put_session;
3849f0f51f5cSJ. Bruce Fields 	status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
385066b2b9b2SJ. Bruce Fields 	if (status)
3851f0f51f5cSJ. Bruce Fields 		goto out_put_session;
3852e10e0cfcSBenny Halevy 	unhash_session(ses);
3853c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
3854e10e0cfcSBenny Halevy 
385584f5f7ccSJ. Bruce Fields 	nfsd4_probe_callback_sync(ses->se_client);
385619cf5c02SJ. Bruce Fields 
3857c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3858e10e0cfcSBenny Halevy 	status = nfs_ok;
3859f0f51f5cSJ. Bruce Fields out_put_session:
3860d4e19e70STrond Myklebust 	nfsd4_put_session_locked(ses);
3861abcdff09SJ. Bruce Fields out_client_lock:
3862abcdff09SJ. Bruce Fields 	spin_unlock(&nn->client_lock);
3863e10e0cfcSBenny Halevy out:
3864e10e0cfcSBenny Halevy 	return status;
3865069b6ad4SAndy Adamson }
3866069b6ad4SAndy Adamson 
nfsd4_sequence_check_conn(struct nfsd4_conn * new,struct nfsd4_session * ses)386757266a6eSJ. Bruce Fields static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3868328ead28SJ. Bruce Fields {
3869328ead28SJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
3870a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *c;
387157266a6eSJ. Bruce Fields 	__be32 status = nfs_ok;
387221b75b01SJ. Bruce Fields 	int ret;
3873328ead28SJ. Bruce Fields 
3874328ead28SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
3875a663bdd8SJ. Bruce Fields 	c = __nfsd4_find_conn(new->cn_xprt, ses);
387657266a6eSJ. Bruce Fields 	if (c)
387757266a6eSJ. Bruce Fields 		goto out_free;
387857266a6eSJ. Bruce Fields 	status = nfserr_conn_not_bound_to_session;
387957266a6eSJ. Bruce Fields 	if (clp->cl_mach_cred)
388057266a6eSJ. Bruce Fields 		goto out_free;
3881328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(new, ses);
3882328ead28SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
388321b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(new);
388421b75b01SJ. Bruce Fields 	if (ret)
388521b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
388621b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&new->cn_xpt_user);
388757266a6eSJ. Bruce Fields 	return nfs_ok;
388857266a6eSJ. Bruce Fields out_free:
388957266a6eSJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
389057266a6eSJ. Bruce Fields 	free_conn(new);
389157266a6eSJ. Bruce Fields 	return status;
3892328ead28SJ. Bruce Fields }
3893328ead28SJ. Bruce Fields 
nfsd4_session_too_many_ops(struct svc_rqst * rqstp,struct nfsd4_session * session)3894868b89c3SMi Jinlong static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3895868b89c3SMi Jinlong {
3896868b89c3SMi Jinlong 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
3897868b89c3SMi Jinlong 
3898868b89c3SMi Jinlong 	return args->opcnt > session->se_fchannel.maxops;
3899868b89c3SMi Jinlong }
3900868b89c3SMi Jinlong 
nfsd4_request_too_big(struct svc_rqst * rqstp,struct nfsd4_session * session)3901ae82a8d0SMi Jinlong static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3902ae82a8d0SMi Jinlong 				  struct nfsd4_session *session)
3903ae82a8d0SMi Jinlong {
3904ae82a8d0SMi Jinlong 	struct xdr_buf *xb = &rqstp->rq_arg;
3905ae82a8d0SMi Jinlong 
3906ae82a8d0SMi Jinlong 	return xb->len > session->se_fchannel.maxreq_sz;
3907ae82a8d0SMi Jinlong }
3908ae82a8d0SMi Jinlong 
replay_matches_cache(struct svc_rqst * rqstp,struct nfsd4_sequence * seq,struct nfsd4_slot * slot)390953da6a53SJ. Bruce Fields static bool replay_matches_cache(struct svc_rqst *rqstp,
391053da6a53SJ. Bruce Fields 		 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
391153da6a53SJ. Bruce Fields {
391253da6a53SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
391353da6a53SJ. Bruce Fields 
391453da6a53SJ. Bruce Fields 	if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
391553da6a53SJ. Bruce Fields 	    (bool)seq->cachethis)
391653da6a53SJ. Bruce Fields 		return false;
391753da6a53SJ. Bruce Fields 	/*
39186e73e92bSScott Mayhew 	 * If there's an error then the reply can have fewer ops than
39196e73e92bSScott Mayhew 	 * the call.
392053da6a53SJ. Bruce Fields 	 */
39216e73e92bSScott Mayhew 	if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
39226e73e92bSScott Mayhew 		return false;
39236e73e92bSScott Mayhew 	/*
39246e73e92bSScott Mayhew 	 * But if we cached a reply with *more* ops than the call you're
39256e73e92bSScott Mayhew 	 * sending us now, then this new call is clearly not really a
39266e73e92bSScott Mayhew 	 * replay of the old one:
39276e73e92bSScott Mayhew 	 */
39286e73e92bSScott Mayhew 	if (slot->sl_opcnt > argp->opcnt)
392953da6a53SJ. Bruce Fields 		return false;
393053da6a53SJ. Bruce Fields 	/* This is the only check explicitly called by spec: */
393153da6a53SJ. Bruce Fields 	if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
393253da6a53SJ. Bruce Fields 		return false;
393353da6a53SJ. Bruce Fields 	/*
393453da6a53SJ. Bruce Fields 	 * There may be more comparisons we could actually do, but the
393553da6a53SJ. Bruce Fields 	 * spec doesn't require us to catch every case where the calls
393653da6a53SJ. Bruce Fields 	 * don't match (that would require caching the call as well as
393753da6a53SJ. Bruce Fields 	 * the reply), so we don't bother.
393853da6a53SJ. Bruce Fields 	 */
393953da6a53SJ. Bruce Fields 	return true;
394053da6a53SJ. Bruce Fields }
394153da6a53SJ. Bruce Fields 
3942069b6ad4SAndy Adamson __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3943eb69853dSChristoph Hellwig nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3944eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
3945069b6ad4SAndy Adamson {
3946eb69853dSChristoph Hellwig 	struct nfsd4_sequence *seq = &u->sequence;
3947f9bb94c4SAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3948bddfdbcdSChuck Lever 	struct xdr_stream *xdr = resp->xdr;
3949b85d4c01SBenny Halevy 	struct nfsd4_session *session;
3950221a6876SJ. Bruce Fields 	struct nfs4_client *clp;
3951b85d4c01SBenny Halevy 	struct nfsd4_slot *slot;
3952a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *conn;
395357b7b43bSJ. Bruce Fields 	__be32 status;
395447ee5298SJ. Bruce Fields 	int buflen;
3955d4e19e70STrond Myklebust 	struct net *net = SVC_NET(rqstp);
3956d4e19e70STrond Myklebust 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3957b85d4c01SBenny Halevy 
3958f9bb94c4SAndy Adamson 	if (resp->opcnt != 1)
3959f9bb94c4SAndy Adamson 		return nfserr_sequence_pos;
3960f9bb94c4SAndy Adamson 
3961a663bdd8SJ. Bruce Fields 	/*
3962a663bdd8SJ. Bruce Fields 	 * Will be either used or freed by nfsd4_sequence_check_conn
3963a663bdd8SJ. Bruce Fields 	 * below.
3964a663bdd8SJ. Bruce Fields 	 */
3965a663bdd8SJ. Bruce Fields 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3966a663bdd8SJ. Bruce Fields 	if (!conn)
3967a663bdd8SJ. Bruce Fields 		return nfserr_jukebox;
3968a663bdd8SJ. Bruce Fields 
3969c9a49628SStanislav Kinsbursky 	spin_lock(&nn->client_lock);
3970d4e19e70STrond Myklebust 	session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3971b85d4c01SBenny Halevy 	if (!session)
3972221a6876SJ. Bruce Fields 		goto out_no_session;
3973221a6876SJ. Bruce Fields 	clp = session->se_client;
3974b85d4c01SBenny Halevy 
3975868b89c3SMi Jinlong 	status = nfserr_too_many_ops;
3976868b89c3SMi Jinlong 	if (nfsd4_session_too_many_ops(rqstp, session))
397766b2b9b2SJ. Bruce Fields 		goto out_put_session;
3978868b89c3SMi Jinlong 
3979ae82a8d0SMi Jinlong 	status = nfserr_req_too_big;
3980ae82a8d0SMi Jinlong 	if (nfsd4_request_too_big(rqstp, session))
398166b2b9b2SJ. Bruce Fields 		goto out_put_session;
3982ae82a8d0SMi Jinlong 
3983b85d4c01SBenny Halevy 	status = nfserr_badslot;
39846c18ba9fSAlexandros Batsakis 	if (seq->slotid >= session->se_fchannel.maxreqs)
398566b2b9b2SJ. Bruce Fields 		goto out_put_session;
3986b85d4c01SBenny Halevy 
3987557ce264SAndy Adamson 	slot = session->se_slots[seq->slotid];
3988b85d4c01SBenny Halevy 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
3989b85d4c01SBenny Halevy 
3990a8dfdaebSAndy Adamson 	/* We do not negotiate the number of slots yet, so set the
3991a8dfdaebSAndy Adamson 	 * maxslots to the session maxreqs which is used to encode
3992a8dfdaebSAndy Adamson 	 * sr_highest_slotid and the sr_target_slot id to maxslots */
3993a8dfdaebSAndy Adamson 	seq->maxslots = session->se_fchannel.maxreqs;
3994a8dfdaebSAndy Adamson 
399573e79482SJ. Bruce Fields 	status = check_slot_seqid(seq->seqid, slot->sl_seqid,
399673e79482SJ. Bruce Fields 					slot->sl_flags & NFSD4_SLOT_INUSE);
3997b85d4c01SBenny Halevy 	if (status == nfserr_replay_cache) {
3998bf5c43c8SJ. Bruce Fields 		status = nfserr_seq_misordered;
3999bf5c43c8SJ. Bruce Fields 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
400066b2b9b2SJ. Bruce Fields 			goto out_put_session;
400153da6a53SJ. Bruce Fields 		status = nfserr_seq_false_retry;
400253da6a53SJ. Bruce Fields 		if (!replay_matches_cache(rqstp, seq, slot))
400353da6a53SJ. Bruce Fields 			goto out_put_session;
4004b85d4c01SBenny Halevy 		cstate->slot = slot;
4005b85d4c01SBenny Halevy 		cstate->session = session;
40064b24ca7dSJeff Layton 		cstate->clp = clp;
4007da3846a2SAndy Adamson 		/* Return the cached reply status and set cstate->status
4008557ce264SAndy Adamson 		 * for nfsd4_proc_compound processing */
4009bf864a31SAndy Adamson 		status = nfsd4_replay_cache_entry(resp, seq);
4010da3846a2SAndy Adamson 		cstate->status = nfserr_replay_cache;
4011aaf84eb9SBenny Halevy 		goto out;
4012b85d4c01SBenny Halevy 	}
4013b85d4c01SBenny Halevy 	if (status)
401466b2b9b2SJ. Bruce Fields 		goto out_put_session;
4015b85d4c01SBenny Halevy 
401657266a6eSJ. Bruce Fields 	status = nfsd4_sequence_check_conn(conn, session);
4017a663bdd8SJ. Bruce Fields 	conn = NULL;
401857266a6eSJ. Bruce Fields 	if (status)
401957266a6eSJ. Bruce Fields 		goto out_put_session;
4020328ead28SJ. Bruce Fields 
402147ee5298SJ. Bruce Fields 	buflen = (seq->cachethis) ?
402247ee5298SJ. Bruce Fields 			session->se_fchannel.maxresp_cached :
402347ee5298SJ. Bruce Fields 			session->se_fchannel.maxresp_sz;
402447ee5298SJ. Bruce Fields 	status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
402547ee5298SJ. Bruce Fields 				    nfserr_rep_too_big;
4026a5cddc88SJ. Bruce Fields 	if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
402747ee5298SJ. Bruce Fields 		goto out_put_session;
402832aaa62eSJ. Bruce Fields 	svc_reserve(rqstp, buflen);
402947ee5298SJ. Bruce Fields 
403047ee5298SJ. Bruce Fields 	status = nfs_ok;
4031b85d4c01SBenny Halevy 	/* Success! bump slot seqid */
4032b85d4c01SBenny Halevy 	slot->sl_seqid = seq->seqid;
4033bf5c43c8SJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INUSE;
403473e79482SJ. Bruce Fields 	if (seq->cachethis)
403573e79482SJ. Bruce Fields 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
4036bf5c43c8SJ. Bruce Fields 	else
4037bf5c43c8SJ. Bruce Fields 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
4038b85d4c01SBenny Halevy 
4039b85d4c01SBenny Halevy 	cstate->slot = slot;
4040b85d4c01SBenny Halevy 	cstate->session = session;
40414b24ca7dSJeff Layton 	cstate->clp = clp;
4042b85d4c01SBenny Halevy 
4043b85d4c01SBenny Halevy out:
40445423732aSBenny Halevy 	switch (clp->cl_cb_state) {
40455423732aSBenny Halevy 	case NFSD4_CB_DOWN:
4046fc0c3dd1SBenny Halevy 		seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
40475423732aSBenny Halevy 		break;
40485423732aSBenny Halevy 	case NFSD4_CB_FAULT:
4049fc0c3dd1SBenny Halevy 		seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
40505423732aSBenny Halevy 		break;
4051fc0c3dd1SBenny Halevy 	default:
4052fc0c3dd1SBenny Halevy 		seq->status_flags = 0;
40535423732aSBenny Halevy 	}
40543bd64a5bSJ. Bruce Fields 	if (!list_empty(&clp->cl_revoked))
40553bd64a5bSJ. Bruce Fields 		seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
4056221a6876SJ. Bruce Fields out_no_session:
40573f42d2c4SKinglong Mee 	if (conn)
40583f42d2c4SKinglong Mee 		free_conn(conn);
4059c9a49628SStanislav Kinsbursky 	spin_unlock(&nn->client_lock);
4060b85d4c01SBenny Halevy 	return status;
406166b2b9b2SJ. Bruce Fields out_put_session:
4062d4e19e70STrond Myklebust 	nfsd4_put_session_locked(session);
4063221a6876SJ. Bruce Fields 	goto out_no_session;
4064069b6ad4SAndy Adamson }
4065069b6ad4SAndy Adamson 
4066b607664eSTrond Myklebust void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)4067b607664eSTrond Myklebust nfsd4_sequence_done(struct nfsd4_compoundres *resp)
4068b607664eSTrond Myklebust {
4069b607664eSTrond Myklebust 	struct nfsd4_compound_state *cs = &resp->cstate;
4070b607664eSTrond Myklebust 
4071b607664eSTrond Myklebust 	if (nfsd4_has_session(cs)) {
4072b607664eSTrond Myklebust 		if (cs->status != nfserr_replay_cache) {
4073b607664eSTrond Myklebust 			nfsd4_store_cache_entry(resp);
4074b607664eSTrond Myklebust 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
4075b607664eSTrond Myklebust 		}
4076d4e19e70STrond Myklebust 		/* Drop session reference that was taken in nfsd4_sequence() */
4077b607664eSTrond Myklebust 		nfsd4_put_session(cs->session);
40784b24ca7dSJeff Layton 	} else if (cs->clp)
40794b24ca7dSJeff Layton 		put_client_renew(cs->clp);
4080b607664eSTrond Myklebust }
4081b607664eSTrond Myklebust 
4082345c2842SMi Jinlong __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4083eb69853dSChristoph Hellwig nfsd4_destroy_clientid(struct svc_rqst *rqstp,
4084eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate,
4085eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
4086345c2842SMi Jinlong {
4087eb69853dSChristoph Hellwig 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
40886b10ad19STrond Myklebust 	struct nfs4_client *conf, *unconf;
40896b10ad19STrond Myklebust 	struct nfs4_client *clp = NULL;
409057b7b43bSJ. Bruce Fields 	__be32 status = 0;
40918daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4092345c2842SMi Jinlong 
40936b10ad19STrond Myklebust 	spin_lock(&nn->client_lock);
40940a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(&dc->clientid, true, nn);
40958daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(&dc->clientid, true, nn);
409678389046SJ. Bruce Fields 	WARN_ON_ONCE(conf && unconf);
4097345c2842SMi Jinlong 
4098345c2842SMi Jinlong 	if (conf) {
4099c0293b01SJ. Bruce Fields 		if (client_has_state(conf)) {
4100345c2842SMi Jinlong 			status = nfserr_clientid_busy;
4101345c2842SMi Jinlong 			goto out;
4102345c2842SMi Jinlong 		}
4103fd699b8aSJeff Layton 		status = mark_client_expired_locked(conf);
4104fd699b8aSJeff Layton 		if (status)
4105fd699b8aSJeff Layton 			goto out;
41066b10ad19STrond Myklebust 		clp = conf;
4107345c2842SMi Jinlong 	} else if (unconf)
4108345c2842SMi Jinlong 		clp = unconf;
4109345c2842SMi Jinlong 	else {
4110345c2842SMi Jinlong 		status = nfserr_stale_clientid;
4111345c2842SMi Jinlong 		goto out;
4112345c2842SMi Jinlong 	}
4113dedeb13fSAndrew Elble 	if (!nfsd4_mach_creds_match(clp, rqstp)) {
41146b10ad19STrond Myklebust 		clp = NULL;
411557266a6eSJ. Bruce Fields 		status = nfserr_wrong_cred;
411657266a6eSJ. Bruce Fields 		goto out;
411757266a6eSJ. Bruce Fields 	}
4118c41a9b7aSChuck Lever 	trace_nfsd_clid_destroyed(&clp->cl_clientid);
41196b10ad19STrond Myklebust 	unhash_client_locked(clp);
4120345c2842SMi Jinlong out:
41216b10ad19STrond Myklebust 	spin_unlock(&nn->client_lock);
41226b10ad19STrond Myklebust 	if (clp)
41236b10ad19STrond Myklebust 		expire_client(clp);
4124345c2842SMi Jinlong 	return status;
4125345c2842SMi Jinlong }
4126345c2842SMi Jinlong 
4127069b6ad4SAndy Adamson __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4128eb69853dSChristoph Hellwig nfsd4_reclaim_complete(struct svc_rqst *rqstp,
4129eb69853dSChristoph Hellwig 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
41304dc6ec00SJ. Bruce Fields {
4131eb69853dSChristoph Hellwig 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
4132ec59659bSJ. Bruce Fields 	struct nfs4_client *clp = cstate->clp;
413357b7b43bSJ. Bruce Fields 	__be32 status = 0;
4134bcecf1ccSMi Jinlong 
41354dc6ec00SJ. Bruce Fields 	if (rc->rca_one_fs) {
41364dc6ec00SJ. Bruce Fields 		if (!cstate->current_fh.fh_dentry)
41374dc6ec00SJ. Bruce Fields 			return nfserr_nofilehandle;
41384dc6ec00SJ. Bruce Fields 		/*
41394dc6ec00SJ. Bruce Fields 		 * We don't take advantage of the rca_one_fs case.
41404dc6ec00SJ. Bruce Fields 		 * That's OK, it's optional, we can safely ignore it.
41414dc6ec00SJ. Bruce Fields 		 */
41424dc6ec00SJ. Bruce Fields 		return nfs_ok;
41434dc6ec00SJ. Bruce Fields 	}
4144bcecf1ccSMi Jinlong 
4145bcecf1ccSMi Jinlong 	status = nfserr_complete_already;
4146ec59659bSJ. Bruce Fields 	if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
4147bcecf1ccSMi Jinlong 		goto out;
4148bcecf1ccSMi Jinlong 
4149bcecf1ccSMi Jinlong 	status = nfserr_stale_clientid;
4150ec59659bSJ. Bruce Fields 	if (is_client_expired(clp))
41514dc6ec00SJ. Bruce Fields 		/*
41524dc6ec00SJ. Bruce Fields 		 * The following error isn't really legal.
41534dc6ec00SJ. Bruce Fields 		 * But we only get here if the client just explicitly
41544dc6ec00SJ. Bruce Fields 		 * destroyed the client.  Surely it no longer cares what
41554dc6ec00SJ. Bruce Fields 		 * error it gets back on an operation for the dead
41564dc6ec00SJ. Bruce Fields 		 * client.
41574dc6ec00SJ. Bruce Fields 		 */
4158bcecf1ccSMi Jinlong 		goto out;
4159bcecf1ccSMi Jinlong 
4160bcecf1ccSMi Jinlong 	status = nfs_ok;
4161cee8aa07SChuck Lever 	trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
4162ec59659bSJ. Bruce Fields 	nfsd4_client_record_create(clp);
4163ec59659bSJ. Bruce Fields 	inc_reclaim_complete(clp);
4164bcecf1ccSMi Jinlong out:
4165bcecf1ccSMi Jinlong 	return status;
41664dc6ec00SJ. Bruce Fields }
41674dc6ec00SJ. Bruce Fields 
41684dc6ec00SJ. Bruce Fields __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4169b591480bSJ.Bruce Fields nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4170eb69853dSChristoph Hellwig 		  union nfsd4_op_u *u)
41711da177e4SLinus Torvalds {
4172eb69853dSChristoph Hellwig 	struct nfsd4_setclientid *setclid = &u->setclientid;
4173a084daf5SJ. Bruce Fields 	struct xdr_netobj 	clname = setclid->se_name;
41741da177e4SLinus Torvalds 	nfs4_verifier		clverifier = setclid->se_verf;
41753dbacee6STrond Myklebust 	struct nfs4_client	*conf, *new;
41763dbacee6STrond Myklebust 	struct nfs4_client	*unconf = NULL;
4177b37ad28bSAl Viro 	__be32 			status;
4178c212cecfSStanislav Kinsbursky 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4179a55370a3SNeilBrown 
41805cc40fd7STrond Myklebust 	new = create_client(clname, rqstp, &clverifier);
41815cc40fd7STrond Myklebust 	if (new == NULL)
41825cc40fd7STrond Myklebust 		return nfserr_jukebox;
41833dbacee6STrond Myklebust 	spin_lock(&nn->client_lock);
4184382a62e7SStanislav Kinsbursky 	conf = find_confirmed_client_by_name(&clname, nn);
41852b634821SJ. Bruce Fields 	if (conf && client_has_state(conf)) {
41861da177e4SLinus Torvalds 		status = nfserr_clid_inuse;
4187e203d506SJ. Bruce Fields 		if (clp_used_exchangeid(conf))
4188e203d506SJ. Bruce Fields 			goto out;
4189026722c2SJ. Bruce Fields 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
419027787733SChuck Lever 			trace_nfsd_clid_cred_mismatch(conf, rqstp);
41911da177e4SLinus Torvalds 			goto out;
41921da177e4SLinus Torvalds 		}
41931da177e4SLinus Torvalds 	}
4194a99454aaSStanislav Kinsbursky 	unconf = find_unconfirmed_client_by_name(&clname, nn);
41951da177e4SLinus Torvalds 	if (unconf)
41963dbacee6STrond Myklebust 		unhash_client_locked(unconf);
4197744ea54cSChuck Lever 	if (conf) {
4198744ea54cSChuck Lever 		if (same_verf(&conf->cl_verifier, &clverifier)) {
41991da177e4SLinus Torvalds 			copy_clid(new, conf);
420041eb1670SKinglong Mee 			gen_confirm(new, nn);
4201744ea54cSChuck Lever 		} else
4202744ea54cSChuck Lever 			trace_nfsd_clid_verf_mismatch(conf, rqstp,
4203744ea54cSChuck Lever 						      &clverifier);
4204237f91c8SChuck Lever 	} else
4205237f91c8SChuck Lever 		trace_nfsd_clid_fresh(new);
42068323c3b2SJ. Bruce Fields 	new->cl_minorversion = 0;
42076f3d772fSTakuma Umeya 	gen_callback(new, setclid, rqstp);
4208ac55fdc4SJeff Layton 	add_to_unconfirmed(new);
42091da177e4SLinus Torvalds 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
42101da177e4SLinus Torvalds 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
42111da177e4SLinus Torvalds 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
42125cc40fd7STrond Myklebust 	new = NULL;
42131da177e4SLinus Torvalds 	status = nfs_ok;
42141da177e4SLinus Torvalds out:
42153dbacee6STrond Myklebust 	spin_unlock(&nn->client_lock);
42165cc40fd7STrond Myklebust 	if (new)
42175cc40fd7STrond Myklebust 		free_client(new);
4218237f91c8SChuck Lever 	if (unconf) {
4219237f91c8SChuck Lever 		trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
42203dbacee6STrond Myklebust 		expire_client(unconf);
4221237f91c8SChuck Lever 	}
42221da177e4SLinus Torvalds 	return status;
42231da177e4SLinus Torvalds }
42241da177e4SLinus Torvalds 
4225b37ad28bSAl Viro __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4226b591480bSJ.Bruce Fields nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4227b591480bSJ.Bruce Fields 			struct nfsd4_compound_state *cstate,
4228eb69853dSChristoph Hellwig 			union nfsd4_op_u *u)
42291da177e4SLinus Torvalds {
4230eb69853dSChristoph Hellwig 	struct nfsd4_setclientid_confirm *setclientid_confirm =
4231eb69853dSChristoph Hellwig 			&u->setclientid_confirm;
423221ab45a4SNeilBrown 	struct nfs4_client *conf, *unconf;
4233d20c11d8SJeff Layton 	struct nfs4_client *old = NULL;
42341da177e4SLinus Torvalds 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
42351da177e4SLinus Torvalds 	clientid_t * clid = &setclientid_confirm->sc_clientid;
4236b37ad28bSAl Viro 	__be32 status;
42377f2210faSStanislav Kinsbursky 	struct nfsd_net	*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
42381da177e4SLinus Torvalds 
42392c142baaSStanislav Kinsbursky 	if (STALE_CLIENTID(clid, nn))
42401da177e4SLinus Torvalds 		return nfserr_stale_clientid;
424121ab45a4SNeilBrown 
4242d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
42438daae4dcSStanislav Kinsbursky 	conf = find_confirmed_client(clid, false, nn);
42440a7ec377SStanislav Kinsbursky 	unconf = find_unconfirmed_client(clid, false, nn);
4245a186e767SJ. Bruce Fields 	/*
42468695b90aSJ. Bruce Fields 	 * We try hard to give out unique clientid's, so if we get an
42478695b90aSJ. Bruce Fields 	 * attempt to confirm the same clientid with a different cred,
4248f984a7ceSJ. Bruce Fields 	 * the client may be buggy; this should never happen.
4249f984a7ceSJ. Bruce Fields 	 *
4250f984a7ceSJ. Bruce Fields 	 * Nevertheless, RFC 7530 recommends INUSE for this case:
4251a186e767SJ. Bruce Fields 	 */
4252f984a7ceSJ. Bruce Fields 	status = nfserr_clid_inuse;
425327787733SChuck Lever 	if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
425427787733SChuck Lever 		trace_nfsd_clid_cred_mismatch(unconf, rqstp);
42558695b90aSJ. Bruce Fields 		goto out;
425627787733SChuck Lever 	}
425727787733SChuck Lever 	if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
425827787733SChuck Lever 		trace_nfsd_clid_cred_mismatch(conf, rqstp);
42598695b90aSJ. Bruce Fields 		goto out;
426027787733SChuck Lever 	}
426190d700b7SJ. Bruce Fields 	if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
42627d22fc11SJ. Bruce Fields 		if (conf && same_verf(&confirm, &conf->cl_confirm)) {
426390d700b7SJ. Bruce Fields 			status = nfs_ok;
4264237f91c8SChuck Lever 		} else
426590d700b7SJ. Bruce Fields 			status = nfserr_stale_clientid;
426690d700b7SJ. Bruce Fields 		goto out;
426790d700b7SJ. Bruce Fields 	}
426890d700b7SJ. Bruce Fields 	status = nfs_ok;
4269237f91c8SChuck Lever 	if (conf) {
4270d20c11d8SJeff Layton 		old = unconf;
4271d20c11d8SJeff Layton 		unhash_client_locked(old);
42725a3c9d71SJ. Bruce Fields 		nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4273237f91c8SChuck Lever 	} else {
4274d20c11d8SJeff Layton 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4275d20c11d8SJeff Layton 		if (old) {
42762b634821SJ. Bruce Fields 			status = nfserr_clid_inuse;
42772b634821SJ. Bruce Fields 			if (client_has_state(old)
42782b634821SJ. Bruce Fields 					&& !same_creds(&unconf->cl_cred,
4279ab451ea9SDai Ngo 							&old->cl_cred)) {
4280ab451ea9SDai Ngo 				old = NULL;
42812b634821SJ. Bruce Fields 				goto out;
4282ab451ea9SDai Ngo 			}
4283d20c11d8SJeff Layton 			status = mark_client_expired_locked(old);
42847abea1e8SJeff Layton 			if (status) {
42857abea1e8SJeff Layton 				old = NULL;
4286221a6876SJ. Bruce Fields 				goto out;
4287221a6876SJ. Bruce Fields 			}
42882958d2eeSChuck Lever 			trace_nfsd_clid_replaced(&old->cl_clientid);
42897abea1e8SJeff Layton 		}
42901da177e4SLinus Torvalds 		move_to_confirmed(unconf);
4291d20c11d8SJeff Layton 		conf = unconf;
429208e8987cSNeilBrown 	}
4293d20c11d8SJeff Layton 	get_client_locked(conf);
4294d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
4295934bd07fSJ. Bruce Fields 	if (conf == unconf)
4296934bd07fSJ. Bruce Fields 		fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
4297d20c11d8SJeff Layton 	nfsd4_probe_callback(conf);
4298d20c11d8SJeff Layton 	spin_lock(&nn->client_lock);
4299d20c11d8SJeff Layton 	put_client_renew_locked(conf);
43001da177e4SLinus Torvalds out:
4301d20c11d8SJeff Layton 	spin_unlock(&nn->client_lock);
4302d20c11d8SJeff Layton 	if (old)
4303d20c11d8SJeff Layton 		expire_client(old);
43041da177e4SLinus Torvalds 	return status;
43051da177e4SLinus Torvalds }
43061da177e4SLinus Torvalds 
nfsd4_alloc_file(void)430732513b40SJ. Bruce Fields static struct nfs4_file *nfsd4_alloc_file(void)
43081da177e4SLinus Torvalds {
430932513b40SJ. Bruce Fields 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
431032513b40SJ. Bruce Fields }
431132513b40SJ. Bruce Fields 
431232513b40SJ. Bruce Fields /* OPEN Share state helper functions */
4313950e0118STrond Myklebust 
nfsd4_file_init(const struct svc_fh * fh,struct nfs4_file * fp)431481a21fa3SChuck Lever static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
431581a21fa3SChuck Lever {
4316818a34ebSElena Reshetova 	refcount_set(&fp->fi_ref, 1);
43171d31a253STrond Myklebust 	spin_lock_init(&fp->fi_lock);
43188beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_stateids);
43198beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_delegations);
43208287f009SSachin Bhamare 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4321f9b60e22SJ. Bruce Fields 	fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
43220c637be8SJeff Layton 	fp->fi_deleg_file = NULL;
432347f9940cSMeelap Shah 	fp->fi_had_conflict = false;
4324baeb4ff0SJeff Layton 	fp->fi_share_deny = 0;
4325f9d7562fSJ. Bruce Fields 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4326f9d7562fSJ. Bruce Fields 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
4327a0ce4837SJ. Bruce Fields 	fp->fi_aliased = false;
4328a0ce4837SJ. Bruce Fields 	fp->fi_inode = d_inode(fh->fh_dentry);
43299cf514ccSChristoph Hellwig #ifdef CONFIG_NFSD_PNFS
43309cf514ccSChristoph Hellwig 	INIT_LIST_HEAD(&fp->fi_lo_states);
4331c5c707f9SChristoph Hellwig 	atomic_set(&fp->fi_lo_recalls, 0);
43329cf514ccSChristoph Hellwig #endif
43331da177e4SLinus Torvalds }
43341da177e4SLinus Torvalds 
4335e8ff2a84SJ. Bruce Fields void
nfsd4_free_slabs(void)4336e60d4398SNeilBrown nfsd4_free_slabs(void)
4337e60d4398SNeilBrown {
43389258a2d5SJeff Layton 	kmem_cache_destroy(client_slab);
4339abf1135bSChristoph Hellwig 	kmem_cache_destroy(openowner_slab);
4340abf1135bSChristoph Hellwig 	kmem_cache_destroy(lockowner_slab);
4341abf1135bSChristoph Hellwig 	kmem_cache_destroy(file_slab);
4342abf1135bSChristoph Hellwig 	kmem_cache_destroy(stateid_slab);
4343abf1135bSChristoph Hellwig 	kmem_cache_destroy(deleg_slab);
43449258a2d5SJeff Layton 	kmem_cache_destroy(odstate_slab);
4345e60d4398SNeilBrown }
43461da177e4SLinus Torvalds 
434772083396SBryan Schumaker int
nfsd4_init_slabs(void)43481da177e4SLinus Torvalds nfsd4_init_slabs(void)
43491da177e4SLinus Torvalds {
43509258a2d5SJeff Layton 	client_slab = kmem_cache_create("nfsd4_clients",
43519258a2d5SJeff Layton 			sizeof(struct nfs4_client), 0, 0, NULL);
43529258a2d5SJeff Layton 	if (client_slab == NULL)
43539258a2d5SJeff Layton 		goto out;
4354fe0750e5SJ. Bruce Fields 	openowner_slab = kmem_cache_create("nfsd4_openowners",
4355fe0750e5SJ. Bruce Fields 			sizeof(struct nfs4_openowner), 0, 0, NULL);
4356fe0750e5SJ. Bruce Fields 	if (openowner_slab == NULL)
43579258a2d5SJeff Layton 		goto out_free_client_slab;
4358fe0750e5SJ. Bruce Fields 	lockowner_slab = kmem_cache_create("nfsd4_lockowners",
43593c40794bSYanchuan Nian 			sizeof(struct nfs4_lockowner), 0, 0, NULL);
4360fe0750e5SJ. Bruce Fields 	if (lockowner_slab == NULL)
4361abf1135bSChristoph Hellwig 		goto out_free_openowner_slab;
4362e60d4398SNeilBrown 	file_slab = kmem_cache_create("nfsd4_files",
436320c2df83SPaul Mundt 			sizeof(struct nfs4_file), 0, 0, NULL);
4364e60d4398SNeilBrown 	if (file_slab == NULL)
4365abf1135bSChristoph Hellwig 		goto out_free_lockowner_slab;
43665ac049acSNeilBrown 	stateid_slab = kmem_cache_create("nfsd4_stateids",
4367dcef0413SJ. Bruce Fields 			sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
43685ac049acSNeilBrown 	if (stateid_slab == NULL)
4369abf1135bSChristoph Hellwig 		goto out_free_file_slab;
43705b2d21c1SNeilBrown 	deleg_slab = kmem_cache_create("nfsd4_delegations",
437120c2df83SPaul Mundt 			sizeof(struct nfs4_delegation), 0, 0, NULL);
43725b2d21c1SNeilBrown 	if (deleg_slab == NULL)
4373abf1135bSChristoph Hellwig 		goto out_free_stateid_slab;
43748287f009SSachin Bhamare 	odstate_slab = kmem_cache_create("nfsd4_odstate",
43758287f009SSachin Bhamare 			sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
43768287f009SSachin Bhamare 	if (odstate_slab == NULL)
43778287f009SSachin Bhamare 		goto out_free_deleg_slab;
4378e60d4398SNeilBrown 	return 0;
4379abf1135bSChristoph Hellwig 
43808287f009SSachin Bhamare out_free_deleg_slab:
43818287f009SSachin Bhamare 	kmem_cache_destroy(deleg_slab);
4382abf1135bSChristoph Hellwig out_free_stateid_slab:
4383abf1135bSChristoph Hellwig 	kmem_cache_destroy(stateid_slab);
4384abf1135bSChristoph Hellwig out_free_file_slab:
4385abf1135bSChristoph Hellwig 	kmem_cache_destroy(file_slab);
4386abf1135bSChristoph Hellwig out_free_lockowner_slab:
4387abf1135bSChristoph Hellwig 	kmem_cache_destroy(lockowner_slab);
4388abf1135bSChristoph Hellwig out_free_openowner_slab:
4389abf1135bSChristoph Hellwig 	kmem_cache_destroy(openowner_slab);
43909258a2d5SJeff Layton out_free_client_slab:
43919258a2d5SJeff Layton 	kmem_cache_destroy(client_slab);
4392abf1135bSChristoph Hellwig out:
43931da177e4SLinus Torvalds 	return -ENOMEM;
43941da177e4SLinus Torvalds }
43951da177e4SLinus Torvalds 
43967746b32fSDai Ngo static unsigned long
nfsd4_state_shrinker_count(struct shrinker * shrink,struct shrink_control * sc)4397a1049eb4SDai Ngo nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
43987746b32fSDai Ngo {
439944df6f43SDai Ngo 	int count;
44007746b32fSDai Ngo 	struct nfsd_net *nn = container_of(shrink,
44017746b32fSDai Ngo 			struct nfsd_net, nfsd_client_shrinker);
44027746b32fSDai Ngo 
440344df6f43SDai Ngo 	count = atomic_read(&nn->nfsd_courtesy_clients);
440444df6f43SDai Ngo 	if (!count)
440544df6f43SDai Ngo 		count = atomic_long_read(&num_delegations);
440644df6f43SDai Ngo 	if (count)
44077c24fa22SDai Ngo 		queue_work(laundry_wq, &nn->nfsd_shrinker_work);
440844df6f43SDai Ngo 	return (unsigned long)count;
44097746b32fSDai Ngo }
44107746b32fSDai Ngo 
44117746b32fSDai Ngo static unsigned long
nfsd4_state_shrinker_scan(struct shrinker * shrink,struct shrink_control * sc)4412a1049eb4SDai Ngo nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
44137746b32fSDai Ngo {
44147746b32fSDai Ngo 	return SHRINK_STOP;
44157746b32fSDai Ngo }
44167746b32fSDai Ngo 
4417f385f7d2SDai Ngo void
nfsd4_init_leases_net(struct nfsd_net * nn)44187746b32fSDai Ngo nfsd4_init_leases_net(struct nfsd_net *nn)
44196867137eSDai Ngo {
44204271c2c0SDai Ngo 	struct sysinfo si;
44214271c2c0SDai Ngo 	u64 max_clients;
44224271c2c0SDai Ngo 
44236867137eSDai Ngo 	nn->nfsd4_lease = 90;	/* default lease time */
44246867137eSDai Ngo 	nn->nfsd4_grace = 90;
44256867137eSDai Ngo 	nn->somebody_reclaimed = false;
44266867137eSDai Ngo 	nn->track_reclaim_completes = false;
4427a251c17aSJason A. Donenfeld 	nn->clverifier_counter = get_random_u32();
4428a251c17aSJason A. Donenfeld 	nn->clientid_base = get_random_u32();
44296867137eSDai Ngo 	nn->clientid_counter = nn->clientid_base + 1;
44306867137eSDai Ngo 	nn->s2s_cp_cl_id = nn->clientid_counter++;
44310926c395SDai Ngo 
44320926c395SDai Ngo 	atomic_set(&nn->nfs4_client_count, 0);
44334271c2c0SDai Ngo 	si_meminfo(&si);
44344271c2c0SDai Ngo 	max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
44354271c2c0SDai Ngo 	max_clients *= NFS4_CLIENTS_PER_GB;
44364271c2c0SDai Ngo 	nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
44373a4ea23dSDai Ngo 
44383a4ea23dSDai Ngo 	atomic_set(&nn->nfsd_courtesy_clients, 0);
44396867137eSDai Ngo }
44406867137eSDai Ngo 
init_nfs4_replay(struct nfs4_replay * rp)4441ff194bd9SJ. Bruce Fields static void init_nfs4_replay(struct nfs4_replay *rp)
4442ff194bd9SJ. Bruce Fields {
4443ff194bd9SJ. Bruce Fields 	rp->rp_status = nfserr_serverfault;
4444ff194bd9SJ. Bruce Fields 	rp->rp_buflen = 0;
4445ff194bd9SJ. Bruce Fields 	rp->rp_buf = rp->rp_ibuf;
444658fb12e6SJeff Layton 	mutex_init(&rp->rp_mutex);
444758fb12e6SJeff Layton }
444858fb12e6SJeff Layton 
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)444958fb12e6SJeff Layton static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
445058fb12e6SJeff Layton 		struct nfs4_stateowner *so)
445158fb12e6SJeff Layton {
445258fb12e6SJeff Layton 	if (!nfsd4_has_session(cstate)) {
445358fb12e6SJeff Layton 		mutex_lock(&so->so_replay.rp_mutex);
4454b5971afaSKinglong Mee 		cstate->replay_owner = nfs4_get_stateowner(so);
445558fb12e6SJeff Layton 	}
445658fb12e6SJeff Layton }
445758fb12e6SJeff Layton 
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)445858fb12e6SJeff Layton void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
445958fb12e6SJeff Layton {
446058fb12e6SJeff Layton 	struct nfs4_stateowner *so = cstate->replay_owner;
446158fb12e6SJeff Layton 
446258fb12e6SJeff Layton 	if (so != NULL) {
446358fb12e6SJeff Layton 		cstate->replay_owner = NULL;
446458fb12e6SJeff Layton 		mutex_unlock(&so->so_replay.rp_mutex);
446558fb12e6SJeff Layton 		nfs4_put_stateowner(so);
446658fb12e6SJeff Layton 	}
4467ff194bd9SJ. Bruce Fields }
4468ff194bd9SJ. Bruce Fields 
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)4469fe0750e5SJ. Bruce Fields static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
44701da177e4SLinus Torvalds {
44711da177e4SLinus Torvalds 	struct nfs4_stateowner *sop;
44721da177e4SLinus Torvalds 
4473fe0750e5SJ. Bruce Fields 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
4474ff194bd9SJ. Bruce Fields 	if (!sop)
4475ff194bd9SJ. Bruce Fields 		return NULL;
4476ff194bd9SJ. Bruce Fields 
44776f4859b8SJ. Bruce Fields 	xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4478ff194bd9SJ. Bruce Fields 	if (!sop->so_owner.data) {
4479fe0750e5SJ. Bruce Fields 		kmem_cache_free(slab, sop);
4480ff194bd9SJ. Bruce Fields 		return NULL;
4481ff194bd9SJ. Bruce Fields 	}
4482ff194bd9SJ. Bruce Fields 
4483ff194bd9SJ. Bruce Fields 	INIT_LIST_HEAD(&sop->so_stateids);
4484ff194bd9SJ. Bruce Fields 	sop->so_client = clp;
4485ff194bd9SJ. Bruce Fields 	init_nfs4_replay(&sop->so_replay);
44866b180f0bSJeff Layton 	atomic_set(&sop->so_count, 1);
44871da177e4SLinus Torvalds 	return sop;
44881da177e4SLinus Torvalds }
4489ff194bd9SJ. Bruce Fields 
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)4490fe0750e5SJ. Bruce Fields static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4491ff194bd9SJ. Bruce Fields {
4492d4f0489fSTrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
44939b531137SStanislav Kinsbursky 
4494d4f0489fSTrond Myklebust 	list_add(&oo->oo_owner.so_strhash,
4495d4f0489fSTrond Myklebust 		 &clp->cl_ownerstr_hashtbl[strhashval]);
4496fe0750e5SJ. Bruce Fields 	list_add(&oo->oo_perclient, &clp->cl_openowners);
44971da177e4SLinus Torvalds }
44981da177e4SLinus Torvalds 
nfs4_unhash_openowner(struct nfs4_stateowner * so)44998f4b54c5SJeff Layton static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
45008f4b54c5SJeff Layton {
4501d4f0489fSTrond Myklebust 	unhash_openowner_locked(openowner(so));
45028f4b54c5SJeff Layton }
45038f4b54c5SJeff Layton 
nfs4_free_openowner(struct nfs4_stateowner * so)45046b180f0bSJeff Layton static void nfs4_free_openowner(struct nfs4_stateowner *so)
45056b180f0bSJeff Layton {
45066b180f0bSJeff Layton 	struct nfs4_openowner *oo = openowner(so);
45076b180f0bSJeff Layton 
45086b180f0bSJeff Layton 	kmem_cache_free(openowner_slab, oo);
45096b180f0bSJeff Layton }
45106b180f0bSJeff Layton 
45116b180f0bSJeff Layton static const struct nfs4_stateowner_operations openowner_ops = {
45128f4b54c5SJeff Layton 	.so_unhash =	nfs4_unhash_openowner,
45136b180f0bSJeff Layton 	.so_free =	nfs4_free_openowner,
45146b180f0bSJeff Layton };
45156b180f0bSJeff Layton 
45167fc0564eSAndrew Elble static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)45177fc0564eSAndrew Elble nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
45187fc0564eSAndrew Elble {
45197fc0564eSAndrew Elble 	struct nfs4_ol_stateid *local, *ret = NULL;
45207fc0564eSAndrew Elble 	struct nfs4_openowner *oo = open->op_openowner;
45217fc0564eSAndrew Elble 
45227fc0564eSAndrew Elble 	lockdep_assert_held(&fp->fi_lock);
45237fc0564eSAndrew Elble 
45247fc0564eSAndrew Elble 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
45257fc0564eSAndrew Elble 		/* ignore lock owners */
45267fc0564eSAndrew Elble 		if (local->st_stateowner->so_is_open_owner == 0)
45277fc0564eSAndrew Elble 			continue;
452815ca08d3STrond Myklebust 		if (local->st_stateowner != &oo->oo_owner)
452915ca08d3STrond Myklebust 			continue;
453015ca08d3STrond Myklebust 		if (local->st_stid.sc_type == NFS4_OPEN_STID) {
45317fc0564eSAndrew Elble 			ret = local;
4532a15dfcd5SElena Reshetova 			refcount_inc(&ret->st_stid.sc_count);
45337fc0564eSAndrew Elble 			break;
45347fc0564eSAndrew Elble 		}
45357fc0564eSAndrew Elble 	}
45367fc0564eSAndrew Elble 	return ret;
45377fc0564eSAndrew Elble }
45387fc0564eSAndrew Elble 
453915ca08d3STrond Myklebust static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)454015ca08d3STrond Myklebust nfsd4_verify_open_stid(struct nfs4_stid *s)
454115ca08d3STrond Myklebust {
454215ca08d3STrond Myklebust 	__be32 ret = nfs_ok;
454315ca08d3STrond Myklebust 
454415ca08d3STrond Myklebust 	switch (s->sc_type) {
454515ca08d3STrond Myklebust 	default:
454615ca08d3STrond Myklebust 		break;
45474f176417STrond Myklebust 	case 0:
454815ca08d3STrond Myklebust 	case NFS4_CLOSED_STID:
454915ca08d3STrond Myklebust 	case NFS4_CLOSED_DELEG_STID:
455015ca08d3STrond Myklebust 		ret = nfserr_bad_stateid;
455115ca08d3STrond Myklebust 		break;
455215ca08d3STrond Myklebust 	case NFS4_REVOKED_DELEG_STID:
455315ca08d3STrond Myklebust 		ret = nfserr_deleg_revoked;
455415ca08d3STrond Myklebust 	}
455515ca08d3STrond Myklebust 	return ret;
455615ca08d3STrond Myklebust }
455715ca08d3STrond Myklebust 
455815ca08d3STrond Myklebust /* Lock the stateid st_mutex, and deal with races with CLOSE */
455915ca08d3STrond Myklebust static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)456015ca08d3STrond Myklebust nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
456115ca08d3STrond Myklebust {
456215ca08d3STrond Myklebust 	__be32 ret;
456315ca08d3STrond Myklebust 
45644f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
456515ca08d3STrond Myklebust 	ret = nfsd4_verify_open_stid(&stp->st_stid);
456615ca08d3STrond Myklebust 	if (ret != nfs_ok)
456715ca08d3STrond Myklebust 		mutex_unlock(&stp->st_mutex);
456815ca08d3STrond Myklebust 	return ret;
456915ca08d3STrond Myklebust }
457015ca08d3STrond Myklebust 
457115ca08d3STrond Myklebust static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)457215ca08d3STrond Myklebust nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
457315ca08d3STrond Myklebust {
457415ca08d3STrond Myklebust 	struct nfs4_ol_stateid *stp;
457515ca08d3STrond Myklebust 	for (;;) {
457615ca08d3STrond Myklebust 		spin_lock(&fp->fi_lock);
457715ca08d3STrond Myklebust 		stp = nfsd4_find_existing_open(fp, open);
457815ca08d3STrond Myklebust 		spin_unlock(&fp->fi_lock);
457915ca08d3STrond Myklebust 		if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
458015ca08d3STrond Myklebust 			break;
458115ca08d3STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
458215ca08d3STrond Myklebust 	}
458315ca08d3STrond Myklebust 	return stp;
458415ca08d3STrond Myklebust }
458515ca08d3STrond Myklebust 
4586fe0750e5SJ. Bruce Fields static struct nfs4_openowner *
alloc_init_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)458713d6f66bSTrond Myklebust alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4588db24b3b4SJeff Layton 			   struct nfsd4_compound_state *cstate)
4589db24b3b4SJeff Layton {
459013d6f66bSTrond Myklebust 	struct nfs4_client *clp = cstate->clp;
45917ffb5880STrond Myklebust 	struct nfs4_openowner *oo, *ret;
45921da177e4SLinus Torvalds 
4593fe0750e5SJ. Bruce Fields 	oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4594fe0750e5SJ. Bruce Fields 	if (!oo)
45951da177e4SLinus Torvalds 		return NULL;
45966b180f0bSJeff Layton 	oo->oo_owner.so_ops = &openowner_ops;
4597fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_is_open_owner = 1;
4598fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_seqid = open->op_seqid;
4599d3134b10SJeff Layton 	oo->oo_flags = 0;
4600db24b3b4SJeff Layton 	if (nfsd4_has_session(cstate))
4601db24b3b4SJeff Layton 		oo->oo_flags |= NFS4_OO_CONFIRMED;
4602fe0750e5SJ. Bruce Fields 	oo->oo_time = 0;
460338c387b5SJ. Bruce Fields 	oo->oo_last_closed_stid = NULL;
4604fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&oo->oo_close_lru);
4605d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
4606d4f0489fSTrond Myklebust 	ret = find_openstateowner_str_locked(strhashval, open, clp);
46077ffb5880STrond Myklebust 	if (ret == NULL) {
4608fe0750e5SJ. Bruce Fields 		hash_openowner(oo, clp, strhashval);
46097ffb5880STrond Myklebust 		ret = oo;
46107ffb5880STrond Myklebust 	} else
4611d50ffdedSKinglong Mee 		nfs4_free_stateowner(&oo->oo_owner);
4612d50ffdedSKinglong Mee 
4613d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
4614c5952338SJeff Layton 	return ret;
46151da177e4SLinus Torvalds }
46161da177e4SLinus Torvalds 
46177fc0564eSAndrew Elble static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)46188c7245abSOleg Drokin init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
46197fc0564eSAndrew Elble {
46201da177e4SLinus Torvalds 
46217fc0564eSAndrew Elble 	struct nfs4_openowner *oo = open->op_openowner;
46227fc0564eSAndrew Elble 	struct nfs4_ol_stateid *retstp = NULL;
46238c7245abSOleg Drokin 	struct nfs4_ol_stateid *stp;
46247fc0564eSAndrew Elble 
46258c7245abSOleg Drokin 	stp = open->op_stp;
46265cc1fb2aSOleg Drokin 	/* We are moving these outside of the spinlocks to avoid the warnings */
46275cc1fb2aSOleg Drokin 	mutex_init(&stp->st_mutex);
46284f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
46295cc1fb2aSOleg Drokin 
463015ca08d3STrond Myklebust retry:
46317fc0564eSAndrew Elble 	spin_lock(&oo->oo_owner.so_client->cl_lock);
46327fc0564eSAndrew Elble 	spin_lock(&fp->fi_lock);
46337fc0564eSAndrew Elble 
46347fc0564eSAndrew Elble 	retstp = nfsd4_find_existing_open(fp, open);
46357fc0564eSAndrew Elble 	if (retstp)
46367fc0564eSAndrew Elble 		goto out_unlock;
46378c7245abSOleg Drokin 
46388c7245abSOleg Drokin 	open->op_stp = NULL;
4639a15dfcd5SElena Reshetova 	refcount_inc(&stp->st_stid.sc_count);
46403abdb607SJ. Bruce Fields 	stp->st_stid.sc_type = NFS4_OPEN_STID;
46413c87b9b7STrond Myklebust 	INIT_LIST_HEAD(&stp->st_locks);
4642b5971afaSKinglong Mee 	stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
464313cd2184SNeilBrown 	get_nfs4_file(fp);
464411b9164aSTrond Myklebust 	stp->st_stid.sc_file = fp;
46451da177e4SLinus Torvalds 	stp->st_access_bmap = 0;
46461da177e4SLinus Torvalds 	stp->st_deny_bmap = 0;
46474c4cd222SNeilBrown 	stp->st_openstp = NULL;
46481c755dc1SJeff Layton 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
46491d31a253STrond Myklebust 	list_add(&stp->st_perfile, &fp->fi_stateids);
46507fc0564eSAndrew Elble 
46517fc0564eSAndrew Elble out_unlock:
46521d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
46531c755dc1SJeff Layton 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
46545cc1fb2aSOleg Drokin 	if (retstp) {
465515ca08d3STrond Myklebust 		/* Handle races with CLOSE */
465615ca08d3STrond Myklebust 		if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
465715ca08d3STrond Myklebust 			nfs4_put_stid(&retstp->st_stid);
465815ca08d3STrond Myklebust 			goto retry;
465915ca08d3STrond Myklebust 		}
46608c7245abSOleg Drokin 		/* To keep mutex tracking happy */
46615cc1fb2aSOleg Drokin 		mutex_unlock(&stp->st_mutex);
46628c7245abSOleg Drokin 		stp = retstp;
46635cc1fb2aSOleg Drokin 	}
46648c7245abSOleg Drokin 	return stp;
46651da177e4SLinus Torvalds }
46661da177e4SLinus Torvalds 
4667d3134b10SJeff Layton /*
4668d3134b10SJeff Layton  * In the 4.0 case we need to keep the owners around a little while to handle
4669d3134b10SJeff Layton  * CLOSE replay. We still do need to release any file access that is held by
4670d3134b10SJeff Layton  * them before returning however.
4671d3134b10SJeff Layton  */
46721da177e4SLinus Torvalds static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)4673d3134b10SJeff Layton move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
46741da177e4SLinus Torvalds {
4675217526e7SJeff Layton 	struct nfs4_ol_stateid *last;
4676d3134b10SJeff Layton 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
4677d3134b10SJeff Layton 	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4678d3134b10SJeff Layton 						nfsd_net_id);
467973758fedSStanislav Kinsbursky 
4680fe0750e5SJ. Bruce Fields 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
46811da177e4SLinus Torvalds 
4682b401be22SJeff Layton 	/*
4683b401be22SJeff Layton 	 * We know that we hold one reference via nfsd4_close, and another
4684b401be22SJeff Layton 	 * "persistent" reference for the client. If the refcount is higher
4685b401be22SJeff Layton 	 * than 2, then there are still calls in progress that are using this
4686b401be22SJeff Layton 	 * stateid. We can't put the sc_file reference until they are finished.
4687b401be22SJeff Layton 	 * Wait for the refcount to drop to 2. Since it has been unhashed,
4688b401be22SJeff Layton 	 * there should be no danger of the refcount going back up again at
4689b401be22SJeff Layton 	 * this point.
4690b401be22SJeff Layton 	 */
4691a15dfcd5SElena Reshetova 	wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4692b401be22SJeff Layton 
4693d3134b10SJeff Layton 	release_all_access(s);
4694d3134b10SJeff Layton 	if (s->st_stid.sc_file) {
4695d3134b10SJeff Layton 		put_nfs4_file(s->st_stid.sc_file);
4696d3134b10SJeff Layton 		s->st_stid.sc_file = NULL;
4697d3134b10SJeff Layton 	}
4698217526e7SJeff Layton 
4699217526e7SJeff Layton 	spin_lock(&nn->client_lock);
4700217526e7SJeff Layton 	last = oo->oo_last_closed_stid;
4701d3134b10SJeff Layton 	oo->oo_last_closed_stid = s;
470273758fedSStanislav Kinsbursky 	list_move_tail(&oo->oo_close_lru, &nn->close_lru);
470320b7d86fSArnd Bergmann 	oo->oo_time = ktime_get_boottime_seconds();
4704217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
4705217526e7SJeff Layton 	if (last)
4706217526e7SJeff Layton 		nfs4_put_stid(&last->st_stid);
47071da177e4SLinus Torvalds }
47081da177e4SLinus Torvalds 
470915424748SChuck Lever static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_lookup(const struct svc_fh * fhp)471015424748SChuck Lever nfsd4_file_hash_lookup(const struct svc_fh *fhp)
47111da177e4SLinus Torvalds {
4712d47b295eSChuck Lever 	struct inode *inode = d_inode(fhp->fh_dentry);
4713d47b295eSChuck Lever 	struct rhlist_head *tmp, *list;
471415424748SChuck Lever 	struct nfs4_file *fi;
47151da177e4SLinus Torvalds 
47165b095e99SJeff Layton 	rcu_read_lock();
4717d47b295eSChuck Lever 	list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4718d47b295eSChuck Lever 			       nfs4_file_rhash_params);
4719d47b295eSChuck Lever 	rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
472015424748SChuck Lever 		if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
472115424748SChuck Lever 			if (refcount_inc_not_zero(&fi->fi_ref)) {
47225b095e99SJeff Layton 				rcu_read_unlock();
472315424748SChuck Lever 				return fi;
4724950e0118STrond Myklebust 			}
4725950e0118STrond Myklebust 		}
472615424748SChuck Lever 	}
472715424748SChuck Lever 	rcu_read_unlock();
4728950e0118STrond Myklebust 	return NULL;
4729950e0118STrond Myklebust }
4730950e0118STrond Myklebust 
47319270fc51SChuck Lever /*
47329270fc51SChuck Lever  * On hash insertion, identify entries with the same inode but
4733d47b295eSChuck Lever  * distinct filehandles. They will all be on the list returned
4734d47b295eSChuck Lever  * by rhltable_lookup().
4735d47b295eSChuck Lever  *
4736d47b295eSChuck Lever  * inode->i_lock prevents racing insertions from adding an entry
4737d47b295eSChuck Lever  * for the same inode/fhp pair twice.
47389270fc51SChuck Lever  */
47399270fc51SChuck Lever static noinline_for_stack struct nfs4_file *
nfsd4_file_hash_insert(struct nfs4_file * new,const struct svc_fh * fhp)47409270fc51SChuck Lever nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
4741950e0118STrond Myklebust {
4742d47b295eSChuck Lever 	struct inode *inode = d_inode(fhp->fh_dentry);
4743d47b295eSChuck Lever 	struct rhlist_head *tmp, *list;
4744950e0118STrond Myklebust 	struct nfs4_file *ret = NULL;
4745ca943217STrond Myklebust 	bool alias_found = false;
47469270fc51SChuck Lever 	struct nfs4_file *fi;
4747d47b295eSChuck Lever 	int err;
4748950e0118STrond Myklebust 
4749d47b295eSChuck Lever 	rcu_read_lock();
4750d47b295eSChuck Lever 	spin_lock(&inode->i_lock);
4751d47b295eSChuck Lever 
4752d47b295eSChuck Lever 	list = rhltable_lookup(&nfs4_file_rhltable, &inode,
4753d47b295eSChuck Lever 			       nfs4_file_rhash_params);
4754d47b295eSChuck Lever 	rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
47559270fc51SChuck Lever 		if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
47569270fc51SChuck Lever 			if (refcount_inc_not_zero(&fi->fi_ref))
47579270fc51SChuck Lever 				ret = fi;
4758d47b295eSChuck Lever 		} else
47599270fc51SChuck Lever 			fi->fi_aliased = alias_found = true;
47601da177e4SLinus Torvalds 	}
4761d47b295eSChuck Lever 	if (ret)
4762d47b295eSChuck Lever 		goto out_unlock;
4763d47b295eSChuck Lever 
47649270fc51SChuck Lever 	nfsd4_file_init(fhp, new);
4765d47b295eSChuck Lever 	err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
4766d47b295eSChuck Lever 			      nfs4_file_rhash_params);
4767d47b295eSChuck Lever 	if (err)
4768d47b295eSChuck Lever 		goto out_unlock;
4769d47b295eSChuck Lever 
47701da177e4SLinus Torvalds 	new->fi_aliased = alias_found;
4771cdc97505SBenny Halevy 	ret = new;
4772d47b295eSChuck Lever 
4773d47b295eSChuck Lever out_unlock:
4774d47b295eSChuck Lever 	spin_unlock(&inode->i_lock);
4775d47b295eSChuck Lever 	rcu_read_unlock();
477613cd2184SNeilBrown 	return ret;
4777cdc97505SBenny Halevy }
47781da177e4SLinus Torvalds 
nfsd4_file_hash_remove(struct nfs4_file * fi)47793341678fSChuck Lever static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
4780950e0118STrond Myklebust {
4781d47b295eSChuck Lever 	rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
4782d47b295eSChuck Lever 			nfs4_file_rhash_params);
4783950e0118STrond Myklebust }
4784950e0118STrond Myklebust 
47854f83aa30SJ. Bruce Fields /*
47861da177e4SLinus Torvalds  * Called to check deny when READ with all zero stateid or
47871da177e4SLinus Torvalds  * WRITE with all zero or all one stateid
47881da177e4SLinus Torvalds  */
4789b37ad28bSAl Viro static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)47901da177e4SLinus Torvalds nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
47911da177e4SLinus Torvalds {
47921da177e4SLinus Torvalds 	struct nfs4_file *fp;
4793baeb4ff0SJeff Layton 	__be32 ret = nfs_ok;
47941da177e4SLinus Torvalds 
479515424748SChuck Lever 	fp = nfsd4_file_hash_lookup(current_fh);
479613cd2184SNeilBrown 	if (!fp)
4797baeb4ff0SJeff Layton 		return ret;
479815424748SChuck Lever 
4799baeb4ff0SJeff Layton 	/* Check for conflicting share reservations */
48001d31a253STrond Myklebust 	spin_lock(&fp->fi_lock);
4801baeb4ff0SJeff Layton 	if (fp->fi_share_deny & deny_type)
4802baeb4ff0SJeff Layton 		ret = nfserr_locked;
48031d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
480413cd2184SNeilBrown 	put_nfs4_file(fp);
480513cd2184SNeilBrown 	return ret;
48061da177e4SLinus Torvalds }
48071da177e4SLinus Torvalds 
nfsd4_deleg_present(const struct inode * inode)4808c035362eSChuck Lever static bool nfsd4_deleg_present(const struct inode *inode)
4809c035362eSChuck Lever {
481077c67530SJeff Layton 	struct file_lock_context *ctx = locks_inode_context(inode);
4811c035362eSChuck Lever 
4812c035362eSChuck Lever 	return ctx && !list_empty_careful(&ctx->flc_lease);
4813c035362eSChuck Lever }
4814c035362eSChuck Lever 
4815c035362eSChuck Lever /**
4816c035362eSChuck Lever  * nfsd_wait_for_delegreturn - wait for delegations to be returned
4817c035362eSChuck Lever  * @rqstp: the RPC transaction being executed
4818c035362eSChuck Lever  * @inode: in-core inode of the file being waited for
4819c035362eSChuck Lever  *
4820c035362eSChuck Lever  * The timeout prevents deadlock if all nfsd threads happen to be
4821c035362eSChuck Lever  * tied up waiting for returning delegations.
4822c035362eSChuck Lever  *
4823c035362eSChuck Lever  * Return values:
4824c035362eSChuck Lever  *   %true: delegation was returned
4825c035362eSChuck Lever  *   %false: timed out waiting for delegreturn
4826c035362eSChuck Lever  */
nfsd_wait_for_delegreturn(struct svc_rqst * rqstp,struct inode * inode)4827c035362eSChuck Lever bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
4828c035362eSChuck Lever {
4829c035362eSChuck Lever 	long __maybe_unused timeo;
4830c035362eSChuck Lever 
4831c035362eSChuck Lever 	timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
4832c035362eSChuck Lever 				       NFSD_DELEGRETURN_TIMEOUT);
4833c035362eSChuck Lever 	trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
4834c035362eSChuck Lever 	return timeo > 0;
4835c035362eSChuck Lever }
4836c035362eSChuck Lever 
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)48370162ac2bSChristoph Hellwig static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
48381da177e4SLinus Torvalds {
48390162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
484011b9164aSTrond Myklebust 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
484111b9164aSTrond Myklebust 					  nfsd_net_id);
4842e8c69d17SJ. Bruce Fields 
484311b9164aSTrond Myklebust 	block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4844f54fe962SJeff Layton 
484502e1215fSJeff Layton 	/*
484602e1215fSJeff Layton 	 * We can't do this in nfsd_break_deleg_cb because it is
4847f54fe962SJeff Layton 	 * already holding inode->i_lock.
4848f54fe962SJeff Layton 	 *
4849dff1399fSJeff Layton 	 * If the dl_time != 0, then we know that it has already been
4850dff1399fSJeff Layton 	 * queued for a lease break. Don't queue it again.
4851dff1399fSJeff Layton 	 */
4852f54fe962SJeff Layton 	spin_lock(&state_lock);
4853548ec080SJ. Bruce Fields 	if (delegation_hashed(dp) && dp->dl_time == 0) {
485420b7d86fSArnd Bergmann 		dp->dl_time = ktime_get_boottime_seconds();
485502e1215fSJeff Layton 		list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
485602e1215fSJeff Layton 	}
485702e1215fSJeff Layton 	spin_unlock(&state_lock);
4858dff1399fSJeff Layton }
48591da177e4SLinus Torvalds 
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)48600162ac2bSChristoph Hellwig static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
48610162ac2bSChristoph Hellwig 		struct rpc_task *task)
48620162ac2bSChristoph Hellwig {
48630162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
48640162ac2bSChristoph Hellwig 
48651035d654SChuck Lever 	trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
48661035d654SChuck Lever 
486712ed22f3SJ. Bruce Fields 	if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
486812ed22f3SJ. Bruce Fields 	    dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4869a457974fSAndrew Elble 	        return 1;
4870a457974fSAndrew Elble 
48710162ac2bSChristoph Hellwig 	switch (task->tk_status) {
48720162ac2bSChristoph Hellwig 	case 0:
48730162ac2bSChristoph Hellwig 		return 1;
48741c73b9d2SScott Mayhew 	case -NFS4ERR_DELAY:
48751c73b9d2SScott Mayhew 		rpc_delay(task, 2 * HZ);
48761c73b9d2SScott Mayhew 		return 0;
48770162ac2bSChristoph Hellwig 	case -EBADHANDLE:
48780162ac2bSChristoph Hellwig 	case -NFS4ERR_BAD_STATEID:
48790162ac2bSChristoph Hellwig 		/*
48800162ac2bSChristoph Hellwig 		 * Race: client probably got cb_recall before open reply
48810162ac2bSChristoph Hellwig 		 * granting delegation.
48820162ac2bSChristoph Hellwig 		 */
48830162ac2bSChristoph Hellwig 		if (dp->dl_retries--) {
48840162ac2bSChristoph Hellwig 			rpc_delay(task, 2 * HZ);
48850162ac2bSChristoph Hellwig 			return 0;
48860162ac2bSChristoph Hellwig 		}
4887df561f66SGustavo A. R. Silva 		fallthrough;
48880162ac2bSChristoph Hellwig 	default:
48891c73b9d2SScott Mayhew 		return 1;
48900162ac2bSChristoph Hellwig 	}
48910162ac2bSChristoph Hellwig }
48920162ac2bSChristoph Hellwig 
nfsd4_cb_recall_release(struct nfsd4_callback * cb)48930162ac2bSChristoph Hellwig static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
48940162ac2bSChristoph Hellwig {
48950162ac2bSChristoph Hellwig 	struct nfs4_delegation *dp = cb_to_delegation(cb);
48960162ac2bSChristoph Hellwig 
48970162ac2bSChristoph Hellwig 	nfs4_put_stid(&dp->dl_stid);
48980162ac2bSChristoph Hellwig }
48990162ac2bSChristoph Hellwig 
4900c4cb8974SJulia Lawall static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
49010162ac2bSChristoph Hellwig 	.prepare	= nfsd4_cb_recall_prepare,
49020162ac2bSChristoph Hellwig 	.done		= nfsd4_cb_recall_done,
49030162ac2bSChristoph Hellwig 	.release	= nfsd4_cb_recall_release,
49040162ac2bSChristoph Hellwig };
49050162ac2bSChristoph Hellwig 
nfsd_break_one_deleg(struct nfs4_delegation * dp)490602e1215fSJeff Layton static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
490702e1215fSJeff Layton {
490802e1215fSJeff Layton 	/*
490902e1215fSJeff Layton 	 * We're assuming the state code never drops its reference
491002e1215fSJeff Layton 	 * without first removing the lease.  Since we're in this lease
49114a269efbSJ. Bruce Fields 	 * callback (and since the lease code is serialized by the
491225fbe1fcSJeff Layton 	 * flc_lock) we know the server hasn't removed the lease yet, and
49134a269efbSJ. Bruce Fields 	 * we know it's safe to take a reference.
491402e1215fSJeff Layton 	 */
4915a15dfcd5SElena Reshetova 	refcount_inc(&dp->dl_stid.sc_count);
4916b95239caSJeff Layton 	WARN_ON_ONCE(!nfsd4_run_cb(&dp->dl_recall));
49176b57d9c8SJ. Bruce Fields }
49186b57d9c8SJ. Bruce Fields 
491925fbe1fcSJeff Layton /* Called from break_lease() with flc_lock held. */
49204d01b7f5SJeff Layton static bool
nfsd_break_deleg_cb(struct file_lock * fl)49214d01b7f5SJeff Layton nfsd_break_deleg_cb(struct file_lock *fl)
49226b57d9c8SJ. Bruce Fields {
4923653e514eSJ. Bruce Fields 	struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4924653e514eSJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_stid.sc_file;
492566af2579SDai Ngo 	struct nfs4_client *clp = dp->dl_stid.sc_client;
492666af2579SDai Ngo 	struct nfsd_net *nn;
49276b57d9c8SJ. Bruce Fields 
492817d76ddfSChuck Lever 	trace_nfsd_cb_recall(&dp->dl_stid);
4929dd5e3fbcSChuck Lever 
493066af2579SDai Ngo 	dp->dl_recalled = true;
493166af2579SDai Ngo 	atomic_inc(&clp->cl_delegs_in_recall);
493266af2579SDai Ngo 	if (try_to_expire_client(clp)) {
493366af2579SDai Ngo 		nn = net_generic(clp->net, nfsd_net_id);
493466af2579SDai Ngo 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
493566af2579SDai Ngo 	}
493666af2579SDai Ngo 
49370272e1fdSJ. Bruce Fields 	/*
49380272e1fdSJ. Bruce Fields 	 * We don't want the locks code to timeout the lease for us;
4939acfdf5c3SJ. Bruce Fields 	 * we'll remove it ourself if a delegation isn't returned
49406b57d9c8SJ. Bruce Fields 	 * in time:
49410272e1fdSJ. Bruce Fields 	 */
49420272e1fdSJ. Bruce Fields 	fl->fl_break_time = 0;
49431da177e4SLinus Torvalds 
4944417c6629SJeff Layton 	fp->fi_had_conflict = true;
49455d926e8cSJ. Bruce Fields 	nfsd_break_one_deleg(dp);
4946b95239caSJeff Layton 	return false;
49471da177e4SLinus Torvalds }
49481da177e4SLinus Torvalds 
494950719bf3SChuck Lever /**
495050719bf3SChuck Lever  * nfsd_breaker_owns_lease - Check if lease conflict was resolved
495150719bf3SChuck Lever  * @fl: Lock state to check
495250719bf3SChuck Lever  *
495350719bf3SChuck Lever  * Return values:
495450719bf3SChuck Lever  *   %true: Lease conflict was resolved
495550719bf3SChuck Lever  *   %false: Lease conflict was not resolved.
495650719bf3SChuck Lever  */
nfsd_breaker_owns_lease(struct file_lock * fl)495728df3d15SJ. Bruce Fields static bool nfsd_breaker_owns_lease(struct file_lock *fl)
495828df3d15SJ. Bruce Fields {
495928df3d15SJ. Bruce Fields 	struct nfs4_delegation *dl = fl->fl_owner;
496028df3d15SJ. Bruce Fields 	struct svc_rqst *rqst;
496128df3d15SJ. Bruce Fields 	struct nfs4_client *clp;
496228df3d15SJ. Bruce Fields 
496328df3d15SJ. Bruce Fields 	if (!i_am_nfsd())
496450719bf3SChuck Lever 		return false;
496528df3d15SJ. Bruce Fields 	rqst = kthread_data(current);
496613956160SJ. Bruce Fields 	/* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
496713956160SJ. Bruce Fields 	if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
496850719bf3SChuck Lever 		return false;
496928df3d15SJ. Bruce Fields 	clp = *(rqst->rq_lease_breaker);
497028df3d15SJ. Bruce Fields 	return dl->dl_stid.sc_client == clp;
497128df3d15SJ. Bruce Fields }
497228df3d15SJ. Bruce Fields 
4973c45198edSJeff Layton static int
nfsd_change_deleg_cb(struct file_lock * onlist,int arg,struct list_head * dispose)49747448cc37SJeff Layton nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
49757448cc37SJeff Layton 		     struct list_head *dispose)
49761da177e4SLinus Torvalds {
497766af2579SDai Ngo 	struct nfs4_delegation *dp = (struct nfs4_delegation *)onlist->fl_owner;
497866af2579SDai Ngo 	struct nfs4_client *clp = dp->dl_stid.sc_client;
497966af2579SDai Ngo 
498066af2579SDai Ngo 	if (arg & F_UNLCK) {
498166af2579SDai Ngo 		if (dp->dl_recalled)
498266af2579SDai Ngo 			atomic_dec(&clp->cl_delegs_in_recall);
4983c45198edSJeff Layton 		return lease_modify(onlist, arg, dispose);
498466af2579SDai Ngo 	} else
49851da177e4SLinus Torvalds 		return -EAGAIN;
49861da177e4SLinus Torvalds }
49871da177e4SLinus Torvalds 
49887b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_lease_mng_ops = {
498928df3d15SJ. Bruce Fields 	.lm_breaker_owns_lease = nfsd_breaker_owns_lease,
49908fb47a4fSJ. Bruce Fields 	.lm_break = nfsd_break_deleg_cb,
49918fb47a4fSJ. Bruce Fields 	.lm_change = nfsd_change_deleg_cb,
49921da177e4SLinus Torvalds };
49931da177e4SLinus Torvalds 
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)49947a8711c9SJ. Bruce Fields static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
49957a8711c9SJ. Bruce Fields {
49967a8711c9SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
49977a8711c9SJ. Bruce Fields 		return nfs_ok;
49987a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid - 1)
49997a8711c9SJ. Bruce Fields 		return nfserr_replay_me;
50007a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid)
50017a8711c9SJ. Bruce Fields 		return nfs_ok;
50027a8711c9SJ. Bruce Fields 	return nfserr_bad_seqid;
50037a8711c9SJ. Bruce Fields }
50041da177e4SLinus Torvalds 
lookup_clientid(clientid_t * clid,bool sessions,struct nfsd_net * nn)50057950b531SJ. Bruce Fields static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
50067950b531SJ. Bruce Fields 						struct nfsd_net *nn)
50077950b531SJ. Bruce Fields {
50087950b531SJ. Bruce Fields 	struct nfs4_client *found;
50097950b531SJ. Bruce Fields 
50107950b531SJ. Bruce Fields 	spin_lock(&nn->client_lock);
50117950b531SJ. Bruce Fields 	found = find_confirmed_client(clid, sessions, nn);
50127950b531SJ. Bruce Fields 	if (found)
50137950b531SJ. Bruce Fields 		atomic_inc(&found->cl_rpc_users);
50147950b531SJ. Bruce Fields 	spin_unlock(&nn->client_lock);
50157950b531SJ. Bruce Fields 	return found;
50167950b531SJ. Bruce Fields }
50177950b531SJ. Bruce Fields 
set_client(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)5018460d2709SJ. Bruce Fields static __be32 set_client(clientid_t *clid,
50194b24ca7dSJeff Layton 		struct nfsd4_compound_state *cstate,
5020f71475baSJ. Bruce Fields 		struct nfsd_net *nn)
50214b24ca7dSJeff Layton {
50224b24ca7dSJeff Layton 	if (cstate->clp) {
50237950b531SJ. Bruce Fields 		if (!same_clid(&cstate->clp->cl_clientid, clid))
50244b24ca7dSJeff Layton 			return nfserr_stale_clientid;
50254b24ca7dSJeff Layton 		return nfs_ok;
50264b24ca7dSJeff Layton 	}
50274b24ca7dSJeff Layton 	if (STALE_CLIENTID(clid, nn))
50284b24ca7dSJeff Layton 		return nfserr_stale_clientid;
50294b24ca7dSJeff Layton 	/*
5030f71475baSJ. Bruce Fields 	 * We're in the 4.0 case (otherwise the SEQUENCE op would have
5031f71475baSJ. Bruce Fields 	 * set cstate->clp), so session = false:
50324b24ca7dSJeff Layton 	 */
5033f71475baSJ. Bruce Fields 	cstate->clp = lookup_clientid(clid, false, nn);
50347950b531SJ. Bruce Fields 	if (!cstate->clp)
50354b24ca7dSJeff Layton 		return nfserr_expired;
50364b24ca7dSJeff Layton 	return nfs_ok;
50374b24ca7dSJeff Layton }
50384b24ca7dSJeff Layton 
5039b37ad28bSAl Viro __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)50406668958fSAndy Adamson nfsd4_process_open1(struct nfsd4_compound_state *cstate,
50413320fef1SStanislav Kinsbursky 		    struct nfsd4_open *open, struct nfsd_net *nn)
50421da177e4SLinus Torvalds {
50431da177e4SLinus Torvalds 	clientid_t *clientid = &open->op_clientid;
50441da177e4SLinus Torvalds 	struct nfs4_client *clp = NULL;
50451da177e4SLinus Torvalds 	unsigned int strhashval;
5046fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = NULL;
50474cdc951bSJ. Bruce Fields 	__be32 status;
50481da177e4SLinus Torvalds 
504932513b40SJ. Bruce Fields 	/*
505032513b40SJ. Bruce Fields 	 * In case we need it later, after we've already created the
505132513b40SJ. Bruce Fields 	 * file and don't want to risk a further failure:
505232513b40SJ. Bruce Fields 	 */
505332513b40SJ. Bruce Fields 	open->op_file = nfsd4_alloc_file();
505432513b40SJ. Bruce Fields 	if (open->op_file == NULL)
505532513b40SJ. Bruce Fields 		return nfserr_jukebox;
50561da177e4SLinus Torvalds 
5057f71475baSJ. Bruce Fields 	status = set_client(clientid, cstate, nn);
505813d6f66bSTrond Myklebust 	if (status)
505913d6f66bSTrond Myklebust 		return status;
506013d6f66bSTrond Myklebust 	clp = cstate->clp;
50612d91e895STrond Myklebust 
5062d4f0489fSTrond Myklebust 	strhashval = ownerstr_hashval(&open->op_owner);
5063d4f0489fSTrond Myklebust 	oo = find_openstateowner_str(strhashval, open, clp);
50642d91e895STrond Myklebust 	open->op_openowner = oo;
50652d91e895STrond Myklebust 	if (!oo) {
5066bcf130f9SJ. Bruce Fields 		goto new_owner;
50670f442aa2SJ. Bruce Fields 	}
5068dad1c067SJ. Bruce Fields 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
50690f442aa2SJ. Bruce Fields 		/* Replace unconfirmed owners without checking for replay. */
5070fe0750e5SJ. Bruce Fields 		release_openowner(oo);
5071fe0750e5SJ. Bruce Fields 		open->op_openowner = NULL;
5072bcf130f9SJ. Bruce Fields 		goto new_owner;
50730f442aa2SJ. Bruce Fields 	}
50744cdc951bSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
50754cdc951bSJ. Bruce Fields 	if (status)
50764cdc951bSJ. Bruce Fields 		return status;
50774cdc951bSJ. Bruce Fields 	goto alloc_stateid;
5078bcf130f9SJ. Bruce Fields new_owner:
507913d6f66bSTrond Myklebust 	oo = alloc_init_open_stateowner(strhashval, open, cstate);
5080fe0750e5SJ. Bruce Fields 	if (oo == NULL)
50813e772463SJ. Bruce Fields 		return nfserr_jukebox;
5082fe0750e5SJ. Bruce Fields 	open->op_openowner = oo;
50834cdc951bSJ. Bruce Fields alloc_stateid:
5084b49e084dSJeff Layton 	open->op_stp = nfs4_alloc_open_stateid(clp);
50854cdc951bSJ. Bruce Fields 	if (!open->op_stp)
50864cdc951bSJ. Bruce Fields 		return nfserr_jukebox;
50878287f009SSachin Bhamare 
50888287f009SSachin Bhamare 	if (nfsd4_has_session(cstate) &&
50898287f009SSachin Bhamare 	    (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
50908287f009SSachin Bhamare 		open->op_odstate = alloc_clnt_odstate(clp);
50918287f009SSachin Bhamare 		if (!open->op_odstate)
50928287f009SSachin Bhamare 			return nfserr_jukebox;
50938287f009SSachin Bhamare 	}
50948287f009SSachin Bhamare 
50950f442aa2SJ. Bruce Fields 	return nfs_ok;
50961da177e4SLinus Torvalds }
50971da177e4SLinus Torvalds 
5098b37ad28bSAl Viro static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)50994a6e43e6SNeilBrown nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
51004a6e43e6SNeilBrown {
51014a6e43e6SNeilBrown 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
51024a6e43e6SNeilBrown 		return nfserr_openmode;
51034a6e43e6SNeilBrown 	else
51044a6e43e6SNeilBrown 		return nfs_ok;
51054a6e43e6SNeilBrown }
51064a6e43e6SNeilBrown 
share_access_to_flags(u32 share_access)5107c47d832bSDaniel Mack static int share_access_to_flags(u32 share_access)
510824a0111eSJ. Bruce Fields {
510924a0111eSJ. Bruce Fields 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
511024a0111eSJ. Bruce Fields }
511124a0111eSJ. Bruce Fields 
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)511238c2f4b1SJ. Bruce Fields static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
5113f459e453SJ. Bruce Fields {
5114f459e453SJ. Bruce Fields 	struct nfs4_stid *ret;
5115f459e453SJ. Bruce Fields 
511695da1b3aSAndrew Elble 	ret = find_stateid_by_type(cl, s,
511795da1b3aSAndrew Elble 				NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
5118f459e453SJ. Bruce Fields 	if (!ret)
5119f459e453SJ. Bruce Fields 		return NULL;
5120f459e453SJ. Bruce Fields 	return delegstateid(ret);
5121f459e453SJ. Bruce Fields }
5122f459e453SJ. Bruce Fields 
nfsd4_is_deleg_cur(struct nfsd4_open * open)51238b289b2cSJ. Bruce Fields static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
51248b289b2cSJ. Bruce Fields {
51258b289b2cSJ. Bruce Fields 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
51268b289b2cSJ. Bruce Fields 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
51278b289b2cSJ. Bruce Fields }
51288b289b2cSJ. Bruce Fields 
5129b37ad28bSAl Viro static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)513041d22663SJ. Bruce Fields nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
5131567d9829SNeilBrown 		struct nfs4_delegation **dp)
5132567d9829SNeilBrown {
5133567d9829SNeilBrown 	int flags;
5134b37ad28bSAl Viro 	__be32 status = nfserr_bad_stateid;
5135dcd94cc2STrond Myklebust 	struct nfs4_delegation *deleg;
5136567d9829SNeilBrown 
5137dcd94cc2STrond Myklebust 	deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
5138dcd94cc2STrond Myklebust 	if (deleg == NULL)
5139c44c5eebSNeilBrown 		goto out;
514095da1b3aSAndrew Elble 	if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
514195da1b3aSAndrew Elble 		nfs4_put_stid(&deleg->dl_stid);
514295da1b3aSAndrew Elble 		if (cl->cl_minorversion)
514395da1b3aSAndrew Elble 			status = nfserr_deleg_revoked;
514495da1b3aSAndrew Elble 		goto out;
514595da1b3aSAndrew Elble 	}
514624a0111eSJ. Bruce Fields 	flags = share_access_to_flags(open->op_share_access);
5147dcd94cc2STrond Myklebust 	status = nfs4_check_delegmode(deleg, flags);
5148dcd94cc2STrond Myklebust 	if (status) {
5149dcd94cc2STrond Myklebust 		nfs4_put_stid(&deleg->dl_stid);
5150dcd94cc2STrond Myklebust 		goto out;
5151dcd94cc2STrond Myklebust 	}
5152dcd94cc2STrond Myklebust 	*dp = deleg;
5153c44c5eebSNeilBrown out:
51548b289b2cSJ. Bruce Fields 	if (!nfsd4_is_deleg_cur(open))
5155c44c5eebSNeilBrown 		return nfs_ok;
5156c44c5eebSNeilBrown 	if (status)
5157c44c5eebSNeilBrown 		return status;
5158dad1c067SJ. Bruce Fields 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5159c44c5eebSNeilBrown 	return nfs_ok;
5160567d9829SNeilBrown }
5161567d9829SNeilBrown 
nfs4_access_to_access(u32 nfs4_access)516221fb4016SJ. Bruce Fields static inline int nfs4_access_to_access(u32 nfs4_access)
516321fb4016SJ. Bruce Fields {
516421fb4016SJ. Bruce Fields 	int flags = 0;
516521fb4016SJ. Bruce Fields 
516621fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
516721fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_READ;
516821fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
516921fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_WRITE;
517021fb4016SJ. Bruce Fields 	return flags;
517121fb4016SJ. Bruce Fields }
517221fb4016SJ. Bruce Fields 
5173b37ad28bSAl Viro static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)51741da177e4SLinus Torvalds nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
51751da177e4SLinus Torvalds 		struct nfsd4_open *open)
51761da177e4SLinus Torvalds {
51771da177e4SLinus Torvalds 	struct iattr iattr = {
51781da177e4SLinus Torvalds 		.ia_valid = ATTR_SIZE,
51791da177e4SLinus Torvalds 		.ia_size = 0,
51801da177e4SLinus Torvalds 	};
51817fe2a71dSNeilBrown 	struct nfsd_attrs attrs = {
51827fe2a71dSNeilBrown 		.na_iattr	= &iattr,
51837fe2a71dSNeilBrown 	};
51841da177e4SLinus Torvalds 	if (!open->op_truncate)
51851da177e4SLinus Torvalds 		return 0;
51861da177e4SLinus Torvalds 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
51879246585aSAl Viro 		return nfserr_inval;
51887fe2a71dSNeilBrown 	return nfsd_setattr(rqstp, fh, &attrs, 0, (time64_t)0);
51891da177e4SLinus Torvalds }
51901da177e4SLinus Torvalds 
nfs4_get_vfs_file(struct svc_rqst * rqstp,struct nfs4_file * fp,struct svc_fh * cur_fh,struct nfs4_ol_stateid * stp,struct nfsd4_open * open,bool new_stp)51917e6a72e5SChristoph Hellwig static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
51926eb3a1d0SJeff Layton 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
51933d694271SDai Ngo 		struct nfsd4_open *open, bool new_stp)
51947e6a72e5SChristoph Hellwig {
5195fd4f83fdSJeff Layton 	struct nfsd_file *nf = NULL;
51967e6a72e5SChristoph Hellwig 	__be32 status;
51977e6a72e5SChristoph Hellwig 	int oflag = nfs4_access_to_omode(open->op_share_access);
51987e6a72e5SChristoph Hellwig 	int access = nfs4_access_to_access(open->op_share_access);
5199baeb4ff0SJeff Layton 	unsigned char old_access_bmap, old_deny_bmap;
52007e6a72e5SChristoph Hellwig 
5201de18643dSTrond Myklebust 	spin_lock(&fp->fi_lock);
5202baeb4ff0SJeff Layton 
5203baeb4ff0SJeff Layton 	/*
5204baeb4ff0SJeff Layton 	 * Are we trying to set a deny mode that would conflict with
5205baeb4ff0SJeff Layton 	 * current access?
5206baeb4ff0SJeff Layton 	 */
5207baeb4ff0SJeff Layton 	status = nfs4_file_check_deny(fp, open->op_share_deny);
5208baeb4ff0SJeff Layton 	if (status != nfs_ok) {
52093d694271SDai Ngo 		if (status != nfserr_share_denied) {
52103d694271SDai Ngo 			spin_unlock(&fp->fi_lock);
52113d694271SDai Ngo 			goto out;
52123d694271SDai Ngo 		}
52133d694271SDai Ngo 		if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
52143d694271SDai Ngo 				stp, open->op_share_deny, false))
52153d694271SDai Ngo 			status = nfserr_jukebox;
5216baeb4ff0SJeff Layton 		spin_unlock(&fp->fi_lock);
5217baeb4ff0SJeff Layton 		goto out;
5218baeb4ff0SJeff Layton 	}
5219baeb4ff0SJeff Layton 
5220baeb4ff0SJeff Layton 	/* set access to the file */
5221baeb4ff0SJeff Layton 	status = nfs4_file_get_access(fp, open->op_share_access);
5222baeb4ff0SJeff Layton 	if (status != nfs_ok) {
52233d694271SDai Ngo 		if (status != nfserr_share_denied) {
52243d694271SDai Ngo 			spin_unlock(&fp->fi_lock);
52253d694271SDai Ngo 			goto out;
52263d694271SDai Ngo 		}
52273d694271SDai Ngo 		if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
52283d694271SDai Ngo 				stp, open->op_share_access, true))
52293d694271SDai Ngo 			status = nfserr_jukebox;
5230baeb4ff0SJeff Layton 		spin_unlock(&fp->fi_lock);
5231baeb4ff0SJeff Layton 		goto out;
5232baeb4ff0SJeff Layton 	}
5233baeb4ff0SJeff Layton 
5234baeb4ff0SJeff Layton 	/* Set access bits in stateid */
5235baeb4ff0SJeff Layton 	old_access_bmap = stp->st_access_bmap;
5236baeb4ff0SJeff Layton 	set_access(open->op_share_access, stp);
5237baeb4ff0SJeff Layton 
5238baeb4ff0SJeff Layton 	/* Set new deny mask */
5239baeb4ff0SJeff Layton 	old_deny_bmap = stp->st_deny_bmap;
5240baeb4ff0SJeff Layton 	set_deny(open->op_share_deny, stp);
5241baeb4ff0SJeff Layton 	fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5242baeb4ff0SJeff Layton 
52437e6a72e5SChristoph Hellwig 	if (!fp->fi_fds[oflag]) {
5244de18643dSTrond Myklebust 		spin_unlock(&fp->fi_lock);
5245fb70bf12SChuck Lever 
52460b3a551fSJeff Layton 		status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
52470b3a551fSJeff Layton 						  open->op_filp, &nf);
5248fb70bf12SChuck Lever 		if (status != nfs_ok)
5249baeb4ff0SJeff Layton 			goto out_put_access;
5250fb70bf12SChuck Lever 
5251de18643dSTrond Myklebust 		spin_lock(&fp->fi_lock);
5252de18643dSTrond Myklebust 		if (!fp->fi_fds[oflag]) {
5253fd4f83fdSJeff Layton 			fp->fi_fds[oflag] = nf;
5254fd4f83fdSJeff Layton 			nf = NULL;
5255de18643dSTrond Myklebust 		}
52567e6a72e5SChristoph Hellwig 	}
5257de18643dSTrond Myklebust 	spin_unlock(&fp->fi_lock);
5258fd4f83fdSJeff Layton 	if (nf)
5259fd4f83fdSJeff Layton 		nfsd_file_put(nf);
52607e6a72e5SChristoph Hellwig 
5261217fd6f6SJ. Bruce Fields 	status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
5262217fd6f6SJ. Bruce Fields 								access));
5263217fd6f6SJ. Bruce Fields 	if (status)
5264217fd6f6SJ. Bruce Fields 		goto out_put_access;
5265217fd6f6SJ. Bruce Fields 
52667e6a72e5SChristoph Hellwig 	status = nfsd4_truncate(rqstp, cur_fh, open);
52677e6a72e5SChristoph Hellwig 	if (status)
52687e6a72e5SChristoph Hellwig 		goto out_put_access;
52697e6a72e5SChristoph Hellwig out:
52707e6a72e5SChristoph Hellwig 	return status;
5271baeb4ff0SJeff Layton out_put_access:
5272baeb4ff0SJeff Layton 	stp->st_access_bmap = old_access_bmap;
5273baeb4ff0SJeff Layton 	nfs4_file_put_access(fp, open->op_share_access);
5274baeb4ff0SJeff Layton 	reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
5275baeb4ff0SJeff Layton 	goto out;
52767e6a72e5SChristoph Hellwig }
52777e6a72e5SChristoph Hellwig 
5278b37ad28bSAl Viro static __be32
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)52793d694271SDai Ngo nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
52803d694271SDai Ngo 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
52813d694271SDai Ngo 		struct nfsd4_open *open)
52821da177e4SLinus Torvalds {
5283b37ad28bSAl Viro 	__be32 status;
52846ac75368SArnd Bergmann 	unsigned char old_deny_bmap = stp->st_deny_bmap;
52851da177e4SLinus Torvalds 
52866eb3a1d0SJeff Layton 	if (!test_access(open->op_share_access, stp))
52873d694271SDai Ngo 		return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
52887e6a72e5SChristoph Hellwig 
5289baeb4ff0SJeff Layton 	/* test and set deny mode */
5290baeb4ff0SJeff Layton 	spin_lock(&fp->fi_lock);
5291baeb4ff0SJeff Layton 	status = nfs4_file_check_deny(fp, open->op_share_deny);
5292dcd779dcSJeff Layton 	switch (status) {
5293dcd779dcSJeff Layton 	case nfs_ok:
5294baeb4ff0SJeff Layton 		set_deny(open->op_share_deny, stp);
5295baeb4ff0SJeff Layton 		fp->fi_share_deny |=
5296baeb4ff0SJeff Layton 			(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
5297dcd779dcSJeff Layton 		break;
5298dcd779dcSJeff Layton 	case nfserr_share_denied:
52993d694271SDai Ngo 		if (nfs4_resolve_deny_conflicts_locked(fp, false,
53003d694271SDai Ngo 				stp, open->op_share_deny, false))
53013d694271SDai Ngo 			status = nfserr_jukebox;
5302dcd779dcSJeff Layton 		break;
53031da177e4SLinus Torvalds 	}
5304baeb4ff0SJeff Layton 	spin_unlock(&fp->fi_lock);
53051da177e4SLinus Torvalds 
5306baeb4ff0SJeff Layton 	if (status != nfs_ok)
5307baeb4ff0SJeff Layton 		return status;
5308baeb4ff0SJeff Layton 
5309baeb4ff0SJeff Layton 	status = nfsd4_truncate(rqstp, cur_fh, open);
5310baeb4ff0SJeff Layton 	if (status != nfs_ok)
5311baeb4ff0SJeff Layton 		reset_union_bmap_deny(old_deny_bmap, stp);
5312baeb4ff0SJeff Layton 	return status;
5313baeb4ff0SJeff Layton }
53141da177e4SLinus Torvalds 
531514a24e99SJ. Bruce Fields /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)531614a24e99SJ. Bruce Fields static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
531714a24e99SJ. Bruce Fields {
531814a24e99SJ. Bruce Fields 	if (clp->cl_cb_state == NFSD4_CB_UP)
531914a24e99SJ. Bruce Fields 		return true;
532014a24e99SJ. Bruce Fields 	/*
532114a24e99SJ. Bruce Fields 	 * In the sessions case, since we don't have to establish a
532214a24e99SJ. Bruce Fields 	 * separate connection for callbacks, we assume it's OK
532314a24e99SJ. Bruce Fields 	 * until we hear otherwise:
532414a24e99SJ. Bruce Fields 	 */
532514a24e99SJ. Bruce Fields 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
532614a24e99SJ. Bruce Fields }
532714a24e99SJ. Bruce Fields 
nfs4_alloc_init_lease(struct nfs4_delegation * dp,int flag)5328653e514eSJ. Bruce Fields static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
5329653e514eSJ. Bruce Fields 						int flag)
533022d38c4cSJ. Bruce Fields {
533122d38c4cSJ. Bruce Fields 	struct file_lock *fl;
533222d38c4cSJ. Bruce Fields 
533322d38c4cSJ. Bruce Fields 	fl = locks_alloc_lock();
533422d38c4cSJ. Bruce Fields 	if (!fl)
533522d38c4cSJ. Bruce Fields 		return NULL;
533622d38c4cSJ. Bruce Fields 	fl->fl_lmops = &nfsd_lease_mng_ops;
5337617588d5SJ. Bruce Fields 	fl->fl_flags = FL_DELEG;
533822d38c4cSJ. Bruce Fields 	fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
533922d38c4cSJ. Bruce Fields 	fl->fl_end = OFFSET_MAX;
5340653e514eSJ. Bruce Fields 	fl->fl_owner = (fl_owner_t)dp;
534122d38c4cSJ. Bruce Fields 	fl->fl_pid = current->tgid;
5342eb82dd39SJeff Layton 	fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
534322d38c4cSJ. Bruce Fields 	return fl;
534422d38c4cSJ. Bruce Fields }
534522d38c4cSJ. Bruce Fields 
nfsd4_check_conflicting_opens(struct nfs4_client * clp,struct nfs4_file * fp)5346aba2072fSJ. Bruce Fields static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
5347aba2072fSJ. Bruce Fields 					 struct nfs4_file *fp)
5348aba2072fSJ. Bruce Fields {
5349aba2072fSJ. Bruce Fields 	struct nfs4_ol_stateid *st;
5350aba2072fSJ. Bruce Fields 	struct file *f = fp->fi_deleg_file->nf_file;
5351c65454a9SJeff Layton 	struct inode *ino = file_inode(f);
5352aba2072fSJ. Bruce Fields 	int writes;
5353aba2072fSJ. Bruce Fields 
5354aba2072fSJ. Bruce Fields 	writes = atomic_read(&ino->i_writecount);
5355aba2072fSJ. Bruce Fields 	if (!writes)
5356aba2072fSJ. Bruce Fields 		return 0;
5357aba2072fSJ. Bruce Fields 	/*
5358aba2072fSJ. Bruce Fields 	 * There could be multiple filehandles (hence multiple
5359aba2072fSJ. Bruce Fields 	 * nfs4_files) referencing this file, but that's not too
5360aba2072fSJ. Bruce Fields 	 * common; let's just give up in that case rather than
5361aba2072fSJ. Bruce Fields 	 * trying to go look up all the clients using that other
5362aba2072fSJ. Bruce Fields 	 * nfs4_file as well:
5363aba2072fSJ. Bruce Fields 	 */
5364aba2072fSJ. Bruce Fields 	if (fp->fi_aliased)
5365aba2072fSJ. Bruce Fields 		return -EAGAIN;
5366aba2072fSJ. Bruce Fields 	/*
5367aba2072fSJ. Bruce Fields 	 * If there's a close in progress, make sure that we see it
5368aba2072fSJ. Bruce Fields 	 * clear any fi_fds[] entries before we see it decrement
5369aba2072fSJ. Bruce Fields 	 * i_writecount:
5370aba2072fSJ. Bruce Fields 	 */
5371aba2072fSJ. Bruce Fields 	smp_mb__after_atomic();
5372aba2072fSJ. Bruce Fields 
5373aba2072fSJ. Bruce Fields 	if (fp->fi_fds[O_WRONLY])
5374aba2072fSJ. Bruce Fields 		writes--;
5375aba2072fSJ. Bruce Fields 	if (fp->fi_fds[O_RDWR])
5376aba2072fSJ. Bruce Fields 		writes--;
5377aba2072fSJ. Bruce Fields 	if (writes > 0)
5378aba2072fSJ. Bruce Fields 		return -EAGAIN; /* There may be non-NFSv4 writers */
5379aba2072fSJ. Bruce Fields 	/*
5380aba2072fSJ. Bruce Fields 	 * It's possible there are non-NFSv4 write opens in progress,
5381aba2072fSJ. Bruce Fields 	 * but if they haven't incremented i_writecount yet then they
5382aba2072fSJ. Bruce Fields 	 * also haven't called break lease yet; so, they'll break this
5383aba2072fSJ. Bruce Fields 	 * lease soon enough.  So, all that's left to check for is NFSv4
5384aba2072fSJ. Bruce Fields 	 * opens:
5385aba2072fSJ. Bruce Fields 	 */
5386aba2072fSJ. Bruce Fields 	spin_lock(&fp->fi_lock);
5387aba2072fSJ. Bruce Fields 	list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
5388aba2072fSJ. Bruce Fields 		if (st->st_openstp == NULL /* it's an open */ &&
5389aba2072fSJ. Bruce Fields 		    access_permit_write(st) &&
5390aba2072fSJ. Bruce Fields 		    st->st_stid.sc_client != clp) {
5391aba2072fSJ. Bruce Fields 			spin_unlock(&fp->fi_lock);
5392aba2072fSJ. Bruce Fields 			return -EAGAIN;
5393aba2072fSJ. Bruce Fields 		}
5394aba2072fSJ. Bruce Fields 	}
5395aba2072fSJ. Bruce Fields 	spin_unlock(&fp->fi_lock);
5396aba2072fSJ. Bruce Fields 	/*
5397aba2072fSJ. Bruce Fields 	 * There's a small chance that we could be racing with another
5398aba2072fSJ. Bruce Fields 	 * NFSv4 open.  However, any open that hasn't added itself to
5399aba2072fSJ. Bruce Fields 	 * the fi_stateids list also hasn't called break_lease yet; so,
5400aba2072fSJ. Bruce Fields 	 * they'll break this lease soon enough.
5401aba2072fSJ. Bruce Fields 	 */
5402aba2072fSJ. Bruce Fields 	return 0;
5403aba2072fSJ. Bruce Fields }
5404aba2072fSJ. Bruce Fields 
5405876c553cSJeff Layton /*
5406876c553cSJeff Layton  * It's possible that between opening the dentry and setting the delegation,
5407876c553cSJeff Layton  * that it has been renamed or unlinked. Redo the lookup to verify that this
5408876c553cSJeff Layton  * hasn't happened.
5409876c553cSJeff Layton  */
5410876c553cSJeff Layton static int
nfsd4_verify_deleg_dentry(struct nfsd4_open * open,struct nfs4_file * fp,struct svc_fh * parent)5411876c553cSJeff Layton nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
5412876c553cSJeff Layton 			  struct svc_fh *parent)
5413876c553cSJeff Layton {
5414876c553cSJeff Layton 	struct svc_export *exp;
5415876c553cSJeff Layton 	struct dentry *child;
5416876c553cSJeff Layton 	__be32 err;
5417876c553cSJeff Layton 
5418876c553cSJeff Layton 	err = nfsd_lookup_dentry(open->op_rqstp, parent,
5419876c553cSJeff Layton 				 open->op_fname, open->op_fnamelen,
5420876c553cSJeff Layton 				 &exp, &child);
5421876c553cSJeff Layton 
5422876c553cSJeff Layton 	if (err)
5423876c553cSJeff Layton 		return -EAGAIN;
5424876c553cSJeff Layton 
542550256e47SJeff Layton 	exp_put(exp);
5426876c553cSJeff Layton 	dput(child);
5427876c553cSJeff Layton 	if (child != file_dentry(fp->fi_deleg_file->nf_file))
5428876c553cSJeff Layton 		return -EAGAIN;
5429876c553cSJeff Layton 
5430876c553cSJeff Layton 	return 0;
5431876c553cSJeff Layton }
5432876c553cSJeff Layton 
5433826b67e6SJeff Layton /*
5434826b67e6SJeff Layton  * We avoid breaking delegations held by a client due to its own activity, but
5435826b67e6SJeff Layton  * clearing setuid/setgid bits on a write is an implicit activity and the client
5436826b67e6SJeff Layton  * may not notice and continue using the old mode. Avoid giving out a delegation
5437826b67e6SJeff Layton  * on setuid/setgid files when the client is requesting an open for write.
5438826b67e6SJeff Layton  */
5439826b67e6SJeff Layton static int
nfsd4_verify_setuid_write(struct nfsd4_open * open,struct nfsd_file * nf)5440826b67e6SJeff Layton nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
5441826b67e6SJeff Layton {
5442826b67e6SJeff Layton 	struct inode *inode = file_inode(nf->nf_file);
5443826b67e6SJeff Layton 
5444826b67e6SJeff Layton 	if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
5445826b67e6SJeff Layton 	    (inode->i_mode & (S_ISUID|S_ISGID)))
5446826b67e6SJeff Layton 		return -EAGAIN;
5447826b67e6SJeff Layton 	return 0;
5448826b67e6SJeff Layton }
5449826b67e6SJeff Layton 
54500b26693cSJeff Layton static struct nfs4_delegation *
nfs4_set_delegation(struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * parent)5451876c553cSJeff Layton nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5452876c553cSJeff Layton 		    struct svc_fh *parent)
5453acfdf5c3SJ. Bruce Fields {
545468b18f52SJ. Bruce Fields 	int status = 0;
5455876c553cSJeff Layton 	struct nfs4_client *clp = stp->st_stid.sc_client;
5456876c553cSJeff Layton 	struct nfs4_file *fp = stp->st_stid.sc_file;
5457876c553cSJeff Layton 	struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
54580b26693cSJeff Layton 	struct nfs4_delegation *dp;
54591d3dd1d5SDai Ngo 	struct nfsd_file *nf = NULL;
5460353601e7SJ. Bruce Fields 	struct file_lock *fl;
54611d3dd1d5SDai Ngo 	u32 dl_type;
5462417c6629SJeff Layton 
5463353601e7SJ. Bruce Fields 	/*
5464353601e7SJ. Bruce Fields 	 * The fi_had_conflict and nfs_get_existing_delegation checks
5465353601e7SJ. Bruce Fields 	 * here are just optimizations; we'll need to recheck them at
5466353601e7SJ. Bruce Fields 	 * the end:
5467353601e7SJ. Bruce Fields 	 */
5468bf7bd3e9SJ. Bruce Fields 	if (fp->fi_had_conflict)
54690b26693cSJeff Layton 		return ERR_PTR(-EAGAIN);
54700b26693cSJeff Layton 
5471aba2072fSJ. Bruce Fields 	/*
54721d3dd1d5SDai Ngo 	 * Try for a write delegation first. RFC8881 section 10.4 says:
54731d3dd1d5SDai Ngo 	 *
54741d3dd1d5SDai Ngo 	 *  "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
54751d3dd1d5SDai Ngo 	 *   on its own, all opens."
54761d3dd1d5SDai Ngo 	 *
54771d3dd1d5SDai Ngo 	 * Furthermore the client can use a write delegation for most READ
54781d3dd1d5SDai Ngo 	 * operations as well, so we require a O_RDWR file here.
54791d3dd1d5SDai Ngo 	 *
54801d3dd1d5SDai Ngo 	 * Offer a write delegation in the case of a BOTH open, and ensure
54811d3dd1d5SDai Ngo 	 * we get the O_RDWR descriptor.
5482aba2072fSJ. Bruce Fields 	 */
54831d3dd1d5SDai Ngo 	if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
54841d3dd1d5SDai Ngo 		nf = find_rw_file(fp);
54851d3dd1d5SDai Ngo 		dl_type = NFS4_OPEN_DELEGATE_WRITE;
5486353601e7SJ. Bruce Fields 	}
54871d3dd1d5SDai Ngo 
54881d3dd1d5SDai Ngo 	/*
54891d3dd1d5SDai Ngo 	 * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
54901d3dd1d5SDai Ngo 	 * file for some reason, then try for a read delegation instead.
54911d3dd1d5SDai Ngo 	 */
54921d3dd1d5SDai Ngo 	if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
54931d3dd1d5SDai Ngo 		nf = find_readable_file(fp);
54941d3dd1d5SDai Ngo 		dl_type = NFS4_OPEN_DELEGATE_READ;
54951d3dd1d5SDai Ngo 	}
54961d3dd1d5SDai Ngo 
54971d3dd1d5SDai Ngo 	if (!nf)
54981d3dd1d5SDai Ngo 		return ERR_PTR(-EAGAIN);
54991d3dd1d5SDai Ngo 
550034ed9872SAndrew Elble 	spin_lock(&state_lock);
550134ed9872SAndrew Elble 	spin_lock(&fp->fi_lock);
550268b18f52SJ. Bruce Fields 	if (nfs4_delegation_exists(clp, fp))
550368b18f52SJ. Bruce Fields 		status = -EAGAIN;
5504826b67e6SJeff Layton 	else if (nfsd4_verify_setuid_write(open, nf))
5505826b67e6SJeff Layton 		status = -EAGAIN;
5506353601e7SJ. Bruce Fields 	else if (!fp->fi_deleg_file) {
5507eb82dd39SJeff Layton 		fp->fi_deleg_file = nf;
5508353601e7SJ. Bruce Fields 		/* increment early to prevent fi_deleg_file from being
5509353601e7SJ. Bruce Fields 		 * cleared */
5510353601e7SJ. Bruce Fields 		fp->fi_delegees = 1;
5511eb82dd39SJeff Layton 		nf = NULL;
5512353601e7SJ. Bruce Fields 	} else
5513353601e7SJ. Bruce Fields 		fp->fi_delegees++;
5514353601e7SJ. Bruce Fields 	spin_unlock(&fp->fi_lock);
5515353601e7SJ. Bruce Fields 	spin_unlock(&state_lock);
5516eb82dd39SJeff Layton 	if (nf)
5517eb82dd39SJeff Layton 		nfsd_file_put(nf);
5518353601e7SJ. Bruce Fields 	if (status)
5519353601e7SJ. Bruce Fields 		return ERR_PTR(status);
5520353601e7SJ. Bruce Fields 
5521353601e7SJ. Bruce Fields 	status = -ENOMEM;
55221d3dd1d5SDai Ngo 	dp = alloc_init_deleg(clp, fp, odstate, dl_type);
5523353601e7SJ. Bruce Fields 	if (!dp)
5524353601e7SJ. Bruce Fields 		goto out_delegees;
5525353601e7SJ. Bruce Fields 
55261d3dd1d5SDai Ngo 	fl = nfs4_alloc_init_lease(dp, dl_type);
5527353601e7SJ. Bruce Fields 	if (!fl)
5528bd8d7250SAndrew Elble 		goto out_clnt_odstate;
5529353601e7SJ. Bruce Fields 
5530eb82dd39SJeff Layton 	status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5531353601e7SJ. Bruce Fields 	if (fl)
5532353601e7SJ. Bruce Fields 		locks_free_lock(fl);
5533353601e7SJ. Bruce Fields 	if (status)
5534353601e7SJ. Bruce Fields 		goto out_clnt_odstate;
5535876c553cSJeff Layton 
5536876c553cSJeff Layton 	if (parent) {
5537876c553cSJeff Layton 		status = nfsd4_verify_deleg_dentry(open, fp, parent);
5538876c553cSJeff Layton 		if (status)
5539876c553cSJeff Layton 			goto out_unlock;
5540876c553cSJeff Layton 	}
5541876c553cSJeff Layton 
5542aba2072fSJ. Bruce Fields 	status = nfsd4_check_conflicting_opens(clp, fp);
5543aba2072fSJ. Bruce Fields 	if (status)
5544aba2072fSJ. Bruce Fields 		goto out_unlock;
5545353601e7SJ. Bruce Fields 
5546826b67e6SJeff Layton 	/*
5547826b67e6SJeff Layton 	 * Now that the deleg is set, check again to ensure that nothing
5548826b67e6SJeff Layton 	 * raced in and changed the mode while we weren't lookng.
5549826b67e6SJeff Layton 	 */
5550826b67e6SJeff Layton 	status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
5551826b67e6SJeff Layton 	if (status)
5552826b67e6SJeff Layton 		goto out_unlock;
5553826b67e6SJeff Layton 
5554940c919bSNeilBrown 	status = -EAGAIN;
5555940c919bSNeilBrown 	if (fp->fi_had_conflict)
5556940c919bSNeilBrown 		goto out_unlock;
5557940c919bSNeilBrown 
5558353601e7SJ. Bruce Fields 	spin_lock(&state_lock);
5559353601e7SJ. Bruce Fields 	spin_lock(&fp->fi_lock);
5560353601e7SJ. Bruce Fields 	status = hash_delegation_locked(dp, fp);
556134ed9872SAndrew Elble 	spin_unlock(&fp->fi_lock);
556234ed9872SAndrew Elble 	spin_unlock(&state_lock);
556334ed9872SAndrew Elble 
556434ed9872SAndrew Elble 	if (status)
5565692ad280SAndrew Elble 		goto out_unlock;
5566692ad280SAndrew Elble 
55670b26693cSJeff Layton 	return dp;
5568692ad280SAndrew Elble out_unlock:
5569eb82dd39SJeff Layton 	vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5570353601e7SJ. Bruce Fields out_clnt_odstate:
5571353601e7SJ. Bruce Fields 	put_clnt_odstate(dp->dl_clnt_odstate);
5572353601e7SJ. Bruce Fields 	nfs4_put_stid(&dp->dl_stid);
5573353601e7SJ. Bruce Fields out_delegees:
5574353601e7SJ. Bruce Fields 	put_deleg_file(fp);
5575353601e7SJ. Bruce Fields 	return ERR_PTR(status);
5576edab9782SJ. Bruce Fields }
5577edab9782SJ. Bruce Fields 
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)55784aa8913cSBenny Halevy static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
55794aa8913cSBenny Halevy {
55804aa8913cSBenny Halevy 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
55814aa8913cSBenny Halevy 	if (status == -EAGAIN)
55824aa8913cSBenny Halevy 		open->op_why_no_deleg = WND4_CONTENTION;
55834aa8913cSBenny Halevy 	else {
55844aa8913cSBenny Halevy 		open->op_why_no_deleg = WND4_RESOURCE;
55854aa8913cSBenny Halevy 		switch (open->op_deleg_want) {
55864aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_READ_DELEG:
55874aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_WRITE_DELEG:
55884aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_ANY_DELEG:
55894aa8913cSBenny Halevy 			break;
55904aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_CANCEL:
55914aa8913cSBenny Halevy 			open->op_why_no_deleg = WND4_CANCELLED;
55924aa8913cSBenny Halevy 			break;
55934aa8913cSBenny Halevy 		case NFS4_SHARE_WANT_NO_DELEG:
5594063b0fb9SJ. Bruce Fields 			WARN_ON_ONCE(1);
55954aa8913cSBenny Halevy 		}
55964aa8913cSBenny Halevy 	}
55974aa8913cSBenny Halevy }
55984aa8913cSBenny Halevy 
55991da177e4SLinus Torvalds /*
56001d3dd1d5SDai Ngo  * The Linux NFS server does not offer write delegations to NFSv4.0
56011d3dd1d5SDai Ngo  * clients in order to avoid conflicts between write delegations and
56021d3dd1d5SDai Ngo  * GETATTRs requesting CHANGE or SIZE attributes.
560399c41515SJ. Bruce Fields  *
56041d3dd1d5SDai Ngo  * With NFSv4.1 and later minorversions, the SEQUENCE operation that
56051d3dd1d5SDai Ngo  * begins each COMPOUND contains a client ID. Delegation recall can
56061d3dd1d5SDai Ngo  * be avoided when the server recognizes the client sending a
56071d3dd1d5SDai Ngo  * GETATTR also holds write delegation it conflicts with.
56081d3dd1d5SDai Ngo  *
56091d3dd1d5SDai Ngo  * However, the NFSv4.0 protocol does not enable a server to
56101d3dd1d5SDai Ngo  * determine that a GETATTR originated from the client holding the
56111d3dd1d5SDai Ngo  * conflicting delegation versus coming from some other client. Per
56121d3dd1d5SDai Ngo  * RFC 7530 Section 16.7.5, the server must recall or send a
56131d3dd1d5SDai Ngo  * CB_GETATTR even when the GETATTR originates from the client that
56141d3dd1d5SDai Ngo  * holds the conflicting delegation.
56151d3dd1d5SDai Ngo  *
56161d3dd1d5SDai Ngo  * An NFSv4.0 client can trigger a pathological situation if it
56171d3dd1d5SDai Ngo  * always sends a DELEGRETURN preceded by a conflicting GETATTR in
56181d3dd1d5SDai Ngo  * the same COMPOUND. COMPOUND execution will always stop at the
56191d3dd1d5SDai Ngo  * GETATTR and the DELEGRETURN will never get executed. The server
56201d3dd1d5SDai Ngo  * eventually revokes the delegation, which can result in loss of
56211d3dd1d5SDai Ngo  * open or lock state.
56221da177e4SLinus Torvalds  */
56231da177e4SLinus Torvalds static void
nfs4_open_delegation(struct nfsd4_open * open,struct nfs4_ol_stateid * stp,struct svc_fh * currentfh)5624876c553cSJeff Layton nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
5625876c553cSJeff Layton 		     struct svc_fh *currentfh)
56261da177e4SLinus Torvalds {
56271da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
56284cf59221SJeff Layton 	struct nfs4_openowner *oo = openowner(stp->st_stateowner);
56294cf59221SJeff Layton 	struct nfs4_client *clp = stp->st_stid.sc_client;
5630876c553cSJeff Layton 	struct svc_fh *parent = NULL;
563114a24e99SJ. Bruce Fields 	int cb_up;
563299c41515SJ. Bruce Fields 	int status = 0;
56331da177e4SLinus Torvalds 
5634fe0750e5SJ. Bruce Fields 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
56357b190fecSNeilBrown 	open->op_recall = 0;
56367b190fecSNeilBrown 	switch (open->op_claim_type) {
56377b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_PREVIOUS:
56382bf23875SJ. Bruce Fields 			if (!cb_up)
56397b190fecSNeilBrown 				open->op_recall = 1;
56407b190fecSNeilBrown 			break;
56417b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_NULL:
5642876c553cSJeff Layton 			parent = currentfh;
5643876c553cSJeff Layton 			fallthrough;
5644ed47b062SMing Chen 		case NFS4_OPEN_CLAIM_FH:
564599c41515SJ. Bruce Fields 			/*
564699c41515SJ. Bruce Fields 			 * Let's not give out any delegations till everyone's
5647c87fb4a3SJ. Bruce Fields 			 * had the chance to reclaim theirs, *and* until
5648c87fb4a3SJ. Bruce Fields 			 * NLM locks have all been reclaimed:
564999c41515SJ. Bruce Fields 			 */
56504cf59221SJeff Layton 			if (locks_in_grace(clp->net))
565199c41515SJ. Bruce Fields 				goto out_no_deleg;
5652dad1c067SJ. Bruce Fields 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
565399c41515SJ. Bruce Fields 				goto out_no_deleg;
56541d3dd1d5SDai Ngo 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
56551d3dd1d5SDai Ngo 					!clp->cl_minorversion)
56561d3dd1d5SDai Ngo 				goto out_no_deleg;
56577b190fecSNeilBrown 			break;
56587b190fecSNeilBrown 		default:
565999c41515SJ. Bruce Fields 			goto out_no_deleg;
56607b190fecSNeilBrown 	}
5661876c553cSJeff Layton 	dp = nfs4_set_delegation(open, stp, parent);
56620b26693cSJeff Layton 	if (IS_ERR(dp))
5663dd239cc0SJ. Bruce Fields 		goto out_no_deleg;
56641da177e4SLinus Torvalds 
5665d5477a8dSJ. Bruce Fields 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
56661da177e4SLinus Torvalds 
56671d3dd1d5SDai Ngo 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
56681d3dd1d5SDai Ngo 		open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
56691d3dd1d5SDai Ngo 		trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
56701d3dd1d5SDai Ngo 	} else {
567199c41515SJ. Bruce Fields 		open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
56721d3dd1d5SDai Ngo 		trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
56731d3dd1d5SDai Ngo 	}
567467cb1279STrond Myklebust 	nfs4_put_stid(&dp->dl_stid);
5675dd239cc0SJ. Bruce Fields 	return;
5676dd239cc0SJ. Bruce Fields out_no_deleg:
567799c41515SJ. Bruce Fields 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
56787b190fecSNeilBrown 	if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5679d08d32e6SJ. Bruce Fields 	    open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
56801da177e4SLinus Torvalds 		dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5681d08d32e6SJ. Bruce Fields 		open->op_recall = 1;
5682d08d32e6SJ. Bruce Fields 	}
5683dd239cc0SJ. Bruce Fields 
5684dd239cc0SJ. Bruce Fields 	/* 4.1 client asking for a delegation? */
5685dd239cc0SJ. Bruce Fields 	if (open->op_deleg_want)
5686dd239cc0SJ. Bruce Fields 		nfsd4_open_deleg_none_ext(open, status);
5687dd239cc0SJ. Bruce Fields 	return;
56881da177e4SLinus Torvalds }
56891da177e4SLinus Torvalds 
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)5690e27f49c3SBenny Halevy static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5691e27f49c3SBenny Halevy 					struct nfs4_delegation *dp)
5692e27f49c3SBenny Halevy {
5693e27f49c3SBenny Halevy 	if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5694e27f49c3SBenny Halevy 	    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5695e27f49c3SBenny Halevy 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5696e27f49c3SBenny Halevy 		open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5697e27f49c3SBenny Halevy 	} else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5698e27f49c3SBenny Halevy 		   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5699e27f49c3SBenny Halevy 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5700e27f49c3SBenny Halevy 		open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5701e27f49c3SBenny Halevy 	}
5702e27f49c3SBenny Halevy 	/* Otherwise the client must be confused wanting a delegation
5703e27f49c3SBenny Halevy 	 * it already has, therefore we don't return
5704e27f49c3SBenny Halevy 	 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5705e27f49c3SBenny Halevy 	 */
5706e27f49c3SBenny Halevy }
5707e27f49c3SBenny Halevy 
57087e2ce0ccSChuck Lever /**
57097e2ce0ccSChuck Lever  * nfsd4_process_open2 - finish open processing
57107e2ce0ccSChuck Lever  * @rqstp: the RPC transaction being executed
57117e2ce0ccSChuck Lever  * @current_fh: NFSv4 COMPOUND's current filehandle
57127e2ce0ccSChuck Lever  * @open: OPEN arguments
57137e2ce0ccSChuck Lever  *
57147e2ce0ccSChuck Lever  * If successful, (1) truncate the file if open->op_truncate was
57157e2ce0ccSChuck Lever  * set, (2) set open->op_stateid, (3) set open->op_delegation.
57167e2ce0ccSChuck Lever  *
57177e2ce0ccSChuck Lever  * Returns %nfs_ok on success; otherwise an nfs4stat value in
57187e2ce0ccSChuck Lever  * network byte order is returned.
57197e2ce0ccSChuck Lever  */
5720b37ad28bSAl Viro __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)57211da177e4SLinus Torvalds nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
57221da177e4SLinus Torvalds {
57236668958fSAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
572438c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
57251da177e4SLinus Torvalds 	struct nfs4_file *fp = NULL;
5726dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp = NULL;
5727567d9829SNeilBrown 	struct nfs4_delegation *dp = NULL;
5728b37ad28bSAl Viro 	__be32 status;
5729d8a1a000STrond Myklebust 	bool new_stp = false;
57301da177e4SLinus Torvalds 
57311da177e4SLinus Torvalds 	/*
57321da177e4SLinus Torvalds 	 * Lookup file; if found, lookup stateid and check open request,
57331da177e4SLinus Torvalds 	 * and check for delegations in the process of being recalled.
57341da177e4SLinus Torvalds 	 * If not found, create the nfs4_file struct
57351da177e4SLinus Torvalds 	 */
57369270fc51SChuck Lever 	fp = nfsd4_file_hash_insert(open->op_file, current_fh);
5737d47b295eSChuck Lever 	if (unlikely(!fp))
5738d47b295eSChuck Lever 		return nfserr_jukebox;
5739950e0118STrond Myklebust 	if (fp != open->op_file) {
574041d22663SJ. Bruce Fields 		status = nfs4_check_deleg(cl, open, &dp);
5741c44c5eebSNeilBrown 		if (status)
5742c44c5eebSNeilBrown 			goto out;
574315ca08d3STrond Myklebust 		stp = nfsd4_find_and_lock_existing_open(fp, open);
57441da177e4SLinus Torvalds 	} else {
5745950e0118STrond Myklebust 		open->op_file = NULL;
5746c44c5eebSNeilBrown 		status = nfserr_bad_stateid;
57478b289b2cSJ. Bruce Fields 		if (nfsd4_is_deleg_cur(open))
5748c44c5eebSNeilBrown 			goto out;
57491da177e4SLinus Torvalds 	}
57501da177e4SLinus Torvalds 
5751d8a1a000STrond Myklebust 	if (!stp) {
5752d8a1a000STrond Myklebust 		stp = init_open_stateid(fp, open);
5753d8a1a000STrond Myklebust 		if (!open->op_stp)
5754d8a1a000STrond Myklebust 			new_stp = true;
5755d8a1a000STrond Myklebust 	}
5756d8a1a000STrond Myklebust 
57571da177e4SLinus Torvalds 	/*
57581da177e4SLinus Torvalds 	 * OPEN the file, or upgrade an existing OPEN.
57591da177e4SLinus Torvalds 	 * If truncate fails, the OPEN fails.
5760d8a1a000STrond Myklebust 	 *
5761d8a1a000STrond Myklebust 	 * stp is already locked.
57621da177e4SLinus Torvalds 	 */
5763d8a1a000STrond Myklebust 	if (!new_stp) {
57641da177e4SLinus Torvalds 		/* Stateid was found, this is an OPEN upgrade */
5765f9d7562fSJ. Bruce Fields 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
576635a92fe8SJeff Layton 		if (status) {
5767feb9dad5SOleg Drokin 			mutex_unlock(&stp->st_mutex);
57681da177e4SLinus Torvalds 			goto out;
576935a92fe8SJeff Layton 		}
57701da177e4SLinus Torvalds 	} else {
57713d694271SDai Ngo 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
57726eb3a1d0SJeff Layton 		if (status) {
5773d8a1a000STrond Myklebust 			stp->st_stid.sc_type = NFS4_CLOSED_STID;
57746eb3a1d0SJeff Layton 			release_open_stateid(stp);
5775d8a1a000STrond Myklebust 			mutex_unlock(&stp->st_mutex);
57766eb3a1d0SJeff Layton 			goto out;
57776eb3a1d0SJeff Layton 		}
57788287f009SSachin Bhamare 
57798287f009SSachin Bhamare 		stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
57808287f009SSachin Bhamare 							open->op_odstate);
57818287f009SSachin Bhamare 		if (stp->st_clnt_odstate == open->op_odstate)
57828287f009SSachin Bhamare 			open->op_odstate = NULL;
57831da177e4SLinus Torvalds 	}
5784d8a1a000STrond Myklebust 
57859767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5786feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
57871da177e4SLinus Torvalds 
5788d24433cdSBenny Halevy 	if (nfsd4_has_session(&resp->cstate)) {
5789d24433cdSBenny Halevy 		if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5790d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5791d24433cdSBenny Halevy 			open->op_why_no_deleg = WND4_NOT_WANTED;
5792d24433cdSBenny Halevy 			goto nodeleg;
5793d24433cdSBenny Halevy 		}
5794d24433cdSBenny Halevy 	}
5795d24433cdSBenny Halevy 
57961da177e4SLinus Torvalds 	/*
57971da177e4SLinus Torvalds 	* Attempt to hand out a delegation. No error return, because the
57981da177e4SLinus Torvalds 	* OPEN succeeds even if we fail.
57991da177e4SLinus Torvalds 	*/
5800876c553cSJeff Layton 	nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
5801d24433cdSBenny Halevy nodeleg:
58021da177e4SLinus Torvalds 	status = nfs_ok;
58033caf9175SHou Tao 	trace_nfsd_open(&stp->st_stid.sc_stateid);
58041da177e4SLinus Torvalds out:
5805d24433cdSBenny Halevy 	/* 4.1 client trying to upgrade/downgrade delegation? */
5806d24433cdSBenny Halevy 	if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5807e27f49c3SBenny Halevy 	    open->op_deleg_want)
5808e27f49c3SBenny Halevy 		nfsd4_deleg_xgrade_none_ext(open, dp);
5809d24433cdSBenny Halevy 
581013cd2184SNeilBrown 	if (fp)
581113cd2184SNeilBrown 		put_nfs4_file(fp);
581237515177SNeilBrown 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
581387186022SKinglong Mee 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
58141da177e4SLinus Torvalds 	/*
58151da177e4SLinus Torvalds 	* To finish the open response, we just need to set the rflags.
58161da177e4SLinus Torvalds 	*/
58171da177e4SLinus Torvalds 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
581819e4c347SJeff Layton 	if (nfsd4_has_session(&resp->cstate))
581919e4c347SJeff Layton 		open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
582019e4c347SJeff Layton 	else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
58211da177e4SLinus Torvalds 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
582219e4c347SJeff Layton 
5823dcd94cc2STrond Myklebust 	if (dp)
5824dcd94cc2STrond Myklebust 		nfs4_put_stid(&dp->dl_stid);
5825d6f2bc5dSTrond Myklebust 	if (stp)
5826d6f2bc5dSTrond Myklebust 		nfs4_put_stid(&stp->st_stid);
58271da177e4SLinus Torvalds 
58281da177e4SLinus Torvalds 	return status;
58291da177e4SLinus Torvalds }
58301da177e4SLinus Torvalds 
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)583158fb12e6SJeff Layton void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
583242297899SJeff Layton 			      struct nfsd4_open *open)
5833d29b20cdSJ. Bruce Fields {
5834d29b20cdSJ. Bruce Fields 	if (open->op_openowner) {
5835d3134b10SJeff Layton 		struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5836d29b20cdSJ. Bruce Fields 
5837d3134b10SJeff Layton 		nfsd4_cstate_assign_replay(cstate, so);
5838d3134b10SJeff Layton 		nfs4_put_stateowner(so);
5839d29b20cdSJ. Bruce Fields 	}
584032513b40SJ. Bruce Fields 	if (open->op_file)
58415b095e99SJeff Layton 		kmem_cache_free(file_slab, open->op_file);
58424cdc951bSJ. Bruce Fields 	if (open->op_stp)
58436011695dSTrond Myklebust 		nfs4_put_stid(&open->op_stp->st_stid);
58448287f009SSachin Bhamare 	if (open->op_odstate)
58458287f009SSachin Bhamare 		kmem_cache_free(odstate_slab, open->op_odstate);
5846d29b20cdSJ. Bruce Fields }
5847d29b20cdSJ. Bruce Fields 
5848b37ad28bSAl Viro __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)5849b591480bSJ.Bruce Fields nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5850eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
58511da177e4SLinus Torvalds {
5852eb69853dSChristoph Hellwig 	clientid_t *clid = &u->renew;
58531da177e4SLinus Torvalds 	struct nfs4_client *clp;
5854b37ad28bSAl Viro 	__be32 status;
58557f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
58561da177e4SLinus Torvalds 
5857dd5e3fbcSChuck Lever 	trace_nfsd_clid_renew(clid);
5858f71475baSJ. Bruce Fields 	status = set_client(clid, cstate, nn);
58599b2ef62bSJ. Bruce Fields 	if (status)
5860b4587eb2SJ. Bruce Fields 		return status;
58614b24ca7dSJeff Layton 	clp = cstate->clp;
5862ea1da636SNeilBrown 	if (!list_empty(&clp->cl_delegations)
586377a3569dSJ. Bruce Fields 			&& clp->cl_cb_state != NFSD4_CB_UP)
5864b4587eb2SJ. Bruce Fields 		return nfserr_cb_path_down;
5865b4587eb2SJ. Bruce Fields 	return nfs_ok;
58661da177e4SLinus Torvalds }
58671da177e4SLinus Torvalds 
58687f5ef2e9SJeff Layton void
nfsd4_end_grace(struct nfsd_net * nn)586912760c66SStanislav Kinsbursky nfsd4_end_grace(struct nfsd_net *nn)
5870a76b4319SNeilBrown {
587133dcc481SJeff Layton 	/* do nothing if grace period already ended */
5872a51c84edSStanislav Kinsbursky 	if (nn->grace_ended)
587333dcc481SJeff Layton 		return;
587433dcc481SJeff Layton 
5875dd5e3fbcSChuck Lever 	trace_nfsd_grace_complete(nn);
5876a51c84edSStanislav Kinsbursky 	nn->grace_ended = true;
587770b28235SJ. Bruce Fields 	/*
587870b28235SJ. Bruce Fields 	 * If the server goes down again right now, an NFSv4
587970b28235SJ. Bruce Fields 	 * client will still be allowed to reclaim after it comes back up,
588070b28235SJ. Bruce Fields 	 * even if it hasn't yet had a chance to reclaim state this time.
588170b28235SJ. Bruce Fields 	 *
588270b28235SJ. Bruce Fields 	 */
5883919b8049SJeff Layton 	nfsd4_record_grace_done(nn);
588470b28235SJ. Bruce Fields 	/*
588570b28235SJ. Bruce Fields 	 * At this point, NFSv4 clients can still reclaim.  But if the
588670b28235SJ. Bruce Fields 	 * server crashes, any that have not yet reclaimed will be out
588770b28235SJ. Bruce Fields 	 * of luck on the next boot.
588870b28235SJ. Bruce Fields 	 *
588970b28235SJ. Bruce Fields 	 * (NFSv4.1+ clients are considered to have reclaimed once they
589070b28235SJ. Bruce Fields 	 * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
589170b28235SJ. Bruce Fields 	 * have reclaimed after their first OPEN.)
589270b28235SJ. Bruce Fields 	 */
58935e1533c7SStanislav Kinsbursky 	locks_end_grace(&nn->nfsd4_manager);
589470b28235SJ. Bruce Fields 	/*
589570b28235SJ. Bruce Fields 	 * At this point, and once lockd and/or any other containers
589670b28235SJ. Bruce Fields 	 * exit their grace period, further reclaims will fail and
589770b28235SJ. Bruce Fields 	 * regular locking can resume.
589870b28235SJ. Bruce Fields 	 */
5899a76b4319SNeilBrown }
5900a76b4319SNeilBrown 
590103f318caSJ. Bruce Fields /*
590203f318caSJ. Bruce Fields  * If we've waited a lease period but there are still clients trying to
590303f318caSJ. Bruce Fields  * reclaim, wait a little longer to give them a chance to finish.
590403f318caSJ. Bruce Fields  */
clients_still_reclaiming(struct nfsd_net * nn)590503f318caSJ. Bruce Fields static bool clients_still_reclaiming(struct nfsd_net *nn)
590603f318caSJ. Bruce Fields {
590720b7d86fSArnd Bergmann 	time64_t double_grace_period_end = nn->boot_time +
590820b7d86fSArnd Bergmann 					   2 * nn->nfsd4_lease;
590903f318caSJ. Bruce Fields 
5910362063a5SScott Mayhew 	if (nn->track_reclaim_completes &&
5911362063a5SScott Mayhew 			atomic_read(&nn->nr_reclaim_complete) ==
5912362063a5SScott Mayhew 			nn->reclaim_str_hashtbl_size)
5913362063a5SScott Mayhew 		return false;
591403f318caSJ. Bruce Fields 	if (!nn->somebody_reclaimed)
591503f318caSJ. Bruce Fields 		return false;
591603f318caSJ. Bruce Fields 	nn->somebody_reclaimed = false;
591703f318caSJ. Bruce Fields 	/*
591803f318caSJ. Bruce Fields 	 * If we've given them *two* lease times to reclaim, and they're
591903f318caSJ. Bruce Fields 	 * still not done, give up:
592003f318caSJ. Bruce Fields 	 */
592120b7d86fSArnd Bergmann 	if (ktime_get_boottime_seconds() > double_grace_period_end)
592203f318caSJ. Bruce Fields 		return false;
592303f318caSJ. Bruce Fields 	return true;
592403f318caSJ. Bruce Fields }
592503f318caSJ. Bruce Fields 
59267f7e7a40SJ. Bruce Fields struct laundry_time {
59277f7e7a40SJ. Bruce Fields 	time64_t cutoff;
59287f7e7a40SJ. Bruce Fields 	time64_t new_timeo;
59297f7e7a40SJ. Bruce Fields };
59307f7e7a40SJ. Bruce Fields 
state_expired(struct laundry_time * lt,time64_t last_refresh)59317f7e7a40SJ. Bruce Fields static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
59327f7e7a40SJ. Bruce Fields {
59337f7e7a40SJ. Bruce Fields 	time64_t time_remaining;
59347f7e7a40SJ. Bruce Fields 
59357f7e7a40SJ. Bruce Fields 	if (last_refresh < lt->cutoff)
59367f7e7a40SJ. Bruce Fields 		return true;
59377f7e7a40SJ. Bruce Fields 	time_remaining = last_refresh - lt->cutoff;
59387f7e7a40SJ. Bruce Fields 	lt->new_timeo = min(lt->new_timeo, time_remaining);
59397f7e7a40SJ. Bruce Fields 	return false;
59407f7e7a40SJ. Bruce Fields }
59417f7e7a40SJ. Bruce Fields 
5942f4e44b39SDai Ngo #ifdef CONFIG_NFSD_V4_2_INTER_SSC
nfsd4_ssc_init_umount_work(struct nfsd_net * nn)5943f4e44b39SDai Ngo void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
5944f4e44b39SDai Ngo {
5945f4e44b39SDai Ngo 	spin_lock_init(&nn->nfsd_ssc_lock);
5946f4e44b39SDai Ngo 	INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
5947f4e44b39SDai Ngo 	init_waitqueue_head(&nn->nfsd_ssc_waitq);
5948f4e44b39SDai Ngo }
5949f4e44b39SDai Ngo EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work);
5950f4e44b39SDai Ngo 
5951f4e44b39SDai Ngo /*
5952f4e44b39SDai Ngo  * This is called when nfsd is being shutdown, after all inter_ssc
5953f4e44b39SDai Ngo  * cleanup were done, to destroy the ssc delayed unmount list.
5954f4e44b39SDai Ngo  */
nfsd4_ssc_shutdown_umount(struct nfsd_net * nn)5955f4e44b39SDai Ngo static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
5956f4e44b39SDai Ngo {
5957f47dc2d3SDai Ngo 	struct nfsd4_ssc_umount_item *ni = NULL;
5958f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
5959f4e44b39SDai Ngo 
5960f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
5961f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5962f4e44b39SDai Ngo 		list_del(&ni->nsui_list);
5963f4e44b39SDai Ngo 		spin_unlock(&nn->nfsd_ssc_lock);
5964f4e44b39SDai Ngo 		mntput(ni->nsui_vfsmount);
5965f4e44b39SDai Ngo 		kfree(ni);
5966f4e44b39SDai Ngo 		spin_lock(&nn->nfsd_ssc_lock);
5967f4e44b39SDai Ngo 	}
5968f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
5969f4e44b39SDai Ngo }
5970f4e44b39SDai Ngo 
nfsd4_ssc_expire_umount(struct nfsd_net * nn)5971f4e44b39SDai Ngo static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
5972f4e44b39SDai Ngo {
5973f4e44b39SDai Ngo 	bool do_wakeup = false;
59748e70bf27SColin Ian King 	struct nfsd4_ssc_umount_item *ni = NULL;
5975f4e44b39SDai Ngo 	struct nfsd4_ssc_umount_item *tmp;
5976f4e44b39SDai Ngo 
5977f4e44b39SDai Ngo 	spin_lock(&nn->nfsd_ssc_lock);
5978f4e44b39SDai Ngo 	list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
5979f4e44b39SDai Ngo 		if (time_after(jiffies, ni->nsui_expire)) {
5980f4e44b39SDai Ngo 			if (refcount_read(&ni->nsui_refcnt) > 1)
5981f4e44b39SDai Ngo 				continue;
5982f4e44b39SDai Ngo 
5983f4e44b39SDai Ngo 			/* mark being unmount */
5984f4e44b39SDai Ngo 			ni->nsui_busy = true;
5985f4e44b39SDai Ngo 			spin_unlock(&nn->nfsd_ssc_lock);
5986f4e44b39SDai Ngo 			mntput(ni->nsui_vfsmount);
5987f4e44b39SDai Ngo 			spin_lock(&nn->nfsd_ssc_lock);
5988f4e44b39SDai Ngo 
5989f4e44b39SDai Ngo 			/* waiters need to start from begin of list */
5990f4e44b39SDai Ngo 			list_del(&ni->nsui_list);
5991f4e44b39SDai Ngo 			kfree(ni);
5992f4e44b39SDai Ngo 
5993f4e44b39SDai Ngo 			/* wakeup ssc_connect waiters */
5994f4e44b39SDai Ngo 			do_wakeup = true;
5995f4e44b39SDai Ngo 			continue;
5996f4e44b39SDai Ngo 		}
5997f4e44b39SDai Ngo 		break;
5998f4e44b39SDai Ngo 	}
5999f4e44b39SDai Ngo 	if (do_wakeup)
6000f4e44b39SDai Ngo 		wake_up_all(&nn->nfsd_ssc_waitq);
6001f4e44b39SDai Ngo 	spin_unlock(&nn->nfsd_ssc_lock);
6002f4e44b39SDai Ngo }
6003f4e44b39SDai Ngo #endif
6004f4e44b39SDai Ngo 
600527431affSDai Ngo /* Check if any lock belonging to this lockowner has any blockers */
60063d694271SDai Ngo static bool
nfs4_lockowner_has_blockers(struct nfs4_lockowner * lo)600727431affSDai Ngo nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
600827431affSDai Ngo {
600927431affSDai Ngo 	struct file_lock_context *ctx;
601027431affSDai Ngo 	struct nfs4_ol_stateid *stp;
601127431affSDai Ngo 	struct nfs4_file *nf;
601227431affSDai Ngo 
601327431affSDai Ngo 	list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
601427431affSDai Ngo 		nf = stp->st_stid.sc_file;
601577c67530SJeff Layton 		ctx = locks_inode_context(nf->fi_inode);
601627431affSDai Ngo 		if (!ctx)
601727431affSDai Ngo 			continue;
601827431affSDai Ngo 		if (locks_owner_has_blockers(ctx, lo))
601927431affSDai Ngo 			return true;
602027431affSDai Ngo 	}
602127431affSDai Ngo 	return false;
602227431affSDai Ngo }
602327431affSDai Ngo 
602427431affSDai Ngo static bool
nfs4_anylock_blockers(struct nfs4_client * clp)602527431affSDai Ngo nfs4_anylock_blockers(struct nfs4_client *clp)
60263d694271SDai Ngo {
60273d694271SDai Ngo 	int i;
60283d694271SDai Ngo 	struct nfs4_stateowner *so;
602927431affSDai Ngo 	struct nfs4_lockowner *lo;
60303d694271SDai Ngo 
603127431affSDai Ngo 	if (atomic_read(&clp->cl_delegs_in_recall))
603227431affSDai Ngo 		return true;
60333d694271SDai Ngo 	spin_lock(&clp->cl_lock);
60343d694271SDai Ngo 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
60353d694271SDai Ngo 		list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
60363d694271SDai Ngo 				so_strhash) {
60373d694271SDai Ngo 			if (so->so_is_open_owner)
60383d694271SDai Ngo 				continue;
603927431affSDai Ngo 			lo = lockowner(so);
604027431affSDai Ngo 			if (nfs4_lockowner_has_blockers(lo)) {
60413d694271SDai Ngo 				spin_unlock(&clp->cl_lock);
60423d694271SDai Ngo 				return true;
60433d694271SDai Ngo 			}
60443d694271SDai Ngo 		}
60453d694271SDai Ngo 	}
604627431affSDai Ngo 	spin_unlock(&clp->cl_lock);
604766af2579SDai Ngo 	return false;
604866af2579SDai Ngo }
604966af2579SDai Ngo 
605066af2579SDai Ngo static void
nfs4_get_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist,struct laundry_time * lt)605166af2579SDai Ngo nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
605266af2579SDai Ngo 				struct laundry_time *lt)
605366af2579SDai Ngo {
60544271c2c0SDai Ngo 	unsigned int maxreap, reapcnt = 0;
605566af2579SDai Ngo 	struct list_head *pos, *next;
605666af2579SDai Ngo 	struct nfs4_client *clp;
605766af2579SDai Ngo 
60584271c2c0SDai Ngo 	maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
60594271c2c0SDai Ngo 			NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
606066af2579SDai Ngo 	INIT_LIST_HEAD(reaplist);
606166af2579SDai Ngo 	spin_lock(&nn->client_lock);
606266af2579SDai Ngo 	list_for_each_safe(pos, next, &nn->client_lru) {
606366af2579SDai Ngo 		clp = list_entry(pos, struct nfs4_client, cl_lru);
606466af2579SDai Ngo 		if (clp->cl_state == NFSD4_EXPIRABLE)
606566af2579SDai Ngo 			goto exp_client;
606666af2579SDai Ngo 		if (!state_expired(lt, clp->cl_time))
606766af2579SDai Ngo 			break;
60683a4ea23dSDai Ngo 		if (!atomic_read(&clp->cl_rpc_users)) {
60693a4ea23dSDai Ngo 			if (clp->cl_state == NFSD4_ACTIVE)
60703a4ea23dSDai Ngo 				atomic_inc(&nn->nfsd_courtesy_clients);
607166af2579SDai Ngo 			clp->cl_state = NFSD4_COURTESY;
60723a4ea23dSDai Ngo 		}
60734271c2c0SDai Ngo 		if (!client_has_state(clp))
607466af2579SDai Ngo 			goto exp_client;
60754271c2c0SDai Ngo 		if (!nfs4_anylock_blockers(clp))
60764271c2c0SDai Ngo 			if (reapcnt >= maxreap)
60774271c2c0SDai Ngo 				continue;
607866af2579SDai Ngo exp_client:
60794271c2c0SDai Ngo 		if (!mark_client_expired_locked(clp)) {
608066af2579SDai Ngo 			list_add(&clp->cl_lru, reaplist);
60814271c2c0SDai Ngo 			reapcnt++;
608266af2579SDai Ngo 		}
608366af2579SDai Ngo 	}
608466af2579SDai Ngo 	spin_unlock(&nn->client_lock);
608566af2579SDai Ngo }
608666af2579SDai Ngo 
60877746b32fSDai Ngo static void
nfs4_get_courtesy_client_reaplist(struct nfsd_net * nn,struct list_head * reaplist)60887746b32fSDai Ngo nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
60897746b32fSDai Ngo 				struct list_head *reaplist)
60907746b32fSDai Ngo {
60917746b32fSDai Ngo 	unsigned int maxreap = 0, reapcnt = 0;
60927746b32fSDai Ngo 	struct list_head *pos, *next;
60937746b32fSDai Ngo 	struct nfs4_client *clp;
60947746b32fSDai Ngo 
60957746b32fSDai Ngo 	maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
60967746b32fSDai Ngo 	INIT_LIST_HEAD(reaplist);
60977746b32fSDai Ngo 
60987746b32fSDai Ngo 	spin_lock(&nn->client_lock);
60997746b32fSDai Ngo 	list_for_each_safe(pos, next, &nn->client_lru) {
61007746b32fSDai Ngo 		clp = list_entry(pos, struct nfs4_client, cl_lru);
61017746b32fSDai Ngo 		if (clp->cl_state == NFSD4_ACTIVE)
61027746b32fSDai Ngo 			break;
61037746b32fSDai Ngo 		if (reapcnt >= maxreap)
61047746b32fSDai Ngo 			break;
61057746b32fSDai Ngo 		if (!mark_client_expired_locked(clp)) {
61067746b32fSDai Ngo 			list_add(&clp->cl_lru, reaplist);
61077746b32fSDai Ngo 			reapcnt++;
61087746b32fSDai Ngo 		}
61097746b32fSDai Ngo 	}
61107746b32fSDai Ngo 	spin_unlock(&nn->client_lock);
61117746b32fSDai Ngo }
61127746b32fSDai Ngo 
61137746b32fSDai Ngo static void
nfs4_process_client_reaplist(struct list_head * reaplist)61147746b32fSDai Ngo nfs4_process_client_reaplist(struct list_head *reaplist)
61157746b32fSDai Ngo {
61167746b32fSDai Ngo 	struct list_head *pos, *next;
61177746b32fSDai Ngo 	struct nfs4_client *clp;
61187746b32fSDai Ngo 
61197746b32fSDai Ngo 	list_for_each_safe(pos, next, reaplist) {
61207746b32fSDai Ngo 		clp = list_entry(pos, struct nfs4_client, cl_lru);
61217746b32fSDai Ngo 		trace_nfsd_clid_purged(&clp->cl_clientid);
61227746b32fSDai Ngo 		list_del_init(&clp->cl_lru);
61237746b32fSDai Ngo 		expire_client(clp);
61247746b32fSDai Ngo 	}
61257746b32fSDai Ngo }
61267746b32fSDai Ngo 
612720b7d86fSArnd Bergmann static time64_t
nfs4_laundromat(struct nfsd_net * nn)612809121281SStanislav Kinsbursky nfs4_laundromat(struct nfsd_net *nn)
61291da177e4SLinus Torvalds {
6130fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
61311da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
6132217526e7SJeff Layton 	struct nfs4_ol_stateid *stp;
61337919d0a2SJeff Layton 	struct nfsd4_blocked_lock *nbl;
61341da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
61357f7e7a40SJ. Bruce Fields 	struct laundry_time lt = {
61367f7e7a40SJ. Bruce Fields 		.cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
61377f7e7a40SJ. Bruce Fields 		.new_timeo = nn->nfsd4_lease
61387f7e7a40SJ. Bruce Fields 	};
6139624322f1SOlga Kornievskaia 	struct nfs4_cpntf_state *cps;
6140624322f1SOlga Kornievskaia 	copy_stateid_t *cps_t;
6141624322f1SOlga Kornievskaia 	int i;
61421da177e4SLinus Torvalds 
614303f318caSJ. Bruce Fields 	if (clients_still_reclaiming(nn)) {
61447f7e7a40SJ. Bruce Fields 		lt.new_timeo = 0;
614503f318caSJ. Bruce Fields 		goto out;
614603f318caSJ. Bruce Fields 	}
614712760c66SStanislav Kinsbursky 	nfsd4_end_grace(nn);
6148624322f1SOlga Kornievskaia 
6149624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
6150624322f1SOlga Kornievskaia 	idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
6151624322f1SOlga Kornievskaia 		cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
6152781fde1aSChuck Lever 		if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
61537f7e7a40SJ. Bruce Fields 				state_expired(&lt, cps->cpntf_time))
6154624322f1SOlga Kornievskaia 			_free_cpntf_state_locked(nn, cps);
6155624322f1SOlga Kornievskaia 	}
6156624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
615766af2579SDai Ngo 	nfs4_get_client_reaplist(nn, &reaplist, &lt);
61587746b32fSDai Ngo 	nfs4_process_client_reaplist(&reaplist);
61597746b32fSDai Ngo 
6160cdc97505SBenny Halevy 	spin_lock(&state_lock);
6161e8c69d17SJ. Bruce Fields 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
61621da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
61637f7e7a40SJ. Bruce Fields 		if (!state_expired(&lt, dp->dl_time))
61641da177e4SLinus Torvalds 			break;
61653fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
616642690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
61671da177e4SLinus Torvalds 	}
6168cdc97505SBenny Halevy 	spin_unlock(&state_lock);
61692d4a532dSJeff Layton 	while (!list_empty(&reaplist)) {
61702d4a532dSJeff Layton 		dp = list_first_entry(&reaplist, struct nfs4_delegation,
61712d4a532dSJeff Layton 					dl_recall_lru);
61722d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
61733bd64a5bSJ. Bruce Fields 		revoke_delegation(dp);
61741da177e4SLinus Torvalds 	}
6175217526e7SJeff Layton 
6176217526e7SJeff Layton 	spin_lock(&nn->client_lock);
6177217526e7SJeff Layton 	while (!list_empty(&nn->close_lru)) {
6178217526e7SJeff Layton 		oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
6179217526e7SJeff Layton 					oo_close_lru);
61807f7e7a40SJ. Bruce Fields 		if (!state_expired(&lt, oo->oo_time))
61811da177e4SLinus Torvalds 			break;
6182217526e7SJeff Layton 		list_del_init(&oo->oo_close_lru);
6183217526e7SJeff Layton 		stp = oo->oo_last_closed_stid;
6184217526e7SJeff Layton 		oo->oo_last_closed_stid = NULL;
6185217526e7SJeff Layton 		spin_unlock(&nn->client_lock);
6186217526e7SJeff Layton 		nfs4_put_stid(&stp->st_stid);
6187217526e7SJeff Layton 		spin_lock(&nn->client_lock);
61881da177e4SLinus Torvalds 	}
6189217526e7SJeff Layton 	spin_unlock(&nn->client_lock);
6190217526e7SJeff Layton 
61917919d0a2SJeff Layton 	/*
61927919d0a2SJeff Layton 	 * It's possible for a client to try and acquire an already held lock
61937919d0a2SJeff Layton 	 * that is being held for a long time, and then lose interest in it.
61947919d0a2SJeff Layton 	 * So, we clean out any un-revisited request after a lease period
61957919d0a2SJeff Layton 	 * under the assumption that the client is no longer interested.
61967919d0a2SJeff Layton 	 *
61977919d0a2SJeff Layton 	 * RFC5661, sec. 9.6 states that the client must not rely on getting
61987919d0a2SJeff Layton 	 * notifications and must continue to poll for locks, even when the
61997919d0a2SJeff Layton 	 * server supports them. Thus this shouldn't lead to clients blocking
62007919d0a2SJeff Layton 	 * indefinitely once the lock does become free.
62017919d0a2SJeff Layton 	 */
62027919d0a2SJeff Layton 	BUG_ON(!list_empty(&reaplist));
62030cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
62047919d0a2SJeff Layton 	while (!list_empty(&nn->blocked_locks_lru)) {
62057919d0a2SJeff Layton 		nbl = list_first_entry(&nn->blocked_locks_lru,
62067919d0a2SJeff Layton 					struct nfsd4_blocked_lock, nbl_lru);
62077f7e7a40SJ. Bruce Fields 		if (!state_expired(&lt, nbl->nbl_time))
62087919d0a2SJeff Layton 			break;
62097919d0a2SJeff Layton 		list_move(&nbl->nbl_lru, &reaplist);
62107919d0a2SJeff Layton 		list_del_init(&nbl->nbl_list);
62117919d0a2SJeff Layton 	}
62120cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
62137919d0a2SJeff Layton 
62147919d0a2SJeff Layton 	while (!list_empty(&reaplist)) {
621564ebe124SNaofumi Honda 		nbl = list_first_entry(&reaplist,
62167919d0a2SJeff Layton 					struct nfsd4_blocked_lock, nbl_lru);
62177919d0a2SJeff Layton 		list_del_init(&nbl->nbl_lru);
62187919d0a2SJeff Layton 		free_blocked_lock(nbl);
62197919d0a2SJeff Layton 	}
6220f4e44b39SDai Ngo #ifdef CONFIG_NFSD_V4_2_INTER_SSC
6221f4e44b39SDai Ngo 	/* service the server-to-server copy delayed unmount list */
6222f4e44b39SDai Ngo 	nfsd4_ssc_expire_umount(nn);
6223f4e44b39SDai Ngo #endif
622403f318caSJ. Bruce Fields out:
62257f7e7a40SJ. Bruce Fields 	return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
62261da177e4SLinus Torvalds }
62271da177e4SLinus Torvalds 
6228a254b246SHarvey Harrison static void laundromat_main(struct work_struct *);
6229a254b246SHarvey Harrison 
6230a254b246SHarvey Harrison static void
laundromat_main(struct work_struct * laundry)623109121281SStanislav Kinsbursky laundromat_main(struct work_struct *laundry)
62321da177e4SLinus Torvalds {
623320b7d86fSArnd Bergmann 	time64_t t;
62342e55f3abSGeliang Tang 	struct delayed_work *dwork = to_delayed_work(laundry);
623509121281SStanislav Kinsbursky 	struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
623609121281SStanislav Kinsbursky 					   laundromat_work);
62371da177e4SLinus Torvalds 
623809121281SStanislav Kinsbursky 	t = nfs4_laundromat(nn);
623909121281SStanislav Kinsbursky 	queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
62401da177e4SLinus Torvalds }
62411da177e4SLinus Torvalds 
62427746b32fSDai Ngo static void
courtesy_client_reaper(struct nfsd_net * nn)6243a1049eb4SDai Ngo courtesy_client_reaper(struct nfsd_net *nn)
62447746b32fSDai Ngo {
62457746b32fSDai Ngo 	struct list_head reaplist;
62467746b32fSDai Ngo 
62477746b32fSDai Ngo 	nfs4_get_courtesy_client_reaplist(nn, &reaplist);
62487746b32fSDai Ngo 	nfs4_process_client_reaplist(&reaplist);
62497746b32fSDai Ngo }
62507746b32fSDai Ngo 
6251a1049eb4SDai Ngo static void
deleg_reaper(struct nfsd_net * nn)625244df6f43SDai Ngo deleg_reaper(struct nfsd_net *nn)
625344df6f43SDai Ngo {
625444df6f43SDai Ngo 	struct list_head *pos, *next;
625544df6f43SDai Ngo 	struct nfs4_client *clp;
625644df6f43SDai Ngo 	struct list_head cblist;
625744df6f43SDai Ngo 
625844df6f43SDai Ngo 	INIT_LIST_HEAD(&cblist);
625944df6f43SDai Ngo 	spin_lock(&nn->client_lock);
626044df6f43SDai Ngo 	list_for_each_safe(pos, next, &nn->client_lru) {
626144df6f43SDai Ngo 		clp = list_entry(pos, struct nfs4_client, cl_lru);
626244df6f43SDai Ngo 		if (clp->cl_state != NFSD4_ACTIVE ||
626344df6f43SDai Ngo 			list_empty(&clp->cl_delegations) ||
626444df6f43SDai Ngo 			atomic_read(&clp->cl_delegs_in_recall) ||
626544df6f43SDai Ngo 			test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags) ||
626644df6f43SDai Ngo 			(ktime_get_boottime_seconds() -
626744df6f43SDai Ngo 				clp->cl_ra_time < 5)) {
626844df6f43SDai Ngo 			continue;
626944df6f43SDai Ngo 		}
627044df6f43SDai Ngo 		list_add(&clp->cl_ra_cblist, &cblist);
627144df6f43SDai Ngo 
627244df6f43SDai Ngo 		/* release in nfsd4_cb_recall_any_release */
6273*9d60e8ecSJeff Layton 		kref_get(&clp->cl_nfsdfs.cl_ref);
627444df6f43SDai Ngo 		set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
627544df6f43SDai Ngo 		clp->cl_ra_time = ktime_get_boottime_seconds();
627644df6f43SDai Ngo 	}
627744df6f43SDai Ngo 	spin_unlock(&nn->client_lock);
627844df6f43SDai Ngo 
627944df6f43SDai Ngo 	while (!list_empty(&cblist)) {
628044df6f43SDai Ngo 		clp = list_first_entry(&cblist, struct nfs4_client,
628144df6f43SDai Ngo 					cl_ra_cblist);
628244df6f43SDai Ngo 		list_del_init(&clp->cl_ra_cblist);
628344df6f43SDai Ngo 		clp->cl_ra->ra_keep = 0;
628444df6f43SDai Ngo 		clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG);
6285638593beSDai Ngo 		trace_nfsd_cb_recall_any(clp->cl_ra);
628644df6f43SDai Ngo 		nfsd4_run_cb(&clp->cl_ra->ra_cb);
628744df6f43SDai Ngo 	}
628844df6f43SDai Ngo }
628944df6f43SDai Ngo 
629044df6f43SDai Ngo static void
nfsd4_state_shrinker_worker(struct work_struct * work)6291a1049eb4SDai Ngo nfsd4_state_shrinker_worker(struct work_struct *work)
6292a1049eb4SDai Ngo {
62937c24fa22SDai Ngo 	struct nfsd_net *nn = container_of(work, struct nfsd_net,
6294a1049eb4SDai Ngo 				nfsd_shrinker_work);
6295a1049eb4SDai Ngo 
6296a1049eb4SDai Ngo 	courtesy_client_reaper(nn);
629744df6f43SDai Ngo 	deleg_reaper(nn);
6298a1049eb4SDai Ngo }
6299a1049eb4SDai Ngo 
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)63008fcd461dSJeff Layton static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
6301f8816512SNeilBrown {
63028fcd461dSJeff Layton 	if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
6303f7a4d872SJ. Bruce Fields 		return nfserr_bad_stateid;
6304f7a4d872SJ. Bruce Fields 	return nfs_ok;
63051da177e4SLinus Torvalds }
63061da177e4SLinus Torvalds 
63071da177e4SLinus Torvalds static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)6308dcef0413SJ. Bruce Fields __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
63091da177e4SLinus Torvalds {
6310b37ad28bSAl Viro         __be32 status = nfserr_openmode;
63111da177e4SLinus Torvalds 
631202921914SJ. Bruce Fields 	/* For lock stateid's, we test the parent open, not the lock: */
631302921914SJ. Bruce Fields 	if (stp->st_openstp)
631402921914SJ. Bruce Fields 		stp = stp->st_openstp;
631582c5ff1bSJeff Layton 	if ((flags & WR_STATE) && !access_permit_write(stp))
63161da177e4SLinus Torvalds                 goto out;
631782c5ff1bSJeff Layton 	if ((flags & RD_STATE) && !access_permit_read(stp))
63181da177e4SLinus Torvalds                 goto out;
63191da177e4SLinus Torvalds 	status = nfs_ok;
63201da177e4SLinus Torvalds out:
63211da177e4SLinus Torvalds 	return status;
63221da177e4SLinus Torvalds }
63231da177e4SLinus Torvalds 
6324b37ad28bSAl Viro static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)63255ccb0066SStanislav Kinsbursky check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
63261da177e4SLinus Torvalds {
6327203a8c8eSJ. Bruce Fields 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
63281da177e4SLinus Torvalds 		return nfs_ok;
6329c87fb4a3SJ. Bruce Fields 	else if (opens_in_grace(net)) {
633025985edcSLucas De Marchi 		/* Answer in remaining cases depends on existence of
63311da177e4SLinus Torvalds 		 * conflicting state; so we must wait out the grace period. */
63321da177e4SLinus Torvalds 		return nfserr_grace;
63331da177e4SLinus Torvalds 	} else if (flags & WR_STATE)
63341da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
63351da177e4SLinus Torvalds 				NFS4_SHARE_DENY_WRITE);
63361da177e4SLinus Torvalds 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
63371da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
63381da177e4SLinus Torvalds 				NFS4_SHARE_DENY_READ);
63391da177e4SLinus Torvalds }
63401da177e4SLinus Torvalds 
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)634157b7b43bSJ. Bruce Fields static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
63420836f587SJ. Bruce Fields {
63436668958fSAndy Adamson 	/*
63446668958fSAndy Adamson 	 * When sessions are used the stateid generation number is ignored
63456668958fSAndy Adamson 	 * when it is zero.
63466668958fSAndy Adamson 	 */
634728dde241SJ. Bruce Fields 	if (has_session && in->si_generation == 0)
634881b82965SJ. Bruce Fields 		return nfs_ok;
634981b82965SJ. Bruce Fields 
635081b82965SJ. Bruce Fields 	if (in->si_generation == ref->si_generation)
635181b82965SJ. Bruce Fields 		return nfs_ok;
63526668958fSAndy Adamson 
63530836f587SJ. Bruce Fields 	/* If the client sends us a stateid from the future, it's buggy: */
635414b7f4a1SJeff Layton 	if (nfsd4_stateid_generation_after(in, ref))
63550836f587SJ. Bruce Fields 		return nfserr_bad_stateid;
63560836f587SJ. Bruce Fields 	/*
635781b82965SJ. Bruce Fields 	 * However, we could see a stateid from the past, even from a
635881b82965SJ. Bruce Fields 	 * non-buggy client.  For example, if the client sends a lock
635981b82965SJ. Bruce Fields 	 * while some IO is outstanding, the lock may bump si_generation
636081b82965SJ. Bruce Fields 	 * while the IO is still in flight.  The client could avoid that
636181b82965SJ. Bruce Fields 	 * situation by waiting for responses on all the IO requests,
636281b82965SJ. Bruce Fields 	 * but better performance may result in retrying IO that
636381b82965SJ. Bruce Fields 	 * receives an old_stateid error if requests are rarely
636481b82965SJ. Bruce Fields 	 * reordered in flight:
63650836f587SJ. Bruce Fields 	 */
63660836f587SJ. Bruce Fields 	return nfserr_old_stateid;
63670836f587SJ. Bruce Fields }
63680836f587SJ. Bruce Fields 
nfsd4_stid_check_stateid_generation(stateid_t * in,struct nfs4_stid * s,bool has_session)636903da3169STrond Myklebust static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
637003da3169STrond Myklebust {
637103da3169STrond Myklebust 	__be32 ret;
637203da3169STrond Myklebust 
637303da3169STrond Myklebust 	spin_lock(&s->sc_lock);
637403da3169STrond Myklebust 	ret = nfsd4_verify_open_stid(s);
637503da3169STrond Myklebust 	if (ret == nfs_ok)
637603da3169STrond Myklebust 		ret = check_stateid_generation(in, &s->sc_stateid, has_session);
637703da3169STrond Myklebust 	spin_unlock(&s->sc_lock);
637803da3169STrond Myklebust 	return ret;
637903da3169STrond Myklebust }
638003da3169STrond Myklebust 
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)6381ebe9cb3bSChristoph Hellwig static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
6382ebe9cb3bSChristoph Hellwig {
6383ebe9cb3bSChristoph Hellwig 	if (ols->st_stateowner->so_is_open_owner &&
6384ebe9cb3bSChristoph Hellwig 	    !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
6385ebe9cb3bSChristoph Hellwig 		return nfserr_bad_stateid;
6386ebe9cb3bSChristoph Hellwig 	return nfs_ok;
6387ebe9cb3bSChristoph Hellwig }
6388ebe9cb3bSChristoph Hellwig 
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)63897df302f7SChuck Lever static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
639017456804SBryan Schumaker {
639197b7e3b6SJ. Bruce Fields 	struct nfs4_stid *s;
63921af71cc8SJeff Layton 	__be32 status = nfserr_bad_stateid;
639317456804SBryan Schumaker 
6394ae254dacSAndrew Elble 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6395ae254dacSAndrew Elble 		CLOSE_STATEID(stateid))
63961af71cc8SJeff Layton 		return status;
63971af71cc8SJeff Layton 	spin_lock(&cl->cl_lock);
63981af71cc8SJeff Layton 	s = find_stateid_locked(cl, stateid);
639997b7e3b6SJ. Bruce Fields 	if (!s)
64001af71cc8SJeff Layton 		goto out_unlock;
640103da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
640217456804SBryan Schumaker 	if (status)
64031af71cc8SJeff Layton 		goto out_unlock;
640423340032SJ. Bruce Fields 	switch (s->sc_type) {
640523340032SJ. Bruce Fields 	case NFS4_DELEG_STID:
64061af71cc8SJeff Layton 		status = nfs_ok;
64071af71cc8SJeff Layton 		break;
64083bd64a5bSJ. Bruce Fields 	case NFS4_REVOKED_DELEG_STID:
64091af71cc8SJeff Layton 		status = nfserr_deleg_revoked;
64101af71cc8SJeff Layton 		break;
641123340032SJ. Bruce Fields 	case NFS4_OPEN_STID:
641223340032SJ. Bruce Fields 	case NFS4_LOCK_STID:
6413ebe9cb3bSChristoph Hellwig 		status = nfsd4_check_openowner_confirmed(openlockstateid(s));
64141af71cc8SJeff Layton 		break;
641523340032SJ. Bruce Fields 	default:
641623340032SJ. Bruce Fields 		printk("unknown stateid type %x\n", s->sc_type);
6417df561f66SGustavo A. R. Silva 		fallthrough;
641823340032SJ. Bruce Fields 	case NFS4_CLOSED_STID:
6419b0fc29d6STrond Myklebust 	case NFS4_CLOSED_DELEG_STID:
64201af71cc8SJeff Layton 		status = nfserr_bad_stateid;
642123340032SJ. Bruce Fields 	}
64221af71cc8SJeff Layton out_unlock:
64231af71cc8SJeff Layton 	spin_unlock(&cl->cl_lock);
64241af71cc8SJeff Layton 	return status;
642517456804SBryan Schumaker }
642617456804SBryan Schumaker 
6427cd61c522SChristoph Hellwig __be32
nfsd4_lookup_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid,unsigned char typemask,struct nfs4_stid ** s,struct nfsd_net * nn)64282dd6e458STrond Myklebust nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
64292dd6e458STrond Myklebust 		     stateid_t *stateid, unsigned char typemask,
64302dd6e458STrond Myklebust 		     struct nfs4_stid **s, struct nfsd_net *nn)
643138c2f4b1SJ. Bruce Fields {
64320eb6f20aSJ. Bruce Fields 	__be32 status;
64334d01416aSJeff Layton 	struct nfs4_stid *stid;
643495da1b3aSAndrew Elble 	bool return_revoked = false;
643595da1b3aSAndrew Elble 
643695da1b3aSAndrew Elble 	/*
643795da1b3aSAndrew Elble 	 *  only return revoked delegations if explicitly asked.
643895da1b3aSAndrew Elble 	 *  otherwise we report revoked or bad_stateid status.
643995da1b3aSAndrew Elble 	 */
644095da1b3aSAndrew Elble 	if (typemask & NFS4_REVOKED_DELEG_STID)
644195da1b3aSAndrew Elble 		return_revoked = true;
644295da1b3aSAndrew Elble 	else if (typemask & NFS4_DELEG_STID)
644395da1b3aSAndrew Elble 		typemask |= NFS4_REVOKED_DELEG_STID;
644438c2f4b1SJ. Bruce Fields 
6445ae254dacSAndrew Elble 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
6446ae254dacSAndrew Elble 		CLOSE_STATEID(stateid))
644738c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
6448f71475baSJ. Bruce Fields 	status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
6449a8a7c677STrond Myklebust 	if (status == nfserr_stale_clientid) {
64504b24ca7dSJeff Layton 		if (cstate->session)
6451a8a7c677STrond Myklebust 			return nfserr_bad_stateid;
645238c2f4b1SJ. Bruce Fields 		return nfserr_stale_stateid;
6453a8a7c677STrond Myklebust 	}
64540eb6f20aSJ. Bruce Fields 	if (status)
64550eb6f20aSJ. Bruce Fields 		return status;
64564d01416aSJeff Layton 	stid = find_stateid_by_type(cstate->clp, stateid, typemask);
64574d01416aSJeff Layton 	if (!stid)
645838c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
64594d01416aSJeff Layton 	if ((stid->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
64604d01416aSJeff Layton 		nfs4_put_stid(stid);
646195da1b3aSAndrew Elble 		if (cstate->minorversion)
646295da1b3aSAndrew Elble 			return nfserr_deleg_revoked;
646395da1b3aSAndrew Elble 		return nfserr_bad_stateid;
646495da1b3aSAndrew Elble 	}
64654d01416aSJeff Layton 	*s = stid;
646638c2f4b1SJ. Bruce Fields 	return nfs_ok;
646738c2f4b1SJ. Bruce Fields }
646838c2f4b1SJ. Bruce Fields 
6469eb82dd39SJeff Layton static struct nfsd_file *
nfs4_find_file(struct nfs4_stid * s,int flags)6470a0649b2dSChristoph Hellwig nfs4_find_file(struct nfs4_stid *s, int flags)
6471a0649b2dSChristoph Hellwig {
6472bd6aaf78SJeff Layton 	struct nfsd_file *ret = NULL;
6473bd6aaf78SJeff Layton 
6474af90f707SChristoph Hellwig 	if (!s)
6475af90f707SChristoph Hellwig 		return NULL;
6476af90f707SChristoph Hellwig 
6477a0649b2dSChristoph Hellwig 	switch (s->sc_type) {
6478a0649b2dSChristoph Hellwig 	case NFS4_DELEG_STID:
6479bd6aaf78SJeff Layton 		spin_lock(&s->sc_file->fi_lock);
6480bd6aaf78SJeff Layton 		ret = nfsd_file_get(s->sc_file->fi_deleg_file);
6481bd6aaf78SJeff Layton 		spin_unlock(&s->sc_file->fi_lock);
6482bd6aaf78SJeff Layton 		break;
6483a0649b2dSChristoph Hellwig 	case NFS4_OPEN_STID:
6484a0649b2dSChristoph Hellwig 	case NFS4_LOCK_STID:
6485a0649b2dSChristoph Hellwig 		if (flags & RD_STATE)
6486bd6aaf78SJeff Layton 			ret = find_readable_file(s->sc_file);
6487a0649b2dSChristoph Hellwig 		else
6488bd6aaf78SJeff Layton 			ret = find_writeable_file(s->sc_file);
6489a0649b2dSChristoph Hellwig 	}
6490a0649b2dSChristoph Hellwig 
6491bd6aaf78SJeff Layton 	return ret;
6492a0649b2dSChristoph Hellwig }
6493a0649b2dSChristoph Hellwig 
6494a0649b2dSChristoph Hellwig static __be32
nfs4_check_olstateid(struct nfs4_ol_stateid * ols,int flags)6495d8836f77SJ. Bruce Fields nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
6496a0649b2dSChristoph Hellwig {
6497a0649b2dSChristoph Hellwig 	__be32 status;
6498a0649b2dSChristoph Hellwig 
6499a0649b2dSChristoph Hellwig 	status = nfsd4_check_openowner_confirmed(ols);
6500a0649b2dSChristoph Hellwig 	if (status)
6501a0649b2dSChristoph Hellwig 		return status;
6502a0649b2dSChristoph Hellwig 	return nfs4_check_openmode(ols, flags);
6503a0649b2dSChristoph Hellwig }
6504a0649b2dSChristoph Hellwig 
6505af90f707SChristoph Hellwig static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct nfsd_file ** nfp,int flags)6506af90f707SChristoph Hellwig nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
65075c4583b2SJeff Layton 		struct nfsd_file **nfp, int flags)
6508af90f707SChristoph Hellwig {
6509af90f707SChristoph Hellwig 	int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
6510eb82dd39SJeff Layton 	struct nfsd_file *nf;
6511af90f707SChristoph Hellwig 	__be32 status;
6512af90f707SChristoph Hellwig 
6513eb82dd39SJeff Layton 	nf = nfs4_find_file(s, flags);
6514eb82dd39SJeff Layton 	if (nf) {
6515af90f707SChristoph Hellwig 		status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
6516af90f707SChristoph Hellwig 				acc | NFSD_MAY_OWNER_OVERRIDE);
65175c4583b2SJeff Layton 		if (status) {
65185c4583b2SJeff Layton 			nfsd_file_put(nf);
6519eb82dd39SJeff Layton 			goto out;
65205c4583b2SJeff Layton 		}
6521af90f707SChristoph Hellwig 	} else {
6522eb82dd39SJeff Layton 		status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
6523af90f707SChristoph Hellwig 		if (status)
6524af90f707SChristoph Hellwig 			return status;
6525af90f707SChristoph Hellwig 	}
65265c4583b2SJeff Layton 	*nfp = nf;
6527eb82dd39SJeff Layton out:
6528eb82dd39SJeff Layton 	return status;
6529af90f707SChristoph Hellwig }
6530624322f1SOlga Kornievskaia static void
_free_cpntf_state_locked(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)6531624322f1SOlga Kornievskaia _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6532624322f1SOlga Kornievskaia {
6533781fde1aSChuck Lever 	WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
6534781fde1aSChuck Lever 	if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
6535624322f1SOlga Kornievskaia 		return;
6536624322f1SOlga Kornievskaia 	list_del(&cps->cp_list);
6537624322f1SOlga Kornievskaia 	idr_remove(&nn->s2s_cp_stateids,
6538781fde1aSChuck Lever 		   cps->cp_stateid.cs_stid.si_opaque.so_id);
6539624322f1SOlga Kornievskaia 	kfree(cps);
6540624322f1SOlga Kornievskaia }
6541b7342204SOlga Kornievskaia /*
6542b7342204SOlga Kornievskaia  * A READ from an inter server to server COPY will have a
6543b7342204SOlga Kornievskaia  * copy stateid. Look up the copy notify stateid from the
6544b7342204SOlga Kornievskaia  * idr structure and take a reference on it.
6545b7342204SOlga Kornievskaia  */
manage_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_client * clp,struct nfs4_cpntf_state ** cps)6546ce0887acSOlga Kornievskaia __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6547ce0887acSOlga Kornievskaia 			  struct nfs4_client *clp,
6548b7342204SOlga Kornievskaia 			  struct nfs4_cpntf_state **cps)
6549b7342204SOlga Kornievskaia {
6550b7342204SOlga Kornievskaia 	copy_stateid_t *cps_t;
6551b7342204SOlga Kornievskaia 	struct nfs4_cpntf_state *state = NULL;
6552b7342204SOlga Kornievskaia 
6553b7342204SOlga Kornievskaia 	if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
6554b7342204SOlga Kornievskaia 		return nfserr_bad_stateid;
6555b7342204SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
6556b7342204SOlga Kornievskaia 	cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
6557b7342204SOlga Kornievskaia 	if (cps_t) {
6558b7342204SOlga Kornievskaia 		state = container_of(cps_t, struct nfs4_cpntf_state,
6559b7342204SOlga Kornievskaia 				     cp_stateid);
6560781fde1aSChuck Lever 		if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
65615277a79eSDan Carpenter 			state = NULL;
65625277a79eSDan Carpenter 			goto unlock;
65635277a79eSDan Carpenter 		}
6564ce0887acSOlga Kornievskaia 		if (!clp)
6565781fde1aSChuck Lever 			refcount_inc(&state->cp_stateid.cs_count);
6566ce0887acSOlga Kornievskaia 		else
6567ce0887acSOlga Kornievskaia 			_free_cpntf_state_locked(nn, state);
6568b7342204SOlga Kornievskaia 	}
65695277a79eSDan Carpenter unlock:
6570b7342204SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
6571b7342204SOlga Kornievskaia 	if (!state)
6572b7342204SOlga Kornievskaia 		return nfserr_bad_stateid;
6573ce0887acSOlga Kornievskaia 	if (!clp && state)
6574b7342204SOlga Kornievskaia 		*cps = state;
6575b7342204SOlga Kornievskaia 	return 0;
6576b7342204SOlga Kornievskaia }
6577b7342204SOlga Kornievskaia 
find_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_stid ** stid)6578b7342204SOlga Kornievskaia static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
6579b7342204SOlga Kornievskaia 			       struct nfs4_stid **stid)
6580b7342204SOlga Kornievskaia {
6581b7342204SOlga Kornievskaia 	__be32 status;
6582b7342204SOlga Kornievskaia 	struct nfs4_cpntf_state *cps = NULL;
658347fdb22dSJ. Bruce Fields 	struct nfs4_client *found;
6584b7342204SOlga Kornievskaia 
6585ce0887acSOlga Kornievskaia 	status = manage_cpntf_state(nn, st, NULL, &cps);
6586b7342204SOlga Kornievskaia 	if (status)
6587b7342204SOlga Kornievskaia 		return status;
6588b7342204SOlga Kornievskaia 
658920b7d86fSArnd Bergmann 	cps->cpntf_time = ktime_get_boottime_seconds();
659047fdb22dSJ. Bruce Fields 
659147fdb22dSJ. Bruce Fields 	status = nfserr_expired;
659247fdb22dSJ. Bruce Fields 	found = lookup_clientid(&cps->cp_p_clid, true, nn);
659347fdb22dSJ. Bruce Fields 	if (!found)
6594b7342204SOlga Kornievskaia 		goto out;
659547fdb22dSJ. Bruce Fields 
659647fdb22dSJ. Bruce Fields 	*stid = find_stateid_by_type(found, &cps->cp_p_stateid,
659747fdb22dSJ. Bruce Fields 			NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID);
659847fdb22dSJ. Bruce Fields 	if (*stid)
659947fdb22dSJ. Bruce Fields 		status = nfs_ok;
660047fdb22dSJ. Bruce Fields 	else
660147fdb22dSJ. Bruce Fields 		status = nfserr_bad_stateid;
660247fdb22dSJ. Bruce Fields 
660347fdb22dSJ. Bruce Fields 	put_client_renew(found);
6604b7342204SOlga Kornievskaia out:
6605b7342204SOlga Kornievskaia 	nfs4_put_cpntf_state(nn, cps);
6606b7342204SOlga Kornievskaia 	return status;
6607b7342204SOlga Kornievskaia }
6608624322f1SOlga Kornievskaia 
nfs4_put_cpntf_state(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)6609624322f1SOlga Kornievskaia void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
6610624322f1SOlga Kornievskaia {
6611624322f1SOlga Kornievskaia 	spin_lock(&nn->s2s_cp_lock);
6612624322f1SOlga Kornievskaia 	_free_cpntf_state_locked(nn, cps);
6613624322f1SOlga Kornievskaia 	spin_unlock(&nn->s2s_cp_lock);
6614624322f1SOlga Kornievskaia }
6615af90f707SChristoph Hellwig 
6616ee97e730SJeff Layton /**
6617ee97e730SJeff Layton  * nfs4_preprocess_stateid_op - find and prep stateid for an operation
6618ee97e730SJeff Layton  * @rqstp: incoming request from client
6619ee97e730SJeff Layton  * @cstate: current compound state
6620ee97e730SJeff Layton  * @fhp: filehandle associated with requested stateid
6621ee97e730SJeff Layton  * @stateid: stateid (provided by client)
6622ee97e730SJeff Layton  * @flags: flags describing type of operation to be done
6623ee97e730SJeff Layton  * @nfp: optional nfsd_file return pointer (may be NULL)
6624ee97e730SJeff Layton  * @cstid: optional returned nfs4_stid pointer (may be NULL)
6625ee97e730SJeff Layton  *
6626ee97e730SJeff Layton  * Given info from the client, look up a nfs4_stid for the operation. On
6627ee97e730SJeff Layton  * success, it returns a reference to the nfs4_stid and/or the nfsd_file
6628ee97e730SJeff Layton  * associated with it.
66291da177e4SLinus Torvalds  */
6630b37ad28bSAl Viro __be32
nfs4_preprocess_stateid_op(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,struct svc_fh * fhp,stateid_t * stateid,int flags,struct nfsd_file ** nfp,struct nfs4_stid ** cstid)6631af90f707SChristoph Hellwig nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
6632aa0d6aedSAnna Schumaker 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
6633624322f1SOlga Kornievskaia 		stateid_t *stateid, int flags, struct nfsd_file **nfp,
6634624322f1SOlga Kornievskaia 		struct nfs4_stid **cstid)
66351da177e4SLinus Torvalds {
6636af90f707SChristoph Hellwig 	struct net *net = SVC_NET(rqstp);
66373320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6638af90f707SChristoph Hellwig 	struct nfs4_stid *s = NULL;
6639b37ad28bSAl Viro 	__be32 status;
66401da177e4SLinus Torvalds 
66415c4583b2SJeff Layton 	if (nfp)
66425c4583b2SJeff Layton 		*nfp = NULL;
66431da177e4SLinus Torvalds 
6644af90f707SChristoph Hellwig 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
6645074b07d9SJ. Bruce Fields 		if (cstid)
6646074b07d9SJ. Bruce Fields 			status = nfserr_bad_stateid;
6647074b07d9SJ. Bruce Fields 		else
6648074b07d9SJ. Bruce Fields 			status = check_special_stateids(net, fhp, stateid,
6649074b07d9SJ. Bruce Fields 									flags);
6650af90f707SChristoph Hellwig 		goto done;
6651af90f707SChristoph Hellwig 	}
66521da177e4SLinus Torvalds 
66532dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid,
6654db24b3b4SJeff Layton 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
66552dd6e458STrond Myklebust 				&s, nn);
6656b7342204SOlga Kornievskaia 	if (status == nfserr_bad_stateid)
6657b7342204SOlga Kornievskaia 		status = find_cpntf_state(nn, stateid, &s);
665838c2f4b1SJ. Bruce Fields 	if (status)
6659c2d1d6a8STrond Myklebust 		return status;
666003da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, s,
6661a0649b2dSChristoph Hellwig 			nfsd4_has_session(cstate));
66620c2a498fSJ. Bruce Fields 	if (status)
66630c2a498fSJ. Bruce Fields 		goto out;
6664a0649b2dSChristoph Hellwig 
6665f7a4d872SJ. Bruce Fields 	switch (s->sc_type) {
6666f7a4d872SJ. Bruce Fields 	case NFS4_DELEG_STID:
6667a0649b2dSChristoph Hellwig 		status = nfs4_check_delegmode(delegstateid(s), flags);
6668f7a4d872SJ. Bruce Fields 		break;
6669f7a4d872SJ. Bruce Fields 	case NFS4_OPEN_STID:
6670f7a4d872SJ. Bruce Fields 	case NFS4_LOCK_STID:
6671d8836f77SJ. Bruce Fields 		status = nfs4_check_olstateid(openlockstateid(s), flags);
6672f7a4d872SJ. Bruce Fields 		break;
6673f7a4d872SJ. Bruce Fields 	default:
667414bcab1aSTrond Myklebust 		status = nfserr_bad_stateid;
6675a0649b2dSChristoph Hellwig 		break;
66761da177e4SLinus Torvalds 	}
66778fcd461dSJeff Layton 	if (status)
66788fcd461dSJeff Layton 		goto out;
66798fcd461dSJeff Layton 	status = nfs4_check_fh(fhp, s);
6680a0649b2dSChristoph Hellwig 
6681af90f707SChristoph Hellwig done:
66825c4583b2SJeff Layton 	if (status == nfs_ok && nfp)
66835c4583b2SJeff Layton 		status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
66841da177e4SLinus Torvalds out:
6685624322f1SOlga Kornievskaia 	if (s) {
6686624322f1SOlga Kornievskaia 		if (!status && cstid)
6687624322f1SOlga Kornievskaia 			*cstid = s;
6688624322f1SOlga Kornievskaia 		else
6689fd911011STrond Myklebust 			nfs4_put_stid(s);
6690624322f1SOlga Kornievskaia 	}
66911da177e4SLinus Torvalds 	return status;
66921da177e4SLinus Torvalds }
66931da177e4SLinus Torvalds 
6694e1ca12dfSBryan Schumaker /*
669517456804SBryan Schumaker  * Test if the stateid is valid
669617456804SBryan Schumaker  */
669717456804SBryan Schumaker __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)669817456804SBryan Schumaker nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6699eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
670017456804SBryan Schumaker {
6701eb69853dSChristoph Hellwig 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
670203cfb420SBryan Schumaker 	struct nfsd4_test_stateid_id *stateid;
6703ec59659bSJ. Bruce Fields 	struct nfs4_client *cl = cstate->clp;
670403cfb420SBryan Schumaker 
670503cfb420SBryan Schumaker 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
67067df302f7SChuck Lever 		stateid->ts_id_status =
67077df302f7SChuck Lever 			nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
670803cfb420SBryan Schumaker 
670917456804SBryan Schumaker 	return nfs_ok;
671017456804SBryan Schumaker }
671117456804SBryan Schumaker 
671242691398SChuck Lever static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)671342691398SChuck Lever nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
671442691398SChuck Lever {
671542691398SChuck Lever 	struct nfs4_ol_stateid *stp = openlockstateid(s);
671642691398SChuck Lever 	__be32 ret;
671742691398SChuck Lever 
6718659aefb6STrond Myklebust 	ret = nfsd4_lock_ol_stateid(stp);
6719659aefb6STrond Myklebust 	if (ret)
6720659aefb6STrond Myklebust 		goto out_put_stid;
672142691398SChuck Lever 
672242691398SChuck Lever 	ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
672342691398SChuck Lever 	if (ret)
672442691398SChuck Lever 		goto out;
672542691398SChuck Lever 
672642691398SChuck Lever 	ret = nfserr_locks_held;
672742691398SChuck Lever 	if (check_for_locks(stp->st_stid.sc_file,
672842691398SChuck Lever 			    lockowner(stp->st_stateowner)))
672942691398SChuck Lever 		goto out;
673042691398SChuck Lever 
673142691398SChuck Lever 	release_lock_stateid(stp);
673242691398SChuck Lever 	ret = nfs_ok;
673342691398SChuck Lever 
673442691398SChuck Lever out:
673542691398SChuck Lever 	mutex_unlock(&stp->st_mutex);
6736659aefb6STrond Myklebust out_put_stid:
673742691398SChuck Lever 	nfs4_put_stid(s);
673842691398SChuck Lever 	return ret;
673942691398SChuck Lever }
674042691398SChuck Lever 
6741e1ca12dfSBryan Schumaker __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6742e1ca12dfSBryan Schumaker nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6743eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
6744e1ca12dfSBryan Schumaker {
6745eb69853dSChristoph Hellwig 	struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
6746e1ca12dfSBryan Schumaker 	stateid_t *stateid = &free_stateid->fr_stateid;
67472da1cec7SJ. Bruce Fields 	struct nfs4_stid *s;
67483bd64a5bSJ. Bruce Fields 	struct nfs4_delegation *dp;
6749ec59659bSJ. Bruce Fields 	struct nfs4_client *cl = cstate->clp;
67502da1cec7SJ. Bruce Fields 	__be32 ret = nfserr_bad_stateid;
6751e1ca12dfSBryan Schumaker 
67521af71cc8SJeff Layton 	spin_lock(&cl->cl_lock);
67531af71cc8SJeff Layton 	s = find_stateid_locked(cl, stateid);
67542da1cec7SJ. Bruce Fields 	if (!s)
67551af71cc8SJeff Layton 		goto out_unlock;
675603da3169STrond Myklebust 	spin_lock(&s->sc_lock);
67572da1cec7SJ. Bruce Fields 	switch (s->sc_type) {
67582da1cec7SJ. Bruce Fields 	case NFS4_DELEG_STID:
6759e1ca12dfSBryan Schumaker 		ret = nfserr_locks_held;
67601af71cc8SJeff Layton 		break;
67612da1cec7SJ. Bruce Fields 	case NFS4_OPEN_STID:
67621af71cc8SJeff Layton 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
67631af71cc8SJeff Layton 		if (ret)
67641af71cc8SJeff Layton 			break;
67651af71cc8SJeff Layton 		ret = nfserr_locks_held;
67661af71cc8SJeff Layton 		break;
67672da1cec7SJ. Bruce Fields 	case NFS4_LOCK_STID:
676803da3169STrond Myklebust 		spin_unlock(&s->sc_lock);
6769a15dfcd5SElena Reshetova 		refcount_inc(&s->sc_count);
67701af71cc8SJeff Layton 		spin_unlock(&cl->cl_lock);
677142691398SChuck Lever 		ret = nfsd4_free_lock_stateid(stateid, s);
67721af71cc8SJeff Layton 		goto out;
67733bd64a5bSJ. Bruce Fields 	case NFS4_REVOKED_DELEG_STID:
677403da3169STrond Myklebust 		spin_unlock(&s->sc_lock);
67753bd64a5bSJ. Bruce Fields 		dp = delegstateid(s);
67762d4a532dSJeff Layton 		list_del_init(&dp->dl_recall_lru);
67772d4a532dSJeff Layton 		spin_unlock(&cl->cl_lock);
67786011695dSTrond Myklebust 		nfs4_put_stid(s);
67793bd64a5bSJ. Bruce Fields 		ret = nfs_ok;
67801af71cc8SJeff Layton 		goto out;
67811af71cc8SJeff Layton 	/* Default falls through and returns nfserr_bad_stateid */
6782e1ca12dfSBryan Schumaker 	}
678303da3169STrond Myklebust 	spin_unlock(&s->sc_lock);
67841af71cc8SJeff Layton out_unlock:
67851af71cc8SJeff Layton 	spin_unlock(&cl->cl_lock);
6786e1ca12dfSBryan Schumaker out:
6787e1ca12dfSBryan Schumaker 	return ret;
6788e1ca12dfSBryan Schumaker }
6789e1ca12dfSBryan Schumaker 
67904c4cd222SNeilBrown static inline int
setlkflg(int type)67914c4cd222SNeilBrown setlkflg (int type)
67924c4cd222SNeilBrown {
67934c4cd222SNeilBrown 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
67944c4cd222SNeilBrown 		RD_STATE : WR_STATE;
67954c4cd222SNeilBrown }
67961da177e4SLinus Torvalds 
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)6797dcef0413SJ. Bruce Fields static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6798c0a5d93eSJ. Bruce Fields {
6799c0a5d93eSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
6800c0a5d93eSJ. Bruce Fields 	struct nfs4_stateowner *sop = stp->st_stateowner;
6801c0a5d93eSJ. Bruce Fields 	__be32 status;
6802c0a5d93eSJ. Bruce Fields 
6803c0a5d93eSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, sop, seqid);
6804c0a5d93eSJ. Bruce Fields 	if (status)
6805c0a5d93eSJ. Bruce Fields 		return status;
68069271d7e5STrond Myklebust 	status = nfsd4_lock_ol_stateid(stp);
68079271d7e5STrond Myklebust 	if (status != nfs_ok)
68089271d7e5STrond Myklebust 		return status;
6809f7a4d872SJ. Bruce Fields 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
681035a92fe8SJeff Layton 	if (status == nfs_ok)
681135a92fe8SJeff Layton 		status = nfs4_check_fh(current_fh, &stp->st_stid);
681235a92fe8SJeff Layton 	if (status != nfs_ok)
6813feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
6814f7a4d872SJ. Bruce Fields 	return status;
6815c0a5d93eSJ. Bruce Fields }
6816c0a5d93eSJ. Bruce Fields 
6817ee97e730SJeff Layton /**
6818ee97e730SJeff Layton  * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
6819ee97e730SJeff Layton  * @cstate: compund state
6820ee97e730SJeff Layton  * @seqid: seqid (provided by client)
6821ee97e730SJeff Layton  * @stateid: stateid (provided by client)
6822ee97e730SJeff Layton  * @typemask: mask of allowable types for this operation
6823ee97e730SJeff Layton  * @stpp: return pointer for the stateid found
6824ee97e730SJeff Layton  * @nn: net namespace for request
6825ee97e730SJeff Layton  *
6826ee97e730SJeff Layton  * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
6827ee97e730SJeff Layton  * return it in @stpp. On a nfs_ok return, the returned stateid will
6828ee97e730SJeff Layton  * have its st_mutex locked.
68291da177e4SLinus Torvalds  */
6830b37ad28bSAl Viro static __be32
nfs4_preprocess_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,char typemask,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)6831dd453dfdSBenny Halevy nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
68322288d0e3SJ. Bruce Fields 			 stateid_t *stateid, char typemask,
68333320fef1SStanislav Kinsbursky 			 struct nfs4_ol_stateid **stpp,
68343320fef1SStanislav Kinsbursky 			 struct nfsd_net *nn)
68351da177e4SLinus Torvalds {
68360836f587SJ. Bruce Fields 	__be32 status;
683738c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
6838e17f99b7STrond Myklebust 	struct nfs4_ol_stateid *stp = NULL;
68391da177e4SLinus Torvalds 
6840dd5e3fbcSChuck Lever 	trace_nfsd_preprocess(seqid, stateid);
68411da177e4SLinus Torvalds 
68421da177e4SLinus Torvalds 	*stpp = NULL;
68432dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6844c0a5d93eSJ. Bruce Fields 	if (status)
6845c0a5d93eSJ. Bruce Fields 		return status;
6846e17f99b7STrond Myklebust 	stp = openlockstateid(s);
684758fb12e6SJeff Layton 	nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
68481da177e4SLinus Torvalds 
6849e17f99b7STrond Myklebust 	status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6850fd911011STrond Myklebust 	if (!status)
6851e17f99b7STrond Myklebust 		*stpp = stp;
6852fd911011STrond Myklebust 	else
6853fd911011STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
6854e17f99b7STrond Myklebust 	return status;
68551da177e4SLinus Torvalds }
68561da177e4SLinus Torvalds 
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)68573320fef1SStanislav Kinsbursky static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
68583320fef1SStanislav Kinsbursky 						 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6859c0a5d93eSJ. Bruce Fields {
6860c0a5d93eSJ. Bruce Fields 	__be32 status;
6861c0a5d93eSJ. Bruce Fields 	struct nfs4_openowner *oo;
68624cbfc9f7STrond Myklebust 	struct nfs4_ol_stateid *stp;
68631da177e4SLinus Torvalds 
6864c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
68654cbfc9f7STrond Myklebust 						NFS4_OPEN_STID, &stp, nn);
68660836f587SJ. Bruce Fields 	if (status)
68670836f587SJ. Bruce Fields 		return status;
68684cbfc9f7STrond Myklebust 	oo = openowner(stp->st_stateowner);
68694cbfc9f7STrond Myklebust 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6870feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
68714cbfc9f7STrond Myklebust 		nfs4_put_stid(&stp->st_stid);
6872c0a5d93eSJ. Bruce Fields 		return nfserr_bad_stateid;
68734cbfc9f7STrond Myklebust 	}
68744cbfc9f7STrond Myklebust 	*stpp = stp;
68753a4f98bbSNeilBrown 	return nfs_ok;
68761da177e4SLinus Torvalds }
68771da177e4SLinus Torvalds 
6878b37ad28bSAl Viro __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6879ca364317SJ.Bruce Fields nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6880eb69853dSChristoph Hellwig 		   union nfsd4_op_u *u)
68811da177e4SLinus Torvalds {
6882eb69853dSChristoph Hellwig 	struct nfsd4_open_confirm *oc = &u->open_confirm;
6883b37ad28bSAl Viro 	__be32 status;
6884fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
6885dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
68863320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
68871da177e4SLinus Torvalds 
6888a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6889a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
68901da177e4SLinus Torvalds 
6891ca364317SJ.Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6892a8cddc5dSJ. Bruce Fields 	if (status)
6893a8cddc5dSJ. Bruce Fields 		return status;
68941da177e4SLinus Torvalds 
68959072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate,
6896ca364317SJ.Bruce Fields 					oc->oc_seqid, &oc->oc_req_stateid,
68973320fef1SStanislav Kinsbursky 					NFS4_OPEN_STID, &stp, nn);
68989072d5c6SJ. Bruce Fields 	if (status)
68991da177e4SLinus Torvalds 		goto out;
6900fe0750e5SJ. Bruce Fields 	oo = openowner(stp->st_stateowner);
690168b66e82SJ. Bruce Fields 	status = nfserr_bad_stateid;
690235a92fe8SJeff Layton 	if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6903feb9dad5SOleg Drokin 		mutex_unlock(&stp->st_mutex);
69042585fc79STrond Myklebust 		goto put_stateid;
690535a92fe8SJeff Layton 	}
6906dad1c067SJ. Bruce Fields 	oo->oo_flags |= NFS4_OO_CONFIRMED;
69079767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6908feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
6909dd5e3fbcSChuck Lever 	trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
69102a4317c5SJeff Layton 	nfsd4_client_record_create(oo->oo_owner.so_client);
691168b66e82SJ. Bruce Fields 	status = nfs_ok;
69122585fc79STrond Myklebust put_stateid:
69132585fc79STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
69141da177e4SLinus Torvalds out:
69159411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
69161da177e4SLinus Torvalds 	return status;
69171da177e4SLinus Torvalds }
69181da177e4SLinus Torvalds 
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)69196409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
69201da177e4SLinus Torvalds {
692182c5ff1bSJeff Layton 	if (!test_access(access, stp))
69226409a5a6SJ. Bruce Fields 		return;
692311b9164aSTrond Myklebust 	nfs4_file_put_access(stp->st_stid.sc_file, access);
692482c5ff1bSJeff Layton 	clear_access(access, stp);
6925f197c271SJ. Bruce Fields }
69266409a5a6SJ. Bruce Fields 
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)69276409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
69286409a5a6SJ. Bruce Fields {
69296409a5a6SJ. Bruce Fields 	switch (to_access) {
69306409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
69316409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
69326409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
69336409a5a6SJ. Bruce Fields 		break;
69346409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
69356409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
69366409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
69376409a5a6SJ. Bruce Fields 		break;
69386409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
69396409a5a6SJ. Bruce Fields 		break;
69406409a5a6SJ. Bruce Fields 	default:
6941063b0fb9SJ. Bruce Fields 		WARN_ON_ONCE(1);
69421da177e4SLinus Torvalds 	}
69431da177e4SLinus Torvalds }
69441da177e4SLinus Torvalds 
6945b37ad28bSAl Viro __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6946ca364317SJ.Bruce Fields nfsd4_open_downgrade(struct svc_rqst *rqstp,
6947eb69853dSChristoph Hellwig 		     struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
69481da177e4SLinus Torvalds {
6949eb69853dSChristoph Hellwig 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
6950b37ad28bSAl Viro 	__be32 status;
6951dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
69523320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
69531da177e4SLinus Torvalds 
6954a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6955a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
69561da177e4SLinus Torvalds 
6957c30e92dfSJ. Bruce Fields 	/* We don't yet support WANT bits: */
69582c8bd7e0SBenny Halevy 	if (od->od_deleg_want)
69592c8bd7e0SBenny Halevy 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
69602c8bd7e0SBenny Halevy 			od->od_deleg_want);
69611da177e4SLinus Torvalds 
6962c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
69633320fef1SStanislav Kinsbursky 					&od->od_stateid, &stp, nn);
69649072d5c6SJ. Bruce Fields 	if (status)
69651da177e4SLinus Torvalds 		goto out;
69661da177e4SLinus Torvalds 	status = nfserr_inval;
696782c5ff1bSJeff Layton 	if (!test_access(od->od_share_access, stp)) {
6968c11c591fSJeff Layton 		dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
69691da177e4SLinus Torvalds 			stp->st_access_bmap, od->od_share_access);
69700667b1e9STrond Myklebust 		goto put_stateid;
69711da177e4SLinus Torvalds 	}
6972ce0fc43cSJeff Layton 	if (!test_deny(od->od_share_deny, stp)) {
6973c11c591fSJeff Layton 		dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
69741da177e4SLinus Torvalds 			stp->st_deny_bmap, od->od_share_deny);
69750667b1e9STrond Myklebust 		goto put_stateid;
69761da177e4SLinus Torvalds 	}
69776409a5a6SJ. Bruce Fields 	nfs4_stateid_downgrade(stp, od->od_share_access);
6978ce0fc43cSJeff Layton 	reset_union_bmap_deny(od->od_share_deny, stp);
69799767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
69801da177e4SLinus Torvalds 	status = nfs_ok;
69810667b1e9STrond Myklebust put_stateid:
6982feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
69830667b1e9STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
69841da177e4SLinus Torvalds out:
69859411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
69861da177e4SLinus Torvalds 	return status;
69871da177e4SLinus Torvalds }
69881da177e4SLinus Torvalds 
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)6989f7a4d872SJ. Bruce Fields static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6990f7a4d872SJ. Bruce Fields {
6991acf9295bSTrond Myklebust 	struct nfs4_client *clp = s->st_stid.sc_client;
6992e8568739SJeff Layton 	bool unhashed;
6993d83017f9SJeff Layton 	LIST_HEAD(reaplist);
6994019805feSDai Ngo 	struct nfs4_ol_stateid *stp;
6995acf9295bSTrond Myklebust 
69962c41beb0SJeff Layton 	spin_lock(&clp->cl_lock);
6997e8568739SJeff Layton 	unhashed = unhash_open_stateid(s, &reaplist);
6998acf9295bSTrond Myklebust 
6999d83017f9SJeff Layton 	if (clp->cl_minorversion) {
7000e8568739SJeff Layton 		if (unhashed)
7001d83017f9SJeff Layton 			put_ol_stateid_locked(s, &reaplist);
7002d83017f9SJeff Layton 		spin_unlock(&clp->cl_lock);
7003019805feSDai Ngo 		list_for_each_entry(stp, &reaplist, st_locks)
7004019805feSDai Ngo 			nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
7005d83017f9SJeff Layton 		free_ol_stateid_reaplist(&reaplist);
7006d83017f9SJeff Layton 	} else {
7007d83017f9SJeff Layton 		spin_unlock(&clp->cl_lock);
7008d83017f9SJeff Layton 		free_ol_stateid_reaplist(&reaplist);
7009e8568739SJeff Layton 		if (unhashed)
7010d3134b10SJeff Layton 			move_to_close_lru(s, clp->net);
701138c387b5SJ. Bruce Fields 	}
7012d83017f9SJeff Layton }
701338c387b5SJ. Bruce Fields 
70141da177e4SLinus Torvalds /*
70151da177e4SLinus Torvalds  * nfs4_unlock_state() called after encode
70161da177e4SLinus Torvalds  */
7017b37ad28bSAl Viro __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7018ca364317SJ.Bruce Fields nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7019eb69853dSChristoph Hellwig 		union nfsd4_op_u *u)
70201da177e4SLinus Torvalds {
7021eb69853dSChristoph Hellwig 	struct nfsd4_close *close = &u->close;
7022b37ad28bSAl Viro 	__be32 status;
7023dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
70243320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
70253320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
70261da177e4SLinus Torvalds 
7027a6a9f18fSAl Viro 	dprintk("NFSD: nfsd4_close on file %pd\n",
7028a6a9f18fSAl Viro 			cstate->current_fh.fh_dentry);
70291da177e4SLinus Torvalds 
7030f7a4d872SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
7031f7a4d872SJ. Bruce Fields 					&close->cl_stateid,
7032f7a4d872SJ. Bruce Fields 					NFS4_OPEN_STID|NFS4_CLOSED_STID,
70333320fef1SStanislav Kinsbursky 					&stp, nn);
70349411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
70359072d5c6SJ. Bruce Fields 	if (status)
70361da177e4SLinus Torvalds 		goto out;
703715ca08d3STrond Myklebust 
703815ca08d3STrond Myklebust 	stp->st_stid.sc_type = NFS4_CLOSED_STID;
7039bd2decacSJeff Layton 
7040bd2decacSJeff Layton 	/*
7041bd2decacSJeff Layton 	 * Technically we don't _really_ have to increment or copy it, since
7042bd2decacSJeff Layton 	 * it should just be gone after this operation and we clobber the
7043bd2decacSJeff Layton 	 * copied value below, but we continue to do so here just to ensure
7044bd2decacSJeff Layton 	 * that racing ops see that there was a state change.
7045bd2decacSJeff Layton 	 */
70469767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
70471da177e4SLinus Torvalds 
7048f7a4d872SJ. Bruce Fields 	nfsd4_close_open_stateid(stp);
704915ca08d3STrond Myklebust 	mutex_unlock(&stp->st_mutex);
70508a0b589dSTrond Myklebust 
7051bd2decacSJeff Layton 	/* v4.1+ suggests that we send a special stateid in here, since the
7052bd2decacSJeff Layton 	 * clients should just ignore this anyway. Since this is not useful
7053bd2decacSJeff Layton 	 * for v4.0 clients either, we set it to the special close_stateid
7054bd2decacSJeff Layton 	 * universally.
7055bd2decacSJeff Layton 	 *
7056bd2decacSJeff Layton 	 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
7057bd2decacSJeff Layton 	 */
7058bd2decacSJeff Layton 	memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
7059fb500a7cSTrond Myklebust 
70608a0b589dSTrond Myklebust 	/* put reference from nfs4_preprocess_seqid_op */
70618a0b589dSTrond Myklebust 	nfs4_put_stid(&stp->st_stid);
70621da177e4SLinus Torvalds out:
70631da177e4SLinus Torvalds 	return status;
70641da177e4SLinus Torvalds }
70651da177e4SLinus Torvalds 
7066b37ad28bSAl Viro __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7067ca364317SJ.Bruce Fields nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7068eb69853dSChristoph Hellwig 		  union nfsd4_op_u *u)
70691da177e4SLinus Torvalds {
7070eb69853dSChristoph Hellwig 	struct nfsd4_delegreturn *dr = &u->delegreturn;
7071203a8c8eSJ. Bruce Fields 	struct nfs4_delegation *dp;
7072203a8c8eSJ. Bruce Fields 	stateid_t *stateid = &dr->dr_stateid;
707338c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
7074b37ad28bSAl Viro 	__be32 status;
70753320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
70761da177e4SLinus Torvalds 
7077ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
7078203a8c8eSJ. Bruce Fields 		return status;
70791da177e4SLinus Torvalds 
70802dd6e458STrond Myklebust 	status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
708138c2f4b1SJ. Bruce Fields 	if (status)
7082203a8c8eSJ. Bruce Fields 		goto out;
708338c2f4b1SJ. Bruce Fields 	dp = delegstateid(s);
708403da3169STrond Myklebust 	status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
7085203a8c8eSJ. Bruce Fields 	if (status)
7086fd911011STrond Myklebust 		goto put_stateid;
7087203a8c8eSJ. Bruce Fields 
708820eee313SChuck Lever 	trace_nfsd_deleg_return(stateid);
7089c035362eSChuck Lever 	wake_up_var(d_inode(cstate->current_fh.fh_dentry));
70903bd64a5bSJ. Bruce Fields 	destroy_delegation(dp);
7091fd911011STrond Myklebust put_stateid:
7092fd911011STrond Myklebust 	nfs4_put_stid(&dp->dl_stid);
70931da177e4SLinus Torvalds out:
70941da177e4SLinus Torvalds 	return status;
70951da177e4SLinus Torvalds }
70961da177e4SLinus Torvalds 
709787df4de8SBenny Halevy /* last octet in a range */
709887df4de8SBenny Halevy static inline u64
last_byte_offset(u64 start,u64 len)709987df4de8SBenny Halevy last_byte_offset(u64 start, u64 len)
710087df4de8SBenny Halevy {
710187df4de8SBenny Halevy 	u64 end;
710287df4de8SBenny Halevy 
7103063b0fb9SJ. Bruce Fields 	WARN_ON_ONCE(!len);
710487df4de8SBenny Halevy 	end = start + len;
710587df4de8SBenny Halevy 	return end > start ? end - 1: NFS4_MAX_UINT64;
710687df4de8SBenny Halevy }
710787df4de8SBenny Halevy 
71081da177e4SLinus Torvalds /*
71091da177e4SLinus Torvalds  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
71101da177e4SLinus Torvalds  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
71111da177e4SLinus Torvalds  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
71121da177e4SLinus Torvalds  * locking, this prevents us from being completely protocol-compliant.  The
71131da177e4SLinus Torvalds  * real solution to this problem is to start using unsigned file offsets in
71141da177e4SLinus Torvalds  * the VFS, but this is a very deep change!
71151da177e4SLinus Torvalds  */
71161da177e4SLinus Torvalds static inline void
nfs4_transform_lock_offset(struct file_lock * lock)71171da177e4SLinus Torvalds nfs4_transform_lock_offset(struct file_lock *lock)
71181da177e4SLinus Torvalds {
71191da177e4SLinus Torvalds 	if (lock->fl_start < 0)
71201da177e4SLinus Torvalds 		lock->fl_start = OFFSET_MAX;
71211da177e4SLinus Torvalds 	if (lock->fl_end < 0)
71221da177e4SLinus Torvalds 		lock->fl_end = OFFSET_MAX;
71231da177e4SLinus Torvalds }
71241da177e4SLinus Torvalds 
7125cae80b30SJeff Layton static fl_owner_t
nfsd4_lm_get_owner(fl_owner_t owner)712635aff067SChuck Lever nfsd4_lm_get_owner(fl_owner_t owner)
7127aef9583bSKinglong Mee {
7128cae80b30SJeff Layton 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7129cae80b30SJeff Layton 
7130cae80b30SJeff Layton 	nfs4_get_stateowner(&lo->lo_owner);
7131cae80b30SJeff Layton 	return owner;
7132aef9583bSKinglong Mee }
7133aef9583bSKinglong Mee 
7134cae80b30SJeff Layton static void
nfsd4_lm_put_owner(fl_owner_t owner)713535aff067SChuck Lever nfsd4_lm_put_owner(fl_owner_t owner)
7136aef9583bSKinglong Mee {
7137cae80b30SJeff Layton 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
7138aef9583bSKinglong Mee 
7139cae80b30SJeff Layton 	if (lo)
7140aef9583bSKinglong Mee 		nfs4_put_stateowner(&lo->lo_owner);
7141aef9583bSKinglong Mee }
7142aef9583bSKinglong Mee 
714327431affSDai Ngo /* return pointer to struct nfs4_client if client is expirable */
714427431affSDai Ngo static bool
nfsd4_lm_lock_expirable(struct file_lock * cfl)714527431affSDai Ngo nfsd4_lm_lock_expirable(struct file_lock *cfl)
714627431affSDai Ngo {
714727431affSDai Ngo 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)cfl->fl_owner;
714827431affSDai Ngo 	struct nfs4_client *clp = lo->lo_owner.so_client;
714927431affSDai Ngo 	struct nfsd_net *nn;
715027431affSDai Ngo 
715127431affSDai Ngo 	if (try_to_expire_client(clp)) {
715227431affSDai Ngo 		nn = net_generic(clp->net, nfsd_net_id);
715327431affSDai Ngo 		mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
715427431affSDai Ngo 		return true;
715527431affSDai Ngo 	}
715627431affSDai Ngo 	return false;
715727431affSDai Ngo }
715827431affSDai Ngo 
715927431affSDai Ngo /* schedule laundromat to run immediately and wait for it to complete */
716027431affSDai Ngo static void
nfsd4_lm_expire_lock(void)716127431affSDai Ngo nfsd4_lm_expire_lock(void)
716227431affSDai Ngo {
716327431affSDai Ngo 	flush_workqueue(laundry_wq);
716427431affSDai Ngo }
716527431affSDai Ngo 
716676d348faSJeff Layton static void
nfsd4_lm_notify(struct file_lock * fl)716776d348faSJeff Layton nfsd4_lm_notify(struct file_lock *fl)
716876d348faSJeff Layton {
716976d348faSJeff Layton 	struct nfs4_lockowner		*lo = (struct nfs4_lockowner *)fl->fl_owner;
717076d348faSJeff Layton 	struct net			*net = lo->lo_owner.so_client->net;
717176d348faSJeff Layton 	struct nfsd_net			*nn = net_generic(net, nfsd_net_id);
717276d348faSJeff Layton 	struct nfsd4_blocked_lock	*nbl = container_of(fl,
717376d348faSJeff Layton 						struct nfsd4_blocked_lock, nbl_lock);
717476d348faSJeff Layton 	bool queue = false;
717576d348faSJeff Layton 
71767919d0a2SJeff Layton 	/* An empty list means that something else is going to be using it */
71770cc11a61SJeff Layton 	spin_lock(&nn->blocked_locks_lock);
717876d348faSJeff Layton 	if (!list_empty(&nbl->nbl_list)) {
717976d348faSJeff Layton 		list_del_init(&nbl->nbl_list);
71807919d0a2SJeff Layton 		list_del_init(&nbl->nbl_lru);
718176d348faSJeff Layton 		queue = true;
718276d348faSJeff Layton 	}
71830cc11a61SJeff Layton 	spin_unlock(&nn->blocked_locks_lock);
718476d348faSJeff Layton 
71852cde7f81SChuck Lever 	if (queue) {
71862cde7f81SChuck Lever 		trace_nfsd_cb_notify_lock(lo, nbl);
718776d348faSJeff Layton 		nfsd4_run_cb(&nbl->nbl_cb);
718876d348faSJeff Layton 	}
71892cde7f81SChuck Lever }
719076d348faSJeff Layton 
71917b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_posix_mng_ops  = {
719227431affSDai Ngo 	.lm_mod_owner = THIS_MODULE,
719376d348faSJeff Layton 	.lm_notify = nfsd4_lm_notify,
719435aff067SChuck Lever 	.lm_get_owner = nfsd4_lm_get_owner,
719535aff067SChuck Lever 	.lm_put_owner = nfsd4_lm_put_owner,
719627431affSDai Ngo 	.lm_lock_expirable = nfsd4_lm_lock_expirable,
719727431affSDai Ngo 	.lm_expire_lock = nfsd4_lm_expire_lock,
7198d5b9026aSNeilBrown };
71991da177e4SLinus Torvalds 
72001da177e4SLinus Torvalds static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)72011da177e4SLinus Torvalds nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
72021da177e4SLinus Torvalds {
7203fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
72041da177e4SLinus Torvalds 
7205d5b9026aSNeilBrown 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
7206fe0750e5SJ. Bruce Fields 		lo = (struct nfs4_lockowner *) fl->fl_owner;
72076f4859b8SJ. Bruce Fields 		xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
72086f4859b8SJ. Bruce Fields 						GFP_KERNEL);
72097c13f344SJ. Bruce Fields 		if (!deny->ld_owner.data)
72107c13f344SJ. Bruce Fields 			/* We just don't care that much */
72117c13f344SJ. Bruce Fields 			goto nevermind;
7212fe0750e5SJ. Bruce Fields 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
7213d5b9026aSNeilBrown 	} else {
72147c13f344SJ. Bruce Fields nevermind:
72157c13f344SJ. Bruce Fields 		deny->ld_owner.len = 0;
72167c13f344SJ. Bruce Fields 		deny->ld_owner.data = NULL;
7217d5b9026aSNeilBrown 		deny->ld_clientid.cl_boot = 0;
7218d5b9026aSNeilBrown 		deny->ld_clientid.cl_id = 0;
72191da177e4SLinus Torvalds 	}
72201da177e4SLinus Torvalds 	deny->ld_start = fl->fl_start;
722187df4de8SBenny Halevy 	deny->ld_length = NFS4_MAX_UINT64;
722287df4de8SBenny Halevy 	if (fl->fl_end != NFS4_MAX_UINT64)
72231da177e4SLinus Torvalds 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
72241da177e4SLinus Torvalds 	deny->ld_type = NFS4_READ_LT;
72251da177e4SLinus Torvalds 	if (fl->fl_type != F_RDLCK)
72261da177e4SLinus Torvalds 		deny->ld_type = NFS4_WRITE_LT;
72271da177e4SLinus Torvalds }
72281da177e4SLinus Torvalds 
7229fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)7230c8623999SKinglong Mee find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
72311da177e4SLinus Torvalds {
7232d4f0489fSTrond Myklebust 	unsigned int strhashval = ownerstr_hashval(owner);
7233b3c32bcdSTrond Myklebust 	struct nfs4_stateowner *so;
72341da177e4SLinus Torvalds 
72350a880a28STrond Myklebust 	lockdep_assert_held(&clp->cl_lock);
72360a880a28STrond Myklebust 
7237d4f0489fSTrond Myklebust 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
7238d4f0489fSTrond Myklebust 			    so_strhash) {
7239b3c32bcdSTrond Myklebust 		if (so->so_is_open_owner)
7240b3c32bcdSTrond Myklebust 			continue;
7241b5971afaSKinglong Mee 		if (same_owner_str(so, owner))
7242b5971afaSKinglong Mee 			return lockowner(nfs4_get_stateowner(so));
72431da177e4SLinus Torvalds 	}
72441da177e4SLinus Torvalds 	return NULL;
72451da177e4SLinus Torvalds }
72461da177e4SLinus Torvalds 
7247c58c6610STrond Myklebust static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)7248c8623999SKinglong Mee find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
7249c58c6610STrond Myklebust {
7250c58c6610STrond Myklebust 	struct nfs4_lockowner *lo;
7251c58c6610STrond Myklebust 
7252d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
7253c8623999SKinglong Mee 	lo = find_lockowner_str_locked(clp, owner);
7254d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
7255c58c6610STrond Myklebust 	return lo;
7256c58c6610STrond Myklebust }
7257c58c6610STrond Myklebust 
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)72588f4b54c5SJeff Layton static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
72598f4b54c5SJeff Layton {
7260c58c6610STrond Myklebust 	unhash_lockowner_locked(lockowner(sop));
72618f4b54c5SJeff Layton }
72628f4b54c5SJeff Layton 
nfs4_free_lockowner(struct nfs4_stateowner * sop)72636b180f0bSJeff Layton static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
72646b180f0bSJeff Layton {
72656b180f0bSJeff Layton 	struct nfs4_lockowner *lo = lockowner(sop);
72666b180f0bSJeff Layton 
72676b180f0bSJeff Layton 	kmem_cache_free(lockowner_slab, lo);
72686b180f0bSJeff Layton }
72696b180f0bSJeff Layton 
72706b180f0bSJeff Layton static const struct nfs4_stateowner_operations lockowner_ops = {
72718f4b54c5SJeff Layton 	.so_unhash =	nfs4_unhash_lockowner,
72726b180f0bSJeff Layton 	.so_free =	nfs4_free_lockowner,
72736b180f0bSJeff Layton };
72746b180f0bSJeff Layton 
72751da177e4SLinus Torvalds /*
72761da177e4SLinus Torvalds  * Alloc a lock owner structure.
72771da177e4SLinus Torvalds  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
727825985edcSLucas De Marchi  * occurred.
72791da177e4SLinus Torvalds  *
728016bfdaafSJ. Bruce Fields  * strhashval = ownerstr_hashval
72811da177e4SLinus Torvalds  */
7282fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
alloc_init_lock_stateowner(unsigned int strhashval,struct nfs4_client * clp,struct nfs4_ol_stateid * open_stp,struct nfsd4_lock * lock)7283c58c6610STrond Myklebust alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
7284c58c6610STrond Myklebust 			   struct nfs4_ol_stateid *open_stp,
7285c58c6610STrond Myklebust 			   struct nfsd4_lock *lock)
7286c58c6610STrond Myklebust {
7287c58c6610STrond Myklebust 	struct nfs4_lockowner *lo, *ret;
72881da177e4SLinus Torvalds 
7289fe0750e5SJ. Bruce Fields 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
7290fe0750e5SJ. Bruce Fields 	if (!lo)
72911da177e4SLinus Torvalds 		return NULL;
729276d348faSJeff Layton 	INIT_LIST_HEAD(&lo->lo_blocked);
7293fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
7294fe0750e5SJ. Bruce Fields 	lo->lo_owner.so_is_open_owner = 0;
72955db1c03fSJeff Layton 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
72966b180f0bSJeff Layton 	lo->lo_owner.so_ops = &lockowner_ops;
7297d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
7298c8623999SKinglong Mee 	ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
7299c58c6610STrond Myklebust 	if (ret == NULL) {
7300c58c6610STrond Myklebust 		list_add(&lo->lo_owner.so_strhash,
7301d4f0489fSTrond Myklebust 			 &clp->cl_ownerstr_hashtbl[strhashval]);
7302c58c6610STrond Myklebust 		ret = lo;
7303c58c6610STrond Myklebust 	} else
7304d50ffdedSKinglong Mee 		nfs4_free_stateowner(&lo->lo_owner);
7305d50ffdedSKinglong Mee 
7306d4f0489fSTrond Myklebust 	spin_unlock(&clp->cl_lock);
7307340f0ba1SJ. Bruce Fields 	return ret;
73081da177e4SLinus Torvalds }
73091da177e4SLinus Torvalds 
7310fd1fd685STrond Myklebust static struct nfs4_ol_stateid *
find_lock_stateid(const struct nfs4_lockowner * lo,const struct nfs4_ol_stateid * ost)7311a451b123STrond Myklebust find_lock_stateid(const struct nfs4_lockowner *lo,
7312a451b123STrond Myklebust 		  const struct nfs4_ol_stateid *ost)
7313fd1fd685STrond Myklebust {
7314fd1fd685STrond Myklebust 	struct nfs4_ol_stateid *lst;
7315fd1fd685STrond Myklebust 
7316a451b123STrond Myklebust 	lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
7317fd1fd685STrond Myklebust 
7318a451b123STrond Myklebust 	/* If ost is not hashed, ost->st_locks will not be valid */
7319a451b123STrond Myklebust 	if (!nfs4_ol_stateid_unhashed(ost))
7320a451b123STrond Myklebust 		list_for_each_entry(lst, &ost->st_locks, st_locks) {
7321a451b123STrond Myklebust 			if (lst->st_stateowner == &lo->lo_owner) {
7322fd1fd685STrond Myklebust 				refcount_inc(&lst->st_stid.sc_count);
7323fd1fd685STrond Myklebust 				return lst;
7324fd1fd685STrond Myklebust 			}
7325fd1fd685STrond Myklebust 		}
7326fd1fd685STrond Myklebust 	return NULL;
7327fd1fd685STrond Myklebust }
7328fd1fd685STrond Myklebust 
7329beeca19cSTrond Myklebust static struct nfs4_ol_stateid *
init_lock_stateid(struct nfs4_ol_stateid * stp,struct nfs4_lockowner * lo,struct nfs4_file * fp,struct inode * inode,struct nfs4_ol_stateid * open_stp)7330356a95ecSJeff Layton init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
7331356a95ecSJeff Layton 		  struct nfs4_file *fp, struct inode *inode,
7332f9c00c3aSJeff Layton 		  struct nfs4_ol_stateid *open_stp)
73331da177e4SLinus Torvalds {
7334d3b313a4SJ. Bruce Fields 	struct nfs4_client *clp = lo->lo_owner.so_client;
7335beeca19cSTrond Myklebust 	struct nfs4_ol_stateid *retstp;
73361da177e4SLinus Torvalds 
7337beeca19cSTrond Myklebust 	mutex_init(&stp->st_mutex);
73384f34bd05SAndrew Elble 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
7339beeca19cSTrond Myklebust retry:
7340beeca19cSTrond Myklebust 	spin_lock(&clp->cl_lock);
7341a451b123STrond Myklebust 	if (nfs4_ol_stateid_unhashed(open_stp))
7342a451b123STrond Myklebust 		goto out_close;
7343a451b123STrond Myklebust 	retstp = find_lock_stateid(lo, open_stp);
7344beeca19cSTrond Myklebust 	if (retstp)
7345a451b123STrond Myklebust 		goto out_found;
7346a15dfcd5SElena Reshetova 	refcount_inc(&stp->st_stid.sc_count);
73473abdb607SJ. Bruce Fields 	stp->st_stid.sc_type = NFS4_LOCK_STID;
7348b5971afaSKinglong Mee 	stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
734913cd2184SNeilBrown 	get_nfs4_file(fp);
735011b9164aSTrond Myklebust 	stp->st_stid.sc_file = fp;
73510997b173SJ. Bruce Fields 	stp->st_access_bmap = 0;
73521da177e4SLinus Torvalds 	stp->st_deny_bmap = open_stp->st_deny_bmap;
73534c4cd222SNeilBrown 	stp->st_openstp = open_stp;
7354a451b123STrond Myklebust 	spin_lock(&fp->fi_lock);
73553c87b9b7STrond Myklebust 	list_add(&stp->st_locks, &open_stp->st_locks);
73561c755dc1SJeff Layton 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
73571d31a253STrond Myklebust 	list_add(&stp->st_perfile, &fp->fi_stateids);
73581d31a253STrond Myklebust 	spin_unlock(&fp->fi_lock);
7359beeca19cSTrond Myklebust 	spin_unlock(&clp->cl_lock);
7360a451b123STrond Myklebust 	return stp;
7361a451b123STrond Myklebust out_found:
7362a451b123STrond Myklebust 	spin_unlock(&clp->cl_lock);
7363beeca19cSTrond Myklebust 	if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
7364beeca19cSTrond Myklebust 		nfs4_put_stid(&retstp->st_stid);
7365beeca19cSTrond Myklebust 		goto retry;
7366beeca19cSTrond Myklebust 	}
7367beeca19cSTrond Myklebust 	/* To keep mutex tracking happy */
7368beeca19cSTrond Myklebust 	mutex_unlock(&stp->st_mutex);
7369a451b123STrond Myklebust 	return retstp;
7370a451b123STrond Myklebust out_close:
7371a451b123STrond Myklebust 	spin_unlock(&clp->cl_lock);
7372a451b123STrond Myklebust 	mutex_unlock(&stp->st_mutex);
7373a451b123STrond Myklebust 	return NULL;
73741da177e4SLinus Torvalds }
73751da177e4SLinus Torvalds 
7376c53530daSJeff Layton static struct nfs4_ol_stateid *
find_or_create_lock_stateid(struct nfs4_lockowner * lo,struct nfs4_file * fi,struct inode * inode,struct nfs4_ol_stateid * ost,bool * new)7377356a95ecSJeff Layton find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
7378356a95ecSJeff Layton 			    struct inode *inode, struct nfs4_ol_stateid *ost,
7379356a95ecSJeff Layton 			    bool *new)
7380356a95ecSJeff Layton {
7381356a95ecSJeff Layton 	struct nfs4_stid *ns = NULL;
7382356a95ecSJeff Layton 	struct nfs4_ol_stateid *lst;
7383356a95ecSJeff Layton 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
7384356a95ecSJeff Layton 	struct nfs4_client *clp = oo->oo_owner.so_client;
7385356a95ecSJeff Layton 
7386beeca19cSTrond Myklebust 	*new = false;
7387356a95ecSJeff Layton 	spin_lock(&clp->cl_lock);
7388a451b123STrond Myklebust 	lst = find_lock_stateid(lo, ost);
7389356a95ecSJeff Layton 	spin_unlock(&clp->cl_lock);
7390beeca19cSTrond Myklebust 	if (lst != NULL) {
7391beeca19cSTrond Myklebust 		if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
7392beeca19cSTrond Myklebust 			goto out;
7393beeca19cSTrond Myklebust 		nfs4_put_stid(&lst->st_stid);
7394beeca19cSTrond Myklebust 	}
7395d19fb70dSKinglong Mee 	ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
7396356a95ecSJeff Layton 	if (ns == NULL)
7397356a95ecSJeff Layton 		return NULL;
7398356a95ecSJeff Layton 
7399beeca19cSTrond Myklebust 	lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
7400beeca19cSTrond Myklebust 	if (lst == openlockstateid(ns))
7401356a95ecSJeff Layton 		*new = true;
7402beeca19cSTrond Myklebust 	else
7403356a95ecSJeff Layton 		nfs4_put_stid(ns);
7404beeca19cSTrond Myklebust out:
7405356a95ecSJeff Layton 	return lst;
7406356a95ecSJeff Layton }
7407c53530daSJeff Layton 
7408fd39ca9aSNeilBrown static int
check_lock_length(u64 offset,u64 length)74091da177e4SLinus Torvalds check_lock_length(u64 offset, u64 length)
74101da177e4SLinus Torvalds {
741187df4de8SBenny Halevy 	return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
7412e7969315SKinglong Mee 		(length > ~offset)));
74131da177e4SLinus Torvalds }
74141da177e4SLinus Torvalds 
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)7415dcef0413SJ. Bruce Fields static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
74160997b173SJ. Bruce Fields {
741711b9164aSTrond Myklebust 	struct nfs4_file *fp = lock_stp->st_stid.sc_file;
74180997b173SJ. Bruce Fields 
74197214e860SJeff Layton 	lockdep_assert_held(&fp->fi_lock);
74207214e860SJeff Layton 
742182c5ff1bSJeff Layton 	if (test_access(access, lock_stp))
74220997b173SJ. Bruce Fields 		return;
742312659651SJeff Layton 	__nfs4_file_get_access(fp, access);
742482c5ff1bSJeff Layton 	set_access(access, lock_stp);
74250997b173SJ. Bruce Fields }
74260997b173SJ. Bruce Fields 
7427356a95ecSJeff Layton static __be32
lookup_or_create_lock_state(struct nfsd4_compound_state * cstate,struct nfs4_ol_stateid * ost,struct nfsd4_lock * lock,struct nfs4_ol_stateid ** plst,bool * new)7428356a95ecSJeff Layton lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
7429356a95ecSJeff Layton 			    struct nfs4_ol_stateid *ost,
7430356a95ecSJeff Layton 			    struct nfsd4_lock *lock,
7431dd257933SJeff Layton 			    struct nfs4_ol_stateid **plst, bool *new)
743264a284d0SJ. Bruce Fields {
74335db1c03fSJeff Layton 	__be32 status;
743411b9164aSTrond Myklebust 	struct nfs4_file *fi = ost->st_stid.sc_file;
743564a284d0SJ. Bruce Fields 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
743664a284d0SJ. Bruce Fields 	struct nfs4_client *cl = oo->oo_owner.so_client;
74372b0143b5SDavid Howells 	struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
743864a284d0SJ. Bruce Fields 	struct nfs4_lockowner *lo;
7439dd257933SJeff Layton 	struct nfs4_ol_stateid *lst;
744064a284d0SJ. Bruce Fields 	unsigned int strhashval;
744164a284d0SJ. Bruce Fields 
7442c8623999SKinglong Mee 	lo = find_lockowner_str(cl, &lock->lk_new_owner);
7443c53530daSJeff Layton 	if (!lo) {
744476f6c9e1SKinglong Mee 		strhashval = ownerstr_hashval(&lock->lk_new_owner);
744564a284d0SJ. Bruce Fields 		lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
744664a284d0SJ. Bruce Fields 		if (lo == NULL)
744764a284d0SJ. Bruce Fields 			return nfserr_jukebox;
7448c53530daSJeff Layton 	} else {
7449c53530daSJeff Layton 		/* with an existing lockowner, seqids must be the same */
74505db1c03fSJeff Layton 		status = nfserr_bad_seqid;
7451c53530daSJeff Layton 		if (!cstate->minorversion &&
7452c53530daSJeff Layton 		    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
74535db1c03fSJeff Layton 			goto out;
7454c53530daSJeff Layton 	}
7455c53530daSJeff Layton 
7456dd257933SJeff Layton 	lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
7457dd257933SJeff Layton 	if (lst == NULL) {
74585db1c03fSJeff Layton 		status = nfserr_jukebox;
74595db1c03fSJeff Layton 		goto out;
746064a284d0SJ. Bruce Fields 	}
7461dd257933SJeff Layton 
74625db1c03fSJeff Layton 	status = nfs_ok;
7463dd257933SJeff Layton 	*plst = lst;
74645db1c03fSJeff Layton out:
74655db1c03fSJeff Layton 	nfs4_put_stateowner(&lo->lo_owner);
74665db1c03fSJeff Layton 	return status;
746764a284d0SJ. Bruce Fields }
746864a284d0SJ. Bruce Fields 
74691da177e4SLinus Torvalds /*
74701da177e4SLinus Torvalds  *  LOCK operation
74711da177e4SLinus Torvalds  */
7472b37ad28bSAl Viro __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7473ca364317SJ.Bruce Fields nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7474eb69853dSChristoph Hellwig 	   union nfsd4_op_u *u)
74751da177e4SLinus Torvalds {
7476eb69853dSChristoph Hellwig 	struct nfsd4_lock *lock = &u->lock;
7477fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *open_sop = NULL;
7478fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lock_sop = NULL;
74793d0fabd5STrond Myklebust 	struct nfs4_ol_stateid *lock_stp = NULL;
74800667b1e9STrond Myklebust 	struct nfs4_ol_stateid *open_stp = NULL;
74817214e860SJeff Layton 	struct nfs4_file *fp;
7482eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
748376d348faSJeff Layton 	struct nfsd4_blocked_lock *nbl = NULL;
748421179d81SJeff Layton 	struct file_lock *file_lock = NULL;
748521179d81SJeff Layton 	struct file_lock *conflock = NULL;
7486b37ad28bSAl Viro 	__be32 status = 0;
7487b34f27aaSJ. Bruce Fields 	int lkflg;
7488b8dd7b9aSAl Viro 	int err;
74895db1c03fSJeff Layton 	bool new = false;
749076d348faSJeff Layton 	unsigned char fl_type;
749176d348faSJeff Layton 	unsigned int fl_flags = FL_POSIX;
74923320fef1SStanislav Kinsbursky 	struct net *net = SVC_NET(rqstp);
74933320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
74941da177e4SLinus Torvalds 
74951da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
74961da177e4SLinus Torvalds 		(long long) lock->lk_offset,
74971da177e4SLinus Torvalds 		(long long) lock->lk_length);
74981da177e4SLinus Torvalds 
74991da177e4SLinus Torvalds 	if (check_lock_length(lock->lk_offset, lock->lk_length))
75001da177e4SLinus Torvalds 		 return nfserr_inval;
75011da177e4SLinus Torvalds 
7502ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh,
75038837abcaSMiklos Szeredi 				S_IFREG, NFSD_MAY_LOCK))) {
7504a6f6ef2fSAndy Adamson 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
7505a6f6ef2fSAndy Adamson 		return status;
7506a6f6ef2fSAndy Adamson 	}
7507a6f6ef2fSAndy Adamson 
75081da177e4SLinus Torvalds 	if (lock->lk_is_new) {
7509684e5638SJ. Bruce Fields 		if (nfsd4_has_session(cstate))
7510684e5638SJ. Bruce Fields 			/* See rfc 5661 18.10.3: given clientid is ignored: */
751176f6c9e1SKinglong Mee 			memcpy(&lock->lk_new_clientid,
7512ec59659bSJ. Bruce Fields 				&cstate->clp->cl_clientid,
7513684e5638SJ. Bruce Fields 				sizeof(clientid_t));
7514684e5638SJ. Bruce Fields 
75151da177e4SLinus Torvalds 		/* validate and update open stateid and open seqid */
7516c0a5d93eSJ. Bruce Fields 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
75171da177e4SLinus Torvalds 				        lock->lk_new_open_seqid,
75181da177e4SLinus Torvalds 		                        &lock->lk_new_open_stateid,
75193320fef1SStanislav Kinsbursky 					&open_stp, nn);
752037515177SNeilBrown 		if (status)
75211da177e4SLinus Torvalds 			goto out;
7522feb9dad5SOleg Drokin 		mutex_unlock(&open_stp->st_mutex);
7523fe0750e5SJ. Bruce Fields 		open_sop = openowner(open_stp->st_stateowner);
7524b34f27aaSJ. Bruce Fields 		status = nfserr_bad_stateid;
7525684e5638SJ. Bruce Fields 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
752676f6c9e1SKinglong Mee 						&lock->lk_new_clientid))
7527b34f27aaSJ. Bruce Fields 			goto out;
752864a284d0SJ. Bruce Fields 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
75295db1c03fSJeff Layton 							&lock_stp, &new);
75303d0fabd5STrond Myklebust 	} else {
7531dd453dfdSBenny Halevy 		status = nfs4_preprocess_seqid_op(cstate,
75321da177e4SLinus Torvalds 				       lock->lk_old_lock_seqid,
75331da177e4SLinus Torvalds 				       &lock->lk_old_lock_stateid,
75343320fef1SStanislav Kinsbursky 				       NFS4_LOCK_STID, &lock_stp, nn);
75353d0fabd5STrond Myklebust 	}
75361da177e4SLinus Torvalds 	if (status)
75371da177e4SLinus Torvalds 		goto out;
7538fe0750e5SJ. Bruce Fields 	lock_sop = lockowner(lock_stp->st_stateowner);
75391da177e4SLinus Torvalds 
7540b34f27aaSJ. Bruce Fields 	lkflg = setlkflg(lock->lk_type);
7541b34f27aaSJ. Bruce Fields 	status = nfs4_check_openmode(lock_stp, lkflg);
7542b34f27aaSJ. Bruce Fields 	if (status)
7543b34f27aaSJ. Bruce Fields 		goto out;
7544b34f27aaSJ. Bruce Fields 
75450dd395dcSNeilBrown 	status = nfserr_grace;
75463320fef1SStanislav Kinsbursky 	if (locks_in_grace(net) && !lock->lk_reclaim)
75470dd395dcSNeilBrown 		goto out;
75480dd395dcSNeilBrown 	status = nfserr_no_grace;
75493320fef1SStanislav Kinsbursky 	if (!locks_in_grace(net) && lock->lk_reclaim)
75500dd395dcSNeilBrown 		goto out;
75510dd395dcSNeilBrown 
7552bb0a55bbSJ. Bruce Fields 	if (lock->lk_reclaim)
7553bb0a55bbSJ. Bruce Fields 		fl_flags |= FL_RECLAIM;
7554bb0a55bbSJ. Bruce Fields 
755511b9164aSTrond Myklebust 	fp = lock_stp->st_stid.sc_file;
75561da177e4SLinus Torvalds 	switch (lock->lk_type) {
75571da177e4SLinus Torvalds 		case NFS4_READW_LT:
755840595cdcSJ. Bruce Fields 			if (nfsd4_has_session(cstate))
755976d348faSJeff Layton 				fl_flags |= FL_SLEEP;
7560df561f66SGustavo A. R. Silva 			fallthrough;
756176d348faSJeff Layton 		case NFS4_READ_LT:
75627214e860SJeff Layton 			spin_lock(&fp->fi_lock);
7563eb82dd39SJeff Layton 			nf = find_readable_file_locked(fp);
7564eb82dd39SJeff Layton 			if (nf)
75650997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
75667214e860SJeff Layton 			spin_unlock(&fp->fi_lock);
756776d348faSJeff Layton 			fl_type = F_RDLCK;
75681da177e4SLinus Torvalds 			break;
75691da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
757040595cdcSJ. Bruce Fields 			if (nfsd4_has_session(cstate))
757176d348faSJeff Layton 				fl_flags |= FL_SLEEP;
7572df561f66SGustavo A. R. Silva 			fallthrough;
757376d348faSJeff Layton 		case NFS4_WRITE_LT:
75747214e860SJeff Layton 			spin_lock(&fp->fi_lock);
7575eb82dd39SJeff Layton 			nf = find_writeable_file_locked(fp);
7576eb82dd39SJeff Layton 			if (nf)
75770997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
75787214e860SJeff Layton 			spin_unlock(&fp->fi_lock);
757976d348faSJeff Layton 			fl_type = F_WRLCK;
75801da177e4SLinus Torvalds 			break;
75811da177e4SLinus Torvalds 		default:
75821da177e4SLinus Torvalds 			status = nfserr_inval;
75831da177e4SLinus Torvalds 		goto out;
75841da177e4SLinus Torvalds 	}
758576d348faSJeff Layton 
7586eb82dd39SJeff Layton 	if (!nf) {
7587f9d7562fSJ. Bruce Fields 		status = nfserr_openmode;
7588f9d7562fSJ. Bruce Fields 		goto out;
7589f9d7562fSJ. Bruce Fields 	}
7590aef9583bSKinglong Mee 
759140595cdcSJ. Bruce Fields 	/*
759240595cdcSJ. Bruce Fields 	 * Most filesystems with their own ->lock operations will block
759340595cdcSJ. Bruce Fields 	 * the nfsd thread waiting to acquire the lock.  That leads to
759440595cdcSJ. Bruce Fields 	 * deadlocks (we don't want every nfsd thread tied up waiting
759540595cdcSJ. Bruce Fields 	 * for file locks), so don't attempt blocking lock notifications
759640595cdcSJ. Bruce Fields 	 * on those filesystems:
759740595cdcSJ. Bruce Fields 	 */
759840595cdcSJ. Bruce Fields 	if (nf->nf_file->f_op->lock)
759940595cdcSJ. Bruce Fields 		fl_flags &= ~FL_SLEEP;
760040595cdcSJ. Bruce Fields 
760176d348faSJeff Layton 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
760276d348faSJeff Layton 	if (!nbl) {
760376d348faSJeff Layton 		dprintk("NFSD: %s: unable to allocate block!\n", __func__);
760476d348faSJeff Layton 		status = nfserr_jukebox;
760576d348faSJeff Layton 		goto out;
760676d348faSJeff Layton 	}
760776d348faSJeff Layton 
760876d348faSJeff Layton 	file_lock = &nbl->nbl_lock;
760976d348faSJeff Layton 	file_lock->fl_type = fl_type;
7610aef9583bSKinglong Mee 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
761121179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
7612eb82dd39SJeff Layton 	file_lock->fl_file = nf->nf_file;
761376d348faSJeff Layton 	file_lock->fl_flags = fl_flags;
761421179d81SJeff Layton 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
761521179d81SJeff Layton 	file_lock->fl_start = lock->lk_offset;
761621179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
761721179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
76181da177e4SLinus Torvalds 
761921179d81SJeff Layton 	conflock = locks_alloc_lock();
762021179d81SJeff Layton 	if (!conflock) {
762121179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
762221179d81SJeff Layton 		status = nfserr_jukebox;
762321179d81SJeff Layton 		goto out;
762421179d81SJeff Layton 	}
76251da177e4SLinus Torvalds 
762676d348faSJeff Layton 	if (fl_flags & FL_SLEEP) {
762720b7d86fSArnd Bergmann 		nbl->nbl_time = ktime_get_boottime_seconds();
76280cc11a61SJeff Layton 		spin_lock(&nn->blocked_locks_lock);
762976d348faSJeff Layton 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
76307919d0a2SJeff Layton 		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
763147446d74SVasily Averin 		kref_get(&nbl->nbl_kref);
76320cc11a61SJeff Layton 		spin_unlock(&nn->blocked_locks_lock);
763376d348faSJeff Layton 	}
763476d348faSJeff Layton 
7635eb82dd39SJeff Layton 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
763676d348faSJeff Layton 	switch (err) {
76371da177e4SLinus Torvalds 	case 0: /* success! */
76389767feb2SJeff Layton 		nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
7639b8dd7b9aSAl Viro 		status = 0;
764003f318caSJ. Bruce Fields 		if (lock->lk_reclaim)
764103f318caSJ. Bruce Fields 			nn->somebody_reclaimed = true;
7642eb76b3fdSAndy Adamson 		break;
764376d348faSJeff Layton 	case FILE_LOCK_DEFERRED:
764447446d74SVasily Averin 		kref_put(&nbl->nbl_kref, free_nbl);
764576d348faSJeff Layton 		nbl = NULL;
7646df561f66SGustavo A. R. Silva 		fallthrough;
764776d348faSJeff Layton 	case -EAGAIN:		/* conflock holds conflicting lock */
7648eb76b3fdSAndy Adamson 		status = nfserr_denied;
7649eb76b3fdSAndy Adamson 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
765021179d81SJeff Layton 		nfs4_set_lock_denied(conflock, &lock->lk_denied);
7651eb76b3fdSAndy Adamson 		break;
765276d348faSJeff Layton 	case -EDEADLK:
76531da177e4SLinus Torvalds 		status = nfserr_deadlock;
7654eb76b3fdSAndy Adamson 		break;
76551da177e4SLinus Torvalds 	default:
7656fd85b817SMarc Eshel 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
76573e772463SJ. Bruce Fields 		status = nfserrno(err);
7658eb76b3fdSAndy Adamson 		break;
76591da177e4SLinus Torvalds 	}
76601da177e4SLinus Torvalds out:
766176d348faSJeff Layton 	if (nbl) {
766276d348faSJeff Layton 		/* dequeue it if we queued it before */
766376d348faSJeff Layton 		if (fl_flags & FL_SLEEP) {
76640cc11a61SJeff Layton 			spin_lock(&nn->blocked_locks_lock);
766547446d74SVasily Averin 			if (!list_empty(&nbl->nbl_list) &&
766647446d74SVasily Averin 			    !list_empty(&nbl->nbl_lru)) {
766776d348faSJeff Layton 				list_del_init(&nbl->nbl_list);
76687919d0a2SJeff Layton 				list_del_init(&nbl->nbl_lru);
766947446d74SVasily Averin 				kref_put(&nbl->nbl_kref, free_nbl);
767047446d74SVasily Averin 			}
767147446d74SVasily Averin 			/* nbl can use one of lists to be linked to reaplist */
76720cc11a61SJeff Layton 			spin_unlock(&nn->blocked_locks_lock);
767376d348faSJeff Layton 		}
767476d348faSJeff Layton 		free_blocked_lock(nbl);
767576d348faSJeff Layton 	}
7676eb82dd39SJeff Layton 	if (nf)
7677eb82dd39SJeff Layton 		nfsd_file_put(nf);
76785db1c03fSJeff Layton 	if (lock_stp) {
76795db1c03fSJeff Layton 		/* Bump seqid manually if the 4.0 replay owner is openowner */
76805db1c03fSJeff Layton 		if (cstate->replay_owner &&
76815db1c03fSJeff Layton 		    cstate->replay_owner != &lock_sop->lo_owner &&
76825db1c03fSJeff Layton 		    seqid_mutating_err(ntohl(status)))
76835db1c03fSJeff Layton 			lock_sop->lo_owner.so_seqid++;
76845db1c03fSJeff Layton 
76855db1c03fSJeff Layton 		/*
76865db1c03fSJeff Layton 		 * If this is a new, never-before-used stateid, and we are
76875db1c03fSJeff Layton 		 * returning an error, then just go ahead and release it.
76885db1c03fSJeff Layton 		 */
768925020720SJ. Bruce Fields 		if (status && new)
76905db1c03fSJeff Layton 			release_lock_stateid(lock_stp);
7691beeca19cSTrond Myklebust 
7692beeca19cSTrond Myklebust 		mutex_unlock(&lock_stp->st_mutex);
76935db1c03fSJeff Layton 
76943d0fabd5STrond Myklebust 		nfs4_put_stid(&lock_stp->st_stid);
76955db1c03fSJeff Layton 	}
76960667b1e9STrond Myklebust 	if (open_stp)
76970667b1e9STrond Myklebust 		nfs4_put_stid(&open_stp->st_stid);
76989411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
769921179d81SJeff Layton 	if (conflock)
770021179d81SJeff Layton 		locks_free_lock(conflock);
77011da177e4SLinus Torvalds 	return status;
77021da177e4SLinus Torvalds }
77031da177e4SLinus Torvalds 
77041da177e4SLinus Torvalds /*
770555ef1274SJ. Bruce Fields  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
770655ef1274SJ. Bruce Fields  * so we do a temporary open here just to get an open file to pass to
77070bcc7ca4SJ. Bruce Fields  * vfs_test_lock.
770855ef1274SJ. Bruce Fields  */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)770904da6e9dSAl Viro static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
771055ef1274SJ. Bruce Fields {
77116b556ca2SJeff Layton 	struct nfsd_file *nf;
7712bb4d53d6SNeilBrown 	struct inode *inode;
7713217fd6f6SJ. Bruce Fields 	__be32 err;
7714217fd6f6SJ. Bruce Fields 
7715217fd6f6SJ. Bruce Fields 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
7716217fd6f6SJ. Bruce Fields 	if (err)
7717217fd6f6SJ. Bruce Fields 		return err;
7718bb4d53d6SNeilBrown 	inode = fhp->fh_dentry->d_inode;
7719bb4d53d6SNeilBrown 	inode_lock(inode); /* to block new leases till after test_lock: */
7720bb4d53d6SNeilBrown 	err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
7721217fd6f6SJ. Bruce Fields 	if (err)
7722217fd6f6SJ. Bruce Fields 		goto out;
77230bcc7ca4SJ. Bruce Fields 	lock->fl_file = nf->nf_file;
77246b556ca2SJeff Layton 	err = nfserrno(vfs_test_lock(nf->nf_file, lock));
77250bcc7ca4SJ. Bruce Fields 	lock->fl_file = NULL;
7726217fd6f6SJ. Bruce Fields out:
7727bb4d53d6SNeilBrown 	inode_unlock(inode);
77286b556ca2SJeff Layton 	nfsd_file_put(nf);
772955ef1274SJ. Bruce Fields 	return err;
773055ef1274SJ. Bruce Fields }
773155ef1274SJ. Bruce Fields 
773255ef1274SJ. Bruce Fields /*
77331da177e4SLinus Torvalds  * LOCKT operation
77341da177e4SLinus Torvalds  */
7735b37ad28bSAl Viro __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7736ca364317SJ.Bruce Fields nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7737eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
77381da177e4SLinus Torvalds {
7739eb69853dSChristoph Hellwig 	struct nfsd4_lockt *lockt = &u->lockt;
774021179d81SJeff Layton 	struct file_lock *file_lock = NULL;
77415db1c03fSJeff Layton 	struct nfs4_lockowner *lo = NULL;
7742b37ad28bSAl Viro 	__be32 status;
77437f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
77441da177e4SLinus Torvalds 
77455ccb0066SStanislav Kinsbursky 	if (locks_in_grace(SVC_NET(rqstp)))
77461da177e4SLinus Torvalds 		return nfserr_grace;
77471da177e4SLinus Torvalds 
77481da177e4SLinus Torvalds 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
77491da177e4SLinus Torvalds 		 return nfserr_inval;
77501da177e4SLinus Torvalds 
77519b2ef62bSJ. Bruce Fields 	if (!nfsd4_has_session(cstate)) {
7752f71475baSJ. Bruce Fields 		status = set_client(&lockt->lt_clientid, cstate, nn);
77539b2ef62bSJ. Bruce Fields 		if (status)
77541da177e4SLinus Torvalds 			goto out;
77559b2ef62bSJ. Bruce Fields 	}
77561da177e4SLinus Torvalds 
775775c096f7SJ. Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
77581da177e4SLinus Torvalds 		goto out;
77591da177e4SLinus Torvalds 
776021179d81SJeff Layton 	file_lock = locks_alloc_lock();
776121179d81SJeff Layton 	if (!file_lock) {
776221179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
776321179d81SJeff Layton 		status = nfserr_jukebox;
776421179d81SJeff Layton 		goto out;
776521179d81SJeff Layton 	}
77666cd90662SKinglong Mee 
77671da177e4SLinus Torvalds 	switch (lockt->lt_type) {
77681da177e4SLinus Torvalds 		case NFS4_READ_LT:
77691da177e4SLinus Torvalds 		case NFS4_READW_LT:
777021179d81SJeff Layton 			file_lock->fl_type = F_RDLCK;
77711da177e4SLinus Torvalds 			break;
77721da177e4SLinus Torvalds 		case NFS4_WRITE_LT:
77731da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
777421179d81SJeff Layton 			file_lock->fl_type = F_WRLCK;
77751da177e4SLinus Torvalds 			break;
77761da177e4SLinus Torvalds 		default:
77772fdada03SJ. Bruce Fields 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
77781da177e4SLinus Torvalds 			status = nfserr_inval;
77791da177e4SLinus Torvalds 			goto out;
77801da177e4SLinus Torvalds 	}
77811da177e4SLinus Torvalds 
7782c8623999SKinglong Mee 	lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
7783fe0750e5SJ. Bruce Fields 	if (lo)
778421179d81SJeff Layton 		file_lock->fl_owner = (fl_owner_t)lo;
778521179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
778621179d81SJeff Layton 	file_lock->fl_flags = FL_POSIX;
77871da177e4SLinus Torvalds 
778821179d81SJeff Layton 	file_lock->fl_start = lockt->lt_offset;
778921179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
77901da177e4SLinus Torvalds 
779121179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
77921da177e4SLinus Torvalds 
779321179d81SJeff Layton 	status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
779404da6e9dSAl Viro 	if (status)
7795fd85b817SMarc Eshel 		goto out;
779604da6e9dSAl Viro 
779721179d81SJeff Layton 	if (file_lock->fl_type != F_UNLCK) {
77981da177e4SLinus Torvalds 		status = nfserr_denied;
779921179d81SJeff Layton 		nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
78001da177e4SLinus Torvalds 	}
78011da177e4SLinus Torvalds out:
78025db1c03fSJeff Layton 	if (lo)
78035db1c03fSJeff Layton 		nfs4_put_stateowner(&lo->lo_owner);
780421179d81SJeff Layton 	if (file_lock)
780521179d81SJeff Layton 		locks_free_lock(file_lock);
78061da177e4SLinus Torvalds 	return status;
78071da177e4SLinus Torvalds }
78081da177e4SLinus Torvalds 
7809b37ad28bSAl Viro __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7810ca364317SJ.Bruce Fields nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
7811eb69853dSChristoph Hellwig 	    union nfsd4_op_u *u)
78121da177e4SLinus Torvalds {
7813eb69853dSChristoph Hellwig 	struct nfsd4_locku *locku = &u->locku;
7814dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
7815eb82dd39SJeff Layton 	struct nfsd_file *nf = NULL;
781621179d81SJeff Layton 	struct file_lock *file_lock = NULL;
7817b37ad28bSAl Viro 	__be32 status;
7818b8dd7b9aSAl Viro 	int err;
78193320fef1SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
78201da177e4SLinus Torvalds 
78211da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
78221da177e4SLinus Torvalds 		(long long) locku->lu_offset,
78231da177e4SLinus Torvalds 		(long long) locku->lu_length);
78241da177e4SLinus Torvalds 
78251da177e4SLinus Torvalds 	if (check_lock_length(locku->lu_offset, locku->lu_length))
78261da177e4SLinus Torvalds 		 return nfserr_inval;
78271da177e4SLinus Torvalds 
78289072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
78293320fef1SStanislav Kinsbursky 					&locku->lu_stateid, NFS4_LOCK_STID,
78303320fef1SStanislav Kinsbursky 					&stp, nn);
78319072d5c6SJ. Bruce Fields 	if (status)
78321da177e4SLinus Torvalds 		goto out;
7833eb82dd39SJeff Layton 	nf = find_any_file(stp->st_stid.sc_file);
7834eb82dd39SJeff Layton 	if (!nf) {
7835f9d7562fSJ. Bruce Fields 		status = nfserr_lock_range;
7836858cc573STrond Myklebust 		goto put_stateid;
7837f9d7562fSJ. Bruce Fields 	}
783821179d81SJeff Layton 	file_lock = locks_alloc_lock();
783921179d81SJeff Layton 	if (!file_lock) {
784021179d81SJeff Layton 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
784121179d81SJeff Layton 		status = nfserr_jukebox;
7842eb82dd39SJeff Layton 		goto put_file;
784321179d81SJeff Layton 	}
78446cd90662SKinglong Mee 
784521179d81SJeff Layton 	file_lock->fl_type = F_UNLCK;
7846aef9583bSKinglong Mee 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
784721179d81SJeff Layton 	file_lock->fl_pid = current->tgid;
7848eb82dd39SJeff Layton 	file_lock->fl_file = nf->nf_file;
784921179d81SJeff Layton 	file_lock->fl_flags = FL_POSIX;
785021179d81SJeff Layton 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
785121179d81SJeff Layton 	file_lock->fl_start = locku->lu_offset;
78521da177e4SLinus Torvalds 
785321179d81SJeff Layton 	file_lock->fl_end = last_byte_offset(locku->lu_offset,
785421179d81SJeff Layton 						locku->lu_length);
785521179d81SJeff Layton 	nfs4_transform_lock_offset(file_lock);
78561da177e4SLinus Torvalds 
7857eb82dd39SJeff Layton 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7858b8dd7b9aSAl Viro 	if (err) {
7859fd85b817SMarc Eshel 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
78601da177e4SLinus Torvalds 		goto out_nfserr;
78611da177e4SLinus Torvalds 	}
78629767feb2SJeff Layton 	nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7863eb82dd39SJeff Layton put_file:
7864eb82dd39SJeff Layton 	nfsd_file_put(nf);
7865858cc573STrond Myklebust put_stateid:
7866feb9dad5SOleg Drokin 	mutex_unlock(&stp->st_mutex);
7867858cc573STrond Myklebust 	nfs4_put_stid(&stp->st_stid);
78681da177e4SLinus Torvalds out:
78699411b1d4SJ. Bruce Fields 	nfsd4_bump_seqid(cstate, status);
787021179d81SJeff Layton 	if (file_lock)
787121179d81SJeff Layton 		locks_free_lock(file_lock);
78721da177e4SLinus Torvalds 	return status;
78731da177e4SLinus Torvalds 
78741da177e4SLinus Torvalds out_nfserr:
7875b8dd7b9aSAl Viro 	status = nfserrno(err);
7876eb82dd39SJeff Layton 	goto put_file;
78771da177e4SLinus Torvalds }
78781da177e4SLinus Torvalds 
78791da177e4SLinus Torvalds /*
78801da177e4SLinus Torvalds  * returns
7881f9c00c3aSJeff Layton  * 	true:  locks held by lockowner
7882f9c00c3aSJeff Layton  * 	false: no locks held by lockowner
78831da177e4SLinus Torvalds  */
7884f9c00c3aSJeff Layton static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)7885f9c00c3aSJeff Layton check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
78861da177e4SLinus Torvalds {
7887bd61e0a9SJeff Layton 	struct file_lock *fl;
7888f9c00c3aSJeff Layton 	int status = false;
7889b7d2eee1SNeilBrown 	struct nfsd_file *nf;
7890f9c00c3aSJeff Layton 	struct inode *inode;
7891bd61e0a9SJeff Layton 	struct file_lock_context *flctx;
7892f9c00c3aSJeff Layton 
7893b7d2eee1SNeilBrown 	spin_lock(&fp->fi_lock);
7894b7d2eee1SNeilBrown 	nf = find_any_file_locked(fp);
7895eb82dd39SJeff Layton 	if (!nf) {
7896f9c00c3aSJeff Layton 		/* Any valid lock stateid should have some sort of access */
7897f9c00c3aSJeff Layton 		WARN_ON_ONCE(1);
7898b7d2eee1SNeilBrown 		goto out;
7899f9c00c3aSJeff Layton 	}
7900f9c00c3aSJeff Layton 
7901c65454a9SJeff Layton 	inode = file_inode(nf->nf_file);
790277c67530SJeff Layton 	flctx = locks_inode_context(inode);
79031da177e4SLinus Torvalds 
7904bd61e0a9SJeff Layton 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
79056109c850SJeff Layton 		spin_lock(&flctx->flc_lock);
7906bd61e0a9SJeff Layton 		list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7907bd61e0a9SJeff Layton 			if (fl->fl_owner == (fl_owner_t)lowner) {
7908f9c00c3aSJeff Layton 				status = true;
7909f9c00c3aSJeff Layton 				break;
79101da177e4SLinus Torvalds 			}
7911796dadfdSJ. Bruce Fields 		}
79126109c850SJeff Layton 		spin_unlock(&flctx->flc_lock);
7913bd61e0a9SJeff Layton 	}
7914b7d2eee1SNeilBrown out:
7915b7d2eee1SNeilBrown 	spin_unlock(&fp->fi_lock);
79161da177e4SLinus Torvalds 	return status;
79171da177e4SLinus Torvalds }
79181da177e4SLinus Torvalds 
7919043862b0SChuck Lever /**
7920043862b0SChuck Lever  * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
7921043862b0SChuck Lever  * @rqstp: RPC transaction
7922043862b0SChuck Lever  * @cstate: NFSv4 COMPOUND state
7923043862b0SChuck Lever  * @u: RELEASE_LOCKOWNER arguments
7924043862b0SChuck Lever  *
7925b7d2eee1SNeilBrown  * Check if theree are any locks still held and if not - free the lockowner
7926b7d2eee1SNeilBrown  * and any lock state that is owned.
7927043862b0SChuck Lever  *
7928043862b0SChuck Lever  * Return values:
7929043862b0SChuck Lever  *   %nfs_ok: lockowner released or not found
7930043862b0SChuck Lever  *   %nfserr_locks_held: lockowner still in use
7931043862b0SChuck Lever  *   %nfserr_stale_clientid: clientid no longer active
7932043862b0SChuck Lever  *   %nfserr_expired: clientid not recognized
7933043862b0SChuck Lever  */
7934b37ad28bSAl Viro __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7935b591480bSJ.Bruce Fields nfsd4_release_lockowner(struct svc_rqst *rqstp,
7936b591480bSJ.Bruce Fields 			struct nfsd4_compound_state *cstate,
7937eb69853dSChristoph Hellwig 			union nfsd4_op_u *u)
79381da177e4SLinus Torvalds {
7939eb69853dSChristoph Hellwig 	struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
79407f2210faSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7941bd8fdb6eSChuck Lever 	clientid_t *clid = &rlockowner->rl_clientid;
7942bd8fdb6eSChuck Lever 	struct nfs4_ol_stateid *stp;
7943bd8fdb6eSChuck Lever 	struct nfs4_lockowner *lo;
7944c58c6610STrond Myklebust 	struct nfs4_client *clp;
794588584818SChuck Lever 	LIST_HEAD(reaplist);
7946bd8fdb6eSChuck Lever 	__be32 status;
79471da177e4SLinus Torvalds 
79481da177e4SLinus Torvalds 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
79491da177e4SLinus Torvalds 		clid->cl_boot, clid->cl_id);
79501da177e4SLinus Torvalds 
7951f71475baSJ. Bruce Fields 	status = set_client(clid, cstate, nn);
79529b2ef62bSJ. Bruce Fields 	if (status)
795351f5e783STrond Myklebust 		return status;
7954d4f0489fSTrond Myklebust 	clp = cstate->clp;
7955bd8fdb6eSChuck Lever 
7956d4f0489fSTrond Myklebust 	spin_lock(&clp->cl_lock);
7957bd8fdb6eSChuck Lever 	lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
795888584818SChuck Lever 	if (!lo) {
7959d4f0489fSTrond Myklebust 		spin_unlock(&clp->cl_lock);
7960043862b0SChuck Lever 		return nfs_ok;
796188584818SChuck Lever 	}
7962b7d2eee1SNeilBrown 
7963b7d2eee1SNeilBrown 	list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
7964b7d2eee1SNeilBrown 		if (check_for_locks(stp->st_stid.sc_file, lo)) {
7965bd8fdb6eSChuck Lever 			spin_unlock(&clp->cl_lock);
7966bd8fdb6eSChuck Lever 			nfs4_put_stateowner(&lo->lo_owner);
7967bd8fdb6eSChuck Lever 			return nfserr_locks_held;
7968bd8fdb6eSChuck Lever 		}
7969b7d2eee1SNeilBrown 	}
797088584818SChuck Lever 	unhash_lockowner_locked(lo);
797188584818SChuck Lever 	while (!list_empty(&lo->lo_owner.so_stateids)) {
797288584818SChuck Lever 		stp = list_first_entry(&lo->lo_owner.so_stateids,
797388584818SChuck Lever 				       struct nfs4_ol_stateid,
797488584818SChuck Lever 				       st_perstateowner);
797588584818SChuck Lever 		WARN_ON(!unhash_lock_stateid(stp));
797688584818SChuck Lever 		put_ol_stateid_locked(stp, &reaplist);
797788584818SChuck Lever 	}
797888584818SChuck Lever 	spin_unlock(&clp->cl_lock);
7979043862b0SChuck Lever 
798088584818SChuck Lever 	free_ol_stateid_reaplist(&reaplist);
798168ef3bc3SJeff Layton 	remove_blocked_locks(lo);
798288584818SChuck Lever 	nfs4_put_stateowner(&lo->lo_owner);
7983043862b0SChuck Lever 	return nfs_ok;
79841da177e4SLinus Torvalds }
79851da177e4SLinus Torvalds 
79861da177e4SLinus Torvalds static inline struct nfs4_client_reclaim *
alloc_reclaim(void)7987a55370a3SNeilBrown alloc_reclaim(void)
79881da177e4SLinus Torvalds {
7989a55370a3SNeilBrown 	return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
79901da177e4SLinus Torvalds }
79911da177e4SLinus Torvalds 
79920ce0c2b5SJeff Layton bool
nfs4_has_reclaimed_state(struct xdr_netobj name,struct nfsd_net * nn)79936b189105SScott Mayhew nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7994c7b9a459SNeilBrown {
79950ce0c2b5SJeff Layton 	struct nfs4_client_reclaim *crp;
7996c7b9a459SNeilBrown 
799752e19c09SStanislav Kinsbursky 	crp = nfsd4_find_reclaim_client(name, nn);
79980ce0c2b5SJeff Layton 	return (crp && crp->cr_clp);
7999c7b9a459SNeilBrown }
8000c7b9a459SNeilBrown 
80011da177e4SLinus Torvalds /*
80021da177e4SLinus Torvalds  * failure => all reset bets are off, nfserr_no_grace...
80036b189105SScott Mayhew  *
80046b189105SScott Mayhew  * The caller is responsible for freeing name.data if NULL is returned (it
80056b189105SScott Mayhew  * will be freed in nfs4_remove_reclaim_record in the normal case).
80061da177e4SLinus Torvalds  */
8007772a9bbbSJeff Layton struct nfs4_client_reclaim *
nfs4_client_to_reclaim(struct xdr_netobj name,struct xdr_netobj princhash,struct nfsd_net * nn)80086ee95d1cSScott Mayhew nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
80096ee95d1cSScott Mayhew 		struct nfsd_net *nn)
80101da177e4SLinus Torvalds {
80111da177e4SLinus Torvalds 	unsigned int strhashval;
8012772a9bbbSJeff Layton 	struct nfs4_client_reclaim *crp;
80131da177e4SLinus Torvalds 
8014a55370a3SNeilBrown 	crp = alloc_reclaim();
8015772a9bbbSJeff Layton 	if (crp) {
8016a55370a3SNeilBrown 		strhashval = clientstr_hashval(name);
80171da177e4SLinus Torvalds 		INIT_LIST_HEAD(&crp->cr_strhash);
801852e19c09SStanislav Kinsbursky 		list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
80196b189105SScott Mayhew 		crp->cr_name.data = name.data;
80206b189105SScott Mayhew 		crp->cr_name.len = name.len;
80216ee95d1cSScott Mayhew 		crp->cr_princhash.data = princhash.data;
80226ee95d1cSScott Mayhew 		crp->cr_princhash.len = princhash.len;
80230ce0c2b5SJeff Layton 		crp->cr_clp = NULL;
802452e19c09SStanislav Kinsbursky 		nn->reclaim_str_hashtbl_size++;
8025772a9bbbSJeff Layton 	}
8026772a9bbbSJeff Layton 	return crp;
80271da177e4SLinus Torvalds }
80281da177e4SLinus Torvalds 
80292a4317c5SJeff Layton void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)803052e19c09SStanislav Kinsbursky nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
8031ce30e539SJeff Layton {
8032ce30e539SJeff Layton 	list_del(&crp->cr_strhash);
80336b189105SScott Mayhew 	kfree(crp->cr_name.data);
80346ee95d1cSScott Mayhew 	kfree(crp->cr_princhash.data);
8035ce30e539SJeff Layton 	kfree(crp);
803652e19c09SStanislav Kinsbursky 	nn->reclaim_str_hashtbl_size--;
8037ce30e539SJeff Layton }
8038ce30e539SJeff Layton 
8039ce30e539SJeff Layton void
nfs4_release_reclaim(struct nfsd_net * nn)804052e19c09SStanislav Kinsbursky nfs4_release_reclaim(struct nfsd_net *nn)
80411da177e4SLinus Torvalds {
80421da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
80431da177e4SLinus Torvalds 	int i;
80441da177e4SLinus Torvalds 
80451da177e4SLinus Torvalds 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
804652e19c09SStanislav Kinsbursky 		while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
804752e19c09SStanislav Kinsbursky 			crp = list_entry(nn->reclaim_str_hashtbl[i].next,
80481da177e4SLinus Torvalds 			                struct nfs4_client_reclaim, cr_strhash);
804952e19c09SStanislav Kinsbursky 			nfs4_remove_reclaim_record(crp, nn);
80501da177e4SLinus Torvalds 		}
80511da177e4SLinus Torvalds 	}
8052063b0fb9SJ. Bruce Fields 	WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
80531da177e4SLinus Torvalds }
80541da177e4SLinus Torvalds 
80551da177e4SLinus Torvalds /*
80561da177e4SLinus Torvalds  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
80572a4317c5SJeff Layton struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(struct xdr_netobj name,struct nfsd_net * nn)80586b189105SScott Mayhew nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
80591da177e4SLinus Torvalds {
80601da177e4SLinus Torvalds 	unsigned int strhashval;
80611da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
80621da177e4SLinus Torvalds 
80636b189105SScott Mayhew 	strhashval = clientstr_hashval(name);
806452e19c09SStanislav Kinsbursky 	list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
80656b189105SScott Mayhew 		if (compare_blob(&crp->cr_name, &name) == 0) {
80661da177e4SLinus Torvalds 			return crp;
80671da177e4SLinus Torvalds 		}
80681da177e4SLinus Torvalds 	}
80691da177e4SLinus Torvalds 	return NULL;
80701da177e4SLinus Torvalds }
80711da177e4SLinus Torvalds 
8072b37ad28bSAl Viro __be32
nfs4_check_open_reclaim(struct nfs4_client * clp)80731722b046SJ. Bruce Fields nfs4_check_open_reclaim(struct nfs4_client *clp)
80741da177e4SLinus Torvalds {
80751722b046SJ. Bruce Fields 	if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
80763b3e7b72SJeff Layton 		return nfserr_no_grace;
80773b3e7b72SJeff Layton 
80781722b046SJ. Bruce Fields 	if (nfsd4_client_record_check(clp))
80790fe492dbSTrond Myklebust 		return nfserr_reclaim_bad;
80800fe492dbSTrond Myklebust 
80810fe492dbSTrond Myklebust 	return nfs_ok;
80821da177e4SLinus Torvalds }
80831da177e4SLinus Torvalds 
8084c2f1a551SMeelap Shah /*
8085c2f1a551SMeelap Shah  * Since the lifetime of a delegation isn't limited to that of an open, a
8086c2f1a551SMeelap Shah  * client may quite reasonably hang on to a delegation as long as it has
8087c2f1a551SMeelap Shah  * the inode cached.  This becomes an obvious problem the first time a
8088c2f1a551SMeelap Shah  * client's inode cache approaches the size of the server's total memory.
8089c2f1a551SMeelap Shah  *
8090c2f1a551SMeelap Shah  * For now we avoid this problem by imposing a hard limit on the number
8091c2f1a551SMeelap Shah  * of delegations, which varies according to the server's memory size.
8092c2f1a551SMeelap Shah  */
8093c2f1a551SMeelap Shah static void
set_max_delegations(void)8094c2f1a551SMeelap Shah set_max_delegations(void)
8095c2f1a551SMeelap Shah {
8096c2f1a551SMeelap Shah 	/*
8097c2f1a551SMeelap Shah 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
8098c2f1a551SMeelap Shah 	 * estimates suggest that in the worst case (where every delegation
8099c2f1a551SMeelap Shah 	 * is for a different inode), a delegation could take about 1.5K,
8100c2f1a551SMeelap Shah 	 * giving a worst case usage of about 6% of memory.
8101c2f1a551SMeelap Shah 	 */
8102c2f1a551SMeelap Shah 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
8103c2f1a551SMeelap Shah }
8104c2f1a551SMeelap Shah 
nfs4_state_create_net(struct net * net)8105d85ed443SStanislav Kinsbursky static int nfs4_state_create_net(struct net *net)
81068daae4dcSStanislav Kinsbursky {
81078daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
81088daae4dcSStanislav Kinsbursky 	int i;
81098daae4dcSStanislav Kinsbursky 
81106da2ec56SKees Cook 	nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
81116da2ec56SKees Cook 					    sizeof(struct list_head),
81126da2ec56SKees Cook 					    GFP_KERNEL);
81138daae4dcSStanislav Kinsbursky 	if (!nn->conf_id_hashtbl)
8114382a62e7SStanislav Kinsbursky 		goto err;
81156da2ec56SKees Cook 	nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
81166da2ec56SKees Cook 					      sizeof(struct list_head),
81176da2ec56SKees Cook 					      GFP_KERNEL);
81180a7ec377SStanislav Kinsbursky 	if (!nn->unconf_id_hashtbl)
81190a7ec377SStanislav Kinsbursky 		goto err_unconf_id;
81206da2ec56SKees Cook 	nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
81216da2ec56SKees Cook 					      sizeof(struct list_head),
81226da2ec56SKees Cook 					      GFP_KERNEL);
81231872de0eSStanislav Kinsbursky 	if (!nn->sessionid_hashtbl)
81241872de0eSStanislav Kinsbursky 		goto err_sessionid;
81258daae4dcSStanislav Kinsbursky 
8126382a62e7SStanislav Kinsbursky 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
81278daae4dcSStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
81280a7ec377SStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
8129382a62e7SStanislav Kinsbursky 	}
81301872de0eSStanislav Kinsbursky 	for (i = 0; i < SESSION_HASH_SIZE; i++)
81311872de0eSStanislav Kinsbursky 		INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
8132382a62e7SStanislav Kinsbursky 	nn->conf_name_tree = RB_ROOT;
8133a99454aaSStanislav Kinsbursky 	nn->unconf_name_tree = RB_ROOT;
81349cc76801SArnd Bergmann 	nn->boot_time = ktime_get_real_seconds();
813581833de1SVasily Averin 	nn->grace_ended = false;
813681833de1SVasily Averin 	nn->nfsd4_manager.block_opens = true;
813781833de1SVasily Averin 	INIT_LIST_HEAD(&nn->nfsd4_manager.list);
81385ed58bb2SStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->client_lru);
813973758fedSStanislav Kinsbursky 	INIT_LIST_HEAD(&nn->close_lru);
8140e8c69d17SJ. Bruce Fields 	INIT_LIST_HEAD(&nn->del_recall_lru);
8141c9a49628SStanislav Kinsbursky 	spin_lock_init(&nn->client_lock);
8142e0639dc5SOlga Kornievskaia 	spin_lock_init(&nn->s2s_cp_lock);
8143e0639dc5SOlga Kornievskaia 	idr_init(&nn->s2s_cp_stateids);
81448daae4dcSStanislav Kinsbursky 
81450cc11a61SJeff Layton 	spin_lock_init(&nn->blocked_locks_lock);
81460cc11a61SJeff Layton 	INIT_LIST_HEAD(&nn->blocked_locks_lru);
81470cc11a61SJeff Layton 
814809121281SStanislav Kinsbursky 	INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
81497c24fa22SDai Ngo 	INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
8150d85ed443SStanislav Kinsbursky 	get_net(net);
815109121281SStanislav Kinsbursky 
8152f385f7d2SDai Ngo 	nn->nfsd_client_shrinker.scan_objects = nfsd4_state_shrinker_scan;
8153f385f7d2SDai Ngo 	nn->nfsd_client_shrinker.count_objects = nfsd4_state_shrinker_count;
8154f385f7d2SDai Ngo 	nn->nfsd_client_shrinker.seeks = DEFAULT_SEEKS;
8155f385f7d2SDai Ngo 
8156f385f7d2SDai Ngo 	if (register_shrinker(&nn->nfsd_client_shrinker, "nfsd-client"))
8157f385f7d2SDai Ngo 		goto err_shrinker;
81588daae4dcSStanislav Kinsbursky 	return 0;
8159382a62e7SStanislav Kinsbursky 
8160f385f7d2SDai Ngo err_shrinker:
8161f385f7d2SDai Ngo 	put_net(net);
8162f385f7d2SDai Ngo 	kfree(nn->sessionid_hashtbl);
81631872de0eSStanislav Kinsbursky err_sessionid:
81649b531137SStanislav Kinsbursky 	kfree(nn->unconf_id_hashtbl);
81650a7ec377SStanislav Kinsbursky err_unconf_id:
81660a7ec377SStanislav Kinsbursky 	kfree(nn->conf_id_hashtbl);
8167382a62e7SStanislav Kinsbursky err:
8168382a62e7SStanislav Kinsbursky 	return -ENOMEM;
81698daae4dcSStanislav Kinsbursky }
81708daae4dcSStanislav Kinsbursky 
81718daae4dcSStanislav Kinsbursky static void
nfs4_state_destroy_net(struct net * net)81724dce0ac9SStanislav Kinsbursky nfs4_state_destroy_net(struct net *net)
81738daae4dcSStanislav Kinsbursky {
81748daae4dcSStanislav Kinsbursky 	int i;
81758daae4dcSStanislav Kinsbursky 	struct nfs4_client *clp = NULL;
81768daae4dcSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
81778daae4dcSStanislav Kinsbursky 
81788daae4dcSStanislav Kinsbursky 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
81798daae4dcSStanislav Kinsbursky 		while (!list_empty(&nn->conf_id_hashtbl[i])) {
81808daae4dcSStanislav Kinsbursky 			clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
81818daae4dcSStanislav Kinsbursky 			destroy_client(clp);
81828daae4dcSStanislav Kinsbursky 		}
81838daae4dcSStanislav Kinsbursky 	}
8184a99454aaSStanislav Kinsbursky 
818568ef3bc3SJeff Layton 	WARN_ON(!list_empty(&nn->blocked_locks_lru));
818668ef3bc3SJeff Layton 
81872b905635SKinglong Mee 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
81882b905635SKinglong Mee 		while (!list_empty(&nn->unconf_id_hashtbl[i])) {
81892b905635SKinglong Mee 			clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
8190a99454aaSStanislav Kinsbursky 			destroy_client(clp);
8191a99454aaSStanislav Kinsbursky 		}
81922b905635SKinglong Mee 	}
8193a99454aaSStanislav Kinsbursky 
81941872de0eSStanislav Kinsbursky 	kfree(nn->sessionid_hashtbl);
81950a7ec377SStanislav Kinsbursky 	kfree(nn->unconf_id_hashtbl);
81968daae4dcSStanislav Kinsbursky 	kfree(nn->conf_id_hashtbl);
81974dce0ac9SStanislav Kinsbursky 	put_net(net);
81988daae4dcSStanislav Kinsbursky }
81998daae4dcSStanislav Kinsbursky 
8200f252bc68SStanislav Kinsbursky int
nfs4_state_start_net(struct net * net)8201d85ed443SStanislav Kinsbursky nfs4_state_start_net(struct net *net)
8202ac4d8ff2SNeilBrown {
82035e1533c7SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
8204b5a1a81eSJ. Bruce Fields 	int ret;
8205b5a1a81eSJ. Bruce Fields 
8206c6c7f2a8STrond Myklebust 	ret = nfs4_state_create_net(net);
82078daae4dcSStanislav Kinsbursky 	if (ret)
82088daae4dcSStanislav Kinsbursky 		return ret;
8209d4318acdSJeff Layton 	locks_start_grace(net, &nn->nfsd4_manager);
8210d4318acdSJeff Layton 	nfsd4_client_tracking_init(net);
8211362063a5SScott Mayhew 	if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
8212362063a5SScott Mayhew 		goto skip_grace;
821320b7d86fSArnd Bergmann 	printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
82147e981a8aSVasily Averin 	       nn->nfsd4_grace, net->ns.inum);
8215dd5e3fbcSChuck Lever 	trace_nfsd_grace_start(nn);
82165284b44eSStanislav Kinsbursky 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
8217d85ed443SStanislav Kinsbursky 	return 0;
8218362063a5SScott Mayhew 
8219362063a5SScott Mayhew skip_grace:
8220362063a5SScott Mayhew 	printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
8221362063a5SScott Mayhew 			net->ns.inum);
8222362063a5SScott Mayhew 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
8223362063a5SScott Mayhew 	nfsd4_end_grace(nn);
8224362063a5SScott Mayhew 	return 0;
8225a6d6b781SJeff Layton }
8226d85ed443SStanislav Kinsbursky 
8227d85ed443SStanislav Kinsbursky /* initialization to perform when the nfsd service is started: */
8228d85ed443SStanislav Kinsbursky 
8229d85ed443SStanislav Kinsbursky int
nfs4_state_start(void)8230d85ed443SStanislav Kinsbursky nfs4_state_start(void)
8231d85ed443SStanislav Kinsbursky {
8232d85ed443SStanislav Kinsbursky 	int ret;
8233d85ed443SStanislav Kinsbursky 
8234d47b295eSChuck Lever 	ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
8235b5a1a81eSJ. Bruce Fields 	if (ret)
8236d76cc46bSDai Ngo 		return ret;
823709121281SStanislav Kinsbursky 
8238d47b295eSChuck Lever 	ret = nfsd4_create_callback_queue();
8239d47b295eSChuck Lever 	if (ret) {
8240d47b295eSChuck Lever 		rhltable_destroy(&nfs4_file_rhltable);
8241d47b295eSChuck Lever 		return ret;
8242d47b295eSChuck Lever 	}
8243d47b295eSChuck Lever 
8244c2f1a551SMeelap Shah 	set_max_delegations();
8245b5a1a81eSJ. Bruce Fields 	return 0;
82461da177e4SLinus Torvalds }
82471da177e4SLinus Torvalds 
8248f252bc68SStanislav Kinsbursky void
nfs4_state_shutdown_net(struct net * net)82494dce0ac9SStanislav Kinsbursky nfs4_state_shutdown_net(struct net *net)
82501da177e4SLinus Torvalds {
82511da177e4SLinus Torvalds 	struct nfs4_delegation *dp = NULL;
82521da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
82534dce0ac9SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
82541da177e4SLinus Torvalds 
8255f385f7d2SDai Ngo 	unregister_shrinker(&nn->nfsd_client_shrinker);
82567c24fa22SDai Ngo 	cancel_work(&nn->nfsd_shrinker_work);
82574dce0ac9SStanislav Kinsbursky 	cancel_delayed_work_sync(&nn->laundromat_work);
82584dce0ac9SStanislav Kinsbursky 	locks_end_grace(&nn->nfsd4_manager);
8259ac55fdc4SJeff Layton 
82601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
8261cdc97505SBenny Halevy 	spin_lock(&state_lock);
8262e8c69d17SJ. Bruce Fields 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
82631da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
82643fcbbd24SJeff Layton 		WARN_ON(!unhash_delegation_locked(dp));
826542690676SJeff Layton 		list_add(&dp->dl_recall_lru, &reaplist);
82661da177e4SLinus Torvalds 	}
8267cdc97505SBenny Halevy 	spin_unlock(&state_lock);
82681da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &reaplist) {
82691da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
827042690676SJeff Layton 		list_del_init(&dp->dl_recall_lru);
82710af6e690SJ. Bruce Fields 		destroy_unhashed_deleg(dp);
82721da177e4SLinus Torvalds 	}
82731da177e4SLinus Torvalds 
82743320fef1SStanislav Kinsbursky 	nfsd4_client_tracking_exit(net);
82754dce0ac9SStanislav Kinsbursky 	nfs4_state_destroy_net(net);
8276f4e44b39SDai Ngo #ifdef CONFIG_NFSD_V4_2_INTER_SSC
8277f4e44b39SDai Ngo 	nfsd4_ssc_shutdown_umount(nn);
8278f4e44b39SDai Ngo #endif
82791da177e4SLinus Torvalds }
82801da177e4SLinus Torvalds 
82811da177e4SLinus Torvalds void
nfs4_state_shutdown(void)82821da177e4SLinus Torvalds nfs4_state_shutdown(void)
82831da177e4SLinus Torvalds {
8284c3935e30SJ. Bruce Fields 	nfsd4_destroy_callback_queue();
82854102db17SJeff Layton 	rhltable_destroy(&nfs4_file_rhltable);
82861da177e4SLinus Torvalds }
82878b70484cSTigran Mkrtchyan 
82888b70484cSTigran Mkrtchyan static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)82898b70484cSTigran Mkrtchyan get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
82908b70484cSTigran Mkrtchyan {
829151100d2bSOlga Kornievskaia 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
829251100d2bSOlga Kornievskaia 	    CURRENT_STATEID(stateid))
829337c593c5STigran Mkrtchyan 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
82948b70484cSTigran Mkrtchyan }
82958b70484cSTigran Mkrtchyan 
82968b70484cSTigran Mkrtchyan static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)82978b70484cSTigran Mkrtchyan put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
82988b70484cSTigran Mkrtchyan {
829937c593c5STigran Mkrtchyan 	if (cstate->minorversion) {
830037c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
830151100d2bSOlga Kornievskaia 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
830237c593c5STigran Mkrtchyan 	}
830337c593c5STigran Mkrtchyan }
830437c593c5STigran Mkrtchyan 
830537c593c5STigran Mkrtchyan void
clear_current_stateid(struct nfsd4_compound_state * cstate)830637c593c5STigran Mkrtchyan clear_current_stateid(struct nfsd4_compound_state *cstate)
830737c593c5STigran Mkrtchyan {
830851100d2bSOlga Kornievskaia 	CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
83098b70484cSTigran Mkrtchyan }
83108b70484cSTigran Mkrtchyan 
831162cd4a59STigran Mkrtchyan /*
831262cd4a59STigran Mkrtchyan  * functions to set current state id
831362cd4a59STigran Mkrtchyan  */
83148b70484cSTigran Mkrtchyan void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8315b60e9859SChristoph Hellwig nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
8316b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
83179428fe1aSTigran Mkrtchyan {
8318b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->open_downgrade.od_stateid);
83199428fe1aSTigran Mkrtchyan }
83209428fe1aSTigran Mkrtchyan 
83219428fe1aSTigran Mkrtchyan void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8322b60e9859SChristoph Hellwig nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
8323b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
83248b70484cSTigran Mkrtchyan {
8325b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->open.op_stateid);
83268b70484cSTigran Mkrtchyan }
83278b70484cSTigran Mkrtchyan 
83288b70484cSTigran Mkrtchyan void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8329b60e9859SChristoph Hellwig nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
8330b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
833162cd4a59STigran Mkrtchyan {
8332b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->close.cl_stateid);
833362cd4a59STigran Mkrtchyan }
833462cd4a59STigran Mkrtchyan 
833562cd4a59STigran Mkrtchyan void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)8336b60e9859SChristoph Hellwig nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
8337b60e9859SChristoph Hellwig 		union nfsd4_op_u *u)
833862cd4a59STigran Mkrtchyan {
8339b60e9859SChristoph Hellwig 	put_stateid(cstate, &u->lock.lk_resp_stateid);
834062cd4a59STigran Mkrtchyan }
834162cd4a59STigran Mkrtchyan 
834262cd4a59STigran Mkrtchyan /*
834362cd4a59STigran Mkrtchyan  * functions to consume current state id
834462cd4a59STigran Mkrtchyan  */
83451e97b519STigran Mkrtchyan 
83461e97b519STigran Mkrtchyan void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)834757832e7bSChristoph Hellwig nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
834857832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83499428fe1aSTigran Mkrtchyan {
835057832e7bSChristoph Hellwig 	get_stateid(cstate, &u->open_downgrade.od_stateid);
83519428fe1aSTigran Mkrtchyan }
83529428fe1aSTigran Mkrtchyan 
83539428fe1aSTigran Mkrtchyan void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)835457832e7bSChristoph Hellwig nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
835557832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83569428fe1aSTigran Mkrtchyan {
835757832e7bSChristoph Hellwig 	get_stateid(cstate, &u->delegreturn.dr_stateid);
83589428fe1aSTigran Mkrtchyan }
83599428fe1aSTigran Mkrtchyan 
83609428fe1aSTigran Mkrtchyan void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)836157832e7bSChristoph Hellwig nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
836257832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83631e97b519STigran Mkrtchyan {
836457832e7bSChristoph Hellwig 	get_stateid(cstate, &u->free_stateid.fr_stateid);
83651e97b519STigran Mkrtchyan }
83661e97b519STigran Mkrtchyan 
83671e97b519STigran Mkrtchyan void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)836857832e7bSChristoph Hellwig nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
836957832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83701e97b519STigran Mkrtchyan {
837157832e7bSChristoph Hellwig 	get_stateid(cstate, &u->setattr.sa_stateid);
83721e97b519STigran Mkrtchyan }
83731e97b519STigran Mkrtchyan 
837462cd4a59STigran Mkrtchyan void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)837557832e7bSChristoph Hellwig nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
837657832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83778b70484cSTigran Mkrtchyan {
837857832e7bSChristoph Hellwig 	get_stateid(cstate, &u->close.cl_stateid);
83798b70484cSTigran Mkrtchyan }
83808b70484cSTigran Mkrtchyan 
83818b70484cSTigran Mkrtchyan void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)838257832e7bSChristoph Hellwig nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
838357832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
83848b70484cSTigran Mkrtchyan {
838557832e7bSChristoph Hellwig 	get_stateid(cstate, &u->locku.lu_stateid);
83868b70484cSTigran Mkrtchyan }
838730813e27STigran Mkrtchyan 
838830813e27STigran Mkrtchyan void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)838957832e7bSChristoph Hellwig nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
839057832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
839130813e27STigran Mkrtchyan {
839257832e7bSChristoph Hellwig 	get_stateid(cstate, &u->read.rd_stateid);
839330813e27STigran Mkrtchyan }
839430813e27STigran Mkrtchyan 
839530813e27STigran Mkrtchyan void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)839657832e7bSChristoph Hellwig nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
839757832e7bSChristoph Hellwig 		union nfsd4_op_u *u)
839830813e27STigran Mkrtchyan {
839957832e7bSChristoph Hellwig 	get_stateid(cstate, &u->write.wr_stateid);
840030813e27STigran Mkrtchyan }
8401fd19ca36SDai Ngo 
8402fd19ca36SDai Ngo /**
8403fd19ca36SDai Ngo  * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
8404fd19ca36SDai Ngo  * @rqstp: RPC transaction context
8405fd19ca36SDai Ngo  * @inode: file to be checked for a conflict
8406fd19ca36SDai Ngo  *
8407fd19ca36SDai Ngo  * This function is called when there is a conflict between a write
8408fd19ca36SDai Ngo  * delegation and a change/size GETATTR from another client. The server
8409fd19ca36SDai Ngo  * must either use the CB_GETATTR to get the current values of the
8410fd19ca36SDai Ngo  * attributes from the client that holds the delegation or recall the
8411fd19ca36SDai Ngo  * delegation before replying to the GETATTR. See RFC 8881 section
8412fd19ca36SDai Ngo  * 18.7.4.
8413fd19ca36SDai Ngo  *
8414fd19ca36SDai Ngo  * The current implementation does not support CB_GETATTR yet. However
8415fd19ca36SDai Ngo  * this can avoid recalling the delegation could be added in follow up
8416fd19ca36SDai Ngo  * work.
8417fd19ca36SDai Ngo  *
8418fd19ca36SDai Ngo  * Returns 0 if there is no conflict; otherwise an nfs_stat
8419fd19ca36SDai Ngo  * code is returned.
8420fd19ca36SDai Ngo  */
8421fd19ca36SDai Ngo __be32
nfsd4_deleg_getattr_conflict(struct svc_rqst * rqstp,struct inode * inode)8422fd19ca36SDai Ngo nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct inode *inode)
8423fd19ca36SDai Ngo {
8424fd19ca36SDai Ngo 	__be32 status;
8425fd19ca36SDai Ngo 	struct file_lock_context *ctx;
8426fd19ca36SDai Ngo 	struct file_lock *fl;
8427fd19ca36SDai Ngo 	struct nfs4_delegation *dp;
8428fd19ca36SDai Ngo 
8429fd19ca36SDai Ngo 	ctx = locks_inode_context(inode);
8430fd19ca36SDai Ngo 	if (!ctx)
8431fd19ca36SDai Ngo 		return 0;
8432fd19ca36SDai Ngo 	spin_lock(&ctx->flc_lock);
8433fd19ca36SDai Ngo 	list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
8434fd19ca36SDai Ngo 		if (fl->fl_flags == FL_LAYOUT)
8435fd19ca36SDai Ngo 			continue;
8436fd19ca36SDai Ngo 		if (fl->fl_lmops != &nfsd_lease_mng_ops) {
8437fd19ca36SDai Ngo 			/*
8438fd19ca36SDai Ngo 			 * non-nfs lease, if it's a lease with F_RDLCK then
8439fd19ca36SDai Ngo 			 * we are done; there isn't any write delegation
8440fd19ca36SDai Ngo 			 * on this inode
8441fd19ca36SDai Ngo 			 */
8442fd19ca36SDai Ngo 			if (fl->fl_type == F_RDLCK)
8443fd19ca36SDai Ngo 				break;
8444fd19ca36SDai Ngo 			goto break_lease;
8445fd19ca36SDai Ngo 		}
8446fd19ca36SDai Ngo 		if (fl->fl_type == F_WRLCK) {
8447fd19ca36SDai Ngo 			dp = fl->fl_owner;
8448fd19ca36SDai Ngo 			if (dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
8449fd19ca36SDai Ngo 				spin_unlock(&ctx->flc_lock);
8450fd19ca36SDai Ngo 				return 0;
8451fd19ca36SDai Ngo 			}
8452fd19ca36SDai Ngo break_lease:
8453fd19ca36SDai Ngo 			spin_unlock(&ctx->flc_lock);
8454fd19ca36SDai Ngo 			nfsd_stats_wdeleg_getattr_inc();
8455fd19ca36SDai Ngo 			status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
8456fd19ca36SDai Ngo 			if (status != nfserr_jukebox ||
8457fd19ca36SDai Ngo 					!nfsd_wait_for_delegreturn(rqstp, inode))
8458fd19ca36SDai Ngo 				return status;
8459fd19ca36SDai Ngo 			return 0;
8460fd19ca36SDai Ngo 		}
8461fd19ca36SDai Ngo 		break;
8462fd19ca36SDai Ngo 	}
8463fd19ca36SDai Ngo 	spin_unlock(&ctx->flc_lock);
8464fd19ca36SDai Ngo 	return 0;
8465fd19ca36SDai Ngo }
8466