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