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