xref: /openbmc/linux/fs/nfsd/nfs4state.c (revision 393d8ed8)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds *  Copyright (c) 2001 The Regents of the University of Michigan.
31da177e4SLinus Torvalds *  All rights reserved.
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds *  Kendrick Smith <kmsmith@umich.edu>
61da177e4SLinus Torvalds *  Andy Adamson <kandros@umich.edu>
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds *  Redistribution and use in source and binary forms, with or without
91da177e4SLinus Torvalds *  modification, are permitted provided that the following conditions
101da177e4SLinus Torvalds *  are met:
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds *  1. Redistributions of source code must retain the above copyright
131da177e4SLinus Torvalds *     notice, this list of conditions and the following disclaimer.
141da177e4SLinus Torvalds *  2. Redistributions in binary form must reproduce the above copyright
151da177e4SLinus Torvalds *     notice, this list of conditions and the following disclaimer in the
161da177e4SLinus Torvalds *     documentation and/or other materials provided with the distribution.
171da177e4SLinus Torvalds *  3. Neither the name of the University nor the names of its
181da177e4SLinus Torvalds *     contributors may be used to endorse or promote products derived
191da177e4SLinus Torvalds *     from this software without specific prior written permission.
201da177e4SLinus Torvalds *
211da177e4SLinus Torvalds *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
221da177e4SLinus Torvalds *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
231da177e4SLinus Torvalds *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
241da177e4SLinus Torvalds *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251da177e4SLinus Torvalds *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
261da177e4SLinus Torvalds *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
271da177e4SLinus Torvalds *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
281da177e4SLinus Torvalds *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
291da177e4SLinus Torvalds *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
301da177e4SLinus Torvalds *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
311da177e4SLinus Torvalds *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321da177e4SLinus Torvalds *
331da177e4SLinus Torvalds */
341da177e4SLinus Torvalds 
35aceaf78dSDave Hansen #include <linux/file.h>
36b89f4321SArnd Bergmann #include <linux/fs.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
380964a3d3SNeilBrown #include <linux/namei.h>
39c2f1a551SMeelap Shah #include <linux/swap.h>
4017456804SBryan Schumaker #include <linux/pagemap.h>
4168e76ad0SOlga Kornievskaia #include <linux/sunrpc/svcauth_gss.h>
42363168b4SJeff Layton #include <linux/sunrpc/clnt.h>
439a74af21SBoaz Harrosh #include "xdr4.h"
440a3adadeSJ. Bruce Fields #include "vfs.h"
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define NFSDDBG_FACILITY                NFSDDBG_PROC
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* Globals */
49cf07d2eaSJ. Bruce Fields time_t nfsd4_lease = 90;     /* default lease time */
50efc4bb4fSJ. Bruce Fields time_t nfsd4_grace = 90;
51fd39ca9aSNeilBrown static time_t boot_time;
52f32f3c2dSJ. Bruce Fields 
53f32f3c2dSJ. Bruce Fields #define all_ones {{~0,~0},~0}
54f32f3c2dSJ. Bruce Fields static const stateid_t one_stateid = {
55f32f3c2dSJ. Bruce Fields 	.si_generation = ~0,
56f32f3c2dSJ. Bruce Fields 	.si_opaque = all_ones,
57f32f3c2dSJ. Bruce Fields };
58f32f3c2dSJ. Bruce Fields static const stateid_t zero_stateid = {
59f32f3c2dSJ. Bruce Fields 	/* all fields zero */
60f32f3c2dSJ. Bruce Fields };
6119ff0f28STigran Mkrtchyan static const stateid_t currentstateid = {
6219ff0f28STigran Mkrtchyan 	.si_generation = 1,
6319ff0f28STigran Mkrtchyan };
64f32f3c2dSJ. Bruce Fields 
65ec6b5d7bSAndy Adamson static u64 current_sessionid = 1;
66fd39ca9aSNeilBrown 
67f32f3c2dSJ. Bruce Fields #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
68f32f3c2dSJ. Bruce Fields #define ONE_STATEID(stateid)  (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
6919ff0f28STigran Mkrtchyan #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /* forward declarations */
72fe0750e5SJ. Bruce Fields static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
731da177e4SLinus Torvalds 
748b671b80SJ. Bruce Fields /* Locking: */
758b671b80SJ. Bruce Fields 
768b671b80SJ. Bruce Fields /* Currently used for almost all code touching nfsv4 state: */
77353ab6e9SIngo Molnar static DEFINE_MUTEX(client_mutex);
781da177e4SLinus Torvalds 
798b671b80SJ. Bruce Fields /*
808b671b80SJ. Bruce Fields  * Currently used for the del_recall_lru and file hash table.  In an
818b671b80SJ. Bruce Fields  * effort to decrease the scope of the client_mutex, this spinlock may
828b671b80SJ. Bruce Fields  * eventually cover more:
838b671b80SJ. Bruce Fields  */
848b671b80SJ. Bruce Fields static DEFINE_SPINLOCK(recall_lock);
858b671b80SJ. Bruce Fields 
86fe0750e5SJ. Bruce Fields static struct kmem_cache *openowner_slab = NULL;
87fe0750e5SJ. Bruce Fields static struct kmem_cache *lockowner_slab = NULL;
88e18b890bSChristoph Lameter static struct kmem_cache *file_slab = NULL;
89e18b890bSChristoph Lameter static struct kmem_cache *stateid_slab = NULL;
90e18b890bSChristoph Lameter static struct kmem_cache *deleg_slab = NULL;
91e60d4398SNeilBrown 
921da177e4SLinus Torvalds void
931da177e4SLinus Torvalds nfs4_lock_state(void)
941da177e4SLinus Torvalds {
95353ab6e9SIngo Molnar 	mutex_lock(&client_mutex);
961da177e4SLinus Torvalds }
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds void
991da177e4SLinus Torvalds nfs4_unlock_state(void)
1001da177e4SLinus Torvalds {
101353ab6e9SIngo Molnar 	mutex_unlock(&client_mutex);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds static inline u32
1051da177e4SLinus Torvalds opaque_hashval(const void *ptr, int nbytes)
1061da177e4SLinus Torvalds {
1071da177e4SLinus Torvalds 	unsigned char *cptr = (unsigned char *) ptr;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	u32 x = 0;
1101da177e4SLinus Torvalds 	while (nbytes--) {
1111da177e4SLinus Torvalds 		x *= 37;
1121da177e4SLinus Torvalds 		x += *cptr++;
1131da177e4SLinus Torvalds 	}
1141da177e4SLinus Torvalds 	return x;
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static struct list_head del_recall_lru;
1181da177e4SLinus Torvalds 
11932513b40SJ. Bruce Fields static void nfsd4_free_file(struct nfs4_file *f)
12032513b40SJ. Bruce Fields {
12132513b40SJ. Bruce Fields 	kmem_cache_free(file_slab, f);
12232513b40SJ. Bruce Fields }
12332513b40SJ. Bruce Fields 
12413cd2184SNeilBrown static inline void
12513cd2184SNeilBrown put_nfs4_file(struct nfs4_file *fi)
12613cd2184SNeilBrown {
1278b671b80SJ. Bruce Fields 	if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
1288b671b80SJ. Bruce Fields 		list_del(&fi->fi_hash);
1298b671b80SJ. Bruce Fields 		spin_unlock(&recall_lock);
1308b671b80SJ. Bruce Fields 		iput(fi->fi_inode);
13132513b40SJ. Bruce Fields 		nfsd4_free_file(fi);
1328b671b80SJ. Bruce Fields 	}
13313cd2184SNeilBrown }
13413cd2184SNeilBrown 
13513cd2184SNeilBrown static inline void
13613cd2184SNeilBrown get_nfs4_file(struct nfs4_file *fi)
13713cd2184SNeilBrown {
1388b671b80SJ. Bruce Fields 	atomic_inc(&fi->fi_ref);
13913cd2184SNeilBrown }
14013cd2184SNeilBrown 
141ef0f3390SNeilBrown static int num_delegations;
142c2f1a551SMeelap Shah unsigned int max_delegations;
143ef0f3390SNeilBrown 
144ef0f3390SNeilBrown /*
145ef0f3390SNeilBrown  * Open owner state (share locks)
146ef0f3390SNeilBrown  */
147ef0f3390SNeilBrown 
14816bfdaafSJ. Bruce Fields /* hash tables for lock and open owners */
14916bfdaafSJ. Bruce Fields #define OWNER_HASH_BITS              8
15016bfdaafSJ. Bruce Fields #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
15116bfdaafSJ. Bruce Fields #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
152ef0f3390SNeilBrown 
15316bfdaafSJ. Bruce Fields static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
154ddc04c41SJ. Bruce Fields {
155ddc04c41SJ. Bruce Fields 	unsigned int ret;
156ddc04c41SJ. Bruce Fields 
157ddc04c41SJ. Bruce Fields 	ret = opaque_hashval(ownername->data, ownername->len);
158ddc04c41SJ. Bruce Fields 	ret += clientid;
15916bfdaafSJ. Bruce Fields 	return ret & OWNER_HASH_MASK;
160ddc04c41SJ. Bruce Fields }
161ef0f3390SNeilBrown 
16216bfdaafSJ. Bruce Fields static struct list_head	ownerstr_hashtbl[OWNER_HASH_SIZE];
163ef0f3390SNeilBrown 
164ef0f3390SNeilBrown /* hash table for nfs4_file */
165ef0f3390SNeilBrown #define FILE_HASH_BITS                   8
166ef0f3390SNeilBrown #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
16735079582SShan Wei 
168ddc04c41SJ. Bruce Fields static unsigned int file_hashval(struct inode *ino)
169ddc04c41SJ. Bruce Fields {
170ddc04c41SJ. Bruce Fields 	/* XXX: why are we hashing on inode pointer, anyway? */
171ddc04c41SJ. Bruce Fields 	return hash_ptr(ino, FILE_HASH_BITS);
172ddc04c41SJ. Bruce Fields }
173ddc04c41SJ. Bruce Fields 
174ef0f3390SNeilBrown static struct list_head file_hashtbl[FILE_HASH_SIZE];
175ef0f3390SNeilBrown 
176998db52cSJ. Bruce Fields static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
177f9d7562fSJ. Bruce Fields {
178f9d7562fSJ. Bruce Fields 	BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
179f9d7562fSJ. Bruce Fields 	atomic_inc(&fp->fi_access[oflag]);
180f9d7562fSJ. Bruce Fields }
181f9d7562fSJ. Bruce Fields 
182998db52cSJ. Bruce Fields static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
183998db52cSJ. Bruce Fields {
184998db52cSJ. Bruce Fields 	if (oflag == O_RDWR) {
185998db52cSJ. Bruce Fields 		__nfs4_file_get_access(fp, O_RDONLY);
186998db52cSJ. Bruce Fields 		__nfs4_file_get_access(fp, O_WRONLY);
187998db52cSJ. Bruce Fields 	} else
188998db52cSJ. Bruce Fields 		__nfs4_file_get_access(fp, oflag);
189998db52cSJ. Bruce Fields }
190998db52cSJ. Bruce Fields 
191998db52cSJ. Bruce Fields static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
192f9d7562fSJ. Bruce Fields {
193f9d7562fSJ. Bruce Fields 	if (fp->fi_fds[oflag]) {
194f9d7562fSJ. Bruce Fields 		fput(fp->fi_fds[oflag]);
195f9d7562fSJ. Bruce Fields 		fp->fi_fds[oflag] = NULL;
196f9d7562fSJ. Bruce Fields 	}
197f9d7562fSJ. Bruce Fields }
198f9d7562fSJ. Bruce Fields 
199998db52cSJ. Bruce Fields static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
200f9d7562fSJ. Bruce Fields {
201f9d7562fSJ. Bruce Fields 	if (atomic_dec_and_test(&fp->fi_access[oflag])) {
202f9d7562fSJ. Bruce Fields 		nfs4_file_put_fd(fp, oflag);
2033d02fa29SJ. Bruce Fields 		/*
2043d02fa29SJ. Bruce Fields 		 * It's also safe to get rid of the RDWR open *if*
2053d02fa29SJ. Bruce Fields 		 * we no longer have need of the other kind of access
2063d02fa29SJ. Bruce Fields 		 * or if we already have the other kind of open:
2073d02fa29SJ. Bruce Fields 		 */
2083d02fa29SJ. Bruce Fields 		if (fp->fi_fds[1-oflag]
2093d02fa29SJ. Bruce Fields 			|| atomic_read(&fp->fi_access[1 - oflag]) == 0)
2103d02fa29SJ. Bruce Fields 			nfs4_file_put_fd(fp, O_RDWR);
211f9d7562fSJ. Bruce Fields 	}
212f9d7562fSJ. Bruce Fields }
213f9d7562fSJ. Bruce Fields 
214998db52cSJ. Bruce Fields static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
215998db52cSJ. Bruce Fields {
216998db52cSJ. Bruce Fields 	if (oflag == O_RDWR) {
217998db52cSJ. Bruce Fields 		__nfs4_file_put_access(fp, O_RDONLY);
218998db52cSJ. Bruce Fields 		__nfs4_file_put_access(fp, O_WRONLY);
219998db52cSJ. Bruce Fields 	} else
220998db52cSJ. Bruce Fields 		__nfs4_file_put_access(fp, oflag);
221998db52cSJ. Bruce Fields }
222998db52cSJ. Bruce Fields 
2236136d2b4SJ. Bruce Fields static inline int get_new_stid(struct nfs4_stid *stid)
22436d44c60SJ. Bruce Fields {
2256136d2b4SJ. Bruce Fields 	static int min_stateid = 0;
22638c2f4b1SJ. Bruce Fields 	struct idr *stateids = &stid->sc_client->cl_stateids;
2276136d2b4SJ. Bruce Fields 	int new_stid;
2286136d2b4SJ. Bruce Fields 	int error;
22936d44c60SJ. Bruce Fields 
23038c2f4b1SJ. Bruce Fields 	error = idr_get_new_above(stateids, stid, min_stateid, &new_stid);
2316136d2b4SJ. Bruce Fields 	/*
232996e0938SJ. Bruce Fields 	 * Note: the necessary preallocation was done in
233996e0938SJ. Bruce Fields 	 * nfs4_alloc_stateid().  The idr code caps the number of
234996e0938SJ. Bruce Fields 	 * preallocations that can exist at a time, but the state lock
235996e0938SJ. Bruce Fields 	 * prevents anyone from using ours before we get here:
2366136d2b4SJ. Bruce Fields 	 */
2376136d2b4SJ. Bruce Fields 	BUG_ON(error);
2386136d2b4SJ. Bruce Fields 	/*
2396136d2b4SJ. Bruce Fields 	 * It shouldn't be a problem to reuse an opaque stateid value.
2406136d2b4SJ. Bruce Fields 	 * I don't think it is for 4.1.  But with 4.0 I worry that, for
2416136d2b4SJ. Bruce Fields 	 * example, a stray write retransmission could be accepted by
2426136d2b4SJ. Bruce Fields 	 * the server when it should have been rejected.  Therefore,
2436136d2b4SJ. Bruce Fields 	 * adopt a trick from the sctp code to attempt to maximize the
2446136d2b4SJ. Bruce Fields 	 * amount of time until an id is reused, by ensuring they always
2456136d2b4SJ. Bruce Fields 	 * "increase" (mod INT_MAX):
2466136d2b4SJ. Bruce Fields 	 */
2476136d2b4SJ. Bruce Fields 
2486136d2b4SJ. Bruce Fields 	min_stateid = new_stid+1;
2496136d2b4SJ. Bruce Fields 	if (min_stateid == INT_MAX)
2506136d2b4SJ. Bruce Fields 		min_stateid = 0;
2516136d2b4SJ. Bruce Fields 	return new_stid;
25236d44c60SJ. Bruce Fields }
25336d44c60SJ. Bruce Fields 
254996e0938SJ. Bruce Fields static void init_stid(struct nfs4_stid *stid, struct nfs4_client *cl, unsigned char type)
2552a74aba7SJ. Bruce Fields {
2562a74aba7SJ. Bruce Fields 	stateid_t *s = &stid->sc_stateid;
2576136d2b4SJ. Bruce Fields 	int new_id;
2582a74aba7SJ. Bruce Fields 
2592a74aba7SJ. Bruce Fields 	stid->sc_type = type;
2602a74aba7SJ. Bruce Fields 	stid->sc_client = cl;
2612a74aba7SJ. Bruce Fields 	s->si_opaque.so_clid = cl->cl_clientid;
2626136d2b4SJ. Bruce Fields 	new_id = get_new_stid(stid);
2636136d2b4SJ. Bruce Fields 	s->si_opaque.so_id = (u32)new_id;
2642a74aba7SJ. Bruce Fields 	/* Will be incremented before return to client: */
2652a74aba7SJ. Bruce Fields 	s->si_generation = 0;
266996e0938SJ. Bruce Fields }
267996e0938SJ. Bruce Fields 
268996e0938SJ. Bruce Fields static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab)
269996e0938SJ. Bruce Fields {
270996e0938SJ. Bruce Fields 	struct idr *stateids = &cl->cl_stateids;
271996e0938SJ. Bruce Fields 
272996e0938SJ. Bruce Fields 	if (!idr_pre_get(stateids, GFP_KERNEL))
273996e0938SJ. Bruce Fields 		return NULL;
274996e0938SJ. Bruce Fields 	/*
275996e0938SJ. Bruce Fields 	 * Note: if we fail here (or any time between now and the time
276996e0938SJ. Bruce Fields 	 * we actually get the new idr), we won't need to undo the idr
277996e0938SJ. Bruce Fields 	 * preallocation, since the idr code caps the number of
278996e0938SJ. Bruce Fields 	 * preallocated entries.
279996e0938SJ. Bruce Fields 	 */
280996e0938SJ. Bruce Fields 	return kmem_cache_alloc(slab, GFP_KERNEL);
2812a74aba7SJ. Bruce Fields }
2822a74aba7SJ. Bruce Fields 
2834cdc951bSJ. Bruce Fields static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
2844cdc951bSJ. Bruce Fields {
2854cdc951bSJ. Bruce Fields 	return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
2864cdc951bSJ. Bruce Fields }
2874cdc951bSJ. Bruce Fields 
2881da177e4SLinus Torvalds static struct nfs4_delegation *
289dcef0413SJ. Bruce Fields alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
2901da177e4SLinus Torvalds {
2911da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
2921da177e4SLinus Torvalds 	struct nfs4_file *fp = stp->st_file;
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	dprintk("NFSD alloc_init_deleg\n");
295c3e48080SJ. Bruce Fields 	/*
296c3e48080SJ. Bruce Fields 	 * Major work on the lease subsystem (for example, to support
297c3e48080SJ. Bruce Fields 	 * calbacks on stat) will be required before we can support
298c3e48080SJ. Bruce Fields 	 * write delegations properly.
299c3e48080SJ. Bruce Fields 	 */
300c3e48080SJ. Bruce Fields 	if (type != NFS4_OPEN_DELEGATE_READ)
301c3e48080SJ. Bruce Fields 		return NULL;
30247f9940cSMeelap Shah 	if (fp->fi_had_conflict)
30347f9940cSMeelap Shah 		return NULL;
304c2f1a551SMeelap Shah 	if (num_delegations > max_delegations)
305ef0f3390SNeilBrown 		return NULL;
306996e0938SJ. Bruce Fields 	dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
3075b2d21c1SNeilBrown 	if (dp == NULL)
3081da177e4SLinus Torvalds 		return dp;
309996e0938SJ. Bruce Fields 	init_stid(&dp->dl_stid, clp, NFS4_DELEG_STID);
3102a74aba7SJ. Bruce Fields 	/*
3112a74aba7SJ. Bruce Fields 	 * delegation seqid's are never incremented.  The 4.1 special
3126136d2b4SJ. Bruce Fields 	 * meaning of seqid 0 isn't meaningful, really, but let's avoid
3136136d2b4SJ. Bruce Fields 	 * 0 anyway just for consistency and use 1:
3142a74aba7SJ. Bruce Fields 	 */
3152a74aba7SJ. Bruce Fields 	dp->dl_stid.sc_stateid.si_generation = 1;
316ef0f3390SNeilBrown 	num_delegations++;
317ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perfile);
318ea1da636SNeilBrown 	INIT_LIST_HEAD(&dp->dl_perclnt);
3191da177e4SLinus Torvalds 	INIT_LIST_HEAD(&dp->dl_recall_lru);
32013cd2184SNeilBrown 	get_nfs4_file(fp);
3211da177e4SLinus Torvalds 	dp->dl_file = fp;
3221da177e4SLinus Torvalds 	dp->dl_type = type;
3236c02eaa1SJ. Bruce Fields 	fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
3241da177e4SLinus Torvalds 	dp->dl_time = 0;
3251da177e4SLinus Torvalds 	atomic_set(&dp->dl_count, 1);
326b5a1a81eSJ. Bruce Fields 	INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
3271da177e4SLinus Torvalds 	return dp;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds void
3311da177e4SLinus Torvalds nfs4_put_delegation(struct nfs4_delegation *dp)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	if (atomic_dec_and_test(&dp->dl_count)) {
3341da177e4SLinus Torvalds 		dprintk("NFSD: freeing dp %p\n",dp);
33513cd2184SNeilBrown 		put_nfs4_file(dp->dl_file);
3365b2d21c1SNeilBrown 		kmem_cache_free(deleg_slab, dp);
337ef0f3390SNeilBrown 		num_delegations--;
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
341acfdf5c3SJ. Bruce Fields static void nfs4_put_deleg_lease(struct nfs4_file *fp)
3421da177e4SLinus Torvalds {
343acfdf5c3SJ. Bruce Fields 	if (atomic_dec_and_test(&fp->fi_delegees)) {
344acfdf5c3SJ. Bruce Fields 		vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
345acfdf5c3SJ. Bruce Fields 		fp->fi_lease = NULL;
3464ee63624SJ. Bruce Fields 		fput(fp->fi_deleg_file);
347acfdf5c3SJ. Bruce Fields 		fp->fi_deleg_file = NULL;
348acfdf5c3SJ. Bruce Fields 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3516136d2b4SJ. Bruce Fields static void unhash_stid(struct nfs4_stid *s)
3526136d2b4SJ. Bruce Fields {
35338c2f4b1SJ. Bruce Fields 	struct idr *stateids = &s->sc_client->cl_stateids;
35438c2f4b1SJ. Bruce Fields 
35538c2f4b1SJ. Bruce Fields 	idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
3566136d2b4SJ. Bruce Fields }
3576136d2b4SJ. Bruce Fields 
3581da177e4SLinus Torvalds /* Called under the state lock. */
3591da177e4SLinus Torvalds static void
3601da177e4SLinus Torvalds unhash_delegation(struct nfs4_delegation *dp)
3611da177e4SLinus Torvalds {
3626136d2b4SJ. Bruce Fields 	unhash_stid(&dp->dl_stid);
363ea1da636SNeilBrown 	list_del_init(&dp->dl_perclnt);
3641da177e4SLinus Torvalds 	spin_lock(&recall_lock);
3655d926e8cSJ. Bruce Fields 	list_del_init(&dp->dl_perfile);
3661da177e4SLinus Torvalds 	list_del_init(&dp->dl_recall_lru);
3671da177e4SLinus Torvalds 	spin_unlock(&recall_lock);
368acfdf5c3SJ. Bruce Fields 	nfs4_put_deleg_lease(dp->dl_file);
3691da177e4SLinus Torvalds 	nfs4_put_delegation(dp);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds /*
3731da177e4SLinus Torvalds  * SETCLIENTID state
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds 
37636acb66bSBenny Halevy /* client_lock protects the client lru list and session hash table */
3779089f1b4SBenny Halevy static DEFINE_SPINLOCK(client_lock);
3789089f1b4SBenny Halevy 
3791da177e4SLinus Torvalds /* Hash tables for nfs4_clientid state */
3801da177e4SLinus Torvalds #define CLIENT_HASH_BITS                 4
3811da177e4SLinus Torvalds #define CLIENT_HASH_SIZE                (1 << CLIENT_HASH_BITS)
3821da177e4SLinus Torvalds #define CLIENT_HASH_MASK                (CLIENT_HASH_SIZE - 1)
3831da177e4SLinus Torvalds 
384ddc04c41SJ. Bruce Fields static unsigned int clientid_hashval(u32 id)
385ddc04c41SJ. Bruce Fields {
386ddc04c41SJ. Bruce Fields 	return id & CLIENT_HASH_MASK;
387ddc04c41SJ. Bruce Fields }
388ddc04c41SJ. Bruce Fields 
389ddc04c41SJ. Bruce Fields static unsigned int clientstr_hashval(const char *name)
390ddc04c41SJ. Bruce Fields {
391ddc04c41SJ. Bruce Fields 	return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
392ddc04c41SJ. Bruce Fields }
393ddc04c41SJ. Bruce Fields 
3941da177e4SLinus Torvalds /*
3951da177e4SLinus Torvalds  * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
3961da177e4SLinus Torvalds  * used in reboot/reset lease grace period processing
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
3991da177e4SLinus Torvalds  * setclientid_confirmed info.
4001da177e4SLinus Torvalds  *
4011da177e4SLinus Torvalds  * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
4021da177e4SLinus Torvalds  * setclientid info.
4031da177e4SLinus Torvalds  *
4041da177e4SLinus Torvalds  * client_lru holds client queue ordered by nfs4_client.cl_time
4051da177e4SLinus Torvalds  * for lease renewal.
4061da177e4SLinus Torvalds  *
4071da177e4SLinus Torvalds  * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
4081da177e4SLinus Torvalds  * for last close replay.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static struct list_head	reclaim_str_hashtbl[CLIENT_HASH_SIZE];
4111da177e4SLinus Torvalds static int reclaim_str_hashtbl_size = 0;
4121da177e4SLinus Torvalds static struct list_head	conf_id_hashtbl[CLIENT_HASH_SIZE];
4131da177e4SLinus Torvalds static struct list_head	conf_str_hashtbl[CLIENT_HASH_SIZE];
4141da177e4SLinus Torvalds static struct list_head	unconf_str_hashtbl[CLIENT_HASH_SIZE];
4151da177e4SLinus Torvalds static struct list_head	unconf_id_hashtbl[CLIENT_HASH_SIZE];
4161da177e4SLinus Torvalds static struct list_head client_lru;
4171da177e4SLinus Torvalds static struct list_head close_lru;
4181da177e4SLinus Torvalds 
419f9d7562fSJ. Bruce Fields /*
420f9d7562fSJ. Bruce Fields  * We store the NONE, READ, WRITE, and BOTH bits separately in the
421f9d7562fSJ. Bruce Fields  * st_{access,deny}_bmap field of the stateid, in order to track not
422f9d7562fSJ. Bruce Fields  * only what share bits are currently in force, but also what
423f9d7562fSJ. Bruce Fields  * combinations of share bits previous opens have used.  This allows us
424f9d7562fSJ. Bruce Fields  * to enforce the recommendation of rfc 3530 14.2.19 that the server
425f9d7562fSJ. Bruce Fields  * return an error if the client attempt to downgrade to a combination
426f9d7562fSJ. Bruce Fields  * of share bits not explicable by closing some of its previous opens.
427f9d7562fSJ. Bruce Fields  *
428f9d7562fSJ. Bruce Fields  * XXX: This enforcement is actually incomplete, since we don't keep
429f9d7562fSJ. Bruce Fields  * track of access/deny bit combinations; so, e.g., we allow:
430f9d7562fSJ. Bruce Fields  *
431f9d7562fSJ. Bruce Fields  *	OPEN allow read, deny write
432f9d7562fSJ. Bruce Fields  *	OPEN allow both, deny none
433f9d7562fSJ. Bruce Fields  *	DOWNGRADE allow read, deny none
434f9d7562fSJ. Bruce Fields  *
435f9d7562fSJ. Bruce Fields  * which we should reject.
436f9d7562fSJ. Bruce Fields  */
437f9d7562fSJ. Bruce Fields static void
438f9d7562fSJ. Bruce Fields set_access(unsigned int *access, unsigned long bmap) {
439f9d7562fSJ. Bruce Fields 	int i;
440f9d7562fSJ. Bruce Fields 
441f9d7562fSJ. Bruce Fields 	*access = 0;
442f9d7562fSJ. Bruce Fields 	for (i = 1; i < 4; i++) {
443f9d7562fSJ. Bruce Fields 		if (test_bit(i, &bmap))
444f9d7562fSJ. Bruce Fields 			*access |= i;
445f9d7562fSJ. Bruce Fields 	}
446f9d7562fSJ. Bruce Fields }
447f9d7562fSJ. Bruce Fields 
448f9d7562fSJ. Bruce Fields static void
449f9d7562fSJ. Bruce Fields set_deny(unsigned int *deny, unsigned long bmap) {
450f9d7562fSJ. Bruce Fields 	int i;
451f9d7562fSJ. Bruce Fields 
452f9d7562fSJ. Bruce Fields 	*deny = 0;
453f9d7562fSJ. Bruce Fields 	for (i = 0; i < 4; i++) {
454f9d7562fSJ. Bruce Fields 		if (test_bit(i, &bmap))
455f9d7562fSJ. Bruce Fields 			*deny |= i ;
456f9d7562fSJ. Bruce Fields 	}
457f9d7562fSJ. Bruce Fields }
458f9d7562fSJ. Bruce Fields 
459f9d7562fSJ. Bruce Fields static int
460dcef0413SJ. Bruce Fields test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
461f9d7562fSJ. Bruce Fields 	unsigned int access, deny;
462f9d7562fSJ. Bruce Fields 
463f9d7562fSJ. Bruce Fields 	set_access(&access, stp->st_access_bmap);
464f9d7562fSJ. Bruce Fields 	set_deny(&deny, stp->st_deny_bmap);
465f9d7562fSJ. Bruce Fields 	if ((access & open->op_share_deny) || (deny & open->op_share_access))
466f9d7562fSJ. Bruce Fields 		return 0;
467f9d7562fSJ. Bruce Fields 	return 1;
468f9d7562fSJ. Bruce Fields }
469f9d7562fSJ. Bruce Fields 
470f9d7562fSJ. Bruce Fields static int nfs4_access_to_omode(u32 access)
471f9d7562fSJ. Bruce Fields {
4728f34a430SJ. Bruce Fields 	switch (access & NFS4_SHARE_ACCESS_BOTH) {
473f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
474f9d7562fSJ. Bruce Fields 		return O_RDONLY;
475f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
476f9d7562fSJ. Bruce Fields 		return O_WRONLY;
477f9d7562fSJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
478f9d7562fSJ. Bruce Fields 		return O_RDWR;
479f9d7562fSJ. Bruce Fields 	}
480f9d7562fSJ. Bruce Fields 	BUG();
481f9d7562fSJ. Bruce Fields }
482f9d7562fSJ. Bruce Fields 
483dcef0413SJ. Bruce Fields static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
484529d7b2aSJ. Bruce Fields {
485529d7b2aSJ. Bruce Fields 	list_del(&stp->st_perfile);
486529d7b2aSJ. Bruce Fields 	list_del(&stp->st_perstateowner);
487529d7b2aSJ. Bruce Fields }
488529d7b2aSJ. Bruce Fields 
489dcef0413SJ. Bruce Fields static void close_generic_stateid(struct nfs4_ol_stateid *stp)
490529d7b2aSJ. Bruce Fields {
491499f3edcSJ. Bruce Fields 	int i;
4920997b173SJ. Bruce Fields 
49323fcf2ecSJ. Bruce Fields 	if (stp->st_access_bmap) {
494499f3edcSJ. Bruce Fields 		for (i = 1; i < 4; i++) {
495499f3edcSJ. Bruce Fields 			if (test_bit(i, &stp->st_access_bmap))
496499f3edcSJ. Bruce Fields 				nfs4_file_put_access(stp->st_file,
497499f3edcSJ. Bruce Fields 						nfs4_access_to_omode(i));
4984665e2baSJ. Bruce Fields 			__clear_bit(i, &stp->st_access_bmap);
499499f3edcSJ. Bruce Fields 		}
50023fcf2ecSJ. Bruce Fields 	}
501a96e5b90SOGAWA Hirofumi 	put_nfs4_file(stp->st_file);
5024665e2baSJ. Bruce Fields 	stp->st_file = NULL;
5034665e2baSJ. Bruce Fields }
5044665e2baSJ. Bruce Fields 
505dcef0413SJ. Bruce Fields static void free_generic_stateid(struct nfs4_ol_stateid *stp)
5064665e2baSJ. Bruce Fields {
507529d7b2aSJ. Bruce Fields 	kmem_cache_free(stateid_slab, stp);
508529d7b2aSJ. Bruce Fields }
509529d7b2aSJ. Bruce Fields 
510dcef0413SJ. Bruce Fields static void release_lock_stateid(struct nfs4_ol_stateid *stp)
511529d7b2aSJ. Bruce Fields {
512529d7b2aSJ. Bruce Fields 	struct file *file;
513529d7b2aSJ. Bruce Fields 
514529d7b2aSJ. Bruce Fields 	unhash_generic_stateid(stp);
5156136d2b4SJ. Bruce Fields 	unhash_stid(&stp->st_stid);
516529d7b2aSJ. Bruce Fields 	file = find_any_file(stp->st_file);
517529d7b2aSJ. Bruce Fields 	if (file)
518fe0750e5SJ. Bruce Fields 		locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
51938c387b5SJ. Bruce Fields 	close_generic_stateid(stp);
520529d7b2aSJ. Bruce Fields 	free_generic_stateid(stp);
521529d7b2aSJ. Bruce Fields }
522529d7b2aSJ. Bruce Fields 
523fe0750e5SJ. Bruce Fields static void unhash_lockowner(struct nfs4_lockowner *lo)
524529d7b2aSJ. Bruce Fields {
525dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
526529d7b2aSJ. Bruce Fields 
527fe0750e5SJ. Bruce Fields 	list_del(&lo->lo_owner.so_strhash);
528fe0750e5SJ. Bruce Fields 	list_del(&lo->lo_perstateid);
529009673b4SJ. Bruce Fields 	list_del(&lo->lo_owner_ino_hash);
530fe0750e5SJ. Bruce Fields 	while (!list_empty(&lo->lo_owner.so_stateids)) {
531fe0750e5SJ. Bruce Fields 		stp = list_first_entry(&lo->lo_owner.so_stateids,
532dcef0413SJ. Bruce Fields 				struct nfs4_ol_stateid, st_perstateowner);
533529d7b2aSJ. Bruce Fields 		release_lock_stateid(stp);
534529d7b2aSJ. Bruce Fields 	}
535529d7b2aSJ. Bruce Fields }
536529d7b2aSJ. Bruce Fields 
537fe0750e5SJ. Bruce Fields static void release_lockowner(struct nfs4_lockowner *lo)
538529d7b2aSJ. Bruce Fields {
539fe0750e5SJ. Bruce Fields 	unhash_lockowner(lo);
540fe0750e5SJ. Bruce Fields 	nfs4_free_lockowner(lo);
541529d7b2aSJ. Bruce Fields }
542529d7b2aSJ. Bruce Fields 
543529d7b2aSJ. Bruce Fields static void
544dcef0413SJ. Bruce Fields release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
545529d7b2aSJ. Bruce Fields {
546fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
547529d7b2aSJ. Bruce Fields 
548529d7b2aSJ. Bruce Fields 	while (!list_empty(&open_stp->st_lockowners)) {
549fe0750e5SJ. Bruce Fields 		lo = list_entry(open_stp->st_lockowners.next,
550fe0750e5SJ. Bruce Fields 				struct nfs4_lockowner, lo_perstateid);
551fe0750e5SJ. Bruce Fields 		release_lockowner(lo);
552529d7b2aSJ. Bruce Fields 	}
553529d7b2aSJ. Bruce Fields }
554529d7b2aSJ. Bruce Fields 
55538c387b5SJ. Bruce Fields static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
5562283963fSJ. Bruce Fields {
5572283963fSJ. Bruce Fields 	unhash_generic_stateid(stp);
5582283963fSJ. Bruce Fields 	release_stateid_lockowners(stp);
55938c387b5SJ. Bruce Fields 	close_generic_stateid(stp);
56038c387b5SJ. Bruce Fields }
56138c387b5SJ. Bruce Fields 
56238c387b5SJ. Bruce Fields static void release_open_stateid(struct nfs4_ol_stateid *stp)
56338c387b5SJ. Bruce Fields {
56438c387b5SJ. Bruce Fields 	unhash_open_stateid(stp);
5656136d2b4SJ. Bruce Fields 	unhash_stid(&stp->st_stid);
5662283963fSJ. Bruce Fields 	free_generic_stateid(stp);
5672283963fSJ. Bruce Fields }
5682283963fSJ. Bruce Fields 
569fe0750e5SJ. Bruce Fields static void unhash_openowner(struct nfs4_openowner *oo)
570f1d110caSJ. Bruce Fields {
571dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
572f1d110caSJ. Bruce Fields 
573fe0750e5SJ. Bruce Fields 	list_del(&oo->oo_owner.so_strhash);
574fe0750e5SJ. Bruce Fields 	list_del(&oo->oo_perclient);
575fe0750e5SJ. Bruce Fields 	while (!list_empty(&oo->oo_owner.so_stateids)) {
576fe0750e5SJ. Bruce Fields 		stp = list_first_entry(&oo->oo_owner.so_stateids,
577dcef0413SJ. Bruce Fields 				struct nfs4_ol_stateid, st_perstateowner);
578f1d110caSJ. Bruce Fields 		release_open_stateid(stp);
579f1d110caSJ. Bruce Fields 	}
580f1d110caSJ. Bruce Fields }
581f1d110caSJ. Bruce Fields 
582f7a4d872SJ. Bruce Fields static void release_last_closed_stateid(struct nfs4_openowner *oo)
583f7a4d872SJ. Bruce Fields {
584f7a4d872SJ. Bruce Fields 	struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
585f7a4d872SJ. Bruce Fields 
586f7a4d872SJ. Bruce Fields 	if (s) {
5876136d2b4SJ. Bruce Fields 		unhash_stid(&s->st_stid);
588f7a4d872SJ. Bruce Fields 		free_generic_stateid(s);
589f7a4d872SJ. Bruce Fields 		oo->oo_last_closed_stid = NULL;
590f7a4d872SJ. Bruce Fields 	}
591f7a4d872SJ. Bruce Fields }
592f7a4d872SJ. Bruce Fields 
593fe0750e5SJ. Bruce Fields static void release_openowner(struct nfs4_openowner *oo)
594f1d110caSJ. Bruce Fields {
595fe0750e5SJ. Bruce Fields 	unhash_openowner(oo);
596fe0750e5SJ. Bruce Fields 	list_del(&oo->oo_close_lru);
597f7a4d872SJ. Bruce Fields 	release_last_closed_stateid(oo);
598fe0750e5SJ. Bruce Fields 	nfs4_free_openowner(oo);
599f1d110caSJ. Bruce Fields }
600f1d110caSJ. Bruce Fields 
6015282fd72SMarc Eshel #define SESSION_HASH_SIZE	512
6025282fd72SMarc Eshel static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
6035282fd72SMarc Eshel 
6045282fd72SMarc Eshel static inline int
6055282fd72SMarc Eshel hash_sessionid(struct nfs4_sessionid *sessionid)
6065282fd72SMarc Eshel {
6075282fd72SMarc Eshel 	struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
6085282fd72SMarc Eshel 
6095282fd72SMarc Eshel 	return sid->sequence % SESSION_HASH_SIZE;
6105282fd72SMarc Eshel }
6115282fd72SMarc Eshel 
6125282fd72SMarc Eshel static inline void
6135282fd72SMarc Eshel dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
6145282fd72SMarc Eshel {
6155282fd72SMarc Eshel 	u32 *ptr = (u32 *)(&sessionid->data[0]);
6165282fd72SMarc Eshel 	dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
6175282fd72SMarc Eshel }
6185282fd72SMarc Eshel 
619ec6b5d7bSAndy Adamson static void
620ec6b5d7bSAndy Adamson gen_sessionid(struct nfsd4_session *ses)
621ec6b5d7bSAndy Adamson {
622ec6b5d7bSAndy Adamson 	struct nfs4_client *clp = ses->se_client;
623ec6b5d7bSAndy Adamson 	struct nfsd4_sessionid *sid;
624ec6b5d7bSAndy Adamson 
625ec6b5d7bSAndy Adamson 	sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
626ec6b5d7bSAndy Adamson 	sid->clientid = clp->cl_clientid;
627ec6b5d7bSAndy Adamson 	sid->sequence = current_sessionid++;
628ec6b5d7bSAndy Adamson 	sid->reserved = 0;
629ec6b5d7bSAndy Adamson }
630ec6b5d7bSAndy Adamson 
631ec6b5d7bSAndy Adamson /*
632a649637cSAndy Adamson  * The protocol defines ca_maxresponssize_cached to include the size of
633a649637cSAndy Adamson  * the rpc header, but all we need to cache is the data starting after
634a649637cSAndy Adamson  * the end of the initial SEQUENCE operation--the rest we regenerate
635a649637cSAndy Adamson  * each time.  Therefore we can advertise a ca_maxresponssize_cached
636a649637cSAndy Adamson  * value that is the number of bytes in our cache plus a few additional
637a649637cSAndy Adamson  * bytes.  In order to stay on the safe side, and not promise more than
638a649637cSAndy Adamson  * we can cache, those additional bytes must be the minimum possible: 24
639a649637cSAndy Adamson  * bytes of rpc header (xid through accept state, with AUTH_NULL
640a649637cSAndy Adamson  * verifier), 12 for the compound header (with zero-length tag), and 44
641a649637cSAndy Adamson  * for the SEQUENCE op response:
642ec6b5d7bSAndy Adamson  */
643a649637cSAndy Adamson #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
644a649637cSAndy Adamson 
645557ce264SAndy Adamson static void
646557ce264SAndy Adamson free_session_slots(struct nfsd4_session *ses)
647557ce264SAndy Adamson {
648557ce264SAndy Adamson 	int i;
649557ce264SAndy Adamson 
650557ce264SAndy Adamson 	for (i = 0; i < ses->se_fchannel.maxreqs; i++)
651557ce264SAndy Adamson 		kfree(ses->se_slots[i]);
652557ce264SAndy Adamson }
653557ce264SAndy Adamson 
654efe0cb6dSJ. Bruce Fields /*
655efe0cb6dSJ. Bruce Fields  * We don't actually need to cache the rpc and session headers, so we
656efe0cb6dSJ. Bruce Fields  * can allocate a little less for each slot:
657efe0cb6dSJ. Bruce Fields  */
658efe0cb6dSJ. Bruce Fields static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
659efe0cb6dSJ. Bruce Fields {
660efe0cb6dSJ. Bruce Fields 	return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
661efe0cb6dSJ. Bruce Fields }
662efe0cb6dSJ. Bruce Fields 
6635b6feee9SJ. Bruce Fields static int nfsd4_sanitize_slot_size(u32 size)
664ec6b5d7bSAndy Adamson {
6655b6feee9SJ. Bruce Fields 	size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
6665b6feee9SJ. Bruce Fields 	size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
667ec6b5d7bSAndy Adamson 
6685b6feee9SJ. Bruce Fields 	return size;
669557ce264SAndy Adamson }
670557ce264SAndy Adamson 
6715b6feee9SJ. Bruce Fields /*
6725b6feee9SJ. Bruce Fields  * XXX: If we run out of reserved DRC memory we could (up to a point)
6735b6feee9SJ. Bruce Fields  * re-negotiate active sessions and reduce their slot usage to make
67442b2aa86SJustin P. Mattock  * room for new connections. For now we just fail the create session.
6755b6feee9SJ. Bruce Fields  */
6765b6feee9SJ. Bruce Fields static int nfsd4_get_drc_mem(int slotsize, u32 num)
6775b6feee9SJ. Bruce Fields {
6785b6feee9SJ. Bruce Fields 	int avail;
6795b6feee9SJ. Bruce Fields 
6805b6feee9SJ. Bruce Fields 	num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
6815b6feee9SJ. Bruce Fields 
6825b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
6835b6feee9SJ. Bruce Fields 	avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
6845b6feee9SJ. Bruce Fields 			nfsd_drc_max_mem - nfsd_drc_mem_used);
6855b6feee9SJ. Bruce Fields 	num = min_t(int, num, avail / slotsize);
6865b6feee9SJ. Bruce Fields 	nfsd_drc_mem_used += num * slotsize;
6875b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
6885b6feee9SJ. Bruce Fields 
6895b6feee9SJ. Bruce Fields 	return num;
6905b6feee9SJ. Bruce Fields }
6915b6feee9SJ. Bruce Fields 
6925b6feee9SJ. Bruce Fields static void nfsd4_put_drc_mem(int slotsize, int num)
6935b6feee9SJ. Bruce Fields {
6945b6feee9SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
6955b6feee9SJ. Bruce Fields 	nfsd_drc_mem_used -= slotsize * num;
6965b6feee9SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
6975b6feee9SJ. Bruce Fields }
6985b6feee9SJ. Bruce Fields 
6995b6feee9SJ. Bruce Fields static struct nfsd4_session *alloc_session(int slotsize, int numslots)
7005b6feee9SJ. Bruce Fields {
7015b6feee9SJ. Bruce Fields 	struct nfsd4_session *new;
7025b6feee9SJ. Bruce Fields 	int mem, i;
703ec6b5d7bSAndy Adamson 
704c23753daSJ. Bruce Fields 	BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
705ec6b5d7bSAndy Adamson 			+ sizeof(struct nfsd4_session) > PAGE_SIZE);
7065b6feee9SJ. Bruce Fields 	mem = numslots * sizeof(struct nfsd4_slot *);
707ec6b5d7bSAndy Adamson 
7085b6feee9SJ. Bruce Fields 	new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
7096c18ba9fSAlexandros Batsakis 	if (!new)
7105b6feee9SJ. Bruce Fields 		return NULL;
711ec6b5d7bSAndy Adamson 	/* allocate each struct nfsd4_slot and data cache in one piece */
7125b6feee9SJ. Bruce Fields 	for (i = 0; i < numslots; i++) {
7135b6feee9SJ. Bruce Fields 		mem = sizeof(struct nfsd4_slot) + slotsize;
7145b6feee9SJ. Bruce Fields 		new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
7155b6feee9SJ. Bruce Fields 		if (!new->se_slots[i])
716ec6b5d7bSAndy Adamson 			goto out_free;
717ec6b5d7bSAndy Adamson 	}
7185b6feee9SJ. Bruce Fields 	return new;
7195b6feee9SJ. Bruce Fields out_free:
7205b6feee9SJ. Bruce Fields 	while (i--)
7215b6feee9SJ. Bruce Fields 		kfree(new->se_slots[i]);
7225b6feee9SJ. Bruce Fields 	kfree(new);
7235b6feee9SJ. Bruce Fields 	return NULL;
7245b6feee9SJ. Bruce Fields }
7255b6feee9SJ. Bruce Fields 
7265b6feee9SJ. Bruce Fields static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
7275b6feee9SJ. Bruce Fields {
7285b6feee9SJ. Bruce Fields 	u32 maxrpc = nfsd_serv->sv_max_mesg;
7295b6feee9SJ. Bruce Fields 
7305b6feee9SJ. Bruce Fields 	new->maxreqs = numslots;
731d2b21743SMi Jinlong 	new->maxresp_cached = min_t(u32, req->maxresp_cached,
732d2b21743SMi Jinlong 					slotsize + NFSD_MIN_HDR_SEQ_SZ);
7335b6feee9SJ. Bruce Fields 	new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
7345b6feee9SJ. Bruce Fields 	new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
7355b6feee9SJ. Bruce Fields 	new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
7365b6feee9SJ. Bruce Fields }
7375b6feee9SJ. Bruce Fields 
73819cf5c02SJ. Bruce Fields static void free_conn(struct nfsd4_conn *c)
73919cf5c02SJ. Bruce Fields {
74019cf5c02SJ. Bruce Fields 	svc_xprt_put(c->cn_xprt);
74119cf5c02SJ. Bruce Fields 	kfree(c);
74219cf5c02SJ. Bruce Fields }
74319cf5c02SJ. Bruce Fields 
74419cf5c02SJ. Bruce Fields static void nfsd4_conn_lost(struct svc_xpt_user *u)
74519cf5c02SJ. Bruce Fields {
74619cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
74719cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = c->cn_session->se_client;
74819cf5c02SJ. Bruce Fields 
74919cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
75019cf5c02SJ. Bruce Fields 	if (!list_empty(&c->cn_persession)) {
75119cf5c02SJ. Bruce Fields 		list_del(&c->cn_persession);
75219cf5c02SJ. Bruce Fields 		free_conn(c);
75319cf5c02SJ. Bruce Fields 	}
75419cf5c02SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
755eea49806SJ. Bruce Fields 	nfsd4_probe_callback(clp);
75619cf5c02SJ. Bruce Fields }
75719cf5c02SJ. Bruce Fields 
758d29c374cSJ. Bruce Fields static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
759c7662518SJ. Bruce Fields {
760c7662518SJ. Bruce Fields 	struct nfsd4_conn *conn;
761c7662518SJ. Bruce Fields 
762c7662518SJ. Bruce Fields 	conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
763c7662518SJ. Bruce Fields 	if (!conn)
764db90681dSJ. Bruce Fields 		return NULL;
765c7662518SJ. Bruce Fields 	svc_xprt_get(rqstp->rq_xprt);
766c7662518SJ. Bruce Fields 	conn->cn_xprt = rqstp->rq_xprt;
767d29c374cSJ. Bruce Fields 	conn->cn_flags = flags;
768db90681dSJ. Bruce Fields 	INIT_LIST_HEAD(&conn->cn_xpt_user.list);
769db90681dSJ. Bruce Fields 	return conn;
770db90681dSJ. Bruce Fields }
771db90681dSJ. Bruce Fields 
772328ead28SJ. Bruce Fields static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
773328ead28SJ. Bruce Fields {
774328ead28SJ. Bruce Fields 	conn->cn_session = ses;
775328ead28SJ. Bruce Fields 	list_add(&conn->cn_persession, &ses->se_conns);
776328ead28SJ. Bruce Fields }
777328ead28SJ. Bruce Fields 
778db90681dSJ. Bruce Fields static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
779db90681dSJ. Bruce Fields {
780db90681dSJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
781c7662518SJ. Bruce Fields 
782c7662518SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
783328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(conn, ses);
784c7662518SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
785db90681dSJ. Bruce Fields }
786c7662518SJ. Bruce Fields 
78721b75b01SJ. Bruce Fields static int nfsd4_register_conn(struct nfsd4_conn *conn)
788db90681dSJ. Bruce Fields {
78919cf5c02SJ. Bruce Fields 	conn->cn_xpt_user.callback = nfsd4_conn_lost;
79021b75b01SJ. Bruce Fields 	return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
791db90681dSJ. Bruce Fields }
792db90681dSJ. Bruce Fields 
7931d1bc8f2SJ. Bruce Fields static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
794db90681dSJ. Bruce Fields {
795db90681dSJ. Bruce Fields 	struct nfsd4_conn *conn;
79621b75b01SJ. Bruce Fields 	int ret;
797db90681dSJ. Bruce Fields 
7981d1bc8f2SJ. Bruce Fields 	conn = alloc_conn(rqstp, dir);
799db90681dSJ. Bruce Fields 	if (!conn)
800db90681dSJ. Bruce Fields 		return nfserr_jukebox;
801db90681dSJ. Bruce Fields 	nfsd4_hash_conn(conn, ses);
80221b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(conn);
80321b75b01SJ. Bruce Fields 	if (ret)
80421b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
80521b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&conn->cn_xpt_user);
806c7662518SJ. Bruce Fields 	return nfs_ok;
807c7662518SJ. Bruce Fields }
808c7662518SJ. Bruce Fields 
8091d1bc8f2SJ. Bruce Fields static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
8101d1bc8f2SJ. Bruce Fields {
8111d1bc8f2SJ. Bruce Fields 	u32 dir = NFS4_CDFC4_FORE;
8121d1bc8f2SJ. Bruce Fields 
8131d1bc8f2SJ. Bruce Fields 	if (ses->se_flags & SESSION4_BACK_CHAN)
8141d1bc8f2SJ. Bruce Fields 		dir |= NFS4_CDFC4_BACK;
8151d1bc8f2SJ. Bruce Fields 
8161d1bc8f2SJ. Bruce Fields 	return nfsd4_new_conn(rqstp, ses, dir);
8171d1bc8f2SJ. Bruce Fields }
8181d1bc8f2SJ. Bruce Fields 
8191d1bc8f2SJ. Bruce Fields /* must be called under client_lock */
82019cf5c02SJ. Bruce Fields static void nfsd4_del_conns(struct nfsd4_session *s)
821c7662518SJ. Bruce Fields {
82219cf5c02SJ. Bruce Fields 	struct nfs4_client *clp = s->se_client;
82319cf5c02SJ. Bruce Fields 	struct nfsd4_conn *c;
82419cf5c02SJ. Bruce Fields 
82519cf5c02SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
82619cf5c02SJ. Bruce Fields 	while (!list_empty(&s->se_conns)) {
82719cf5c02SJ. Bruce Fields 		c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
82819cf5c02SJ. Bruce Fields 		list_del_init(&c->cn_persession);
82919cf5c02SJ. Bruce Fields 		spin_unlock(&clp->cl_lock);
83019cf5c02SJ. Bruce Fields 
83119cf5c02SJ. Bruce Fields 		unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
83219cf5c02SJ. Bruce Fields 		free_conn(c);
83319cf5c02SJ. Bruce Fields 
83419cf5c02SJ. Bruce Fields 		spin_lock(&clp->cl_lock);
83519cf5c02SJ. Bruce Fields 	}
83619cf5c02SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
837c7662518SJ. Bruce Fields }
838c7662518SJ. Bruce Fields 
839c7662518SJ. Bruce Fields void free_session(struct kref *kref)
840c7662518SJ. Bruce Fields {
841c7662518SJ. Bruce Fields 	struct nfsd4_session *ses;
842c7662518SJ. Bruce Fields 	int mem;
843c7662518SJ. Bruce Fields 
844c7662518SJ. Bruce Fields 	ses = container_of(kref, struct nfsd4_session, se_ref);
84519cf5c02SJ. Bruce Fields 	nfsd4_del_conns(ses);
846c7662518SJ. Bruce Fields 	spin_lock(&nfsd_drc_lock);
847c7662518SJ. Bruce Fields 	mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
848c7662518SJ. Bruce Fields 	nfsd_drc_mem_used -= mem;
849c7662518SJ. Bruce Fields 	spin_unlock(&nfsd_drc_lock);
850c7662518SJ. Bruce Fields 	free_session_slots(ses);
851c7662518SJ. Bruce Fields 	kfree(ses);
852c7662518SJ. Bruce Fields }
853c7662518SJ. Bruce Fields 
854ac7c46f2SJ. Bruce Fields static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
8555b6feee9SJ. Bruce Fields {
8565b6feee9SJ. Bruce Fields 	struct nfsd4_session *new;
8575b6feee9SJ. Bruce Fields 	struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
8585b6feee9SJ. Bruce Fields 	int numslots, slotsize;
859c7662518SJ. Bruce Fields 	int status;
8605b6feee9SJ. Bruce Fields 	int idx;
8615b6feee9SJ. Bruce Fields 
8625b6feee9SJ. Bruce Fields 	/*
8635b6feee9SJ. Bruce Fields 	 * Note decreasing slot size below client's request may
8645b6feee9SJ. Bruce Fields 	 * make it difficult for client to function correctly, whereas
8655b6feee9SJ. Bruce Fields 	 * decreasing the number of slots will (just?) affect
8665b6feee9SJ. Bruce Fields 	 * performance.  When short on memory we therefore prefer to
8675b6feee9SJ. Bruce Fields 	 * decrease number of slots instead of their size.
8685b6feee9SJ. Bruce Fields 	 */
8695b6feee9SJ. Bruce Fields 	slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
8705b6feee9SJ. Bruce Fields 	numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
871ced6dfe9SMi Jinlong 	if (numslots < 1)
872ced6dfe9SMi Jinlong 		return NULL;
8735b6feee9SJ. Bruce Fields 
8745b6feee9SJ. Bruce Fields 	new = alloc_session(slotsize, numslots);
8755b6feee9SJ. Bruce Fields 	if (!new) {
8765b6feee9SJ. Bruce Fields 		nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
877ac7c46f2SJ. Bruce Fields 		return NULL;
8785b6feee9SJ. Bruce Fields 	}
8795b6feee9SJ. Bruce Fields 	init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
880ec6b5d7bSAndy Adamson 
881ec6b5d7bSAndy Adamson 	new->se_client = clp;
882ec6b5d7bSAndy Adamson 	gen_sessionid(new);
883ec6b5d7bSAndy Adamson 
884c7662518SJ. Bruce Fields 	INIT_LIST_HEAD(&new->se_conns);
885c7662518SJ. Bruce Fields 
886ac7c46f2SJ. Bruce Fields 	new->se_cb_seq_nr = 1;
887ec6b5d7bSAndy Adamson 	new->se_flags = cses->flags;
8888b5ce5cdSJ. Bruce Fields 	new->se_cb_prog = cses->callback_prog;
889ec6b5d7bSAndy Adamson 	kref_init(&new->se_ref);
8905b6feee9SJ. Bruce Fields 	idx = hash_sessionid(&new->se_sessionid);
8919089f1b4SBenny Halevy 	spin_lock(&client_lock);
892ec6b5d7bSAndy Adamson 	list_add(&new->se_hash, &sessionid_hashtbl[idx]);
8934c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
894ec6b5d7bSAndy Adamson 	list_add(&new->se_perclnt, &clp->cl_sessions);
8954c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
8969089f1b4SBenny Halevy 	spin_unlock(&client_lock);
897ec6b5d7bSAndy Adamson 
8981d1bc8f2SJ. Bruce Fields 	status = nfsd4_new_conn_from_crses(rqstp, new);
899ac7c46f2SJ. Bruce Fields 	/* whoops: benny points out, status is ignored! (err, or bogus) */
900c7662518SJ. Bruce Fields 	if (status) {
901c7662518SJ. Bruce Fields 		free_session(&new->se_ref);
902ac7c46f2SJ. Bruce Fields 		return NULL;
903c7662518SJ. Bruce Fields 	}
904dcbeaa68SJ. Bruce Fields 	if (cses->flags & SESSION4_BACK_CHAN) {
905edd76786SJ. Bruce Fields 		struct sockaddr *sa = svc_addr(rqstp);
906dcbeaa68SJ. Bruce Fields 		/*
907dcbeaa68SJ. Bruce Fields 		 * This is a little silly; with sessions there's no real
908dcbeaa68SJ. Bruce Fields 		 * use for the callback address.  Use the peer address
909dcbeaa68SJ. Bruce Fields 		 * as a reasonable default for now, but consider fixing
910dcbeaa68SJ. Bruce Fields 		 * the rpc client not to require an address in the
911dcbeaa68SJ. Bruce Fields 		 * future:
912dcbeaa68SJ. Bruce Fields 		 */
913edd76786SJ. Bruce Fields 		rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
914edd76786SJ. Bruce Fields 		clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
915edd76786SJ. Bruce Fields 	}
916dcbeaa68SJ. Bruce Fields 	nfsd4_probe_callback(clp);
917ac7c46f2SJ. Bruce Fields 	return new;
918ec6b5d7bSAndy Adamson }
919ec6b5d7bSAndy Adamson 
9209089f1b4SBenny Halevy /* caller must hold client_lock */
9215282fd72SMarc Eshel static struct nfsd4_session *
9225282fd72SMarc Eshel find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
9235282fd72SMarc Eshel {
9245282fd72SMarc Eshel 	struct nfsd4_session *elem;
9255282fd72SMarc Eshel 	int idx;
9265282fd72SMarc Eshel 
9275282fd72SMarc Eshel 	dump_sessionid(__func__, sessionid);
9285282fd72SMarc Eshel 	idx = hash_sessionid(sessionid);
9295282fd72SMarc Eshel 	/* Search in the appropriate list */
9305282fd72SMarc Eshel 	list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
9315282fd72SMarc Eshel 		if (!memcmp(elem->se_sessionid.data, sessionid->data,
9325282fd72SMarc Eshel 			    NFS4_MAX_SESSIONID_LEN)) {
9335282fd72SMarc Eshel 			return elem;
9345282fd72SMarc Eshel 		}
9355282fd72SMarc Eshel 	}
9365282fd72SMarc Eshel 
9375282fd72SMarc Eshel 	dprintk("%s: session not found\n", __func__);
9385282fd72SMarc Eshel 	return NULL;
9395282fd72SMarc Eshel }
9405282fd72SMarc Eshel 
9419089f1b4SBenny Halevy /* caller must hold client_lock */
9427116ed6bSAndy Adamson static void
9435282fd72SMarc Eshel unhash_session(struct nfsd4_session *ses)
9447116ed6bSAndy Adamson {
9457116ed6bSAndy Adamson 	list_del(&ses->se_hash);
9464c649378SJ. Bruce Fields 	spin_lock(&ses->se_client->cl_lock);
9477116ed6bSAndy Adamson 	list_del(&ses->se_perclnt);
9484c649378SJ. Bruce Fields 	spin_unlock(&ses->se_client->cl_lock);
9495282fd72SMarc Eshel }
9505282fd72SMarc Eshel 
95136acb66bSBenny Halevy /* must be called under the client_lock */
9521da177e4SLinus Torvalds static inline void
95336acb66bSBenny Halevy renew_client_locked(struct nfs4_client *clp)
9541da177e4SLinus Torvalds {
95507cd4909SBenny Halevy 	if (is_client_expired(clp)) {
95607cd4909SBenny Halevy 		dprintk("%s: client (clientid %08x/%08x) already expired\n",
95707cd4909SBenny Halevy 			__func__,
95807cd4909SBenny Halevy 			clp->cl_clientid.cl_boot,
95907cd4909SBenny Halevy 			clp->cl_clientid.cl_id);
96007cd4909SBenny Halevy 		return;
96107cd4909SBenny Halevy 	}
96207cd4909SBenny Halevy 
9631da177e4SLinus Torvalds 	dprintk("renewing client (clientid %08x/%08x)\n",
9641da177e4SLinus Torvalds 			clp->cl_clientid.cl_boot,
9651da177e4SLinus Torvalds 			clp->cl_clientid.cl_id);
9661da177e4SLinus Torvalds 	list_move_tail(&clp->cl_lru, &client_lru);
9671da177e4SLinus Torvalds 	clp->cl_time = get_seconds();
9681da177e4SLinus Torvalds }
9691da177e4SLinus Torvalds 
97036acb66bSBenny Halevy static inline void
97136acb66bSBenny Halevy renew_client(struct nfs4_client *clp)
97236acb66bSBenny Halevy {
97336acb66bSBenny Halevy 	spin_lock(&client_lock);
97436acb66bSBenny Halevy 	renew_client_locked(clp);
97536acb66bSBenny Halevy 	spin_unlock(&client_lock);
97636acb66bSBenny Halevy }
97736acb66bSBenny Halevy 
9781da177e4SLinus Torvalds /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
9791da177e4SLinus Torvalds static int
9801da177e4SLinus Torvalds STALE_CLIENTID(clientid_t *clid)
9811da177e4SLinus Torvalds {
9821da177e4SLinus Torvalds 	if (clid->cl_boot == boot_time)
9831da177e4SLinus Torvalds 		return 0;
98460adfc50SAndy Adamson 	dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
98560adfc50SAndy Adamson 		clid->cl_boot, clid->cl_id, boot_time);
9861da177e4SLinus Torvalds 	return 1;
9871da177e4SLinus Torvalds }
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds /*
9901da177e4SLinus Torvalds  * XXX Should we use a slab cache ?
9911da177e4SLinus Torvalds  * This type of memory management is somewhat inefficient, but we use it
9921da177e4SLinus Torvalds  * anyway since SETCLIENTID is not a common operation.
9931da177e4SLinus Torvalds  */
99435bba9a3SJ. Bruce Fields static struct nfs4_client *alloc_client(struct xdr_netobj name)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	struct nfs4_client *clp;
9971da177e4SLinus Torvalds 
99835bba9a3SJ. Bruce Fields 	clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
99935bba9a3SJ. Bruce Fields 	if (clp == NULL)
100035bba9a3SJ. Bruce Fields 		return NULL;
100167114fe6SThomas Meyer 	clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
100235bba9a3SJ. Bruce Fields 	if (clp->cl_name.data == NULL) {
100335bba9a3SJ. Bruce Fields 		kfree(clp);
100435bba9a3SJ. Bruce Fields 		return NULL;
100535bba9a3SJ. Bruce Fields 	}
10061da177e4SLinus Torvalds 	clp->cl_name.len = name.len;
10071da177e4SLinus Torvalds 	return clp;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
10101da177e4SLinus Torvalds static inline void
10111da177e4SLinus Torvalds free_client(struct nfs4_client *clp)
10121da177e4SLinus Torvalds {
1013792c95ddSJ. Bruce Fields 	while (!list_empty(&clp->cl_sessions)) {
1014792c95ddSJ. Bruce Fields 		struct nfsd4_session *ses;
1015792c95ddSJ. Bruce Fields 		ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1016792c95ddSJ. Bruce Fields 				se_perclnt);
1017792c95ddSJ. Bruce Fields 		list_del(&ses->se_perclnt);
1018792c95ddSJ. Bruce Fields 		nfsd4_put_session(ses);
1019792c95ddSJ. Bruce Fields 	}
10201da177e4SLinus Torvalds 	if (clp->cl_cred.cr_group_info)
10211da177e4SLinus Torvalds 		put_group_info(clp->cl_cred.cr_group_info);
102268e76ad0SOlga Kornievskaia 	kfree(clp->cl_principal);
10231da177e4SLinus Torvalds 	kfree(clp->cl_name.data);
10241da177e4SLinus Torvalds 	kfree(clp);
10251da177e4SLinus Torvalds }
10261da177e4SLinus Torvalds 
1027d7682988SBenny Halevy void
1028d7682988SBenny Halevy release_session_client(struct nfsd4_session *session)
1029d7682988SBenny Halevy {
1030d7682988SBenny Halevy 	struct nfs4_client *clp = session->se_client;
1031d7682988SBenny Halevy 
1032d7682988SBenny Halevy 	if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
1033d7682988SBenny Halevy 		return;
1034d7682988SBenny Halevy 	if (is_client_expired(clp)) {
1035d7682988SBenny Halevy 		free_client(clp);
1036d7682988SBenny Halevy 		session->se_client = NULL;
1037d7682988SBenny Halevy 	} else
1038d7682988SBenny Halevy 		renew_client_locked(clp);
1039d7682988SBenny Halevy 	spin_unlock(&client_lock);
1040d7682988SBenny Halevy }
1041d7682988SBenny Halevy 
104284d38ac9SBenny Halevy /* must be called under the client_lock */
104384d38ac9SBenny Halevy static inline void
104484d38ac9SBenny Halevy unhash_client_locked(struct nfs4_client *clp)
104584d38ac9SBenny Halevy {
1046792c95ddSJ. Bruce Fields 	struct nfsd4_session *ses;
1047792c95ddSJ. Bruce Fields 
104807cd4909SBenny Halevy 	mark_client_expired(clp);
104984d38ac9SBenny Halevy 	list_del(&clp->cl_lru);
10504c649378SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1051792c95ddSJ. Bruce Fields 	list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1052792c95ddSJ. Bruce Fields 		list_del_init(&ses->se_hash);
10534c649378SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
105484d38ac9SBenny Halevy }
105584d38ac9SBenny Halevy 
10561da177e4SLinus Torvalds static void
10571da177e4SLinus Torvalds expire_client(struct nfs4_client *clp)
10581da177e4SLinus Torvalds {
1059fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
10601da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
10611da177e4SLinus Torvalds 	struct list_head reaplist;
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
10641da177e4SLinus Torvalds 	spin_lock(&recall_lock);
1065ea1da636SNeilBrown 	while (!list_empty(&clp->cl_delegations)) {
1066ea1da636SNeilBrown 		dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1067ea1da636SNeilBrown 		list_del_init(&dp->dl_perclnt);
10681da177e4SLinus Torvalds 		list_move(&dp->dl_recall_lru, &reaplist);
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 	spin_unlock(&recall_lock);
10711da177e4SLinus Torvalds 	while (!list_empty(&reaplist)) {
10721da177e4SLinus Torvalds 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
10731da177e4SLinus Torvalds 		unhash_delegation(dp);
10741da177e4SLinus Torvalds 	}
1075ea1da636SNeilBrown 	while (!list_empty(&clp->cl_openowners)) {
1076fe0750e5SJ. Bruce Fields 		oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1077fe0750e5SJ. Bruce Fields 		release_openowner(oo);
10781da177e4SLinus Torvalds 	}
10796ff8da08SJ. Bruce Fields 	nfsd4_shutdown_callback(clp);
10802bf23875SJ. Bruce Fields 	if (clp->cl_cb_conn.cb_xprt)
10812bf23875SJ. Bruce Fields 		svc_xprt_put(clp->cl_cb_conn.cb_xprt);
108284d38ac9SBenny Halevy 	list_del(&clp->cl_idhash);
108384d38ac9SBenny Halevy 	list_del(&clp->cl_strhash);
108484d38ac9SBenny Halevy 	spin_lock(&client_lock);
108584d38ac9SBenny Halevy 	unhash_client_locked(clp);
108646583e25SBenny Halevy 	if (atomic_read(&clp->cl_refcount) == 0)
1087b12a05cbSJ. Bruce Fields 		free_client(clp);
108846583e25SBenny Halevy 	spin_unlock(&client_lock);
10891da177e4SLinus Torvalds }
10901da177e4SLinus Torvalds 
109135bba9a3SJ. Bruce Fields static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
109235bba9a3SJ. Bruce Fields {
109335bba9a3SJ. Bruce Fields 	memcpy(target->cl_verifier.data, source->data,
109435bba9a3SJ. Bruce Fields 			sizeof(target->cl_verifier.data));
10951da177e4SLinus Torvalds }
10961da177e4SLinus Torvalds 
109735bba9a3SJ. Bruce Fields static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
109835bba9a3SJ. Bruce Fields {
10991da177e4SLinus Torvalds 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
11001da177e4SLinus Torvalds 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds 
110335bba9a3SJ. Bruce Fields static void copy_cred(struct svc_cred *target, struct svc_cred *source)
110435bba9a3SJ. Bruce Fields {
11051da177e4SLinus Torvalds 	target->cr_uid = source->cr_uid;
11061da177e4SLinus Torvalds 	target->cr_gid = source->cr_gid;
11071da177e4SLinus Torvalds 	target->cr_group_info = source->cr_group_info;
11081da177e4SLinus Torvalds 	get_group_info(target->cr_group_info);
11091da177e4SLinus Torvalds }
11101da177e4SLinus Torvalds 
111135bba9a3SJ. Bruce Fields static int same_name(const char *n1, const char *n2)
1112599e0a22SJ. Bruce Fields {
1113a55370a3SNeilBrown 	return 0 == memcmp(n1, n2, HEXDIR_LEN);
11141da177e4SLinus Torvalds }
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds static int
1117599e0a22SJ. Bruce Fields same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1118599e0a22SJ. Bruce Fields {
1119599e0a22SJ. Bruce Fields 	return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
11201da177e4SLinus Torvalds }
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds static int
1123599e0a22SJ. Bruce Fields same_clid(clientid_t *cl1, clientid_t *cl2)
1124599e0a22SJ. Bruce Fields {
1125599e0a22SJ. Bruce Fields 	return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds /* XXX what about NGROUP */
11291da177e4SLinus Torvalds static int
1130599e0a22SJ. Bruce Fields same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1131599e0a22SJ. Bruce Fields {
1132599e0a22SJ. Bruce Fields 	return cr1->cr_uid == cr2->cr_uid;
11331da177e4SLinus Torvalds }
11341da177e4SLinus Torvalds 
11355ec7b46cSJ. Bruce Fields static void gen_clid(struct nfs4_client *clp)
11365ec7b46cSJ. Bruce Fields {
11375ec7b46cSJ. Bruce Fields 	static u32 current_clientid = 1;
11385ec7b46cSJ. Bruce Fields 
11391da177e4SLinus Torvalds 	clp->cl_clientid.cl_boot = boot_time;
11401da177e4SLinus Torvalds 	clp->cl_clientid.cl_id = current_clientid++;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds 
1143deda2faaSJ. Bruce Fields static void gen_confirm(struct nfs4_client *clp)
1144deda2faaSJ. Bruce Fields {
1145deda2faaSJ. Bruce Fields 	static u32 i;
11461da177e4SLinus Torvalds 	u32 *p;
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds 	p = (u32 *)clp->cl_confirm.data;
1149deda2faaSJ. Bruce Fields 	*p++ = get_seconds();
1150deda2faaSJ. Bruce Fields 	*p++ = i++;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds 
115338c2f4b1SJ. Bruce Fields static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
11544581d140SJ. Bruce Fields {
115538c2f4b1SJ. Bruce Fields 	return idr_find(&cl->cl_stateids, t->si_opaque.so_id);
11564581d140SJ. Bruce Fields }
11574d71ab87SJ. Bruce Fields 
115838c2f4b1SJ. Bruce Fields static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1159f459e453SJ. Bruce Fields {
1160f459e453SJ. Bruce Fields 	struct nfs4_stid *s;
1161f459e453SJ. Bruce Fields 
116238c2f4b1SJ. Bruce Fields 	s = find_stateid(cl, t);
1163f459e453SJ. Bruce Fields 	if (!s)
1164f459e453SJ. Bruce Fields 		return NULL;
1165f459e453SJ. Bruce Fields 	if (typemask & s->sc_type)
11664d71ab87SJ. Bruce Fields 		return s;
11674581d140SJ. Bruce Fields 	return NULL;
11684581d140SJ. Bruce Fields }
11694581d140SJ. Bruce Fields 
1170b09333c4SRicardo Labiaga static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1171b09333c4SRicardo Labiaga 		struct svc_rqst *rqstp, nfs4_verifier *verf)
1172b09333c4SRicardo Labiaga {
1173b09333c4SRicardo Labiaga 	struct nfs4_client *clp;
1174b09333c4SRicardo Labiaga 	struct sockaddr *sa = svc_addr(rqstp);
1175b09333c4SRicardo Labiaga 	char *princ;
1176b09333c4SRicardo Labiaga 
1177b09333c4SRicardo Labiaga 	clp = alloc_client(name);
1178b09333c4SRicardo Labiaga 	if (clp == NULL)
1179b09333c4SRicardo Labiaga 		return NULL;
1180b09333c4SRicardo Labiaga 
1181792c95ddSJ. Bruce Fields 	INIT_LIST_HEAD(&clp->cl_sessions);
1182792c95ddSJ. Bruce Fields 
1183b09333c4SRicardo Labiaga 	princ = svc_gss_principal(rqstp);
1184b09333c4SRicardo Labiaga 	if (princ) {
1185b09333c4SRicardo Labiaga 		clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1186b09333c4SRicardo Labiaga 		if (clp->cl_principal == NULL) {
1187b09333c4SRicardo Labiaga 			free_client(clp);
1188b09333c4SRicardo Labiaga 			return NULL;
1189b09333c4SRicardo Labiaga 		}
1190b09333c4SRicardo Labiaga 	}
1191b09333c4SRicardo Labiaga 
119238c2f4b1SJ. Bruce Fields 	idr_init(&clp->cl_stateids);
1193b09333c4SRicardo Labiaga 	memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
119446583e25SBenny Halevy 	atomic_set(&clp->cl_refcount, 0);
119577a3569dSJ. Bruce Fields 	clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1196b09333c4SRicardo Labiaga 	INIT_LIST_HEAD(&clp->cl_idhash);
1197b09333c4SRicardo Labiaga 	INIT_LIST_HEAD(&clp->cl_strhash);
1198b09333c4SRicardo Labiaga 	INIT_LIST_HEAD(&clp->cl_openowners);
1199b09333c4SRicardo Labiaga 	INIT_LIST_HEAD(&clp->cl_delegations);
1200b09333c4SRicardo Labiaga 	INIT_LIST_HEAD(&clp->cl_lru);
12015ce8ba25SJ. Bruce Fields 	INIT_LIST_HEAD(&clp->cl_callbacks);
12026ff8da08SJ. Bruce Fields 	spin_lock_init(&clp->cl_lock);
1203cee277d9SJ. Bruce Fields 	INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
120407cd4909SBenny Halevy 	clp->cl_time = get_seconds();
1205b09333c4SRicardo Labiaga 	clear_bit(0, &clp->cl_cb_slot_busy);
1206b09333c4SRicardo Labiaga 	rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1207b09333c4SRicardo Labiaga 	copy_verf(clp, verf);
1208b09333c4SRicardo Labiaga 	rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1209b09333c4SRicardo Labiaga 	clp->cl_flavor = rqstp->rq_flavor;
1210b09333c4SRicardo Labiaga 	copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1211b09333c4SRicardo Labiaga 	gen_confirm(clp);
1212edd76786SJ. Bruce Fields 	clp->cl_cb_session = NULL;
1213b09333c4SRicardo Labiaga 	return clp;
1214b09333c4SRicardo Labiaga }
1215b09333c4SRicardo Labiaga 
1216fd39ca9aSNeilBrown static void
12171da177e4SLinus Torvalds add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
12181da177e4SLinus Torvalds {
12191da177e4SLinus Torvalds 	unsigned int idhashval;
12201da177e4SLinus Torvalds 
12211da177e4SLinus Torvalds 	list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
12221da177e4SLinus Torvalds 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
12231da177e4SLinus Torvalds 	list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
122436acb66bSBenny Halevy 	renew_client(clp);
12251da177e4SLinus Torvalds }
12261da177e4SLinus Torvalds 
1227fd39ca9aSNeilBrown static void
12281da177e4SLinus Torvalds move_to_confirmed(struct nfs4_client *clp)
12291da177e4SLinus Torvalds {
12301da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
12311da177e4SLinus Torvalds 	unsigned int strhashval;
12321da177e4SLinus Torvalds 
12331da177e4SLinus Torvalds 	dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1234f116629dSAkinobu Mita 	list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1235a55370a3SNeilBrown 	strhashval = clientstr_hashval(clp->cl_recdir);
1236328efbabSBenny Halevy 	list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
12371da177e4SLinus Torvalds 	renew_client(clp);
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds static struct nfs4_client *
12411da177e4SLinus Torvalds find_confirmed_client(clientid_t *clid)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	struct nfs4_client *clp;
12441da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clid->cl_id);
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1247a50d2ad1SJ. Bruce Fields 		if (same_clid(&clp->cl_clientid, clid)) {
1248a50d2ad1SJ. Bruce Fields 			renew_client(clp);
12491da177e4SLinus Torvalds 			return clp;
12501da177e4SLinus Torvalds 		}
1251a50d2ad1SJ. Bruce Fields 	}
12521da177e4SLinus Torvalds 	return NULL;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds static struct nfs4_client *
12561da177e4SLinus Torvalds find_unconfirmed_client(clientid_t *clid)
12571da177e4SLinus Torvalds {
12581da177e4SLinus Torvalds 	struct nfs4_client *clp;
12591da177e4SLinus Torvalds 	unsigned int idhashval = clientid_hashval(clid->cl_id);
12601da177e4SLinus Torvalds 
12611da177e4SLinus Torvalds 	list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1262599e0a22SJ. Bruce Fields 		if (same_clid(&clp->cl_clientid, clid))
12631da177e4SLinus Torvalds 			return clp;
12641da177e4SLinus Torvalds 	}
12651da177e4SLinus Torvalds 	return NULL;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12686e5f15c9SJ. Bruce Fields static bool clp_used_exchangeid(struct nfs4_client *clp)
1269a1bcecd2SAndy Adamson {
12706e5f15c9SJ. Bruce Fields 	return clp->cl_exchange_flags != 0;
1271a1bcecd2SAndy Adamson }
1272a1bcecd2SAndy Adamson 
127328ce6054SNeilBrown static struct nfs4_client *
1274e203d506SJ. Bruce Fields find_confirmed_client_by_str(const char *dname, unsigned int hashval)
127528ce6054SNeilBrown {
127628ce6054SNeilBrown 	struct nfs4_client *clp;
127728ce6054SNeilBrown 
127828ce6054SNeilBrown 	list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1279e203d506SJ. Bruce Fields 		if (same_name(clp->cl_recdir, dname))
128028ce6054SNeilBrown 			return clp;
128128ce6054SNeilBrown 	}
128228ce6054SNeilBrown 	return NULL;
128328ce6054SNeilBrown }
128428ce6054SNeilBrown 
128528ce6054SNeilBrown static struct nfs4_client *
1286e203d506SJ. Bruce Fields find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
128728ce6054SNeilBrown {
128828ce6054SNeilBrown 	struct nfs4_client *clp;
128928ce6054SNeilBrown 
129028ce6054SNeilBrown 	list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1291e203d506SJ. Bruce Fields 		if (same_name(clp->cl_recdir, dname))
129228ce6054SNeilBrown 			return clp;
129328ce6054SNeilBrown 	}
129428ce6054SNeilBrown 	return NULL;
129528ce6054SNeilBrown }
129628ce6054SNeilBrown 
1297fd39ca9aSNeilBrown static void
12986f3d772fSTakuma Umeya gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
12991da177e4SLinus Torvalds {
130007263f1eSJ. Bruce Fields 	struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
13016f3d772fSTakuma Umeya 	struct sockaddr	*sa = svc_addr(rqstp);
13026f3d772fSTakuma Umeya 	u32 scopeid = rpc_get_scope_id(sa);
13037077ecbaSJeff Layton 	unsigned short expected_family;
13041da177e4SLinus Torvalds 
13057077ecbaSJeff Layton 	/* Currently, we only support tcp and tcp6 for the callback channel */
13067077ecbaSJeff Layton 	if (se->se_callback_netid_len == 3 &&
13077077ecbaSJeff Layton 	    !memcmp(se->se_callback_netid_val, "tcp", 3))
13087077ecbaSJeff Layton 		expected_family = AF_INET;
13097077ecbaSJeff Layton 	else if (se->se_callback_netid_len == 4 &&
13107077ecbaSJeff Layton 		 !memcmp(se->se_callback_netid_val, "tcp6", 4))
13117077ecbaSJeff Layton 		expected_family = AF_INET6;
13127077ecbaSJeff Layton 	else
13131da177e4SLinus Torvalds 		goto out_err;
13141da177e4SLinus Torvalds 
131507263f1eSJ. Bruce Fields 	conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1316aa9a4ec7SJeff Layton 					    se->se_callback_addr_len,
131707263f1eSJ. Bruce Fields 					    (struct sockaddr *)&conn->cb_addr,
131807263f1eSJ. Bruce Fields 					    sizeof(conn->cb_addr));
1319aa9a4ec7SJeff Layton 
132007263f1eSJ. Bruce Fields 	if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
13211da177e4SLinus Torvalds 		goto out_err;
1322aa9a4ec7SJeff Layton 
132307263f1eSJ. Bruce Fields 	if (conn->cb_addr.ss_family == AF_INET6)
132407263f1eSJ. Bruce Fields 		((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1325fbf4665fSJeff Layton 
132607263f1eSJ. Bruce Fields 	conn->cb_prog = se->se_callback_prog;
132707263f1eSJ. Bruce Fields 	conn->cb_ident = se->se_callback_ident;
1328849a1cf1SMi Jinlong 	memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
13291da177e4SLinus Torvalds 	return;
13301da177e4SLinus Torvalds out_err:
133107263f1eSJ. Bruce Fields 	conn->cb_addr.ss_family = AF_UNSPEC;
133207263f1eSJ. Bruce Fields 	conn->cb_addrlen = 0;
1333849823c5SNeil Brown 	dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
13341da177e4SLinus Torvalds 		"will not receive delegations\n",
13351da177e4SLinus Torvalds 		clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
13361da177e4SLinus Torvalds 
13371da177e4SLinus Torvalds 	return;
13381da177e4SLinus Torvalds }
13391da177e4SLinus Torvalds 
1340074fe897SAndy Adamson /*
1341557ce264SAndy Adamson  * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1342074fe897SAndy Adamson  */
1343074fe897SAndy Adamson void
1344074fe897SAndy Adamson nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1345074fe897SAndy Adamson {
1346557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
1347557ce264SAndy Adamson 	unsigned int base;
1348074fe897SAndy Adamson 
1349557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
1350074fe897SAndy Adamson 
1351557ce264SAndy Adamson 	slot->sl_opcnt = resp->opcnt;
1352557ce264SAndy Adamson 	slot->sl_status = resp->cstate.status;
1353bf864a31SAndy Adamson 
1354bf5c43c8SJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
1355bf864a31SAndy Adamson 	if (nfsd4_not_cached(resp)) {
1356557ce264SAndy Adamson 		slot->sl_datalen = 0;
1357bf864a31SAndy Adamson 		return;
1358bf864a31SAndy Adamson 	}
1359557ce264SAndy Adamson 	slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1360557ce264SAndy Adamson 	base = (char *)resp->cstate.datap -
1361557ce264SAndy Adamson 					(char *)resp->xbuf->head[0].iov_base;
1362557ce264SAndy Adamson 	if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1363557ce264SAndy Adamson 				    slot->sl_datalen))
1364557ce264SAndy Adamson 		WARN("%s: sessions DRC could not cache compound\n", __func__);
1365557ce264SAndy Adamson 	return;
1366074fe897SAndy Adamson }
1367074fe897SAndy Adamson 
1368074fe897SAndy Adamson /*
1369abfabf8cSAndy Adamson  * Encode the replay sequence operation from the slot values.
1370abfabf8cSAndy Adamson  * If cachethis is FALSE encode the uncached rep error on the next
1371abfabf8cSAndy Adamson  * operation which sets resp->p and increments resp->opcnt for
1372abfabf8cSAndy Adamson  * nfs4svc_encode_compoundres.
1373abfabf8cSAndy Adamson  *
1374074fe897SAndy Adamson  */
1375abfabf8cSAndy Adamson static __be32
1376abfabf8cSAndy Adamson nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1377abfabf8cSAndy Adamson 			  struct nfsd4_compoundres *resp)
1378074fe897SAndy Adamson {
1379abfabf8cSAndy Adamson 	struct nfsd4_op *op;
1380abfabf8cSAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
1381074fe897SAndy Adamson 
1382abfabf8cSAndy Adamson 	/* Encode the replayed sequence operation */
1383abfabf8cSAndy Adamson 	op = &args->ops[resp->opcnt - 1];
1384abfabf8cSAndy Adamson 	nfsd4_encode_operation(resp, op);
1385abfabf8cSAndy Adamson 
1386abfabf8cSAndy Adamson 	/* Return nfserr_retry_uncached_rep in next operation. */
138773e79482SJ. Bruce Fields 	if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
1388abfabf8cSAndy Adamson 		op = &args->ops[resp->opcnt++];
1389abfabf8cSAndy Adamson 		op->status = nfserr_retry_uncached_rep;
1390abfabf8cSAndy Adamson 		nfsd4_encode_operation(resp, op);
1391074fe897SAndy Adamson 	}
1392abfabf8cSAndy Adamson 	return op->status;
1393074fe897SAndy Adamson }
1394074fe897SAndy Adamson 
1395074fe897SAndy Adamson /*
1396557ce264SAndy Adamson  * The sequence operation is not cached because we can use the slot and
1397557ce264SAndy Adamson  * session values.
1398074fe897SAndy Adamson  */
1399074fe897SAndy Adamson __be32
1400bf864a31SAndy Adamson nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1401bf864a31SAndy Adamson 			 struct nfsd4_sequence *seq)
1402074fe897SAndy Adamson {
1403557ce264SAndy Adamson 	struct nfsd4_slot *slot = resp->cstate.slot;
1404074fe897SAndy Adamson 	__be32 status;
1405074fe897SAndy Adamson 
1406557ce264SAndy Adamson 	dprintk("--> %s slot %p\n", __func__, slot);
1407074fe897SAndy Adamson 
1408abfabf8cSAndy Adamson 	/* Either returns 0 or nfserr_retry_uncached */
1409abfabf8cSAndy Adamson 	status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1410abfabf8cSAndy Adamson 	if (status == nfserr_retry_uncached_rep)
1411abfabf8cSAndy Adamson 		return status;
1412074fe897SAndy Adamson 
1413557ce264SAndy Adamson 	/* The sequence operation has been encoded, cstate->datap set. */
1414557ce264SAndy Adamson 	memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1415074fe897SAndy Adamson 
1416557ce264SAndy Adamson 	resp->opcnt = slot->sl_opcnt;
1417557ce264SAndy Adamson 	resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1418557ce264SAndy Adamson 	status = slot->sl_status;
1419074fe897SAndy Adamson 
1420074fe897SAndy Adamson 	return status;
1421074fe897SAndy Adamson }
1422074fe897SAndy Adamson 
14230733d213SAndy Adamson /*
14240733d213SAndy Adamson  * Set the exchange_id flags returned by the server.
14250733d213SAndy Adamson  */
14260733d213SAndy Adamson static void
14270733d213SAndy Adamson nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
14280733d213SAndy Adamson {
14290733d213SAndy Adamson 	/* pNFS is not supported */
14300733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
14310733d213SAndy Adamson 
14320733d213SAndy Adamson 	/* Referrals are supported, Migration is not. */
14330733d213SAndy Adamson 	new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
14340733d213SAndy Adamson 
14350733d213SAndy Adamson 	/* set the wire flags to return to client. */
14360733d213SAndy Adamson 	clid->flags = new->cl_exchange_flags;
14370733d213SAndy Adamson }
14380733d213SAndy Adamson 
1439b37ad28bSAl Viro __be32
1440069b6ad4SAndy Adamson nfsd4_exchange_id(struct svc_rqst *rqstp,
1441069b6ad4SAndy Adamson 		  struct nfsd4_compound_state *cstate,
1442069b6ad4SAndy Adamson 		  struct nfsd4_exchange_id *exid)
1443069b6ad4SAndy Adamson {
14440733d213SAndy Adamson 	struct nfs4_client *unconf, *conf, *new;
14450733d213SAndy Adamson 	int status;
14460733d213SAndy Adamson 	unsigned int		strhashval;
14470733d213SAndy Adamson 	char			dname[HEXDIR_LEN];
1448363168b4SJeff Layton 	char			addr_str[INET6_ADDRSTRLEN];
14490733d213SAndy Adamson 	nfs4_verifier		verf = exid->verifier;
1450363168b4SJeff Layton 	struct sockaddr		*sa = svc_addr(rqstp);
14510733d213SAndy Adamson 
1452363168b4SJeff Layton 	rpc_ntop(sa, addr_str, sizeof(addr_str));
14530733d213SAndy Adamson 	dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1454363168b4SJeff Layton 		"ip_addr=%s flags %x, spa_how %d\n",
14550733d213SAndy Adamson 		__func__, rqstp, exid, exid->clname.len, exid->clname.data,
1456363168b4SJeff Layton 		addr_str, exid->flags, exid->spa_how);
14570733d213SAndy Adamson 
1458a084daf5SJ. Bruce Fields 	if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
14590733d213SAndy Adamson 		return nfserr_inval;
14600733d213SAndy Adamson 
14610733d213SAndy Adamson 	/* Currently only support SP4_NONE */
14620733d213SAndy Adamson 	switch (exid->spa_how) {
14630733d213SAndy Adamson 	case SP4_NONE:
14640733d213SAndy Adamson 		break;
14650733d213SAndy Adamson 	case SP4_SSV:
1466044bc1d4SJ. Bruce Fields 		return nfserr_serverfault;
14670733d213SAndy Adamson 	default:
14680733d213SAndy Adamson 		BUG();				/* checked by xdr code */
14690733d213SAndy Adamson 	case SP4_MACH_CRED:
14700733d213SAndy Adamson 		return nfserr_serverfault;	/* no excuse :-/ */
14710733d213SAndy Adamson 	}
14720733d213SAndy Adamson 
14730733d213SAndy Adamson 	status = nfs4_make_rec_clidname(dname, &exid->clname);
14740733d213SAndy Adamson 
14750733d213SAndy Adamson 	if (status)
14760733d213SAndy Adamson 		goto error;
14770733d213SAndy Adamson 
14780733d213SAndy Adamson 	strhashval = clientstr_hashval(dname);
14790733d213SAndy Adamson 
14800733d213SAndy Adamson 	nfs4_lock_state();
14810733d213SAndy Adamson 	status = nfs_ok;
14820733d213SAndy Adamson 
1483e203d506SJ. Bruce Fields 	conf = find_confirmed_client_by_str(dname, strhashval);
14840733d213SAndy Adamson 	if (conf) {
1485e203d506SJ. Bruce Fields 		if (!clp_used_exchangeid(conf)) {
1486e203d506SJ. Bruce Fields 			status = nfserr_clid_inuse; /* XXX: ? */
1487e203d506SJ. Bruce Fields 			goto out;
1488e203d506SJ. Bruce Fields 		}
14890733d213SAndy Adamson 		if (!same_verf(&verf, &conf->cl_verifier)) {
14900733d213SAndy Adamson 			/* 18.35.4 case 8 */
14910733d213SAndy Adamson 			if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
14920733d213SAndy Adamson 				status = nfserr_not_same;
14930733d213SAndy Adamson 				goto out;
14940733d213SAndy Adamson 			}
14950733d213SAndy Adamson 			/* Client reboot: destroy old state */
14960733d213SAndy Adamson 			expire_client(conf);
14970733d213SAndy Adamson 			goto out_new;
14980733d213SAndy Adamson 		}
14990733d213SAndy Adamson 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
15000733d213SAndy Adamson 			/* 18.35.4 case 9 */
15010733d213SAndy Adamson 			if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
15020733d213SAndy Adamson 				status = nfserr_perm;
15030733d213SAndy Adamson 				goto out;
15040733d213SAndy Adamson 			}
15050733d213SAndy Adamson 			expire_client(conf);
15060733d213SAndy Adamson 			goto out_new;
15070733d213SAndy Adamson 		}
15080733d213SAndy Adamson 		/*
15090733d213SAndy Adamson 		 * Set bit when the owner id and verifier map to an already
15100733d213SAndy Adamson 		 * confirmed client id (18.35.3).
15110733d213SAndy Adamson 		 */
15120733d213SAndy Adamson 		exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
15130733d213SAndy Adamson 
15140733d213SAndy Adamson 		/*
15150733d213SAndy Adamson 		 * Falling into 18.35.4 case 2, possible router replay.
15160733d213SAndy Adamson 		 * Leave confirmed record intact and return same result.
15170733d213SAndy Adamson 		 */
15180733d213SAndy Adamson 		copy_verf(conf, &verf);
15190733d213SAndy Adamson 		new = conf;
15200733d213SAndy Adamson 		goto out_copy;
15216ddbbbfeSMike Sager 	}
15226ddbbbfeSMike Sager 
15230733d213SAndy Adamson 	/* 18.35.4 case 7 */
15240733d213SAndy Adamson 	if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
15250733d213SAndy Adamson 		status = nfserr_noent;
15260733d213SAndy Adamson 		goto out;
15270733d213SAndy Adamson 	}
15280733d213SAndy Adamson 
1529e203d506SJ. Bruce Fields 	unconf  = find_unconfirmed_client_by_str(dname, strhashval);
15300733d213SAndy Adamson 	if (unconf) {
15310733d213SAndy Adamson 		/*
15320733d213SAndy Adamson 		 * Possible retry or client restart.  Per 18.35.4 case 4,
15330733d213SAndy Adamson 		 * a new unconfirmed record should be generated regardless
15340733d213SAndy Adamson 		 * of whether any properties have changed.
15350733d213SAndy Adamson 		 */
15360733d213SAndy Adamson 		expire_client(unconf);
15370733d213SAndy Adamson 	}
15380733d213SAndy Adamson 
15390733d213SAndy Adamson out_new:
15400733d213SAndy Adamson 	/* Normal case */
1541b09333c4SRicardo Labiaga 	new = create_client(exid->clname, dname, rqstp, &verf);
15420733d213SAndy Adamson 	if (new == NULL) {
15434731030dSJ. Bruce Fields 		status = nfserr_jukebox;
15440733d213SAndy Adamson 		goto out;
15450733d213SAndy Adamson 	}
15460733d213SAndy Adamson 
15470733d213SAndy Adamson 	gen_clid(new);
15480733d213SAndy Adamson 	add_to_unconfirmed(new, strhashval);
15490733d213SAndy Adamson out_copy:
15500733d213SAndy Adamson 	exid->clientid.cl_boot = new->cl_clientid.cl_boot;
15510733d213SAndy Adamson 	exid->clientid.cl_id = new->cl_clientid.cl_id;
15520733d213SAndy Adamson 
155338eb76a5SAndy Adamson 	exid->seqid = 1;
15540733d213SAndy Adamson 	nfsd4_set_ex_flags(new, exid);
15550733d213SAndy Adamson 
15560733d213SAndy Adamson 	dprintk("nfsd4_exchange_id seqid %d flags %x\n",
155749557cc7SAndy Adamson 		new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
15580733d213SAndy Adamson 	status = nfs_ok;
15590733d213SAndy Adamson 
15600733d213SAndy Adamson out:
15610733d213SAndy Adamson 	nfs4_unlock_state();
15620733d213SAndy Adamson error:
15630733d213SAndy Adamson 	dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
15640733d213SAndy Adamson 	return status;
1565069b6ad4SAndy Adamson }
1566069b6ad4SAndy Adamson 
1567b85d4c01SBenny Halevy static int
156888e588d5SAndy Adamson check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1569b85d4c01SBenny Halevy {
157088e588d5SAndy Adamson 	dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
157188e588d5SAndy Adamson 		slot_seqid);
1572b85d4c01SBenny Halevy 
1573b85d4c01SBenny Halevy 	/* The slot is in use, and no response has been sent. */
157488e588d5SAndy Adamson 	if (slot_inuse) {
157588e588d5SAndy Adamson 		if (seqid == slot_seqid)
1576b85d4c01SBenny Halevy 			return nfserr_jukebox;
1577b85d4c01SBenny Halevy 		else
1578b85d4c01SBenny Halevy 			return nfserr_seq_misordered;
1579b85d4c01SBenny Halevy 	}
1580f6d82485SJ. Bruce Fields 	/* Note unsigned 32-bit arithmetic handles wraparound: */
158188e588d5SAndy Adamson 	if (likely(seqid == slot_seqid + 1))
1582b85d4c01SBenny Halevy 		return nfs_ok;
158388e588d5SAndy Adamson 	if (seqid == slot_seqid)
1584b85d4c01SBenny Halevy 		return nfserr_replay_cache;
1585b85d4c01SBenny Halevy 	return nfserr_seq_misordered;
1586b85d4c01SBenny Halevy }
1587b85d4c01SBenny Halevy 
158849557cc7SAndy Adamson /*
158949557cc7SAndy Adamson  * Cache the create session result into the create session single DRC
159049557cc7SAndy Adamson  * slot cache by saving the xdr structure. sl_seqid has been set.
159149557cc7SAndy Adamson  * Do this for solo or embedded create session operations.
159249557cc7SAndy Adamson  */
159349557cc7SAndy Adamson static void
159449557cc7SAndy Adamson nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
159549557cc7SAndy Adamson 			   struct nfsd4_clid_slot *slot, int nfserr)
159649557cc7SAndy Adamson {
159749557cc7SAndy Adamson 	slot->sl_status = nfserr;
159849557cc7SAndy Adamson 	memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
159949557cc7SAndy Adamson }
160049557cc7SAndy Adamson 
160149557cc7SAndy Adamson static __be32
160249557cc7SAndy Adamson nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
160349557cc7SAndy Adamson 			    struct nfsd4_clid_slot *slot)
160449557cc7SAndy Adamson {
160549557cc7SAndy Adamson 	memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
160649557cc7SAndy Adamson 	return slot->sl_status;
160749557cc7SAndy Adamson }
160849557cc7SAndy Adamson 
16091b74c25bSMi Jinlong #define NFSD_MIN_REQ_HDR_SEQ_SZ	((\
16101b74c25bSMi Jinlong 			2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
16111b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
16121b74c25bSMi Jinlong 			3 +	/* version, opcount, opcode */ \
16131b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
16141b74c25bSMi Jinlong 				/* seqid, slotID, slotID, cache */ \
16151b74c25bSMi Jinlong 			4 ) * sizeof(__be32))
16161b74c25bSMi Jinlong 
16171b74c25bSMi Jinlong #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
16181b74c25bSMi Jinlong 			2 +	/* verifier: AUTH_NULL, length 0 */\
16191b74c25bSMi Jinlong 			1 +	/* status */ \
16201b74c25bSMi Jinlong 			1 +	/* MIN tag is length with zero, only length */ \
16211b74c25bSMi Jinlong 			3 +	/* opcount, opcode, opstatus*/ \
16221b74c25bSMi Jinlong 			XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
16231b74c25bSMi Jinlong 				/* seqid, slotID, slotID, slotID, status */ \
16241b74c25bSMi Jinlong 			5 ) * sizeof(__be32))
16251b74c25bSMi Jinlong 
16261b74c25bSMi Jinlong static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs fchannel)
16271b74c25bSMi Jinlong {
16281b74c25bSMi Jinlong 	return fchannel.maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ
16291b74c25bSMi Jinlong 		|| fchannel.maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ;
16301b74c25bSMi Jinlong }
16311b74c25bSMi Jinlong 
1632069b6ad4SAndy Adamson __be32
1633069b6ad4SAndy Adamson nfsd4_create_session(struct svc_rqst *rqstp,
1634069b6ad4SAndy Adamson 		     struct nfsd4_compound_state *cstate,
1635069b6ad4SAndy Adamson 		     struct nfsd4_create_session *cr_ses)
1636069b6ad4SAndy Adamson {
1637363168b4SJeff Layton 	struct sockaddr *sa = svc_addr(rqstp);
1638ec6b5d7bSAndy Adamson 	struct nfs4_client *conf, *unconf;
1639ac7c46f2SJ. Bruce Fields 	struct nfsd4_session *new;
164049557cc7SAndy Adamson 	struct nfsd4_clid_slot *cs_slot = NULL;
164186c3e16cSJ. Bruce Fields 	bool confirm_me = false;
1642ec6b5d7bSAndy Adamson 	int status = 0;
1643ec6b5d7bSAndy Adamson 
1644a62573dcSMi Jinlong 	if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1645a62573dcSMi Jinlong 		return nfserr_inval;
1646a62573dcSMi Jinlong 
1647ec6b5d7bSAndy Adamson 	nfs4_lock_state();
1648ec6b5d7bSAndy Adamson 	unconf = find_unconfirmed_client(&cr_ses->clientid);
1649ec6b5d7bSAndy Adamson 	conf = find_confirmed_client(&cr_ses->clientid);
1650ec6b5d7bSAndy Adamson 
1651ec6b5d7bSAndy Adamson 	if (conf) {
165249557cc7SAndy Adamson 		cs_slot = &conf->cl_cs_slot;
165349557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
165438eb76a5SAndy Adamson 		if (status == nfserr_replay_cache) {
1655ec6b5d7bSAndy Adamson 			dprintk("Got a create_session replay! seqid= %d\n",
165649557cc7SAndy Adamson 				cs_slot->sl_seqid);
165738eb76a5SAndy Adamson 			/* Return the cached reply status */
165849557cc7SAndy Adamson 			status = nfsd4_replay_create_session(cr_ses, cs_slot);
165938eb76a5SAndy Adamson 			goto out;
166049557cc7SAndy Adamson 		} else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1661ec6b5d7bSAndy Adamson 			status = nfserr_seq_misordered;
1662ec6b5d7bSAndy Adamson 			dprintk("Sequence misordered!\n");
1663ec6b5d7bSAndy Adamson 			dprintk("Expected seqid= %d but got seqid= %d\n",
166449557cc7SAndy Adamson 				cs_slot->sl_seqid, cr_ses->seqid);
1665ec6b5d7bSAndy Adamson 			goto out;
1666ec6b5d7bSAndy Adamson 		}
1667ec6b5d7bSAndy Adamson 	} else if (unconf) {
1668ec6b5d7bSAndy Adamson 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1669363168b4SJeff Layton 		    !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1670ec6b5d7bSAndy Adamson 			status = nfserr_clid_inuse;
1671ec6b5d7bSAndy Adamson 			goto out;
1672ec6b5d7bSAndy Adamson 		}
1673ec6b5d7bSAndy Adamson 
167449557cc7SAndy Adamson 		cs_slot = &unconf->cl_cs_slot;
167549557cc7SAndy Adamson 		status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
167638eb76a5SAndy Adamson 		if (status) {
167738eb76a5SAndy Adamson 			/* an unconfirmed replay returns misordered */
1678ec6b5d7bSAndy Adamson 			status = nfserr_seq_misordered;
1679cd5b8144SJ. Bruce Fields 			goto out;
1680ec6b5d7bSAndy Adamson 		}
1681ec6b5d7bSAndy Adamson 
168286c3e16cSJ. Bruce Fields 		confirm_me = true;
1683ec6b5d7bSAndy Adamson 		conf = unconf;
1684ec6b5d7bSAndy Adamson 	} else {
1685ec6b5d7bSAndy Adamson 		status = nfserr_stale_clientid;
1686ec6b5d7bSAndy Adamson 		goto out;
1687ec6b5d7bSAndy Adamson 	}
1688ec6b5d7bSAndy Adamson 
1689408b79bcSJ. Bruce Fields 	/*
16908323c3b2SJ. Bruce Fields 	 * XXX: we should probably set this at creation time, and check
16918323c3b2SJ. Bruce Fields 	 * for consistent minorversion use throughout:
16928323c3b2SJ. Bruce Fields 	 */
16938323c3b2SJ. Bruce Fields 	conf->cl_minorversion = 1;
16948323c3b2SJ. Bruce Fields 	/*
1695408b79bcSJ. Bruce Fields 	 * We do not support RDMA or persistent sessions
1696408b79bcSJ. Bruce Fields 	 */
1697408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_PERSIST;
1698408b79bcSJ. Bruce Fields 	cr_ses->flags &= ~SESSION4_RDMA;
1699408b79bcSJ. Bruce Fields 
17001b74c25bSMi Jinlong 	status = nfserr_toosmall;
17011b74c25bSMi Jinlong 	if (check_forechannel_attrs(cr_ses->fore_channel))
17021b74c25bSMi Jinlong 		goto out;
17031b74c25bSMi Jinlong 
1704ac7c46f2SJ. Bruce Fields 	status = nfserr_jukebox;
1705ac7c46f2SJ. Bruce Fields 	new = alloc_init_session(rqstp, conf, cr_ses);
1706ac7c46f2SJ. Bruce Fields 	if (!new)
1707ec6b5d7bSAndy Adamson 		goto out;
1708ac7c46f2SJ. Bruce Fields 	status = nfs_ok;
1709ac7c46f2SJ. Bruce Fields 	memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1710ec6b5d7bSAndy Adamson 	       NFS4_MAX_SESSIONID_LEN);
171112050657SMi Jinlong 	memcpy(&cr_ses->fore_channel, &new->se_fchannel,
171212050657SMi Jinlong 		sizeof(struct nfsd4_channel_attrs));
171386c3e16cSJ. Bruce Fields 	cs_slot->sl_seqid++;
171449557cc7SAndy Adamson 	cr_ses->seqid = cs_slot->sl_seqid;
1715ec6b5d7bSAndy Adamson 
171649557cc7SAndy Adamson 	/* cache solo and embedded create sessions under the state lock */
171749557cc7SAndy Adamson 	nfsd4_cache_create_session(cr_ses, cs_slot, status);
171886c3e16cSJ. Bruce Fields 	if (confirm_me)
171986c3e16cSJ. Bruce Fields 		move_to_confirmed(conf);
1720ec6b5d7bSAndy Adamson out:
1721ec6b5d7bSAndy Adamson 	nfs4_unlock_state();
1722ec6b5d7bSAndy Adamson 	dprintk("%s returns %d\n", __func__, ntohl(status));
1723ec6b5d7bSAndy Adamson 	return status;
1724069b6ad4SAndy Adamson }
1725069b6ad4SAndy Adamson 
172657716355SJ. Bruce Fields static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
172757716355SJ. Bruce Fields {
172857716355SJ. Bruce Fields 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
172957716355SJ. Bruce Fields 	struct nfsd4_compoundargs *argp = rqstp->rq_argp;
173057716355SJ. Bruce Fields 
173157716355SJ. Bruce Fields 	return argp->opcnt == resp->opcnt;
173257716355SJ. Bruce Fields }
173357716355SJ. Bruce Fields 
17341d1bc8f2SJ. Bruce Fields static __be32 nfsd4_map_bcts_dir(u32 *dir)
17351d1bc8f2SJ. Bruce Fields {
17361d1bc8f2SJ. Bruce Fields 	switch (*dir) {
17371d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE:
17381d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK:
17391d1bc8f2SJ. Bruce Fields 		return nfs_ok;
17401d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_FORE_OR_BOTH:
17411d1bc8f2SJ. Bruce Fields 	case NFS4_CDFC4_BACK_OR_BOTH:
17421d1bc8f2SJ. Bruce Fields 		*dir = NFS4_CDFC4_BOTH;
17431d1bc8f2SJ. Bruce Fields 		return nfs_ok;
17441d1bc8f2SJ. Bruce Fields 	};
17451d1bc8f2SJ. Bruce Fields 	return nfserr_inval;
17461d1bc8f2SJ. Bruce Fields }
17471d1bc8f2SJ. Bruce Fields 
17481d1bc8f2SJ. Bruce Fields __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
17491d1bc8f2SJ. Bruce Fields 		     struct nfsd4_compound_state *cstate,
17501d1bc8f2SJ. Bruce Fields 		     struct nfsd4_bind_conn_to_session *bcts)
17511d1bc8f2SJ. Bruce Fields {
17521d1bc8f2SJ. Bruce Fields 	__be32 status;
17531d1bc8f2SJ. Bruce Fields 
17541d1bc8f2SJ. Bruce Fields 	if (!nfsd4_last_compound_op(rqstp))
17551d1bc8f2SJ. Bruce Fields 		return nfserr_not_only_op;
17561d1bc8f2SJ. Bruce Fields 	spin_lock(&client_lock);
17571d1bc8f2SJ. Bruce Fields 	cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
17581d1bc8f2SJ. Bruce Fields 	/* Sorta weird: we only need the refcnt'ing because new_conn acquires
17591d1bc8f2SJ. Bruce Fields 	 * client_lock iself: */
17601d1bc8f2SJ. Bruce Fields 	if (cstate->session) {
17611d1bc8f2SJ. Bruce Fields 		nfsd4_get_session(cstate->session);
17621d1bc8f2SJ. Bruce Fields 		atomic_inc(&cstate->session->se_client->cl_refcount);
17631d1bc8f2SJ. Bruce Fields 	}
17641d1bc8f2SJ. Bruce Fields 	spin_unlock(&client_lock);
17651d1bc8f2SJ. Bruce Fields 	if (!cstate->session)
17661d1bc8f2SJ. Bruce Fields 		return nfserr_badsession;
17671d1bc8f2SJ. Bruce Fields 
17681d1bc8f2SJ. Bruce Fields 	status = nfsd4_map_bcts_dir(&bcts->dir);
17691db2b9ddSBryan Schumaker 	if (!status)
17701d1bc8f2SJ. Bruce Fields 		nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
17711db2b9ddSBryan Schumaker 	return status;
17721d1bc8f2SJ. Bruce Fields }
17731d1bc8f2SJ. Bruce Fields 
17745d4cec2fSJ. Bruce Fields static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
17755d4cec2fSJ. Bruce Fields {
17765d4cec2fSJ. Bruce Fields 	if (!session)
17775d4cec2fSJ. Bruce Fields 		return 0;
17785d4cec2fSJ. Bruce Fields 	return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
17795d4cec2fSJ. Bruce Fields }
17805d4cec2fSJ. Bruce Fields 
1781069b6ad4SAndy Adamson __be32
1782069b6ad4SAndy Adamson nfsd4_destroy_session(struct svc_rqst *r,
1783069b6ad4SAndy Adamson 		      struct nfsd4_compound_state *cstate,
1784069b6ad4SAndy Adamson 		      struct nfsd4_destroy_session *sessionid)
1785069b6ad4SAndy Adamson {
1786e10e0cfcSBenny Halevy 	struct nfsd4_session *ses;
1787e10e0cfcSBenny Halevy 	u32 status = nfserr_badsession;
1788e10e0cfcSBenny Halevy 
1789e10e0cfcSBenny Halevy 	/* Notes:
1790e10e0cfcSBenny Halevy 	 * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1791e10e0cfcSBenny Halevy 	 * - Should we return nfserr_back_chan_busy if waiting for
1792e10e0cfcSBenny Halevy 	 *   callbacks on to-be-destroyed session?
1793e10e0cfcSBenny Halevy 	 * - Do we need to clear any callback info from previous session?
1794e10e0cfcSBenny Halevy 	 */
1795e10e0cfcSBenny Halevy 
17965d4cec2fSJ. Bruce Fields 	if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
179757716355SJ. Bruce Fields 		if (!nfsd4_last_compound_op(r))
179857716355SJ. Bruce Fields 			return nfserr_not_only_op;
179957716355SJ. Bruce Fields 	}
1800e10e0cfcSBenny Halevy 	dump_sessionid(__func__, &sessionid->sessionid);
18019089f1b4SBenny Halevy 	spin_lock(&client_lock);
1802e10e0cfcSBenny Halevy 	ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1803e10e0cfcSBenny Halevy 	if (!ses) {
18049089f1b4SBenny Halevy 		spin_unlock(&client_lock);
1805e10e0cfcSBenny Halevy 		goto out;
1806e10e0cfcSBenny Halevy 	}
1807e10e0cfcSBenny Halevy 
1808e10e0cfcSBenny Halevy 	unhash_session(ses);
18099089f1b4SBenny Halevy 	spin_unlock(&client_lock);
1810e10e0cfcSBenny Halevy 
1811ab707e15SBenny Halevy 	nfs4_lock_state();
181284f5f7ccSJ. Bruce Fields 	nfsd4_probe_callback_sync(ses->se_client);
1813ab707e15SBenny Halevy 	nfs4_unlock_state();
181419cf5c02SJ. Bruce Fields 
181519cf5c02SJ. Bruce Fields 	nfsd4_del_conns(ses);
181619cf5c02SJ. Bruce Fields 
1817e10e0cfcSBenny Halevy 	nfsd4_put_session(ses);
1818e10e0cfcSBenny Halevy 	status = nfs_ok;
1819e10e0cfcSBenny Halevy out:
1820e10e0cfcSBenny Halevy 	dprintk("%s returns %d\n", __func__, ntohl(status));
1821e10e0cfcSBenny Halevy 	return status;
1822069b6ad4SAndy Adamson }
1823069b6ad4SAndy Adamson 
1824a663bdd8SJ. Bruce Fields static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1825328ead28SJ. Bruce Fields {
1826328ead28SJ. Bruce Fields 	struct nfsd4_conn *c;
1827328ead28SJ. Bruce Fields 
1828328ead28SJ. Bruce Fields 	list_for_each_entry(c, &s->se_conns, cn_persession) {
1829a663bdd8SJ. Bruce Fields 		if (c->cn_xprt == xpt) {
1830328ead28SJ. Bruce Fields 			return c;
1831328ead28SJ. Bruce Fields 		}
1832328ead28SJ. Bruce Fields 	}
1833328ead28SJ. Bruce Fields 	return NULL;
1834328ead28SJ. Bruce Fields }
1835328ead28SJ. Bruce Fields 
1836a663bdd8SJ. Bruce Fields static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1837328ead28SJ. Bruce Fields {
1838328ead28SJ. Bruce Fields 	struct nfs4_client *clp = ses->se_client;
1839a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *c;
184021b75b01SJ. Bruce Fields 	int ret;
1841328ead28SJ. Bruce Fields 
1842328ead28SJ. Bruce Fields 	spin_lock(&clp->cl_lock);
1843a663bdd8SJ. Bruce Fields 	c = __nfsd4_find_conn(new->cn_xprt, ses);
1844328ead28SJ. Bruce Fields 	if (c) {
1845328ead28SJ. Bruce Fields 		spin_unlock(&clp->cl_lock);
1846328ead28SJ. Bruce Fields 		free_conn(new);
1847328ead28SJ. Bruce Fields 		return;
1848328ead28SJ. Bruce Fields 	}
1849328ead28SJ. Bruce Fields 	__nfsd4_hash_conn(new, ses);
1850328ead28SJ. Bruce Fields 	spin_unlock(&clp->cl_lock);
185121b75b01SJ. Bruce Fields 	ret = nfsd4_register_conn(new);
185221b75b01SJ. Bruce Fields 	if (ret)
185321b75b01SJ. Bruce Fields 		/* oops; xprt is already down: */
185421b75b01SJ. Bruce Fields 		nfsd4_conn_lost(&new->cn_xpt_user);
1855328ead28SJ. Bruce Fields 	return;
1856328ead28SJ. Bruce Fields }
1857328ead28SJ. Bruce Fields 
1858868b89c3SMi Jinlong static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1859868b89c3SMi Jinlong {
1860868b89c3SMi Jinlong 	struct nfsd4_compoundargs *args = rqstp->rq_argp;
1861868b89c3SMi Jinlong 
1862868b89c3SMi Jinlong 	return args->opcnt > session->se_fchannel.maxops;
1863868b89c3SMi Jinlong }
1864868b89c3SMi Jinlong 
1865ae82a8d0SMi Jinlong static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
1866ae82a8d0SMi Jinlong 				  struct nfsd4_session *session)
1867ae82a8d0SMi Jinlong {
1868ae82a8d0SMi Jinlong 	struct xdr_buf *xb = &rqstp->rq_arg;
1869ae82a8d0SMi Jinlong 
1870ae82a8d0SMi Jinlong 	return xb->len > session->se_fchannel.maxreq_sz;
1871ae82a8d0SMi Jinlong }
1872ae82a8d0SMi Jinlong 
1873069b6ad4SAndy Adamson __be32
1874b85d4c01SBenny Halevy nfsd4_sequence(struct svc_rqst *rqstp,
1875069b6ad4SAndy Adamson 	       struct nfsd4_compound_state *cstate,
1876069b6ad4SAndy Adamson 	       struct nfsd4_sequence *seq)
1877069b6ad4SAndy Adamson {
1878f9bb94c4SAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
1879b85d4c01SBenny Halevy 	struct nfsd4_session *session;
1880b85d4c01SBenny Halevy 	struct nfsd4_slot *slot;
1881a663bdd8SJ. Bruce Fields 	struct nfsd4_conn *conn;
1882b85d4c01SBenny Halevy 	int status;
1883b85d4c01SBenny Halevy 
1884f9bb94c4SAndy Adamson 	if (resp->opcnt != 1)
1885f9bb94c4SAndy Adamson 		return nfserr_sequence_pos;
1886f9bb94c4SAndy Adamson 
1887a663bdd8SJ. Bruce Fields 	/*
1888a663bdd8SJ. Bruce Fields 	 * Will be either used or freed by nfsd4_sequence_check_conn
1889a663bdd8SJ. Bruce Fields 	 * below.
1890a663bdd8SJ. Bruce Fields 	 */
1891a663bdd8SJ. Bruce Fields 	conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1892a663bdd8SJ. Bruce Fields 	if (!conn)
1893a663bdd8SJ. Bruce Fields 		return nfserr_jukebox;
1894a663bdd8SJ. Bruce Fields 
18959089f1b4SBenny Halevy 	spin_lock(&client_lock);
1896b85d4c01SBenny Halevy 	status = nfserr_badsession;
1897b85d4c01SBenny Halevy 	session = find_in_sessionid_hashtbl(&seq->sessionid);
1898b85d4c01SBenny Halevy 	if (!session)
1899b85d4c01SBenny Halevy 		goto out;
1900b85d4c01SBenny Halevy 
1901868b89c3SMi Jinlong 	status = nfserr_too_many_ops;
1902868b89c3SMi Jinlong 	if (nfsd4_session_too_many_ops(rqstp, session))
1903868b89c3SMi Jinlong 		goto out;
1904868b89c3SMi Jinlong 
1905ae82a8d0SMi Jinlong 	status = nfserr_req_too_big;
1906ae82a8d0SMi Jinlong 	if (nfsd4_request_too_big(rqstp, session))
1907ae82a8d0SMi Jinlong 		goto out;
1908ae82a8d0SMi Jinlong 
1909b85d4c01SBenny Halevy 	status = nfserr_badslot;
19106c18ba9fSAlexandros Batsakis 	if (seq->slotid >= session->se_fchannel.maxreqs)
1911b85d4c01SBenny Halevy 		goto out;
1912b85d4c01SBenny Halevy 
1913557ce264SAndy Adamson 	slot = session->se_slots[seq->slotid];
1914b85d4c01SBenny Halevy 	dprintk("%s: slotid %d\n", __func__, seq->slotid);
1915b85d4c01SBenny Halevy 
1916a8dfdaebSAndy Adamson 	/* We do not negotiate the number of slots yet, so set the
1917a8dfdaebSAndy Adamson 	 * maxslots to the session maxreqs which is used to encode
1918a8dfdaebSAndy Adamson 	 * sr_highest_slotid and the sr_target_slot id to maxslots */
1919a8dfdaebSAndy Adamson 	seq->maxslots = session->se_fchannel.maxreqs;
1920a8dfdaebSAndy Adamson 
192173e79482SJ. Bruce Fields 	status = check_slot_seqid(seq->seqid, slot->sl_seqid,
192273e79482SJ. Bruce Fields 					slot->sl_flags & NFSD4_SLOT_INUSE);
1923b85d4c01SBenny Halevy 	if (status == nfserr_replay_cache) {
1924bf5c43c8SJ. Bruce Fields 		status = nfserr_seq_misordered;
1925bf5c43c8SJ. Bruce Fields 		if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
1926bf5c43c8SJ. Bruce Fields 			goto out;
1927b85d4c01SBenny Halevy 		cstate->slot = slot;
1928b85d4c01SBenny Halevy 		cstate->session = session;
1929da3846a2SAndy Adamson 		/* Return the cached reply status and set cstate->status
1930557ce264SAndy Adamson 		 * for nfsd4_proc_compound processing */
1931bf864a31SAndy Adamson 		status = nfsd4_replay_cache_entry(resp, seq);
1932da3846a2SAndy Adamson 		cstate->status = nfserr_replay_cache;
1933aaf84eb9SBenny Halevy 		goto out;
1934b85d4c01SBenny Halevy 	}
1935b85d4c01SBenny Halevy 	if (status)
1936b85d4c01SBenny Halevy 		goto out;
1937b85d4c01SBenny Halevy 
1938a663bdd8SJ. Bruce Fields 	nfsd4_sequence_check_conn(conn, session);
1939a663bdd8SJ. Bruce Fields 	conn = NULL;
1940328ead28SJ. Bruce Fields 
1941b85d4c01SBenny Halevy 	/* Success! bump slot seqid */
1942b85d4c01SBenny Halevy 	slot->sl_seqid = seq->seqid;
1943bf5c43c8SJ. Bruce Fields 	slot->sl_flags |= NFSD4_SLOT_INUSE;
194473e79482SJ. Bruce Fields 	if (seq->cachethis)
194573e79482SJ. Bruce Fields 		slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
1946bf5c43c8SJ. Bruce Fields 	else
1947bf5c43c8SJ. Bruce Fields 		slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
1948b85d4c01SBenny Halevy 
1949b85d4c01SBenny Halevy 	cstate->slot = slot;
1950b85d4c01SBenny Halevy 	cstate->session = session;
1951b85d4c01SBenny Halevy 
1952b85d4c01SBenny Halevy out:
195326c0c75eSJ. Bruce Fields 	/* Hold a session reference until done processing the compound. */
1954aaf84eb9SBenny Halevy 	if (cstate->session) {
19550d7bb719SJ. Bruce Fields 		struct nfs4_client *clp = session->se_client;
19560d7bb719SJ. Bruce Fields 
195736acb66bSBenny Halevy 		nfsd4_get_session(cstate->session);
19580d7bb719SJ. Bruce Fields 		atomic_inc(&clp->cl_refcount);
19595423732aSBenny Halevy 		switch (clp->cl_cb_state) {
19605423732aSBenny Halevy 		case NFSD4_CB_DOWN:
1961fc0c3dd1SBenny Halevy 			seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
19625423732aSBenny Halevy 			break;
19635423732aSBenny Halevy 		case NFSD4_CB_FAULT:
1964fc0c3dd1SBenny Halevy 			seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
19655423732aSBenny Halevy 			break;
1966fc0c3dd1SBenny Halevy 		default:
1967fc0c3dd1SBenny Halevy 			seq->status_flags = 0;
19685423732aSBenny Halevy 		}
1969aaf84eb9SBenny Halevy 	}
1970a663bdd8SJ. Bruce Fields 	kfree(conn);
197136acb66bSBenny Halevy 	spin_unlock(&client_lock);
1972b85d4c01SBenny Halevy 	dprintk("%s: return %d\n", __func__, ntohl(status));
1973b85d4c01SBenny Halevy 	return status;
1974069b6ad4SAndy Adamson }
1975069b6ad4SAndy Adamson 
1976345c2842SMi Jinlong static inline bool has_resources(struct nfs4_client *clp)
1977345c2842SMi Jinlong {
1978345c2842SMi Jinlong 	return !list_empty(&clp->cl_openowners)
1979345c2842SMi Jinlong 		|| !list_empty(&clp->cl_delegations)
1980345c2842SMi Jinlong 		|| !list_empty(&clp->cl_sessions);
1981345c2842SMi Jinlong }
1982345c2842SMi Jinlong 
1983345c2842SMi Jinlong __be32
1984345c2842SMi Jinlong nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
1985345c2842SMi Jinlong {
1986345c2842SMi Jinlong 	struct nfs4_client *conf, *unconf, *clp;
1987345c2842SMi Jinlong 	int status = 0;
1988345c2842SMi Jinlong 
1989345c2842SMi Jinlong 	nfs4_lock_state();
1990345c2842SMi Jinlong 	unconf = find_unconfirmed_client(&dc->clientid);
1991345c2842SMi Jinlong 	conf = find_confirmed_client(&dc->clientid);
1992345c2842SMi Jinlong 
1993345c2842SMi Jinlong 	if (conf) {
1994345c2842SMi Jinlong 		clp = conf;
1995345c2842SMi Jinlong 
1996345c2842SMi Jinlong 		if (!is_client_expired(conf) && has_resources(conf)) {
1997345c2842SMi Jinlong 			status = nfserr_clientid_busy;
1998345c2842SMi Jinlong 			goto out;
1999345c2842SMi Jinlong 		}
2000345c2842SMi Jinlong 
2001345c2842SMi Jinlong 		/* rfc5661 18.50.3 */
2002345c2842SMi Jinlong 		if (cstate->session && conf == cstate->session->se_client) {
2003345c2842SMi Jinlong 			status = nfserr_clientid_busy;
2004345c2842SMi Jinlong 			goto out;
2005345c2842SMi Jinlong 		}
2006345c2842SMi Jinlong 	} else if (unconf)
2007345c2842SMi Jinlong 		clp = unconf;
2008345c2842SMi Jinlong 	else {
2009345c2842SMi Jinlong 		status = nfserr_stale_clientid;
2010345c2842SMi Jinlong 		goto out;
2011345c2842SMi Jinlong 	}
2012345c2842SMi Jinlong 
2013345c2842SMi Jinlong 	expire_client(clp);
2014345c2842SMi Jinlong out:
2015345c2842SMi Jinlong 	nfs4_unlock_state();
2016345c2842SMi Jinlong 	dprintk("%s return %d\n", __func__, ntohl(status));
2017345c2842SMi Jinlong 	return status;
2018345c2842SMi Jinlong }
2019345c2842SMi Jinlong 
2020069b6ad4SAndy Adamson __be32
20214dc6ec00SJ. Bruce Fields nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
20224dc6ec00SJ. Bruce Fields {
2023bcecf1ccSMi Jinlong 	int status = 0;
2024bcecf1ccSMi Jinlong 
20254dc6ec00SJ. Bruce Fields 	if (rc->rca_one_fs) {
20264dc6ec00SJ. Bruce Fields 		if (!cstate->current_fh.fh_dentry)
20274dc6ec00SJ. Bruce Fields 			return nfserr_nofilehandle;
20284dc6ec00SJ. Bruce Fields 		/*
20294dc6ec00SJ. Bruce Fields 		 * We don't take advantage of the rca_one_fs case.
20304dc6ec00SJ. Bruce Fields 		 * That's OK, it's optional, we can safely ignore it.
20314dc6ec00SJ. Bruce Fields 		 */
20324dc6ec00SJ. Bruce Fields 		 return nfs_ok;
20334dc6ec00SJ. Bruce Fields 	}
2034bcecf1ccSMi Jinlong 
20354dc6ec00SJ. Bruce Fields 	nfs4_lock_state();
2036bcecf1ccSMi Jinlong 	status = nfserr_complete_already;
2037bcecf1ccSMi Jinlong 	if (cstate->session->se_client->cl_firststate)
2038bcecf1ccSMi Jinlong 		goto out;
2039bcecf1ccSMi Jinlong 
2040bcecf1ccSMi Jinlong 	status = nfserr_stale_clientid;
2041bcecf1ccSMi Jinlong 	if (is_client_expired(cstate->session->se_client))
20424dc6ec00SJ. Bruce Fields 		/*
20434dc6ec00SJ. Bruce Fields 		 * The following error isn't really legal.
20444dc6ec00SJ. Bruce Fields 		 * But we only get here if the client just explicitly
20454dc6ec00SJ. Bruce Fields 		 * destroyed the client.  Surely it no longer cares what
20464dc6ec00SJ. Bruce Fields 		 * error it gets back on an operation for the dead
20474dc6ec00SJ. Bruce Fields 		 * client.
20484dc6ec00SJ. Bruce Fields 		 */
2049bcecf1ccSMi Jinlong 		goto out;
2050bcecf1ccSMi Jinlong 
2051bcecf1ccSMi Jinlong 	status = nfs_ok;
20524dc6ec00SJ. Bruce Fields 	nfsd4_create_clid_dir(cstate->session->se_client);
2053bcecf1ccSMi Jinlong out:
20544dc6ec00SJ. Bruce Fields 	nfs4_unlock_state();
2055bcecf1ccSMi Jinlong 	return status;
20564dc6ec00SJ. Bruce Fields }
20574dc6ec00SJ. Bruce Fields 
20584dc6ec00SJ. Bruce Fields __be32
2059b591480bSJ.Bruce Fields nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2060b591480bSJ.Bruce Fields 		  struct nfsd4_setclientid *setclid)
20611da177e4SLinus Torvalds {
2062a084daf5SJ. Bruce Fields 	struct xdr_netobj 	clname = setclid->se_name;
20631da177e4SLinus Torvalds 	nfs4_verifier		clverifier = setclid->se_verf;
20641da177e4SLinus Torvalds 	unsigned int 		strhashval;
206528ce6054SNeilBrown 	struct nfs4_client	*conf, *unconf, *new;
2066b37ad28bSAl Viro 	__be32 			status;
2067a55370a3SNeilBrown 	char                    dname[HEXDIR_LEN];
20681da177e4SLinus Torvalds 
2069a55370a3SNeilBrown 	status = nfs4_make_rec_clidname(dname, &clname);
2070a55370a3SNeilBrown 	if (status)
207173aea4ecSNeil Brown 		return status;
2072a55370a3SNeilBrown 
20731da177e4SLinus Torvalds 	/*
20741da177e4SLinus Torvalds 	 * XXX The Duplicate Request Cache (DRC) has been checked (??)
20751da177e4SLinus Torvalds 	 * We get here on a DRC miss.
20761da177e4SLinus Torvalds 	 */
20771da177e4SLinus Torvalds 
2078a55370a3SNeilBrown 	strhashval = clientstr_hashval(dname);
20791da177e4SLinus Torvalds 
20801da177e4SLinus Torvalds 	nfs4_lock_state();
2081e203d506SJ. Bruce Fields 	conf = find_confirmed_client_by_str(dname, strhashval);
208228ce6054SNeilBrown 	if (conf) {
2083a186e767SJ. Bruce Fields 		/* RFC 3530 14.2.33 CASE 0: */
20841da177e4SLinus Torvalds 		status = nfserr_clid_inuse;
2085e203d506SJ. Bruce Fields 		if (clp_used_exchangeid(conf))
2086e203d506SJ. Bruce Fields 			goto out;
2087026722c2SJ. Bruce Fields 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2088363168b4SJeff Layton 			char addr_str[INET6_ADDRSTRLEN];
2089363168b4SJeff Layton 			rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2090363168b4SJeff Layton 				 sizeof(addr_str));
2091026722c2SJ. Bruce Fields 			dprintk("NFSD: setclientid: string in use by client "
2092363168b4SJeff Layton 				"at %s\n", addr_str);
20931da177e4SLinus Torvalds 			goto out;
20941da177e4SLinus Torvalds 		}
20951da177e4SLinus Torvalds 	}
2096a186e767SJ. Bruce Fields 	/*
2097a186e767SJ. Bruce Fields 	 * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
2098a186e767SJ. Bruce Fields 	 * has a description of SETCLIENTID request processing consisting
2099a186e767SJ. Bruce Fields 	 * of 5 bullet points, labeled as CASE0 - CASE4 below.
2100a186e767SJ. Bruce Fields 	 */
2101e203d506SJ. Bruce Fields 	unconf = find_unconfirmed_client_by_str(dname, strhashval);
21023e772463SJ. Bruce Fields 	status = nfserr_jukebox;
21031da177e4SLinus Torvalds 	if (!conf) {
21041da177e4SLinus Torvalds 		/*
2105a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.33 CASE 4:
2106a186e767SJ. Bruce Fields 		 * placed first, because it is the normal case
21071da177e4SLinus Torvalds 		 */
21081da177e4SLinus Torvalds 		if (unconf)
21091da177e4SLinus Torvalds 			expire_client(unconf);
2110b09333c4SRicardo Labiaga 		new = create_client(clname, dname, rqstp, &clverifier);
2111a55370a3SNeilBrown 		if (new == NULL)
21121da177e4SLinus Torvalds 			goto out;
21131da177e4SLinus Torvalds 		gen_clid(new);
2114599e0a22SJ. Bruce Fields 	} else if (same_verf(&conf->cl_verifier, &clverifier)) {
21151da177e4SLinus Torvalds 		/*
2116a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.33 CASE 1:
2117a186e767SJ. Bruce Fields 		 * probable callback update
21181da177e4SLinus Torvalds 		 */
211931f4a6c1SNeilBrown 		if (unconf) {
212031f4a6c1SNeilBrown 			/* Note this is removing unconfirmed {*x***},
212131f4a6c1SNeilBrown 			 * which is stronger than RFC recommended {vxc**}.
212231f4a6c1SNeilBrown 			 * This has the advantage that there is at most
212331f4a6c1SNeilBrown 			 * one {*x***} in either list at any time.
212431f4a6c1SNeilBrown 			 */
21251da177e4SLinus Torvalds 			expire_client(unconf);
21261da177e4SLinus Torvalds 		}
2127b09333c4SRicardo Labiaga 		new = create_client(clname, dname, rqstp, &clverifier);
2128a55370a3SNeilBrown 		if (new == NULL)
21291da177e4SLinus Torvalds 			goto out;
21301da177e4SLinus Torvalds 		copy_clid(new, conf);
21311da177e4SLinus Torvalds 	} else if (!unconf) {
21321da177e4SLinus Torvalds 		/*
2133a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.33 CASE 2:
2134a186e767SJ. Bruce Fields 		 * probable client reboot; state will be removed if
2135a186e767SJ. Bruce Fields 		 * confirmed.
21361da177e4SLinus Torvalds 		 */
2137b09333c4SRicardo Labiaga 		new = create_client(clname, dname, rqstp, &clverifier);
2138a55370a3SNeilBrown 		if (new == NULL)
21391da177e4SLinus Torvalds 			goto out;
21401da177e4SLinus Torvalds 		gen_clid(new);
214149ba8781SJ. Bruce Fields 	} else {
21421da177e4SLinus Torvalds 		/*
2143a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.33 CASE 3:
2144a186e767SJ. Bruce Fields 		 * probable client reboot; state will be removed if
2145a186e767SJ. Bruce Fields 		 * confirmed.
21461da177e4SLinus Torvalds 		 */
21471da177e4SLinus Torvalds 		expire_client(unconf);
2148b09333c4SRicardo Labiaga 		new = create_client(clname, dname, rqstp, &clverifier);
2149a55370a3SNeilBrown 		if (new == NULL)
21501da177e4SLinus Torvalds 			goto out;
21511da177e4SLinus Torvalds 		gen_clid(new);
21521da177e4SLinus Torvalds 	}
21538323c3b2SJ. Bruce Fields 	/*
21548323c3b2SJ. Bruce Fields 	 * XXX: we should probably set this at creation time, and check
21558323c3b2SJ. Bruce Fields 	 * for consistent minorversion use throughout:
21568323c3b2SJ. Bruce Fields 	 */
21578323c3b2SJ. Bruce Fields 	new->cl_minorversion = 0;
21586f3d772fSTakuma Umeya 	gen_callback(new, setclid, rqstp);
2159c175b83cSJ. Bruce Fields 	add_to_unconfirmed(new, strhashval);
21601da177e4SLinus Torvalds 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
21611da177e4SLinus Torvalds 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
21621da177e4SLinus Torvalds 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
21631da177e4SLinus Torvalds 	status = nfs_ok;
21641da177e4SLinus Torvalds out:
21651da177e4SLinus Torvalds 	nfs4_unlock_state();
21661da177e4SLinus Torvalds 	return status;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds 
21701da177e4SLinus Torvalds /*
2171a186e767SJ. Bruce Fields  * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
2172a186e767SJ. Bruce Fields  * a description of SETCLIENTID_CONFIRM request processing consisting of 4
2173a186e767SJ. Bruce Fields  * bullets, labeled as CASE1 - CASE4 below.
21741da177e4SLinus Torvalds  */
2175b37ad28bSAl Viro __be32
2176b591480bSJ.Bruce Fields nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2177b591480bSJ.Bruce Fields 			 struct nfsd4_compound_state *cstate,
2178b591480bSJ.Bruce Fields 			 struct nfsd4_setclientid_confirm *setclientid_confirm)
21791da177e4SLinus Torvalds {
2180363168b4SJeff Layton 	struct sockaddr *sa = svc_addr(rqstp);
218121ab45a4SNeilBrown 	struct nfs4_client *conf, *unconf;
21821da177e4SLinus Torvalds 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
21831da177e4SLinus Torvalds 	clientid_t * clid = &setclientid_confirm->sc_clientid;
2184b37ad28bSAl Viro 	__be32 status;
21851da177e4SLinus Torvalds 
21861da177e4SLinus Torvalds 	if (STALE_CLIENTID(clid))
21871da177e4SLinus Torvalds 		return nfserr_stale_clientid;
21881da177e4SLinus Torvalds 	/*
21891da177e4SLinus Torvalds 	 * XXX The Duplicate Request Cache (DRC) has been checked (??)
21901da177e4SLinus Torvalds 	 * We get here on a DRC miss.
21911da177e4SLinus Torvalds 	 */
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	nfs4_lock_state();
219421ab45a4SNeilBrown 
219521ab45a4SNeilBrown 	conf = find_confirmed_client(clid);
219621ab45a4SNeilBrown 	unconf = find_unconfirmed_client(clid);
219721ab45a4SNeilBrown 
219822de4d83SNeilBrown 	status = nfserr_clid_inuse;
2199363168b4SJeff Layton 	if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
22001da177e4SLinus Torvalds 		goto out;
2201363168b4SJeff Layton 	if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
22021da177e4SLinus Torvalds 		goto out;
220321ab45a4SNeilBrown 
2204a186e767SJ. Bruce Fields 	/*
2205a186e767SJ. Bruce Fields 	 * section 14.2.34 of RFC 3530 has a description of
2206a186e767SJ. Bruce Fields 	 * SETCLIENTID_CONFIRM request processing consisting
2207a186e767SJ. Bruce Fields 	 * of 4 bullet points, labeled as CASE1 - CASE4 below.
2208a186e767SJ. Bruce Fields 	 */
2209366e0c1dSJ. Bruce Fields 	if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2210a186e767SJ. Bruce Fields 		/*
2211a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.34 CASE 1:
2212a186e767SJ. Bruce Fields 		 * callback update
22137c79f737SNeilBrown 		 */
2214599e0a22SJ. Bruce Fields 		if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
22151da177e4SLinus Torvalds 			status = nfserr_clid_inuse;
22161da177e4SLinus Torvalds 		else {
22175a3c9d71SJ. Bruce Fields 			nfsd4_change_callback(conf, &unconf->cl_cb_conn);
22185a3c9d71SJ. Bruce Fields 			nfsd4_probe_callback(conf);
22191a69c179SNeilBrown 			expire_client(unconf);
22201da177e4SLinus Torvalds 			status = nfs_ok;
22211a69c179SNeilBrown 
22221da177e4SLinus Torvalds 		}
2223f3aba4e5SJ. Bruce Fields 	} else if (conf && !unconf) {
2224a186e767SJ. Bruce Fields 		/*
2225a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.34 CASE 2:
2226a186e767SJ. Bruce Fields 		 * probable retransmitted request; play it safe and
2227a186e767SJ. Bruce Fields 		 * do nothing.
22287c79f737SNeilBrown 		 */
2229599e0a22SJ. Bruce Fields 		if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
22301da177e4SLinus Torvalds 			status = nfserr_clid_inuse;
223121ab45a4SNeilBrown 		else
22321da177e4SLinus Torvalds 			status = nfs_ok;
22337c79f737SNeilBrown 	} else if (!conf && unconf
2234599e0a22SJ. Bruce Fields 			&& same_verf(&unconf->cl_confirm, &confirm)) {
2235a186e767SJ. Bruce Fields 		/*
2236a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.34 CASE 3:
2237a186e767SJ. Bruce Fields 		 * Normal case; new or rebooted client:
22381da177e4SLinus Torvalds 		 */
2239599e0a22SJ. Bruce Fields 		if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
22401da177e4SLinus Torvalds 			status = nfserr_clid_inuse;
22411da177e4SLinus Torvalds 		} else {
22421a69c179SNeilBrown 			unsigned int hash =
22431a69c179SNeilBrown 				clientstr_hashval(unconf->cl_recdir);
22441a69c179SNeilBrown 			conf = find_confirmed_client_by_str(unconf->cl_recdir,
2245e203d506SJ. Bruce Fields 							    hash);
22461a69c179SNeilBrown 			if (conf) {
2247c7b9a459SNeilBrown 				nfsd4_remove_clid_dir(conf);
22481a69c179SNeilBrown 				expire_client(conf);
22491a69c179SNeilBrown 			}
22501da177e4SLinus Torvalds 			move_to_confirmed(unconf);
225121ab45a4SNeilBrown 			conf = unconf;
22525a3c9d71SJ. Bruce Fields 			nfsd4_probe_callback(conf);
22531a69c179SNeilBrown 			status = nfs_ok;
22541da177e4SLinus Torvalds 		}
2255599e0a22SJ. Bruce Fields 	} else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2256599e0a22SJ. Bruce Fields 	    && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
22577c79f737SNeilBrown 				    				&confirm)))) {
2258a186e767SJ. Bruce Fields 		/*
2259a186e767SJ. Bruce Fields 		 * RFC 3530 14.2.34 CASE 4:
2260a186e767SJ. Bruce Fields 		 * Client probably hasn't noticed that we rebooted yet.
22611da177e4SLinus Torvalds 		 */
22621da177e4SLinus Torvalds 		status = nfserr_stale_clientid;
22637c79f737SNeilBrown 	} else {
22641da177e4SLinus Torvalds 		/* check that we have hit one of the cases...*/
226522de4d83SNeilBrown 		status = nfserr_clid_inuse;
226608e8987cSNeilBrown 	}
22671da177e4SLinus Torvalds out:
22681da177e4SLinus Torvalds 	nfs4_unlock_state();
22691da177e4SLinus Torvalds 	return status;
22701da177e4SLinus Torvalds }
22711da177e4SLinus Torvalds 
227232513b40SJ. Bruce Fields static struct nfs4_file *nfsd4_alloc_file(void)
22731da177e4SLinus Torvalds {
227432513b40SJ. Bruce Fields 	return kmem_cache_alloc(file_slab, GFP_KERNEL);
227532513b40SJ. Bruce Fields }
227632513b40SJ. Bruce Fields 
227732513b40SJ. Bruce Fields /* OPEN Share state helper functions */
227832513b40SJ. Bruce Fields static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
227932513b40SJ. Bruce Fields {
22801da177e4SLinus Torvalds 	unsigned int hashval = file_hashval(ino);
22811da177e4SLinus Torvalds 
22828b671b80SJ. Bruce Fields 	atomic_set(&fp->fi_ref, 1);
22831da177e4SLinus Torvalds 	INIT_LIST_HEAD(&fp->fi_hash);
22848beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_stateids);
22858beefa24SNeilBrown 	INIT_LIST_HEAD(&fp->fi_delegations);
22861da177e4SLinus Torvalds 	fp->fi_inode = igrab(ino);
228747f9940cSMeelap Shah 	fp->fi_had_conflict = false;
2288acfdf5c3SJ. Bruce Fields 	fp->fi_lease = NULL;
2289f9d7562fSJ. Bruce Fields 	memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2290f9d7562fSJ. Bruce Fields 	memset(fp->fi_access, 0, sizeof(fp->fi_access));
229147cee541SPavel Emelyanov 	spin_lock(&recall_lock);
229247cee541SPavel Emelyanov 	list_add(&fp->fi_hash, &file_hashtbl[hashval]);
229347cee541SPavel Emelyanov 	spin_unlock(&recall_lock);
22941da177e4SLinus Torvalds }
22951da177e4SLinus Torvalds 
22961da177e4SLinus Torvalds static void
2297e18b890bSChristoph Lameter nfsd4_free_slab(struct kmem_cache **slab)
2298e60d4398SNeilBrown {
2299e60d4398SNeilBrown 	if (*slab == NULL)
2300e60d4398SNeilBrown 		return;
23011a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(*slab);
2302e60d4398SNeilBrown 	*slab = NULL;
2303e60d4398SNeilBrown }
2304e60d4398SNeilBrown 
2305e8ff2a84SJ. Bruce Fields void
2306e60d4398SNeilBrown nfsd4_free_slabs(void)
2307e60d4398SNeilBrown {
2308fe0750e5SJ. Bruce Fields 	nfsd4_free_slab(&openowner_slab);
2309fe0750e5SJ. Bruce Fields 	nfsd4_free_slab(&lockowner_slab);
2310e60d4398SNeilBrown 	nfsd4_free_slab(&file_slab);
23115ac049acSNeilBrown 	nfsd4_free_slab(&stateid_slab);
23125b2d21c1SNeilBrown 	nfsd4_free_slab(&deleg_slab);
2313e60d4398SNeilBrown }
23141da177e4SLinus Torvalds 
231572083396SBryan Schumaker int
23161da177e4SLinus Torvalds nfsd4_init_slabs(void)
23171da177e4SLinus Torvalds {
2318fe0750e5SJ. Bruce Fields 	openowner_slab = kmem_cache_create("nfsd4_openowners",
2319fe0750e5SJ. Bruce Fields 			sizeof(struct nfs4_openowner), 0, 0, NULL);
2320fe0750e5SJ. Bruce Fields 	if (openowner_slab == NULL)
2321fe0750e5SJ. Bruce Fields 		goto out_nomem;
2322fe0750e5SJ. Bruce Fields 	lockowner_slab = kmem_cache_create("nfsd4_lockowners",
2323fe0750e5SJ. Bruce Fields 			sizeof(struct nfs4_openowner), 0, 0, NULL);
2324fe0750e5SJ. Bruce Fields 	if (lockowner_slab == NULL)
2325e60d4398SNeilBrown 		goto out_nomem;
2326e60d4398SNeilBrown 	file_slab = kmem_cache_create("nfsd4_files",
232720c2df83SPaul Mundt 			sizeof(struct nfs4_file), 0, 0, NULL);
2328e60d4398SNeilBrown 	if (file_slab == NULL)
2329e60d4398SNeilBrown 		goto out_nomem;
23305ac049acSNeilBrown 	stateid_slab = kmem_cache_create("nfsd4_stateids",
2331dcef0413SJ. Bruce Fields 			sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
23325ac049acSNeilBrown 	if (stateid_slab == NULL)
23335ac049acSNeilBrown 		goto out_nomem;
23345b2d21c1SNeilBrown 	deleg_slab = kmem_cache_create("nfsd4_delegations",
233520c2df83SPaul Mundt 			sizeof(struct nfs4_delegation), 0, 0, NULL);
23365b2d21c1SNeilBrown 	if (deleg_slab == NULL)
23375b2d21c1SNeilBrown 		goto out_nomem;
2338e60d4398SNeilBrown 	return 0;
2339e60d4398SNeilBrown out_nomem:
2340e60d4398SNeilBrown 	nfsd4_free_slabs();
23411da177e4SLinus Torvalds 	dprintk("nfsd4: out of memory while initializing nfsv4\n");
23421da177e4SLinus Torvalds 	return -ENOMEM;
23431da177e4SLinus Torvalds }
23441da177e4SLinus Torvalds 
2345fe0750e5SJ. Bruce Fields void nfs4_free_openowner(struct nfs4_openowner *oo)
23461da177e4SLinus Torvalds {
2347fe0750e5SJ. Bruce Fields 	kfree(oo->oo_owner.so_owner.data);
2348fe0750e5SJ. Bruce Fields 	kmem_cache_free(openowner_slab, oo);
2349fe0750e5SJ. Bruce Fields }
2350fe0750e5SJ. Bruce Fields 
2351fe0750e5SJ. Bruce Fields void nfs4_free_lockowner(struct nfs4_lockowner *lo)
2352fe0750e5SJ. Bruce Fields {
2353fe0750e5SJ. Bruce Fields 	kfree(lo->lo_owner.so_owner.data);
2354fe0750e5SJ. Bruce Fields 	kmem_cache_free(lockowner_slab, lo);
23551da177e4SLinus Torvalds }
23561da177e4SLinus Torvalds 
2357ff194bd9SJ. Bruce Fields static void init_nfs4_replay(struct nfs4_replay *rp)
2358ff194bd9SJ. Bruce Fields {
2359ff194bd9SJ. Bruce Fields 	rp->rp_status = nfserr_serverfault;
2360ff194bd9SJ. Bruce Fields 	rp->rp_buflen = 0;
2361ff194bd9SJ. Bruce Fields 	rp->rp_buf = rp->rp_ibuf;
2362ff194bd9SJ. Bruce Fields }
2363ff194bd9SJ. Bruce Fields 
2364fe0750e5SJ. Bruce Fields static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
23651da177e4SLinus Torvalds {
23661da177e4SLinus Torvalds 	struct nfs4_stateowner *sop;
23671da177e4SLinus Torvalds 
2368fe0750e5SJ. Bruce Fields 	sop = kmem_cache_alloc(slab, GFP_KERNEL);
2369ff194bd9SJ. Bruce Fields 	if (!sop)
2370ff194bd9SJ. Bruce Fields 		return NULL;
2371ff194bd9SJ. Bruce Fields 
2372ff194bd9SJ. Bruce Fields 	sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
2373ff194bd9SJ. Bruce Fields 	if (!sop->so_owner.data) {
2374fe0750e5SJ. Bruce Fields 		kmem_cache_free(slab, sop);
2375ff194bd9SJ. Bruce Fields 		return NULL;
2376ff194bd9SJ. Bruce Fields 	}
23771da177e4SLinus Torvalds 	sop->so_owner.len = owner->len;
2378ff194bd9SJ. Bruce Fields 
2379ff194bd9SJ. Bruce Fields 	INIT_LIST_HEAD(&sop->so_stateids);
2380ff194bd9SJ. Bruce Fields 	sop->so_client = clp;
2381ff194bd9SJ. Bruce Fields 	init_nfs4_replay(&sop->so_replay);
23821da177e4SLinus Torvalds 	return sop;
23831da177e4SLinus Torvalds }
2384ff194bd9SJ. Bruce Fields 
2385fe0750e5SJ. Bruce Fields static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
2386ff194bd9SJ. Bruce Fields {
238716bfdaafSJ. Bruce Fields 	list_add(&oo->oo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
2388fe0750e5SJ. Bruce Fields 	list_add(&oo->oo_perclient, &clp->cl_openowners);
23891da177e4SLinus Torvalds }
23901da177e4SLinus Torvalds 
2391fe0750e5SJ. Bruce Fields static struct nfs4_openowner *
23921da177e4SLinus Torvalds alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2393fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
23941da177e4SLinus Torvalds 
2395fe0750e5SJ. Bruce Fields 	oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
2396fe0750e5SJ. Bruce Fields 	if (!oo)
23971da177e4SLinus Torvalds 		return NULL;
2398fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_is_open_owner = 1;
2399fe0750e5SJ. Bruce Fields 	oo->oo_owner.so_seqid = open->op_seqid;
2400d29b20cdSJ. Bruce Fields 	oo->oo_flags = NFS4_OO_NEW;
2401fe0750e5SJ. Bruce Fields 	oo->oo_time = 0;
240238c387b5SJ. Bruce Fields 	oo->oo_last_closed_stid = NULL;
2403fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&oo->oo_close_lru);
2404fe0750e5SJ. Bruce Fields 	hash_openowner(oo, clp, strhashval);
2405fe0750e5SJ. Bruce Fields 	return oo;
24061da177e4SLinus Torvalds }
24071da177e4SLinus Torvalds 
2408996e0938SJ. Bruce Fields static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2409fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = open->op_openowner;
2410d3b313a4SJ. Bruce Fields 	struct nfs4_client *clp = oo->oo_owner.so_client;
24111da177e4SLinus Torvalds 
2412996e0938SJ. Bruce Fields 	init_stid(&stp->st_stid, clp, NFS4_OPEN_STID);
2413ea1da636SNeilBrown 	INIT_LIST_HEAD(&stp->st_lockowners);
2414fe0750e5SJ. Bruce Fields 	list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
24158beefa24SNeilBrown 	list_add(&stp->st_perfile, &fp->fi_stateids);
2416fe0750e5SJ. Bruce Fields 	stp->st_stateowner = &oo->oo_owner;
241713cd2184SNeilBrown 	get_nfs4_file(fp);
24181da177e4SLinus Torvalds 	stp->st_file = fp;
24191da177e4SLinus Torvalds 	stp->st_access_bmap = 0;
24201da177e4SLinus Torvalds 	stp->st_deny_bmap = 0;
2421b6d2f1caSJ. Bruce Fields 	__set_bit(open->op_share_access, &stp->st_access_bmap);
24221da177e4SLinus Torvalds 	__set_bit(open->op_share_deny, &stp->st_deny_bmap);
24234c4cd222SNeilBrown 	stp->st_openstp = NULL;
24241da177e4SLinus Torvalds }
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds static void
2427fe0750e5SJ. Bruce Fields move_to_close_lru(struct nfs4_openowner *oo)
24281da177e4SLinus Torvalds {
2429fe0750e5SJ. Bruce Fields 	dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
24301da177e4SLinus Torvalds 
2431fe0750e5SJ. Bruce Fields 	list_move_tail(&oo->oo_close_lru, &close_lru);
2432fe0750e5SJ. Bruce Fields 	oo->oo_time = get_seconds();
24331da177e4SLinus Torvalds }
24341da177e4SLinus Torvalds 
24351da177e4SLinus Torvalds static int
2436599e0a22SJ. Bruce Fields same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2437599e0a22SJ. Bruce Fields 							clientid_t *clid)
2438599e0a22SJ. Bruce Fields {
2439599e0a22SJ. Bruce Fields 	return (sop->so_owner.len == owner->len) &&
2440599e0a22SJ. Bruce Fields 		0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2441599e0a22SJ. Bruce Fields 		(sop->so_client->cl_clientid.cl_id == clid->cl_id);
24421da177e4SLinus Torvalds }
24431da177e4SLinus Torvalds 
2444fe0750e5SJ. Bruce Fields static struct nfs4_openowner *
24451da177e4SLinus Torvalds find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
24461da177e4SLinus Torvalds {
2447a50d2ad1SJ. Bruce Fields 	struct nfs4_stateowner *so;
2448a50d2ad1SJ. Bruce Fields 	struct nfs4_openowner *oo;
24491da177e4SLinus Torvalds 
245016bfdaafSJ. Bruce Fields 	list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
245116bfdaafSJ. Bruce Fields 		if (!so->so_is_open_owner)
245216bfdaafSJ. Bruce Fields 			continue;
2453a50d2ad1SJ. Bruce Fields 		if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
2454a50d2ad1SJ. Bruce Fields 			oo = openowner(so);
2455a50d2ad1SJ. Bruce Fields 			renew_client(oo->oo_owner.so_client);
2456a50d2ad1SJ. Bruce Fields 			return oo;
2457a50d2ad1SJ. Bruce Fields 		}
24581da177e4SLinus Torvalds 	}
24591da177e4SLinus Torvalds 	return NULL;
24601da177e4SLinus Torvalds }
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds /* search file_hashtbl[] for file */
24631da177e4SLinus Torvalds static struct nfs4_file *
24641da177e4SLinus Torvalds find_file(struct inode *ino)
24651da177e4SLinus Torvalds {
24661da177e4SLinus Torvalds 	unsigned int hashval = file_hashval(ino);
24671da177e4SLinus Torvalds 	struct nfs4_file *fp;
24681da177e4SLinus Torvalds 
24698b671b80SJ. Bruce Fields 	spin_lock(&recall_lock);
24701da177e4SLinus Torvalds 	list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
247113cd2184SNeilBrown 		if (fp->fi_inode == ino) {
247213cd2184SNeilBrown 			get_nfs4_file(fp);
24738b671b80SJ. Bruce Fields 			spin_unlock(&recall_lock);
24741da177e4SLinus Torvalds 			return fp;
24751da177e4SLinus Torvalds 		}
247613cd2184SNeilBrown 	}
24778b671b80SJ. Bruce Fields 	spin_unlock(&recall_lock);
24781da177e4SLinus Torvalds 	return NULL;
24791da177e4SLinus Torvalds }
24801da177e4SLinus Torvalds 
24814f83aa30SJ. Bruce Fields /*
24821da177e4SLinus Torvalds  * Called to check deny when READ with all zero stateid or
24831da177e4SLinus Torvalds  * WRITE with all zero or all one stateid
24841da177e4SLinus Torvalds  */
2485b37ad28bSAl Viro static __be32
24861da177e4SLinus Torvalds nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
24871da177e4SLinus Torvalds {
24881da177e4SLinus Torvalds 	struct inode *ino = current_fh->fh_dentry->d_inode;
24891da177e4SLinus Torvalds 	struct nfs4_file *fp;
2490dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
2491b37ad28bSAl Viro 	__be32 ret;
24921da177e4SLinus Torvalds 
24931da177e4SLinus Torvalds 	dprintk("NFSD: nfs4_share_conflict\n");
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	fp = find_file(ino);
249613cd2184SNeilBrown 	if (!fp)
249713cd2184SNeilBrown 		return nfs_ok;
2498b700949bSNeilBrown 	ret = nfserr_locked;
24991da177e4SLinus Torvalds 	/* Search for conflicting share reservations */
25008beefa24SNeilBrown 	list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
25011da177e4SLinus Torvalds 		if (test_bit(deny_type, &stp->st_deny_bmap) ||
25021da177e4SLinus Torvalds 		    test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
250313cd2184SNeilBrown 			goto out;
25041da177e4SLinus Torvalds 	}
250513cd2184SNeilBrown 	ret = nfs_ok;
250613cd2184SNeilBrown out:
250713cd2184SNeilBrown 	put_nfs4_file(fp);
250813cd2184SNeilBrown 	return ret;
25091da177e4SLinus Torvalds }
25101da177e4SLinus Torvalds 
25116b57d9c8SJ. Bruce Fields static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
25121da177e4SLinus Torvalds {
25131da177e4SLinus Torvalds 	/* We're assuming the state code never drops its reference
25141da177e4SLinus Torvalds 	 * without first removing the lease.  Since we're in this lease
25151da177e4SLinus Torvalds 	 * callback (and since the lease code is serialized by the kernel
25161da177e4SLinus Torvalds 	 * lock) we know the server hasn't removed the lease yet, we know
25171da177e4SLinus Torvalds 	 * it's safe to take a reference: */
25181da177e4SLinus Torvalds 	atomic_inc(&dp->dl_count);
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
25211da177e4SLinus Torvalds 
2522460781b5SArnd Bergmann 	/* only place dl_time is set. protected by lock_flocks*/
25231da177e4SLinus Torvalds 	dp->dl_time = get_seconds();
25241da177e4SLinus Torvalds 
25256b57d9c8SJ. Bruce Fields 	nfsd4_cb_recall(dp);
25266b57d9c8SJ. Bruce Fields }
25276b57d9c8SJ. Bruce Fields 
2528acfdf5c3SJ. Bruce Fields /* Called from break_lease() with lock_flocks() held. */
25296b57d9c8SJ. Bruce Fields static void nfsd_break_deleg_cb(struct file_lock *fl)
25306b57d9c8SJ. Bruce Fields {
2531acfdf5c3SJ. Bruce Fields 	struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2532acfdf5c3SJ. Bruce Fields 	struct nfs4_delegation *dp;
25336b57d9c8SJ. Bruce Fields 
2534acfdf5c3SJ. Bruce Fields 	BUG_ON(!fp);
2535acfdf5c3SJ. Bruce Fields 	/* We assume break_lease is only called once per lease: */
2536acfdf5c3SJ. Bruce Fields 	BUG_ON(fp->fi_had_conflict);
25370272e1fdSJ. Bruce Fields 	/*
25380272e1fdSJ. Bruce Fields 	 * We don't want the locks code to timeout the lease for us;
2539acfdf5c3SJ. Bruce Fields 	 * we'll remove it ourself if a delegation isn't returned
25406b57d9c8SJ. Bruce Fields 	 * in time:
25410272e1fdSJ. Bruce Fields 	 */
25420272e1fdSJ. Bruce Fields 	fl->fl_break_time = 0;
25431da177e4SLinus Torvalds 
25445d926e8cSJ. Bruce Fields 	spin_lock(&recall_lock);
2545acfdf5c3SJ. Bruce Fields 	fp->fi_had_conflict = true;
2546acfdf5c3SJ. Bruce Fields 	list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
25475d926e8cSJ. Bruce Fields 		nfsd_break_one_deleg(dp);
25485d926e8cSJ. Bruce Fields 	spin_unlock(&recall_lock);
25491da177e4SLinus Torvalds }
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds static
25521da177e4SLinus Torvalds int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
25531da177e4SLinus Torvalds {
25541da177e4SLinus Torvalds 	if (arg & F_UNLCK)
25551da177e4SLinus Torvalds 		return lease_modify(onlist, arg);
25561da177e4SLinus Torvalds 	else
25571da177e4SLinus Torvalds 		return -EAGAIN;
25581da177e4SLinus Torvalds }
25591da177e4SLinus Torvalds 
25607b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_lease_mng_ops = {
25618fb47a4fSJ. Bruce Fields 	.lm_break = nfsd_break_deleg_cb,
25628fb47a4fSJ. Bruce Fields 	.lm_change = nfsd_change_deleg_cb,
25631da177e4SLinus Torvalds };
25641da177e4SLinus Torvalds 
25657a8711c9SJ. Bruce Fields static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
25667a8711c9SJ. Bruce Fields {
25677a8711c9SJ. Bruce Fields 	if (nfsd4_has_session(cstate))
25687a8711c9SJ. Bruce Fields 		return nfs_ok;
25697a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid - 1)
25707a8711c9SJ. Bruce Fields 		return nfserr_replay_me;
25717a8711c9SJ. Bruce Fields 	if (seqid == so->so_seqid)
25727a8711c9SJ. Bruce Fields 		return nfs_ok;
25737a8711c9SJ. Bruce Fields 	return nfserr_bad_seqid;
25747a8711c9SJ. Bruce Fields }
25751da177e4SLinus Torvalds 
2576b37ad28bSAl Viro __be32
25776668958fSAndy Adamson nfsd4_process_open1(struct nfsd4_compound_state *cstate,
25786668958fSAndy Adamson 		    struct nfsd4_open *open)
25791da177e4SLinus Torvalds {
25801da177e4SLinus Torvalds 	clientid_t *clientid = &open->op_clientid;
25811da177e4SLinus Torvalds 	struct nfs4_client *clp = NULL;
25821da177e4SLinus Torvalds 	unsigned int strhashval;
2583fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = NULL;
25844cdc951bSJ. Bruce Fields 	__be32 status;
25851da177e4SLinus Torvalds 
25861da177e4SLinus Torvalds 	if (STALE_CLIENTID(&open->op_clientid))
25871da177e4SLinus Torvalds 		return nfserr_stale_clientid;
258832513b40SJ. Bruce Fields 	/*
258932513b40SJ. Bruce Fields 	 * In case we need it later, after we've already created the
259032513b40SJ. Bruce Fields 	 * file and don't want to risk a further failure:
259132513b40SJ. Bruce Fields 	 */
259232513b40SJ. Bruce Fields 	open->op_file = nfsd4_alloc_file();
259332513b40SJ. Bruce Fields 	if (open->op_file == NULL)
259432513b40SJ. Bruce Fields 		return nfserr_jukebox;
25951da177e4SLinus Torvalds 
259616bfdaafSJ. Bruce Fields 	strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
2597fe0750e5SJ. Bruce Fields 	oo = find_openstateowner_str(strhashval, open);
2598fe0750e5SJ. Bruce Fields 	open->op_openowner = oo;
2599fe0750e5SJ. Bruce Fields 	if (!oo) {
26000f442aa2SJ. Bruce Fields 		clp = find_confirmed_client(clientid);
26010f442aa2SJ. Bruce Fields 		if (clp == NULL)
26020f442aa2SJ. Bruce Fields 			return nfserr_expired;
2603bcf130f9SJ. Bruce Fields 		goto new_owner;
26040f442aa2SJ. Bruce Fields 	}
2605dad1c067SJ. Bruce Fields 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
26060f442aa2SJ. Bruce Fields 		/* Replace unconfirmed owners without checking for replay. */
2607fe0750e5SJ. Bruce Fields 		clp = oo->oo_owner.so_client;
2608fe0750e5SJ. Bruce Fields 		release_openowner(oo);
2609fe0750e5SJ. Bruce Fields 		open->op_openowner = NULL;
2610bcf130f9SJ. Bruce Fields 		goto new_owner;
26110f442aa2SJ. Bruce Fields 	}
26124cdc951bSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
26134cdc951bSJ. Bruce Fields 	if (status)
26144cdc951bSJ. Bruce Fields 		return status;
26154cdc951bSJ. Bruce Fields 	clp = oo->oo_owner.so_client;
26164cdc951bSJ. Bruce Fields 	goto alloc_stateid;
2617bcf130f9SJ. Bruce Fields new_owner:
2618fe0750e5SJ. Bruce Fields 	oo = alloc_init_open_stateowner(strhashval, clp, open);
2619fe0750e5SJ. Bruce Fields 	if (oo == NULL)
26203e772463SJ. Bruce Fields 		return nfserr_jukebox;
2621fe0750e5SJ. Bruce Fields 	open->op_openowner = oo;
26224cdc951bSJ. Bruce Fields alloc_stateid:
26234cdc951bSJ. Bruce Fields 	open->op_stp = nfs4_alloc_stateid(clp);
26244cdc951bSJ. Bruce Fields 	if (!open->op_stp)
26254cdc951bSJ. Bruce Fields 		return nfserr_jukebox;
26260f442aa2SJ. Bruce Fields 	return nfs_ok;
26271da177e4SLinus Torvalds }
26281da177e4SLinus Torvalds 
2629b37ad28bSAl Viro static inline __be32
26304a6e43e6SNeilBrown nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
26314a6e43e6SNeilBrown {
26324a6e43e6SNeilBrown 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
26334a6e43e6SNeilBrown 		return nfserr_openmode;
26344a6e43e6SNeilBrown 	else
26354a6e43e6SNeilBrown 		return nfs_ok;
26364a6e43e6SNeilBrown }
26374a6e43e6SNeilBrown 
2638c47d832bSDaniel Mack static int share_access_to_flags(u32 share_access)
263924a0111eSJ. Bruce Fields {
264024a0111eSJ. Bruce Fields 	return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
264124a0111eSJ. Bruce Fields }
264224a0111eSJ. Bruce Fields 
264338c2f4b1SJ. Bruce Fields static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
2644f459e453SJ. Bruce Fields {
2645f459e453SJ. Bruce Fields 	struct nfs4_stid *ret;
2646f459e453SJ. Bruce Fields 
264738c2f4b1SJ. Bruce Fields 	ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
2648f459e453SJ. Bruce Fields 	if (!ret)
2649f459e453SJ. Bruce Fields 		return NULL;
2650f459e453SJ. Bruce Fields 	return delegstateid(ret);
2651f459e453SJ. Bruce Fields }
2652f459e453SJ. Bruce Fields 
26538b289b2cSJ. Bruce Fields static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
26548b289b2cSJ. Bruce Fields {
26558b289b2cSJ. Bruce Fields 	return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
26568b289b2cSJ. Bruce Fields 	       open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
26578b289b2cSJ. Bruce Fields }
26588b289b2cSJ. Bruce Fields 
2659b37ad28bSAl Viro static __be32
266038c2f4b1SJ. Bruce Fields nfs4_check_deleg(struct nfs4_client *cl, struct nfs4_file *fp, struct nfsd4_open *open,
2661567d9829SNeilBrown 		struct nfs4_delegation **dp)
2662567d9829SNeilBrown {
2663567d9829SNeilBrown 	int flags;
2664b37ad28bSAl Viro 	__be32 status = nfserr_bad_stateid;
2665567d9829SNeilBrown 
266638c2f4b1SJ. Bruce Fields 	*dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
2667567d9829SNeilBrown 	if (*dp == NULL)
2668c44c5eebSNeilBrown 		goto out;
266924a0111eSJ. Bruce Fields 	flags = share_access_to_flags(open->op_share_access);
2670567d9829SNeilBrown 	status = nfs4_check_delegmode(*dp, flags);
2671567d9829SNeilBrown 	if (status)
2672567d9829SNeilBrown 		*dp = NULL;
2673c44c5eebSNeilBrown out:
26748b289b2cSJ. Bruce Fields 	if (!nfsd4_is_deleg_cur(open))
2675c44c5eebSNeilBrown 		return nfs_ok;
2676c44c5eebSNeilBrown 	if (status)
2677c44c5eebSNeilBrown 		return status;
2678dad1c067SJ. Bruce Fields 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
2679c44c5eebSNeilBrown 	return nfs_ok;
2680567d9829SNeilBrown }
2681567d9829SNeilBrown 
2682b37ad28bSAl Viro static __be32
2683dcef0413SJ. Bruce Fields nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
26841da177e4SLinus Torvalds {
2685dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *local;
2686fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = open->op_openowner;
26871da177e4SLinus Torvalds 
26888beefa24SNeilBrown 	list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
26891da177e4SLinus Torvalds 		/* ignore lock owners */
26901da177e4SLinus Torvalds 		if (local->st_stateowner->so_is_open_owner == 0)
26911da177e4SLinus Torvalds 			continue;
26921da177e4SLinus Torvalds 		/* remember if we have seen this open owner */
2693fe0750e5SJ. Bruce Fields 		if (local->st_stateowner == &oo->oo_owner)
26941da177e4SLinus Torvalds 			*stpp = local;
26951da177e4SLinus Torvalds 		/* check for conflicting share reservations */
26961da177e4SLinus Torvalds 		if (!test_share(local, open))
269777eaae8dSJ. Bruce Fields 			return nfserr_share_denied;
26981da177e4SLinus Torvalds 	}
269977eaae8dSJ. Bruce Fields 	return nfs_ok;
27001da177e4SLinus Torvalds }
27011da177e4SLinus Torvalds 
2702996e0938SJ. Bruce Fields static void nfs4_free_stateid(struct nfs4_ol_stateid *s)
2703996e0938SJ. Bruce Fields {
2704996e0938SJ. Bruce Fields 	kmem_cache_free(stateid_slab, s);
27055ac049acSNeilBrown }
27065ac049acSNeilBrown 
270721fb4016SJ. Bruce Fields static inline int nfs4_access_to_access(u32 nfs4_access)
270821fb4016SJ. Bruce Fields {
270921fb4016SJ. Bruce Fields 	int flags = 0;
271021fb4016SJ. Bruce Fields 
271121fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_READ)
271221fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_READ;
271321fb4016SJ. Bruce Fields 	if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
271421fb4016SJ. Bruce Fields 		flags |= NFSD_MAY_WRITE;
271521fb4016SJ. Bruce Fields 	return flags;
271621fb4016SJ. Bruce Fields }
271721fb4016SJ. Bruce Fields 
27180c12eaffSCasey Bodley static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
27190c12eaffSCasey Bodley 		struct svc_fh *cur_fh, struct nfsd4_open *open)
2720f9d7562fSJ. Bruce Fields {
2721f9d7562fSJ. Bruce Fields 	__be32 status;
27220c12eaffSCasey Bodley 	int oflag = nfs4_access_to_omode(open->op_share_access);
27230c12eaffSCasey Bodley 	int access = nfs4_access_to_access(open->op_share_access);
27240c12eaffSCasey Bodley 
2725f9d7562fSJ. Bruce Fields 	if (!fp->fi_fds[oflag]) {
2726f9d7562fSJ. Bruce Fields 		status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2727f9d7562fSJ. Bruce Fields 			&fp->fi_fds[oflag]);
2728f9d7562fSJ. Bruce Fields 		if (status)
2729f9d7562fSJ. Bruce Fields 			return status;
2730f9d7562fSJ. Bruce Fields 	}
2731f9d7562fSJ. Bruce Fields 	nfs4_file_get_access(fp, oflag);
2732f9d7562fSJ. Bruce Fields 
2733f9d7562fSJ. Bruce Fields 	return nfs_ok;
2734f9d7562fSJ. Bruce Fields }
2735f9d7562fSJ. Bruce Fields 
2736b37ad28bSAl Viro static inline __be32
27371da177e4SLinus Torvalds nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
27381da177e4SLinus Torvalds 		struct nfsd4_open *open)
27391da177e4SLinus Torvalds {
27401da177e4SLinus Torvalds 	struct iattr iattr = {
27411da177e4SLinus Torvalds 		.ia_valid = ATTR_SIZE,
27421da177e4SLinus Torvalds 		.ia_size = 0,
27431da177e4SLinus Torvalds 	};
27441da177e4SLinus Torvalds 	if (!open->op_truncate)
27451da177e4SLinus Torvalds 		return 0;
27461da177e4SLinus Torvalds 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
27479246585aSAl Viro 		return nfserr_inval;
27481da177e4SLinus Torvalds 	return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
27491da177e4SLinus Torvalds }
27501da177e4SLinus Torvalds 
2751b37ad28bSAl Viro static __be32
2752dcef0413SJ. Bruce Fields nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
27531da177e4SLinus Torvalds {
2754b6d2f1caSJ. Bruce Fields 	u32 op_share_access = open->op_share_access;
27557d947842SJ. Bruce Fields 	bool new_access;
2756b37ad28bSAl Viro 	__be32 status;
27571da177e4SLinus Torvalds 
27587d947842SJ. Bruce Fields 	new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2759f9d7562fSJ. Bruce Fields 	if (new_access) {
27600c12eaffSCasey Bodley 		status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2761f9d7562fSJ. Bruce Fields 		if (status)
2762f9d7562fSJ. Bruce Fields 			return status;
27636c26d08fSJ. Bruce Fields 	}
27641da177e4SLinus Torvalds 	status = nfsd4_truncate(rqstp, cur_fh, open);
27651da177e4SLinus Torvalds 	if (status) {
2766f9d7562fSJ. Bruce Fields 		if (new_access) {
2767f197c271SJ. Bruce Fields 			int oflag = nfs4_access_to_omode(op_share_access);
2768f9d7562fSJ. Bruce Fields 			nfs4_file_put_access(fp, oflag);
2769f9d7562fSJ. Bruce Fields 		}
27701da177e4SLinus Torvalds 		return status;
27711da177e4SLinus Torvalds 	}
27721da177e4SLinus Torvalds 	/* remember the open */
277324a0111eSJ. Bruce Fields 	__set_bit(op_share_access, &stp->st_access_bmap);
2774b55e0ba1SJ. Bruce Fields 	__set_bit(open->op_share_deny, &stp->st_deny_bmap);
27751da177e4SLinus Torvalds 
27761da177e4SLinus Torvalds 	return nfs_ok;
27771da177e4SLinus Torvalds }
27781da177e4SLinus Torvalds 
27791da177e4SLinus Torvalds 
27801da177e4SLinus Torvalds static void
27811255a8f3SJ. Bruce Fields nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
27821da177e4SLinus Torvalds {
2783dad1c067SJ. Bruce Fields 	open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
27841255a8f3SJ. Bruce Fields 	/*
27851255a8f3SJ. Bruce Fields 	 * On a 4.1+ client, we don't create a state record for a client
27861255a8f3SJ. Bruce Fields 	 * until it performs RECLAIM_COMPLETE:
27871255a8f3SJ. Bruce Fields 	 */
27881255a8f3SJ. Bruce Fields 	if (!has_session)
2789fe0750e5SJ. Bruce Fields 		open->op_openowner->oo_owner.so_client->cl_firststate = 1;
27901da177e4SLinus Torvalds }
27911da177e4SLinus Torvalds 
279214a24e99SJ. Bruce Fields /* Should we give out recallable state?: */
279314a24e99SJ. Bruce Fields static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
279414a24e99SJ. Bruce Fields {
279514a24e99SJ. Bruce Fields 	if (clp->cl_cb_state == NFSD4_CB_UP)
279614a24e99SJ. Bruce Fields 		return true;
279714a24e99SJ. Bruce Fields 	/*
279814a24e99SJ. Bruce Fields 	 * In the sessions case, since we don't have to establish a
279914a24e99SJ. Bruce Fields 	 * separate connection for callbacks, we assume it's OK
280014a24e99SJ. Bruce Fields 	 * until we hear otherwise:
280114a24e99SJ. Bruce Fields 	 */
280214a24e99SJ. Bruce Fields 	return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
280314a24e99SJ. Bruce Fields }
280414a24e99SJ. Bruce Fields 
280522d38c4cSJ. Bruce Fields static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
280622d38c4cSJ. Bruce Fields {
280722d38c4cSJ. Bruce Fields 	struct file_lock *fl;
280822d38c4cSJ. Bruce Fields 
280922d38c4cSJ. Bruce Fields 	fl = locks_alloc_lock();
281022d38c4cSJ. Bruce Fields 	if (!fl)
281122d38c4cSJ. Bruce Fields 		return NULL;
281222d38c4cSJ. Bruce Fields 	locks_init_lock(fl);
281322d38c4cSJ. Bruce Fields 	fl->fl_lmops = &nfsd_lease_mng_ops;
281422d38c4cSJ. Bruce Fields 	fl->fl_flags = FL_LEASE;
281522d38c4cSJ. Bruce Fields 	fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
281622d38c4cSJ. Bruce Fields 	fl->fl_end = OFFSET_MAX;
2817acfdf5c3SJ. Bruce Fields 	fl->fl_owner = (fl_owner_t)(dp->dl_file);
281822d38c4cSJ. Bruce Fields 	fl->fl_pid = current->tgid;
281922d38c4cSJ. Bruce Fields 	return fl;
282022d38c4cSJ. Bruce Fields }
282122d38c4cSJ. Bruce Fields 
2822edab9782SJ. Bruce Fields static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2823edab9782SJ. Bruce Fields {
2824acfdf5c3SJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_file;
2825edab9782SJ. Bruce Fields 	struct file_lock *fl;
2826edab9782SJ. Bruce Fields 	int status;
2827edab9782SJ. Bruce Fields 
2828edab9782SJ. Bruce Fields 	fl = nfs4_alloc_init_lease(dp, flag);
2829edab9782SJ. Bruce Fields 	if (!fl)
2830edab9782SJ. Bruce Fields 		return -ENOMEM;
2831acfdf5c3SJ. Bruce Fields 	fl->fl_file = find_readable_file(fp);
28322a74aba7SJ. Bruce Fields 	list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2833acfdf5c3SJ. Bruce Fields 	status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2834edab9782SJ. Bruce Fields 	if (status) {
2835acfdf5c3SJ. Bruce Fields 		list_del_init(&dp->dl_perclnt);
2836edab9782SJ. Bruce Fields 		locks_free_lock(fl);
2837edab9782SJ. Bruce Fields 		return -ENOMEM;
2838edab9782SJ. Bruce Fields 	}
2839acfdf5c3SJ. Bruce Fields 	fp->fi_lease = fl;
2840acfdf5c3SJ. Bruce Fields 	fp->fi_deleg_file = fl->fl_file;
2841acfdf5c3SJ. Bruce Fields 	get_file(fp->fi_deleg_file);
2842acfdf5c3SJ. Bruce Fields 	atomic_set(&fp->fi_delegees, 1);
2843acfdf5c3SJ. Bruce Fields 	list_add(&dp->dl_perfile, &fp->fi_delegations);
2844acfdf5c3SJ. Bruce Fields 	return 0;
2845acfdf5c3SJ. Bruce Fields }
2846acfdf5c3SJ. Bruce Fields 
2847acfdf5c3SJ. Bruce Fields static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2848acfdf5c3SJ. Bruce Fields {
2849acfdf5c3SJ. Bruce Fields 	struct nfs4_file *fp = dp->dl_file;
2850acfdf5c3SJ. Bruce Fields 
2851acfdf5c3SJ. Bruce Fields 	if (!fp->fi_lease)
2852acfdf5c3SJ. Bruce Fields 		return nfs4_setlease(dp, flag);
2853acfdf5c3SJ. Bruce Fields 	spin_lock(&recall_lock);
2854acfdf5c3SJ. Bruce Fields 	if (fp->fi_had_conflict) {
2855acfdf5c3SJ. Bruce Fields 		spin_unlock(&recall_lock);
2856acfdf5c3SJ. Bruce Fields 		return -EAGAIN;
2857acfdf5c3SJ. Bruce Fields 	}
2858acfdf5c3SJ. Bruce Fields 	atomic_inc(&fp->fi_delegees);
2859acfdf5c3SJ. Bruce Fields 	list_add(&dp->dl_perfile, &fp->fi_delegations);
2860acfdf5c3SJ. Bruce Fields 	spin_unlock(&recall_lock);
28612a74aba7SJ. Bruce Fields 	list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
2862edab9782SJ. Bruce Fields 	return 0;
2863edab9782SJ. Bruce Fields }
2864edab9782SJ. Bruce Fields 
28651da177e4SLinus Torvalds /*
28661da177e4SLinus Torvalds  * Attempt to hand out a delegation.
28671da177e4SLinus Torvalds  */
28681da177e4SLinus Torvalds static void
2869dcef0413SJ. Bruce Fields nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
28701da177e4SLinus Torvalds {
28711da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
2872fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
287314a24e99SJ. Bruce Fields 	int cb_up;
2874d24433cdSBenny Halevy 	int status = 0, flag = 0;
28751da177e4SLinus Torvalds 
2876fe0750e5SJ. Bruce Fields 	cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
28771da177e4SLinus Torvalds 	flag = NFS4_OPEN_DELEGATE_NONE;
28787b190fecSNeilBrown 	open->op_recall = 0;
28797b190fecSNeilBrown 	switch (open->op_claim_type) {
28807b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_PREVIOUS:
28812bf23875SJ. Bruce Fields 			if (!cb_up)
28827b190fecSNeilBrown 				open->op_recall = 1;
28837b190fecSNeilBrown 			flag = open->op_delegate_type;
28847b190fecSNeilBrown 			if (flag == NFS4_OPEN_DELEGATE_NONE)
28851da177e4SLinus Torvalds 				goto out;
28867b190fecSNeilBrown 			break;
28877b190fecSNeilBrown 		case NFS4_OPEN_CLAIM_NULL:
28887b190fecSNeilBrown 			/* Let's not give out any delegations till everyone's
28897b190fecSNeilBrown 			 * had the chance to reclaim theirs.... */
2890af558e33SJ. Bruce Fields 			if (locks_in_grace())
28917b190fecSNeilBrown 				goto out;
2892dad1c067SJ. Bruce Fields 			if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
28937b190fecSNeilBrown 				goto out;
28941da177e4SLinus Torvalds 			if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
28951da177e4SLinus Torvalds 				flag = NFS4_OPEN_DELEGATE_WRITE;
28961da177e4SLinus Torvalds 			else
28971da177e4SLinus Torvalds 				flag = NFS4_OPEN_DELEGATE_READ;
28987b190fecSNeilBrown 			break;
28997b190fecSNeilBrown 		default:
29007b190fecSNeilBrown 			goto out;
29017b190fecSNeilBrown 	}
29021da177e4SLinus Torvalds 
2903fe0750e5SJ. Bruce Fields 	dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
2904dd239cc0SJ. Bruce Fields 	if (dp == NULL)
2905dd239cc0SJ. Bruce Fields 		goto out_no_deleg;
2906acfdf5c3SJ. Bruce Fields 	status = nfs4_set_delegation(dp, flag);
2907edab9782SJ. Bruce Fields 	if (status)
2908dd239cc0SJ. Bruce Fields 		goto out_free;
29091da177e4SLinus Torvalds 
2910d5477a8dSJ. Bruce Fields 	memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
29111da177e4SLinus Torvalds 
29128c10cbdbSBenny Halevy 	dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2913d5477a8dSJ. Bruce Fields 		STATEID_VAL(&dp->dl_stid.sc_stateid));
29141da177e4SLinus Torvalds out:
29151da177e4SLinus Torvalds 	open->op_delegate_type = flag;
2916d24433cdSBenny Halevy 	if (flag == NFS4_OPEN_DELEGATE_NONE) {
2917d24433cdSBenny Halevy 		if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
2918d24433cdSBenny Halevy 		    open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2919d24433cdSBenny Halevy 			dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2920d24433cdSBenny Halevy 
2921d24433cdSBenny Halevy 		if (open->op_deleg_want) {
2922d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
2923d24433cdSBenny Halevy 			if (status == -EAGAIN)
2924d24433cdSBenny Halevy 				open->op_why_no_deleg = WND4_CONTENTION;
2925d24433cdSBenny Halevy 			else {
2926d24433cdSBenny Halevy 				open->op_why_no_deleg = WND4_RESOURCE;
2927d24433cdSBenny Halevy 				switch (open->op_deleg_want) {
2928d24433cdSBenny Halevy 				case NFS4_SHARE_WANT_READ_DELEG:
2929d24433cdSBenny Halevy 				case NFS4_SHARE_WANT_WRITE_DELEG:
2930d24433cdSBenny Halevy 				case NFS4_SHARE_WANT_ANY_DELEG:
2931d24433cdSBenny Halevy 					break;
2932d24433cdSBenny Halevy 				case NFS4_SHARE_WANT_CANCEL:
2933d24433cdSBenny Halevy 					open->op_why_no_deleg = WND4_CANCELLED;
2934d24433cdSBenny Halevy 					break;
2935d24433cdSBenny Halevy 				case NFS4_SHARE_WANT_NO_DELEG:
2936d24433cdSBenny Halevy 					BUG();	/* not supposed to get here */
2937d24433cdSBenny Halevy 				}
2938d24433cdSBenny Halevy 			}
2939d24433cdSBenny Halevy 		}
2940d24433cdSBenny Halevy 	}
2941dd239cc0SJ. Bruce Fields 	return;
2942dd239cc0SJ. Bruce Fields out_free:
2943acfdf5c3SJ. Bruce Fields 	nfs4_put_delegation(dp);
2944dd239cc0SJ. Bruce Fields out_no_deleg:
2945dd239cc0SJ. Bruce Fields 	flag = NFS4_OPEN_DELEGATE_NONE;
2946dd239cc0SJ. Bruce Fields 	goto out;
29471da177e4SLinus Torvalds }
29481da177e4SLinus Torvalds 
29491da177e4SLinus Torvalds /*
29501da177e4SLinus Torvalds  * called with nfs4_lock_state() held.
29511da177e4SLinus Torvalds  */
2952b37ad28bSAl Viro __be32
29531da177e4SLinus Torvalds nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
29541da177e4SLinus Torvalds {
29556668958fSAndy Adamson 	struct nfsd4_compoundres *resp = rqstp->rq_resp;
295638c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
29571da177e4SLinus Torvalds 	struct nfs4_file *fp = NULL;
29581da177e4SLinus Torvalds 	struct inode *ino = current_fh->fh_dentry->d_inode;
2959dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp = NULL;
2960567d9829SNeilBrown 	struct nfs4_delegation *dp = NULL;
2961b37ad28bSAl Viro 	__be32 status;
29621da177e4SLinus Torvalds 
29631da177e4SLinus Torvalds 	/*
29641da177e4SLinus Torvalds 	 * Lookup file; if found, lookup stateid and check open request,
29651da177e4SLinus Torvalds 	 * and check for delegations in the process of being recalled.
29661da177e4SLinus Torvalds 	 * If not found, create the nfs4_file struct
29671da177e4SLinus Torvalds 	 */
29681da177e4SLinus Torvalds 	fp = find_file(ino);
29691da177e4SLinus Torvalds 	if (fp) {
29701da177e4SLinus Torvalds 		if ((status = nfs4_check_open(fp, open, &stp)))
29711da177e4SLinus Torvalds 			goto out;
297238c2f4b1SJ. Bruce Fields 		status = nfs4_check_deleg(cl, fp, open, &dp);
2973c44c5eebSNeilBrown 		if (status)
2974c44c5eebSNeilBrown 			goto out;
29751da177e4SLinus Torvalds 	} else {
2976c44c5eebSNeilBrown 		status = nfserr_bad_stateid;
29778b289b2cSJ. Bruce Fields 		if (nfsd4_is_deleg_cur(open))
2978c44c5eebSNeilBrown 			goto out;
29793e772463SJ. Bruce Fields 		status = nfserr_jukebox;
298032513b40SJ. Bruce Fields 		fp = open->op_file;
298132513b40SJ. Bruce Fields 		open->op_file = NULL;
298232513b40SJ. Bruce Fields 		nfsd4_init_file(fp, ino);
29831da177e4SLinus Torvalds 	}
29841da177e4SLinus Torvalds 
29851da177e4SLinus Torvalds 	/*
29861da177e4SLinus Torvalds 	 * OPEN the file, or upgrade an existing OPEN.
29871da177e4SLinus Torvalds 	 * If truncate fails, the OPEN fails.
29881da177e4SLinus Torvalds 	 */
29891da177e4SLinus Torvalds 	if (stp) {
29901da177e4SLinus Torvalds 		/* Stateid was found, this is an OPEN upgrade */
2991f9d7562fSJ. Bruce Fields 		status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
29921da177e4SLinus Torvalds 		if (status)
29931da177e4SLinus Torvalds 			goto out;
29941da177e4SLinus Torvalds 	} else {
29954cdc951bSJ. Bruce Fields 		status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
2996567d9829SNeilBrown 		if (status)
29971da177e4SLinus Torvalds 			goto out;
29984cdc951bSJ. Bruce Fields 		stp = open->op_stp;
29994cdc951bSJ. Bruce Fields 		open->op_stp = NULL;
3000996e0938SJ. Bruce Fields 		init_open_stateid(stp, fp, open);
30011da177e4SLinus Torvalds 		status = nfsd4_truncate(rqstp, current_fh, open);
30021da177e4SLinus Torvalds 		if (status) {
30032283963fSJ. Bruce Fields 			release_open_stateid(stp);
30041da177e4SLinus Torvalds 			goto out;
30051da177e4SLinus Torvalds 		}
30061da177e4SLinus Torvalds 	}
3007dcef0413SJ. Bruce Fields 	update_stateid(&stp->st_stid.sc_stateid);
3008dcef0413SJ. Bruce Fields 	memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
30091da177e4SLinus Torvalds 
3010d24433cdSBenny Halevy 	if (nfsd4_has_session(&resp->cstate)) {
3011dad1c067SJ. Bruce Fields 		open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
30126668958fSAndy Adamson 
3013d24433cdSBenny Halevy 		if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
3014d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3015d24433cdSBenny Halevy 			open->op_why_no_deleg = WND4_NOT_WANTED;
3016d24433cdSBenny Halevy 			goto nodeleg;
3017d24433cdSBenny Halevy 		}
3018d24433cdSBenny Halevy 	}
3019d24433cdSBenny Halevy 
30201da177e4SLinus Torvalds 	/*
30211da177e4SLinus Torvalds 	* Attempt to hand out a delegation. No error return, because the
30221da177e4SLinus Torvalds 	* OPEN succeeds even if we fail.
30231da177e4SLinus Torvalds 	*/
30241da177e4SLinus Torvalds 	nfs4_open_delegation(current_fh, open, stp);
3025d24433cdSBenny Halevy nodeleg:
30261da177e4SLinus Torvalds 	status = nfs_ok;
30271da177e4SLinus Torvalds 
30288c10cbdbSBenny Halevy 	dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
3029dcef0413SJ. Bruce Fields 		STATEID_VAL(&stp->st_stid.sc_stateid));
30301da177e4SLinus Torvalds out:
3031d24433cdSBenny Halevy 	/* 4.1 client trying to upgrade/downgrade delegation? */
3032d24433cdSBenny Halevy 	if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
3033d24433cdSBenny Halevy 	    open->op_deleg_want) {
3034d24433cdSBenny Halevy 		if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
3035d24433cdSBenny Halevy 		    dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3036d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3037d24433cdSBenny Halevy 			open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
3038d24433cdSBenny Halevy 		} else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
3039d24433cdSBenny Halevy 			   dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
3040d24433cdSBenny Halevy 			open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3041d24433cdSBenny Halevy 			open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
3042d24433cdSBenny Halevy 		}
3043d24433cdSBenny Halevy 		/* Otherwise the client must be confused wanting a delegation
3044d24433cdSBenny Halevy 		 * it already has, therefore we don't return
3045d24433cdSBenny Halevy 		 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
3046d24433cdSBenny Halevy 		 */
3047d24433cdSBenny Halevy 	}
3048d24433cdSBenny Halevy 
304913cd2184SNeilBrown 	if (fp)
305013cd2184SNeilBrown 		put_nfs4_file(fp);
305137515177SNeilBrown 	if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
30521255a8f3SJ. Bruce Fields 		nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
30531da177e4SLinus Torvalds 	/*
30541da177e4SLinus Torvalds 	* To finish the open response, we just need to set the rflags.
30551da177e4SLinus Torvalds 	*/
30561da177e4SLinus Torvalds 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
3057dad1c067SJ. Bruce Fields 	if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
30586668958fSAndy Adamson 	    !nfsd4_has_session(&resp->cstate))
30591da177e4SLinus Torvalds 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
30601da177e4SLinus Torvalds 
30611da177e4SLinus Torvalds 	return status;
30621da177e4SLinus Torvalds }
30631da177e4SLinus Torvalds 
3064d29b20cdSJ. Bruce Fields void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
3065d29b20cdSJ. Bruce Fields {
3066d29b20cdSJ. Bruce Fields 	if (open->op_openowner) {
3067d29b20cdSJ. Bruce Fields 		struct nfs4_openowner *oo = open->op_openowner;
3068d29b20cdSJ. Bruce Fields 
3069d29b20cdSJ. Bruce Fields 		if (!list_empty(&oo->oo_owner.so_stateids))
3070d29b20cdSJ. Bruce Fields 			list_del_init(&oo->oo_close_lru);
3071d29b20cdSJ. Bruce Fields 		if (oo->oo_flags & NFS4_OO_NEW) {
3072d29b20cdSJ. Bruce Fields 			if (status) {
3073d29b20cdSJ. Bruce Fields 				release_openowner(oo);
3074d29b20cdSJ. Bruce Fields 				open->op_openowner = NULL;
3075d29b20cdSJ. Bruce Fields 			} else
3076d29b20cdSJ. Bruce Fields 				oo->oo_flags &= ~NFS4_OO_NEW;
3077d29b20cdSJ. Bruce Fields 		}
3078d29b20cdSJ. Bruce Fields 	}
307932513b40SJ. Bruce Fields 	if (open->op_file)
308032513b40SJ. Bruce Fields 		nfsd4_free_file(open->op_file);
30814cdc951bSJ. Bruce Fields 	if (open->op_stp)
30824cdc951bSJ. Bruce Fields 		nfs4_free_stateid(open->op_stp);
3083d29b20cdSJ. Bruce Fields }
3084d29b20cdSJ. Bruce Fields 
3085b37ad28bSAl Viro __be32
3086b591480bSJ.Bruce Fields nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3087b591480bSJ.Bruce Fields 	    clientid_t *clid)
30881da177e4SLinus Torvalds {
30891da177e4SLinus Torvalds 	struct nfs4_client *clp;
3090b37ad28bSAl Viro 	__be32 status;
30911da177e4SLinus Torvalds 
30921da177e4SLinus Torvalds 	nfs4_lock_state();
30931da177e4SLinus Torvalds 	dprintk("process_renew(%08x/%08x): starting\n",
30941da177e4SLinus Torvalds 			clid->cl_boot, clid->cl_id);
30951da177e4SLinus Torvalds 	status = nfserr_stale_clientid;
30961da177e4SLinus Torvalds 	if (STALE_CLIENTID(clid))
30971da177e4SLinus Torvalds 		goto out;
30981da177e4SLinus Torvalds 	clp = find_confirmed_client(clid);
30991da177e4SLinus Torvalds 	status = nfserr_expired;
31001da177e4SLinus Torvalds 	if (clp == NULL) {
31011da177e4SLinus Torvalds 		/* We assume the client took too long to RENEW. */
31021da177e4SLinus Torvalds 		dprintk("nfsd4_renew: clientid not found!\n");
31031da177e4SLinus Torvalds 		goto out;
31041da177e4SLinus Torvalds 	}
31051da177e4SLinus Torvalds 	status = nfserr_cb_path_down;
3106ea1da636SNeilBrown 	if (!list_empty(&clp->cl_delegations)
310777a3569dSJ. Bruce Fields 			&& clp->cl_cb_state != NFSD4_CB_UP)
31081da177e4SLinus Torvalds 		goto out;
31091da177e4SLinus Torvalds 	status = nfs_ok;
31101da177e4SLinus Torvalds out:
31111da177e4SLinus Torvalds 	nfs4_unlock_state();
31121da177e4SLinus Torvalds 	return status;
31131da177e4SLinus Torvalds }
31141da177e4SLinus Torvalds 
3115c47d832bSDaniel Mack static struct lock_manager nfsd4_manager = {
3116af558e33SJ. Bruce Fields };
3117af558e33SJ. Bruce Fields 
3118a76b4319SNeilBrown static void
3119af558e33SJ. Bruce Fields nfsd4_end_grace(void)
3120a76b4319SNeilBrown {
3121a76b4319SNeilBrown 	dprintk("NFSD: end of grace period\n");
3122c7b9a459SNeilBrown 	nfsd4_recdir_purge_old();
3123af558e33SJ. Bruce Fields 	locks_end_grace(&nfsd4_manager);
3124e46b498cSJ. Bruce Fields 	/*
3125e46b498cSJ. Bruce Fields 	 * Now that every NFSv4 client has had the chance to recover and
3126e46b498cSJ. Bruce Fields 	 * to see the (possibly new, possibly shorter) lease time, we
3127e46b498cSJ. Bruce Fields 	 * can safely set the next grace time to the current lease time:
3128e46b498cSJ. Bruce Fields 	 */
3129e46b498cSJ. Bruce Fields 	nfsd4_grace = nfsd4_lease;
3130a76b4319SNeilBrown }
3131a76b4319SNeilBrown 
3132fd39ca9aSNeilBrown static time_t
31331da177e4SLinus Torvalds nfs4_laundromat(void)
31341da177e4SLinus Torvalds {
31351da177e4SLinus Torvalds 	struct nfs4_client *clp;
3136fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
31371da177e4SLinus Torvalds 	struct nfs4_delegation *dp;
31381da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
3139cf07d2eaSJ. Bruce Fields 	time_t cutoff = get_seconds() - nfsd4_lease;
3140cf07d2eaSJ. Bruce Fields 	time_t t, clientid_val = nfsd4_lease;
3141cf07d2eaSJ. Bruce Fields 	time_t u, test_val = nfsd4_lease;
31421da177e4SLinus Torvalds 
31431da177e4SLinus Torvalds 	nfs4_lock_state();
31441da177e4SLinus Torvalds 
31451da177e4SLinus Torvalds 	dprintk("NFSD: laundromat service - starting\n");
3146af558e33SJ. Bruce Fields 	if (locks_in_grace())
3147af558e33SJ. Bruce Fields 		nfsd4_end_grace();
314836acb66bSBenny Halevy 	INIT_LIST_HEAD(&reaplist);
314936acb66bSBenny Halevy 	spin_lock(&client_lock);
31501da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &client_lru) {
31511da177e4SLinus Torvalds 		clp = list_entry(pos, struct nfs4_client, cl_lru);
31521da177e4SLinus Torvalds 		if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
31531da177e4SLinus Torvalds 			t = clp->cl_time - cutoff;
31541da177e4SLinus Torvalds 			if (clientid_val > t)
31551da177e4SLinus Torvalds 				clientid_val = t;
31561da177e4SLinus Torvalds 			break;
31571da177e4SLinus Torvalds 		}
3158d7682988SBenny Halevy 		if (atomic_read(&clp->cl_refcount)) {
3159d7682988SBenny Halevy 			dprintk("NFSD: client in use (clientid %08x)\n",
3160d7682988SBenny Halevy 				clp->cl_clientid.cl_id);
3161d7682988SBenny Halevy 			continue;
3162d7682988SBenny Halevy 		}
3163d7682988SBenny Halevy 		unhash_client_locked(clp);
3164d7682988SBenny Halevy 		list_add(&clp->cl_lru, &reaplist);
316536acb66bSBenny Halevy 	}
316636acb66bSBenny Halevy 	spin_unlock(&client_lock);
316736acb66bSBenny Halevy 	list_for_each_safe(pos, next, &reaplist) {
316836acb66bSBenny Halevy 		clp = list_entry(pos, struct nfs4_client, cl_lru);
31691da177e4SLinus Torvalds 		dprintk("NFSD: purging unused client (clientid %08x)\n",
31701da177e4SLinus Torvalds 			clp->cl_clientid.cl_id);
3171c7b9a459SNeilBrown 		nfsd4_remove_clid_dir(clp);
31721da177e4SLinus Torvalds 		expire_client(clp);
31731da177e4SLinus Torvalds 	}
31741da177e4SLinus Torvalds 	spin_lock(&recall_lock);
31751da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &del_recall_lru) {
31761da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
31771da177e4SLinus Torvalds 		if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
31781da177e4SLinus Torvalds 			u = dp->dl_time - cutoff;
31791da177e4SLinus Torvalds 			if (test_val > u)
31801da177e4SLinus Torvalds 				test_val = u;
31811da177e4SLinus Torvalds 			break;
31821da177e4SLinus Torvalds 		}
31831da177e4SLinus Torvalds 		list_move(&dp->dl_recall_lru, &reaplist);
31841da177e4SLinus Torvalds 	}
31851da177e4SLinus Torvalds 	spin_unlock(&recall_lock);
31861da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &reaplist) {
31871da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
31881da177e4SLinus Torvalds 		unhash_delegation(dp);
31891da177e4SLinus Torvalds 	}
3190cf07d2eaSJ. Bruce Fields 	test_val = nfsd4_lease;
31911da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &close_lru) {
3192fe0750e5SJ. Bruce Fields 		oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
3193fe0750e5SJ. Bruce Fields 		if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
3194fe0750e5SJ. Bruce Fields 			u = oo->oo_time - cutoff;
31951da177e4SLinus Torvalds 			if (test_val > u)
31961da177e4SLinus Torvalds 				test_val = u;
31971da177e4SLinus Torvalds 			break;
31981da177e4SLinus Torvalds 		}
3199fe0750e5SJ. Bruce Fields 		release_openowner(oo);
32001da177e4SLinus Torvalds 	}
32011da177e4SLinus Torvalds 	if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
32021da177e4SLinus Torvalds 		clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
32031da177e4SLinus Torvalds 	nfs4_unlock_state();
32041da177e4SLinus Torvalds 	return clientid_val;
32051da177e4SLinus Torvalds }
32061da177e4SLinus Torvalds 
3207a254b246SHarvey Harrison static struct workqueue_struct *laundry_wq;
3208a254b246SHarvey Harrison static void laundromat_main(struct work_struct *);
3209a254b246SHarvey Harrison static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3210a254b246SHarvey Harrison 
3211a254b246SHarvey Harrison static void
3212c4028958SDavid Howells laundromat_main(struct work_struct *not_used)
32131da177e4SLinus Torvalds {
32141da177e4SLinus Torvalds 	time_t t;
32151da177e4SLinus Torvalds 
32161da177e4SLinus Torvalds 	t = nfs4_laundromat();
32171da177e4SLinus Torvalds 	dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
321858da282bSNeilBrown 	queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
32191da177e4SLinus Torvalds }
32201da177e4SLinus Torvalds 
3221f7a4d872SJ. Bruce Fields static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
3222f8816512SNeilBrown {
3223f7a4d872SJ. Bruce Fields 	if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
3224f7a4d872SJ. Bruce Fields 		return nfserr_bad_stateid;
3225f7a4d872SJ. Bruce Fields 	return nfs_ok;
32261da177e4SLinus Torvalds }
32271da177e4SLinus Torvalds 
32281da177e4SLinus Torvalds static int
32291da177e4SLinus Torvalds STALE_STATEID(stateid_t *stateid)
32301da177e4SLinus Torvalds {
3231d3b313a4SJ. Bruce Fields 	if (stateid->si_opaque.so_clid.cl_boot == boot_time)
3232e4e83ea4SJ. Bruce Fields 		return 0;
32338c10cbdbSBenny Halevy 	dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
32348c10cbdbSBenny Halevy 		STATEID_VAL(stateid));
32351da177e4SLinus Torvalds 	return 1;
32361da177e4SLinus Torvalds }
32371da177e4SLinus Torvalds 
32381da177e4SLinus Torvalds static inline int
32391da177e4SLinus Torvalds access_permit_read(unsigned long access_bmap)
32401da177e4SLinus Torvalds {
32411da177e4SLinus Torvalds 	return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
32421da177e4SLinus Torvalds 		test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
32431da177e4SLinus Torvalds 		test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
32441da177e4SLinus Torvalds }
32451da177e4SLinus Torvalds 
32461da177e4SLinus Torvalds static inline int
32471da177e4SLinus Torvalds access_permit_write(unsigned long access_bmap)
32481da177e4SLinus Torvalds {
32491da177e4SLinus Torvalds 	return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
32501da177e4SLinus Torvalds 		test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
32511da177e4SLinus Torvalds }
32521da177e4SLinus Torvalds 
32531da177e4SLinus Torvalds static
3254dcef0413SJ. Bruce Fields __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
32551da177e4SLinus Torvalds {
3256b37ad28bSAl Viro         __be32 status = nfserr_openmode;
32571da177e4SLinus Torvalds 
325802921914SJ. Bruce Fields 	/* For lock stateid's, we test the parent open, not the lock: */
325902921914SJ. Bruce Fields 	if (stp->st_openstp)
326002921914SJ. Bruce Fields 		stp = stp->st_openstp;
32611da177e4SLinus Torvalds 	if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
32621da177e4SLinus Torvalds                 goto out;
32631da177e4SLinus Torvalds 	if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
32641da177e4SLinus Torvalds                 goto out;
32651da177e4SLinus Torvalds 	status = nfs_ok;
32661da177e4SLinus Torvalds out:
32671da177e4SLinus Torvalds 	return status;
32681da177e4SLinus Torvalds }
32691da177e4SLinus Torvalds 
3270b37ad28bSAl Viro static inline __be32
32711da177e4SLinus Torvalds check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
32721da177e4SLinus Torvalds {
3273203a8c8eSJ. Bruce Fields 	if (ONE_STATEID(stateid) && (flags & RD_STATE))
32741da177e4SLinus Torvalds 		return nfs_ok;
3275af558e33SJ. Bruce Fields 	else if (locks_in_grace()) {
327625985edcSLucas De Marchi 		/* Answer in remaining cases depends on existence of
32771da177e4SLinus Torvalds 		 * conflicting state; so we must wait out the grace period. */
32781da177e4SLinus Torvalds 		return nfserr_grace;
32791da177e4SLinus Torvalds 	} else if (flags & WR_STATE)
32801da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
32811da177e4SLinus Torvalds 				NFS4_SHARE_DENY_WRITE);
32821da177e4SLinus Torvalds 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
32831da177e4SLinus Torvalds 		return nfs4_share_conflict(current_fh,
32841da177e4SLinus Torvalds 				NFS4_SHARE_DENY_READ);
32851da177e4SLinus Torvalds }
32861da177e4SLinus Torvalds 
32871da177e4SLinus Torvalds /*
32881da177e4SLinus Torvalds  * Allow READ/WRITE during grace period on recovered state only for files
32891da177e4SLinus Torvalds  * that are not able to provide mandatory locking.
32901da177e4SLinus Torvalds  */
32911da177e4SLinus Torvalds static inline int
329218f82731SJ. Bruce Fields grace_disallows_io(struct inode *inode)
32931da177e4SLinus Torvalds {
3294203a8c8eSJ. Bruce Fields 	return locks_in_grace() && mandatory_lock(inode);
32951da177e4SLinus Torvalds }
32961da177e4SLinus Torvalds 
329781b82965SJ. Bruce Fields /* Returns true iff a is later than b: */
329881b82965SJ. Bruce Fields static bool stateid_generation_after(stateid_t *a, stateid_t *b)
329981b82965SJ. Bruce Fields {
330081b82965SJ. Bruce Fields 	return (s32)a->si_generation - (s32)b->si_generation > 0;
330181b82965SJ. Bruce Fields }
330281b82965SJ. Bruce Fields 
330328dde241SJ. Bruce Fields static int check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
33040836f587SJ. Bruce Fields {
33056668958fSAndy Adamson 	/*
33066668958fSAndy Adamson 	 * When sessions are used the stateid generation number is ignored
33076668958fSAndy Adamson 	 * when it is zero.
33086668958fSAndy Adamson 	 */
330928dde241SJ. Bruce Fields 	if (has_session && in->si_generation == 0)
331081b82965SJ. Bruce Fields 		return nfs_ok;
331181b82965SJ. Bruce Fields 
331281b82965SJ. Bruce Fields 	if (in->si_generation == ref->si_generation)
331381b82965SJ. Bruce Fields 		return nfs_ok;
33146668958fSAndy Adamson 
33150836f587SJ. Bruce Fields 	/* If the client sends us a stateid from the future, it's buggy: */
331681b82965SJ. Bruce Fields 	if (stateid_generation_after(in, ref))
33170836f587SJ. Bruce Fields 		return nfserr_bad_stateid;
33180836f587SJ. Bruce Fields 	/*
331981b82965SJ. Bruce Fields 	 * However, we could see a stateid from the past, even from a
332081b82965SJ. Bruce Fields 	 * non-buggy client.  For example, if the client sends a lock
332181b82965SJ. Bruce Fields 	 * while some IO is outstanding, the lock may bump si_generation
332281b82965SJ. Bruce Fields 	 * while the IO is still in flight.  The client could avoid that
332381b82965SJ. Bruce Fields 	 * situation by waiting for responses on all the IO requests,
332481b82965SJ. Bruce Fields 	 * but better performance may result in retrying IO that
332581b82965SJ. Bruce Fields 	 * receives an old_stateid error if requests are rarely
332681b82965SJ. Bruce Fields 	 * reordered in flight:
33270836f587SJ. Bruce Fields 	 */
33280836f587SJ. Bruce Fields 	return nfserr_old_stateid;
33290836f587SJ. Bruce Fields }
33300836f587SJ. Bruce Fields 
333138c2f4b1SJ. Bruce Fields __be32 nfs4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
333217456804SBryan Schumaker {
333397b7e3b6SJ. Bruce Fields 	struct nfs4_stid *s;
333497b7e3b6SJ. Bruce Fields 	struct nfs4_ol_stateid *ols;
333597b7e3b6SJ. Bruce Fields 	__be32 status;
333617456804SBryan Schumaker 
333717456804SBryan Schumaker 	if (STALE_STATEID(stateid))
333897b7e3b6SJ. Bruce Fields 		return nfserr_stale_stateid;
333917456804SBryan Schumaker 
334038c2f4b1SJ. Bruce Fields 	s = find_stateid(cl, stateid);
334197b7e3b6SJ. Bruce Fields 	if (!s)
334297b7e3b6SJ. Bruce Fields 		 return nfserr_stale_stateid;
334336279ac1SJ. Bruce Fields 	status = check_stateid_generation(stateid, &s->sc_stateid, 1);
334417456804SBryan Schumaker 	if (status)
334517456804SBryan Schumaker 		return status;
334697b7e3b6SJ. Bruce Fields 	if (!(s->sc_type & (NFS4_OPEN_STID | NFS4_LOCK_STID)))
334797b7e3b6SJ. Bruce Fields 		return nfs_ok;
334897b7e3b6SJ. Bruce Fields 	ols = openlockstateid(s);
334997b7e3b6SJ. Bruce Fields 	if (ols->st_stateowner->so_is_open_owner
3350dad1c067SJ. Bruce Fields 	    && !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
335197b7e3b6SJ. Bruce Fields 		return nfserr_bad_stateid;
335297b7e3b6SJ. Bruce Fields 	return nfs_ok;
335317456804SBryan Schumaker }
335417456804SBryan Schumaker 
335538c2f4b1SJ. Bruce Fields static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask, struct nfs4_stid **s)
335638c2f4b1SJ. Bruce Fields {
335738c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl;
335838c2f4b1SJ. Bruce Fields 
335938c2f4b1SJ. Bruce Fields 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
336038c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
336138c2f4b1SJ. Bruce Fields 	if (STALE_STATEID(stateid))
336238c2f4b1SJ. Bruce Fields 		return nfserr_stale_stateid;
336338c2f4b1SJ. Bruce Fields 	cl = find_confirmed_client(&stateid->si_opaque.so_clid);
336438c2f4b1SJ. Bruce Fields 	if (!cl)
336538c2f4b1SJ. Bruce Fields 		return nfserr_expired;
336638c2f4b1SJ. Bruce Fields 	*s = find_stateid_by_type(cl, stateid, typemask);
336738c2f4b1SJ. Bruce Fields 	if (!*s)
336838c2f4b1SJ. Bruce Fields 		return nfserr_bad_stateid;
336938c2f4b1SJ. Bruce Fields 	return nfs_ok;
337038c2f4b1SJ. Bruce Fields 
337138c2f4b1SJ. Bruce Fields }
337238c2f4b1SJ. Bruce Fields 
33731da177e4SLinus Torvalds /*
33741da177e4SLinus Torvalds * Checks for stateid operations
33751da177e4SLinus Torvalds */
3376b37ad28bSAl Viro __be32
3377dd453dfdSBenny Halevy nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3378dd453dfdSBenny Halevy 			   stateid_t *stateid, int flags, struct file **filpp)
33791da177e4SLinus Torvalds {
338069064a27SJ. Bruce Fields 	struct nfs4_stid *s;
3381dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp = NULL;
33821da177e4SLinus Torvalds 	struct nfs4_delegation *dp = NULL;
3383dd453dfdSBenny Halevy 	struct svc_fh *current_fh = &cstate->current_fh;
33841da177e4SLinus Torvalds 	struct inode *ino = current_fh->fh_dentry->d_inode;
3385b37ad28bSAl Viro 	__be32 status;
33861da177e4SLinus Torvalds 
33871da177e4SLinus Torvalds 	if (filpp)
33881da177e4SLinus Torvalds 		*filpp = NULL;
33891da177e4SLinus Torvalds 
339018f82731SJ. Bruce Fields 	if (grace_disallows_io(ino))
33911da177e4SLinus Torvalds 		return nfserr_grace;
33921da177e4SLinus Torvalds 
33931da177e4SLinus Torvalds 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
33941da177e4SLinus Torvalds 		return check_special_stateids(current_fh, stateid, flags);
33951da177e4SLinus Torvalds 
339638c2f4b1SJ. Bruce Fields 	status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, &s);
339738c2f4b1SJ. Bruce Fields 	if (status)
339838c2f4b1SJ. Bruce Fields 		return status;
339969064a27SJ. Bruce Fields 	status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
34000c2a498fSJ. Bruce Fields 	if (status)
34010c2a498fSJ. Bruce Fields 		goto out;
3402f7a4d872SJ. Bruce Fields 	switch (s->sc_type) {
3403f7a4d872SJ. Bruce Fields 	case NFS4_DELEG_STID:
340469064a27SJ. Bruce Fields 		dp = delegstateid(s);
3405dc9bf700SJ. Bruce Fields 		status = nfs4_check_delegmode(dp, flags);
3406dc9bf700SJ. Bruce Fields 		if (status)
3407dc9bf700SJ. Bruce Fields 			goto out;
340843b0178eSDan Carpenter 		if (filpp) {
3409acfdf5c3SJ. Bruce Fields 			*filpp = dp->dl_file->fi_deleg_file;
3410f9d7562fSJ. Bruce Fields 			BUG_ON(!*filpp);
341143b0178eSDan Carpenter 		}
3412f7a4d872SJ. Bruce Fields 		break;
3413f7a4d872SJ. Bruce Fields 	case NFS4_OPEN_STID:
3414f7a4d872SJ. Bruce Fields 	case NFS4_LOCK_STID:
341569064a27SJ. Bruce Fields 		stp = openlockstateid(s);
3416f7a4d872SJ. Bruce Fields 		status = nfs4_check_fh(current_fh, stp);
3417f7a4d872SJ. Bruce Fields 		if (status)
34181da177e4SLinus Torvalds 			goto out;
3419fe0750e5SJ. Bruce Fields 		if (stp->st_stateowner->so_is_open_owner
3420dad1c067SJ. Bruce Fields 		    && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
34211da177e4SLinus Torvalds 			goto out;
3422a4455be0SJ. Bruce Fields 		status = nfs4_check_openmode(stp, flags);
3423a4455be0SJ. Bruce Fields 		if (status)
34241da177e4SLinus Torvalds 			goto out;
3425f9d7562fSJ. Bruce Fields 		if (filpp) {
3426f9d7562fSJ. Bruce Fields 			if (flags & RD_STATE)
3427f9d7562fSJ. Bruce Fields 				*filpp = find_readable_file(stp->st_file);
3428f9d7562fSJ. Bruce Fields 			else
3429f9d7562fSJ. Bruce Fields 				*filpp = find_writeable_file(stp->st_file);
3430f9d7562fSJ. Bruce Fields 		}
3431f7a4d872SJ. Bruce Fields 		break;
3432f7a4d872SJ. Bruce Fields 	default:
3433f7a4d872SJ. Bruce Fields 		return nfserr_bad_stateid;
34341da177e4SLinus Torvalds 	}
34351da177e4SLinus Torvalds 	status = nfs_ok;
34361da177e4SLinus Torvalds out:
34371da177e4SLinus Torvalds 	return status;
34381da177e4SLinus Torvalds }
34391da177e4SLinus Torvalds 
3440e1ca12dfSBryan Schumaker static __be32
3441dcef0413SJ. Bruce Fields nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
3442e1ca12dfSBryan Schumaker {
3443fe0750e5SJ. Bruce Fields 	if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
3444e1ca12dfSBryan Schumaker 		return nfserr_locks_held;
3445e1ca12dfSBryan Schumaker 	release_lock_stateid(stp);
3446e1ca12dfSBryan Schumaker 	return nfs_ok;
3447e1ca12dfSBryan Schumaker }
3448e1ca12dfSBryan Schumaker 
3449e1ca12dfSBryan Schumaker /*
345017456804SBryan Schumaker  * Test if the stateid is valid
345117456804SBryan Schumaker  */
345217456804SBryan Schumaker __be32
345317456804SBryan Schumaker nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
345417456804SBryan Schumaker 		   struct nfsd4_test_stateid *test_stateid)
345517456804SBryan Schumaker {
345603cfb420SBryan Schumaker 	struct nfsd4_test_stateid_id *stateid;
345703cfb420SBryan Schumaker 	struct nfs4_client *cl = cstate->session->se_client;
345803cfb420SBryan Schumaker 
345903cfb420SBryan Schumaker 	nfs4_lock_state();
346003cfb420SBryan Schumaker 	list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
346103cfb420SBryan Schumaker 		stateid->ts_id_status = nfs4_validate_stateid(cl, &stateid->ts_id_stateid);
346203cfb420SBryan Schumaker 	nfs4_unlock_state();
346303cfb420SBryan Schumaker 
346417456804SBryan Schumaker 	return nfs_ok;
346517456804SBryan Schumaker }
346617456804SBryan Schumaker 
3467e1ca12dfSBryan Schumaker __be32
3468e1ca12dfSBryan Schumaker nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3469e1ca12dfSBryan Schumaker 		   struct nfsd4_free_stateid *free_stateid)
3470e1ca12dfSBryan Schumaker {
3471e1ca12dfSBryan Schumaker 	stateid_t *stateid = &free_stateid->fr_stateid;
34722da1cec7SJ. Bruce Fields 	struct nfs4_stid *s;
347338c2f4b1SJ. Bruce Fields 	struct nfs4_client *cl = cstate->session->se_client;
34742da1cec7SJ. Bruce Fields 	__be32 ret = nfserr_bad_stateid;
3475e1ca12dfSBryan Schumaker 
3476e1ca12dfSBryan Schumaker 	nfs4_lock_state();
347738c2f4b1SJ. Bruce Fields 	s = find_stateid(cl, stateid);
34782da1cec7SJ. Bruce Fields 	if (!s)
3479e1ca12dfSBryan Schumaker 		goto out;
34802da1cec7SJ. Bruce Fields 	switch (s->sc_type) {
34812da1cec7SJ. Bruce Fields 	case NFS4_DELEG_STID:
3482e1ca12dfSBryan Schumaker 		ret = nfserr_locks_held;
3483e1ca12dfSBryan Schumaker 		goto out;
34842da1cec7SJ. Bruce Fields 	case NFS4_OPEN_STID:
34852da1cec7SJ. Bruce Fields 	case NFS4_LOCK_STID:
34862da1cec7SJ. Bruce Fields 		ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
34872da1cec7SJ. Bruce Fields 		if (ret)
3488e1ca12dfSBryan Schumaker 			goto out;
34892da1cec7SJ. Bruce Fields 		if (s->sc_type == NFS4_LOCK_STID)
34902da1cec7SJ. Bruce Fields 			ret = nfsd4_free_lock_stateid(openlockstateid(s));
34912da1cec7SJ. Bruce Fields 		else
34922da1cec7SJ. Bruce Fields 			ret = nfserr_locks_held;
3493f7a4d872SJ. Bruce Fields 		break;
3494f7a4d872SJ. Bruce Fields 	default:
3495f7a4d872SJ. Bruce Fields 		ret = nfserr_bad_stateid;
3496e1ca12dfSBryan Schumaker 	}
3497e1ca12dfSBryan Schumaker out:
3498e1ca12dfSBryan Schumaker 	nfs4_unlock_state();
3499e1ca12dfSBryan Schumaker 	return ret;
3500e1ca12dfSBryan Schumaker }
3501e1ca12dfSBryan Schumaker 
35024c4cd222SNeilBrown static inline int
35034c4cd222SNeilBrown setlkflg (int type)
35044c4cd222SNeilBrown {
35054c4cd222SNeilBrown 	return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
35064c4cd222SNeilBrown 		RD_STATE : WR_STATE;
35074c4cd222SNeilBrown }
35081da177e4SLinus Torvalds 
3509dcef0413SJ. Bruce Fields static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
3510c0a5d93eSJ. Bruce Fields {
3511c0a5d93eSJ. Bruce Fields 	struct svc_fh *current_fh = &cstate->current_fh;
3512c0a5d93eSJ. Bruce Fields 	struct nfs4_stateowner *sop = stp->st_stateowner;
3513c0a5d93eSJ. Bruce Fields 	__be32 status;
3514c0a5d93eSJ. Bruce Fields 
3515c0a5d93eSJ. Bruce Fields 	status = nfsd4_check_seqid(cstate, sop, seqid);
3516c0a5d93eSJ. Bruce Fields 	if (status)
3517c0a5d93eSJ. Bruce Fields 		return status;
3518f7a4d872SJ. Bruce Fields 	if (stp->st_stid.sc_type == NFS4_CLOSED_STID)
3519f7a4d872SJ. Bruce Fields 		/*
3520f7a4d872SJ. Bruce Fields 		 * "Closed" stateid's exist *only* to return
3521f7a4d872SJ. Bruce Fields 		 * nfserr_replay_me from the previous step.
3522f7a4d872SJ. Bruce Fields 		 */
3523f7a4d872SJ. Bruce Fields 		return nfserr_bad_stateid;
3524f7a4d872SJ. Bruce Fields 	status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
3525f7a4d872SJ. Bruce Fields 	if (status)
3526f7a4d872SJ. Bruce Fields 		return status;
3527f7a4d872SJ. Bruce Fields 	return nfs4_check_fh(current_fh, stp);
3528c0a5d93eSJ. Bruce Fields }
3529c0a5d93eSJ. Bruce Fields 
35301da177e4SLinus Torvalds /*
35311da177e4SLinus Torvalds  * Checks for sequence id mutating operations.
35321da177e4SLinus Torvalds  */
3533b37ad28bSAl Viro static __be32
3534dd453dfdSBenny Halevy nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
35352288d0e3SJ. Bruce Fields 			 stateid_t *stateid, char typemask,
3536dcef0413SJ. Bruce Fields 			 struct nfs4_ol_stateid **stpp)
35371da177e4SLinus Torvalds {
35380836f587SJ. Bruce Fields 	__be32 status;
353938c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
35401da177e4SLinus Torvalds 
35418c10cbdbSBenny Halevy 	dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
35428c10cbdbSBenny Halevy 		seqid, STATEID_VAL(stateid));
35431da177e4SLinus Torvalds 
35441da177e4SLinus Torvalds 	*stpp = NULL;
354538c2f4b1SJ. Bruce Fields 	status = nfsd4_lookup_stateid(stateid, typemask, &s);
3546c0a5d93eSJ. Bruce Fields 	if (status)
3547c0a5d93eSJ. Bruce Fields 		return status;
354838c2f4b1SJ. Bruce Fields 	*stpp = openlockstateid(s);
3549c0a5d93eSJ. Bruce Fields 	cstate->replay_owner = (*stpp)->st_stateowner;
35501da177e4SLinus Torvalds 
3551c0a5d93eSJ. Bruce Fields 	return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
35521da177e4SLinus Torvalds }
35531da177e4SLinus Torvalds 
3554dcef0413SJ. Bruce Fields static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, stateid_t *stateid, struct nfs4_ol_stateid **stpp)
3555c0a5d93eSJ. Bruce Fields {
3556c0a5d93eSJ. Bruce Fields 	__be32 status;
3557c0a5d93eSJ. Bruce Fields 	struct nfs4_openowner *oo;
35581da177e4SLinus Torvalds 
3559c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
35602288d0e3SJ. Bruce Fields 						NFS4_OPEN_STID, stpp);
35610836f587SJ. Bruce Fields 	if (status)
35620836f587SJ. Bruce Fields 		return status;
3563c0a5d93eSJ. Bruce Fields 	oo = openowner((*stpp)->st_stateowner);
3564dad1c067SJ. Bruce Fields 	if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
3565c0a5d93eSJ. Bruce Fields 		return nfserr_bad_stateid;
35663a4f98bbSNeilBrown 	return nfs_ok;
35671da177e4SLinus Torvalds }
35681da177e4SLinus Torvalds 
3569b37ad28bSAl Viro __be32
3570ca364317SJ.Bruce Fields nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3571a4f1706aSJ.Bruce Fields 		   struct nfsd4_open_confirm *oc)
35721da177e4SLinus Torvalds {
3573b37ad28bSAl Viro 	__be32 status;
3574fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
3575dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
35761da177e4SLinus Torvalds 
35771da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3578ca364317SJ.Bruce Fields 			(int)cstate->current_fh.fh_dentry->d_name.len,
3579ca364317SJ.Bruce Fields 			cstate->current_fh.fh_dentry->d_name.name);
35801da177e4SLinus Torvalds 
3581ca364317SJ.Bruce Fields 	status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3582a8cddc5dSJ. Bruce Fields 	if (status)
3583a8cddc5dSJ. Bruce Fields 		return status;
35841da177e4SLinus Torvalds 
35851da177e4SLinus Torvalds 	nfs4_lock_state();
35861da177e4SLinus Torvalds 
35879072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate,
3588ca364317SJ.Bruce Fields 					oc->oc_seqid, &oc->oc_req_stateid,
35892288d0e3SJ. Bruce Fields 					NFS4_OPEN_STID, &stp);
35909072d5c6SJ. Bruce Fields 	if (status)
35911da177e4SLinus Torvalds 		goto out;
3592fe0750e5SJ. Bruce Fields 	oo = openowner(stp->st_stateowner);
359368b66e82SJ. Bruce Fields 	status = nfserr_bad_stateid;
3594dad1c067SJ. Bruce Fields 	if (oo->oo_flags & NFS4_OO_CONFIRMED)
359568b66e82SJ. Bruce Fields 		goto out;
3596dad1c067SJ. Bruce Fields 	oo->oo_flags |= NFS4_OO_CONFIRMED;
3597dcef0413SJ. Bruce Fields 	update_stateid(&stp->st_stid.sc_stateid);
3598dcef0413SJ. Bruce Fields 	memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
35998c10cbdbSBenny Halevy 	dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3600dcef0413SJ. Bruce Fields 		__func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
3601c7b9a459SNeilBrown 
3602fe0750e5SJ. Bruce Fields 	nfsd4_create_clid_dir(oo->oo_owner.so_client);
360368b66e82SJ. Bruce Fields 	status = nfs_ok;
36041da177e4SLinus Torvalds out:
36055ec094c1SJ. Bruce Fields 	if (!cstate->replay_owner)
36061da177e4SLinus Torvalds 		nfs4_unlock_state();
36071da177e4SLinus Torvalds 	return status;
36081da177e4SLinus Torvalds }
36091da177e4SLinus Torvalds 
36106409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
36111da177e4SLinus Torvalds {
36126409a5a6SJ. Bruce Fields 	if (!test_bit(access, &stp->st_access_bmap))
36136409a5a6SJ. Bruce Fields 		return;
36146409a5a6SJ. Bruce Fields 	nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
36156409a5a6SJ. Bruce Fields 	__clear_bit(access, &stp->st_access_bmap);
3616f197c271SJ. Bruce Fields }
36176409a5a6SJ. Bruce Fields 
36186409a5a6SJ. Bruce Fields static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
36196409a5a6SJ. Bruce Fields {
36206409a5a6SJ. Bruce Fields 	switch (to_access) {
36216409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_READ:
36226409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
36236409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
36246409a5a6SJ. Bruce Fields 		break;
36256409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_WRITE:
36266409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
36276409a5a6SJ. Bruce Fields 		nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
36286409a5a6SJ. Bruce Fields 		break;
36296409a5a6SJ. Bruce Fields 	case NFS4_SHARE_ACCESS_BOTH:
36306409a5a6SJ. Bruce Fields 		break;
36316409a5a6SJ. Bruce Fields 	default:
36326409a5a6SJ. Bruce Fields 		BUG();
36331da177e4SLinus Torvalds 	}
36341da177e4SLinus Torvalds }
36351da177e4SLinus Torvalds 
36361da177e4SLinus Torvalds static void
36371da177e4SLinus Torvalds reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
36381da177e4SLinus Torvalds {
36391da177e4SLinus Torvalds 	int i;
36401da177e4SLinus Torvalds 	for (i = 0; i < 4; i++) {
36411da177e4SLinus Torvalds 		if ((i & deny) != i)
36421da177e4SLinus Torvalds 			__clear_bit(i, bmap);
36431da177e4SLinus Torvalds 	}
36441da177e4SLinus Torvalds }
36451da177e4SLinus Torvalds 
3646b37ad28bSAl Viro __be32
3647ca364317SJ.Bruce Fields nfsd4_open_downgrade(struct svc_rqst *rqstp,
3648ca364317SJ.Bruce Fields 		     struct nfsd4_compound_state *cstate,
3649a4f1706aSJ.Bruce Fields 		     struct nfsd4_open_downgrade *od)
36501da177e4SLinus Torvalds {
3651b37ad28bSAl Viro 	__be32 status;
3652dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
36531da177e4SLinus Torvalds 
36541da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
3655ca364317SJ.Bruce Fields 			(int)cstate->current_fh.fh_dentry->d_name.len,
3656ca364317SJ.Bruce Fields 			cstate->current_fh.fh_dentry->d_name.name);
36571da177e4SLinus Torvalds 
3658c30e92dfSJ. Bruce Fields 	/* We don't yet support WANT bits: */
36592c8bd7e0SBenny Halevy 	if (od->od_deleg_want)
36602c8bd7e0SBenny Halevy 		dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
36612c8bd7e0SBenny Halevy 			od->od_deleg_want);
36621da177e4SLinus Torvalds 
36631da177e4SLinus Torvalds 	nfs4_lock_state();
3664c0a5d93eSJ. Bruce Fields 	status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3665c0a5d93eSJ. Bruce Fields 					&od->od_stateid, &stp);
36669072d5c6SJ. Bruce Fields 	if (status)
36671da177e4SLinus Torvalds 		goto out;
36681da177e4SLinus Torvalds 	status = nfserr_inval;
36691da177e4SLinus Torvalds 	if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
36701da177e4SLinus Torvalds 		dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
36711da177e4SLinus Torvalds 			stp->st_access_bmap, od->od_share_access);
36721da177e4SLinus Torvalds 		goto out;
36731da177e4SLinus Torvalds 	}
36741da177e4SLinus Torvalds 	if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
36751da177e4SLinus Torvalds 		dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
36761da177e4SLinus Torvalds 			stp->st_deny_bmap, od->od_share_deny);
36771da177e4SLinus Torvalds 		goto out;
36781da177e4SLinus Torvalds 	}
36796409a5a6SJ. Bruce Fields 	nfs4_stateid_downgrade(stp, od->od_share_access);
36801da177e4SLinus Torvalds 
36811da177e4SLinus Torvalds 	reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
36821da177e4SLinus Torvalds 
3683dcef0413SJ. Bruce Fields 	update_stateid(&stp->st_stid.sc_stateid);
3684dcef0413SJ. Bruce Fields 	memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
36851da177e4SLinus Torvalds 	status = nfs_ok;
36861da177e4SLinus Torvalds out:
36875ec094c1SJ. Bruce Fields 	if (!cstate->replay_owner)
36881da177e4SLinus Torvalds 		nfs4_unlock_state();
36891da177e4SLinus Torvalds 	return status;
36901da177e4SLinus Torvalds }
36911da177e4SLinus Torvalds 
369238c387b5SJ. Bruce Fields void nfsd4_purge_closed_stateid(struct nfs4_stateowner *so)
369338c387b5SJ. Bruce Fields {
369438c387b5SJ. Bruce Fields 	struct nfs4_openowner *oo;
369538c387b5SJ. Bruce Fields 	struct nfs4_ol_stateid *s;
369638c387b5SJ. Bruce Fields 
369738c387b5SJ. Bruce Fields 	if (!so->so_is_open_owner)
369838c387b5SJ. Bruce Fields 		return;
369938c387b5SJ. Bruce Fields 	oo = openowner(so);
370038c387b5SJ. Bruce Fields 	s = oo->oo_last_closed_stid;
370138c387b5SJ. Bruce Fields 	if (!s)
370238c387b5SJ. Bruce Fields 		return;
370338c387b5SJ. Bruce Fields 	if (!(oo->oo_flags & NFS4_OO_PURGE_CLOSE)) {
370438c387b5SJ. Bruce Fields 		/* Release the last_closed_stid on the next seqid bump: */
370538c387b5SJ. Bruce Fields 		oo->oo_flags |= NFS4_OO_PURGE_CLOSE;
370638c387b5SJ. Bruce Fields 		return;
370738c387b5SJ. Bruce Fields 	}
370838c387b5SJ. Bruce Fields 	oo->oo_flags &= ~NFS4_OO_PURGE_CLOSE;
3709f7a4d872SJ. Bruce Fields 	release_last_closed_stateid(oo);
3710f7a4d872SJ. Bruce Fields }
3711f7a4d872SJ. Bruce Fields 
3712f7a4d872SJ. Bruce Fields static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
3713f7a4d872SJ. Bruce Fields {
3714f7a4d872SJ. Bruce Fields 	unhash_open_stateid(s);
3715f7a4d872SJ. Bruce Fields 	s->st_stid.sc_type = NFS4_CLOSED_STID;
371638c387b5SJ. Bruce Fields }
371738c387b5SJ. Bruce Fields 
37181da177e4SLinus Torvalds /*
37191da177e4SLinus Torvalds  * nfs4_unlock_state() called after encode
37201da177e4SLinus Torvalds  */
3721b37ad28bSAl Viro __be32
3722ca364317SJ.Bruce Fields nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3723a4f1706aSJ.Bruce Fields 	    struct nfsd4_close *close)
37241da177e4SLinus Torvalds {
3725b37ad28bSAl Viro 	__be32 status;
3726fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *oo;
3727dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
37281da177e4SLinus Torvalds 
37291da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_close on file %.*s\n",
3730ca364317SJ.Bruce Fields 			(int)cstate->current_fh.fh_dentry->d_name.len,
3731ca364317SJ.Bruce Fields 			cstate->current_fh.fh_dentry->d_name.name);
37321da177e4SLinus Torvalds 
37331da177e4SLinus Torvalds 	nfs4_lock_state();
3734f7a4d872SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
3735f7a4d872SJ. Bruce Fields 					&close->cl_stateid,
3736f7a4d872SJ. Bruce Fields 					NFS4_OPEN_STID|NFS4_CLOSED_STID,
3737f7a4d872SJ. Bruce Fields 					&stp);
37389072d5c6SJ. Bruce Fields 	if (status)
37391da177e4SLinus Torvalds 		goto out;
3740fe0750e5SJ. Bruce Fields 	oo = openowner(stp->st_stateowner);
37411da177e4SLinus Torvalds 	status = nfs_ok;
3742dcef0413SJ. Bruce Fields 	update_stateid(&stp->st_stid.sc_stateid);
3743dcef0413SJ. Bruce Fields 	memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
37441da177e4SLinus Torvalds 
3745f7a4d872SJ. Bruce Fields 	nfsd4_close_open_stateid(stp);
374638c387b5SJ. Bruce Fields 	oo->oo_last_closed_stid = stp;
374704ef5954SJ. Bruce Fields 
374804ef5954SJ. Bruce Fields 	/* place unused nfs4_stateowners on so_close_lru list to be
374904ef5954SJ. Bruce Fields 	 * released by the laundromat service after the lease period
375004ef5954SJ. Bruce Fields 	 * to enable us to handle CLOSE replay
375104ef5954SJ. Bruce Fields 	 */
3752fe0750e5SJ. Bruce Fields 	if (list_empty(&oo->oo_owner.so_stateids))
3753fe0750e5SJ. Bruce Fields 		move_to_close_lru(oo);
37541da177e4SLinus Torvalds out:
37555ec094c1SJ. Bruce Fields 	if (!cstate->replay_owner)
37561da177e4SLinus Torvalds 		nfs4_unlock_state();
37571da177e4SLinus Torvalds 	return status;
37581da177e4SLinus Torvalds }
37591da177e4SLinus Torvalds 
3760b37ad28bSAl Viro __be32
3761ca364317SJ.Bruce Fields nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3762ca364317SJ.Bruce Fields 		  struct nfsd4_delegreturn *dr)
37631da177e4SLinus Torvalds {
3764203a8c8eSJ. Bruce Fields 	struct nfs4_delegation *dp;
3765203a8c8eSJ. Bruce Fields 	stateid_t *stateid = &dr->dr_stateid;
376638c2f4b1SJ. Bruce Fields 	struct nfs4_stid *s;
3767203a8c8eSJ. Bruce Fields 	struct inode *inode;
3768b37ad28bSAl Viro 	__be32 status;
37691da177e4SLinus Torvalds 
3770ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3771203a8c8eSJ. Bruce Fields 		return status;
3772203a8c8eSJ. Bruce Fields 	inode = cstate->current_fh.fh_dentry->d_inode;
37731da177e4SLinus Torvalds 
37741da177e4SLinus Torvalds 	nfs4_lock_state();
377538c2f4b1SJ. Bruce Fields 	status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s);
377638c2f4b1SJ. Bruce Fields 	if (status)
3777203a8c8eSJ. Bruce Fields 		goto out;
377838c2f4b1SJ. Bruce Fields 	dp = delegstateid(s);
3779d5477a8dSJ. Bruce Fields 	status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
3780203a8c8eSJ. Bruce Fields 	if (status)
3781203a8c8eSJ. Bruce Fields 		goto out;
3782203a8c8eSJ. Bruce Fields 
3783203a8c8eSJ. Bruce Fields 	unhash_delegation(dp);
37841da177e4SLinus Torvalds out:
3785203a8c8eSJ. Bruce Fields 	nfs4_unlock_state();
3786203a8c8eSJ. Bruce Fields 
37871da177e4SLinus Torvalds 	return status;
37881da177e4SLinus Torvalds }
37891da177e4SLinus Torvalds 
37901da177e4SLinus Torvalds 
37911da177e4SLinus Torvalds #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
37921da177e4SLinus Torvalds 
3793009673b4SJ. Bruce Fields #define LOCKOWNER_INO_HASH_BITS 8
3794009673b4SJ. Bruce Fields #define LOCKOWNER_INO_HASH_SIZE (1 << LOCKOWNER_INO_HASH_BITS)
3795009673b4SJ. Bruce Fields #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
37961da177e4SLinus Torvalds 
379787df4de8SBenny Halevy static inline u64
379887df4de8SBenny Halevy end_offset(u64 start, u64 len)
379987df4de8SBenny Halevy {
380087df4de8SBenny Halevy 	u64 end;
380187df4de8SBenny Halevy 
380287df4de8SBenny Halevy 	end = start + len;
380387df4de8SBenny Halevy 	return end >= start ? end: NFS4_MAX_UINT64;
380487df4de8SBenny Halevy }
380587df4de8SBenny Halevy 
380687df4de8SBenny Halevy /* last octet in a range */
380787df4de8SBenny Halevy static inline u64
380887df4de8SBenny Halevy last_byte_offset(u64 start, u64 len)
380987df4de8SBenny Halevy {
381087df4de8SBenny Halevy 	u64 end;
381187df4de8SBenny Halevy 
381287df4de8SBenny Halevy 	BUG_ON(!len);
381387df4de8SBenny Halevy 	end = start + len;
381487df4de8SBenny Halevy 	return end > start ? end - 1: NFS4_MAX_UINT64;
381587df4de8SBenny Halevy }
381687df4de8SBenny Halevy 
3817009673b4SJ. Bruce Fields static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
38181da177e4SLinus Torvalds {
38191da177e4SLinus Torvalds 	return (file_hashval(inode) + cl_id
38201da177e4SLinus Torvalds 			+ opaque_hashval(ownername->data, ownername->len))
3821009673b4SJ. Bruce Fields 		& LOCKOWNER_INO_HASH_MASK;
38221da177e4SLinus Torvalds }
38231da177e4SLinus Torvalds 
3824009673b4SJ. Bruce Fields static struct list_head lockowner_ino_hashtbl[LOCKOWNER_INO_HASH_SIZE];
38251da177e4SLinus Torvalds 
38261da177e4SLinus Torvalds /*
38271da177e4SLinus Torvalds  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
38281da177e4SLinus Torvalds  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
38291da177e4SLinus Torvalds  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
38301da177e4SLinus Torvalds  * locking, this prevents us from being completely protocol-compliant.  The
38311da177e4SLinus Torvalds  * real solution to this problem is to start using unsigned file offsets in
38321da177e4SLinus Torvalds  * the VFS, but this is a very deep change!
38331da177e4SLinus Torvalds  */
38341da177e4SLinus Torvalds static inline void
38351da177e4SLinus Torvalds nfs4_transform_lock_offset(struct file_lock *lock)
38361da177e4SLinus Torvalds {
38371da177e4SLinus Torvalds 	if (lock->fl_start < 0)
38381da177e4SLinus Torvalds 		lock->fl_start = OFFSET_MAX;
38391da177e4SLinus Torvalds 	if (lock->fl_end < 0)
38401da177e4SLinus Torvalds 		lock->fl_end = OFFSET_MAX;
38411da177e4SLinus Torvalds }
38421da177e4SLinus Torvalds 
3843d5b9026aSNeilBrown /* Hack!: For now, we're defining this just so we can use a pointer to it
3844d5b9026aSNeilBrown  * as a unique cookie to identify our (NFSv4's) posix locks. */
38457b021967SAlexey Dobriyan static const struct lock_manager_operations nfsd_posix_mng_ops  = {
3846d5b9026aSNeilBrown };
38471da177e4SLinus Torvalds 
38481da177e4SLinus Torvalds static inline void
38491da177e4SLinus Torvalds nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
38501da177e4SLinus Torvalds {
3851fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
38521da177e4SLinus Torvalds 
3853d5b9026aSNeilBrown 	if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3854fe0750e5SJ. Bruce Fields 		lo = (struct nfs4_lockowner *) fl->fl_owner;
3855fe0750e5SJ. Bruce Fields 		deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
3856fe0750e5SJ. Bruce Fields 					lo->lo_owner.so_owner.len, GFP_KERNEL);
38577c13f344SJ. Bruce Fields 		if (!deny->ld_owner.data)
38587c13f344SJ. Bruce Fields 			/* We just don't care that much */
38597c13f344SJ. Bruce Fields 			goto nevermind;
3860fe0750e5SJ. Bruce Fields 		deny->ld_owner.len = lo->lo_owner.so_owner.len;
3861fe0750e5SJ. Bruce Fields 		deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
3862d5b9026aSNeilBrown 	} else {
38637c13f344SJ. Bruce Fields nevermind:
38647c13f344SJ. Bruce Fields 		deny->ld_owner.len = 0;
38657c13f344SJ. Bruce Fields 		deny->ld_owner.data = NULL;
3866d5b9026aSNeilBrown 		deny->ld_clientid.cl_boot = 0;
3867d5b9026aSNeilBrown 		deny->ld_clientid.cl_id = 0;
38681da177e4SLinus Torvalds 	}
38691da177e4SLinus Torvalds 	deny->ld_start = fl->fl_start;
387087df4de8SBenny Halevy 	deny->ld_length = NFS4_MAX_UINT64;
387187df4de8SBenny Halevy 	if (fl->fl_end != NFS4_MAX_UINT64)
38721da177e4SLinus Torvalds 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
38731da177e4SLinus Torvalds 	deny->ld_type = NFS4_READ_LT;
38741da177e4SLinus Torvalds 	if (fl->fl_type != F_RDLCK)
38751da177e4SLinus Torvalds 		deny->ld_type = NFS4_WRITE_LT;
38761da177e4SLinus Torvalds }
38771da177e4SLinus Torvalds 
3878b93d87c1SJ. Bruce Fields static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
3879b93d87c1SJ. Bruce Fields {
3880b93d87c1SJ. Bruce Fields 	struct nfs4_ol_stateid *lst;
3881b93d87c1SJ. Bruce Fields 
3882b93d87c1SJ. Bruce Fields 	if (!same_owner_str(&lo->lo_owner, owner, clid))
3883b93d87c1SJ. Bruce Fields 		return false;
3884b93d87c1SJ. Bruce Fields 	lst = list_first_entry(&lo->lo_owner.so_stateids,
3885b93d87c1SJ. Bruce Fields 			       struct nfs4_ol_stateid, st_perstateowner);
3886b93d87c1SJ. Bruce Fields 	return lst->st_file->fi_inode == inode;
3887b93d87c1SJ. Bruce Fields }
3888b93d87c1SJ. Bruce Fields 
3889fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
3890fe0750e5SJ. Bruce Fields find_lockowner_str(struct inode *inode, clientid_t *clid,
38911da177e4SLinus Torvalds 		struct xdr_netobj *owner)
38921da177e4SLinus Torvalds {
3893009673b4SJ. Bruce Fields 	unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
3894b93d87c1SJ. Bruce Fields 	struct nfs4_lockowner *lo;
38951da177e4SLinus Torvalds 
3896009673b4SJ. Bruce Fields 	list_for_each_entry(lo, &lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
3897b93d87c1SJ. Bruce Fields 		if (same_lockowner_ino(lo, inode, clid, owner))
3898b93d87c1SJ. Bruce Fields 			return lo;
38991da177e4SLinus Torvalds 	}
39001da177e4SLinus Torvalds 	return NULL;
39011da177e4SLinus Torvalds }
39021da177e4SLinus Torvalds 
3903dcef0413SJ. Bruce Fields static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
3904ff194bd9SJ. Bruce Fields {
3905009673b4SJ. Bruce Fields 	struct inode *inode = open_stp->st_file->fi_inode;
3906009673b4SJ. Bruce Fields 	unsigned int inohash = lockowner_ino_hashval(inode,
3907009673b4SJ. Bruce Fields 			clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
3908009673b4SJ. Bruce Fields 
390916bfdaafSJ. Bruce Fields 	list_add(&lo->lo_owner.so_strhash, &ownerstr_hashtbl[strhashval]);
3910009673b4SJ. Bruce Fields 	list_add(&lo->lo_owner_ino_hash, &lockowner_ino_hashtbl[inohash]);
3911fe0750e5SJ. Bruce Fields 	list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
3912ff194bd9SJ. Bruce Fields }
3913ff194bd9SJ. Bruce Fields 
39141da177e4SLinus Torvalds /*
39151da177e4SLinus Torvalds  * Alloc a lock owner structure.
39161da177e4SLinus Torvalds  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
391725985edcSLucas De Marchi  * occurred.
39181da177e4SLinus Torvalds  *
391916bfdaafSJ. Bruce Fields  * strhashval = ownerstr_hashval
39201da177e4SLinus Torvalds  */
39211da177e4SLinus Torvalds 
3922fe0750e5SJ. Bruce Fields static struct nfs4_lockowner *
3923dcef0413SJ. Bruce Fields alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
3924fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
39251da177e4SLinus Torvalds 
3926fe0750e5SJ. Bruce Fields 	lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
3927fe0750e5SJ. Bruce Fields 	if (!lo)
39281da177e4SLinus Torvalds 		return NULL;
3929fe0750e5SJ. Bruce Fields 	INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
3930fe0750e5SJ. Bruce Fields 	lo->lo_owner.so_is_open_owner = 0;
3931b59e3c0eSNeil Brown 	/* It is the openowner seqid that will be incremented in encode in the
3932b59e3c0eSNeil Brown 	 * case of new lockowners; so increment the lock seqid manually: */
3933fe0750e5SJ. Bruce Fields 	lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
3934fe0750e5SJ. Bruce Fields 	hash_lockowner(lo, strhashval, clp, open_stp);
3935fe0750e5SJ. Bruce Fields 	return lo;
39361da177e4SLinus Torvalds }
39371da177e4SLinus Torvalds 
3938dcef0413SJ. Bruce Fields static struct nfs4_ol_stateid *
3939dcef0413SJ. Bruce Fields alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
39401da177e4SLinus Torvalds {
3941dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
3942d3b313a4SJ. Bruce Fields 	struct nfs4_client *clp = lo->lo_owner.so_client;
39431da177e4SLinus Torvalds 
3944996e0938SJ. Bruce Fields 	stp = nfs4_alloc_stateid(clp);
39455ac049acSNeilBrown 	if (stp == NULL)
39466136d2b4SJ. Bruce Fields 		return NULL;
3947996e0938SJ. Bruce Fields 	init_stid(&stp->st_stid, clp, NFS4_LOCK_STID);
39488beefa24SNeilBrown 	list_add(&stp->st_perfile, &fp->fi_stateids);
3949fe0750e5SJ. Bruce Fields 	list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
3950fe0750e5SJ. Bruce Fields 	stp->st_stateowner = &lo->lo_owner;
395113cd2184SNeilBrown 	get_nfs4_file(fp);
39521da177e4SLinus Torvalds 	stp->st_file = fp;
39530997b173SJ. Bruce Fields 	stp->st_access_bmap = 0;
39541da177e4SLinus Torvalds 	stp->st_deny_bmap = open_stp->st_deny_bmap;
39554c4cd222SNeilBrown 	stp->st_openstp = open_stp;
39561da177e4SLinus Torvalds 	return stp;
39571da177e4SLinus Torvalds }
39581da177e4SLinus Torvalds 
3959fd39ca9aSNeilBrown static int
39601da177e4SLinus Torvalds check_lock_length(u64 offset, u64 length)
39611da177e4SLinus Torvalds {
396287df4de8SBenny Halevy 	return ((length == 0)  || ((length != NFS4_MAX_UINT64) &&
39631da177e4SLinus Torvalds 	     LOFF_OVERFLOW(offset, length)));
39641da177e4SLinus Torvalds }
39651da177e4SLinus Torvalds 
3966dcef0413SJ. Bruce Fields static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
39670997b173SJ. Bruce Fields {
39680997b173SJ. Bruce Fields 	struct nfs4_file *fp = lock_stp->st_file;
39690997b173SJ. Bruce Fields 	int oflag = nfs4_access_to_omode(access);
39700997b173SJ. Bruce Fields 
39710997b173SJ. Bruce Fields 	if (test_bit(access, &lock_stp->st_access_bmap))
39720997b173SJ. Bruce Fields 		return;
39730997b173SJ. Bruce Fields 	nfs4_file_get_access(fp, oflag);
39740997b173SJ. Bruce Fields 	__set_bit(access, &lock_stp->st_access_bmap);
39750997b173SJ. Bruce Fields }
39760997b173SJ. Bruce Fields 
397764a284d0SJ. Bruce Fields __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
397864a284d0SJ. Bruce Fields {
397964a284d0SJ. Bruce Fields 	struct nfs4_file *fi = ost->st_file;
398064a284d0SJ. Bruce Fields 	struct nfs4_openowner *oo = openowner(ost->st_stateowner);
398164a284d0SJ. Bruce Fields 	struct nfs4_client *cl = oo->oo_owner.so_client;
398264a284d0SJ. Bruce Fields 	struct nfs4_lockowner *lo;
398364a284d0SJ. Bruce Fields 	unsigned int strhashval;
398464a284d0SJ. Bruce Fields 
398564a284d0SJ. Bruce Fields 	lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid, &lock->v.new.owner);
398664a284d0SJ. Bruce Fields 	if (lo) {
398764a284d0SJ. Bruce Fields 		if (!cstate->minorversion)
398864a284d0SJ. Bruce Fields 			return nfserr_bad_seqid;
398964a284d0SJ. Bruce Fields 		/* XXX: a lockowner always has exactly one stateid: */
399064a284d0SJ. Bruce Fields 		*lst = list_first_entry(&lo->lo_owner.so_stateids,
399164a284d0SJ. Bruce Fields 				struct nfs4_ol_stateid, st_perstateowner);
399264a284d0SJ. Bruce Fields 		return nfs_ok;
399364a284d0SJ. Bruce Fields 	}
399416bfdaafSJ. Bruce Fields 	strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
399564a284d0SJ. Bruce Fields 			&lock->v.new.owner);
399664a284d0SJ. Bruce Fields 	lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
399764a284d0SJ. Bruce Fields 	if (lo == NULL)
399864a284d0SJ. Bruce Fields 		return nfserr_jukebox;
399964a284d0SJ. Bruce Fields 	*lst = alloc_init_lock_stateid(lo, fi, ost);
400064a284d0SJ. Bruce Fields 	if (*lst == NULL) {
400164a284d0SJ. Bruce Fields 		release_lockowner(lo);
400264a284d0SJ. Bruce Fields 		return nfserr_jukebox;
400364a284d0SJ. Bruce Fields 	}
400464a284d0SJ. Bruce Fields 	*new = true;
400564a284d0SJ. Bruce Fields 	return nfs_ok;
400664a284d0SJ. Bruce Fields }
400764a284d0SJ. Bruce Fields 
40081da177e4SLinus Torvalds /*
40091da177e4SLinus Torvalds  *  LOCK operation
40101da177e4SLinus Torvalds  */
4011b37ad28bSAl Viro __be32
4012ca364317SJ.Bruce Fields nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4013a4f1706aSJ.Bruce Fields 	   struct nfsd4_lock *lock)
40141da177e4SLinus Torvalds {
4015fe0750e5SJ. Bruce Fields 	struct nfs4_openowner *open_sop = NULL;
4016fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lock_sop = NULL;
4017dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *lock_stp;
40187d947842SJ. Bruce Fields 	struct nfs4_file *fp;
40197d947842SJ. Bruce Fields 	struct file *filp = NULL;
40201da177e4SLinus Torvalds 	struct file_lock file_lock;
40218dc7c311SAndy Adamson 	struct file_lock conflock;
4022b37ad28bSAl Viro 	__be32 status = 0;
402364a284d0SJ. Bruce Fields 	bool new_state = false;
4024b34f27aaSJ. Bruce Fields 	int lkflg;
4025b8dd7b9aSAl Viro 	int err;
40261da177e4SLinus Torvalds 
40271da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
40281da177e4SLinus Torvalds 		(long long) lock->lk_offset,
40291da177e4SLinus Torvalds 		(long long) lock->lk_length);
40301da177e4SLinus Torvalds 
40311da177e4SLinus Torvalds 	if (check_lock_length(lock->lk_offset, lock->lk_length))
40321da177e4SLinus Torvalds 		 return nfserr_inval;
40331da177e4SLinus Torvalds 
4034ca364317SJ.Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh,
40358837abcaSMiklos Szeredi 				S_IFREG, NFSD_MAY_LOCK))) {
4036a6f6ef2fSAndy Adamson 		dprintk("NFSD: nfsd4_lock: permission denied!\n");
4037a6f6ef2fSAndy Adamson 		return status;
4038a6f6ef2fSAndy Adamson 	}
4039a6f6ef2fSAndy Adamson 
40401da177e4SLinus Torvalds 	nfs4_lock_state();
40411da177e4SLinus Torvalds 
40421da177e4SLinus Torvalds 	if (lock->lk_is_new) {
40431da177e4SLinus Torvalds 		/*
40441da177e4SLinus Torvalds 		 * Client indicates that this is a new lockowner.
4045893f8770SNeilBrown 		 * Use open owner and open stateid to create lock owner and
4046893f8770SNeilBrown 		 * lock stateid.
40471da177e4SLinus Torvalds 		 */
4048dcef0413SJ. Bruce Fields 		struct nfs4_ol_stateid *open_stp = NULL;
40491da177e4SLinus Torvalds 
4050684e5638SJ. Bruce Fields 		if (nfsd4_has_session(cstate))
4051684e5638SJ. Bruce Fields 			/* See rfc 5661 18.10.3: given clientid is ignored: */
4052684e5638SJ. Bruce Fields 			memcpy(&lock->v.new.clientid,
4053684e5638SJ. Bruce Fields 				&cstate->session->se_client->cl_clientid,
4054684e5638SJ. Bruce Fields 				sizeof(clientid_t));
4055684e5638SJ. Bruce Fields 
40561da177e4SLinus Torvalds 		status = nfserr_stale_clientid;
4057684e5638SJ. Bruce Fields 		if (STALE_CLIENTID(&lock->lk_new_clientid))
40581da177e4SLinus Torvalds 			goto out;
40591da177e4SLinus Torvalds 
40601da177e4SLinus Torvalds 		/* validate and update open stateid and open seqid */
4061c0a5d93eSJ. Bruce Fields 		status = nfs4_preprocess_confirmed_seqid_op(cstate,
40621da177e4SLinus Torvalds 				        lock->lk_new_open_seqid,
40631da177e4SLinus Torvalds 		                        &lock->lk_new_open_stateid,
4064c0a5d93eSJ. Bruce Fields 					&open_stp);
406537515177SNeilBrown 		if (status)
40661da177e4SLinus Torvalds 			goto out;
4067fe0750e5SJ. Bruce Fields 		open_sop = openowner(open_stp->st_stateowner);
4068b34f27aaSJ. Bruce Fields 		status = nfserr_bad_stateid;
4069684e5638SJ. Bruce Fields 		if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
4070b34f27aaSJ. Bruce Fields 						&lock->v.new.clientid))
4071b34f27aaSJ. Bruce Fields 			goto out;
407264a284d0SJ. Bruce Fields 		status = lookup_or_create_lock_state(cstate, open_stp, lock,
407364a284d0SJ. Bruce Fields 							&lock_stp, &new_state);
407464a284d0SJ. Bruce Fields 		if (status)
40751da177e4SLinus Torvalds 			goto out;
40761da177e4SLinus Torvalds 	} else {
40771da177e4SLinus Torvalds 		/* lock (lock owner + lock stateid) already exists */
4078dd453dfdSBenny Halevy 		status = nfs4_preprocess_seqid_op(cstate,
40791da177e4SLinus Torvalds 				       lock->lk_old_lock_seqid,
40801da177e4SLinus Torvalds 				       &lock->lk_old_lock_stateid,
40812288d0e3SJ. Bruce Fields 				       NFS4_LOCK_STID, &lock_stp);
40821da177e4SLinus Torvalds 		if (status)
40831da177e4SLinus Torvalds 			goto out;
408464a284d0SJ. Bruce Fields 	}
4085fe0750e5SJ. Bruce Fields 	lock_sop = lockowner(lock_stp->st_stateowner);
40867d947842SJ. Bruce Fields 	fp = lock_stp->st_file;
40871da177e4SLinus Torvalds 
4088b34f27aaSJ. Bruce Fields 	lkflg = setlkflg(lock->lk_type);
4089b34f27aaSJ. Bruce Fields 	status = nfs4_check_openmode(lock_stp, lkflg);
4090b34f27aaSJ. Bruce Fields 	if (status)
4091b34f27aaSJ. Bruce Fields 		goto out;
4092b34f27aaSJ. Bruce Fields 
40930dd395dcSNeilBrown 	status = nfserr_grace;
4094af558e33SJ. Bruce Fields 	if (locks_in_grace() && !lock->lk_reclaim)
40950dd395dcSNeilBrown 		goto out;
40960dd395dcSNeilBrown 	status = nfserr_no_grace;
4097af558e33SJ. Bruce Fields 	if (!locks_in_grace() && lock->lk_reclaim)
40980dd395dcSNeilBrown 		goto out;
40990dd395dcSNeilBrown 
41001da177e4SLinus Torvalds 	locks_init_lock(&file_lock);
41011da177e4SLinus Torvalds 	switch (lock->lk_type) {
41021da177e4SLinus Torvalds 		case NFS4_READ_LT:
41031da177e4SLinus Torvalds 		case NFS4_READW_LT:
4104f9d7562fSJ. Bruce Fields 			filp = find_readable_file(lock_stp->st_file);
41050997b173SJ. Bruce Fields 			if (filp)
41060997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
41071da177e4SLinus Torvalds 			file_lock.fl_type = F_RDLCK;
41081da177e4SLinus Torvalds 			break;
41091da177e4SLinus Torvalds 		case NFS4_WRITE_LT:
41101da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
4111f9d7562fSJ. Bruce Fields 			filp = find_writeable_file(lock_stp->st_file);
41120997b173SJ. Bruce Fields 			if (filp)
41130997b173SJ. Bruce Fields 				get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
41141da177e4SLinus Torvalds 			file_lock.fl_type = F_WRLCK;
41151da177e4SLinus Torvalds 			break;
41161da177e4SLinus Torvalds 		default:
41171da177e4SLinus Torvalds 			status = nfserr_inval;
41181da177e4SLinus Torvalds 		goto out;
41191da177e4SLinus Torvalds 	}
4120f9d7562fSJ. Bruce Fields 	if (!filp) {
4121f9d7562fSJ. Bruce Fields 		status = nfserr_openmode;
4122f9d7562fSJ. Bruce Fields 		goto out;
4123f9d7562fSJ. Bruce Fields 	}
4124b59e3c0eSNeil Brown 	file_lock.fl_owner = (fl_owner_t)lock_sop;
41251da177e4SLinus Torvalds 	file_lock.fl_pid = current->tgid;
41261da177e4SLinus Torvalds 	file_lock.fl_file = filp;
41271da177e4SLinus Torvalds 	file_lock.fl_flags = FL_POSIX;
4128d5b9026aSNeilBrown 	file_lock.fl_lmops = &nfsd_posix_mng_ops;
41291da177e4SLinus Torvalds 
41301da177e4SLinus Torvalds 	file_lock.fl_start = lock->lk_offset;
413187df4de8SBenny Halevy 	file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
41321da177e4SLinus Torvalds 	nfs4_transform_lock_offset(&file_lock);
41331da177e4SLinus Torvalds 
41341da177e4SLinus Torvalds 	/*
41351da177e4SLinus Torvalds 	* Try to lock the file in the VFS.
41361da177e4SLinus Torvalds 	* Note: locks.c uses the BKL to protect the inode's lock list.
41371da177e4SLinus Torvalds 	*/
41381da177e4SLinus Torvalds 
4139529d7b2aSJ. Bruce Fields 	err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
4140b8dd7b9aSAl Viro 	switch (-err) {
41411da177e4SLinus Torvalds 	case 0: /* success! */
4142dcef0413SJ. Bruce Fields 		update_stateid(&lock_stp->st_stid.sc_stateid);
4143dcef0413SJ. Bruce Fields 		memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
41441da177e4SLinus Torvalds 				sizeof(stateid_t));
4145b8dd7b9aSAl Viro 		status = 0;
4146eb76b3fdSAndy Adamson 		break;
4147eb76b3fdSAndy Adamson 	case (EAGAIN):		/* conflock holds conflicting lock */
4148eb76b3fdSAndy Adamson 		status = nfserr_denied;
4149eb76b3fdSAndy Adamson 		dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
4150eb76b3fdSAndy Adamson 		nfs4_set_lock_denied(&conflock, &lock->lk_denied);
4151eb76b3fdSAndy Adamson 		break;
41521da177e4SLinus Torvalds 	case (EDEADLK):
41531da177e4SLinus Torvalds 		status = nfserr_deadlock;
4154eb76b3fdSAndy Adamson 		break;
41551da177e4SLinus Torvalds 	default:
4156fd85b817SMarc Eshel 		dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
41573e772463SJ. Bruce Fields 		status = nfserrno(err);
4158eb76b3fdSAndy Adamson 		break;
41591da177e4SLinus Torvalds 	}
41601da177e4SLinus Torvalds out:
416164a284d0SJ. Bruce Fields 	if (status && new_state)
4162f044ff83SJ. Bruce Fields 		release_lockowner(lock_sop);
41635ec094c1SJ. Bruce Fields 	if (!cstate->replay_owner)
41641da177e4SLinus Torvalds 		nfs4_unlock_state();
41651da177e4SLinus Torvalds 	return status;
41661da177e4SLinus Torvalds }
41671da177e4SLinus Torvalds 
41681da177e4SLinus Torvalds /*
416955ef1274SJ. Bruce Fields  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
417055ef1274SJ. Bruce Fields  * so we do a temporary open here just to get an open file to pass to
417155ef1274SJ. Bruce Fields  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
417255ef1274SJ. Bruce Fields  * inode operation.)
417355ef1274SJ. Bruce Fields  */
417455ef1274SJ. Bruce Fields static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
417555ef1274SJ. Bruce Fields {
417655ef1274SJ. Bruce Fields 	struct file *file;
417755ef1274SJ. Bruce Fields 	int err;
417855ef1274SJ. Bruce Fields 
417955ef1274SJ. Bruce Fields 	err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
418055ef1274SJ. Bruce Fields 	if (err)
418155ef1274SJ. Bruce Fields 		return err;
418255ef1274SJ. Bruce Fields 	err = vfs_test_lock(file, lock);
418355ef1274SJ. Bruce Fields 	nfsd_close(file);
418455ef1274SJ. Bruce Fields 	return err;
418555ef1274SJ. Bruce Fields }
418655ef1274SJ. Bruce Fields 
418755ef1274SJ. Bruce Fields /*
41881da177e4SLinus Torvalds  * LOCKT operation
41891da177e4SLinus Torvalds  */
4190b37ad28bSAl Viro __be32
4191ca364317SJ.Bruce Fields nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4192ca364317SJ.Bruce Fields 	    struct nfsd4_lockt *lockt)
41931da177e4SLinus Torvalds {
41941da177e4SLinus Torvalds 	struct inode *inode;
41951da177e4SLinus Torvalds 	struct file_lock file_lock;
4196fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
4197fd85b817SMarc Eshel 	int error;
4198b37ad28bSAl Viro 	__be32 status;
41991da177e4SLinus Torvalds 
4200af558e33SJ. Bruce Fields 	if (locks_in_grace())
42011da177e4SLinus Torvalds 		return nfserr_grace;
42021da177e4SLinus Torvalds 
42031da177e4SLinus Torvalds 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
42041da177e4SLinus Torvalds 		 return nfserr_inval;
42051da177e4SLinus Torvalds 
42061da177e4SLinus Torvalds 	nfs4_lock_state();
42071da177e4SLinus Torvalds 
42081da177e4SLinus Torvalds 	status = nfserr_stale_clientid;
420960adfc50SAndy Adamson 	if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
42101da177e4SLinus Torvalds 		goto out;
42111da177e4SLinus Torvalds 
421275c096f7SJ. Bruce Fields 	if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
42131da177e4SLinus Torvalds 		goto out;
42141da177e4SLinus Torvalds 
4215ca364317SJ.Bruce Fields 	inode = cstate->current_fh.fh_dentry->d_inode;
42161da177e4SLinus Torvalds 	locks_init_lock(&file_lock);
42171da177e4SLinus Torvalds 	switch (lockt->lt_type) {
42181da177e4SLinus Torvalds 		case NFS4_READ_LT:
42191da177e4SLinus Torvalds 		case NFS4_READW_LT:
42201da177e4SLinus Torvalds 			file_lock.fl_type = F_RDLCK;
42211da177e4SLinus Torvalds 		break;
42221da177e4SLinus Torvalds 		case NFS4_WRITE_LT:
42231da177e4SLinus Torvalds 		case NFS4_WRITEW_LT:
42241da177e4SLinus Torvalds 			file_lock.fl_type = F_WRLCK;
42251da177e4SLinus Torvalds 		break;
42261da177e4SLinus Torvalds 		default:
42272fdada03SJ. Bruce Fields 			dprintk("NFSD: nfs4_lockt: bad lock type!\n");
42281da177e4SLinus Torvalds 			status = nfserr_inval;
42291da177e4SLinus Torvalds 		goto out;
42301da177e4SLinus Torvalds 	}
42311da177e4SLinus Torvalds 
4232fe0750e5SJ. Bruce Fields 	lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner);
4233fe0750e5SJ. Bruce Fields 	if (lo)
4234fe0750e5SJ. Bruce Fields 		file_lock.fl_owner = (fl_owner_t)lo;
42351da177e4SLinus Torvalds 	file_lock.fl_pid = current->tgid;
42361da177e4SLinus Torvalds 	file_lock.fl_flags = FL_POSIX;
42371da177e4SLinus Torvalds 
42381da177e4SLinus Torvalds 	file_lock.fl_start = lockt->lt_offset;
423987df4de8SBenny Halevy 	file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
42401da177e4SLinus Torvalds 
42411da177e4SLinus Torvalds 	nfs4_transform_lock_offset(&file_lock);
42421da177e4SLinus Torvalds 
42431da177e4SLinus Torvalds 	status = nfs_ok;
424455ef1274SJ. Bruce Fields 	error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4245fd85b817SMarc Eshel 	if (error) {
4246fd85b817SMarc Eshel 		status = nfserrno(error);
4247fd85b817SMarc Eshel 		goto out;
4248fd85b817SMarc Eshel 	}
42499d6a8c5cSMarc Eshel 	if (file_lock.fl_type != F_UNLCK) {
42501da177e4SLinus Torvalds 		status = nfserr_denied;
42519d6a8c5cSMarc Eshel 		nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
42521da177e4SLinus Torvalds 	}
42531da177e4SLinus Torvalds out:
42541da177e4SLinus Torvalds 	nfs4_unlock_state();
42551da177e4SLinus Torvalds 	return status;
42561da177e4SLinus Torvalds }
42571da177e4SLinus Torvalds 
4258b37ad28bSAl Viro __be32
4259ca364317SJ.Bruce Fields nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4260a4f1706aSJ.Bruce Fields 	    struct nfsd4_locku *locku)
42611da177e4SLinus Torvalds {
4262dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
42631da177e4SLinus Torvalds 	struct file *filp = NULL;
42641da177e4SLinus Torvalds 	struct file_lock file_lock;
4265b37ad28bSAl Viro 	__be32 status;
4266b8dd7b9aSAl Viro 	int err;
42671da177e4SLinus Torvalds 
42681da177e4SLinus Torvalds 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
42691da177e4SLinus Torvalds 		(long long) locku->lu_offset,
42701da177e4SLinus Torvalds 		(long long) locku->lu_length);
42711da177e4SLinus Torvalds 
42721da177e4SLinus Torvalds 	if (check_lock_length(locku->lu_offset, locku->lu_length))
42731da177e4SLinus Torvalds 		 return nfserr_inval;
42741da177e4SLinus Torvalds 
42751da177e4SLinus Torvalds 	nfs4_lock_state();
42761da177e4SLinus Torvalds 
42779072d5c6SJ. Bruce Fields 	status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
42782288d0e3SJ. Bruce Fields 					&locku->lu_stateid, NFS4_LOCK_STID, &stp);
42799072d5c6SJ. Bruce Fields 	if (status)
42801da177e4SLinus Torvalds 		goto out;
4281f9d7562fSJ. Bruce Fields 	filp = find_any_file(stp->st_file);
4282f9d7562fSJ. Bruce Fields 	if (!filp) {
4283f9d7562fSJ. Bruce Fields 		status = nfserr_lock_range;
4284f9d7562fSJ. Bruce Fields 		goto out;
4285f9d7562fSJ. Bruce Fields 	}
42861da177e4SLinus Torvalds 	BUG_ON(!filp);
42871da177e4SLinus Torvalds 	locks_init_lock(&file_lock);
42881da177e4SLinus Torvalds 	file_lock.fl_type = F_UNLCK;
4289fe0750e5SJ. Bruce Fields 	file_lock.fl_owner = (fl_owner_t)lockowner(stp->st_stateowner);
42901da177e4SLinus Torvalds 	file_lock.fl_pid = current->tgid;
42911da177e4SLinus Torvalds 	file_lock.fl_file = filp;
42921da177e4SLinus Torvalds 	file_lock.fl_flags = FL_POSIX;
4293d5b9026aSNeilBrown 	file_lock.fl_lmops = &nfsd_posix_mng_ops;
42941da177e4SLinus Torvalds 	file_lock.fl_start = locku->lu_offset;
42951da177e4SLinus Torvalds 
429687df4de8SBenny Halevy 	file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
42971da177e4SLinus Torvalds 	nfs4_transform_lock_offset(&file_lock);
42981da177e4SLinus Torvalds 
42991da177e4SLinus Torvalds 	/*
43001da177e4SLinus Torvalds 	*  Try to unlock the file in the VFS.
43011da177e4SLinus Torvalds 	*/
4302fd85b817SMarc Eshel 	err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4303b8dd7b9aSAl Viro 	if (err) {
4304fd85b817SMarc Eshel 		dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
43051da177e4SLinus Torvalds 		goto out_nfserr;
43061da177e4SLinus Torvalds 	}
43071da177e4SLinus Torvalds 	/*
43081da177e4SLinus Torvalds 	* OK, unlock succeeded; the only thing left to do is update the stateid.
43091da177e4SLinus Torvalds 	*/
4310dcef0413SJ. Bruce Fields 	update_stateid(&stp->st_stid.sc_stateid);
4311dcef0413SJ. Bruce Fields 	memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
43121da177e4SLinus Torvalds 
43131da177e4SLinus Torvalds out:
431471c3bcd7SJ. Bruce Fields 	if (!cstate->replay_owner)
43151da177e4SLinus Torvalds 		nfs4_unlock_state();
43161da177e4SLinus Torvalds 	return status;
43171da177e4SLinus Torvalds 
43181da177e4SLinus Torvalds out_nfserr:
4319b8dd7b9aSAl Viro 	status = nfserrno(err);
43201da177e4SLinus Torvalds 	goto out;
43211da177e4SLinus Torvalds }
43221da177e4SLinus Torvalds 
43231da177e4SLinus Torvalds /*
43241da177e4SLinus Torvalds  * returns
43251da177e4SLinus Torvalds  * 	1: locks held by lockowner
43261da177e4SLinus Torvalds  * 	0: no locks held by lockowner
43271da177e4SLinus Torvalds  */
43281da177e4SLinus Torvalds static int
4329fe0750e5SJ. Bruce Fields check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
43301da177e4SLinus Torvalds {
43311da177e4SLinus Torvalds 	struct file_lock **flpp;
4332f9d7562fSJ. Bruce Fields 	struct inode *inode = filp->fi_inode;
43331da177e4SLinus Torvalds 	int status = 0;
43341da177e4SLinus Torvalds 
4335b89f4321SArnd Bergmann 	lock_flocks();
43361da177e4SLinus Torvalds 	for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4337796dadfdSJ. Bruce Fields 		if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
43381da177e4SLinus Torvalds 			status = 1;
43391da177e4SLinus Torvalds 			goto out;
43401da177e4SLinus Torvalds 		}
4341796dadfdSJ. Bruce Fields 	}
43421da177e4SLinus Torvalds out:
4343b89f4321SArnd Bergmann 	unlock_flocks();
43441da177e4SLinus Torvalds 	return status;
43451da177e4SLinus Torvalds }
43461da177e4SLinus Torvalds 
4347b37ad28bSAl Viro __be32
4348b591480bSJ.Bruce Fields nfsd4_release_lockowner(struct svc_rqst *rqstp,
4349b591480bSJ.Bruce Fields 			struct nfsd4_compound_state *cstate,
4350b591480bSJ.Bruce Fields 			struct nfsd4_release_lockowner *rlockowner)
43511da177e4SLinus Torvalds {
43521da177e4SLinus Torvalds 	clientid_t *clid = &rlockowner->rl_clientid;
43533e9e3dbeSNeilBrown 	struct nfs4_stateowner *sop;
4354fe0750e5SJ. Bruce Fields 	struct nfs4_lockowner *lo;
4355dcef0413SJ. Bruce Fields 	struct nfs4_ol_stateid *stp;
43561da177e4SLinus Torvalds 	struct xdr_netobj *owner = &rlockowner->rl_owner;
43573e9e3dbeSNeilBrown 	struct list_head matches;
435816bfdaafSJ. Bruce Fields 	unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
4359b37ad28bSAl Viro 	__be32 status;
43601da177e4SLinus Torvalds 
43611da177e4SLinus Torvalds 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
43621da177e4SLinus Torvalds 		clid->cl_boot, clid->cl_id);
43631da177e4SLinus Torvalds 
43641da177e4SLinus Torvalds 	/* XXX check for lease expiration */
43651da177e4SLinus Torvalds 
43661da177e4SLinus Torvalds 	status = nfserr_stale_clientid;
4367849823c5SNeil Brown 	if (STALE_CLIENTID(clid))
43681da177e4SLinus Torvalds 		return status;
43691da177e4SLinus Torvalds 
43701da177e4SLinus Torvalds 	nfs4_lock_state();
43711da177e4SLinus Torvalds 
43721da177e4SLinus Torvalds 	status = nfserr_locks_held;
43733e9e3dbeSNeilBrown 	INIT_LIST_HEAD(&matches);
437406f1f864SJ. Bruce Fields 
437516bfdaafSJ. Bruce Fields 	list_for_each_entry(sop, &ownerstr_hashtbl[hashval], so_strhash) {
437616bfdaafSJ. Bruce Fields 		if (sop->so_is_open_owner)
437716bfdaafSJ. Bruce Fields 			continue;
4378599e0a22SJ. Bruce Fields 		if (!same_owner_str(sop, owner, clid))
43793e9e3dbeSNeilBrown 			continue;
43803e9e3dbeSNeilBrown 		list_for_each_entry(stp, &sop->so_stateids,
4381ea1da636SNeilBrown 				st_perstateowner) {
4382fe0750e5SJ. Bruce Fields 			lo = lockowner(sop);
4383fe0750e5SJ. Bruce Fields 			if (check_for_locks(stp->st_file, lo))
43841da177e4SLinus Torvalds 				goto out;
4385fe0750e5SJ. Bruce Fields 			list_add(&lo->lo_list, &matches);
43861da177e4SLinus Torvalds 		}
43873e9e3dbeSNeilBrown 	}
43883e9e3dbeSNeilBrown 	/* Clients probably won't expect us to return with some (but not all)
43893e9e3dbeSNeilBrown 	 * of the lockowner state released; so don't release any until all
43903e9e3dbeSNeilBrown 	 * have been checked. */
43911da177e4SLinus Torvalds 	status = nfs_ok;
43920fa822e4SNeilBrown 	while (!list_empty(&matches)) {
4393fe0750e5SJ. Bruce Fields 		lo = list_entry(matches.next, struct nfs4_lockowner,
4394fe0750e5SJ. Bruce Fields 								lo_list);
43950fa822e4SNeilBrown 		/* unhash_stateowner deletes so_perclient only
43960fa822e4SNeilBrown 		 * for openowners. */
4397fe0750e5SJ. Bruce Fields 		list_del(&lo->lo_list);
4398fe0750e5SJ. Bruce Fields 		release_lockowner(lo);
43991da177e4SLinus Torvalds 	}
44001da177e4SLinus Torvalds out:
44011da177e4SLinus Torvalds 	nfs4_unlock_state();
44021da177e4SLinus Torvalds 	return status;
44031da177e4SLinus Torvalds }
44041da177e4SLinus Torvalds 
44051da177e4SLinus Torvalds static inline struct nfs4_client_reclaim *
4406a55370a3SNeilBrown alloc_reclaim(void)
44071da177e4SLinus Torvalds {
4408a55370a3SNeilBrown 	return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
44091da177e4SLinus Torvalds }
44101da177e4SLinus Torvalds 
4411c7b9a459SNeilBrown int
4412a1bcecd2SAndy Adamson nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4413c7b9a459SNeilBrown {
4414c7b9a459SNeilBrown 	unsigned int strhashval = clientstr_hashval(name);
4415c7b9a459SNeilBrown 	struct nfs4_client *clp;
4416c7b9a459SNeilBrown 
4417e203d506SJ. Bruce Fields 	clp = find_confirmed_client_by_str(name, strhashval);
4418*393d8ed8SJ. Bruce Fields 	if (!clp)
4419*393d8ed8SJ. Bruce Fields 		return 0;
4420*393d8ed8SJ. Bruce Fields 	return clp->cl_firststate;
4421c7b9a459SNeilBrown }
4422c7b9a459SNeilBrown 
44231da177e4SLinus Torvalds /*
44241da177e4SLinus Torvalds  * failure => all reset bets are off, nfserr_no_grace...
44251da177e4SLinus Torvalds  */
4426190e4fbfSNeilBrown int
4427190e4fbfSNeilBrown nfs4_client_to_reclaim(const char *name)
44281da177e4SLinus Torvalds {
44291da177e4SLinus Torvalds 	unsigned int strhashval;
44301da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
44311da177e4SLinus Torvalds 
4432a55370a3SNeilBrown 	dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4433a55370a3SNeilBrown 	crp = alloc_reclaim();
44341da177e4SLinus Torvalds 	if (!crp)
44351da177e4SLinus Torvalds 		return 0;
4436a55370a3SNeilBrown 	strhashval = clientstr_hashval(name);
44371da177e4SLinus Torvalds 	INIT_LIST_HEAD(&crp->cr_strhash);
44381da177e4SLinus Torvalds 	list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4439a55370a3SNeilBrown 	memcpy(crp->cr_recdir, name, HEXDIR_LEN);
44401da177e4SLinus Torvalds 	reclaim_str_hashtbl_size++;
44411da177e4SLinus Torvalds 	return 1;
44421da177e4SLinus Torvalds }
44431da177e4SLinus Torvalds 
44441da177e4SLinus Torvalds static void
44451da177e4SLinus Torvalds nfs4_release_reclaim(void)
44461da177e4SLinus Torvalds {
44471da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
44481da177e4SLinus Torvalds 	int i;
44491da177e4SLinus Torvalds 
44501da177e4SLinus Torvalds 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
44511da177e4SLinus Torvalds 		while (!list_empty(&reclaim_str_hashtbl[i])) {
44521da177e4SLinus Torvalds 			crp = list_entry(reclaim_str_hashtbl[i].next,
44531da177e4SLinus Torvalds 			                struct nfs4_client_reclaim, cr_strhash);
44541da177e4SLinus Torvalds 			list_del(&crp->cr_strhash);
44551da177e4SLinus Torvalds 			kfree(crp);
44561da177e4SLinus Torvalds 			reclaim_str_hashtbl_size--;
44571da177e4SLinus Torvalds 		}
44581da177e4SLinus Torvalds 	}
44591da177e4SLinus Torvalds 	BUG_ON(reclaim_str_hashtbl_size);
44601da177e4SLinus Torvalds }
44611da177e4SLinus Torvalds 
44621da177e4SLinus Torvalds /*
44631da177e4SLinus Torvalds  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4464fd39ca9aSNeilBrown static struct nfs4_client_reclaim *
44651da177e4SLinus Torvalds nfs4_find_reclaim_client(clientid_t *clid)
44661da177e4SLinus Torvalds {
44671da177e4SLinus Torvalds 	unsigned int strhashval;
44681da177e4SLinus Torvalds 	struct nfs4_client *clp;
44691da177e4SLinus Torvalds 	struct nfs4_client_reclaim *crp = NULL;
44701da177e4SLinus Torvalds 
44711da177e4SLinus Torvalds 
44721da177e4SLinus Torvalds 	/* find clientid in conf_id_hashtbl */
44731da177e4SLinus Torvalds 	clp = find_confirmed_client(clid);
44741da177e4SLinus Torvalds 	if (clp == NULL)
44751da177e4SLinus Torvalds 		return NULL;
44761da177e4SLinus Torvalds 
4477a55370a3SNeilBrown 	dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4478a55370a3SNeilBrown 		            clp->cl_name.len, clp->cl_name.data,
4479a55370a3SNeilBrown 			    clp->cl_recdir);
44801da177e4SLinus Torvalds 
44811da177e4SLinus Torvalds 	/* find clp->cl_name in reclaim_str_hashtbl */
4482a55370a3SNeilBrown 	strhashval = clientstr_hashval(clp->cl_recdir);
44831da177e4SLinus Torvalds 	list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4484a55370a3SNeilBrown 		if (same_name(crp->cr_recdir, clp->cl_recdir)) {
44851da177e4SLinus Torvalds 			return crp;
44861da177e4SLinus Torvalds 		}
44871da177e4SLinus Torvalds 	}
44881da177e4SLinus Torvalds 	return NULL;
44891da177e4SLinus Torvalds }
44901da177e4SLinus Torvalds 
44911da177e4SLinus Torvalds /*
44921da177e4SLinus Torvalds * Called from OPEN. Look for clientid in reclaim list.
44931da177e4SLinus Torvalds */
4494b37ad28bSAl Viro __be32
44951da177e4SLinus Torvalds nfs4_check_open_reclaim(clientid_t *clid)
44961da177e4SLinus Torvalds {
4497dfc83565SNeilBrown 	return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
44981da177e4SLinus Torvalds }
44991da177e4SLinus Torvalds 
450065178db4SBryan Schumaker #ifdef CONFIG_NFSD_FAULT_INJECTION
450165178db4SBryan Schumaker 
450265178db4SBryan Schumaker void nfsd_forget_clients(u64 num)
450365178db4SBryan Schumaker {
450465178db4SBryan Schumaker 	struct nfs4_client *clp, *next;
450565178db4SBryan Schumaker 	int count = 0;
450665178db4SBryan Schumaker 
450765178db4SBryan Schumaker 	nfs4_lock_state();
450865178db4SBryan Schumaker 	list_for_each_entry_safe(clp, next, &client_lru, cl_lru) {
450965178db4SBryan Schumaker 		nfsd4_remove_clid_dir(clp);
451065178db4SBryan Schumaker 		expire_client(clp);
451165178db4SBryan Schumaker 		if (++count == num)
451265178db4SBryan Schumaker 			break;
451365178db4SBryan Schumaker 	}
451465178db4SBryan Schumaker 	nfs4_unlock_state();
451565178db4SBryan Schumaker 
451665178db4SBryan Schumaker 	printk(KERN_INFO "NFSD: Forgot %d clients", count);
451765178db4SBryan Schumaker }
451865178db4SBryan Schumaker 
451965178db4SBryan Schumaker static void release_lockowner_sop(struct nfs4_stateowner *sop)
452065178db4SBryan Schumaker {
452165178db4SBryan Schumaker 	release_lockowner(lockowner(sop));
452265178db4SBryan Schumaker }
452365178db4SBryan Schumaker 
452465178db4SBryan Schumaker static void release_openowner_sop(struct nfs4_stateowner *sop)
452565178db4SBryan Schumaker {
452665178db4SBryan Schumaker 	release_openowner(openowner(sop));
452765178db4SBryan Schumaker }
452865178db4SBryan Schumaker 
4529353de31bSJ. Bruce Fields static int nfsd_release_n_owners(u64 num, bool is_open_owner,
453065178db4SBryan Schumaker 				void (*release_sop)(struct nfs4_stateowner *))
453165178db4SBryan Schumaker {
453265178db4SBryan Schumaker 	int i, count = 0;
453365178db4SBryan Schumaker 	struct nfs4_stateowner *sop, *next;
453465178db4SBryan Schumaker 
453516bfdaafSJ. Bruce Fields 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
453616bfdaafSJ. Bruce Fields 		list_for_each_entry_safe(sop, next, &ownerstr_hashtbl[i], so_strhash) {
453716bfdaafSJ. Bruce Fields 			if (sop->so_is_open_owner != is_open_owner)
453816bfdaafSJ. Bruce Fields 				continue;
453965178db4SBryan Schumaker 			release_sop(sop);
454065178db4SBryan Schumaker 			if (++count == num)
454165178db4SBryan Schumaker 				return count;
454265178db4SBryan Schumaker 		}
454365178db4SBryan Schumaker 	}
454465178db4SBryan Schumaker 	return count;
454565178db4SBryan Schumaker }
454665178db4SBryan Schumaker 
454765178db4SBryan Schumaker void nfsd_forget_locks(u64 num)
454865178db4SBryan Schumaker {
454965178db4SBryan Schumaker 	int count;
455065178db4SBryan Schumaker 
455165178db4SBryan Schumaker 	nfs4_lock_state();
455216bfdaafSJ. Bruce Fields 	count = nfsd_release_n_owners(num, false, release_lockowner_sop);
455365178db4SBryan Schumaker 	nfs4_unlock_state();
455465178db4SBryan Schumaker 
455565178db4SBryan Schumaker 	printk(KERN_INFO "NFSD: Forgot %d locks", count);
455665178db4SBryan Schumaker }
455765178db4SBryan Schumaker 
455865178db4SBryan Schumaker void nfsd_forget_openowners(u64 num)
455965178db4SBryan Schumaker {
456065178db4SBryan Schumaker 	int count;
456165178db4SBryan Schumaker 
456265178db4SBryan Schumaker 	nfs4_lock_state();
456316bfdaafSJ. Bruce Fields 	count = nfsd_release_n_owners(num, true, release_openowner_sop);
456465178db4SBryan Schumaker 	nfs4_unlock_state();
456565178db4SBryan Schumaker 
456665178db4SBryan Schumaker 	printk(KERN_INFO "NFSD: Forgot %d open owners", count);
456765178db4SBryan Schumaker }
456865178db4SBryan Schumaker 
456965178db4SBryan Schumaker int nfsd_process_n_delegations(u64 num, void (*deleg_func)(struct nfs4_delegation *))
457065178db4SBryan Schumaker {
457165178db4SBryan Schumaker 	int i, count = 0;
45722d3475c0SBryan Schumaker 	struct nfs4_file *fp, *fnext;
45732d3475c0SBryan Schumaker 	struct nfs4_delegation *dp, *dnext;
457465178db4SBryan Schumaker 
457565178db4SBryan Schumaker 	for (i = 0; i < FILE_HASH_SIZE; i++) {
45762d3475c0SBryan Schumaker 		list_for_each_entry_safe(fp, fnext, &file_hashtbl[i], fi_hash) {
45772d3475c0SBryan Schumaker 			list_for_each_entry_safe(dp, dnext, &fp->fi_delegations, dl_perfile) {
457865178db4SBryan Schumaker 				deleg_func(dp);
457965178db4SBryan Schumaker 				if (++count == num)
458065178db4SBryan Schumaker 					return count;
458165178db4SBryan Schumaker 			}
458265178db4SBryan Schumaker 		}
458365178db4SBryan Schumaker 	}
45842d3475c0SBryan Schumaker 
458565178db4SBryan Schumaker 	return count;
458665178db4SBryan Schumaker }
458765178db4SBryan Schumaker 
458865178db4SBryan Schumaker void nfsd_forget_delegations(u64 num)
458965178db4SBryan Schumaker {
459065178db4SBryan Schumaker 	unsigned int count;
459165178db4SBryan Schumaker 
459265178db4SBryan Schumaker 	nfs4_lock_state();
459365178db4SBryan Schumaker 	count = nfsd_process_n_delegations(num, unhash_delegation);
459465178db4SBryan Schumaker 	nfs4_unlock_state();
459565178db4SBryan Schumaker 
459665178db4SBryan Schumaker 	printk(KERN_INFO "NFSD: Forgot %d delegations", count);
459765178db4SBryan Schumaker }
459865178db4SBryan Schumaker 
459965178db4SBryan Schumaker void nfsd_recall_delegations(u64 num)
460065178db4SBryan Schumaker {
460165178db4SBryan Schumaker 	unsigned int count;
460265178db4SBryan Schumaker 
460365178db4SBryan Schumaker 	nfs4_lock_state();
460465178db4SBryan Schumaker 	spin_lock(&recall_lock);
460565178db4SBryan Schumaker 	count = nfsd_process_n_delegations(num, nfsd_break_one_deleg);
460665178db4SBryan Schumaker 	spin_unlock(&recall_lock);
460765178db4SBryan Schumaker 	nfs4_unlock_state();
460865178db4SBryan Schumaker 
460965178db4SBryan Schumaker 	printk(KERN_INFO "NFSD: Recalled %d delegations", count);
461065178db4SBryan Schumaker }
461165178db4SBryan Schumaker 
461265178db4SBryan Schumaker #endif /* CONFIG_NFSD_FAULT_INJECTION */
461365178db4SBryan Schumaker 
4614ac4d8ff2SNeilBrown /* initialization to perform at module load time: */
46151da177e4SLinus Torvalds 
461672083396SBryan Schumaker void
4617ac4d8ff2SNeilBrown nfs4_state_init(void)
46181da177e4SLinus Torvalds {
461972083396SBryan Schumaker 	int i;
46201da177e4SLinus Torvalds 
46211da177e4SLinus Torvalds 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
46221da177e4SLinus Torvalds 		INIT_LIST_HEAD(&conf_id_hashtbl[i]);
46231da177e4SLinus Torvalds 		INIT_LIST_HEAD(&conf_str_hashtbl[i]);
46241da177e4SLinus Torvalds 		INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
46251da177e4SLinus Torvalds 		INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
462602cb2858SWang Chen 		INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
46271da177e4SLinus Torvalds 	}
46285282fd72SMarc Eshel 	for (i = 0; i < SESSION_HASH_SIZE; i++)
46295282fd72SMarc Eshel 		INIT_LIST_HEAD(&sessionid_hashtbl[i]);
46301da177e4SLinus Torvalds 	for (i = 0; i < FILE_HASH_SIZE; i++) {
46311da177e4SLinus Torvalds 		INIT_LIST_HEAD(&file_hashtbl[i]);
46321da177e4SLinus Torvalds 	}
463316bfdaafSJ. Bruce Fields 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
463416bfdaafSJ. Bruce Fields 		INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
46351da177e4SLinus Torvalds 	}
4636009673b4SJ. Bruce Fields 	for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
4637009673b4SJ. Bruce Fields 		INIT_LIST_HEAD(&lockowner_ino_hashtbl[i]);
46381da177e4SLinus Torvalds 	INIT_LIST_HEAD(&close_lru);
46391da177e4SLinus Torvalds 	INIT_LIST_HEAD(&client_lru);
46401da177e4SLinus Torvalds 	INIT_LIST_HEAD(&del_recall_lru);
4641ac4d8ff2SNeilBrown 	reclaim_str_hashtbl_size = 0;
4642ac4d8ff2SNeilBrown }
4643ac4d8ff2SNeilBrown 
4644190e4fbfSNeilBrown static void
4645190e4fbfSNeilBrown nfsd4_load_reboot_recovery_data(void)
4646190e4fbfSNeilBrown {
4647190e4fbfSNeilBrown 	int status;
4648190e4fbfSNeilBrown 
46490964a3d3SNeilBrown 	nfs4_lock_state();
465048483bf2SJ. Bruce Fields 	nfsd4_init_recdir();
4651190e4fbfSNeilBrown 	status = nfsd4_recdir_load();
46520964a3d3SNeilBrown 	nfs4_unlock_state();
4653190e4fbfSNeilBrown 	if (status)
4654190e4fbfSNeilBrown 		printk("NFSD: Failure reading reboot recovery data\n");
4655190e4fbfSNeilBrown }
4656190e4fbfSNeilBrown 
4657c2f1a551SMeelap Shah /*
4658c2f1a551SMeelap Shah  * Since the lifetime of a delegation isn't limited to that of an open, a
4659c2f1a551SMeelap Shah  * client may quite reasonably hang on to a delegation as long as it has
4660c2f1a551SMeelap Shah  * the inode cached.  This becomes an obvious problem the first time a
4661c2f1a551SMeelap Shah  * client's inode cache approaches the size of the server's total memory.
4662c2f1a551SMeelap Shah  *
4663c2f1a551SMeelap Shah  * For now we avoid this problem by imposing a hard limit on the number
4664c2f1a551SMeelap Shah  * of delegations, which varies according to the server's memory size.
4665c2f1a551SMeelap Shah  */
4666c2f1a551SMeelap Shah static void
4667c2f1a551SMeelap Shah set_max_delegations(void)
4668c2f1a551SMeelap Shah {
4669c2f1a551SMeelap Shah 	/*
4670c2f1a551SMeelap Shah 	 * Allow at most 4 delegations per megabyte of RAM.  Quick
4671c2f1a551SMeelap Shah 	 * estimates suggest that in the worst case (where every delegation
4672c2f1a551SMeelap Shah 	 * is for a different inode), a delegation could take about 1.5K,
4673c2f1a551SMeelap Shah 	 * giving a worst case usage of about 6% of memory.
4674c2f1a551SMeelap Shah 	 */
4675c2f1a551SMeelap Shah 	max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4676c2f1a551SMeelap Shah }
4677c2f1a551SMeelap Shah 
4678ac4d8ff2SNeilBrown /* initialization to perform when the nfsd service is started: */
4679ac4d8ff2SNeilBrown 
468029ab23ccSJ. Bruce Fields static int
4681ac4d8ff2SNeilBrown __nfs4_state_start(void)
4682ac4d8ff2SNeilBrown {
4683b5a1a81eSJ. Bruce Fields 	int ret;
4684b5a1a81eSJ. Bruce Fields 
46851da177e4SLinus Torvalds 	boot_time = get_seconds();
4686af558e33SJ. Bruce Fields 	locks_start_grace(&nfsd4_manager);
46879a8db97eSMarc Eshel 	printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4688e46b498cSJ. Bruce Fields 	       nfsd4_grace);
4689b5a1a81eSJ. Bruce Fields 	ret = set_callback_cred();
4690b5a1a81eSJ. Bruce Fields 	if (ret)
4691b5a1a81eSJ. Bruce Fields 		return -ENOMEM;
469258da282bSNeilBrown 	laundry_wq = create_singlethread_workqueue("nfsd4");
469329ab23ccSJ. Bruce Fields 	if (laundry_wq == NULL)
469429ab23ccSJ. Bruce Fields 		return -ENOMEM;
4695b5a1a81eSJ. Bruce Fields 	ret = nfsd4_create_callback_queue();
4696b5a1a81eSJ. Bruce Fields 	if (ret)
4697b5a1a81eSJ. Bruce Fields 		goto out_free_laundry;
4698e46b498cSJ. Bruce Fields 	queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4699c2f1a551SMeelap Shah 	set_max_delegations();
4700b5a1a81eSJ. Bruce Fields 	return 0;
4701b5a1a81eSJ. Bruce Fields out_free_laundry:
4702b5a1a81eSJ. Bruce Fields 	destroy_workqueue(laundry_wq);
4703b5a1a81eSJ. Bruce Fields 	return ret;
47041da177e4SLinus Torvalds }
47051da177e4SLinus Torvalds 
470629ab23ccSJ. Bruce Fields int
470776a3550eSNeilBrown nfs4_state_start(void)
47081da177e4SLinus Torvalds {
4709190e4fbfSNeilBrown 	nfsd4_load_reboot_recovery_data();
47104ad9a344SJeff Layton 	return __nfs4_state_start();
47111da177e4SLinus Torvalds }
47121da177e4SLinus Torvalds 
47131da177e4SLinus Torvalds static void
47141da177e4SLinus Torvalds __nfs4_state_shutdown(void)
47151da177e4SLinus Torvalds {
47161da177e4SLinus Torvalds 	int i;
47171da177e4SLinus Torvalds 	struct nfs4_client *clp = NULL;
47181da177e4SLinus Torvalds 	struct nfs4_delegation *dp = NULL;
47191da177e4SLinus Torvalds 	struct list_head *pos, *next, reaplist;
47201da177e4SLinus Torvalds 
47211da177e4SLinus Torvalds 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
47221da177e4SLinus Torvalds 		while (!list_empty(&conf_id_hashtbl[i])) {
47231da177e4SLinus Torvalds 			clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
47241da177e4SLinus Torvalds 			expire_client(clp);
47251da177e4SLinus Torvalds 		}
47261da177e4SLinus Torvalds 		while (!list_empty(&unconf_str_hashtbl[i])) {
47271da177e4SLinus Torvalds 			clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
47281da177e4SLinus Torvalds 			expire_client(clp);
47291da177e4SLinus Torvalds 		}
47301da177e4SLinus Torvalds 	}
47311da177e4SLinus Torvalds 	INIT_LIST_HEAD(&reaplist);
47321da177e4SLinus Torvalds 	spin_lock(&recall_lock);
47331da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &del_recall_lru) {
47341da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
47351da177e4SLinus Torvalds 		list_move(&dp->dl_recall_lru, &reaplist);
47361da177e4SLinus Torvalds 	}
47371da177e4SLinus Torvalds 	spin_unlock(&recall_lock);
47381da177e4SLinus Torvalds 	list_for_each_safe(pos, next, &reaplist) {
47391da177e4SLinus Torvalds 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
47401da177e4SLinus Torvalds 		unhash_delegation(dp);
47411da177e4SLinus Torvalds 	}
47421da177e4SLinus Torvalds 
4743190e4fbfSNeilBrown 	nfsd4_shutdown_recdir();
47441da177e4SLinus Torvalds }
47451da177e4SLinus Torvalds 
47461da177e4SLinus Torvalds void
47471da177e4SLinus Torvalds nfs4_state_shutdown(void)
47481da177e4SLinus Torvalds {
4749afe2c511STejun Heo 	cancel_delayed_work_sync(&laundromat_work);
47505e8d5c29SNeilBrown 	destroy_workqueue(laundry_wq);
47512c5e7615SJ. Bruce Fields 	locks_end_grace(&nfsd4_manager);
47521da177e4SLinus Torvalds 	nfs4_lock_state();
47531da177e4SLinus Torvalds 	nfs4_release_reclaim();
47541da177e4SLinus Torvalds 	__nfs4_state_shutdown();
47551da177e4SLinus Torvalds 	nfs4_unlock_state();
4756c3935e30SJ. Bruce Fields 	nfsd4_destroy_callback_queue();
47571da177e4SLinus Torvalds }
47588b70484cSTigran Mkrtchyan 
47598b70484cSTigran Mkrtchyan static void
47608b70484cSTigran Mkrtchyan get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
47618b70484cSTigran Mkrtchyan {
476237c593c5STigran Mkrtchyan 	if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
476337c593c5STigran Mkrtchyan 		memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
47648b70484cSTigran Mkrtchyan }
47658b70484cSTigran Mkrtchyan 
47668b70484cSTigran Mkrtchyan static void
47678b70484cSTigran Mkrtchyan put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
47688b70484cSTigran Mkrtchyan {
476937c593c5STigran Mkrtchyan 	if (cstate->minorversion) {
477037c593c5STigran Mkrtchyan 		memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
477137c593c5STigran Mkrtchyan 		SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
477237c593c5STigran Mkrtchyan 	}
477337c593c5STigran Mkrtchyan }
477437c593c5STigran Mkrtchyan 
477537c593c5STigran Mkrtchyan void
477637c593c5STigran Mkrtchyan clear_current_stateid(struct nfsd4_compound_state *cstate)
477737c593c5STigran Mkrtchyan {
477837c593c5STigran Mkrtchyan 	CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
47798b70484cSTigran Mkrtchyan }
47808b70484cSTigran Mkrtchyan 
478162cd4a59STigran Mkrtchyan /*
478262cd4a59STigran Mkrtchyan  * functions to set current state id
478362cd4a59STigran Mkrtchyan  */
47848b70484cSTigran Mkrtchyan void
47859428fe1aSTigran Mkrtchyan nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
47869428fe1aSTigran Mkrtchyan {
47879428fe1aSTigran Mkrtchyan 	put_stateid(cstate, &odp->od_stateid);
47889428fe1aSTigran Mkrtchyan }
47899428fe1aSTigran Mkrtchyan 
47909428fe1aSTigran Mkrtchyan void
47918b70484cSTigran Mkrtchyan nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
47928b70484cSTigran Mkrtchyan {
47938b70484cSTigran Mkrtchyan 	put_stateid(cstate, &open->op_stateid);
47948b70484cSTigran Mkrtchyan }
47958b70484cSTigran Mkrtchyan 
47968b70484cSTigran Mkrtchyan void
479762cd4a59STigran Mkrtchyan nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
479862cd4a59STigran Mkrtchyan {
479962cd4a59STigran Mkrtchyan 	put_stateid(cstate, &close->cl_stateid);
480062cd4a59STigran Mkrtchyan }
480162cd4a59STigran Mkrtchyan 
480262cd4a59STigran Mkrtchyan void
480362cd4a59STigran Mkrtchyan nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
480462cd4a59STigran Mkrtchyan {
480562cd4a59STigran Mkrtchyan 	put_stateid(cstate, &lock->lk_resp_stateid);
480662cd4a59STigran Mkrtchyan }
480762cd4a59STigran Mkrtchyan 
480862cd4a59STigran Mkrtchyan /*
480962cd4a59STigran Mkrtchyan  * functions to consume current state id
481062cd4a59STigran Mkrtchyan  */
48111e97b519STigran Mkrtchyan 
48121e97b519STigran Mkrtchyan void
48139428fe1aSTigran Mkrtchyan nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
48149428fe1aSTigran Mkrtchyan {
48159428fe1aSTigran Mkrtchyan 	get_stateid(cstate, &odp->od_stateid);
48169428fe1aSTigran Mkrtchyan }
48179428fe1aSTigran Mkrtchyan 
48189428fe1aSTigran Mkrtchyan void
48199428fe1aSTigran Mkrtchyan nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
48209428fe1aSTigran Mkrtchyan {
48219428fe1aSTigran Mkrtchyan 	get_stateid(cstate, &drp->dr_stateid);
48229428fe1aSTigran Mkrtchyan }
48239428fe1aSTigran Mkrtchyan 
48249428fe1aSTigran Mkrtchyan void
48251e97b519STigran Mkrtchyan nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
48261e97b519STigran Mkrtchyan {
48271e97b519STigran Mkrtchyan 	get_stateid(cstate, &fsp->fr_stateid);
48281e97b519STigran Mkrtchyan }
48291e97b519STigran Mkrtchyan 
48301e97b519STigran Mkrtchyan void
48311e97b519STigran Mkrtchyan nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
48321e97b519STigran Mkrtchyan {
48331e97b519STigran Mkrtchyan 	get_stateid(cstate, &setattr->sa_stateid);
48341e97b519STigran Mkrtchyan }
48351e97b519STigran Mkrtchyan 
483662cd4a59STigran Mkrtchyan void
48378b70484cSTigran Mkrtchyan nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
48388b70484cSTigran Mkrtchyan {
48398b70484cSTigran Mkrtchyan 	get_stateid(cstate, &close->cl_stateid);
48408b70484cSTigran Mkrtchyan }
48418b70484cSTigran Mkrtchyan 
48428b70484cSTigran Mkrtchyan void
484362cd4a59STigran Mkrtchyan nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
48448b70484cSTigran Mkrtchyan {
484562cd4a59STigran Mkrtchyan 	get_stateid(cstate, &locku->lu_stateid);
48468b70484cSTigran Mkrtchyan }
484730813e27STigran Mkrtchyan 
484830813e27STigran Mkrtchyan void
484930813e27STigran Mkrtchyan nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
485030813e27STigran Mkrtchyan {
485130813e27STigran Mkrtchyan 	get_stateid(cstate, &read->rd_stateid);
485230813e27STigran Mkrtchyan }
485330813e27STigran Mkrtchyan 
485430813e27STigran Mkrtchyan void
485530813e27STigran Mkrtchyan nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
485630813e27STigran Mkrtchyan {
485730813e27STigran Mkrtchyan 	get_stateid(cstate, &write->wr_stateid);
485830813e27STigran Mkrtchyan }
4859