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