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