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