xref: /OK3568_Linux_fs/kernel/fs/nfsd/nfs4state.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *  Copyright (c) 2001 The Regents of the University of Michigan.
3*4882a593Smuzhiyun *  All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun *  Kendrick Smith <kmsmith@umich.edu>
6*4882a593Smuzhiyun *  Andy Adamson <kandros@umich.edu>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *  Redistribution and use in source and binary forms, with or without
9*4882a593Smuzhiyun *  modification, are permitted provided that the following conditions
10*4882a593Smuzhiyun *  are met:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun *  1. Redistributions of source code must retain the above copyright
13*4882a593Smuzhiyun *     notice, this list of conditions and the following disclaimer.
14*4882a593Smuzhiyun *  2. Redistributions in binary form must reproduce the above copyright
15*4882a593Smuzhiyun *     notice, this list of conditions and the following disclaimer in the
16*4882a593Smuzhiyun *     documentation and/or other materials provided with the distribution.
17*4882a593Smuzhiyun *  3. Neither the name of the University nor the names of its
18*4882a593Smuzhiyun *     contributors may be used to endorse or promote products derived
19*4882a593Smuzhiyun *     from this software without specific prior written permission.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22*4882a593Smuzhiyun *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23*4882a593Smuzhiyun *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24*4882a593Smuzhiyun *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*4882a593Smuzhiyun *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26*4882a593Smuzhiyun *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27*4882a593Smuzhiyun *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28*4882a593Smuzhiyun *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29*4882a593Smuzhiyun *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*4882a593Smuzhiyun *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31*4882a593Smuzhiyun *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include <linux/file.h>
36*4882a593Smuzhiyun #include <linux/fs.h>
37*4882a593Smuzhiyun #include <linux/slab.h>
38*4882a593Smuzhiyun #include <linux/namei.h>
39*4882a593Smuzhiyun #include <linux/swap.h>
40*4882a593Smuzhiyun #include <linux/pagemap.h>
41*4882a593Smuzhiyun #include <linux/ratelimit.h>
42*4882a593Smuzhiyun #include <linux/sunrpc/svcauth_gss.h>
43*4882a593Smuzhiyun #include <linux/sunrpc/addr.h>
44*4882a593Smuzhiyun #include <linux/jhash.h>
45*4882a593Smuzhiyun #include <linux/string_helpers.h>
46*4882a593Smuzhiyun #include "xdr4.h"
47*4882a593Smuzhiyun #include "xdr4cb.h"
48*4882a593Smuzhiyun #include "vfs.h"
49*4882a593Smuzhiyun #include "current_stateid.h"
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun #include "netns.h"
52*4882a593Smuzhiyun #include "pnfs.h"
53*4882a593Smuzhiyun #include "filecache.h"
54*4882a593Smuzhiyun #include "trace.h"
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun #define NFSDDBG_FACILITY                NFSDDBG_PROC
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun #define all_ones {{~0,~0},~0}
59*4882a593Smuzhiyun static const stateid_t one_stateid = {
60*4882a593Smuzhiyun 	.si_generation = ~0,
61*4882a593Smuzhiyun 	.si_opaque = all_ones,
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun static const stateid_t zero_stateid = {
64*4882a593Smuzhiyun 	/* all fields zero */
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun static const stateid_t currentstateid = {
67*4882a593Smuzhiyun 	.si_generation = 1,
68*4882a593Smuzhiyun };
69*4882a593Smuzhiyun static const stateid_t close_stateid = {
70*4882a593Smuzhiyun 	.si_generation = 0xffffffffU,
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static u64 current_sessionid = 1;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76*4882a593Smuzhiyun #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
77*4882a593Smuzhiyun #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
78*4882a593Smuzhiyun #define CLOSE_STATEID(stateid)  (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /* forward declarations */
81*4882a593Smuzhiyun static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
82*4882a593Smuzhiyun static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
83*4882a593Smuzhiyun void nfsd4_end_grace(struct nfsd_net *nn);
84*4882a593Smuzhiyun static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun /* Locking: */
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * Currently used for the del_recall_lru and file hash table.  In an
90*4882a593Smuzhiyun  * effort to decrease the scope of the client_mutex, this spinlock may
91*4882a593Smuzhiyun  * eventually cover more:
92*4882a593Smuzhiyun  */
93*4882a593Smuzhiyun static DEFINE_SPINLOCK(state_lock);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun enum nfsd4_st_mutex_lock_subclass {
96*4882a593Smuzhiyun 	OPEN_STATEID_MUTEX = 0,
97*4882a593Smuzhiyun 	LOCK_STATEID_MUTEX = 1,
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun /*
101*4882a593Smuzhiyun  * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102*4882a593Smuzhiyun  * the refcount on the open stateid to drop.
103*4882a593Smuzhiyun  */
104*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun /*
107*4882a593Smuzhiyun  * A waitqueue where a writer to clients/#/ctl destroying a client can
108*4882a593Smuzhiyun  * wait for cl_rpc_users to drop to 0 and then for the client to be
109*4882a593Smuzhiyun  * unhashed.
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun static struct kmem_cache *client_slab;
114*4882a593Smuzhiyun static struct kmem_cache *openowner_slab;
115*4882a593Smuzhiyun static struct kmem_cache *lockowner_slab;
116*4882a593Smuzhiyun static struct kmem_cache *file_slab;
117*4882a593Smuzhiyun static struct kmem_cache *stateid_slab;
118*4882a593Smuzhiyun static struct kmem_cache *deleg_slab;
119*4882a593Smuzhiyun static struct kmem_cache *odstate_slab;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun static void free_session(struct nfsd4_session *);
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
124*4882a593Smuzhiyun static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
125*4882a593Smuzhiyun 
is_session_dead(struct nfsd4_session * ses)126*4882a593Smuzhiyun static bool is_session_dead(struct nfsd4_session *ses)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun 	return ses->se_flags & NFS4_SESSION_DEAD;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
mark_session_dead_locked(struct nfsd4_session * ses,int ref_held_by_me)131*4882a593Smuzhiyun static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	if (atomic_read(&ses->se_ref) > ref_held_by_me)
134*4882a593Smuzhiyun 		return nfserr_jukebox;
135*4882a593Smuzhiyun 	ses->se_flags |= NFS4_SESSION_DEAD;
136*4882a593Smuzhiyun 	return nfs_ok;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
is_client_expired(struct nfs4_client * clp)139*4882a593Smuzhiyun static bool is_client_expired(struct nfs4_client *clp)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	return clp->cl_time == 0;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
get_client_locked(struct nfs4_client * clp)144*4882a593Smuzhiyun static __be32 get_client_locked(struct nfs4_client *clp)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (is_client_expired(clp))
151*4882a593Smuzhiyun 		return nfserr_expired;
152*4882a593Smuzhiyun 	atomic_inc(&clp->cl_rpc_users);
153*4882a593Smuzhiyun 	return nfs_ok;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun /* must be called under the client_lock */
157*4882a593Smuzhiyun static inline void
renew_client_locked(struct nfs4_client * clp)158*4882a593Smuzhiyun renew_client_locked(struct nfs4_client *clp)
159*4882a593Smuzhiyun {
160*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (is_client_expired(clp)) {
163*4882a593Smuzhiyun 		WARN_ON(1);
164*4882a593Smuzhiyun 		printk("%s: client (clientid %08x/%08x) already expired\n",
165*4882a593Smuzhiyun 			__func__,
166*4882a593Smuzhiyun 			clp->cl_clientid.cl_boot,
167*4882a593Smuzhiyun 			clp->cl_clientid.cl_id);
168*4882a593Smuzhiyun 		return;
169*4882a593Smuzhiyun 	}
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	list_move_tail(&clp->cl_lru, &nn->client_lru);
172*4882a593Smuzhiyun 	clp->cl_time = ktime_get_boottime_seconds();
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
put_client_renew_locked(struct nfs4_client * clp)175*4882a593Smuzhiyun static void put_client_renew_locked(struct nfs4_client *clp)
176*4882a593Smuzhiyun {
177*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (!atomic_dec_and_test(&clp->cl_rpc_users))
182*4882a593Smuzhiyun 		return;
183*4882a593Smuzhiyun 	if (!is_client_expired(clp))
184*4882a593Smuzhiyun 		renew_client_locked(clp);
185*4882a593Smuzhiyun 	else
186*4882a593Smuzhiyun 		wake_up_all(&expiry_wq);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
put_client_renew(struct nfs4_client * clp)189*4882a593Smuzhiyun static void put_client_renew(struct nfs4_client *clp)
190*4882a593Smuzhiyun {
191*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
194*4882a593Smuzhiyun 		return;
195*4882a593Smuzhiyun 	if (!is_client_expired(clp))
196*4882a593Smuzhiyun 		renew_client_locked(clp);
197*4882a593Smuzhiyun 	else
198*4882a593Smuzhiyun 		wake_up_all(&expiry_wq);
199*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
nfsd4_get_session_locked(struct nfsd4_session * ses)202*4882a593Smuzhiyun static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	__be32 status;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	if (is_session_dead(ses))
207*4882a593Smuzhiyun 		return nfserr_badsession;
208*4882a593Smuzhiyun 	status = get_client_locked(ses->se_client);
209*4882a593Smuzhiyun 	if (status)
210*4882a593Smuzhiyun 		return status;
211*4882a593Smuzhiyun 	atomic_inc(&ses->se_ref);
212*4882a593Smuzhiyun 	return nfs_ok;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun 
nfsd4_put_session_locked(struct nfsd4_session * ses)215*4882a593Smuzhiyun static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
218*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 	if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223*4882a593Smuzhiyun 		free_session(ses);
224*4882a593Smuzhiyun 	put_client_renew_locked(clp);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
nfsd4_put_session(struct nfsd4_session * ses)227*4882a593Smuzhiyun static void nfsd4_put_session(struct nfsd4_session *ses)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
230*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
233*4882a593Smuzhiyun 	nfsd4_put_session_locked(ses);
234*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun static struct nfsd4_blocked_lock *
find_blocked_lock(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)238*4882a593Smuzhiyun find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
239*4882a593Smuzhiyun 			struct nfsd_net *nn)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct nfsd4_blocked_lock *cur, *found = NULL;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	spin_lock(&nn->blocked_locks_lock);
244*4882a593Smuzhiyun 	list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245*4882a593Smuzhiyun 		if (fh_match(fh, &cur->nbl_fh)) {
246*4882a593Smuzhiyun 			list_del_init(&cur->nbl_list);
247*4882a593Smuzhiyun 			list_del_init(&cur->nbl_lru);
248*4882a593Smuzhiyun 			found = cur;
249*4882a593Smuzhiyun 			break;
250*4882a593Smuzhiyun 		}
251*4882a593Smuzhiyun 	}
252*4882a593Smuzhiyun 	spin_unlock(&nn->blocked_locks_lock);
253*4882a593Smuzhiyun 	if (found)
254*4882a593Smuzhiyun 		locks_delete_block(&found->nbl_lock);
255*4882a593Smuzhiyun 	return found;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun static struct nfsd4_blocked_lock *
find_or_allocate_block(struct nfs4_lockowner * lo,struct knfsd_fh * fh,struct nfsd_net * nn)259*4882a593Smuzhiyun find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
260*4882a593Smuzhiyun 			struct nfsd_net *nn)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun 	struct nfsd4_blocked_lock *nbl;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	nbl = find_blocked_lock(lo, fh, nn);
265*4882a593Smuzhiyun 	if (!nbl) {
266*4882a593Smuzhiyun 		nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
267*4882a593Smuzhiyun 		if (nbl) {
268*4882a593Smuzhiyun 			INIT_LIST_HEAD(&nbl->nbl_list);
269*4882a593Smuzhiyun 			INIT_LIST_HEAD(&nbl->nbl_lru);
270*4882a593Smuzhiyun 			fh_copy_shallow(&nbl->nbl_fh, fh);
271*4882a593Smuzhiyun 			locks_init_lock(&nbl->nbl_lock);
272*4882a593Smuzhiyun 			nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
273*4882a593Smuzhiyun 					&nfsd4_cb_notify_lock_ops,
274*4882a593Smuzhiyun 					NFSPROC4_CLNT_CB_NOTIFY_LOCK);
275*4882a593Smuzhiyun 		}
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 	return nbl;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun static void
free_blocked_lock(struct nfsd4_blocked_lock * nbl)281*4882a593Smuzhiyun free_blocked_lock(struct nfsd4_blocked_lock *nbl)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun 	locks_delete_block(&nbl->nbl_lock);
284*4882a593Smuzhiyun 	locks_release_private(&nbl->nbl_lock);
285*4882a593Smuzhiyun 	kfree(nbl);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun static void
remove_blocked_locks(struct nfs4_lockowner * lo)289*4882a593Smuzhiyun remove_blocked_locks(struct nfs4_lockowner *lo)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	struct nfs4_client *clp = lo->lo_owner.so_client;
292*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
293*4882a593Smuzhiyun 	struct nfsd4_blocked_lock *nbl;
294*4882a593Smuzhiyun 	LIST_HEAD(reaplist);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* Dequeue all blocked locks */
297*4882a593Smuzhiyun 	spin_lock(&nn->blocked_locks_lock);
298*4882a593Smuzhiyun 	while (!list_empty(&lo->lo_blocked)) {
299*4882a593Smuzhiyun 		nbl = list_first_entry(&lo->lo_blocked,
300*4882a593Smuzhiyun 					struct nfsd4_blocked_lock,
301*4882a593Smuzhiyun 					nbl_list);
302*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_list);
303*4882a593Smuzhiyun 		list_move(&nbl->nbl_lru, &reaplist);
304*4882a593Smuzhiyun 	}
305*4882a593Smuzhiyun 	spin_unlock(&nn->blocked_locks_lock);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/* Now free them */
308*4882a593Smuzhiyun 	while (!list_empty(&reaplist)) {
309*4882a593Smuzhiyun 		nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
310*4882a593Smuzhiyun 					nbl_lru);
311*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_lru);
312*4882a593Smuzhiyun 		free_blocked_lock(nbl);
313*4882a593Smuzhiyun 	}
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun static void
nfsd4_cb_notify_lock_prepare(struct nfsd4_callback * cb)317*4882a593Smuzhiyun nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
320*4882a593Smuzhiyun 						struct nfsd4_blocked_lock, nbl_cb);
321*4882a593Smuzhiyun 	locks_delete_block(&nbl->nbl_lock);
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun static int
nfsd4_cb_notify_lock_done(struct nfsd4_callback * cb,struct rpc_task * task)325*4882a593Smuzhiyun nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun 	/*
328*4882a593Smuzhiyun 	 * Since this is just an optimization, we don't try very hard if it
329*4882a593Smuzhiyun 	 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330*4882a593Smuzhiyun 	 * just quit trying on anything else.
331*4882a593Smuzhiyun 	 */
332*4882a593Smuzhiyun 	switch (task->tk_status) {
333*4882a593Smuzhiyun 	case -NFS4ERR_DELAY:
334*4882a593Smuzhiyun 		rpc_delay(task, 1 * HZ);
335*4882a593Smuzhiyun 		return 0;
336*4882a593Smuzhiyun 	default:
337*4882a593Smuzhiyun 		return 1;
338*4882a593Smuzhiyun 	}
339*4882a593Smuzhiyun }
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun static void
nfsd4_cb_notify_lock_release(struct nfsd4_callback * cb)342*4882a593Smuzhiyun nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	struct nfsd4_blocked_lock	*nbl = container_of(cb,
345*4882a593Smuzhiyun 						struct nfsd4_blocked_lock, nbl_cb);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	free_blocked_lock(nbl);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
351*4882a593Smuzhiyun 	.prepare	= nfsd4_cb_notify_lock_prepare,
352*4882a593Smuzhiyun 	.done		= nfsd4_cb_notify_lock_done,
353*4882a593Smuzhiyun 	.release	= nfsd4_cb_notify_lock_release,
354*4882a593Smuzhiyun };
355*4882a593Smuzhiyun 
356*4882a593Smuzhiyun static inline struct nfs4_stateowner *
nfs4_get_stateowner(struct nfs4_stateowner * sop)357*4882a593Smuzhiyun nfs4_get_stateowner(struct nfs4_stateowner *sop)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	atomic_inc(&sop->so_count);
360*4882a593Smuzhiyun 	return sop;
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun static int
same_owner_str(struct nfs4_stateowner * sop,struct xdr_netobj * owner)364*4882a593Smuzhiyun same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
365*4882a593Smuzhiyun {
366*4882a593Smuzhiyun 	return (sop->so_owner.len == owner->len) &&
367*4882a593Smuzhiyun 		0 == memcmp(sop->so_owner.data, owner->data, owner->len);
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun static struct nfs4_openowner *
find_openstateowner_str_locked(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)371*4882a593Smuzhiyun find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
372*4882a593Smuzhiyun 			struct nfs4_client *clp)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	struct nfs4_stateowner *so;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
379*4882a593Smuzhiyun 			    so_strhash) {
380*4882a593Smuzhiyun 		if (!so->so_is_open_owner)
381*4882a593Smuzhiyun 			continue;
382*4882a593Smuzhiyun 		if (same_owner_str(so, &open->op_owner))
383*4882a593Smuzhiyun 			return openowner(nfs4_get_stateowner(so));
384*4882a593Smuzhiyun 	}
385*4882a593Smuzhiyun 	return NULL;
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun static struct nfs4_openowner *
find_openstateowner_str(unsigned int hashval,struct nfsd4_open * open,struct nfs4_client * clp)389*4882a593Smuzhiyun find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
390*4882a593Smuzhiyun 			struct nfs4_client *clp)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
395*4882a593Smuzhiyun 	oo = find_openstateowner_str_locked(hashval, open, clp);
396*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
397*4882a593Smuzhiyun 	return oo;
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun static inline u32
opaque_hashval(const void * ptr,int nbytes)401*4882a593Smuzhiyun opaque_hashval(const void *ptr, int nbytes)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	unsigned char *cptr = (unsigned char *) ptr;
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	u32 x = 0;
406*4882a593Smuzhiyun 	while (nbytes--) {
407*4882a593Smuzhiyun 		x *= 37;
408*4882a593Smuzhiyun 		x += *cptr++;
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun 	return x;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun 
nfsd4_free_file_rcu(struct rcu_head * rcu)413*4882a593Smuzhiyun static void nfsd4_free_file_rcu(struct rcu_head *rcu)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	kmem_cache_free(file_slab, fp);
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun void
put_nfs4_file(struct nfs4_file * fi)421*4882a593Smuzhiyun put_nfs4_file(struct nfs4_file *fi)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	might_lock(&state_lock);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
426*4882a593Smuzhiyun 		hlist_del_rcu(&fi->fi_hash);
427*4882a593Smuzhiyun 		spin_unlock(&state_lock);
428*4882a593Smuzhiyun 		WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
429*4882a593Smuzhiyun 		WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
430*4882a593Smuzhiyun 		call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun static struct nfsd_file *
__nfs4_get_fd(struct nfs4_file * f,int oflag)435*4882a593Smuzhiyun __nfs4_get_fd(struct nfs4_file *f, int oflag)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	if (f->fi_fds[oflag])
438*4882a593Smuzhiyun 		return nfsd_file_get(f->fi_fds[oflag]);
439*4882a593Smuzhiyun 	return NULL;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun static struct nfsd_file *
find_writeable_file_locked(struct nfs4_file * f)443*4882a593Smuzhiyun find_writeable_file_locked(struct nfs4_file *f)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct nfsd_file *ret;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	lockdep_assert_held(&f->fi_lock);
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	ret = __nfs4_get_fd(f, O_WRONLY);
450*4882a593Smuzhiyun 	if (!ret)
451*4882a593Smuzhiyun 		ret = __nfs4_get_fd(f, O_RDWR);
452*4882a593Smuzhiyun 	return ret;
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun static struct nfsd_file *
find_writeable_file(struct nfs4_file * f)456*4882a593Smuzhiyun find_writeable_file(struct nfs4_file *f)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun 	struct nfsd_file *ret;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	spin_lock(&f->fi_lock);
461*4882a593Smuzhiyun 	ret = find_writeable_file_locked(f);
462*4882a593Smuzhiyun 	spin_unlock(&f->fi_lock);
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	return ret;
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun static struct nfsd_file *
find_readable_file_locked(struct nfs4_file * f)468*4882a593Smuzhiyun find_readable_file_locked(struct nfs4_file *f)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	struct nfsd_file *ret;
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	lockdep_assert_held(&f->fi_lock);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	ret = __nfs4_get_fd(f, O_RDONLY);
475*4882a593Smuzhiyun 	if (!ret)
476*4882a593Smuzhiyun 		ret = __nfs4_get_fd(f, O_RDWR);
477*4882a593Smuzhiyun 	return ret;
478*4882a593Smuzhiyun }
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun static struct nfsd_file *
find_readable_file(struct nfs4_file * f)481*4882a593Smuzhiyun find_readable_file(struct nfs4_file *f)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	struct nfsd_file *ret;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	spin_lock(&f->fi_lock);
486*4882a593Smuzhiyun 	ret = find_readable_file_locked(f);
487*4882a593Smuzhiyun 	spin_unlock(&f->fi_lock);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	return ret;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun struct nfsd_file *
find_any_file(struct nfs4_file * f)493*4882a593Smuzhiyun find_any_file(struct nfs4_file *f)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun 	struct nfsd_file *ret;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	if (!f)
498*4882a593Smuzhiyun 		return NULL;
499*4882a593Smuzhiyun 	spin_lock(&f->fi_lock);
500*4882a593Smuzhiyun 	ret = __nfs4_get_fd(f, O_RDWR);
501*4882a593Smuzhiyun 	if (!ret) {
502*4882a593Smuzhiyun 		ret = __nfs4_get_fd(f, O_WRONLY);
503*4882a593Smuzhiyun 		if (!ret)
504*4882a593Smuzhiyun 			ret = __nfs4_get_fd(f, O_RDONLY);
505*4882a593Smuzhiyun 	}
506*4882a593Smuzhiyun 	spin_unlock(&f->fi_lock);
507*4882a593Smuzhiyun 	return ret;
508*4882a593Smuzhiyun }
509*4882a593Smuzhiyun 
find_deleg_file(struct nfs4_file * f)510*4882a593Smuzhiyun static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	struct nfsd_file *ret = NULL;
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 	spin_lock(&f->fi_lock);
515*4882a593Smuzhiyun 	if (f->fi_deleg_file)
516*4882a593Smuzhiyun 		ret = nfsd_file_get(f->fi_deleg_file);
517*4882a593Smuzhiyun 	spin_unlock(&f->fi_lock);
518*4882a593Smuzhiyun 	return ret;
519*4882a593Smuzhiyun }
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun static atomic_long_t num_delegations;
522*4882a593Smuzhiyun unsigned long max_delegations;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun /*
525*4882a593Smuzhiyun  * Open owner state (share locks)
526*4882a593Smuzhiyun  */
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun /* hash tables for lock and open owners */
529*4882a593Smuzhiyun #define OWNER_HASH_BITS              8
530*4882a593Smuzhiyun #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
531*4882a593Smuzhiyun #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
532*4882a593Smuzhiyun 
ownerstr_hashval(struct xdr_netobj * ownername)533*4882a593Smuzhiyun static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	unsigned int ret;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	ret = opaque_hashval(ownername->data, ownername->len);
538*4882a593Smuzhiyun 	return ret & OWNER_HASH_MASK;
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun /* hash table for nfs4_file */
542*4882a593Smuzhiyun #define FILE_HASH_BITS                   8
543*4882a593Smuzhiyun #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
544*4882a593Smuzhiyun 
nfsd_fh_hashval(struct knfsd_fh * fh)545*4882a593Smuzhiyun static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
548*4882a593Smuzhiyun }
549*4882a593Smuzhiyun 
file_hashval(struct knfsd_fh * fh)550*4882a593Smuzhiyun static unsigned int file_hashval(struct knfsd_fh *fh)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun static void
__nfs4_file_get_access(struct nfs4_file * fp,u32 access)558*4882a593Smuzhiyun __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	if (access & NFS4_SHARE_ACCESS_WRITE)
563*4882a593Smuzhiyun 		atomic_inc(&fp->fi_access[O_WRONLY]);
564*4882a593Smuzhiyun 	if (access & NFS4_SHARE_ACCESS_READ)
565*4882a593Smuzhiyun 		atomic_inc(&fp->fi_access[O_RDONLY]);
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun static __be32
nfs4_file_get_access(struct nfs4_file * fp,u32 access)569*4882a593Smuzhiyun nfs4_file_get_access(struct nfs4_file *fp, u32 access)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	/* Does this access mode make sense? */
574*4882a593Smuzhiyun 	if (access & ~NFS4_SHARE_ACCESS_BOTH)
575*4882a593Smuzhiyun 		return nfserr_inval;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	/* Does it conflict with a deny mode already set? */
578*4882a593Smuzhiyun 	if ((access & fp->fi_share_deny) != 0)
579*4882a593Smuzhiyun 		return nfserr_share_denied;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	__nfs4_file_get_access(fp, access);
582*4882a593Smuzhiyun 	return nfs_ok;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
nfs4_file_check_deny(struct nfs4_file * fp,u32 deny)585*4882a593Smuzhiyun static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	/* Common case is that there is no deny mode. */
588*4882a593Smuzhiyun 	if (deny) {
589*4882a593Smuzhiyun 		/* Does this deny mode make sense? */
590*4882a593Smuzhiyun 		if (deny & ~NFS4_SHARE_DENY_BOTH)
591*4882a593Smuzhiyun 			return nfserr_inval;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 		if ((deny & NFS4_SHARE_DENY_READ) &&
594*4882a593Smuzhiyun 		    atomic_read(&fp->fi_access[O_RDONLY]))
595*4882a593Smuzhiyun 			return nfserr_share_denied;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		if ((deny & NFS4_SHARE_DENY_WRITE) &&
598*4882a593Smuzhiyun 		    atomic_read(&fp->fi_access[O_WRONLY]))
599*4882a593Smuzhiyun 			return nfserr_share_denied;
600*4882a593Smuzhiyun 	}
601*4882a593Smuzhiyun 	return nfs_ok;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun 
__nfs4_file_put_access(struct nfs4_file * fp,int oflag)604*4882a593Smuzhiyun static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun 	might_lock(&fp->fi_lock);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
609*4882a593Smuzhiyun 		struct nfsd_file *f1 = NULL;
610*4882a593Smuzhiyun 		struct nfsd_file *f2 = NULL;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 		swap(f1, fp->fi_fds[oflag]);
613*4882a593Smuzhiyun 		if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
614*4882a593Smuzhiyun 			swap(f2, fp->fi_fds[O_RDWR]);
615*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
616*4882a593Smuzhiyun 		if (f1)
617*4882a593Smuzhiyun 			nfsd_file_put(f1);
618*4882a593Smuzhiyun 		if (f2)
619*4882a593Smuzhiyun 			nfsd_file_put(f2);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
nfs4_file_put_access(struct nfs4_file * fp,u32 access)623*4882a593Smuzhiyun static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
624*4882a593Smuzhiyun {
625*4882a593Smuzhiyun 	WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	if (access & NFS4_SHARE_ACCESS_WRITE)
628*4882a593Smuzhiyun 		__nfs4_file_put_access(fp, O_WRONLY);
629*4882a593Smuzhiyun 	if (access & NFS4_SHARE_ACCESS_READ)
630*4882a593Smuzhiyun 		__nfs4_file_put_access(fp, O_RDONLY);
631*4882a593Smuzhiyun }
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun /*
634*4882a593Smuzhiyun  * Allocate a new open/delegation state counter. This is needed for
635*4882a593Smuzhiyun  * pNFS for proper return on close semantics.
636*4882a593Smuzhiyun  *
637*4882a593Smuzhiyun  * Note that we only allocate it for pNFS-enabled exports, otherwise
638*4882a593Smuzhiyun  * all pointers to struct nfs4_clnt_odstate are always NULL.
639*4882a593Smuzhiyun  */
640*4882a593Smuzhiyun static struct nfs4_clnt_odstate *
alloc_clnt_odstate(struct nfs4_client * clp)641*4882a593Smuzhiyun alloc_clnt_odstate(struct nfs4_client *clp)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun 	struct nfs4_clnt_odstate *co;
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
646*4882a593Smuzhiyun 	if (co) {
647*4882a593Smuzhiyun 		co->co_client = clp;
648*4882a593Smuzhiyun 		refcount_set(&co->co_odcount, 1);
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 	return co;
651*4882a593Smuzhiyun }
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun static void
hash_clnt_odstate_locked(struct nfs4_clnt_odstate * co)654*4882a593Smuzhiyun hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
655*4882a593Smuzhiyun {
656*4882a593Smuzhiyun 	struct nfs4_file *fp = co->co_file;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
659*4882a593Smuzhiyun 	list_add(&co->co_perfile, &fp->fi_clnt_odstate);
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun static inline void
get_clnt_odstate(struct nfs4_clnt_odstate * co)663*4882a593Smuzhiyun get_clnt_odstate(struct nfs4_clnt_odstate *co)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	if (co)
666*4882a593Smuzhiyun 		refcount_inc(&co->co_odcount);
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun static void
put_clnt_odstate(struct nfs4_clnt_odstate * co)670*4882a593Smuzhiyun put_clnt_odstate(struct nfs4_clnt_odstate *co)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun 	struct nfs4_file *fp;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	if (!co)
675*4882a593Smuzhiyun 		return;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	fp = co->co_file;
678*4882a593Smuzhiyun 	if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
679*4882a593Smuzhiyun 		list_del(&co->co_perfile);
680*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 		nfsd4_return_all_file_layouts(co->co_client, fp);
683*4882a593Smuzhiyun 		kmem_cache_free(odstate_slab, co);
684*4882a593Smuzhiyun 	}
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun static struct nfs4_clnt_odstate *
find_or_hash_clnt_odstate(struct nfs4_file * fp,struct nfs4_clnt_odstate * new)688*4882a593Smuzhiyun find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
689*4882a593Smuzhiyun {
690*4882a593Smuzhiyun 	struct nfs4_clnt_odstate *co;
691*4882a593Smuzhiyun 	struct nfs4_client *cl;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	if (!new)
694*4882a593Smuzhiyun 		return NULL;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	cl = new->co_client;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
699*4882a593Smuzhiyun 	list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
700*4882a593Smuzhiyun 		if (co->co_client == cl) {
701*4882a593Smuzhiyun 			get_clnt_odstate(co);
702*4882a593Smuzhiyun 			goto out;
703*4882a593Smuzhiyun 		}
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 	co = new;
706*4882a593Smuzhiyun 	co->co_file = fp;
707*4882a593Smuzhiyun 	hash_clnt_odstate_locked(new);
708*4882a593Smuzhiyun out:
709*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
710*4882a593Smuzhiyun 	return co;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
nfs4_alloc_stid(struct nfs4_client * cl,struct kmem_cache * slab,void (* sc_free)(struct nfs4_stid *))713*4882a593Smuzhiyun struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
714*4882a593Smuzhiyun 				  void (*sc_free)(struct nfs4_stid *))
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct nfs4_stid *stid;
717*4882a593Smuzhiyun 	int new_id;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	stid = kmem_cache_zalloc(slab, GFP_KERNEL);
720*4882a593Smuzhiyun 	if (!stid)
721*4882a593Smuzhiyun 		return NULL;
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	idr_preload(GFP_KERNEL);
724*4882a593Smuzhiyun 	spin_lock(&cl->cl_lock);
725*4882a593Smuzhiyun 	/* Reserving 0 for start of file in nfsdfs "states" file: */
726*4882a593Smuzhiyun 	new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
727*4882a593Smuzhiyun 	spin_unlock(&cl->cl_lock);
728*4882a593Smuzhiyun 	idr_preload_end();
729*4882a593Smuzhiyun 	if (new_id < 0)
730*4882a593Smuzhiyun 		goto out_free;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	stid->sc_free = sc_free;
733*4882a593Smuzhiyun 	stid->sc_client = cl;
734*4882a593Smuzhiyun 	stid->sc_stateid.si_opaque.so_id = new_id;
735*4882a593Smuzhiyun 	stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
736*4882a593Smuzhiyun 	/* Will be incremented before return to client: */
737*4882a593Smuzhiyun 	refcount_set(&stid->sc_count, 1);
738*4882a593Smuzhiyun 	spin_lock_init(&stid->sc_lock);
739*4882a593Smuzhiyun 	INIT_LIST_HEAD(&stid->sc_cp_list);
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun 	/*
742*4882a593Smuzhiyun 	 * It shouldn't be a problem to reuse an opaque stateid value.
743*4882a593Smuzhiyun 	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
744*4882a593Smuzhiyun 	 * example, a stray write retransmission could be accepted by
745*4882a593Smuzhiyun 	 * the server when it should have been rejected.  Therefore,
746*4882a593Smuzhiyun 	 * adopt a trick from the sctp code to attempt to maximize the
747*4882a593Smuzhiyun 	 * amount of time until an id is reused, by ensuring they always
748*4882a593Smuzhiyun 	 * "increase" (mod INT_MAX):
749*4882a593Smuzhiyun 	 */
750*4882a593Smuzhiyun 	return stid;
751*4882a593Smuzhiyun out_free:
752*4882a593Smuzhiyun 	kmem_cache_free(slab, stid);
753*4882a593Smuzhiyun 	return NULL;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun /*
757*4882a593Smuzhiyun  * Create a unique stateid_t to represent each COPY.
758*4882a593Smuzhiyun  */
nfs4_init_cp_state(struct nfsd_net * nn,copy_stateid_t * stid,unsigned char sc_type)759*4882a593Smuzhiyun static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
760*4882a593Smuzhiyun 			      unsigned char sc_type)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun 	int new_id;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
765*4882a593Smuzhiyun 	stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
766*4882a593Smuzhiyun 	stid->sc_type = sc_type;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	idr_preload(GFP_KERNEL);
769*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
770*4882a593Smuzhiyun 	new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
771*4882a593Smuzhiyun 	stid->stid.si_opaque.so_id = new_id;
772*4882a593Smuzhiyun 	stid->stid.si_generation = 1;
773*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
774*4882a593Smuzhiyun 	idr_preload_end();
775*4882a593Smuzhiyun 	if (new_id < 0)
776*4882a593Smuzhiyun 		return 0;
777*4882a593Smuzhiyun 	return 1;
778*4882a593Smuzhiyun }
779*4882a593Smuzhiyun 
nfs4_init_copy_state(struct nfsd_net * nn,struct nfsd4_copy * copy)780*4882a593Smuzhiyun int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
781*4882a593Smuzhiyun {
782*4882a593Smuzhiyun 	return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
783*4882a593Smuzhiyun }
784*4882a593Smuzhiyun 
nfs4_alloc_init_cpntf_state(struct nfsd_net * nn,struct nfs4_stid * p_stid)785*4882a593Smuzhiyun struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
786*4882a593Smuzhiyun 						     struct nfs4_stid *p_stid)
787*4882a593Smuzhiyun {
788*4882a593Smuzhiyun 	struct nfs4_cpntf_state *cps;
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
791*4882a593Smuzhiyun 	if (!cps)
792*4882a593Smuzhiyun 		return NULL;
793*4882a593Smuzhiyun 	cps->cpntf_time = ktime_get_boottime_seconds();
794*4882a593Smuzhiyun 	refcount_set(&cps->cp_stateid.sc_count, 1);
795*4882a593Smuzhiyun 	if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
796*4882a593Smuzhiyun 		goto out_free;
797*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
798*4882a593Smuzhiyun 	list_add(&cps->cp_list, &p_stid->sc_cp_list);
799*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
800*4882a593Smuzhiyun 	return cps;
801*4882a593Smuzhiyun out_free:
802*4882a593Smuzhiyun 	kfree(cps);
803*4882a593Smuzhiyun 	return NULL;
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun 
nfs4_free_copy_state(struct nfsd4_copy * copy)806*4882a593Smuzhiyun void nfs4_free_copy_state(struct nfsd4_copy *copy)
807*4882a593Smuzhiyun {
808*4882a593Smuzhiyun 	struct nfsd_net *nn;
809*4882a593Smuzhiyun 
810*4882a593Smuzhiyun 	WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
811*4882a593Smuzhiyun 	nn = net_generic(copy->cp_clp->net, nfsd_net_id);
812*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
813*4882a593Smuzhiyun 	idr_remove(&nn->s2s_cp_stateids,
814*4882a593Smuzhiyun 		   copy->cp_stateid.stid.si_opaque.so_id);
815*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
816*4882a593Smuzhiyun }
817*4882a593Smuzhiyun 
nfs4_free_cpntf_statelist(struct net * net,struct nfs4_stid * stid)818*4882a593Smuzhiyun static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct nfs4_cpntf_state *cps;
821*4882a593Smuzhiyun 	struct nfsd_net *nn;
822*4882a593Smuzhiyun 
823*4882a593Smuzhiyun 	nn = net_generic(net, nfsd_net_id);
824*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
825*4882a593Smuzhiyun 	while (!list_empty(&stid->sc_cp_list)) {
826*4882a593Smuzhiyun 		cps = list_first_entry(&stid->sc_cp_list,
827*4882a593Smuzhiyun 				       struct nfs4_cpntf_state, cp_list);
828*4882a593Smuzhiyun 		_free_cpntf_state_locked(nn, cps);
829*4882a593Smuzhiyun 	}
830*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
nfs4_alloc_open_stateid(struct nfs4_client * clp)833*4882a593Smuzhiyun static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	struct nfs4_stid *stid;
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 	stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
838*4882a593Smuzhiyun 	if (!stid)
839*4882a593Smuzhiyun 		return NULL;
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return openlockstateid(stid);
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
nfs4_free_deleg(struct nfs4_stid * stid)844*4882a593Smuzhiyun static void nfs4_free_deleg(struct nfs4_stid *stid)
845*4882a593Smuzhiyun {
846*4882a593Smuzhiyun 	WARN_ON(!list_empty(&stid->sc_cp_list));
847*4882a593Smuzhiyun 	kmem_cache_free(deleg_slab, stid);
848*4882a593Smuzhiyun 	atomic_long_dec(&num_delegations);
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun /*
852*4882a593Smuzhiyun  * When we recall a delegation, we should be careful not to hand it
853*4882a593Smuzhiyun  * out again straight away.
854*4882a593Smuzhiyun  * To ensure this we keep a pair of bloom filters ('new' and 'old')
855*4882a593Smuzhiyun  * in which the filehandles of recalled delegations are "stored".
856*4882a593Smuzhiyun  * If a filehandle appear in either filter, a delegation is blocked.
857*4882a593Smuzhiyun  * When a delegation is recalled, the filehandle is stored in the "new"
858*4882a593Smuzhiyun  * filter.
859*4882a593Smuzhiyun  * Every 30 seconds we swap the filters and clear the "new" one,
860*4882a593Smuzhiyun  * unless both are empty of course.
861*4882a593Smuzhiyun  *
862*4882a593Smuzhiyun  * Each filter is 256 bits.  We hash the filehandle to 32bit and use the
863*4882a593Smuzhiyun  * low 3 bytes as hash-table indices.
864*4882a593Smuzhiyun  *
865*4882a593Smuzhiyun  * 'blocked_delegations_lock', which is always taken in block_delegations(),
866*4882a593Smuzhiyun  * is used to manage concurrent access.  Testing does not need the lock
867*4882a593Smuzhiyun  * except when swapping the two filters.
868*4882a593Smuzhiyun  */
869*4882a593Smuzhiyun static DEFINE_SPINLOCK(blocked_delegations_lock);
870*4882a593Smuzhiyun static struct bloom_pair {
871*4882a593Smuzhiyun 	int	entries, old_entries;
872*4882a593Smuzhiyun 	time64_t swap_time;
873*4882a593Smuzhiyun 	int	new; /* index into 'set' */
874*4882a593Smuzhiyun 	DECLARE_BITMAP(set[2], 256);
875*4882a593Smuzhiyun } blocked_delegations;
876*4882a593Smuzhiyun 
delegation_blocked(struct knfsd_fh * fh)877*4882a593Smuzhiyun static int delegation_blocked(struct knfsd_fh *fh)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	u32 hash;
880*4882a593Smuzhiyun 	struct bloom_pair *bd = &blocked_delegations;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	if (bd->entries == 0)
883*4882a593Smuzhiyun 		return 0;
884*4882a593Smuzhiyun 	if (ktime_get_seconds() - bd->swap_time > 30) {
885*4882a593Smuzhiyun 		spin_lock(&blocked_delegations_lock);
886*4882a593Smuzhiyun 		if (ktime_get_seconds() - bd->swap_time > 30) {
887*4882a593Smuzhiyun 			bd->entries -= bd->old_entries;
888*4882a593Smuzhiyun 			bd->old_entries = bd->entries;
889*4882a593Smuzhiyun 			memset(bd->set[bd->new], 0,
890*4882a593Smuzhiyun 			       sizeof(bd->set[0]));
891*4882a593Smuzhiyun 			bd->new = 1-bd->new;
892*4882a593Smuzhiyun 			bd->swap_time = ktime_get_seconds();
893*4882a593Smuzhiyun 		}
894*4882a593Smuzhiyun 		spin_unlock(&blocked_delegations_lock);
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
897*4882a593Smuzhiyun 	if (test_bit(hash&255, bd->set[0]) &&
898*4882a593Smuzhiyun 	    test_bit((hash>>8)&255, bd->set[0]) &&
899*4882a593Smuzhiyun 	    test_bit((hash>>16)&255, bd->set[0]))
900*4882a593Smuzhiyun 		return 1;
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (test_bit(hash&255, bd->set[1]) &&
903*4882a593Smuzhiyun 	    test_bit((hash>>8)&255, bd->set[1]) &&
904*4882a593Smuzhiyun 	    test_bit((hash>>16)&255, bd->set[1]))
905*4882a593Smuzhiyun 		return 1;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	return 0;
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun 
block_delegations(struct knfsd_fh * fh)910*4882a593Smuzhiyun static void block_delegations(struct knfsd_fh *fh)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	u32 hash;
913*4882a593Smuzhiyun 	struct bloom_pair *bd = &blocked_delegations;
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun 	hash = jhash(&fh->fh_base, fh->fh_size, 0);
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	spin_lock(&blocked_delegations_lock);
918*4882a593Smuzhiyun 	__set_bit(hash&255, bd->set[bd->new]);
919*4882a593Smuzhiyun 	__set_bit((hash>>8)&255, bd->set[bd->new]);
920*4882a593Smuzhiyun 	__set_bit((hash>>16)&255, bd->set[bd->new]);
921*4882a593Smuzhiyun 	if (bd->entries == 0)
922*4882a593Smuzhiyun 		bd->swap_time = ktime_get_seconds();
923*4882a593Smuzhiyun 	bd->entries += 1;
924*4882a593Smuzhiyun 	spin_unlock(&blocked_delegations_lock);
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun static struct nfs4_delegation *
alloc_init_deleg(struct nfs4_client * clp,struct nfs4_file * fp,struct svc_fh * current_fh,struct nfs4_clnt_odstate * odstate)928*4882a593Smuzhiyun alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
929*4882a593Smuzhiyun 		 struct svc_fh *current_fh,
930*4882a593Smuzhiyun 		 struct nfs4_clnt_odstate *odstate)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
933*4882a593Smuzhiyun 	long n;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	dprintk("NFSD alloc_init_deleg\n");
936*4882a593Smuzhiyun 	n = atomic_long_inc_return(&num_delegations);
937*4882a593Smuzhiyun 	if (n < 0 || n > max_delegations)
938*4882a593Smuzhiyun 		goto out_dec;
939*4882a593Smuzhiyun 	if (delegation_blocked(&current_fh->fh_handle))
940*4882a593Smuzhiyun 		goto out_dec;
941*4882a593Smuzhiyun 	dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
942*4882a593Smuzhiyun 	if (dp == NULL)
943*4882a593Smuzhiyun 		goto out_dec;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/*
946*4882a593Smuzhiyun 	 * delegation seqid's are never incremented.  The 4.1 special
947*4882a593Smuzhiyun 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
948*4882a593Smuzhiyun 	 * 0 anyway just for consistency and use 1:
949*4882a593Smuzhiyun 	 */
950*4882a593Smuzhiyun 	dp->dl_stid.sc_stateid.si_generation = 1;
951*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dp->dl_perfile);
952*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dp->dl_perclnt);
953*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dp->dl_recall_lru);
954*4882a593Smuzhiyun 	dp->dl_clnt_odstate = odstate;
955*4882a593Smuzhiyun 	get_clnt_odstate(odstate);
956*4882a593Smuzhiyun 	dp->dl_type = NFS4_OPEN_DELEGATE_READ;
957*4882a593Smuzhiyun 	dp->dl_retries = 1;
958*4882a593Smuzhiyun 	nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
959*4882a593Smuzhiyun 		      &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
960*4882a593Smuzhiyun 	get_nfs4_file(fp);
961*4882a593Smuzhiyun 	dp->dl_stid.sc_file = fp;
962*4882a593Smuzhiyun 	return dp;
963*4882a593Smuzhiyun out_dec:
964*4882a593Smuzhiyun 	atomic_long_dec(&num_delegations);
965*4882a593Smuzhiyun 	return NULL;
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun void
nfs4_put_stid(struct nfs4_stid * s)969*4882a593Smuzhiyun nfs4_put_stid(struct nfs4_stid *s)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun 	struct nfs4_file *fp = s->sc_file;
972*4882a593Smuzhiyun 	struct nfs4_client *clp = s->sc_client;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	might_lock(&clp->cl_lock);
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
977*4882a593Smuzhiyun 		wake_up_all(&close_wq);
978*4882a593Smuzhiyun 		return;
979*4882a593Smuzhiyun 	}
980*4882a593Smuzhiyun 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
981*4882a593Smuzhiyun 	nfs4_free_cpntf_statelist(clp->net, s);
982*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
983*4882a593Smuzhiyun 	s->sc_free(s);
984*4882a593Smuzhiyun 	if (fp)
985*4882a593Smuzhiyun 		put_nfs4_file(fp);
986*4882a593Smuzhiyun }
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun void
nfs4_inc_and_copy_stateid(stateid_t * dst,struct nfs4_stid * stid)989*4882a593Smuzhiyun nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	stateid_t *src = &stid->sc_stateid;
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	spin_lock(&stid->sc_lock);
994*4882a593Smuzhiyun 	if (unlikely(++src->si_generation == 0))
995*4882a593Smuzhiyun 		src->si_generation = 1;
996*4882a593Smuzhiyun 	memcpy(dst, src, sizeof(*dst));
997*4882a593Smuzhiyun 	spin_unlock(&stid->sc_lock);
998*4882a593Smuzhiyun }
999*4882a593Smuzhiyun 
put_deleg_file(struct nfs4_file * fp)1000*4882a593Smuzhiyun static void put_deleg_file(struct nfs4_file *fp)
1001*4882a593Smuzhiyun {
1002*4882a593Smuzhiyun 	struct nfsd_file *nf = NULL;
1003*4882a593Smuzhiyun 
1004*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
1005*4882a593Smuzhiyun 	if (--fp->fi_delegees == 0)
1006*4882a593Smuzhiyun 		swap(nf, fp->fi_deleg_file);
1007*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (nf)
1010*4882a593Smuzhiyun 		nfsd_file_put(nf);
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun 
nfs4_unlock_deleg_lease(struct nfs4_delegation * dp)1013*4882a593Smuzhiyun static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1014*4882a593Smuzhiyun {
1015*4882a593Smuzhiyun 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1016*4882a593Smuzhiyun 	struct nfsd_file *nf = fp->fi_deleg_file;
1017*4882a593Smuzhiyun 
1018*4882a593Smuzhiyun 	WARN_ON_ONCE(!fp->fi_delegees);
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 	vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
1021*4882a593Smuzhiyun 	put_deleg_file(fp);
1022*4882a593Smuzhiyun }
1023*4882a593Smuzhiyun 
destroy_unhashed_deleg(struct nfs4_delegation * dp)1024*4882a593Smuzhiyun static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1025*4882a593Smuzhiyun {
1026*4882a593Smuzhiyun 	put_clnt_odstate(dp->dl_clnt_odstate);
1027*4882a593Smuzhiyun 	nfs4_unlock_deleg_lease(dp);
1028*4882a593Smuzhiyun 	nfs4_put_stid(&dp->dl_stid);
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun 
nfs4_unhash_stid(struct nfs4_stid * s)1031*4882a593Smuzhiyun void nfs4_unhash_stid(struct nfs4_stid *s)
1032*4882a593Smuzhiyun {
1033*4882a593Smuzhiyun 	s->sc_type = 0;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun 
1036*4882a593Smuzhiyun /**
1037*4882a593Smuzhiyun  * nfs4_delegation_exists - Discover if this delegation already exists
1038*4882a593Smuzhiyun  * @clp:     a pointer to the nfs4_client we're granting a delegation to
1039*4882a593Smuzhiyun  * @fp:      a pointer to the nfs4_file we're granting a delegation on
1040*4882a593Smuzhiyun  *
1041*4882a593Smuzhiyun  * Return:
1042*4882a593Smuzhiyun  *      On success: true iff an existing delegation is found
1043*4882a593Smuzhiyun  */
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun static bool
nfs4_delegation_exists(struct nfs4_client * clp,struct nfs4_file * fp)1046*4882a593Smuzhiyun nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
1047*4882a593Smuzhiyun {
1048*4882a593Smuzhiyun 	struct nfs4_delegation *searchdp = NULL;
1049*4882a593Smuzhiyun 	struct nfs4_client *searchclp = NULL;
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun 	lockdep_assert_held(&state_lock);
1052*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
1053*4882a593Smuzhiyun 
1054*4882a593Smuzhiyun 	list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1055*4882a593Smuzhiyun 		searchclp = searchdp->dl_stid.sc_client;
1056*4882a593Smuzhiyun 		if (clp == searchclp) {
1057*4882a593Smuzhiyun 			return true;
1058*4882a593Smuzhiyun 		}
1059*4882a593Smuzhiyun 	}
1060*4882a593Smuzhiyun 	return false;
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun /**
1064*4882a593Smuzhiyun  * hash_delegation_locked - Add a delegation to the appropriate lists
1065*4882a593Smuzhiyun  * @dp:     a pointer to the nfs4_delegation we are adding.
1066*4882a593Smuzhiyun  * @fp:     a pointer to the nfs4_file we're granting a delegation on
1067*4882a593Smuzhiyun  *
1068*4882a593Smuzhiyun  * Return:
1069*4882a593Smuzhiyun  *      On success: NULL if the delegation was successfully hashed.
1070*4882a593Smuzhiyun  *
1071*4882a593Smuzhiyun  *      On error: -EAGAIN if one was previously granted to this
1072*4882a593Smuzhiyun  *                 nfs4_client for this nfs4_file. Delegation is not hashed.
1073*4882a593Smuzhiyun  *
1074*4882a593Smuzhiyun  */
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun static int
hash_delegation_locked(struct nfs4_delegation * dp,struct nfs4_file * fp)1077*4882a593Smuzhiyun hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1078*4882a593Smuzhiyun {
1079*4882a593Smuzhiyun 	struct nfs4_client *clp = dp->dl_stid.sc_client;
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	lockdep_assert_held(&state_lock);
1082*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	if (nfs4_delegation_exists(clp, fp))
1085*4882a593Smuzhiyun 		return -EAGAIN;
1086*4882a593Smuzhiyun 	refcount_inc(&dp->dl_stid.sc_count);
1087*4882a593Smuzhiyun 	dp->dl_stid.sc_type = NFS4_DELEG_STID;
1088*4882a593Smuzhiyun 	list_add(&dp->dl_perfile, &fp->fi_delegations);
1089*4882a593Smuzhiyun 	list_add(&dp->dl_perclnt, &clp->cl_delegations);
1090*4882a593Smuzhiyun 	return 0;
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun 
delegation_hashed(struct nfs4_delegation * dp)1093*4882a593Smuzhiyun static bool delegation_hashed(struct nfs4_delegation *dp)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	return !(list_empty(&dp->dl_perfile));
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun static bool
unhash_delegation_locked(struct nfs4_delegation * dp)1099*4882a593Smuzhiyun unhash_delegation_locked(struct nfs4_delegation *dp)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun 	struct nfs4_file *fp = dp->dl_stid.sc_file;
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	lockdep_assert_held(&state_lock);
1104*4882a593Smuzhiyun 
1105*4882a593Smuzhiyun 	if (!delegation_hashed(dp))
1106*4882a593Smuzhiyun 		return false;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
1109*4882a593Smuzhiyun 	/* Ensure that deleg break won't try to requeue it */
1110*4882a593Smuzhiyun 	++dp->dl_time;
1111*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
1112*4882a593Smuzhiyun 	list_del_init(&dp->dl_perclnt);
1113*4882a593Smuzhiyun 	list_del_init(&dp->dl_recall_lru);
1114*4882a593Smuzhiyun 	list_del_init(&dp->dl_perfile);
1115*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
1116*4882a593Smuzhiyun 	return true;
1117*4882a593Smuzhiyun }
1118*4882a593Smuzhiyun 
destroy_delegation(struct nfs4_delegation * dp)1119*4882a593Smuzhiyun static void destroy_delegation(struct nfs4_delegation *dp)
1120*4882a593Smuzhiyun {
1121*4882a593Smuzhiyun 	bool unhashed;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	spin_lock(&state_lock);
1124*4882a593Smuzhiyun 	unhashed = unhash_delegation_locked(dp);
1125*4882a593Smuzhiyun 	spin_unlock(&state_lock);
1126*4882a593Smuzhiyun 	if (unhashed)
1127*4882a593Smuzhiyun 		destroy_unhashed_deleg(dp);
1128*4882a593Smuzhiyun }
1129*4882a593Smuzhiyun 
revoke_delegation(struct nfs4_delegation * dp)1130*4882a593Smuzhiyun static void revoke_delegation(struct nfs4_delegation *dp)
1131*4882a593Smuzhiyun {
1132*4882a593Smuzhiyun 	struct nfs4_client *clp = dp->dl_stid.sc_client;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	WARN_ON(!list_empty(&dp->dl_recall_lru));
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	if (clp->cl_minorversion) {
1137*4882a593Smuzhiyun 		dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
1138*4882a593Smuzhiyun 		refcount_inc(&dp->dl_stid.sc_count);
1139*4882a593Smuzhiyun 		spin_lock(&clp->cl_lock);
1140*4882a593Smuzhiyun 		list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1141*4882a593Smuzhiyun 		spin_unlock(&clp->cl_lock);
1142*4882a593Smuzhiyun 	}
1143*4882a593Smuzhiyun 	destroy_unhashed_deleg(dp);
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun /*
1147*4882a593Smuzhiyun  * SETCLIENTID state
1148*4882a593Smuzhiyun  */
1149*4882a593Smuzhiyun 
clientid_hashval(u32 id)1150*4882a593Smuzhiyun static unsigned int clientid_hashval(u32 id)
1151*4882a593Smuzhiyun {
1152*4882a593Smuzhiyun 	return id & CLIENT_HASH_MASK;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun 
clientstr_hashval(struct xdr_netobj name)1155*4882a593Smuzhiyun static unsigned int clientstr_hashval(struct xdr_netobj name)
1156*4882a593Smuzhiyun {
1157*4882a593Smuzhiyun 	return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun 
1160*4882a593Smuzhiyun /*
1161*4882a593Smuzhiyun  * We store the NONE, READ, WRITE, and BOTH bits separately in the
1162*4882a593Smuzhiyun  * st_{access,deny}_bmap field of the stateid, in order to track not
1163*4882a593Smuzhiyun  * only what share bits are currently in force, but also what
1164*4882a593Smuzhiyun  * combinations of share bits previous opens have used.  This allows us
1165*4882a593Smuzhiyun  * to enforce the recommendation of rfc 3530 14.2.19 that the server
1166*4882a593Smuzhiyun  * return an error if the client attempt to downgrade to a combination
1167*4882a593Smuzhiyun  * of share bits not explicable by closing some of its previous opens.
1168*4882a593Smuzhiyun  *
1169*4882a593Smuzhiyun  * XXX: This enforcement is actually incomplete, since we don't keep
1170*4882a593Smuzhiyun  * track of access/deny bit combinations; so, e.g., we allow:
1171*4882a593Smuzhiyun  *
1172*4882a593Smuzhiyun  *	OPEN allow read, deny write
1173*4882a593Smuzhiyun  *	OPEN allow both, deny none
1174*4882a593Smuzhiyun  *	DOWNGRADE allow read, deny none
1175*4882a593Smuzhiyun  *
1176*4882a593Smuzhiyun  * which we should reject.
1177*4882a593Smuzhiyun  */
1178*4882a593Smuzhiyun static unsigned int
bmap_to_share_mode(unsigned long bmap)1179*4882a593Smuzhiyun bmap_to_share_mode(unsigned long bmap) {
1180*4882a593Smuzhiyun 	int i;
1181*4882a593Smuzhiyun 	unsigned int access = 0;
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	for (i = 1; i < 4; i++) {
1184*4882a593Smuzhiyun 		if (test_bit(i, &bmap))
1185*4882a593Smuzhiyun 			access |= i;
1186*4882a593Smuzhiyun 	}
1187*4882a593Smuzhiyun 	return access;
1188*4882a593Smuzhiyun }
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun /* set share access for a given stateid */
1191*4882a593Smuzhiyun static inline void
set_access(u32 access,struct nfs4_ol_stateid * stp)1192*4882a593Smuzhiyun set_access(u32 access, struct nfs4_ol_stateid *stp)
1193*4882a593Smuzhiyun {
1194*4882a593Smuzhiyun 	unsigned char mask = 1 << access;
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1197*4882a593Smuzhiyun 	stp->st_access_bmap |= mask;
1198*4882a593Smuzhiyun }
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun /* clear share access for a given stateid */
1201*4882a593Smuzhiyun static inline void
clear_access(u32 access,struct nfs4_ol_stateid * stp)1202*4882a593Smuzhiyun clear_access(u32 access, struct nfs4_ol_stateid *stp)
1203*4882a593Smuzhiyun {
1204*4882a593Smuzhiyun 	unsigned char mask = 1 << access;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1207*4882a593Smuzhiyun 	stp->st_access_bmap &= ~mask;
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun /* test whether a given stateid has access */
1211*4882a593Smuzhiyun static inline bool
test_access(u32 access,struct nfs4_ol_stateid * stp)1212*4882a593Smuzhiyun test_access(u32 access, struct nfs4_ol_stateid *stp)
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun 	unsigned char mask = 1 << access;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun 	return (bool)(stp->st_access_bmap & mask);
1217*4882a593Smuzhiyun }
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun /* set share deny for a given stateid */
1220*4882a593Smuzhiyun static inline void
set_deny(u32 deny,struct nfs4_ol_stateid * stp)1221*4882a593Smuzhiyun set_deny(u32 deny, struct nfs4_ol_stateid *stp)
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun 	unsigned char mask = 1 << deny;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1226*4882a593Smuzhiyun 	stp->st_deny_bmap |= mask;
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun /* clear share deny for a given stateid */
1230*4882a593Smuzhiyun static inline void
clear_deny(u32 deny,struct nfs4_ol_stateid * stp)1231*4882a593Smuzhiyun clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun 	unsigned char mask = 1 << deny;
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun 	WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1236*4882a593Smuzhiyun 	stp->st_deny_bmap &= ~mask;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
1239*4882a593Smuzhiyun /* test whether a given stateid is denying specific access */
1240*4882a593Smuzhiyun static inline bool
test_deny(u32 deny,struct nfs4_ol_stateid * stp)1241*4882a593Smuzhiyun test_deny(u32 deny, struct nfs4_ol_stateid *stp)
1242*4882a593Smuzhiyun {
1243*4882a593Smuzhiyun 	unsigned char mask = 1 << deny;
1244*4882a593Smuzhiyun 
1245*4882a593Smuzhiyun 	return (bool)(stp->st_deny_bmap & mask);
1246*4882a593Smuzhiyun }
1247*4882a593Smuzhiyun 
nfs4_access_to_omode(u32 access)1248*4882a593Smuzhiyun static int nfs4_access_to_omode(u32 access)
1249*4882a593Smuzhiyun {
1250*4882a593Smuzhiyun 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
1251*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_READ:
1252*4882a593Smuzhiyun 		return O_RDONLY;
1253*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_WRITE:
1254*4882a593Smuzhiyun 		return O_WRONLY;
1255*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_BOTH:
1256*4882a593Smuzhiyun 		return O_RDWR;
1257*4882a593Smuzhiyun 	}
1258*4882a593Smuzhiyun 	WARN_ON_ONCE(1);
1259*4882a593Smuzhiyun 	return O_RDONLY;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun /*
1263*4882a593Smuzhiyun  * A stateid that had a deny mode associated with it is being released
1264*4882a593Smuzhiyun  * or downgraded. Recalculate the deny mode on the file.
1265*4882a593Smuzhiyun  */
1266*4882a593Smuzhiyun static void
recalculate_deny_mode(struct nfs4_file * fp)1267*4882a593Smuzhiyun recalculate_deny_mode(struct nfs4_file *fp)
1268*4882a593Smuzhiyun {
1269*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
1272*4882a593Smuzhiyun 	fp->fi_share_deny = 0;
1273*4882a593Smuzhiyun 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1274*4882a593Smuzhiyun 		fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1275*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun static void
reset_union_bmap_deny(u32 deny,struct nfs4_ol_stateid * stp)1279*4882a593Smuzhiyun reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1280*4882a593Smuzhiyun {
1281*4882a593Smuzhiyun 	int i;
1282*4882a593Smuzhiyun 	bool change = false;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	for (i = 1; i < 4; i++) {
1285*4882a593Smuzhiyun 		if ((i & deny) != i) {
1286*4882a593Smuzhiyun 			change = true;
1287*4882a593Smuzhiyun 			clear_deny(i, stp);
1288*4882a593Smuzhiyun 		}
1289*4882a593Smuzhiyun 	}
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun 	/* Recalculate per-file deny mode if there was a change */
1292*4882a593Smuzhiyun 	if (change)
1293*4882a593Smuzhiyun 		recalculate_deny_mode(stp->st_stid.sc_file);
1294*4882a593Smuzhiyun }
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun /* release all access and file references for a given stateid */
1297*4882a593Smuzhiyun static void
release_all_access(struct nfs4_ol_stateid * stp)1298*4882a593Smuzhiyun release_all_access(struct nfs4_ol_stateid *stp)
1299*4882a593Smuzhiyun {
1300*4882a593Smuzhiyun 	int i;
1301*4882a593Smuzhiyun 	struct nfs4_file *fp = stp->st_stid.sc_file;
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	if (fp && stp->st_deny_bmap != 0)
1304*4882a593Smuzhiyun 		recalculate_deny_mode(fp);
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	for (i = 1; i < 4; i++) {
1307*4882a593Smuzhiyun 		if (test_access(i, stp))
1308*4882a593Smuzhiyun 			nfs4_file_put_access(stp->st_stid.sc_file, i);
1309*4882a593Smuzhiyun 		clear_access(i, stp);
1310*4882a593Smuzhiyun 	}
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun 
nfs4_free_stateowner(struct nfs4_stateowner * sop)1313*4882a593Smuzhiyun static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1314*4882a593Smuzhiyun {
1315*4882a593Smuzhiyun 	kfree(sop->so_owner.data);
1316*4882a593Smuzhiyun 	sop->so_ops->so_free(sop);
1317*4882a593Smuzhiyun }
1318*4882a593Smuzhiyun 
nfs4_put_stateowner(struct nfs4_stateowner * sop)1319*4882a593Smuzhiyun static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1320*4882a593Smuzhiyun {
1321*4882a593Smuzhiyun 	struct nfs4_client *clp = sop->so_client;
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	might_lock(&clp->cl_lock);
1324*4882a593Smuzhiyun 
1325*4882a593Smuzhiyun 	if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
1326*4882a593Smuzhiyun 		return;
1327*4882a593Smuzhiyun 	sop->so_ops->so_unhash(sop);
1328*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1329*4882a593Smuzhiyun 	nfs4_free_stateowner(sop);
1330*4882a593Smuzhiyun }
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun static bool
nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid * stp)1333*4882a593Smuzhiyun nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun 	return list_empty(&stp->st_perfile);
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
unhash_ol_stateid(struct nfs4_ol_stateid * stp)1338*4882a593Smuzhiyun static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun 	struct nfs4_file *fp = stp->st_stid.sc_file;
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun 	if (list_empty(&stp->st_perfile))
1345*4882a593Smuzhiyun 		return false;
1346*4882a593Smuzhiyun 
1347*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
1348*4882a593Smuzhiyun 	list_del_init(&stp->st_perfile);
1349*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
1350*4882a593Smuzhiyun 	list_del(&stp->st_perstateowner);
1351*4882a593Smuzhiyun 	return true;
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun 
nfs4_free_ol_stateid(struct nfs4_stid * stid)1354*4882a593Smuzhiyun static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
1355*4882a593Smuzhiyun {
1356*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	put_clnt_odstate(stp->st_clnt_odstate);
1359*4882a593Smuzhiyun 	release_all_access(stp);
1360*4882a593Smuzhiyun 	if (stp->st_stateowner)
1361*4882a593Smuzhiyun 		nfs4_put_stateowner(stp->st_stateowner);
1362*4882a593Smuzhiyun 	WARN_ON(!list_empty(&stid->sc_cp_list));
1363*4882a593Smuzhiyun 	kmem_cache_free(stateid_slab, stid);
1364*4882a593Smuzhiyun }
1365*4882a593Smuzhiyun 
nfs4_free_lock_stateid(struct nfs4_stid * stid)1366*4882a593Smuzhiyun static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
1367*4882a593Smuzhiyun {
1368*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp = openlockstateid(stid);
1369*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
1370*4882a593Smuzhiyun 	struct nfsd_file *nf;
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	nf = find_any_file(stp->st_stid.sc_file);
1373*4882a593Smuzhiyun 	if (nf) {
1374*4882a593Smuzhiyun 		get_file(nf->nf_file);
1375*4882a593Smuzhiyun 		filp_close(nf->nf_file, (fl_owner_t)lo);
1376*4882a593Smuzhiyun 		nfsd_file_put(nf);
1377*4882a593Smuzhiyun 	}
1378*4882a593Smuzhiyun 	nfs4_free_ol_stateid(stid);
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun /*
1382*4882a593Smuzhiyun  * Put the persistent reference to an already unhashed generic stateid, while
1383*4882a593Smuzhiyun  * holding the cl_lock. If it's the last reference, then put it onto the
1384*4882a593Smuzhiyun  * reaplist for later destruction.
1385*4882a593Smuzhiyun  */
put_ol_stateid_locked(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1386*4882a593Smuzhiyun static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1387*4882a593Smuzhiyun 				       struct list_head *reaplist)
1388*4882a593Smuzhiyun {
1389*4882a593Smuzhiyun 	struct nfs4_stid *s = &stp->st_stid;
1390*4882a593Smuzhiyun 	struct nfs4_client *clp = s->sc_client;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
1393*4882a593Smuzhiyun 
1394*4882a593Smuzhiyun 	WARN_ON_ONCE(!list_empty(&stp->st_locks));
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	if (!refcount_dec_and_test(&s->sc_count)) {
1397*4882a593Smuzhiyun 		wake_up_all(&close_wq);
1398*4882a593Smuzhiyun 		return;
1399*4882a593Smuzhiyun 	}
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1402*4882a593Smuzhiyun 	list_add(&stp->st_locks, reaplist);
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun 
unhash_lock_stateid(struct nfs4_ol_stateid * stp)1405*4882a593Smuzhiyun static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	if (!unhash_ol_stateid(stp))
1410*4882a593Smuzhiyun 		return false;
1411*4882a593Smuzhiyun 	list_del_init(&stp->st_locks);
1412*4882a593Smuzhiyun 	nfs4_unhash_stid(&stp->st_stid);
1413*4882a593Smuzhiyun 	return true;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
release_lock_stateid(struct nfs4_ol_stateid * stp)1416*4882a593Smuzhiyun static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1417*4882a593Smuzhiyun {
1418*4882a593Smuzhiyun 	struct nfs4_client *clp = stp->st_stid.sc_client;
1419*4882a593Smuzhiyun 	bool unhashed;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1422*4882a593Smuzhiyun 	unhashed = unhash_lock_stateid(stp);
1423*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1424*4882a593Smuzhiyun 	if (unhashed)
1425*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun 
unhash_lockowner_locked(struct nfs4_lockowner * lo)1428*4882a593Smuzhiyun static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun 	struct nfs4_client *clp = lo->lo_owner.so_client;
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun 	list_del_init(&lo->lo_owner.so_strhash);
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun /*
1438*4882a593Smuzhiyun  * Free a list of generic stateids that were collected earlier after being
1439*4882a593Smuzhiyun  * fully unhashed.
1440*4882a593Smuzhiyun  */
1441*4882a593Smuzhiyun static void
free_ol_stateid_reaplist(struct list_head * reaplist)1442*4882a593Smuzhiyun free_ol_stateid_reaplist(struct list_head *reaplist)
1443*4882a593Smuzhiyun {
1444*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
1445*4882a593Smuzhiyun 	struct nfs4_file *fp;
1446*4882a593Smuzhiyun 
1447*4882a593Smuzhiyun 	might_sleep();
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	while (!list_empty(reaplist)) {
1450*4882a593Smuzhiyun 		stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1451*4882a593Smuzhiyun 				       st_locks);
1452*4882a593Smuzhiyun 		list_del(&stp->st_locks);
1453*4882a593Smuzhiyun 		fp = stp->st_stid.sc_file;
1454*4882a593Smuzhiyun 		stp->st_stid.sc_free(&stp->st_stid);
1455*4882a593Smuzhiyun 		if (fp)
1456*4882a593Smuzhiyun 			put_nfs4_file(fp);
1457*4882a593Smuzhiyun 	}
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun 
release_open_stateid_locks(struct nfs4_ol_stateid * open_stp,struct list_head * reaplist)1460*4882a593Smuzhiyun static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1461*4882a593Smuzhiyun 				       struct list_head *reaplist)
1462*4882a593Smuzhiyun {
1463*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1466*4882a593Smuzhiyun 
1467*4882a593Smuzhiyun 	while (!list_empty(&open_stp->st_locks)) {
1468*4882a593Smuzhiyun 		stp = list_entry(open_stp->st_locks.next,
1469*4882a593Smuzhiyun 				struct nfs4_ol_stateid, st_locks);
1470*4882a593Smuzhiyun 		WARN_ON(!unhash_lock_stateid(stp));
1471*4882a593Smuzhiyun 		put_ol_stateid_locked(stp, reaplist);
1472*4882a593Smuzhiyun 	}
1473*4882a593Smuzhiyun }
1474*4882a593Smuzhiyun 
unhash_open_stateid(struct nfs4_ol_stateid * stp,struct list_head * reaplist)1475*4882a593Smuzhiyun static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
1476*4882a593Smuzhiyun 				struct list_head *reaplist)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	if (!unhash_ol_stateid(stp))
1481*4882a593Smuzhiyun 		return false;
1482*4882a593Smuzhiyun 	release_open_stateid_locks(stp, reaplist);
1483*4882a593Smuzhiyun 	return true;
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun 
release_open_stateid(struct nfs4_ol_stateid * stp)1486*4882a593Smuzhiyun static void release_open_stateid(struct nfs4_ol_stateid *stp)
1487*4882a593Smuzhiyun {
1488*4882a593Smuzhiyun 	LIST_HEAD(reaplist);
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	spin_lock(&stp->st_stid.sc_client->cl_lock);
1491*4882a593Smuzhiyun 	if (unhash_open_stateid(stp, &reaplist))
1492*4882a593Smuzhiyun 		put_ol_stateid_locked(stp, &reaplist);
1493*4882a593Smuzhiyun 	spin_unlock(&stp->st_stid.sc_client->cl_lock);
1494*4882a593Smuzhiyun 	free_ol_stateid_reaplist(&reaplist);
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun 
unhash_openowner_locked(struct nfs4_openowner * oo)1497*4882a593Smuzhiyun static void unhash_openowner_locked(struct nfs4_openowner *oo)
1498*4882a593Smuzhiyun {
1499*4882a593Smuzhiyun 	struct nfs4_client *clp = oo->oo_owner.so_client;
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	list_del_init(&oo->oo_owner.so_strhash);
1504*4882a593Smuzhiyun 	list_del_init(&oo->oo_perclient);
1505*4882a593Smuzhiyun }
1506*4882a593Smuzhiyun 
release_last_closed_stateid(struct nfs4_openowner * oo)1507*4882a593Smuzhiyun static void release_last_closed_stateid(struct nfs4_openowner *oo)
1508*4882a593Smuzhiyun {
1509*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1510*4882a593Smuzhiyun 					  nfsd_net_id);
1511*4882a593Smuzhiyun 	struct nfs4_ol_stateid *s;
1512*4882a593Smuzhiyun 
1513*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
1514*4882a593Smuzhiyun 	s = oo->oo_last_closed_stid;
1515*4882a593Smuzhiyun 	if (s) {
1516*4882a593Smuzhiyun 		list_del_init(&oo->oo_close_lru);
1517*4882a593Smuzhiyun 		oo->oo_last_closed_stid = NULL;
1518*4882a593Smuzhiyun 	}
1519*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
1520*4882a593Smuzhiyun 	if (s)
1521*4882a593Smuzhiyun 		nfs4_put_stid(&s->st_stid);
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun 
release_openowner(struct nfs4_openowner * oo)1524*4882a593Smuzhiyun static void release_openowner(struct nfs4_openowner *oo)
1525*4882a593Smuzhiyun {
1526*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
1527*4882a593Smuzhiyun 	struct nfs4_client *clp = oo->oo_owner.so_client;
1528*4882a593Smuzhiyun 	struct list_head reaplist;
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun 	INIT_LIST_HEAD(&reaplist);
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1533*4882a593Smuzhiyun 	unhash_openowner_locked(oo);
1534*4882a593Smuzhiyun 	while (!list_empty(&oo->oo_owner.so_stateids)) {
1535*4882a593Smuzhiyun 		stp = list_first_entry(&oo->oo_owner.so_stateids,
1536*4882a593Smuzhiyun 				struct nfs4_ol_stateid, st_perstateowner);
1537*4882a593Smuzhiyun 		if (unhash_open_stateid(stp, &reaplist))
1538*4882a593Smuzhiyun 			put_ol_stateid_locked(stp, &reaplist);
1539*4882a593Smuzhiyun 	}
1540*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1541*4882a593Smuzhiyun 	free_ol_stateid_reaplist(&reaplist);
1542*4882a593Smuzhiyun 	release_last_closed_stateid(oo);
1543*4882a593Smuzhiyun 	nfs4_put_stateowner(&oo->oo_owner);
1544*4882a593Smuzhiyun }
1545*4882a593Smuzhiyun 
1546*4882a593Smuzhiyun static inline int
hash_sessionid(struct nfs4_sessionid * sessionid)1547*4882a593Smuzhiyun hash_sessionid(struct nfs4_sessionid *sessionid)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1550*4882a593Smuzhiyun 
1551*4882a593Smuzhiyun 	return sid->sequence % SESSION_HASH_SIZE;
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun #ifdef CONFIG_SUNRPC_DEBUG
1555*4882a593Smuzhiyun static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1556*4882a593Smuzhiyun dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1557*4882a593Smuzhiyun {
1558*4882a593Smuzhiyun 	u32 *ptr = (u32 *)(&sessionid->data[0]);
1559*4882a593Smuzhiyun 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun #else
1562*4882a593Smuzhiyun static inline void
dump_sessionid(const char * fn,struct nfs4_sessionid * sessionid)1563*4882a593Smuzhiyun dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1564*4882a593Smuzhiyun {
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun #endif
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun /*
1569*4882a593Smuzhiyun  * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1570*4882a593Smuzhiyun  * won't be used for replay.
1571*4882a593Smuzhiyun  */
nfsd4_bump_seqid(struct nfsd4_compound_state * cstate,__be32 nfserr)1572*4882a593Smuzhiyun void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1573*4882a593Smuzhiyun {
1574*4882a593Smuzhiyun 	struct nfs4_stateowner *so = cstate->replay_owner;
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun 	if (nfserr == nfserr_replay_me)
1577*4882a593Smuzhiyun 		return;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	if (!seqid_mutating_err(ntohl(nfserr))) {
1580*4882a593Smuzhiyun 		nfsd4_cstate_clear_replay(cstate);
1581*4882a593Smuzhiyun 		return;
1582*4882a593Smuzhiyun 	}
1583*4882a593Smuzhiyun 	if (!so)
1584*4882a593Smuzhiyun 		return;
1585*4882a593Smuzhiyun 	if (so->so_is_open_owner)
1586*4882a593Smuzhiyun 		release_last_closed_stateid(openowner(so));
1587*4882a593Smuzhiyun 	so->so_seqid++;
1588*4882a593Smuzhiyun 	return;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun static void
gen_sessionid(struct nfsd4_session * ses)1592*4882a593Smuzhiyun gen_sessionid(struct nfsd4_session *ses)
1593*4882a593Smuzhiyun {
1594*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
1595*4882a593Smuzhiyun 	struct nfsd4_sessionid *sid;
1596*4882a593Smuzhiyun 
1597*4882a593Smuzhiyun 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1598*4882a593Smuzhiyun 	sid->clientid = clp->cl_clientid;
1599*4882a593Smuzhiyun 	sid->sequence = current_sessionid++;
1600*4882a593Smuzhiyun 	sid->reserved = 0;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun /*
1604*4882a593Smuzhiyun  * The protocol defines ca_maxresponssize_cached to include the size of
1605*4882a593Smuzhiyun  * the rpc header, but all we need to cache is the data starting after
1606*4882a593Smuzhiyun  * the end of the initial SEQUENCE operation--the rest we regenerate
1607*4882a593Smuzhiyun  * each time.  Therefore we can advertise a ca_maxresponssize_cached
1608*4882a593Smuzhiyun  * value that is the number of bytes in our cache plus a few additional
1609*4882a593Smuzhiyun  * bytes.  In order to stay on the safe side, and not promise more than
1610*4882a593Smuzhiyun  * we can cache, those additional bytes must be the minimum possible: 24
1611*4882a593Smuzhiyun  * bytes of rpc header (xid through accept state, with AUTH_NULL
1612*4882a593Smuzhiyun  * verifier), 12 for the compound header (with zero-length tag), and 44
1613*4882a593Smuzhiyun  * for the SEQUENCE op response:
1614*4882a593Smuzhiyun  */
1615*4882a593Smuzhiyun #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun static void
free_session_slots(struct nfsd4_session * ses)1618*4882a593Smuzhiyun free_session_slots(struct nfsd4_session *ses)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun 	int i;
1621*4882a593Smuzhiyun 
1622*4882a593Smuzhiyun 	for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1623*4882a593Smuzhiyun 		free_svc_cred(&ses->se_slots[i]->sl_cred);
1624*4882a593Smuzhiyun 		kfree(ses->se_slots[i]);
1625*4882a593Smuzhiyun 	}
1626*4882a593Smuzhiyun }
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun /*
1629*4882a593Smuzhiyun  * We don't actually need to cache the rpc and session headers, so we
1630*4882a593Smuzhiyun  * can allocate a little less for each slot:
1631*4882a593Smuzhiyun  */
slot_bytes(struct nfsd4_channel_attrs * ca)1632*4882a593Smuzhiyun static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1633*4882a593Smuzhiyun {
1634*4882a593Smuzhiyun 	u32 size;
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun 	if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1637*4882a593Smuzhiyun 		size = 0;
1638*4882a593Smuzhiyun 	else
1639*4882a593Smuzhiyun 		size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1640*4882a593Smuzhiyun 	return size + sizeof(struct nfsd4_slot);
1641*4882a593Smuzhiyun }
1642*4882a593Smuzhiyun 
1643*4882a593Smuzhiyun /*
1644*4882a593Smuzhiyun  * XXX: If we run out of reserved DRC memory we could (up to a point)
1645*4882a593Smuzhiyun  * re-negotiate active sessions and reduce their slot usage to make
1646*4882a593Smuzhiyun  * room for new connections. For now we just fail the create session.
1647*4882a593Smuzhiyun  */
nfsd4_get_drc_mem(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)1648*4882a593Smuzhiyun static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1649*4882a593Smuzhiyun {
1650*4882a593Smuzhiyun 	u32 slotsize = slot_bytes(ca);
1651*4882a593Smuzhiyun 	u32 num = ca->maxreqs;
1652*4882a593Smuzhiyun 	unsigned long avail, total_avail;
1653*4882a593Smuzhiyun 	unsigned int scale_factor;
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun 	spin_lock(&nfsd_drc_lock);
1656*4882a593Smuzhiyun 	if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1657*4882a593Smuzhiyun 		total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1658*4882a593Smuzhiyun 	else
1659*4882a593Smuzhiyun 		/* We have handed out more space than we chose in
1660*4882a593Smuzhiyun 		 * set_max_drc() to allow.  That isn't really a
1661*4882a593Smuzhiyun 		 * problem as long as that doesn't make us think we
1662*4882a593Smuzhiyun 		 * have lots more due to integer overflow.
1663*4882a593Smuzhiyun 		 */
1664*4882a593Smuzhiyun 		total_avail = 0;
1665*4882a593Smuzhiyun 	avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
1666*4882a593Smuzhiyun 	/*
1667*4882a593Smuzhiyun 	 * Never use more than a fraction of the remaining memory,
1668*4882a593Smuzhiyun 	 * unless it's the only way to give this client a slot.
1669*4882a593Smuzhiyun 	 * The chosen fraction is either 1/8 or 1/number of threads,
1670*4882a593Smuzhiyun 	 * whichever is smaller.  This ensures there are adequate
1671*4882a593Smuzhiyun 	 * slots to support multiple clients per thread.
1672*4882a593Smuzhiyun 	 * Give the client one slot even if that would require
1673*4882a593Smuzhiyun 	 * over-allocation--it is better than failure.
1674*4882a593Smuzhiyun 	 */
1675*4882a593Smuzhiyun 	scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun 	avail = clamp_t(unsigned long, avail, slotsize,
1678*4882a593Smuzhiyun 			total_avail/scale_factor);
1679*4882a593Smuzhiyun 	num = min_t(int, num, avail / slotsize);
1680*4882a593Smuzhiyun 	num = max_t(int, num, 1);
1681*4882a593Smuzhiyun 	nfsd_drc_mem_used += num * slotsize;
1682*4882a593Smuzhiyun 	spin_unlock(&nfsd_drc_lock);
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 	return num;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun 
nfsd4_put_drc_mem(struct nfsd4_channel_attrs * ca)1687*4882a593Smuzhiyun static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1688*4882a593Smuzhiyun {
1689*4882a593Smuzhiyun 	int slotsize = slot_bytes(ca);
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun 	spin_lock(&nfsd_drc_lock);
1692*4882a593Smuzhiyun 	nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1693*4882a593Smuzhiyun 	spin_unlock(&nfsd_drc_lock);
1694*4882a593Smuzhiyun }
1695*4882a593Smuzhiyun 
alloc_session(struct nfsd4_channel_attrs * fattrs,struct nfsd4_channel_attrs * battrs)1696*4882a593Smuzhiyun static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1697*4882a593Smuzhiyun 					   struct nfsd4_channel_attrs *battrs)
1698*4882a593Smuzhiyun {
1699*4882a593Smuzhiyun 	int numslots = fattrs->maxreqs;
1700*4882a593Smuzhiyun 	int slotsize = slot_bytes(fattrs);
1701*4882a593Smuzhiyun 	struct nfsd4_session *new;
1702*4882a593Smuzhiyun 	int mem, i;
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun 	BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1705*4882a593Smuzhiyun 			+ sizeof(struct nfsd4_session) > PAGE_SIZE);
1706*4882a593Smuzhiyun 	mem = numslots * sizeof(struct nfsd4_slot *);
1707*4882a593Smuzhiyun 
1708*4882a593Smuzhiyun 	new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1709*4882a593Smuzhiyun 	if (!new)
1710*4882a593Smuzhiyun 		return NULL;
1711*4882a593Smuzhiyun 	/* allocate each struct nfsd4_slot and data cache in one piece */
1712*4882a593Smuzhiyun 	for (i = 0; i < numslots; i++) {
1713*4882a593Smuzhiyun 		new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1714*4882a593Smuzhiyun 		if (!new->se_slots[i])
1715*4882a593Smuzhiyun 			goto out_free;
1716*4882a593Smuzhiyun 	}
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1719*4882a593Smuzhiyun 	memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	return new;
1722*4882a593Smuzhiyun out_free:
1723*4882a593Smuzhiyun 	while (i--)
1724*4882a593Smuzhiyun 		kfree(new->se_slots[i]);
1725*4882a593Smuzhiyun 	kfree(new);
1726*4882a593Smuzhiyun 	return NULL;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun 
free_conn(struct nfsd4_conn * c)1729*4882a593Smuzhiyun static void free_conn(struct nfsd4_conn *c)
1730*4882a593Smuzhiyun {
1731*4882a593Smuzhiyun 	svc_xprt_put(c->cn_xprt);
1732*4882a593Smuzhiyun 	kfree(c);
1733*4882a593Smuzhiyun }
1734*4882a593Smuzhiyun 
nfsd4_conn_lost(struct svc_xpt_user * u)1735*4882a593Smuzhiyun static void nfsd4_conn_lost(struct svc_xpt_user *u)
1736*4882a593Smuzhiyun {
1737*4882a593Smuzhiyun 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1738*4882a593Smuzhiyun 	struct nfs4_client *clp = c->cn_session->se_client;
1739*4882a593Smuzhiyun 
1740*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1741*4882a593Smuzhiyun 	if (!list_empty(&c->cn_persession)) {
1742*4882a593Smuzhiyun 		list_del(&c->cn_persession);
1743*4882a593Smuzhiyun 		free_conn(c);
1744*4882a593Smuzhiyun 	}
1745*4882a593Smuzhiyun 	nfsd4_probe_callback(clp);
1746*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1747*4882a593Smuzhiyun }
1748*4882a593Smuzhiyun 
alloc_conn(struct svc_rqst * rqstp,u32 flags)1749*4882a593Smuzhiyun static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1750*4882a593Smuzhiyun {
1751*4882a593Smuzhiyun 	struct nfsd4_conn *conn;
1752*4882a593Smuzhiyun 
1753*4882a593Smuzhiyun 	conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1754*4882a593Smuzhiyun 	if (!conn)
1755*4882a593Smuzhiyun 		return NULL;
1756*4882a593Smuzhiyun 	svc_xprt_get(rqstp->rq_xprt);
1757*4882a593Smuzhiyun 	conn->cn_xprt = rqstp->rq_xprt;
1758*4882a593Smuzhiyun 	conn->cn_flags = flags;
1759*4882a593Smuzhiyun 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1760*4882a593Smuzhiyun 	return conn;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun 
__nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1763*4882a593Smuzhiyun static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1764*4882a593Smuzhiyun {
1765*4882a593Smuzhiyun 	conn->cn_session = ses;
1766*4882a593Smuzhiyun 	list_add(&conn->cn_persession, &ses->se_conns);
1767*4882a593Smuzhiyun }
1768*4882a593Smuzhiyun 
nfsd4_hash_conn(struct nfsd4_conn * conn,struct nfsd4_session * ses)1769*4882a593Smuzhiyun static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1770*4882a593Smuzhiyun {
1771*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1774*4882a593Smuzhiyun 	__nfsd4_hash_conn(conn, ses);
1775*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1776*4882a593Smuzhiyun }
1777*4882a593Smuzhiyun 
nfsd4_register_conn(struct nfsd4_conn * conn)1778*4882a593Smuzhiyun static int nfsd4_register_conn(struct nfsd4_conn *conn)
1779*4882a593Smuzhiyun {
1780*4882a593Smuzhiyun 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
1781*4882a593Smuzhiyun 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1782*4882a593Smuzhiyun }
1783*4882a593Smuzhiyun 
nfsd4_init_conn(struct svc_rqst * rqstp,struct nfsd4_conn * conn,struct nfsd4_session * ses)1784*4882a593Smuzhiyun static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1785*4882a593Smuzhiyun {
1786*4882a593Smuzhiyun 	int ret;
1787*4882a593Smuzhiyun 
1788*4882a593Smuzhiyun 	nfsd4_hash_conn(conn, ses);
1789*4882a593Smuzhiyun 	ret = nfsd4_register_conn(conn);
1790*4882a593Smuzhiyun 	if (ret)
1791*4882a593Smuzhiyun 		/* oops; xprt is already down: */
1792*4882a593Smuzhiyun 		nfsd4_conn_lost(&conn->cn_xpt_user);
1793*4882a593Smuzhiyun 	/* We may have gained or lost a callback channel: */
1794*4882a593Smuzhiyun 	nfsd4_probe_callback_sync(ses->se_client);
1795*4882a593Smuzhiyun }
1796*4882a593Smuzhiyun 
alloc_conn_from_crses(struct svc_rqst * rqstp,struct nfsd4_create_session * cses)1797*4882a593Smuzhiyun static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1798*4882a593Smuzhiyun {
1799*4882a593Smuzhiyun 	u32 dir = NFS4_CDFC4_FORE;
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun 	if (cses->flags & SESSION4_BACK_CHAN)
1802*4882a593Smuzhiyun 		dir |= NFS4_CDFC4_BACK;
1803*4882a593Smuzhiyun 	return alloc_conn(rqstp, dir);
1804*4882a593Smuzhiyun }
1805*4882a593Smuzhiyun 
1806*4882a593Smuzhiyun /* must be called under client_lock */
nfsd4_del_conns(struct nfsd4_session * s)1807*4882a593Smuzhiyun static void nfsd4_del_conns(struct nfsd4_session *s)
1808*4882a593Smuzhiyun {
1809*4882a593Smuzhiyun 	struct nfs4_client *clp = s->se_client;
1810*4882a593Smuzhiyun 	struct nfsd4_conn *c;
1811*4882a593Smuzhiyun 
1812*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1813*4882a593Smuzhiyun 	while (!list_empty(&s->se_conns)) {
1814*4882a593Smuzhiyun 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1815*4882a593Smuzhiyun 		list_del_init(&c->cn_persession);
1816*4882a593Smuzhiyun 		spin_unlock(&clp->cl_lock);
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1819*4882a593Smuzhiyun 		free_conn(c);
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 		spin_lock(&clp->cl_lock);
1822*4882a593Smuzhiyun 	}
1823*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1824*4882a593Smuzhiyun }
1825*4882a593Smuzhiyun 
__free_session(struct nfsd4_session * ses)1826*4882a593Smuzhiyun static void __free_session(struct nfsd4_session *ses)
1827*4882a593Smuzhiyun {
1828*4882a593Smuzhiyun 	free_session_slots(ses);
1829*4882a593Smuzhiyun 	kfree(ses);
1830*4882a593Smuzhiyun }
1831*4882a593Smuzhiyun 
free_session(struct nfsd4_session * ses)1832*4882a593Smuzhiyun static void free_session(struct nfsd4_session *ses)
1833*4882a593Smuzhiyun {
1834*4882a593Smuzhiyun 	nfsd4_del_conns(ses);
1835*4882a593Smuzhiyun 	nfsd4_put_drc_mem(&ses->se_fchannel);
1836*4882a593Smuzhiyun 	__free_session(ses);
1837*4882a593Smuzhiyun }
1838*4882a593Smuzhiyun 
init_session(struct svc_rqst * rqstp,struct nfsd4_session * new,struct nfs4_client * clp,struct nfsd4_create_session * cses)1839*4882a593Smuzhiyun static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1840*4882a593Smuzhiyun {
1841*4882a593Smuzhiyun 	int idx;
1842*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 	new->se_client = clp;
1845*4882a593Smuzhiyun 	gen_sessionid(new);
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	INIT_LIST_HEAD(&new->se_conns);
1848*4882a593Smuzhiyun 
1849*4882a593Smuzhiyun 	new->se_cb_seq_nr = 1;
1850*4882a593Smuzhiyun 	new->se_flags = cses->flags;
1851*4882a593Smuzhiyun 	new->se_cb_prog = cses->callback_prog;
1852*4882a593Smuzhiyun 	new->se_cb_sec = cses->cb_sec;
1853*4882a593Smuzhiyun 	atomic_set(&new->se_ref, 0);
1854*4882a593Smuzhiyun 	idx = hash_sessionid(&new->se_sessionid);
1855*4882a593Smuzhiyun 	list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1856*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
1857*4882a593Smuzhiyun 	list_add(&new->se_perclnt, &clp->cl_sessions);
1858*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	{
1861*4882a593Smuzhiyun 		struct sockaddr *sa = svc_addr(rqstp);
1862*4882a593Smuzhiyun 		/*
1863*4882a593Smuzhiyun 		 * This is a little silly; with sessions there's no real
1864*4882a593Smuzhiyun 		 * use for the callback address.  Use the peer address
1865*4882a593Smuzhiyun 		 * as a reasonable default for now, but consider fixing
1866*4882a593Smuzhiyun 		 * the rpc client not to require an address in the
1867*4882a593Smuzhiyun 		 * future:
1868*4882a593Smuzhiyun 		 */
1869*4882a593Smuzhiyun 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1870*4882a593Smuzhiyun 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1871*4882a593Smuzhiyun 	}
1872*4882a593Smuzhiyun }
1873*4882a593Smuzhiyun 
1874*4882a593Smuzhiyun /* caller must hold client_lock */
1875*4882a593Smuzhiyun static struct nfsd4_session *
__find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net)1876*4882a593Smuzhiyun __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1877*4882a593Smuzhiyun {
1878*4882a593Smuzhiyun 	struct nfsd4_session *elem;
1879*4882a593Smuzhiyun 	int idx;
1880*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1881*4882a593Smuzhiyun 
1882*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
1883*4882a593Smuzhiyun 
1884*4882a593Smuzhiyun 	dump_sessionid(__func__, sessionid);
1885*4882a593Smuzhiyun 	idx = hash_sessionid(sessionid);
1886*4882a593Smuzhiyun 	/* Search in the appropriate list */
1887*4882a593Smuzhiyun 	list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1888*4882a593Smuzhiyun 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
1889*4882a593Smuzhiyun 			    NFS4_MAX_SESSIONID_LEN)) {
1890*4882a593Smuzhiyun 			return elem;
1891*4882a593Smuzhiyun 		}
1892*4882a593Smuzhiyun 	}
1893*4882a593Smuzhiyun 
1894*4882a593Smuzhiyun 	dprintk("%s: session not found\n", __func__);
1895*4882a593Smuzhiyun 	return NULL;
1896*4882a593Smuzhiyun }
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun static struct nfsd4_session *
find_in_sessionid_hashtbl(struct nfs4_sessionid * sessionid,struct net * net,__be32 * ret)1899*4882a593Smuzhiyun find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1900*4882a593Smuzhiyun 		__be32 *ret)
1901*4882a593Smuzhiyun {
1902*4882a593Smuzhiyun 	struct nfsd4_session *session;
1903*4882a593Smuzhiyun 	__be32 status = nfserr_badsession;
1904*4882a593Smuzhiyun 
1905*4882a593Smuzhiyun 	session = __find_in_sessionid_hashtbl(sessionid, net);
1906*4882a593Smuzhiyun 	if (!session)
1907*4882a593Smuzhiyun 		goto out;
1908*4882a593Smuzhiyun 	status = nfsd4_get_session_locked(session);
1909*4882a593Smuzhiyun 	if (status)
1910*4882a593Smuzhiyun 		session = NULL;
1911*4882a593Smuzhiyun out:
1912*4882a593Smuzhiyun 	*ret = status;
1913*4882a593Smuzhiyun 	return session;
1914*4882a593Smuzhiyun }
1915*4882a593Smuzhiyun 
1916*4882a593Smuzhiyun /* caller must hold client_lock */
1917*4882a593Smuzhiyun static void
unhash_session(struct nfsd4_session * ses)1918*4882a593Smuzhiyun unhash_session(struct nfsd4_session *ses)
1919*4882a593Smuzhiyun {
1920*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
1921*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1922*4882a593Smuzhiyun 
1923*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	list_del(&ses->se_hash);
1926*4882a593Smuzhiyun 	spin_lock(&ses->se_client->cl_lock);
1927*4882a593Smuzhiyun 	list_del(&ses->se_perclnt);
1928*4882a593Smuzhiyun 	spin_unlock(&ses->se_client->cl_lock);
1929*4882a593Smuzhiyun }
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1932*4882a593Smuzhiyun static int
STALE_CLIENTID(clientid_t * clid,struct nfsd_net * nn)1933*4882a593Smuzhiyun STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1934*4882a593Smuzhiyun {
1935*4882a593Smuzhiyun 	/*
1936*4882a593Smuzhiyun 	 * We're assuming the clid was not given out from a boot
1937*4882a593Smuzhiyun 	 * precisely 2^32 (about 136 years) before this one.  That seems
1938*4882a593Smuzhiyun 	 * a safe assumption:
1939*4882a593Smuzhiyun 	 */
1940*4882a593Smuzhiyun 	if (clid->cl_boot == (u32)nn->boot_time)
1941*4882a593Smuzhiyun 		return 0;
1942*4882a593Smuzhiyun 	trace_nfsd_clid_stale(clid);
1943*4882a593Smuzhiyun 	return 1;
1944*4882a593Smuzhiyun }
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun /*
1947*4882a593Smuzhiyun  * XXX Should we use a slab cache ?
1948*4882a593Smuzhiyun  * This type of memory management is somewhat inefficient, but we use it
1949*4882a593Smuzhiyun  * anyway since SETCLIENTID is not a common operation.
1950*4882a593Smuzhiyun  */
alloc_client(struct xdr_netobj name)1951*4882a593Smuzhiyun static struct nfs4_client *alloc_client(struct xdr_netobj name)
1952*4882a593Smuzhiyun {
1953*4882a593Smuzhiyun 	struct nfs4_client *clp;
1954*4882a593Smuzhiyun 	int i;
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun 	clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
1957*4882a593Smuzhiyun 	if (clp == NULL)
1958*4882a593Smuzhiyun 		return NULL;
1959*4882a593Smuzhiyun 	xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
1960*4882a593Smuzhiyun 	if (clp->cl_name.data == NULL)
1961*4882a593Smuzhiyun 		goto err_no_name;
1962*4882a593Smuzhiyun 	clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1963*4882a593Smuzhiyun 						 sizeof(struct list_head),
1964*4882a593Smuzhiyun 						 GFP_KERNEL);
1965*4882a593Smuzhiyun 	if (!clp->cl_ownerstr_hashtbl)
1966*4882a593Smuzhiyun 		goto err_no_hashtbl;
1967*4882a593Smuzhiyun 	for (i = 0; i < OWNER_HASH_SIZE; i++)
1968*4882a593Smuzhiyun 		INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1969*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_sessions);
1970*4882a593Smuzhiyun 	idr_init(&clp->cl_stateids);
1971*4882a593Smuzhiyun 	atomic_set(&clp->cl_rpc_users, 0);
1972*4882a593Smuzhiyun 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1973*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_idhash);
1974*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_openowners);
1975*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_delegations);
1976*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_lru);
1977*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_revoked);
1978*4882a593Smuzhiyun #ifdef CONFIG_NFSD_PNFS
1979*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_lo_states);
1980*4882a593Smuzhiyun #endif
1981*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->async_copies);
1982*4882a593Smuzhiyun 	spin_lock_init(&clp->async_lock);
1983*4882a593Smuzhiyun 	spin_lock_init(&clp->cl_lock);
1984*4882a593Smuzhiyun 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1985*4882a593Smuzhiyun 	return clp;
1986*4882a593Smuzhiyun err_no_hashtbl:
1987*4882a593Smuzhiyun 	kfree(clp->cl_name.data);
1988*4882a593Smuzhiyun err_no_name:
1989*4882a593Smuzhiyun 	kmem_cache_free(client_slab, clp);
1990*4882a593Smuzhiyun 	return NULL;
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun 
__free_client(struct kref * k)1993*4882a593Smuzhiyun static void __free_client(struct kref *k)
1994*4882a593Smuzhiyun {
1995*4882a593Smuzhiyun 	struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1996*4882a593Smuzhiyun 	struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	free_svc_cred(&clp->cl_cred);
1999*4882a593Smuzhiyun 	kfree(clp->cl_ownerstr_hashtbl);
2000*4882a593Smuzhiyun 	kfree(clp->cl_name.data);
2001*4882a593Smuzhiyun 	kfree(clp->cl_nii_domain.data);
2002*4882a593Smuzhiyun 	kfree(clp->cl_nii_name.data);
2003*4882a593Smuzhiyun 	idr_destroy(&clp->cl_stateids);
2004*4882a593Smuzhiyun 	kmem_cache_free(client_slab, clp);
2005*4882a593Smuzhiyun }
2006*4882a593Smuzhiyun 
drop_client(struct nfs4_client * clp)2007*4882a593Smuzhiyun static void drop_client(struct nfs4_client *clp)
2008*4882a593Smuzhiyun {
2009*4882a593Smuzhiyun 	kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
2010*4882a593Smuzhiyun }
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun static void
free_client(struct nfs4_client * clp)2013*4882a593Smuzhiyun free_client(struct nfs4_client *clp)
2014*4882a593Smuzhiyun {
2015*4882a593Smuzhiyun 	while (!list_empty(&clp->cl_sessions)) {
2016*4882a593Smuzhiyun 		struct nfsd4_session *ses;
2017*4882a593Smuzhiyun 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2018*4882a593Smuzhiyun 				se_perclnt);
2019*4882a593Smuzhiyun 		list_del(&ses->se_perclnt);
2020*4882a593Smuzhiyun 		WARN_ON_ONCE(atomic_read(&ses->se_ref));
2021*4882a593Smuzhiyun 		free_session(ses);
2022*4882a593Smuzhiyun 	}
2023*4882a593Smuzhiyun 	rpc_destroy_wait_queue(&clp->cl_cb_waitq);
2024*4882a593Smuzhiyun 	if (clp->cl_nfsd_dentry) {
2025*4882a593Smuzhiyun 		nfsd_client_rmdir(clp->cl_nfsd_dentry);
2026*4882a593Smuzhiyun 		clp->cl_nfsd_dentry = NULL;
2027*4882a593Smuzhiyun 		wake_up_all(&expiry_wq);
2028*4882a593Smuzhiyun 	}
2029*4882a593Smuzhiyun 	drop_client(clp);
2030*4882a593Smuzhiyun }
2031*4882a593Smuzhiyun 
2032*4882a593Smuzhiyun /* must be called under the client_lock */
2033*4882a593Smuzhiyun static void
unhash_client_locked(struct nfs4_client * clp)2034*4882a593Smuzhiyun unhash_client_locked(struct nfs4_client *clp)
2035*4882a593Smuzhiyun {
2036*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2037*4882a593Smuzhiyun 	struct nfsd4_session *ses;
2038*4882a593Smuzhiyun 
2039*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun 	/* Mark the client as expired! */
2042*4882a593Smuzhiyun 	clp->cl_time = 0;
2043*4882a593Smuzhiyun 	/* Make it invisible */
2044*4882a593Smuzhiyun 	if (!list_empty(&clp->cl_idhash)) {
2045*4882a593Smuzhiyun 		list_del_init(&clp->cl_idhash);
2046*4882a593Smuzhiyun 		if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2047*4882a593Smuzhiyun 			rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2048*4882a593Smuzhiyun 		else
2049*4882a593Smuzhiyun 			rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2050*4882a593Smuzhiyun 	}
2051*4882a593Smuzhiyun 	list_del_init(&clp->cl_lru);
2052*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
2053*4882a593Smuzhiyun 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2054*4882a593Smuzhiyun 		list_del_init(&ses->se_hash);
2055*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
2056*4882a593Smuzhiyun }
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun static void
unhash_client(struct nfs4_client * clp)2059*4882a593Smuzhiyun unhash_client(struct nfs4_client *clp)
2060*4882a593Smuzhiyun {
2061*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
2064*4882a593Smuzhiyun 	unhash_client_locked(clp);
2065*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun 
mark_client_expired_locked(struct nfs4_client * clp)2068*4882a593Smuzhiyun static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2069*4882a593Smuzhiyun {
2070*4882a593Smuzhiyun 	if (atomic_read(&clp->cl_rpc_users))
2071*4882a593Smuzhiyun 		return nfserr_jukebox;
2072*4882a593Smuzhiyun 	unhash_client_locked(clp);
2073*4882a593Smuzhiyun 	return nfs_ok;
2074*4882a593Smuzhiyun }
2075*4882a593Smuzhiyun 
2076*4882a593Smuzhiyun static void
__destroy_client(struct nfs4_client * clp)2077*4882a593Smuzhiyun __destroy_client(struct nfs4_client *clp)
2078*4882a593Smuzhiyun {
2079*4882a593Smuzhiyun 	int i;
2080*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
2081*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
2082*4882a593Smuzhiyun 	struct list_head reaplist;
2083*4882a593Smuzhiyun 
2084*4882a593Smuzhiyun 	INIT_LIST_HEAD(&reaplist);
2085*4882a593Smuzhiyun 	spin_lock(&state_lock);
2086*4882a593Smuzhiyun 	while (!list_empty(&clp->cl_delegations)) {
2087*4882a593Smuzhiyun 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
2088*4882a593Smuzhiyun 		WARN_ON(!unhash_delegation_locked(dp));
2089*4882a593Smuzhiyun 		list_add(&dp->dl_recall_lru, &reaplist);
2090*4882a593Smuzhiyun 	}
2091*4882a593Smuzhiyun 	spin_unlock(&state_lock);
2092*4882a593Smuzhiyun 	while (!list_empty(&reaplist)) {
2093*4882a593Smuzhiyun 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
2094*4882a593Smuzhiyun 		list_del_init(&dp->dl_recall_lru);
2095*4882a593Smuzhiyun 		destroy_unhashed_deleg(dp);
2096*4882a593Smuzhiyun 	}
2097*4882a593Smuzhiyun 	while (!list_empty(&clp->cl_revoked)) {
2098*4882a593Smuzhiyun 		dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2099*4882a593Smuzhiyun 		list_del_init(&dp->dl_recall_lru);
2100*4882a593Smuzhiyun 		nfs4_put_stid(&dp->dl_stid);
2101*4882a593Smuzhiyun 	}
2102*4882a593Smuzhiyun 	while (!list_empty(&clp->cl_openowners)) {
2103*4882a593Smuzhiyun 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
2104*4882a593Smuzhiyun 		nfs4_get_stateowner(&oo->oo_owner);
2105*4882a593Smuzhiyun 		release_openowner(oo);
2106*4882a593Smuzhiyun 	}
2107*4882a593Smuzhiyun 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
2108*4882a593Smuzhiyun 		struct nfs4_stateowner *so, *tmp;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 		list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2111*4882a593Smuzhiyun 					 so_strhash) {
2112*4882a593Smuzhiyun 			/* Should be no openowners at this point */
2113*4882a593Smuzhiyun 			WARN_ON_ONCE(so->so_is_open_owner);
2114*4882a593Smuzhiyun 			remove_blocked_locks(lockowner(so));
2115*4882a593Smuzhiyun 		}
2116*4882a593Smuzhiyun 	}
2117*4882a593Smuzhiyun 	nfsd4_return_all_client_layouts(clp);
2118*4882a593Smuzhiyun 	nfsd4_shutdown_copy(clp);
2119*4882a593Smuzhiyun 	nfsd4_shutdown_callback(clp);
2120*4882a593Smuzhiyun 	if (clp->cl_cb_conn.cb_xprt)
2121*4882a593Smuzhiyun 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
2122*4882a593Smuzhiyun 	free_client(clp);
2123*4882a593Smuzhiyun 	wake_up_all(&expiry_wq);
2124*4882a593Smuzhiyun }
2125*4882a593Smuzhiyun 
2126*4882a593Smuzhiyun static void
destroy_client(struct nfs4_client * clp)2127*4882a593Smuzhiyun destroy_client(struct nfs4_client *clp)
2128*4882a593Smuzhiyun {
2129*4882a593Smuzhiyun 	unhash_client(clp);
2130*4882a593Smuzhiyun 	__destroy_client(clp);
2131*4882a593Smuzhiyun }
2132*4882a593Smuzhiyun 
inc_reclaim_complete(struct nfs4_client * clp)2133*4882a593Smuzhiyun static void inc_reclaim_complete(struct nfs4_client *clp)
2134*4882a593Smuzhiyun {
2135*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun 	if (!nn->track_reclaim_completes)
2138*4882a593Smuzhiyun 		return;
2139*4882a593Smuzhiyun 	if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2140*4882a593Smuzhiyun 		return;
2141*4882a593Smuzhiyun 	if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2142*4882a593Smuzhiyun 			nn->reclaim_str_hashtbl_size) {
2143*4882a593Smuzhiyun 		printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2144*4882a593Smuzhiyun 				clp->net->ns.inum);
2145*4882a593Smuzhiyun 		nfsd4_end_grace(nn);
2146*4882a593Smuzhiyun 	}
2147*4882a593Smuzhiyun }
2148*4882a593Smuzhiyun 
expire_client(struct nfs4_client * clp)2149*4882a593Smuzhiyun static void expire_client(struct nfs4_client *clp)
2150*4882a593Smuzhiyun {
2151*4882a593Smuzhiyun 	unhash_client(clp);
2152*4882a593Smuzhiyun 	nfsd4_client_record_remove(clp);
2153*4882a593Smuzhiyun 	__destroy_client(clp);
2154*4882a593Smuzhiyun }
2155*4882a593Smuzhiyun 
copy_verf(struct nfs4_client * target,nfs4_verifier * source)2156*4882a593Smuzhiyun static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2157*4882a593Smuzhiyun {
2158*4882a593Smuzhiyun 	memcpy(target->cl_verifier.data, source->data,
2159*4882a593Smuzhiyun 			sizeof(target->cl_verifier.data));
2160*4882a593Smuzhiyun }
2161*4882a593Smuzhiyun 
copy_clid(struct nfs4_client * target,struct nfs4_client * source)2162*4882a593Smuzhiyun static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2163*4882a593Smuzhiyun {
2164*4882a593Smuzhiyun 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2165*4882a593Smuzhiyun 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2166*4882a593Smuzhiyun }
2167*4882a593Smuzhiyun 
copy_cred(struct svc_cred * target,struct svc_cred * source)2168*4882a593Smuzhiyun static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2169*4882a593Smuzhiyun {
2170*4882a593Smuzhiyun 	target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2171*4882a593Smuzhiyun 	target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2172*4882a593Smuzhiyun 								GFP_KERNEL);
2173*4882a593Smuzhiyun 	target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2174*4882a593Smuzhiyun 	if ((source->cr_principal && !target->cr_principal) ||
2175*4882a593Smuzhiyun 	    (source->cr_raw_principal && !target->cr_raw_principal) ||
2176*4882a593Smuzhiyun 	    (source->cr_targ_princ && !target->cr_targ_princ))
2177*4882a593Smuzhiyun 		return -ENOMEM;
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun 	target->cr_flavor = source->cr_flavor;
2180*4882a593Smuzhiyun 	target->cr_uid = source->cr_uid;
2181*4882a593Smuzhiyun 	target->cr_gid = source->cr_gid;
2182*4882a593Smuzhiyun 	target->cr_group_info = source->cr_group_info;
2183*4882a593Smuzhiyun 	get_group_info(target->cr_group_info);
2184*4882a593Smuzhiyun 	target->cr_gss_mech = source->cr_gss_mech;
2185*4882a593Smuzhiyun 	if (source->cr_gss_mech)
2186*4882a593Smuzhiyun 		gss_mech_get(source->cr_gss_mech);
2187*4882a593Smuzhiyun 	return 0;
2188*4882a593Smuzhiyun }
2189*4882a593Smuzhiyun 
2190*4882a593Smuzhiyun static int
compare_blob(const struct xdr_netobj * o1,const struct xdr_netobj * o2)2191*4882a593Smuzhiyun compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2192*4882a593Smuzhiyun {
2193*4882a593Smuzhiyun 	if (o1->len < o2->len)
2194*4882a593Smuzhiyun 		return -1;
2195*4882a593Smuzhiyun 	if (o1->len > o2->len)
2196*4882a593Smuzhiyun 		return 1;
2197*4882a593Smuzhiyun 	return memcmp(o1->data, o2->data, o1->len);
2198*4882a593Smuzhiyun }
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun static int
same_verf(nfs4_verifier * v1,nfs4_verifier * v2)2201*4882a593Smuzhiyun same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
2204*4882a593Smuzhiyun }
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun static int
same_clid(clientid_t * cl1,clientid_t * cl2)2207*4882a593Smuzhiyun same_clid(clientid_t *cl1, clientid_t *cl2)
2208*4882a593Smuzhiyun {
2209*4882a593Smuzhiyun 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun 
groups_equal(struct group_info * g1,struct group_info * g2)2212*4882a593Smuzhiyun static bool groups_equal(struct group_info *g1, struct group_info *g2)
2213*4882a593Smuzhiyun {
2214*4882a593Smuzhiyun 	int i;
2215*4882a593Smuzhiyun 
2216*4882a593Smuzhiyun 	if (g1->ngroups != g2->ngroups)
2217*4882a593Smuzhiyun 		return false;
2218*4882a593Smuzhiyun 	for (i=0; i<g1->ngroups; i++)
2219*4882a593Smuzhiyun 		if (!gid_eq(g1->gid[i], g2->gid[i]))
2220*4882a593Smuzhiyun 			return false;
2221*4882a593Smuzhiyun 	return true;
2222*4882a593Smuzhiyun }
2223*4882a593Smuzhiyun 
2224*4882a593Smuzhiyun /*
2225*4882a593Smuzhiyun  * RFC 3530 language requires clid_inuse be returned when the
2226*4882a593Smuzhiyun  * "principal" associated with a requests differs from that previously
2227*4882a593Smuzhiyun  * used.  We use uid, gid's, and gss principal string as our best
2228*4882a593Smuzhiyun  * approximation.  We also don't want to allow non-gss use of a client
2229*4882a593Smuzhiyun  * established using gss: in theory cr_principal should catch that
2230*4882a593Smuzhiyun  * change, but in practice cr_principal can be null even in the gss case
2231*4882a593Smuzhiyun  * since gssd doesn't always pass down a principal string.
2232*4882a593Smuzhiyun  */
is_gss_cred(struct svc_cred * cr)2233*4882a593Smuzhiyun static bool is_gss_cred(struct svc_cred *cr)
2234*4882a593Smuzhiyun {
2235*4882a593Smuzhiyun 	/* Is cr_flavor one of the gss "pseudoflavors"?: */
2236*4882a593Smuzhiyun 	return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2237*4882a593Smuzhiyun }
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 
2240*4882a593Smuzhiyun static bool
same_creds(struct svc_cred * cr1,struct svc_cred * cr2)2241*4882a593Smuzhiyun same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2242*4882a593Smuzhiyun {
2243*4882a593Smuzhiyun 	if ((is_gss_cred(cr1) != is_gss_cred(cr2))
2244*4882a593Smuzhiyun 		|| (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2245*4882a593Smuzhiyun 		|| (!gid_eq(cr1->cr_gid, cr2->cr_gid))
2246*4882a593Smuzhiyun 		|| !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2247*4882a593Smuzhiyun 		return false;
2248*4882a593Smuzhiyun 	/* XXX: check that cr_targ_princ fields match ? */
2249*4882a593Smuzhiyun 	if (cr1->cr_principal == cr2->cr_principal)
2250*4882a593Smuzhiyun 		return true;
2251*4882a593Smuzhiyun 	if (!cr1->cr_principal || !cr2->cr_principal)
2252*4882a593Smuzhiyun 		return false;
2253*4882a593Smuzhiyun 	return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun 
svc_rqst_integrity_protected(struct svc_rqst * rqstp)2256*4882a593Smuzhiyun static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2257*4882a593Smuzhiyun {
2258*4882a593Smuzhiyun 	struct svc_cred *cr = &rqstp->rq_cred;
2259*4882a593Smuzhiyun 	u32 service;
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun 	if (!cr->cr_gss_mech)
2262*4882a593Smuzhiyun 		return false;
2263*4882a593Smuzhiyun 	service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2264*4882a593Smuzhiyun 	return service == RPC_GSS_SVC_INTEGRITY ||
2265*4882a593Smuzhiyun 	       service == RPC_GSS_SVC_PRIVACY;
2266*4882a593Smuzhiyun }
2267*4882a593Smuzhiyun 
nfsd4_mach_creds_match(struct nfs4_client * cl,struct svc_rqst * rqstp)2268*4882a593Smuzhiyun bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
2269*4882a593Smuzhiyun {
2270*4882a593Smuzhiyun 	struct svc_cred *cr = &rqstp->rq_cred;
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 	if (!cl->cl_mach_cred)
2273*4882a593Smuzhiyun 		return true;
2274*4882a593Smuzhiyun 	if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2275*4882a593Smuzhiyun 		return false;
2276*4882a593Smuzhiyun 	if (!svc_rqst_integrity_protected(rqstp))
2277*4882a593Smuzhiyun 		return false;
2278*4882a593Smuzhiyun 	if (cl->cl_cred.cr_raw_principal)
2279*4882a593Smuzhiyun 		return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2280*4882a593Smuzhiyun 						cr->cr_raw_principal);
2281*4882a593Smuzhiyun 	if (!cr->cr_principal)
2282*4882a593Smuzhiyun 		return false;
2283*4882a593Smuzhiyun 	return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2284*4882a593Smuzhiyun }
2285*4882a593Smuzhiyun 
gen_confirm(struct nfs4_client * clp,struct nfsd_net * nn)2286*4882a593Smuzhiyun static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
2287*4882a593Smuzhiyun {
2288*4882a593Smuzhiyun 	__be32 verf[2];
2289*4882a593Smuzhiyun 
2290*4882a593Smuzhiyun 	/*
2291*4882a593Smuzhiyun 	 * This is opaque to client, so no need to byte-swap. Use
2292*4882a593Smuzhiyun 	 * __force to keep sparse happy
2293*4882a593Smuzhiyun 	 */
2294*4882a593Smuzhiyun 	verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
2295*4882a593Smuzhiyun 	verf[1] = (__force __be32)nn->clverifier_counter++;
2296*4882a593Smuzhiyun 	memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
2297*4882a593Smuzhiyun }
2298*4882a593Smuzhiyun 
gen_clid(struct nfs4_client * clp,struct nfsd_net * nn)2299*4882a593Smuzhiyun static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2300*4882a593Smuzhiyun {
2301*4882a593Smuzhiyun 	clp->cl_clientid.cl_boot = (u32)nn->boot_time;
2302*4882a593Smuzhiyun 	clp->cl_clientid.cl_id = nn->clientid_counter++;
2303*4882a593Smuzhiyun 	gen_confirm(clp, nn);
2304*4882a593Smuzhiyun }
2305*4882a593Smuzhiyun 
2306*4882a593Smuzhiyun static struct nfs4_stid *
find_stateid_locked(struct nfs4_client * cl,stateid_t * t)2307*4882a593Smuzhiyun find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
2308*4882a593Smuzhiyun {
2309*4882a593Smuzhiyun 	struct nfs4_stid *ret;
2310*4882a593Smuzhiyun 
2311*4882a593Smuzhiyun 	ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2312*4882a593Smuzhiyun 	if (!ret || !ret->sc_type)
2313*4882a593Smuzhiyun 		return NULL;
2314*4882a593Smuzhiyun 	return ret;
2315*4882a593Smuzhiyun }
2316*4882a593Smuzhiyun 
2317*4882a593Smuzhiyun static struct nfs4_stid *
find_stateid_by_type(struct nfs4_client * cl,stateid_t * t,char typemask)2318*4882a593Smuzhiyun find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
2319*4882a593Smuzhiyun {
2320*4882a593Smuzhiyun 	struct nfs4_stid *s;
2321*4882a593Smuzhiyun 
2322*4882a593Smuzhiyun 	spin_lock(&cl->cl_lock);
2323*4882a593Smuzhiyun 	s = find_stateid_locked(cl, t);
2324*4882a593Smuzhiyun 	if (s != NULL) {
2325*4882a593Smuzhiyun 		if (typemask & s->sc_type)
2326*4882a593Smuzhiyun 			refcount_inc(&s->sc_count);
2327*4882a593Smuzhiyun 		else
2328*4882a593Smuzhiyun 			s = NULL;
2329*4882a593Smuzhiyun 	}
2330*4882a593Smuzhiyun 	spin_unlock(&cl->cl_lock);
2331*4882a593Smuzhiyun 	return s;
2332*4882a593Smuzhiyun }
2333*4882a593Smuzhiyun 
get_nfsdfs_clp(struct inode * inode)2334*4882a593Smuzhiyun static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2335*4882a593Smuzhiyun {
2336*4882a593Smuzhiyun 	struct nfsdfs_client *nc;
2337*4882a593Smuzhiyun 	nc = get_nfsdfs_client(inode);
2338*4882a593Smuzhiyun 	if (!nc)
2339*4882a593Smuzhiyun 		return NULL;
2340*4882a593Smuzhiyun 	return container_of(nc, struct nfs4_client, cl_nfsdfs);
2341*4882a593Smuzhiyun }
2342*4882a593Smuzhiyun 
seq_quote_mem(struct seq_file * m,char * data,int len)2343*4882a593Smuzhiyun static void seq_quote_mem(struct seq_file *m, char *data, int len)
2344*4882a593Smuzhiyun {
2345*4882a593Smuzhiyun 	seq_printf(m, "\"");
2346*4882a593Smuzhiyun 	seq_escape_mem_ascii(m, data, len);
2347*4882a593Smuzhiyun 	seq_printf(m, "\"");
2348*4882a593Smuzhiyun }
2349*4882a593Smuzhiyun 
client_info_show(struct seq_file * m,void * v)2350*4882a593Smuzhiyun static int client_info_show(struct seq_file *m, void *v)
2351*4882a593Smuzhiyun {
2352*4882a593Smuzhiyun 	struct inode *inode = m->private;
2353*4882a593Smuzhiyun 	struct nfs4_client *clp;
2354*4882a593Smuzhiyun 	u64 clid;
2355*4882a593Smuzhiyun 
2356*4882a593Smuzhiyun 	clp = get_nfsdfs_clp(inode);
2357*4882a593Smuzhiyun 	if (!clp)
2358*4882a593Smuzhiyun 		return -ENXIO;
2359*4882a593Smuzhiyun 	memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2360*4882a593Smuzhiyun 	seq_printf(m, "clientid: 0x%llx\n", clid);
2361*4882a593Smuzhiyun 	seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2362*4882a593Smuzhiyun 	seq_printf(m, "name: ");
2363*4882a593Smuzhiyun 	seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2364*4882a593Smuzhiyun 	seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
2365*4882a593Smuzhiyun 	if (clp->cl_nii_domain.data) {
2366*4882a593Smuzhiyun 		seq_printf(m, "Implementation domain: ");
2367*4882a593Smuzhiyun 		seq_quote_mem(m, clp->cl_nii_domain.data,
2368*4882a593Smuzhiyun 					clp->cl_nii_domain.len);
2369*4882a593Smuzhiyun 		seq_printf(m, "\nImplementation name: ");
2370*4882a593Smuzhiyun 		seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
2371*4882a593Smuzhiyun 		seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
2372*4882a593Smuzhiyun 			clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2373*4882a593Smuzhiyun 	}
2374*4882a593Smuzhiyun 	drop_client(clp);
2375*4882a593Smuzhiyun 
2376*4882a593Smuzhiyun 	return 0;
2377*4882a593Smuzhiyun }
2378*4882a593Smuzhiyun 
client_info_open(struct inode * inode,struct file * file)2379*4882a593Smuzhiyun static int client_info_open(struct inode *inode, struct file *file)
2380*4882a593Smuzhiyun {
2381*4882a593Smuzhiyun 	return single_open(file, client_info_show, inode);
2382*4882a593Smuzhiyun }
2383*4882a593Smuzhiyun 
2384*4882a593Smuzhiyun static const struct file_operations client_info_fops = {
2385*4882a593Smuzhiyun 	.open		= client_info_open,
2386*4882a593Smuzhiyun 	.read		= seq_read,
2387*4882a593Smuzhiyun 	.llseek		= seq_lseek,
2388*4882a593Smuzhiyun 	.release	= single_release,
2389*4882a593Smuzhiyun };
2390*4882a593Smuzhiyun 
states_start(struct seq_file * s,loff_t * pos)2391*4882a593Smuzhiyun static void *states_start(struct seq_file *s, loff_t *pos)
2392*4882a593Smuzhiyun 	__acquires(&clp->cl_lock)
2393*4882a593Smuzhiyun {
2394*4882a593Smuzhiyun 	struct nfs4_client *clp = s->private;
2395*4882a593Smuzhiyun 	unsigned long id = *pos;
2396*4882a593Smuzhiyun 	void *ret;
2397*4882a593Smuzhiyun 
2398*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
2399*4882a593Smuzhiyun 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
2400*4882a593Smuzhiyun 	*pos = id;
2401*4882a593Smuzhiyun 	return ret;
2402*4882a593Smuzhiyun }
2403*4882a593Smuzhiyun 
states_next(struct seq_file * s,void * v,loff_t * pos)2404*4882a593Smuzhiyun static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2405*4882a593Smuzhiyun {
2406*4882a593Smuzhiyun 	struct nfs4_client *clp = s->private;
2407*4882a593Smuzhiyun 	unsigned long id = *pos;
2408*4882a593Smuzhiyun 	void *ret;
2409*4882a593Smuzhiyun 
2410*4882a593Smuzhiyun 	id = *pos;
2411*4882a593Smuzhiyun 	id++;
2412*4882a593Smuzhiyun 	ret = idr_get_next_ul(&clp->cl_stateids, &id);
2413*4882a593Smuzhiyun 	*pos = id;
2414*4882a593Smuzhiyun 	return ret;
2415*4882a593Smuzhiyun }
2416*4882a593Smuzhiyun 
states_stop(struct seq_file * s,void * v)2417*4882a593Smuzhiyun static void states_stop(struct seq_file *s, void *v)
2418*4882a593Smuzhiyun 	__releases(&clp->cl_lock)
2419*4882a593Smuzhiyun {
2420*4882a593Smuzhiyun 	struct nfs4_client *clp = s->private;
2421*4882a593Smuzhiyun 
2422*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
2423*4882a593Smuzhiyun }
2424*4882a593Smuzhiyun 
nfs4_show_fname(struct seq_file * s,struct nfsd_file * f)2425*4882a593Smuzhiyun static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2426*4882a593Smuzhiyun {
2427*4882a593Smuzhiyun          seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2428*4882a593Smuzhiyun }
2429*4882a593Smuzhiyun 
nfs4_show_superblock(struct seq_file * s,struct nfsd_file * f)2430*4882a593Smuzhiyun static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
2431*4882a593Smuzhiyun {
2432*4882a593Smuzhiyun 	struct inode *inode = f->nf_inode;
2433*4882a593Smuzhiyun 
2434*4882a593Smuzhiyun 	seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2435*4882a593Smuzhiyun 					MAJOR(inode->i_sb->s_dev),
2436*4882a593Smuzhiyun 					 MINOR(inode->i_sb->s_dev),
2437*4882a593Smuzhiyun 					 inode->i_ino);
2438*4882a593Smuzhiyun }
2439*4882a593Smuzhiyun 
nfs4_show_owner(struct seq_file * s,struct nfs4_stateowner * oo)2440*4882a593Smuzhiyun static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2441*4882a593Smuzhiyun {
2442*4882a593Smuzhiyun 	seq_printf(s, "owner: ");
2443*4882a593Smuzhiyun 	seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2444*4882a593Smuzhiyun }
2445*4882a593Smuzhiyun 
nfs4_show_stateid(struct seq_file * s,stateid_t * stid)2446*4882a593Smuzhiyun static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2447*4882a593Smuzhiyun {
2448*4882a593Smuzhiyun 	seq_printf(s, "0x%.8x", stid->si_generation);
2449*4882a593Smuzhiyun 	seq_printf(s, "%12phN", &stid->si_opaque);
2450*4882a593Smuzhiyun }
2451*4882a593Smuzhiyun 
nfs4_show_open(struct seq_file * s,struct nfs4_stid * st)2452*4882a593Smuzhiyun static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2453*4882a593Smuzhiyun {
2454*4882a593Smuzhiyun 	struct nfs4_ol_stateid *ols;
2455*4882a593Smuzhiyun 	struct nfs4_file *nf;
2456*4882a593Smuzhiyun 	struct nfsd_file *file;
2457*4882a593Smuzhiyun 	struct nfs4_stateowner *oo;
2458*4882a593Smuzhiyun 	unsigned int access, deny;
2459*4882a593Smuzhiyun 
2460*4882a593Smuzhiyun 	if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2461*4882a593Smuzhiyun 		return 0; /* XXX: or SEQ_SKIP? */
2462*4882a593Smuzhiyun 	ols = openlockstateid(st);
2463*4882a593Smuzhiyun 	oo = ols->st_stateowner;
2464*4882a593Smuzhiyun 	nf = st->sc_file;
2465*4882a593Smuzhiyun 	file = find_any_file(nf);
2466*4882a593Smuzhiyun 	if (!file)
2467*4882a593Smuzhiyun 		return 0;
2468*4882a593Smuzhiyun 
2469*4882a593Smuzhiyun 	seq_printf(s, "- ");
2470*4882a593Smuzhiyun 	nfs4_show_stateid(s, &st->sc_stateid);
2471*4882a593Smuzhiyun 	seq_printf(s, ": { type: open, ");
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 	access = bmap_to_share_mode(ols->st_access_bmap);
2474*4882a593Smuzhiyun 	deny   = bmap_to_share_mode(ols->st_deny_bmap);
2475*4882a593Smuzhiyun 
2476*4882a593Smuzhiyun 	seq_printf(s, "access: %s%s, ",
2477*4882a593Smuzhiyun 		access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2478*4882a593Smuzhiyun 		access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2479*4882a593Smuzhiyun 	seq_printf(s, "deny: %s%s, ",
2480*4882a593Smuzhiyun 		deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2481*4882a593Smuzhiyun 		deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2482*4882a593Smuzhiyun 
2483*4882a593Smuzhiyun 	nfs4_show_superblock(s, file);
2484*4882a593Smuzhiyun 	seq_printf(s, ", ");
2485*4882a593Smuzhiyun 	nfs4_show_fname(s, file);
2486*4882a593Smuzhiyun 	seq_printf(s, ", ");
2487*4882a593Smuzhiyun 	nfs4_show_owner(s, oo);
2488*4882a593Smuzhiyun 	seq_printf(s, " }\n");
2489*4882a593Smuzhiyun 	nfsd_file_put(file);
2490*4882a593Smuzhiyun 
2491*4882a593Smuzhiyun 	return 0;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun 
nfs4_show_lock(struct seq_file * s,struct nfs4_stid * st)2494*4882a593Smuzhiyun static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2495*4882a593Smuzhiyun {
2496*4882a593Smuzhiyun 	struct nfs4_ol_stateid *ols;
2497*4882a593Smuzhiyun 	struct nfs4_file *nf;
2498*4882a593Smuzhiyun 	struct nfsd_file *file;
2499*4882a593Smuzhiyun 	struct nfs4_stateowner *oo;
2500*4882a593Smuzhiyun 
2501*4882a593Smuzhiyun 	ols = openlockstateid(st);
2502*4882a593Smuzhiyun 	oo = ols->st_stateowner;
2503*4882a593Smuzhiyun 	nf = st->sc_file;
2504*4882a593Smuzhiyun 	file = find_any_file(nf);
2505*4882a593Smuzhiyun 	if (!file)
2506*4882a593Smuzhiyun 		return 0;
2507*4882a593Smuzhiyun 
2508*4882a593Smuzhiyun 	seq_printf(s, "- ");
2509*4882a593Smuzhiyun 	nfs4_show_stateid(s, &st->sc_stateid);
2510*4882a593Smuzhiyun 	seq_printf(s, ": { type: lock, ");
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun 	/*
2513*4882a593Smuzhiyun 	 * Note: a lock stateid isn't really the same thing as a lock,
2514*4882a593Smuzhiyun 	 * it's the locking state held by one owner on a file, and there
2515*4882a593Smuzhiyun 	 * may be multiple (or no) lock ranges associated with it.
2516*4882a593Smuzhiyun 	 * (Same for the matter is true of open stateids.)
2517*4882a593Smuzhiyun 	 */
2518*4882a593Smuzhiyun 
2519*4882a593Smuzhiyun 	nfs4_show_superblock(s, file);
2520*4882a593Smuzhiyun 	/* XXX: open stateid? */
2521*4882a593Smuzhiyun 	seq_printf(s, ", ");
2522*4882a593Smuzhiyun 	nfs4_show_fname(s, file);
2523*4882a593Smuzhiyun 	seq_printf(s, ", ");
2524*4882a593Smuzhiyun 	nfs4_show_owner(s, oo);
2525*4882a593Smuzhiyun 	seq_printf(s, " }\n");
2526*4882a593Smuzhiyun 	nfsd_file_put(file);
2527*4882a593Smuzhiyun 
2528*4882a593Smuzhiyun 	return 0;
2529*4882a593Smuzhiyun }
2530*4882a593Smuzhiyun 
nfs4_show_deleg(struct seq_file * s,struct nfs4_stid * st)2531*4882a593Smuzhiyun static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2532*4882a593Smuzhiyun {
2533*4882a593Smuzhiyun 	struct nfs4_delegation *ds;
2534*4882a593Smuzhiyun 	struct nfs4_file *nf;
2535*4882a593Smuzhiyun 	struct nfsd_file *file;
2536*4882a593Smuzhiyun 
2537*4882a593Smuzhiyun 	ds = delegstateid(st);
2538*4882a593Smuzhiyun 	nf = st->sc_file;
2539*4882a593Smuzhiyun 	file = find_deleg_file(nf);
2540*4882a593Smuzhiyun 	if (!file)
2541*4882a593Smuzhiyun 		return 0;
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	seq_printf(s, "- ");
2544*4882a593Smuzhiyun 	nfs4_show_stateid(s, &st->sc_stateid);
2545*4882a593Smuzhiyun 	seq_printf(s, ": { type: deleg, ");
2546*4882a593Smuzhiyun 
2547*4882a593Smuzhiyun 	/* Kinda dead code as long as we only support read delegs: */
2548*4882a593Smuzhiyun 	seq_printf(s, "access: %s, ",
2549*4882a593Smuzhiyun 		ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2550*4882a593Smuzhiyun 
2551*4882a593Smuzhiyun 	/* XXX: lease time, whether it's being recalled. */
2552*4882a593Smuzhiyun 
2553*4882a593Smuzhiyun 	nfs4_show_superblock(s, file);
2554*4882a593Smuzhiyun 	seq_printf(s, ", ");
2555*4882a593Smuzhiyun 	nfs4_show_fname(s, file);
2556*4882a593Smuzhiyun 	seq_printf(s, " }\n");
2557*4882a593Smuzhiyun 	nfsd_file_put(file);
2558*4882a593Smuzhiyun 
2559*4882a593Smuzhiyun 	return 0;
2560*4882a593Smuzhiyun }
2561*4882a593Smuzhiyun 
nfs4_show_layout(struct seq_file * s,struct nfs4_stid * st)2562*4882a593Smuzhiyun static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2563*4882a593Smuzhiyun {
2564*4882a593Smuzhiyun 	struct nfs4_layout_stateid *ls;
2565*4882a593Smuzhiyun 	struct nfsd_file *file;
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2568*4882a593Smuzhiyun 	file = ls->ls_file;
2569*4882a593Smuzhiyun 
2570*4882a593Smuzhiyun 	seq_printf(s, "- ");
2571*4882a593Smuzhiyun 	nfs4_show_stateid(s, &st->sc_stateid);
2572*4882a593Smuzhiyun 	seq_printf(s, ": { type: layout, ");
2573*4882a593Smuzhiyun 
2574*4882a593Smuzhiyun 	/* XXX: What else would be useful? */
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 	nfs4_show_superblock(s, file);
2577*4882a593Smuzhiyun 	seq_printf(s, ", ");
2578*4882a593Smuzhiyun 	nfs4_show_fname(s, file);
2579*4882a593Smuzhiyun 	seq_printf(s, " }\n");
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 	return 0;
2582*4882a593Smuzhiyun }
2583*4882a593Smuzhiyun 
states_show(struct seq_file * s,void * v)2584*4882a593Smuzhiyun static int states_show(struct seq_file *s, void *v)
2585*4882a593Smuzhiyun {
2586*4882a593Smuzhiyun 	struct nfs4_stid *st = v;
2587*4882a593Smuzhiyun 
2588*4882a593Smuzhiyun 	switch (st->sc_type) {
2589*4882a593Smuzhiyun 	case NFS4_OPEN_STID:
2590*4882a593Smuzhiyun 		return nfs4_show_open(s, st);
2591*4882a593Smuzhiyun 	case NFS4_LOCK_STID:
2592*4882a593Smuzhiyun 		return nfs4_show_lock(s, st);
2593*4882a593Smuzhiyun 	case NFS4_DELEG_STID:
2594*4882a593Smuzhiyun 		return nfs4_show_deleg(s, st);
2595*4882a593Smuzhiyun 	case NFS4_LAYOUT_STID:
2596*4882a593Smuzhiyun 		return nfs4_show_layout(s, st);
2597*4882a593Smuzhiyun 	default:
2598*4882a593Smuzhiyun 		return 0; /* XXX: or SEQ_SKIP? */
2599*4882a593Smuzhiyun 	}
2600*4882a593Smuzhiyun 	/* XXX: copy stateids? */
2601*4882a593Smuzhiyun }
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun static struct seq_operations states_seq_ops = {
2604*4882a593Smuzhiyun 	.start = states_start,
2605*4882a593Smuzhiyun 	.next = states_next,
2606*4882a593Smuzhiyun 	.stop = states_stop,
2607*4882a593Smuzhiyun 	.show = states_show
2608*4882a593Smuzhiyun };
2609*4882a593Smuzhiyun 
client_states_open(struct inode * inode,struct file * file)2610*4882a593Smuzhiyun static int client_states_open(struct inode *inode, struct file *file)
2611*4882a593Smuzhiyun {
2612*4882a593Smuzhiyun 	struct seq_file *s;
2613*4882a593Smuzhiyun 	struct nfs4_client *clp;
2614*4882a593Smuzhiyun 	int ret;
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	clp = get_nfsdfs_clp(inode);
2617*4882a593Smuzhiyun 	if (!clp)
2618*4882a593Smuzhiyun 		return -ENXIO;
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 	ret = seq_open(file, &states_seq_ops);
2621*4882a593Smuzhiyun 	if (ret)
2622*4882a593Smuzhiyun 		return ret;
2623*4882a593Smuzhiyun 	s = file->private_data;
2624*4882a593Smuzhiyun 	s->private = clp;
2625*4882a593Smuzhiyun 	return 0;
2626*4882a593Smuzhiyun }
2627*4882a593Smuzhiyun 
client_opens_release(struct inode * inode,struct file * file)2628*4882a593Smuzhiyun static int client_opens_release(struct inode *inode, struct file *file)
2629*4882a593Smuzhiyun {
2630*4882a593Smuzhiyun 	struct seq_file *m = file->private_data;
2631*4882a593Smuzhiyun 	struct nfs4_client *clp = m->private;
2632*4882a593Smuzhiyun 
2633*4882a593Smuzhiyun 	/* XXX: alternatively, we could get/drop in seq start/stop */
2634*4882a593Smuzhiyun 	drop_client(clp);
2635*4882a593Smuzhiyun 	return 0;
2636*4882a593Smuzhiyun }
2637*4882a593Smuzhiyun 
2638*4882a593Smuzhiyun static const struct file_operations client_states_fops = {
2639*4882a593Smuzhiyun 	.open		= client_states_open,
2640*4882a593Smuzhiyun 	.read		= seq_read,
2641*4882a593Smuzhiyun 	.llseek		= seq_lseek,
2642*4882a593Smuzhiyun 	.release	= client_opens_release,
2643*4882a593Smuzhiyun };
2644*4882a593Smuzhiyun 
2645*4882a593Smuzhiyun /*
2646*4882a593Smuzhiyun  * Normally we refuse to destroy clients that are in use, but here the
2647*4882a593Smuzhiyun  * administrator is telling us to just do it.  We also want to wait
2648*4882a593Smuzhiyun  * so the caller has a guarantee that the client's locks are gone by
2649*4882a593Smuzhiyun  * the time the write returns:
2650*4882a593Smuzhiyun  */
force_expire_client(struct nfs4_client * clp)2651*4882a593Smuzhiyun static void force_expire_client(struct nfs4_client *clp)
2652*4882a593Smuzhiyun {
2653*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2654*4882a593Smuzhiyun 	bool already_expired;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
2657*4882a593Smuzhiyun 	clp->cl_time = 0;
2658*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
2659*4882a593Smuzhiyun 
2660*4882a593Smuzhiyun 	wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2661*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
2662*4882a593Smuzhiyun 	already_expired = list_empty(&clp->cl_lru);
2663*4882a593Smuzhiyun 	if (!already_expired)
2664*4882a593Smuzhiyun 		unhash_client_locked(clp);
2665*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
2666*4882a593Smuzhiyun 
2667*4882a593Smuzhiyun 	if (!already_expired)
2668*4882a593Smuzhiyun 		expire_client(clp);
2669*4882a593Smuzhiyun 	else
2670*4882a593Smuzhiyun 		wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2671*4882a593Smuzhiyun }
2672*4882a593Smuzhiyun 
client_ctl_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)2673*4882a593Smuzhiyun static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2674*4882a593Smuzhiyun 				   size_t size, loff_t *pos)
2675*4882a593Smuzhiyun {
2676*4882a593Smuzhiyun 	char *data;
2677*4882a593Smuzhiyun 	struct nfs4_client *clp;
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	data = simple_transaction_get(file, buf, size);
2680*4882a593Smuzhiyun 	if (IS_ERR(data))
2681*4882a593Smuzhiyun 		return PTR_ERR(data);
2682*4882a593Smuzhiyun 	if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2683*4882a593Smuzhiyun 		return -EINVAL;
2684*4882a593Smuzhiyun 	clp = get_nfsdfs_clp(file_inode(file));
2685*4882a593Smuzhiyun 	if (!clp)
2686*4882a593Smuzhiyun 		return -ENXIO;
2687*4882a593Smuzhiyun 	force_expire_client(clp);
2688*4882a593Smuzhiyun 	drop_client(clp);
2689*4882a593Smuzhiyun 	return 7;
2690*4882a593Smuzhiyun }
2691*4882a593Smuzhiyun 
2692*4882a593Smuzhiyun static const struct file_operations client_ctl_fops = {
2693*4882a593Smuzhiyun 	.write		= client_ctl_write,
2694*4882a593Smuzhiyun 	.release	= simple_transaction_release,
2695*4882a593Smuzhiyun };
2696*4882a593Smuzhiyun 
2697*4882a593Smuzhiyun static const struct tree_descr client_files[] = {
2698*4882a593Smuzhiyun 	[0] = {"info", &client_info_fops, S_IRUSR},
2699*4882a593Smuzhiyun 	[1] = {"states", &client_states_fops, S_IRUSR},
2700*4882a593Smuzhiyun 	[2] = {"ctl", &client_ctl_fops, S_IWUSR},
2701*4882a593Smuzhiyun 	[3] = {""},
2702*4882a593Smuzhiyun };
2703*4882a593Smuzhiyun 
create_client(struct xdr_netobj name,struct svc_rqst * rqstp,nfs4_verifier * verf)2704*4882a593Smuzhiyun static struct nfs4_client *create_client(struct xdr_netobj name,
2705*4882a593Smuzhiyun 		struct svc_rqst *rqstp, nfs4_verifier *verf)
2706*4882a593Smuzhiyun {
2707*4882a593Smuzhiyun 	struct nfs4_client *clp;
2708*4882a593Smuzhiyun 	struct sockaddr *sa = svc_addr(rqstp);
2709*4882a593Smuzhiyun 	int ret;
2710*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
2711*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2712*4882a593Smuzhiyun 
2713*4882a593Smuzhiyun 	clp = alloc_client(name);
2714*4882a593Smuzhiyun 	if (clp == NULL)
2715*4882a593Smuzhiyun 		return NULL;
2716*4882a593Smuzhiyun 
2717*4882a593Smuzhiyun 	ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2718*4882a593Smuzhiyun 	if (ret) {
2719*4882a593Smuzhiyun 		free_client(clp);
2720*4882a593Smuzhiyun 		return NULL;
2721*4882a593Smuzhiyun 	}
2722*4882a593Smuzhiyun 	gen_clid(clp, nn);
2723*4882a593Smuzhiyun 	kref_init(&clp->cl_nfsdfs.cl_ref);
2724*4882a593Smuzhiyun 	nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
2725*4882a593Smuzhiyun 	clp->cl_time = ktime_get_boottime_seconds();
2726*4882a593Smuzhiyun 	clear_bit(0, &clp->cl_cb_slot_busy);
2727*4882a593Smuzhiyun 	copy_verf(clp, verf);
2728*4882a593Smuzhiyun 	memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
2729*4882a593Smuzhiyun 	clp->cl_cb_session = NULL;
2730*4882a593Smuzhiyun 	clp->net = net;
2731*4882a593Smuzhiyun 	clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
2732*4882a593Smuzhiyun 			clp->cl_clientid.cl_id - nn->clientid_base,
2733*4882a593Smuzhiyun 			client_files);
2734*4882a593Smuzhiyun 	if (!clp->cl_nfsd_dentry) {
2735*4882a593Smuzhiyun 		free_client(clp);
2736*4882a593Smuzhiyun 		return NULL;
2737*4882a593Smuzhiyun 	}
2738*4882a593Smuzhiyun 	return clp;
2739*4882a593Smuzhiyun }
2740*4882a593Smuzhiyun 
2741*4882a593Smuzhiyun static void
add_clp_to_name_tree(struct nfs4_client * new_clp,struct rb_root * root)2742*4882a593Smuzhiyun add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2743*4882a593Smuzhiyun {
2744*4882a593Smuzhiyun 	struct rb_node **new = &(root->rb_node), *parent = NULL;
2745*4882a593Smuzhiyun 	struct nfs4_client *clp;
2746*4882a593Smuzhiyun 
2747*4882a593Smuzhiyun 	while (*new) {
2748*4882a593Smuzhiyun 		clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2749*4882a593Smuzhiyun 		parent = *new;
2750*4882a593Smuzhiyun 
2751*4882a593Smuzhiyun 		if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2752*4882a593Smuzhiyun 			new = &((*new)->rb_left);
2753*4882a593Smuzhiyun 		else
2754*4882a593Smuzhiyun 			new = &((*new)->rb_right);
2755*4882a593Smuzhiyun 	}
2756*4882a593Smuzhiyun 
2757*4882a593Smuzhiyun 	rb_link_node(&new_clp->cl_namenode, parent, new);
2758*4882a593Smuzhiyun 	rb_insert_color(&new_clp->cl_namenode, root);
2759*4882a593Smuzhiyun }
2760*4882a593Smuzhiyun 
2761*4882a593Smuzhiyun static struct nfs4_client *
find_clp_in_name_tree(struct xdr_netobj * name,struct rb_root * root)2762*4882a593Smuzhiyun find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2763*4882a593Smuzhiyun {
2764*4882a593Smuzhiyun 	int cmp;
2765*4882a593Smuzhiyun 	struct rb_node *node = root->rb_node;
2766*4882a593Smuzhiyun 	struct nfs4_client *clp;
2767*4882a593Smuzhiyun 
2768*4882a593Smuzhiyun 	while (node) {
2769*4882a593Smuzhiyun 		clp = rb_entry(node, struct nfs4_client, cl_namenode);
2770*4882a593Smuzhiyun 		cmp = compare_blob(&clp->cl_name, name);
2771*4882a593Smuzhiyun 		if (cmp > 0)
2772*4882a593Smuzhiyun 			node = node->rb_left;
2773*4882a593Smuzhiyun 		else if (cmp < 0)
2774*4882a593Smuzhiyun 			node = node->rb_right;
2775*4882a593Smuzhiyun 		else
2776*4882a593Smuzhiyun 			return clp;
2777*4882a593Smuzhiyun 	}
2778*4882a593Smuzhiyun 	return NULL;
2779*4882a593Smuzhiyun }
2780*4882a593Smuzhiyun 
2781*4882a593Smuzhiyun static void
add_to_unconfirmed(struct nfs4_client * clp)2782*4882a593Smuzhiyun add_to_unconfirmed(struct nfs4_client *clp)
2783*4882a593Smuzhiyun {
2784*4882a593Smuzhiyun 	unsigned int idhashval;
2785*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2788*4882a593Smuzhiyun 
2789*4882a593Smuzhiyun 	clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2790*4882a593Smuzhiyun 	add_clp_to_name_tree(clp, &nn->unconf_name_tree);
2791*4882a593Smuzhiyun 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2792*4882a593Smuzhiyun 	list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
2793*4882a593Smuzhiyun 	renew_client_locked(clp);
2794*4882a593Smuzhiyun }
2795*4882a593Smuzhiyun 
2796*4882a593Smuzhiyun static void
move_to_confirmed(struct nfs4_client * clp)2797*4882a593Smuzhiyun move_to_confirmed(struct nfs4_client *clp)
2798*4882a593Smuzhiyun {
2799*4882a593Smuzhiyun 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
2800*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2801*4882a593Smuzhiyun 
2802*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2803*4882a593Smuzhiyun 
2804*4882a593Smuzhiyun 	dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
2805*4882a593Smuzhiyun 	list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
2806*4882a593Smuzhiyun 	rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2807*4882a593Smuzhiyun 	add_clp_to_name_tree(clp, &nn->conf_name_tree);
2808*4882a593Smuzhiyun 	set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
2809*4882a593Smuzhiyun 	renew_client_locked(clp);
2810*4882a593Smuzhiyun }
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun static struct nfs4_client *
find_client_in_id_table(struct list_head * tbl,clientid_t * clid,bool sessions)2813*4882a593Smuzhiyun find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
2814*4882a593Smuzhiyun {
2815*4882a593Smuzhiyun 	struct nfs4_client *clp;
2816*4882a593Smuzhiyun 	unsigned int idhashval = clientid_hashval(clid->cl_id);
2817*4882a593Smuzhiyun 
2818*4882a593Smuzhiyun 	list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
2819*4882a593Smuzhiyun 		if (same_clid(&clp->cl_clientid, clid)) {
2820*4882a593Smuzhiyun 			if ((bool)clp->cl_minorversion != sessions)
2821*4882a593Smuzhiyun 				return NULL;
2822*4882a593Smuzhiyun 			renew_client_locked(clp);
2823*4882a593Smuzhiyun 			return clp;
2824*4882a593Smuzhiyun 		}
2825*4882a593Smuzhiyun 	}
2826*4882a593Smuzhiyun 	return NULL;
2827*4882a593Smuzhiyun }
2828*4882a593Smuzhiyun 
2829*4882a593Smuzhiyun static struct nfs4_client *
find_confirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)2830*4882a593Smuzhiyun find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2831*4882a593Smuzhiyun {
2832*4882a593Smuzhiyun 	struct list_head *tbl = nn->conf_id_hashtbl;
2833*4882a593Smuzhiyun 
2834*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2835*4882a593Smuzhiyun 	return find_client_in_id_table(tbl, clid, sessions);
2836*4882a593Smuzhiyun }
2837*4882a593Smuzhiyun 
2838*4882a593Smuzhiyun static struct nfs4_client *
find_unconfirmed_client(clientid_t * clid,bool sessions,struct nfsd_net * nn)2839*4882a593Smuzhiyun find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2840*4882a593Smuzhiyun {
2841*4882a593Smuzhiyun 	struct list_head *tbl = nn->unconf_id_hashtbl;
2842*4882a593Smuzhiyun 
2843*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2844*4882a593Smuzhiyun 	return find_client_in_id_table(tbl, clid, sessions);
2845*4882a593Smuzhiyun }
2846*4882a593Smuzhiyun 
clp_used_exchangeid(struct nfs4_client * clp)2847*4882a593Smuzhiyun static bool clp_used_exchangeid(struct nfs4_client *clp)
2848*4882a593Smuzhiyun {
2849*4882a593Smuzhiyun 	return clp->cl_exchange_flags != 0;
2850*4882a593Smuzhiyun }
2851*4882a593Smuzhiyun 
2852*4882a593Smuzhiyun static struct nfs4_client *
find_confirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)2853*4882a593Smuzhiyun find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2854*4882a593Smuzhiyun {
2855*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2856*4882a593Smuzhiyun 	return find_clp_in_name_tree(name, &nn->conf_name_tree);
2857*4882a593Smuzhiyun }
2858*4882a593Smuzhiyun 
2859*4882a593Smuzhiyun static struct nfs4_client *
find_unconfirmed_client_by_name(struct xdr_netobj * name,struct nfsd_net * nn)2860*4882a593Smuzhiyun find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2861*4882a593Smuzhiyun {
2862*4882a593Smuzhiyun 	lockdep_assert_held(&nn->client_lock);
2863*4882a593Smuzhiyun 	return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2864*4882a593Smuzhiyun }
2865*4882a593Smuzhiyun 
2866*4882a593Smuzhiyun static void
gen_callback(struct nfs4_client * clp,struct nfsd4_setclientid * se,struct svc_rqst * rqstp)2867*4882a593Smuzhiyun gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2868*4882a593Smuzhiyun {
2869*4882a593Smuzhiyun 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2870*4882a593Smuzhiyun 	struct sockaddr	*sa = svc_addr(rqstp);
2871*4882a593Smuzhiyun 	u32 scopeid = rpc_get_scope_id(sa);
2872*4882a593Smuzhiyun 	unsigned short expected_family;
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun 	/* Currently, we only support tcp and tcp6 for the callback channel */
2875*4882a593Smuzhiyun 	if (se->se_callback_netid_len == 3 &&
2876*4882a593Smuzhiyun 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
2877*4882a593Smuzhiyun 		expected_family = AF_INET;
2878*4882a593Smuzhiyun 	else if (se->se_callback_netid_len == 4 &&
2879*4882a593Smuzhiyun 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2880*4882a593Smuzhiyun 		expected_family = AF_INET6;
2881*4882a593Smuzhiyun 	else
2882*4882a593Smuzhiyun 		goto out_err;
2883*4882a593Smuzhiyun 
2884*4882a593Smuzhiyun 	conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2885*4882a593Smuzhiyun 					    se->se_callback_addr_len,
2886*4882a593Smuzhiyun 					    (struct sockaddr *)&conn->cb_addr,
2887*4882a593Smuzhiyun 					    sizeof(conn->cb_addr));
2888*4882a593Smuzhiyun 
2889*4882a593Smuzhiyun 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2890*4882a593Smuzhiyun 		goto out_err;
2891*4882a593Smuzhiyun 
2892*4882a593Smuzhiyun 	if (conn->cb_addr.ss_family == AF_INET6)
2893*4882a593Smuzhiyun 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2894*4882a593Smuzhiyun 
2895*4882a593Smuzhiyun 	conn->cb_prog = se->se_callback_prog;
2896*4882a593Smuzhiyun 	conn->cb_ident = se->se_callback_ident;
2897*4882a593Smuzhiyun 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2898*4882a593Smuzhiyun 	trace_nfsd_cb_args(clp, conn);
2899*4882a593Smuzhiyun 	return;
2900*4882a593Smuzhiyun out_err:
2901*4882a593Smuzhiyun 	conn->cb_addr.ss_family = AF_UNSPEC;
2902*4882a593Smuzhiyun 	conn->cb_addrlen = 0;
2903*4882a593Smuzhiyun 	trace_nfsd_cb_nodelegs(clp);
2904*4882a593Smuzhiyun 	return;
2905*4882a593Smuzhiyun }
2906*4882a593Smuzhiyun 
2907*4882a593Smuzhiyun /*
2908*4882a593Smuzhiyun  * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2909*4882a593Smuzhiyun  */
2910*4882a593Smuzhiyun static void
nfsd4_store_cache_entry(struct nfsd4_compoundres * resp)2911*4882a593Smuzhiyun nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2912*4882a593Smuzhiyun {
2913*4882a593Smuzhiyun 	struct xdr_buf *buf = resp->xdr.buf;
2914*4882a593Smuzhiyun 	struct nfsd4_slot *slot = resp->cstate.slot;
2915*4882a593Smuzhiyun 	unsigned int base;
2916*4882a593Smuzhiyun 
2917*4882a593Smuzhiyun 	dprintk("--> %s slot %p\n", __func__, slot);
2918*4882a593Smuzhiyun 
2919*4882a593Smuzhiyun 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2920*4882a593Smuzhiyun 	slot->sl_opcnt = resp->opcnt;
2921*4882a593Smuzhiyun 	slot->sl_status = resp->cstate.status;
2922*4882a593Smuzhiyun 	free_svc_cred(&slot->sl_cred);
2923*4882a593Smuzhiyun 	copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
2924*4882a593Smuzhiyun 
2925*4882a593Smuzhiyun 	if (!nfsd4_cache_this(resp)) {
2926*4882a593Smuzhiyun 		slot->sl_flags &= ~NFSD4_SLOT_CACHED;
2927*4882a593Smuzhiyun 		return;
2928*4882a593Smuzhiyun 	}
2929*4882a593Smuzhiyun 	slot->sl_flags |= NFSD4_SLOT_CACHED;
2930*4882a593Smuzhiyun 
2931*4882a593Smuzhiyun 	base = resp->cstate.data_offset;
2932*4882a593Smuzhiyun 	slot->sl_datalen = buf->len - base;
2933*4882a593Smuzhiyun 	if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2934*4882a593Smuzhiyun 		WARN(1, "%s: sessions DRC could not cache compound\n",
2935*4882a593Smuzhiyun 		     __func__);
2936*4882a593Smuzhiyun 	return;
2937*4882a593Smuzhiyun }
2938*4882a593Smuzhiyun 
2939*4882a593Smuzhiyun /*
2940*4882a593Smuzhiyun  * Encode the replay sequence operation from the slot values.
2941*4882a593Smuzhiyun  * If cachethis is FALSE encode the uncached rep error on the next
2942*4882a593Smuzhiyun  * operation which sets resp->p and increments resp->opcnt for
2943*4882a593Smuzhiyun  * nfs4svc_encode_compoundres.
2944*4882a593Smuzhiyun  *
2945*4882a593Smuzhiyun  */
2946*4882a593Smuzhiyun static __be32
nfsd4_enc_sequence_replay(struct nfsd4_compoundargs * args,struct nfsd4_compoundres * resp)2947*4882a593Smuzhiyun nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2948*4882a593Smuzhiyun 			  struct nfsd4_compoundres *resp)
2949*4882a593Smuzhiyun {
2950*4882a593Smuzhiyun 	struct nfsd4_op *op;
2951*4882a593Smuzhiyun 	struct nfsd4_slot *slot = resp->cstate.slot;
2952*4882a593Smuzhiyun 
2953*4882a593Smuzhiyun 	/* Encode the replayed sequence operation */
2954*4882a593Smuzhiyun 	op = &args->ops[resp->opcnt - 1];
2955*4882a593Smuzhiyun 	nfsd4_encode_operation(resp, op);
2956*4882a593Smuzhiyun 
2957*4882a593Smuzhiyun 	if (slot->sl_flags & NFSD4_SLOT_CACHED)
2958*4882a593Smuzhiyun 		return op->status;
2959*4882a593Smuzhiyun 	if (args->opcnt == 1) {
2960*4882a593Smuzhiyun 		/*
2961*4882a593Smuzhiyun 		 * The original operation wasn't a solo sequence--we
2962*4882a593Smuzhiyun 		 * always cache those--so this retry must not match the
2963*4882a593Smuzhiyun 		 * original:
2964*4882a593Smuzhiyun 		 */
2965*4882a593Smuzhiyun 		op->status = nfserr_seq_false_retry;
2966*4882a593Smuzhiyun 	} else {
2967*4882a593Smuzhiyun 		op = &args->ops[resp->opcnt++];
2968*4882a593Smuzhiyun 		op->status = nfserr_retry_uncached_rep;
2969*4882a593Smuzhiyun 		nfsd4_encode_operation(resp, op);
2970*4882a593Smuzhiyun 	}
2971*4882a593Smuzhiyun 	return op->status;
2972*4882a593Smuzhiyun }
2973*4882a593Smuzhiyun 
2974*4882a593Smuzhiyun /*
2975*4882a593Smuzhiyun  * The sequence operation is not cached because we can use the slot and
2976*4882a593Smuzhiyun  * session values.
2977*4882a593Smuzhiyun  */
2978*4882a593Smuzhiyun static __be32
nfsd4_replay_cache_entry(struct nfsd4_compoundres * resp,struct nfsd4_sequence * seq)2979*4882a593Smuzhiyun nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2980*4882a593Smuzhiyun 			 struct nfsd4_sequence *seq)
2981*4882a593Smuzhiyun {
2982*4882a593Smuzhiyun 	struct nfsd4_slot *slot = resp->cstate.slot;
2983*4882a593Smuzhiyun 	struct xdr_stream *xdr = &resp->xdr;
2984*4882a593Smuzhiyun 	__be32 *p;
2985*4882a593Smuzhiyun 	__be32 status;
2986*4882a593Smuzhiyun 
2987*4882a593Smuzhiyun 	dprintk("--> %s slot %p\n", __func__, slot);
2988*4882a593Smuzhiyun 
2989*4882a593Smuzhiyun 	status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2990*4882a593Smuzhiyun 	if (status)
2991*4882a593Smuzhiyun 		return status;
2992*4882a593Smuzhiyun 
2993*4882a593Smuzhiyun 	p = xdr_reserve_space(xdr, slot->sl_datalen);
2994*4882a593Smuzhiyun 	if (!p) {
2995*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
2996*4882a593Smuzhiyun 		return nfserr_serverfault;
2997*4882a593Smuzhiyun 	}
2998*4882a593Smuzhiyun 	xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2999*4882a593Smuzhiyun 	xdr_commit_encode(xdr);
3000*4882a593Smuzhiyun 
3001*4882a593Smuzhiyun 	resp->opcnt = slot->sl_opcnt;
3002*4882a593Smuzhiyun 	return slot->sl_status;
3003*4882a593Smuzhiyun }
3004*4882a593Smuzhiyun 
3005*4882a593Smuzhiyun /*
3006*4882a593Smuzhiyun  * Set the exchange_id flags returned by the server.
3007*4882a593Smuzhiyun  */
3008*4882a593Smuzhiyun static void
nfsd4_set_ex_flags(struct nfs4_client * new,struct nfsd4_exchange_id * clid)3009*4882a593Smuzhiyun nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3010*4882a593Smuzhiyun {
3011*4882a593Smuzhiyun #ifdef CONFIG_NFSD_PNFS
3012*4882a593Smuzhiyun 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3013*4882a593Smuzhiyun #else
3014*4882a593Smuzhiyun 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
3015*4882a593Smuzhiyun #endif
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun 	/* Referrals are supported, Migration is not. */
3018*4882a593Smuzhiyun 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3019*4882a593Smuzhiyun 
3020*4882a593Smuzhiyun 	/* set the wire flags to return to client. */
3021*4882a593Smuzhiyun 	clid->flags = new->cl_exchange_flags;
3022*4882a593Smuzhiyun }
3023*4882a593Smuzhiyun 
client_has_openowners(struct nfs4_client * clp)3024*4882a593Smuzhiyun static bool client_has_openowners(struct nfs4_client *clp)
3025*4882a593Smuzhiyun {
3026*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
3027*4882a593Smuzhiyun 
3028*4882a593Smuzhiyun 	list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3029*4882a593Smuzhiyun 		if (!list_empty(&oo->oo_owner.so_stateids))
3030*4882a593Smuzhiyun 			return true;
3031*4882a593Smuzhiyun 	}
3032*4882a593Smuzhiyun 	return false;
3033*4882a593Smuzhiyun }
3034*4882a593Smuzhiyun 
client_has_state(struct nfs4_client * clp)3035*4882a593Smuzhiyun static bool client_has_state(struct nfs4_client *clp)
3036*4882a593Smuzhiyun {
3037*4882a593Smuzhiyun 	return client_has_openowners(clp)
3038*4882a593Smuzhiyun #ifdef CONFIG_NFSD_PNFS
3039*4882a593Smuzhiyun 		|| !list_empty(&clp->cl_lo_states)
3040*4882a593Smuzhiyun #endif
3041*4882a593Smuzhiyun 		|| !list_empty(&clp->cl_delegations)
3042*4882a593Smuzhiyun 		|| !list_empty(&clp->cl_sessions)
3043*4882a593Smuzhiyun 		|| !list_empty(&clp->async_copies);
3044*4882a593Smuzhiyun }
3045*4882a593Smuzhiyun 
copy_impl_id(struct nfs4_client * clp,struct nfsd4_exchange_id * exid)3046*4882a593Smuzhiyun static __be32 copy_impl_id(struct nfs4_client *clp,
3047*4882a593Smuzhiyun 				struct nfsd4_exchange_id *exid)
3048*4882a593Smuzhiyun {
3049*4882a593Smuzhiyun 	if (!exid->nii_domain.data)
3050*4882a593Smuzhiyun 		return 0;
3051*4882a593Smuzhiyun 	xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3052*4882a593Smuzhiyun 	if (!clp->cl_nii_domain.data)
3053*4882a593Smuzhiyun 		return nfserr_jukebox;
3054*4882a593Smuzhiyun 	xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3055*4882a593Smuzhiyun 	if (!clp->cl_nii_name.data)
3056*4882a593Smuzhiyun 		return nfserr_jukebox;
3057*4882a593Smuzhiyun 	clp->cl_nii_time = exid->nii_time;
3058*4882a593Smuzhiyun 	return 0;
3059*4882a593Smuzhiyun }
3060*4882a593Smuzhiyun 
3061*4882a593Smuzhiyun __be32
nfsd4_exchange_id(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3062*4882a593Smuzhiyun nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3063*4882a593Smuzhiyun 		union nfsd4_op_u *u)
3064*4882a593Smuzhiyun {
3065*4882a593Smuzhiyun 	struct nfsd4_exchange_id *exid = &u->exchange_id;
3066*4882a593Smuzhiyun 	struct nfs4_client *conf, *new;
3067*4882a593Smuzhiyun 	struct nfs4_client *unconf = NULL;
3068*4882a593Smuzhiyun 	__be32 status;
3069*4882a593Smuzhiyun 	char			addr_str[INET6_ADDRSTRLEN];
3070*4882a593Smuzhiyun 	nfs4_verifier		verf = exid->verifier;
3071*4882a593Smuzhiyun 	struct sockaddr		*sa = svc_addr(rqstp);
3072*4882a593Smuzhiyun 	bool	update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
3073*4882a593Smuzhiyun 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3074*4882a593Smuzhiyun 
3075*4882a593Smuzhiyun 	rpc_ntop(sa, addr_str, sizeof(addr_str));
3076*4882a593Smuzhiyun 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3077*4882a593Smuzhiyun 		"ip_addr=%s flags %x, spa_how %d\n",
3078*4882a593Smuzhiyun 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
3079*4882a593Smuzhiyun 		addr_str, exid->flags, exid->spa_how);
3080*4882a593Smuzhiyun 
3081*4882a593Smuzhiyun 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
3082*4882a593Smuzhiyun 		return nfserr_inval;
3083*4882a593Smuzhiyun 
3084*4882a593Smuzhiyun 	new = create_client(exid->clname, rqstp, &verf);
3085*4882a593Smuzhiyun 	if (new == NULL)
3086*4882a593Smuzhiyun 		return nfserr_jukebox;
3087*4882a593Smuzhiyun 	status = copy_impl_id(new, exid);
3088*4882a593Smuzhiyun 	if (status)
3089*4882a593Smuzhiyun 		goto out_nolock;
3090*4882a593Smuzhiyun 
3091*4882a593Smuzhiyun 	switch (exid->spa_how) {
3092*4882a593Smuzhiyun 	case SP4_MACH_CRED:
3093*4882a593Smuzhiyun 		exid->spo_must_enforce[0] = 0;
3094*4882a593Smuzhiyun 		exid->spo_must_enforce[1] = (
3095*4882a593Smuzhiyun 			1 << (OP_BIND_CONN_TO_SESSION - 32) |
3096*4882a593Smuzhiyun 			1 << (OP_EXCHANGE_ID - 32) |
3097*4882a593Smuzhiyun 			1 << (OP_CREATE_SESSION - 32) |
3098*4882a593Smuzhiyun 			1 << (OP_DESTROY_SESSION - 32) |
3099*4882a593Smuzhiyun 			1 << (OP_DESTROY_CLIENTID - 32));
3100*4882a593Smuzhiyun 
3101*4882a593Smuzhiyun 		exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3102*4882a593Smuzhiyun 					1 << (OP_OPEN_DOWNGRADE) |
3103*4882a593Smuzhiyun 					1 << (OP_LOCKU) |
3104*4882a593Smuzhiyun 					1 << (OP_DELEGRETURN));
3105*4882a593Smuzhiyun 
3106*4882a593Smuzhiyun 		exid->spo_must_allow[1] &= (
3107*4882a593Smuzhiyun 					1 << (OP_TEST_STATEID - 32) |
3108*4882a593Smuzhiyun 					1 << (OP_FREE_STATEID - 32));
3109*4882a593Smuzhiyun 		if (!svc_rqst_integrity_protected(rqstp)) {
3110*4882a593Smuzhiyun 			status = nfserr_inval;
3111*4882a593Smuzhiyun 			goto out_nolock;
3112*4882a593Smuzhiyun 		}
3113*4882a593Smuzhiyun 		/*
3114*4882a593Smuzhiyun 		 * Sometimes userspace doesn't give us a principal.
3115*4882a593Smuzhiyun 		 * Which is a bug, really.  Anyway, we can't enforce
3116*4882a593Smuzhiyun 		 * MACH_CRED in that case, better to give up now:
3117*4882a593Smuzhiyun 		 */
3118*4882a593Smuzhiyun 		if (!new->cl_cred.cr_principal &&
3119*4882a593Smuzhiyun 					!new->cl_cred.cr_raw_principal) {
3120*4882a593Smuzhiyun 			status = nfserr_serverfault;
3121*4882a593Smuzhiyun 			goto out_nolock;
3122*4882a593Smuzhiyun 		}
3123*4882a593Smuzhiyun 		new->cl_mach_cred = true;
3124*4882a593Smuzhiyun 	case SP4_NONE:
3125*4882a593Smuzhiyun 		break;
3126*4882a593Smuzhiyun 	default:				/* checked by xdr code */
3127*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
3128*4882a593Smuzhiyun 		fallthrough;
3129*4882a593Smuzhiyun 	case SP4_SSV:
3130*4882a593Smuzhiyun 		status = nfserr_encr_alg_unsupp;
3131*4882a593Smuzhiyun 		goto out_nolock;
3132*4882a593Smuzhiyun 	}
3133*4882a593Smuzhiyun 
3134*4882a593Smuzhiyun 	/* Cases below refer to rfc 5661 section 18.35.4: */
3135*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3136*4882a593Smuzhiyun 	conf = find_confirmed_client_by_name(&exid->clname, nn);
3137*4882a593Smuzhiyun 	if (conf) {
3138*4882a593Smuzhiyun 		bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3139*4882a593Smuzhiyun 		bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3140*4882a593Smuzhiyun 
3141*4882a593Smuzhiyun 		if (update) {
3142*4882a593Smuzhiyun 			if (!clp_used_exchangeid(conf)) { /* buggy client */
3143*4882a593Smuzhiyun 				status = nfserr_inval;
3144*4882a593Smuzhiyun 				goto out;
3145*4882a593Smuzhiyun 			}
3146*4882a593Smuzhiyun 			if (!nfsd4_mach_creds_match(conf, rqstp)) {
3147*4882a593Smuzhiyun 				status = nfserr_wrong_cred;
3148*4882a593Smuzhiyun 				goto out;
3149*4882a593Smuzhiyun 			}
3150*4882a593Smuzhiyun 			if (!creds_match) { /* case 9 */
3151*4882a593Smuzhiyun 				status = nfserr_perm;
3152*4882a593Smuzhiyun 				goto out;
3153*4882a593Smuzhiyun 			}
3154*4882a593Smuzhiyun 			if (!verfs_match) { /* case 8 */
3155*4882a593Smuzhiyun 				status = nfserr_not_same;
3156*4882a593Smuzhiyun 				goto out;
3157*4882a593Smuzhiyun 			}
3158*4882a593Smuzhiyun 			/* case 6 */
3159*4882a593Smuzhiyun 			exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
3160*4882a593Smuzhiyun 			goto out_copy;
3161*4882a593Smuzhiyun 		}
3162*4882a593Smuzhiyun 		if (!creds_match) { /* case 3 */
3163*4882a593Smuzhiyun 			if (client_has_state(conf)) {
3164*4882a593Smuzhiyun 				status = nfserr_clid_inuse;
3165*4882a593Smuzhiyun 				goto out;
3166*4882a593Smuzhiyun 			}
3167*4882a593Smuzhiyun 			goto out_new;
3168*4882a593Smuzhiyun 		}
3169*4882a593Smuzhiyun 		if (verfs_match) { /* case 2 */
3170*4882a593Smuzhiyun 			conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
3171*4882a593Smuzhiyun 			goto out_copy;
3172*4882a593Smuzhiyun 		}
3173*4882a593Smuzhiyun 		/* case 5, client reboot */
3174*4882a593Smuzhiyun 		conf = NULL;
3175*4882a593Smuzhiyun 		goto out_new;
3176*4882a593Smuzhiyun 	}
3177*4882a593Smuzhiyun 
3178*4882a593Smuzhiyun 	if (update) { /* case 7 */
3179*4882a593Smuzhiyun 		status = nfserr_noent;
3180*4882a593Smuzhiyun 		goto out;
3181*4882a593Smuzhiyun 	}
3182*4882a593Smuzhiyun 
3183*4882a593Smuzhiyun 	unconf  = find_unconfirmed_client_by_name(&exid->clname, nn);
3184*4882a593Smuzhiyun 	if (unconf) /* case 4, possible retry or client restart */
3185*4882a593Smuzhiyun 		unhash_client_locked(unconf);
3186*4882a593Smuzhiyun 
3187*4882a593Smuzhiyun 	/* case 1 (normal case) */
3188*4882a593Smuzhiyun out_new:
3189*4882a593Smuzhiyun 	if (conf) {
3190*4882a593Smuzhiyun 		status = mark_client_expired_locked(conf);
3191*4882a593Smuzhiyun 		if (status)
3192*4882a593Smuzhiyun 			goto out;
3193*4882a593Smuzhiyun 	}
3194*4882a593Smuzhiyun 	new->cl_minorversion = cstate->minorversion;
3195*4882a593Smuzhiyun 	new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3196*4882a593Smuzhiyun 	new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
3197*4882a593Smuzhiyun 
3198*4882a593Smuzhiyun 	add_to_unconfirmed(new);
3199*4882a593Smuzhiyun 	swap(new, conf);
3200*4882a593Smuzhiyun out_copy:
3201*4882a593Smuzhiyun 	exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3202*4882a593Smuzhiyun 	exid->clientid.cl_id = conf->cl_clientid.cl_id;
3203*4882a593Smuzhiyun 
3204*4882a593Smuzhiyun 	exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3205*4882a593Smuzhiyun 	nfsd4_set_ex_flags(conf, exid);
3206*4882a593Smuzhiyun 
3207*4882a593Smuzhiyun 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3208*4882a593Smuzhiyun 		conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
3209*4882a593Smuzhiyun 	status = nfs_ok;
3210*4882a593Smuzhiyun 
3211*4882a593Smuzhiyun out:
3212*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3213*4882a593Smuzhiyun out_nolock:
3214*4882a593Smuzhiyun 	if (new)
3215*4882a593Smuzhiyun 		expire_client(new);
3216*4882a593Smuzhiyun 	if (unconf)
3217*4882a593Smuzhiyun 		expire_client(unconf);
3218*4882a593Smuzhiyun 	return status;
3219*4882a593Smuzhiyun }
3220*4882a593Smuzhiyun 
3221*4882a593Smuzhiyun static __be32
check_slot_seqid(u32 seqid,u32 slot_seqid,int slot_inuse)3222*4882a593Smuzhiyun check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
3223*4882a593Smuzhiyun {
3224*4882a593Smuzhiyun 	dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3225*4882a593Smuzhiyun 		slot_seqid);
3226*4882a593Smuzhiyun 
3227*4882a593Smuzhiyun 	/* The slot is in use, and no response has been sent. */
3228*4882a593Smuzhiyun 	if (slot_inuse) {
3229*4882a593Smuzhiyun 		if (seqid == slot_seqid)
3230*4882a593Smuzhiyun 			return nfserr_jukebox;
3231*4882a593Smuzhiyun 		else
3232*4882a593Smuzhiyun 			return nfserr_seq_misordered;
3233*4882a593Smuzhiyun 	}
3234*4882a593Smuzhiyun 	/* Note unsigned 32-bit arithmetic handles wraparound: */
3235*4882a593Smuzhiyun 	if (likely(seqid == slot_seqid + 1))
3236*4882a593Smuzhiyun 		return nfs_ok;
3237*4882a593Smuzhiyun 	if (seqid == slot_seqid)
3238*4882a593Smuzhiyun 		return nfserr_replay_cache;
3239*4882a593Smuzhiyun 	return nfserr_seq_misordered;
3240*4882a593Smuzhiyun }
3241*4882a593Smuzhiyun 
3242*4882a593Smuzhiyun /*
3243*4882a593Smuzhiyun  * Cache the create session result into the create session single DRC
3244*4882a593Smuzhiyun  * slot cache by saving the xdr structure. sl_seqid has been set.
3245*4882a593Smuzhiyun  * Do this for solo or embedded create session operations.
3246*4882a593Smuzhiyun  */
3247*4882a593Smuzhiyun static void
nfsd4_cache_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot,__be32 nfserr)3248*4882a593Smuzhiyun nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
3249*4882a593Smuzhiyun 			   struct nfsd4_clid_slot *slot, __be32 nfserr)
3250*4882a593Smuzhiyun {
3251*4882a593Smuzhiyun 	slot->sl_status = nfserr;
3252*4882a593Smuzhiyun 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3253*4882a593Smuzhiyun }
3254*4882a593Smuzhiyun 
3255*4882a593Smuzhiyun static __be32
nfsd4_replay_create_session(struct nfsd4_create_session * cr_ses,struct nfsd4_clid_slot * slot)3256*4882a593Smuzhiyun nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3257*4882a593Smuzhiyun 			    struct nfsd4_clid_slot *slot)
3258*4882a593Smuzhiyun {
3259*4882a593Smuzhiyun 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3260*4882a593Smuzhiyun 	return slot->sl_status;
3261*4882a593Smuzhiyun }
3262*4882a593Smuzhiyun 
3263*4882a593Smuzhiyun #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
3264*4882a593Smuzhiyun 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3265*4882a593Smuzhiyun 			1 +	/* MIN tag is length with zero, only length */ \
3266*4882a593Smuzhiyun 			3 +	/* version, opcount, opcode */ \
3267*4882a593Smuzhiyun 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3268*4882a593Smuzhiyun 				/* seqid, slotID, slotID, cache */ \
3269*4882a593Smuzhiyun 			4 ) * sizeof(__be32))
3270*4882a593Smuzhiyun 
3271*4882a593Smuzhiyun #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3272*4882a593Smuzhiyun 			2 +	/* verifier: AUTH_NULL, length 0 */\
3273*4882a593Smuzhiyun 			1 +	/* status */ \
3274*4882a593Smuzhiyun 			1 +	/* MIN tag is length with zero, only length */ \
3275*4882a593Smuzhiyun 			3 +	/* opcount, opcode, opstatus*/ \
3276*4882a593Smuzhiyun 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3277*4882a593Smuzhiyun 				/* seqid, slotID, slotID, slotID, status */ \
3278*4882a593Smuzhiyun 			5 ) * sizeof(__be32))
3279*4882a593Smuzhiyun 
check_forechannel_attrs(struct nfsd4_channel_attrs * ca,struct nfsd_net * nn)3280*4882a593Smuzhiyun static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
3281*4882a593Smuzhiyun {
3282*4882a593Smuzhiyun 	u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3283*4882a593Smuzhiyun 
3284*4882a593Smuzhiyun 	if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3285*4882a593Smuzhiyun 		return nfserr_toosmall;
3286*4882a593Smuzhiyun 	if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3287*4882a593Smuzhiyun 		return nfserr_toosmall;
3288*4882a593Smuzhiyun 	ca->headerpadsz = 0;
3289*4882a593Smuzhiyun 	ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3290*4882a593Smuzhiyun 	ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3291*4882a593Smuzhiyun 	ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3292*4882a593Smuzhiyun 	ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3293*4882a593Smuzhiyun 			NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3294*4882a593Smuzhiyun 	ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3295*4882a593Smuzhiyun 	/*
3296*4882a593Smuzhiyun 	 * Note decreasing slot size below client's request may make it
3297*4882a593Smuzhiyun 	 * difficult for client to function correctly, whereas
3298*4882a593Smuzhiyun 	 * decreasing the number of slots will (just?) affect
3299*4882a593Smuzhiyun 	 * performance.  When short on memory we therefore prefer to
3300*4882a593Smuzhiyun 	 * decrease number of slots instead of their size.  Clients that
3301*4882a593Smuzhiyun 	 * request larger slots than they need will get poor results:
3302*4882a593Smuzhiyun 	 * Note that we always allow at least one slot, because our
3303*4882a593Smuzhiyun 	 * accounting is soft and provides no guarantees either way.
3304*4882a593Smuzhiyun 	 */
3305*4882a593Smuzhiyun 	ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
3306*4882a593Smuzhiyun 
3307*4882a593Smuzhiyun 	return nfs_ok;
3308*4882a593Smuzhiyun }
3309*4882a593Smuzhiyun 
3310*4882a593Smuzhiyun /*
3311*4882a593Smuzhiyun  * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3312*4882a593Smuzhiyun  * These are based on similar macros in linux/sunrpc/msg_prot.h .
3313*4882a593Smuzhiyun  */
3314*4882a593Smuzhiyun #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3315*4882a593Smuzhiyun 	(RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3316*4882a593Smuzhiyun 
3317*4882a593Smuzhiyun #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3318*4882a593Smuzhiyun 	(RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3319*4882a593Smuzhiyun 
3320*4882a593Smuzhiyun #define NFSD_CB_MAX_REQ_SZ	((NFS4_enc_cb_recall_sz + \
3321*4882a593Smuzhiyun 				 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3322*4882a593Smuzhiyun #define NFSD_CB_MAX_RESP_SZ	((NFS4_dec_cb_recall_sz + \
3323*4882a593Smuzhiyun 				 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3324*4882a593Smuzhiyun 				 sizeof(__be32))
3325*4882a593Smuzhiyun 
check_backchannel_attrs(struct nfsd4_channel_attrs * ca)3326*4882a593Smuzhiyun static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
3327*4882a593Smuzhiyun {
3328*4882a593Smuzhiyun 	ca->headerpadsz = 0;
3329*4882a593Smuzhiyun 
3330*4882a593Smuzhiyun 	if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
3331*4882a593Smuzhiyun 		return nfserr_toosmall;
3332*4882a593Smuzhiyun 	if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
3333*4882a593Smuzhiyun 		return nfserr_toosmall;
3334*4882a593Smuzhiyun 	ca->maxresp_cached = 0;
3335*4882a593Smuzhiyun 	if (ca->maxops < 2)
3336*4882a593Smuzhiyun 		return nfserr_toosmall;
3337*4882a593Smuzhiyun 
3338*4882a593Smuzhiyun 	return nfs_ok;
3339*4882a593Smuzhiyun }
3340*4882a593Smuzhiyun 
nfsd4_check_cb_sec(struct nfsd4_cb_sec * cbs)3341*4882a593Smuzhiyun static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3342*4882a593Smuzhiyun {
3343*4882a593Smuzhiyun 	switch (cbs->flavor) {
3344*4882a593Smuzhiyun 	case RPC_AUTH_NULL:
3345*4882a593Smuzhiyun 	case RPC_AUTH_UNIX:
3346*4882a593Smuzhiyun 		return nfs_ok;
3347*4882a593Smuzhiyun 	default:
3348*4882a593Smuzhiyun 		/*
3349*4882a593Smuzhiyun 		 * GSS case: the spec doesn't allow us to return this
3350*4882a593Smuzhiyun 		 * error.  But it also doesn't allow us not to support
3351*4882a593Smuzhiyun 		 * GSS.
3352*4882a593Smuzhiyun 		 * I'd rather this fail hard than return some error the
3353*4882a593Smuzhiyun 		 * client might think it can already handle:
3354*4882a593Smuzhiyun 		 */
3355*4882a593Smuzhiyun 		return nfserr_encr_alg_unsupp;
3356*4882a593Smuzhiyun 	}
3357*4882a593Smuzhiyun }
3358*4882a593Smuzhiyun 
3359*4882a593Smuzhiyun __be32
nfsd4_create_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3360*4882a593Smuzhiyun nfsd4_create_session(struct svc_rqst *rqstp,
3361*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3362*4882a593Smuzhiyun {
3363*4882a593Smuzhiyun 	struct nfsd4_create_session *cr_ses = &u->create_session;
3364*4882a593Smuzhiyun 	struct sockaddr *sa = svc_addr(rqstp);
3365*4882a593Smuzhiyun 	struct nfs4_client *conf, *unconf;
3366*4882a593Smuzhiyun 	struct nfs4_client *old = NULL;
3367*4882a593Smuzhiyun 	struct nfsd4_session *new;
3368*4882a593Smuzhiyun 	struct nfsd4_conn *conn;
3369*4882a593Smuzhiyun 	struct nfsd4_clid_slot *cs_slot = NULL;
3370*4882a593Smuzhiyun 	__be32 status = 0;
3371*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3372*4882a593Smuzhiyun 
3373*4882a593Smuzhiyun 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3374*4882a593Smuzhiyun 		return nfserr_inval;
3375*4882a593Smuzhiyun 	status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3376*4882a593Smuzhiyun 	if (status)
3377*4882a593Smuzhiyun 		return status;
3378*4882a593Smuzhiyun 	status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
3379*4882a593Smuzhiyun 	if (status)
3380*4882a593Smuzhiyun 		return status;
3381*4882a593Smuzhiyun 	status = check_backchannel_attrs(&cr_ses->back_channel);
3382*4882a593Smuzhiyun 	if (status)
3383*4882a593Smuzhiyun 		goto out_release_drc_mem;
3384*4882a593Smuzhiyun 	status = nfserr_jukebox;
3385*4882a593Smuzhiyun 	new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
3386*4882a593Smuzhiyun 	if (!new)
3387*4882a593Smuzhiyun 		goto out_release_drc_mem;
3388*4882a593Smuzhiyun 	conn = alloc_conn_from_crses(rqstp, cr_ses);
3389*4882a593Smuzhiyun 	if (!conn)
3390*4882a593Smuzhiyun 		goto out_free_session;
3391*4882a593Smuzhiyun 
3392*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3393*4882a593Smuzhiyun 	unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
3394*4882a593Smuzhiyun 	conf = find_confirmed_client(&cr_ses->clientid, true, nn);
3395*4882a593Smuzhiyun 	WARN_ON_ONCE(conf && unconf);
3396*4882a593Smuzhiyun 
3397*4882a593Smuzhiyun 	if (conf) {
3398*4882a593Smuzhiyun 		status = nfserr_wrong_cred;
3399*4882a593Smuzhiyun 		if (!nfsd4_mach_creds_match(conf, rqstp))
3400*4882a593Smuzhiyun 			goto out_free_conn;
3401*4882a593Smuzhiyun 		cs_slot = &conf->cl_cs_slot;
3402*4882a593Smuzhiyun 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3403*4882a593Smuzhiyun 		if (status) {
3404*4882a593Smuzhiyun 			if (status == nfserr_replay_cache)
3405*4882a593Smuzhiyun 				status = nfsd4_replay_create_session(cr_ses, cs_slot);
3406*4882a593Smuzhiyun 			goto out_free_conn;
3407*4882a593Smuzhiyun 		}
3408*4882a593Smuzhiyun 	} else if (unconf) {
3409*4882a593Smuzhiyun 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
3410*4882a593Smuzhiyun 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
3411*4882a593Smuzhiyun 			status = nfserr_clid_inuse;
3412*4882a593Smuzhiyun 			goto out_free_conn;
3413*4882a593Smuzhiyun 		}
3414*4882a593Smuzhiyun 		status = nfserr_wrong_cred;
3415*4882a593Smuzhiyun 		if (!nfsd4_mach_creds_match(unconf, rqstp))
3416*4882a593Smuzhiyun 			goto out_free_conn;
3417*4882a593Smuzhiyun 		cs_slot = &unconf->cl_cs_slot;
3418*4882a593Smuzhiyun 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
3419*4882a593Smuzhiyun 		if (status) {
3420*4882a593Smuzhiyun 			/* an unconfirmed replay returns misordered */
3421*4882a593Smuzhiyun 			status = nfserr_seq_misordered;
3422*4882a593Smuzhiyun 			goto out_free_conn;
3423*4882a593Smuzhiyun 		}
3424*4882a593Smuzhiyun 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3425*4882a593Smuzhiyun 		if (old) {
3426*4882a593Smuzhiyun 			status = mark_client_expired_locked(old);
3427*4882a593Smuzhiyun 			if (status) {
3428*4882a593Smuzhiyun 				old = NULL;
3429*4882a593Smuzhiyun 				goto out_free_conn;
3430*4882a593Smuzhiyun 			}
3431*4882a593Smuzhiyun 		}
3432*4882a593Smuzhiyun 		move_to_confirmed(unconf);
3433*4882a593Smuzhiyun 		conf = unconf;
3434*4882a593Smuzhiyun 	} else {
3435*4882a593Smuzhiyun 		status = nfserr_stale_clientid;
3436*4882a593Smuzhiyun 		goto out_free_conn;
3437*4882a593Smuzhiyun 	}
3438*4882a593Smuzhiyun 	status = nfs_ok;
3439*4882a593Smuzhiyun 	/* Persistent sessions are not supported */
3440*4882a593Smuzhiyun 	cr_ses->flags &= ~SESSION4_PERSIST;
3441*4882a593Smuzhiyun 	/* Upshifting from TCP to RDMA is not supported */
3442*4882a593Smuzhiyun 	cr_ses->flags &= ~SESSION4_RDMA;
3443*4882a593Smuzhiyun 
3444*4882a593Smuzhiyun 	init_session(rqstp, new, conf, cr_ses);
3445*4882a593Smuzhiyun 	nfsd4_get_session_locked(new);
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
3448*4882a593Smuzhiyun 	       NFS4_MAX_SESSIONID_LEN);
3449*4882a593Smuzhiyun 	cs_slot->sl_seqid++;
3450*4882a593Smuzhiyun 	cr_ses->seqid = cs_slot->sl_seqid;
3451*4882a593Smuzhiyun 
3452*4882a593Smuzhiyun 	/* cache solo and embedded create sessions under the client_lock */
3453*4882a593Smuzhiyun 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
3454*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3455*4882a593Smuzhiyun 	/* init connection and backchannel */
3456*4882a593Smuzhiyun 	nfsd4_init_conn(rqstp, conn, new);
3457*4882a593Smuzhiyun 	nfsd4_put_session(new);
3458*4882a593Smuzhiyun 	if (old)
3459*4882a593Smuzhiyun 		expire_client(old);
3460*4882a593Smuzhiyun 	return status;
3461*4882a593Smuzhiyun out_free_conn:
3462*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3463*4882a593Smuzhiyun 	free_conn(conn);
3464*4882a593Smuzhiyun 	if (old)
3465*4882a593Smuzhiyun 		expire_client(old);
3466*4882a593Smuzhiyun out_free_session:
3467*4882a593Smuzhiyun 	__free_session(new);
3468*4882a593Smuzhiyun out_release_drc_mem:
3469*4882a593Smuzhiyun 	nfsd4_put_drc_mem(&cr_ses->fore_channel);
3470*4882a593Smuzhiyun 	return status;
3471*4882a593Smuzhiyun }
3472*4882a593Smuzhiyun 
nfsd4_map_bcts_dir(u32 * dir)3473*4882a593Smuzhiyun static __be32 nfsd4_map_bcts_dir(u32 *dir)
3474*4882a593Smuzhiyun {
3475*4882a593Smuzhiyun 	switch (*dir) {
3476*4882a593Smuzhiyun 	case NFS4_CDFC4_FORE:
3477*4882a593Smuzhiyun 	case NFS4_CDFC4_BACK:
3478*4882a593Smuzhiyun 		return nfs_ok;
3479*4882a593Smuzhiyun 	case NFS4_CDFC4_FORE_OR_BOTH:
3480*4882a593Smuzhiyun 	case NFS4_CDFC4_BACK_OR_BOTH:
3481*4882a593Smuzhiyun 		*dir = NFS4_CDFC4_BOTH;
3482*4882a593Smuzhiyun 		return nfs_ok;
3483*4882a593Smuzhiyun 	}
3484*4882a593Smuzhiyun 	return nfserr_inval;
3485*4882a593Smuzhiyun }
3486*4882a593Smuzhiyun 
nfsd4_backchannel_ctl(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3487*4882a593Smuzhiyun __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3488*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate,
3489*4882a593Smuzhiyun 		union nfsd4_op_u *u)
3490*4882a593Smuzhiyun {
3491*4882a593Smuzhiyun 	struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
3492*4882a593Smuzhiyun 	struct nfsd4_session *session = cstate->session;
3493*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3494*4882a593Smuzhiyun 	__be32 status;
3495*4882a593Smuzhiyun 
3496*4882a593Smuzhiyun 	status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3497*4882a593Smuzhiyun 	if (status)
3498*4882a593Smuzhiyun 		return status;
3499*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3500*4882a593Smuzhiyun 	session->se_cb_prog = bc->bc_cb_program;
3501*4882a593Smuzhiyun 	session->se_cb_sec = bc->bc_cb_sec;
3502*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3503*4882a593Smuzhiyun 
3504*4882a593Smuzhiyun 	nfsd4_probe_callback(session->se_client);
3505*4882a593Smuzhiyun 
3506*4882a593Smuzhiyun 	return nfs_ok;
3507*4882a593Smuzhiyun }
3508*4882a593Smuzhiyun 
__nfsd4_find_conn(struct svc_xprt * xpt,struct nfsd4_session * s)3509*4882a593Smuzhiyun static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3510*4882a593Smuzhiyun {
3511*4882a593Smuzhiyun 	struct nfsd4_conn *c;
3512*4882a593Smuzhiyun 
3513*4882a593Smuzhiyun 	list_for_each_entry(c, &s->se_conns, cn_persession) {
3514*4882a593Smuzhiyun 		if (c->cn_xprt == xpt) {
3515*4882a593Smuzhiyun 			return c;
3516*4882a593Smuzhiyun 		}
3517*4882a593Smuzhiyun 	}
3518*4882a593Smuzhiyun 	return NULL;
3519*4882a593Smuzhiyun }
3520*4882a593Smuzhiyun 
nfsd4_match_existing_connection(struct svc_rqst * rqst,struct nfsd4_session * session,u32 req,struct nfsd4_conn ** conn)3521*4882a593Smuzhiyun static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3522*4882a593Smuzhiyun 		struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
3523*4882a593Smuzhiyun {
3524*4882a593Smuzhiyun 	struct nfs4_client *clp = session->se_client;
3525*4882a593Smuzhiyun 	struct svc_xprt *xpt = rqst->rq_xprt;
3526*4882a593Smuzhiyun 	struct nfsd4_conn *c;
3527*4882a593Smuzhiyun 	__be32 status;
3528*4882a593Smuzhiyun 
3529*4882a593Smuzhiyun 	/* Following the last paragraph of RFC 5661 Section 18.34.3: */
3530*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
3531*4882a593Smuzhiyun 	c = __nfsd4_find_conn(xpt, session);
3532*4882a593Smuzhiyun 	if (!c)
3533*4882a593Smuzhiyun 		status = nfserr_noent;
3534*4882a593Smuzhiyun 	else if (req == c->cn_flags)
3535*4882a593Smuzhiyun 		status = nfs_ok;
3536*4882a593Smuzhiyun 	else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3537*4882a593Smuzhiyun 				c->cn_flags != NFS4_CDFC4_BACK)
3538*4882a593Smuzhiyun 		status = nfs_ok;
3539*4882a593Smuzhiyun 	else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3540*4882a593Smuzhiyun 				c->cn_flags != NFS4_CDFC4_FORE)
3541*4882a593Smuzhiyun 		status = nfs_ok;
3542*4882a593Smuzhiyun 	else
3543*4882a593Smuzhiyun 		status = nfserr_inval;
3544*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
3545*4882a593Smuzhiyun 	if (status == nfs_ok && conn)
3546*4882a593Smuzhiyun 		*conn = c;
3547*4882a593Smuzhiyun 	return status;
3548*4882a593Smuzhiyun }
3549*4882a593Smuzhiyun 
nfsd4_bind_conn_to_session(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3550*4882a593Smuzhiyun __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3551*4882a593Smuzhiyun 		     struct nfsd4_compound_state *cstate,
3552*4882a593Smuzhiyun 		     union nfsd4_op_u *u)
3553*4882a593Smuzhiyun {
3554*4882a593Smuzhiyun 	struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
3555*4882a593Smuzhiyun 	__be32 status;
3556*4882a593Smuzhiyun 	struct nfsd4_conn *conn;
3557*4882a593Smuzhiyun 	struct nfsd4_session *session;
3558*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
3559*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3560*4882a593Smuzhiyun 
3561*4882a593Smuzhiyun 	if (!nfsd4_last_compound_op(rqstp))
3562*4882a593Smuzhiyun 		return nfserr_not_only_op;
3563*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3564*4882a593Smuzhiyun 	session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
3565*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3566*4882a593Smuzhiyun 	if (!session)
3567*4882a593Smuzhiyun 		goto out_no_session;
3568*4882a593Smuzhiyun 	status = nfserr_wrong_cred;
3569*4882a593Smuzhiyun 	if (!nfsd4_mach_creds_match(session->se_client, rqstp))
3570*4882a593Smuzhiyun 		goto out;
3571*4882a593Smuzhiyun 	status = nfsd4_match_existing_connection(rqstp, session,
3572*4882a593Smuzhiyun 			bcts->dir, &conn);
3573*4882a593Smuzhiyun 	if (status == nfs_ok) {
3574*4882a593Smuzhiyun 		if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
3575*4882a593Smuzhiyun 				bcts->dir == NFS4_CDFC4_BACK)
3576*4882a593Smuzhiyun 			conn->cn_flags |= NFS4_CDFC4_BACK;
3577*4882a593Smuzhiyun 		nfsd4_probe_callback(session->se_client);
3578*4882a593Smuzhiyun 		goto out;
3579*4882a593Smuzhiyun 	}
3580*4882a593Smuzhiyun 	if (status == nfserr_inval)
3581*4882a593Smuzhiyun 		goto out;
3582*4882a593Smuzhiyun 	status = nfsd4_map_bcts_dir(&bcts->dir);
3583*4882a593Smuzhiyun 	if (status)
3584*4882a593Smuzhiyun 		goto out;
3585*4882a593Smuzhiyun 	conn = alloc_conn(rqstp, bcts->dir);
3586*4882a593Smuzhiyun 	status = nfserr_jukebox;
3587*4882a593Smuzhiyun 	if (!conn)
3588*4882a593Smuzhiyun 		goto out;
3589*4882a593Smuzhiyun 	nfsd4_init_conn(rqstp, conn, session);
3590*4882a593Smuzhiyun 	status = nfs_ok;
3591*4882a593Smuzhiyun out:
3592*4882a593Smuzhiyun 	nfsd4_put_session(session);
3593*4882a593Smuzhiyun out_no_session:
3594*4882a593Smuzhiyun 	return status;
3595*4882a593Smuzhiyun }
3596*4882a593Smuzhiyun 
nfsd4_compound_in_session(struct nfsd4_compound_state * cstate,struct nfs4_sessionid * sid)3597*4882a593Smuzhiyun static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
3598*4882a593Smuzhiyun {
3599*4882a593Smuzhiyun 	if (!cstate->session)
3600*4882a593Smuzhiyun 		return false;
3601*4882a593Smuzhiyun 	return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
3602*4882a593Smuzhiyun }
3603*4882a593Smuzhiyun 
3604*4882a593Smuzhiyun __be32
nfsd4_destroy_session(struct svc_rqst * r,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3605*4882a593Smuzhiyun nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3606*4882a593Smuzhiyun 		union nfsd4_op_u *u)
3607*4882a593Smuzhiyun {
3608*4882a593Smuzhiyun 	struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
3609*4882a593Smuzhiyun 	struct nfsd4_session *ses;
3610*4882a593Smuzhiyun 	__be32 status;
3611*4882a593Smuzhiyun 	int ref_held_by_me = 0;
3612*4882a593Smuzhiyun 	struct net *net = SVC_NET(r);
3613*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3614*4882a593Smuzhiyun 
3615*4882a593Smuzhiyun 	status = nfserr_not_only_op;
3616*4882a593Smuzhiyun 	if (nfsd4_compound_in_session(cstate, sessionid)) {
3617*4882a593Smuzhiyun 		if (!nfsd4_last_compound_op(r))
3618*4882a593Smuzhiyun 			goto out;
3619*4882a593Smuzhiyun 		ref_held_by_me++;
3620*4882a593Smuzhiyun 	}
3621*4882a593Smuzhiyun 	dump_sessionid(__func__, sessionid);
3622*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3623*4882a593Smuzhiyun 	ses = find_in_sessionid_hashtbl(sessionid, net, &status);
3624*4882a593Smuzhiyun 	if (!ses)
3625*4882a593Smuzhiyun 		goto out_client_lock;
3626*4882a593Smuzhiyun 	status = nfserr_wrong_cred;
3627*4882a593Smuzhiyun 	if (!nfsd4_mach_creds_match(ses->se_client, r))
3628*4882a593Smuzhiyun 		goto out_put_session;
3629*4882a593Smuzhiyun 	status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
3630*4882a593Smuzhiyun 	if (status)
3631*4882a593Smuzhiyun 		goto out_put_session;
3632*4882a593Smuzhiyun 	unhash_session(ses);
3633*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3634*4882a593Smuzhiyun 
3635*4882a593Smuzhiyun 	nfsd4_probe_callback_sync(ses->se_client);
3636*4882a593Smuzhiyun 
3637*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3638*4882a593Smuzhiyun 	status = nfs_ok;
3639*4882a593Smuzhiyun out_put_session:
3640*4882a593Smuzhiyun 	nfsd4_put_session_locked(ses);
3641*4882a593Smuzhiyun out_client_lock:
3642*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3643*4882a593Smuzhiyun out:
3644*4882a593Smuzhiyun 	return status;
3645*4882a593Smuzhiyun }
3646*4882a593Smuzhiyun 
nfsd4_sequence_check_conn(struct nfsd4_conn * new,struct nfsd4_session * ses)3647*4882a593Smuzhiyun static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
3648*4882a593Smuzhiyun {
3649*4882a593Smuzhiyun 	struct nfs4_client *clp = ses->se_client;
3650*4882a593Smuzhiyun 	struct nfsd4_conn *c;
3651*4882a593Smuzhiyun 	__be32 status = nfs_ok;
3652*4882a593Smuzhiyun 	int ret;
3653*4882a593Smuzhiyun 
3654*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
3655*4882a593Smuzhiyun 	c = __nfsd4_find_conn(new->cn_xprt, ses);
3656*4882a593Smuzhiyun 	if (c)
3657*4882a593Smuzhiyun 		goto out_free;
3658*4882a593Smuzhiyun 	status = nfserr_conn_not_bound_to_session;
3659*4882a593Smuzhiyun 	if (clp->cl_mach_cred)
3660*4882a593Smuzhiyun 		goto out_free;
3661*4882a593Smuzhiyun 	__nfsd4_hash_conn(new, ses);
3662*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
3663*4882a593Smuzhiyun 	ret = nfsd4_register_conn(new);
3664*4882a593Smuzhiyun 	if (ret)
3665*4882a593Smuzhiyun 		/* oops; xprt is already down: */
3666*4882a593Smuzhiyun 		nfsd4_conn_lost(&new->cn_xpt_user);
3667*4882a593Smuzhiyun 	return nfs_ok;
3668*4882a593Smuzhiyun out_free:
3669*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
3670*4882a593Smuzhiyun 	free_conn(new);
3671*4882a593Smuzhiyun 	return status;
3672*4882a593Smuzhiyun }
3673*4882a593Smuzhiyun 
nfsd4_session_too_many_ops(struct svc_rqst * rqstp,struct nfsd4_session * session)3674*4882a593Smuzhiyun static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3675*4882a593Smuzhiyun {
3676*4882a593Smuzhiyun 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
3677*4882a593Smuzhiyun 
3678*4882a593Smuzhiyun 	return args->opcnt > session->se_fchannel.maxops;
3679*4882a593Smuzhiyun }
3680*4882a593Smuzhiyun 
nfsd4_request_too_big(struct svc_rqst * rqstp,struct nfsd4_session * session)3681*4882a593Smuzhiyun static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3682*4882a593Smuzhiyun 				  struct nfsd4_session *session)
3683*4882a593Smuzhiyun {
3684*4882a593Smuzhiyun 	struct xdr_buf *xb = &rqstp->rq_arg;
3685*4882a593Smuzhiyun 
3686*4882a593Smuzhiyun 	return xb->len > session->se_fchannel.maxreq_sz;
3687*4882a593Smuzhiyun }
3688*4882a593Smuzhiyun 
replay_matches_cache(struct svc_rqst * rqstp,struct nfsd4_sequence * seq,struct nfsd4_slot * slot)3689*4882a593Smuzhiyun static bool replay_matches_cache(struct svc_rqst *rqstp,
3690*4882a593Smuzhiyun 		 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3691*4882a593Smuzhiyun {
3692*4882a593Smuzhiyun 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3693*4882a593Smuzhiyun 
3694*4882a593Smuzhiyun 	if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3695*4882a593Smuzhiyun 	    (bool)seq->cachethis)
3696*4882a593Smuzhiyun 		return false;
3697*4882a593Smuzhiyun 	/*
3698*4882a593Smuzhiyun 	 * If there's an error then the reply can have fewer ops than
3699*4882a593Smuzhiyun 	 * the call.
3700*4882a593Smuzhiyun 	 */
3701*4882a593Smuzhiyun 	if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3702*4882a593Smuzhiyun 		return false;
3703*4882a593Smuzhiyun 	/*
3704*4882a593Smuzhiyun 	 * But if we cached a reply with *more* ops than the call you're
3705*4882a593Smuzhiyun 	 * sending us now, then this new call is clearly not really a
3706*4882a593Smuzhiyun 	 * replay of the old one:
3707*4882a593Smuzhiyun 	 */
3708*4882a593Smuzhiyun 	if (slot->sl_opcnt > argp->opcnt)
3709*4882a593Smuzhiyun 		return false;
3710*4882a593Smuzhiyun 	/* This is the only check explicitly called by spec: */
3711*4882a593Smuzhiyun 	if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3712*4882a593Smuzhiyun 		return false;
3713*4882a593Smuzhiyun 	/*
3714*4882a593Smuzhiyun 	 * There may be more comparisons we could actually do, but the
3715*4882a593Smuzhiyun 	 * spec doesn't require us to catch every case where the calls
3716*4882a593Smuzhiyun 	 * don't match (that would require caching the call as well as
3717*4882a593Smuzhiyun 	 * the reply), so we don't bother.
3718*4882a593Smuzhiyun 	 */
3719*4882a593Smuzhiyun 	return true;
3720*4882a593Smuzhiyun }
3721*4882a593Smuzhiyun 
3722*4882a593Smuzhiyun __be32
nfsd4_sequence(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3723*4882a593Smuzhiyun nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3724*4882a593Smuzhiyun 		union nfsd4_op_u *u)
3725*4882a593Smuzhiyun {
3726*4882a593Smuzhiyun 	struct nfsd4_sequence *seq = &u->sequence;
3727*4882a593Smuzhiyun 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
3728*4882a593Smuzhiyun 	struct xdr_stream *xdr = &resp->xdr;
3729*4882a593Smuzhiyun 	struct nfsd4_session *session;
3730*4882a593Smuzhiyun 	struct nfs4_client *clp;
3731*4882a593Smuzhiyun 	struct nfsd4_slot *slot;
3732*4882a593Smuzhiyun 	struct nfsd4_conn *conn;
3733*4882a593Smuzhiyun 	__be32 status;
3734*4882a593Smuzhiyun 	int buflen;
3735*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
3736*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
3737*4882a593Smuzhiyun 
3738*4882a593Smuzhiyun 	if (resp->opcnt != 1)
3739*4882a593Smuzhiyun 		return nfserr_sequence_pos;
3740*4882a593Smuzhiyun 
3741*4882a593Smuzhiyun 	/*
3742*4882a593Smuzhiyun 	 * Will be either used or freed by nfsd4_sequence_check_conn
3743*4882a593Smuzhiyun 	 * below.
3744*4882a593Smuzhiyun 	 */
3745*4882a593Smuzhiyun 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3746*4882a593Smuzhiyun 	if (!conn)
3747*4882a593Smuzhiyun 		return nfserr_jukebox;
3748*4882a593Smuzhiyun 
3749*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3750*4882a593Smuzhiyun 	session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
3751*4882a593Smuzhiyun 	if (!session)
3752*4882a593Smuzhiyun 		goto out_no_session;
3753*4882a593Smuzhiyun 	clp = session->se_client;
3754*4882a593Smuzhiyun 
3755*4882a593Smuzhiyun 	status = nfserr_too_many_ops;
3756*4882a593Smuzhiyun 	if (nfsd4_session_too_many_ops(rqstp, session))
3757*4882a593Smuzhiyun 		goto out_put_session;
3758*4882a593Smuzhiyun 
3759*4882a593Smuzhiyun 	status = nfserr_req_too_big;
3760*4882a593Smuzhiyun 	if (nfsd4_request_too_big(rqstp, session))
3761*4882a593Smuzhiyun 		goto out_put_session;
3762*4882a593Smuzhiyun 
3763*4882a593Smuzhiyun 	status = nfserr_badslot;
3764*4882a593Smuzhiyun 	if (seq->slotid >= session->se_fchannel.maxreqs)
3765*4882a593Smuzhiyun 		goto out_put_session;
3766*4882a593Smuzhiyun 
3767*4882a593Smuzhiyun 	slot = session->se_slots[seq->slotid];
3768*4882a593Smuzhiyun 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
3769*4882a593Smuzhiyun 
3770*4882a593Smuzhiyun 	/* We do not negotiate the number of slots yet, so set the
3771*4882a593Smuzhiyun 	 * maxslots to the session maxreqs which is used to encode
3772*4882a593Smuzhiyun 	 * sr_highest_slotid and the sr_target_slot id to maxslots */
3773*4882a593Smuzhiyun 	seq->maxslots = session->se_fchannel.maxreqs;
3774*4882a593Smuzhiyun 
3775*4882a593Smuzhiyun 	status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3776*4882a593Smuzhiyun 					slot->sl_flags & NFSD4_SLOT_INUSE);
3777*4882a593Smuzhiyun 	if (status == nfserr_replay_cache) {
3778*4882a593Smuzhiyun 		status = nfserr_seq_misordered;
3779*4882a593Smuzhiyun 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
3780*4882a593Smuzhiyun 			goto out_put_session;
3781*4882a593Smuzhiyun 		status = nfserr_seq_false_retry;
3782*4882a593Smuzhiyun 		if (!replay_matches_cache(rqstp, seq, slot))
3783*4882a593Smuzhiyun 			goto out_put_session;
3784*4882a593Smuzhiyun 		cstate->slot = slot;
3785*4882a593Smuzhiyun 		cstate->session = session;
3786*4882a593Smuzhiyun 		cstate->clp = clp;
3787*4882a593Smuzhiyun 		/* Return the cached reply status and set cstate->status
3788*4882a593Smuzhiyun 		 * for nfsd4_proc_compound processing */
3789*4882a593Smuzhiyun 		status = nfsd4_replay_cache_entry(resp, seq);
3790*4882a593Smuzhiyun 		cstate->status = nfserr_replay_cache;
3791*4882a593Smuzhiyun 		goto out;
3792*4882a593Smuzhiyun 	}
3793*4882a593Smuzhiyun 	if (status)
3794*4882a593Smuzhiyun 		goto out_put_session;
3795*4882a593Smuzhiyun 
3796*4882a593Smuzhiyun 	status = nfsd4_sequence_check_conn(conn, session);
3797*4882a593Smuzhiyun 	conn = NULL;
3798*4882a593Smuzhiyun 	if (status)
3799*4882a593Smuzhiyun 		goto out_put_session;
3800*4882a593Smuzhiyun 
3801*4882a593Smuzhiyun 	buflen = (seq->cachethis) ?
3802*4882a593Smuzhiyun 			session->se_fchannel.maxresp_cached :
3803*4882a593Smuzhiyun 			session->se_fchannel.maxresp_sz;
3804*4882a593Smuzhiyun 	status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3805*4882a593Smuzhiyun 				    nfserr_rep_too_big;
3806*4882a593Smuzhiyun 	if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
3807*4882a593Smuzhiyun 		goto out_put_session;
3808*4882a593Smuzhiyun 	svc_reserve(rqstp, buflen);
3809*4882a593Smuzhiyun 
3810*4882a593Smuzhiyun 	status = nfs_ok;
3811*4882a593Smuzhiyun 	/* Success! bump slot seqid */
3812*4882a593Smuzhiyun 	slot->sl_seqid = seq->seqid;
3813*4882a593Smuzhiyun 	slot->sl_flags |= NFSD4_SLOT_INUSE;
3814*4882a593Smuzhiyun 	if (seq->cachethis)
3815*4882a593Smuzhiyun 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
3816*4882a593Smuzhiyun 	else
3817*4882a593Smuzhiyun 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
3818*4882a593Smuzhiyun 
3819*4882a593Smuzhiyun 	cstate->slot = slot;
3820*4882a593Smuzhiyun 	cstate->session = session;
3821*4882a593Smuzhiyun 	cstate->clp = clp;
3822*4882a593Smuzhiyun 
3823*4882a593Smuzhiyun out:
3824*4882a593Smuzhiyun 	switch (clp->cl_cb_state) {
3825*4882a593Smuzhiyun 	case NFSD4_CB_DOWN:
3826*4882a593Smuzhiyun 		seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3827*4882a593Smuzhiyun 		break;
3828*4882a593Smuzhiyun 	case NFSD4_CB_FAULT:
3829*4882a593Smuzhiyun 		seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3830*4882a593Smuzhiyun 		break;
3831*4882a593Smuzhiyun 	default:
3832*4882a593Smuzhiyun 		seq->status_flags = 0;
3833*4882a593Smuzhiyun 	}
3834*4882a593Smuzhiyun 	if (!list_empty(&clp->cl_revoked))
3835*4882a593Smuzhiyun 		seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
3836*4882a593Smuzhiyun out_no_session:
3837*4882a593Smuzhiyun 	if (conn)
3838*4882a593Smuzhiyun 		free_conn(conn);
3839*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3840*4882a593Smuzhiyun 	return status;
3841*4882a593Smuzhiyun out_put_session:
3842*4882a593Smuzhiyun 	nfsd4_put_session_locked(session);
3843*4882a593Smuzhiyun 	goto out_no_session;
3844*4882a593Smuzhiyun }
3845*4882a593Smuzhiyun 
3846*4882a593Smuzhiyun void
nfsd4_sequence_done(struct nfsd4_compoundres * resp)3847*4882a593Smuzhiyun nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3848*4882a593Smuzhiyun {
3849*4882a593Smuzhiyun 	struct nfsd4_compound_state *cs = &resp->cstate;
3850*4882a593Smuzhiyun 
3851*4882a593Smuzhiyun 	if (nfsd4_has_session(cs)) {
3852*4882a593Smuzhiyun 		if (cs->status != nfserr_replay_cache) {
3853*4882a593Smuzhiyun 			nfsd4_store_cache_entry(resp);
3854*4882a593Smuzhiyun 			cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3855*4882a593Smuzhiyun 		}
3856*4882a593Smuzhiyun 		/* Drop session reference that was taken in nfsd4_sequence() */
3857*4882a593Smuzhiyun 		nfsd4_put_session(cs->session);
3858*4882a593Smuzhiyun 	} else if (cs->clp)
3859*4882a593Smuzhiyun 		put_client_renew(cs->clp);
3860*4882a593Smuzhiyun }
3861*4882a593Smuzhiyun 
3862*4882a593Smuzhiyun __be32
nfsd4_destroy_clientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3863*4882a593Smuzhiyun nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3864*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate,
3865*4882a593Smuzhiyun 		union nfsd4_op_u *u)
3866*4882a593Smuzhiyun {
3867*4882a593Smuzhiyun 	struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
3868*4882a593Smuzhiyun 	struct nfs4_client *conf, *unconf;
3869*4882a593Smuzhiyun 	struct nfs4_client *clp = NULL;
3870*4882a593Smuzhiyun 	__be32 status = 0;
3871*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3872*4882a593Smuzhiyun 
3873*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3874*4882a593Smuzhiyun 	unconf = find_unconfirmed_client(&dc->clientid, true, nn);
3875*4882a593Smuzhiyun 	conf = find_confirmed_client(&dc->clientid, true, nn);
3876*4882a593Smuzhiyun 	WARN_ON_ONCE(conf && unconf);
3877*4882a593Smuzhiyun 
3878*4882a593Smuzhiyun 	if (conf) {
3879*4882a593Smuzhiyun 		if (client_has_state(conf)) {
3880*4882a593Smuzhiyun 			status = nfserr_clientid_busy;
3881*4882a593Smuzhiyun 			goto out;
3882*4882a593Smuzhiyun 		}
3883*4882a593Smuzhiyun 		status = mark_client_expired_locked(conf);
3884*4882a593Smuzhiyun 		if (status)
3885*4882a593Smuzhiyun 			goto out;
3886*4882a593Smuzhiyun 		clp = conf;
3887*4882a593Smuzhiyun 	} else if (unconf)
3888*4882a593Smuzhiyun 		clp = unconf;
3889*4882a593Smuzhiyun 	else {
3890*4882a593Smuzhiyun 		status = nfserr_stale_clientid;
3891*4882a593Smuzhiyun 		goto out;
3892*4882a593Smuzhiyun 	}
3893*4882a593Smuzhiyun 	if (!nfsd4_mach_creds_match(clp, rqstp)) {
3894*4882a593Smuzhiyun 		clp = NULL;
3895*4882a593Smuzhiyun 		status = nfserr_wrong_cred;
3896*4882a593Smuzhiyun 		goto out;
3897*4882a593Smuzhiyun 	}
3898*4882a593Smuzhiyun 	unhash_client_locked(clp);
3899*4882a593Smuzhiyun out:
3900*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3901*4882a593Smuzhiyun 	if (clp)
3902*4882a593Smuzhiyun 		expire_client(clp);
3903*4882a593Smuzhiyun 	return status;
3904*4882a593Smuzhiyun }
3905*4882a593Smuzhiyun 
3906*4882a593Smuzhiyun __be32
nfsd4_reclaim_complete(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3907*4882a593Smuzhiyun nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3908*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
3909*4882a593Smuzhiyun {
3910*4882a593Smuzhiyun 	struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
3911*4882a593Smuzhiyun 	__be32 status = 0;
3912*4882a593Smuzhiyun 
3913*4882a593Smuzhiyun 	if (rc->rca_one_fs) {
3914*4882a593Smuzhiyun 		if (!cstate->current_fh.fh_dentry)
3915*4882a593Smuzhiyun 			return nfserr_nofilehandle;
3916*4882a593Smuzhiyun 		/*
3917*4882a593Smuzhiyun 		 * We don't take advantage of the rca_one_fs case.
3918*4882a593Smuzhiyun 		 * That's OK, it's optional, we can safely ignore it.
3919*4882a593Smuzhiyun 		 */
3920*4882a593Smuzhiyun 		return nfs_ok;
3921*4882a593Smuzhiyun 	}
3922*4882a593Smuzhiyun 
3923*4882a593Smuzhiyun 	status = nfserr_complete_already;
3924*4882a593Smuzhiyun 	if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3925*4882a593Smuzhiyun 			     &cstate->session->se_client->cl_flags))
3926*4882a593Smuzhiyun 		goto out;
3927*4882a593Smuzhiyun 
3928*4882a593Smuzhiyun 	status = nfserr_stale_clientid;
3929*4882a593Smuzhiyun 	if (is_client_expired(cstate->session->se_client))
3930*4882a593Smuzhiyun 		/*
3931*4882a593Smuzhiyun 		 * The following error isn't really legal.
3932*4882a593Smuzhiyun 		 * But we only get here if the client just explicitly
3933*4882a593Smuzhiyun 		 * destroyed the client.  Surely it no longer cares what
3934*4882a593Smuzhiyun 		 * error it gets back on an operation for the dead
3935*4882a593Smuzhiyun 		 * client.
3936*4882a593Smuzhiyun 		 */
3937*4882a593Smuzhiyun 		goto out;
3938*4882a593Smuzhiyun 
3939*4882a593Smuzhiyun 	status = nfs_ok;
3940*4882a593Smuzhiyun 	nfsd4_client_record_create(cstate->session->se_client);
3941*4882a593Smuzhiyun 	inc_reclaim_complete(cstate->session->se_client);
3942*4882a593Smuzhiyun out:
3943*4882a593Smuzhiyun 	return status;
3944*4882a593Smuzhiyun }
3945*4882a593Smuzhiyun 
3946*4882a593Smuzhiyun __be32
nfsd4_setclientid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)3947*4882a593Smuzhiyun nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3948*4882a593Smuzhiyun 		  union nfsd4_op_u *u)
3949*4882a593Smuzhiyun {
3950*4882a593Smuzhiyun 	struct nfsd4_setclientid *setclid = &u->setclientid;
3951*4882a593Smuzhiyun 	struct xdr_netobj 	clname = setclid->se_name;
3952*4882a593Smuzhiyun 	nfs4_verifier		clverifier = setclid->se_verf;
3953*4882a593Smuzhiyun 	struct nfs4_client	*conf, *new;
3954*4882a593Smuzhiyun 	struct nfs4_client	*unconf = NULL;
3955*4882a593Smuzhiyun 	__be32 			status;
3956*4882a593Smuzhiyun 	struct nfsd_net		*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3957*4882a593Smuzhiyun 
3958*4882a593Smuzhiyun 	new = create_client(clname, rqstp, &clverifier);
3959*4882a593Smuzhiyun 	if (new == NULL)
3960*4882a593Smuzhiyun 		return nfserr_jukebox;
3961*4882a593Smuzhiyun 	/* Cases below refer to rfc 3530 section 14.2.33: */
3962*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
3963*4882a593Smuzhiyun 	conf = find_confirmed_client_by_name(&clname, nn);
3964*4882a593Smuzhiyun 	if (conf && client_has_state(conf)) {
3965*4882a593Smuzhiyun 		/* case 0: */
3966*4882a593Smuzhiyun 		status = nfserr_clid_inuse;
3967*4882a593Smuzhiyun 		if (clp_used_exchangeid(conf))
3968*4882a593Smuzhiyun 			goto out;
3969*4882a593Smuzhiyun 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
3970*4882a593Smuzhiyun 			trace_nfsd_clid_inuse_err(conf);
3971*4882a593Smuzhiyun 			goto out;
3972*4882a593Smuzhiyun 		}
3973*4882a593Smuzhiyun 	}
3974*4882a593Smuzhiyun 	unconf = find_unconfirmed_client_by_name(&clname, nn);
3975*4882a593Smuzhiyun 	if (unconf)
3976*4882a593Smuzhiyun 		unhash_client_locked(unconf);
3977*4882a593Smuzhiyun 	/* We need to handle only case 1: probable callback update */
3978*4882a593Smuzhiyun 	if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
3979*4882a593Smuzhiyun 		copy_clid(new, conf);
3980*4882a593Smuzhiyun 		gen_confirm(new, nn);
3981*4882a593Smuzhiyun 	}
3982*4882a593Smuzhiyun 	new->cl_minorversion = 0;
3983*4882a593Smuzhiyun 	gen_callback(new, setclid, rqstp);
3984*4882a593Smuzhiyun 	add_to_unconfirmed(new);
3985*4882a593Smuzhiyun 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3986*4882a593Smuzhiyun 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3987*4882a593Smuzhiyun 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
3988*4882a593Smuzhiyun 	new = NULL;
3989*4882a593Smuzhiyun 	status = nfs_ok;
3990*4882a593Smuzhiyun out:
3991*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
3992*4882a593Smuzhiyun 	if (new)
3993*4882a593Smuzhiyun 		free_client(new);
3994*4882a593Smuzhiyun 	if (unconf)
3995*4882a593Smuzhiyun 		expire_client(unconf);
3996*4882a593Smuzhiyun 	return status;
3997*4882a593Smuzhiyun }
3998*4882a593Smuzhiyun 
3999*4882a593Smuzhiyun 
4000*4882a593Smuzhiyun __be32
nfsd4_setclientid_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)4001*4882a593Smuzhiyun nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
4002*4882a593Smuzhiyun 			struct nfsd4_compound_state *cstate,
4003*4882a593Smuzhiyun 			union nfsd4_op_u *u)
4004*4882a593Smuzhiyun {
4005*4882a593Smuzhiyun 	struct nfsd4_setclientid_confirm *setclientid_confirm =
4006*4882a593Smuzhiyun 			&u->setclientid_confirm;
4007*4882a593Smuzhiyun 	struct nfs4_client *conf, *unconf;
4008*4882a593Smuzhiyun 	struct nfs4_client *old = NULL;
4009*4882a593Smuzhiyun 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
4010*4882a593Smuzhiyun 	clientid_t * clid = &setclientid_confirm->sc_clientid;
4011*4882a593Smuzhiyun 	__be32 status;
4012*4882a593Smuzhiyun 	struct nfsd_net	*nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4013*4882a593Smuzhiyun 
4014*4882a593Smuzhiyun 	if (STALE_CLIENTID(clid, nn))
4015*4882a593Smuzhiyun 		return nfserr_stale_clientid;
4016*4882a593Smuzhiyun 
4017*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
4018*4882a593Smuzhiyun 	conf = find_confirmed_client(clid, false, nn);
4019*4882a593Smuzhiyun 	unconf = find_unconfirmed_client(clid, false, nn);
4020*4882a593Smuzhiyun 	/*
4021*4882a593Smuzhiyun 	 * We try hard to give out unique clientid's, so if we get an
4022*4882a593Smuzhiyun 	 * attempt to confirm the same clientid with a different cred,
4023*4882a593Smuzhiyun 	 * the client may be buggy; this should never happen.
4024*4882a593Smuzhiyun 	 *
4025*4882a593Smuzhiyun 	 * Nevertheless, RFC 7530 recommends INUSE for this case:
4026*4882a593Smuzhiyun 	 */
4027*4882a593Smuzhiyun 	status = nfserr_clid_inuse;
4028*4882a593Smuzhiyun 	if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4029*4882a593Smuzhiyun 		goto out;
4030*4882a593Smuzhiyun 	if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4031*4882a593Smuzhiyun 		goto out;
4032*4882a593Smuzhiyun 	/* cases below refer to rfc 3530 section 14.2.34: */
4033*4882a593Smuzhiyun 	if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
4034*4882a593Smuzhiyun 		if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4035*4882a593Smuzhiyun 			/* case 2: probable retransmit */
4036*4882a593Smuzhiyun 			status = nfs_ok;
4037*4882a593Smuzhiyun 		} else /* case 4: client hasn't noticed we rebooted yet? */
4038*4882a593Smuzhiyun 			status = nfserr_stale_clientid;
4039*4882a593Smuzhiyun 		goto out;
4040*4882a593Smuzhiyun 	}
4041*4882a593Smuzhiyun 	status = nfs_ok;
4042*4882a593Smuzhiyun 	if (conf) { /* case 1: callback update */
4043*4882a593Smuzhiyun 		old = unconf;
4044*4882a593Smuzhiyun 		unhash_client_locked(old);
4045*4882a593Smuzhiyun 		nfsd4_change_callback(conf, &unconf->cl_cb_conn);
4046*4882a593Smuzhiyun 	} else { /* case 3: normal case; new or rebooted client */
4047*4882a593Smuzhiyun 		old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4048*4882a593Smuzhiyun 		if (old) {
4049*4882a593Smuzhiyun 			status = nfserr_clid_inuse;
4050*4882a593Smuzhiyun 			if (client_has_state(old)
4051*4882a593Smuzhiyun 					&& !same_creds(&unconf->cl_cred,
4052*4882a593Smuzhiyun 							&old->cl_cred)) {
4053*4882a593Smuzhiyun 				old = NULL;
4054*4882a593Smuzhiyun 				goto out;
4055*4882a593Smuzhiyun 			}
4056*4882a593Smuzhiyun 			status = mark_client_expired_locked(old);
4057*4882a593Smuzhiyun 			if (status) {
4058*4882a593Smuzhiyun 				old = NULL;
4059*4882a593Smuzhiyun 				goto out;
4060*4882a593Smuzhiyun 			}
4061*4882a593Smuzhiyun 		}
4062*4882a593Smuzhiyun 		move_to_confirmed(unconf);
4063*4882a593Smuzhiyun 		conf = unconf;
4064*4882a593Smuzhiyun 	}
4065*4882a593Smuzhiyun 	get_client_locked(conf);
4066*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
4067*4882a593Smuzhiyun 	nfsd4_probe_callback(conf);
4068*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
4069*4882a593Smuzhiyun 	put_client_renew_locked(conf);
4070*4882a593Smuzhiyun out:
4071*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
4072*4882a593Smuzhiyun 	if (old)
4073*4882a593Smuzhiyun 		expire_client(old);
4074*4882a593Smuzhiyun 	return status;
4075*4882a593Smuzhiyun }
4076*4882a593Smuzhiyun 
nfsd4_alloc_file(void)4077*4882a593Smuzhiyun static struct nfs4_file *nfsd4_alloc_file(void)
4078*4882a593Smuzhiyun {
4079*4882a593Smuzhiyun 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
4080*4882a593Smuzhiyun }
4081*4882a593Smuzhiyun 
4082*4882a593Smuzhiyun /* OPEN Share state helper functions */
nfsd4_init_file(struct knfsd_fh * fh,unsigned int hashval,struct nfs4_file * fp)4083*4882a593Smuzhiyun static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4084*4882a593Smuzhiyun 				struct nfs4_file *fp)
4085*4882a593Smuzhiyun {
4086*4882a593Smuzhiyun 	lockdep_assert_held(&state_lock);
4087*4882a593Smuzhiyun 
4088*4882a593Smuzhiyun 	refcount_set(&fp->fi_ref, 1);
4089*4882a593Smuzhiyun 	spin_lock_init(&fp->fi_lock);
4090*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fp->fi_stateids);
4091*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fp->fi_delegations);
4092*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fp->fi_clnt_odstate);
4093*4882a593Smuzhiyun 	fh_copy_shallow(&fp->fi_fhandle, fh);
4094*4882a593Smuzhiyun 	fp->fi_deleg_file = NULL;
4095*4882a593Smuzhiyun 	fp->fi_had_conflict = false;
4096*4882a593Smuzhiyun 	fp->fi_share_deny = 0;
4097*4882a593Smuzhiyun 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4098*4882a593Smuzhiyun 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
4099*4882a593Smuzhiyun #ifdef CONFIG_NFSD_PNFS
4100*4882a593Smuzhiyun 	INIT_LIST_HEAD(&fp->fi_lo_states);
4101*4882a593Smuzhiyun 	atomic_set(&fp->fi_lo_recalls, 0);
4102*4882a593Smuzhiyun #endif
4103*4882a593Smuzhiyun 	hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
4104*4882a593Smuzhiyun }
4105*4882a593Smuzhiyun 
4106*4882a593Smuzhiyun void
nfsd4_free_slabs(void)4107*4882a593Smuzhiyun nfsd4_free_slabs(void)
4108*4882a593Smuzhiyun {
4109*4882a593Smuzhiyun 	kmem_cache_destroy(client_slab);
4110*4882a593Smuzhiyun 	kmem_cache_destroy(openowner_slab);
4111*4882a593Smuzhiyun 	kmem_cache_destroy(lockowner_slab);
4112*4882a593Smuzhiyun 	kmem_cache_destroy(file_slab);
4113*4882a593Smuzhiyun 	kmem_cache_destroy(stateid_slab);
4114*4882a593Smuzhiyun 	kmem_cache_destroy(deleg_slab);
4115*4882a593Smuzhiyun 	kmem_cache_destroy(odstate_slab);
4116*4882a593Smuzhiyun }
4117*4882a593Smuzhiyun 
4118*4882a593Smuzhiyun int
nfsd4_init_slabs(void)4119*4882a593Smuzhiyun nfsd4_init_slabs(void)
4120*4882a593Smuzhiyun {
4121*4882a593Smuzhiyun 	client_slab = kmem_cache_create("nfsd4_clients",
4122*4882a593Smuzhiyun 			sizeof(struct nfs4_client), 0, 0, NULL);
4123*4882a593Smuzhiyun 	if (client_slab == NULL)
4124*4882a593Smuzhiyun 		goto out;
4125*4882a593Smuzhiyun 	openowner_slab = kmem_cache_create("nfsd4_openowners",
4126*4882a593Smuzhiyun 			sizeof(struct nfs4_openowner), 0, 0, NULL);
4127*4882a593Smuzhiyun 	if (openowner_slab == NULL)
4128*4882a593Smuzhiyun 		goto out_free_client_slab;
4129*4882a593Smuzhiyun 	lockowner_slab = kmem_cache_create("nfsd4_lockowners",
4130*4882a593Smuzhiyun 			sizeof(struct nfs4_lockowner), 0, 0, NULL);
4131*4882a593Smuzhiyun 	if (lockowner_slab == NULL)
4132*4882a593Smuzhiyun 		goto out_free_openowner_slab;
4133*4882a593Smuzhiyun 	file_slab = kmem_cache_create("nfsd4_files",
4134*4882a593Smuzhiyun 			sizeof(struct nfs4_file), 0, 0, NULL);
4135*4882a593Smuzhiyun 	if (file_slab == NULL)
4136*4882a593Smuzhiyun 		goto out_free_lockowner_slab;
4137*4882a593Smuzhiyun 	stateid_slab = kmem_cache_create("nfsd4_stateids",
4138*4882a593Smuzhiyun 			sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
4139*4882a593Smuzhiyun 	if (stateid_slab == NULL)
4140*4882a593Smuzhiyun 		goto out_free_file_slab;
4141*4882a593Smuzhiyun 	deleg_slab = kmem_cache_create("nfsd4_delegations",
4142*4882a593Smuzhiyun 			sizeof(struct nfs4_delegation), 0, 0, NULL);
4143*4882a593Smuzhiyun 	if (deleg_slab == NULL)
4144*4882a593Smuzhiyun 		goto out_free_stateid_slab;
4145*4882a593Smuzhiyun 	odstate_slab = kmem_cache_create("nfsd4_odstate",
4146*4882a593Smuzhiyun 			sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4147*4882a593Smuzhiyun 	if (odstate_slab == NULL)
4148*4882a593Smuzhiyun 		goto out_free_deleg_slab;
4149*4882a593Smuzhiyun 	return 0;
4150*4882a593Smuzhiyun 
4151*4882a593Smuzhiyun out_free_deleg_slab:
4152*4882a593Smuzhiyun 	kmem_cache_destroy(deleg_slab);
4153*4882a593Smuzhiyun out_free_stateid_slab:
4154*4882a593Smuzhiyun 	kmem_cache_destroy(stateid_slab);
4155*4882a593Smuzhiyun out_free_file_slab:
4156*4882a593Smuzhiyun 	kmem_cache_destroy(file_slab);
4157*4882a593Smuzhiyun out_free_lockowner_slab:
4158*4882a593Smuzhiyun 	kmem_cache_destroy(lockowner_slab);
4159*4882a593Smuzhiyun out_free_openowner_slab:
4160*4882a593Smuzhiyun 	kmem_cache_destroy(openowner_slab);
4161*4882a593Smuzhiyun out_free_client_slab:
4162*4882a593Smuzhiyun 	kmem_cache_destroy(client_slab);
4163*4882a593Smuzhiyun out:
4164*4882a593Smuzhiyun 	return -ENOMEM;
4165*4882a593Smuzhiyun }
4166*4882a593Smuzhiyun 
init_nfs4_replay(struct nfs4_replay * rp)4167*4882a593Smuzhiyun static void init_nfs4_replay(struct nfs4_replay *rp)
4168*4882a593Smuzhiyun {
4169*4882a593Smuzhiyun 	rp->rp_status = nfserr_serverfault;
4170*4882a593Smuzhiyun 	rp->rp_buflen = 0;
4171*4882a593Smuzhiyun 	rp->rp_buf = rp->rp_ibuf;
4172*4882a593Smuzhiyun 	mutex_init(&rp->rp_mutex);
4173*4882a593Smuzhiyun }
4174*4882a593Smuzhiyun 
nfsd4_cstate_assign_replay(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so)4175*4882a593Smuzhiyun static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4176*4882a593Smuzhiyun 		struct nfs4_stateowner *so)
4177*4882a593Smuzhiyun {
4178*4882a593Smuzhiyun 	if (!nfsd4_has_session(cstate)) {
4179*4882a593Smuzhiyun 		mutex_lock(&so->so_replay.rp_mutex);
4180*4882a593Smuzhiyun 		cstate->replay_owner = nfs4_get_stateowner(so);
4181*4882a593Smuzhiyun 	}
4182*4882a593Smuzhiyun }
4183*4882a593Smuzhiyun 
nfsd4_cstate_clear_replay(struct nfsd4_compound_state * cstate)4184*4882a593Smuzhiyun void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4185*4882a593Smuzhiyun {
4186*4882a593Smuzhiyun 	struct nfs4_stateowner *so = cstate->replay_owner;
4187*4882a593Smuzhiyun 
4188*4882a593Smuzhiyun 	if (so != NULL) {
4189*4882a593Smuzhiyun 		cstate->replay_owner = NULL;
4190*4882a593Smuzhiyun 		mutex_unlock(&so->so_replay.rp_mutex);
4191*4882a593Smuzhiyun 		nfs4_put_stateowner(so);
4192*4882a593Smuzhiyun 	}
4193*4882a593Smuzhiyun }
4194*4882a593Smuzhiyun 
alloc_stateowner(struct kmem_cache * slab,struct xdr_netobj * owner,struct nfs4_client * clp)4195*4882a593Smuzhiyun static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
4196*4882a593Smuzhiyun {
4197*4882a593Smuzhiyun 	struct nfs4_stateowner *sop;
4198*4882a593Smuzhiyun 
4199*4882a593Smuzhiyun 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
4200*4882a593Smuzhiyun 	if (!sop)
4201*4882a593Smuzhiyun 		return NULL;
4202*4882a593Smuzhiyun 
4203*4882a593Smuzhiyun 	xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
4204*4882a593Smuzhiyun 	if (!sop->so_owner.data) {
4205*4882a593Smuzhiyun 		kmem_cache_free(slab, sop);
4206*4882a593Smuzhiyun 		return NULL;
4207*4882a593Smuzhiyun 	}
4208*4882a593Smuzhiyun 
4209*4882a593Smuzhiyun 	INIT_LIST_HEAD(&sop->so_stateids);
4210*4882a593Smuzhiyun 	sop->so_client = clp;
4211*4882a593Smuzhiyun 	init_nfs4_replay(&sop->so_replay);
4212*4882a593Smuzhiyun 	atomic_set(&sop->so_count, 1);
4213*4882a593Smuzhiyun 	return sop;
4214*4882a593Smuzhiyun }
4215*4882a593Smuzhiyun 
hash_openowner(struct nfs4_openowner * oo,struct nfs4_client * clp,unsigned int strhashval)4216*4882a593Smuzhiyun static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
4217*4882a593Smuzhiyun {
4218*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
4219*4882a593Smuzhiyun 
4220*4882a593Smuzhiyun 	list_add(&oo->oo_owner.so_strhash,
4221*4882a593Smuzhiyun 		 &clp->cl_ownerstr_hashtbl[strhashval]);
4222*4882a593Smuzhiyun 	list_add(&oo->oo_perclient, &clp->cl_openowners);
4223*4882a593Smuzhiyun }
4224*4882a593Smuzhiyun 
nfs4_unhash_openowner(struct nfs4_stateowner * so)4225*4882a593Smuzhiyun static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4226*4882a593Smuzhiyun {
4227*4882a593Smuzhiyun 	unhash_openowner_locked(openowner(so));
4228*4882a593Smuzhiyun }
4229*4882a593Smuzhiyun 
nfs4_free_openowner(struct nfs4_stateowner * so)4230*4882a593Smuzhiyun static void nfs4_free_openowner(struct nfs4_stateowner *so)
4231*4882a593Smuzhiyun {
4232*4882a593Smuzhiyun 	struct nfs4_openowner *oo = openowner(so);
4233*4882a593Smuzhiyun 
4234*4882a593Smuzhiyun 	kmem_cache_free(openowner_slab, oo);
4235*4882a593Smuzhiyun }
4236*4882a593Smuzhiyun 
4237*4882a593Smuzhiyun static const struct nfs4_stateowner_operations openowner_ops = {
4238*4882a593Smuzhiyun 	.so_unhash =	nfs4_unhash_openowner,
4239*4882a593Smuzhiyun 	.so_free =	nfs4_free_openowner,
4240*4882a593Smuzhiyun };
4241*4882a593Smuzhiyun 
4242*4882a593Smuzhiyun static struct nfs4_ol_stateid *
nfsd4_find_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)4243*4882a593Smuzhiyun nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4244*4882a593Smuzhiyun {
4245*4882a593Smuzhiyun 	struct nfs4_ol_stateid *local, *ret = NULL;
4246*4882a593Smuzhiyun 	struct nfs4_openowner *oo = open->op_openowner;
4247*4882a593Smuzhiyun 
4248*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
4249*4882a593Smuzhiyun 
4250*4882a593Smuzhiyun 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4251*4882a593Smuzhiyun 		/* ignore lock owners */
4252*4882a593Smuzhiyun 		if (local->st_stateowner->so_is_open_owner == 0)
4253*4882a593Smuzhiyun 			continue;
4254*4882a593Smuzhiyun 		if (local->st_stateowner != &oo->oo_owner)
4255*4882a593Smuzhiyun 			continue;
4256*4882a593Smuzhiyun 		if (local->st_stid.sc_type == NFS4_OPEN_STID) {
4257*4882a593Smuzhiyun 			ret = local;
4258*4882a593Smuzhiyun 			refcount_inc(&ret->st_stid.sc_count);
4259*4882a593Smuzhiyun 			break;
4260*4882a593Smuzhiyun 		}
4261*4882a593Smuzhiyun 	}
4262*4882a593Smuzhiyun 	return ret;
4263*4882a593Smuzhiyun }
4264*4882a593Smuzhiyun 
4265*4882a593Smuzhiyun static __be32
nfsd4_verify_open_stid(struct nfs4_stid * s)4266*4882a593Smuzhiyun nfsd4_verify_open_stid(struct nfs4_stid *s)
4267*4882a593Smuzhiyun {
4268*4882a593Smuzhiyun 	__be32 ret = nfs_ok;
4269*4882a593Smuzhiyun 
4270*4882a593Smuzhiyun 	switch (s->sc_type) {
4271*4882a593Smuzhiyun 	default:
4272*4882a593Smuzhiyun 		break;
4273*4882a593Smuzhiyun 	case 0:
4274*4882a593Smuzhiyun 	case NFS4_CLOSED_STID:
4275*4882a593Smuzhiyun 	case NFS4_CLOSED_DELEG_STID:
4276*4882a593Smuzhiyun 		ret = nfserr_bad_stateid;
4277*4882a593Smuzhiyun 		break;
4278*4882a593Smuzhiyun 	case NFS4_REVOKED_DELEG_STID:
4279*4882a593Smuzhiyun 		ret = nfserr_deleg_revoked;
4280*4882a593Smuzhiyun 	}
4281*4882a593Smuzhiyun 	return ret;
4282*4882a593Smuzhiyun }
4283*4882a593Smuzhiyun 
4284*4882a593Smuzhiyun /* Lock the stateid st_mutex, and deal with races with CLOSE */
4285*4882a593Smuzhiyun static __be32
nfsd4_lock_ol_stateid(struct nfs4_ol_stateid * stp)4286*4882a593Smuzhiyun nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4287*4882a593Smuzhiyun {
4288*4882a593Smuzhiyun 	__be32 ret;
4289*4882a593Smuzhiyun 
4290*4882a593Smuzhiyun 	mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
4291*4882a593Smuzhiyun 	ret = nfsd4_verify_open_stid(&stp->st_stid);
4292*4882a593Smuzhiyun 	if (ret != nfs_ok)
4293*4882a593Smuzhiyun 		mutex_unlock(&stp->st_mutex);
4294*4882a593Smuzhiyun 	return ret;
4295*4882a593Smuzhiyun }
4296*4882a593Smuzhiyun 
4297*4882a593Smuzhiyun static struct nfs4_ol_stateid *
nfsd4_find_and_lock_existing_open(struct nfs4_file * fp,struct nfsd4_open * open)4298*4882a593Smuzhiyun nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4299*4882a593Smuzhiyun {
4300*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
4301*4882a593Smuzhiyun 	for (;;) {
4302*4882a593Smuzhiyun 		spin_lock(&fp->fi_lock);
4303*4882a593Smuzhiyun 		stp = nfsd4_find_existing_open(fp, open);
4304*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
4305*4882a593Smuzhiyun 		if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4306*4882a593Smuzhiyun 			break;
4307*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
4308*4882a593Smuzhiyun 	}
4309*4882a593Smuzhiyun 	return stp;
4310*4882a593Smuzhiyun }
4311*4882a593Smuzhiyun 
4312*4882a593Smuzhiyun static struct nfs4_openowner *
alloc_init_open_stateowner(unsigned int strhashval,struct nfsd4_open * open,struct nfsd4_compound_state * cstate)4313*4882a593Smuzhiyun alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
4314*4882a593Smuzhiyun 			   struct nfsd4_compound_state *cstate)
4315*4882a593Smuzhiyun {
4316*4882a593Smuzhiyun 	struct nfs4_client *clp = cstate->clp;
4317*4882a593Smuzhiyun 	struct nfs4_openowner *oo, *ret;
4318*4882a593Smuzhiyun 
4319*4882a593Smuzhiyun 	oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4320*4882a593Smuzhiyun 	if (!oo)
4321*4882a593Smuzhiyun 		return NULL;
4322*4882a593Smuzhiyun 	oo->oo_owner.so_ops = &openowner_ops;
4323*4882a593Smuzhiyun 	oo->oo_owner.so_is_open_owner = 1;
4324*4882a593Smuzhiyun 	oo->oo_owner.so_seqid = open->op_seqid;
4325*4882a593Smuzhiyun 	oo->oo_flags = 0;
4326*4882a593Smuzhiyun 	if (nfsd4_has_session(cstate))
4327*4882a593Smuzhiyun 		oo->oo_flags |= NFS4_OO_CONFIRMED;
4328*4882a593Smuzhiyun 	oo->oo_time = 0;
4329*4882a593Smuzhiyun 	oo->oo_last_closed_stid = NULL;
4330*4882a593Smuzhiyun 	INIT_LIST_HEAD(&oo->oo_close_lru);
4331*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
4332*4882a593Smuzhiyun 	ret = find_openstateowner_str_locked(strhashval, open, clp);
4333*4882a593Smuzhiyun 	if (ret == NULL) {
4334*4882a593Smuzhiyun 		hash_openowner(oo, clp, strhashval);
4335*4882a593Smuzhiyun 		ret = oo;
4336*4882a593Smuzhiyun 	} else
4337*4882a593Smuzhiyun 		nfs4_free_stateowner(&oo->oo_owner);
4338*4882a593Smuzhiyun 
4339*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
4340*4882a593Smuzhiyun 	return ret;
4341*4882a593Smuzhiyun }
4342*4882a593Smuzhiyun 
4343*4882a593Smuzhiyun static struct nfs4_ol_stateid *
init_open_stateid(struct nfs4_file * fp,struct nfsd4_open * open)4344*4882a593Smuzhiyun init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
4345*4882a593Smuzhiyun {
4346*4882a593Smuzhiyun 
4347*4882a593Smuzhiyun 	struct nfs4_openowner *oo = open->op_openowner;
4348*4882a593Smuzhiyun 	struct nfs4_ol_stateid *retstp = NULL;
4349*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
4350*4882a593Smuzhiyun 
4351*4882a593Smuzhiyun 	stp = open->op_stp;
4352*4882a593Smuzhiyun 	/* We are moving these outside of the spinlocks to avoid the warnings */
4353*4882a593Smuzhiyun 	mutex_init(&stp->st_mutex);
4354*4882a593Smuzhiyun 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
4355*4882a593Smuzhiyun 
4356*4882a593Smuzhiyun retry:
4357*4882a593Smuzhiyun 	spin_lock(&oo->oo_owner.so_client->cl_lock);
4358*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
4359*4882a593Smuzhiyun 
4360*4882a593Smuzhiyun 	retstp = nfsd4_find_existing_open(fp, open);
4361*4882a593Smuzhiyun 	if (retstp)
4362*4882a593Smuzhiyun 		goto out_unlock;
4363*4882a593Smuzhiyun 
4364*4882a593Smuzhiyun 	open->op_stp = NULL;
4365*4882a593Smuzhiyun 	refcount_inc(&stp->st_stid.sc_count);
4366*4882a593Smuzhiyun 	stp->st_stid.sc_type = NFS4_OPEN_STID;
4367*4882a593Smuzhiyun 	INIT_LIST_HEAD(&stp->st_locks);
4368*4882a593Smuzhiyun 	stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
4369*4882a593Smuzhiyun 	get_nfs4_file(fp);
4370*4882a593Smuzhiyun 	stp->st_stid.sc_file = fp;
4371*4882a593Smuzhiyun 	stp->st_access_bmap = 0;
4372*4882a593Smuzhiyun 	stp->st_deny_bmap = 0;
4373*4882a593Smuzhiyun 	stp->st_openstp = NULL;
4374*4882a593Smuzhiyun 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
4375*4882a593Smuzhiyun 	list_add(&stp->st_perfile, &fp->fi_stateids);
4376*4882a593Smuzhiyun 
4377*4882a593Smuzhiyun out_unlock:
4378*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
4379*4882a593Smuzhiyun 	spin_unlock(&oo->oo_owner.so_client->cl_lock);
4380*4882a593Smuzhiyun 	if (retstp) {
4381*4882a593Smuzhiyun 		/* Handle races with CLOSE */
4382*4882a593Smuzhiyun 		if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4383*4882a593Smuzhiyun 			nfs4_put_stid(&retstp->st_stid);
4384*4882a593Smuzhiyun 			goto retry;
4385*4882a593Smuzhiyun 		}
4386*4882a593Smuzhiyun 		/* To keep mutex tracking happy */
4387*4882a593Smuzhiyun 		mutex_unlock(&stp->st_mutex);
4388*4882a593Smuzhiyun 		stp = retstp;
4389*4882a593Smuzhiyun 	}
4390*4882a593Smuzhiyun 	return stp;
4391*4882a593Smuzhiyun }
4392*4882a593Smuzhiyun 
4393*4882a593Smuzhiyun /*
4394*4882a593Smuzhiyun  * In the 4.0 case we need to keep the owners around a little while to handle
4395*4882a593Smuzhiyun  * CLOSE replay. We still do need to release any file access that is held by
4396*4882a593Smuzhiyun  * them before returning however.
4397*4882a593Smuzhiyun  */
4398*4882a593Smuzhiyun static void
move_to_close_lru(struct nfs4_ol_stateid * s,struct net * net)4399*4882a593Smuzhiyun move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
4400*4882a593Smuzhiyun {
4401*4882a593Smuzhiyun 	struct nfs4_ol_stateid *last;
4402*4882a593Smuzhiyun 	struct nfs4_openowner *oo = openowner(s->st_stateowner);
4403*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4404*4882a593Smuzhiyun 						nfsd_net_id);
4405*4882a593Smuzhiyun 
4406*4882a593Smuzhiyun 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
4407*4882a593Smuzhiyun 
4408*4882a593Smuzhiyun 	/*
4409*4882a593Smuzhiyun 	 * We know that we hold one reference via nfsd4_close, and another
4410*4882a593Smuzhiyun 	 * "persistent" reference for the client. If the refcount is higher
4411*4882a593Smuzhiyun 	 * than 2, then there are still calls in progress that are using this
4412*4882a593Smuzhiyun 	 * stateid. We can't put the sc_file reference until they are finished.
4413*4882a593Smuzhiyun 	 * Wait for the refcount to drop to 2. Since it has been unhashed,
4414*4882a593Smuzhiyun 	 * there should be no danger of the refcount going back up again at
4415*4882a593Smuzhiyun 	 * this point.
4416*4882a593Smuzhiyun 	 */
4417*4882a593Smuzhiyun 	wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
4418*4882a593Smuzhiyun 
4419*4882a593Smuzhiyun 	release_all_access(s);
4420*4882a593Smuzhiyun 	if (s->st_stid.sc_file) {
4421*4882a593Smuzhiyun 		put_nfs4_file(s->st_stid.sc_file);
4422*4882a593Smuzhiyun 		s->st_stid.sc_file = NULL;
4423*4882a593Smuzhiyun 	}
4424*4882a593Smuzhiyun 
4425*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
4426*4882a593Smuzhiyun 	last = oo->oo_last_closed_stid;
4427*4882a593Smuzhiyun 	oo->oo_last_closed_stid = s;
4428*4882a593Smuzhiyun 	list_move_tail(&oo->oo_close_lru, &nn->close_lru);
4429*4882a593Smuzhiyun 	oo->oo_time = ktime_get_boottime_seconds();
4430*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
4431*4882a593Smuzhiyun 	if (last)
4432*4882a593Smuzhiyun 		nfs4_put_stid(&last->st_stid);
4433*4882a593Smuzhiyun }
4434*4882a593Smuzhiyun 
4435*4882a593Smuzhiyun /* search file_hashtbl[] for file */
4436*4882a593Smuzhiyun static struct nfs4_file *
find_file_locked(struct knfsd_fh * fh,unsigned int hashval)4437*4882a593Smuzhiyun find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
4438*4882a593Smuzhiyun {
4439*4882a593Smuzhiyun 	struct nfs4_file *fp;
4440*4882a593Smuzhiyun 
4441*4882a593Smuzhiyun 	hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4442*4882a593Smuzhiyun 				lockdep_is_held(&state_lock)) {
4443*4882a593Smuzhiyun 		if (fh_match(&fp->fi_fhandle, fh)) {
4444*4882a593Smuzhiyun 			if (refcount_inc_not_zero(&fp->fi_ref))
4445*4882a593Smuzhiyun 				return fp;
4446*4882a593Smuzhiyun 		}
4447*4882a593Smuzhiyun 	}
4448*4882a593Smuzhiyun 	return NULL;
4449*4882a593Smuzhiyun }
4450*4882a593Smuzhiyun 
4451*4882a593Smuzhiyun struct nfs4_file *
find_file(struct knfsd_fh * fh)4452*4882a593Smuzhiyun find_file(struct knfsd_fh *fh)
4453*4882a593Smuzhiyun {
4454*4882a593Smuzhiyun 	struct nfs4_file *fp;
4455*4882a593Smuzhiyun 	unsigned int hashval = file_hashval(fh);
4456*4882a593Smuzhiyun 
4457*4882a593Smuzhiyun 	rcu_read_lock();
4458*4882a593Smuzhiyun 	fp = find_file_locked(fh, hashval);
4459*4882a593Smuzhiyun 	rcu_read_unlock();
4460*4882a593Smuzhiyun 	return fp;
4461*4882a593Smuzhiyun }
4462*4882a593Smuzhiyun 
4463*4882a593Smuzhiyun static struct nfs4_file *
find_or_add_file(struct nfs4_file * new,struct knfsd_fh * fh)4464*4882a593Smuzhiyun find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
4465*4882a593Smuzhiyun {
4466*4882a593Smuzhiyun 	struct nfs4_file *fp;
4467*4882a593Smuzhiyun 	unsigned int hashval = file_hashval(fh);
4468*4882a593Smuzhiyun 
4469*4882a593Smuzhiyun 	rcu_read_lock();
4470*4882a593Smuzhiyun 	fp = find_file_locked(fh, hashval);
4471*4882a593Smuzhiyun 	rcu_read_unlock();
4472*4882a593Smuzhiyun 	if (fp)
4473*4882a593Smuzhiyun 		return fp;
4474*4882a593Smuzhiyun 
4475*4882a593Smuzhiyun 	spin_lock(&state_lock);
4476*4882a593Smuzhiyun 	fp = find_file_locked(fh, hashval);
4477*4882a593Smuzhiyun 	if (likely(fp == NULL)) {
4478*4882a593Smuzhiyun 		nfsd4_init_file(fh, hashval, new);
4479*4882a593Smuzhiyun 		fp = new;
4480*4882a593Smuzhiyun 	}
4481*4882a593Smuzhiyun 	spin_unlock(&state_lock);
4482*4882a593Smuzhiyun 
4483*4882a593Smuzhiyun 	return fp;
4484*4882a593Smuzhiyun }
4485*4882a593Smuzhiyun 
4486*4882a593Smuzhiyun /*
4487*4882a593Smuzhiyun  * Called to check deny when READ with all zero stateid or
4488*4882a593Smuzhiyun  * WRITE with all zero or all one stateid
4489*4882a593Smuzhiyun  */
4490*4882a593Smuzhiyun static __be32
nfs4_share_conflict(struct svc_fh * current_fh,unsigned int deny_type)4491*4882a593Smuzhiyun nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4492*4882a593Smuzhiyun {
4493*4882a593Smuzhiyun 	struct nfs4_file *fp;
4494*4882a593Smuzhiyun 	__be32 ret = nfs_ok;
4495*4882a593Smuzhiyun 
4496*4882a593Smuzhiyun 	fp = find_file(&current_fh->fh_handle);
4497*4882a593Smuzhiyun 	if (!fp)
4498*4882a593Smuzhiyun 		return ret;
4499*4882a593Smuzhiyun 	/* Check for conflicting share reservations */
4500*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
4501*4882a593Smuzhiyun 	if (fp->fi_share_deny & deny_type)
4502*4882a593Smuzhiyun 		ret = nfserr_locked;
4503*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
4504*4882a593Smuzhiyun 	put_nfs4_file(fp);
4505*4882a593Smuzhiyun 	return ret;
4506*4882a593Smuzhiyun }
4507*4882a593Smuzhiyun 
nfsd4_cb_recall_prepare(struct nfsd4_callback * cb)4508*4882a593Smuzhiyun static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
4509*4882a593Smuzhiyun {
4510*4882a593Smuzhiyun 	struct nfs4_delegation *dp = cb_to_delegation(cb);
4511*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4512*4882a593Smuzhiyun 					  nfsd_net_id);
4513*4882a593Smuzhiyun 
4514*4882a593Smuzhiyun 	block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
4515*4882a593Smuzhiyun 
4516*4882a593Smuzhiyun 	/*
4517*4882a593Smuzhiyun 	 * We can't do this in nfsd_break_deleg_cb because it is
4518*4882a593Smuzhiyun 	 * already holding inode->i_lock.
4519*4882a593Smuzhiyun 	 *
4520*4882a593Smuzhiyun 	 * If the dl_time != 0, then we know that it has already been
4521*4882a593Smuzhiyun 	 * queued for a lease break. Don't queue it again.
4522*4882a593Smuzhiyun 	 */
4523*4882a593Smuzhiyun 	spin_lock(&state_lock);
4524*4882a593Smuzhiyun 	if (delegation_hashed(dp) && dp->dl_time == 0) {
4525*4882a593Smuzhiyun 		dp->dl_time = ktime_get_boottime_seconds();
4526*4882a593Smuzhiyun 		list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
4527*4882a593Smuzhiyun 	}
4528*4882a593Smuzhiyun 	spin_unlock(&state_lock);
4529*4882a593Smuzhiyun }
4530*4882a593Smuzhiyun 
nfsd4_cb_recall_done(struct nfsd4_callback * cb,struct rpc_task * task)4531*4882a593Smuzhiyun static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4532*4882a593Smuzhiyun 		struct rpc_task *task)
4533*4882a593Smuzhiyun {
4534*4882a593Smuzhiyun 	struct nfs4_delegation *dp = cb_to_delegation(cb);
4535*4882a593Smuzhiyun 
4536*4882a593Smuzhiyun 	if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4537*4882a593Smuzhiyun 	    dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4538*4882a593Smuzhiyun 	        return 1;
4539*4882a593Smuzhiyun 
4540*4882a593Smuzhiyun 	switch (task->tk_status) {
4541*4882a593Smuzhiyun 	case 0:
4542*4882a593Smuzhiyun 		return 1;
4543*4882a593Smuzhiyun 	case -NFS4ERR_DELAY:
4544*4882a593Smuzhiyun 		rpc_delay(task, 2 * HZ);
4545*4882a593Smuzhiyun 		return 0;
4546*4882a593Smuzhiyun 	case -EBADHANDLE:
4547*4882a593Smuzhiyun 	case -NFS4ERR_BAD_STATEID:
4548*4882a593Smuzhiyun 		/*
4549*4882a593Smuzhiyun 		 * Race: client probably got cb_recall before open reply
4550*4882a593Smuzhiyun 		 * granting delegation.
4551*4882a593Smuzhiyun 		 */
4552*4882a593Smuzhiyun 		if (dp->dl_retries--) {
4553*4882a593Smuzhiyun 			rpc_delay(task, 2 * HZ);
4554*4882a593Smuzhiyun 			return 0;
4555*4882a593Smuzhiyun 		}
4556*4882a593Smuzhiyun 		fallthrough;
4557*4882a593Smuzhiyun 	default:
4558*4882a593Smuzhiyun 		return 1;
4559*4882a593Smuzhiyun 	}
4560*4882a593Smuzhiyun }
4561*4882a593Smuzhiyun 
nfsd4_cb_recall_release(struct nfsd4_callback * cb)4562*4882a593Smuzhiyun static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4563*4882a593Smuzhiyun {
4564*4882a593Smuzhiyun 	struct nfs4_delegation *dp = cb_to_delegation(cb);
4565*4882a593Smuzhiyun 
4566*4882a593Smuzhiyun 	nfs4_put_stid(&dp->dl_stid);
4567*4882a593Smuzhiyun }
4568*4882a593Smuzhiyun 
4569*4882a593Smuzhiyun static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
4570*4882a593Smuzhiyun 	.prepare	= nfsd4_cb_recall_prepare,
4571*4882a593Smuzhiyun 	.done		= nfsd4_cb_recall_done,
4572*4882a593Smuzhiyun 	.release	= nfsd4_cb_recall_release,
4573*4882a593Smuzhiyun };
4574*4882a593Smuzhiyun 
nfsd_break_one_deleg(struct nfs4_delegation * dp)4575*4882a593Smuzhiyun static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4576*4882a593Smuzhiyun {
4577*4882a593Smuzhiyun 	/*
4578*4882a593Smuzhiyun 	 * We're assuming the state code never drops its reference
4579*4882a593Smuzhiyun 	 * without first removing the lease.  Since we're in this lease
4580*4882a593Smuzhiyun 	 * callback (and since the lease code is serialized by the
4581*4882a593Smuzhiyun 	 * i_lock) we know the server hasn't removed the lease yet, and
4582*4882a593Smuzhiyun 	 * we know it's safe to take a reference.
4583*4882a593Smuzhiyun 	 */
4584*4882a593Smuzhiyun 	refcount_inc(&dp->dl_stid.sc_count);
4585*4882a593Smuzhiyun 	nfsd4_run_cb(&dp->dl_recall);
4586*4882a593Smuzhiyun }
4587*4882a593Smuzhiyun 
4588*4882a593Smuzhiyun /* Called from break_lease() with i_lock held. */
4589*4882a593Smuzhiyun static bool
nfsd_break_deleg_cb(struct file_lock * fl)4590*4882a593Smuzhiyun nfsd_break_deleg_cb(struct file_lock *fl)
4591*4882a593Smuzhiyun {
4592*4882a593Smuzhiyun 	bool ret = false;
4593*4882a593Smuzhiyun 	struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4594*4882a593Smuzhiyun 	struct nfs4_file *fp = dp->dl_stid.sc_file;
4595*4882a593Smuzhiyun 
4596*4882a593Smuzhiyun 	trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4597*4882a593Smuzhiyun 
4598*4882a593Smuzhiyun 	/*
4599*4882a593Smuzhiyun 	 * We don't want the locks code to timeout the lease for us;
4600*4882a593Smuzhiyun 	 * we'll remove it ourself if a delegation isn't returned
4601*4882a593Smuzhiyun 	 * in time:
4602*4882a593Smuzhiyun 	 */
4603*4882a593Smuzhiyun 	fl->fl_break_time = 0;
4604*4882a593Smuzhiyun 
4605*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
4606*4882a593Smuzhiyun 	fp->fi_had_conflict = true;
4607*4882a593Smuzhiyun 	nfsd_break_one_deleg(dp);
4608*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
4609*4882a593Smuzhiyun 	return ret;
4610*4882a593Smuzhiyun }
4611*4882a593Smuzhiyun 
4612*4882a593Smuzhiyun /**
4613*4882a593Smuzhiyun  * nfsd_breaker_owns_lease - Check if lease conflict was resolved
4614*4882a593Smuzhiyun  * @fl: Lock state to check
4615*4882a593Smuzhiyun  *
4616*4882a593Smuzhiyun  * Return values:
4617*4882a593Smuzhiyun  *   %true: Lease conflict was resolved
4618*4882a593Smuzhiyun  *   %false: Lease conflict was not resolved.
4619*4882a593Smuzhiyun  */
nfsd_breaker_owns_lease(struct file_lock * fl)4620*4882a593Smuzhiyun static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4621*4882a593Smuzhiyun {
4622*4882a593Smuzhiyun 	struct nfs4_delegation *dl = fl->fl_owner;
4623*4882a593Smuzhiyun 	struct svc_rqst *rqst;
4624*4882a593Smuzhiyun 	struct nfs4_client *clp;
4625*4882a593Smuzhiyun 
4626*4882a593Smuzhiyun 	if (!i_am_nfsd())
4627*4882a593Smuzhiyun 		return false;
4628*4882a593Smuzhiyun 	rqst = kthread_data(current);
4629*4882a593Smuzhiyun 	/* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4630*4882a593Smuzhiyun 	if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
4631*4882a593Smuzhiyun 		return false;
4632*4882a593Smuzhiyun 	clp = *(rqst->rq_lease_breaker);
4633*4882a593Smuzhiyun 	return dl->dl_stid.sc_client == clp;
4634*4882a593Smuzhiyun }
4635*4882a593Smuzhiyun 
4636*4882a593Smuzhiyun static int
nfsd_change_deleg_cb(struct file_lock * onlist,int arg,struct list_head * dispose)4637*4882a593Smuzhiyun nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4638*4882a593Smuzhiyun 		     struct list_head *dispose)
4639*4882a593Smuzhiyun {
4640*4882a593Smuzhiyun 	if (arg & F_UNLCK)
4641*4882a593Smuzhiyun 		return lease_modify(onlist, arg, dispose);
4642*4882a593Smuzhiyun 	else
4643*4882a593Smuzhiyun 		return -EAGAIN;
4644*4882a593Smuzhiyun }
4645*4882a593Smuzhiyun 
4646*4882a593Smuzhiyun static const struct lock_manager_operations nfsd_lease_mng_ops = {
4647*4882a593Smuzhiyun 	.lm_breaker_owns_lease = nfsd_breaker_owns_lease,
4648*4882a593Smuzhiyun 	.lm_break = nfsd_break_deleg_cb,
4649*4882a593Smuzhiyun 	.lm_change = nfsd_change_deleg_cb,
4650*4882a593Smuzhiyun };
4651*4882a593Smuzhiyun 
nfsd4_check_seqid(struct nfsd4_compound_state * cstate,struct nfs4_stateowner * so,u32 seqid)4652*4882a593Smuzhiyun static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4653*4882a593Smuzhiyun {
4654*4882a593Smuzhiyun 	if (nfsd4_has_session(cstate))
4655*4882a593Smuzhiyun 		return nfs_ok;
4656*4882a593Smuzhiyun 	if (seqid == so->so_seqid - 1)
4657*4882a593Smuzhiyun 		return nfserr_replay_me;
4658*4882a593Smuzhiyun 	if (seqid == so->so_seqid)
4659*4882a593Smuzhiyun 		return nfs_ok;
4660*4882a593Smuzhiyun 	return nfserr_bad_seqid;
4661*4882a593Smuzhiyun }
4662*4882a593Smuzhiyun 
lookup_clientid(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn,bool sessions)4663*4882a593Smuzhiyun static __be32 lookup_clientid(clientid_t *clid,
4664*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate,
4665*4882a593Smuzhiyun 		struct nfsd_net *nn,
4666*4882a593Smuzhiyun 		bool sessions)
4667*4882a593Smuzhiyun {
4668*4882a593Smuzhiyun 	struct nfs4_client *found;
4669*4882a593Smuzhiyun 
4670*4882a593Smuzhiyun 	if (cstate->clp) {
4671*4882a593Smuzhiyun 		found = cstate->clp;
4672*4882a593Smuzhiyun 		if (!same_clid(&found->cl_clientid, clid))
4673*4882a593Smuzhiyun 			return nfserr_stale_clientid;
4674*4882a593Smuzhiyun 		return nfs_ok;
4675*4882a593Smuzhiyun 	}
4676*4882a593Smuzhiyun 
4677*4882a593Smuzhiyun 	if (STALE_CLIENTID(clid, nn))
4678*4882a593Smuzhiyun 		return nfserr_stale_clientid;
4679*4882a593Smuzhiyun 
4680*4882a593Smuzhiyun 	/*
4681*4882a593Smuzhiyun 	 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4682*4882a593Smuzhiyun 	 * cached already then we know this is for is for v4.0 and "sessions"
4683*4882a593Smuzhiyun 	 * will be false.
4684*4882a593Smuzhiyun 	 */
4685*4882a593Smuzhiyun 	WARN_ON_ONCE(cstate->session);
4686*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
4687*4882a593Smuzhiyun 	found = find_confirmed_client(clid, sessions, nn);
4688*4882a593Smuzhiyun 	if (!found) {
4689*4882a593Smuzhiyun 		spin_unlock(&nn->client_lock);
4690*4882a593Smuzhiyun 		return nfserr_expired;
4691*4882a593Smuzhiyun 	}
4692*4882a593Smuzhiyun 	atomic_inc(&found->cl_rpc_users);
4693*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
4694*4882a593Smuzhiyun 
4695*4882a593Smuzhiyun 	/* Cache the nfs4_client in cstate! */
4696*4882a593Smuzhiyun 	cstate->clp = found;
4697*4882a593Smuzhiyun 	return nfs_ok;
4698*4882a593Smuzhiyun }
4699*4882a593Smuzhiyun 
4700*4882a593Smuzhiyun __be32
nfsd4_process_open1(struct nfsd4_compound_state * cstate,struct nfsd4_open * open,struct nfsd_net * nn)4701*4882a593Smuzhiyun nfsd4_process_open1(struct nfsd4_compound_state *cstate,
4702*4882a593Smuzhiyun 		    struct nfsd4_open *open, struct nfsd_net *nn)
4703*4882a593Smuzhiyun {
4704*4882a593Smuzhiyun 	clientid_t *clientid = &open->op_clientid;
4705*4882a593Smuzhiyun 	struct nfs4_client *clp = NULL;
4706*4882a593Smuzhiyun 	unsigned int strhashval;
4707*4882a593Smuzhiyun 	struct nfs4_openowner *oo = NULL;
4708*4882a593Smuzhiyun 	__be32 status;
4709*4882a593Smuzhiyun 
4710*4882a593Smuzhiyun 	if (STALE_CLIENTID(&open->op_clientid, nn))
4711*4882a593Smuzhiyun 		return nfserr_stale_clientid;
4712*4882a593Smuzhiyun 	/*
4713*4882a593Smuzhiyun 	 * In case we need it later, after we've already created the
4714*4882a593Smuzhiyun 	 * file and don't want to risk a further failure:
4715*4882a593Smuzhiyun 	 */
4716*4882a593Smuzhiyun 	open->op_file = nfsd4_alloc_file();
4717*4882a593Smuzhiyun 	if (open->op_file == NULL)
4718*4882a593Smuzhiyun 		return nfserr_jukebox;
4719*4882a593Smuzhiyun 
4720*4882a593Smuzhiyun 	status = lookup_clientid(clientid, cstate, nn, false);
4721*4882a593Smuzhiyun 	if (status)
4722*4882a593Smuzhiyun 		return status;
4723*4882a593Smuzhiyun 	clp = cstate->clp;
4724*4882a593Smuzhiyun 
4725*4882a593Smuzhiyun 	strhashval = ownerstr_hashval(&open->op_owner);
4726*4882a593Smuzhiyun 	oo = find_openstateowner_str(strhashval, open, clp);
4727*4882a593Smuzhiyun 	open->op_openowner = oo;
4728*4882a593Smuzhiyun 	if (!oo) {
4729*4882a593Smuzhiyun 		goto new_owner;
4730*4882a593Smuzhiyun 	}
4731*4882a593Smuzhiyun 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4732*4882a593Smuzhiyun 		/* Replace unconfirmed owners without checking for replay. */
4733*4882a593Smuzhiyun 		release_openowner(oo);
4734*4882a593Smuzhiyun 		open->op_openowner = NULL;
4735*4882a593Smuzhiyun 		goto new_owner;
4736*4882a593Smuzhiyun 	}
4737*4882a593Smuzhiyun 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4738*4882a593Smuzhiyun 	if (status)
4739*4882a593Smuzhiyun 		return status;
4740*4882a593Smuzhiyun 	goto alloc_stateid;
4741*4882a593Smuzhiyun new_owner:
4742*4882a593Smuzhiyun 	oo = alloc_init_open_stateowner(strhashval, open, cstate);
4743*4882a593Smuzhiyun 	if (oo == NULL)
4744*4882a593Smuzhiyun 		return nfserr_jukebox;
4745*4882a593Smuzhiyun 	open->op_openowner = oo;
4746*4882a593Smuzhiyun alloc_stateid:
4747*4882a593Smuzhiyun 	open->op_stp = nfs4_alloc_open_stateid(clp);
4748*4882a593Smuzhiyun 	if (!open->op_stp)
4749*4882a593Smuzhiyun 		return nfserr_jukebox;
4750*4882a593Smuzhiyun 
4751*4882a593Smuzhiyun 	if (nfsd4_has_session(cstate) &&
4752*4882a593Smuzhiyun 	    (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4753*4882a593Smuzhiyun 		open->op_odstate = alloc_clnt_odstate(clp);
4754*4882a593Smuzhiyun 		if (!open->op_odstate)
4755*4882a593Smuzhiyun 			return nfserr_jukebox;
4756*4882a593Smuzhiyun 	}
4757*4882a593Smuzhiyun 
4758*4882a593Smuzhiyun 	return nfs_ok;
4759*4882a593Smuzhiyun }
4760*4882a593Smuzhiyun 
4761*4882a593Smuzhiyun static inline __be32
nfs4_check_delegmode(struct nfs4_delegation * dp,int flags)4762*4882a593Smuzhiyun nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4763*4882a593Smuzhiyun {
4764*4882a593Smuzhiyun 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4765*4882a593Smuzhiyun 		return nfserr_openmode;
4766*4882a593Smuzhiyun 	else
4767*4882a593Smuzhiyun 		return nfs_ok;
4768*4882a593Smuzhiyun }
4769*4882a593Smuzhiyun 
share_access_to_flags(u32 share_access)4770*4882a593Smuzhiyun static int share_access_to_flags(u32 share_access)
4771*4882a593Smuzhiyun {
4772*4882a593Smuzhiyun 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
4773*4882a593Smuzhiyun }
4774*4882a593Smuzhiyun 
find_deleg_stateid(struct nfs4_client * cl,stateid_t * s)4775*4882a593Smuzhiyun static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
4776*4882a593Smuzhiyun {
4777*4882a593Smuzhiyun 	struct nfs4_stid *ret;
4778*4882a593Smuzhiyun 
4779*4882a593Smuzhiyun 	ret = find_stateid_by_type(cl, s,
4780*4882a593Smuzhiyun 				NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
4781*4882a593Smuzhiyun 	if (!ret)
4782*4882a593Smuzhiyun 		return NULL;
4783*4882a593Smuzhiyun 	return delegstateid(ret);
4784*4882a593Smuzhiyun }
4785*4882a593Smuzhiyun 
nfsd4_is_deleg_cur(struct nfsd4_open * open)4786*4882a593Smuzhiyun static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4787*4882a593Smuzhiyun {
4788*4882a593Smuzhiyun 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4789*4882a593Smuzhiyun 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4790*4882a593Smuzhiyun }
4791*4882a593Smuzhiyun 
4792*4882a593Smuzhiyun static __be32
nfs4_check_deleg(struct nfs4_client * cl,struct nfsd4_open * open,struct nfs4_delegation ** dp)4793*4882a593Smuzhiyun nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
4794*4882a593Smuzhiyun 		struct nfs4_delegation **dp)
4795*4882a593Smuzhiyun {
4796*4882a593Smuzhiyun 	int flags;
4797*4882a593Smuzhiyun 	__be32 status = nfserr_bad_stateid;
4798*4882a593Smuzhiyun 	struct nfs4_delegation *deleg;
4799*4882a593Smuzhiyun 
4800*4882a593Smuzhiyun 	deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4801*4882a593Smuzhiyun 	if (deleg == NULL)
4802*4882a593Smuzhiyun 		goto out;
4803*4882a593Smuzhiyun 	if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4804*4882a593Smuzhiyun 		nfs4_put_stid(&deleg->dl_stid);
4805*4882a593Smuzhiyun 		if (cl->cl_minorversion)
4806*4882a593Smuzhiyun 			status = nfserr_deleg_revoked;
4807*4882a593Smuzhiyun 		goto out;
4808*4882a593Smuzhiyun 	}
4809*4882a593Smuzhiyun 	flags = share_access_to_flags(open->op_share_access);
4810*4882a593Smuzhiyun 	status = nfs4_check_delegmode(deleg, flags);
4811*4882a593Smuzhiyun 	if (status) {
4812*4882a593Smuzhiyun 		nfs4_put_stid(&deleg->dl_stid);
4813*4882a593Smuzhiyun 		goto out;
4814*4882a593Smuzhiyun 	}
4815*4882a593Smuzhiyun 	*dp = deleg;
4816*4882a593Smuzhiyun out:
4817*4882a593Smuzhiyun 	if (!nfsd4_is_deleg_cur(open))
4818*4882a593Smuzhiyun 		return nfs_ok;
4819*4882a593Smuzhiyun 	if (status)
4820*4882a593Smuzhiyun 		return status;
4821*4882a593Smuzhiyun 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
4822*4882a593Smuzhiyun 	return nfs_ok;
4823*4882a593Smuzhiyun }
4824*4882a593Smuzhiyun 
nfs4_access_to_access(u32 nfs4_access)4825*4882a593Smuzhiyun static inline int nfs4_access_to_access(u32 nfs4_access)
4826*4882a593Smuzhiyun {
4827*4882a593Smuzhiyun 	int flags = 0;
4828*4882a593Smuzhiyun 
4829*4882a593Smuzhiyun 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4830*4882a593Smuzhiyun 		flags |= NFSD_MAY_READ;
4831*4882a593Smuzhiyun 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4832*4882a593Smuzhiyun 		flags |= NFSD_MAY_WRITE;
4833*4882a593Smuzhiyun 	return flags;
4834*4882a593Smuzhiyun }
4835*4882a593Smuzhiyun 
4836*4882a593Smuzhiyun static inline __be32
nfsd4_truncate(struct svc_rqst * rqstp,struct svc_fh * fh,struct nfsd4_open * open)4837*4882a593Smuzhiyun nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4838*4882a593Smuzhiyun 		struct nfsd4_open *open)
4839*4882a593Smuzhiyun {
4840*4882a593Smuzhiyun 	struct iattr iattr = {
4841*4882a593Smuzhiyun 		.ia_valid = ATTR_SIZE,
4842*4882a593Smuzhiyun 		.ia_size = 0,
4843*4882a593Smuzhiyun 	};
4844*4882a593Smuzhiyun 	if (!open->op_truncate)
4845*4882a593Smuzhiyun 		return 0;
4846*4882a593Smuzhiyun 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4847*4882a593Smuzhiyun 		return nfserr_inval;
4848*4882a593Smuzhiyun 	return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
4849*4882a593Smuzhiyun }
4850*4882a593Smuzhiyun 
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)4851*4882a593Smuzhiyun static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
4852*4882a593Smuzhiyun 		struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4853*4882a593Smuzhiyun 		struct nfsd4_open *open)
4854*4882a593Smuzhiyun {
4855*4882a593Smuzhiyun 	struct nfsd_file *nf = NULL;
4856*4882a593Smuzhiyun 	__be32 status;
4857*4882a593Smuzhiyun 	int oflag = nfs4_access_to_omode(open->op_share_access);
4858*4882a593Smuzhiyun 	int access = nfs4_access_to_access(open->op_share_access);
4859*4882a593Smuzhiyun 	unsigned char old_access_bmap, old_deny_bmap;
4860*4882a593Smuzhiyun 
4861*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
4862*4882a593Smuzhiyun 
4863*4882a593Smuzhiyun 	/*
4864*4882a593Smuzhiyun 	 * Are we trying to set a deny mode that would conflict with
4865*4882a593Smuzhiyun 	 * current access?
4866*4882a593Smuzhiyun 	 */
4867*4882a593Smuzhiyun 	status = nfs4_file_check_deny(fp, open->op_share_deny);
4868*4882a593Smuzhiyun 	if (status != nfs_ok) {
4869*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
4870*4882a593Smuzhiyun 		goto out;
4871*4882a593Smuzhiyun 	}
4872*4882a593Smuzhiyun 
4873*4882a593Smuzhiyun 	/* set access to the file */
4874*4882a593Smuzhiyun 	status = nfs4_file_get_access(fp, open->op_share_access);
4875*4882a593Smuzhiyun 	if (status != nfs_ok) {
4876*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
4877*4882a593Smuzhiyun 		goto out;
4878*4882a593Smuzhiyun 	}
4879*4882a593Smuzhiyun 
4880*4882a593Smuzhiyun 	/* Set access bits in stateid */
4881*4882a593Smuzhiyun 	old_access_bmap = stp->st_access_bmap;
4882*4882a593Smuzhiyun 	set_access(open->op_share_access, stp);
4883*4882a593Smuzhiyun 
4884*4882a593Smuzhiyun 	/* Set new deny mask */
4885*4882a593Smuzhiyun 	old_deny_bmap = stp->st_deny_bmap;
4886*4882a593Smuzhiyun 	set_deny(open->op_share_deny, stp);
4887*4882a593Smuzhiyun 	fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4888*4882a593Smuzhiyun 
4889*4882a593Smuzhiyun 	if (!fp->fi_fds[oflag]) {
4890*4882a593Smuzhiyun 		spin_unlock(&fp->fi_lock);
4891*4882a593Smuzhiyun 		status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
4892*4882a593Smuzhiyun 		if (status)
4893*4882a593Smuzhiyun 			goto out_put_access;
4894*4882a593Smuzhiyun 		spin_lock(&fp->fi_lock);
4895*4882a593Smuzhiyun 		if (!fp->fi_fds[oflag]) {
4896*4882a593Smuzhiyun 			fp->fi_fds[oflag] = nf;
4897*4882a593Smuzhiyun 			nf = NULL;
4898*4882a593Smuzhiyun 		}
4899*4882a593Smuzhiyun 	}
4900*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
4901*4882a593Smuzhiyun 	if (nf)
4902*4882a593Smuzhiyun 		nfsd_file_put(nf);
4903*4882a593Smuzhiyun 
4904*4882a593Smuzhiyun 	status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
4905*4882a593Smuzhiyun 								access));
4906*4882a593Smuzhiyun 	if (status)
4907*4882a593Smuzhiyun 		goto out_put_access;
4908*4882a593Smuzhiyun 
4909*4882a593Smuzhiyun 	status = nfsd4_truncate(rqstp, cur_fh, open);
4910*4882a593Smuzhiyun 	if (status)
4911*4882a593Smuzhiyun 		goto out_put_access;
4912*4882a593Smuzhiyun out:
4913*4882a593Smuzhiyun 	return status;
4914*4882a593Smuzhiyun out_put_access:
4915*4882a593Smuzhiyun 	stp->st_access_bmap = old_access_bmap;
4916*4882a593Smuzhiyun 	nfs4_file_put_access(fp, open->op_share_access);
4917*4882a593Smuzhiyun 	reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4918*4882a593Smuzhiyun 	goto out;
4919*4882a593Smuzhiyun }
4920*4882a593Smuzhiyun 
4921*4882a593Smuzhiyun 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)4922*4882a593Smuzhiyun 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)
4923*4882a593Smuzhiyun {
4924*4882a593Smuzhiyun 	__be32 status;
4925*4882a593Smuzhiyun 	unsigned char old_deny_bmap = stp->st_deny_bmap;
4926*4882a593Smuzhiyun 
4927*4882a593Smuzhiyun 	if (!test_access(open->op_share_access, stp))
4928*4882a593Smuzhiyun 		return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
4929*4882a593Smuzhiyun 
4930*4882a593Smuzhiyun 	/* test and set deny mode */
4931*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
4932*4882a593Smuzhiyun 	status = nfs4_file_check_deny(fp, open->op_share_deny);
4933*4882a593Smuzhiyun 	if (status == nfs_ok) {
4934*4882a593Smuzhiyun 		set_deny(open->op_share_deny, stp);
4935*4882a593Smuzhiyun 		fp->fi_share_deny |=
4936*4882a593Smuzhiyun 				(open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4937*4882a593Smuzhiyun 	}
4938*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
4939*4882a593Smuzhiyun 
4940*4882a593Smuzhiyun 	if (status != nfs_ok)
4941*4882a593Smuzhiyun 		return status;
4942*4882a593Smuzhiyun 
4943*4882a593Smuzhiyun 	status = nfsd4_truncate(rqstp, cur_fh, open);
4944*4882a593Smuzhiyun 	if (status != nfs_ok)
4945*4882a593Smuzhiyun 		reset_union_bmap_deny(old_deny_bmap, stp);
4946*4882a593Smuzhiyun 	return status;
4947*4882a593Smuzhiyun }
4948*4882a593Smuzhiyun 
4949*4882a593Smuzhiyun /* Should we give out recallable state?: */
nfsd4_cb_channel_good(struct nfs4_client * clp)4950*4882a593Smuzhiyun static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4951*4882a593Smuzhiyun {
4952*4882a593Smuzhiyun 	if (clp->cl_cb_state == NFSD4_CB_UP)
4953*4882a593Smuzhiyun 		return true;
4954*4882a593Smuzhiyun 	/*
4955*4882a593Smuzhiyun 	 * In the sessions case, since we don't have to establish a
4956*4882a593Smuzhiyun 	 * separate connection for callbacks, we assume it's OK
4957*4882a593Smuzhiyun 	 * until we hear otherwise:
4958*4882a593Smuzhiyun 	 */
4959*4882a593Smuzhiyun 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4960*4882a593Smuzhiyun }
4961*4882a593Smuzhiyun 
nfs4_alloc_init_lease(struct nfs4_delegation * dp,int flag)4962*4882a593Smuzhiyun static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4963*4882a593Smuzhiyun 						int flag)
4964*4882a593Smuzhiyun {
4965*4882a593Smuzhiyun 	struct file_lock *fl;
4966*4882a593Smuzhiyun 
4967*4882a593Smuzhiyun 	fl = locks_alloc_lock();
4968*4882a593Smuzhiyun 	if (!fl)
4969*4882a593Smuzhiyun 		return NULL;
4970*4882a593Smuzhiyun 	fl->fl_lmops = &nfsd_lease_mng_ops;
4971*4882a593Smuzhiyun 	fl->fl_flags = FL_DELEG;
4972*4882a593Smuzhiyun 	fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4973*4882a593Smuzhiyun 	fl->fl_end = OFFSET_MAX;
4974*4882a593Smuzhiyun 	fl->fl_owner = (fl_owner_t)dp;
4975*4882a593Smuzhiyun 	fl->fl_pid = current->tgid;
4976*4882a593Smuzhiyun 	fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
4977*4882a593Smuzhiyun 	return fl;
4978*4882a593Smuzhiyun }
4979*4882a593Smuzhiyun 
4980*4882a593Smuzhiyun static struct nfs4_delegation *
nfs4_set_delegation(struct nfs4_client * clp,struct svc_fh * fh,struct nfs4_file * fp,struct nfs4_clnt_odstate * odstate)4981*4882a593Smuzhiyun nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
4982*4882a593Smuzhiyun 		    struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
4983*4882a593Smuzhiyun {
4984*4882a593Smuzhiyun 	int status = 0;
4985*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
4986*4882a593Smuzhiyun 	struct nfsd_file *nf;
4987*4882a593Smuzhiyun 	struct file_lock *fl;
4988*4882a593Smuzhiyun 
4989*4882a593Smuzhiyun 	/*
4990*4882a593Smuzhiyun 	 * The fi_had_conflict and nfs_get_existing_delegation checks
4991*4882a593Smuzhiyun 	 * here are just optimizations; we'll need to recheck them at
4992*4882a593Smuzhiyun 	 * the end:
4993*4882a593Smuzhiyun 	 */
4994*4882a593Smuzhiyun 	if (fp->fi_had_conflict)
4995*4882a593Smuzhiyun 		return ERR_PTR(-EAGAIN);
4996*4882a593Smuzhiyun 
4997*4882a593Smuzhiyun 	nf = find_readable_file(fp);
4998*4882a593Smuzhiyun 	if (!nf) {
4999*4882a593Smuzhiyun 		/* We should always have a readable file here */
5000*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
5001*4882a593Smuzhiyun 		return ERR_PTR(-EBADF);
5002*4882a593Smuzhiyun 	}
5003*4882a593Smuzhiyun 	spin_lock(&state_lock);
5004*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
5005*4882a593Smuzhiyun 	if (nfs4_delegation_exists(clp, fp))
5006*4882a593Smuzhiyun 		status = -EAGAIN;
5007*4882a593Smuzhiyun 	else if (!fp->fi_deleg_file) {
5008*4882a593Smuzhiyun 		fp->fi_deleg_file = nf;
5009*4882a593Smuzhiyun 		/* increment early to prevent fi_deleg_file from being
5010*4882a593Smuzhiyun 		 * cleared */
5011*4882a593Smuzhiyun 		fp->fi_delegees = 1;
5012*4882a593Smuzhiyun 		nf = NULL;
5013*4882a593Smuzhiyun 	} else
5014*4882a593Smuzhiyun 		fp->fi_delegees++;
5015*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
5016*4882a593Smuzhiyun 	spin_unlock(&state_lock);
5017*4882a593Smuzhiyun 	if (nf)
5018*4882a593Smuzhiyun 		nfsd_file_put(nf);
5019*4882a593Smuzhiyun 	if (status)
5020*4882a593Smuzhiyun 		return ERR_PTR(status);
5021*4882a593Smuzhiyun 
5022*4882a593Smuzhiyun 	status = -ENOMEM;
5023*4882a593Smuzhiyun 	dp = alloc_init_deleg(clp, fp, fh, odstate);
5024*4882a593Smuzhiyun 	if (!dp)
5025*4882a593Smuzhiyun 		goto out_delegees;
5026*4882a593Smuzhiyun 
5027*4882a593Smuzhiyun 	fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5028*4882a593Smuzhiyun 	if (!fl)
5029*4882a593Smuzhiyun 		goto out_clnt_odstate;
5030*4882a593Smuzhiyun 
5031*4882a593Smuzhiyun 	status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
5032*4882a593Smuzhiyun 	if (fl)
5033*4882a593Smuzhiyun 		locks_free_lock(fl);
5034*4882a593Smuzhiyun 	if (status)
5035*4882a593Smuzhiyun 		goto out_clnt_odstate;
5036*4882a593Smuzhiyun 
5037*4882a593Smuzhiyun 	spin_lock(&state_lock);
5038*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
5039*4882a593Smuzhiyun 	if (fp->fi_had_conflict)
5040*4882a593Smuzhiyun 		status = -EAGAIN;
5041*4882a593Smuzhiyun 	else
5042*4882a593Smuzhiyun 		status = hash_delegation_locked(dp, fp);
5043*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
5044*4882a593Smuzhiyun 	spin_unlock(&state_lock);
5045*4882a593Smuzhiyun 
5046*4882a593Smuzhiyun 	if (status)
5047*4882a593Smuzhiyun 		goto out_unlock;
5048*4882a593Smuzhiyun 
5049*4882a593Smuzhiyun 	return dp;
5050*4882a593Smuzhiyun out_unlock:
5051*4882a593Smuzhiyun 	vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
5052*4882a593Smuzhiyun out_clnt_odstate:
5053*4882a593Smuzhiyun 	put_clnt_odstate(dp->dl_clnt_odstate);
5054*4882a593Smuzhiyun 	nfs4_put_stid(&dp->dl_stid);
5055*4882a593Smuzhiyun out_delegees:
5056*4882a593Smuzhiyun 	put_deleg_file(fp);
5057*4882a593Smuzhiyun 	return ERR_PTR(status);
5058*4882a593Smuzhiyun }
5059*4882a593Smuzhiyun 
nfsd4_open_deleg_none_ext(struct nfsd4_open * open,int status)5060*4882a593Smuzhiyun static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5061*4882a593Smuzhiyun {
5062*4882a593Smuzhiyun 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5063*4882a593Smuzhiyun 	if (status == -EAGAIN)
5064*4882a593Smuzhiyun 		open->op_why_no_deleg = WND4_CONTENTION;
5065*4882a593Smuzhiyun 	else {
5066*4882a593Smuzhiyun 		open->op_why_no_deleg = WND4_RESOURCE;
5067*4882a593Smuzhiyun 		switch (open->op_deleg_want) {
5068*4882a593Smuzhiyun 		case NFS4_SHARE_WANT_READ_DELEG:
5069*4882a593Smuzhiyun 		case NFS4_SHARE_WANT_WRITE_DELEG:
5070*4882a593Smuzhiyun 		case NFS4_SHARE_WANT_ANY_DELEG:
5071*4882a593Smuzhiyun 			break;
5072*4882a593Smuzhiyun 		case NFS4_SHARE_WANT_CANCEL:
5073*4882a593Smuzhiyun 			open->op_why_no_deleg = WND4_CANCELLED;
5074*4882a593Smuzhiyun 			break;
5075*4882a593Smuzhiyun 		case NFS4_SHARE_WANT_NO_DELEG:
5076*4882a593Smuzhiyun 			WARN_ON_ONCE(1);
5077*4882a593Smuzhiyun 		}
5078*4882a593Smuzhiyun 	}
5079*4882a593Smuzhiyun }
5080*4882a593Smuzhiyun 
5081*4882a593Smuzhiyun /*
5082*4882a593Smuzhiyun  * Attempt to hand out a delegation.
5083*4882a593Smuzhiyun  *
5084*4882a593Smuzhiyun  * Note we don't support write delegations, and won't until the vfs has
5085*4882a593Smuzhiyun  * proper support for them.
5086*4882a593Smuzhiyun  */
5087*4882a593Smuzhiyun static void
nfs4_open_delegation(struct svc_fh * fh,struct nfsd4_open * open,struct nfs4_ol_stateid * stp)5088*4882a593Smuzhiyun nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5089*4882a593Smuzhiyun 			struct nfs4_ol_stateid *stp)
5090*4882a593Smuzhiyun {
5091*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
5092*4882a593Smuzhiyun 	struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5093*4882a593Smuzhiyun 	struct nfs4_client *clp = stp->st_stid.sc_client;
5094*4882a593Smuzhiyun 	int cb_up;
5095*4882a593Smuzhiyun 	int status = 0;
5096*4882a593Smuzhiyun 
5097*4882a593Smuzhiyun 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
5098*4882a593Smuzhiyun 	open->op_recall = 0;
5099*4882a593Smuzhiyun 	switch (open->op_claim_type) {
5100*4882a593Smuzhiyun 		case NFS4_OPEN_CLAIM_PREVIOUS:
5101*4882a593Smuzhiyun 			if (!cb_up)
5102*4882a593Smuzhiyun 				open->op_recall = 1;
5103*4882a593Smuzhiyun 			if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5104*4882a593Smuzhiyun 				goto out_no_deleg;
5105*4882a593Smuzhiyun 			break;
5106*4882a593Smuzhiyun 		case NFS4_OPEN_CLAIM_NULL:
5107*4882a593Smuzhiyun 		case NFS4_OPEN_CLAIM_FH:
5108*4882a593Smuzhiyun 			/*
5109*4882a593Smuzhiyun 			 * Let's not give out any delegations till everyone's
5110*4882a593Smuzhiyun 			 * had the chance to reclaim theirs, *and* until
5111*4882a593Smuzhiyun 			 * NLM locks have all been reclaimed:
5112*4882a593Smuzhiyun 			 */
5113*4882a593Smuzhiyun 			if (locks_in_grace(clp->net))
5114*4882a593Smuzhiyun 				goto out_no_deleg;
5115*4882a593Smuzhiyun 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
5116*4882a593Smuzhiyun 				goto out_no_deleg;
5117*4882a593Smuzhiyun 			/*
5118*4882a593Smuzhiyun 			 * Also, if the file was opened for write or
5119*4882a593Smuzhiyun 			 * create, there's a good chance the client's
5120*4882a593Smuzhiyun 			 * about to write to it, resulting in an
5121*4882a593Smuzhiyun 			 * immediate recall (since we don't support
5122*4882a593Smuzhiyun 			 * write delegations):
5123*4882a593Smuzhiyun 			 */
5124*4882a593Smuzhiyun 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
5125*4882a593Smuzhiyun 				goto out_no_deleg;
5126*4882a593Smuzhiyun 			if (open->op_create == NFS4_OPEN_CREATE)
5127*4882a593Smuzhiyun 				goto out_no_deleg;
5128*4882a593Smuzhiyun 			break;
5129*4882a593Smuzhiyun 		default:
5130*4882a593Smuzhiyun 			goto out_no_deleg;
5131*4882a593Smuzhiyun 	}
5132*4882a593Smuzhiyun 	dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
5133*4882a593Smuzhiyun 	if (IS_ERR(dp))
5134*4882a593Smuzhiyun 		goto out_no_deleg;
5135*4882a593Smuzhiyun 
5136*4882a593Smuzhiyun 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
5137*4882a593Smuzhiyun 
5138*4882a593Smuzhiyun 	trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
5139*4882a593Smuzhiyun 	open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
5140*4882a593Smuzhiyun 	nfs4_put_stid(&dp->dl_stid);
5141*4882a593Smuzhiyun 	return;
5142*4882a593Smuzhiyun out_no_deleg:
5143*4882a593Smuzhiyun 	open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5144*4882a593Smuzhiyun 	if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
5145*4882a593Smuzhiyun 	    open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
5146*4882a593Smuzhiyun 		dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5147*4882a593Smuzhiyun 		open->op_recall = 1;
5148*4882a593Smuzhiyun 	}
5149*4882a593Smuzhiyun 
5150*4882a593Smuzhiyun 	/* 4.1 client asking for a delegation? */
5151*4882a593Smuzhiyun 	if (open->op_deleg_want)
5152*4882a593Smuzhiyun 		nfsd4_open_deleg_none_ext(open, status);
5153*4882a593Smuzhiyun 	return;
5154*4882a593Smuzhiyun }
5155*4882a593Smuzhiyun 
nfsd4_deleg_xgrade_none_ext(struct nfsd4_open * open,struct nfs4_delegation * dp)5156*4882a593Smuzhiyun static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5157*4882a593Smuzhiyun 					struct nfs4_delegation *dp)
5158*4882a593Smuzhiyun {
5159*4882a593Smuzhiyun 	if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5160*4882a593Smuzhiyun 	    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5161*4882a593Smuzhiyun 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5162*4882a593Smuzhiyun 		open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5163*4882a593Smuzhiyun 	} else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5164*4882a593Smuzhiyun 		   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5165*4882a593Smuzhiyun 		open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5166*4882a593Smuzhiyun 		open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5167*4882a593Smuzhiyun 	}
5168*4882a593Smuzhiyun 	/* Otherwise the client must be confused wanting a delegation
5169*4882a593Smuzhiyun 	 * it already has, therefore we don't return
5170*4882a593Smuzhiyun 	 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5171*4882a593Smuzhiyun 	 */
5172*4882a593Smuzhiyun }
5173*4882a593Smuzhiyun 
5174*4882a593Smuzhiyun __be32
nfsd4_process_open2(struct svc_rqst * rqstp,struct svc_fh * current_fh,struct nfsd4_open * open)5175*4882a593Smuzhiyun nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5176*4882a593Smuzhiyun {
5177*4882a593Smuzhiyun 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
5178*4882a593Smuzhiyun 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
5179*4882a593Smuzhiyun 	struct nfs4_file *fp = NULL;
5180*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp = NULL;
5181*4882a593Smuzhiyun 	struct nfs4_delegation *dp = NULL;
5182*4882a593Smuzhiyun 	__be32 status;
5183*4882a593Smuzhiyun 	bool new_stp = false;
5184*4882a593Smuzhiyun 
5185*4882a593Smuzhiyun 	/*
5186*4882a593Smuzhiyun 	 * Lookup file; if found, lookup stateid and check open request,
5187*4882a593Smuzhiyun 	 * and check for delegations in the process of being recalled.
5188*4882a593Smuzhiyun 	 * If not found, create the nfs4_file struct
5189*4882a593Smuzhiyun 	 */
5190*4882a593Smuzhiyun 	fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
5191*4882a593Smuzhiyun 	if (fp != open->op_file) {
5192*4882a593Smuzhiyun 		status = nfs4_check_deleg(cl, open, &dp);
5193*4882a593Smuzhiyun 		if (status)
5194*4882a593Smuzhiyun 			goto out;
5195*4882a593Smuzhiyun 		stp = nfsd4_find_and_lock_existing_open(fp, open);
5196*4882a593Smuzhiyun 	} else {
5197*4882a593Smuzhiyun 		open->op_file = NULL;
5198*4882a593Smuzhiyun 		status = nfserr_bad_stateid;
5199*4882a593Smuzhiyun 		if (nfsd4_is_deleg_cur(open))
5200*4882a593Smuzhiyun 			goto out;
5201*4882a593Smuzhiyun 	}
5202*4882a593Smuzhiyun 
5203*4882a593Smuzhiyun 	if (!stp) {
5204*4882a593Smuzhiyun 		stp = init_open_stateid(fp, open);
5205*4882a593Smuzhiyun 		if (!open->op_stp)
5206*4882a593Smuzhiyun 			new_stp = true;
5207*4882a593Smuzhiyun 	}
5208*4882a593Smuzhiyun 
5209*4882a593Smuzhiyun 	/*
5210*4882a593Smuzhiyun 	 * OPEN the file, or upgrade an existing OPEN.
5211*4882a593Smuzhiyun 	 * If truncate fails, the OPEN fails.
5212*4882a593Smuzhiyun 	 *
5213*4882a593Smuzhiyun 	 * stp is already locked.
5214*4882a593Smuzhiyun 	 */
5215*4882a593Smuzhiyun 	if (!new_stp) {
5216*4882a593Smuzhiyun 		/* Stateid was found, this is an OPEN upgrade */
5217*4882a593Smuzhiyun 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
5218*4882a593Smuzhiyun 		if (status) {
5219*4882a593Smuzhiyun 			mutex_unlock(&stp->st_mutex);
5220*4882a593Smuzhiyun 			goto out;
5221*4882a593Smuzhiyun 		}
5222*4882a593Smuzhiyun 	} else {
5223*4882a593Smuzhiyun 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5224*4882a593Smuzhiyun 		if (status) {
5225*4882a593Smuzhiyun 			stp->st_stid.sc_type = NFS4_CLOSED_STID;
5226*4882a593Smuzhiyun 			release_open_stateid(stp);
5227*4882a593Smuzhiyun 			mutex_unlock(&stp->st_mutex);
5228*4882a593Smuzhiyun 			goto out;
5229*4882a593Smuzhiyun 		}
5230*4882a593Smuzhiyun 
5231*4882a593Smuzhiyun 		stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5232*4882a593Smuzhiyun 							open->op_odstate);
5233*4882a593Smuzhiyun 		if (stp->st_clnt_odstate == open->op_odstate)
5234*4882a593Smuzhiyun 			open->op_odstate = NULL;
5235*4882a593Smuzhiyun 	}
5236*4882a593Smuzhiyun 
5237*4882a593Smuzhiyun 	nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
5238*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
5239*4882a593Smuzhiyun 
5240*4882a593Smuzhiyun 	if (nfsd4_has_session(&resp->cstate)) {
5241*4882a593Smuzhiyun 		if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5242*4882a593Smuzhiyun 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5243*4882a593Smuzhiyun 			open->op_why_no_deleg = WND4_NOT_WANTED;
5244*4882a593Smuzhiyun 			goto nodeleg;
5245*4882a593Smuzhiyun 		}
5246*4882a593Smuzhiyun 	}
5247*4882a593Smuzhiyun 
5248*4882a593Smuzhiyun 	/*
5249*4882a593Smuzhiyun 	* Attempt to hand out a delegation. No error return, because the
5250*4882a593Smuzhiyun 	* OPEN succeeds even if we fail.
5251*4882a593Smuzhiyun 	*/
5252*4882a593Smuzhiyun 	nfs4_open_delegation(current_fh, open, stp);
5253*4882a593Smuzhiyun nodeleg:
5254*4882a593Smuzhiyun 	status = nfs_ok;
5255*4882a593Smuzhiyun 	trace_nfsd_open(&stp->st_stid.sc_stateid);
5256*4882a593Smuzhiyun out:
5257*4882a593Smuzhiyun 	/* 4.1 client trying to upgrade/downgrade delegation? */
5258*4882a593Smuzhiyun 	if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
5259*4882a593Smuzhiyun 	    open->op_deleg_want)
5260*4882a593Smuzhiyun 		nfsd4_deleg_xgrade_none_ext(open, dp);
5261*4882a593Smuzhiyun 
5262*4882a593Smuzhiyun 	if (fp)
5263*4882a593Smuzhiyun 		put_nfs4_file(fp);
5264*4882a593Smuzhiyun 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
5265*4882a593Smuzhiyun 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
5266*4882a593Smuzhiyun 	/*
5267*4882a593Smuzhiyun 	* To finish the open response, we just need to set the rflags.
5268*4882a593Smuzhiyun 	*/
5269*4882a593Smuzhiyun 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
5270*4882a593Smuzhiyun 	if (nfsd4_has_session(&resp->cstate))
5271*4882a593Smuzhiyun 		open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5272*4882a593Smuzhiyun 	else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
5273*4882a593Smuzhiyun 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
5274*4882a593Smuzhiyun 
5275*4882a593Smuzhiyun 	if (dp)
5276*4882a593Smuzhiyun 		nfs4_put_stid(&dp->dl_stid);
5277*4882a593Smuzhiyun 	if (stp)
5278*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
5279*4882a593Smuzhiyun 
5280*4882a593Smuzhiyun 	return status;
5281*4882a593Smuzhiyun }
5282*4882a593Smuzhiyun 
nfsd4_cleanup_open_state(struct nfsd4_compound_state * cstate,struct nfsd4_open * open)5283*4882a593Smuzhiyun void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
5284*4882a593Smuzhiyun 			      struct nfsd4_open *open)
5285*4882a593Smuzhiyun {
5286*4882a593Smuzhiyun 	if (open->op_openowner) {
5287*4882a593Smuzhiyun 		struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5288*4882a593Smuzhiyun 
5289*4882a593Smuzhiyun 		nfsd4_cstate_assign_replay(cstate, so);
5290*4882a593Smuzhiyun 		nfs4_put_stateowner(so);
5291*4882a593Smuzhiyun 	}
5292*4882a593Smuzhiyun 	if (open->op_file)
5293*4882a593Smuzhiyun 		kmem_cache_free(file_slab, open->op_file);
5294*4882a593Smuzhiyun 	if (open->op_stp)
5295*4882a593Smuzhiyun 		nfs4_put_stid(&open->op_stp->st_stid);
5296*4882a593Smuzhiyun 	if (open->op_odstate)
5297*4882a593Smuzhiyun 		kmem_cache_free(odstate_slab, open->op_odstate);
5298*4882a593Smuzhiyun }
5299*4882a593Smuzhiyun 
5300*4882a593Smuzhiyun __be32
nfsd4_renew(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)5301*4882a593Smuzhiyun nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5302*4882a593Smuzhiyun 	    union nfsd4_op_u *u)
5303*4882a593Smuzhiyun {
5304*4882a593Smuzhiyun 	clientid_t *clid = &u->renew;
5305*4882a593Smuzhiyun 	struct nfs4_client *clp;
5306*4882a593Smuzhiyun 	__be32 status;
5307*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5308*4882a593Smuzhiyun 
5309*4882a593Smuzhiyun 	trace_nfsd_clid_renew(clid);
5310*4882a593Smuzhiyun 	status = lookup_clientid(clid, cstate, nn, false);
5311*4882a593Smuzhiyun 	if (status)
5312*4882a593Smuzhiyun 		goto out;
5313*4882a593Smuzhiyun 	clp = cstate->clp;
5314*4882a593Smuzhiyun 	status = nfserr_cb_path_down;
5315*4882a593Smuzhiyun 	if (!list_empty(&clp->cl_delegations)
5316*4882a593Smuzhiyun 			&& clp->cl_cb_state != NFSD4_CB_UP)
5317*4882a593Smuzhiyun 		goto out;
5318*4882a593Smuzhiyun 	status = nfs_ok;
5319*4882a593Smuzhiyun out:
5320*4882a593Smuzhiyun 	return status;
5321*4882a593Smuzhiyun }
5322*4882a593Smuzhiyun 
5323*4882a593Smuzhiyun void
nfsd4_end_grace(struct nfsd_net * nn)5324*4882a593Smuzhiyun nfsd4_end_grace(struct nfsd_net *nn)
5325*4882a593Smuzhiyun {
5326*4882a593Smuzhiyun 	/* do nothing if grace period already ended */
5327*4882a593Smuzhiyun 	if (nn->grace_ended)
5328*4882a593Smuzhiyun 		return;
5329*4882a593Smuzhiyun 
5330*4882a593Smuzhiyun 	trace_nfsd_grace_complete(nn);
5331*4882a593Smuzhiyun 	nn->grace_ended = true;
5332*4882a593Smuzhiyun 	/*
5333*4882a593Smuzhiyun 	 * If the server goes down again right now, an NFSv4
5334*4882a593Smuzhiyun 	 * client will still be allowed to reclaim after it comes back up,
5335*4882a593Smuzhiyun 	 * even if it hasn't yet had a chance to reclaim state this time.
5336*4882a593Smuzhiyun 	 *
5337*4882a593Smuzhiyun 	 */
5338*4882a593Smuzhiyun 	nfsd4_record_grace_done(nn);
5339*4882a593Smuzhiyun 	/*
5340*4882a593Smuzhiyun 	 * At this point, NFSv4 clients can still reclaim.  But if the
5341*4882a593Smuzhiyun 	 * server crashes, any that have not yet reclaimed will be out
5342*4882a593Smuzhiyun 	 * of luck on the next boot.
5343*4882a593Smuzhiyun 	 *
5344*4882a593Smuzhiyun 	 * (NFSv4.1+ clients are considered to have reclaimed once they
5345*4882a593Smuzhiyun 	 * call RECLAIM_COMPLETE.  NFSv4.0 clients are considered to
5346*4882a593Smuzhiyun 	 * have reclaimed after their first OPEN.)
5347*4882a593Smuzhiyun 	 */
5348*4882a593Smuzhiyun 	locks_end_grace(&nn->nfsd4_manager);
5349*4882a593Smuzhiyun 	/*
5350*4882a593Smuzhiyun 	 * At this point, and once lockd and/or any other containers
5351*4882a593Smuzhiyun 	 * exit their grace period, further reclaims will fail and
5352*4882a593Smuzhiyun 	 * regular locking can resume.
5353*4882a593Smuzhiyun 	 */
5354*4882a593Smuzhiyun }
5355*4882a593Smuzhiyun 
5356*4882a593Smuzhiyun /*
5357*4882a593Smuzhiyun  * If we've waited a lease period but there are still clients trying to
5358*4882a593Smuzhiyun  * reclaim, wait a little longer to give them a chance to finish.
5359*4882a593Smuzhiyun  */
clients_still_reclaiming(struct nfsd_net * nn)5360*4882a593Smuzhiyun static bool clients_still_reclaiming(struct nfsd_net *nn)
5361*4882a593Smuzhiyun {
5362*4882a593Smuzhiyun 	time64_t double_grace_period_end = nn->boot_time +
5363*4882a593Smuzhiyun 					   2 * nn->nfsd4_lease;
5364*4882a593Smuzhiyun 
5365*4882a593Smuzhiyun 	if (nn->track_reclaim_completes &&
5366*4882a593Smuzhiyun 			atomic_read(&nn->nr_reclaim_complete) ==
5367*4882a593Smuzhiyun 			nn->reclaim_str_hashtbl_size)
5368*4882a593Smuzhiyun 		return false;
5369*4882a593Smuzhiyun 	if (!nn->somebody_reclaimed)
5370*4882a593Smuzhiyun 		return false;
5371*4882a593Smuzhiyun 	nn->somebody_reclaimed = false;
5372*4882a593Smuzhiyun 	/*
5373*4882a593Smuzhiyun 	 * If we've given them *two* lease times to reclaim, and they're
5374*4882a593Smuzhiyun 	 * still not done, give up:
5375*4882a593Smuzhiyun 	 */
5376*4882a593Smuzhiyun 	if (ktime_get_boottime_seconds() > double_grace_period_end)
5377*4882a593Smuzhiyun 		return false;
5378*4882a593Smuzhiyun 	return true;
5379*4882a593Smuzhiyun }
5380*4882a593Smuzhiyun 
5381*4882a593Smuzhiyun static time64_t
nfs4_laundromat(struct nfsd_net * nn)5382*4882a593Smuzhiyun nfs4_laundromat(struct nfsd_net *nn)
5383*4882a593Smuzhiyun {
5384*4882a593Smuzhiyun 	struct nfs4_client *clp;
5385*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
5386*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
5387*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
5388*4882a593Smuzhiyun 	struct nfsd4_blocked_lock *nbl;
5389*4882a593Smuzhiyun 	struct list_head *pos, *next, reaplist;
5390*4882a593Smuzhiyun 	time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5391*4882a593Smuzhiyun 	time64_t t, new_timeo = nn->nfsd4_lease;
5392*4882a593Smuzhiyun 	struct nfs4_cpntf_state *cps;
5393*4882a593Smuzhiyun 	copy_stateid_t *cps_t;
5394*4882a593Smuzhiyun 	int i;
5395*4882a593Smuzhiyun 
5396*4882a593Smuzhiyun 	if (clients_still_reclaiming(nn)) {
5397*4882a593Smuzhiyun 		new_timeo = 0;
5398*4882a593Smuzhiyun 		goto out;
5399*4882a593Smuzhiyun 	}
5400*4882a593Smuzhiyun 	nfsd4_end_grace(nn);
5401*4882a593Smuzhiyun 	INIT_LIST_HEAD(&reaplist);
5402*4882a593Smuzhiyun 
5403*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
5404*4882a593Smuzhiyun 	idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5405*4882a593Smuzhiyun 		cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5406*4882a593Smuzhiyun 		if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
5407*4882a593Smuzhiyun 				cps->cpntf_time < cutoff)
5408*4882a593Smuzhiyun 			_free_cpntf_state_locked(nn, cps);
5409*4882a593Smuzhiyun 	}
5410*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
5411*4882a593Smuzhiyun 
5412*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
5413*4882a593Smuzhiyun 	list_for_each_safe(pos, next, &nn->client_lru) {
5414*4882a593Smuzhiyun 		clp = list_entry(pos, struct nfs4_client, cl_lru);
5415*4882a593Smuzhiyun 		if (clp->cl_time > cutoff) {
5416*4882a593Smuzhiyun 			t = clp->cl_time - cutoff;
5417*4882a593Smuzhiyun 			new_timeo = min(new_timeo, t);
5418*4882a593Smuzhiyun 			break;
5419*4882a593Smuzhiyun 		}
5420*4882a593Smuzhiyun 		if (mark_client_expired_locked(clp)) {
5421*4882a593Smuzhiyun 			trace_nfsd_clid_expired(&clp->cl_clientid);
5422*4882a593Smuzhiyun 			continue;
5423*4882a593Smuzhiyun 		}
5424*4882a593Smuzhiyun 		list_add(&clp->cl_lru, &reaplist);
5425*4882a593Smuzhiyun 	}
5426*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
5427*4882a593Smuzhiyun 	list_for_each_safe(pos, next, &reaplist) {
5428*4882a593Smuzhiyun 		clp = list_entry(pos, struct nfs4_client, cl_lru);
5429*4882a593Smuzhiyun 		trace_nfsd_clid_purged(&clp->cl_clientid);
5430*4882a593Smuzhiyun 		list_del_init(&clp->cl_lru);
5431*4882a593Smuzhiyun 		expire_client(clp);
5432*4882a593Smuzhiyun 	}
5433*4882a593Smuzhiyun 	spin_lock(&state_lock);
5434*4882a593Smuzhiyun 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
5435*4882a593Smuzhiyun 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
5436*4882a593Smuzhiyun 		if (dp->dl_time > cutoff) {
5437*4882a593Smuzhiyun 			t = dp->dl_time - cutoff;
5438*4882a593Smuzhiyun 			new_timeo = min(new_timeo, t);
5439*4882a593Smuzhiyun 			break;
5440*4882a593Smuzhiyun 		}
5441*4882a593Smuzhiyun 		WARN_ON(!unhash_delegation_locked(dp));
5442*4882a593Smuzhiyun 		list_add(&dp->dl_recall_lru, &reaplist);
5443*4882a593Smuzhiyun 	}
5444*4882a593Smuzhiyun 	spin_unlock(&state_lock);
5445*4882a593Smuzhiyun 	while (!list_empty(&reaplist)) {
5446*4882a593Smuzhiyun 		dp = list_first_entry(&reaplist, struct nfs4_delegation,
5447*4882a593Smuzhiyun 					dl_recall_lru);
5448*4882a593Smuzhiyun 		list_del_init(&dp->dl_recall_lru);
5449*4882a593Smuzhiyun 		revoke_delegation(dp);
5450*4882a593Smuzhiyun 	}
5451*4882a593Smuzhiyun 
5452*4882a593Smuzhiyun 	spin_lock(&nn->client_lock);
5453*4882a593Smuzhiyun 	while (!list_empty(&nn->close_lru)) {
5454*4882a593Smuzhiyun 		oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5455*4882a593Smuzhiyun 					oo_close_lru);
5456*4882a593Smuzhiyun 		if (oo->oo_time > cutoff) {
5457*4882a593Smuzhiyun 			t = oo->oo_time - cutoff;
5458*4882a593Smuzhiyun 			new_timeo = min(new_timeo, t);
5459*4882a593Smuzhiyun 			break;
5460*4882a593Smuzhiyun 		}
5461*4882a593Smuzhiyun 		list_del_init(&oo->oo_close_lru);
5462*4882a593Smuzhiyun 		stp = oo->oo_last_closed_stid;
5463*4882a593Smuzhiyun 		oo->oo_last_closed_stid = NULL;
5464*4882a593Smuzhiyun 		spin_unlock(&nn->client_lock);
5465*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
5466*4882a593Smuzhiyun 		spin_lock(&nn->client_lock);
5467*4882a593Smuzhiyun 	}
5468*4882a593Smuzhiyun 	spin_unlock(&nn->client_lock);
5469*4882a593Smuzhiyun 
5470*4882a593Smuzhiyun 	/*
5471*4882a593Smuzhiyun 	 * It's possible for a client to try and acquire an already held lock
5472*4882a593Smuzhiyun 	 * that is being held for a long time, and then lose interest in it.
5473*4882a593Smuzhiyun 	 * So, we clean out any un-revisited request after a lease period
5474*4882a593Smuzhiyun 	 * under the assumption that the client is no longer interested.
5475*4882a593Smuzhiyun 	 *
5476*4882a593Smuzhiyun 	 * RFC5661, sec. 9.6 states that the client must not rely on getting
5477*4882a593Smuzhiyun 	 * notifications and must continue to poll for locks, even when the
5478*4882a593Smuzhiyun 	 * server supports them. Thus this shouldn't lead to clients blocking
5479*4882a593Smuzhiyun 	 * indefinitely once the lock does become free.
5480*4882a593Smuzhiyun 	 */
5481*4882a593Smuzhiyun 	BUG_ON(!list_empty(&reaplist));
5482*4882a593Smuzhiyun 	spin_lock(&nn->blocked_locks_lock);
5483*4882a593Smuzhiyun 	while (!list_empty(&nn->blocked_locks_lru)) {
5484*4882a593Smuzhiyun 		nbl = list_first_entry(&nn->blocked_locks_lru,
5485*4882a593Smuzhiyun 					struct nfsd4_blocked_lock, nbl_lru);
5486*4882a593Smuzhiyun 		if (nbl->nbl_time > cutoff) {
5487*4882a593Smuzhiyun 			t = nbl->nbl_time - cutoff;
5488*4882a593Smuzhiyun 			new_timeo = min(new_timeo, t);
5489*4882a593Smuzhiyun 			break;
5490*4882a593Smuzhiyun 		}
5491*4882a593Smuzhiyun 		list_move(&nbl->nbl_lru, &reaplist);
5492*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_list);
5493*4882a593Smuzhiyun 	}
5494*4882a593Smuzhiyun 	spin_unlock(&nn->blocked_locks_lock);
5495*4882a593Smuzhiyun 
5496*4882a593Smuzhiyun 	while (!list_empty(&reaplist)) {
5497*4882a593Smuzhiyun 		nbl = list_first_entry(&reaplist,
5498*4882a593Smuzhiyun 					struct nfsd4_blocked_lock, nbl_lru);
5499*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_lru);
5500*4882a593Smuzhiyun 		free_blocked_lock(nbl);
5501*4882a593Smuzhiyun 	}
5502*4882a593Smuzhiyun out:
5503*4882a593Smuzhiyun 	new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
5504*4882a593Smuzhiyun 	return new_timeo;
5505*4882a593Smuzhiyun }
5506*4882a593Smuzhiyun 
5507*4882a593Smuzhiyun static struct workqueue_struct *laundry_wq;
5508*4882a593Smuzhiyun static void laundromat_main(struct work_struct *);
5509*4882a593Smuzhiyun 
5510*4882a593Smuzhiyun static void
laundromat_main(struct work_struct * laundry)5511*4882a593Smuzhiyun laundromat_main(struct work_struct *laundry)
5512*4882a593Smuzhiyun {
5513*4882a593Smuzhiyun 	time64_t t;
5514*4882a593Smuzhiyun 	struct delayed_work *dwork = to_delayed_work(laundry);
5515*4882a593Smuzhiyun 	struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5516*4882a593Smuzhiyun 					   laundromat_work);
5517*4882a593Smuzhiyun 
5518*4882a593Smuzhiyun 	t = nfs4_laundromat(nn);
5519*4882a593Smuzhiyun 	queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
5520*4882a593Smuzhiyun }
5521*4882a593Smuzhiyun 
nfs4_check_fh(struct svc_fh * fhp,struct nfs4_stid * stp)5522*4882a593Smuzhiyun static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
5523*4882a593Smuzhiyun {
5524*4882a593Smuzhiyun 	if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
5525*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5526*4882a593Smuzhiyun 	return nfs_ok;
5527*4882a593Smuzhiyun }
5528*4882a593Smuzhiyun 
5529*4882a593Smuzhiyun static inline int
access_permit_read(struct nfs4_ol_stateid * stp)5530*4882a593Smuzhiyun access_permit_read(struct nfs4_ol_stateid *stp)
5531*4882a593Smuzhiyun {
5532*4882a593Smuzhiyun 	return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5533*4882a593Smuzhiyun 		test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5534*4882a593Smuzhiyun 		test_access(NFS4_SHARE_ACCESS_WRITE, stp);
5535*4882a593Smuzhiyun }
5536*4882a593Smuzhiyun 
5537*4882a593Smuzhiyun static inline int
access_permit_write(struct nfs4_ol_stateid * stp)5538*4882a593Smuzhiyun access_permit_write(struct nfs4_ol_stateid *stp)
5539*4882a593Smuzhiyun {
5540*4882a593Smuzhiyun 	return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5541*4882a593Smuzhiyun 		test_access(NFS4_SHARE_ACCESS_BOTH, stp);
5542*4882a593Smuzhiyun }
5543*4882a593Smuzhiyun 
5544*4882a593Smuzhiyun static
nfs4_check_openmode(struct nfs4_ol_stateid * stp,int flags)5545*4882a593Smuzhiyun __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
5546*4882a593Smuzhiyun {
5547*4882a593Smuzhiyun         __be32 status = nfserr_openmode;
5548*4882a593Smuzhiyun 
5549*4882a593Smuzhiyun 	/* For lock stateid's, we test the parent open, not the lock: */
5550*4882a593Smuzhiyun 	if (stp->st_openstp)
5551*4882a593Smuzhiyun 		stp = stp->st_openstp;
5552*4882a593Smuzhiyun 	if ((flags & WR_STATE) && !access_permit_write(stp))
5553*4882a593Smuzhiyun                 goto out;
5554*4882a593Smuzhiyun 	if ((flags & RD_STATE) && !access_permit_read(stp))
5555*4882a593Smuzhiyun                 goto out;
5556*4882a593Smuzhiyun 	status = nfs_ok;
5557*4882a593Smuzhiyun out:
5558*4882a593Smuzhiyun 	return status;
5559*4882a593Smuzhiyun }
5560*4882a593Smuzhiyun 
5561*4882a593Smuzhiyun static inline __be32
check_special_stateids(struct net * net,svc_fh * current_fh,stateid_t * stateid,int flags)5562*4882a593Smuzhiyun check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
5563*4882a593Smuzhiyun {
5564*4882a593Smuzhiyun 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
5565*4882a593Smuzhiyun 		return nfs_ok;
5566*4882a593Smuzhiyun 	else if (opens_in_grace(net)) {
5567*4882a593Smuzhiyun 		/* Answer in remaining cases depends on existence of
5568*4882a593Smuzhiyun 		 * conflicting state; so we must wait out the grace period. */
5569*4882a593Smuzhiyun 		return nfserr_grace;
5570*4882a593Smuzhiyun 	} else if (flags & WR_STATE)
5571*4882a593Smuzhiyun 		return nfs4_share_conflict(current_fh,
5572*4882a593Smuzhiyun 				NFS4_SHARE_DENY_WRITE);
5573*4882a593Smuzhiyun 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5574*4882a593Smuzhiyun 		return nfs4_share_conflict(current_fh,
5575*4882a593Smuzhiyun 				NFS4_SHARE_DENY_READ);
5576*4882a593Smuzhiyun }
5577*4882a593Smuzhiyun 
5578*4882a593Smuzhiyun /*
5579*4882a593Smuzhiyun  * Allow READ/WRITE during grace period on recovered state only for files
5580*4882a593Smuzhiyun  * that are not able to provide mandatory locking.
5581*4882a593Smuzhiyun  */
5582*4882a593Smuzhiyun static inline int
grace_disallows_io(struct net * net,struct inode * inode)5583*4882a593Smuzhiyun grace_disallows_io(struct net *net, struct inode *inode)
5584*4882a593Smuzhiyun {
5585*4882a593Smuzhiyun 	return opens_in_grace(net) && mandatory_lock(inode);
5586*4882a593Smuzhiyun }
5587*4882a593Smuzhiyun 
check_stateid_generation(stateid_t * in,stateid_t * ref,bool has_session)5588*4882a593Smuzhiyun static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
5589*4882a593Smuzhiyun {
5590*4882a593Smuzhiyun 	/*
5591*4882a593Smuzhiyun 	 * When sessions are used the stateid generation number is ignored
5592*4882a593Smuzhiyun 	 * when it is zero.
5593*4882a593Smuzhiyun 	 */
5594*4882a593Smuzhiyun 	if (has_session && in->si_generation == 0)
5595*4882a593Smuzhiyun 		return nfs_ok;
5596*4882a593Smuzhiyun 
5597*4882a593Smuzhiyun 	if (in->si_generation == ref->si_generation)
5598*4882a593Smuzhiyun 		return nfs_ok;
5599*4882a593Smuzhiyun 
5600*4882a593Smuzhiyun 	/* If the client sends us a stateid from the future, it's buggy: */
5601*4882a593Smuzhiyun 	if (nfsd4_stateid_generation_after(in, ref))
5602*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5603*4882a593Smuzhiyun 	/*
5604*4882a593Smuzhiyun 	 * However, we could see a stateid from the past, even from a
5605*4882a593Smuzhiyun 	 * non-buggy client.  For example, if the client sends a lock
5606*4882a593Smuzhiyun 	 * while some IO is outstanding, the lock may bump si_generation
5607*4882a593Smuzhiyun 	 * while the IO is still in flight.  The client could avoid that
5608*4882a593Smuzhiyun 	 * situation by waiting for responses on all the IO requests,
5609*4882a593Smuzhiyun 	 * but better performance may result in retrying IO that
5610*4882a593Smuzhiyun 	 * receives an old_stateid error if requests are rarely
5611*4882a593Smuzhiyun 	 * reordered in flight:
5612*4882a593Smuzhiyun 	 */
5613*4882a593Smuzhiyun 	return nfserr_old_stateid;
5614*4882a593Smuzhiyun }
5615*4882a593Smuzhiyun 
nfsd4_stid_check_stateid_generation(stateid_t * in,struct nfs4_stid * s,bool has_session)5616*4882a593Smuzhiyun static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5617*4882a593Smuzhiyun {
5618*4882a593Smuzhiyun 	__be32 ret;
5619*4882a593Smuzhiyun 
5620*4882a593Smuzhiyun 	spin_lock(&s->sc_lock);
5621*4882a593Smuzhiyun 	ret = nfsd4_verify_open_stid(s);
5622*4882a593Smuzhiyun 	if (ret == nfs_ok)
5623*4882a593Smuzhiyun 		ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5624*4882a593Smuzhiyun 	spin_unlock(&s->sc_lock);
5625*4882a593Smuzhiyun 	return ret;
5626*4882a593Smuzhiyun }
5627*4882a593Smuzhiyun 
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid * ols)5628*4882a593Smuzhiyun static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5629*4882a593Smuzhiyun {
5630*4882a593Smuzhiyun 	if (ols->st_stateowner->so_is_open_owner &&
5631*4882a593Smuzhiyun 	    !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5632*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5633*4882a593Smuzhiyun 	return nfs_ok;
5634*4882a593Smuzhiyun }
5635*4882a593Smuzhiyun 
nfsd4_validate_stateid(struct nfs4_client * cl,stateid_t * stateid)5636*4882a593Smuzhiyun static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
5637*4882a593Smuzhiyun {
5638*4882a593Smuzhiyun 	struct nfs4_stid *s;
5639*4882a593Smuzhiyun 	__be32 status = nfserr_bad_stateid;
5640*4882a593Smuzhiyun 
5641*4882a593Smuzhiyun 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5642*4882a593Smuzhiyun 		CLOSE_STATEID(stateid))
5643*4882a593Smuzhiyun 		return status;
5644*4882a593Smuzhiyun 	if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
5645*4882a593Smuzhiyun 		return status;
5646*4882a593Smuzhiyun 	spin_lock(&cl->cl_lock);
5647*4882a593Smuzhiyun 	s = find_stateid_locked(cl, stateid);
5648*4882a593Smuzhiyun 	if (!s)
5649*4882a593Smuzhiyun 		goto out_unlock;
5650*4882a593Smuzhiyun 	status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
5651*4882a593Smuzhiyun 	if (status)
5652*4882a593Smuzhiyun 		goto out_unlock;
5653*4882a593Smuzhiyun 	switch (s->sc_type) {
5654*4882a593Smuzhiyun 	case NFS4_DELEG_STID:
5655*4882a593Smuzhiyun 		status = nfs_ok;
5656*4882a593Smuzhiyun 		break;
5657*4882a593Smuzhiyun 	case NFS4_REVOKED_DELEG_STID:
5658*4882a593Smuzhiyun 		status = nfserr_deleg_revoked;
5659*4882a593Smuzhiyun 		break;
5660*4882a593Smuzhiyun 	case NFS4_OPEN_STID:
5661*4882a593Smuzhiyun 	case NFS4_LOCK_STID:
5662*4882a593Smuzhiyun 		status = nfsd4_check_openowner_confirmed(openlockstateid(s));
5663*4882a593Smuzhiyun 		break;
5664*4882a593Smuzhiyun 	default:
5665*4882a593Smuzhiyun 		printk("unknown stateid type %x\n", s->sc_type);
5666*4882a593Smuzhiyun 		fallthrough;
5667*4882a593Smuzhiyun 	case NFS4_CLOSED_STID:
5668*4882a593Smuzhiyun 	case NFS4_CLOSED_DELEG_STID:
5669*4882a593Smuzhiyun 		status = nfserr_bad_stateid;
5670*4882a593Smuzhiyun 	}
5671*4882a593Smuzhiyun out_unlock:
5672*4882a593Smuzhiyun 	spin_unlock(&cl->cl_lock);
5673*4882a593Smuzhiyun 	return status;
5674*4882a593Smuzhiyun }
5675*4882a593Smuzhiyun 
5676*4882a593Smuzhiyun __be32
nfsd4_lookup_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid,unsigned char typemask,struct nfs4_stid ** s,struct nfsd_net * nn)5677*4882a593Smuzhiyun nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5678*4882a593Smuzhiyun 		     stateid_t *stateid, unsigned char typemask,
5679*4882a593Smuzhiyun 		     struct nfs4_stid **s, struct nfsd_net *nn)
5680*4882a593Smuzhiyun {
5681*4882a593Smuzhiyun 	__be32 status;
5682*4882a593Smuzhiyun 	bool return_revoked = false;
5683*4882a593Smuzhiyun 
5684*4882a593Smuzhiyun 	/*
5685*4882a593Smuzhiyun 	 *  only return revoked delegations if explicitly asked.
5686*4882a593Smuzhiyun 	 *  otherwise we report revoked or bad_stateid status.
5687*4882a593Smuzhiyun 	 */
5688*4882a593Smuzhiyun 	if (typemask & NFS4_REVOKED_DELEG_STID)
5689*4882a593Smuzhiyun 		return_revoked = true;
5690*4882a593Smuzhiyun 	else if (typemask & NFS4_DELEG_STID)
5691*4882a593Smuzhiyun 		typemask |= NFS4_REVOKED_DELEG_STID;
5692*4882a593Smuzhiyun 
5693*4882a593Smuzhiyun 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5694*4882a593Smuzhiyun 		CLOSE_STATEID(stateid))
5695*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5696*4882a593Smuzhiyun 	status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5697*4882a593Smuzhiyun 				 false);
5698*4882a593Smuzhiyun 	if (status == nfserr_stale_clientid) {
5699*4882a593Smuzhiyun 		if (cstate->session)
5700*4882a593Smuzhiyun 			return nfserr_bad_stateid;
5701*4882a593Smuzhiyun 		return nfserr_stale_stateid;
5702*4882a593Smuzhiyun 	}
5703*4882a593Smuzhiyun 	if (status)
5704*4882a593Smuzhiyun 		return status;
5705*4882a593Smuzhiyun 	*s = find_stateid_by_type(cstate->clp, stateid, typemask);
5706*4882a593Smuzhiyun 	if (!*s)
5707*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5708*4882a593Smuzhiyun 	if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5709*4882a593Smuzhiyun 		nfs4_put_stid(*s);
5710*4882a593Smuzhiyun 		if (cstate->minorversion)
5711*4882a593Smuzhiyun 			return nfserr_deleg_revoked;
5712*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5713*4882a593Smuzhiyun 	}
5714*4882a593Smuzhiyun 	return nfs_ok;
5715*4882a593Smuzhiyun }
5716*4882a593Smuzhiyun 
5717*4882a593Smuzhiyun static struct nfsd_file *
nfs4_find_file(struct nfs4_stid * s,int flags)5718*4882a593Smuzhiyun nfs4_find_file(struct nfs4_stid *s, int flags)
5719*4882a593Smuzhiyun {
5720*4882a593Smuzhiyun 	if (!s)
5721*4882a593Smuzhiyun 		return NULL;
5722*4882a593Smuzhiyun 
5723*4882a593Smuzhiyun 	switch (s->sc_type) {
5724*4882a593Smuzhiyun 	case NFS4_DELEG_STID:
5725*4882a593Smuzhiyun 		if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5726*4882a593Smuzhiyun 			return NULL;
5727*4882a593Smuzhiyun 		return nfsd_file_get(s->sc_file->fi_deleg_file);
5728*4882a593Smuzhiyun 	case NFS4_OPEN_STID:
5729*4882a593Smuzhiyun 	case NFS4_LOCK_STID:
5730*4882a593Smuzhiyun 		if (flags & RD_STATE)
5731*4882a593Smuzhiyun 			return find_readable_file(s->sc_file);
5732*4882a593Smuzhiyun 		else
5733*4882a593Smuzhiyun 			return find_writeable_file(s->sc_file);
5734*4882a593Smuzhiyun 	}
5735*4882a593Smuzhiyun 
5736*4882a593Smuzhiyun 	return NULL;
5737*4882a593Smuzhiyun }
5738*4882a593Smuzhiyun 
5739*4882a593Smuzhiyun static __be32
nfs4_check_olstateid(struct nfs4_ol_stateid * ols,int flags)5740*4882a593Smuzhiyun nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
5741*4882a593Smuzhiyun {
5742*4882a593Smuzhiyun 	__be32 status;
5743*4882a593Smuzhiyun 
5744*4882a593Smuzhiyun 	status = nfsd4_check_openowner_confirmed(ols);
5745*4882a593Smuzhiyun 	if (status)
5746*4882a593Smuzhiyun 		return status;
5747*4882a593Smuzhiyun 	return nfs4_check_openmode(ols, flags);
5748*4882a593Smuzhiyun }
5749*4882a593Smuzhiyun 
5750*4882a593Smuzhiyun static __be32
nfs4_check_file(struct svc_rqst * rqstp,struct svc_fh * fhp,struct nfs4_stid * s,struct nfsd_file ** nfp,int flags)5751*4882a593Smuzhiyun nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5752*4882a593Smuzhiyun 		struct nfsd_file **nfp, int flags)
5753*4882a593Smuzhiyun {
5754*4882a593Smuzhiyun 	int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
5755*4882a593Smuzhiyun 	struct nfsd_file *nf;
5756*4882a593Smuzhiyun 	__be32 status;
5757*4882a593Smuzhiyun 
5758*4882a593Smuzhiyun 	nf = nfs4_find_file(s, flags);
5759*4882a593Smuzhiyun 	if (nf) {
5760*4882a593Smuzhiyun 		status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5761*4882a593Smuzhiyun 				acc | NFSD_MAY_OWNER_OVERRIDE);
5762*4882a593Smuzhiyun 		if (status) {
5763*4882a593Smuzhiyun 			nfsd_file_put(nf);
5764*4882a593Smuzhiyun 			goto out;
5765*4882a593Smuzhiyun 		}
5766*4882a593Smuzhiyun 	} else {
5767*4882a593Smuzhiyun 		status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
5768*4882a593Smuzhiyun 		if (status)
5769*4882a593Smuzhiyun 			return status;
5770*4882a593Smuzhiyun 	}
5771*4882a593Smuzhiyun 	*nfp = nf;
5772*4882a593Smuzhiyun out:
5773*4882a593Smuzhiyun 	return status;
5774*4882a593Smuzhiyun }
5775*4882a593Smuzhiyun static void
_free_cpntf_state_locked(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)5776*4882a593Smuzhiyun _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5777*4882a593Smuzhiyun {
5778*4882a593Smuzhiyun 	WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5779*4882a593Smuzhiyun 	if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5780*4882a593Smuzhiyun 		return;
5781*4882a593Smuzhiyun 	list_del(&cps->cp_list);
5782*4882a593Smuzhiyun 	idr_remove(&nn->s2s_cp_stateids,
5783*4882a593Smuzhiyun 		   cps->cp_stateid.stid.si_opaque.so_id);
5784*4882a593Smuzhiyun 	kfree(cps);
5785*4882a593Smuzhiyun }
5786*4882a593Smuzhiyun /*
5787*4882a593Smuzhiyun  * A READ from an inter server to server COPY will have a
5788*4882a593Smuzhiyun  * copy stateid. Look up the copy notify stateid from the
5789*4882a593Smuzhiyun  * idr structure and take a reference on it.
5790*4882a593Smuzhiyun  */
manage_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_client * clp,struct nfs4_cpntf_state ** cps)5791*4882a593Smuzhiyun __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5792*4882a593Smuzhiyun 			  struct nfs4_client *clp,
5793*4882a593Smuzhiyun 			  struct nfs4_cpntf_state **cps)
5794*4882a593Smuzhiyun {
5795*4882a593Smuzhiyun 	copy_stateid_t *cps_t;
5796*4882a593Smuzhiyun 	struct nfs4_cpntf_state *state = NULL;
5797*4882a593Smuzhiyun 
5798*4882a593Smuzhiyun 	if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5799*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5800*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
5801*4882a593Smuzhiyun 	cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5802*4882a593Smuzhiyun 	if (cps_t) {
5803*4882a593Smuzhiyun 		state = container_of(cps_t, struct nfs4_cpntf_state,
5804*4882a593Smuzhiyun 				     cp_stateid);
5805*4882a593Smuzhiyun 		if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5806*4882a593Smuzhiyun 			state = NULL;
5807*4882a593Smuzhiyun 			goto unlock;
5808*4882a593Smuzhiyun 		}
5809*4882a593Smuzhiyun 		if (!clp)
5810*4882a593Smuzhiyun 			refcount_inc(&state->cp_stateid.sc_count);
5811*4882a593Smuzhiyun 		else
5812*4882a593Smuzhiyun 			_free_cpntf_state_locked(nn, state);
5813*4882a593Smuzhiyun 	}
5814*4882a593Smuzhiyun unlock:
5815*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
5816*4882a593Smuzhiyun 	if (!state)
5817*4882a593Smuzhiyun 		return nfserr_bad_stateid;
5818*4882a593Smuzhiyun 	if (!clp && state)
5819*4882a593Smuzhiyun 		*cps = state;
5820*4882a593Smuzhiyun 	return 0;
5821*4882a593Smuzhiyun }
5822*4882a593Smuzhiyun 
find_cpntf_state(struct nfsd_net * nn,stateid_t * st,struct nfs4_stid ** stid)5823*4882a593Smuzhiyun static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5824*4882a593Smuzhiyun 			       struct nfs4_stid **stid)
5825*4882a593Smuzhiyun {
5826*4882a593Smuzhiyun 	__be32 status;
5827*4882a593Smuzhiyun 	struct nfs4_cpntf_state *cps = NULL;
5828*4882a593Smuzhiyun 	struct nfsd4_compound_state cstate;
5829*4882a593Smuzhiyun 
5830*4882a593Smuzhiyun 	status = manage_cpntf_state(nn, st, NULL, &cps);
5831*4882a593Smuzhiyun 	if (status)
5832*4882a593Smuzhiyun 		return status;
5833*4882a593Smuzhiyun 
5834*4882a593Smuzhiyun 	cps->cpntf_time = ktime_get_boottime_seconds();
5835*4882a593Smuzhiyun 	memset(&cstate, 0, sizeof(cstate));
5836*4882a593Smuzhiyun 	status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5837*4882a593Smuzhiyun 	if (status)
5838*4882a593Smuzhiyun 		goto out;
5839*4882a593Smuzhiyun 	status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5840*4882a593Smuzhiyun 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5841*4882a593Smuzhiyun 				stid, nn);
5842*4882a593Smuzhiyun 	put_client_renew(cstate.clp);
5843*4882a593Smuzhiyun out:
5844*4882a593Smuzhiyun 	nfs4_put_cpntf_state(nn, cps);
5845*4882a593Smuzhiyun 	return status;
5846*4882a593Smuzhiyun }
5847*4882a593Smuzhiyun 
nfs4_put_cpntf_state(struct nfsd_net * nn,struct nfs4_cpntf_state * cps)5848*4882a593Smuzhiyun void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5849*4882a593Smuzhiyun {
5850*4882a593Smuzhiyun 	spin_lock(&nn->s2s_cp_lock);
5851*4882a593Smuzhiyun 	_free_cpntf_state_locked(nn, cps);
5852*4882a593Smuzhiyun 	spin_unlock(&nn->s2s_cp_lock);
5853*4882a593Smuzhiyun }
5854*4882a593Smuzhiyun 
5855*4882a593Smuzhiyun /*
5856*4882a593Smuzhiyun  * Checks for stateid operations
5857*4882a593Smuzhiyun  */
5858*4882a593Smuzhiyun __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)5859*4882a593Smuzhiyun nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
5860*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
5861*4882a593Smuzhiyun 		stateid_t *stateid, int flags, struct nfsd_file **nfp,
5862*4882a593Smuzhiyun 		struct nfs4_stid **cstid)
5863*4882a593Smuzhiyun {
5864*4882a593Smuzhiyun 	struct inode *ino = d_inode(fhp->fh_dentry);
5865*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
5866*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5867*4882a593Smuzhiyun 	struct nfs4_stid *s = NULL;
5868*4882a593Smuzhiyun 	__be32 status;
5869*4882a593Smuzhiyun 
5870*4882a593Smuzhiyun 	if (nfp)
5871*4882a593Smuzhiyun 		*nfp = NULL;
5872*4882a593Smuzhiyun 
5873*4882a593Smuzhiyun 	if (grace_disallows_io(net, ino))
5874*4882a593Smuzhiyun 		return nfserr_grace;
5875*4882a593Smuzhiyun 
5876*4882a593Smuzhiyun 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5877*4882a593Smuzhiyun 		status = check_special_stateids(net, fhp, stateid, flags);
5878*4882a593Smuzhiyun 		goto done;
5879*4882a593Smuzhiyun 	}
5880*4882a593Smuzhiyun 
5881*4882a593Smuzhiyun 	status = nfsd4_lookup_stateid(cstate, stateid,
5882*4882a593Smuzhiyun 				NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5883*4882a593Smuzhiyun 				&s, nn);
5884*4882a593Smuzhiyun 	if (status == nfserr_bad_stateid)
5885*4882a593Smuzhiyun 		status = find_cpntf_state(nn, stateid, &s);
5886*4882a593Smuzhiyun 	if (status)
5887*4882a593Smuzhiyun 		return status;
5888*4882a593Smuzhiyun 	status = nfsd4_stid_check_stateid_generation(stateid, s,
5889*4882a593Smuzhiyun 			nfsd4_has_session(cstate));
5890*4882a593Smuzhiyun 	if (status)
5891*4882a593Smuzhiyun 		goto out;
5892*4882a593Smuzhiyun 
5893*4882a593Smuzhiyun 	switch (s->sc_type) {
5894*4882a593Smuzhiyun 	case NFS4_DELEG_STID:
5895*4882a593Smuzhiyun 		status = nfs4_check_delegmode(delegstateid(s), flags);
5896*4882a593Smuzhiyun 		break;
5897*4882a593Smuzhiyun 	case NFS4_OPEN_STID:
5898*4882a593Smuzhiyun 	case NFS4_LOCK_STID:
5899*4882a593Smuzhiyun 		status = nfs4_check_olstateid(openlockstateid(s), flags);
5900*4882a593Smuzhiyun 		break;
5901*4882a593Smuzhiyun 	default:
5902*4882a593Smuzhiyun 		status = nfserr_bad_stateid;
5903*4882a593Smuzhiyun 		break;
5904*4882a593Smuzhiyun 	}
5905*4882a593Smuzhiyun 	if (status)
5906*4882a593Smuzhiyun 		goto out;
5907*4882a593Smuzhiyun 	status = nfs4_check_fh(fhp, s);
5908*4882a593Smuzhiyun 
5909*4882a593Smuzhiyun done:
5910*4882a593Smuzhiyun 	if (status == nfs_ok && nfp)
5911*4882a593Smuzhiyun 		status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
5912*4882a593Smuzhiyun out:
5913*4882a593Smuzhiyun 	if (s) {
5914*4882a593Smuzhiyun 		if (!status && cstid)
5915*4882a593Smuzhiyun 			*cstid = s;
5916*4882a593Smuzhiyun 		else
5917*4882a593Smuzhiyun 			nfs4_put_stid(s);
5918*4882a593Smuzhiyun 	}
5919*4882a593Smuzhiyun 	return status;
5920*4882a593Smuzhiyun }
5921*4882a593Smuzhiyun 
5922*4882a593Smuzhiyun /*
5923*4882a593Smuzhiyun  * Test if the stateid is valid
5924*4882a593Smuzhiyun  */
5925*4882a593Smuzhiyun __be32
nfsd4_test_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)5926*4882a593Smuzhiyun nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5927*4882a593Smuzhiyun 		   union nfsd4_op_u *u)
5928*4882a593Smuzhiyun {
5929*4882a593Smuzhiyun 	struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
5930*4882a593Smuzhiyun 	struct nfsd4_test_stateid_id *stateid;
5931*4882a593Smuzhiyun 	struct nfs4_client *cl = cstate->session->se_client;
5932*4882a593Smuzhiyun 
5933*4882a593Smuzhiyun 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
5934*4882a593Smuzhiyun 		stateid->ts_id_status =
5935*4882a593Smuzhiyun 			nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
5936*4882a593Smuzhiyun 
5937*4882a593Smuzhiyun 	return nfs_ok;
5938*4882a593Smuzhiyun }
5939*4882a593Smuzhiyun 
5940*4882a593Smuzhiyun static __be32
nfsd4_free_lock_stateid(stateid_t * stateid,struct nfs4_stid * s)5941*4882a593Smuzhiyun nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5942*4882a593Smuzhiyun {
5943*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp = openlockstateid(s);
5944*4882a593Smuzhiyun 	__be32 ret;
5945*4882a593Smuzhiyun 
5946*4882a593Smuzhiyun 	ret = nfsd4_lock_ol_stateid(stp);
5947*4882a593Smuzhiyun 	if (ret)
5948*4882a593Smuzhiyun 		goto out_put_stid;
5949*4882a593Smuzhiyun 
5950*4882a593Smuzhiyun 	ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5951*4882a593Smuzhiyun 	if (ret)
5952*4882a593Smuzhiyun 		goto out;
5953*4882a593Smuzhiyun 
5954*4882a593Smuzhiyun 	ret = nfserr_locks_held;
5955*4882a593Smuzhiyun 	if (check_for_locks(stp->st_stid.sc_file,
5956*4882a593Smuzhiyun 			    lockowner(stp->st_stateowner)))
5957*4882a593Smuzhiyun 		goto out;
5958*4882a593Smuzhiyun 
5959*4882a593Smuzhiyun 	release_lock_stateid(stp);
5960*4882a593Smuzhiyun 	ret = nfs_ok;
5961*4882a593Smuzhiyun 
5962*4882a593Smuzhiyun out:
5963*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
5964*4882a593Smuzhiyun out_put_stid:
5965*4882a593Smuzhiyun 	nfs4_put_stid(s);
5966*4882a593Smuzhiyun 	return ret;
5967*4882a593Smuzhiyun }
5968*4882a593Smuzhiyun 
5969*4882a593Smuzhiyun __be32
nfsd4_free_stateid(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)5970*4882a593Smuzhiyun nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5971*4882a593Smuzhiyun 		   union nfsd4_op_u *u)
5972*4882a593Smuzhiyun {
5973*4882a593Smuzhiyun 	struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
5974*4882a593Smuzhiyun 	stateid_t *stateid = &free_stateid->fr_stateid;
5975*4882a593Smuzhiyun 	struct nfs4_stid *s;
5976*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
5977*4882a593Smuzhiyun 	struct nfs4_client *cl = cstate->session->se_client;
5978*4882a593Smuzhiyun 	__be32 ret = nfserr_bad_stateid;
5979*4882a593Smuzhiyun 
5980*4882a593Smuzhiyun 	spin_lock(&cl->cl_lock);
5981*4882a593Smuzhiyun 	s = find_stateid_locked(cl, stateid);
5982*4882a593Smuzhiyun 	if (!s)
5983*4882a593Smuzhiyun 		goto out_unlock;
5984*4882a593Smuzhiyun 	spin_lock(&s->sc_lock);
5985*4882a593Smuzhiyun 	switch (s->sc_type) {
5986*4882a593Smuzhiyun 	case NFS4_DELEG_STID:
5987*4882a593Smuzhiyun 		ret = nfserr_locks_held;
5988*4882a593Smuzhiyun 		break;
5989*4882a593Smuzhiyun 	case NFS4_OPEN_STID:
5990*4882a593Smuzhiyun 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5991*4882a593Smuzhiyun 		if (ret)
5992*4882a593Smuzhiyun 			break;
5993*4882a593Smuzhiyun 		ret = nfserr_locks_held;
5994*4882a593Smuzhiyun 		break;
5995*4882a593Smuzhiyun 	case NFS4_LOCK_STID:
5996*4882a593Smuzhiyun 		spin_unlock(&s->sc_lock);
5997*4882a593Smuzhiyun 		refcount_inc(&s->sc_count);
5998*4882a593Smuzhiyun 		spin_unlock(&cl->cl_lock);
5999*4882a593Smuzhiyun 		ret = nfsd4_free_lock_stateid(stateid, s);
6000*4882a593Smuzhiyun 		goto out;
6001*4882a593Smuzhiyun 	case NFS4_REVOKED_DELEG_STID:
6002*4882a593Smuzhiyun 		spin_unlock(&s->sc_lock);
6003*4882a593Smuzhiyun 		dp = delegstateid(s);
6004*4882a593Smuzhiyun 		list_del_init(&dp->dl_recall_lru);
6005*4882a593Smuzhiyun 		spin_unlock(&cl->cl_lock);
6006*4882a593Smuzhiyun 		nfs4_put_stid(s);
6007*4882a593Smuzhiyun 		ret = nfs_ok;
6008*4882a593Smuzhiyun 		goto out;
6009*4882a593Smuzhiyun 	/* Default falls through and returns nfserr_bad_stateid */
6010*4882a593Smuzhiyun 	}
6011*4882a593Smuzhiyun 	spin_unlock(&s->sc_lock);
6012*4882a593Smuzhiyun out_unlock:
6013*4882a593Smuzhiyun 	spin_unlock(&cl->cl_lock);
6014*4882a593Smuzhiyun out:
6015*4882a593Smuzhiyun 	return ret;
6016*4882a593Smuzhiyun }
6017*4882a593Smuzhiyun 
6018*4882a593Smuzhiyun static inline int
setlkflg(int type)6019*4882a593Smuzhiyun setlkflg (int type)
6020*4882a593Smuzhiyun {
6021*4882a593Smuzhiyun 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6022*4882a593Smuzhiyun 		RD_STATE : WR_STATE;
6023*4882a593Smuzhiyun }
6024*4882a593Smuzhiyun 
nfs4_seqid_op_checks(struct nfsd4_compound_state * cstate,stateid_t * stateid,u32 seqid,struct nfs4_ol_stateid * stp)6025*4882a593Smuzhiyun static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
6026*4882a593Smuzhiyun {
6027*4882a593Smuzhiyun 	struct svc_fh *current_fh = &cstate->current_fh;
6028*4882a593Smuzhiyun 	struct nfs4_stateowner *sop = stp->st_stateowner;
6029*4882a593Smuzhiyun 	__be32 status;
6030*4882a593Smuzhiyun 
6031*4882a593Smuzhiyun 	status = nfsd4_check_seqid(cstate, sop, seqid);
6032*4882a593Smuzhiyun 	if (status)
6033*4882a593Smuzhiyun 		return status;
6034*4882a593Smuzhiyun 	status = nfsd4_lock_ol_stateid(stp);
6035*4882a593Smuzhiyun 	if (status != nfs_ok)
6036*4882a593Smuzhiyun 		return status;
6037*4882a593Smuzhiyun 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
6038*4882a593Smuzhiyun 	if (status == nfs_ok)
6039*4882a593Smuzhiyun 		status = nfs4_check_fh(current_fh, &stp->st_stid);
6040*4882a593Smuzhiyun 	if (status != nfs_ok)
6041*4882a593Smuzhiyun 		mutex_unlock(&stp->st_mutex);
6042*4882a593Smuzhiyun 	return status;
6043*4882a593Smuzhiyun }
6044*4882a593Smuzhiyun 
6045*4882a593Smuzhiyun /*
6046*4882a593Smuzhiyun  * Checks for sequence id mutating operations.
6047*4882a593Smuzhiyun  */
6048*4882a593Smuzhiyun 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)6049*4882a593Smuzhiyun nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6050*4882a593Smuzhiyun 			 stateid_t *stateid, char typemask,
6051*4882a593Smuzhiyun 			 struct nfs4_ol_stateid **stpp,
6052*4882a593Smuzhiyun 			 struct nfsd_net *nn)
6053*4882a593Smuzhiyun {
6054*4882a593Smuzhiyun 	__be32 status;
6055*4882a593Smuzhiyun 	struct nfs4_stid *s;
6056*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp = NULL;
6057*4882a593Smuzhiyun 
6058*4882a593Smuzhiyun 	trace_nfsd_preprocess(seqid, stateid);
6059*4882a593Smuzhiyun 
6060*4882a593Smuzhiyun 	*stpp = NULL;
6061*4882a593Smuzhiyun 	status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
6062*4882a593Smuzhiyun 	if (status)
6063*4882a593Smuzhiyun 		return status;
6064*4882a593Smuzhiyun 	stp = openlockstateid(s);
6065*4882a593Smuzhiyun 	nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
6066*4882a593Smuzhiyun 
6067*4882a593Smuzhiyun 	status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
6068*4882a593Smuzhiyun 	if (!status)
6069*4882a593Smuzhiyun 		*stpp = stp;
6070*4882a593Smuzhiyun 	else
6071*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
6072*4882a593Smuzhiyun 	return status;
6073*4882a593Smuzhiyun }
6074*4882a593Smuzhiyun 
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state * cstate,u32 seqid,stateid_t * stateid,struct nfs4_ol_stateid ** stpp,struct nfsd_net * nn)6075*4882a593Smuzhiyun static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6076*4882a593Smuzhiyun 						 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
6077*4882a593Smuzhiyun {
6078*4882a593Smuzhiyun 	__be32 status;
6079*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
6080*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6081*4882a593Smuzhiyun 
6082*4882a593Smuzhiyun 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
6083*4882a593Smuzhiyun 						NFS4_OPEN_STID, &stp, nn);
6084*4882a593Smuzhiyun 	if (status)
6085*4882a593Smuzhiyun 		return status;
6086*4882a593Smuzhiyun 	oo = openowner(stp->st_stateowner);
6087*4882a593Smuzhiyun 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
6088*4882a593Smuzhiyun 		mutex_unlock(&stp->st_mutex);
6089*4882a593Smuzhiyun 		nfs4_put_stid(&stp->st_stid);
6090*4882a593Smuzhiyun 		return nfserr_bad_stateid;
6091*4882a593Smuzhiyun 	}
6092*4882a593Smuzhiyun 	*stpp = stp;
6093*4882a593Smuzhiyun 	return nfs_ok;
6094*4882a593Smuzhiyun }
6095*4882a593Smuzhiyun 
6096*4882a593Smuzhiyun __be32
nfsd4_open_confirm(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6097*4882a593Smuzhiyun nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6098*4882a593Smuzhiyun 		   union nfsd4_op_u *u)
6099*4882a593Smuzhiyun {
6100*4882a593Smuzhiyun 	struct nfsd4_open_confirm *oc = &u->open_confirm;
6101*4882a593Smuzhiyun 	__be32 status;
6102*4882a593Smuzhiyun 	struct nfs4_openowner *oo;
6103*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6104*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6105*4882a593Smuzhiyun 
6106*4882a593Smuzhiyun 	dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6107*4882a593Smuzhiyun 			cstate->current_fh.fh_dentry);
6108*4882a593Smuzhiyun 
6109*4882a593Smuzhiyun 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
6110*4882a593Smuzhiyun 	if (status)
6111*4882a593Smuzhiyun 		return status;
6112*4882a593Smuzhiyun 
6113*4882a593Smuzhiyun 	status = nfs4_preprocess_seqid_op(cstate,
6114*4882a593Smuzhiyun 					oc->oc_seqid, &oc->oc_req_stateid,
6115*4882a593Smuzhiyun 					NFS4_OPEN_STID, &stp, nn);
6116*4882a593Smuzhiyun 	if (status)
6117*4882a593Smuzhiyun 		goto out;
6118*4882a593Smuzhiyun 	oo = openowner(stp->st_stateowner);
6119*4882a593Smuzhiyun 	status = nfserr_bad_stateid;
6120*4882a593Smuzhiyun 	if (oo->oo_flags & NFS4_OO_CONFIRMED) {
6121*4882a593Smuzhiyun 		mutex_unlock(&stp->st_mutex);
6122*4882a593Smuzhiyun 		goto put_stateid;
6123*4882a593Smuzhiyun 	}
6124*4882a593Smuzhiyun 	oo->oo_flags |= NFS4_OO_CONFIRMED;
6125*4882a593Smuzhiyun 	nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
6126*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
6127*4882a593Smuzhiyun 	trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
6128*4882a593Smuzhiyun 	nfsd4_client_record_create(oo->oo_owner.so_client);
6129*4882a593Smuzhiyun 	status = nfs_ok;
6130*4882a593Smuzhiyun put_stateid:
6131*4882a593Smuzhiyun 	nfs4_put_stid(&stp->st_stid);
6132*4882a593Smuzhiyun out:
6133*4882a593Smuzhiyun 	nfsd4_bump_seqid(cstate, status);
6134*4882a593Smuzhiyun 	return status;
6135*4882a593Smuzhiyun }
6136*4882a593Smuzhiyun 
nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid * stp,u32 access)6137*4882a593Smuzhiyun static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
6138*4882a593Smuzhiyun {
6139*4882a593Smuzhiyun 	if (!test_access(access, stp))
6140*4882a593Smuzhiyun 		return;
6141*4882a593Smuzhiyun 	nfs4_file_put_access(stp->st_stid.sc_file, access);
6142*4882a593Smuzhiyun 	clear_access(access, stp);
6143*4882a593Smuzhiyun }
6144*4882a593Smuzhiyun 
nfs4_stateid_downgrade(struct nfs4_ol_stateid * stp,u32 to_access)6145*4882a593Smuzhiyun static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6146*4882a593Smuzhiyun {
6147*4882a593Smuzhiyun 	switch (to_access) {
6148*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_READ:
6149*4882a593Smuzhiyun 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6150*4882a593Smuzhiyun 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6151*4882a593Smuzhiyun 		break;
6152*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_WRITE:
6153*4882a593Smuzhiyun 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6154*4882a593Smuzhiyun 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6155*4882a593Smuzhiyun 		break;
6156*4882a593Smuzhiyun 	case NFS4_SHARE_ACCESS_BOTH:
6157*4882a593Smuzhiyun 		break;
6158*4882a593Smuzhiyun 	default:
6159*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
6160*4882a593Smuzhiyun 	}
6161*4882a593Smuzhiyun }
6162*4882a593Smuzhiyun 
6163*4882a593Smuzhiyun __be32
nfsd4_open_downgrade(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6164*4882a593Smuzhiyun nfsd4_open_downgrade(struct svc_rqst *rqstp,
6165*4882a593Smuzhiyun 		     struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
6166*4882a593Smuzhiyun {
6167*4882a593Smuzhiyun 	struct nfsd4_open_downgrade *od = &u->open_downgrade;
6168*4882a593Smuzhiyun 	__be32 status;
6169*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6170*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6171*4882a593Smuzhiyun 
6172*4882a593Smuzhiyun 	dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6173*4882a593Smuzhiyun 			cstate->current_fh.fh_dentry);
6174*4882a593Smuzhiyun 
6175*4882a593Smuzhiyun 	/* We don't yet support WANT bits: */
6176*4882a593Smuzhiyun 	if (od->od_deleg_want)
6177*4882a593Smuzhiyun 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6178*4882a593Smuzhiyun 			od->od_deleg_want);
6179*4882a593Smuzhiyun 
6180*4882a593Smuzhiyun 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
6181*4882a593Smuzhiyun 					&od->od_stateid, &stp, nn);
6182*4882a593Smuzhiyun 	if (status)
6183*4882a593Smuzhiyun 		goto out;
6184*4882a593Smuzhiyun 	status = nfserr_inval;
6185*4882a593Smuzhiyun 	if (!test_access(od->od_share_access, stp)) {
6186*4882a593Smuzhiyun 		dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6187*4882a593Smuzhiyun 			stp->st_access_bmap, od->od_share_access);
6188*4882a593Smuzhiyun 		goto put_stateid;
6189*4882a593Smuzhiyun 	}
6190*4882a593Smuzhiyun 	if (!test_deny(od->od_share_deny, stp)) {
6191*4882a593Smuzhiyun 		dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6192*4882a593Smuzhiyun 			stp->st_deny_bmap, od->od_share_deny);
6193*4882a593Smuzhiyun 		goto put_stateid;
6194*4882a593Smuzhiyun 	}
6195*4882a593Smuzhiyun 	nfs4_stateid_downgrade(stp, od->od_share_access);
6196*4882a593Smuzhiyun 	reset_union_bmap_deny(od->od_share_deny, stp);
6197*4882a593Smuzhiyun 	nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
6198*4882a593Smuzhiyun 	status = nfs_ok;
6199*4882a593Smuzhiyun put_stateid:
6200*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
6201*4882a593Smuzhiyun 	nfs4_put_stid(&stp->st_stid);
6202*4882a593Smuzhiyun out:
6203*4882a593Smuzhiyun 	nfsd4_bump_seqid(cstate, status);
6204*4882a593Smuzhiyun 	return status;
6205*4882a593Smuzhiyun }
6206*4882a593Smuzhiyun 
nfsd4_close_open_stateid(struct nfs4_ol_stateid * s)6207*4882a593Smuzhiyun static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6208*4882a593Smuzhiyun {
6209*4882a593Smuzhiyun 	struct nfs4_client *clp = s->st_stid.sc_client;
6210*4882a593Smuzhiyun 	bool unhashed;
6211*4882a593Smuzhiyun 	LIST_HEAD(reaplist);
6212*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6213*4882a593Smuzhiyun 
6214*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
6215*4882a593Smuzhiyun 	unhashed = unhash_open_stateid(s, &reaplist);
6216*4882a593Smuzhiyun 
6217*4882a593Smuzhiyun 	if (clp->cl_minorversion) {
6218*4882a593Smuzhiyun 		if (unhashed)
6219*4882a593Smuzhiyun 			put_ol_stateid_locked(s, &reaplist);
6220*4882a593Smuzhiyun 		spin_unlock(&clp->cl_lock);
6221*4882a593Smuzhiyun 		list_for_each_entry(stp, &reaplist, st_locks)
6222*4882a593Smuzhiyun 			nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
6223*4882a593Smuzhiyun 		free_ol_stateid_reaplist(&reaplist);
6224*4882a593Smuzhiyun 	} else {
6225*4882a593Smuzhiyun 		spin_unlock(&clp->cl_lock);
6226*4882a593Smuzhiyun 		free_ol_stateid_reaplist(&reaplist);
6227*4882a593Smuzhiyun 		if (unhashed)
6228*4882a593Smuzhiyun 			move_to_close_lru(s, clp->net);
6229*4882a593Smuzhiyun 	}
6230*4882a593Smuzhiyun }
6231*4882a593Smuzhiyun 
6232*4882a593Smuzhiyun /*
6233*4882a593Smuzhiyun  * nfs4_unlock_state() called after encode
6234*4882a593Smuzhiyun  */
6235*4882a593Smuzhiyun __be32
nfsd4_close(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6236*4882a593Smuzhiyun nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6237*4882a593Smuzhiyun 		union nfsd4_op_u *u)
6238*4882a593Smuzhiyun {
6239*4882a593Smuzhiyun 	struct nfsd4_close *close = &u->close;
6240*4882a593Smuzhiyun 	__be32 status;
6241*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6242*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
6243*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6244*4882a593Smuzhiyun 
6245*4882a593Smuzhiyun 	dprintk("NFSD: nfsd4_close on file %pd\n",
6246*4882a593Smuzhiyun 			cstate->current_fh.fh_dentry);
6247*4882a593Smuzhiyun 
6248*4882a593Smuzhiyun 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6249*4882a593Smuzhiyun 					&close->cl_stateid,
6250*4882a593Smuzhiyun 					NFS4_OPEN_STID|NFS4_CLOSED_STID,
6251*4882a593Smuzhiyun 					&stp, nn);
6252*4882a593Smuzhiyun 	nfsd4_bump_seqid(cstate, status);
6253*4882a593Smuzhiyun 	if (status)
6254*4882a593Smuzhiyun 		goto out;
6255*4882a593Smuzhiyun 
6256*4882a593Smuzhiyun 	stp->st_stid.sc_type = NFS4_CLOSED_STID;
6257*4882a593Smuzhiyun 
6258*4882a593Smuzhiyun 	/*
6259*4882a593Smuzhiyun 	 * Technically we don't _really_ have to increment or copy it, since
6260*4882a593Smuzhiyun 	 * it should just be gone after this operation and we clobber the
6261*4882a593Smuzhiyun 	 * copied value below, but we continue to do so here just to ensure
6262*4882a593Smuzhiyun 	 * that racing ops see that there was a state change.
6263*4882a593Smuzhiyun 	 */
6264*4882a593Smuzhiyun 	nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
6265*4882a593Smuzhiyun 
6266*4882a593Smuzhiyun 	nfsd4_close_open_stateid(stp);
6267*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
6268*4882a593Smuzhiyun 
6269*4882a593Smuzhiyun 	/* v4.1+ suggests that we send a special stateid in here, since the
6270*4882a593Smuzhiyun 	 * clients should just ignore this anyway. Since this is not useful
6271*4882a593Smuzhiyun 	 * for v4.0 clients either, we set it to the special close_stateid
6272*4882a593Smuzhiyun 	 * universally.
6273*4882a593Smuzhiyun 	 *
6274*4882a593Smuzhiyun 	 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6275*4882a593Smuzhiyun 	 */
6276*4882a593Smuzhiyun 	memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
6277*4882a593Smuzhiyun 
6278*4882a593Smuzhiyun 	/* put reference from nfs4_preprocess_seqid_op */
6279*4882a593Smuzhiyun 	nfs4_put_stid(&stp->st_stid);
6280*4882a593Smuzhiyun out:
6281*4882a593Smuzhiyun 	return status;
6282*4882a593Smuzhiyun }
6283*4882a593Smuzhiyun 
6284*4882a593Smuzhiyun __be32
nfsd4_delegreturn(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6285*4882a593Smuzhiyun nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6286*4882a593Smuzhiyun 		  union nfsd4_op_u *u)
6287*4882a593Smuzhiyun {
6288*4882a593Smuzhiyun 	struct nfsd4_delegreturn *dr = &u->delegreturn;
6289*4882a593Smuzhiyun 	struct nfs4_delegation *dp;
6290*4882a593Smuzhiyun 	stateid_t *stateid = &dr->dr_stateid;
6291*4882a593Smuzhiyun 	struct nfs4_stid *s;
6292*4882a593Smuzhiyun 	__be32 status;
6293*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6294*4882a593Smuzhiyun 
6295*4882a593Smuzhiyun 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6296*4882a593Smuzhiyun 		return status;
6297*4882a593Smuzhiyun 
6298*4882a593Smuzhiyun 	status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
6299*4882a593Smuzhiyun 	if (status)
6300*4882a593Smuzhiyun 		goto out;
6301*4882a593Smuzhiyun 	dp = delegstateid(s);
6302*4882a593Smuzhiyun 	status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
6303*4882a593Smuzhiyun 	if (status)
6304*4882a593Smuzhiyun 		goto put_stateid;
6305*4882a593Smuzhiyun 
6306*4882a593Smuzhiyun 	destroy_delegation(dp);
6307*4882a593Smuzhiyun put_stateid:
6308*4882a593Smuzhiyun 	nfs4_put_stid(&dp->dl_stid);
6309*4882a593Smuzhiyun out:
6310*4882a593Smuzhiyun 	return status;
6311*4882a593Smuzhiyun }
6312*4882a593Smuzhiyun 
6313*4882a593Smuzhiyun static inline u64
end_offset(u64 start,u64 len)6314*4882a593Smuzhiyun end_offset(u64 start, u64 len)
6315*4882a593Smuzhiyun {
6316*4882a593Smuzhiyun 	u64 end;
6317*4882a593Smuzhiyun 
6318*4882a593Smuzhiyun 	end = start + len;
6319*4882a593Smuzhiyun 	return end >= start ? end: NFS4_MAX_UINT64;
6320*4882a593Smuzhiyun }
6321*4882a593Smuzhiyun 
6322*4882a593Smuzhiyun /* last octet in a range */
6323*4882a593Smuzhiyun static inline u64
last_byte_offset(u64 start,u64 len)6324*4882a593Smuzhiyun last_byte_offset(u64 start, u64 len)
6325*4882a593Smuzhiyun {
6326*4882a593Smuzhiyun 	u64 end;
6327*4882a593Smuzhiyun 
6328*4882a593Smuzhiyun 	WARN_ON_ONCE(!len);
6329*4882a593Smuzhiyun 	end = start + len;
6330*4882a593Smuzhiyun 	return end > start ? end - 1: NFS4_MAX_UINT64;
6331*4882a593Smuzhiyun }
6332*4882a593Smuzhiyun 
6333*4882a593Smuzhiyun /*
6334*4882a593Smuzhiyun  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6335*4882a593Smuzhiyun  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6336*4882a593Smuzhiyun  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
6337*4882a593Smuzhiyun  * locking, this prevents us from being completely protocol-compliant.  The
6338*4882a593Smuzhiyun  * real solution to this problem is to start using unsigned file offsets in
6339*4882a593Smuzhiyun  * the VFS, but this is a very deep change!
6340*4882a593Smuzhiyun  */
6341*4882a593Smuzhiyun static inline void
nfs4_transform_lock_offset(struct file_lock * lock)6342*4882a593Smuzhiyun nfs4_transform_lock_offset(struct file_lock *lock)
6343*4882a593Smuzhiyun {
6344*4882a593Smuzhiyun 	if (lock->fl_start < 0)
6345*4882a593Smuzhiyun 		lock->fl_start = OFFSET_MAX;
6346*4882a593Smuzhiyun 	if (lock->fl_end < 0)
6347*4882a593Smuzhiyun 		lock->fl_end = OFFSET_MAX;
6348*4882a593Smuzhiyun }
6349*4882a593Smuzhiyun 
6350*4882a593Smuzhiyun static fl_owner_t
nfsd4_fl_get_owner(fl_owner_t owner)6351*4882a593Smuzhiyun nfsd4_fl_get_owner(fl_owner_t owner)
6352*4882a593Smuzhiyun {
6353*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6354*4882a593Smuzhiyun 
6355*4882a593Smuzhiyun 	nfs4_get_stateowner(&lo->lo_owner);
6356*4882a593Smuzhiyun 	return owner;
6357*4882a593Smuzhiyun }
6358*4882a593Smuzhiyun 
6359*4882a593Smuzhiyun static void
nfsd4_fl_put_owner(fl_owner_t owner)6360*4882a593Smuzhiyun nfsd4_fl_put_owner(fl_owner_t owner)
6361*4882a593Smuzhiyun {
6362*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6363*4882a593Smuzhiyun 
6364*4882a593Smuzhiyun 	if (lo)
6365*4882a593Smuzhiyun 		nfs4_put_stateowner(&lo->lo_owner);
6366*4882a593Smuzhiyun }
6367*4882a593Smuzhiyun 
6368*4882a593Smuzhiyun static void
nfsd4_lm_notify(struct file_lock * fl)6369*4882a593Smuzhiyun nfsd4_lm_notify(struct file_lock *fl)
6370*4882a593Smuzhiyun {
6371*4882a593Smuzhiyun 	struct nfs4_lockowner		*lo = (struct nfs4_lockowner *)fl->fl_owner;
6372*4882a593Smuzhiyun 	struct net			*net = lo->lo_owner.so_client->net;
6373*4882a593Smuzhiyun 	struct nfsd_net			*nn = net_generic(net, nfsd_net_id);
6374*4882a593Smuzhiyun 	struct nfsd4_blocked_lock	*nbl = container_of(fl,
6375*4882a593Smuzhiyun 						struct nfsd4_blocked_lock, nbl_lock);
6376*4882a593Smuzhiyun 	bool queue = false;
6377*4882a593Smuzhiyun 
6378*4882a593Smuzhiyun 	/* An empty list means that something else is going to be using it */
6379*4882a593Smuzhiyun 	spin_lock(&nn->blocked_locks_lock);
6380*4882a593Smuzhiyun 	if (!list_empty(&nbl->nbl_list)) {
6381*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_list);
6382*4882a593Smuzhiyun 		list_del_init(&nbl->nbl_lru);
6383*4882a593Smuzhiyun 		queue = true;
6384*4882a593Smuzhiyun 	}
6385*4882a593Smuzhiyun 	spin_unlock(&nn->blocked_locks_lock);
6386*4882a593Smuzhiyun 
6387*4882a593Smuzhiyun 	if (queue)
6388*4882a593Smuzhiyun 		nfsd4_run_cb(&nbl->nbl_cb);
6389*4882a593Smuzhiyun }
6390*4882a593Smuzhiyun 
6391*4882a593Smuzhiyun static const struct lock_manager_operations nfsd_posix_mng_ops  = {
6392*4882a593Smuzhiyun 	.lm_notify = nfsd4_lm_notify,
6393*4882a593Smuzhiyun 	.lm_get_owner = nfsd4_fl_get_owner,
6394*4882a593Smuzhiyun 	.lm_put_owner = nfsd4_fl_put_owner,
6395*4882a593Smuzhiyun };
6396*4882a593Smuzhiyun 
6397*4882a593Smuzhiyun static inline void
nfs4_set_lock_denied(struct file_lock * fl,struct nfsd4_lock_denied * deny)6398*4882a593Smuzhiyun nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6399*4882a593Smuzhiyun {
6400*4882a593Smuzhiyun 	struct nfs4_lockowner *lo;
6401*4882a593Smuzhiyun 
6402*4882a593Smuzhiyun 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
6403*4882a593Smuzhiyun 		lo = (struct nfs4_lockowner *) fl->fl_owner;
6404*4882a593Smuzhiyun 		xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6405*4882a593Smuzhiyun 						GFP_KERNEL);
6406*4882a593Smuzhiyun 		if (!deny->ld_owner.data)
6407*4882a593Smuzhiyun 			/* We just don't care that much */
6408*4882a593Smuzhiyun 			goto nevermind;
6409*4882a593Smuzhiyun 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
6410*4882a593Smuzhiyun 	} else {
6411*4882a593Smuzhiyun nevermind:
6412*4882a593Smuzhiyun 		deny->ld_owner.len = 0;
6413*4882a593Smuzhiyun 		deny->ld_owner.data = NULL;
6414*4882a593Smuzhiyun 		deny->ld_clientid.cl_boot = 0;
6415*4882a593Smuzhiyun 		deny->ld_clientid.cl_id = 0;
6416*4882a593Smuzhiyun 	}
6417*4882a593Smuzhiyun 	deny->ld_start = fl->fl_start;
6418*4882a593Smuzhiyun 	deny->ld_length = NFS4_MAX_UINT64;
6419*4882a593Smuzhiyun 	if (fl->fl_end != NFS4_MAX_UINT64)
6420*4882a593Smuzhiyun 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
6421*4882a593Smuzhiyun 	deny->ld_type = NFS4_READ_LT;
6422*4882a593Smuzhiyun 	if (fl->fl_type != F_RDLCK)
6423*4882a593Smuzhiyun 		deny->ld_type = NFS4_WRITE_LT;
6424*4882a593Smuzhiyun }
6425*4882a593Smuzhiyun 
6426*4882a593Smuzhiyun static struct nfs4_lockowner *
find_lockowner_str_locked(struct nfs4_client * clp,struct xdr_netobj * owner)6427*4882a593Smuzhiyun find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
6428*4882a593Smuzhiyun {
6429*4882a593Smuzhiyun 	unsigned int strhashval = ownerstr_hashval(owner);
6430*4882a593Smuzhiyun 	struct nfs4_stateowner *so;
6431*4882a593Smuzhiyun 
6432*4882a593Smuzhiyun 	lockdep_assert_held(&clp->cl_lock);
6433*4882a593Smuzhiyun 
6434*4882a593Smuzhiyun 	list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6435*4882a593Smuzhiyun 			    so_strhash) {
6436*4882a593Smuzhiyun 		if (so->so_is_open_owner)
6437*4882a593Smuzhiyun 			continue;
6438*4882a593Smuzhiyun 		if (same_owner_str(so, owner))
6439*4882a593Smuzhiyun 			return lockowner(nfs4_get_stateowner(so));
6440*4882a593Smuzhiyun 	}
6441*4882a593Smuzhiyun 	return NULL;
6442*4882a593Smuzhiyun }
6443*4882a593Smuzhiyun 
6444*4882a593Smuzhiyun static struct nfs4_lockowner *
find_lockowner_str(struct nfs4_client * clp,struct xdr_netobj * owner)6445*4882a593Smuzhiyun find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
6446*4882a593Smuzhiyun {
6447*4882a593Smuzhiyun 	struct nfs4_lockowner *lo;
6448*4882a593Smuzhiyun 
6449*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
6450*4882a593Smuzhiyun 	lo = find_lockowner_str_locked(clp, owner);
6451*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6452*4882a593Smuzhiyun 	return lo;
6453*4882a593Smuzhiyun }
6454*4882a593Smuzhiyun 
nfs4_unhash_lockowner(struct nfs4_stateowner * sop)6455*4882a593Smuzhiyun static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6456*4882a593Smuzhiyun {
6457*4882a593Smuzhiyun 	unhash_lockowner_locked(lockowner(sop));
6458*4882a593Smuzhiyun }
6459*4882a593Smuzhiyun 
nfs4_free_lockowner(struct nfs4_stateowner * sop)6460*4882a593Smuzhiyun static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6461*4882a593Smuzhiyun {
6462*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = lockowner(sop);
6463*4882a593Smuzhiyun 
6464*4882a593Smuzhiyun 	kmem_cache_free(lockowner_slab, lo);
6465*4882a593Smuzhiyun }
6466*4882a593Smuzhiyun 
6467*4882a593Smuzhiyun static const struct nfs4_stateowner_operations lockowner_ops = {
6468*4882a593Smuzhiyun 	.so_unhash =	nfs4_unhash_lockowner,
6469*4882a593Smuzhiyun 	.so_free =	nfs4_free_lockowner,
6470*4882a593Smuzhiyun };
6471*4882a593Smuzhiyun 
6472*4882a593Smuzhiyun /*
6473*4882a593Smuzhiyun  * Alloc a lock owner structure.
6474*4882a593Smuzhiyun  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6475*4882a593Smuzhiyun  * occurred.
6476*4882a593Smuzhiyun  *
6477*4882a593Smuzhiyun  * strhashval = ownerstr_hashval
6478*4882a593Smuzhiyun  */
6479*4882a593Smuzhiyun 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)6480*4882a593Smuzhiyun alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6481*4882a593Smuzhiyun 			   struct nfs4_ol_stateid *open_stp,
6482*4882a593Smuzhiyun 			   struct nfsd4_lock *lock)
6483*4882a593Smuzhiyun {
6484*4882a593Smuzhiyun 	struct nfs4_lockowner *lo, *ret;
6485*4882a593Smuzhiyun 
6486*4882a593Smuzhiyun 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6487*4882a593Smuzhiyun 	if (!lo)
6488*4882a593Smuzhiyun 		return NULL;
6489*4882a593Smuzhiyun 	INIT_LIST_HEAD(&lo->lo_blocked);
6490*4882a593Smuzhiyun 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6491*4882a593Smuzhiyun 	lo->lo_owner.so_is_open_owner = 0;
6492*4882a593Smuzhiyun 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6493*4882a593Smuzhiyun 	lo->lo_owner.so_ops = &lockowner_ops;
6494*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
6495*4882a593Smuzhiyun 	ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
6496*4882a593Smuzhiyun 	if (ret == NULL) {
6497*4882a593Smuzhiyun 		list_add(&lo->lo_owner.so_strhash,
6498*4882a593Smuzhiyun 			 &clp->cl_ownerstr_hashtbl[strhashval]);
6499*4882a593Smuzhiyun 		ret = lo;
6500*4882a593Smuzhiyun 	} else
6501*4882a593Smuzhiyun 		nfs4_free_stateowner(&lo->lo_owner);
6502*4882a593Smuzhiyun 
6503*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6504*4882a593Smuzhiyun 	return ret;
6505*4882a593Smuzhiyun }
6506*4882a593Smuzhiyun 
6507*4882a593Smuzhiyun static struct nfs4_ol_stateid *
find_lock_stateid(const struct nfs4_lockowner * lo,const struct nfs4_ol_stateid * ost)6508*4882a593Smuzhiyun find_lock_stateid(const struct nfs4_lockowner *lo,
6509*4882a593Smuzhiyun 		  const struct nfs4_ol_stateid *ost)
6510*4882a593Smuzhiyun {
6511*4882a593Smuzhiyun 	struct nfs4_ol_stateid *lst;
6512*4882a593Smuzhiyun 
6513*4882a593Smuzhiyun 	lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
6514*4882a593Smuzhiyun 
6515*4882a593Smuzhiyun 	/* If ost is not hashed, ost->st_locks will not be valid */
6516*4882a593Smuzhiyun 	if (!nfs4_ol_stateid_unhashed(ost))
6517*4882a593Smuzhiyun 		list_for_each_entry(lst, &ost->st_locks, st_locks) {
6518*4882a593Smuzhiyun 			if (lst->st_stateowner == &lo->lo_owner) {
6519*4882a593Smuzhiyun 				refcount_inc(&lst->st_stid.sc_count);
6520*4882a593Smuzhiyun 				return lst;
6521*4882a593Smuzhiyun 			}
6522*4882a593Smuzhiyun 		}
6523*4882a593Smuzhiyun 	return NULL;
6524*4882a593Smuzhiyun }
6525*4882a593Smuzhiyun 
6526*4882a593Smuzhiyun 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)6527*4882a593Smuzhiyun init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6528*4882a593Smuzhiyun 		  struct nfs4_file *fp, struct inode *inode,
6529*4882a593Smuzhiyun 		  struct nfs4_ol_stateid *open_stp)
6530*4882a593Smuzhiyun {
6531*4882a593Smuzhiyun 	struct nfs4_client *clp = lo->lo_owner.so_client;
6532*4882a593Smuzhiyun 	struct nfs4_ol_stateid *retstp;
6533*4882a593Smuzhiyun 
6534*4882a593Smuzhiyun 	mutex_init(&stp->st_mutex);
6535*4882a593Smuzhiyun 	mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
6536*4882a593Smuzhiyun retry:
6537*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
6538*4882a593Smuzhiyun 	if (nfs4_ol_stateid_unhashed(open_stp))
6539*4882a593Smuzhiyun 		goto out_close;
6540*4882a593Smuzhiyun 	retstp = find_lock_stateid(lo, open_stp);
6541*4882a593Smuzhiyun 	if (retstp)
6542*4882a593Smuzhiyun 		goto out_found;
6543*4882a593Smuzhiyun 	refcount_inc(&stp->st_stid.sc_count);
6544*4882a593Smuzhiyun 	stp->st_stid.sc_type = NFS4_LOCK_STID;
6545*4882a593Smuzhiyun 	stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
6546*4882a593Smuzhiyun 	get_nfs4_file(fp);
6547*4882a593Smuzhiyun 	stp->st_stid.sc_file = fp;
6548*4882a593Smuzhiyun 	stp->st_access_bmap = 0;
6549*4882a593Smuzhiyun 	stp->st_deny_bmap = open_stp->st_deny_bmap;
6550*4882a593Smuzhiyun 	stp->st_openstp = open_stp;
6551*4882a593Smuzhiyun 	spin_lock(&fp->fi_lock);
6552*4882a593Smuzhiyun 	list_add(&stp->st_locks, &open_stp->st_locks);
6553*4882a593Smuzhiyun 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
6554*4882a593Smuzhiyun 	list_add(&stp->st_perfile, &fp->fi_stateids);
6555*4882a593Smuzhiyun 	spin_unlock(&fp->fi_lock);
6556*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6557*4882a593Smuzhiyun 	return stp;
6558*4882a593Smuzhiyun out_found:
6559*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6560*4882a593Smuzhiyun 	if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6561*4882a593Smuzhiyun 		nfs4_put_stid(&retstp->st_stid);
6562*4882a593Smuzhiyun 		goto retry;
6563*4882a593Smuzhiyun 	}
6564*4882a593Smuzhiyun 	/* To keep mutex tracking happy */
6565*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
6566*4882a593Smuzhiyun 	return retstp;
6567*4882a593Smuzhiyun out_close:
6568*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6569*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
6570*4882a593Smuzhiyun 	return NULL;
6571*4882a593Smuzhiyun }
6572*4882a593Smuzhiyun 
6573*4882a593Smuzhiyun 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)6574*4882a593Smuzhiyun find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6575*4882a593Smuzhiyun 			    struct inode *inode, struct nfs4_ol_stateid *ost,
6576*4882a593Smuzhiyun 			    bool *new)
6577*4882a593Smuzhiyun {
6578*4882a593Smuzhiyun 	struct nfs4_stid *ns = NULL;
6579*4882a593Smuzhiyun 	struct nfs4_ol_stateid *lst;
6580*4882a593Smuzhiyun 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6581*4882a593Smuzhiyun 	struct nfs4_client *clp = oo->oo_owner.so_client;
6582*4882a593Smuzhiyun 
6583*4882a593Smuzhiyun 	*new = false;
6584*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
6585*4882a593Smuzhiyun 	lst = find_lock_stateid(lo, ost);
6586*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
6587*4882a593Smuzhiyun 	if (lst != NULL) {
6588*4882a593Smuzhiyun 		if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6589*4882a593Smuzhiyun 			goto out;
6590*4882a593Smuzhiyun 		nfs4_put_stid(&lst->st_stid);
6591*4882a593Smuzhiyun 	}
6592*4882a593Smuzhiyun 	ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6593*4882a593Smuzhiyun 	if (ns == NULL)
6594*4882a593Smuzhiyun 		return NULL;
6595*4882a593Smuzhiyun 
6596*4882a593Smuzhiyun 	lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6597*4882a593Smuzhiyun 	if (lst == openlockstateid(ns))
6598*4882a593Smuzhiyun 		*new = true;
6599*4882a593Smuzhiyun 	else
6600*4882a593Smuzhiyun 		nfs4_put_stid(ns);
6601*4882a593Smuzhiyun out:
6602*4882a593Smuzhiyun 	return lst;
6603*4882a593Smuzhiyun }
6604*4882a593Smuzhiyun 
6605*4882a593Smuzhiyun static int
check_lock_length(u64 offset,u64 length)6606*4882a593Smuzhiyun check_lock_length(u64 offset, u64 length)
6607*4882a593Smuzhiyun {
6608*4882a593Smuzhiyun 	return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6609*4882a593Smuzhiyun 		(length > ~offset)));
6610*4882a593Smuzhiyun }
6611*4882a593Smuzhiyun 
get_lock_access(struct nfs4_ol_stateid * lock_stp,u32 access)6612*4882a593Smuzhiyun static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
6613*4882a593Smuzhiyun {
6614*4882a593Smuzhiyun 	struct nfs4_file *fp = lock_stp->st_stid.sc_file;
6615*4882a593Smuzhiyun 
6616*4882a593Smuzhiyun 	lockdep_assert_held(&fp->fi_lock);
6617*4882a593Smuzhiyun 
6618*4882a593Smuzhiyun 	if (test_access(access, lock_stp))
6619*4882a593Smuzhiyun 		return;
6620*4882a593Smuzhiyun 	__nfs4_file_get_access(fp, access);
6621*4882a593Smuzhiyun 	set_access(access, lock_stp);
6622*4882a593Smuzhiyun }
6623*4882a593Smuzhiyun 
6624*4882a593Smuzhiyun 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)6625*4882a593Smuzhiyun lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6626*4882a593Smuzhiyun 			    struct nfs4_ol_stateid *ost,
6627*4882a593Smuzhiyun 			    struct nfsd4_lock *lock,
6628*4882a593Smuzhiyun 			    struct nfs4_ol_stateid **plst, bool *new)
6629*4882a593Smuzhiyun {
6630*4882a593Smuzhiyun 	__be32 status;
6631*4882a593Smuzhiyun 	struct nfs4_file *fi = ost->st_stid.sc_file;
6632*4882a593Smuzhiyun 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6633*4882a593Smuzhiyun 	struct nfs4_client *cl = oo->oo_owner.so_client;
6634*4882a593Smuzhiyun 	struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
6635*4882a593Smuzhiyun 	struct nfs4_lockowner *lo;
6636*4882a593Smuzhiyun 	struct nfs4_ol_stateid *lst;
6637*4882a593Smuzhiyun 	unsigned int strhashval;
6638*4882a593Smuzhiyun 
6639*4882a593Smuzhiyun 	lo = find_lockowner_str(cl, &lock->lk_new_owner);
6640*4882a593Smuzhiyun 	if (!lo) {
6641*4882a593Smuzhiyun 		strhashval = ownerstr_hashval(&lock->lk_new_owner);
6642*4882a593Smuzhiyun 		lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6643*4882a593Smuzhiyun 		if (lo == NULL)
6644*4882a593Smuzhiyun 			return nfserr_jukebox;
6645*4882a593Smuzhiyun 	} else {
6646*4882a593Smuzhiyun 		/* with an existing lockowner, seqids must be the same */
6647*4882a593Smuzhiyun 		status = nfserr_bad_seqid;
6648*4882a593Smuzhiyun 		if (!cstate->minorversion &&
6649*4882a593Smuzhiyun 		    lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
6650*4882a593Smuzhiyun 			goto out;
6651*4882a593Smuzhiyun 	}
6652*4882a593Smuzhiyun 
6653*4882a593Smuzhiyun 	lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6654*4882a593Smuzhiyun 	if (lst == NULL) {
6655*4882a593Smuzhiyun 		status = nfserr_jukebox;
6656*4882a593Smuzhiyun 		goto out;
6657*4882a593Smuzhiyun 	}
6658*4882a593Smuzhiyun 
6659*4882a593Smuzhiyun 	status = nfs_ok;
6660*4882a593Smuzhiyun 	*plst = lst;
6661*4882a593Smuzhiyun out:
6662*4882a593Smuzhiyun 	nfs4_put_stateowner(&lo->lo_owner);
6663*4882a593Smuzhiyun 	return status;
6664*4882a593Smuzhiyun }
6665*4882a593Smuzhiyun 
6666*4882a593Smuzhiyun /*
6667*4882a593Smuzhiyun  *  LOCK operation
6668*4882a593Smuzhiyun  */
6669*4882a593Smuzhiyun __be32
nfsd4_lock(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6670*4882a593Smuzhiyun nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6671*4882a593Smuzhiyun 	   union nfsd4_op_u *u)
6672*4882a593Smuzhiyun {
6673*4882a593Smuzhiyun 	struct nfsd4_lock *lock = &u->lock;
6674*4882a593Smuzhiyun 	struct nfs4_openowner *open_sop = NULL;
6675*4882a593Smuzhiyun 	struct nfs4_lockowner *lock_sop = NULL;
6676*4882a593Smuzhiyun 	struct nfs4_ol_stateid *lock_stp = NULL;
6677*4882a593Smuzhiyun 	struct nfs4_ol_stateid *open_stp = NULL;
6678*4882a593Smuzhiyun 	struct nfs4_file *fp;
6679*4882a593Smuzhiyun 	struct nfsd_file *nf = NULL;
6680*4882a593Smuzhiyun 	struct nfsd4_blocked_lock *nbl = NULL;
6681*4882a593Smuzhiyun 	struct file_lock *file_lock = NULL;
6682*4882a593Smuzhiyun 	struct file_lock *conflock = NULL;
6683*4882a593Smuzhiyun 	__be32 status = 0;
6684*4882a593Smuzhiyun 	int lkflg;
6685*4882a593Smuzhiyun 	int err;
6686*4882a593Smuzhiyun 	bool new = false;
6687*4882a593Smuzhiyun 	unsigned char fl_type;
6688*4882a593Smuzhiyun 	unsigned int fl_flags = FL_POSIX;
6689*4882a593Smuzhiyun 	struct net *net = SVC_NET(rqstp);
6690*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6691*4882a593Smuzhiyun 
6692*4882a593Smuzhiyun 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6693*4882a593Smuzhiyun 		(long long) lock->lk_offset,
6694*4882a593Smuzhiyun 		(long long) lock->lk_length);
6695*4882a593Smuzhiyun 
6696*4882a593Smuzhiyun 	if (check_lock_length(lock->lk_offset, lock->lk_length))
6697*4882a593Smuzhiyun 		 return nfserr_inval;
6698*4882a593Smuzhiyun 
6699*4882a593Smuzhiyun 	if ((status = fh_verify(rqstp, &cstate->current_fh,
6700*4882a593Smuzhiyun 				S_IFREG, NFSD_MAY_LOCK))) {
6701*4882a593Smuzhiyun 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
6702*4882a593Smuzhiyun 		return status;
6703*4882a593Smuzhiyun 	}
6704*4882a593Smuzhiyun 
6705*4882a593Smuzhiyun 	if (lock->lk_is_new) {
6706*4882a593Smuzhiyun 		if (nfsd4_has_session(cstate))
6707*4882a593Smuzhiyun 			/* See rfc 5661 18.10.3: given clientid is ignored: */
6708*4882a593Smuzhiyun 			memcpy(&lock->lk_new_clientid,
6709*4882a593Smuzhiyun 				&cstate->session->se_client->cl_clientid,
6710*4882a593Smuzhiyun 				sizeof(clientid_t));
6711*4882a593Smuzhiyun 
6712*4882a593Smuzhiyun 		status = nfserr_stale_clientid;
6713*4882a593Smuzhiyun 		if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
6714*4882a593Smuzhiyun 			goto out;
6715*4882a593Smuzhiyun 
6716*4882a593Smuzhiyun 		/* validate and update open stateid and open seqid */
6717*4882a593Smuzhiyun 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
6718*4882a593Smuzhiyun 				        lock->lk_new_open_seqid,
6719*4882a593Smuzhiyun 		                        &lock->lk_new_open_stateid,
6720*4882a593Smuzhiyun 					&open_stp, nn);
6721*4882a593Smuzhiyun 		if (status)
6722*4882a593Smuzhiyun 			goto out;
6723*4882a593Smuzhiyun 		mutex_unlock(&open_stp->st_mutex);
6724*4882a593Smuzhiyun 		open_sop = openowner(open_stp->st_stateowner);
6725*4882a593Smuzhiyun 		status = nfserr_bad_stateid;
6726*4882a593Smuzhiyun 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
6727*4882a593Smuzhiyun 						&lock->lk_new_clientid))
6728*4882a593Smuzhiyun 			goto out;
6729*4882a593Smuzhiyun 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
6730*4882a593Smuzhiyun 							&lock_stp, &new);
6731*4882a593Smuzhiyun 	} else {
6732*4882a593Smuzhiyun 		status = nfs4_preprocess_seqid_op(cstate,
6733*4882a593Smuzhiyun 				       lock->lk_old_lock_seqid,
6734*4882a593Smuzhiyun 				       &lock->lk_old_lock_stateid,
6735*4882a593Smuzhiyun 				       NFS4_LOCK_STID, &lock_stp, nn);
6736*4882a593Smuzhiyun 	}
6737*4882a593Smuzhiyun 	if (status)
6738*4882a593Smuzhiyun 		goto out;
6739*4882a593Smuzhiyun 	lock_sop = lockowner(lock_stp->st_stateowner);
6740*4882a593Smuzhiyun 
6741*4882a593Smuzhiyun 	lkflg = setlkflg(lock->lk_type);
6742*4882a593Smuzhiyun 	status = nfs4_check_openmode(lock_stp, lkflg);
6743*4882a593Smuzhiyun 	if (status)
6744*4882a593Smuzhiyun 		goto out;
6745*4882a593Smuzhiyun 
6746*4882a593Smuzhiyun 	status = nfserr_grace;
6747*4882a593Smuzhiyun 	if (locks_in_grace(net) && !lock->lk_reclaim)
6748*4882a593Smuzhiyun 		goto out;
6749*4882a593Smuzhiyun 	status = nfserr_no_grace;
6750*4882a593Smuzhiyun 	if (!locks_in_grace(net) && lock->lk_reclaim)
6751*4882a593Smuzhiyun 		goto out;
6752*4882a593Smuzhiyun 
6753*4882a593Smuzhiyun 	fp = lock_stp->st_stid.sc_file;
6754*4882a593Smuzhiyun 	switch (lock->lk_type) {
6755*4882a593Smuzhiyun 		case NFS4_READW_LT:
6756*4882a593Smuzhiyun 			if (nfsd4_has_session(cstate))
6757*4882a593Smuzhiyun 				fl_flags |= FL_SLEEP;
6758*4882a593Smuzhiyun 			fallthrough;
6759*4882a593Smuzhiyun 		case NFS4_READ_LT:
6760*4882a593Smuzhiyun 			spin_lock(&fp->fi_lock);
6761*4882a593Smuzhiyun 			nf = find_readable_file_locked(fp);
6762*4882a593Smuzhiyun 			if (nf)
6763*4882a593Smuzhiyun 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
6764*4882a593Smuzhiyun 			spin_unlock(&fp->fi_lock);
6765*4882a593Smuzhiyun 			fl_type = F_RDLCK;
6766*4882a593Smuzhiyun 			break;
6767*4882a593Smuzhiyun 		case NFS4_WRITEW_LT:
6768*4882a593Smuzhiyun 			if (nfsd4_has_session(cstate))
6769*4882a593Smuzhiyun 				fl_flags |= FL_SLEEP;
6770*4882a593Smuzhiyun 			fallthrough;
6771*4882a593Smuzhiyun 		case NFS4_WRITE_LT:
6772*4882a593Smuzhiyun 			spin_lock(&fp->fi_lock);
6773*4882a593Smuzhiyun 			nf = find_writeable_file_locked(fp);
6774*4882a593Smuzhiyun 			if (nf)
6775*4882a593Smuzhiyun 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
6776*4882a593Smuzhiyun 			spin_unlock(&fp->fi_lock);
6777*4882a593Smuzhiyun 			fl_type = F_WRLCK;
6778*4882a593Smuzhiyun 			break;
6779*4882a593Smuzhiyun 		default:
6780*4882a593Smuzhiyun 			status = nfserr_inval;
6781*4882a593Smuzhiyun 		goto out;
6782*4882a593Smuzhiyun 	}
6783*4882a593Smuzhiyun 
6784*4882a593Smuzhiyun 	if (!nf) {
6785*4882a593Smuzhiyun 		status = nfserr_openmode;
6786*4882a593Smuzhiyun 		goto out;
6787*4882a593Smuzhiyun 	}
6788*4882a593Smuzhiyun 
6789*4882a593Smuzhiyun 	nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6790*4882a593Smuzhiyun 	if (!nbl) {
6791*4882a593Smuzhiyun 		dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6792*4882a593Smuzhiyun 		status = nfserr_jukebox;
6793*4882a593Smuzhiyun 		goto out;
6794*4882a593Smuzhiyun 	}
6795*4882a593Smuzhiyun 
6796*4882a593Smuzhiyun 	file_lock = &nbl->nbl_lock;
6797*4882a593Smuzhiyun 	file_lock->fl_type = fl_type;
6798*4882a593Smuzhiyun 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
6799*4882a593Smuzhiyun 	file_lock->fl_pid = current->tgid;
6800*4882a593Smuzhiyun 	file_lock->fl_file = nf->nf_file;
6801*4882a593Smuzhiyun 	file_lock->fl_flags = fl_flags;
6802*4882a593Smuzhiyun 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
6803*4882a593Smuzhiyun 	file_lock->fl_start = lock->lk_offset;
6804*4882a593Smuzhiyun 	file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6805*4882a593Smuzhiyun 	nfs4_transform_lock_offset(file_lock);
6806*4882a593Smuzhiyun 
6807*4882a593Smuzhiyun 	conflock = locks_alloc_lock();
6808*4882a593Smuzhiyun 	if (!conflock) {
6809*4882a593Smuzhiyun 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6810*4882a593Smuzhiyun 		status = nfserr_jukebox;
6811*4882a593Smuzhiyun 		goto out;
6812*4882a593Smuzhiyun 	}
6813*4882a593Smuzhiyun 
6814*4882a593Smuzhiyun 	if (fl_flags & FL_SLEEP) {
6815*4882a593Smuzhiyun 		nbl->nbl_time = ktime_get_boottime_seconds();
6816*4882a593Smuzhiyun 		spin_lock(&nn->blocked_locks_lock);
6817*4882a593Smuzhiyun 		list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
6818*4882a593Smuzhiyun 		list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
6819*4882a593Smuzhiyun 		spin_unlock(&nn->blocked_locks_lock);
6820*4882a593Smuzhiyun 	}
6821*4882a593Smuzhiyun 
6822*4882a593Smuzhiyun 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
6823*4882a593Smuzhiyun 	switch (err) {
6824*4882a593Smuzhiyun 	case 0: /* success! */
6825*4882a593Smuzhiyun 		nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
6826*4882a593Smuzhiyun 		status = 0;
6827*4882a593Smuzhiyun 		if (lock->lk_reclaim)
6828*4882a593Smuzhiyun 			nn->somebody_reclaimed = true;
6829*4882a593Smuzhiyun 		break;
6830*4882a593Smuzhiyun 	case FILE_LOCK_DEFERRED:
6831*4882a593Smuzhiyun 		nbl = NULL;
6832*4882a593Smuzhiyun 		fallthrough;
6833*4882a593Smuzhiyun 	case -EAGAIN:		/* conflock holds conflicting lock */
6834*4882a593Smuzhiyun 		status = nfserr_denied;
6835*4882a593Smuzhiyun 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6836*4882a593Smuzhiyun 		nfs4_set_lock_denied(conflock, &lock->lk_denied);
6837*4882a593Smuzhiyun 		break;
6838*4882a593Smuzhiyun 	case -EDEADLK:
6839*4882a593Smuzhiyun 		status = nfserr_deadlock;
6840*4882a593Smuzhiyun 		break;
6841*4882a593Smuzhiyun 	default:
6842*4882a593Smuzhiyun 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
6843*4882a593Smuzhiyun 		status = nfserrno(err);
6844*4882a593Smuzhiyun 		break;
6845*4882a593Smuzhiyun 	}
6846*4882a593Smuzhiyun out:
6847*4882a593Smuzhiyun 	if (nbl) {
6848*4882a593Smuzhiyun 		/* dequeue it if we queued it before */
6849*4882a593Smuzhiyun 		if (fl_flags & FL_SLEEP) {
6850*4882a593Smuzhiyun 			spin_lock(&nn->blocked_locks_lock);
6851*4882a593Smuzhiyun 			list_del_init(&nbl->nbl_list);
6852*4882a593Smuzhiyun 			list_del_init(&nbl->nbl_lru);
6853*4882a593Smuzhiyun 			spin_unlock(&nn->blocked_locks_lock);
6854*4882a593Smuzhiyun 		}
6855*4882a593Smuzhiyun 		free_blocked_lock(nbl);
6856*4882a593Smuzhiyun 	}
6857*4882a593Smuzhiyun 	if (nf)
6858*4882a593Smuzhiyun 		nfsd_file_put(nf);
6859*4882a593Smuzhiyun 	if (lock_stp) {
6860*4882a593Smuzhiyun 		/* Bump seqid manually if the 4.0 replay owner is openowner */
6861*4882a593Smuzhiyun 		if (cstate->replay_owner &&
6862*4882a593Smuzhiyun 		    cstate->replay_owner != &lock_sop->lo_owner &&
6863*4882a593Smuzhiyun 		    seqid_mutating_err(ntohl(status)))
6864*4882a593Smuzhiyun 			lock_sop->lo_owner.so_seqid++;
6865*4882a593Smuzhiyun 
6866*4882a593Smuzhiyun 		/*
6867*4882a593Smuzhiyun 		 * If this is a new, never-before-used stateid, and we are
6868*4882a593Smuzhiyun 		 * returning an error, then just go ahead and release it.
6869*4882a593Smuzhiyun 		 */
6870*4882a593Smuzhiyun 		if (status && new)
6871*4882a593Smuzhiyun 			release_lock_stateid(lock_stp);
6872*4882a593Smuzhiyun 
6873*4882a593Smuzhiyun 		mutex_unlock(&lock_stp->st_mutex);
6874*4882a593Smuzhiyun 
6875*4882a593Smuzhiyun 		nfs4_put_stid(&lock_stp->st_stid);
6876*4882a593Smuzhiyun 	}
6877*4882a593Smuzhiyun 	if (open_stp)
6878*4882a593Smuzhiyun 		nfs4_put_stid(&open_stp->st_stid);
6879*4882a593Smuzhiyun 	nfsd4_bump_seqid(cstate, status);
6880*4882a593Smuzhiyun 	if (conflock)
6881*4882a593Smuzhiyun 		locks_free_lock(conflock);
6882*4882a593Smuzhiyun 	return status;
6883*4882a593Smuzhiyun }
6884*4882a593Smuzhiyun 
6885*4882a593Smuzhiyun /*
6886*4882a593Smuzhiyun  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6887*4882a593Smuzhiyun  * so we do a temporary open here just to get an open file to pass to
6888*4882a593Smuzhiyun  * vfs_test_lock.
6889*4882a593Smuzhiyun  */
nfsd_test_lock(struct svc_rqst * rqstp,struct svc_fh * fhp,struct file_lock * lock)6890*4882a593Smuzhiyun static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
6891*4882a593Smuzhiyun {
6892*4882a593Smuzhiyun 	struct nfsd_file *nf;
6893*4882a593Smuzhiyun 	__be32 err;
6894*4882a593Smuzhiyun 
6895*4882a593Smuzhiyun 	err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
6896*4882a593Smuzhiyun 	if (err)
6897*4882a593Smuzhiyun 		return err;
6898*4882a593Smuzhiyun 	fh_lock(fhp); /* to block new leases till after test_lock: */
6899*4882a593Smuzhiyun 	err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode,
6900*4882a593Smuzhiyun 							NFSD_MAY_READ));
6901*4882a593Smuzhiyun 	if (err)
6902*4882a593Smuzhiyun 		goto out;
6903*4882a593Smuzhiyun 	lock->fl_file = nf->nf_file;
6904*4882a593Smuzhiyun 	err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6905*4882a593Smuzhiyun 	lock->fl_file = NULL;
6906*4882a593Smuzhiyun out:
6907*4882a593Smuzhiyun 	fh_unlock(fhp);
6908*4882a593Smuzhiyun 	nfsd_file_put(nf);
6909*4882a593Smuzhiyun 	return err;
6910*4882a593Smuzhiyun }
6911*4882a593Smuzhiyun 
6912*4882a593Smuzhiyun /*
6913*4882a593Smuzhiyun  * LOCKT operation
6914*4882a593Smuzhiyun  */
6915*4882a593Smuzhiyun __be32
nfsd4_lockt(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6916*4882a593Smuzhiyun nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6917*4882a593Smuzhiyun 	    union nfsd4_op_u *u)
6918*4882a593Smuzhiyun {
6919*4882a593Smuzhiyun 	struct nfsd4_lockt *lockt = &u->lockt;
6920*4882a593Smuzhiyun 	struct file_lock *file_lock = NULL;
6921*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = NULL;
6922*4882a593Smuzhiyun 	__be32 status;
6923*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6924*4882a593Smuzhiyun 
6925*4882a593Smuzhiyun 	if (locks_in_grace(SVC_NET(rqstp)))
6926*4882a593Smuzhiyun 		return nfserr_grace;
6927*4882a593Smuzhiyun 
6928*4882a593Smuzhiyun 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6929*4882a593Smuzhiyun 		 return nfserr_inval;
6930*4882a593Smuzhiyun 
6931*4882a593Smuzhiyun 	if (!nfsd4_has_session(cstate)) {
6932*4882a593Smuzhiyun 		status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6933*4882a593Smuzhiyun 					 false);
6934*4882a593Smuzhiyun 		if (status)
6935*4882a593Smuzhiyun 			goto out;
6936*4882a593Smuzhiyun 	}
6937*4882a593Smuzhiyun 
6938*4882a593Smuzhiyun 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
6939*4882a593Smuzhiyun 		goto out;
6940*4882a593Smuzhiyun 
6941*4882a593Smuzhiyun 	file_lock = locks_alloc_lock();
6942*4882a593Smuzhiyun 	if (!file_lock) {
6943*4882a593Smuzhiyun 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6944*4882a593Smuzhiyun 		status = nfserr_jukebox;
6945*4882a593Smuzhiyun 		goto out;
6946*4882a593Smuzhiyun 	}
6947*4882a593Smuzhiyun 
6948*4882a593Smuzhiyun 	switch (lockt->lt_type) {
6949*4882a593Smuzhiyun 		case NFS4_READ_LT:
6950*4882a593Smuzhiyun 		case NFS4_READW_LT:
6951*4882a593Smuzhiyun 			file_lock->fl_type = F_RDLCK;
6952*4882a593Smuzhiyun 			break;
6953*4882a593Smuzhiyun 		case NFS4_WRITE_LT:
6954*4882a593Smuzhiyun 		case NFS4_WRITEW_LT:
6955*4882a593Smuzhiyun 			file_lock->fl_type = F_WRLCK;
6956*4882a593Smuzhiyun 			break;
6957*4882a593Smuzhiyun 		default:
6958*4882a593Smuzhiyun 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6959*4882a593Smuzhiyun 			status = nfserr_inval;
6960*4882a593Smuzhiyun 			goto out;
6961*4882a593Smuzhiyun 	}
6962*4882a593Smuzhiyun 
6963*4882a593Smuzhiyun 	lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
6964*4882a593Smuzhiyun 	if (lo)
6965*4882a593Smuzhiyun 		file_lock->fl_owner = (fl_owner_t)lo;
6966*4882a593Smuzhiyun 	file_lock->fl_pid = current->tgid;
6967*4882a593Smuzhiyun 	file_lock->fl_flags = FL_POSIX;
6968*4882a593Smuzhiyun 
6969*4882a593Smuzhiyun 	file_lock->fl_start = lockt->lt_offset;
6970*4882a593Smuzhiyun 	file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
6971*4882a593Smuzhiyun 
6972*4882a593Smuzhiyun 	nfs4_transform_lock_offset(file_lock);
6973*4882a593Smuzhiyun 
6974*4882a593Smuzhiyun 	status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
6975*4882a593Smuzhiyun 	if (status)
6976*4882a593Smuzhiyun 		goto out;
6977*4882a593Smuzhiyun 
6978*4882a593Smuzhiyun 	if (file_lock->fl_type != F_UNLCK) {
6979*4882a593Smuzhiyun 		status = nfserr_denied;
6980*4882a593Smuzhiyun 		nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
6981*4882a593Smuzhiyun 	}
6982*4882a593Smuzhiyun out:
6983*4882a593Smuzhiyun 	if (lo)
6984*4882a593Smuzhiyun 		nfs4_put_stateowner(&lo->lo_owner);
6985*4882a593Smuzhiyun 	if (file_lock)
6986*4882a593Smuzhiyun 		locks_free_lock(file_lock);
6987*4882a593Smuzhiyun 	return status;
6988*4882a593Smuzhiyun }
6989*4882a593Smuzhiyun 
6990*4882a593Smuzhiyun __be32
nfsd4_locku(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)6991*4882a593Smuzhiyun nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
6992*4882a593Smuzhiyun 	    union nfsd4_op_u *u)
6993*4882a593Smuzhiyun {
6994*4882a593Smuzhiyun 	struct nfsd4_locku *locku = &u->locku;
6995*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
6996*4882a593Smuzhiyun 	struct nfsd_file *nf = NULL;
6997*4882a593Smuzhiyun 	struct file_lock *file_lock = NULL;
6998*4882a593Smuzhiyun 	__be32 status;
6999*4882a593Smuzhiyun 	int err;
7000*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7001*4882a593Smuzhiyun 
7002*4882a593Smuzhiyun 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
7003*4882a593Smuzhiyun 		(long long) locku->lu_offset,
7004*4882a593Smuzhiyun 		(long long) locku->lu_length);
7005*4882a593Smuzhiyun 
7006*4882a593Smuzhiyun 	if (check_lock_length(locku->lu_offset, locku->lu_length))
7007*4882a593Smuzhiyun 		 return nfserr_inval;
7008*4882a593Smuzhiyun 
7009*4882a593Smuzhiyun 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
7010*4882a593Smuzhiyun 					&locku->lu_stateid, NFS4_LOCK_STID,
7011*4882a593Smuzhiyun 					&stp, nn);
7012*4882a593Smuzhiyun 	if (status)
7013*4882a593Smuzhiyun 		goto out;
7014*4882a593Smuzhiyun 	nf = find_any_file(stp->st_stid.sc_file);
7015*4882a593Smuzhiyun 	if (!nf) {
7016*4882a593Smuzhiyun 		status = nfserr_lock_range;
7017*4882a593Smuzhiyun 		goto put_stateid;
7018*4882a593Smuzhiyun 	}
7019*4882a593Smuzhiyun 	file_lock = locks_alloc_lock();
7020*4882a593Smuzhiyun 	if (!file_lock) {
7021*4882a593Smuzhiyun 		dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7022*4882a593Smuzhiyun 		status = nfserr_jukebox;
7023*4882a593Smuzhiyun 		goto put_file;
7024*4882a593Smuzhiyun 	}
7025*4882a593Smuzhiyun 
7026*4882a593Smuzhiyun 	file_lock->fl_type = F_UNLCK;
7027*4882a593Smuzhiyun 	file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
7028*4882a593Smuzhiyun 	file_lock->fl_pid = current->tgid;
7029*4882a593Smuzhiyun 	file_lock->fl_file = nf->nf_file;
7030*4882a593Smuzhiyun 	file_lock->fl_flags = FL_POSIX;
7031*4882a593Smuzhiyun 	file_lock->fl_lmops = &nfsd_posix_mng_ops;
7032*4882a593Smuzhiyun 	file_lock->fl_start = locku->lu_offset;
7033*4882a593Smuzhiyun 
7034*4882a593Smuzhiyun 	file_lock->fl_end = last_byte_offset(locku->lu_offset,
7035*4882a593Smuzhiyun 						locku->lu_length);
7036*4882a593Smuzhiyun 	nfs4_transform_lock_offset(file_lock);
7037*4882a593Smuzhiyun 
7038*4882a593Smuzhiyun 	err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
7039*4882a593Smuzhiyun 	if (err) {
7040*4882a593Smuzhiyun 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
7041*4882a593Smuzhiyun 		goto out_nfserr;
7042*4882a593Smuzhiyun 	}
7043*4882a593Smuzhiyun 	nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
7044*4882a593Smuzhiyun put_file:
7045*4882a593Smuzhiyun 	nfsd_file_put(nf);
7046*4882a593Smuzhiyun put_stateid:
7047*4882a593Smuzhiyun 	mutex_unlock(&stp->st_mutex);
7048*4882a593Smuzhiyun 	nfs4_put_stid(&stp->st_stid);
7049*4882a593Smuzhiyun out:
7050*4882a593Smuzhiyun 	nfsd4_bump_seqid(cstate, status);
7051*4882a593Smuzhiyun 	if (file_lock)
7052*4882a593Smuzhiyun 		locks_free_lock(file_lock);
7053*4882a593Smuzhiyun 	return status;
7054*4882a593Smuzhiyun 
7055*4882a593Smuzhiyun out_nfserr:
7056*4882a593Smuzhiyun 	status = nfserrno(err);
7057*4882a593Smuzhiyun 	goto put_file;
7058*4882a593Smuzhiyun }
7059*4882a593Smuzhiyun 
7060*4882a593Smuzhiyun /*
7061*4882a593Smuzhiyun  * returns
7062*4882a593Smuzhiyun  * 	true:  locks held by lockowner
7063*4882a593Smuzhiyun  * 	false: no locks held by lockowner
7064*4882a593Smuzhiyun  */
7065*4882a593Smuzhiyun static bool
check_for_locks(struct nfs4_file * fp,struct nfs4_lockowner * lowner)7066*4882a593Smuzhiyun check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
7067*4882a593Smuzhiyun {
7068*4882a593Smuzhiyun 	struct file_lock *fl;
7069*4882a593Smuzhiyun 	int status = false;
7070*4882a593Smuzhiyun 	struct nfsd_file *nf = find_any_file(fp);
7071*4882a593Smuzhiyun 	struct inode *inode;
7072*4882a593Smuzhiyun 	struct file_lock_context *flctx;
7073*4882a593Smuzhiyun 
7074*4882a593Smuzhiyun 	if (!nf) {
7075*4882a593Smuzhiyun 		/* Any valid lock stateid should have some sort of access */
7076*4882a593Smuzhiyun 		WARN_ON_ONCE(1);
7077*4882a593Smuzhiyun 		return status;
7078*4882a593Smuzhiyun 	}
7079*4882a593Smuzhiyun 
7080*4882a593Smuzhiyun 	inode = locks_inode(nf->nf_file);
7081*4882a593Smuzhiyun 	flctx = inode->i_flctx;
7082*4882a593Smuzhiyun 
7083*4882a593Smuzhiyun 	if (flctx && !list_empty_careful(&flctx->flc_posix)) {
7084*4882a593Smuzhiyun 		spin_lock(&flctx->flc_lock);
7085*4882a593Smuzhiyun 		list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7086*4882a593Smuzhiyun 			if (fl->fl_owner == (fl_owner_t)lowner) {
7087*4882a593Smuzhiyun 				status = true;
7088*4882a593Smuzhiyun 				break;
7089*4882a593Smuzhiyun 			}
7090*4882a593Smuzhiyun 		}
7091*4882a593Smuzhiyun 		spin_unlock(&flctx->flc_lock);
7092*4882a593Smuzhiyun 	}
7093*4882a593Smuzhiyun 	nfsd_file_put(nf);
7094*4882a593Smuzhiyun 	return status;
7095*4882a593Smuzhiyun }
7096*4882a593Smuzhiyun 
7097*4882a593Smuzhiyun __be32
nfsd4_release_lockowner(struct svc_rqst * rqstp,struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7098*4882a593Smuzhiyun nfsd4_release_lockowner(struct svc_rqst *rqstp,
7099*4882a593Smuzhiyun 			struct nfsd4_compound_state *cstate,
7100*4882a593Smuzhiyun 			union nfsd4_op_u *u)
7101*4882a593Smuzhiyun {
7102*4882a593Smuzhiyun 	struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
7103*4882a593Smuzhiyun 	clientid_t *clid = &rlockowner->rl_clientid;
7104*4882a593Smuzhiyun 	struct nfs4_stateowner *sop;
7105*4882a593Smuzhiyun 	struct nfs4_lockowner *lo = NULL;
7106*4882a593Smuzhiyun 	struct nfs4_ol_stateid *stp;
7107*4882a593Smuzhiyun 	struct xdr_netobj *owner = &rlockowner->rl_owner;
7108*4882a593Smuzhiyun 	unsigned int hashval = ownerstr_hashval(owner);
7109*4882a593Smuzhiyun 	__be32 status;
7110*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
7111*4882a593Smuzhiyun 	struct nfs4_client *clp;
7112*4882a593Smuzhiyun 	LIST_HEAD (reaplist);
7113*4882a593Smuzhiyun 
7114*4882a593Smuzhiyun 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7115*4882a593Smuzhiyun 		clid->cl_boot, clid->cl_id);
7116*4882a593Smuzhiyun 
7117*4882a593Smuzhiyun 	status = lookup_clientid(clid, cstate, nn, false);
7118*4882a593Smuzhiyun 	if (status)
7119*4882a593Smuzhiyun 		return status;
7120*4882a593Smuzhiyun 
7121*4882a593Smuzhiyun 	clp = cstate->clp;
7122*4882a593Smuzhiyun 	/* Find the matching lock stateowner */
7123*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
7124*4882a593Smuzhiyun 	list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
7125*4882a593Smuzhiyun 			    so_strhash) {
7126*4882a593Smuzhiyun 
7127*4882a593Smuzhiyun 		if (sop->so_is_open_owner || !same_owner_str(sop, owner))
7128*4882a593Smuzhiyun 			continue;
7129*4882a593Smuzhiyun 
7130*4882a593Smuzhiyun 		if (atomic_read(&sop->so_count) != 1) {
7131*4882a593Smuzhiyun 			spin_unlock(&clp->cl_lock);
7132*4882a593Smuzhiyun 			return nfserr_locks_held;
7133*4882a593Smuzhiyun 		}
7134*4882a593Smuzhiyun 
7135*4882a593Smuzhiyun 		lo = lockowner(sop);
7136*4882a593Smuzhiyun 		nfs4_get_stateowner(sop);
7137*4882a593Smuzhiyun 		break;
7138*4882a593Smuzhiyun 	}
7139*4882a593Smuzhiyun 	if (!lo) {
7140*4882a593Smuzhiyun 		spin_unlock(&clp->cl_lock);
7141*4882a593Smuzhiyun 		return status;
7142*4882a593Smuzhiyun 	}
7143*4882a593Smuzhiyun 
7144*4882a593Smuzhiyun 	unhash_lockowner_locked(lo);
7145*4882a593Smuzhiyun 	while (!list_empty(&lo->lo_owner.so_stateids)) {
7146*4882a593Smuzhiyun 		stp = list_first_entry(&lo->lo_owner.so_stateids,
7147*4882a593Smuzhiyun 				       struct nfs4_ol_stateid,
7148*4882a593Smuzhiyun 				       st_perstateowner);
7149*4882a593Smuzhiyun 		WARN_ON(!unhash_lock_stateid(stp));
7150*4882a593Smuzhiyun 		put_ol_stateid_locked(stp, &reaplist);
7151*4882a593Smuzhiyun 	}
7152*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
7153*4882a593Smuzhiyun 	free_ol_stateid_reaplist(&reaplist);
7154*4882a593Smuzhiyun 	remove_blocked_locks(lo);
7155*4882a593Smuzhiyun 	nfs4_put_stateowner(&lo->lo_owner);
7156*4882a593Smuzhiyun 
7157*4882a593Smuzhiyun 	return status;
7158*4882a593Smuzhiyun }
7159*4882a593Smuzhiyun 
7160*4882a593Smuzhiyun static inline struct nfs4_client_reclaim *
alloc_reclaim(void)7161*4882a593Smuzhiyun alloc_reclaim(void)
7162*4882a593Smuzhiyun {
7163*4882a593Smuzhiyun 	return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
7164*4882a593Smuzhiyun }
7165*4882a593Smuzhiyun 
7166*4882a593Smuzhiyun bool
nfs4_has_reclaimed_state(struct xdr_netobj name,struct nfsd_net * nn)7167*4882a593Smuzhiyun nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
7168*4882a593Smuzhiyun {
7169*4882a593Smuzhiyun 	struct nfs4_client_reclaim *crp;
7170*4882a593Smuzhiyun 
7171*4882a593Smuzhiyun 	crp = nfsd4_find_reclaim_client(name, nn);
7172*4882a593Smuzhiyun 	return (crp && crp->cr_clp);
7173*4882a593Smuzhiyun }
7174*4882a593Smuzhiyun 
7175*4882a593Smuzhiyun /*
7176*4882a593Smuzhiyun  * failure => all reset bets are off, nfserr_no_grace...
7177*4882a593Smuzhiyun  *
7178*4882a593Smuzhiyun  * The caller is responsible for freeing name.data if NULL is returned (it
7179*4882a593Smuzhiyun  * will be freed in nfs4_remove_reclaim_record in the normal case).
7180*4882a593Smuzhiyun  */
7181*4882a593Smuzhiyun struct nfs4_client_reclaim *
nfs4_client_to_reclaim(struct xdr_netobj name,struct xdr_netobj princhash,struct nfsd_net * nn)7182*4882a593Smuzhiyun nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7183*4882a593Smuzhiyun 		struct nfsd_net *nn)
7184*4882a593Smuzhiyun {
7185*4882a593Smuzhiyun 	unsigned int strhashval;
7186*4882a593Smuzhiyun 	struct nfs4_client_reclaim *crp;
7187*4882a593Smuzhiyun 
7188*4882a593Smuzhiyun 	crp = alloc_reclaim();
7189*4882a593Smuzhiyun 	if (crp) {
7190*4882a593Smuzhiyun 		strhashval = clientstr_hashval(name);
7191*4882a593Smuzhiyun 		INIT_LIST_HEAD(&crp->cr_strhash);
7192*4882a593Smuzhiyun 		list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
7193*4882a593Smuzhiyun 		crp->cr_name.data = name.data;
7194*4882a593Smuzhiyun 		crp->cr_name.len = name.len;
7195*4882a593Smuzhiyun 		crp->cr_princhash.data = princhash.data;
7196*4882a593Smuzhiyun 		crp->cr_princhash.len = princhash.len;
7197*4882a593Smuzhiyun 		crp->cr_clp = NULL;
7198*4882a593Smuzhiyun 		nn->reclaim_str_hashtbl_size++;
7199*4882a593Smuzhiyun 	}
7200*4882a593Smuzhiyun 	return crp;
7201*4882a593Smuzhiyun }
7202*4882a593Smuzhiyun 
7203*4882a593Smuzhiyun void
nfs4_remove_reclaim_record(struct nfs4_client_reclaim * crp,struct nfsd_net * nn)7204*4882a593Smuzhiyun nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
7205*4882a593Smuzhiyun {
7206*4882a593Smuzhiyun 	list_del(&crp->cr_strhash);
7207*4882a593Smuzhiyun 	kfree(crp->cr_name.data);
7208*4882a593Smuzhiyun 	kfree(crp->cr_princhash.data);
7209*4882a593Smuzhiyun 	kfree(crp);
7210*4882a593Smuzhiyun 	nn->reclaim_str_hashtbl_size--;
7211*4882a593Smuzhiyun }
7212*4882a593Smuzhiyun 
7213*4882a593Smuzhiyun void
nfs4_release_reclaim(struct nfsd_net * nn)7214*4882a593Smuzhiyun nfs4_release_reclaim(struct nfsd_net *nn)
7215*4882a593Smuzhiyun {
7216*4882a593Smuzhiyun 	struct nfs4_client_reclaim *crp = NULL;
7217*4882a593Smuzhiyun 	int i;
7218*4882a593Smuzhiyun 
7219*4882a593Smuzhiyun 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7220*4882a593Smuzhiyun 		while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7221*4882a593Smuzhiyun 			crp = list_entry(nn->reclaim_str_hashtbl[i].next,
7222*4882a593Smuzhiyun 			                struct nfs4_client_reclaim, cr_strhash);
7223*4882a593Smuzhiyun 			nfs4_remove_reclaim_record(crp, nn);
7224*4882a593Smuzhiyun 		}
7225*4882a593Smuzhiyun 	}
7226*4882a593Smuzhiyun 	WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
7227*4882a593Smuzhiyun }
7228*4882a593Smuzhiyun 
7229*4882a593Smuzhiyun /*
7230*4882a593Smuzhiyun  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7231*4882a593Smuzhiyun struct nfs4_client_reclaim *
nfsd4_find_reclaim_client(struct xdr_netobj name,struct nfsd_net * nn)7232*4882a593Smuzhiyun nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
7233*4882a593Smuzhiyun {
7234*4882a593Smuzhiyun 	unsigned int strhashval;
7235*4882a593Smuzhiyun 	struct nfs4_client_reclaim *crp = NULL;
7236*4882a593Smuzhiyun 
7237*4882a593Smuzhiyun 	strhashval = clientstr_hashval(name);
7238*4882a593Smuzhiyun 	list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
7239*4882a593Smuzhiyun 		if (compare_blob(&crp->cr_name, &name) == 0) {
7240*4882a593Smuzhiyun 			return crp;
7241*4882a593Smuzhiyun 		}
7242*4882a593Smuzhiyun 	}
7243*4882a593Smuzhiyun 	return NULL;
7244*4882a593Smuzhiyun }
7245*4882a593Smuzhiyun 
7246*4882a593Smuzhiyun /*
7247*4882a593Smuzhiyun * Called from OPEN. Look for clientid in reclaim list.
7248*4882a593Smuzhiyun */
7249*4882a593Smuzhiyun __be32
nfs4_check_open_reclaim(clientid_t * clid,struct nfsd4_compound_state * cstate,struct nfsd_net * nn)7250*4882a593Smuzhiyun nfs4_check_open_reclaim(clientid_t *clid,
7251*4882a593Smuzhiyun 		struct nfsd4_compound_state *cstate,
7252*4882a593Smuzhiyun 		struct nfsd_net *nn)
7253*4882a593Smuzhiyun {
7254*4882a593Smuzhiyun 	__be32 status;
7255*4882a593Smuzhiyun 
7256*4882a593Smuzhiyun 	/* find clientid in conf_id_hashtbl */
7257*4882a593Smuzhiyun 	status = lookup_clientid(clid, cstate, nn, false);
7258*4882a593Smuzhiyun 	if (status)
7259*4882a593Smuzhiyun 		return nfserr_reclaim_bad;
7260*4882a593Smuzhiyun 
7261*4882a593Smuzhiyun 	if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7262*4882a593Smuzhiyun 		return nfserr_no_grace;
7263*4882a593Smuzhiyun 
7264*4882a593Smuzhiyun 	if (nfsd4_client_record_check(cstate->clp))
7265*4882a593Smuzhiyun 		return nfserr_reclaim_bad;
7266*4882a593Smuzhiyun 
7267*4882a593Smuzhiyun 	return nfs_ok;
7268*4882a593Smuzhiyun }
7269*4882a593Smuzhiyun 
7270*4882a593Smuzhiyun /*
7271*4882a593Smuzhiyun  * Since the lifetime of a delegation isn't limited to that of an open, a
7272*4882a593Smuzhiyun  * client may quite reasonably hang on to a delegation as long as it has
7273*4882a593Smuzhiyun  * the inode cached.  This becomes an obvious problem the first time a
7274*4882a593Smuzhiyun  * client's inode cache approaches the size of the server's total memory.
7275*4882a593Smuzhiyun  *
7276*4882a593Smuzhiyun  * For now we avoid this problem by imposing a hard limit on the number
7277*4882a593Smuzhiyun  * of delegations, which varies according to the server's memory size.
7278*4882a593Smuzhiyun  */
7279*4882a593Smuzhiyun static void
set_max_delegations(void)7280*4882a593Smuzhiyun set_max_delegations(void)
7281*4882a593Smuzhiyun {
7282*4882a593Smuzhiyun 	/*
7283*4882a593Smuzhiyun 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
7284*4882a593Smuzhiyun 	 * estimates suggest that in the worst case (where every delegation
7285*4882a593Smuzhiyun 	 * is for a different inode), a delegation could take about 1.5K,
7286*4882a593Smuzhiyun 	 * giving a worst case usage of about 6% of memory.
7287*4882a593Smuzhiyun 	 */
7288*4882a593Smuzhiyun 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7289*4882a593Smuzhiyun }
7290*4882a593Smuzhiyun 
nfs4_state_create_net(struct net * net)7291*4882a593Smuzhiyun static int nfs4_state_create_net(struct net *net)
7292*4882a593Smuzhiyun {
7293*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7294*4882a593Smuzhiyun 	int i;
7295*4882a593Smuzhiyun 
7296*4882a593Smuzhiyun 	nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7297*4882a593Smuzhiyun 					    sizeof(struct list_head),
7298*4882a593Smuzhiyun 					    GFP_KERNEL);
7299*4882a593Smuzhiyun 	if (!nn->conf_id_hashtbl)
7300*4882a593Smuzhiyun 		goto err;
7301*4882a593Smuzhiyun 	nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7302*4882a593Smuzhiyun 					      sizeof(struct list_head),
7303*4882a593Smuzhiyun 					      GFP_KERNEL);
7304*4882a593Smuzhiyun 	if (!nn->unconf_id_hashtbl)
7305*4882a593Smuzhiyun 		goto err_unconf_id;
7306*4882a593Smuzhiyun 	nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7307*4882a593Smuzhiyun 					      sizeof(struct list_head),
7308*4882a593Smuzhiyun 					      GFP_KERNEL);
7309*4882a593Smuzhiyun 	if (!nn->sessionid_hashtbl)
7310*4882a593Smuzhiyun 		goto err_sessionid;
7311*4882a593Smuzhiyun 
7312*4882a593Smuzhiyun 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7313*4882a593Smuzhiyun 		INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
7314*4882a593Smuzhiyun 		INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
7315*4882a593Smuzhiyun 	}
7316*4882a593Smuzhiyun 	for (i = 0; i < SESSION_HASH_SIZE; i++)
7317*4882a593Smuzhiyun 		INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
7318*4882a593Smuzhiyun 	nn->conf_name_tree = RB_ROOT;
7319*4882a593Smuzhiyun 	nn->unconf_name_tree = RB_ROOT;
7320*4882a593Smuzhiyun 	nn->boot_time = ktime_get_real_seconds();
7321*4882a593Smuzhiyun 	nn->grace_ended = false;
7322*4882a593Smuzhiyun 	nn->nfsd4_manager.block_opens = true;
7323*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nn->nfsd4_manager.list);
7324*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nn->client_lru);
7325*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nn->close_lru);
7326*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nn->del_recall_lru);
7327*4882a593Smuzhiyun 	spin_lock_init(&nn->client_lock);
7328*4882a593Smuzhiyun 	spin_lock_init(&nn->s2s_cp_lock);
7329*4882a593Smuzhiyun 	idr_init(&nn->s2s_cp_stateids);
7330*4882a593Smuzhiyun 
7331*4882a593Smuzhiyun 	spin_lock_init(&nn->blocked_locks_lock);
7332*4882a593Smuzhiyun 	INIT_LIST_HEAD(&nn->blocked_locks_lru);
7333*4882a593Smuzhiyun 
7334*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
7335*4882a593Smuzhiyun 	get_net(net);
7336*4882a593Smuzhiyun 
7337*4882a593Smuzhiyun 	return 0;
7338*4882a593Smuzhiyun 
7339*4882a593Smuzhiyun err_sessionid:
7340*4882a593Smuzhiyun 	kfree(nn->unconf_id_hashtbl);
7341*4882a593Smuzhiyun err_unconf_id:
7342*4882a593Smuzhiyun 	kfree(nn->conf_id_hashtbl);
7343*4882a593Smuzhiyun err:
7344*4882a593Smuzhiyun 	return -ENOMEM;
7345*4882a593Smuzhiyun }
7346*4882a593Smuzhiyun 
7347*4882a593Smuzhiyun static void
nfs4_state_destroy_net(struct net * net)7348*4882a593Smuzhiyun nfs4_state_destroy_net(struct net *net)
7349*4882a593Smuzhiyun {
7350*4882a593Smuzhiyun 	int i;
7351*4882a593Smuzhiyun 	struct nfs4_client *clp = NULL;
7352*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7353*4882a593Smuzhiyun 
7354*4882a593Smuzhiyun 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7355*4882a593Smuzhiyun 		while (!list_empty(&nn->conf_id_hashtbl[i])) {
7356*4882a593Smuzhiyun 			clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7357*4882a593Smuzhiyun 			destroy_client(clp);
7358*4882a593Smuzhiyun 		}
7359*4882a593Smuzhiyun 	}
7360*4882a593Smuzhiyun 
7361*4882a593Smuzhiyun 	WARN_ON(!list_empty(&nn->blocked_locks_lru));
7362*4882a593Smuzhiyun 
7363*4882a593Smuzhiyun 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7364*4882a593Smuzhiyun 		while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7365*4882a593Smuzhiyun 			clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7366*4882a593Smuzhiyun 			destroy_client(clp);
7367*4882a593Smuzhiyun 		}
7368*4882a593Smuzhiyun 	}
7369*4882a593Smuzhiyun 
7370*4882a593Smuzhiyun 	kfree(nn->sessionid_hashtbl);
7371*4882a593Smuzhiyun 	kfree(nn->unconf_id_hashtbl);
7372*4882a593Smuzhiyun 	kfree(nn->conf_id_hashtbl);
7373*4882a593Smuzhiyun 	put_net(net);
7374*4882a593Smuzhiyun }
7375*4882a593Smuzhiyun 
7376*4882a593Smuzhiyun int
nfs4_state_start_net(struct net * net)7377*4882a593Smuzhiyun nfs4_state_start_net(struct net *net)
7378*4882a593Smuzhiyun {
7379*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7380*4882a593Smuzhiyun 	int ret;
7381*4882a593Smuzhiyun 
7382*4882a593Smuzhiyun 	ret = get_nfsdfs(net);
7383*4882a593Smuzhiyun 	if (ret)
7384*4882a593Smuzhiyun 		return ret;
7385*4882a593Smuzhiyun 	ret = nfs4_state_create_net(net);
7386*4882a593Smuzhiyun 	if (ret) {
7387*4882a593Smuzhiyun 		mntput(nn->nfsd_mnt);
7388*4882a593Smuzhiyun 		return ret;
7389*4882a593Smuzhiyun 	}
7390*4882a593Smuzhiyun 	locks_start_grace(net, &nn->nfsd4_manager);
7391*4882a593Smuzhiyun 	nfsd4_client_tracking_init(net);
7392*4882a593Smuzhiyun 	if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7393*4882a593Smuzhiyun 		goto skip_grace;
7394*4882a593Smuzhiyun 	printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7395*4882a593Smuzhiyun 	       nn->nfsd4_grace, net->ns.inum);
7396*4882a593Smuzhiyun 	trace_nfsd_grace_start(nn);
7397*4882a593Smuzhiyun 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
7398*4882a593Smuzhiyun 	return 0;
7399*4882a593Smuzhiyun 
7400*4882a593Smuzhiyun skip_grace:
7401*4882a593Smuzhiyun 	printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7402*4882a593Smuzhiyun 			net->ns.inum);
7403*4882a593Smuzhiyun 	queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7404*4882a593Smuzhiyun 	nfsd4_end_grace(nn);
7405*4882a593Smuzhiyun 	return 0;
7406*4882a593Smuzhiyun }
7407*4882a593Smuzhiyun 
7408*4882a593Smuzhiyun /* initialization to perform when the nfsd service is started: */
7409*4882a593Smuzhiyun 
7410*4882a593Smuzhiyun int
nfs4_state_start(void)7411*4882a593Smuzhiyun nfs4_state_start(void)
7412*4882a593Smuzhiyun {
7413*4882a593Smuzhiyun 	int ret;
7414*4882a593Smuzhiyun 
7415*4882a593Smuzhiyun 	laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
7416*4882a593Smuzhiyun 	if (laundry_wq == NULL) {
7417*4882a593Smuzhiyun 		ret = -ENOMEM;
7418*4882a593Smuzhiyun 		goto out;
7419*4882a593Smuzhiyun 	}
7420*4882a593Smuzhiyun 	ret = nfsd4_create_callback_queue();
7421*4882a593Smuzhiyun 	if (ret)
7422*4882a593Smuzhiyun 		goto out_free_laundry;
7423*4882a593Smuzhiyun 
7424*4882a593Smuzhiyun 	set_max_delegations();
7425*4882a593Smuzhiyun 	return 0;
7426*4882a593Smuzhiyun 
7427*4882a593Smuzhiyun out_free_laundry:
7428*4882a593Smuzhiyun 	destroy_workqueue(laundry_wq);
7429*4882a593Smuzhiyun out:
7430*4882a593Smuzhiyun 	return ret;
7431*4882a593Smuzhiyun }
7432*4882a593Smuzhiyun 
7433*4882a593Smuzhiyun void
nfs4_state_shutdown_net(struct net * net)7434*4882a593Smuzhiyun nfs4_state_shutdown_net(struct net *net)
7435*4882a593Smuzhiyun {
7436*4882a593Smuzhiyun 	struct nfs4_delegation *dp = NULL;
7437*4882a593Smuzhiyun 	struct list_head *pos, *next, reaplist;
7438*4882a593Smuzhiyun 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7439*4882a593Smuzhiyun 
7440*4882a593Smuzhiyun 	cancel_delayed_work_sync(&nn->laundromat_work);
7441*4882a593Smuzhiyun 	locks_end_grace(&nn->nfsd4_manager);
7442*4882a593Smuzhiyun 
7443*4882a593Smuzhiyun 	INIT_LIST_HEAD(&reaplist);
7444*4882a593Smuzhiyun 	spin_lock(&state_lock);
7445*4882a593Smuzhiyun 	list_for_each_safe(pos, next, &nn->del_recall_lru) {
7446*4882a593Smuzhiyun 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7447*4882a593Smuzhiyun 		WARN_ON(!unhash_delegation_locked(dp));
7448*4882a593Smuzhiyun 		list_add(&dp->dl_recall_lru, &reaplist);
7449*4882a593Smuzhiyun 	}
7450*4882a593Smuzhiyun 	spin_unlock(&state_lock);
7451*4882a593Smuzhiyun 	list_for_each_safe(pos, next, &reaplist) {
7452*4882a593Smuzhiyun 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
7453*4882a593Smuzhiyun 		list_del_init(&dp->dl_recall_lru);
7454*4882a593Smuzhiyun 		destroy_unhashed_deleg(dp);
7455*4882a593Smuzhiyun 	}
7456*4882a593Smuzhiyun 
7457*4882a593Smuzhiyun 	nfsd4_client_tracking_exit(net);
7458*4882a593Smuzhiyun 	nfs4_state_destroy_net(net);
7459*4882a593Smuzhiyun 	mntput(nn->nfsd_mnt);
7460*4882a593Smuzhiyun }
7461*4882a593Smuzhiyun 
7462*4882a593Smuzhiyun void
nfs4_state_shutdown(void)7463*4882a593Smuzhiyun nfs4_state_shutdown(void)
7464*4882a593Smuzhiyun {
7465*4882a593Smuzhiyun 	destroy_workqueue(laundry_wq);
7466*4882a593Smuzhiyun 	nfsd4_destroy_callback_queue();
7467*4882a593Smuzhiyun }
7468*4882a593Smuzhiyun 
7469*4882a593Smuzhiyun static void
get_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)7470*4882a593Smuzhiyun get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7471*4882a593Smuzhiyun {
7472*4882a593Smuzhiyun 	if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7473*4882a593Smuzhiyun 	    CURRENT_STATEID(stateid))
7474*4882a593Smuzhiyun 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
7475*4882a593Smuzhiyun }
7476*4882a593Smuzhiyun 
7477*4882a593Smuzhiyun static void
put_stateid(struct nfsd4_compound_state * cstate,stateid_t * stateid)7478*4882a593Smuzhiyun put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7479*4882a593Smuzhiyun {
7480*4882a593Smuzhiyun 	if (cstate->minorversion) {
7481*4882a593Smuzhiyun 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
7482*4882a593Smuzhiyun 		SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7483*4882a593Smuzhiyun 	}
7484*4882a593Smuzhiyun }
7485*4882a593Smuzhiyun 
7486*4882a593Smuzhiyun void
clear_current_stateid(struct nfsd4_compound_state * cstate)7487*4882a593Smuzhiyun clear_current_stateid(struct nfsd4_compound_state *cstate)
7488*4882a593Smuzhiyun {
7489*4882a593Smuzhiyun 	CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
7490*4882a593Smuzhiyun }
7491*4882a593Smuzhiyun 
7492*4882a593Smuzhiyun /*
7493*4882a593Smuzhiyun  * functions to set current state id
7494*4882a593Smuzhiyun  */
7495*4882a593Smuzhiyun void
nfsd4_set_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7496*4882a593Smuzhiyun nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7497*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7498*4882a593Smuzhiyun {
7499*4882a593Smuzhiyun 	put_stateid(cstate, &u->open_downgrade.od_stateid);
7500*4882a593Smuzhiyun }
7501*4882a593Smuzhiyun 
7502*4882a593Smuzhiyun void
nfsd4_set_openstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7503*4882a593Smuzhiyun nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7504*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7505*4882a593Smuzhiyun {
7506*4882a593Smuzhiyun 	put_stateid(cstate, &u->open.op_stateid);
7507*4882a593Smuzhiyun }
7508*4882a593Smuzhiyun 
7509*4882a593Smuzhiyun void
nfsd4_set_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7510*4882a593Smuzhiyun nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7511*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7512*4882a593Smuzhiyun {
7513*4882a593Smuzhiyun 	put_stateid(cstate, &u->close.cl_stateid);
7514*4882a593Smuzhiyun }
7515*4882a593Smuzhiyun 
7516*4882a593Smuzhiyun void
nfsd4_set_lockstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7517*4882a593Smuzhiyun nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7518*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7519*4882a593Smuzhiyun {
7520*4882a593Smuzhiyun 	put_stateid(cstate, &u->lock.lk_resp_stateid);
7521*4882a593Smuzhiyun }
7522*4882a593Smuzhiyun 
7523*4882a593Smuzhiyun /*
7524*4882a593Smuzhiyun  * functions to consume current state id
7525*4882a593Smuzhiyun  */
7526*4882a593Smuzhiyun 
7527*4882a593Smuzhiyun void
nfsd4_get_opendowngradestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7528*4882a593Smuzhiyun nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7529*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7530*4882a593Smuzhiyun {
7531*4882a593Smuzhiyun 	get_stateid(cstate, &u->open_downgrade.od_stateid);
7532*4882a593Smuzhiyun }
7533*4882a593Smuzhiyun 
7534*4882a593Smuzhiyun void
nfsd4_get_delegreturnstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7535*4882a593Smuzhiyun nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7536*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7537*4882a593Smuzhiyun {
7538*4882a593Smuzhiyun 	get_stateid(cstate, &u->delegreturn.dr_stateid);
7539*4882a593Smuzhiyun }
7540*4882a593Smuzhiyun 
7541*4882a593Smuzhiyun void
nfsd4_get_freestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7542*4882a593Smuzhiyun nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7543*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7544*4882a593Smuzhiyun {
7545*4882a593Smuzhiyun 	get_stateid(cstate, &u->free_stateid.fr_stateid);
7546*4882a593Smuzhiyun }
7547*4882a593Smuzhiyun 
7548*4882a593Smuzhiyun void
nfsd4_get_setattrstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7549*4882a593Smuzhiyun nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7550*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7551*4882a593Smuzhiyun {
7552*4882a593Smuzhiyun 	get_stateid(cstate, &u->setattr.sa_stateid);
7553*4882a593Smuzhiyun }
7554*4882a593Smuzhiyun 
7555*4882a593Smuzhiyun void
nfsd4_get_closestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7556*4882a593Smuzhiyun nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7557*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7558*4882a593Smuzhiyun {
7559*4882a593Smuzhiyun 	get_stateid(cstate, &u->close.cl_stateid);
7560*4882a593Smuzhiyun }
7561*4882a593Smuzhiyun 
7562*4882a593Smuzhiyun void
nfsd4_get_lockustateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7563*4882a593Smuzhiyun nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7564*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7565*4882a593Smuzhiyun {
7566*4882a593Smuzhiyun 	get_stateid(cstate, &u->locku.lu_stateid);
7567*4882a593Smuzhiyun }
7568*4882a593Smuzhiyun 
7569*4882a593Smuzhiyun void
nfsd4_get_readstateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7570*4882a593Smuzhiyun nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7571*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7572*4882a593Smuzhiyun {
7573*4882a593Smuzhiyun 	get_stateid(cstate, &u->read.rd_stateid);
7574*4882a593Smuzhiyun }
7575*4882a593Smuzhiyun 
7576*4882a593Smuzhiyun void
nfsd4_get_writestateid(struct nfsd4_compound_state * cstate,union nfsd4_op_u * u)7577*4882a593Smuzhiyun nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7578*4882a593Smuzhiyun 		union nfsd4_op_u *u)
7579*4882a593Smuzhiyun {
7580*4882a593Smuzhiyun 	get_stateid(cstate, &u->write.wr_stateid);
7581*4882a593Smuzhiyun }
7582