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