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