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