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