[PATCH] knfsd: nfsd4: nfs4_check_open_reclaim cleanup
[powerpc.git] / 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 /* forward declarations */
69 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
70 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
71 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
72
73 /* Locking:
74  *
75  * client_sema: 
76  *      protects clientid_hashtbl[], clientstr_hashtbl[],
77  *      unconfstr_hashtbl[], uncofid_hashtbl[].
78  */
79 static DECLARE_MUTEX(client_sema);
80
81 kmem_cache_t *stateowner_slab = NULL;
82 kmem_cache_t *file_slab = NULL;
83 kmem_cache_t *stateid_slab = NULL;
84 kmem_cache_t *deleg_slab = NULL;
85
86 void
87 nfs4_lock_state(void)
88 {
89         down(&client_sema);
90 }
91
92 void
93 nfs4_unlock_state(void)
94 {
95         up(&client_sema);
96 }
97
98 static inline u32
99 opaque_hashval(const void *ptr, int nbytes)
100 {
101         unsigned char *cptr = (unsigned char *) ptr;
102
103         u32 x = 0;
104         while (nbytes--) {
105                 x *= 37;
106                 x += *cptr++;
107         }
108         return x;
109 }
110
111 /* forward declarations */
112 static void release_stateowner(struct nfs4_stateowner *sop);
113 static void release_stateid(struct nfs4_stateid *stp, int flags);
114
115 /*
116  * Delegation state
117  */
118
119 /* recall_lock protects the del_recall_lru */
120 spinlock_t recall_lock;
121 static struct list_head del_recall_lru;
122
123 static void
124 free_nfs4_file(struct kref *kref)
125 {
126         struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
127         list_del(&fp->fi_hash);
128         iput(fp->fi_inode);
129         kmem_cache_free(file_slab, fp);
130 }
131
132 static inline void
133 put_nfs4_file(struct nfs4_file *fi)
134 {
135         kref_put(&fi->fi_ref, free_nfs4_file);
136 }
137
138 static inline void
139 get_nfs4_file(struct nfs4_file *fi)
140 {
141         kref_get(&fi->fi_ref);
142 }
143
144 static struct nfs4_delegation *
145 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
146 {
147         struct nfs4_delegation *dp;
148         struct nfs4_file *fp = stp->st_file;
149         struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
150
151         dprintk("NFSD alloc_init_deleg\n");
152         dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
153         if (dp == NULL)
154                 return dp;
155         INIT_LIST_HEAD(&dp->dl_del_perfile);
156         INIT_LIST_HEAD(&dp->dl_del_perclnt);
157         INIT_LIST_HEAD(&dp->dl_recall_lru);
158         dp->dl_client = clp;
159         get_nfs4_file(fp);
160         dp->dl_file = fp;
161         dp->dl_flock = NULL;
162         get_file(stp->st_vfs_file);
163         dp->dl_vfs_file = stp->st_vfs_file;
164         dp->dl_type = type;
165         dp->dl_recall.cbr_dp = NULL;
166         dp->dl_recall.cbr_ident = cb->cb_ident;
167         dp->dl_recall.cbr_trunc = 0;
168         dp->dl_stateid.si_boot = boot_time;
169         dp->dl_stateid.si_stateownerid = current_delegid++;
170         dp->dl_stateid.si_fileid = 0;
171         dp->dl_stateid.si_generation = 0;
172         dp->dl_fhlen = current_fh->fh_handle.fh_size;
173         memcpy(dp->dl_fhval, &current_fh->fh_handle.fh_base,
174                         current_fh->fh_handle.fh_size);
175         dp->dl_time = 0;
176         atomic_set(&dp->dl_count, 1);
177         list_add(&dp->dl_del_perfile, &fp->fi_delegations);
178         list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
179         return dp;
180 }
181
182 void
183 nfs4_put_delegation(struct nfs4_delegation *dp)
184 {
185         if (atomic_dec_and_test(&dp->dl_count)) {
186                 dprintk("NFSD: freeing dp %p\n",dp);
187                 put_nfs4_file(dp->dl_file);
188                 kmem_cache_free(deleg_slab, dp);
189         }
190 }
191
192 /* Remove the associated file_lock first, then remove the delegation.
193  * lease_modify() is called to remove the FS_LEASE file_lock from
194  * the i_flock list, eventually calling nfsd's lock_manager
195  * fl_release_callback.
196  */
197 static void
198 nfs4_close_delegation(struct nfs4_delegation *dp)
199 {
200         struct file *filp = dp->dl_vfs_file;
201
202         dprintk("NFSD: close_delegation dp %p\n",dp);
203         dp->dl_vfs_file = NULL;
204         /* The following nfsd_close may not actually close the file,
205          * but we want to remove the lease in any case. */
206         if (dp->dl_flock)
207                 setlease(filp, F_UNLCK, &dp->dl_flock);
208         nfsd_close(filp);
209 }
210
211 /* Called under the state lock. */
212 static void
213 unhash_delegation(struct nfs4_delegation *dp)
214 {
215         list_del_init(&dp->dl_del_perfile);
216         list_del_init(&dp->dl_del_perclnt);
217         spin_lock(&recall_lock);
218         list_del_init(&dp->dl_recall_lru);
219         spin_unlock(&recall_lock);
220         nfs4_close_delegation(dp);
221         nfs4_put_delegation(dp);
222 }
223
224 /* 
225  * SETCLIENTID state 
226  */
227
228 /* Hash tables for nfs4_clientid state */
229 #define CLIENT_HASH_BITS                 4
230 #define CLIENT_HASH_SIZE                (1 << CLIENT_HASH_BITS)
231 #define CLIENT_HASH_MASK                (CLIENT_HASH_SIZE - 1)
232
233 #define clientid_hashval(id) \
234         ((id) & CLIENT_HASH_MASK)
235 #define clientstr_hashval(name, namelen) \
236         (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
237 /*
238  * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
239  * used in reboot/reset lease grace period processing
240  *
241  * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
242  * setclientid_confirmed info. 
243  *
244  * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed 
245  * setclientid info.
246  *
247  * client_lru holds client queue ordered by nfs4_client.cl_time
248  * for lease renewal.
249  *
250  * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
251  * for last close replay.
252  */
253 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
254 static int reclaim_str_hashtbl_size = 0;
255 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
256 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
257 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
258 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
259 static struct list_head client_lru;
260 static struct list_head close_lru;
261
262 static inline void
263 renew_client(struct nfs4_client *clp)
264 {
265         /*
266         * Move client to the end to the LRU list.
267         */
268         dprintk("renewing client (clientid %08x/%08x)\n", 
269                         clp->cl_clientid.cl_boot, 
270                         clp->cl_clientid.cl_id);
271         list_move_tail(&clp->cl_lru, &client_lru);
272         clp->cl_time = get_seconds();
273 }
274
275 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
276 static int
277 STALE_CLIENTID(clientid_t *clid)
278 {
279         if (clid->cl_boot == boot_time)
280                 return 0;
281         dprintk("NFSD stale clientid (%08x/%08x)\n", 
282                         clid->cl_boot, clid->cl_id);
283         return 1;
284 }
285
286 /* 
287  * XXX Should we use a slab cache ?
288  * This type of memory management is somewhat inefficient, but we use it
289  * anyway since SETCLIENTID is not a common operation.
290  */
291 static inline struct nfs4_client *
292 alloc_client(struct xdr_netobj name)
293 {
294         struct nfs4_client *clp;
295
296         if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
297                 memset(clp, 0, sizeof(*clp));
298                 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
299                         memcpy(clp->cl_name.data, name.data, name.len);
300                         clp->cl_name.len = name.len;
301                 }
302                 else {
303                         kfree(clp);
304                         clp = NULL;
305                 }
306         }
307         return clp;
308 }
309
310 static inline void
311 free_client(struct nfs4_client *clp)
312 {
313         if (clp->cl_cred.cr_group_info)
314                 put_group_info(clp->cl_cred.cr_group_info);
315         kfree(clp->cl_name.data);
316         kfree(clp);
317 }
318
319 void
320 put_nfs4_client(struct nfs4_client *clp)
321 {
322         if (atomic_dec_and_test(&clp->cl_count))
323                 free_client(clp);
324 }
325
326 static void
327 expire_client(struct nfs4_client *clp)
328 {
329         struct nfs4_stateowner *sop;
330         struct nfs4_delegation *dp;
331         struct nfs4_callback *cb = &clp->cl_callback;
332         struct rpc_clnt *clnt = clp->cl_callback.cb_client;
333         struct list_head reaplist;
334
335         dprintk("NFSD: expire_client cl_count %d\n",
336                             atomic_read(&clp->cl_count));
337
338         /* shutdown rpc client, ending any outstanding recall rpcs */
339         if (atomic_read(&cb->cb_set) == 1 && clnt) {
340                 rpc_shutdown_client(clnt);
341                 clnt = clp->cl_callback.cb_client = NULL;
342         }
343
344         INIT_LIST_HEAD(&reaplist);
345         spin_lock(&recall_lock);
346         while (!list_empty(&clp->cl_del_perclnt)) {
347                 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
348                 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
349                                 dp->dl_flock);
350                 list_del_init(&dp->dl_del_perclnt);
351                 list_move(&dp->dl_recall_lru, &reaplist);
352         }
353         spin_unlock(&recall_lock);
354         while (!list_empty(&reaplist)) {
355                 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
356                 list_del_init(&dp->dl_recall_lru);
357                 unhash_delegation(dp);
358         }
359         list_del(&clp->cl_idhash);
360         list_del(&clp->cl_strhash);
361         list_del(&clp->cl_lru);
362         while (!list_empty(&clp->cl_perclient)) {
363                 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
364                 release_stateowner(sop);
365         }
366         put_nfs4_client(clp);
367 }
368
369 static struct nfs4_client *
370 create_client(struct xdr_netobj name) {
371         struct nfs4_client *clp;
372
373         if (!(clp = alloc_client(name)))
374                 goto out;
375         atomic_set(&clp->cl_count, 1);
376         atomic_set(&clp->cl_callback.cb_set, 0);
377         clp->cl_callback.cb_parsed = 0;
378         INIT_LIST_HEAD(&clp->cl_idhash);
379         INIT_LIST_HEAD(&clp->cl_strhash);
380         INIT_LIST_HEAD(&clp->cl_perclient);
381         INIT_LIST_HEAD(&clp->cl_del_perclnt);
382         INIT_LIST_HEAD(&clp->cl_lru);
383 out:
384         return clp;
385 }
386
387 static void
388 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
389         memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
390 }
391
392 static void
393 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
394         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
395         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
396 }
397
398 static void
399 copy_cred(struct svc_cred *target, struct svc_cred *source) {
400
401         target->cr_uid = source->cr_uid;
402         target->cr_gid = source->cr_gid;
403         target->cr_group_info = source->cr_group_info;
404         get_group_info(target->cr_group_info);
405 }
406
407 static int
408 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
409         if (!n1 || !n2)
410                 return 0;
411         return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
412 }
413
414 static int
415 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
416         return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
417 }
418
419 static int
420 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
421         return((cl1->cl_boot == cl2->cl_boot) &&
422                 (cl1->cl_id == cl2->cl_id));
423 }
424
425 /* XXX what about NGROUP */
426 static int
427 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
428         return(cr1->cr_uid == cr2->cr_uid);
429
430 }
431
432 static void
433 gen_clid(struct nfs4_client *clp) {
434         clp->cl_clientid.cl_boot = boot_time;
435         clp->cl_clientid.cl_id = current_clientid++; 
436 }
437
438 static void
439 gen_confirm(struct nfs4_client *clp) {
440         struct timespec         tv;
441         u32 *                   p;
442
443         tv = CURRENT_TIME;
444         p = (u32 *)clp->cl_confirm.data;
445         *p++ = tv.tv_sec;
446         *p++ = tv.tv_nsec;
447 }
448
449 static int
450 check_name(struct xdr_netobj name) {
451
452         if (name.len == 0) 
453                 return 0;
454         if (name.len > NFS4_OPAQUE_LIMIT) {
455                 printk("NFSD: check_name: name too long(%d)!\n", name.len);
456                 return 0;
457         }
458         return 1;
459 }
460
461 void
462 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
463 {
464         unsigned int idhashval;
465
466         list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
467         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
468         list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
469         list_add_tail(&clp->cl_lru, &client_lru);
470         clp->cl_time = get_seconds();
471 }
472
473 void
474 move_to_confirmed(struct nfs4_client *clp)
475 {
476         unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
477         unsigned int strhashval;
478
479         dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
480         list_del_init(&clp->cl_strhash);
481         list_del_init(&clp->cl_idhash);
482         list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
483         strhashval = clientstr_hashval(clp->cl_name.data, 
484                         clp->cl_name.len);
485         list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
486         renew_client(clp);
487 }
488
489 static struct nfs4_client *
490 find_confirmed_client(clientid_t *clid)
491 {
492         struct nfs4_client *clp;
493         unsigned int idhashval = clientid_hashval(clid->cl_id);
494
495         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
496                 if (cmp_clid(&clp->cl_clientid, clid))
497                         return clp;
498         }
499         return NULL;
500 }
501
502 static struct nfs4_client *
503 find_unconfirmed_client(clientid_t *clid)
504 {
505         struct nfs4_client *clp;
506         unsigned int idhashval = clientid_hashval(clid->cl_id);
507
508         list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
509                 if (cmp_clid(&clp->cl_clientid, clid))
510                         return clp;
511         }
512         return NULL;
513 }
514
515 /* a helper function for parse_callback */
516 static int
517 parse_octet(unsigned int *lenp, char **addrp)
518 {
519         unsigned int len = *lenp;
520         char *p = *addrp;
521         int n = -1;
522         char c;
523
524         for (;;) {
525                 if (!len)
526                         break;
527                 len--;
528                 c = *p++;
529                 if (c == '.')
530                         break;
531                 if ((c < '0') || (c > '9')) {
532                         n = -1;
533                         break;
534                 }
535                 if (n < 0)
536                         n = 0;
537                 n = (n * 10) + (c - '0');
538                 if (n > 255) {
539                         n = -1;
540                         break;
541                 }
542         }
543         *lenp = len;
544         *addrp = p;
545         return n;
546 }
547
548 /* parse and set the setclientid ipv4 callback address */
549 int
550 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
551 {
552         int temp = 0;
553         u32 cbaddr = 0;
554         u16 cbport = 0;
555         u32 addrlen = addr_len;
556         char *addr = addr_val;
557         int i, shift;
558
559         /* ipaddress */
560         shift = 24;
561         for(i = 4; i > 0  ; i--) {
562                 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
563                         return 0;
564                 }
565                 cbaddr |= (temp << shift);
566                 if (shift > 0)
567                 shift -= 8;
568         }
569         *cbaddrp = cbaddr;
570
571         /* port */
572         shift = 8;
573         for(i = 2; i > 0  ; i--) {
574                 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
575                         return 0;
576                 }
577                 cbport |= (temp << shift);
578                 if (shift > 0)
579                         shift -= 8;
580         }
581         *cbportp = cbport;
582         return 1;
583 }
584
585 void
586 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
587 {
588         struct nfs4_callback *cb = &clp->cl_callback;
589
590         /* Currently, we only support tcp for the callback channel */
591         if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
592                 goto out_err;
593
594         if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
595                          &cb->cb_addr, &cb->cb_port)))
596                 goto out_err;
597         cb->cb_prog = se->se_callback_prog;
598         cb->cb_ident = se->se_callback_ident;
599         cb->cb_parsed = 1;
600         return;
601 out_err:
602         printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
603                 "will not receive delegations\n",
604                 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
605
606         cb->cb_parsed = 0;
607         return;
608 }
609
610 /*
611  * RFC 3010 has a complex implmentation description of processing a 
612  * SETCLIENTID request consisting of 5 bullets, labeled as 
613  * CASE0 - CASE4 below.
614  *
615  * NOTES:
616  *      callback information will be processed in a future patch
617  *
618  *      an unconfirmed record is added when:
619  *      NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
620  *      CASE 1: confirmed record found with matching name, principal,
621  *              verifier, and clientid.
622  *      CASE 2: confirmed record found with matching name, principal,
623  *              and there is no unconfirmed record with matching
624  *              name and principal
625  *
626  *      an unconfirmed record is replaced when:
627  *      CASE 3: confirmed record found with matching name, principal,
628  *              and an unconfirmed record is found with matching 
629  *              name, principal, and with clientid and
630  *              confirm that does not match the confirmed record.
631  *      CASE 4: there is no confirmed record with matching name and 
632  *              principal. there is an unconfirmed record with 
633  *              matching name, principal.
634  *
635  *      an unconfirmed record is deleted when:
636  *      CASE 1: an unconfirmed record that matches input name, verifier,
637  *              and confirmed clientid.
638  *      CASE 4: any unconfirmed records with matching name and principal
639  *              that exist after an unconfirmed record has been replaced
640  *              as described above.
641  *
642  */
643 int
644 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
645 {
646         u32                     ip_addr = rqstp->rq_addr.sin_addr.s_addr;
647         struct xdr_netobj       clname = { 
648                 .len = setclid->se_namelen,
649                 .data = setclid->se_name,
650         };
651         nfs4_verifier           clverifier = setclid->se_verf;
652         unsigned int            strhashval;
653         struct nfs4_client *    conf, * unconf, * new, * clp;
654         int                     status;
655         
656         status = nfserr_inval;
657         if (!check_name(clname))
658                 goto out;
659
660         /* 
661          * XXX The Duplicate Request Cache (DRC) has been checked (??)
662          * We get here on a DRC miss.
663          */
664
665         strhashval = clientstr_hashval(clname.data, clname.len);
666
667         conf = NULL;
668         nfs4_lock_state();
669         list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
670                 if (!cmp_name(&clp->cl_name, &clname))
671                         continue;
672                 /* 
673                  * CASE 0:
674                  * clname match, confirmed, different principal
675                  * or different ip_address
676                  */
677                 status = nfserr_clid_inuse;
678                 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
679                         printk("NFSD: setclientid: string in use by client"
680                         "(clientid %08x/%08x)\n",
681                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
682                         goto out;
683                 }
684                 if (clp->cl_addr != ip_addr) { 
685                         printk("NFSD: setclientid: string in use by client"
686                         "(clientid %08x/%08x)\n",
687                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
688                         goto out;
689                 }
690
691                 /* 
692                  * cl_name match from a previous SETCLIENTID operation
693                  * XXX check for additional matches?
694                  */
695                 conf = clp;
696                 break;
697         }
698         unconf = NULL;
699         list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
700                 if (!cmp_name(&clp->cl_name, &clname))
701                         continue;
702                 /* cl_name match from a previous SETCLIENTID operation */
703                 unconf = clp;
704                 break;
705         }
706         status = nfserr_resource;
707         if (!conf) {
708                 /* 
709                  * CASE 4:
710                  * placed first, because it is the normal case.
711                  */
712                 if (unconf)
713                         expire_client(unconf);
714                 if (!(new = create_client(clname)))
715                         goto out;
716                 copy_verf(new, &clverifier);
717                 new->cl_addr = ip_addr;
718                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
719                 gen_clid(new);
720                 gen_confirm(new);
721                 gen_callback(new, setclid);
722                 add_to_unconfirmed(new, strhashval);
723         } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
724                 /*
725                  * CASE 1:
726                  * cl_name match, confirmed, principal match
727                  * verifier match: probable callback update
728                  *
729                  * remove any unconfirmed nfs4_client with 
730                  * matching cl_name, cl_verifier, and cl_clientid
731                  *
732                  * create and insert an unconfirmed nfs4_client with same 
733                  * cl_name, cl_verifier, and cl_clientid as existing 
734                  * nfs4_client,  but with the new callback info and a 
735                  * new cl_confirm
736                  */
737                 if ((unconf) && 
738                     cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
739                      cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
740                                 expire_client(unconf);
741                 }
742                 if (!(new = create_client(clname)))
743                         goto out;
744                 copy_verf(new,&conf->cl_verifier);
745                 new->cl_addr = ip_addr;
746                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
747                 copy_clid(new, conf);
748                 gen_confirm(new);
749                 gen_callback(new, setclid);
750                 add_to_unconfirmed(new,strhashval);
751         } else if (!unconf) {
752                 /*
753                  * CASE 2:
754                  * clname match, confirmed, principal match
755                  * verfier does not match
756                  * no unconfirmed. create a new unconfirmed nfs4_client
757                  * using input clverifier, clname, and callback info
758                  * and generate a new cl_clientid and cl_confirm.
759                  */
760                 if (!(new = create_client(clname)))
761                         goto out;
762                 copy_verf(new,&clverifier);
763                 new->cl_addr = ip_addr;
764                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
765                 gen_clid(new);
766                 gen_confirm(new);
767                 gen_callback(new, setclid);
768                 add_to_unconfirmed(new, strhashval);
769         } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
770                 /*      
771                  * CASE3:
772                  * confirmed found (name, principal match)
773                  * confirmed verifier does not match input clverifier
774                  *
775                  * unconfirmed found (name match)
776                  * confirmed->cl_confirm != unconfirmed->cl_confirm
777                  *
778                  * remove unconfirmed.
779                  *
780                  * create an unconfirmed nfs4_client 
781                  * with same cl_name as existing confirmed nfs4_client, 
782                  * but with new callback info, new cl_clientid,
783                  * new cl_verifier and a new cl_confirm
784                  */
785                 expire_client(unconf);
786                 if (!(new = create_client(clname)))
787                         goto out;
788                 copy_verf(new,&clverifier);
789                 new->cl_addr = ip_addr;
790                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
791                 gen_clid(new);
792                 gen_confirm(new);
793                 gen_callback(new, setclid);
794                 add_to_unconfirmed(new, strhashval);
795         } else {
796                 /* No cases hit !!! */
797                 status = nfserr_inval;
798                 goto out;
799
800         }
801         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
802         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
803         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
804         status = nfs_ok;
805 out:
806         nfs4_unlock_state();
807         return status;
808 }
809
810
811 /*
812  * RFC 3010 has a complex implmentation description of processing a 
813  * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
814  * processing on a DRC miss, labeled as CASE1 - CASE4 below.
815  *
816  * NOTE: callback information will be processed here in a future patch
817  */
818 int
819 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
820 {
821         u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
822         struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
823         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
824         clientid_t * clid = &setclientid_confirm->sc_clientid;
825         int status;
826
827         if (STALE_CLIENTID(clid))
828                 return nfserr_stale_clientid;
829         /* 
830          * XXX The Duplicate Request Cache (DRC) has been checked (??)
831          * We get here on a DRC miss.
832          */
833
834         nfs4_lock_state();
835         clp = find_confirmed_client(clid);
836         if (clp) {
837                 status = nfserr_inval;
838                 /* 
839                  * Found a record for this clientid. If the IP addresses
840                  * don't match, return ERR_INVAL just as if the record had
841                  * not been found.
842                  */
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                 conf = clp;
850         }
851         clp = find_unconfirmed_client(clid);
852         if (clp) {
853                 status = nfserr_inval;
854                 if (clp->cl_addr != ip_addr) { 
855                         printk("NFSD: setclientid: string in use by client"
856                         "(clientid %08x/%08x)\n",
857                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
858                         goto out;
859                 }
860                 unconf = clp;
861         }
862         /* CASE 1: 
863         * unconf record that matches input clientid and input confirm.
864         * conf record that matches input clientid.
865         * conf  and unconf records match names, verifiers 
866         */
867         if ((conf && unconf) && 
868             (cmp_verf(&unconf->cl_confirm, &confirm)) &&
869             (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
870             (cmp_name(&conf->cl_name,&unconf->cl_name))  &&
871             (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
872                 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred)) 
873                         status = nfserr_clid_inuse;
874                 else {
875                         expire_client(conf);
876                         clp = unconf;
877                         move_to_confirmed(unconf);
878                         status = nfs_ok;
879                 }
880                 goto out;
881         } 
882         /* CASE 2:
883          * conf record that matches input clientid.
884          * if unconf record that matches input clientid, then unconf->cl_name
885          * or unconf->cl_verifier don't match the conf record.
886          */
887         if ((conf && !unconf) || 
888             ((conf && unconf) && 
889              (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
890               !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
891                 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
892                         status = nfserr_clid_inuse;
893                 } else {
894                         clp = conf;
895                         status = nfs_ok;
896                 }
897                 goto out;
898         }
899         /* CASE 3:
900          * conf record not found.
901          * unconf record found. 
902          * unconf->cl_confirm matches input confirm
903          */ 
904         if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
905                 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
906                         status = nfserr_clid_inuse;
907                 } else {
908                         status = nfs_ok;
909                         clp = unconf;
910                         move_to_confirmed(unconf);
911                 }
912                 goto out;
913         }
914         /* CASE 4:
915          * conf record not found, or if conf, then conf->cl_confirm does not
916          * match input confirm.
917          * unconf record not found, or if unconf, then unconf->cl_confirm 
918          * does not match input confirm.
919          */
920         if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
921             (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
922                 status = nfserr_stale_clientid;
923                 goto out;
924         }
925         /* check that we have hit one of the cases...*/
926         status = nfserr_inval;
927         goto out;
928 out:
929         if (!status)
930                 nfsd4_probe_callback(clp);
931         nfs4_unlock_state();
932         return status;
933 }
934
935 /* 
936  * Open owner state (share locks)
937  */
938
939 /* hash tables for nfs4_stateowner */
940 #define OWNER_HASH_BITS              8
941 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
942 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
943
944 #define ownerid_hashval(id) \
945         ((id) & OWNER_HASH_MASK)
946 #define ownerstr_hashval(clientid, ownername) \
947         (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
948
949 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
950 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
951
952 /* hash table for nfs4_file */
953 #define FILE_HASH_BITS                   8
954 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
955 #define FILE_HASH_MASK                  (FILE_HASH_SIZE - 1)
956 /* hash table for (open)nfs4_stateid */
957 #define STATEID_HASH_BITS              10
958 #define STATEID_HASH_SIZE              (1 << STATEID_HASH_BITS)
959 #define STATEID_HASH_MASK              (STATEID_HASH_SIZE - 1)
960
961 #define file_hashval(x) \
962         hash_ptr(x, FILE_HASH_BITS)
963 #define stateid_hashval(owner_id, file_id)  \
964         (((owner_id) + (file_id)) & STATEID_HASH_MASK)
965
966 static struct list_head file_hashtbl[FILE_HASH_SIZE];
967 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
968
969 /* OPEN Share state helper functions */
970 static inline struct nfs4_file *
971 alloc_init_file(struct inode *ino)
972 {
973         struct nfs4_file *fp;
974         unsigned int hashval = file_hashval(ino);
975
976         fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
977         if (fp) {
978                 kref_init(&fp->fi_ref);
979                 INIT_LIST_HEAD(&fp->fi_hash);
980                 INIT_LIST_HEAD(&fp->fi_stateids);
981                 INIT_LIST_HEAD(&fp->fi_delegations);
982                 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
983                 fp->fi_inode = igrab(ino);
984                 fp->fi_id = current_fileid++;
985                 return fp;
986         }
987         return NULL;
988 }
989
990 static void
991 nfsd4_free_slab(kmem_cache_t **slab)
992 {
993         int status;
994
995         if (*slab == NULL)
996                 return;
997         status = kmem_cache_destroy(*slab);
998         *slab = NULL;
999         WARN_ON(status);
1000 }
1001
1002 static void
1003 nfsd4_free_slabs(void)
1004 {
1005         nfsd4_free_slab(&stateowner_slab);
1006         nfsd4_free_slab(&file_slab);
1007         nfsd4_free_slab(&stateid_slab);
1008         nfsd4_free_slab(&deleg_slab);
1009 }
1010
1011 static int
1012 nfsd4_init_slabs(void)
1013 {
1014         stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1015                         sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1016         if (stateowner_slab == NULL)
1017                 goto out_nomem;
1018         file_slab = kmem_cache_create("nfsd4_files",
1019                         sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1020         if (file_slab == NULL)
1021                 goto out_nomem;
1022         stateid_slab = kmem_cache_create("nfsd4_stateids",
1023                         sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1024         if (stateid_slab == NULL)
1025                 goto out_nomem;
1026         deleg_slab = kmem_cache_create("nfsd4_delegations",
1027                         sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1028         if (deleg_slab == NULL)
1029                 goto out_nomem;
1030         return 0;
1031 out_nomem:
1032         nfsd4_free_slabs();
1033         dprintk("nfsd4: out of memory while initializing nfsv4\n");
1034         return -ENOMEM;
1035 }
1036
1037 void
1038 nfs4_free_stateowner(struct kref *kref)
1039 {
1040         struct nfs4_stateowner *sop =
1041                 container_of(kref, struct nfs4_stateowner, so_ref);
1042         kfree(sop->so_owner.data);
1043         kmem_cache_free(stateowner_slab, sop);
1044 }
1045
1046 static inline struct nfs4_stateowner *
1047 alloc_stateowner(struct xdr_netobj *owner)
1048 {
1049         struct nfs4_stateowner *sop;
1050
1051         if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1052                 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1053                         memcpy(sop->so_owner.data, owner->data, owner->len);
1054                         sop->so_owner.len = owner->len;
1055                         kref_init(&sop->so_ref);
1056                         return sop;
1057                 } 
1058                 kmem_cache_free(stateowner_slab, sop);
1059         }
1060         return NULL;
1061 }
1062
1063 static struct nfs4_stateowner *
1064 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1065         struct nfs4_stateowner *sop;
1066         struct nfs4_replay *rp;
1067         unsigned int idhashval;
1068
1069         if (!(sop = alloc_stateowner(&open->op_owner)))
1070                 return NULL;
1071         idhashval = ownerid_hashval(current_ownerid);
1072         INIT_LIST_HEAD(&sop->so_idhash);
1073         INIT_LIST_HEAD(&sop->so_strhash);
1074         INIT_LIST_HEAD(&sop->so_perclient);
1075         INIT_LIST_HEAD(&sop->so_perfilestate);
1076         INIT_LIST_HEAD(&sop->so_perlockowner);  /* not used */
1077         INIT_LIST_HEAD(&sop->so_close_lru);
1078         sop->so_time = 0;
1079         list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1080         list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1081         list_add(&sop->so_perclient, &clp->cl_perclient);
1082         sop->so_is_open_owner = 1;
1083         sop->so_id = current_ownerid++;
1084         sop->so_client = clp;
1085         sop->so_seqid = open->op_seqid;
1086         sop->so_confirmed = 0;
1087         rp = &sop->so_replay;
1088         rp->rp_status = NFSERR_SERVERFAULT;
1089         rp->rp_buflen = 0;
1090         rp->rp_buf = rp->rp_ibuf;
1091         return sop;
1092 }
1093
1094 static void
1095 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1096 {
1097         struct nfs4_stateowner *lock_sop;
1098
1099         while (!list_empty(&open_stp->st_perlockowner)) {
1100                 lock_sop = list_entry(open_stp->st_perlockowner.next,
1101                                 struct nfs4_stateowner, so_perlockowner);
1102                 /* list_del(&open_stp->st_perlockowner);  */
1103                 BUG_ON(lock_sop->so_is_open_owner);
1104                 release_stateowner(lock_sop);
1105         }
1106 }
1107
1108 static void
1109 unhash_stateowner(struct nfs4_stateowner *sop)
1110 {
1111         struct nfs4_stateid *stp;
1112
1113         list_del(&sop->so_idhash);
1114         list_del(&sop->so_strhash);
1115         if (sop->so_is_open_owner)
1116                 list_del(&sop->so_perclient);
1117         list_del(&sop->so_perlockowner);
1118         while (!list_empty(&sop->so_perfilestate)) {
1119                 stp = list_entry(sop->so_perfilestate.next, 
1120                         struct nfs4_stateid, st_perfilestate);
1121                 if (sop->so_is_open_owner)
1122                         release_stateid(stp, OPEN_STATE);
1123                 else
1124                         release_stateid(stp, LOCK_STATE);
1125         }
1126 }
1127
1128 static void
1129 release_stateowner(struct nfs4_stateowner *sop)
1130 {
1131         unhash_stateowner(sop);
1132         list_del(&sop->so_close_lru);
1133         nfs4_put_stateowner(sop);
1134 }
1135
1136 static inline void
1137 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1138         struct nfs4_stateowner *sop = open->op_stateowner;
1139         unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1140
1141         INIT_LIST_HEAD(&stp->st_hash);
1142         INIT_LIST_HEAD(&stp->st_perfilestate);
1143         INIT_LIST_HEAD(&stp->st_perlockowner);
1144         INIT_LIST_HEAD(&stp->st_perfile);
1145         list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1146         list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1147         list_add(&stp->st_perfile, &fp->fi_stateids);
1148         stp->st_stateowner = sop;
1149         get_nfs4_file(fp);
1150         stp->st_file = fp;
1151         stp->st_stateid.si_boot = boot_time;
1152         stp->st_stateid.si_stateownerid = sop->so_id;
1153         stp->st_stateid.si_fileid = fp->fi_id;
1154         stp->st_stateid.si_generation = 0;
1155         stp->st_access_bmap = 0;
1156         stp->st_deny_bmap = 0;
1157         __set_bit(open->op_share_access, &stp->st_access_bmap);
1158         __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1159 }
1160
1161 static void
1162 release_stateid(struct nfs4_stateid *stp, int flags)
1163 {
1164         struct file *filp = stp->st_vfs_file;
1165
1166         list_del(&stp->st_hash);
1167         list_del(&stp->st_perfile);
1168         list_del(&stp->st_perfilestate);
1169         if (flags & OPEN_STATE) {
1170                 release_stateid_lockowners(stp);
1171                 stp->st_vfs_file = NULL;
1172                 nfsd_close(filp);
1173         } else if (flags & LOCK_STATE)
1174                 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1175         put_nfs4_file(stp->st_file);
1176         kmem_cache_free(stateid_slab, stp);
1177         stp = NULL;
1178 }
1179
1180 void
1181 move_to_close_lru(struct nfs4_stateowner *sop)
1182 {
1183         dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1184
1185         unhash_stateowner(sop);
1186         list_add_tail(&sop->so_close_lru, &close_lru);
1187         sop->so_time = get_seconds();
1188 }
1189
1190 void
1191 release_state_owner(struct nfs4_stateid *stp, int flag)
1192 {
1193         struct nfs4_stateowner *sop = stp->st_stateowner;
1194
1195         dprintk("NFSD: release_state_owner\n");
1196         release_stateid(stp, flag);
1197
1198         /* place unused nfs4_stateowners on so_close_lru list to be
1199          * released by the laundromat service after the lease period
1200          * to enable us to handle CLOSE replay
1201          */
1202         if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1203                 move_to_close_lru(sop);
1204 }
1205
1206 static int
1207 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1208         return ((sop->so_owner.len == owner->len) && 
1209          !memcmp(sop->so_owner.data, owner->data, owner->len) && 
1210           (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1211 }
1212
1213 static struct nfs4_stateowner *
1214 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1215 {
1216         struct nfs4_stateowner *so = NULL;
1217
1218         list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1219                 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1220                         return so;
1221         }
1222         return NULL;
1223 }
1224
1225 /* search file_hashtbl[] for file */
1226 static struct nfs4_file *
1227 find_file(struct inode *ino)
1228 {
1229         unsigned int hashval = file_hashval(ino);
1230         struct nfs4_file *fp;
1231
1232         list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1233                 if (fp->fi_inode == ino) {
1234                         get_nfs4_file(fp);
1235                         return fp;
1236                 }
1237         }
1238         return NULL;
1239 }
1240
1241 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1242 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1243
1244 void
1245 set_access(unsigned int *access, unsigned long bmap) {
1246         int i;
1247
1248         *access = 0;
1249         for (i = 1; i < 4; i++) {
1250                 if (test_bit(i, &bmap))
1251                         *access |= i;
1252         }
1253 }
1254
1255 void
1256 set_deny(unsigned int *deny, unsigned long bmap) {
1257         int i;
1258
1259         *deny = 0;
1260         for (i = 0; i < 4; i++) {
1261                 if (test_bit(i, &bmap))
1262                         *deny |= i ;
1263         }
1264 }
1265
1266 static int
1267 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1268         unsigned int access, deny;
1269
1270         set_access(&access, stp->st_access_bmap);
1271         set_deny(&deny, stp->st_deny_bmap);
1272         if ((access & open->op_share_deny) || (deny & open->op_share_access))
1273                 return 0;
1274         return 1;
1275 }
1276
1277 /*
1278  * Called to check deny when READ with all zero stateid or
1279  * WRITE with all zero or all one stateid
1280  */
1281 int
1282 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1283 {
1284         struct inode *ino = current_fh->fh_dentry->d_inode;
1285         struct nfs4_file *fp;
1286         struct nfs4_stateid *stp;
1287         int ret;
1288
1289         dprintk("NFSD: nfs4_share_conflict\n");
1290
1291         fp = find_file(ino);
1292         if (!fp)
1293                 return nfs_ok;
1294         ret = nfserr_share_denied;
1295         /* Search for conflicting share reservations */
1296         list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1297                 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1298                     test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1299                         goto out;
1300         }
1301         ret = nfs_ok;
1302 out:
1303         put_nfs4_file(fp);
1304         return ret;
1305 }
1306
1307 static inline void
1308 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1309 {
1310         if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1311                 put_write_access(filp->f_dentry->d_inode);
1312                 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1313         }
1314 }
1315
1316 /*
1317  * Recall a delegation
1318  */
1319 static int
1320 do_recall(void *__dp)
1321 {
1322         struct nfs4_delegation *dp = __dp;
1323
1324         daemonize("nfsv4-recall");
1325
1326         nfsd4_cb_recall(dp);
1327         return 0;
1328 }
1329
1330 /*
1331  * Spawn a thread to perform a recall on the delegation represented
1332  * by the lease (file_lock)
1333  *
1334  * Called from break_lease() with lock_kernel() held.
1335  * Note: we assume break_lease will only call this *once* for any given
1336  * lease.
1337  */
1338 static
1339 void nfsd_break_deleg_cb(struct file_lock *fl)
1340 {
1341         struct nfs4_delegation *dp=  (struct nfs4_delegation *)fl->fl_owner;
1342         struct task_struct *t;
1343
1344         dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1345         if (!dp)
1346                 return;
1347
1348         /* We're assuming the state code never drops its reference
1349          * without first removing the lease.  Since we're in this lease
1350          * callback (and since the lease code is serialized by the kernel
1351          * lock) we know the server hasn't removed the lease yet, we know
1352          * it's safe to take a reference: */
1353         atomic_inc(&dp->dl_count);
1354
1355         spin_lock(&recall_lock);
1356         list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1357         spin_unlock(&recall_lock);
1358
1359         /* only place dl_time is set. protected by lock_kernel*/
1360         dp->dl_time = get_seconds();
1361
1362         /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1363         fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1364
1365         t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1366         if (IS_ERR(t)) {
1367                 struct nfs4_client *clp = dp->dl_client;
1368
1369                 printk(KERN_INFO "NFSD: Callback thread failed for "
1370                         "for client (clientid %08x/%08x)\n",
1371                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1372                 nfs4_put_delegation(dp);
1373         }
1374 }
1375
1376 /*
1377  * The file_lock is being reapd.
1378  *
1379  * Called by locks_free_lock() with lock_kernel() held.
1380  */
1381 static
1382 void nfsd_release_deleg_cb(struct file_lock *fl)
1383 {
1384         struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1385
1386         dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1387
1388         if (!(fl->fl_flags & FL_LEASE) || !dp)
1389                 return;
1390         dp->dl_flock = NULL;
1391 }
1392
1393 /*
1394  * Set the delegation file_lock back pointer.
1395  *
1396  * Called from __setlease() with lock_kernel() held.
1397  */
1398 static
1399 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1400 {
1401         struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1402
1403         dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1404         if (!dp)
1405                 return;
1406         dp->dl_flock = new;
1407 }
1408
1409 /*
1410  * Called from __setlease() with lock_kernel() held
1411  */
1412 static
1413 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1414 {
1415         struct nfs4_delegation *onlistd =
1416                 (struct nfs4_delegation *)onlist->fl_owner;
1417         struct nfs4_delegation *tryd =
1418                 (struct nfs4_delegation *)try->fl_owner;
1419
1420         if (onlist->fl_lmops != try->fl_lmops)
1421                 return 0;
1422
1423         return onlistd->dl_client == tryd->dl_client;
1424 }
1425
1426
1427 static
1428 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1429 {
1430         if (arg & F_UNLCK)
1431                 return lease_modify(onlist, arg);
1432         else
1433                 return -EAGAIN;
1434 }
1435
1436 struct lock_manager_operations nfsd_lease_mng_ops = {
1437         .fl_break = nfsd_break_deleg_cb,
1438         .fl_release_private = nfsd_release_deleg_cb,
1439         .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1440         .fl_mylease = nfsd_same_client_deleg_cb,
1441         .fl_change = nfsd_change_deleg_cb,
1442 };
1443
1444
1445 /*
1446  * nfsd4_process_open1()
1447  *      lookup stateowner.
1448  *              found:
1449  *                      check confirmed 
1450  *                              confirmed:
1451  *                                      check seqid
1452  *                              not confirmed:
1453  *                                      delete owner
1454  *                                      create new owner
1455  *              notfound:
1456  *                      verify clientid
1457  *                      create new owner
1458  *
1459  * called with nfs4_lock_state() held.
1460  */
1461 int
1462 nfsd4_process_open1(struct nfsd4_open *open)
1463 {
1464         int status;
1465         clientid_t *clientid = &open->op_clientid;
1466         struct nfs4_client *clp = NULL;
1467         unsigned int strhashval;
1468         struct nfs4_stateowner *sop = NULL;
1469
1470         status = nfserr_inval;
1471         if (!check_name(open->op_owner))
1472                 goto out;
1473
1474         if (STALE_CLIENTID(&open->op_clientid))
1475                 return nfserr_stale_clientid;
1476
1477         strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1478         sop = find_openstateowner_str(strhashval, open);
1479         if (sop) {
1480                 open->op_stateowner = sop;
1481                 /* check for replay */
1482                 if (open->op_seqid == sop->so_seqid){
1483                         if (sop->so_replay.rp_buflen)
1484                                 return NFSERR_REPLAY_ME;
1485                         else {
1486                                 /* The original OPEN failed so spectacularly
1487                                  * that we don't even have replay data saved!
1488                                  * Therefore, we have no choice but to continue
1489                                  * processing this OPEN; presumably, we'll
1490                                  * fail again for the same reason.
1491                                  */
1492                                 dprintk("nfsd4_process_open1:"
1493                                         " replay with no replay cache\n");
1494                                 goto renew;
1495                         }
1496                 } else if (sop->so_confirmed) {
1497                         if (open->op_seqid == sop->so_seqid + 1)
1498                                 goto renew;
1499                         status = nfserr_bad_seqid;
1500                         goto out;
1501                 } else {
1502                         /* If we get here, we received an OPEN for an
1503                          * unconfirmed nfs4_stateowner. Since the seqid's are
1504                          * different, purge the existing nfs4_stateowner, and
1505                          * instantiate a new one.
1506                          */
1507                         clp = sop->so_client;
1508                         release_stateowner(sop);
1509                 }
1510         } else {
1511                 /* nfs4_stateowner not found.
1512                  * Verify clientid and instantiate new nfs4_stateowner.
1513                  * If verify fails this is presumably the result of the
1514                  * client's lease expiring.
1515                  */
1516                 status = nfserr_expired;
1517                 clp = find_confirmed_client(clientid);
1518                 if (clp == NULL)
1519                         goto out;
1520         }
1521         status = nfserr_resource;
1522         sop = alloc_init_open_stateowner(strhashval, clp, open);
1523         if (sop == NULL)
1524                 goto out;
1525         open->op_stateowner = sop;
1526 renew:
1527         status = nfs_ok;
1528         renew_client(sop->so_client);
1529 out:
1530         if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1531                 status = nfserr_reclaim_bad;
1532         return status;
1533 }
1534
1535 static inline int
1536 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1537 {
1538         if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1539                 return nfserr_openmode;
1540         else
1541                 return nfs_ok;
1542 }
1543
1544 static struct nfs4_delegation *
1545 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1546 {
1547         struct nfs4_delegation *dp;
1548
1549         list_for_each_entry(dp, &fp->fi_delegations, dl_del_perfile) {
1550                 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1551                         return dp;
1552         }
1553         return NULL;
1554 }
1555
1556 static int
1557 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1558                 struct nfs4_delegation **dp)
1559 {
1560         int flags;
1561         int status = nfserr_bad_stateid;
1562
1563         *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1564         if (*dp == NULL)
1565                 goto out;
1566         flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1567                                                 RD_STATE : WR_STATE;
1568         status = nfs4_check_delegmode(*dp, flags);
1569         if (status)
1570                 *dp = NULL;
1571 out:
1572         if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1573                 return nfs_ok;
1574         if (status)
1575                 return status;
1576         open->op_stateowner->so_confirmed = 1;
1577         return nfs_ok;
1578 }
1579
1580 static int
1581 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1582 {
1583         struct nfs4_stateid *local;
1584         int status = nfserr_share_denied;
1585         struct nfs4_stateowner *sop = open->op_stateowner;
1586
1587         list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1588                 /* ignore lock owners */
1589                 if (local->st_stateowner->so_is_open_owner == 0)
1590                         continue;
1591                 /* remember if we have seen this open owner */
1592                 if (local->st_stateowner == sop)
1593                         *stpp = local;
1594                 /* check for conflicting share reservations */
1595                 if (!test_share(local, open))
1596                         goto out;
1597         }
1598         status = 0;
1599 out:
1600         return status;
1601 }
1602
1603 static inline struct nfs4_stateid *
1604 nfs4_alloc_stateid(void)
1605 {
1606         return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
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 = nfs4_alloc_stateid();
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                         kmem_cache_free(stateid_slab, stp);
1631                         return status;
1632                 }
1633         }
1634         *stpp = stp;
1635         return 0;
1636 }
1637
1638 static inline int
1639 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1640                 struct nfsd4_open *open)
1641 {
1642         struct iattr iattr = {
1643                 .ia_valid = ATTR_SIZE,
1644                 .ia_size = 0,
1645         };
1646         if (!open->op_truncate)
1647                 return 0;
1648         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1649                 return -EINVAL;
1650         return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1651 }
1652
1653 static int
1654 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1655 {
1656         struct file *filp = stp->st_vfs_file;
1657         struct inode *inode = filp->f_dentry->d_inode;
1658         unsigned int share_access;
1659         int status;
1660
1661         set_access(&share_access, stp->st_access_bmap);
1662         share_access = ~share_access;
1663         share_access &= open->op_share_access;
1664
1665         if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1666                 return nfsd4_truncate(rqstp, cur_fh, open);
1667
1668         status = get_write_access(inode);
1669         if (status)
1670                 return nfserrno(status);
1671         status = nfsd4_truncate(rqstp, cur_fh, open);
1672         if (status) {
1673                 put_write_access(inode);
1674                 return status;
1675         }
1676         /* remember the open */
1677         filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1678         set_bit(open->op_share_access, &stp->st_access_bmap);
1679         set_bit(open->op_share_deny, &stp->st_deny_bmap);
1680
1681         return nfs_ok;
1682 }
1683
1684
1685 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1686 static void
1687 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1688 {
1689         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1690                 if (*status)
1691                         *status = nfserr_reclaim_bad;
1692                 else {
1693                         open->op_stateowner->so_confirmed = 1;
1694                         open->op_stateowner->so_seqid--;
1695                 }
1696         }
1697 }
1698
1699 /*
1700  * Attempt to hand out a delegation.
1701  */
1702 static void
1703 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1704 {
1705         struct nfs4_delegation *dp;
1706         struct nfs4_stateowner *sop = stp->st_stateowner;
1707         struct nfs4_callback *cb = &sop->so_client->cl_callback;
1708         struct file_lock fl, *flp = &fl;
1709         int status, flag = 0;
1710
1711         flag = NFS4_OPEN_DELEGATE_NONE;
1712         if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1713              || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1714                 goto out;
1715
1716         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1717                 flag = NFS4_OPEN_DELEGATE_WRITE;
1718         else
1719                 flag = NFS4_OPEN_DELEGATE_READ;
1720
1721         dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1722         if (dp == NULL) {
1723                 flag = NFS4_OPEN_DELEGATE_NONE;
1724                 goto out;
1725         }
1726         locks_init_lock(&fl);
1727         fl.fl_lmops = &nfsd_lease_mng_ops;
1728         fl.fl_flags = FL_LEASE;
1729         fl.fl_end = OFFSET_MAX;
1730         fl.fl_owner =  (fl_owner_t)dp;
1731         fl.fl_file = stp->st_vfs_file;
1732         fl.fl_pid = current->tgid;
1733
1734         /* setlease checks to see if delegation should be handed out.
1735          * the lock_manager callbacks fl_mylease and fl_change are used
1736          */
1737         if ((status = setlease(stp->st_vfs_file,
1738                 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1739                 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1740                 unhash_delegation(dp);
1741                 flag = NFS4_OPEN_DELEGATE_NONE;
1742                 goto out;
1743         }
1744
1745         memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1746
1747         dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1748                      dp->dl_stateid.si_boot,
1749                      dp->dl_stateid.si_stateownerid,
1750                      dp->dl_stateid.si_fileid,
1751                      dp->dl_stateid.si_generation);
1752 out:
1753         open->op_delegate_type = flag;
1754 }
1755
1756 /*
1757  * called with nfs4_lock_state() held.
1758  */
1759 int
1760 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1761 {
1762         struct nfs4_file *fp = NULL;
1763         struct inode *ino = current_fh->fh_dentry->d_inode;
1764         struct nfs4_stateid *stp = NULL;
1765         struct nfs4_delegation *dp = NULL;
1766         int status;
1767
1768         status = nfserr_inval;
1769         if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1770                 goto out;
1771         /*
1772          * Lookup file; if found, lookup stateid and check open request,
1773          * and check for delegations in the process of being recalled.
1774          * If not found, create the nfs4_file struct
1775          */
1776         fp = find_file(ino);
1777         if (fp) {
1778                 if ((status = nfs4_check_open(fp, open, &stp)))
1779                         goto out;
1780                 status = nfs4_check_deleg(fp, open, &dp);
1781                 if (status)
1782                         goto out;
1783         } else {
1784                 status = nfserr_bad_stateid;
1785                 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1786                         goto out;
1787                 status = nfserr_resource;
1788                 fp = alloc_init_file(ino);
1789                 if (fp == NULL)
1790                         goto out;
1791         }
1792
1793         /*
1794          * OPEN the file, or upgrade an existing OPEN.
1795          * If truncate fails, the OPEN fails.
1796          */
1797         if (stp) {
1798                 /* Stateid was found, this is an OPEN upgrade */
1799                 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1800                 if (status)
1801                         goto out;
1802         } else {
1803                 /* Stateid was not found, this is a new OPEN */
1804                 int flags = 0;
1805                 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1806                         flags = MAY_WRITE;
1807                 else
1808                         flags = MAY_READ;
1809                 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1810                 if (status)
1811                         goto out;
1812                 init_stateid(stp, fp, open);
1813                 status = nfsd4_truncate(rqstp, current_fh, open);
1814                 if (status) {
1815                         release_stateid(stp, OPEN_STATE);
1816                         goto out;
1817                 }
1818         }
1819         memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1820
1821         /*
1822         * Attempt to hand out a delegation. No error return, because the
1823         * OPEN succeeds even if we fail.
1824         */
1825         nfs4_open_delegation(current_fh, open, stp);
1826
1827         status = nfs_ok;
1828
1829         dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1830                     stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1831                     stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1832 out:
1833         if (fp)
1834                 put_nfs4_file(fp);
1835         /* CLAIM_PREVIOUS has different error returns */
1836         nfs4_set_claim_prev(open, &status);
1837         /*
1838         * To finish the open response, we just need to set the rflags.
1839         */
1840         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1841         if (!open->op_stateowner->so_confirmed)
1842                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1843
1844         return status;
1845 }
1846
1847 static struct work_struct laundromat_work;
1848 static void laundromat_main(void *);
1849 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1850
1851 int 
1852 nfsd4_renew(clientid_t *clid)
1853 {
1854         struct nfs4_client *clp;
1855         int status;
1856
1857         nfs4_lock_state();
1858         dprintk("process_renew(%08x/%08x): starting\n", 
1859                         clid->cl_boot, clid->cl_id);
1860         status = nfserr_stale_clientid;
1861         if (STALE_CLIENTID(clid))
1862                 goto out;
1863         clp = find_confirmed_client(clid);
1864         status = nfserr_expired;
1865         if (clp == NULL) {
1866                 /* We assume the client took too long to RENEW. */
1867                 dprintk("nfsd4_renew: clientid not found!\n");
1868                 goto out;
1869         }
1870         renew_client(clp);
1871         status = nfserr_cb_path_down;
1872         if (!list_empty(&clp->cl_del_perclnt)
1873                         && !atomic_read(&clp->cl_callback.cb_set))
1874                 goto out;
1875         status = nfs_ok;
1876 out:
1877         nfs4_unlock_state();
1878         return status;
1879 }
1880
1881 time_t
1882 nfs4_laundromat(void)
1883 {
1884         struct nfs4_client *clp;
1885         struct nfs4_stateowner *sop;
1886         struct nfs4_delegation *dp;
1887         struct list_head *pos, *next, reaplist;
1888         time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1889         time_t t, clientid_val = NFSD_LEASE_TIME;
1890         time_t u, test_val = NFSD_LEASE_TIME;
1891
1892         nfs4_lock_state();
1893
1894         dprintk("NFSD: laundromat service - starting\n");
1895         list_for_each_safe(pos, next, &client_lru) {
1896                 clp = list_entry(pos, struct nfs4_client, cl_lru);
1897                 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1898                         t = clp->cl_time - cutoff;
1899                         if (clientid_val > t)
1900                                 clientid_val = t;
1901                         break;
1902                 }
1903                 dprintk("NFSD: purging unused client (clientid %08x)\n",
1904                         clp->cl_clientid.cl_id);
1905                 expire_client(clp);
1906         }
1907         INIT_LIST_HEAD(&reaplist);
1908         spin_lock(&recall_lock);
1909         list_for_each_safe(pos, next, &del_recall_lru) {
1910                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1911                 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1912                         u = dp->dl_time - cutoff;
1913                         if (test_val > u)
1914                                 test_val = u;
1915                         break;
1916                 }
1917                 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1918                                     dp, dp->dl_flock);
1919                 list_move(&dp->dl_recall_lru, &reaplist);
1920         }
1921         spin_unlock(&recall_lock);
1922         list_for_each_safe(pos, next, &reaplist) {
1923                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1924                 list_del_init(&dp->dl_recall_lru);
1925                 unhash_delegation(dp);
1926         }
1927         test_val = NFSD_LEASE_TIME;
1928         list_for_each_safe(pos, next, &close_lru) {
1929                 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1930                 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1931                         u = sop->so_time - cutoff;
1932                         if (test_val > u)
1933                                 test_val = u;
1934                         break;
1935                 }
1936                 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1937                         sop->so_id);
1938                 list_del(&sop->so_close_lru);
1939                 nfs4_put_stateowner(sop);
1940         }
1941         if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1942                 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1943         nfs4_unlock_state();
1944         return clientid_val;
1945 }
1946
1947 void
1948 laundromat_main(void *not_used)
1949 {
1950         time_t t;
1951
1952         t = nfs4_laundromat();
1953         dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1954         schedule_delayed_work(&laundromat_work, t*HZ);
1955 }
1956
1957 /* search ownerid_hashtbl[] and close_lru for stateid owner
1958  * (stateid->si_stateownerid)
1959  */
1960 struct nfs4_stateowner *
1961 find_openstateowner_id(u32 st_id, int flags) {
1962         struct nfs4_stateowner *local = NULL;
1963
1964         dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1965         if (flags & CLOSE_STATE) {
1966                 list_for_each_entry(local, &close_lru, so_close_lru) {
1967                         if (local->so_id == st_id)
1968                                 return local;
1969                 }
1970         }
1971         return NULL;
1972 }
1973
1974 static inline int
1975 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1976 {
1977         return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1978 }
1979
1980 static int
1981 STALE_STATEID(stateid_t *stateid)
1982 {
1983         if (stateid->si_boot == boot_time)
1984                 return 0;
1985         printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1986                 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1987                 stateid->si_generation);
1988         return 1;
1989 }
1990
1991 static inline int
1992 access_permit_read(unsigned long access_bmap)
1993 {
1994         return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1995                 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1996                 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1997 }
1998
1999 static inline int
2000 access_permit_write(unsigned long access_bmap)
2001 {
2002         return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2003                 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2004 }
2005
2006 static
2007 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2008 {
2009         int status = nfserr_openmode;
2010
2011         if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2012                 goto out;
2013         if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2014                 goto out;
2015         status = nfs_ok;
2016 out:
2017         return status;
2018 }
2019
2020 static inline int
2021 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2022 {
2023         /* Trying to call delegreturn with a special stateid? Yuch: */
2024         if (!(flags & (RD_STATE | WR_STATE)))
2025                 return nfserr_bad_stateid;
2026         else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2027                 return nfs_ok;
2028         else if (nfs4_in_grace()) {
2029                 /* Answer in remaining cases depends on existance of
2030                  * conflicting state; so we must wait out the grace period. */
2031                 return nfserr_grace;
2032         } else if (flags & WR_STATE)
2033                 return nfs4_share_conflict(current_fh,
2034                                 NFS4_SHARE_DENY_WRITE);
2035         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2036                 return nfs4_share_conflict(current_fh,
2037                                 NFS4_SHARE_DENY_READ);
2038 }
2039
2040 /*
2041  * Allow READ/WRITE during grace period on recovered state only for files
2042  * that are not able to provide mandatory locking.
2043  */
2044 static inline int
2045 io_during_grace_disallowed(struct inode *inode, int flags)
2046 {
2047         return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2048                 && MANDATORY_LOCK(inode);
2049 }
2050
2051 /*
2052 * Checks for stateid operations
2053 */
2054 int
2055 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2056 {
2057         struct nfs4_stateid *stp = NULL;
2058         struct nfs4_delegation *dp = NULL;
2059         stateid_t *stidp;
2060         struct inode *ino = current_fh->fh_dentry->d_inode;
2061         int status;
2062
2063         dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2064                 stateid->si_boot, stateid->si_stateownerid, 
2065                 stateid->si_fileid, stateid->si_generation); 
2066         if (filpp)
2067                 *filpp = NULL;
2068
2069         if (io_during_grace_disallowed(ino, flags))
2070                 return nfserr_grace;
2071
2072         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2073                 return check_special_stateids(current_fh, stateid, flags);
2074
2075         /* STALE STATEID */
2076         status = nfserr_stale_stateid;
2077         if (STALE_STATEID(stateid)) 
2078                 goto out;
2079
2080         /* BAD STATEID */
2081         status = nfserr_bad_stateid;
2082         if (!stateid->si_fileid) { /* delegation stateid */
2083                 if(!(dp = find_delegation_stateid(ino, stateid))) {
2084                         dprintk("NFSD: delegation stateid not found\n");
2085                         if (nfs4_in_grace())
2086                                 status = nfserr_grace;
2087                         goto out;
2088                 }
2089                 stidp = &dp->dl_stateid;
2090         } else { /* open or lock stateid */
2091                 if (!(stp = find_stateid(stateid, flags))) {
2092                         dprintk("NFSD: open or lock stateid not found\n");
2093                         if (nfs4_in_grace())
2094                                 status = nfserr_grace;
2095                         goto out;
2096                 }
2097                 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2098                         goto out;
2099                 if (!stp->st_stateowner->so_confirmed)
2100                         goto out;
2101                 stidp = &stp->st_stateid;
2102         }
2103         if (stateid->si_generation > stidp->si_generation)
2104                 goto out;
2105
2106         /* OLD STATEID */
2107         status = nfserr_old_stateid;
2108         if (stateid->si_generation < stidp->si_generation)
2109                 goto out;
2110         if (stp) {
2111                 if ((status = nfs4_check_openmode(stp,flags)))
2112                         goto out;
2113                 renew_client(stp->st_stateowner->so_client);
2114                 if (filpp)
2115                         *filpp = stp->st_vfs_file;
2116         } else if (dp) {
2117                 if ((status = nfs4_check_delegmode(dp, flags)))
2118                         goto out;
2119                 renew_client(dp->dl_client);
2120                 if (flags & DELEG_RET)
2121                         unhash_delegation(dp);
2122                 if (filpp)
2123                         *filpp = dp->dl_vfs_file;
2124         }
2125         status = nfs_ok;
2126 out:
2127         return status;
2128 }
2129
2130
2131 /* 
2132  * Checks for sequence id mutating operations. 
2133  */
2134 int
2135 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)
2136 {
2137         int status;
2138         struct nfs4_stateid *stp;
2139         struct nfs4_stateowner *sop;
2140
2141         dprintk("NFSD: preprocess_seqid_op: seqid=%d " 
2142                         "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2143                 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2144                 stateid->si_generation);
2145                                 
2146         *stpp = NULL;
2147         *sopp = NULL;
2148
2149         status = nfserr_bad_stateid;
2150         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2151                 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2152                 goto out;
2153         }
2154
2155         status = nfserr_stale_stateid;
2156         if (STALE_STATEID(stateid))
2157                 goto out;
2158         /*
2159         * We return BAD_STATEID if filehandle doesn't match stateid, 
2160         * the confirmed flag is incorrecly set, or the generation 
2161         * number is incorrect.  
2162         * If there is no entry in the openfile table for this id, 
2163         * we can't always return BAD_STATEID;
2164         * this might be a retransmitted CLOSE which has arrived after 
2165         * the openfile has been released.
2166         */
2167         if (!(stp = find_stateid(stateid, flags)))
2168                 goto no_nfs4_stateid;
2169
2170         status = nfserr_bad_stateid;
2171
2172         /* for new lock stateowners:
2173          * check that the lock->v.new.open_stateid
2174          * refers to an open stateowner
2175          *
2176          * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2177          * as the open_stateid->st_stateowner->so_client->clientid
2178          */
2179         if (lockclid) {
2180                 struct nfs4_stateowner *sop = stp->st_stateowner;
2181                 struct nfs4_client *clp = sop->so_client;
2182
2183                 if (!sop->so_is_open_owner)
2184                         goto out;
2185                 if (!cmp_clid(&clp->cl_clientid, lockclid))
2186                         goto out;
2187         }
2188
2189         if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2190                 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2191                 goto out;
2192         }
2193
2194         *stpp = stp;
2195         *sopp = sop = stp->st_stateowner;
2196
2197         /*
2198         *  We now validate the seqid and stateid generation numbers.
2199         *  For the moment, we ignore the possibility of 
2200         *  generation number wraparound.
2201         */
2202         if (seqid != sop->so_seqid + 1)
2203                 goto check_replay;
2204
2205         if (sop->so_confirmed) {
2206                 if (flags & CONFIRM) {
2207                         printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2208                         goto out;
2209                 }
2210         }
2211         else {
2212                 if (!(flags & CONFIRM)) {
2213                         printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2214                         goto out;
2215                 }
2216         }
2217         if (stateid->si_generation > stp->st_stateid.si_generation) {
2218                 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2219                 goto out;
2220         }
2221
2222         status = nfserr_old_stateid;
2223         if (stateid->si_generation < stp->st_stateid.si_generation) {
2224                 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2225                 goto out;
2226         }
2227         /* XXX renew the client lease here */
2228         status = nfs_ok;
2229
2230 out:
2231         return status;
2232
2233 no_nfs4_stateid:
2234
2235         /*
2236         * We determine whether this is a bad stateid or a replay, 
2237         * starting by trying to look up the stateowner.
2238         * If stateowner is not found - stateid is bad.
2239         */
2240         if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2241                 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2242                 status = nfserr_bad_stateid;
2243                 goto out;
2244         }
2245         *sopp = sop;
2246
2247 check_replay:
2248         if (seqid == sop->so_seqid) {
2249                 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2250                 /* indicate replay to calling function */
2251                 status = NFSERR_REPLAY_ME;
2252         } else  {
2253                 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2254
2255                 *sopp = NULL;
2256                 status = nfserr_bad_seqid;
2257         }
2258         goto out;
2259 }
2260
2261 int
2262 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2263 {
2264         int status;
2265         struct nfs4_stateowner *sop;
2266         struct nfs4_stateid *stp;
2267
2268         dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2269                         (int)current_fh->fh_dentry->d_name.len,
2270                         current_fh->fh_dentry->d_name.name);
2271
2272         if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2273                 goto out;
2274
2275         nfs4_lock_state();
2276
2277         if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2278                                         &oc->oc_req_stateid,
2279                                         CHECK_FH | CONFIRM | OPEN_STATE,
2280                                         &oc->oc_stateowner, &stp, NULL)))
2281                 goto out; 
2282
2283         sop = oc->oc_stateowner;
2284         sop->so_confirmed = 1;
2285         update_stateid(&stp->st_stateid);
2286         memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2287         dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d " 
2288                 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2289                          stp->st_stateid.si_boot,
2290                          stp->st_stateid.si_stateownerid,
2291                          stp->st_stateid.si_fileid,
2292                          stp->st_stateid.si_generation);
2293 out:
2294         if (oc->oc_stateowner)
2295                 nfs4_get_stateowner(oc->oc_stateowner);
2296         nfs4_unlock_state();
2297         return status;
2298 }
2299
2300
2301 /*
2302  * unset all bits in union bitmap (bmap) that
2303  * do not exist in share (from successful OPEN_DOWNGRADE)
2304  */
2305 static void
2306 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2307 {
2308         int i;
2309         for (i = 1; i < 4; i++) {
2310                 if ((i & access) != i)
2311                         __clear_bit(i, bmap);
2312         }
2313 }
2314
2315 static void
2316 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2317 {
2318         int i;
2319         for (i = 0; i < 4; i++) {
2320                 if ((i & deny) != i)
2321                         __clear_bit(i, bmap);
2322         }
2323 }
2324
2325 int
2326 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2327 {
2328         int status;
2329         struct nfs4_stateid *stp;
2330         unsigned int share_access;
2331
2332         dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", 
2333                         (int)current_fh->fh_dentry->d_name.len,
2334                         current_fh->fh_dentry->d_name.name);
2335
2336         if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2337                 return nfserr_inval;
2338
2339         nfs4_lock_state();
2340         if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid, 
2341                                         &od->od_stateid, 
2342                                         CHECK_FH | OPEN_STATE, 
2343                                         &od->od_stateowner, &stp, NULL)))
2344                 goto out; 
2345
2346         status = nfserr_inval;
2347         if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2348                 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2349                         stp->st_access_bmap, od->od_share_access);
2350                 goto out;
2351         }
2352         if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2353                 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2354                         stp->st_deny_bmap, od->od_share_deny);
2355                 goto out;
2356         }
2357         set_access(&share_access, stp->st_access_bmap);
2358         nfs4_file_downgrade(stp->st_vfs_file,
2359                             share_access & ~od->od_share_access);
2360
2361         reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2362         reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2363
2364         update_stateid(&stp->st_stateid);
2365         memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2366         status = nfs_ok;
2367 out:
2368         if (od->od_stateowner)
2369                 nfs4_get_stateowner(od->od_stateowner);
2370         nfs4_unlock_state();
2371         return status;
2372 }
2373
2374 /*
2375  * nfs4_unlock_state() called after encode
2376  */
2377 int
2378 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2379 {
2380         int status;
2381         struct nfs4_stateid *stp;
2382
2383         dprintk("NFSD: nfsd4_close on file %.*s\n", 
2384                         (int)current_fh->fh_dentry->d_name.len,
2385                         current_fh->fh_dentry->d_name.name);
2386
2387         nfs4_lock_state();
2388         /* check close_lru for replay */
2389         if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid, 
2390                                         &close->cl_stateid, 
2391                                         CHECK_FH | OPEN_STATE | CLOSE_STATE,
2392                                         &close->cl_stateowner, &stp, NULL)))
2393                 goto out; 
2394         /*
2395         *  Return success, but first update the stateid.
2396         */
2397         status = nfs_ok;
2398         update_stateid(&stp->st_stateid);
2399         memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2400
2401         /* release_state_owner() calls nfsd_close() if needed */
2402         release_state_owner(stp, OPEN_STATE);
2403 out:
2404         if (close->cl_stateowner)
2405                 nfs4_get_stateowner(close->cl_stateowner);
2406         nfs4_unlock_state();
2407         return status;
2408 }
2409
2410 int
2411 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2412 {
2413         int status;
2414
2415         if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2416                 goto out;
2417
2418         nfs4_lock_state();
2419         status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2420         nfs4_unlock_state();
2421 out:
2422         return status;
2423 }
2424
2425
2426 /* 
2427  * Lock owner state (byte-range locks)
2428  */
2429 #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
2430 #define LOCK_HASH_BITS              8
2431 #define LOCK_HASH_SIZE             (1 << LOCK_HASH_BITS)
2432 #define LOCK_HASH_MASK             (LOCK_HASH_SIZE - 1)
2433
2434 #define lockownerid_hashval(id) \
2435         ((id) & LOCK_HASH_MASK)
2436
2437 static inline unsigned int
2438 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2439                 struct xdr_netobj *ownername)
2440 {
2441         return (file_hashval(inode) + cl_id
2442                         + opaque_hashval(ownername->data, ownername->len))
2443                 & LOCK_HASH_MASK;
2444 }
2445
2446 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2447 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2448 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2449
2450 struct nfs4_stateid *
2451 find_stateid(stateid_t *stid, int flags)
2452 {
2453         struct nfs4_stateid *local = NULL;
2454         u32 st_id = stid->si_stateownerid;
2455         u32 f_id = stid->si_fileid;
2456         unsigned int hashval;
2457
2458         dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2459         if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2460                 hashval = stateid_hashval(st_id, f_id);
2461                 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2462                         if ((local->st_stateid.si_stateownerid == st_id) &&
2463                             (local->st_stateid.si_fileid == f_id))
2464                                 return local;
2465                 }
2466         } 
2467         if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2468                 hashval = stateid_hashval(st_id, f_id);
2469                 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2470                         if ((local->st_stateid.si_stateownerid == st_id) &&
2471                             (local->st_stateid.si_fileid == f_id))
2472                                 return local;
2473                 }
2474         } else
2475                 printk("NFSD: find_stateid: ERROR: no state flag\n");
2476         return NULL;
2477 }
2478
2479 static struct nfs4_delegation *
2480 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2481 {
2482         struct nfs4_file *fp;
2483         struct nfs4_delegation *dl;
2484
2485         dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2486                     stid->si_boot, stid->si_stateownerid,
2487                     stid->si_fileid, stid->si_generation);
2488
2489         fp = find_file(ino);
2490         if (!fp)
2491                 return NULL;
2492         dl = find_delegation_file(fp, stid);
2493         put_nfs4_file(fp);
2494         return dl;
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         stp = nfs4_alloc_stateid();
2631         if (stp == 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_stateids);
2639         list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2640         stp->st_stateowner = sop;
2641         get_nfs4_file(fp);
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         return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3159 }
3160
3161
3162 /* 
3163  * Start and stop routines
3164  */
3165
3166 static void
3167 __nfs4_state_init(void)
3168 {
3169         int i;
3170         time_t grace_time;
3171
3172         if (!nfs4_reclaim_init) {
3173                 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3174                         INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3175                 reclaim_str_hashtbl_size = 0;
3176                 nfs4_reclaim_init = 1;
3177         }
3178         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3179                 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3180                 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3181                 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3182                 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3183         }
3184         for (i = 0; i < FILE_HASH_SIZE; i++) {
3185                 INIT_LIST_HEAD(&file_hashtbl[i]);
3186         }
3187         for (i = 0; i < OWNER_HASH_SIZE; i++) {
3188                 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3189                 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3190         }
3191         for (i = 0; i < STATEID_HASH_SIZE; i++) {
3192                 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3193                 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3194         }
3195         for (i = 0; i < LOCK_HASH_SIZE; i++) {
3196                 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3197                 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3198         }
3199         memset(&zerostateid, 0, sizeof(stateid_t));
3200         memset(&onestateid, ~0, sizeof(stateid_t));
3201
3202         INIT_LIST_HEAD(&close_lru);
3203         INIT_LIST_HEAD(&client_lru);
3204         INIT_LIST_HEAD(&del_recall_lru);
3205         spin_lock_init(&recall_lock);
3206         boot_time = get_seconds();
3207         grace_time = max(old_lease_time, lease_time);
3208         if (reclaim_str_hashtbl_size == 0)
3209                 grace_time = 0;
3210         if (grace_time)
3211                 printk("NFSD: starting %ld-second grace period\n", grace_time);
3212         grace_end = boot_time + grace_time;
3213         INIT_WORK(&laundromat_work,laundromat_main, NULL);
3214         schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3215 }
3216
3217 int
3218 nfs4_state_init(void)
3219 {
3220         int status;
3221
3222         if (nfs4_init)
3223                 return 0;
3224         status = nfsd4_init_slabs();
3225         if (status)
3226                 return status;
3227         __nfs4_state_init();
3228         nfs4_init = 1;
3229         return 0;
3230 }
3231
3232 int
3233 nfs4_in_grace(void)
3234 {
3235         return get_seconds() < grace_end;
3236 }
3237
3238 void
3239 set_no_grace(void)
3240 {
3241         printk("NFSD: ERROR in reboot recovery.  State reclaims will fail.\n");
3242         grace_end = get_seconds();
3243 }
3244
3245 time_t
3246 nfs4_lease_time(void)
3247 {
3248         return lease_time;
3249 }
3250
3251 static void
3252 __nfs4_state_shutdown(void)
3253 {
3254         int i;
3255         struct nfs4_client *clp = NULL;
3256         struct nfs4_delegation *dp = NULL;
3257         struct nfs4_stateowner *sop = NULL;
3258         struct list_head *pos, *next, reaplist;
3259
3260         list_for_each_safe(pos, next, &close_lru) {
3261                 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3262                 list_del(&sop->so_close_lru);
3263                 nfs4_put_stateowner(sop);
3264         }
3265
3266         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3267                 while (!list_empty(&conf_id_hashtbl[i])) {
3268                         clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3269                         expire_client(clp);
3270                 }
3271                 while (!list_empty(&unconf_str_hashtbl[i])) {
3272                         clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3273                         expire_client(clp);
3274                 }
3275         }
3276         INIT_LIST_HEAD(&reaplist);
3277         spin_lock(&recall_lock);
3278         list_for_each_safe(pos, next, &del_recall_lru) {
3279                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3280                 list_move(&dp->dl_recall_lru, &reaplist);
3281         }
3282         spin_unlock(&recall_lock);
3283         list_for_each_safe(pos, next, &reaplist) {
3284                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3285                 list_del_init(&dp->dl_recall_lru);
3286                 unhash_delegation(dp);
3287         }
3288
3289         cancel_delayed_work(&laundromat_work);
3290         flush_scheduled_work();
3291         nfs4_init = 0;
3292 }
3293
3294 void
3295 nfs4_state_shutdown(void)
3296 {
3297         nfs4_lock_state();
3298         nfs4_release_reclaim();
3299         __nfs4_state_shutdown();
3300         nfsd4_free_slabs();
3301         nfs4_unlock_state();
3302 }
3303
3304 /*
3305  * Called when leasetime is changed.
3306  *
3307  * if nfsd is not started, simply set the global lease.
3308  *
3309  * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3310  * e.g: boot_time is reset, existing nfs4_client structs are
3311  * used to fill reclaim_str_hashtbl, then all state (except for the
3312  * reclaim_str_hashtbl) is re-initialized.
3313  *
3314  * if the old lease time is greater than the new lease time, the grace
3315  * period needs to be set to the old lease time to allow clients to reclaim
3316  * their state. XXX - we may want to set the grace period == lease time
3317  * after an initial grace period == old lease time
3318  *
3319  * if an error occurs in this process, the new lease is set, but the server
3320  * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3321  * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3322  *
3323  * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3324  * OPEN and LOCK reclaims.
3325  */
3326 void
3327 nfs4_reset_lease(time_t leasetime)
3328 {
3329         struct nfs4_client *clp;
3330         int i;
3331
3332         printk("NFSD: New leasetime %ld\n",leasetime);
3333         if (!nfs4_init)
3334                 return;
3335         nfs4_lock_state();
3336         old_lease_time = lease_time;
3337         lease_time = leasetime;
3338
3339         nfs4_release_reclaim();
3340
3341         /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3342         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3343                 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3344                         if (!nfs4_client_to_reclaim(clp->cl_name.data,
3345                                                 clp->cl_name.len)) {
3346                                 nfs4_release_reclaim();
3347                                 goto init_state;
3348                         }
3349                 }
3350         }
3351 init_state:
3352         __nfs4_state_shutdown();
3353         __nfs4_state_init();
3354         nfs4_unlock_state();
3355 }
3356