]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/nfsd/nfs4state.c
3d893645cc69eca8abe77b8b19d0044d43488115
[karo-tx-linux.git] / fs / nfsd / nfs4state.c
1 /*
2 *  Copyright (c) 2001 The Regents of the University of Michigan.
3 *  All rights reserved.
4 *
5 *  Kendrick Smith <kmsmith@umich.edu>
6 *  Andy Adamson <kandros@umich.edu>
7 *
8 *  Redistribution and use in source and binary forms, with or without
9 *  modification, are permitted provided that the following conditions
10 *  are met:
11 *
12 *  1. Redistributions of source code must retain the above copyright
13 *     notice, this list of conditions and the following disclaimer.
14 *  2. Redistributions in binary form must reproduce the above copyright
15 *     notice, this list of conditions and the following disclaimer in the
16 *     documentation and/or other materials provided with the distribution.
17 *  3. Neither the name of the University nor the names of its
18 *     contributors may be used to endorse or promote products derived
19 *     from this software without specific prior written permission.
20 *
21 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include <linux/file.h>
36 #include <linux/fs.h>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/sunrpc/svcauth_gss.h>
41 #include <linux/sunrpc/clnt.h>
42 #include "xdr4.h"
43 #include "vfs.h"
44
45 #define NFSDDBG_FACILITY                NFSDDBG_PROC
46
47 /* Globals */
48 time_t nfsd4_lease = 90;     /* default lease time */
49 time_t nfsd4_grace = 90;
50 static time_t boot_time;
51 static u32 current_ownerid = 1;
52 static u32 current_fileid = 1;
53 static u32 current_delegid = 1;
54 static stateid_t zerostateid;             /* bits all 0 */
55 static stateid_t onestateid;              /* bits all 1 */
56 static u64 current_sessionid = 1;
57
58 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
59 #define ONE_STATEID(stateid)  (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
60
61 /* forward declarations */
62 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
63 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
64 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
65 static void nfs4_set_recdir(char *recdir);
66
67 /* Locking: */
68
69 /* Currently used for almost all code touching nfsv4 state: */
70 static DEFINE_MUTEX(client_mutex);
71
72 /*
73  * Currently used for the del_recall_lru and file hash table.  In an
74  * effort to decrease the scope of the client_mutex, this spinlock may
75  * eventually cover more:
76  */
77 static DEFINE_SPINLOCK(recall_lock);
78
79 static struct kmem_cache *stateowner_slab = NULL;
80 static struct kmem_cache *file_slab = NULL;
81 static struct kmem_cache *stateid_slab = NULL;
82 static struct kmem_cache *deleg_slab = NULL;
83
84 void
85 nfs4_lock_state(void)
86 {
87         mutex_lock(&client_mutex);
88 }
89
90 void
91 nfs4_unlock_state(void)
92 {
93         mutex_unlock(&client_mutex);
94 }
95
96 static inline u32
97 opaque_hashval(const void *ptr, int nbytes)
98 {
99         unsigned char *cptr = (unsigned char *) ptr;
100
101         u32 x = 0;
102         while (nbytes--) {
103                 x *= 37;
104                 x += *cptr++;
105         }
106         return x;
107 }
108
109 static struct list_head del_recall_lru;
110
111 static inline void
112 put_nfs4_file(struct nfs4_file *fi)
113 {
114         if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
115                 list_del(&fi->fi_hash);
116                 spin_unlock(&recall_lock);
117                 iput(fi->fi_inode);
118                 kmem_cache_free(file_slab, fi);
119         }
120 }
121
122 static inline void
123 get_nfs4_file(struct nfs4_file *fi)
124 {
125         atomic_inc(&fi->fi_ref);
126 }
127
128 static int num_delegations;
129 unsigned int max_delegations;
130
131 /*
132  * Open owner state (share locks)
133  */
134
135 /* hash tables for nfs4_stateowner */
136 #define OWNER_HASH_BITS              8
137 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
138 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
139
140 #define ownerid_hashval(id) \
141         ((id) & OWNER_HASH_MASK)
142 #define ownerstr_hashval(clientid, ownername) \
143         (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
144
145 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
146 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
147
148 /* hash table for nfs4_file */
149 #define FILE_HASH_BITS                   8
150 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
151
152 /* hash table for (open)nfs4_stateid */
153 #define STATEID_HASH_BITS              10
154 #define STATEID_HASH_SIZE              (1 << STATEID_HASH_BITS)
155 #define STATEID_HASH_MASK              (STATEID_HASH_SIZE - 1)
156
157 #define file_hashval(x) \
158         hash_ptr(x, FILE_HASH_BITS)
159 #define stateid_hashval(owner_id, file_id)  \
160         (((owner_id) + (file_id)) & STATEID_HASH_MASK)
161
162 static struct list_head file_hashtbl[FILE_HASH_SIZE];
163 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
164
165 static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
166 {
167         BUG_ON(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
168         atomic_inc(&fp->fi_access[oflag]);
169 }
170
171 static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
172 {
173         if (oflag == O_RDWR) {
174                 __nfs4_file_get_access(fp, O_RDONLY);
175                 __nfs4_file_get_access(fp, O_WRONLY);
176         } else
177                 __nfs4_file_get_access(fp, oflag);
178 }
179
180 static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
181 {
182         if (fp->fi_fds[oflag]) {
183                 fput(fp->fi_fds[oflag]);
184                 fp->fi_fds[oflag] = NULL;
185         }
186 }
187
188 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
189 {
190         if (atomic_dec_and_test(&fp->fi_access[oflag])) {
191                 nfs4_file_put_fd(fp, oflag);
192                 /*
193                  * It's also safe to get rid of the RDWR open *if*
194                  * we no longer have need of the other kind of access
195                  * or if we already have the other kind of open:
196                  */
197                 if (fp->fi_fds[1-oflag]
198                         || atomic_read(&fp->fi_access[1 - oflag]) == 0)
199                         nfs4_file_put_fd(fp, O_RDWR);
200         }
201 }
202
203 static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
204 {
205         if (oflag == O_RDWR) {
206                 __nfs4_file_put_access(fp, O_RDONLY);
207                 __nfs4_file_put_access(fp, O_WRONLY);
208         } else
209                 __nfs4_file_put_access(fp, oflag);
210 }
211
212 static struct nfs4_delegation *
213 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
214 {
215         struct nfs4_delegation *dp;
216         struct nfs4_file *fp = stp->st_file;
217
218         dprintk("NFSD alloc_init_deleg\n");
219         /*
220          * Major work on the lease subsystem (for example, to support
221          * calbacks on stat) will be required before we can support
222          * write delegations properly.
223          */
224         if (type != NFS4_OPEN_DELEGATE_READ)
225                 return NULL;
226         if (fp->fi_had_conflict)
227                 return NULL;
228         if (num_delegations > max_delegations)
229                 return NULL;
230         dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
231         if (dp == NULL)
232                 return dp;
233         num_delegations++;
234         INIT_LIST_HEAD(&dp->dl_perfile);
235         INIT_LIST_HEAD(&dp->dl_perclnt);
236         INIT_LIST_HEAD(&dp->dl_recall_lru);
237         dp->dl_client = clp;
238         get_nfs4_file(fp);
239         dp->dl_file = fp;
240         dp->dl_type = type;
241         dp->dl_stateid.si_boot = boot_time;
242         dp->dl_stateid.si_stateownerid = current_delegid++;
243         dp->dl_stateid.si_fileid = 0;
244         dp->dl_stateid.si_generation = 0;
245         fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
246         dp->dl_time = 0;
247         atomic_set(&dp->dl_count, 1);
248         INIT_WORK(&dp->dl_recall.cb_work, nfsd4_do_callback_rpc);
249         return dp;
250 }
251
252 void
253 nfs4_put_delegation(struct nfs4_delegation *dp)
254 {
255         if (atomic_dec_and_test(&dp->dl_count)) {
256                 dprintk("NFSD: freeing dp %p\n",dp);
257                 put_nfs4_file(dp->dl_file);
258                 kmem_cache_free(deleg_slab, dp);
259                 num_delegations--;
260         }
261 }
262
263 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
264 {
265         if (atomic_dec_and_test(&fp->fi_delegees)) {
266                 vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
267                 fp->fi_lease = NULL;
268                 fput(fp->fi_deleg_file);
269                 fp->fi_deleg_file = NULL;
270         }
271 }
272
273 /* Called under the state lock. */
274 static void
275 unhash_delegation(struct nfs4_delegation *dp)
276 {
277         list_del_init(&dp->dl_perclnt);
278         spin_lock(&recall_lock);
279         list_del_init(&dp->dl_perfile);
280         list_del_init(&dp->dl_recall_lru);
281         spin_unlock(&recall_lock);
282         nfs4_put_deleg_lease(dp->dl_file);
283         nfs4_put_delegation(dp);
284 }
285
286 /* 
287  * SETCLIENTID state 
288  */
289
290 /* client_lock protects the client lru list and session hash table */
291 static DEFINE_SPINLOCK(client_lock);
292
293 /* Hash tables for nfs4_clientid state */
294 #define CLIENT_HASH_BITS                 4
295 #define CLIENT_HASH_SIZE                (1 << CLIENT_HASH_BITS)
296 #define CLIENT_HASH_MASK                (CLIENT_HASH_SIZE - 1)
297
298 #define clientid_hashval(id) \
299         ((id) & CLIENT_HASH_MASK)
300 #define clientstr_hashval(name) \
301         (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
302 /*
303  * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
304  * used in reboot/reset lease grace period processing
305  *
306  * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
307  * setclientid_confirmed info. 
308  *
309  * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed 
310  * setclientid info.
311  *
312  * client_lru holds client queue ordered by nfs4_client.cl_time
313  * for lease renewal.
314  *
315  * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
316  * for last close replay.
317  */
318 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
319 static int reclaim_str_hashtbl_size = 0;
320 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
321 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
322 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
323 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
324 static struct list_head client_lru;
325 static struct list_head close_lru;
326
327 /*
328  * We store the NONE, READ, WRITE, and BOTH bits separately in the
329  * st_{access,deny}_bmap field of the stateid, in order to track not
330  * only what share bits are currently in force, but also what
331  * combinations of share bits previous opens have used.  This allows us
332  * to enforce the recommendation of rfc 3530 14.2.19 that the server
333  * return an error if the client attempt to downgrade to a combination
334  * of share bits not explicable by closing some of its previous opens.
335  *
336  * XXX: This enforcement is actually incomplete, since we don't keep
337  * track of access/deny bit combinations; so, e.g., we allow:
338  *
339  *      OPEN allow read, deny write
340  *      OPEN allow both, deny none
341  *      DOWNGRADE allow read, deny none
342  *
343  * which we should reject.
344  */
345 static void
346 set_access(unsigned int *access, unsigned long bmap) {
347         int i;
348
349         *access = 0;
350         for (i = 1; i < 4; i++) {
351                 if (test_bit(i, &bmap))
352                         *access |= i;
353         }
354 }
355
356 static void
357 set_deny(unsigned int *deny, unsigned long bmap) {
358         int i;
359
360         *deny = 0;
361         for (i = 0; i < 4; i++) {
362                 if (test_bit(i, &bmap))
363                         *deny |= i ;
364         }
365 }
366
367 static int
368 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
369         unsigned int access, deny;
370
371         set_access(&access, stp->st_access_bmap);
372         set_deny(&deny, stp->st_deny_bmap);
373         if ((access & open->op_share_deny) || (deny & open->op_share_access))
374                 return 0;
375         return 1;
376 }
377
378 static int nfs4_access_to_omode(u32 access)
379 {
380         switch (access & NFS4_SHARE_ACCESS_BOTH) {
381         case NFS4_SHARE_ACCESS_READ:
382                 return O_RDONLY;
383         case NFS4_SHARE_ACCESS_WRITE:
384                 return O_WRONLY;
385         case NFS4_SHARE_ACCESS_BOTH:
386                 return O_RDWR;
387         }
388         BUG();
389 }
390
391 static void unhash_generic_stateid(struct nfs4_stateid *stp)
392 {
393         list_del(&stp->st_hash);
394         list_del(&stp->st_perfile);
395         list_del(&stp->st_perstateowner);
396 }
397
398 static void free_generic_stateid(struct nfs4_stateid *stp)
399 {
400         int i;
401
402         if (stp->st_access_bmap) {
403                 for (i = 1; i < 4; i++) {
404                         if (test_bit(i, &stp->st_access_bmap))
405                                 nfs4_file_put_access(stp->st_file,
406                                                 nfs4_access_to_omode(i));
407                 }
408         }
409         put_nfs4_file(stp->st_file);
410         kmem_cache_free(stateid_slab, stp);
411 }
412
413 static void release_lock_stateid(struct nfs4_stateid *stp)
414 {
415         struct file *file;
416
417         unhash_generic_stateid(stp);
418         file = find_any_file(stp->st_file);
419         if (file)
420                 locks_remove_posix(file, (fl_owner_t)stp->st_stateowner);
421         free_generic_stateid(stp);
422 }
423
424 static void unhash_lockowner(struct nfs4_stateowner *sop)
425 {
426         struct nfs4_stateid *stp;
427
428         list_del(&sop->so_idhash);
429         list_del(&sop->so_strhash);
430         list_del(&sop->so_perstateid);
431         while (!list_empty(&sop->so_stateids)) {
432                 stp = list_first_entry(&sop->so_stateids,
433                                 struct nfs4_stateid, st_perstateowner);
434                 release_lock_stateid(stp);
435         }
436 }
437
438 static void release_lockowner(struct nfs4_stateowner *sop)
439 {
440         unhash_lockowner(sop);
441         nfs4_put_stateowner(sop);
442 }
443
444 static void
445 release_stateid_lockowners(struct nfs4_stateid *open_stp)
446 {
447         struct nfs4_stateowner *lock_sop;
448
449         while (!list_empty(&open_stp->st_lockowners)) {
450                 lock_sop = list_entry(open_stp->st_lockowners.next,
451                                 struct nfs4_stateowner, so_perstateid);
452                 /* list_del(&open_stp->st_lockowners);  */
453                 BUG_ON(lock_sop->so_is_open_owner);
454                 release_lockowner(lock_sop);
455         }
456 }
457
458 static void release_open_stateid(struct nfs4_stateid *stp)
459 {
460         unhash_generic_stateid(stp);
461         release_stateid_lockowners(stp);
462         free_generic_stateid(stp);
463 }
464
465 static void unhash_openowner(struct nfs4_stateowner *sop)
466 {
467         struct nfs4_stateid *stp;
468
469         list_del(&sop->so_idhash);
470         list_del(&sop->so_strhash);
471         list_del(&sop->so_perclient);
472         list_del(&sop->so_perstateid); /* XXX: necessary? */
473         while (!list_empty(&sop->so_stateids)) {
474                 stp = list_first_entry(&sop->so_stateids,
475                                 struct nfs4_stateid, st_perstateowner);
476                 release_open_stateid(stp);
477         }
478 }
479
480 static void release_openowner(struct nfs4_stateowner *sop)
481 {
482         unhash_openowner(sop);
483         list_del(&sop->so_close_lru);
484         nfs4_put_stateowner(sop);
485 }
486
487 #define SESSION_HASH_SIZE       512
488 static struct list_head sessionid_hashtbl[SESSION_HASH_SIZE];
489
490 static inline int
491 hash_sessionid(struct nfs4_sessionid *sessionid)
492 {
493         struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
494
495         return sid->sequence % SESSION_HASH_SIZE;
496 }
497
498 static inline void
499 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
500 {
501         u32 *ptr = (u32 *)(&sessionid->data[0]);
502         dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
503 }
504
505 static void
506 gen_sessionid(struct nfsd4_session *ses)
507 {
508         struct nfs4_client *clp = ses->se_client;
509         struct nfsd4_sessionid *sid;
510
511         sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
512         sid->clientid = clp->cl_clientid;
513         sid->sequence = current_sessionid++;
514         sid->reserved = 0;
515 }
516
517 /*
518  * The protocol defines ca_maxresponssize_cached to include the size of
519  * the rpc header, but all we need to cache is the data starting after
520  * the end of the initial SEQUENCE operation--the rest we regenerate
521  * each time.  Therefore we can advertise a ca_maxresponssize_cached
522  * value that is the number of bytes in our cache plus a few additional
523  * bytes.  In order to stay on the safe side, and not promise more than
524  * we can cache, those additional bytes must be the minimum possible: 24
525  * bytes of rpc header (xid through accept state, with AUTH_NULL
526  * verifier), 12 for the compound header (with zero-length tag), and 44
527  * for the SEQUENCE op response:
528  */
529 #define NFSD_MIN_HDR_SEQ_SZ  (24 + 12 + 44)
530
531 static void
532 free_session_slots(struct nfsd4_session *ses)
533 {
534         int i;
535
536         for (i = 0; i < ses->se_fchannel.maxreqs; i++)
537                 kfree(ses->se_slots[i]);
538 }
539
540 /*
541  * We don't actually need to cache the rpc and session headers, so we
542  * can allocate a little less for each slot:
543  */
544 static inline int slot_bytes(struct nfsd4_channel_attrs *ca)
545 {
546         return ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
547 }
548
549 static int nfsd4_sanitize_slot_size(u32 size)
550 {
551         size -= NFSD_MIN_HDR_SEQ_SZ; /* We don't cache the rpc header */
552         size = min_t(u32, size, NFSD_SLOT_CACHE_SIZE);
553
554         return size;
555 }
556
557 /*
558  * XXX: If we run out of reserved DRC memory we could (up to a point)
559  * re-negotiate active sessions and reduce their slot usage to make
560  * rooom for new connections. For now we just fail the create session.
561  */
562 static int nfsd4_get_drc_mem(int slotsize, u32 num)
563 {
564         int avail;
565
566         num = min_t(u32, num, NFSD_MAX_SLOTS_PER_SESSION);
567
568         spin_lock(&nfsd_drc_lock);
569         avail = min_t(int, NFSD_MAX_MEM_PER_SESSION,
570                         nfsd_drc_max_mem - nfsd_drc_mem_used);
571         num = min_t(int, num, avail / slotsize);
572         nfsd_drc_mem_used += num * slotsize;
573         spin_unlock(&nfsd_drc_lock);
574
575         return num;
576 }
577
578 static void nfsd4_put_drc_mem(int slotsize, int num)
579 {
580         spin_lock(&nfsd_drc_lock);
581         nfsd_drc_mem_used -= slotsize * num;
582         spin_unlock(&nfsd_drc_lock);
583 }
584
585 static struct nfsd4_session *alloc_session(int slotsize, int numslots)
586 {
587         struct nfsd4_session *new;
588         int mem, i;
589
590         BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
591                         + sizeof(struct nfsd4_session) > PAGE_SIZE);
592         mem = numslots * sizeof(struct nfsd4_slot *);
593
594         new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
595         if (!new)
596                 return NULL;
597         /* allocate each struct nfsd4_slot and data cache in one piece */
598         for (i = 0; i < numslots; i++) {
599                 mem = sizeof(struct nfsd4_slot) + slotsize;
600                 new->se_slots[i] = kzalloc(mem, GFP_KERNEL);
601                 if (!new->se_slots[i])
602                         goto out_free;
603         }
604         return new;
605 out_free:
606         while (i--)
607                 kfree(new->se_slots[i]);
608         kfree(new);
609         return NULL;
610 }
611
612 static void init_forechannel_attrs(struct nfsd4_channel_attrs *new, struct nfsd4_channel_attrs *req, int numslots, int slotsize)
613 {
614         u32 maxrpc = nfsd_serv->sv_max_mesg;
615
616         new->maxreqs = numslots;
617         new->maxresp_cached = min_t(u32, req->maxresp_cached,
618                                         slotsize + NFSD_MIN_HDR_SEQ_SZ);
619         new->maxreq_sz = min_t(u32, req->maxreq_sz, maxrpc);
620         new->maxresp_sz = min_t(u32, req->maxresp_sz, maxrpc);
621         new->maxops = min_t(u32, req->maxops, NFSD_MAX_OPS_PER_COMPOUND);
622 }
623
624 static void free_conn(struct nfsd4_conn *c)
625 {
626         svc_xprt_put(c->cn_xprt);
627         kfree(c);
628 }
629
630 static void nfsd4_conn_lost(struct svc_xpt_user *u)
631 {
632         struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
633         struct nfs4_client *clp = c->cn_session->se_client;
634
635         spin_lock(&clp->cl_lock);
636         if (!list_empty(&c->cn_persession)) {
637                 list_del(&c->cn_persession);
638                 free_conn(c);
639         }
640         spin_unlock(&clp->cl_lock);
641         nfsd4_probe_callback(clp);
642 }
643
644 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
645 {
646         struct nfsd4_conn *conn;
647
648         conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
649         if (!conn)
650                 return NULL;
651         svc_xprt_get(rqstp->rq_xprt);
652         conn->cn_xprt = rqstp->rq_xprt;
653         conn->cn_flags = flags;
654         INIT_LIST_HEAD(&conn->cn_xpt_user.list);
655         return conn;
656 }
657
658 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
659 {
660         conn->cn_session = ses;
661         list_add(&conn->cn_persession, &ses->se_conns);
662 }
663
664 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
665 {
666         struct nfs4_client *clp = ses->se_client;
667
668         spin_lock(&clp->cl_lock);
669         __nfsd4_hash_conn(conn, ses);
670         spin_unlock(&clp->cl_lock);
671 }
672
673 static int nfsd4_register_conn(struct nfsd4_conn *conn)
674 {
675         conn->cn_xpt_user.callback = nfsd4_conn_lost;
676         return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
677 }
678
679 static __be32 nfsd4_new_conn(struct svc_rqst *rqstp, struct nfsd4_session *ses, u32 dir)
680 {
681         struct nfsd4_conn *conn;
682         int ret;
683
684         conn = alloc_conn(rqstp, dir);
685         if (!conn)
686                 return nfserr_jukebox;
687         nfsd4_hash_conn(conn, ses);
688         ret = nfsd4_register_conn(conn);
689         if (ret)
690                 /* oops; xprt is already down: */
691                 nfsd4_conn_lost(&conn->cn_xpt_user);
692         return nfs_ok;
693 }
694
695 static __be32 nfsd4_new_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_session *ses)
696 {
697         u32 dir = NFS4_CDFC4_FORE;
698
699         if (ses->se_flags & SESSION4_BACK_CHAN)
700                 dir |= NFS4_CDFC4_BACK;
701
702         return nfsd4_new_conn(rqstp, ses, dir);
703 }
704
705 /* must be called under client_lock */
706 static void nfsd4_del_conns(struct nfsd4_session *s)
707 {
708         struct nfs4_client *clp = s->se_client;
709         struct nfsd4_conn *c;
710
711         spin_lock(&clp->cl_lock);
712         while (!list_empty(&s->se_conns)) {
713                 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
714                 list_del_init(&c->cn_persession);
715                 spin_unlock(&clp->cl_lock);
716
717                 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
718                 free_conn(c);
719
720                 spin_lock(&clp->cl_lock);
721         }
722         spin_unlock(&clp->cl_lock);
723 }
724
725 void free_session(struct kref *kref)
726 {
727         struct nfsd4_session *ses;
728         int mem;
729
730         ses = container_of(kref, struct nfsd4_session, se_ref);
731         nfsd4_del_conns(ses);
732         spin_lock(&nfsd_drc_lock);
733         mem = ses->se_fchannel.maxreqs * slot_bytes(&ses->se_fchannel);
734         nfsd_drc_mem_used -= mem;
735         spin_unlock(&nfsd_drc_lock);
736         free_session_slots(ses);
737         kfree(ses);
738 }
739
740 static struct nfsd4_session *alloc_init_session(struct svc_rqst *rqstp, struct nfs4_client *clp, struct nfsd4_create_session *cses)
741 {
742         struct nfsd4_session *new;
743         struct nfsd4_channel_attrs *fchan = &cses->fore_channel;
744         int numslots, slotsize;
745         int status;
746         int idx;
747
748         /*
749          * Note decreasing slot size below client's request may
750          * make it difficult for client to function correctly, whereas
751          * decreasing the number of slots will (just?) affect
752          * performance.  When short on memory we therefore prefer to
753          * decrease number of slots instead of their size.
754          */
755         slotsize = nfsd4_sanitize_slot_size(fchan->maxresp_cached);
756         numslots = nfsd4_get_drc_mem(slotsize, fchan->maxreqs);
757         if (numslots < 1)
758                 return NULL;
759
760         new = alloc_session(slotsize, numslots);
761         if (!new) {
762                 nfsd4_put_drc_mem(slotsize, fchan->maxreqs);
763                 return NULL;
764         }
765         init_forechannel_attrs(&new->se_fchannel, fchan, numslots, slotsize);
766
767         new->se_client = clp;
768         gen_sessionid(new);
769
770         INIT_LIST_HEAD(&new->se_conns);
771
772         new->se_cb_seq_nr = 1;
773         new->se_flags = cses->flags;
774         new->se_cb_prog = cses->callback_prog;
775         kref_init(&new->se_ref);
776         idx = hash_sessionid(&new->se_sessionid);
777         spin_lock(&client_lock);
778         list_add(&new->se_hash, &sessionid_hashtbl[idx]);
779         spin_lock(&clp->cl_lock);
780         list_add(&new->se_perclnt, &clp->cl_sessions);
781         spin_unlock(&clp->cl_lock);
782         spin_unlock(&client_lock);
783
784         status = nfsd4_new_conn_from_crses(rqstp, new);
785         /* whoops: benny points out, status is ignored! (err, or bogus) */
786         if (status) {
787                 free_session(&new->se_ref);
788                 return NULL;
789         }
790         if (cses->flags & SESSION4_BACK_CHAN) {
791                 struct sockaddr *sa = svc_addr(rqstp);
792                 /*
793                  * This is a little silly; with sessions there's no real
794                  * use for the callback address.  Use the peer address
795                  * as a reasonable default for now, but consider fixing
796                  * the rpc client not to require an address in the
797                  * future:
798                  */
799                 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
800                 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
801         }
802         nfsd4_probe_callback(clp);
803         return new;
804 }
805
806 /* caller must hold client_lock */
807 static struct nfsd4_session *
808 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid)
809 {
810         struct nfsd4_session *elem;
811         int idx;
812
813         dump_sessionid(__func__, sessionid);
814         idx = hash_sessionid(sessionid);
815         /* Search in the appropriate list */
816         list_for_each_entry(elem, &sessionid_hashtbl[idx], se_hash) {
817                 if (!memcmp(elem->se_sessionid.data, sessionid->data,
818                             NFS4_MAX_SESSIONID_LEN)) {
819                         return elem;
820                 }
821         }
822
823         dprintk("%s: session not found\n", __func__);
824         return NULL;
825 }
826
827 /* caller must hold client_lock */
828 static void
829 unhash_session(struct nfsd4_session *ses)
830 {
831         list_del(&ses->se_hash);
832         spin_lock(&ses->se_client->cl_lock);
833         list_del(&ses->se_perclnt);
834         spin_unlock(&ses->se_client->cl_lock);
835 }
836
837 /* must be called under the client_lock */
838 static inline void
839 renew_client_locked(struct nfs4_client *clp)
840 {
841         if (is_client_expired(clp)) {
842                 dprintk("%s: client (clientid %08x/%08x) already expired\n",
843                         __func__,
844                         clp->cl_clientid.cl_boot,
845                         clp->cl_clientid.cl_id);
846                 return;
847         }
848
849         /*
850         * Move client to the end to the LRU list.
851         */
852         dprintk("renewing client (clientid %08x/%08x)\n", 
853                         clp->cl_clientid.cl_boot, 
854                         clp->cl_clientid.cl_id);
855         list_move_tail(&clp->cl_lru, &client_lru);
856         clp->cl_time = get_seconds();
857 }
858
859 static inline void
860 renew_client(struct nfs4_client *clp)
861 {
862         spin_lock(&client_lock);
863         renew_client_locked(clp);
864         spin_unlock(&client_lock);
865 }
866
867 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
868 static int
869 STALE_CLIENTID(clientid_t *clid)
870 {
871         if (clid->cl_boot == boot_time)
872                 return 0;
873         dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
874                 clid->cl_boot, clid->cl_id, boot_time);
875         return 1;
876 }
877
878 /* 
879  * XXX Should we use a slab cache ?
880  * This type of memory management is somewhat inefficient, but we use it
881  * anyway since SETCLIENTID is not a common operation.
882  */
883 static struct nfs4_client *alloc_client(struct xdr_netobj name)
884 {
885         struct nfs4_client *clp;
886
887         clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
888         if (clp == NULL)
889                 return NULL;
890         clp->cl_name.data = kmalloc(name.len, GFP_KERNEL);
891         if (clp->cl_name.data == NULL) {
892                 kfree(clp);
893                 return NULL;
894         }
895         memcpy(clp->cl_name.data, name.data, name.len);
896         clp->cl_name.len = name.len;
897         return clp;
898 }
899
900 static inline void
901 free_client(struct nfs4_client *clp)
902 {
903         while (!list_empty(&clp->cl_sessions)) {
904                 struct nfsd4_session *ses;
905                 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
906                                 se_perclnt);
907                 list_del(&ses->se_perclnt);
908                 nfsd4_put_session(ses);
909         }
910         if (clp->cl_cred.cr_group_info)
911                 put_group_info(clp->cl_cred.cr_group_info);
912         kfree(clp->cl_principal);
913         kfree(clp->cl_name.data);
914         kfree(clp);
915 }
916
917 void
918 release_session_client(struct nfsd4_session *session)
919 {
920         struct nfs4_client *clp = session->se_client;
921
922         if (!atomic_dec_and_lock(&clp->cl_refcount, &client_lock))
923                 return;
924         if (is_client_expired(clp)) {
925                 free_client(clp);
926                 session->se_client = NULL;
927         } else
928                 renew_client_locked(clp);
929         spin_unlock(&client_lock);
930 }
931
932 /* must be called under the client_lock */
933 static inline void
934 unhash_client_locked(struct nfs4_client *clp)
935 {
936         struct nfsd4_session *ses;
937
938         mark_client_expired(clp);
939         list_del(&clp->cl_lru);
940         spin_lock(&clp->cl_lock);
941         list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
942                 list_del_init(&ses->se_hash);
943         spin_unlock(&clp->cl_lock);
944 }
945
946 static void
947 expire_client(struct nfs4_client *clp)
948 {
949         struct nfs4_stateowner *sop;
950         struct nfs4_delegation *dp;
951         struct list_head reaplist;
952
953         INIT_LIST_HEAD(&reaplist);
954         spin_lock(&recall_lock);
955         while (!list_empty(&clp->cl_delegations)) {
956                 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
957                 list_del_init(&dp->dl_perclnt);
958                 list_move(&dp->dl_recall_lru, &reaplist);
959         }
960         spin_unlock(&recall_lock);
961         while (!list_empty(&reaplist)) {
962                 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
963                 list_del_init(&dp->dl_recall_lru);
964                 unhash_delegation(dp);
965         }
966         while (!list_empty(&clp->cl_openowners)) {
967                 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
968                 release_openowner(sop);
969         }
970         nfsd4_shutdown_callback(clp);
971         if (clp->cl_cb_conn.cb_xprt)
972                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
973         list_del(&clp->cl_idhash);
974         list_del(&clp->cl_strhash);
975         spin_lock(&client_lock);
976         unhash_client_locked(clp);
977         if (atomic_read(&clp->cl_refcount) == 0)
978                 free_client(clp);
979         spin_unlock(&client_lock);
980 }
981
982 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
983 {
984         memcpy(target->cl_verifier.data, source->data,
985                         sizeof(target->cl_verifier.data));
986 }
987
988 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
989 {
990         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
991         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
992 }
993
994 static void copy_cred(struct svc_cred *target, struct svc_cred *source)
995 {
996         target->cr_uid = source->cr_uid;
997         target->cr_gid = source->cr_gid;
998         target->cr_group_info = source->cr_group_info;
999         get_group_info(target->cr_group_info);
1000 }
1001
1002 static int same_name(const char *n1, const char *n2)
1003 {
1004         return 0 == memcmp(n1, n2, HEXDIR_LEN);
1005 }
1006
1007 static int
1008 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1009 {
1010         return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1011 }
1012
1013 static int
1014 same_clid(clientid_t *cl1, clientid_t *cl2)
1015 {
1016         return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1017 }
1018
1019 /* XXX what about NGROUP */
1020 static int
1021 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1022 {
1023         return cr1->cr_uid == cr2->cr_uid;
1024 }
1025
1026 static void gen_clid(struct nfs4_client *clp)
1027 {
1028         static u32 current_clientid = 1;
1029
1030         clp->cl_clientid.cl_boot = boot_time;
1031         clp->cl_clientid.cl_id = current_clientid++; 
1032 }
1033
1034 static void gen_confirm(struct nfs4_client *clp)
1035 {
1036         static u32 i;
1037         u32 *p;
1038
1039         p = (u32 *)clp->cl_confirm.data;
1040         *p++ = get_seconds();
1041         *p++ = i++;
1042 }
1043
1044 static struct nfs4_client *create_client(struct xdr_netobj name, char *recdir,
1045                 struct svc_rqst *rqstp, nfs4_verifier *verf)
1046 {
1047         struct nfs4_client *clp;
1048         struct sockaddr *sa = svc_addr(rqstp);
1049         char *princ;
1050
1051         clp = alloc_client(name);
1052         if (clp == NULL)
1053                 return NULL;
1054
1055         INIT_LIST_HEAD(&clp->cl_sessions);
1056
1057         princ = svc_gss_principal(rqstp);
1058         if (princ) {
1059                 clp->cl_principal = kstrdup(princ, GFP_KERNEL);
1060                 if (clp->cl_principal == NULL) {
1061                         free_client(clp);
1062                         return NULL;
1063                 }
1064         }
1065
1066         memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
1067         atomic_set(&clp->cl_refcount, 0);
1068         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1069         INIT_LIST_HEAD(&clp->cl_idhash);
1070         INIT_LIST_HEAD(&clp->cl_strhash);
1071         INIT_LIST_HEAD(&clp->cl_openowners);
1072         INIT_LIST_HEAD(&clp->cl_delegations);
1073         INIT_LIST_HEAD(&clp->cl_lru);
1074         INIT_LIST_HEAD(&clp->cl_callbacks);
1075         spin_lock_init(&clp->cl_lock);
1076         INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_do_callback_rpc);
1077         clp->cl_time = get_seconds();
1078         clear_bit(0, &clp->cl_cb_slot_busy);
1079         rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1080         copy_verf(clp, verf);
1081         rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1082         clp->cl_flavor = rqstp->rq_flavor;
1083         copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1084         gen_confirm(clp);
1085         clp->cl_cb_session = NULL;
1086         return clp;
1087 }
1088
1089 static int check_name(struct xdr_netobj name)
1090 {
1091         if (name.len == 0) 
1092                 return 0;
1093         if (name.len > NFS4_OPAQUE_LIMIT) {
1094                 dprintk("NFSD: check_name: name too long(%d)!\n", name.len);
1095                 return 0;
1096         }
1097         return 1;
1098 }
1099
1100 static void
1101 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
1102 {
1103         unsigned int idhashval;
1104
1105         list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
1106         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1107         list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
1108         renew_client(clp);
1109 }
1110
1111 static void
1112 move_to_confirmed(struct nfs4_client *clp)
1113 {
1114         unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1115         unsigned int strhashval;
1116
1117         dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1118         list_move(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
1119         strhashval = clientstr_hashval(clp->cl_recdir);
1120         list_move(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
1121         renew_client(clp);
1122 }
1123
1124 static struct nfs4_client *
1125 find_confirmed_client(clientid_t *clid)
1126 {
1127         struct nfs4_client *clp;
1128         unsigned int idhashval = clientid_hashval(clid->cl_id);
1129
1130         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1131                 if (same_clid(&clp->cl_clientid, clid))
1132                         return clp;
1133         }
1134         return NULL;
1135 }
1136
1137 static struct nfs4_client *
1138 find_unconfirmed_client(clientid_t *clid)
1139 {
1140         struct nfs4_client *clp;
1141         unsigned int idhashval = clientid_hashval(clid->cl_id);
1142
1143         list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1144                 if (same_clid(&clp->cl_clientid, clid))
1145                         return clp;
1146         }
1147         return NULL;
1148 }
1149
1150 static bool clp_used_exchangeid(struct nfs4_client *clp)
1151 {
1152         return clp->cl_exchange_flags != 0;
1153
1154
1155 static struct nfs4_client *
1156 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
1157 {
1158         struct nfs4_client *clp;
1159
1160         list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
1161                 if (same_name(clp->cl_recdir, dname))
1162                         return clp;
1163         }
1164         return NULL;
1165 }
1166
1167 static struct nfs4_client *
1168 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
1169 {
1170         struct nfs4_client *clp;
1171
1172         list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
1173                 if (same_name(clp->cl_recdir, dname))
1174                         return clp;
1175         }
1176         return NULL;
1177 }
1178
1179 static void rpc_svcaddr2sockaddr(struct sockaddr *sa, unsigned short family, union svc_addr_u *svcaddr)
1180 {
1181         switch (family) {
1182         case AF_INET:
1183                 ((struct sockaddr_in *)sa)->sin_family = AF_INET;
1184                 ((struct sockaddr_in *)sa)->sin_addr = svcaddr->addr;
1185                 return;
1186         case AF_INET6:
1187                 ((struct sockaddr_in6 *)sa)->sin6_family = AF_INET6;
1188                 ((struct sockaddr_in6 *)sa)->sin6_addr = svcaddr->addr6;
1189                 return;
1190         }
1191 }
1192
1193 static void
1194 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1195 {
1196         struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
1197         struct sockaddr *sa = svc_addr(rqstp);
1198         u32 scopeid = rpc_get_scope_id(sa);
1199         unsigned short expected_family;
1200
1201         /* Currently, we only support tcp and tcp6 for the callback channel */
1202         if (se->se_callback_netid_len == 3 &&
1203             !memcmp(se->se_callback_netid_val, "tcp", 3))
1204                 expected_family = AF_INET;
1205         else if (se->se_callback_netid_len == 4 &&
1206                  !memcmp(se->se_callback_netid_val, "tcp6", 4))
1207                 expected_family = AF_INET6;
1208         else
1209                 goto out_err;
1210
1211         conn->cb_addrlen = rpc_uaddr2sockaddr(se->se_callback_addr_val,
1212                                             se->se_callback_addr_len,
1213                                             (struct sockaddr *)&conn->cb_addr,
1214                                             sizeof(conn->cb_addr));
1215
1216         if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1217                 goto out_err;
1218
1219         if (conn->cb_addr.ss_family == AF_INET6)
1220                 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
1221
1222         conn->cb_prog = se->se_callback_prog;
1223         conn->cb_ident = se->se_callback_ident;
1224         rpc_svcaddr2sockaddr((struct sockaddr *)&conn->cb_saddr, expected_family, &rqstp->rq_daddr);
1225         return;
1226 out_err:
1227         conn->cb_addr.ss_family = AF_UNSPEC;
1228         conn->cb_addrlen = 0;
1229         dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
1230                 "will not receive delegations\n",
1231                 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1232
1233         return;
1234 }
1235
1236 /*
1237  * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
1238  */
1239 void
1240 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
1241 {
1242         struct nfsd4_slot *slot = resp->cstate.slot;
1243         unsigned int base;
1244
1245         dprintk("--> %s slot %p\n", __func__, slot);
1246
1247         slot->sl_opcnt = resp->opcnt;
1248         slot->sl_status = resp->cstate.status;
1249
1250         if (nfsd4_not_cached(resp)) {
1251                 slot->sl_datalen = 0;
1252                 return;
1253         }
1254         slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
1255         base = (char *)resp->cstate.datap -
1256                                         (char *)resp->xbuf->head[0].iov_base;
1257         if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
1258                                     slot->sl_datalen))
1259                 WARN("%s: sessions DRC could not cache compound\n", __func__);
1260         return;
1261 }
1262
1263 /*
1264  * Encode the replay sequence operation from the slot values.
1265  * If cachethis is FALSE encode the uncached rep error on the next
1266  * operation which sets resp->p and increments resp->opcnt for
1267  * nfs4svc_encode_compoundres.
1268  *
1269  */
1270 static __be32
1271 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
1272                           struct nfsd4_compoundres *resp)
1273 {
1274         struct nfsd4_op *op;
1275         struct nfsd4_slot *slot = resp->cstate.slot;
1276
1277         dprintk("--> %s resp->opcnt %d cachethis %u \n", __func__,
1278                 resp->opcnt, resp->cstate.slot->sl_cachethis);
1279
1280         /* Encode the replayed sequence operation */
1281         op = &args->ops[resp->opcnt - 1];
1282         nfsd4_encode_operation(resp, op);
1283
1284         /* Return nfserr_retry_uncached_rep in next operation. */
1285         if (args->opcnt > 1 && slot->sl_cachethis == 0) {
1286                 op = &args->ops[resp->opcnt++];
1287                 op->status = nfserr_retry_uncached_rep;
1288                 nfsd4_encode_operation(resp, op);
1289         }
1290         return op->status;
1291 }
1292
1293 /*
1294  * The sequence operation is not cached because we can use the slot and
1295  * session values.
1296  */
1297 __be32
1298 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
1299                          struct nfsd4_sequence *seq)
1300 {
1301         struct nfsd4_slot *slot = resp->cstate.slot;
1302         __be32 status;
1303
1304         dprintk("--> %s slot %p\n", __func__, slot);
1305
1306         /* Either returns 0 or nfserr_retry_uncached */
1307         status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
1308         if (status == nfserr_retry_uncached_rep)
1309                 return status;
1310
1311         /* The sequence operation has been encoded, cstate->datap set. */
1312         memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
1313
1314         resp->opcnt = slot->sl_opcnt;
1315         resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
1316         status = slot->sl_status;
1317
1318         return status;
1319 }
1320
1321 /*
1322  * Set the exchange_id flags returned by the server.
1323  */
1324 static void
1325 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
1326 {
1327         /* pNFS is not supported */
1328         new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
1329
1330         /* Referrals are supported, Migration is not. */
1331         new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
1332
1333         /* set the wire flags to return to client. */
1334         clid->flags = new->cl_exchange_flags;
1335 }
1336
1337 __be32
1338 nfsd4_exchange_id(struct svc_rqst *rqstp,
1339                   struct nfsd4_compound_state *cstate,
1340                   struct nfsd4_exchange_id *exid)
1341 {
1342         struct nfs4_client *unconf, *conf, *new;
1343         int status;
1344         unsigned int            strhashval;
1345         char                    dname[HEXDIR_LEN];
1346         char                    addr_str[INET6_ADDRSTRLEN];
1347         nfs4_verifier           verf = exid->verifier;
1348         struct sockaddr         *sa = svc_addr(rqstp);
1349
1350         rpc_ntop(sa, addr_str, sizeof(addr_str));
1351         dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
1352                 "ip_addr=%s flags %x, spa_how %d\n",
1353                 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
1354                 addr_str, exid->flags, exid->spa_how);
1355
1356         if (!check_name(exid->clname) || (exid->flags & ~EXCHGID4_FLAG_MASK_A))
1357                 return nfserr_inval;
1358
1359         /* Currently only support SP4_NONE */
1360         switch (exid->spa_how) {
1361         case SP4_NONE:
1362                 break;
1363         case SP4_SSV:
1364                 return nfserr_serverfault;
1365         default:
1366                 BUG();                          /* checked by xdr code */
1367         case SP4_MACH_CRED:
1368                 return nfserr_serverfault;      /* no excuse :-/ */
1369         }
1370
1371         status = nfs4_make_rec_clidname(dname, &exid->clname);
1372
1373         if (status)
1374                 goto error;
1375
1376         strhashval = clientstr_hashval(dname);
1377
1378         nfs4_lock_state();
1379         status = nfs_ok;
1380
1381         conf = find_confirmed_client_by_str(dname, strhashval);
1382         if (conf) {
1383                 if (!clp_used_exchangeid(conf)) {
1384                         status = nfserr_clid_inuse; /* XXX: ? */
1385                         goto out;
1386                 }
1387                 if (!same_verf(&verf, &conf->cl_verifier)) {
1388                         /* 18.35.4 case 8 */
1389                         if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1390                                 status = nfserr_not_same;
1391                                 goto out;
1392                         }
1393                         /* Client reboot: destroy old state */
1394                         expire_client(conf);
1395                         goto out_new;
1396                 }
1397                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1398                         /* 18.35.4 case 9 */
1399                         if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1400                                 status = nfserr_perm;
1401                                 goto out;
1402                         }
1403                         expire_client(conf);
1404                         goto out_new;
1405                 }
1406                 /*
1407                  * Set bit when the owner id and verifier map to an already
1408                  * confirmed client id (18.35.3).
1409                  */
1410                 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
1411
1412                 /*
1413                  * Falling into 18.35.4 case 2, possible router replay.
1414                  * Leave confirmed record intact and return same result.
1415                  */
1416                 copy_verf(conf, &verf);
1417                 new = conf;
1418                 goto out_copy;
1419         }
1420
1421         /* 18.35.4 case 7 */
1422         if (exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A) {
1423                 status = nfserr_noent;
1424                 goto out;
1425         }
1426
1427         unconf  = find_unconfirmed_client_by_str(dname, strhashval);
1428         if (unconf) {
1429                 /*
1430                  * Possible retry or client restart.  Per 18.35.4 case 4,
1431                  * a new unconfirmed record should be generated regardless
1432                  * of whether any properties have changed.
1433                  */
1434                 expire_client(unconf);
1435         }
1436
1437 out_new:
1438         /* Normal case */
1439         new = create_client(exid->clname, dname, rqstp, &verf);
1440         if (new == NULL) {
1441                 status = nfserr_jukebox;
1442                 goto out;
1443         }
1444
1445         gen_clid(new);
1446         add_to_unconfirmed(new, strhashval);
1447 out_copy:
1448         exid->clientid.cl_boot = new->cl_clientid.cl_boot;
1449         exid->clientid.cl_id = new->cl_clientid.cl_id;
1450
1451         exid->seqid = 1;
1452         nfsd4_set_ex_flags(new, exid);
1453
1454         dprintk("nfsd4_exchange_id seqid %d flags %x\n",
1455                 new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
1456         status = nfs_ok;
1457
1458 out:
1459         nfs4_unlock_state();
1460 error:
1461         dprintk("nfsd4_exchange_id returns %d\n", ntohl(status));
1462         return status;
1463 }
1464
1465 static int
1466 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
1467 {
1468         dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
1469                 slot_seqid);
1470
1471         /* The slot is in use, and no response has been sent. */
1472         if (slot_inuse) {
1473                 if (seqid == slot_seqid)
1474                         return nfserr_jukebox;
1475                 else
1476                         return nfserr_seq_misordered;
1477         }
1478         /* Normal */
1479         if (likely(seqid == slot_seqid + 1))
1480                 return nfs_ok;
1481         /* Replay */
1482         if (seqid == slot_seqid)
1483                 return nfserr_replay_cache;
1484         /* Wraparound */
1485         if (seqid == 1 && (slot_seqid + 1) == 0)
1486                 return nfs_ok;
1487         /* Misordered replay or misordered new request */
1488         return nfserr_seq_misordered;
1489 }
1490
1491 /*
1492  * Cache the create session result into the create session single DRC
1493  * slot cache by saving the xdr structure. sl_seqid has been set.
1494  * Do this for solo or embedded create session operations.
1495  */
1496 static void
1497 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
1498                            struct nfsd4_clid_slot *slot, int nfserr)
1499 {
1500         slot->sl_status = nfserr;
1501         memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
1502 }
1503
1504 static __be32
1505 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
1506                             struct nfsd4_clid_slot *slot)
1507 {
1508         memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
1509         return slot->sl_status;
1510 }
1511
1512 __be32
1513 nfsd4_create_session(struct svc_rqst *rqstp,
1514                      struct nfsd4_compound_state *cstate,
1515                      struct nfsd4_create_session *cr_ses)
1516 {
1517         struct sockaddr *sa = svc_addr(rqstp);
1518         struct nfs4_client *conf, *unconf;
1519         struct nfsd4_session *new;
1520         struct nfsd4_clid_slot *cs_slot = NULL;
1521         bool confirm_me = false;
1522         int status = 0;
1523
1524         if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
1525                 return nfserr_inval;
1526
1527         nfs4_lock_state();
1528         unconf = find_unconfirmed_client(&cr_ses->clientid);
1529         conf = find_confirmed_client(&cr_ses->clientid);
1530
1531         if (conf) {
1532                 cs_slot = &conf->cl_cs_slot;
1533                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1534                 if (status == nfserr_replay_cache) {
1535                         dprintk("Got a create_session replay! seqid= %d\n",
1536                                 cs_slot->sl_seqid);
1537                         /* Return the cached reply status */
1538                         status = nfsd4_replay_create_session(cr_ses, cs_slot);
1539                         goto out;
1540                 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
1541                         status = nfserr_seq_misordered;
1542                         dprintk("Sequence misordered!\n");
1543                         dprintk("Expected seqid= %d but got seqid= %d\n",
1544                                 cs_slot->sl_seqid, cr_ses->seqid);
1545                         goto out;
1546                 }
1547         } else if (unconf) {
1548                 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
1549                     !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
1550                         status = nfserr_clid_inuse;
1551                         goto out;
1552                 }
1553
1554                 cs_slot = &unconf->cl_cs_slot;
1555                 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
1556                 if (status) {
1557                         /* an unconfirmed replay returns misordered */
1558                         status = nfserr_seq_misordered;
1559                         goto out;
1560                 }
1561
1562                 confirm_me = true;
1563                 conf = unconf;
1564         } else {
1565                 status = nfserr_stale_clientid;
1566                 goto out;
1567         }
1568
1569         /*
1570          * XXX: we should probably set this at creation time, and check
1571          * for consistent minorversion use throughout:
1572          */
1573         conf->cl_minorversion = 1;
1574         /*
1575          * We do not support RDMA or persistent sessions
1576          */
1577         cr_ses->flags &= ~SESSION4_PERSIST;
1578         cr_ses->flags &= ~SESSION4_RDMA;
1579
1580         status = nfserr_jukebox;
1581         new = alloc_init_session(rqstp, conf, cr_ses);
1582         if (!new)
1583                 goto out;
1584         status = nfs_ok;
1585         memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
1586                NFS4_MAX_SESSIONID_LEN);
1587         memcpy(&cr_ses->fore_channel, &new->se_fchannel,
1588                 sizeof(struct nfsd4_channel_attrs));
1589         cs_slot->sl_seqid++;
1590         cr_ses->seqid = cs_slot->sl_seqid;
1591
1592         /* cache solo and embedded create sessions under the state lock */
1593         nfsd4_cache_create_session(cr_ses, cs_slot, status);
1594         if (confirm_me)
1595                 move_to_confirmed(conf);
1596 out:
1597         nfs4_unlock_state();
1598         dprintk("%s returns %d\n", __func__, ntohl(status));
1599         return status;
1600 }
1601
1602 static bool nfsd4_last_compound_op(struct svc_rqst *rqstp)
1603 {
1604         struct nfsd4_compoundres *resp = rqstp->rq_resp;
1605         struct nfsd4_compoundargs *argp = rqstp->rq_argp;
1606
1607         return argp->opcnt == resp->opcnt;
1608 }
1609
1610 static __be32 nfsd4_map_bcts_dir(u32 *dir)
1611 {
1612         switch (*dir) {
1613         case NFS4_CDFC4_FORE:
1614         case NFS4_CDFC4_BACK:
1615                 return nfs_ok;
1616         case NFS4_CDFC4_FORE_OR_BOTH:
1617         case NFS4_CDFC4_BACK_OR_BOTH:
1618                 *dir = NFS4_CDFC4_BOTH;
1619                 return nfs_ok;
1620         };
1621         return nfserr_inval;
1622 }
1623
1624 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
1625                      struct nfsd4_compound_state *cstate,
1626                      struct nfsd4_bind_conn_to_session *bcts)
1627 {
1628         __be32 status;
1629
1630         if (!nfsd4_last_compound_op(rqstp))
1631                 return nfserr_not_only_op;
1632         spin_lock(&client_lock);
1633         cstate->session = find_in_sessionid_hashtbl(&bcts->sessionid);
1634         /* Sorta weird: we only need the refcnt'ing because new_conn acquires
1635          * client_lock iself: */
1636         if (cstate->session) {
1637                 nfsd4_get_session(cstate->session);
1638                 atomic_inc(&cstate->session->se_client->cl_refcount);
1639         }
1640         spin_unlock(&client_lock);
1641         if (!cstate->session)
1642                 return nfserr_badsession;
1643
1644         status = nfsd4_map_bcts_dir(&bcts->dir);
1645         if (!status)
1646                 nfsd4_new_conn(rqstp, cstate->session, bcts->dir);
1647         return status;
1648 }
1649
1650 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
1651 {
1652         if (!session)
1653                 return 0;
1654         return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
1655 }
1656
1657 __be32
1658 nfsd4_destroy_session(struct svc_rqst *r,
1659                       struct nfsd4_compound_state *cstate,
1660                       struct nfsd4_destroy_session *sessionid)
1661 {
1662         struct nfsd4_session *ses;
1663         u32 status = nfserr_badsession;
1664
1665         /* Notes:
1666          * - The confirmed nfs4_client->cl_sessionid holds destroyed sessinid
1667          * - Should we return nfserr_back_chan_busy if waiting for
1668          *   callbacks on to-be-destroyed session?
1669          * - Do we need to clear any callback info from previous session?
1670          */
1671
1672         if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
1673                 if (!nfsd4_last_compound_op(r))
1674                         return nfserr_not_only_op;
1675         }
1676         dump_sessionid(__func__, &sessionid->sessionid);
1677         spin_lock(&client_lock);
1678         ses = find_in_sessionid_hashtbl(&sessionid->sessionid);
1679         if (!ses) {
1680                 spin_unlock(&client_lock);
1681                 goto out;
1682         }
1683
1684         unhash_session(ses);
1685         spin_unlock(&client_lock);
1686
1687         nfs4_lock_state();
1688         nfsd4_probe_callback_sync(ses->se_client);
1689         nfs4_unlock_state();
1690
1691         nfsd4_del_conns(ses);
1692
1693         nfsd4_put_session(ses);
1694         status = nfs_ok;
1695 out:
1696         dprintk("%s returns %d\n", __func__, ntohl(status));
1697         return status;
1698 }
1699
1700 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
1701 {
1702         struct nfsd4_conn *c;
1703
1704         list_for_each_entry(c, &s->se_conns, cn_persession) {
1705                 if (c->cn_xprt == xpt) {
1706                         return c;
1707                 }
1708         }
1709         return NULL;
1710 }
1711
1712 static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
1713 {
1714         struct nfs4_client *clp = ses->se_client;
1715         struct nfsd4_conn *c;
1716         int ret;
1717
1718         spin_lock(&clp->cl_lock);
1719         c = __nfsd4_find_conn(new->cn_xprt, ses);
1720         if (c) {
1721                 spin_unlock(&clp->cl_lock);
1722                 free_conn(new);
1723                 return;
1724         }
1725         __nfsd4_hash_conn(new, ses);
1726         spin_unlock(&clp->cl_lock);
1727         ret = nfsd4_register_conn(new);
1728         if (ret)
1729                 /* oops; xprt is already down: */
1730                 nfsd4_conn_lost(&new->cn_xpt_user);
1731         return;
1732 }
1733
1734 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
1735 {
1736         struct nfsd4_compoundargs *args = rqstp->rq_argp;
1737
1738         return args->opcnt > session->se_fchannel.maxops;
1739 }
1740
1741 __be32
1742 nfsd4_sequence(struct svc_rqst *rqstp,
1743                struct nfsd4_compound_state *cstate,
1744                struct nfsd4_sequence *seq)
1745 {
1746         struct nfsd4_compoundres *resp = rqstp->rq_resp;
1747         struct nfsd4_session *session;
1748         struct nfsd4_slot *slot;
1749         struct nfsd4_conn *conn;
1750         int status;
1751
1752         if (resp->opcnt != 1)
1753                 return nfserr_sequence_pos;
1754
1755         /*
1756          * Will be either used or freed by nfsd4_sequence_check_conn
1757          * below.
1758          */
1759         conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
1760         if (!conn)
1761                 return nfserr_jukebox;
1762
1763         spin_lock(&client_lock);
1764         status = nfserr_badsession;
1765         session = find_in_sessionid_hashtbl(&seq->sessionid);
1766         if (!session)
1767                 goto out;
1768
1769         status = nfserr_too_many_ops;
1770         if (nfsd4_session_too_many_ops(rqstp, session))
1771                 goto out;
1772
1773         status = nfserr_badslot;
1774         if (seq->slotid >= session->se_fchannel.maxreqs)
1775                 goto out;
1776
1777         slot = session->se_slots[seq->slotid];
1778         dprintk("%s: slotid %d\n", __func__, seq->slotid);
1779
1780         /* We do not negotiate the number of slots yet, so set the
1781          * maxslots to the session maxreqs which is used to encode
1782          * sr_highest_slotid and the sr_target_slot id to maxslots */
1783         seq->maxslots = session->se_fchannel.maxreqs;
1784
1785         status = check_slot_seqid(seq->seqid, slot->sl_seqid, slot->sl_inuse);
1786         if (status == nfserr_replay_cache) {
1787                 cstate->slot = slot;
1788                 cstate->session = session;
1789                 /* Return the cached reply status and set cstate->status
1790                  * for nfsd4_proc_compound processing */
1791                 status = nfsd4_replay_cache_entry(resp, seq);
1792                 cstate->status = nfserr_replay_cache;
1793                 goto out;
1794         }
1795         if (status)
1796                 goto out;
1797
1798         nfsd4_sequence_check_conn(conn, session);
1799         conn = NULL;
1800
1801         /* Success! bump slot seqid */
1802         slot->sl_inuse = true;
1803         slot->sl_seqid = seq->seqid;
1804         slot->sl_cachethis = seq->cachethis;
1805
1806         cstate->slot = slot;
1807         cstate->session = session;
1808
1809 out:
1810         /* Hold a session reference until done processing the compound. */
1811         if (cstate->session) {
1812                 struct nfs4_client *clp = session->se_client;
1813
1814                 nfsd4_get_session(cstate->session);
1815                 atomic_inc(&clp->cl_refcount);
1816                 if (clp->cl_cb_state == NFSD4_CB_DOWN)
1817                         seq->status_flags |= SEQ4_STATUS_CB_PATH_DOWN;
1818         }
1819         kfree(conn);
1820         spin_unlock(&client_lock);
1821         dprintk("%s: return %d\n", __func__, ntohl(status));
1822         return status;
1823 }
1824
1825 __be32
1826 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
1827 {
1828         int status = 0;
1829
1830         if (rc->rca_one_fs) {
1831                 if (!cstate->current_fh.fh_dentry)
1832                         return nfserr_nofilehandle;
1833                 /*
1834                  * We don't take advantage of the rca_one_fs case.
1835                  * That's OK, it's optional, we can safely ignore it.
1836                  */
1837                  return nfs_ok;
1838         }
1839
1840         nfs4_lock_state();
1841         status = nfserr_complete_already;
1842         if (cstate->session->se_client->cl_firststate)
1843                 goto out;
1844
1845         status = nfserr_stale_clientid;
1846         if (is_client_expired(cstate->session->se_client))
1847                 /*
1848                  * The following error isn't really legal.
1849                  * But we only get here if the client just explicitly
1850                  * destroyed the client.  Surely it no longer cares what
1851                  * error it gets back on an operation for the dead
1852                  * client.
1853                  */
1854                 goto out;
1855
1856         status = nfs_ok;
1857         nfsd4_create_clid_dir(cstate->session->se_client);
1858 out:
1859         nfs4_unlock_state();
1860         return status;
1861 }
1862
1863 __be32
1864 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
1865                   struct nfsd4_setclientid *setclid)
1866 {
1867         struct xdr_netobj       clname = { 
1868                 .len = setclid->se_namelen,
1869                 .data = setclid->se_name,
1870         };
1871         nfs4_verifier           clverifier = setclid->se_verf;
1872         unsigned int            strhashval;
1873         struct nfs4_client      *conf, *unconf, *new;
1874         __be32                  status;
1875         char                    dname[HEXDIR_LEN];
1876         
1877         if (!check_name(clname))
1878                 return nfserr_inval;
1879
1880         status = nfs4_make_rec_clidname(dname, &clname);
1881         if (status)
1882                 return status;
1883
1884         /* 
1885          * XXX The Duplicate Request Cache (DRC) has been checked (??)
1886          * We get here on a DRC miss.
1887          */
1888
1889         strhashval = clientstr_hashval(dname);
1890
1891         nfs4_lock_state();
1892         conf = find_confirmed_client_by_str(dname, strhashval);
1893         if (conf) {
1894                 /* RFC 3530 14.2.33 CASE 0: */
1895                 status = nfserr_clid_inuse;
1896                 if (clp_used_exchangeid(conf))
1897                         goto out;
1898                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
1899                         char addr_str[INET6_ADDRSTRLEN];
1900                         rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
1901                                  sizeof(addr_str));
1902                         dprintk("NFSD: setclientid: string in use by client "
1903                                 "at %s\n", addr_str);
1904                         goto out;
1905                 }
1906         }
1907         /*
1908          * section 14.2.33 of RFC 3530 (under the heading "IMPLEMENTATION")
1909          * has a description of SETCLIENTID request processing consisting
1910          * of 5 bullet points, labeled as CASE0 - CASE4 below.
1911          */
1912         unconf = find_unconfirmed_client_by_str(dname, strhashval);
1913         status = nfserr_jukebox;
1914         if (!conf) {
1915                 /*
1916                  * RFC 3530 14.2.33 CASE 4:
1917                  * placed first, because it is the normal case
1918                  */
1919                 if (unconf)
1920                         expire_client(unconf);
1921                 new = create_client(clname, dname, rqstp, &clverifier);
1922                 if (new == NULL)
1923                         goto out;
1924                 gen_clid(new);
1925         } else if (same_verf(&conf->cl_verifier, &clverifier)) {
1926                 /*
1927                  * RFC 3530 14.2.33 CASE 1:
1928                  * probable callback update
1929                  */
1930                 if (unconf) {
1931                         /* Note this is removing unconfirmed {*x***},
1932                          * which is stronger than RFC recommended {vxc**}.
1933                          * This has the advantage that there is at most
1934                          * one {*x***} in either list at any time.
1935                          */
1936                         expire_client(unconf);
1937                 }
1938                 new = create_client(clname, dname, rqstp, &clverifier);
1939                 if (new == NULL)
1940                         goto out;
1941                 copy_clid(new, conf);
1942         } else if (!unconf) {
1943                 /*
1944                  * RFC 3530 14.2.33 CASE 2:
1945                  * probable client reboot; state will be removed if
1946                  * confirmed.
1947                  */
1948                 new = create_client(clname, dname, rqstp, &clverifier);
1949                 if (new == NULL)
1950                         goto out;
1951                 gen_clid(new);
1952         } else {
1953                 /*
1954                  * RFC 3530 14.2.33 CASE 3:
1955                  * probable client reboot; state will be removed if
1956                  * confirmed.
1957                  */
1958                 expire_client(unconf);
1959                 new = create_client(clname, dname, rqstp, &clverifier);
1960                 if (new == NULL)
1961                         goto out;
1962                 gen_clid(new);
1963         }
1964         /*
1965          * XXX: we should probably set this at creation time, and check
1966          * for consistent minorversion use throughout:
1967          */
1968         new->cl_minorversion = 0;
1969         gen_callback(new, setclid, rqstp);
1970         add_to_unconfirmed(new, strhashval);
1971         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
1972         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
1973         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
1974         status = nfs_ok;
1975 out:
1976         nfs4_unlock_state();
1977         return status;
1978 }
1979
1980
1981 /*
1982  * Section 14.2.34 of RFC 3530 (under the heading "IMPLEMENTATION") has
1983  * a description of SETCLIENTID_CONFIRM request processing consisting of 4
1984  * bullets, labeled as CASE1 - CASE4 below.
1985  */
1986 __be32
1987 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
1988                          struct nfsd4_compound_state *cstate,
1989                          struct nfsd4_setclientid_confirm *setclientid_confirm)
1990 {
1991         struct sockaddr *sa = svc_addr(rqstp);
1992         struct nfs4_client *conf, *unconf;
1993         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
1994         clientid_t * clid = &setclientid_confirm->sc_clientid;
1995         __be32 status;
1996
1997         if (STALE_CLIENTID(clid))
1998                 return nfserr_stale_clientid;
1999         /* 
2000          * XXX The Duplicate Request Cache (DRC) has been checked (??)
2001          * We get here on a DRC miss.
2002          */
2003
2004         nfs4_lock_state();
2005
2006         conf = find_confirmed_client(clid);
2007         unconf = find_unconfirmed_client(clid);
2008
2009         status = nfserr_clid_inuse;
2010         if (conf && !rpc_cmp_addr((struct sockaddr *) &conf->cl_addr, sa))
2011                 goto out;
2012         if (unconf && !rpc_cmp_addr((struct sockaddr *) &unconf->cl_addr, sa))
2013                 goto out;
2014
2015         /*
2016          * section 14.2.34 of RFC 3530 has a description of
2017          * SETCLIENTID_CONFIRM request processing consisting
2018          * of 4 bullet points, labeled as CASE1 - CASE4 below.
2019          */
2020         if (conf && unconf && same_verf(&confirm, &unconf->cl_confirm)) {
2021                 /*
2022                  * RFC 3530 14.2.34 CASE 1:
2023                  * callback update
2024                  */
2025                 if (!same_creds(&conf->cl_cred, &unconf->cl_cred))
2026                         status = nfserr_clid_inuse;
2027                 else {
2028                         nfsd4_change_callback(conf, &unconf->cl_cb_conn);
2029                         nfsd4_probe_callback(conf);
2030                         expire_client(unconf);
2031                         status = nfs_ok;
2032
2033                 }
2034         } else if (conf && !unconf) {
2035                 /*
2036                  * RFC 3530 14.2.34 CASE 2:
2037                  * probable retransmitted request; play it safe and
2038                  * do nothing.
2039                  */
2040                 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred))
2041                         status = nfserr_clid_inuse;
2042                 else
2043                         status = nfs_ok;
2044         } else if (!conf && unconf
2045                         && same_verf(&unconf->cl_confirm, &confirm)) {
2046                 /*
2047                  * RFC 3530 14.2.34 CASE 3:
2048                  * Normal case; new or rebooted client:
2049                  */
2050                 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
2051                         status = nfserr_clid_inuse;
2052                 } else {
2053                         unsigned int hash =
2054                                 clientstr_hashval(unconf->cl_recdir);
2055                         conf = find_confirmed_client_by_str(unconf->cl_recdir,
2056                                                             hash);
2057                         if (conf) {
2058                                 nfsd4_remove_clid_dir(conf);
2059                                 expire_client(conf);
2060                         }
2061                         move_to_confirmed(unconf);
2062                         conf = unconf;
2063                         nfsd4_probe_callback(conf);
2064                         status = nfs_ok;
2065                 }
2066         } else if ((!conf || (conf && !same_verf(&conf->cl_confirm, &confirm)))
2067             && (!unconf || (unconf && !same_verf(&unconf->cl_confirm,
2068                                                                 &confirm)))) {
2069                 /*
2070                  * RFC 3530 14.2.34 CASE 4:
2071                  * Client probably hasn't noticed that we rebooted yet.
2072                  */
2073                 status = nfserr_stale_clientid;
2074         } else {
2075                 /* check that we have hit one of the cases...*/
2076                 status = nfserr_clid_inuse;
2077         }
2078 out:
2079         nfs4_unlock_state();
2080         return status;
2081 }
2082
2083 /* OPEN Share state helper functions */
2084 static inline struct nfs4_file *
2085 alloc_init_file(struct inode *ino)
2086 {
2087         struct nfs4_file *fp;
2088         unsigned int hashval = file_hashval(ino);
2089
2090         fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
2091         if (fp) {
2092                 atomic_set(&fp->fi_ref, 1);
2093                 INIT_LIST_HEAD(&fp->fi_hash);
2094                 INIT_LIST_HEAD(&fp->fi_stateids);
2095                 INIT_LIST_HEAD(&fp->fi_delegations);
2096                 fp->fi_inode = igrab(ino);
2097                 fp->fi_id = current_fileid++;
2098                 fp->fi_had_conflict = false;
2099                 fp->fi_lease = NULL;
2100                 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
2101                 memset(fp->fi_access, 0, sizeof(fp->fi_access));
2102                 spin_lock(&recall_lock);
2103                 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
2104                 spin_unlock(&recall_lock);
2105                 return fp;
2106         }
2107         return NULL;
2108 }
2109
2110 static void
2111 nfsd4_free_slab(struct kmem_cache **slab)
2112 {
2113         if (*slab == NULL)
2114                 return;
2115         kmem_cache_destroy(*slab);
2116         *slab = NULL;
2117 }
2118
2119 void
2120 nfsd4_free_slabs(void)
2121 {
2122         nfsd4_free_slab(&stateowner_slab);
2123         nfsd4_free_slab(&file_slab);
2124         nfsd4_free_slab(&stateid_slab);
2125         nfsd4_free_slab(&deleg_slab);
2126 }
2127
2128 static int
2129 nfsd4_init_slabs(void)
2130 {
2131         stateowner_slab = kmem_cache_create("nfsd4_stateowners",
2132                         sizeof(struct nfs4_stateowner), 0, 0, NULL);
2133         if (stateowner_slab == NULL)
2134                 goto out_nomem;
2135         file_slab = kmem_cache_create("nfsd4_files",
2136                         sizeof(struct nfs4_file), 0, 0, NULL);
2137         if (file_slab == NULL)
2138                 goto out_nomem;
2139         stateid_slab = kmem_cache_create("nfsd4_stateids",
2140                         sizeof(struct nfs4_stateid), 0, 0, NULL);
2141         if (stateid_slab == NULL)
2142                 goto out_nomem;
2143         deleg_slab = kmem_cache_create("nfsd4_delegations",
2144                         sizeof(struct nfs4_delegation), 0, 0, NULL);
2145         if (deleg_slab == NULL)
2146                 goto out_nomem;
2147         return 0;
2148 out_nomem:
2149         nfsd4_free_slabs();
2150         dprintk("nfsd4: out of memory while initializing nfsv4\n");
2151         return -ENOMEM;
2152 }
2153
2154 void
2155 nfs4_free_stateowner(struct kref *kref)
2156 {
2157         struct nfs4_stateowner *sop =
2158                 container_of(kref, struct nfs4_stateowner, so_ref);
2159         kfree(sop->so_owner.data);
2160         kmem_cache_free(stateowner_slab, sop);
2161 }
2162
2163 static inline struct nfs4_stateowner *
2164 alloc_stateowner(struct xdr_netobj *owner)
2165 {
2166         struct nfs4_stateowner *sop;
2167
2168         if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
2169                 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
2170                         memcpy(sop->so_owner.data, owner->data, owner->len);
2171                         sop->so_owner.len = owner->len;
2172                         kref_init(&sop->so_ref);
2173                         return sop;
2174                 } 
2175                 kmem_cache_free(stateowner_slab, sop);
2176         }
2177         return NULL;
2178 }
2179
2180 static struct nfs4_stateowner *
2181 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
2182         struct nfs4_stateowner *sop;
2183         struct nfs4_replay *rp;
2184         unsigned int idhashval;
2185
2186         if (!(sop = alloc_stateowner(&open->op_owner)))
2187                 return NULL;
2188         idhashval = ownerid_hashval(current_ownerid);
2189         INIT_LIST_HEAD(&sop->so_idhash);
2190         INIT_LIST_HEAD(&sop->so_strhash);
2191         INIT_LIST_HEAD(&sop->so_perclient);
2192         INIT_LIST_HEAD(&sop->so_stateids);
2193         INIT_LIST_HEAD(&sop->so_perstateid);  /* not used */
2194         INIT_LIST_HEAD(&sop->so_close_lru);
2195         sop->so_time = 0;
2196         list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
2197         list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
2198         list_add(&sop->so_perclient, &clp->cl_openowners);
2199         sop->so_is_open_owner = 1;
2200         sop->so_id = current_ownerid++;
2201         sop->so_client = clp;
2202         sop->so_seqid = open->op_seqid;
2203         sop->so_confirmed = 0;
2204         rp = &sop->so_replay;
2205         rp->rp_status = nfserr_serverfault;
2206         rp->rp_buflen = 0;
2207         rp->rp_buf = rp->rp_ibuf;
2208         return sop;
2209 }
2210
2211 static inline void
2212 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
2213         struct nfs4_stateowner *sop = open->op_stateowner;
2214         unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2215
2216         INIT_LIST_HEAD(&stp->st_hash);
2217         INIT_LIST_HEAD(&stp->st_perstateowner);
2218         INIT_LIST_HEAD(&stp->st_lockowners);
2219         INIT_LIST_HEAD(&stp->st_perfile);
2220         list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
2221         list_add(&stp->st_perstateowner, &sop->so_stateids);
2222         list_add(&stp->st_perfile, &fp->fi_stateids);
2223         stp->st_stateowner = sop;
2224         get_nfs4_file(fp);
2225         stp->st_file = fp;
2226         stp->st_stateid.si_boot = boot_time;
2227         stp->st_stateid.si_stateownerid = sop->so_id;
2228         stp->st_stateid.si_fileid = fp->fi_id;
2229         stp->st_stateid.si_generation = 0;
2230         stp->st_access_bmap = 0;
2231         stp->st_deny_bmap = 0;
2232         __set_bit(open->op_share_access & ~NFS4_SHARE_WANT_MASK,
2233                   &stp->st_access_bmap);
2234         __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2235         stp->st_openstp = NULL;
2236 }
2237
2238 static void
2239 move_to_close_lru(struct nfs4_stateowner *sop)
2240 {
2241         dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
2242
2243         list_move_tail(&sop->so_close_lru, &close_lru);
2244         sop->so_time = get_seconds();
2245 }
2246
2247 static int
2248 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
2249                                                         clientid_t *clid)
2250 {
2251         return (sop->so_owner.len == owner->len) &&
2252                 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
2253                 (sop->so_client->cl_clientid.cl_id == clid->cl_id);
2254 }
2255
2256 static struct nfs4_stateowner *
2257 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
2258 {
2259         struct nfs4_stateowner *so = NULL;
2260
2261         list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
2262                 if (same_owner_str(so, &open->op_owner, &open->op_clientid))
2263                         return so;
2264         }
2265         return NULL;
2266 }
2267
2268 /* search file_hashtbl[] for file */
2269 static struct nfs4_file *
2270 find_file(struct inode *ino)
2271 {
2272         unsigned int hashval = file_hashval(ino);
2273         struct nfs4_file *fp;
2274
2275         spin_lock(&recall_lock);
2276         list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
2277                 if (fp->fi_inode == ino) {
2278                         get_nfs4_file(fp);
2279                         spin_unlock(&recall_lock);
2280                         return fp;
2281                 }
2282         }
2283         spin_unlock(&recall_lock);
2284         return NULL;
2285 }
2286
2287 static inline int access_valid(u32 x, u32 minorversion)
2288 {
2289         if ((x & NFS4_SHARE_ACCESS_MASK) < NFS4_SHARE_ACCESS_READ)
2290                 return 0;
2291         if ((x & NFS4_SHARE_ACCESS_MASK) > NFS4_SHARE_ACCESS_BOTH)
2292                 return 0;
2293         x &= ~NFS4_SHARE_ACCESS_MASK;
2294         if (minorversion && x) {
2295                 if ((x & NFS4_SHARE_WANT_MASK) > NFS4_SHARE_WANT_CANCEL)
2296                         return 0;
2297                 if ((x & NFS4_SHARE_WHEN_MASK) > NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED)
2298                         return 0;
2299                 x &= ~(NFS4_SHARE_WANT_MASK | NFS4_SHARE_WHEN_MASK);
2300         }
2301         if (x)
2302                 return 0;
2303         return 1;
2304 }
2305
2306 static inline int deny_valid(u32 x)
2307 {
2308         /* Note: unlike access bits, deny bits may be zero. */
2309         return x <= NFS4_SHARE_DENY_BOTH;
2310 }
2311
2312 /*
2313  * Called to check deny when READ with all zero stateid or
2314  * WRITE with all zero or all one stateid
2315  */
2316 static __be32
2317 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
2318 {
2319         struct inode *ino = current_fh->fh_dentry->d_inode;
2320         struct nfs4_file *fp;
2321         struct nfs4_stateid *stp;
2322         __be32 ret;
2323
2324         dprintk("NFSD: nfs4_share_conflict\n");
2325
2326         fp = find_file(ino);
2327         if (!fp)
2328                 return nfs_ok;
2329         ret = nfserr_locked;
2330         /* Search for conflicting share reservations */
2331         list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
2332                 if (test_bit(deny_type, &stp->st_deny_bmap) ||
2333                     test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
2334                         goto out;
2335         }
2336         ret = nfs_ok;
2337 out:
2338         put_nfs4_file(fp);
2339         return ret;
2340 }
2341
2342 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
2343 {
2344         /* We're assuming the state code never drops its reference
2345          * without first removing the lease.  Since we're in this lease
2346          * callback (and since the lease code is serialized by the kernel
2347          * lock) we know the server hasn't removed the lease yet, we know
2348          * it's safe to take a reference: */
2349         atomic_inc(&dp->dl_count);
2350
2351         list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
2352
2353         /* only place dl_time is set. protected by lock_flocks*/
2354         dp->dl_time = get_seconds();
2355
2356         nfsd4_cb_recall(dp);
2357 }
2358
2359 /* Called from break_lease() with lock_flocks() held. */
2360 static void nfsd_break_deleg_cb(struct file_lock *fl)
2361 {
2362         struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
2363         struct nfs4_delegation *dp;
2364
2365         BUG_ON(!fp);
2366         /* We assume break_lease is only called once per lease: */
2367         BUG_ON(fp->fi_had_conflict);
2368         /*
2369          * We don't want the locks code to timeout the lease for us;
2370          * we'll remove it ourself if a delegation isn't returned
2371          * in time:
2372          */
2373         fl->fl_break_time = 0;
2374
2375         spin_lock(&recall_lock);
2376         fp->fi_had_conflict = true;
2377         list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2378                 nfsd_break_one_deleg(dp);
2379         spin_unlock(&recall_lock);
2380 }
2381
2382 static
2383 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
2384 {
2385         if (arg & F_UNLCK)
2386                 return lease_modify(onlist, arg);
2387         else
2388                 return -EAGAIN;
2389 }
2390
2391 static const struct lock_manager_operations nfsd_lease_mng_ops = {
2392         .fl_break = nfsd_break_deleg_cb,
2393         .fl_change = nfsd_change_deleg_cb,
2394 };
2395
2396
2397 __be32
2398 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
2399                     struct nfsd4_open *open)
2400 {
2401         clientid_t *clientid = &open->op_clientid;
2402         struct nfs4_client *clp = NULL;
2403         unsigned int strhashval;
2404         struct nfs4_stateowner *sop = NULL;
2405
2406         if (!check_name(open->op_owner))
2407                 return nfserr_inval;
2408
2409         if (STALE_CLIENTID(&open->op_clientid))
2410                 return nfserr_stale_clientid;
2411
2412         strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
2413         sop = find_openstateowner_str(strhashval, open);
2414         open->op_stateowner = sop;
2415         if (!sop) {
2416                 /* Make sure the client's lease hasn't expired. */
2417                 clp = find_confirmed_client(clientid);
2418                 if (clp == NULL)
2419                         return nfserr_expired;
2420                 goto renew;
2421         }
2422         /* When sessions are used, skip open sequenceid processing */
2423         if (nfsd4_has_session(cstate))
2424                 goto renew;
2425         if (!sop->so_confirmed) {
2426                 /* Replace unconfirmed owners without checking for replay. */
2427                 clp = sop->so_client;
2428                 release_openowner(sop);
2429                 open->op_stateowner = NULL;
2430                 goto renew;
2431         }
2432         if (open->op_seqid == sop->so_seqid - 1) {
2433                 if (sop->so_replay.rp_buflen)
2434                         return nfserr_replay_me;
2435                 /* The original OPEN failed so spectacularly
2436                  * that we don't even have replay data saved!
2437                  * Therefore, we have no choice but to continue
2438                  * processing this OPEN; presumably, we'll
2439                  * fail again for the same reason.
2440                  */
2441                 dprintk("nfsd4_process_open1: replay with no replay cache\n");
2442                 goto renew;
2443         }
2444         if (open->op_seqid != sop->so_seqid)
2445                 return nfserr_bad_seqid;
2446 renew:
2447         if (open->op_stateowner == NULL) {
2448                 sop = alloc_init_open_stateowner(strhashval, clp, open);
2449                 if (sop == NULL)
2450                         return nfserr_jukebox;
2451                 open->op_stateowner = sop;
2452         }
2453         list_del_init(&sop->so_close_lru);
2454         renew_client(sop->so_client);
2455         return nfs_ok;
2456 }
2457
2458 static inline __be32
2459 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
2460 {
2461         if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
2462                 return nfserr_openmode;
2463         else
2464                 return nfs_ok;
2465 }
2466
2467 static struct nfs4_delegation *
2468 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
2469 {
2470         struct nfs4_delegation *dp;
2471
2472         spin_lock(&recall_lock);
2473         list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
2474                 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid) {
2475                         spin_unlock(&recall_lock);
2476                         return dp;
2477                 }
2478         spin_unlock(&recall_lock);
2479         return NULL;
2480 }
2481
2482 static int share_access_to_flags(u32 share_access)
2483 {
2484         share_access &= ~NFS4_SHARE_WANT_MASK;
2485
2486         return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
2487 }
2488
2489 static __be32
2490 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
2491                 struct nfs4_delegation **dp)
2492 {
2493         int flags;
2494         __be32 status = nfserr_bad_stateid;
2495
2496         *dp = find_delegation_file(fp, &open->op_delegate_stateid);
2497         if (*dp == NULL)
2498                 goto out;
2499         flags = share_access_to_flags(open->op_share_access);
2500         status = nfs4_check_delegmode(*dp, flags);
2501         if (status)
2502                 *dp = NULL;
2503 out:
2504         if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
2505                 return nfs_ok;
2506         if (status)
2507                 return status;
2508         open->op_stateowner->so_confirmed = 1;
2509         return nfs_ok;
2510 }
2511
2512 static __be32
2513 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
2514 {
2515         struct nfs4_stateid *local;
2516         __be32 status = nfserr_share_denied;
2517         struct nfs4_stateowner *sop = open->op_stateowner;
2518
2519         list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
2520                 /* ignore lock owners */
2521                 if (local->st_stateowner->so_is_open_owner == 0)
2522                         continue;
2523                 /* remember if we have seen this open owner */
2524                 if (local->st_stateowner == sop)
2525                         *stpp = local;
2526                 /* check for conflicting share reservations */
2527                 if (!test_share(local, open))
2528                         goto out;
2529         }
2530         status = 0;
2531 out:
2532         return status;
2533 }
2534
2535 static inline struct nfs4_stateid *
2536 nfs4_alloc_stateid(void)
2537 {
2538         return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
2539 }
2540
2541 static inline int nfs4_access_to_access(u32 nfs4_access)
2542 {
2543         int flags = 0;
2544
2545         if (nfs4_access & NFS4_SHARE_ACCESS_READ)
2546                 flags |= NFSD_MAY_READ;
2547         if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
2548                 flags |= NFSD_MAY_WRITE;
2549         return flags;
2550 }
2551
2552 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
2553                 struct svc_fh *cur_fh, struct nfsd4_open *open)
2554 {
2555         __be32 status;
2556         int oflag = nfs4_access_to_omode(open->op_share_access);
2557         int access = nfs4_access_to_access(open->op_share_access);
2558
2559         /* CLAIM_DELEGATE_CUR is used in response to a broken lease;
2560          * allowing it to break the lease and return EAGAIN leaves the
2561          * client unable to make progress in returning the delegation */
2562         if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2563                 access |= NFSD_MAY_NOT_BREAK_LEASE;
2564
2565         if (!fp->fi_fds[oflag]) {
2566                 status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
2567                         &fp->fi_fds[oflag]);
2568                 if (status)
2569                         return status;
2570         }
2571         nfs4_file_get_access(fp, oflag);
2572
2573         return nfs_ok;
2574 }
2575
2576 static __be32
2577 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
2578                 struct nfs4_file *fp, struct svc_fh *cur_fh,
2579                 struct nfsd4_open *open)
2580 {
2581         struct nfs4_stateid *stp;
2582         __be32 status;
2583
2584         stp = nfs4_alloc_stateid();
2585         if (stp == NULL)
2586                 return nfserr_jukebox;
2587
2588         status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2589         if (status) {
2590                 kmem_cache_free(stateid_slab, stp);
2591                 return status;
2592         }
2593         *stpp = stp;
2594         return 0;
2595 }
2596
2597 static inline __be32
2598 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
2599                 struct nfsd4_open *open)
2600 {
2601         struct iattr iattr = {
2602                 .ia_valid = ATTR_SIZE,
2603                 .ia_size = 0,
2604         };
2605         if (!open->op_truncate)
2606                 return 0;
2607         if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
2608                 return nfserr_inval;
2609         return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
2610 }
2611
2612 static __be32
2613 nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
2614 {
2615         u32 op_share_access = open->op_share_access & ~NFS4_SHARE_WANT_MASK;
2616         bool new_access;
2617         __be32 status;
2618
2619         new_access = !test_bit(op_share_access, &stp->st_access_bmap);
2620         if (new_access) {
2621                 status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
2622                 if (status)
2623                         return status;
2624         }
2625         status = nfsd4_truncate(rqstp, cur_fh, open);
2626         if (status) {
2627                 if (new_access) {
2628                         int oflag = nfs4_access_to_omode(op_share_access);
2629                         nfs4_file_put_access(fp, oflag);
2630                 }
2631                 return status;
2632         }
2633         /* remember the open */
2634         __set_bit(op_share_access, &stp->st_access_bmap);
2635         __set_bit(open->op_share_deny, &stp->st_deny_bmap);
2636
2637         return nfs_ok;
2638 }
2639
2640
2641 static void
2642 nfs4_set_claim_prev(struct nfsd4_open *open)
2643 {
2644         open->op_stateowner->so_confirmed = 1;
2645         open->op_stateowner->so_client->cl_firststate = 1;
2646 }
2647
2648 /* Should we give out recallable state?: */
2649 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
2650 {
2651         if (clp->cl_cb_state == NFSD4_CB_UP)
2652                 return true;
2653         /*
2654          * In the sessions case, since we don't have to establish a
2655          * separate connection for callbacks, we assume it's OK
2656          * until we hear otherwise:
2657          */
2658         return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
2659 }
2660
2661 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
2662 {
2663         struct file_lock *fl;
2664
2665         fl = locks_alloc_lock();
2666         if (!fl)
2667                 return NULL;
2668         locks_init_lock(fl);
2669         fl->fl_lmops = &nfsd_lease_mng_ops;
2670         fl->fl_flags = FL_LEASE;
2671         fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
2672         fl->fl_end = OFFSET_MAX;
2673         fl->fl_owner = (fl_owner_t)(dp->dl_file);
2674         fl->fl_pid = current->tgid;
2675         return fl;
2676 }
2677
2678 static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
2679 {
2680         struct nfs4_file *fp = dp->dl_file;
2681         struct file_lock *fl;
2682         int status;
2683
2684         fl = nfs4_alloc_init_lease(dp, flag);
2685         if (!fl)
2686                 return -ENOMEM;
2687         fl->fl_file = find_readable_file(fp);
2688         list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2689         status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
2690         if (status) {
2691                 list_del_init(&dp->dl_perclnt);
2692                 locks_free_lock(fl);
2693                 return -ENOMEM;
2694         }
2695         fp->fi_lease = fl;
2696         fp->fi_deleg_file = fl->fl_file;
2697         get_file(fp->fi_deleg_file);
2698         atomic_set(&fp->fi_delegees, 1);
2699         list_add(&dp->dl_perfile, &fp->fi_delegations);
2700         return 0;
2701 }
2702
2703 static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
2704 {
2705         struct nfs4_file *fp = dp->dl_file;
2706
2707         if (!fp->fi_lease)
2708                 return nfs4_setlease(dp, flag);
2709         spin_lock(&recall_lock);
2710         if (fp->fi_had_conflict) {
2711                 spin_unlock(&recall_lock);
2712                 return -EAGAIN;
2713         }
2714         atomic_inc(&fp->fi_delegees);
2715         list_add(&dp->dl_perfile, &fp->fi_delegations);
2716         spin_unlock(&recall_lock);
2717         list_add(&dp->dl_perclnt, &dp->dl_client->cl_delegations);
2718         return 0;
2719 }
2720
2721 /*
2722  * Attempt to hand out a delegation.
2723  */
2724 static void
2725 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
2726 {
2727         struct nfs4_delegation *dp;
2728         struct nfs4_stateowner *sop = stp->st_stateowner;
2729         int cb_up;
2730         int status, flag = 0;
2731
2732         cb_up = nfsd4_cb_channel_good(sop->so_client);
2733         flag = NFS4_OPEN_DELEGATE_NONE;
2734         open->op_recall = 0;
2735         switch (open->op_claim_type) {
2736                 case NFS4_OPEN_CLAIM_PREVIOUS:
2737                         if (!cb_up)
2738                                 open->op_recall = 1;
2739                         flag = open->op_delegate_type;
2740                         if (flag == NFS4_OPEN_DELEGATE_NONE)
2741                                 goto out;
2742                         break;
2743                 case NFS4_OPEN_CLAIM_NULL:
2744                         /* Let's not give out any delegations till everyone's
2745                          * had the chance to reclaim theirs.... */
2746                         if (locks_in_grace())
2747                                 goto out;
2748                         if (!cb_up || !sop->so_confirmed)
2749                                 goto out;
2750                         if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
2751                                 flag = NFS4_OPEN_DELEGATE_WRITE;
2752                         else
2753                                 flag = NFS4_OPEN_DELEGATE_READ;
2754                         break;
2755                 default:
2756                         goto out;
2757         }
2758
2759         dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
2760         if (dp == NULL)
2761                 goto out_no_deleg;
2762         status = nfs4_set_delegation(dp, flag);
2763         if (status)
2764                 goto out_free;
2765
2766         memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
2767
2768         dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
2769                 STATEID_VAL(&dp->dl_stateid));
2770 out:
2771         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
2772                         && flag == NFS4_OPEN_DELEGATE_NONE
2773                         && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
2774                 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
2775         open->op_delegate_type = flag;
2776         return;
2777 out_free:
2778         nfs4_put_delegation(dp);
2779 out_no_deleg:
2780         flag = NFS4_OPEN_DELEGATE_NONE;
2781         goto out;
2782 }
2783
2784 /*
2785  * called with nfs4_lock_state() held.
2786  */
2787 __be32
2788 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
2789 {
2790         struct nfsd4_compoundres *resp = rqstp->rq_resp;
2791         struct nfs4_file *fp = NULL;
2792         struct inode *ino = current_fh->fh_dentry->d_inode;
2793         struct nfs4_stateid *stp = NULL;
2794         struct nfs4_delegation *dp = NULL;
2795         __be32 status;
2796
2797         status = nfserr_inval;
2798         if (!access_valid(open->op_share_access, resp->cstate.minorversion)
2799                         || !deny_valid(open->op_share_deny))
2800                 goto out;
2801         /*
2802          * Lookup file; if found, lookup stateid and check open request,
2803          * and check for delegations in the process of being recalled.
2804          * If not found, create the nfs4_file struct
2805          */
2806         fp = find_file(ino);
2807         if (fp) {
2808                 if ((status = nfs4_check_open(fp, open, &stp)))
2809                         goto out;
2810                 status = nfs4_check_deleg(fp, open, &dp);
2811                 if (status)
2812                         goto out;
2813         } else {
2814                 status = nfserr_bad_stateid;
2815                 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
2816                         goto out;
2817                 status = nfserr_jukebox;
2818                 fp = alloc_init_file(ino);
2819                 if (fp == NULL)
2820                         goto out;
2821         }
2822
2823         /*
2824          * OPEN the file, or upgrade an existing OPEN.
2825          * If truncate fails, the OPEN fails.
2826          */
2827         if (stp) {
2828                 /* Stateid was found, this is an OPEN upgrade */
2829                 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
2830                 if (status)
2831                         goto out;
2832                 update_stateid(&stp->st_stateid);
2833         } else {
2834                 status = nfs4_new_open(rqstp, &stp, fp, current_fh, open);
2835                 if (status)
2836                         goto out;
2837                 init_stateid(stp, fp, open);
2838                 status = nfsd4_truncate(rqstp, current_fh, open);
2839                 if (status) {
2840                         release_open_stateid(stp);
2841                         goto out;
2842                 }
2843                 if (nfsd4_has_session(&resp->cstate))
2844                         update_stateid(&stp->st_stateid);
2845         }
2846         memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
2847
2848         if (nfsd4_has_session(&resp->cstate))
2849                 open->op_stateowner->so_confirmed = 1;
2850
2851         /*
2852         * Attempt to hand out a delegation. No error return, because the
2853         * OPEN succeeds even if we fail.
2854         */
2855         nfs4_open_delegation(current_fh, open, stp);
2856
2857         status = nfs_ok;
2858
2859         dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
2860                 STATEID_VAL(&stp->st_stateid));
2861 out:
2862         if (fp)
2863                 put_nfs4_file(fp);
2864         if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
2865                 nfs4_set_claim_prev(open);
2866         /*
2867         * To finish the open response, we just need to set the rflags.
2868         */
2869         open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
2870         if (!open->op_stateowner->so_confirmed &&
2871             !nfsd4_has_session(&resp->cstate))
2872                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
2873
2874         return status;
2875 }
2876
2877 __be32
2878 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2879             clientid_t *clid)
2880 {
2881         struct nfs4_client *clp;
2882         __be32 status;
2883
2884         nfs4_lock_state();
2885         dprintk("process_renew(%08x/%08x): starting\n", 
2886                         clid->cl_boot, clid->cl_id);
2887         status = nfserr_stale_clientid;
2888         if (STALE_CLIENTID(clid))
2889                 goto out;
2890         clp = find_confirmed_client(clid);
2891         status = nfserr_expired;
2892         if (clp == NULL) {
2893                 /* We assume the client took too long to RENEW. */
2894                 dprintk("nfsd4_renew: clientid not found!\n");
2895                 goto out;
2896         }
2897         renew_client(clp);
2898         status = nfserr_cb_path_down;
2899         if (!list_empty(&clp->cl_delegations)
2900                         && clp->cl_cb_state != NFSD4_CB_UP)
2901                 goto out;
2902         status = nfs_ok;
2903 out:
2904         nfs4_unlock_state();
2905         return status;
2906 }
2907
2908 static struct lock_manager nfsd4_manager = {
2909 };
2910
2911 static void
2912 nfsd4_end_grace(void)
2913 {
2914         dprintk("NFSD: end of grace period\n");
2915         nfsd4_recdir_purge_old();
2916         locks_end_grace(&nfsd4_manager);
2917         /*
2918          * Now that every NFSv4 client has had the chance to recover and
2919          * to see the (possibly new, possibly shorter) lease time, we
2920          * can safely set the next grace time to the current lease time:
2921          */
2922         nfsd4_grace = nfsd4_lease;
2923 }
2924
2925 static time_t
2926 nfs4_laundromat(void)
2927 {
2928         struct nfs4_client *clp;
2929         struct nfs4_stateowner *sop;
2930         struct nfs4_delegation *dp;
2931         struct list_head *pos, *next, reaplist;
2932         time_t cutoff = get_seconds() - nfsd4_lease;
2933         time_t t, clientid_val = nfsd4_lease;
2934         time_t u, test_val = nfsd4_lease;
2935
2936         nfs4_lock_state();
2937
2938         dprintk("NFSD: laundromat service - starting\n");
2939         if (locks_in_grace())
2940                 nfsd4_end_grace();
2941         INIT_LIST_HEAD(&reaplist);
2942         spin_lock(&client_lock);
2943         list_for_each_safe(pos, next, &client_lru) {
2944                 clp = list_entry(pos, struct nfs4_client, cl_lru);
2945                 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
2946                         t = clp->cl_time - cutoff;
2947                         if (clientid_val > t)
2948                                 clientid_val = t;
2949                         break;
2950                 }
2951                 if (atomic_read(&clp->cl_refcount)) {
2952                         dprintk("NFSD: client in use (clientid %08x)\n",
2953                                 clp->cl_clientid.cl_id);
2954                         continue;
2955                 }
2956                 unhash_client_locked(clp);
2957                 list_add(&clp->cl_lru, &reaplist);
2958         }
2959         spin_unlock(&client_lock);
2960         list_for_each_safe(pos, next, &reaplist) {
2961                 clp = list_entry(pos, struct nfs4_client, cl_lru);
2962                 dprintk("NFSD: purging unused client (clientid %08x)\n",
2963                         clp->cl_clientid.cl_id);
2964                 nfsd4_remove_clid_dir(clp);
2965                 expire_client(clp);
2966         }
2967         spin_lock(&recall_lock);
2968         list_for_each_safe(pos, next, &del_recall_lru) {
2969                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2970                 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
2971                         u = dp->dl_time - cutoff;
2972                         if (test_val > u)
2973                                 test_val = u;
2974                         break;
2975                 }
2976                 list_move(&dp->dl_recall_lru, &reaplist);
2977         }
2978         spin_unlock(&recall_lock);
2979         list_for_each_safe(pos, next, &reaplist) {
2980                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
2981                 list_del_init(&dp->dl_recall_lru);
2982                 unhash_delegation(dp);
2983         }
2984         test_val = nfsd4_lease;
2985         list_for_each_safe(pos, next, &close_lru) {
2986                 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
2987                 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
2988                         u = sop->so_time - cutoff;
2989                         if (test_val > u)
2990                                 test_val = u;
2991                         break;
2992                 }
2993                 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
2994                         sop->so_id);
2995                 release_openowner(sop);
2996         }
2997         if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
2998                 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
2999         nfs4_unlock_state();
3000         return clientid_val;
3001 }
3002
3003 static struct workqueue_struct *laundry_wq;
3004 static void laundromat_main(struct work_struct *);
3005 static DECLARE_DELAYED_WORK(laundromat_work, laundromat_main);
3006
3007 static void
3008 laundromat_main(struct work_struct *not_used)
3009 {
3010         time_t t;
3011
3012         t = nfs4_laundromat();
3013         dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
3014         queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
3015 }
3016
3017 static struct nfs4_stateowner *
3018 search_close_lru(u32 st_id, int flags)
3019 {
3020         struct nfs4_stateowner *local = NULL;
3021
3022         if (flags & CLOSE_STATE) {
3023                 list_for_each_entry(local, &close_lru, so_close_lru) {
3024                         if (local->so_id == st_id)
3025                                 return local;
3026                 }
3027         }
3028         return NULL;
3029 }
3030
3031 static inline int
3032 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
3033 {
3034         return fhp->fh_dentry->d_inode != stp->st_file->fi_inode;
3035 }
3036
3037 static int
3038 STALE_STATEID(stateid_t *stateid)
3039 {
3040         if (stateid->si_boot == boot_time)
3041                 return 0;
3042         dprintk("NFSD: stale stateid " STATEID_FMT "!\n",
3043                 STATEID_VAL(stateid));
3044         return 1;
3045 }
3046
3047 static inline int
3048 access_permit_read(unsigned long access_bmap)
3049 {
3050         return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
3051                 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
3052                 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
3053 }
3054
3055 static inline int
3056 access_permit_write(unsigned long access_bmap)
3057 {
3058         return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
3059                 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
3060 }
3061
3062 static
3063 __be32 nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
3064 {
3065         __be32 status = nfserr_openmode;
3066
3067         /* For lock stateid's, we test the parent open, not the lock: */
3068         if (stp->st_openstp)
3069                 stp = stp->st_openstp;
3070         if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
3071                 goto out;
3072         if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
3073                 goto out;
3074         status = nfs_ok;
3075 out:
3076         return status;
3077 }
3078
3079 static inline __be32
3080 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
3081 {
3082         if (ONE_STATEID(stateid) && (flags & RD_STATE))
3083                 return nfs_ok;
3084         else if (locks_in_grace()) {
3085                 /* Answer in remaining cases depends on existence of
3086                  * conflicting state; so we must wait out the grace period. */
3087                 return nfserr_grace;
3088         } else if (flags & WR_STATE)
3089                 return nfs4_share_conflict(current_fh,
3090                                 NFS4_SHARE_DENY_WRITE);
3091         else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
3092                 return nfs4_share_conflict(current_fh,
3093                                 NFS4_SHARE_DENY_READ);
3094 }
3095
3096 /*
3097  * Allow READ/WRITE during grace period on recovered state only for files
3098  * that are not able to provide mandatory locking.
3099  */
3100 static inline int
3101 grace_disallows_io(struct inode *inode)
3102 {
3103         return locks_in_grace() && mandatory_lock(inode);
3104 }
3105
3106 static int check_stateid_generation(stateid_t *in, stateid_t *ref, int flags)
3107 {
3108         /*
3109          * When sessions are used the stateid generation number is ignored
3110          * when it is zero.
3111          */
3112         if ((flags & HAS_SESSION) && in->si_generation == 0)
3113                 goto out;
3114
3115         /* If the client sends us a stateid from the future, it's buggy: */
3116         if (in->si_generation > ref->si_generation)
3117                 return nfserr_bad_stateid;
3118         /*
3119          * The following, however, can happen.  For example, if the
3120          * client sends an open and some IO at the same time, the open
3121          * may bump si_generation while the IO is still in flight.
3122          * Thanks to hard links and renames, the client never knows what
3123          * file an open will affect.  So it could avoid that situation
3124          * only by serializing all opens and IO from the same open
3125          * owner.  To recover from the old_stateid error, the client
3126          * will just have to retry the IO:
3127          */
3128         if (in->si_generation < ref->si_generation)
3129                 return nfserr_old_stateid;
3130 out:
3131         return nfs_ok;
3132 }
3133
3134 static int is_delegation_stateid(stateid_t *stateid)
3135 {
3136         return stateid->si_fileid == 0;
3137 }
3138
3139 /*
3140 * Checks for stateid operations
3141 */
3142 __be32
3143 nfs4_preprocess_stateid_op(struct nfsd4_compound_state *cstate,
3144                            stateid_t *stateid, int flags, struct file **filpp)
3145 {
3146         struct nfs4_stateid *stp = NULL;
3147         struct nfs4_delegation *dp = NULL;
3148         struct svc_fh *current_fh = &cstate->current_fh;
3149         struct inode *ino = current_fh->fh_dentry->d_inode;
3150         __be32 status;
3151
3152         if (filpp)
3153                 *filpp = NULL;
3154
3155         if (grace_disallows_io(ino))
3156                 return nfserr_grace;
3157
3158         if (nfsd4_has_session(cstate))
3159                 flags |= HAS_SESSION;
3160
3161         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3162                 return check_special_stateids(current_fh, stateid, flags);
3163
3164         status = nfserr_stale_stateid;
3165         if (STALE_STATEID(stateid)) 
3166                 goto out;
3167
3168         /*
3169          * We assume that any stateid that has the current boot time,
3170          * but that we can't find, is expired:
3171          */
3172         status = nfserr_expired;
3173         if (is_delegation_stateid(stateid)) {
3174                 dp = find_delegation_stateid(ino, stateid);
3175                 if (!dp)
3176                         goto out;
3177                 status = check_stateid_generation(stateid, &dp->dl_stateid,
3178                                                   flags);
3179                 if (status)
3180                         goto out;
3181                 status = nfs4_check_delegmode(dp, flags);
3182                 if (status)
3183                         goto out;
3184                 renew_client(dp->dl_client);
3185                 if (filpp) {
3186                         *filpp = dp->dl_file->fi_deleg_file;
3187                         BUG_ON(!*filpp);
3188                 }
3189         } else { /* open or lock stateid */
3190                 stp = find_stateid(stateid, flags);
3191                 if (!stp)
3192                         goto out;
3193                 status = nfserr_bad_stateid;
3194                 if (nfs4_check_fh(current_fh, stp))
3195                         goto out;
3196                 if (!stp->st_stateowner->so_confirmed)
3197                         goto out;
3198                 status = check_stateid_generation(stateid, &stp->st_stateid,
3199                                                   flags);
3200                 if (status)
3201                         goto out;
3202                 status = nfs4_check_openmode(stp, flags);
3203                 if (status)
3204                         goto out;
3205                 renew_client(stp->st_stateowner->so_client);
3206                 if (filpp) {
3207                         if (flags & RD_STATE)
3208                                 *filpp = find_readable_file(stp->st_file);
3209                         else
3210                                 *filpp = find_writeable_file(stp->st_file);
3211                 }
3212         }
3213         status = nfs_ok;
3214 out:
3215         return status;
3216 }
3217
3218 static inline int
3219 setlkflg (int type)
3220 {
3221         return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
3222                 RD_STATE : WR_STATE;
3223 }
3224
3225 /* 
3226  * Checks for sequence id mutating operations. 
3227  */
3228 static __be32
3229 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
3230                          stateid_t *stateid, int flags,
3231                          struct nfs4_stateowner **sopp,
3232                          struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
3233 {
3234         struct nfs4_stateid *stp;
3235         struct nfs4_stateowner *sop;
3236         struct svc_fh *current_fh = &cstate->current_fh;
3237         __be32 status;
3238
3239         dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
3240                 seqid, STATEID_VAL(stateid));
3241
3242         *stpp = NULL;
3243         *sopp = NULL;
3244
3245         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
3246                 dprintk("NFSD: preprocess_seqid_op: magic stateid!\n");
3247                 return nfserr_bad_stateid;
3248         }
3249
3250         if (STALE_STATEID(stateid))
3251                 return nfserr_stale_stateid;
3252
3253         if (nfsd4_has_session(cstate))
3254                 flags |= HAS_SESSION;
3255
3256         /*
3257         * We return BAD_STATEID if filehandle doesn't match stateid, 
3258         * the confirmed flag is incorrecly set, or the generation 
3259         * number is incorrect.  
3260         */
3261         stp = find_stateid(stateid, flags);
3262         if (stp == NULL) {
3263                 /*
3264                  * Also, we should make sure this isn't just the result of
3265                  * a replayed close:
3266                  */
3267                 sop = search_close_lru(stateid->si_stateownerid, flags);
3268                 /* It's not stale; let's assume it's expired: */
3269                 if (sop == NULL)
3270                         return nfserr_expired;
3271                 *sopp = sop;
3272                 goto check_replay;
3273         }
3274
3275         *stpp = stp;
3276         *sopp = sop = stp->st_stateowner;
3277
3278         if (lock) {
3279                 clientid_t *lockclid = &lock->v.new.clientid;
3280                 struct nfs4_client *clp = sop->so_client;
3281                 int lkflg = 0;
3282                 __be32 status;
3283
3284                 lkflg = setlkflg(lock->lk_type);
3285
3286                 if (lock->lk_is_new) {
3287                         if (!sop->so_is_open_owner)
3288                                 return nfserr_bad_stateid;
3289                         if (!(flags & HAS_SESSION) &&
3290                             !same_clid(&clp->cl_clientid, lockclid))
3291                                 return nfserr_bad_stateid;
3292                         /* stp is the open stateid */
3293                         status = nfs4_check_openmode(stp, lkflg);
3294                         if (status)
3295                                 return status;
3296                 } else {
3297                         /* stp is the lock stateid */
3298                         status = nfs4_check_openmode(stp->st_openstp, lkflg);
3299                         if (status)
3300                                 return status;
3301                }
3302         }
3303
3304         if (nfs4_check_fh(current_fh, stp)) {
3305                 dprintk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
3306                 return nfserr_bad_stateid;
3307         }
3308
3309         /*
3310         *  We now validate the seqid and stateid generation numbers.
3311         *  For the moment, we ignore the possibility of 
3312         *  generation number wraparound.
3313         */
3314         if (!(flags & HAS_SESSION) && seqid != sop->so_seqid)
3315                 goto check_replay;
3316
3317         if (sop->so_confirmed && flags & CONFIRM) {
3318                 dprintk("NFSD: preprocess_seqid_op: expected"
3319                                 " unconfirmed stateowner!\n");
3320                 return nfserr_bad_stateid;
3321         }
3322         if (!sop->so_confirmed && !(flags & CONFIRM)) {
3323                 dprintk("NFSD: preprocess_seqid_op: stateowner not"
3324                                 " confirmed yet!\n");
3325                 return nfserr_bad_stateid;
3326         }
3327         status = check_stateid_generation(stateid, &stp->st_stateid, flags);
3328         if (status)
3329                 return status;
3330         renew_client(sop->so_client);
3331         return nfs_ok;
3332
3333 check_replay:
3334         if (seqid == sop->so_seqid - 1) {
3335                 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
3336                 /* indicate replay to calling function */
3337                 return nfserr_replay_me;
3338         }
3339         dprintk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
3340                         sop->so_seqid, seqid);
3341         *sopp = NULL;
3342         return nfserr_bad_seqid;
3343 }
3344
3345 __be32
3346 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3347                    struct nfsd4_open_confirm *oc)
3348 {
3349         __be32 status;
3350         struct nfs4_stateowner *sop;
3351         struct nfs4_stateid *stp;
3352
3353         dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
3354                         (int)cstate->current_fh.fh_dentry->d_name.len,
3355                         cstate->current_fh.fh_dentry->d_name.name);
3356
3357         status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
3358         if (status)
3359                 return status;
3360
3361         nfs4_lock_state();
3362
3363         if ((status = nfs4_preprocess_seqid_op(cstate,
3364                                         oc->oc_seqid, &oc->oc_req_stateid,
3365                                         CONFIRM | OPEN_STATE,
3366                                         &oc->oc_stateowner, &stp, NULL)))
3367                 goto out; 
3368
3369         sop = oc->oc_stateowner;
3370         sop->so_confirmed = 1;
3371         update_stateid(&stp->st_stateid);
3372         memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
3373         dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
3374                 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stateid));
3375
3376         nfsd4_create_clid_dir(sop->so_client);
3377 out:
3378         if (oc->oc_stateowner) {
3379                 nfs4_get_stateowner(oc->oc_stateowner);
3380                 cstate->replay_owner = oc->oc_stateowner;
3381         }
3382         nfs4_unlock_state();
3383         return status;
3384 }
3385
3386 static inline void nfs4_file_downgrade(struct nfs4_stateid *stp, unsigned int to_access)
3387 {
3388         int i;
3389
3390         for (i = 1; i < 4; i++) {
3391                 if (test_bit(i, &stp->st_access_bmap)
3392                                         && ((i & to_access) != i)) {
3393                         nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(i));
3394                         __clear_bit(i, &stp->st_access_bmap);
3395                 }
3396         }
3397 }
3398
3399 static void
3400 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
3401 {
3402         int i;
3403         for (i = 0; i < 4; i++) {
3404                 if ((i & deny) != i)
3405                         __clear_bit(i, bmap);
3406         }
3407 }
3408
3409 __be32
3410 nfsd4_open_downgrade(struct svc_rqst *rqstp,
3411                      struct nfsd4_compound_state *cstate,
3412                      struct nfsd4_open_downgrade *od)
3413 {
3414         __be32 status;
3415         struct nfs4_stateid *stp;
3416
3417         dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", 
3418                         (int)cstate->current_fh.fh_dentry->d_name.len,
3419                         cstate->current_fh.fh_dentry->d_name.name);
3420
3421         if (!access_valid(od->od_share_access, cstate->minorversion)
3422                         || !deny_valid(od->od_share_deny))
3423                 return nfserr_inval;
3424
3425         nfs4_lock_state();
3426         if ((status = nfs4_preprocess_seqid_op(cstate,
3427                                         od->od_seqid,
3428                                         &od->od_stateid, 
3429                                         OPEN_STATE,
3430                                         &od->od_stateowner, &stp, NULL)))
3431                 goto out; 
3432
3433         status = nfserr_inval;
3434         if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
3435                 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
3436                         stp->st_access_bmap, od->od_share_access);
3437                 goto out;
3438         }
3439         if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
3440                 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
3441                         stp->st_deny_bmap, od->od_share_deny);
3442                 goto out;
3443         }
3444         nfs4_file_downgrade(stp, od->od_share_access);
3445
3446         reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
3447
3448         update_stateid(&stp->st_stateid);
3449         memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
3450         status = nfs_ok;
3451 out:
3452         if (od->od_stateowner) {
3453                 nfs4_get_stateowner(od->od_stateowner);
3454                 cstate->replay_owner = od->od_stateowner;
3455         }
3456         nfs4_unlock_state();
3457         return status;
3458 }
3459
3460 /*
3461  * nfs4_unlock_state() called after encode
3462  */
3463 __be32
3464 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3465             struct nfsd4_close *close)
3466 {
3467         __be32 status;
3468         struct nfs4_stateid *stp;
3469
3470         dprintk("NFSD: nfsd4_close on file %.*s\n", 
3471                         (int)cstate->current_fh.fh_dentry->d_name.len,
3472                         cstate->current_fh.fh_dentry->d_name.name);
3473
3474         nfs4_lock_state();
3475         /* check close_lru for replay */
3476         if ((status = nfs4_preprocess_seqid_op(cstate,
3477                                         close->cl_seqid,
3478                                         &close->cl_stateid, 
3479                                         OPEN_STATE | CLOSE_STATE,
3480                                         &close->cl_stateowner, &stp, NULL)))
3481                 goto out; 
3482         status = nfs_ok;
3483         update_stateid(&stp->st_stateid);
3484         memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
3485
3486         /* release_stateid() calls nfsd_close() if needed */
3487         release_open_stateid(stp);
3488
3489         /* place unused nfs4_stateowners on so_close_lru list to be
3490          * released by the laundromat service after the lease period
3491          * to enable us to handle CLOSE replay
3492          */
3493         if (list_empty(&close->cl_stateowner->so_stateids))
3494                 move_to_close_lru(close->cl_stateowner);
3495 out:
3496         if (close->cl_stateowner) {
3497                 nfs4_get_stateowner(close->cl_stateowner);
3498                 cstate->replay_owner = close->cl_stateowner;
3499         }
3500         nfs4_unlock_state();
3501         return status;
3502 }
3503
3504 __be32
3505 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3506                   struct nfsd4_delegreturn *dr)
3507 {
3508         struct nfs4_delegation *dp;
3509         stateid_t *stateid = &dr->dr_stateid;
3510         struct inode *inode;
3511         __be32 status;
3512         int flags = 0;
3513
3514         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
3515                 return status;
3516         inode = cstate->current_fh.fh_dentry->d_inode;
3517
3518         if (nfsd4_has_session(cstate))
3519                 flags |= HAS_SESSION;
3520         nfs4_lock_state();
3521         status = nfserr_bad_stateid;
3522         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
3523                 goto out;
3524         status = nfserr_stale_stateid;
3525         if (STALE_STATEID(stateid))
3526                 goto out;
3527         status = nfserr_bad_stateid;
3528         if (!is_delegation_stateid(stateid))
3529                 goto out;
3530         status = nfserr_expired;
3531         dp = find_delegation_stateid(inode, stateid);
3532         if (!dp)
3533                 goto out;
3534         status = check_stateid_generation(stateid, &dp->dl_stateid, flags);
3535         if (status)
3536                 goto out;
3537         renew_client(dp->dl_client);
3538
3539         unhash_delegation(dp);
3540 out:
3541         nfs4_unlock_state();
3542
3543         return status;
3544 }
3545
3546
3547 /* 
3548  * Lock owner state (byte-range locks)
3549  */
3550 #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
3551 #define LOCK_HASH_BITS              8
3552 #define LOCK_HASH_SIZE             (1 << LOCK_HASH_BITS)
3553 #define LOCK_HASH_MASK             (LOCK_HASH_SIZE - 1)
3554
3555 static inline u64
3556 end_offset(u64 start, u64 len)
3557 {
3558         u64 end;
3559
3560         end = start + len;
3561         return end >= start ? end: NFS4_MAX_UINT64;
3562 }
3563
3564 /* last octet in a range */
3565 static inline u64
3566 last_byte_offset(u64 start, u64 len)
3567 {
3568         u64 end;
3569
3570         BUG_ON(!len);
3571         end = start + len;
3572         return end > start ? end - 1: NFS4_MAX_UINT64;
3573 }
3574
3575 #define lockownerid_hashval(id) \
3576         ((id) & LOCK_HASH_MASK)
3577
3578 static inline unsigned int
3579 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
3580                 struct xdr_netobj *ownername)
3581 {
3582         return (file_hashval(inode) + cl_id
3583                         + opaque_hashval(ownername->data, ownername->len))
3584                 & LOCK_HASH_MASK;
3585 }
3586
3587 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
3588 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
3589 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
3590
3591 static struct nfs4_stateid *
3592 find_stateid(stateid_t *stid, int flags)
3593 {
3594         struct nfs4_stateid *local;
3595         u32 st_id = stid->si_stateownerid;
3596         u32 f_id = stid->si_fileid;
3597         unsigned int hashval;
3598
3599         dprintk("NFSD: find_stateid flags 0x%x\n",flags);
3600         if (flags & (LOCK_STATE | RD_STATE | WR_STATE)) {
3601                 hashval = stateid_hashval(st_id, f_id);
3602                 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
3603                         if ((local->st_stateid.si_stateownerid == st_id) &&
3604                             (local->st_stateid.si_fileid == f_id))
3605                                 return local;
3606                 }
3607         } 
3608
3609         if (flags & (OPEN_STATE | RD_STATE | WR_STATE)) {
3610                 hashval = stateid_hashval(st_id, f_id);
3611                 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
3612                         if ((local->st_stateid.si_stateownerid == st_id) &&
3613                             (local->st_stateid.si_fileid == f_id))
3614                                 return local;
3615                 }
3616         }
3617         return NULL;
3618 }
3619
3620 static struct nfs4_delegation *
3621 find_delegation_stateid(struct inode *ino, stateid_t *stid)
3622 {
3623         struct nfs4_file *fp;
3624         struct nfs4_delegation *dl;
3625
3626         dprintk("NFSD: %s: stateid=" STATEID_FMT "\n", __func__,
3627                 STATEID_VAL(stid));
3628
3629         fp = find_file(ino);
3630         if (!fp)
3631                 return NULL;
3632         dl = find_delegation_file(fp, stid);
3633         put_nfs4_file(fp);
3634         return dl;
3635 }
3636
3637 /*
3638  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
3639  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
3640  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
3641  * locking, this prevents us from being completely protocol-compliant.  The
3642  * real solution to this problem is to start using unsigned file offsets in
3643  * the VFS, but this is a very deep change!
3644  */
3645 static inline void
3646 nfs4_transform_lock_offset(struct file_lock *lock)
3647 {
3648         if (lock->fl_start < 0)
3649                 lock->fl_start = OFFSET_MAX;
3650         if (lock->fl_end < 0)
3651                 lock->fl_end = OFFSET_MAX;
3652 }
3653
3654 /* Hack!: For now, we're defining this just so we can use a pointer to it
3655  * as a unique cookie to identify our (NFSv4's) posix locks. */
3656 static const struct lock_manager_operations nfsd_posix_mng_ops  = {
3657 };
3658
3659 static inline void
3660 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
3661 {
3662         struct nfs4_stateowner *sop;
3663
3664         if (fl->fl_lmops == &nfsd_posix_mng_ops) {
3665                 sop = (struct nfs4_stateowner *) fl->fl_owner;
3666                 kref_get(&sop->so_ref);
3667                 deny->ld_sop = sop;
3668                 deny->ld_clientid = sop->so_client->cl_clientid;
3669         } else {
3670                 deny->ld_sop = NULL;
3671                 deny->ld_clientid.cl_boot = 0;
3672                 deny->ld_clientid.cl_id = 0;
3673         }
3674         deny->ld_start = fl->fl_start;
3675         deny->ld_length = NFS4_MAX_UINT64;
3676         if (fl->fl_end != NFS4_MAX_UINT64)
3677                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
3678         deny->ld_type = NFS4_READ_LT;
3679         if (fl->fl_type != F_RDLCK)
3680                 deny->ld_type = NFS4_WRITE_LT;
3681 }
3682
3683 static struct nfs4_stateowner *
3684 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
3685                 struct xdr_netobj *owner)
3686 {
3687         unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
3688         struct nfs4_stateowner *op;
3689
3690         list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
3691                 if (same_owner_str(op, owner, clid))
3692                         return op;
3693         }
3694         return NULL;
3695 }
3696
3697 /*
3698  * Alloc a lock owner structure.
3699  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
3700  * occurred. 
3701  *
3702  * strhashval = lock_ownerstr_hashval 
3703  */
3704
3705 static struct nfs4_stateowner *
3706 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
3707         struct nfs4_stateowner *sop;
3708         struct nfs4_replay *rp;
3709         unsigned int idhashval;
3710
3711         if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
3712                 return NULL;
3713         idhashval = lockownerid_hashval(current_ownerid);
3714         INIT_LIST_HEAD(&sop->so_idhash);
3715         INIT_LIST_HEAD(&sop->so_strhash);
3716         INIT_LIST_HEAD(&sop->so_perclient);
3717         INIT_LIST_HEAD(&sop->so_stateids);
3718         INIT_LIST_HEAD(&sop->so_perstateid);
3719         INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
3720         sop->so_time = 0;
3721         list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
3722         list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
3723         list_add(&sop->so_perstateid, &open_stp->st_lockowners);
3724         sop->so_is_open_owner = 0;
3725         sop->so_id = current_ownerid++;
3726         sop->so_client = clp;
3727         /* It is the openowner seqid that will be incremented in encode in the
3728          * case of new lockowners; so increment the lock seqid manually: */
3729         sop->so_seqid = lock->lk_new_lock_seqid + 1;
3730         sop->so_confirmed = 1;
3731         rp = &sop->so_replay;
3732         rp->rp_status = nfserr_serverfault;
3733         rp->rp_buflen = 0;
3734         rp->rp_buf = rp->rp_ibuf;
3735         return sop;
3736 }
3737
3738 static struct nfs4_stateid *
3739 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
3740 {
3741         struct nfs4_stateid *stp;
3742         unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
3743
3744         stp = nfs4_alloc_stateid();
3745         if (stp == NULL)
3746                 goto out;
3747         INIT_LIST_HEAD(&stp->st_hash);
3748         INIT_LIST_HEAD(&stp->st_perfile);
3749         INIT_LIST_HEAD(&stp->st_perstateowner);
3750         INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
3751         list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
3752         list_add(&stp->st_perfile, &fp->fi_stateids);
3753         list_add(&stp->st_perstateowner, &sop->so_stateids);
3754         stp->st_stateowner = sop;
3755         get_nfs4_file(fp);
3756         stp->st_file = fp;
3757         stp->st_stateid.si_boot = boot_time;
3758         stp->st_stateid.si_stateownerid = sop->so_id;
3759         stp->st_stateid.si_fileid = fp->fi_id;
3760         stp->st_stateid.si_generation = 0;
3761         stp->st_access_bmap = 0;
3762         stp->st_deny_bmap = open_stp->st_deny_bmap;
3763         stp->st_openstp = open_stp;
3764
3765 out:
3766         return stp;
3767 }
3768
3769 static int
3770 check_lock_length(u64 offset, u64 length)
3771 {
3772         return ((length == 0)  || ((length != NFS4_MAX_UINT64) &&
3773              LOFF_OVERFLOW(offset, length)));
3774 }
3775
3776 static void get_lock_access(struct nfs4_stateid *lock_stp, u32 access)
3777 {
3778         struct nfs4_file *fp = lock_stp->st_file;
3779         int oflag = nfs4_access_to_omode(access);
3780
3781         if (test_bit(access, &lock_stp->st_access_bmap))
3782                 return;
3783         nfs4_file_get_access(fp, oflag);
3784         __set_bit(access, &lock_stp->st_access_bmap);
3785 }
3786
3787 /*
3788  *  LOCK operation 
3789  */
3790 __be32
3791 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3792            struct nfsd4_lock *lock)
3793 {
3794         struct nfs4_stateowner *open_sop = NULL;
3795         struct nfs4_stateowner *lock_sop = NULL;
3796         struct nfs4_stateid *lock_stp;
3797         struct nfs4_file *fp;
3798         struct file *filp = NULL;
3799         struct file_lock file_lock;
3800         struct file_lock conflock;
3801         __be32 status = 0;
3802         unsigned int strhashval;
3803         int err;
3804
3805         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
3806                 (long long) lock->lk_offset,
3807                 (long long) lock->lk_length);
3808
3809         if (check_lock_length(lock->lk_offset, lock->lk_length))
3810                  return nfserr_inval;
3811
3812         if ((status = fh_verify(rqstp, &cstate->current_fh,
3813                                 S_IFREG, NFSD_MAY_LOCK))) {
3814                 dprintk("NFSD: nfsd4_lock: permission denied!\n");
3815                 return status;
3816         }
3817
3818         nfs4_lock_state();
3819
3820         if (lock->lk_is_new) {
3821                 /*
3822                  * Client indicates that this is a new lockowner.
3823                  * Use open owner and open stateid to create lock owner and
3824                  * lock stateid.
3825                  */
3826                 struct nfs4_stateid *open_stp = NULL;
3827                 
3828                 status = nfserr_stale_clientid;
3829                 if (!nfsd4_has_session(cstate) &&
3830                     STALE_CLIENTID(&lock->lk_new_clientid))
3831                         goto out;
3832
3833                 /* validate and update open stateid and open seqid */
3834                 status = nfs4_preprocess_seqid_op(cstate,
3835                                         lock->lk_new_open_seqid,
3836                                         &lock->lk_new_open_stateid,
3837                                         OPEN_STATE,
3838                                         &lock->lk_replay_owner, &open_stp,
3839                                         lock);
3840                 if (status)
3841                         goto out;
3842                 open_sop = lock->lk_replay_owner;
3843                 /* create lockowner and lock stateid */
3844                 fp = open_stp->st_file;
3845                 strhashval = lock_ownerstr_hashval(fp->fi_inode, 
3846                                 open_sop->so_client->cl_clientid.cl_id, 
3847                                 &lock->v.new.owner);
3848                 /* XXX: Do we need to check for duplicate stateowners on
3849                  * the same file, or should they just be allowed (and
3850                  * create new stateids)? */
3851                 status = nfserr_jukebox;
3852                 lock_sop = alloc_init_lock_stateowner(strhashval,
3853                                 open_sop->so_client, open_stp, lock);
3854                 if (lock_sop == NULL)
3855                         goto out;
3856                 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
3857                 if (lock_stp == NULL)
3858                         goto out;
3859         } else {
3860                 /* lock (lock owner + lock stateid) already exists */
3861                 status = nfs4_preprocess_seqid_op(cstate,
3862                                        lock->lk_old_lock_seqid, 
3863                                        &lock->lk_old_lock_stateid, 
3864                                        LOCK_STATE,
3865                                        &lock->lk_replay_owner, &lock_stp, lock);
3866                 if (status)
3867                         goto out;
3868                 lock_sop = lock->lk_replay_owner;
3869                 fp = lock_stp->st_file;
3870         }
3871         /* lock->lk_replay_owner and lock_stp have been created or found */
3872
3873         status = nfserr_grace;
3874         if (locks_in_grace() && !lock->lk_reclaim)
3875                 goto out;
3876         status = nfserr_no_grace;
3877         if (!locks_in_grace() && lock->lk_reclaim)
3878                 goto out;
3879
3880         locks_init_lock(&file_lock);
3881         switch (lock->lk_type) {
3882                 case NFS4_READ_LT:
3883                 case NFS4_READW_LT:
3884                         filp = find_readable_file(lock_stp->st_file);
3885                         if (filp)
3886                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
3887                         file_lock.fl_type = F_RDLCK;
3888                         break;
3889                 case NFS4_WRITE_LT:
3890                 case NFS4_WRITEW_LT:
3891                         filp = find_writeable_file(lock_stp->st_file);
3892                         if (filp)
3893                                 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
3894                         file_lock.fl_type = F_WRLCK;
3895                         break;
3896                 default:
3897                         status = nfserr_inval;
3898                 goto out;
3899         }
3900         if (!filp) {
3901                 status = nfserr_openmode;
3902                 goto out;
3903         }
3904         file_lock.fl_owner = (fl_owner_t)lock_sop;
3905         file_lock.fl_pid = current->tgid;
3906         file_lock.fl_file = filp;
3907         file_lock.fl_flags = FL_POSIX;
3908         file_lock.fl_lmops = &nfsd_posix_mng_ops;
3909
3910         file_lock.fl_start = lock->lk_offset;
3911         file_lock.fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
3912         nfs4_transform_lock_offset(&file_lock);
3913
3914         /*
3915         * Try to lock the file in the VFS.
3916         * Note: locks.c uses the BKL to protect the inode's lock list.
3917         */
3918
3919         err = vfs_lock_file(filp, F_SETLK, &file_lock, &conflock);
3920         switch (-err) {
3921         case 0: /* success! */
3922                 update_stateid(&lock_stp->st_stateid);
3923                 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, 
3924                                 sizeof(stateid_t));
3925                 status = 0;
3926                 break;
3927         case (EAGAIN):          /* conflock holds conflicting lock */
3928                 status = nfserr_denied;
3929                 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
3930                 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
3931                 break;
3932         case (EDEADLK):
3933                 status = nfserr_deadlock;
3934                 break;
3935         default:
3936                 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3937                 status = nfserrno(err);
3938                 break;
3939         }
3940 out:
3941         if (status && lock->lk_is_new && lock_sop)
3942                 release_lockowner(lock_sop);
3943         if (lock->lk_replay_owner) {
3944                 nfs4_get_stateowner(lock->lk_replay_owner);
3945                 cstate->replay_owner = lock->lk_replay_owner;
3946         }
3947         nfs4_unlock_state();
3948         return status;
3949 }
3950
3951 /*
3952  * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
3953  * so we do a temporary open here just to get an open file to pass to
3954  * vfs_test_lock.  (Arguably perhaps test_lock should be done with an
3955  * inode operation.)
3956  */
3957 static int nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
3958 {
3959         struct file *file;
3960         int err;
3961
3962         err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
3963         if (err)
3964                 return err;
3965         err = vfs_test_lock(file, lock);
3966         nfsd_close(file);
3967         return err;
3968 }
3969
3970 /*
3971  * LOCKT operation
3972  */
3973 __be32
3974 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3975             struct nfsd4_lockt *lockt)
3976 {
3977         struct inode *inode;
3978         struct file_lock file_lock;
3979         int error;
3980         __be32 status;
3981
3982         if (locks_in_grace())
3983                 return nfserr_grace;
3984
3985         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
3986                  return nfserr_inval;
3987
3988         lockt->lt_stateowner = NULL;
3989         nfs4_lock_state();
3990
3991         status = nfserr_stale_clientid;
3992         if (!nfsd4_has_session(cstate) && STALE_CLIENTID(&lockt->lt_clientid))
3993                 goto out;
3994
3995         if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) {
3996                 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
3997                 if (status == nfserr_symlink)
3998                         status = nfserr_inval;
3999                 goto out;
4000         }
4001
4002         inode = cstate->current_fh.fh_dentry->d_inode;
4003         locks_init_lock(&file_lock);
4004         switch (lockt->lt_type) {
4005                 case NFS4_READ_LT:
4006                 case NFS4_READW_LT:
4007                         file_lock.fl_type = F_RDLCK;
4008                 break;
4009                 case NFS4_WRITE_LT:
4010                 case NFS4_WRITEW_LT:
4011                         file_lock.fl_type = F_WRLCK;
4012                 break;
4013                 default:
4014                         dprintk("NFSD: nfs4_lockt: bad lock type!\n");
4015                         status = nfserr_inval;
4016                 goto out;
4017         }
4018
4019         lockt->lt_stateowner = find_lockstateowner_str(inode,
4020                         &lockt->lt_clientid, &lockt->lt_owner);
4021         if (lockt->lt_stateowner)
4022                 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
4023         file_lock.fl_pid = current->tgid;
4024         file_lock.fl_flags = FL_POSIX;
4025
4026         file_lock.fl_start = lockt->lt_offset;
4027         file_lock.fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
4028
4029         nfs4_transform_lock_offset(&file_lock);
4030
4031         status = nfs_ok;
4032         error = nfsd_test_lock(rqstp, &cstate->current_fh, &file_lock);
4033         if (error) {
4034                 status = nfserrno(error);
4035                 goto out;
4036         }
4037         if (file_lock.fl_type != F_UNLCK) {
4038                 status = nfserr_denied;
4039                 nfs4_set_lock_denied(&file_lock, &lockt->lt_denied);
4040         }
4041 out:
4042         nfs4_unlock_state();
4043         return status;
4044 }
4045
4046 __be32
4047 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4048             struct nfsd4_locku *locku)
4049 {
4050         struct nfs4_stateid *stp;
4051         struct file *filp = NULL;
4052         struct file_lock file_lock;
4053         __be32 status;
4054         int err;
4055                                                         
4056         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
4057                 (long long) locku->lu_offset,
4058                 (long long) locku->lu_length);
4059
4060         if (check_lock_length(locku->lu_offset, locku->lu_length))
4061                  return nfserr_inval;
4062
4063         nfs4_lock_state();
4064                                                                                 
4065         if ((status = nfs4_preprocess_seqid_op(cstate,
4066                                         locku->lu_seqid, 
4067                                         &locku->lu_stateid, 
4068                                         LOCK_STATE,
4069                                         &locku->lu_stateowner, &stp, NULL)))
4070                 goto out;
4071
4072         filp = find_any_file(stp->st_file);
4073         if (!filp) {
4074                 status = nfserr_lock_range;
4075                 goto out;
4076         }
4077         BUG_ON(!filp);
4078         locks_init_lock(&file_lock);
4079         file_lock.fl_type = F_UNLCK;
4080         file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
4081         file_lock.fl_pid = current->tgid;
4082         file_lock.fl_file = filp;
4083         file_lock.fl_flags = FL_POSIX; 
4084         file_lock.fl_lmops = &nfsd_posix_mng_ops;
4085         file_lock.fl_start = locku->lu_offset;
4086
4087         file_lock.fl_end = last_byte_offset(locku->lu_offset, locku->lu_length);
4088         nfs4_transform_lock_offset(&file_lock);
4089
4090         /*
4091         *  Try to unlock the file in the VFS.
4092         */
4093         err = vfs_lock_file(filp, F_SETLK, &file_lock, NULL);
4094         if (err) {
4095                 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
4096                 goto out_nfserr;
4097         }
4098         /*
4099         * OK, unlock succeeded; the only thing left to do is update the stateid.
4100         */
4101         update_stateid(&stp->st_stateid);
4102         memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
4103
4104 out:
4105         if (locku->lu_stateowner) {
4106                 nfs4_get_stateowner(locku->lu_stateowner);
4107                 cstate->replay_owner = locku->lu_stateowner;
4108         }
4109         nfs4_unlock_state();
4110         return status;
4111
4112 out_nfserr:
4113         status = nfserrno(err);
4114         goto out;
4115 }
4116
4117 /*
4118  * returns
4119  *      1: locks held by lockowner
4120  *      0: no locks held by lockowner
4121  */
4122 static int
4123 check_for_locks(struct nfs4_file *filp, struct nfs4_stateowner *lowner)
4124 {
4125         struct file_lock **flpp;
4126         struct inode *inode = filp->fi_inode;
4127         int status = 0;
4128
4129         lock_flocks();
4130         for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
4131                 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
4132                         status = 1;
4133                         goto out;
4134                 }
4135         }
4136 out:
4137         unlock_flocks();
4138         return status;
4139 }
4140
4141 __be32
4142 nfsd4_release_lockowner(struct svc_rqst *rqstp,
4143                         struct nfsd4_compound_state *cstate,
4144                         struct nfsd4_release_lockowner *rlockowner)
4145 {
4146         clientid_t *clid = &rlockowner->rl_clientid;
4147         struct nfs4_stateowner *sop;
4148         struct nfs4_stateid *stp;
4149         struct xdr_netobj *owner = &rlockowner->rl_owner;
4150         struct list_head matches;
4151         int i;
4152         __be32 status;
4153
4154         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
4155                 clid->cl_boot, clid->cl_id);
4156
4157         /* XXX check for lease expiration */
4158
4159         status = nfserr_stale_clientid;
4160         if (STALE_CLIENTID(clid))
4161                 return status;
4162
4163         nfs4_lock_state();
4164
4165         status = nfserr_locks_held;
4166         /* XXX: we're doing a linear search through all the lockowners.
4167          * Yipes!  For now we'll just hope clients aren't really using
4168          * release_lockowner much, but eventually we have to fix these
4169          * data structures. */
4170         INIT_LIST_HEAD(&matches);
4171         for (i = 0; i < LOCK_HASH_SIZE; i++) {
4172                 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
4173                         if (!same_owner_str(sop, owner, clid))
4174                                 continue;
4175                         list_for_each_entry(stp, &sop->so_stateids,
4176                                         st_perstateowner) {
4177                                 if (check_for_locks(stp->st_file, sop))
4178                                         goto out;
4179                                 /* Note: so_perclient unused for lockowners,
4180                                  * so it's OK to fool with here. */
4181                                 list_add(&sop->so_perclient, &matches);
4182                         }
4183                 }
4184         }
4185         /* Clients probably won't expect us to return with some (but not all)
4186          * of the lockowner state released; so don't release any until all
4187          * have been checked. */
4188         status = nfs_ok;
4189         while (!list_empty(&matches)) {
4190                 sop = list_entry(matches.next, struct nfs4_stateowner,
4191                                                                 so_perclient);
4192                 /* unhash_stateowner deletes so_perclient only
4193                  * for openowners. */
4194                 list_del(&sop->so_perclient);
4195                 release_lockowner(sop);
4196         }
4197 out:
4198         nfs4_unlock_state();
4199         return status;
4200 }
4201
4202 static inline struct nfs4_client_reclaim *
4203 alloc_reclaim(void)
4204 {
4205         return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
4206 }
4207
4208 int
4209 nfs4_has_reclaimed_state(const char *name, bool use_exchange_id)
4210 {
4211         unsigned int strhashval = clientstr_hashval(name);
4212         struct nfs4_client *clp;
4213
4214         clp = find_confirmed_client_by_str(name, strhashval);
4215         return clp ? 1 : 0;
4216 }
4217
4218 /*
4219  * failure => all reset bets are off, nfserr_no_grace...
4220  */
4221 int
4222 nfs4_client_to_reclaim(const char *name)
4223 {
4224         unsigned int strhashval;
4225         struct nfs4_client_reclaim *crp = NULL;
4226
4227         dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
4228         crp = alloc_reclaim();
4229         if (!crp)
4230                 return 0;
4231         strhashval = clientstr_hashval(name);
4232         INIT_LIST_HEAD(&crp->cr_strhash);
4233         list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
4234         memcpy(crp->cr_recdir, name, HEXDIR_LEN);
4235         reclaim_str_hashtbl_size++;
4236         return 1;
4237 }
4238
4239 static void
4240 nfs4_release_reclaim(void)
4241 {
4242         struct nfs4_client_reclaim *crp = NULL;
4243         int i;
4244
4245         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4246                 while (!list_empty(&reclaim_str_hashtbl[i])) {
4247                         crp = list_entry(reclaim_str_hashtbl[i].next,
4248                                         struct nfs4_client_reclaim, cr_strhash);
4249                         list_del(&crp->cr_strhash);
4250                         kfree(crp);
4251                         reclaim_str_hashtbl_size--;
4252                 }
4253         }
4254         BUG_ON(reclaim_str_hashtbl_size);
4255 }
4256
4257 /*
4258  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
4259 static struct nfs4_client_reclaim *
4260 nfs4_find_reclaim_client(clientid_t *clid)
4261 {
4262         unsigned int strhashval;
4263         struct nfs4_client *clp;
4264         struct nfs4_client_reclaim *crp = NULL;
4265
4266
4267         /* find clientid in conf_id_hashtbl */
4268         clp = find_confirmed_client(clid);
4269         if (clp == NULL)
4270                 return NULL;
4271
4272         dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
4273                             clp->cl_name.len, clp->cl_name.data,
4274                             clp->cl_recdir);
4275
4276         /* find clp->cl_name in reclaim_str_hashtbl */
4277         strhashval = clientstr_hashval(clp->cl_recdir);
4278         list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
4279                 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
4280                         return crp;
4281                 }
4282         }
4283         return NULL;
4284 }
4285
4286 /*
4287 * Called from OPEN. Look for clientid in reclaim list.
4288 */
4289 __be32
4290 nfs4_check_open_reclaim(clientid_t *clid)
4291 {
4292         return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
4293 }
4294
4295 /* initialization to perform at module load time: */
4296
4297 int
4298 nfs4_state_init(void)
4299 {
4300         int i, status;
4301
4302         status = nfsd4_init_slabs();
4303         if (status)
4304                 return status;
4305         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4306                 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
4307                 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
4308                 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
4309                 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
4310                 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
4311         }
4312         for (i = 0; i < SESSION_HASH_SIZE; i++)
4313                 INIT_LIST_HEAD(&sessionid_hashtbl[i]);
4314         for (i = 0; i < FILE_HASH_SIZE; i++) {
4315                 INIT_LIST_HEAD(&file_hashtbl[i]);
4316         }
4317         for (i = 0; i < OWNER_HASH_SIZE; i++) {
4318                 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
4319                 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
4320         }
4321         for (i = 0; i < STATEID_HASH_SIZE; i++) {
4322                 INIT_LIST_HEAD(&stateid_hashtbl[i]);
4323                 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
4324         }
4325         for (i = 0; i < LOCK_HASH_SIZE; i++) {
4326                 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
4327                 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
4328         }
4329         memset(&onestateid, ~0, sizeof(stateid_t));
4330         INIT_LIST_HEAD(&close_lru);
4331         INIT_LIST_HEAD(&client_lru);
4332         INIT_LIST_HEAD(&del_recall_lru);
4333         reclaim_str_hashtbl_size = 0;
4334         return 0;
4335 }
4336
4337 static void
4338 nfsd4_load_reboot_recovery_data(void)
4339 {
4340         int status;
4341
4342         nfs4_lock_state();
4343         nfsd4_init_recdir(user_recovery_dirname);
4344         status = nfsd4_recdir_load();
4345         nfs4_unlock_state();
4346         if (status)
4347                 printk("NFSD: Failure reading reboot recovery data\n");
4348 }
4349
4350 /*
4351  * Since the lifetime of a delegation isn't limited to that of an open, a
4352  * client may quite reasonably hang on to a delegation as long as it has
4353  * the inode cached.  This becomes an obvious problem the first time a
4354  * client's inode cache approaches the size of the server's total memory.
4355  *
4356  * For now we avoid this problem by imposing a hard limit on the number
4357  * of delegations, which varies according to the server's memory size.
4358  */
4359 static void
4360 set_max_delegations(void)
4361 {
4362         /*
4363          * Allow at most 4 delegations per megabyte of RAM.  Quick
4364          * estimates suggest that in the worst case (where every delegation
4365          * is for a different inode), a delegation could take about 1.5K,
4366          * giving a worst case usage of about 6% of memory.
4367          */
4368         max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
4369 }
4370
4371 /* initialization to perform when the nfsd service is started: */
4372
4373 static int
4374 __nfs4_state_start(void)
4375 {
4376         int ret;
4377
4378         boot_time = get_seconds();
4379         locks_start_grace(&nfsd4_manager);
4380         printk(KERN_INFO "NFSD: starting %ld-second grace period\n",
4381                nfsd4_grace);
4382         ret = set_callback_cred();
4383         if (ret)
4384                 return -ENOMEM;
4385         laundry_wq = create_singlethread_workqueue("nfsd4");
4386         if (laundry_wq == NULL)
4387                 return -ENOMEM;
4388         ret = nfsd4_create_callback_queue();
4389         if (ret)
4390                 goto out_free_laundry;
4391         queue_delayed_work(laundry_wq, &laundromat_work, nfsd4_grace * HZ);
4392         set_max_delegations();
4393         return 0;
4394 out_free_laundry:
4395         destroy_workqueue(laundry_wq);
4396         return ret;
4397 }
4398
4399 int
4400 nfs4_state_start(void)
4401 {
4402         nfsd4_load_reboot_recovery_data();
4403         return __nfs4_state_start();
4404 }
4405
4406 static void
4407 __nfs4_state_shutdown(void)
4408 {
4409         int i;
4410         struct nfs4_client *clp = NULL;
4411         struct nfs4_delegation *dp = NULL;
4412         struct list_head *pos, *next, reaplist;
4413
4414         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
4415                 while (!list_empty(&conf_id_hashtbl[i])) {
4416                         clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
4417                         expire_client(clp);
4418                 }
4419                 while (!list_empty(&unconf_str_hashtbl[i])) {
4420                         clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
4421                         expire_client(clp);
4422                 }
4423         }
4424         INIT_LIST_HEAD(&reaplist);
4425         spin_lock(&recall_lock);
4426         list_for_each_safe(pos, next, &del_recall_lru) {
4427                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4428                 list_move(&dp->dl_recall_lru, &reaplist);
4429         }
4430         spin_unlock(&recall_lock);
4431         list_for_each_safe(pos, next, &reaplist) {
4432                 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4433                 list_del_init(&dp->dl_recall_lru);
4434                 unhash_delegation(dp);
4435         }
4436
4437         nfsd4_shutdown_recdir();
4438 }
4439
4440 void
4441 nfs4_state_shutdown(void)
4442 {
4443         cancel_delayed_work_sync(&laundromat_work);
4444         destroy_workqueue(laundry_wq);
4445         locks_end_grace(&nfsd4_manager);
4446         nfs4_lock_state();
4447         nfs4_release_reclaim();
4448         __nfs4_state_shutdown();
4449         nfs4_unlock_state();
4450         nfsd4_destroy_callback_queue();
4451 }
4452
4453 /*
4454  * user_recovery_dirname is protected by the nfsd_mutex since it's only
4455  * accessed when nfsd is starting.
4456  */
4457 static void
4458 nfs4_set_recdir(char *recdir)
4459 {
4460         strcpy(user_recovery_dirname, recdir);
4461 }
4462
4463 /*
4464  * Change the NFSv4 recovery directory to recdir.
4465  */
4466 int
4467 nfs4_reset_recoverydir(char *recdir)
4468 {
4469         int status;
4470         struct path path;
4471
4472         status = kern_path(recdir, LOOKUP_FOLLOW, &path);
4473         if (status)
4474                 return status;
4475         status = -ENOTDIR;
4476         if (S_ISDIR(path.dentry->d_inode->i_mode)) {
4477                 nfs4_set_recdir(recdir);
4478                 status = 0;
4479         }
4480         path_put(&path);
4481         return status;
4482 }
4483
4484 char *
4485 nfs4_recoverydir(void)
4486 {
4487         return user_recovery_dirname;
4488 }