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