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