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