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