4df0dedef709d079328c40731cb926a7be81eb2d
[linux-2.6.git] / fs / nfsd / nfs4state.c
1 /*
2 *  linux/fs/nfsd/nfs4state.c
3 *
4 *  Copyright (c) 2001 The Regents of the University of Michigan.
5 *  All rights reserved.
6 *
7 *  Kendrick Smith <kmsmith@umich.edu>
8 *  Andy Adamson <kandros@umich.edu>
9 *
10 *  Redistribution and use in source and binary forms, with or without
11 *  modification, are permitted provided that the following conditions
12 *  are met:
13 *
14 *  1. Redistributions of source code must retain the above copyright
15 *     notice, this list of conditions and the following disclaimer.
16 *  2. Redistributions in binary form must reproduce the above copyright
17 *     notice, this list of conditions and the following disclaimer in the
18 *     documentation and/or other materials provided with the distribution.
19 *  3. Neither the name of the University nor the names of its
20 *     contributors may be used to endorse or promote products derived
21 *     from this software without specific prior written permission.
22 *
23 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
40
41 #include <linux/sunrpc/svc.h>
42 #include <linux/nfsd/nfsd.h>
43 #include <linux/nfsd/cache.h>
44 #include <linux/mount.h>
45 #include <linux/workqueue.h>
46 #include <linux/smp_lock.h>
47 #include <linux/nfs4.h>
48 #include <linux/nfsd/state.h>
49 #include <linux/nfsd/xdr4.h>
50
51 #define NFSDDBG_FACILITY                NFSDDBG_PROC
52
53 /* Globals */
54 static time_t lease_time = 90;     /* default lease time */
55 static time_t old_lease_time = 90; /* past incarnation lease time */
56 static u32 nfs4_reclaim_init = 0;
57 time_t boot_time;
58 static time_t grace_end = 0;
59 static u32 current_clientid = 1;
60 static u32 current_ownerid;
61 static u32 current_fileid;
62 static u32 nfs4_init;
63 stateid_t zerostateid;             /* bits all 0 */
64 stateid_t onestateid;              /* bits all 1 */
65
66 /* debug counters */
67 u32 list_add_perfile = 0; 
68 u32 list_del_perfile = 0;
69 u32 add_perclient = 0;
70 u32 del_perclient = 0;
71 u32 alloc_file = 0;
72 u32 free_file = 0;
73 u32 alloc_sowner = 0;
74 u32 free_sowner = 0;
75 u32 vfsopen = 0;
76 u32 vfsclose = 0;
77 u32 alloc_lsowner= 0;
78
79 /* forward declarations */
80 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
81
82 /* Locking:
83  *
84  * client_sema: 
85  *      protects clientid_hashtbl[], clientstr_hashtbl[],
86  *      unconfstr_hashtbl[], uncofid_hashtbl[].
87  */
88 static DECLARE_MUTEX(client_sema);
89
90 void
91 nfs4_lock_state(void)
92 {
93         down(&client_sema);
94 }
95
96 /*
97  * nfs4_unlock_state(); called in encode
98  */
99 void
100 nfs4_unlock_state(void)
101 {
102         up(&client_sema);
103 }
104
105 static inline u32
106 opaque_hashval(const void *ptr, int nbytes)
107 {
108         unsigned char *cptr = (unsigned char *) ptr;
109
110         u32 x = 0;
111         while (nbytes--) {
112                 x *= 37;
113                 x += *cptr++;
114         }
115         return x;
116 }
117
118 /* forward declarations */
119 static void release_stateowner(struct nfs4_stateowner *sop);
120 static void release_stateid(struct nfs4_stateid *stp, int flags);
121 static void release_file(struct nfs4_file *fp);
122
123
124 /* 
125  * SETCLIENTID state 
126  */
127
128 /* Hash tables for nfs4_clientid state */
129 #define CLIENT_HASH_BITS                 4
130 #define CLIENT_HASH_SIZE                (1 << CLIENT_HASH_BITS)
131 #define CLIENT_HASH_MASK                (CLIENT_HASH_SIZE - 1)
132
133 #define clientid_hashval(id) \
134         ((id) & CLIENT_HASH_MASK)
135 #define clientstr_hashval(name, namelen) \
136         (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
137 /*
138  * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
139  * used in reboot/reset lease grace period processing
140  *
141  * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
142  * setclientid_confirmed info. 
143  *
144  * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed 
145  * setclientid info.
146  *
147  * client_lru holds client queue ordered by nfs4_client.cl_time
148  * for lease renewal.
149  *
150  * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
151  * for last close replay.
152  */
153 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
154 static int reclaim_str_hashtbl_size;
155 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
156 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
157 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
158 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
159 static struct list_head client_lru;
160 static struct list_head close_lru;
161
162 static inline void
163 renew_client(struct nfs4_client *clp)
164 {
165         /*
166         * Move client to the end to the LRU list.
167         */
168         dprintk("renewing client (clientid %08x/%08x)\n", 
169                         clp->cl_clientid.cl_boot, 
170                         clp->cl_clientid.cl_id);
171         list_move_tail(&clp->cl_lru, &client_lru);
172         clp->cl_time = get_seconds();
173 }
174
175 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
176 static int
177 STALE_CLIENTID(clientid_t *clid)
178 {
179         if (clid->cl_boot == boot_time)
180                 return 0;
181         dprintk("NFSD stale clientid (%08x/%08x)\n", 
182                         clid->cl_boot, clid->cl_id);
183         return 1;
184 }
185
186 /* 
187  * XXX Should we use a slab cache ?
188  * This type of memory management is somewhat inefficient, but we use it
189  * anyway since SETCLIENTID is not a common operation.
190  */
191 static inline struct nfs4_client *
192 alloc_client(struct xdr_netobj name)
193 {
194         struct nfs4_client *clp;
195
196         if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
197                 memset(clp, 0, sizeof(*clp));
198                 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
199                         memcpy(clp->cl_name.data, name.data, name.len);
200                         clp->cl_name.len = name.len;
201                 }
202                 else {
203                         kfree(clp);
204                         clp = NULL;
205                 }
206         }
207         return clp;
208 }
209
210 static inline void
211 free_client(struct nfs4_client *clp)
212 {
213         if (clp->cl_cred.cr_group_info)
214                 put_group_info(clp->cl_cred.cr_group_info);
215         kfree(clp->cl_name.data);
216         kfree(clp);
217 }
218
219 static void
220 expire_client(struct nfs4_client *clp)
221 {
222         struct nfs4_stateowner *sop;
223
224         dprintk("NFSD: expire_client\n");
225         list_del(&clp->cl_idhash);
226         list_del(&clp->cl_strhash);
227         list_del(&clp->cl_lru);
228         while (!list_empty(&clp->cl_perclient)) {
229                 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
230                 release_stateowner(sop);
231         }
232         free_client(clp);
233 }
234
235 static struct nfs4_client *
236 create_client(struct xdr_netobj name) {
237         struct nfs4_client *clp;
238
239         if(!(clp = alloc_client(name)))
240                 goto out;
241         INIT_LIST_HEAD(&clp->cl_idhash);
242         INIT_LIST_HEAD(&clp->cl_strhash);
243         INIT_LIST_HEAD(&clp->cl_perclient);
244         INIT_LIST_HEAD(&clp->cl_lru);
245 out:
246         return clp;
247 }
248
249 static void
250 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
251         memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
252 }
253
254 static void
255 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
256         target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; 
257         target->cl_clientid.cl_id = source->cl_clientid.cl_id; 
258 }
259
260 static void
261 copy_cred(struct svc_cred *target, struct svc_cred *source) {
262
263         target->cr_uid = source->cr_uid;
264         target->cr_gid = source->cr_gid;
265         target->cr_group_info = source->cr_group_info;
266         get_group_info(target->cr_group_info);
267 }
268
269 static int
270 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
271         if(!n1 || !n2)
272                 return 0;
273         return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
274 }
275
276 static int
277 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
278         return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
279 }
280
281 static int
282 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
283         return((cl1->cl_boot == cl2->cl_boot) &&
284                 (cl1->cl_id == cl2->cl_id));
285 }
286
287 /* XXX what about NGROUP */
288 static int
289 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
290         return(cr1->cr_uid == cr2->cr_uid);
291
292 }
293
294 static void
295 gen_clid(struct nfs4_client *clp) {
296         clp->cl_clientid.cl_boot = boot_time;
297         clp->cl_clientid.cl_id = current_clientid++; 
298 }
299
300 static void
301 gen_confirm(struct nfs4_client *clp) {
302         struct timespec         tv;
303         u32 *                   p;
304
305         tv = CURRENT_TIME;
306         p = (u32 *)clp->cl_confirm.data;
307         *p++ = tv.tv_sec;
308         *p++ = tv.tv_nsec;
309 }
310
311 static int
312 check_name(struct xdr_netobj name) {
313
314         if (name.len == 0) 
315                 return 0;
316         if (name.len > NFS4_OPAQUE_LIMIT) {
317                 printk("NFSD: check_name: name too long(%d)!\n", name.len);
318                 return 0;
319         }
320         return 1;
321 }
322
323 void
324 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
325 {
326         unsigned int idhashval;
327
328         list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
329         idhashval = clientid_hashval(clp->cl_clientid.cl_id);
330         list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
331         list_add_tail(&clp->cl_lru, &client_lru);
332         clp->cl_time = get_seconds();
333 }
334
335 void
336 move_to_confirmed(struct nfs4_client *clp, unsigned int idhashval)
337 {
338         unsigned int strhashval;
339
340         dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
341         list_del_init(&clp->cl_strhash);
342         list_del_init(&clp->cl_idhash);
343         list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
344         strhashval = clientstr_hashval(clp->cl_name.data, 
345                         clp->cl_name.len);
346         list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
347         renew_client(clp);
348 }
349
350
351 /* a helper function for parse_callback */
352 static int
353 parse_octet(unsigned int *lenp, char **addrp)
354 {
355         unsigned int len = *lenp;
356         char *p = *addrp;
357         int n = -1;
358         char c;
359
360         for (;;) {
361                 if (!len)
362                         break;
363                 len--;
364                 c = *p++;
365                 if (c == '.')
366                         break;
367                 if ((c < '0') || (c > '9')) {
368                         n = -1;
369                         break;
370                 }
371                 if (n < 0)
372                         n = 0;
373                 n = (n * 10) + (c - '0');
374                 if (n > 255) {
375                         n = -1;
376                         break;
377                 }
378         }
379         *lenp = len;
380         *addrp = p;
381         return n;
382 }
383
384 /* parse and set the setclientid ipv4 callback address */
385 int
386 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
387 {
388         int temp = 0;
389         u32 cbaddr = 0;
390         u16 cbport = 0;
391         u32 addrlen = addr_len;
392         char *addr = addr_val;
393         int i, shift;
394
395         /* ipaddress */
396         shift = 24;
397         for(i = 4; i > 0  ; i--) {
398                 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
399                         return 0;
400                 }
401                 cbaddr |= (temp << shift);
402                 if(shift > 0)
403                 shift -= 8;
404         }
405         *cbaddrp = cbaddr;
406
407         /* port */
408         shift = 8;
409         for(i = 2; i > 0  ; i--) {
410                 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
411                         return 0;
412                 }
413                 cbport |= (temp << shift);
414                 if(shift > 0)
415                         shift -= 8;
416         }
417         *cbportp = cbport;
418         return 1;
419 }
420
421 void
422 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
423 {
424         struct nfs4_callback *cb = &clp->cl_callback;
425
426         if( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
427                          &cb->cb_addr, &cb->cb_port))) {
428                 printk(KERN_INFO "NFSD: BAD callback address. client will not receive delegations\n");
429                 cb->cb_parsed = 0;
430                 return;
431         }
432         cb->cb_netid.len = se->se_callback_netid_len;
433         cb->cb_netid.data = se->se_callback_netid_val;
434         cb->cb_prog = se->se_callback_prog;
435         cb->cb_ident = se->se_callback_ident;
436         cb->cb_parsed = 1;
437 }
438
439 /*
440  * RFC 3010 has a complex implmentation description of processing a 
441  * SETCLIENTID request consisting of 5 bullets, labeled as 
442  * CASE0 - CASE4 below.
443  *
444  * NOTES:
445  *      callback information will be processed in a future patch
446  *
447  *      an unconfirmed record is added when:
448  *      NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
449  *      CASE 1: confirmed record found with matching name, principal,
450  *              verifier, and clientid.
451  *      CASE 2: confirmed record found with matching name, principal,
452  *              and there is no unconfirmed record with matching
453  *              name and principal
454  *
455  *      an unconfirmed record is replaced when:
456  *      CASE 3: confirmed record found with matching name, principal,
457  *              and an unconfirmed record is found with matching 
458  *              name, principal, and with clientid and
459  *              confirm that does not match the confirmed record.
460  *      CASE 4: there is no confirmed record with matching name and 
461  *              principal. there is an unconfirmed record with 
462  *              matching name, principal.
463  *
464  *      an unconfirmed record is deleted when:
465  *      CASE 1: an unconfirmed record that matches input name, verifier,
466  *              and confirmed clientid.
467  *      CASE 4: any unconfirmed records with matching name and principal
468  *              that exist after an unconfirmed record has been replaced
469  *              as described above.
470  *
471  */
472 int
473 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
474 {
475         u32                     ip_addr = rqstp->rq_addr.sin_addr.s_addr;
476         struct xdr_netobj       clname = { 
477                 .len = setclid->se_namelen,
478                 .data = setclid->se_name,
479         };
480         nfs4_verifier           clverifier = setclid->se_verf;
481         unsigned int            strhashval;
482         struct nfs4_client *    conf, * unconf, * new, * clp;
483         int                     status;
484         
485         status = nfserr_inval;
486         if (!check_name(clname))
487                 goto out;
488
489         /* 
490          * XXX The Duplicate Request Cache (DRC) has been checked (??)
491          * We get here on a DRC miss.
492          */
493
494         strhashval = clientstr_hashval(clname.data, clname.len);
495
496         conf = NULL;
497         nfs4_lock_state();
498         list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
499                 if (!cmp_name(&clp->cl_name, &clname))
500                         continue;
501                 /* 
502                  * CASE 0:
503                  * clname match, confirmed, different principal
504                  * or different ip_address
505                  */
506                 status = nfserr_clid_inuse;
507                 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
508                         printk("NFSD: setclientid: string in use by client"
509                         "(clientid %08x/%08x)\n",
510                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
511                         goto out;
512                 }
513                 if (clp->cl_addr != ip_addr) { 
514                         printk("NFSD: setclientid: string in use by client"
515                         "(clientid %08x/%08x)\n",
516                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
517                         goto out;
518                 }
519
520                 /* 
521                  * cl_name match from a previous SETCLIENTID operation
522                  * XXX check for additional matches?
523                  */
524                 conf = clp;
525                 break;
526         }
527         unconf = NULL;
528         list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
529                 if (!cmp_name(&clp->cl_name, &clname))
530                         continue;
531                 /* cl_name match from a previous SETCLIENTID operation */
532                 unconf = clp;
533                 break;
534         }
535         status = nfserr_resource;
536         if (!conf) {
537                 /* 
538                  * CASE 4:
539                  * placed first, because it is the normal case.
540                  */
541                 if (unconf)
542                         expire_client(unconf);
543                 if (!(new = create_client(clname)))
544                         goto out;
545                 copy_verf(new, &clverifier);
546                 new->cl_addr = ip_addr;
547                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
548                 gen_clid(new);
549                 gen_confirm(new);
550                 gen_callback(new, setclid);
551                 add_to_unconfirmed(new, strhashval);
552         } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
553                 /*
554                  * CASE 1:
555                  * cl_name match, confirmed, principal match
556                  * verifier match: probable callback update
557                  *
558                  * remove any unconfirmed nfs4_client with 
559                  * matching cl_name, cl_verifier, and cl_clientid
560                  *
561                  * create and insert an unconfirmed nfs4_client with same 
562                  * cl_name, cl_verifier, and cl_clientid as existing 
563                  * nfs4_client,  but with the new callback info and a 
564                  * new cl_confirm
565                  */
566                 if ((unconf) && 
567                     cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
568                      cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
569                                 expire_client(unconf);
570                 }
571                 if (!(new = create_client(clname)))
572                         goto out;
573                 copy_verf(new,&conf->cl_verifier);
574                 new->cl_addr = ip_addr;
575                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
576                 copy_clid(new, conf);
577                 gen_confirm(new);
578                 gen_callback(new, setclid);
579                 add_to_unconfirmed(new,strhashval);
580         } else if (!unconf) {
581                 /*
582                  * CASE 2:
583                  * clname match, confirmed, principal match
584                  * verfier does not match
585                  * no unconfirmed. create a new unconfirmed nfs4_client
586                  * using input clverifier, clname, and callback info
587                  * and generate a new cl_clientid and cl_confirm.
588                  */
589                 if (!(new = create_client(clname)))
590                         goto out;
591                 copy_verf(new,&clverifier);
592                 new->cl_addr = ip_addr;
593                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
594                 gen_clid(new);
595                 gen_confirm(new);
596                 gen_callback(new, setclid);
597                 add_to_unconfirmed(new, strhashval);
598         } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
599                 /*      
600                  * CASE3:
601                  * confirmed found (name, principal match)
602                  * confirmed verifier does not match input clverifier
603                  *
604                  * unconfirmed found (name match)
605                  * confirmed->cl_confirm != unconfirmed->cl_confirm
606                  *
607                  * remove unconfirmed.
608                  *
609                  * create an unconfirmed nfs4_client 
610                  * with same cl_name as existing confirmed nfs4_client, 
611                  * but with new callback info, new cl_clientid,
612                  * new cl_verifier and a new cl_confirm
613                  */
614                 expire_client(unconf);
615                 if (!(new = create_client(clname)))
616                         goto out;
617                 copy_verf(new,&clverifier);
618                 new->cl_addr = ip_addr;
619                 copy_cred(&new->cl_cred,&rqstp->rq_cred);
620                 gen_clid(new);
621                 gen_confirm(new);
622                 gen_callback(new, setclid);
623                 add_to_unconfirmed(new, strhashval);
624         } else {
625                 /* No cases hit !!! */
626                 status = nfserr_inval;
627                 goto out;
628
629         }
630         setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
631         setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
632         memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
633         status = nfs_ok;
634 out:
635         nfs4_unlock_state();
636         return status;
637 }
638
639
640 /*
641  * RFC 3010 has a complex implmentation description of processing a 
642  * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
643  * processing on a DRC miss, labeled as CASE1 - CASE4 below.
644  *
645  * NOTE: callback information will be processed here in a future patch
646  */
647 int
648 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
649 {
650         u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
651         unsigned int idhashval;
652         struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
653         nfs4_verifier confirm = setclientid_confirm->sc_confirm; 
654         clientid_t * clid = &setclientid_confirm->sc_clientid;
655         int status;
656
657         status = nfserr_stale_clientid;
658         if (STALE_CLIENTID(clid))
659                 goto out;
660         /* 
661          * XXX The Duplicate Request Cache (DRC) has been checked (??)
662          * We get here on a DRC miss.
663          */
664
665         idhashval = clientid_hashval(clid->cl_id);
666         nfs4_lock_state();
667         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
668                 if (!cmp_clid(&clp->cl_clientid, clid))
669                         continue;
670
671                 status = nfserr_inval;
672                 /* 
673                  * Found a record for this clientid. If the IP addresses
674                  * don't match, return ERR_INVAL just as if the record had
675                  * not been found.
676                  */
677                 if (clp->cl_addr != ip_addr) { 
678                         printk("NFSD: setclientid: string in use by client"
679                         "(clientid %08x/%08x)\n",
680                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
681                         goto out;
682                 }
683                 conf = clp;
684                 break;
685         }
686         list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
687                 if (!cmp_clid(&clp->cl_clientid, clid))
688                         continue;
689                 status = nfserr_inval;
690                 if (clp->cl_addr != ip_addr) { 
691                         printk("NFSD: setclientid: string in use by client"
692                         "(clientid %08x/%08x)\n",
693                         clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
694                         goto out;
695                 }
696                 unconf = clp;
697                 break;
698         }
699         /* CASE 1: 
700         * unconf record that matches input clientid and input confirm.
701         * conf record that matches input clientid.
702         * conf  and unconf records match names, verifiers 
703         */
704         if ((conf && unconf) && 
705             (cmp_verf(&unconf->cl_confirm, &confirm)) &&
706             (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
707             (cmp_name(&conf->cl_name,&unconf->cl_name))  &&
708             (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
709                 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred)) 
710                         status = nfserr_clid_inuse;
711                 else {
712                         expire_client(conf);
713                         move_to_confirmed(unconf, idhashval);
714                         status = nfs_ok;
715                 }
716                 goto out;
717         } 
718         /* CASE 2:
719          * conf record that matches input clientid.
720          * if unconf record that matches input clientid, then unconf->cl_name
721          * or unconf->cl_verifier don't match the conf record.
722          */
723         if ((conf && !unconf) || 
724             ((conf && unconf) && 
725              (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
726               !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
727                 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
728                         status = nfserr_clid_inuse;
729                 } else {
730                         status = nfs_ok;
731                 }
732                 goto out;
733         }
734         /* CASE 3:
735          * conf record not found.
736          * unconf record found. 
737          * unconf->cl_confirm matches input confirm
738          */ 
739         if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
740                 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
741                         status = nfserr_clid_inuse;
742                 } else {
743                         status = nfs_ok;
744                         move_to_confirmed(unconf, idhashval);
745                 }
746                 goto out;
747         }
748         /* CASE 4:
749          * conf record not found, or if conf, then conf->cl_confirm does not
750          * match input confirm.
751          * unconf record not found, or if unconf, then unconf->cl_confirm 
752          * does not match input confirm.
753          */
754         if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
755             (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
756                 status = nfserr_stale_clientid;
757                 goto out;
758         }
759         /* check that we have hit one of the cases...*/
760         status = nfserr_inval;
761         goto out;
762 out:
763         /* XXX if status == nfs_ok, probe callback path */
764         nfs4_unlock_state();
765         return status;
766 }
767
768 /* 
769  * Open owner state (share locks)
770  */
771
772 /* hash tables for nfs4_stateowner */
773 #define OWNER_HASH_BITS              8
774 #define OWNER_HASH_SIZE             (1 << OWNER_HASH_BITS)
775 #define OWNER_HASH_MASK             (OWNER_HASH_SIZE - 1)
776
777 #define ownerid_hashval(id) \
778         ((id) & OWNER_HASH_MASK)
779 #define ownerstr_hashval(clientid, ownername) \
780         (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
781
782 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
783 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
784
785 /* hash table for nfs4_file */
786 #define FILE_HASH_BITS                   8
787 #define FILE_HASH_SIZE                  (1 << FILE_HASH_BITS)
788 #define FILE_HASH_MASK                  (FILE_HASH_SIZE - 1)
789 /* hash table for (open)nfs4_stateid */
790 #define STATEID_HASH_BITS              10
791 #define STATEID_HASH_SIZE              (1 << STATEID_HASH_BITS)
792 #define STATEID_HASH_MASK              (STATEID_HASH_SIZE - 1)
793
794 #define file_hashval(x) \
795         hash_ptr(x, FILE_HASH_BITS)
796 #define stateid_hashval(owner_id, file_id)  \
797         (((owner_id) + (file_id)) & STATEID_HASH_MASK)
798
799 static struct list_head file_hashtbl[FILE_HASH_SIZE];
800 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
801
802 /* OPEN Share state helper functions */
803 static inline struct nfs4_file *
804 alloc_init_file(unsigned int hashval, struct inode *ino) {
805         struct nfs4_file *fp;
806         if ((fp = kmalloc(sizeof(struct nfs4_file),GFP_KERNEL))) {
807                 INIT_LIST_HEAD(&fp->fi_hash);
808                 INIT_LIST_HEAD(&fp->fi_perfile);
809                 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
810                 fp->fi_inode = igrab(ino);
811                 fp->fi_id = current_fileid++;
812                 alloc_file++;
813                 return fp;
814         }
815         return (struct nfs4_file *)NULL;
816 }
817
818 static void
819 release_all_files(void)
820 {
821         int i;
822         struct nfs4_file *fp;
823
824         for (i=0;i<FILE_HASH_SIZE;i++) {
825                 while (!list_empty(&file_hashtbl[i])) {
826                         fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
827                         /* this should never be more than once... */
828                         if(!list_empty(&fp->fi_perfile)) {
829                                 printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
830                         }
831                         release_file(fp);
832                 }
833         }
834 }
835
836 static inline struct nfs4_stateowner *
837 alloc_stateowner(struct xdr_netobj *owner)
838 {
839         struct nfs4_stateowner *sop;
840
841         if ((sop = kmalloc(sizeof(struct nfs4_stateowner),GFP_KERNEL))) {
842                 if((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
843                         memcpy(sop->so_owner.data, owner->data, owner->len);
844                         sop->so_owner.len = owner->len;
845                         return sop;
846                 } 
847                 kfree(sop);
848         }
849         return (struct nfs4_stateowner *)NULL;
850 }
851
852 /* should use a slab cache */
853 static void
854 free_stateowner(struct nfs4_stateowner *sop) {
855         if(sop) {
856                 kfree(sop->so_owner.data);
857                 kfree(sop);
858                 sop = NULL;
859                 free_sowner++;
860         }
861 }
862
863 static struct nfs4_stateowner *
864 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
865         struct nfs4_stateowner *sop;
866         struct nfs4_replay *rp;
867         unsigned int idhashval;
868
869         if (!(sop = alloc_stateowner(&open->op_owner)))
870                 return (struct nfs4_stateowner *)NULL;
871         idhashval = ownerid_hashval(current_ownerid);
872         INIT_LIST_HEAD(&sop->so_idhash);
873         INIT_LIST_HEAD(&sop->so_strhash);
874         INIT_LIST_HEAD(&sop->so_perclient);
875         INIT_LIST_HEAD(&sop->so_perfilestate);
876         INIT_LIST_HEAD(&sop->so_perlockowner);  /* not used */
877         INIT_LIST_HEAD(&sop->so_close_lru);
878         sop->so_time = 0;
879         list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
880         list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
881         list_add(&sop->so_perclient, &clp->cl_perclient);
882         add_perclient++;
883         sop->so_is_open_owner = 1;
884         sop->so_id = current_ownerid++;
885         sop->so_client = clp;
886         sop->so_seqid = open->op_seqid;
887         sop->so_confirmed = 0;
888         rp = &sop->so_replay;
889         rp->rp_status = NFSERR_SERVERFAULT;
890         rp->rp_buflen = 0;
891         rp->rp_buf = rp->rp_ibuf;
892         alloc_sowner++;
893         return sop;
894 }
895
896 static void
897 release_stateid_lockowner(struct nfs4_stateid *open_stp)
898 {
899         struct nfs4_stateowner *lock_sop;
900
901         while (!list_empty(&open_stp->st_perlockowner)) {
902                 lock_sop = list_entry(open_stp->st_perlockowner.next,
903                                 struct nfs4_stateowner, so_perlockowner);
904                 /* list_del(&open_stp->st_perlockowner);  */
905                 BUG_ON(lock_sop->so_is_open_owner);
906                 release_stateowner(lock_sop);
907         }
908 }
909
910 static void
911 release_stateowner(struct nfs4_stateowner *sop)
912 {
913         struct nfs4_stateid *stp;
914
915         list_del(&sop->so_idhash);
916         list_del(&sop->so_strhash);
917         list_del(&sop->so_perclient);
918         list_del(&sop->so_perlockowner);
919         list_del(&sop->so_close_lru);
920         del_perclient++;
921         while (!list_empty(&sop->so_perfilestate)) {
922                 stp = list_entry(sop->so_perfilestate.next, 
923                         struct nfs4_stateid, st_perfilestate);
924                 if(sop->so_is_open_owner)
925                         release_stateid(stp, OPEN_STATE);
926                 else
927                         release_stateid(stp, LOCK_STATE);
928         }
929         free_stateowner(sop);
930 }
931
932 static inline void
933 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfs4_stateowner *sop, struct nfsd4_open *open) {
934         unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
935
936         INIT_LIST_HEAD(&stp->st_hash);
937         INIT_LIST_HEAD(&stp->st_perfilestate);
938         INIT_LIST_HEAD(&stp->st_perlockowner);
939         INIT_LIST_HEAD(&stp->st_perfile);
940         list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
941         list_add(&stp->st_perfilestate, &sop->so_perfilestate);
942         list_add_perfile++;
943         list_add(&stp->st_perfile, &fp->fi_perfile);
944         stp->st_stateowner = sop;
945         stp->st_file = fp;
946         stp->st_stateid.si_boot = boot_time;
947         stp->st_stateid.si_stateownerid = sop->so_id;
948         stp->st_stateid.si_fileid = fp->fi_id;
949         stp->st_stateid.si_generation = 0;
950         stp->st_access_bmap = 0;
951         stp->st_deny_bmap = 0;
952         __set_bit(open->op_share_access, &stp->st_access_bmap);
953         __set_bit(open->op_share_deny, &stp->st_deny_bmap);
954 }
955
956 static void
957 release_stateid(struct nfs4_stateid *stp, int flags) {
958
959         list_del(&stp->st_hash);
960         list_del_perfile++;
961         list_del(&stp->st_perfile);
962         list_del(&stp->st_perfilestate);
963         if((stp->st_vfs_set) && (flags & OPEN_STATE)) {
964                 release_stateid_lockowner(stp);
965                 nfsd_close(&stp->st_vfs_file);
966                 vfsclose++;
967                 dput(stp->st_vfs_file.f_dentry);
968                 mntput(stp->st_vfs_file.f_vfsmnt);
969         } else if ((stp->st_vfs_set) && (flags & LOCK_STATE)) {
970                 struct file *filp = &stp->st_vfs_file;
971
972                 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
973         }
974         kfree(stp);
975         stp = NULL;
976 }
977
978 static void
979 release_file(struct nfs4_file *fp)
980 {
981         free_file++;
982         list_del(&fp->fi_hash);
983         iput(fp->fi_inode);
984         kfree(fp);
985 }       
986
987 void
988 move_to_close_lru(struct nfs4_stateowner *sop)
989 {
990         dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
991         /* remove stateowner from all other hash lists except perclient */
992         list_del_init(&sop->so_idhash);
993         list_del_init(&sop->so_strhash);
994         list_del_init(&sop->so_perlockowner);
995
996         list_add_tail(&sop->so_close_lru, &close_lru);
997         sop->so_time = get_seconds();
998 }
999
1000 void
1001 release_state_owner(struct nfs4_stateid *stp, struct nfs4_stateowner **sopp,
1002                 int flag)
1003 {
1004         struct nfs4_stateowner *sop = stp->st_stateowner;
1005         struct nfs4_file *fp = stp->st_file;
1006
1007         dprintk("NFSD: release_state_owner\n");
1008         release_stateid(stp, flag);
1009
1010         /* place unused nfs4_stateowners on so_close_lru list to be
1011          * released by the laundromat service after the lease period
1012          * to enable us to handle CLOSE replay
1013          */
1014         if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1015                 move_to_close_lru(sop);
1016         /* unused nfs4_file's are releseed. XXX slab cache? */
1017         if (list_empty(&fp->fi_perfile)) {
1018                 release_file(fp);
1019         }
1020 }
1021
1022 static int
1023 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1024         return ((sop->so_owner.len == owner->len) && 
1025          !memcmp(sop->so_owner.data, owner->data, owner->len) && 
1026           (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1027 }
1028
1029 /* search ownerstr_hashtbl[] for owner */
1030 static int
1031 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, struct nfs4_stateowner **op) {
1032         struct nfs4_stateowner *local = NULL;
1033
1034         list_for_each_entry(local, &ownerstr_hashtbl[hashval], so_strhash) {
1035                 if(!cmp_owner_str(local, &open->op_owner, &open->op_clientid)) 
1036                         continue;
1037                 *op = local;
1038                 return(1);
1039         }
1040         return 0;
1041 }
1042
1043 /* see if clientid is in confirmed hash table */
1044 static int
1045 verify_clientid(struct nfs4_client **client, clientid_t *clid) {
1046
1047         struct nfs4_client *clp;
1048         unsigned int idhashval = clientid_hashval(clid->cl_id);
1049
1050         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1051                 if (!cmp_clid(&clp->cl_clientid, clid))
1052                         continue;
1053                 *client = clp;
1054                 return 1;
1055         }
1056         *client = NULL;
1057         return 0;
1058 }
1059
1060 /* search file_hashtbl[] for file */
1061 static int
1062 find_file(unsigned int hashval, struct inode *ino, struct nfs4_file **fp) {
1063         struct nfs4_file *local = NULL;
1064
1065         list_for_each_entry(local, &file_hashtbl[hashval], fi_hash) {
1066                 if (local->fi_inode == ino) {
1067                         *fp = local;
1068                         return(1);
1069                 }
1070         }
1071         return 0;
1072 }
1073
1074 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1075 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1076
1077 void
1078 set_access(unsigned int *access, unsigned long bmap) {
1079         int i;
1080
1081         *access = 0;
1082         for (i = 1; i < 4; i++) {
1083                 if(test_bit(i, &bmap))
1084                         *access |= i;
1085         }
1086 }
1087
1088 void
1089 set_deny(unsigned int *deny, unsigned long bmap) {
1090         int i;
1091
1092         *deny = 0;
1093         for (i = 0; i < 4; i++) {
1094                 if(test_bit(i, &bmap))
1095                         *deny |= i ;
1096         }
1097 }
1098
1099 static int
1100 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1101         unsigned int access, deny;
1102
1103         set_access(&access, stp->st_access_bmap);
1104         set_deny(&deny, stp->st_deny_bmap);
1105         if ((access & open->op_share_deny) || (deny & open->op_share_access))
1106                 return 0;
1107         return 1;
1108 }
1109
1110 /*
1111  * Called to check deny when READ with all zero stateid or
1112  * WRITE with all zero or all one stateid
1113  */
1114 int
1115 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1116 {
1117         struct inode *ino = current_fh->fh_dentry->d_inode;
1118         unsigned int fi_hashval;
1119         struct nfs4_file *fp;
1120         struct nfs4_stateid *stp;
1121
1122         dprintk("NFSD: nfs4_share_conflict\n");
1123
1124         fi_hashval = file_hashval(ino);
1125         if (find_file(fi_hashval, ino, &fp)) {
1126         /* Search for conflicting share reservations */
1127                 list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1128                         if (test_bit(deny_type, &stp->st_deny_bmap) ||
1129                             test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1130                                 return nfserr_share_denied;
1131                 }
1132         }
1133         return nfs_ok;
1134 }
1135
1136 static inline int
1137 nfs4_file_upgrade(struct file *filp, unsigned int share_access)
1138 {
1139 int status;
1140
1141         if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1142                 status = get_write_access(filp->f_dentry->d_inode);
1143                 if (!status)
1144                         filp->f_mode = FMODE_WRITE;
1145                 else
1146                         return nfserrno(status);
1147         }
1148         return nfs_ok;
1149 }
1150
1151 static inline void
1152 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1153 {
1154         if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1155                 put_write_access(filp->f_dentry->d_inode);
1156                 filp->f_mode = FMODE_READ;
1157         }
1158 }
1159
1160
1161 /*
1162  * nfsd4_process_open1()
1163  *      lookup stateowner.
1164  *              found:
1165  *                      check confirmed 
1166  *                              confirmed:
1167  *                                      check seqid
1168  *                              not confirmed:
1169  *                                      delete owner
1170  *                                      create new owner
1171  *              notfound:
1172  *                      verify clientid
1173  *                      create new owner
1174  *
1175  * called with nfs4_lock_state() held.
1176  */
1177 int
1178 nfsd4_process_open1(struct nfsd4_open *open)
1179 {
1180         int status;
1181         clientid_t *clientid = &open->op_clientid;
1182         struct nfs4_client *clp = NULL;
1183         unsigned int strhashval;
1184         struct nfs4_stateowner *sop = NULL;
1185
1186         status = nfserr_inval;
1187         if (!check_name(open->op_owner))
1188                 goto out;
1189
1190         status = nfserr_stale_clientid;
1191         if (STALE_CLIENTID(&open->op_clientid))
1192                 return status;
1193
1194         strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1195         if (find_openstateowner_str(strhashval, open, &sop)) {
1196                 open->op_stateowner = sop;
1197                 /* check for replay */
1198                 if (open->op_seqid == sop->so_seqid){
1199                         if (!sop->so_replay.rp_buflen) {
1200                         /*
1201                         * The original OPEN failed in so spectacularly that we
1202                         * don't even have replay data saved!  Therefore, we
1203                         * have no choice but to continue processing
1204                         * this OPEN; presumably, we'll fail again for the same
1205                         * reason.
1206                         */
1207                                 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1208                                 status = NFS_OK;
1209                                 goto renew;
1210                         }
1211                         /* replay: indicate to calling function */
1212                         status = NFSERR_REPLAY_ME;
1213                         return status;
1214                 }
1215                 if (sop->so_confirmed) {
1216                         if (open->op_seqid == sop->so_seqid + 1) { 
1217                                 status = nfs_ok;
1218                                 goto renew;
1219                         } 
1220                         status = nfserr_bad_seqid;
1221                         goto out;
1222                 }
1223                 /* If we get here, we received and OPEN for an unconfirmed
1224                  * nfs4_stateowner. 
1225                  * Since the sequid's are different, purge the 
1226                  * existing nfs4_stateowner, and instantiate a new one.
1227                  */
1228                 clp = sop->so_client;
1229                 release_stateowner(sop);
1230                 goto instantiate_new_owner;
1231         } 
1232         /* nfs4_stateowner not found. 
1233         * verify clientid and instantiate new nfs4_stateowner
1234         * if verify fails this is presumably the result of the 
1235         * client's lease expiring.
1236         *
1237         * XXX compare clp->cl_addr with rqstp addr? 
1238         */
1239         status = nfserr_expired;
1240         if (!verify_clientid(&clp, clientid))
1241                 goto out;
1242 instantiate_new_owner:
1243         status = nfserr_resource;
1244         if (!(sop = alloc_init_open_stateowner(strhashval, clp, open))) 
1245                 goto out;
1246         open->op_stateowner = sop;
1247         status = nfs_ok;
1248 renew:
1249         renew_client(sop->so_client);
1250 out:
1251         if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1252                 status = nfserr_reclaim_bad;
1253         return status;
1254 }
1255 /*
1256  * called with nfs4_lock_state() held.
1257  */
1258 int
1259 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1260 {
1261         struct iattr iattr;
1262         struct nfs4_stateowner *sop = open->op_stateowner;
1263         struct nfs4_file *fp = NULL;
1264         struct inode *ino;
1265         unsigned int fi_hashval;
1266         struct nfs4_stateid *stq, *stp = NULL;
1267         int status;
1268
1269         status = nfserr_resource;
1270         if (!sop)
1271                 return status;
1272
1273         ino = current_fh->fh_dentry->d_inode;
1274
1275         status = nfserr_inval;
1276         if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1277                 goto out;
1278
1279         fi_hashval = file_hashval(ino);
1280         if (find_file(fi_hashval, ino, &fp)) {
1281                 /* Search for conflicting share reservations */
1282                 status = nfserr_share_denied;
1283                 list_for_each_entry(stq, &fp->fi_perfile, st_perfile) {
1284                         if(stq->st_stateowner == sop) {
1285                                 stp = stq;
1286                                 continue;
1287                         }
1288                         /* ignore lock owners */
1289                         if (stq->st_stateowner->so_is_open_owner == 0)
1290                                 continue;
1291                         if (!test_share(stq,open))      
1292                                 goto out;
1293                 }
1294         } else {
1295         /* No nfs4_file found; allocate and init a new one */
1296                 status = nfserr_resource;
1297                 if ((fp = alloc_init_file(fi_hashval, ino)) == NULL)
1298                         goto out;
1299         }
1300
1301         if (!stp) {
1302                 int flags = 0;
1303
1304                 status = nfserr_resource;
1305                 if ((stp = kmalloc(sizeof(struct nfs4_stateid),
1306                                                 GFP_KERNEL)) == NULL)
1307                         goto out;
1308
1309                 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1310                         flags = MAY_WRITE;
1311                 else
1312                         flags = MAY_READ;
1313                 if ((status = nfsd_open(rqstp, current_fh,  S_IFREG,
1314                                               flags,
1315                                               &stp->st_vfs_file)) != 0)
1316                         goto out_free;
1317
1318                 vfsopen++;
1319                 dget(stp->st_vfs_file.f_dentry);
1320                 mntget(stp->st_vfs_file.f_vfsmnt);
1321
1322                 init_stateid(stp, fp, sop, open);
1323                 stp->st_vfs_set = 1;
1324         } else {
1325                 /* This is an upgrade of an existing OPEN. 
1326                  * OR the incoming share with the existing 
1327                  * nfs4_stateid share */
1328                 unsigned int share_access;
1329
1330                 set_access(&share_access, stp->st_access_bmap);
1331                 share_access = ~share_access;
1332                 share_access &= open->op_share_access;
1333
1334                 /* update the struct file */
1335                 if ((status = nfs4_file_upgrade(&stp->st_vfs_file, share_access)))
1336                         goto out;
1337                 /* remember the open */
1338                 set_bit(open->op_share_access, &stp->st_access_bmap);
1339                 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1340                 /* bump the stateid */
1341                 update_stateid(&stp->st_stateid);
1342         }
1343         dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n\n",
1344                     stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1345                     stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1346
1347         if (open->op_truncate) {
1348                 iattr.ia_valid = ATTR_SIZE;
1349                 iattr.ia_size = 0;
1350                 status = nfsd_setattr(rqstp, current_fh, &iattr, 0, (time_t)0);
1351                 if (status)
1352                         goto out;
1353         }
1354         memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1355
1356         open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
1357         status = nfs_ok;
1358 out:
1359         if (fp && list_empty(&fp->fi_perfile))
1360                 release_file(fp);
1361
1362         if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1363                 if (status)
1364                         status = nfserr_reclaim_bad;
1365                 else {
1366                 /* successful reclaim. so_seqid is decremented because
1367                 * it will be bumped in encode_open
1368                 */
1369                         open->op_stateowner->so_confirmed = 1;
1370                         open->op_stateowner->so_seqid--;
1371                 }
1372         }
1373         /*
1374         * To finish the open response, we just need to set the rflags.
1375         */
1376         open->op_rflags = 0;
1377         if (!open->op_stateowner->so_confirmed)
1378                 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1379
1380         return status;
1381 out_free:
1382         kfree(stp);
1383         goto out;
1384 }
1385
1386 static struct work_struct laundromat_work;
1387 static void laundromat_main(void *);
1388 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1389
1390 int 
1391 nfsd4_renew(clientid_t *clid)
1392 {
1393         struct nfs4_client *clp;
1394         unsigned int idhashval;
1395         int status;
1396
1397         nfs4_lock_state();
1398         dprintk("process_renew(%08x/%08x): starting\n", 
1399                         clid->cl_boot, clid->cl_id);
1400         status = nfserr_stale_clientid;
1401         if (STALE_CLIENTID(clid))
1402                 goto out;
1403         status = nfs_ok;
1404         idhashval = clientid_hashval(clid->cl_id);
1405         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
1406                 if (!cmp_clid(&clp->cl_clientid, clid))
1407                         continue;
1408                 renew_client(clp);
1409                 goto out;
1410         }
1411         list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1412                 if (!cmp_clid(&clp->cl_clientid, clid))
1413                         continue;
1414                 renew_client(clp);
1415         goto out;
1416         }
1417         /*
1418         * Couldn't find an nfs4_client for this clientid.  
1419         * Presumably this is because the client took too long to 
1420         * RENEW, so return NFS4ERR_EXPIRED.
1421         */
1422         dprintk("nfsd4_renew: clientid not found!\n");
1423         status = nfserr_expired;
1424 out:
1425         nfs4_unlock_state();
1426         return status;
1427 }
1428
1429 time_t
1430 nfs4_laundromat(void)
1431 {
1432         struct nfs4_client *clp;
1433         struct nfs4_stateowner *sop;
1434         struct list_head *pos, *next;
1435         time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1436         time_t t, clientid_val = NFSD_LEASE_TIME;
1437         time_t u, close_val = NFSD_LEASE_TIME;
1438
1439         nfs4_lock_state();
1440
1441         dprintk("NFSD: laundromat service - starting, examining clients\n");
1442         list_for_each_safe(pos, next, &client_lru) {
1443                 clp = list_entry(pos, struct nfs4_client, cl_lru);
1444                 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1445                         t = clp->cl_time - cutoff;
1446                         if (clientid_val > t)
1447                                 clientid_val = t;
1448                         break;
1449                 }
1450                 dprintk("NFSD: purging unused client (clientid %08x)\n",
1451                         clp->cl_clientid.cl_id);
1452                 expire_client(clp);
1453         }
1454         list_for_each_safe(pos, next, &close_lru) {
1455                 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1456                 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1457                         u = sop->so_time - cutoff;
1458                         if (close_val > u)
1459                                 close_val = u;
1460                         break;
1461                 }
1462                 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1463                         sop->so_id);
1464                 release_stateowner(sop);
1465         }
1466         if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1467                 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1468         nfs4_unlock_state();
1469         return clientid_val;
1470 }
1471
1472 void
1473 laundromat_main(void *not_used)
1474 {
1475         time_t t;
1476
1477         t = nfs4_laundromat();
1478         dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1479         schedule_delayed_work(&laundromat_work, t*HZ);
1480 }
1481
1482 /* search ownerid_hashtbl[] and close_lru for stateid owner
1483  * (stateid->si_stateownerid)
1484  */
1485 struct nfs4_stateowner *
1486 find_openstateowner_id(u32 st_id, int flags) {
1487         struct nfs4_stateowner *local = NULL;
1488
1489         dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1490         if (flags & CLOSE_STATE) {
1491                 list_for_each_entry(local, &close_lru, so_close_lru) {
1492                         if(local->so_id == st_id)
1493                                 return local;
1494                 }
1495         }
1496         return NULL;
1497 }
1498
1499 static inline int
1500 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1501 {
1502         return (stp->st_vfs_set == 0 ||
1503                 fhp->fh_dentry->d_inode != stp->st_vfs_file.f_dentry->d_inode);
1504 }
1505
1506 static int
1507 STALE_STATEID(stateid_t *stateid)
1508 {
1509         if (stateid->si_boot == boot_time)
1510                 return 0;
1511         printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1512                 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1513                 stateid->si_generation);
1514         return 1;
1515 }
1516
1517
1518 /*
1519 * Checks for stateid operations
1520 */
1521 int
1522 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct nfs4_stateid **stpp)
1523 {
1524         struct nfs4_stateid *stp;
1525         int status;
1526
1527         dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
1528                 stateid->si_boot, stateid->si_stateownerid, 
1529                 stateid->si_fileid, stateid->si_generation); 
1530
1531         *stpp = NULL;
1532
1533         /* STALE STATEID */
1534         status = nfserr_stale_stateid;
1535         if (STALE_STATEID(stateid)) 
1536                 goto out;
1537
1538         /* BAD STATEID */
1539         status = nfserr_bad_stateid;
1540         if (!(stp = find_stateid(stateid, flags))) {
1541                 dprintk("NFSD: preprocess_stateid_op: no open stateid!\n");
1542                 goto out;
1543         }
1544         if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
1545                 dprintk("NFSD: preprocess_stateid_op: fh-stateid mismatch!\n");
1546                 stp->st_vfs_set = 0;
1547                 goto out;
1548         }
1549         if (!stp->st_stateowner->so_confirmed) {
1550                 dprintk("preprocess_stateid_op: lockowner not confirmed yet!\n");
1551                 goto out;
1552         }
1553         if (stateid->si_generation > stp->st_stateid.si_generation) {
1554                 dprintk("preprocess_stateid_op: future stateid?!\n");
1555                 goto out;
1556         }
1557
1558         /* OLD STATEID */
1559         status = nfserr_old_stateid;
1560         if (stateid->si_generation < stp->st_stateid.si_generation) {
1561                 dprintk("preprocess_stateid_op: old stateid!\n");
1562                 goto out;
1563         }
1564         *stpp = stp;
1565         status = nfs_ok;
1566         renew_client(stp->st_stateowner->so_client);
1567 out:
1568         return status;
1569 }
1570
1571
1572 /* 
1573  * Checks for sequence id mutating operations. 
1574  */
1575 int
1576 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, clientid_t *lockclid)
1577 {
1578         int status;
1579         struct nfs4_stateid *stp;
1580         struct nfs4_stateowner *sop;
1581
1582         dprintk("NFSD: preprocess_seqid_op: seqid=%d " 
1583                         "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
1584                 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1585                 stateid->si_generation);
1586                                 
1587         *stpp = NULL;
1588         *sopp = NULL;
1589
1590         status = nfserr_bad_stateid;
1591         if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
1592                 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
1593                 goto out;
1594         }
1595
1596         status = nfserr_stale_stateid;
1597         if (STALE_STATEID(stateid))
1598                 goto out;
1599         /*
1600         * We return BAD_STATEID if filehandle doesn't match stateid, 
1601         * the confirmed flag is incorrecly set, or the generation 
1602         * number is incorrect.  
1603         * If there is no entry in the openfile table for this id, 
1604         * we can't always return BAD_STATEID;
1605         * this might be a retransmitted CLOSE which has arrived after 
1606         * the openfile has been released.
1607         */
1608         if (!(stp = find_stateid(stateid, flags)))
1609                 goto no_nfs4_stateid;
1610
1611         status = nfserr_bad_stateid;
1612
1613         /* for new lock stateowners:
1614          * check that the lock->v.new.open_stateid
1615          * refers to an open stateowner
1616          *
1617          * check that the lockclid (nfs4_lock->v.new.clientid) is the same
1618          * as the open_stateid->st_stateowner->so_client->clientid
1619          */
1620         if (lockclid) {
1621                 struct nfs4_stateowner *sop = stp->st_stateowner;
1622                 struct nfs4_client *clp = sop->so_client;
1623
1624                 if (!sop->so_is_open_owner)
1625                         goto out;
1626                 if (!cmp_clid(&clp->cl_clientid, lockclid))
1627                         goto out;
1628         }
1629
1630         if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
1631                 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
1632                 stp->st_vfs_set = 0;
1633                 goto out;
1634         }
1635
1636         *stpp = stp;
1637         *sopp = sop = stp->st_stateowner;
1638
1639         /*
1640         *  We now validate the seqid and stateid generation numbers.
1641         *  For the moment, we ignore the possibility of 
1642         *  generation number wraparound.
1643         */
1644         if (seqid != sop->so_seqid + 1)
1645                 goto check_replay;
1646
1647         if (sop->so_confirmed) {
1648                 if (flags & CONFIRM) {
1649                         printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
1650                         goto out;
1651                 }
1652         }
1653         else {
1654                 if (!(flags & CONFIRM)) {
1655                         printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
1656                         goto out;
1657                 }
1658         }
1659         if (stateid->si_generation > stp->st_stateid.si_generation) {
1660                 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
1661                 goto out;
1662         }
1663
1664         status = nfserr_old_stateid;
1665         if (stateid->si_generation < stp->st_stateid.si_generation) {
1666                 printk("NFSD: preprocess_seqid_op: old stateid!\n");
1667                 goto out;
1668         }
1669         /* XXX renew the client lease here */
1670         status = nfs_ok;
1671
1672 out:
1673         return status;
1674
1675 no_nfs4_stateid:
1676
1677         /*
1678         * We determine whether this is a bad stateid or a replay, 
1679         * starting by trying to look up the stateowner.
1680         * If stateowner is not found - stateid is bad.
1681         */
1682         if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
1683                 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
1684                 status = nfserr_bad_stateid;
1685                 goto out;
1686         }
1687         *sopp = sop;
1688
1689 check_replay:
1690         if (seqid == sop->so_seqid) {
1691                 printk("NFSD: preprocess_seqid_op: retransmission?\n");
1692                 /* indicate replay to calling function */
1693                 status = NFSERR_REPLAY_ME;
1694         } else  {
1695                 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
1696
1697                 *sopp = NULL;
1698                 status = nfserr_bad_seqid;
1699         }
1700         goto out;
1701 }
1702
1703 /*
1704  * eventually, this will perform an upcall to the 'state daemon' as well as
1705  * set the cl_first_state field.
1706  */
1707 void
1708 first_state(struct nfs4_client *clp)
1709 {
1710         if (!clp->cl_first_state)
1711                 clp->cl_first_state = get_seconds();
1712 }
1713
1714 /*
1715  * nfs4_unlock_state(); called in encode
1716  */
1717 int
1718 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
1719 {
1720         int status;
1721         struct nfs4_stateowner *sop;
1722         struct nfs4_stateid *stp;
1723
1724         dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
1725                         (int)current_fh->fh_dentry->d_name.len,
1726                         current_fh->fh_dentry->d_name.name);
1727
1728         if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
1729                 goto out;
1730
1731         oc->oc_stateowner = NULL;
1732         nfs4_lock_state();
1733
1734         if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
1735                                         &oc->oc_req_stateid,
1736                                         CHECK_FH | CONFIRM | OPEN_STATE,
1737                                         &oc->oc_stateowner, &stp, NULL)))
1738                 goto out; 
1739
1740         sop = oc->oc_stateowner;
1741         sop->so_confirmed = 1;
1742         update_stateid(&stp->st_stateid);
1743         memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
1744         dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d " 
1745                 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
1746                          stp->st_stateid.si_boot,
1747                          stp->st_stateid.si_stateownerid,
1748                          stp->st_stateid.si_fileid,
1749                          stp->st_stateid.si_generation);
1750         status = nfs_ok;
1751         first_state(sop->so_client);
1752 out:
1753         return status;
1754 }
1755
1756
1757 /*
1758  * unset all bits in union bitmap (bmap) that
1759  * do not exist in share (from successful OPEN_DOWNGRADE)
1760  */
1761 static void
1762 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
1763 {
1764         int i;
1765         for (i = 1; i < 4; i++) {
1766                 if ((i & access) != i)
1767                         __clear_bit(i, bmap);
1768         }
1769 }
1770
1771 static void
1772 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
1773 {
1774         int i;
1775         for (i = 0; i < 4; i++) {
1776                 if ((i & deny) != i)
1777                         __clear_bit(i, bmap);
1778         }
1779 }
1780
1781 /*
1782  * nfs4_unlock_state(); called in encode
1783  */
1784
1785 int
1786 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
1787 {
1788         int status;
1789         struct nfs4_stateid *stp;
1790         unsigned int share_access;
1791
1792         dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n", 
1793                         (int)current_fh->fh_dentry->d_name.len,
1794                         current_fh->fh_dentry->d_name.name);
1795
1796         od->od_stateowner = NULL;
1797         status = nfserr_inval;
1798         if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
1799                 goto out;
1800
1801         nfs4_lock_state();
1802         if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid, 
1803                                         &od->od_stateid, 
1804                                         CHECK_FH | OPEN_STATE, 
1805                                         &od->od_stateowner, &stp, NULL)))
1806                 goto out; 
1807
1808         status = nfserr_inval;
1809         if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
1810                 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
1811                         stp->st_access_bmap, od->od_share_access);
1812                 goto out;
1813         }
1814         if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
1815                 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
1816                         stp->st_deny_bmap, od->od_share_deny);
1817                 goto out;
1818         }
1819         set_access(&share_access, stp->st_access_bmap);
1820         nfs4_file_downgrade(&stp->st_vfs_file, 
1821                             share_access & ~od->od_share_access);
1822
1823         reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
1824         reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
1825
1826         update_stateid(&stp->st_stateid);
1827         memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
1828         status = nfs_ok;
1829 out:
1830         return status;
1831 }
1832
1833 /*
1834  * nfs4_unlock_state() called after encode
1835  */
1836 int
1837 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
1838 {
1839         int status;
1840         struct nfs4_stateid *stp;
1841
1842         dprintk("NFSD: nfsd4_close on file %.*s\n", 
1843                         (int)current_fh->fh_dentry->d_name.len,
1844                         current_fh->fh_dentry->d_name.name);
1845
1846         close->cl_stateowner = NULL;
1847         nfs4_lock_state();
1848         /* check close_lru for replay */
1849         if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid, 
1850                                         &close->cl_stateid, 
1851                                         CHECK_FH | OPEN_STATE | CLOSE_STATE,
1852                                         &close->cl_stateowner, &stp, NULL)))
1853                 goto out; 
1854         /*
1855         *  Return success, but first update the stateid.
1856         */
1857         status = nfs_ok;
1858         update_stateid(&stp->st_stateid);
1859         memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
1860
1861         /* release_state_owner() calls nfsd_close() if needed */
1862         release_state_owner(stp, &close->cl_stateowner, OPEN_STATE);
1863 out:
1864         return status;
1865 }
1866
1867 /* 
1868  * Lock owner state (byte-range locks)
1869  */
1870 #define LOFF_OVERFLOW(start, len)      ((u64)(len) > ~(u64)(start))
1871 #define LOCK_HASH_BITS              8
1872 #define LOCK_HASH_SIZE             (1 << LOCK_HASH_BITS)
1873 #define LOCK_HASH_MASK             (LOCK_HASH_SIZE - 1)
1874
1875 #define lockownerid_hashval(id) \
1876         ((id) & LOCK_HASH_MASK)
1877 #define lock_ownerstr_hashval(x, clientid, ownername) \
1878         ((file_hashval(x) + (clientid) + opaque_hashval((ownername.data), (ownername.len))) & LOCK_HASH_MASK)
1879
1880 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
1881 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
1882 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
1883
1884 struct nfs4_stateid *
1885 find_stateid(stateid_t *stid, int flags)
1886 {
1887         struct nfs4_stateid *local = NULL;
1888         u32 st_id = stid->si_stateownerid;
1889         u32 f_id = stid->si_fileid;
1890         unsigned int hashval;
1891
1892         dprintk("NFSD: find_stateid flags 0x%x\n",flags);
1893         if ((flags & LOCK_STATE) || (flags & RDWR_STATE)) {
1894                 hashval = stateid_hashval(st_id, f_id);
1895                 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
1896                         if((local->st_stateid.si_stateownerid == st_id) &&
1897                            (local->st_stateid.si_fileid == f_id))
1898                                 return local;
1899                 }
1900         } 
1901         if ((flags & OPEN_STATE) || (flags & RDWR_STATE)) {
1902                 hashval = stateid_hashval(st_id, f_id);
1903                 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
1904                         if((local->st_stateid.si_stateownerid == st_id) &&
1905                            (local->st_stateid.si_fileid == f_id))
1906                                 return local;
1907                 }
1908         } else
1909                 printk("NFSD: find_stateid: ERROR: no state flag\n");
1910         return NULL;
1911 }
1912
1913
1914 /*
1915  * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
1916  * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
1917  * byte, because of sign extension problems.  Since NFSv4 calls for 64-bit
1918  * locking, this prevents us from being completely protocol-compliant.  The
1919  * real solution to this problem is to start using unsigned file offsets in
1920  * the VFS, but this is a very deep change!
1921  */
1922 static inline void
1923 nfs4_transform_lock_offset(struct file_lock *lock)
1924 {
1925         if (lock->fl_start < 0)
1926                 lock->fl_start = OFFSET_MAX;
1927         if (lock->fl_end < 0)
1928                 lock->fl_end = OFFSET_MAX;
1929 }
1930
1931 int
1932 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
1933 {
1934         struct nfs4_stateowner *local = NULL;
1935         int status = 0;
1936                                 
1937         if (hashval >= LOCK_HASH_SIZE)
1938                 goto out;
1939         list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
1940                 if (local == sop) {
1941                         status = 1;
1942                         goto out;
1943                 }
1944         }
1945 out:
1946         return status;
1947 }
1948
1949
1950 static inline void
1951 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
1952 {
1953         struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
1954
1955         deny->ld_sop = NULL;
1956         if (nfs4_verify_lock_stateowner(sop, fl->fl_pid))
1957                 deny->ld_sop = sop;
1958         deny->ld_start = fl->fl_start;
1959         deny->ld_length = ~(u64)0;
1960         if (fl->fl_end != ~(u64)0)
1961                 deny->ld_length = fl->fl_end - fl->fl_start + 1;        
1962         deny->ld_type = NFS4_READ_LT;
1963         if (fl->fl_type != F_RDLCK)
1964                 deny->ld_type = NFS4_WRITE_LT;
1965 }
1966
1967 static struct nfs4_stateowner *
1968 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
1969 {
1970         struct nfs4_stateowner *local = NULL;
1971         int i;
1972
1973         for (i = 0; i < LOCK_HASH_SIZE; i++) {
1974                 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
1975                         if(!cmp_owner_str(local, owner, clid))
1976                                 continue;
1977                         return local;
1978                 }
1979         }
1980         return NULL;
1981 }
1982
1983 static int
1984 find_lockstateowner_str(unsigned int hashval, struct xdr_netobj *owner, clientid_t *clid, struct nfs4_stateowner **op) {
1985         struct nfs4_stateowner *local = NULL;
1986
1987         list_for_each_entry(local, &lock_ownerstr_hashtbl[hashval], so_strhash) {
1988                 if(!cmp_owner_str(local, owner, clid)) 
1989                         continue;
1990                 *op = local;
1991                 return(1);
1992         }
1993         *op = NULL;
1994         return 0;
1995 }
1996
1997 /*
1998  * Alloc a lock owner structure.
1999  * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has 
2000  * occured. 
2001  *
2002  * strhashval = lock_ownerstr_hashval 
2003  * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode 
2004  */
2005
2006 static struct nfs4_stateowner *
2007 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2008         struct nfs4_stateowner *sop;
2009         struct nfs4_replay *rp;
2010         unsigned int idhashval;
2011
2012         if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2013                 return (struct nfs4_stateowner *)NULL;
2014         idhashval = lockownerid_hashval(current_ownerid);
2015         INIT_LIST_HEAD(&sop->so_idhash);
2016         INIT_LIST_HEAD(&sop->so_strhash);
2017         INIT_LIST_HEAD(&sop->so_perclient);
2018         INIT_LIST_HEAD(&sop->so_perfilestate);
2019         INIT_LIST_HEAD(&sop->so_perlockowner);
2020         INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2021         sop->so_time = 0;
2022         list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2023         list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2024         list_add(&sop->so_perclient, &clp->cl_perclient);
2025         list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2026         add_perclient++;
2027         sop->so_is_open_owner = 0;
2028         sop->so_id = current_ownerid++;
2029         sop->so_client = clp;
2030         sop->so_seqid = lock->lk_new_lock_seqid - 1;
2031         sop->so_confirmed = 1;
2032         rp = &sop->so_replay;
2033         rp->rp_status = NFSERR_SERVERFAULT;
2034         rp->rp_buflen = 0;
2035         rp->rp_buf = rp->rp_ibuf;
2036         alloc_lsowner++;
2037         return sop;
2038 }
2039
2040 struct nfs4_stateid *
2041 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2042 {
2043         struct nfs4_stateid *stp;
2044         unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2045
2046         if ((stp = kmalloc(sizeof(struct nfs4_stateid), 
2047                                         GFP_KERNEL)) == NULL)
2048                 goto out;
2049         INIT_LIST_HEAD(&stp->st_hash);
2050         INIT_LIST_HEAD(&stp->st_perfile);
2051         INIT_LIST_HEAD(&stp->st_perfilestate);
2052         INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2053         list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2054         list_add(&stp->st_perfile, &fp->fi_perfile);
2055         list_add_perfile++;
2056         list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2057         stp->st_stateowner = sop;
2058         stp->st_file = fp;
2059         stp->st_stateid.si_boot = boot_time;
2060         stp->st_stateid.si_stateownerid = sop->so_id;
2061         stp->st_stateid.si_fileid = fp->fi_id;
2062         stp->st_stateid.si_generation = 0;
2063         stp->st_vfs_file = open_stp->st_vfs_file;
2064         stp->st_vfs_set = open_stp->st_vfs_set;
2065         stp->st_access_bmap = open_stp->st_access_bmap;
2066         stp->st_deny_bmap = open_stp->st_deny_bmap;
2067
2068 out:
2069         return stp;
2070 }
2071
2072 int
2073 check_lock_length(u64 offset, u64 length)
2074 {
2075         return ((length == 0)  || ((length != ~(u64)0) &&
2076              LOFF_OVERFLOW(offset, length)));
2077 }
2078
2079 /*
2080  *  LOCK operation 
2081  *
2082  * nfs4_unlock_state(); called in encode
2083  */
2084 int
2085 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2086 {
2087         struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2088         struct nfs4_stateid *lock_stp;
2089         struct file *filp;
2090         struct file_lock file_lock;
2091         struct file_lock *conflock;
2092         int status = 0;
2093         unsigned int strhashval;
2094
2095         dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2096                 (long long) lock->lk_offset,
2097                 (long long) lock->lk_length);
2098
2099         if (nfs4_in_grace() && !lock->lk_reclaim)
2100                 return nfserr_grace;
2101         if (!nfs4_in_grace() && lock->lk_reclaim)
2102                 return nfserr_no_grace;
2103
2104         if (check_lock_length(lock->lk_offset, lock->lk_length))
2105                  return nfserr_inval;
2106
2107         lock->lk_stateowner = NULL;
2108         nfs4_lock_state();
2109
2110         if (lock->lk_is_new) {
2111         /*
2112          * Client indicates that this is a new lockowner.
2113          * Use open owner and open stateid to create lock owner and lock 
2114          * stateid.
2115          */
2116                 struct nfs4_stateid *open_stp = NULL;
2117                 struct nfs4_file *fp;
2118                 
2119                 status = nfserr_stale_clientid;
2120                 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2121                         printk("NFSD: nfsd4_lock: clientid is stale!\n");
2122                         goto out;
2123                 }
2124
2125                 /* is the new lock seqid presented by the client zero? */
2126                 status = nfserr_bad_seqid;
2127                 if (lock->v.new.lock_seqid != 0)
2128                         goto out;
2129
2130                 /* validate and update open stateid and open seqid */
2131                 status = nfs4_preprocess_seqid_op(current_fh, 
2132                                         lock->lk_new_open_seqid,
2133                                         &lock->lk_new_open_stateid,
2134                                         CHECK_FH | OPEN_STATE,
2135                                         &open_sop, &open_stp,
2136                                         &lock->v.new.clientid);
2137                 if (status) {
2138                         if (lock->lk_reclaim)
2139                                 status = nfserr_reclaim_bad;
2140                         goto out;
2141                 }
2142                 /* create lockowner and lock stateid */
2143                 fp = open_stp->st_file;
2144                 strhashval = lock_ownerstr_hashval(fp->fi_inode, 
2145                                 open_sop->so_client->cl_clientid.cl_id, 
2146                                 lock->v.new.owner);
2147                 /* 
2148                  * If we already have this lock owner, the client is in 
2149                  * error (or our bookeeping is wrong!) 
2150                  * for asking for a 'new lock'.
2151                  */
2152                 status = nfserr_bad_stateid;
2153                 lock_sop = find_lockstateowner(&lock->v.new.owner,
2154                                                 &lock->v.new.clientid);
2155                 if (lock_sop)
2156                         goto out;
2157                 status = nfserr_resource;
2158                 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2159                         goto out;
2160                 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner, 
2161                                                 fp, open_stp)) == NULL)
2162                         goto out;
2163                 /* bump the open seqid used to create the lock */
2164                 open_sop->so_seqid++;
2165         } else {
2166                 /* lock (lock owner + lock stateid) already exists */
2167                 status = nfs4_preprocess_seqid_op(current_fh,
2168                                        lock->lk_old_lock_seqid, 
2169                                        &lock->lk_old_lock_stateid, 
2170                                        CHECK_FH | LOCK_STATE, 
2171                                        &lock->lk_stateowner, &lock_stp, NULL);
2172                 if (status)
2173                         goto out;
2174         }
2175         /* lock->lk_stateowner and lock_stp have been created or found */
2176         filp = &lock_stp->st_vfs_file;
2177
2178         if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2179                 printk("NFSD: nfsd4_lock: permission denied!\n");
2180                 goto out;
2181         }
2182
2183         switch (lock->lk_type) {
2184                 case NFS4_READ_LT:
2185                 case NFS4_READW_LT:
2186                         file_lock.fl_type = F_RDLCK;
2187                 break;
2188                 case NFS4_WRITE_LT:
2189                 case NFS4_WRITEW_LT:
2190                         file_lock.fl_type = F_WRLCK;
2191                 break;
2192                 default:
2193                         status = nfserr_inval;
2194                 goto out;
2195         }
2196         file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2197         file_lock.fl_pid = lockownerid_hashval(lock->lk_stateowner->so_id);
2198         file_lock.fl_file = filp;
2199         file_lock.fl_flags = FL_POSIX;
2200         file_lock.fl_notify = NULL;
2201         file_lock.fl_insert = NULL;
2202         file_lock.fl_remove = NULL;
2203
2204         file_lock.fl_start = lock->lk_offset;
2205         if ((lock->lk_length == ~(u64)0) || 
2206                         LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2207                 file_lock.fl_end = ~(u64)0;
2208         else
2209                 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2210         nfs4_transform_lock_offset(&file_lock);
2211
2212         /*
2213         * Try to lock the file in the VFS.
2214         * Note: locks.c uses the BKL to protect the inode's lock list.
2215         */
2216
2217         status = posix_lock_file(filp, &file_lock);
2218         dprintk("NFSD: nfsd4_lock: posix_test_lock passed. posix_lock_file status %d\n",status);
2219         switch (-status) {
2220         case 0: /* success! */
2221                 update_stateid(&lock_stp->st_stateid);
2222                 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid, 
2223                                 sizeof(stateid_t));
2224                 goto out;
2225         case (EAGAIN):
2226                 goto conflicting_lock;
2227         case (EDEADLK):
2228                 status = nfserr_deadlock;
2229         default:        
2230                 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2231                 goto out_destroy_new_stateid;
2232         }
2233
2234 conflicting_lock:
2235         dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2236         status = nfserr_denied;
2237         /* XXX There is a race here. Future patch needed to provide 
2238          * an atomic posix_lock_and_test_file
2239          */
2240         if (!(conflock = posix_test_lock(filp, &file_lock))) {
2241                 status = nfserr_serverfault;
2242                 goto out;
2243         }
2244         nfs4_set_lock_denied(conflock, &lock->lk_denied);
2245
2246 out_destroy_new_stateid:
2247         if (lock->lk_is_new) {
2248                 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2249         /*
2250         * An error encountered after instantiation of the new
2251         * stateid has forced us to destroy it.
2252         */
2253                 if (!seqid_mutating_err(status))
2254                         open_sop->so_seqid--;
2255
2256                 release_state_owner(lock_stp, &lock->lk_stateowner, LOCK_STATE);
2257         }
2258 out:
2259         return status;
2260 }
2261
2262 /*
2263  * LOCKT operation
2264  */
2265 int
2266 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2267 {
2268         struct inode *inode;
2269         struct nfs4_stateowner *sop;
2270         struct file file;
2271         struct file_lock file_lock;
2272         struct file_lock *conflicting_lock;
2273         unsigned int strhashval;
2274         int status;
2275
2276         if (nfs4_in_grace())
2277                 return nfserr_grace;
2278
2279         if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2280                  return nfserr_inval;
2281
2282         lockt->lt_stateowner = NULL;
2283         nfs4_lock_state();
2284
2285         status = nfserr_stale_clientid;
2286         if (STALE_CLIENTID(&lockt->lt_clientid)) {
2287                 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2288                 goto out;
2289         }
2290
2291         if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2292                 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2293                 if (status == nfserr_symlink)
2294                         status = nfserr_inval;
2295                 goto out;
2296         }
2297
2298         inode = current_fh->fh_dentry->d_inode;
2299         switch (lockt->lt_type) {
2300                 case NFS4_READ_LT:
2301                 case NFS4_READW_LT:
2302                         file_lock.fl_type = F_RDLCK;
2303                 break;
2304                 case NFS4_WRITE_LT:
2305                 case NFS4_WRITEW_LT:
2306                         file_lock.fl_type = F_WRLCK;
2307                 break;
2308                 default:
2309                         printk("NFSD: nfs4_lockt: bad lock type!\n");
2310                         status = nfserr_inval;
2311                 goto out;
2312         }
2313
2314         strhashval = lock_ownerstr_hashval(inode, 
2315                         lockt->lt_clientid.cl_id, lockt->lt_owner);
2316
2317         find_lockstateowner_str(strhashval, &lockt->lt_owner,
2318                                         &lockt->lt_clientid, 
2319                                         &lockt->lt_stateowner);
2320         sop = lockt->lt_stateowner;
2321         if (sop) {
2322                 file_lock.fl_owner = (fl_owner_t) sop;
2323                 file_lock.fl_pid = lockownerid_hashval(sop->so_id);
2324         } else {
2325                 file_lock.fl_owner = NULL;
2326                 file_lock.fl_pid = 0;
2327         }
2328         file_lock.fl_flags = FL_POSIX;
2329
2330         file_lock.fl_start = lockt->lt_offset;
2331         if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2332                 file_lock.fl_end = ~(u64)0;
2333         else
2334                 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2335
2336         nfs4_transform_lock_offset(&file_lock);
2337
2338         /* posix_test_lock uses the struct file _only_ to resolve the inode.
2339          * since LOCKT doesn't require an OPEN, and therefore a struct
2340          * file may not exist, pass posix_test_lock a struct file with
2341          * only the dentry:inode set.
2342          */
2343         memset(&file, 0, sizeof (struct file));
2344         file.f_dentry = current_fh->fh_dentry;
2345
2346         status = nfs_ok;
2347         conflicting_lock = posix_test_lock(&file, &file_lock);
2348         if (conflicting_lock) {
2349                 status = nfserr_denied;
2350                 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2351         }
2352 out:
2353         nfs4_unlock_state();
2354         return status;
2355 }
2356
2357 int
2358 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2359 {
2360         struct nfs4_stateid *stp;
2361         struct file *filp = NULL;
2362         struct file_lock file_lock;
2363         int status;
2364                                                         
2365         dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2366                 (long long) locku->lu_offset,
2367                 (long long) locku->lu_length);
2368
2369         if (check_lock_length(locku->lu_offset, locku->lu_length))
2370                  return nfserr_inval;
2371
2372         locku->lu_stateowner = NULL;
2373         nfs4_lock_state();
2374                                                                                 
2375         if ((status = nfs4_preprocess_seqid_op(current_fh, 
2376                                         locku->lu_seqid, 
2377                                         &locku->lu_stateid, 
2378                                         CHECK_FH | LOCK_STATE, 
2379                                         &locku->lu_stateowner, &stp, NULL)))
2380                 goto out;
2381
2382         filp = &stp->st_vfs_file;
2383         BUG_ON(!filp);
2384         file_lock.fl_type = F_UNLCK;
2385         file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2386         file_lock.fl_pid = lockownerid_hashval(locku->lu_stateowner->so_id);
2387         file_lock.fl_file = filp;
2388         file_lock.fl_flags = FL_POSIX; 
2389         file_lock.fl_notify = NULL;
2390         file_lock.fl_insert = NULL;
2391         file_lock.fl_remove = NULL;
2392         file_lock.fl_start = locku->lu_offset;
2393
2394         if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2395                 file_lock.fl_end = ~(u64)0;
2396         else
2397                 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2398         nfs4_transform_lock_offset(&file_lock);
2399
2400         /*
2401         *  Try to unlock the file in the VFS.
2402         */
2403         status = posix_lock_file(filp, &file_lock); 
2404         if (status) {
2405                 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2406                 goto out_nfserr;
2407         }
2408         /*
2409         * OK, unlock succeeded; the only thing left to do is update the stateid.
2410         */
2411         update_stateid(&stp->st_stateid);
2412         memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2413
2414 out:
2415         return status;
2416
2417 out_nfserr:
2418         status = nfserrno(status);
2419         goto out;
2420 }
2421
2422 /*
2423  * returns
2424  *      1: locks held by lockowner
2425  *      0: no locks held by lockowner
2426  */
2427 static int
2428 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2429 {
2430         struct file_lock **flpp;
2431         struct inode *inode = filp->f_dentry->d_inode;
2432         int status = 0;
2433
2434         lock_kernel();
2435         for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2436                 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
2437                         status = 1;
2438                         goto out;
2439         }
2440 out:
2441         unlock_kernel();
2442         return status;
2443 }
2444
2445 int
2446 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2447 {
2448         clientid_t *clid = &rlockowner->rl_clientid;
2449         struct nfs4_stateowner *local = NULL;
2450         struct xdr_netobj *owner = &rlockowner->rl_owner;
2451         int status;
2452
2453         dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2454                 clid->cl_boot, clid->cl_id);
2455
2456         /* XXX check for lease expiration */
2457
2458         status = nfserr_stale_clientid;
2459         if (STALE_CLIENTID(clid)) {
2460                 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
2461                 return status;
2462         }
2463
2464         nfs4_lock_state();
2465
2466         status = nfs_ok;
2467         local = find_lockstateowner(owner, clid);
2468         if (local) {
2469                 struct nfs4_stateid *stp;
2470
2471                 /* check for any locks held by any stateid
2472                  * associated with the (lock) stateowner */
2473                 status = nfserr_locks_held;
2474                 list_for_each_entry(stp, &local->so_perfilestate,
2475                                 st_perfilestate) {
2476                         if(stp->st_vfs_set) {
2477                                 if (check_for_locks(&stp->st_vfs_file, local))
2478                                         goto out;
2479                         }
2480                 }
2481                 /* no locks held by (lock) stateowner */
2482                 status = nfs_ok;
2483                 release_stateowner(local);
2484         }
2485 out:
2486         nfs4_unlock_state();
2487         return status;
2488 }
2489
2490 static inline struct nfs4_client_reclaim *
2491 alloc_reclaim(int namelen)
2492 {
2493         struct nfs4_client_reclaim *crp = NULL;
2494
2495         crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
2496         if (!crp)
2497                 return NULL;
2498         crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
2499         if (!crp->cr_name.data) {
2500                 kfree(crp);
2501                 return NULL;
2502         }
2503         return crp;
2504 }
2505
2506 /*
2507  * failure => all reset bets are off, nfserr_no_grace...
2508  */
2509 static int
2510 nfs4_client_to_reclaim(struct nfs4_client *clp)
2511 {
2512         unsigned int strhashval;
2513         struct nfs4_client_reclaim *crp = NULL;
2514
2515         crp = alloc_reclaim(clp->cl_name.len);
2516         if (!crp)
2517                 return 0;
2518         strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
2519         INIT_LIST_HEAD(&crp->cr_strhash);
2520         list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
2521         memcpy(crp->cr_name.data, clp->cl_name.data, clp->cl_name.len);
2522         crp->cr_name.len = clp->cl_name.len;
2523         crp->cr_first_state = clp->cl_first_state;
2524         crp->cr_expired = 0;
2525         return 1;
2526 }
2527
2528 static void
2529 nfs4_release_reclaim(void)
2530 {
2531         struct nfs4_client_reclaim *crp = NULL;
2532         int i;
2533
2534         BUG_ON(!nfs4_reclaim_init);
2535         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
2536                 while (!list_empty(&reclaim_str_hashtbl[i])) {
2537                         crp = list_entry(reclaim_str_hashtbl[i].next,
2538                                         struct nfs4_client_reclaim, cr_strhash);
2539                         list_del(&crp->cr_strhash);
2540                         kfree(crp->cr_name.data);
2541                         kfree(crp);
2542                         reclaim_str_hashtbl_size--;
2543                 }
2544         }
2545         BUG_ON(reclaim_str_hashtbl_size);
2546 }
2547
2548 /*
2549  * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2550 struct nfs4_client_reclaim *
2551 nfs4_find_reclaim_client(clientid_t *clid)
2552 {
2553         unsigned int idhashval = clientid_hashval(clid->cl_id);
2554         unsigned int strhashval;
2555         struct nfs4_client *clp, *client = NULL;
2556         struct nfs4_client_reclaim *crp = NULL;
2557
2558
2559         /* find clientid in conf_id_hashtbl */
2560         list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
2561                 if (cmp_clid(&clp->cl_clientid, clid)) {
2562                         client = clp;
2563                         break;
2564                 }
2565         }
2566         if (!client)
2567                 return NULL;
2568
2569         /* find clp->cl_name in reclaim_str_hashtbl */
2570         strhashval = clientstr_hashval(client->cl_name.data,
2571                                       client->cl_name.len);
2572         list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
2573                 if(cmp_name(&crp->cr_name, &client->cl_name)) {
2574                         return crp;
2575                 }
2576         }
2577         return NULL;
2578 }
2579
2580 /*
2581 * Called from OPEN. Look for clientid in reclaim list.
2582 */
2583 int
2584 nfs4_check_open_reclaim(clientid_t *clid)
2585 {
2586         struct nfs4_client_reclaim *crp;
2587
2588         if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
2589                 return nfserr_reclaim_bad;
2590         if (crp->cr_expired)
2591                 return nfserr_no_grace;
2592         return nfs_ok;
2593 }
2594
2595
2596 /* 
2597  * Start and stop routines
2598  */
2599
2600 void 
2601 nfs4_state_init(void)
2602 {
2603         int i;
2604         time_t grace_time;
2605
2606         if (nfs4_init)
2607                 return;
2608         if (!nfs4_reclaim_init) {
2609                 for (i = 0; i < CLIENT_HASH_SIZE; i++)
2610                         INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
2611                 reclaim_str_hashtbl_size = 0;
2612                 nfs4_reclaim_init = 1;
2613         }
2614         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
2615                 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
2616                 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
2617                 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
2618                 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
2619         }
2620         for (i = 0; i < FILE_HASH_SIZE; i++) {
2621                 INIT_LIST_HEAD(&file_hashtbl[i]);
2622         }
2623         for (i = 0; i < OWNER_HASH_SIZE; i++) {
2624                 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
2625                 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
2626         }
2627         for (i = 0; i < STATEID_HASH_SIZE; i++) {
2628                 INIT_LIST_HEAD(&stateid_hashtbl[i]);
2629                 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
2630         }
2631         for (i = 0; i < LOCK_HASH_SIZE; i++) {
2632                 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
2633                 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
2634         }
2635         memset(&zerostateid, 0, sizeof(stateid_t));
2636         memset(&onestateid, ~0, sizeof(stateid_t));
2637
2638         INIT_LIST_HEAD(&close_lru);
2639         INIT_LIST_HEAD(&client_lru);
2640         boot_time = get_seconds();
2641         grace_time = max(old_lease_time, lease_time);
2642         if (reclaim_str_hashtbl_size == 0)
2643                 grace_time = 0;
2644         if (grace_time)
2645                 printk("NFSD: starting %ld-second grace period\n", grace_time);
2646         grace_end = boot_time + grace_time;
2647         INIT_WORK(&laundromat_work,laundromat_main, NULL);
2648         schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
2649         nfs4_init = 1;
2650 }
2651
2652 int
2653 nfs4_in_grace(void)
2654 {
2655         return get_seconds() < grace_end;
2656 }
2657
2658 void
2659 set_no_grace(void)
2660 {
2661         printk("NFSD: ERROR in reboot recovery.  State reclaims will fail.\n");
2662         grace_end = get_seconds();
2663 }
2664
2665 time_t
2666 nfs4_lease_time(void)
2667 {
2668         return lease_time;
2669 }
2670
2671 static void
2672 __nfs4_state_shutdown(void)
2673 {
2674         int i;
2675         struct nfs4_client *clp = NULL;
2676
2677         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
2678                 while (!list_empty(&conf_id_hashtbl[i])) {
2679                         clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
2680                         expire_client(clp);
2681                 }
2682                 while (!list_empty(&unconf_str_hashtbl[i])) {
2683                         clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
2684                         expire_client(clp);
2685                 }
2686         }
2687         release_all_files();
2688         cancel_delayed_work(&laundromat_work);
2689         flush_scheduled_work();
2690         nfs4_init = 0;
2691         dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
2692                         list_add_perfile, list_del_perfile);
2693         dprintk("NFSD: add_perclient %d del_perclient %d\n",
2694                         add_perclient, del_perclient);
2695         dprintk("NFSD: alloc_file %d free_file %d\n",
2696                         alloc_file, free_file);
2697         dprintk("NFSD: alloc_sowner %d alloc_lsowner %d free_sowner %d\n",
2698                         alloc_sowner, alloc_lsowner, free_sowner);
2699         dprintk("NFSD: vfsopen %d vfsclose %d\n",
2700                         vfsopen, vfsclose);
2701 }
2702
2703 void
2704 nfs4_state_shutdown(void)
2705 {
2706         nfs4_lock_state();
2707         nfs4_release_reclaim();
2708         __nfs4_state_shutdown();
2709         nfs4_unlock_state();
2710 }
2711
2712 /*
2713  * Called when leasetime is changed.
2714  *
2715  * if nfsd is not started, simply set the global lease.
2716  *
2717  * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
2718  * e.g: boot_time is reset, existing nfs4_client structs are
2719  * used to fill reclaim_str_hashtbl, then all state (except for the
2720  * reclaim_str_hashtbl) is re-initialized.
2721  *
2722  * if the old lease time is greater than the new lease time, the grace
2723  * period needs to be set to the old lease time to allow clients to reclaim
2724  * their state. XXX - we may want to set the grace period == lease time
2725  * after an initial grace period == old lease time
2726  *
2727  * if an error occurs in this process, the new lease is set, but the server
2728  * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
2729  * which means OPEN/LOCK/READ/WRITE will fail during grace period.
2730  *
2731  * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
2732  * OPEN and LOCK reclaims.
2733  */
2734 void
2735 nfs4_reset_lease(time_t leasetime)
2736 {
2737         struct nfs4_client *clp;
2738         int i;
2739
2740         printk("NFSD: New leasetime %ld\n",leasetime);
2741         if (!nfs4_init)
2742                 return;
2743         nfs4_lock_state();
2744         old_lease_time = lease_time;
2745         lease_time = leasetime;
2746
2747         nfs4_release_reclaim();
2748
2749         /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
2750         for (i = 0; i < CLIENT_HASH_SIZE; i++) {
2751                 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
2752                         if (!nfs4_client_to_reclaim(clp)) {
2753                                 nfs4_release_reclaim();
2754                                 goto init_state;
2755                         }
2756                         reclaim_str_hashtbl_size++;
2757                 }
2758         }
2759 init_state:
2760         __nfs4_state_shutdown();
2761         nfs4_state_init();
2762         nfs4_unlock_state();
2763 }
2764