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