xref: /openbmc/linux/fs/nfsd/nfs4state.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2 *  linux/fs/nfsd/nfs4state.c
3 *
4 *  Copyright (c) 2001 The Regents of the University of Michigan.
5 *  All rights reserved.
6 *
7 *  Kendrick Smith <kmsmith@umich.edu>
8 *  Andy Adamson <kandros@umich.edu>
9 *
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions
12 *  are met:
13 *
14 *  1. Redistributions of source code must retain the above copyright
15 *     notice, this list of conditions and the following disclaimer.
16 *  2. Redistributions in binary form must reproduce the above copyright
17 *     notice, this list of conditions and the following disclaimer in the
18 *     documentation and/or other materials provided with the distribution.
19 *  3. Neither the name of the University nor the names of its
20 *     contributors may be used to endorse or promote products derived
21 *     from this software without specific prior written permission.
22 *
23 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36 
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
40 
41 #include <linux/sunrpc/svc.h>
42 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfsd/cache.h>
44 #include <linux/mount.h>
45 #include <linux/workqueue.h>
46 #include <linux/smp_lock.h>
47 #include <linux/kthread.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfsd/state.h>
50 #include <linux/nfsd/xdr4.h>
51 
52 #define NFSDDBG_FACILITY                NFSDDBG_PROC
53 
54 /* Globals */
55 static time_t lease_time = 90;     /* default lease time */
56 static time_t old_lease_time = 90; /* past incarnation lease time */
57 static u32 nfs4_reclaim_init = 0;
58 time_t boot_time;
59 static time_t grace_end = 0;
60 static u32 current_clientid = 1;
61 static u32 current_ownerid = 1;
62 static u32 current_fileid = 1;
63 static u32 current_delegid = 1;
64 static u32 nfs4_init;
65 stateid_t zerostateid;             /* bits all 0 */
66 stateid_t onestateid;              /* bits all 1 */
67 
68 /* debug counters */
69 u32 list_add_perfile = 0;
70 u32 list_del_perfile = 0;
71 u32 add_perclient = 0;
72 u32 del_perclient = 0;
73 u32 alloc_file = 0;
74 u32 free_file = 0;
75 u32 vfsopen = 0;
76 u32 vfsclose = 0;
77 u32 alloc_delegation= 0;
78 u32 free_delegation= 0;
79 
80 /* forward declarations */
81 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
82 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
83 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
84 
85 /* Locking:
86  *
87  * client_sema:
88  * 	protects clientid_hashtbl[], clientstr_hashtbl[],
89  * 	unconfstr_hashtbl[], uncofid_hashtbl[].
90  */
91 static DECLARE_MUTEX(client_sema);
92 
93 void
94 nfs4_lock_state(void)
95 {
96 	down(&client_sema);
97 }
98 
99 void
100 nfs4_unlock_state(void)
101 {
102 	up(&client_sema);
103 }
104 
105 static inline u32
106 opaque_hashval(const void *ptr, int nbytes)
107 {
108 	unsigned char *cptr = (unsigned char *) ptr;
109 
110 	u32 x = 0;
111 	while (nbytes--) {
112 		x *= 37;
113 		x += *cptr++;
114 	}
115 	return x;
116 }
117 
118 /* forward declarations */
119 static void release_stateowner(struct nfs4_stateowner *sop);
120 static void release_stateid(struct nfs4_stateid *stp, int flags);
121 static void release_file(struct nfs4_file *fp);
122 
123 /*
124  * Delegation state
125  */
126 
127 /* recall_lock protects the del_recall_lru */
128 spinlock_t recall_lock;
129 static struct list_head del_recall_lru;
130 
131 static struct nfs4_delegation *
132 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
133 {
134 	struct nfs4_delegation *dp;
135 	struct nfs4_file *fp = stp->st_file;
136 	struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
137 
138 	dprintk("NFSD alloc_init_deleg\n");
139 	if ((dp = kmalloc(sizeof(struct nfs4_delegation),
140 		GFP_KERNEL)) == NULL)
141 		return dp;
142 	INIT_LIST_HEAD(&dp->dl_del_perfile);
143 	INIT_LIST_HEAD(&dp->dl_del_perclnt);
144 	INIT_LIST_HEAD(&dp->dl_recall_lru);
145 	dp->dl_client = clp;
146 	dp->dl_file = fp;
147 	dp->dl_flock = NULL;
148 	get_file(stp->st_vfs_file);
149 	dp->dl_vfs_file = stp->st_vfs_file;
150 	dp->dl_type = type;
151 	dp->dl_recall.cbr_dp = NULL;
152 	dp->dl_recall.cbr_ident = cb->cb_ident;
153 	dp->dl_recall.cbr_trunc = 0;
154 	dp->dl_stateid.si_boot = boot_time;
155 	dp->dl_stateid.si_stateownerid = current_delegid++;
156 	dp->dl_stateid.si_fileid = 0;
157 	dp->dl_stateid.si_generation = 0;
158 	dp->dl_fhlen = current_fh->fh_handle.fh_size;
159 	memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
160 		        current_fh->fh_handle.fh_size);
161 	dp->dl_time = 0;
162 	atomic_set(&dp->dl_count, 1);
163 	list_add(&dp->dl_del_perfile, &fp->fi_del_perfile);
164 	list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
165 	alloc_delegation++;
166 	return dp;
167 }
168 
169 void
170 nfs4_put_delegation(struct nfs4_delegation *dp)
171 {
172 	if (atomic_dec_and_test(&dp->dl_count)) {
173 		dprintk("NFSD: freeing dp %p\n",dp);
174 		kfree(dp);
175 		free_delegation++;
176 	}
177 }
178 
179 /* Remove the associated file_lock first, then remove the delegation.
180  * lease_modify() is called to remove the FS_LEASE file_lock from
181  * the i_flock list, eventually calling nfsd's lock_manager
182  * fl_release_callback.
183  */
184 static void
185 nfs4_close_delegation(struct nfs4_delegation *dp)
186 {
187 	struct file *filp = dp->dl_vfs_file;
188 
189 	dprintk("NFSD: close_delegation dp %p\n",dp);
190 	dp->dl_vfs_file = NULL;
191 	/* The following nfsd_close may not actually close the file,
192 	 * but we want to remove the lease in any case. */
193 	setlease(filp, F_UNLCK, &dp->dl_flock);
194 	nfsd_close(filp);
195 	vfsclose++;
196 }
197 
198 /* Called under the state lock. */
199 static void
200 unhash_delegation(struct nfs4_delegation *dp)
201 {
202 	list_del_init(&dp->dl_del_perfile);
203 	list_del_init(&dp->dl_del_perclnt);
204 	spin_lock(&recall_lock);
205 	list_del_init(&dp->dl_recall_lru);
206 	spin_unlock(&recall_lock);
207 	nfs4_close_delegation(dp);
208 	nfs4_put_delegation(dp);
209 }
210 
211 /*
212  * SETCLIENTID state
213  */
214 
215 /* Hash tables for nfs4_clientid state */
216 #define CLIENT_HASH_BITS                 4
217 #define CLIENT_HASH_SIZE                (1 << CLIENT_HASH_BITS)
218 #define CLIENT_HASH_MASK                (CLIENT_HASH_SIZE - 1)
219 
220 #define clientid_hashval(id) \
221 	((id) & CLIENT_HASH_MASK)
222 #define clientstr_hashval(name, namelen) \
223 	(opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
224 /*
225  * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
226  * used in reboot/reset lease grace period processing
227  *
228  * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
229  * setclientid_confirmed info.
230  *
231  * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
232  * setclientid info.
233  *
234  * client_lru holds client queue ordered by nfs4_client.cl_time
235  * for lease renewal.
236  *
237  * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
238  * for last close replay.
239  */
240 static struct list_head	reclaim_str_hashtbl[CLIENT_HASH_SIZE];
241 static int reclaim_str_hashtbl_size = 0;
242 static struct list_head	conf_id_hashtbl[CLIENT_HASH_SIZE];
243 static struct list_head	conf_str_hashtbl[CLIENT_HASH_SIZE];
244 static struct list_head	unconf_str_hashtbl[CLIENT_HASH_SIZE];
245 static struct list_head	unconf_id_hashtbl[CLIENT_HASH_SIZE];
246 static struct list_head client_lru;
247 static struct list_head close_lru;
248 
249 static inline void
250 renew_client(struct nfs4_client *clp)
251 {
252 	/*
253 	* Move client to the end to the LRU list.
254 	*/
255 	dprintk("renewing client (clientid %08x/%08x)\n",
256 			clp->cl_clientid.cl_boot,
257 			clp->cl_clientid.cl_id);
258 	list_move_tail(&clp->cl_lru, &client_lru);
259 	clp->cl_time = get_seconds();
260 }
261 
262 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
263 static int
264 STALE_CLIENTID(clientid_t *clid)
265 {
266 	if (clid->cl_boot == boot_time)
267 		return 0;
268 	dprintk("NFSD stale clientid (%08x/%08x)\n",
269 			clid->cl_boot, clid->cl_id);
270 	return 1;
271 }
272 
273 /*
274  * XXX Should we use a slab cache ?
275  * This type of memory management is somewhat inefficient, but we use it
276  * anyway since SETCLIENTID is not a common operation.
277  */
278 static inline struct nfs4_client *
279 alloc_client(struct xdr_netobj name)
280 {
281 	struct nfs4_client *clp;
282 
283 	if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
284 		memset(clp, 0, sizeof(*clp));
285 		if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
286 			memcpy(clp->cl_name.data, name.data, name.len);
287 			clp->cl_name.len = name.len;
288 		}
289 		else {
290 			kfree(clp);
291 			clp = NULL;
292 		}
293 	}
294 	return clp;
295 }
296 
297 static inline void
298 free_client(struct nfs4_client *clp)
299 {
300 	if (clp->cl_cred.cr_group_info)
301 		put_group_info(clp->cl_cred.cr_group_info);
302 	kfree(clp->cl_name.data);
303 	kfree(clp);
304 }
305 
306 void
307 put_nfs4_client(struct nfs4_client *clp)
308 {
309 	if (atomic_dec_and_test(&clp->cl_count))
310 		free_client(clp);
311 }
312 
313 static void
314 expire_client(struct nfs4_client *clp)
315 {
316 	struct nfs4_stateowner *sop;
317 	struct nfs4_delegation *dp;
318 	struct nfs4_callback *cb = &clp->cl_callback;
319 	struct rpc_clnt *clnt = clp->cl_callback.cb_client;
320 	struct list_head reaplist;
321 
322 	dprintk("NFSD: expire_client cl_count %d\n",
323 	                    atomic_read(&clp->cl_count));
324 
325 	/* shutdown rpc client, ending any outstanding recall rpcs */
326 	if (atomic_read(&cb->cb_set) == 1 && clnt) {
327 		rpc_shutdown_client(clnt);
328 		clnt = clp->cl_callback.cb_client = NULL;
329 	}
330 
331 	INIT_LIST_HEAD(&reaplist);
332 	spin_lock(&recall_lock);
333 	while (!list_empty(&clp->cl_del_perclnt)) {
334 		dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
335 		dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
336 				dp->dl_flock);
337 		list_del_init(&dp->dl_del_perclnt);
338 		list_move(&dp->dl_recall_lru, &reaplist);
339 	}
340 	spin_unlock(&recall_lock);
341 	while (!list_empty(&reaplist)) {
342 		dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
343 		list_del_init(&dp->dl_recall_lru);
344 		unhash_delegation(dp);
345 	}
346 	list_del(&clp->cl_idhash);
347 	list_del(&clp->cl_strhash);
348 	list_del(&clp->cl_lru);
349 	while (!list_empty(&clp->cl_perclient)) {
350 		sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
351 		release_stateowner(sop);
352 	}
353 	put_nfs4_client(clp);
354 }
355 
356 static struct nfs4_client *
357 create_client(struct xdr_netobj name) {
358 	struct nfs4_client *clp;
359 
360 	if (!(clp = alloc_client(name)))
361 		goto out;
362 	atomic_set(&clp->cl_count, 1);
363 	atomic_set(&clp->cl_callback.cb_set, 0);
364 	clp->cl_callback.cb_parsed = 0;
365 	INIT_LIST_HEAD(&clp->cl_idhash);
366 	INIT_LIST_HEAD(&clp->cl_strhash);
367 	INIT_LIST_HEAD(&clp->cl_perclient);
368 	INIT_LIST_HEAD(&clp->cl_del_perclnt);
369 	INIT_LIST_HEAD(&clp->cl_lru);
370 out:
371 	return clp;
372 }
373 
374 static void
375 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
376 	memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
377 }
378 
379 static void
380 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
381 	target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
382 	target->cl_clientid.cl_id = source->cl_clientid.cl_id;
383 }
384 
385 static void
386 copy_cred(struct svc_cred *target, struct svc_cred *source) {
387 
388 	target->cr_uid = source->cr_uid;
389 	target->cr_gid = source->cr_gid;
390 	target->cr_group_info = source->cr_group_info;
391 	get_group_info(target->cr_group_info);
392 }
393 
394 static int
395 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
396 	if (!n1 || !n2)
397 		return 0;
398 	return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
399 }
400 
401 static int
402 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
403 	return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
404 }
405 
406 static int
407 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
408 	return((cl1->cl_boot == cl2->cl_boot) &&
409 	   	(cl1->cl_id == cl2->cl_id));
410 }
411 
412 /* XXX what about NGROUP */
413 static int
414 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
415 	return(cr1->cr_uid == cr2->cr_uid);
416 
417 }
418 
419 static void
420 gen_clid(struct nfs4_client *clp) {
421 	clp->cl_clientid.cl_boot = boot_time;
422 	clp->cl_clientid.cl_id = current_clientid++;
423 }
424 
425 static void
426 gen_confirm(struct nfs4_client *clp) {
427 	struct timespec 	tv;
428 	u32 *			p;
429 
430 	tv = CURRENT_TIME;
431 	p = (u32 *)clp->cl_confirm.data;
432 	*p++ = tv.tv_sec;
433 	*p++ = tv.tv_nsec;
434 }
435 
436 static int
437 check_name(struct xdr_netobj name) {
438 
439 	if (name.len == 0)
440 		return 0;
441 	if (name.len > NFS4_OPAQUE_LIMIT) {
442 		printk("NFSD: check_name: name too long(%d)!\n", name.len);
443 		return 0;
444 	}
445 	return 1;
446 }
447 
448 void
449 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
450 {
451 	unsigned int idhashval;
452 
453 	list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
454 	idhashval = clientid_hashval(clp->cl_clientid.cl_id);
455 	list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
456 	list_add_tail(&clp->cl_lru, &client_lru);
457 	clp->cl_time = get_seconds();
458 }
459 
460 void
461 move_to_confirmed(struct nfs4_client *clp)
462 {
463 	unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
464 	unsigned int strhashval;
465 
466 	dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
467 	list_del_init(&clp->cl_strhash);
468 	list_del_init(&clp->cl_idhash);
469 	list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
470 	strhashval = clientstr_hashval(clp->cl_name.data,
471 			clp->cl_name.len);
472 	list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
473 	renew_client(clp);
474 }
475 
476 static struct nfs4_client *
477 find_confirmed_client(clientid_t *clid)
478 {
479 	struct nfs4_client *clp;
480 	unsigned int idhashval = clientid_hashval(clid->cl_id);
481 
482 	list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
483 		if (cmp_clid(&clp->cl_clientid, clid))
484 			return clp;
485 	}
486 	return NULL;
487 }
488 
489 static struct nfs4_client *
490 find_unconfirmed_client(clientid_t *clid)
491 {
492 	struct nfs4_client *clp;
493 	unsigned int idhashval = clientid_hashval(clid->cl_id);
494 
495 	list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
496 		if (cmp_clid(&clp->cl_clientid, clid))
497 			return clp;
498 	}
499 	return NULL;
500 }
501 
502 /* a helper function for parse_callback */
503 static int
504 parse_octet(unsigned int *lenp, char **addrp)
505 {
506 	unsigned int len = *lenp;
507 	char *p = *addrp;
508 	int n = -1;
509 	char c;
510 
511 	for (;;) {
512 		if (!len)
513 			break;
514 		len--;
515 		c = *p++;
516 		if (c == '.')
517 			break;
518 		if ((c < '0') || (c > '9')) {
519 			n = -1;
520 			break;
521 		}
522 		if (n < 0)
523 			n = 0;
524 		n = (n * 10) + (c - '0');
525 		if (n > 255) {
526 			n = -1;
527 			break;
528 		}
529 	}
530 	*lenp = len;
531 	*addrp = p;
532 	return n;
533 }
534 
535 /* parse and set the setclientid ipv4 callback address */
536 int
537 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
538 {
539 	int temp = 0;
540 	u32 cbaddr = 0;
541 	u16 cbport = 0;
542 	u32 addrlen = addr_len;
543 	char *addr = addr_val;
544 	int i, shift;
545 
546 	/* ipaddress */
547 	shift = 24;
548 	for(i = 4; i > 0  ; i--) {
549 		if ((temp = parse_octet(&addrlen, &addr)) < 0) {
550 			return 0;
551 		}
552 		cbaddr |= (temp << shift);
553 		if (shift > 0)
554 		shift -= 8;
555 	}
556 	*cbaddrp = cbaddr;
557 
558 	/* port */
559 	shift = 8;
560 	for(i = 2; i > 0  ; i--) {
561 		if ((temp = parse_octet(&addrlen, &addr)) < 0) {
562 			return 0;
563 		}
564 		cbport |= (temp << shift);
565 		if (shift > 0)
566 			shift -= 8;
567 	}
568 	*cbportp = cbport;
569 	return 1;
570 }
571 
572 void
573 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
574 {
575 	struct nfs4_callback *cb = &clp->cl_callback;
576 
577 	/* Currently, we only support tcp for the callback channel */
578 	if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
579 		goto out_err;
580 
581 	if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
582 	                 &cb->cb_addr, &cb->cb_port)))
583 		goto out_err;
584 	cb->cb_prog = se->se_callback_prog;
585 	cb->cb_ident = se->se_callback_ident;
586 	cb->cb_parsed = 1;
587 	return;
588 out_err:
589 	printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
590 		"will not receive delegations\n",
591 		clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
592 
593 	cb->cb_parsed = 0;
594 	return;
595 }
596 
597 /*
598  * RFC 3010 has a complex implmentation description of processing a
599  * SETCLIENTID request consisting of 5 bullets, labeled as
600  * CASE0 - CASE4 below.
601  *
602  * NOTES:
603  * 	callback information will be processed in a future patch
604  *
605  *	an unconfirmed record is added when:
606  *      NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
607  *	CASE 1: confirmed record found with matching name, principal,
608  *		verifier, and clientid.
609  *	CASE 2: confirmed record found with matching name, principal,
610  *		and there is no unconfirmed record with matching
611  *		name and principal
612  *
613  *      an unconfirmed record is replaced when:
614  *	CASE 3: confirmed record found with matching name, principal,
615  *		and an unconfirmed record is found with matching
616  *		name, principal, and with clientid and
617  *		confirm that does not match the confirmed record.
618  *	CASE 4: there is no confirmed record with matching name and
619  *		principal. there is an unconfirmed record with
620  *		matching name, principal.
621  *
622  *	an unconfirmed record is deleted when:
623  *	CASE 1: an unconfirmed record that matches input name, verifier,
624  *		and confirmed clientid.
625  *	CASE 4: any unconfirmed records with matching name and principal
626  *		that exist after an unconfirmed record has been replaced
627  *		as described above.
628  *
629  */
630 int
631 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
632 {
633 	u32 			ip_addr = rqstp->rq_addr.sin_addr.s_addr;
634 	struct xdr_netobj 	clname = {
635 		.len = setclid->se_namelen,
636 		.data = setclid->se_name,
637 	};
638 	nfs4_verifier		clverifier = setclid->se_verf;
639 	unsigned int 		strhashval;
640 	struct nfs4_client *	conf, * unconf, * new, * clp;
641 	int 			status;
642 
643 	status = nfserr_inval;
644 	if (!check_name(clname))
645 		goto out;
646 
647 	/*
648 	 * XXX The Duplicate Request Cache (DRC) has been checked (??)
649 	 * We get here on a DRC miss.
650 	 */
651 
652 	strhashval = clientstr_hashval(clname.data, clname.len);
653 
654 	conf = NULL;
655 	nfs4_lock_state();
656 	list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
657 		if (!cmp_name(&clp->cl_name, &clname))
658 			continue;
659 		/*
660 		 * CASE 0:
661 		 * clname match, confirmed, different principal
662 		 * or different ip_address
663 		 */
664 		status = nfserr_clid_inuse;
665 		if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
666 			printk("NFSD: setclientid: string in use by client"
667 			"(clientid %08x/%08x)\n",
668 			clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
669 			goto out;
670 		}
671 		if (clp->cl_addr != ip_addr) {
672 			printk("NFSD: setclientid: string in use by client"
673 			"(clientid %08x/%08x)\n",
674 			clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
675 			goto out;
676 		}
677 
678 		/*
679 	 	 * cl_name match from a previous SETCLIENTID operation
680 	 	 * XXX check for additional matches?
681 		 */
682 		conf = clp;
683 		break;
684 	}
685 	unconf = NULL;
686 	list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
687 		if (!cmp_name(&clp->cl_name, &clname))
688 			continue;
689 		/* cl_name match from a previous SETCLIENTID operation */
690 		unconf = clp;
691 		break;
692 	}
693 	status = nfserr_resource;
694 	if (!conf) {
695 		/*
696 		 * CASE 4:
697 		 * placed first, because it is the normal case.
698 		 */
699 		if (unconf)
700 			expire_client(unconf);
701 		if (!(new = create_client(clname)))
702 			goto out;
703 		copy_verf(new, &clverifier);
704 		new->cl_addr = ip_addr;
705 		copy_cred(&new->cl_cred,&rqstp->rq_cred);
706 		gen_clid(new);
707 		gen_confirm(new);
708 		gen_callback(new, setclid);
709 		add_to_unconfirmed(new, strhashval);
710 	} else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
711 		/*
712 		 * CASE 1:
713 		 * cl_name match, confirmed, principal match
714 		 * verifier match: probable callback update
715 		 *
716 		 * remove any unconfirmed nfs4_client with
717 		 * matching cl_name, cl_verifier, and cl_clientid
718 		 *
719 		 * create and insert an unconfirmed nfs4_client with same
720 		 * cl_name, cl_verifier, and cl_clientid as existing
721 		 * nfs4_client,  but with the new callback info and a
722 		 * new cl_confirm
723 		 */
724 		if ((unconf) &&
725 		    cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
726 		     cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
727 				expire_client(unconf);
728 		}
729 		if (!(new = create_client(clname)))
730 			goto out;
731 		copy_verf(new,&conf->cl_verifier);
732 		new->cl_addr = ip_addr;
733 		copy_cred(&new->cl_cred,&rqstp->rq_cred);
734 		copy_clid(new, conf);
735 		gen_confirm(new);
736 		gen_callback(new, setclid);
737 		add_to_unconfirmed(new,strhashval);
738 	} else if (!unconf) {
739 		/*
740 		 * CASE 2:
741 		 * clname match, confirmed, principal match
742 		 * verfier does not match
743 		 * no unconfirmed. create a new unconfirmed nfs4_client
744 		 * using input clverifier, clname, and callback info
745 		 * and generate a new cl_clientid and cl_confirm.
746 		 */
747 		if (!(new = create_client(clname)))
748 			goto out;
749 		copy_verf(new,&clverifier);
750 		new->cl_addr = ip_addr;
751 		copy_cred(&new->cl_cred,&rqstp->rq_cred);
752 		gen_clid(new);
753 		gen_confirm(new);
754 		gen_callback(new, setclid);
755 		add_to_unconfirmed(new, strhashval);
756 	} else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
757 		/*
758 		 * CASE3:
759 		 * confirmed found (name, principal match)
760 		 * confirmed verifier does not match input clverifier
761 		 *
762 		 * unconfirmed found (name match)
763 		 * confirmed->cl_confirm != unconfirmed->cl_confirm
764 		 *
765 		 * remove unconfirmed.
766 		 *
767 		 * create an unconfirmed nfs4_client
768 		 * with same cl_name as existing confirmed nfs4_client,
769 		 * but with new callback info, new cl_clientid,
770 		 * new cl_verifier and a new cl_confirm
771 		 */
772 		expire_client(unconf);
773 		if (!(new = create_client(clname)))
774 			goto out;
775 		copy_verf(new,&clverifier);
776 		new->cl_addr = ip_addr;
777 		copy_cred(&new->cl_cred,&rqstp->rq_cred);
778 		gen_clid(new);
779 		gen_confirm(new);
780 		gen_callback(new, setclid);
781 		add_to_unconfirmed(new, strhashval);
782 	} else {
783 		/* No cases hit !!! */
784 		status = nfserr_inval;
785 		goto out;
786 
787 	}
788 	setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
789 	setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
790 	memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
791 	status = nfs_ok;
792 out:
793 	nfs4_unlock_state();
794 	return status;
795 }
796 
797 
798 /*
799  * RFC 3010 has a complex implmentation description of processing a
800  * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
801  * processing on a DRC miss, labeled as CASE1 - CASE4 below.
802  *
803  * NOTE: callback information will be processed here in a future patch
804  */
805 int
806 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
807 {
808 	u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
809 	struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
810 	nfs4_verifier confirm = setclientid_confirm->sc_confirm;
811 	clientid_t * clid = &setclientid_confirm->sc_clientid;
812 	int status;
813 
814 	if (STALE_CLIENTID(clid))
815 		return nfserr_stale_clientid;
816 	/*
817 	 * XXX The Duplicate Request Cache (DRC) has been checked (??)
818 	 * We get here on a DRC miss.
819 	 */
820 
821 	nfs4_lock_state();
822 	clp = find_confirmed_client(clid);
823 	if (clp) {
824 		status = nfserr_inval;
825 		/*
826 		 * Found a record for this clientid. If the IP addresses
827 		 * don't match, return ERR_INVAL just as if the record had
828 		 * not been found.
829 		 */
830 		if (clp->cl_addr != ip_addr) {
831 			printk("NFSD: setclientid: string in use by client"
832 			"(clientid %08x/%08x)\n",
833 			clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
834 			goto out;
835 		}
836 		conf = clp;
837 	}
838 	clp = find_unconfirmed_client(clid);
839 	if (clp) {
840 		status = nfserr_inval;
841 		if (clp->cl_addr != ip_addr) {
842 			printk("NFSD: setclientid: string in use by client"
843 			"(clientid %08x/%08x)\n",
844 			clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
845 			goto out;
846 		}
847 		unconf = clp;
848 	}
849 	/* CASE 1:
850 	* unconf record that matches input clientid and input confirm.
851 	* conf record that matches input clientid.
852 	* conf  and unconf records match names, verifiers
853 	*/
854 	if ((conf && unconf) &&
855 	    (cmp_verf(&unconf->cl_confirm, &confirm)) &&
856 	    (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
857 	    (cmp_name(&conf->cl_name,&unconf->cl_name))  &&
858 	    (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
859 		if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
860 			status = nfserr_clid_inuse;
861 		else {
862 			expire_client(conf);
863 			clp = unconf;
864 			move_to_confirmed(unconf);
865 			status = nfs_ok;
866 		}
867 		goto out;
868 	}
869 	/* CASE 2:
870 	 * conf record that matches input clientid.
871 	 * if unconf record that matches input clientid, then unconf->cl_name
872 	 * or unconf->cl_verifier don't match the conf record.
873 	 */
874 	if ((conf && !unconf) ||
875 	    ((conf && unconf) &&
876 	     (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
877 	      !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
878 		if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
879 			status = nfserr_clid_inuse;
880 		} else {
881 			clp = conf;
882 			status = nfs_ok;
883 		}
884 		goto out;
885 	}
886 	/* CASE 3:
887 	 * conf record not found.
888 	 * unconf record found.
889 	 * unconf->cl_confirm matches input confirm
890 	 */
891 	if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
892 		if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
893 			status = nfserr_clid_inuse;
894 		} else {
895 			status = nfs_ok;
896 			clp = unconf;
897 			move_to_confirmed(unconf);
898 		}
899 		goto out;
900 	}
901 	/* CASE 4:
902 	 * conf record not found, or if conf, then conf->cl_confirm does not
903 	 * match input confirm.
904 	 * unconf record not found, or if unconf, then unconf->cl_confirm
905 	 * does not match input confirm.
906 	 */
907 	if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
908 	    (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
909 		status = nfserr_stale_clientid;
910 		goto out;
911 	}
912 	/* check that we have hit one of the cases...*/
913 	status = nfserr_inval;
914 	goto out;
915 out:
916 	if (!status)
917 		nfsd4_probe_callback(clp);
918 	nfs4_unlock_state();
919 	return status;
920 }
921 
922 /*
923  * Open owner state (share locks)
924  */
925 
926 /* hash tables for nfs4_stateowner */
927 #define OWNER_HASH_BITS              8
928 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
929 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
930 
931 #define ownerid_hashval(id) \
932         ((id) & OWNER_HASH_MASK)
933 #define ownerstr_hashval(clientid, ownername) \
934         (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
935 
936 static struct list_head	ownerid_hashtbl[OWNER_HASH_SIZE];
937 static struct list_head	ownerstr_hashtbl[OWNER_HASH_SIZE];
938 
939 /* hash table for nfs4_file */
940 #define FILE_HASH_BITS                   8
941 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
942 #define FILE_HASH_MASK                  (FILE_HASH_SIZE - 1)
943 /* hash table for (open)nfs4_stateid */
944 #define STATEID_HASH_BITS              10
945 #define STATEID_HASH_SIZE              (1 << STATEID_HASH_BITS)
946 #define STATEID_HASH_MASK              (STATEID_HASH_SIZE - 1)
947 
948 #define file_hashval(x) \
949         hash_ptr(x, FILE_HASH_BITS)
950 #define stateid_hashval(owner_id, file_id)  \
951         (((owner_id) + (file_id)) & STATEID_HASH_MASK)
952 
953 static struct list_head file_hashtbl[FILE_HASH_SIZE];
954 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
955 
956 /* OPEN Share state helper functions */
957 static inline struct nfs4_file *
958 alloc_init_file(struct inode *ino)
959 {
960 	struct nfs4_file *fp;
961 	unsigned int hashval = file_hashval(ino);
962 
963 	if ((fp = kmalloc(sizeof(struct nfs4_file),GFP_KERNEL))) {
964 		INIT_LIST_HEAD(&fp->fi_hash);
965 		INIT_LIST_HEAD(&fp->fi_perfile);
966 		INIT_LIST_HEAD(&fp->fi_del_perfile);
967 		list_add(&fp->fi_hash, &file_hashtbl[hashval]);
968 		fp->fi_inode = igrab(ino);
969 		fp->fi_id = current_fileid++;
970 		alloc_file++;
971 		return fp;
972 	}
973 	return NULL;
974 }
975 
976 static void
977 release_all_files(void)
978 {
979 	int i;
980 	struct nfs4_file *fp;
981 
982 	for (i=0;i<FILE_HASH_SIZE;i++) {
983 		while (!list_empty(&file_hashtbl[i])) {
984 			fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
985 			/* this should never be more than once... */
986 			if (!list_empty(&fp->fi_perfile) || !list_empty(&fp->fi_del_perfile)) {
987 				printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
988 			}
989 			release_file(fp);
990 		}
991 	}
992 }
993 
994 kmem_cache_t *stateowner_slab = NULL;
995 
996 static int
997 nfsd4_init_slabs(void)
998 {
999 	stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1000 			sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1001 	if (stateowner_slab == NULL) {
1002 		dprintk("nfsd4: out of memory while initializing nfsv4\n");
1003 		return -ENOMEM;
1004 	}
1005 	return 0;
1006 }
1007 
1008 static void
1009 nfsd4_free_slabs(void)
1010 {
1011 	int status = 0;
1012 
1013 	if (stateowner_slab)
1014 		status = kmem_cache_destroy(stateowner_slab);
1015 	stateowner_slab = NULL;
1016 	BUG_ON(status);
1017 }
1018 
1019 void
1020 nfs4_free_stateowner(struct kref *kref)
1021 {
1022 	struct nfs4_stateowner *sop =
1023 		container_of(kref, struct nfs4_stateowner, so_ref);
1024 	kfree(sop->so_owner.data);
1025 	kmem_cache_free(stateowner_slab, sop);
1026 }
1027 
1028 static inline struct nfs4_stateowner *
1029 alloc_stateowner(struct xdr_netobj *owner)
1030 {
1031 	struct nfs4_stateowner *sop;
1032 
1033 	if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1034 		if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1035 			memcpy(sop->so_owner.data, owner->data, owner->len);
1036 			sop->so_owner.len = owner->len;
1037 			kref_init(&sop->so_ref);
1038 			return sop;
1039 		}
1040 		kmem_cache_free(stateowner_slab, sop);
1041 	}
1042 	return NULL;
1043 }
1044 
1045 static struct nfs4_stateowner *
1046 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1047 	struct nfs4_stateowner *sop;
1048 	struct nfs4_replay *rp;
1049 	unsigned int idhashval;
1050 
1051 	if (!(sop = alloc_stateowner(&open->op_owner)))
1052 		return NULL;
1053 	idhashval = ownerid_hashval(current_ownerid);
1054 	INIT_LIST_HEAD(&sop->so_idhash);
1055 	INIT_LIST_HEAD(&sop->so_strhash);
1056 	INIT_LIST_HEAD(&sop->so_perclient);
1057 	INIT_LIST_HEAD(&sop->so_perfilestate);
1058 	INIT_LIST_HEAD(&sop->so_perlockowner);  /* not used */
1059 	INIT_LIST_HEAD(&sop->so_close_lru);
1060 	sop->so_time = 0;
1061 	list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1062 	list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1063 	list_add(&sop->so_perclient, &clp->cl_perclient);
1064 	add_perclient++;
1065 	sop->so_is_open_owner = 1;
1066 	sop->so_id = current_ownerid++;
1067 	sop->so_client = clp;
1068 	sop->so_seqid = open->op_seqid;
1069 	sop->so_confirmed = 0;
1070 	rp = &sop->so_replay;
1071 	rp->rp_status = NFSERR_SERVERFAULT;
1072 	rp->rp_buflen = 0;
1073 	rp->rp_buf = rp->rp_ibuf;
1074 	return sop;
1075 }
1076 
1077 static void
1078 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1079 {
1080 	struct nfs4_stateowner *lock_sop;
1081 
1082 	while (!list_empty(&open_stp->st_perlockowner)) {
1083 		lock_sop = list_entry(open_stp->st_perlockowner.next,
1084 				struct nfs4_stateowner, so_perlockowner);
1085 		/* list_del(&open_stp->st_perlockowner);  */
1086 		BUG_ON(lock_sop->so_is_open_owner);
1087 		release_stateowner(lock_sop);
1088 	}
1089 }
1090 
1091 static void
1092 unhash_stateowner(struct nfs4_stateowner *sop)
1093 {
1094 	struct nfs4_stateid *stp;
1095 
1096 	list_del(&sop->so_idhash);
1097 	list_del(&sop->so_strhash);
1098 	if (sop->so_is_open_owner) {
1099 		list_del(&sop->so_perclient);
1100 		del_perclient++;
1101 	}
1102 	list_del(&sop->so_perlockowner);
1103 	while (!list_empty(&sop->so_perfilestate)) {
1104 		stp = list_entry(sop->so_perfilestate.next,
1105 			struct nfs4_stateid, st_perfilestate);
1106 		if (sop->so_is_open_owner)
1107 			release_stateid(stp, OPEN_STATE);
1108 		else
1109 			release_stateid(stp, LOCK_STATE);
1110 	}
1111 }
1112 
1113 static void
1114 release_stateowner(struct nfs4_stateowner *sop)
1115 {
1116 	unhash_stateowner(sop);
1117 	list_del(&sop->so_close_lru);
1118 	nfs4_put_stateowner(sop);
1119 }
1120 
1121 static inline void
1122 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1123 	struct nfs4_stateowner *sop = open->op_stateowner;
1124 	unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1125 
1126 	INIT_LIST_HEAD(&stp->st_hash);
1127 	INIT_LIST_HEAD(&stp->st_perfilestate);
1128 	INIT_LIST_HEAD(&stp->st_perlockowner);
1129 	INIT_LIST_HEAD(&stp->st_perfile);
1130 	list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1131 	list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1132 	list_add_perfile++;
1133 	list_add(&stp->st_perfile, &fp->fi_perfile);
1134 	stp->st_stateowner = sop;
1135 	stp->st_file = fp;
1136 	stp->st_stateid.si_boot = boot_time;
1137 	stp->st_stateid.si_stateownerid = sop->so_id;
1138 	stp->st_stateid.si_fileid = fp->fi_id;
1139 	stp->st_stateid.si_generation = 0;
1140 	stp->st_access_bmap = 0;
1141 	stp->st_deny_bmap = 0;
1142 	__set_bit(open->op_share_access, &stp->st_access_bmap);
1143 	__set_bit(open->op_share_deny, &stp->st_deny_bmap);
1144 }
1145 
1146 static void
1147 release_stateid(struct nfs4_stateid *stp, int flags)
1148 {
1149 	struct file *filp = stp->st_vfs_file;
1150 
1151 	list_del(&stp->st_hash);
1152 	list_del_perfile++;
1153 	list_del(&stp->st_perfile);
1154 	list_del(&stp->st_perfilestate);
1155 	if (flags & OPEN_STATE) {
1156 		release_stateid_lockowners(stp);
1157 		stp->st_vfs_file = NULL;
1158 		nfsd_close(filp);
1159 		vfsclose++;
1160 	} else if (flags & LOCK_STATE)
1161 		locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1162 	kfree(stp);
1163 	stp = NULL;
1164 }
1165 
1166 static void
1167 release_file(struct nfs4_file *fp)
1168 {
1169 	free_file++;
1170 	list_del(&fp->fi_hash);
1171 	iput(fp->fi_inode);
1172 	kfree(fp);
1173 }
1174 
1175 void
1176 move_to_close_lru(struct nfs4_stateowner *sop)
1177 {
1178 	dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1179 
1180 	unhash_stateowner(sop);
1181 	list_add_tail(&sop->so_close_lru, &close_lru);
1182 	sop->so_time = get_seconds();
1183 }
1184 
1185 void
1186 release_state_owner(struct nfs4_stateid *stp, int flag)
1187 {
1188 	struct nfs4_stateowner *sop = stp->st_stateowner;
1189 	struct nfs4_file *fp = stp->st_file;
1190 
1191 	dprintk("NFSD: release_state_owner\n");
1192 	release_stateid(stp, flag);
1193 
1194 	/* place unused nfs4_stateowners on so_close_lru list to be
1195 	 * released by the laundromat service after the lease period
1196 	 * to enable us to handle CLOSE replay
1197 	 */
1198 	if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1199 		move_to_close_lru(sop);
1200 	/* unused nfs4_file's are releseed. XXX slab cache? */
1201 	if (list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile)) {
1202 		release_file(fp);
1203 	}
1204 }
1205 
1206 static int
1207 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1208 	return ((sop->so_owner.len == owner->len) &&
1209 	 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1210 	  (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1211 }
1212 
1213 static struct nfs4_stateowner *
1214 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1215 {
1216 	struct nfs4_stateowner *so = NULL;
1217 
1218 	list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1219 		if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1220 			return so;
1221 	}
1222 	return NULL;
1223 }
1224 
1225 /* search file_hashtbl[] for file */
1226 static struct nfs4_file *
1227 find_file(struct inode *ino)
1228 {
1229 	unsigned int hashval = file_hashval(ino);
1230 	struct nfs4_file *fp;
1231 
1232 	list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1233 		if (fp->fi_inode == ino)
1234 			return fp;
1235 	}
1236 	return NULL;
1237 }
1238 
1239 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1240 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1241 
1242 void
1243 set_access(unsigned int *access, unsigned long bmap) {
1244 	int i;
1245 
1246 	*access = 0;
1247 	for (i = 1; i < 4; i++) {
1248 		if (test_bit(i, &bmap))
1249 			*access |= i;
1250 	}
1251 }
1252 
1253 void
1254 set_deny(unsigned int *deny, unsigned long bmap) {
1255 	int i;
1256 
1257 	*deny = 0;
1258 	for (i = 0; i < 4; i++) {
1259 		if (test_bit(i, &bmap))
1260 			*deny |= i ;
1261 	}
1262 }
1263 
1264 static int
1265 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1266 	unsigned int access, deny;
1267 
1268 	set_access(&access, stp->st_access_bmap);
1269 	set_deny(&deny, stp->st_deny_bmap);
1270 	if ((access & open->op_share_deny) || (deny & open->op_share_access))
1271 		return 0;
1272 	return 1;
1273 }
1274 
1275 /*
1276  * Called to check deny when READ with all zero stateid or
1277  * WRITE with all zero or all one stateid
1278  */
1279 int
1280 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1281 {
1282 	struct inode *ino = current_fh->fh_dentry->d_inode;
1283 	struct nfs4_file *fp;
1284 	struct nfs4_stateid *stp;
1285 
1286 	dprintk("NFSD: nfs4_share_conflict\n");
1287 
1288 	fp = find_file(ino);
1289 	if (fp) {
1290 	/* Search for conflicting share reservations */
1291 		list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1292 			if (test_bit(deny_type, &stp->st_deny_bmap) ||
1293 			    test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1294 				return nfserr_share_denied;
1295 		}
1296 	}
1297 	return nfs_ok;
1298 }
1299 
1300 static inline void
1301 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1302 {
1303 	if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1304 		put_write_access(filp->f_dentry->d_inode);
1305 		filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1306 	}
1307 }
1308 
1309 /*
1310  * Recall a delegation
1311  */
1312 static int
1313 do_recall(void *__dp)
1314 {
1315 	struct nfs4_delegation *dp = __dp;
1316 
1317 	daemonize("nfsv4-recall");
1318 
1319 	nfsd4_cb_recall(dp);
1320 	return 0;
1321 }
1322 
1323 /*
1324  * Spawn a thread to perform a recall on the delegation represented
1325  * by the lease (file_lock)
1326  *
1327  * Called from break_lease() with lock_kernel() held.
1328  * Note: we assume break_lease will only call this *once* for any given
1329  * lease.
1330  */
1331 static
1332 void nfsd_break_deleg_cb(struct file_lock *fl)
1333 {
1334 	struct nfs4_delegation *dp=  (struct nfs4_delegation *)fl->fl_owner;
1335 	struct task_struct *t;
1336 
1337 	dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1338 	if (!dp)
1339 		return;
1340 
1341 	/* We're assuming the state code never drops its reference
1342 	 * without first removing the lease.  Since we're in this lease
1343 	 * callback (and since the lease code is serialized by the kernel
1344 	 * lock) we know the server hasn't removed the lease yet, we know
1345 	 * it's safe to take a reference: */
1346 	atomic_inc(&dp->dl_count);
1347 
1348 	spin_lock(&recall_lock);
1349 	list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1350 	spin_unlock(&recall_lock);
1351 
1352 	/* only place dl_time is set. protected by lock_kernel*/
1353 	dp->dl_time = get_seconds();
1354 
1355 	/* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1356 	fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1357 
1358 	t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1359 	if (IS_ERR(t)) {
1360 		struct nfs4_client *clp = dp->dl_client;
1361 
1362 		printk(KERN_INFO "NFSD: Callback thread failed for "
1363 			"for client (clientid %08x/%08x)\n",
1364 			clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1365 		nfs4_put_delegation(dp);
1366 	}
1367 }
1368 
1369 /*
1370  * The file_lock is being reapd.
1371  *
1372  * Called by locks_free_lock() with lock_kernel() held.
1373  */
1374 static
1375 void nfsd_release_deleg_cb(struct file_lock *fl)
1376 {
1377 	struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1378 
1379 	dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1380 
1381 	if (!(fl->fl_flags & FL_LEASE) || !dp)
1382 		return;
1383 	dp->dl_flock = NULL;
1384 }
1385 
1386 /*
1387  * Set the delegation file_lock back pointer.
1388  *
1389  * Called from __setlease() with lock_kernel() held.
1390  */
1391 static
1392 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1393 {
1394 	struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1395 
1396 	dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1397 	if (!dp)
1398 		return;
1399 	dp->dl_flock = new;
1400 }
1401 
1402 /*
1403  * Called from __setlease() with lock_kernel() held
1404  */
1405 static
1406 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1407 {
1408 	struct nfs4_delegation *onlistd =
1409 		(struct nfs4_delegation *)onlist->fl_owner;
1410 	struct nfs4_delegation *tryd =
1411 		(struct nfs4_delegation *)try->fl_owner;
1412 
1413 	if (onlist->fl_lmops != try->fl_lmops)
1414 		return 0;
1415 
1416 	return onlistd->dl_client == tryd->dl_client;
1417 }
1418 
1419 
1420 static
1421 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1422 {
1423 	if (arg & F_UNLCK)
1424 		return lease_modify(onlist, arg);
1425 	else
1426 		return -EAGAIN;
1427 }
1428 
1429 struct lock_manager_operations nfsd_lease_mng_ops = {
1430 	.fl_break = nfsd_break_deleg_cb,
1431 	.fl_release_private = nfsd_release_deleg_cb,
1432 	.fl_copy_lock = nfsd_copy_lock_deleg_cb,
1433 	.fl_mylease = nfsd_same_client_deleg_cb,
1434 	.fl_change = nfsd_change_deleg_cb,
1435 };
1436 
1437 
1438 /*
1439  * nfsd4_process_open1()
1440  * 	lookup stateowner.
1441  * 		found:
1442  * 			check confirmed
1443  * 				confirmed:
1444  * 					check seqid
1445  * 				not confirmed:
1446  * 					delete owner
1447  * 					create new owner
1448  * 		notfound:
1449  * 			verify clientid
1450  * 			create new owner
1451  *
1452  * called with nfs4_lock_state() held.
1453  */
1454 int
1455 nfsd4_process_open1(struct nfsd4_open *open)
1456 {
1457 	int status;
1458 	clientid_t *clientid = &open->op_clientid;
1459 	struct nfs4_client *clp = NULL;
1460 	unsigned int strhashval;
1461 	struct nfs4_stateowner *sop = NULL;
1462 
1463 	status = nfserr_inval;
1464 	if (!check_name(open->op_owner))
1465 		goto out;
1466 
1467 	if (STALE_CLIENTID(&open->op_clientid))
1468 		return nfserr_stale_clientid;
1469 
1470 	strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1471 	sop = find_openstateowner_str(strhashval, open);
1472 	if (sop) {
1473 		open->op_stateowner = sop;
1474 		/* check for replay */
1475 		if (open->op_seqid == sop->so_seqid){
1476 			if (sop->so_replay.rp_buflen)
1477 				return NFSERR_REPLAY_ME;
1478 			else {
1479 				/* The original OPEN failed so spectacularly
1480 				 * that we don't even have replay data saved!
1481 				 * Therefore, we have no choice but to continue
1482 				 * processing this OPEN; presumably, we'll
1483 				 * fail again for the same reason.
1484 				 */
1485 				dprintk("nfsd4_process_open1:"
1486 					" replay with no replay cache\n");
1487 				goto renew;
1488 			}
1489 		} else if (sop->so_confirmed) {
1490 			if (open->op_seqid == sop->so_seqid + 1)
1491 				goto renew;
1492 			status = nfserr_bad_seqid;
1493 			goto out;
1494 		} else {
1495 			/* If we get here, we received an OPEN for an
1496 			 * unconfirmed nfs4_stateowner. Since the seqid's are
1497 			 * different, purge the existing nfs4_stateowner, and
1498 			 * instantiate a new one.
1499 			 */
1500 			clp = sop->so_client;
1501 			release_stateowner(sop);
1502 		}
1503 	} else {
1504 		/* nfs4_stateowner not found.
1505 		 * Verify clientid and instantiate new nfs4_stateowner.
1506 		 * If verify fails this is presumably the result of the
1507 		 * client's lease expiring.
1508 		 */
1509 		status = nfserr_expired;
1510 		clp = find_confirmed_client(clientid);
1511 		if (clp == NULL)
1512 			goto out;
1513 	}
1514 	status = nfserr_resource;
1515 	sop = alloc_init_open_stateowner(strhashval, clp, open);
1516 	if (sop == NULL)
1517 		goto out;
1518 	open->op_stateowner = sop;
1519 renew:
1520 	status = nfs_ok;
1521 	renew_client(sop->so_client);
1522 out:
1523 	if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1524 		status = nfserr_reclaim_bad;
1525 	return status;
1526 }
1527 
1528 static int
1529 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1530 {
1531 	struct nfs4_stateid *local;
1532 	int status = nfserr_share_denied;
1533 	struct nfs4_stateowner *sop = open->op_stateowner;
1534 
1535 	list_for_each_entry(local, &fp->fi_perfile, st_perfile) {
1536 		/* ignore lock owners */
1537 		if (local->st_stateowner->so_is_open_owner == 0)
1538 			continue;
1539 		/* remember if we have seen this open owner */
1540 		if (local->st_stateowner == sop)
1541 			*stpp = local;
1542 		/* check for conflicting share reservations */
1543 		if (!test_share(local, open))
1544 			goto out;
1545 	}
1546 	status = 0;
1547 out:
1548 	return status;
1549 }
1550 
1551 static int
1552 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1553 		struct svc_fh *cur_fh, int flags)
1554 {
1555 	struct nfs4_stateid *stp;
1556 	int status;
1557 
1558 	stp = kmalloc(sizeof(struct nfs4_stateid), GFP_KERNEL);
1559 	if (stp == NULL)
1560 		return nfserr_resource;
1561 
1562 	status = nfsd_open(rqstp, cur_fh, S_IFREG, flags, &stp->st_vfs_file);
1563 	if (status) {
1564 		if (status == nfserr_dropit)
1565 			status = nfserr_jukebox;
1566 		kfree(stp);
1567 		return status;
1568 	}
1569 	vfsopen++;
1570 	*stpp = stp;
1571 	return 0;
1572 }
1573 
1574 static inline int
1575 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1576 		struct nfsd4_open *open)
1577 {
1578 	struct iattr iattr = {
1579 		.ia_valid = ATTR_SIZE,
1580 		.ia_size = 0,
1581 	};
1582 	if (!open->op_truncate)
1583 		return 0;
1584 	if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1585 		return -EINVAL;
1586 	return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1587 }
1588 
1589 static int
1590 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1591 {
1592 	struct file *filp = stp->st_vfs_file;
1593 	struct inode *inode = filp->f_dentry->d_inode;
1594 	unsigned int share_access;
1595 	int status;
1596 
1597 	set_access(&share_access, stp->st_access_bmap);
1598 	share_access = ~share_access;
1599 	share_access &= open->op_share_access;
1600 
1601 	if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1602 		return nfsd4_truncate(rqstp, cur_fh, open);
1603 
1604 	status = get_write_access(inode);
1605 	if (status)
1606 		return nfserrno(status);
1607 	status = nfsd4_truncate(rqstp, cur_fh, open);
1608 	if (status) {
1609 		put_write_access(inode);
1610 		return status;
1611 	}
1612 	/* remember the open */
1613 	filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1614 	set_bit(open->op_share_access, &stp->st_access_bmap);
1615 	set_bit(open->op_share_deny, &stp->st_deny_bmap);
1616 
1617 	return nfs_ok;
1618 }
1619 
1620 
1621 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1622 static void
1623 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1624 {
1625 	if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1626 		if (*status)
1627 			*status = nfserr_reclaim_bad;
1628 		else {
1629 			open->op_stateowner->so_confirmed = 1;
1630 			open->op_stateowner->so_seqid--;
1631 		}
1632 	}
1633 }
1634 
1635 /*
1636  * Attempt to hand out a delegation.
1637  */
1638 static void
1639 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1640 {
1641 	struct nfs4_delegation *dp;
1642 	struct nfs4_stateowner *sop = stp->st_stateowner;
1643 	struct nfs4_callback *cb = &sop->so_client->cl_callback;
1644 	struct file_lock fl, *flp = &fl;
1645 	int status, flag = 0;
1646 
1647 	flag = NFS4_OPEN_DELEGATE_NONE;
1648 	if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1649 	     || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1650 		goto out;
1651 
1652 	if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1653 		flag = NFS4_OPEN_DELEGATE_WRITE;
1654 	else
1655 		flag = NFS4_OPEN_DELEGATE_READ;
1656 
1657 	dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1658 	if (dp == NULL) {
1659 		flag = NFS4_OPEN_DELEGATE_NONE;
1660 		goto out;
1661 	}
1662 	locks_init_lock(&fl);
1663 	fl.fl_lmops = &nfsd_lease_mng_ops;
1664 	fl.fl_flags = FL_LEASE;
1665 	fl.fl_end = OFFSET_MAX;
1666 	fl.fl_owner =  (fl_owner_t)dp;
1667 	fl.fl_file = stp->st_vfs_file;
1668 	fl.fl_pid = current->tgid;
1669 
1670 	/* setlease checks to see if delegation should be handed out.
1671 	 * the lock_manager callbacks fl_mylease and fl_change are used
1672 	 */
1673 	if ((status = setlease(stp->st_vfs_file,
1674 		flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1675 		dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1676 		list_del(&dp->dl_del_perfile);
1677 		list_del(&dp->dl_del_perclnt);
1678 		nfs4_put_delegation(dp);
1679 		free_delegation++;
1680 		flag = NFS4_OPEN_DELEGATE_NONE;
1681 		goto out;
1682 	}
1683 
1684 	memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1685 
1686 	dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1687 	             dp->dl_stateid.si_boot,
1688 	             dp->dl_stateid.si_stateownerid,
1689 	             dp->dl_stateid.si_fileid,
1690 	             dp->dl_stateid.si_generation);
1691 out:
1692 	open->op_delegate_type = flag;
1693 }
1694 
1695 /*
1696  * called with nfs4_lock_state() held.
1697  */
1698 int
1699 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1700 {
1701 	struct nfs4_file *fp = NULL;
1702 	struct inode *ino = current_fh->fh_dentry->d_inode;
1703 	struct nfs4_stateid *stp = NULL;
1704 	int status;
1705 
1706 	status = nfserr_inval;
1707 	if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1708 		goto out;
1709 	/*
1710 	 * Lookup file; if found, lookup stateid and check open request,
1711 	 * and check for delegations in the process of being recalled.
1712 	 * If not found, create the nfs4_file struct
1713 	 */
1714 	fp = find_file(ino);
1715 	if (fp) {
1716 		if ((status = nfs4_check_open(fp, open, &stp)))
1717 			goto out;
1718 	} else {
1719 		status = nfserr_resource;
1720 		fp = alloc_init_file(ino);
1721 		if (fp == NULL)
1722 			goto out;
1723 	}
1724 
1725 	/*
1726 	 * OPEN the file, or upgrade an existing OPEN.
1727 	 * If truncate fails, the OPEN fails.
1728 	 */
1729 	if (stp) {
1730 		/* Stateid was found, this is an OPEN upgrade */
1731 		status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1732 		if (status)
1733 			goto out;
1734 	} else {
1735 		/* Stateid was not found, this is a new OPEN */
1736 		int flags = 0;
1737 		if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1738 			flags = MAY_WRITE;
1739 		else
1740 			flags = MAY_READ;
1741 		if ((status = nfs4_new_open(rqstp, &stp, current_fh, flags)))
1742 			goto out;
1743 		init_stateid(stp, fp, open);
1744 		status = nfsd4_truncate(rqstp, current_fh, open);
1745 		if (status) {
1746 			release_stateid(stp, OPEN_STATE);
1747 			goto out;
1748 		}
1749 	}
1750 	memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1751 
1752 	/*
1753 	* Attempt to hand out a delegation. No error return, because the
1754 	* OPEN succeeds even if we fail.
1755 	*/
1756 	nfs4_open_delegation(current_fh, open, stp);
1757 
1758 	status = nfs_ok;
1759 
1760 	dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1761 	            stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1762 	            stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1763 out:
1764 	/* take the opportunity to clean up unused state */
1765 	if (fp && list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile))
1766 		release_file(fp);
1767 
1768 	/* CLAIM_PREVIOUS has different error returns */
1769 	nfs4_set_claim_prev(open, &status);
1770 	/*
1771 	* To finish the open response, we just need to set the rflags.
1772 	*/
1773 	open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1774 	if (!open->op_stateowner->so_confirmed)
1775 		open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1776 
1777 	return status;
1778 }
1779 
1780 static struct work_struct laundromat_work;
1781 static void laundromat_main(void *);
1782 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1783 
1784 int
1785 nfsd4_renew(clientid_t *clid)
1786 {
1787 	struct nfs4_client *clp;
1788 	int status;
1789 
1790 	nfs4_lock_state();
1791 	dprintk("process_renew(%08x/%08x): starting\n",
1792 			clid->cl_boot, clid->cl_id);
1793 	status = nfserr_stale_clientid;
1794 	if (STALE_CLIENTID(clid))
1795 		goto out;
1796 	clp = find_confirmed_client(clid);
1797 	status = nfserr_expired;
1798 	if (clp == NULL) {
1799 		/* We assume the client took too long to RENEW. */
1800 		dprintk("nfsd4_renew: clientid not found!\n");
1801 		goto out;
1802 	}
1803 	renew_client(clp);
1804 	status = nfserr_cb_path_down;
1805 	if (!list_empty(&clp->cl_del_perclnt)
1806 			&& !atomic_read(&clp->cl_callback.cb_set))
1807 		goto out;
1808 	status = nfs_ok;
1809 out:
1810 	nfs4_unlock_state();
1811 	return status;
1812 }
1813 
1814 time_t
1815 nfs4_laundromat(void)
1816 {
1817 	struct nfs4_client *clp;
1818 	struct nfs4_stateowner *sop;
1819 	struct nfs4_delegation *dp;
1820 	struct list_head *pos, *next, reaplist;
1821 	time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1822 	time_t t, clientid_val = NFSD_LEASE_TIME;
1823 	time_t u, test_val = NFSD_LEASE_TIME;
1824 
1825 	nfs4_lock_state();
1826 
1827 	dprintk("NFSD: laundromat service - starting\n");
1828 	list_for_each_safe(pos, next, &client_lru) {
1829 		clp = list_entry(pos, struct nfs4_client, cl_lru);
1830 		if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1831 			t = clp->cl_time - cutoff;
1832 			if (clientid_val > t)
1833 				clientid_val = t;
1834 			break;
1835 		}
1836 		dprintk("NFSD: purging unused client (clientid %08x)\n",
1837 			clp->cl_clientid.cl_id);
1838 		expire_client(clp);
1839 	}
1840 	INIT_LIST_HEAD(&reaplist);
1841 	spin_lock(&recall_lock);
1842 	list_for_each_safe(pos, next, &del_recall_lru) {
1843 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1844 		if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1845 			u = dp->dl_time - cutoff;
1846 			if (test_val > u)
1847 				test_val = u;
1848 			break;
1849 		}
1850 		dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1851 			            dp, dp->dl_flock);
1852 		list_move(&dp->dl_recall_lru, &reaplist);
1853 	}
1854 	spin_unlock(&recall_lock);
1855 	list_for_each_safe(pos, next, &reaplist) {
1856 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1857 		list_del_init(&dp->dl_recall_lru);
1858 		unhash_delegation(dp);
1859 	}
1860 	test_val = NFSD_LEASE_TIME;
1861 	list_for_each_safe(pos, next, &close_lru) {
1862 		sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1863 		if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1864 			u = sop->so_time - cutoff;
1865 			if (test_val > u)
1866 				test_val = u;
1867 			break;
1868 		}
1869 		dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1870 			sop->so_id);
1871 		list_del(&sop->so_close_lru);
1872 		nfs4_put_stateowner(sop);
1873 	}
1874 	if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1875 		clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1876 	nfs4_unlock_state();
1877 	return clientid_val;
1878 }
1879 
1880 void
1881 laundromat_main(void *not_used)
1882 {
1883 	time_t t;
1884 
1885 	t = nfs4_laundromat();
1886 	dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1887 	schedule_delayed_work(&laundromat_work, t*HZ);
1888 }
1889 
1890 /* search ownerid_hashtbl[] and close_lru for stateid owner
1891  * (stateid->si_stateownerid)
1892  */
1893 struct nfs4_stateowner *
1894 find_openstateowner_id(u32 st_id, int flags) {
1895 	struct nfs4_stateowner *local = NULL;
1896 
1897 	dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1898 	if (flags & CLOSE_STATE) {
1899 		list_for_each_entry(local, &close_lru, so_close_lru) {
1900 			if (local->so_id == st_id)
1901 				return local;
1902 		}
1903 	}
1904 	return NULL;
1905 }
1906 
1907 static inline int
1908 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1909 {
1910 	return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1911 }
1912 
1913 static int
1914 STALE_STATEID(stateid_t *stateid)
1915 {
1916 	if (stateid->si_boot == boot_time)
1917 		return 0;
1918 	printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1919 		stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1920 		stateid->si_generation);
1921 	return 1;
1922 }
1923 
1924 static inline int
1925 access_permit_read(unsigned long access_bmap)
1926 {
1927 	return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1928 		test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1929 		test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1930 }
1931 
1932 static inline int
1933 access_permit_write(unsigned long access_bmap)
1934 {
1935 	return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1936 		test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1937 }
1938 
1939 static
1940 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1941 {
1942         int status = nfserr_openmode;
1943 
1944 	if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
1945                 goto out;
1946 	if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
1947                 goto out;
1948 	status = nfs_ok;
1949 out:
1950 	return status;
1951 }
1952 
1953 static inline int
1954 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1955 {
1956 	if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1957 		return nfserr_openmode;
1958 	else
1959 		return nfs_ok;
1960 }
1961 
1962 static inline int
1963 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
1964 {
1965 	/* Trying to call delegreturn with a special stateid? Yuch: */
1966 	if (!(flags & (RD_STATE | WR_STATE)))
1967 		return nfserr_bad_stateid;
1968 	else if (ONE_STATEID(stateid) && (flags & RD_STATE))
1969 		return nfs_ok;
1970 	else if (nfs4_in_grace()) {
1971 		/* Answer in remaining cases depends on existance of
1972 		 * conflicting state; so we must wait out the grace period. */
1973 		return nfserr_grace;
1974 	} else if (flags & WR_STATE)
1975 		return nfs4_share_conflict(current_fh,
1976 				NFS4_SHARE_DENY_WRITE);
1977 	else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
1978 		return nfs4_share_conflict(current_fh,
1979 				NFS4_SHARE_DENY_READ);
1980 }
1981 
1982 /*
1983  * Allow READ/WRITE during grace period on recovered state only for files
1984  * that are not able to provide mandatory locking.
1985  */
1986 static inline int
1987 io_during_grace_disallowed(struct inode *inode, int flags)
1988 {
1989 	return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
1990 		&& MANDATORY_LOCK(inode);
1991 }
1992 
1993 /*
1994 * Checks for stateid operations
1995 */
1996 int
1997 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
1998 {
1999 	struct nfs4_stateid *stp = NULL;
2000 	struct nfs4_delegation *dp = NULL;
2001 	stateid_t *stidp;
2002 	struct inode *ino = current_fh->fh_dentry->d_inode;
2003 	int status;
2004 
2005 	dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2006 		stateid->si_boot, stateid->si_stateownerid,
2007 		stateid->si_fileid, stateid->si_generation);
2008 	if (filpp)
2009 		*filpp = NULL;
2010 
2011 	if (io_during_grace_disallowed(ino, flags))
2012 		return nfserr_grace;
2013 
2014 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2015 		return check_special_stateids(current_fh, stateid, flags);
2016 
2017 	/* STALE STATEID */
2018 	status = nfserr_stale_stateid;
2019 	if (STALE_STATEID(stateid))
2020 		goto out;
2021 
2022 	/* BAD STATEID */
2023 	status = nfserr_bad_stateid;
2024 	if (!stateid->si_fileid) { /* delegation stateid */
2025 		if(!(dp = find_delegation_stateid(ino, stateid))) {
2026 			dprintk("NFSD: delegation stateid not found\n");
2027 			if (nfs4_in_grace())
2028 				status = nfserr_grace;
2029 			goto out;
2030 		}
2031 		stidp = &dp->dl_stateid;
2032 	} else { /* open or lock stateid */
2033 		if (!(stp = find_stateid(stateid, flags))) {
2034 			dprintk("NFSD: open or lock stateid not found\n");
2035 			if (nfs4_in_grace())
2036 				status = nfserr_grace;
2037 			goto out;
2038 		}
2039 		if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2040 			goto out;
2041 		if (!stp->st_stateowner->so_confirmed)
2042 			goto out;
2043 		stidp = &stp->st_stateid;
2044 	}
2045 	if (stateid->si_generation > stidp->si_generation)
2046 		goto out;
2047 
2048 	/* OLD STATEID */
2049 	status = nfserr_old_stateid;
2050 	if (stateid->si_generation < stidp->si_generation)
2051 		goto out;
2052 	if (stp) {
2053 		if ((status = nfs4_check_openmode(stp,flags)))
2054 			goto out;
2055 		renew_client(stp->st_stateowner->so_client);
2056 		if (filpp)
2057 			*filpp = stp->st_vfs_file;
2058 	} else if (dp) {
2059 		if ((status = nfs4_check_delegmode(dp, flags)))
2060 			goto out;
2061 		renew_client(dp->dl_client);
2062 		if (flags & DELEG_RET)
2063 			unhash_delegation(dp);
2064 		if (filpp)
2065 			*filpp = dp->dl_vfs_file;
2066 	}
2067 	status = nfs_ok;
2068 out:
2069 	return status;
2070 }
2071 
2072 
2073 /*
2074  * Checks for sequence id mutating operations.
2075  */
2076 int
2077 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, clientid_t *lockclid)
2078 {
2079 	int status;
2080 	struct nfs4_stateid *stp;
2081 	struct nfs4_stateowner *sop;
2082 
2083 	dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2084 			"stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2085 		stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2086 		stateid->si_generation);
2087 
2088 	*stpp = NULL;
2089 	*sopp = NULL;
2090 
2091 	status = nfserr_bad_stateid;
2092 	if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2093 		printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2094 		goto out;
2095 	}
2096 
2097 	status = nfserr_stale_stateid;
2098 	if (STALE_STATEID(stateid))
2099 		goto out;
2100 	/*
2101 	* We return BAD_STATEID if filehandle doesn't match stateid,
2102 	* the confirmed flag is incorrecly set, or the generation
2103 	* number is incorrect.
2104 	* If there is no entry in the openfile table for this id,
2105 	* we can't always return BAD_STATEID;
2106 	* this might be a retransmitted CLOSE which has arrived after
2107 	* the openfile has been released.
2108 	*/
2109 	if (!(stp = find_stateid(stateid, flags)))
2110 		goto no_nfs4_stateid;
2111 
2112 	status = nfserr_bad_stateid;
2113 
2114 	/* for new lock stateowners:
2115 	 * check that the lock->v.new.open_stateid
2116 	 * refers to an open stateowner
2117 	 *
2118 	 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2119 	 * as the open_stateid->st_stateowner->so_client->clientid
2120 	 */
2121 	if (lockclid) {
2122 		struct nfs4_stateowner *sop = stp->st_stateowner;
2123 		struct nfs4_client *clp = sop->so_client;
2124 
2125 		if (!sop->so_is_open_owner)
2126 			goto out;
2127 		if (!cmp_clid(&clp->cl_clientid, lockclid))
2128 			goto out;
2129 	}
2130 
2131 	if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2132 		printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2133 		goto out;
2134 	}
2135 
2136 	*stpp = stp;
2137 	*sopp = sop = stp->st_stateowner;
2138 
2139 	/*
2140 	*  We now validate the seqid and stateid generation numbers.
2141 	*  For the moment, we ignore the possibility of
2142 	*  generation number wraparound.
2143 	*/
2144 	if (seqid != sop->so_seqid + 1)
2145 		goto check_replay;
2146 
2147 	if (sop->so_confirmed) {
2148 		if (flags & CONFIRM) {
2149 			printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2150 			goto out;
2151 		}
2152 	}
2153 	else {
2154 		if (!(flags & CONFIRM)) {
2155 			printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2156 			goto out;
2157 		}
2158 	}
2159 	if (stateid->si_generation > stp->st_stateid.si_generation) {
2160 		printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2161 		goto out;
2162 	}
2163 
2164 	status = nfserr_old_stateid;
2165 	if (stateid->si_generation < stp->st_stateid.si_generation) {
2166 		printk("NFSD: preprocess_seqid_op: old stateid!\n");
2167 		goto out;
2168 	}
2169 	/* XXX renew the client lease here */
2170 	status = nfs_ok;
2171 
2172 out:
2173 	return status;
2174 
2175 no_nfs4_stateid:
2176 
2177 	/*
2178 	* We determine whether this is a bad stateid or a replay,
2179 	* starting by trying to look up the stateowner.
2180 	* If stateowner is not found - stateid is bad.
2181 	*/
2182 	if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2183 		printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2184 		status = nfserr_bad_stateid;
2185 		goto out;
2186 	}
2187 	*sopp = sop;
2188 
2189 check_replay:
2190 	if (seqid == sop->so_seqid) {
2191 		printk("NFSD: preprocess_seqid_op: retransmission?\n");
2192 		/* indicate replay to calling function */
2193 		status = NFSERR_REPLAY_ME;
2194 	} else  {
2195 		printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2196 
2197 		*sopp = NULL;
2198 		status = nfserr_bad_seqid;
2199 	}
2200 	goto out;
2201 }
2202 
2203 int
2204 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2205 {
2206 	int status;
2207 	struct nfs4_stateowner *sop;
2208 	struct nfs4_stateid *stp;
2209 
2210 	dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2211 			(int)current_fh->fh_dentry->d_name.len,
2212 			current_fh->fh_dentry->d_name.name);
2213 
2214 	if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2215 		goto out;
2216 
2217 	nfs4_lock_state();
2218 
2219 	if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2220 					&oc->oc_req_stateid,
2221 					CHECK_FH | CONFIRM | OPEN_STATE,
2222 					&oc->oc_stateowner, &stp, NULL)))
2223 		goto out;
2224 
2225 	sop = oc->oc_stateowner;
2226 	sop->so_confirmed = 1;
2227 	update_stateid(&stp->st_stateid);
2228 	memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2229 	dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2230 		"stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2231 		         stp->st_stateid.si_boot,
2232 		         stp->st_stateid.si_stateownerid,
2233 		         stp->st_stateid.si_fileid,
2234 		         stp->st_stateid.si_generation);
2235 out:
2236 	if (oc->oc_stateowner)
2237 		nfs4_get_stateowner(oc->oc_stateowner);
2238 	nfs4_unlock_state();
2239 	return status;
2240 }
2241 
2242 
2243 /*
2244  * unset all bits in union bitmap (bmap) that
2245  * do not exist in share (from successful OPEN_DOWNGRADE)
2246  */
2247 static void
2248 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2249 {
2250 	int i;
2251 	for (i = 1; i < 4; i++) {
2252 		if ((i & access) != i)
2253 			__clear_bit(i, bmap);
2254 	}
2255 }
2256 
2257 static void
2258 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2259 {
2260 	int i;
2261 	for (i = 0; i < 4; i++) {
2262 		if ((i & deny) != i)
2263 			__clear_bit(i, bmap);
2264 	}
2265 }
2266 
2267 int
2268 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2269 {
2270 	int status;
2271 	struct nfs4_stateid *stp;
2272 	unsigned int share_access;
2273 
2274 	dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2275 			(int)current_fh->fh_dentry->d_name.len,
2276 			current_fh->fh_dentry->d_name.name);
2277 
2278 	if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2279 		return nfserr_inval;
2280 
2281 	nfs4_lock_state();
2282 	if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2283 					&od->od_stateid,
2284 					CHECK_FH | OPEN_STATE,
2285 					&od->od_stateowner, &stp, NULL)))
2286 		goto out;
2287 
2288 	status = nfserr_inval;
2289 	if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2290 		dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2291 			stp->st_access_bmap, od->od_share_access);
2292 		goto out;
2293 	}
2294 	if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2295 		dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2296 			stp->st_deny_bmap, od->od_share_deny);
2297 		goto out;
2298 	}
2299 	set_access(&share_access, stp->st_access_bmap);
2300 	nfs4_file_downgrade(stp->st_vfs_file,
2301 	                    share_access & ~od->od_share_access);
2302 
2303 	reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2304 	reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2305 
2306 	update_stateid(&stp->st_stateid);
2307 	memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2308 	status = nfs_ok;
2309 out:
2310 	if (od->od_stateowner)
2311 		nfs4_get_stateowner(od->od_stateowner);
2312 	nfs4_unlock_state();
2313 	return status;
2314 }
2315 
2316 /*
2317  * nfs4_unlock_state() called after encode
2318  */
2319 int
2320 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2321 {
2322 	int status;
2323 	struct nfs4_stateid *stp;
2324 
2325 	dprintk("NFSD: nfsd4_close on file %.*s\n",
2326 			(int)current_fh->fh_dentry->d_name.len,
2327 			current_fh->fh_dentry->d_name.name);
2328 
2329 	nfs4_lock_state();
2330 	/* check close_lru for replay */
2331 	if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2332 					&close->cl_stateid,
2333 					CHECK_FH | OPEN_STATE | CLOSE_STATE,
2334 					&close->cl_stateowner, &stp, NULL)))
2335 		goto out;
2336 	/*
2337 	*  Return success, but first update the stateid.
2338 	*/
2339 	status = nfs_ok;
2340 	update_stateid(&stp->st_stateid);
2341 	memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2342 
2343 	/* release_state_owner() calls nfsd_close() if needed */
2344 	release_state_owner(stp, OPEN_STATE);
2345 out:
2346 	if (close->cl_stateowner)
2347 		nfs4_get_stateowner(close->cl_stateowner);
2348 	nfs4_unlock_state();
2349 	return status;
2350 }
2351 
2352 int
2353 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2354 {
2355 	int status;
2356 
2357 	if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2358 		goto out;
2359 
2360 	nfs4_lock_state();
2361 	status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2362 	nfs4_unlock_state();
2363 out:
2364 	return status;
2365 }
2366 
2367 
2368 /*
2369  * Lock owner state (byte-range locks)
2370  */
2371 #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
2372 #define LOCK_HASH_BITS              8
2373 #define LOCK_HASH_SIZE             (1 << LOCK_HASH_BITS)
2374 #define LOCK_HASH_MASK             (LOCK_HASH_SIZE - 1)
2375 
2376 #define lockownerid_hashval(id) \
2377         ((id) & LOCK_HASH_MASK)
2378 
2379 static inline unsigned int
2380 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2381 		struct xdr_netobj *ownername)
2382 {
2383 	return (file_hashval(inode) + cl_id
2384 			+ opaque_hashval(ownername->data, ownername->len))
2385 		& LOCK_HASH_MASK;
2386 }
2387 
2388 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2389 static struct list_head	lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2390 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2391 
2392 struct nfs4_stateid *
2393 find_stateid(stateid_t *stid, int flags)
2394 {
2395 	struct nfs4_stateid *local = NULL;
2396 	u32 st_id = stid->si_stateownerid;
2397 	u32 f_id = stid->si_fileid;
2398 	unsigned int hashval;
2399 
2400 	dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2401 	if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2402 		hashval = stateid_hashval(st_id, f_id);
2403 		list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2404 			if ((local->st_stateid.si_stateownerid == st_id) &&
2405 			    (local->st_stateid.si_fileid == f_id))
2406 				return local;
2407 		}
2408 	}
2409 	if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2410 		hashval = stateid_hashval(st_id, f_id);
2411 		list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2412 			if ((local->st_stateid.si_stateownerid == st_id) &&
2413 			    (local->st_stateid.si_fileid == f_id))
2414 				return local;
2415 		}
2416 	} else
2417 		printk("NFSD: find_stateid: ERROR: no state flag\n");
2418 	return NULL;
2419 }
2420 
2421 static struct nfs4_delegation *
2422 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2423 {
2424 	struct nfs4_delegation *dp = NULL;
2425 	struct nfs4_file *fp = NULL;
2426 	u32 st_id;
2427 
2428 	dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2429                     stid->si_boot, stid->si_stateownerid,
2430                     stid->si_fileid, stid->si_generation);
2431 
2432 	st_id = stid->si_stateownerid;
2433 	fp = find_file(ino);
2434 	if (fp) {
2435 		list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
2436 			if(dp->dl_stateid.si_stateownerid == st_id) {
2437 				dprintk("NFSD: find_delegation dp %p\n",dp);
2438 				return dp;
2439 			}
2440 		}
2441 	}
2442 	return NULL;
2443 }
2444 
2445 /*
2446  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2447  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2448  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
2449  * locking, this prevents us from being completely protocol-compliant.  The
2450  * real solution to this problem is to start using unsigned file offsets in
2451  * the VFS, but this is a very deep change!
2452  */
2453 static inline void
2454 nfs4_transform_lock_offset(struct file_lock *lock)
2455 {
2456 	if (lock->fl_start < 0)
2457 		lock->fl_start = OFFSET_MAX;
2458 	if (lock->fl_end < 0)
2459 		lock->fl_end = OFFSET_MAX;
2460 }
2461 
2462 int
2463 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2464 {
2465 	struct nfs4_stateowner *local = NULL;
2466 	int status = 0;
2467 
2468 	if (hashval >= LOCK_HASH_SIZE)
2469 		goto out;
2470 	list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2471 		if (local == sop) {
2472 			status = 1;
2473 			goto out;
2474 		}
2475 	}
2476 out:
2477 	return status;
2478 }
2479 
2480 
2481 static inline void
2482 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2483 {
2484 	struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2485 	unsigned int hval = lockownerid_hashval(sop->so_id);
2486 
2487 	deny->ld_sop = NULL;
2488 	if (nfs4_verify_lock_stateowner(sop, hval)) {
2489 		kref_get(&sop->so_ref);
2490 		deny->ld_sop = sop;
2491 		deny->ld_clientid = sop->so_client->cl_clientid;
2492 	}
2493 	deny->ld_start = fl->fl_start;
2494 	deny->ld_length = ~(u64)0;
2495 	if (fl->fl_end != ~(u64)0)
2496 		deny->ld_length = fl->fl_end - fl->fl_start + 1;
2497 	deny->ld_type = NFS4_READ_LT;
2498 	if (fl->fl_type != F_RDLCK)
2499 		deny->ld_type = NFS4_WRITE_LT;
2500 }
2501 
2502 static struct nfs4_stateowner *
2503 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2504 {
2505 	struct nfs4_stateowner *local = NULL;
2506 	int i;
2507 
2508 	for (i = 0; i < LOCK_HASH_SIZE; i++) {
2509 		list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2510 			if (!cmp_owner_str(local, owner, clid))
2511 				continue;
2512 			return local;
2513 		}
2514 	}
2515 	return NULL;
2516 }
2517 
2518 static struct nfs4_stateowner *
2519 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2520 		struct xdr_netobj *owner)
2521 {
2522 	unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2523 	struct nfs4_stateowner *op;
2524 
2525 	list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2526 		if (cmp_owner_str(op, owner, clid))
2527 			return op;
2528 	}
2529 	return NULL;
2530 }
2531 
2532 /*
2533  * Alloc a lock owner structure.
2534  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2535  * occured.
2536  *
2537  * strhashval = lock_ownerstr_hashval
2538  * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2539  */
2540 
2541 static struct nfs4_stateowner *
2542 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2543 	struct nfs4_stateowner *sop;
2544 	struct nfs4_replay *rp;
2545 	unsigned int idhashval;
2546 
2547 	if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2548 		return NULL;
2549 	idhashval = lockownerid_hashval(current_ownerid);
2550 	INIT_LIST_HEAD(&sop->so_idhash);
2551 	INIT_LIST_HEAD(&sop->so_strhash);
2552 	INIT_LIST_HEAD(&sop->so_perclient);
2553 	INIT_LIST_HEAD(&sop->so_perfilestate);
2554 	INIT_LIST_HEAD(&sop->so_perlockowner);
2555 	INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2556 	sop->so_time = 0;
2557 	list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2558 	list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2559 	list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2560 	sop->so_is_open_owner = 0;
2561 	sop->so_id = current_ownerid++;
2562 	sop->so_client = clp;
2563 	sop->so_seqid = lock->lk_new_lock_seqid - 1;
2564 	sop->so_confirmed = 1;
2565 	rp = &sop->so_replay;
2566 	rp->rp_status = NFSERR_SERVERFAULT;
2567 	rp->rp_buflen = 0;
2568 	rp->rp_buf = rp->rp_ibuf;
2569 	return sop;
2570 }
2571 
2572 struct nfs4_stateid *
2573 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2574 {
2575 	struct nfs4_stateid *stp;
2576 	unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2577 
2578 	if ((stp = kmalloc(sizeof(struct nfs4_stateid),
2579 					GFP_KERNEL)) == NULL)
2580 		goto out;
2581 	INIT_LIST_HEAD(&stp->st_hash);
2582 	INIT_LIST_HEAD(&stp->st_perfile);
2583 	INIT_LIST_HEAD(&stp->st_perfilestate);
2584 	INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2585 	list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2586 	list_add(&stp->st_perfile, &fp->fi_perfile);
2587 	list_add_perfile++;
2588 	list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2589 	stp->st_stateowner = sop;
2590 	stp->st_file = fp;
2591 	stp->st_stateid.si_boot = boot_time;
2592 	stp->st_stateid.si_stateownerid = sop->so_id;
2593 	stp->st_stateid.si_fileid = fp->fi_id;
2594 	stp->st_stateid.si_generation = 0;
2595 	stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2596 	stp->st_access_bmap = open_stp->st_access_bmap;
2597 	stp->st_deny_bmap = open_stp->st_deny_bmap;
2598 
2599 out:
2600 	return stp;
2601 }
2602 
2603 int
2604 check_lock_length(u64 offset, u64 length)
2605 {
2606 	return ((length == 0)  || ((length != ~(u64)0) &&
2607 	     LOFF_OVERFLOW(offset, length)));
2608 }
2609 
2610 /*
2611  *  LOCK operation
2612  */
2613 int
2614 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2615 {
2616 	struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2617 	struct nfs4_stateid *lock_stp;
2618 	struct file *filp;
2619 	struct file_lock file_lock;
2620 	struct file_lock *conflock;
2621 	int status = 0;
2622 	unsigned int strhashval;
2623 
2624 	dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2625 		(long long) lock->lk_offset,
2626 		(long long) lock->lk_length);
2627 
2628 	if (nfs4_in_grace() && !lock->lk_reclaim)
2629 		return nfserr_grace;
2630 	if (!nfs4_in_grace() && lock->lk_reclaim)
2631 		return nfserr_no_grace;
2632 
2633 	if (check_lock_length(lock->lk_offset, lock->lk_length))
2634 		 return nfserr_inval;
2635 
2636 	nfs4_lock_state();
2637 
2638 	if (lock->lk_is_new) {
2639 	/*
2640 	 * Client indicates that this is a new lockowner.
2641 	 * Use open owner and open stateid to create lock owner and lock
2642 	 * stateid.
2643 	 */
2644 		struct nfs4_stateid *open_stp = NULL;
2645 		struct nfs4_file *fp;
2646 
2647 		status = nfserr_stale_clientid;
2648 		if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2649 			printk("NFSD: nfsd4_lock: clientid is stale!\n");
2650 			goto out;
2651 		}
2652 
2653 		/* is the new lock seqid presented by the client zero? */
2654 		status = nfserr_bad_seqid;
2655 		if (lock->v.new.lock_seqid != 0)
2656 			goto out;
2657 
2658 		/* validate and update open stateid and open seqid */
2659 		status = nfs4_preprocess_seqid_op(current_fh,
2660 				        lock->lk_new_open_seqid,
2661 		                        &lock->lk_new_open_stateid,
2662 		                        CHECK_FH | OPEN_STATE,
2663 		                        &open_sop, &open_stp,
2664 					&lock->v.new.clientid);
2665 		if (status) {
2666 			if (lock->lk_reclaim)
2667 				status = nfserr_reclaim_bad;
2668 			goto out;
2669 		}
2670 		/* create lockowner and lock stateid */
2671 		fp = open_stp->st_file;
2672 		strhashval = lock_ownerstr_hashval(fp->fi_inode,
2673 				open_sop->so_client->cl_clientid.cl_id,
2674 				&lock->v.new.owner);
2675 		/*
2676 		 * If we already have this lock owner, the client is in
2677 		 * error (or our bookeeping is wrong!)
2678 		 * for asking for a 'new lock'.
2679 		 */
2680 		status = nfserr_bad_stateid;
2681 		lock_sop = find_lockstateowner(&lock->v.new.owner,
2682 						&lock->v.new.clientid);
2683 		if (lock_sop)
2684 			goto out;
2685 		status = nfserr_resource;
2686 		if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2687 			goto out;
2688 		if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2689 						fp, open_stp)) == NULL) {
2690 			release_stateowner(lock->lk_stateowner);
2691 			lock->lk_stateowner = NULL;
2692 			goto out;
2693 		}
2694 		/* bump the open seqid used to create the lock */
2695 		open_sop->so_seqid++;
2696 	} else {
2697 		/* lock (lock owner + lock stateid) already exists */
2698 		status = nfs4_preprocess_seqid_op(current_fh,
2699 				       lock->lk_old_lock_seqid,
2700 				       &lock->lk_old_lock_stateid,
2701 				       CHECK_FH | LOCK_STATE,
2702 				       &lock->lk_stateowner, &lock_stp, NULL);
2703 		if (status)
2704 			goto out;
2705 	}
2706 	/* lock->lk_stateowner and lock_stp have been created or found */
2707 	filp = lock_stp->st_vfs_file;
2708 
2709 	if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2710 		printk("NFSD: nfsd4_lock: permission denied!\n");
2711 		goto out;
2712 	}
2713 
2714 	locks_init_lock(&file_lock);
2715 	switch (lock->lk_type) {
2716 		case NFS4_READ_LT:
2717 		case NFS4_READW_LT:
2718 			file_lock.fl_type = F_RDLCK;
2719 		break;
2720 		case NFS4_WRITE_LT:
2721 		case NFS4_WRITEW_LT:
2722 			file_lock.fl_type = F_WRLCK;
2723 		break;
2724 		default:
2725 			status = nfserr_inval;
2726 		goto out;
2727 	}
2728 	file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2729 	file_lock.fl_pid = current->tgid;
2730 	file_lock.fl_file = filp;
2731 	file_lock.fl_flags = FL_POSIX;
2732 
2733 	file_lock.fl_start = lock->lk_offset;
2734 	if ((lock->lk_length == ~(u64)0) ||
2735 			LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2736 		file_lock.fl_end = ~(u64)0;
2737 	else
2738 		file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2739 	nfs4_transform_lock_offset(&file_lock);
2740 
2741 	/*
2742 	* Try to lock the file in the VFS.
2743 	* Note: locks.c uses the BKL to protect the inode's lock list.
2744 	*/
2745 
2746 	status = posix_lock_file(filp, &file_lock);
2747 	if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2748 		file_lock.fl_ops->fl_release_private(&file_lock);
2749 	dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2750 	switch (-status) {
2751 	case 0: /* success! */
2752 		update_stateid(&lock_stp->st_stateid);
2753 		memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2754 				sizeof(stateid_t));
2755 		goto out;
2756 	case (EAGAIN):
2757 		goto conflicting_lock;
2758 	case (EDEADLK):
2759 		status = nfserr_deadlock;
2760 	default:
2761 		dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2762 		goto out_destroy_new_stateid;
2763 	}
2764 
2765 conflicting_lock:
2766 	dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2767 	status = nfserr_denied;
2768 	/* XXX There is a race here. Future patch needed to provide
2769 	 * an atomic posix_lock_and_test_file
2770 	 */
2771 	if (!(conflock = posix_test_lock(filp, &file_lock))) {
2772 		status = nfserr_serverfault;
2773 		goto out;
2774 	}
2775 	nfs4_set_lock_denied(conflock, &lock->lk_denied);
2776 
2777 out_destroy_new_stateid:
2778 	if (lock->lk_is_new) {
2779 		dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2780 	/*
2781 	* An error encountered after instantiation of the new
2782 	* stateid has forced us to destroy it.
2783 	*/
2784 		if (!seqid_mutating_err(status))
2785 			open_sop->so_seqid--;
2786 
2787 		release_state_owner(lock_stp, LOCK_STATE);
2788 	}
2789 out:
2790 	if (lock->lk_stateowner)
2791 		nfs4_get_stateowner(lock->lk_stateowner);
2792 	nfs4_unlock_state();
2793 	return status;
2794 }
2795 
2796 /*
2797  * LOCKT operation
2798  */
2799 int
2800 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2801 {
2802 	struct inode *inode;
2803 	struct file file;
2804 	struct file_lock file_lock;
2805 	struct file_lock *conflicting_lock;
2806 	int status;
2807 
2808 	if (nfs4_in_grace())
2809 		return nfserr_grace;
2810 
2811 	if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2812 		 return nfserr_inval;
2813 
2814 	lockt->lt_stateowner = NULL;
2815 	nfs4_lock_state();
2816 
2817 	status = nfserr_stale_clientid;
2818 	if (STALE_CLIENTID(&lockt->lt_clientid)) {
2819 		printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2820 		goto out;
2821 	}
2822 
2823 	if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2824 		printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2825 		if (status == nfserr_symlink)
2826 			status = nfserr_inval;
2827 		goto out;
2828 	}
2829 
2830 	inode = current_fh->fh_dentry->d_inode;
2831 	locks_init_lock(&file_lock);
2832 	switch (lockt->lt_type) {
2833 		case NFS4_READ_LT:
2834 		case NFS4_READW_LT:
2835 			file_lock.fl_type = F_RDLCK;
2836 		break;
2837 		case NFS4_WRITE_LT:
2838 		case NFS4_WRITEW_LT:
2839 			file_lock.fl_type = F_WRLCK;
2840 		break;
2841 		default:
2842 			printk("NFSD: nfs4_lockt: bad lock type!\n");
2843 			status = nfserr_inval;
2844 		goto out;
2845 	}
2846 
2847 	lockt->lt_stateowner = find_lockstateowner_str(inode,
2848 			&lockt->lt_clientid, &lockt->lt_owner);
2849 	if (lockt->lt_stateowner)
2850 		file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2851 	file_lock.fl_pid = current->tgid;
2852 	file_lock.fl_flags = FL_POSIX;
2853 
2854 	file_lock.fl_start = lockt->lt_offset;
2855 	if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2856 		file_lock.fl_end = ~(u64)0;
2857 	else
2858 		file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2859 
2860 	nfs4_transform_lock_offset(&file_lock);
2861 
2862 	/* posix_test_lock uses the struct file _only_ to resolve the inode.
2863 	 * since LOCKT doesn't require an OPEN, and therefore a struct
2864 	 * file may not exist, pass posix_test_lock a struct file with
2865 	 * only the dentry:inode set.
2866 	 */
2867 	memset(&file, 0, sizeof (struct file));
2868 	file.f_dentry = current_fh->fh_dentry;
2869 
2870 	status = nfs_ok;
2871 	conflicting_lock = posix_test_lock(&file, &file_lock);
2872 	if (conflicting_lock) {
2873 		status = nfserr_denied;
2874 		nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2875 	}
2876 out:
2877 	nfs4_unlock_state();
2878 	return status;
2879 }
2880 
2881 int
2882 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2883 {
2884 	struct nfs4_stateid *stp;
2885 	struct file *filp = NULL;
2886 	struct file_lock file_lock;
2887 	int status;
2888 
2889 	dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2890 		(long long) locku->lu_offset,
2891 		(long long) locku->lu_length);
2892 
2893 	if (check_lock_length(locku->lu_offset, locku->lu_length))
2894 		 return nfserr_inval;
2895 
2896 	nfs4_lock_state();
2897 
2898 	if ((status = nfs4_preprocess_seqid_op(current_fh,
2899 					locku->lu_seqid,
2900 					&locku->lu_stateid,
2901 					CHECK_FH | LOCK_STATE,
2902 					&locku->lu_stateowner, &stp, NULL)))
2903 		goto out;
2904 
2905 	filp = stp->st_vfs_file;
2906 	BUG_ON(!filp);
2907 	locks_init_lock(&file_lock);
2908 	file_lock.fl_type = F_UNLCK;
2909 	file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2910 	file_lock.fl_pid = current->tgid;
2911 	file_lock.fl_file = filp;
2912 	file_lock.fl_flags = FL_POSIX;
2913 	file_lock.fl_start = locku->lu_offset;
2914 
2915 	if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2916 		file_lock.fl_end = ~(u64)0;
2917 	else
2918 		file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2919 	nfs4_transform_lock_offset(&file_lock);
2920 
2921 	/*
2922 	*  Try to unlock the file in the VFS.
2923 	*/
2924 	status = posix_lock_file(filp, &file_lock);
2925 	if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2926 		file_lock.fl_ops->fl_release_private(&file_lock);
2927 	if (status) {
2928 		printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2929 		goto out_nfserr;
2930 	}
2931 	/*
2932 	* OK, unlock succeeded; the only thing left to do is update the stateid.
2933 	*/
2934 	update_stateid(&stp->st_stateid);
2935 	memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2936 
2937 out:
2938 	if (locku->lu_stateowner)
2939 		nfs4_get_stateowner(locku->lu_stateowner);
2940 	nfs4_unlock_state();
2941 	return status;
2942 
2943 out_nfserr:
2944 	status = nfserrno(status);
2945 	goto out;
2946 }
2947 
2948 /*
2949  * returns
2950  * 	1: locks held by lockowner
2951  * 	0: no locks held by lockowner
2952  */
2953 static int
2954 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2955 {
2956 	struct file_lock **flpp;
2957 	struct inode *inode = filp->f_dentry->d_inode;
2958 	int status = 0;
2959 
2960 	lock_kernel();
2961 	for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2962 		if ((*flpp)->fl_owner == (fl_owner_t)lowner)
2963 			status = 1;
2964 			goto out;
2965 	}
2966 out:
2967 	unlock_kernel();
2968 	return status;
2969 }
2970 
2971 int
2972 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2973 {
2974 	clientid_t *clid = &rlockowner->rl_clientid;
2975 	struct nfs4_stateowner *local = NULL;
2976 	struct xdr_netobj *owner = &rlockowner->rl_owner;
2977 	int status;
2978 
2979 	dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2980 		clid->cl_boot, clid->cl_id);
2981 
2982 	/* XXX check for lease expiration */
2983 
2984 	status = nfserr_stale_clientid;
2985 	if (STALE_CLIENTID(clid)) {
2986 		printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
2987 		return status;
2988 	}
2989 
2990 	nfs4_lock_state();
2991 
2992 	status = nfs_ok;
2993 	local = find_lockstateowner(owner, clid);
2994 	if (local) {
2995 		struct nfs4_stateid *stp;
2996 
2997 		/* check for any locks held by any stateid
2998 		 * associated with the (lock) stateowner */
2999 		status = nfserr_locks_held;
3000 		list_for_each_entry(stp, &local->so_perfilestate,
3001 				st_perfilestate) {
3002 			if (check_for_locks(stp->st_vfs_file, local))
3003 				goto out;
3004 		}
3005 		/* no locks held by (lock) stateowner */
3006 		status = nfs_ok;
3007 		release_stateowner(local);
3008 	}
3009 out:
3010 	nfs4_unlock_state();
3011 	return status;
3012 }
3013 
3014 static inline struct nfs4_client_reclaim *
3015 alloc_reclaim(int namelen)
3016 {
3017 	struct nfs4_client_reclaim *crp = NULL;
3018 
3019 	crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3020 	if (!crp)
3021 		return NULL;
3022 	crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3023 	if (!crp->cr_name.data) {
3024 		kfree(crp);
3025 		return NULL;
3026 	}
3027 	return crp;
3028 }
3029 
3030 /*
3031  * failure => all reset bets are off, nfserr_no_grace...
3032  */
3033 static int
3034 nfs4_client_to_reclaim(char *name, int namlen)
3035 {
3036 	unsigned int strhashval;
3037 	struct nfs4_client_reclaim *crp = NULL;
3038 
3039 	dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3040 	crp = alloc_reclaim(namlen);
3041 	if (!crp)
3042 		return 0;
3043 	strhashval = clientstr_hashval(name, namlen);
3044 	INIT_LIST_HEAD(&crp->cr_strhash);
3045 	list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3046 	memcpy(crp->cr_name.data, name, namlen);
3047 	crp->cr_name.len = namlen;
3048 	reclaim_str_hashtbl_size++;
3049 	return 1;
3050 }
3051 
3052 static void
3053 nfs4_release_reclaim(void)
3054 {
3055 	struct nfs4_client_reclaim *crp = NULL;
3056 	int i;
3057 
3058 	BUG_ON(!nfs4_reclaim_init);
3059 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3060 		while (!list_empty(&reclaim_str_hashtbl[i])) {
3061 			crp = list_entry(reclaim_str_hashtbl[i].next,
3062 			                struct nfs4_client_reclaim, cr_strhash);
3063 			list_del(&crp->cr_strhash);
3064 			kfree(crp->cr_name.data);
3065 			kfree(crp);
3066 			reclaim_str_hashtbl_size--;
3067 		}
3068 	}
3069 	BUG_ON(reclaim_str_hashtbl_size);
3070 }
3071 
3072 /*
3073  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3074 struct nfs4_client_reclaim *
3075 nfs4_find_reclaim_client(clientid_t *clid)
3076 {
3077 	unsigned int strhashval;
3078 	struct nfs4_client *clp;
3079 	struct nfs4_client_reclaim *crp = NULL;
3080 
3081 
3082 	/* find clientid in conf_id_hashtbl */
3083 	clp = find_confirmed_client(clid);
3084 	if (clp == NULL)
3085 		return NULL;
3086 
3087 	dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3088 		            clp->cl_name.len, clp->cl_name.data);
3089 
3090 	/* find clp->cl_name in reclaim_str_hashtbl */
3091 	strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3092 	list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3093 		if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3094 			return crp;
3095 		}
3096 	}
3097 	return NULL;
3098 }
3099 
3100 /*
3101 * Called from OPEN. Look for clientid in reclaim list.
3102 */
3103 int
3104 nfs4_check_open_reclaim(clientid_t *clid)
3105 {
3106 	struct nfs4_client_reclaim *crp;
3107 
3108 	if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
3109 		return nfserr_reclaim_bad;
3110 	return nfs_ok;
3111 }
3112 
3113 
3114 /*
3115  * Start and stop routines
3116  */
3117 
3118 static void
3119 __nfs4_state_init(void)
3120 {
3121 	int i;
3122 	time_t grace_time;
3123 
3124 	if (!nfs4_reclaim_init) {
3125 		for (i = 0; i < CLIENT_HASH_SIZE; i++)
3126 			INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3127 		reclaim_str_hashtbl_size = 0;
3128 		nfs4_reclaim_init = 1;
3129 	}
3130 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3131 		INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3132 		INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3133 		INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3134 		INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3135 	}
3136 	for (i = 0; i < FILE_HASH_SIZE; i++) {
3137 		INIT_LIST_HEAD(&file_hashtbl[i]);
3138 	}
3139 	for (i = 0; i < OWNER_HASH_SIZE; i++) {
3140 		INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3141 		INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3142 	}
3143 	for (i = 0; i < STATEID_HASH_SIZE; i++) {
3144 		INIT_LIST_HEAD(&stateid_hashtbl[i]);
3145 		INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3146 	}
3147 	for (i = 0; i < LOCK_HASH_SIZE; i++) {
3148 		INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3149 		INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3150 	}
3151 	memset(&zerostateid, 0, sizeof(stateid_t));
3152 	memset(&onestateid, ~0, sizeof(stateid_t));
3153 
3154 	INIT_LIST_HEAD(&close_lru);
3155 	INIT_LIST_HEAD(&client_lru);
3156 	INIT_LIST_HEAD(&del_recall_lru);
3157 	spin_lock_init(&recall_lock);
3158 	boot_time = get_seconds();
3159 	grace_time = max(old_lease_time, lease_time);
3160 	if (reclaim_str_hashtbl_size == 0)
3161 		grace_time = 0;
3162 	if (grace_time)
3163 		printk("NFSD: starting %ld-second grace period\n", grace_time);
3164 	grace_end = boot_time + grace_time;
3165 	INIT_WORK(&laundromat_work,laundromat_main, NULL);
3166 	schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3167 }
3168 
3169 int
3170 nfs4_state_init(void)
3171 {
3172 	int status;
3173 
3174 	if (nfs4_init)
3175 		return 0;
3176 	status = nfsd4_init_slabs();
3177 	if (status)
3178 		return status;
3179 	__nfs4_state_init();
3180 	nfs4_init = 1;
3181 	return 0;
3182 }
3183 
3184 int
3185 nfs4_in_grace(void)
3186 {
3187 	return get_seconds() < grace_end;
3188 }
3189 
3190 void
3191 set_no_grace(void)
3192 {
3193 	printk("NFSD: ERROR in reboot recovery.  State reclaims will fail.\n");
3194 	grace_end = get_seconds();
3195 }
3196 
3197 time_t
3198 nfs4_lease_time(void)
3199 {
3200 	return lease_time;
3201 }
3202 
3203 static void
3204 __nfs4_state_shutdown(void)
3205 {
3206 	int i;
3207 	struct nfs4_client *clp = NULL;
3208 	struct nfs4_delegation *dp = NULL;
3209 	struct nfs4_stateowner *sop = NULL;
3210 	struct list_head *pos, *next, reaplist;
3211 
3212 	list_for_each_safe(pos, next, &close_lru) {
3213 		sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3214 		list_del(&sop->so_close_lru);
3215 		nfs4_put_stateowner(sop);
3216 	}
3217 
3218 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3219 		while (!list_empty(&conf_id_hashtbl[i])) {
3220 			clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3221 			expire_client(clp);
3222 		}
3223 		while (!list_empty(&unconf_str_hashtbl[i])) {
3224 			clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3225 			expire_client(clp);
3226 		}
3227 	}
3228 	INIT_LIST_HEAD(&reaplist);
3229 	spin_lock(&recall_lock);
3230 	list_for_each_safe(pos, next, &del_recall_lru) {
3231 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3232 		list_move(&dp->dl_recall_lru, &reaplist);
3233 	}
3234 	spin_unlock(&recall_lock);
3235 	list_for_each_safe(pos, next, &reaplist) {
3236 		dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3237 		list_del_init(&dp->dl_recall_lru);
3238 		unhash_delegation(dp);
3239 	}
3240 
3241 	release_all_files();
3242 	cancel_delayed_work(&laundromat_work);
3243 	flush_scheduled_work();
3244 	nfs4_init = 0;
3245 	dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
3246 			list_add_perfile, list_del_perfile);
3247 	dprintk("NFSD: add_perclient %d del_perclient %d\n",
3248 			add_perclient, del_perclient);
3249 	dprintk("NFSD: alloc_file %d free_file %d\n",
3250 			alloc_file, free_file);
3251 	dprintk("NFSD: vfsopen %d vfsclose %d\n",
3252 			vfsopen, vfsclose);
3253 	dprintk("NFSD: alloc_delegation %d free_delegation %d\n",
3254 			alloc_delegation, free_delegation);
3255 
3256 }
3257 
3258 void
3259 nfs4_state_shutdown(void)
3260 {
3261 	nfs4_lock_state();
3262 	nfs4_release_reclaim();
3263 	__nfs4_state_shutdown();
3264 	nfsd4_free_slabs();
3265 	nfs4_unlock_state();
3266 }
3267 
3268 /*
3269  * Called when leasetime is changed.
3270  *
3271  * if nfsd is not started, simply set the global lease.
3272  *
3273  * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3274  * e.g: boot_time is reset, existing nfs4_client structs are
3275  * used to fill reclaim_str_hashtbl, then all state (except for the
3276  * reclaim_str_hashtbl) is re-initialized.
3277  *
3278  * if the old lease time is greater than the new lease time, the grace
3279  * period needs to be set to the old lease time to allow clients to reclaim
3280  * their state. XXX - we may want to set the grace period == lease time
3281  * after an initial grace period == old lease time
3282  *
3283  * if an error occurs in this process, the new lease is set, but the server
3284  * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3285  * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3286  *
3287  * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3288  * OPEN and LOCK reclaims.
3289  */
3290 void
3291 nfs4_reset_lease(time_t leasetime)
3292 {
3293 	struct nfs4_client *clp;
3294 	int i;
3295 
3296 	printk("NFSD: New leasetime %ld\n",leasetime);
3297 	if (!nfs4_init)
3298 		return;
3299 	nfs4_lock_state();
3300 	old_lease_time = lease_time;
3301 	lease_time = leasetime;
3302 
3303 	nfs4_release_reclaim();
3304 
3305 	/* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3306 	for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3307 		list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3308 			if (!nfs4_client_to_reclaim(clp->cl_name.data,
3309 						clp->cl_name.len)) {
3310 				nfs4_release_reclaim();
3311 				goto init_state;
3312 			}
3313 		}
3314 	}
3315 init_state:
3316 	__nfs4_state_shutdown();
3317 	__nfs4_state_init();
3318 	nfs4_unlock_state();
3319 }
3320 
3321