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