2 * linux/fs/nfsd/nfs4state.c
4 * Copyright (c) 2001 The Regents of the University of Michigan.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <kandros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
37 #include <linux/param.h>
38 #include <linux/major.h>
39 #include <linux/slab.h>
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>
51 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 static time_t grace_end = 0;
56 static u32 current_clientid = 1;
57 static u32 current_ownerid;
58 static u32 current_fileid;
60 stateid_t zerostateid; /* bits all 0 */
61 stateid_t onestateid; /* bits all 1 */
64 u32 list_add_perfile = 0;
65 u32 list_del_perfile = 0;
66 u32 add_perclient = 0;
67 u32 del_perclient = 0;
76 /* forward declarations */
77 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
82 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
85 static struct semaphore client_sema;
94 * nfs4_unlock_state(); called in encode
97 nfs4_unlock_state(void)
103 opaque_hashval(const void *ptr, int nbytes)
105 unsigned char *cptr = (unsigned char *) ptr;
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);
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)
130 #define clientid_hashval(id) \
131 ((id) & CLIENT_HASH_MASK)
132 #define clientstr_hashval(name, namelen) \
133 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
135 /* conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
136 * setclientid_confirmed info.
138 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
141 * client_lru holds client queue ordered by nfs4_client.cl_time
144 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
145 * for last close replay.
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;
155 renew_client(struct nfs4_client *clp)
158 * Move client to the end to the LRU list.
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();
167 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
169 STALE_CLIENTID(clientid_t *clid)
171 if (clid->cl_boot == boot_time)
173 dprintk("NFSD stale clientid (%08x/%08x)\n",
174 clid->cl_boot, clid->cl_id);
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.
183 static inline struct nfs4_client *
184 alloc_client(struct xdr_netobj name)
186 struct nfs4_client *clp;
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;
203 free_client(struct nfs4_client *clp)
205 if (clp->cl_cred.cr_group_info)
206 put_group_info(clp->cl_cred.cr_group_info);
207 kfree(clp->cl_name.data);
212 expire_client(struct nfs4_client *clp)
214 struct nfs4_stateowner *sop;
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);
227 static struct nfs4_client *
228 create_client(struct xdr_netobj name) {
229 struct nfs4_client *clp;
231 if(!(clp = alloc_client(name)))
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);
242 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
243 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
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;
253 copy_cred(struct svc_cred *target, struct svc_cred *source) {
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);
262 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
265 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
269 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
270 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
274 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
275 return((cl1->cl_boot == cl2->cl_boot) &&
276 (cl1->cl_id == cl2->cl_id));
279 /* XXX what about NGROUP */
281 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
282 return(cr1->cr_uid == cr2->cr_uid);
287 gen_clid(struct nfs4_client *clp) {
288 clp->cl_clientid.cl_boot = boot_time;
289 clp->cl_clientid.cl_id = current_clientid++;
293 gen_confirm(struct nfs4_client *clp) {
298 p = (u32 *)clp->cl_confirm.data;
304 check_name(struct xdr_netobj name) {
308 if (name.len > NFS4_OPAQUE_LIMIT) {
309 printk("NFSD: check_name: name too long(%d)!\n", name.len);
316 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
318 unsigned int idhashval;
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();
328 move_to_confirmed(struct nfs4_client *clp, unsigned int idhashval)
330 unsigned int strhashval;
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,
338 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
343 * RFC 3010 has a complex implmentation description of processing a
344 * SETCLIENTID request consisting of 5 bullets, labeled as
345 * CASE0 - CASE4 below.
348 * callback information will be processed in a future patch
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
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.
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.
376 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
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,
383 nfs4_verifier clverifier = setclid->se_verf;
384 unsigned int strhashval;
385 struct nfs4_client * conf, * unconf, * new, * clp;
388 status = nfserr_inval;
389 if (!check_name(clname))
393 * XXX The Duplicate Request Cache (DRC) has been checked (??)
394 * We get here on a DRC miss.
397 strhashval = clientstr_hashval(clname.data, clname.len);
401 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
402 if (!cmp_name(&clp->cl_name, &clname))
406 * clname match, confirmed, different principal
407 * or different ip_address
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);
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);
424 * cl_name match from a previous SETCLIENTID operation
425 * XXX check for additional matches?
431 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
432 if (!cmp_name(&clp->cl_name, &clname))
434 /* cl_name match from a previous SETCLIENTID operation */
438 status = nfserr_resource;
442 * placed first, because it is the normal case.
445 expire_client(unconf);
446 if (!(new = create_client(clname)))
448 copy_verf(new, &clverifier);
449 new->cl_addr = ip_addr;
450 copy_cred(&new->cl_cred,&rqstp->rq_cred);
453 add_to_unconfirmed(new, strhashval);
454 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
457 * cl_name match, confirmed, principal match
458 * verifier match: probable callback update
460 * remove any unconfirmed nfs4_client with
461 * matching cl_name, cl_verifier, and cl_clientid
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
469 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
470 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
471 expire_client(unconf);
473 if (!(new = create_client(clname)))
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);
480 add_to_unconfirmed(new,strhashval);
481 } else if (!unconf) {
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.
490 if (!(new = create_client(clname)))
492 copy_verf(new,&clverifier);
493 new->cl_addr = ip_addr;
494 copy_cred(&new->cl_cred,&rqstp->rq_cred);
497 add_to_unconfirmed(new, strhashval);
498 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
501 * confirmed found (name, principal match)
502 * confirmed verifier does not match input clverifier
504 * unconfirmed found (name match)
505 * confirmed->cl_confirm != unconfirmed->cl_confirm
507 * remove unconfirmed.
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
514 expire_client(unconf);
515 if (!(new = create_client(clname)))
517 copy_verf(new,&clverifier);
518 new->cl_addr = ip_addr;
519 copy_cred(&new->cl_cred,&rqstp->rq_cred);
522 add_to_unconfirmed(new, strhashval);
524 /* No cases hit !!! */
525 status = nfserr_inval;
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");
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.
545 * NOTE: callback information will be processed here in a future patch
548 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
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;
557 status = nfserr_stale_clientid;
558 if (STALE_CLIENTID(clid))
561 * XXX The Duplicate Request Cache (DRC) has been checked (??)
562 * We get here on a DRC miss.
565 idhashval = clientid_hashval(clid->cl_id);
567 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
568 if (!cmp_clid(&clp->cl_clientid, clid))
571 status = nfserr_inval;
573 * Found a record for this clientid. If the IP addresses
574 * don't match, return ERR_INVAL just as if the record had
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);
586 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
587 if (!cmp_clid(&clp->cl_clientid, clid))
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);
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
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;
613 move_to_confirmed(unconf, idhashval);
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.
623 if ((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;
635 * conf record not found.
636 * unconf record found.
637 * unconf->cl_confirm matches input confirm
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;
644 move_to_confirmed(unconf, idhashval);
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.
654 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
655 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
656 status = nfserr_stale_clientid;
659 /* check that we have hit one of the cases...*/
660 status = nfserr_inval;
663 /* XXX if status == nfs_ok, probe callback path */
669 * Open owner state (share locks)
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)
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)
682 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
683 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
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)
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)
699 static struct list_head file_hashtbl[FILE_HASH_SIZE];
700 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
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++;
715 return (struct nfs4_file *)NULL;
719 release_all_files(void)
722 struct nfs4_file *fp;
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);
736 static inline struct nfs4_stateowner *
737 alloc_stateowner(struct xdr_netobj *owner)
739 struct nfs4_stateowner *sop;
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;
749 return (struct nfs4_stateowner *)NULL;
752 /* should use a slab cache */
754 free_stateowner(struct nfs4_stateowner *sop) {
756 kfree(sop->so_owner.data);
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;
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);
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);
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;
791 rp->rp_buf = rp->rp_ibuf;
797 release_stateid_lockowner(struct nfs4_stateid *open_stp)
799 struct nfs4_stateowner *lock_sop;
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);
811 release_stateowner(struct nfs4_stateowner *sop)
813 struct nfs4_stateid *stp;
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);
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);
827 release_stateid(stp, LOCK_STATE);
829 free_stateowner(sop);
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);
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);
843 list_add(&stp->st_perfile, &fp->fi_perfile);
844 stp->st_stateowner = sop;
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);
857 release_stateid(struct nfs4_stateid *stp, int flags) {
859 list_del(&stp->st_hash);
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);
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;
872 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
879 release_file(struct nfs4_file *fp)
882 list_del(&fp->fi_hash);
888 move_to_close_lru(struct nfs4_stateowner *sop)
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);
896 list_add_tail(&sop->so_close_lru, &close_lru);
897 sop->so_time = get_seconds();
901 release_state_owner(struct nfs4_stateid *stp, struct nfs4_stateowner **sopp,
904 struct nfs4_stateowner *sop = stp->st_stateowner;
905 struct nfs4_file *fp = stp->st_file;
907 dprintk("NFSD: release_state_owner\n");
908 release_stateid(stp, flag);
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
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)) {
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));
929 /* search ownerstr_hashtbl[] for owner */
931 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, struct nfs4_stateowner **op) {
932 struct nfs4_stateowner *local = NULL;
934 list_for_each_entry(local, &ownerstr_hashtbl[hashval], so_strhash) {
935 if(!cmp_owner_str(local, &open->op_owner, &open->op_clientid))
943 /* see if clientid is in confirmed hash table */
945 verify_clientid(struct nfs4_client **client, clientid_t *clid) {
947 struct nfs4_client *clp;
948 unsigned int idhashval = clientid_hashval(clid->cl_id);
950 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
951 if (!cmp_clid(&clp->cl_clientid, clid))
960 /* search file_hashtbl[] for file */
962 find_file(unsigned int hashval, struct inode *ino, struct nfs4_file **fp) {
963 struct nfs4_file *local = NULL;
965 list_for_each_entry(local, &file_hashtbl[hashval], fi_hash) {
966 if (local->fi_inode == ino) {
974 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
975 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
978 set_access(unsigned int *access, unsigned long bmap) {
982 for (i = 1; i < 4; i++) {
983 if(test_bit(i, &bmap))
989 set_deny(unsigned int *deny, unsigned long bmap) {
993 for (i = 0; i < 4; i++) {
994 if(test_bit(i, &bmap))
1000 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1001 unsigned int access, deny;
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))
1011 * Called to check deny when READ with all zero stateid or
1012 * WRITE with all zero or all one stateid
1015 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
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;
1022 dprintk("NFSD: nfs4_share_conflict\n");
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;
1037 nfs4_file_upgrade(struct file *filp, unsigned int share_access)
1041 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1042 status = get_write_access(filp->f_dentry->d_inode);
1044 filp->f_mode = FMODE_WRITE;
1046 return nfserrno(status);
1052 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1054 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1055 put_write_access(filp->f_dentry->d_inode);
1056 filp->f_mode = FMODE_READ;
1062 * nfsd4_process_open1()
1063 * lookup stateowner.
1075 * called with nfs4_lock_state() held.
1078 nfsd4_process_open1(struct nfsd4_open *open)
1081 clientid_t *clientid = &open->op_clientid;
1082 struct nfs4_client *clp = NULL;
1083 unsigned int strhashval;
1084 struct nfs4_stateowner *sop = NULL;
1086 status = nfserr_inval;
1087 if (!check_name(open->op_owner))
1090 status = nfserr_stale_clientid;
1091 if (STALE_CLIENTID(&open->op_clientid))
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) {
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
1107 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1111 /* replay: indicate to calling function */
1112 status = NFSERR_REPLAY_ME;
1115 if (sop->so_confirmed) {
1116 if (open->op_seqid == sop->so_seqid + 1) {
1120 status = nfserr_bad_seqid;
1123 /* If we get here, we received and OPEN for an unconfirmed
1125 * Since the sequid's are different, purge the
1126 * existing nfs4_stateowner, and instantiate a new one.
1128 clp = sop->so_client;
1129 release_stateowner(sop);
1130 goto instantiate_new_owner;
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.
1137 * XXX compare clp->cl_addr with rqstp addr?
1139 status = nfserr_expired;
1140 if (!verify_clientid(&clp, clientid))
1142 instantiate_new_owner:
1143 status = nfserr_resource;
1144 if (!(sop = alloc_init_open_stateowner(strhashval, clp, open)))
1146 open->op_stateowner = sop;
1149 renew_client(sop->so_client);
1151 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1152 status = nfserr_reclaim_bad;
1156 * called with nfs4_lock_state() held.
1159 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1162 struct nfs4_stateowner *sop = open->op_stateowner;
1163 struct nfs4_file *fp = NULL;
1165 unsigned int fi_hashval;
1166 struct nfs4_stateid *stq, *stp = NULL;
1169 status = nfserr_resource;
1173 ino = current_fh->fh_dentry->d_inode;
1175 status = nfserr_inval;
1176 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
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) {
1188 /* ignore lock owners */
1189 if (stq->st_stateowner->so_is_open_owner == 0)
1191 if (!test_share(stq,open))
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)
1204 status = nfserr_resource;
1205 if ((stp = kmalloc(sizeof(struct nfs4_stateid),
1206 GFP_KERNEL)) == NULL)
1209 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1213 if ((status = nfsd_open(rqstp, current_fh, S_IFREG,
1215 &stp->st_vfs_file)) != 0)
1219 dget(stp->st_vfs_file.f_dentry);
1220 mntget(stp->st_vfs_file.f_vfsmnt);
1222 init_stateid(stp, fp, sop, open);
1223 stp->st_vfs_set = 1;
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;
1230 set_access(&share_access, stp->st_access_bmap);
1231 share_access = ~share_access;
1232 share_access &= open->op_share_access;
1234 /* update the struct file */
1235 if ((status = nfs4_file_upgrade(&stp->st_vfs_file, share_access)))
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);
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);
1247 if (open->op_truncate) {
1248 iattr.ia_valid = ATTR_SIZE;
1250 status = nfsd_setattr(rqstp, current_fh, &iattr, 0, (time_t)0);
1254 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1256 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
1259 if (fp && list_empty(&fp->fi_perfile))
1262 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1264 status = nfserr_reclaim_bad;
1266 /* successful reclaim. so_seqid is decremented because
1267 * it will be bumped in encode_open
1269 open->op_stateowner->so_confirmed = 1;
1270 open->op_stateowner->so_seqid--;
1274 * To finish the open response, we just need to set the rflags.
1276 open->op_rflags = 0;
1277 if (!open->op_stateowner->so_confirmed)
1278 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1286 static struct work_struct laundromat_work;
1287 static void laundromat_main(void *);
1288 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1291 nfsd4_renew(clientid_t *clid)
1293 struct nfs4_client *clp;
1294 unsigned int idhashval;
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))
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))
1311 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
1312 if (!cmp_clid(&clp->cl_clientid, clid))
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.
1322 dprintk("nfsd4_renew: clientid not found!\n");
1323 status = nfserr_expired;
1325 nfs4_unlock_state();
1330 nfs4_laundromat(void)
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;
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)
1350 dprintk("NFSD: purging unused client (clientid %08x)\n",
1351 clp->cl_clientid.cl_id);
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;
1362 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1364 release_stateowner(sop);
1366 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1367 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1368 nfs4_unlock_state();
1369 return clientid_val;
1373 laundromat_main(void *not_used)
1377 t = nfs4_laundromat();
1378 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1379 schedule_delayed_work(&laundromat_work, t*HZ);
1382 /* search ownerid_hashtbl[] and close_lru for stateid owner
1383 * (stateid->si_stateownerid)
1385 struct nfs4_stateowner *
1386 find_openstateowner_id(u32 st_id, int flags) {
1387 struct nfs4_stateowner *local = NULL;
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)
1400 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1402 return (stp->st_vfs_set == 0 ||
1403 fhp->fh_dentry->d_inode != stp->st_vfs_file.f_dentry->d_inode);
1407 STALE_STATEID(stateid_t *stateid)
1409 if (stateid->si_boot == boot_time)
1411 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1412 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1413 stateid->si_generation);
1419 * Checks for stateid operations
1422 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct nfs4_stateid **stpp)
1424 struct nfs4_stateid *stp;
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);
1434 status = nfserr_stale_stateid;
1435 if (STALE_STATEID(stateid))
1439 status = nfserr_bad_stateid;
1440 if (!(stp = find_stateid(stateid, flags))) {
1441 dprintk("NFSD: preprocess_stateid_op: no open stateid!\n");
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;
1449 if (!stp->st_stateowner->so_confirmed) {
1450 dprintk("preprocess_stateid_op: lockowner not confirmed yet!\n");
1453 if (stateid->si_generation > stp->st_stateid.si_generation) {
1454 dprintk("preprocess_stateid_op: future stateid?!\n");
1459 status = nfserr_old_stateid;
1460 if (stateid->si_generation < stp->st_stateid.si_generation) {
1461 dprintk("preprocess_stateid_op: old stateid!\n");
1466 renew_client(stp->st_stateowner->so_client);
1473 * Checks for sequence id mutating operations.
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)
1479 struct nfs4_stateid *stp;
1480 struct nfs4_stateowner *sop;
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);
1490 status = nfserr_bad_stateid;
1491 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
1492 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
1496 status = nfserr_stale_stateid;
1497 if (STALE_STATEID(stateid))
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.
1508 if (!(stp = find_stateid(stateid, flags)))
1509 goto no_nfs4_stateid;
1511 status = nfserr_bad_stateid;
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
1519 struct nfs4_stateowner *sop = stp->st_stateowner;
1520 struct nfs4_client *clp = sop->so_client;
1522 if (!sop->so_is_open_owner)
1524 if (!cmp_clid(&clp->cl_clientid, lockclid))
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;
1535 *sopp = sop = stp->st_stateowner;
1538 * We now validate the seqid and stateid generation numbers.
1539 * For the moment, we ignore the possibility of
1540 * generation number wraparound.
1542 if (seqid != sop->so_seqid + 1)
1545 if (sop->so_confirmed) {
1546 if (flags & CONFIRM) {
1547 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
1552 if (!(flags & CONFIRM)) {
1553 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
1557 if (stateid->si_generation > stp->st_stateid.si_generation) {
1558 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
1562 status = nfserr_old_stateid;
1563 if (stateid->si_generation < stp->st_stateid.si_generation) {
1564 printk("NFSD: preprocess_seqid_op: old stateid!\n");
1567 /* XXX renew the client lease here */
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.
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;
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;
1593 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
1596 status = nfserr_bad_seqid;
1602 * nfs4_unlock_state(); called in encode
1605 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
1608 struct nfs4_stateowner *sop;
1609 struct nfs4_stateid *stp;
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);
1615 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
1618 oc->oc_stateowner = NULL;
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)))
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);
1644 * unset all bits in union bitmap (bmap) that
1645 * do not exist in share (from successful OPEN_DOWNGRADE)
1648 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
1651 for (i = 1; i < 4; i++) {
1652 if ((i & access) != i)
1653 __clear_bit(i, bmap);
1658 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
1661 for (i = 0; i < 4; i++) {
1662 if ((i & deny) != i)
1663 __clear_bit(i, bmap);
1668 * nfs4_unlock_state(); called in encode
1672 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
1675 struct nfs4_stateid *stp;
1676 unsigned int share_access;
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);
1682 od->od_stateowner = NULL;
1683 status = nfserr_inval;
1684 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
1688 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
1690 CHECK_FH | OPEN_STATE,
1691 &od->od_stateowner, &stp, NULL)))
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);
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);
1705 set_access(&share_access, stp->st_access_bmap);
1706 nfs4_file_downgrade(&stp->st_vfs_file,
1707 share_access & ~od->od_share_access);
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);
1712 update_stateid(&stp->st_stateid);
1713 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
1720 * nfs4_unlock_state() called after encode
1723 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
1726 struct nfs4_stateid *stp;
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);
1732 close->cl_stateowner = NULL;
1734 /* check close_lru for replay */
1735 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
1737 CHECK_FH | OPEN_STATE | CLOSE_STATE,
1738 &close->cl_stateowner, &stp, NULL)))
1741 * Return success, but first update the stateid.
1744 update_stateid(&stp->st_stateid);
1745 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
1747 /* release_state_owner() calls nfsd_close() if needed */
1748 release_state_owner(stp, &close->cl_stateowner, OPEN_STATE);
1754 * Lock owner state (byte-range locks)
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)
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)
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];
1770 struct nfs4_stateid *
1771 find_stateid(stateid_t *stid, int flags)
1773 struct nfs4_stateid *local = NULL;
1774 u32 st_id = stid->si_stateownerid;
1775 u32 f_id = stid->si_fileid;
1776 unsigned int hashval;
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))
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))
1795 printk("NFSD: find_stateid: ERROR: no state flag\n");
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!
1809 nfs4_transform_lock_offset(struct file_lock *lock)
1811 if (lock->fl_start < 0)
1812 lock->fl_start = OFFSET_MAX;
1813 if (lock->fl_end < 0)
1814 lock->fl_end = OFFSET_MAX;
1818 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
1820 struct nfs4_stateowner *local = NULL;
1823 if (hashval >= LOCK_HASH_SIZE)
1825 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
1837 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
1839 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
1841 deny->ld_sop = NULL;
1842 if (nfs4_verify_lock_stateowner(sop, fl->fl_pid))
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;
1855 find_lockstateowner_str(unsigned int hashval, struct xdr_netobj *owner, clientid_t *clid, struct nfs4_stateowner **op) {
1856 struct nfs4_stateowner *local = NULL;
1858 list_for_each_entry(local, &lock_ownerstr_hashtbl[hashval], so_strhash) {
1859 if(!cmp_owner_str(local, owner, clid))
1869 * Alloc a lock owner structure.
1870 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
1873 * strhashval = lock_ownerstr_hashval
1874 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
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;
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 */
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);
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;
1906 rp->rp_buf = rp->rp_ibuf;
1911 struct nfs4_stateid *
1912 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
1914 struct nfs4_stateid *stp;
1915 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1917 if ((stp = kmalloc(sizeof(struct nfs4_stateid),
1918 GFP_KERNEL)) == NULL)
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);
1927 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1928 stp->st_stateowner = sop;
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;
1944 check_lock_length(u64 offset, u64 length)
1946 return ((length == 0) || ((length != ~(u64)0) &&
1947 LOFF_OVERFLOW(offset, length)));
1953 * nfs4_unlock_state(); called in encode
1956 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
1958 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
1959 struct nfs4_stateid *lock_stp;
1961 struct file_lock file_lock;
1962 struct file_lock *conflock;
1964 unsigned int strhashval;
1966 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
1967 (long long) lock->lk_offset,
1968 (long long) lock->lk_length);
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;
1975 if (check_lock_length(lock->lk_offset, lock->lk_length))
1976 return nfserr_inval;
1978 lock->lk_stateowner = NULL;
1981 if (lock->lk_is_new) {
1983 * Client indicates that this is a new lockowner.
1984 * Use open owner and open stateid to create lock owner and lock
1987 struct nfs4_stateid *open_stp = NULL;
1988 struct nfs4_file *fp;
1990 status = nfserr_stale_clientid;
1991 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
1992 printk("NFSD: nfsd4_lock: clientid is stale!\n");
1995 /* does the clientid in the lock owner own the open stateid? */
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);
2005 if (lock->lk_reclaim)
2006 status = nfserr_reclaim_bad;
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,
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'.
2020 status = nfserr_bad_stateid;
2021 if (find_lockstateowner_str(strhashval, &lock->v.new.owner,
2022 &lock->v.new.clientid, &lock_sop))
2024 status = nfserr_resource;
2025 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2027 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2028 fp, open_stp)) == NULL)
2030 /* bump the open seqid used to create the lock */
2031 open_sop->so_seqid++;
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);
2042 /* lock->lk_stateowner and lock_stp have been created or found */
2043 filp = &lock_stp->st_vfs_file;
2045 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2046 printk("NFSD: nfsd4_lock: permission denied!\n");
2050 switch (lock->lk_type) {
2053 file_lock.fl_type = F_RDLCK;
2056 case NFS4_WRITEW_LT:
2057 file_lock.fl_type = F_WRLCK;
2060 status = nfserr_inval;
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;
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;
2076 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2077 nfs4_transform_lock_offset(&file_lock);
2080 * Try to lock the file in the VFS.
2081 * Note: locks.c uses the BKL to protect the inode's lock list.
2084 status = posix_lock_file(filp, &file_lock);
2085 dprintk("NFSD: nfsd4_lock: posix_test_lock passed. posix_lock_file status %d\n",status);
2087 case 0: /* success! */
2088 update_stateid(&lock_stp->st_stateid);
2089 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2093 goto conflicting_lock;
2095 status = nfserr_deadlock;
2097 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2098 goto out_destroy_new_stateid;
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
2107 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2108 status = nfserr_serverfault;
2111 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2113 out_destroy_new_stateid:
2114 if (lock->lk_is_new) {
2115 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2117 * An error encountered after instantiation of the new
2118 * stateid has forced us to destroy it.
2120 if (!seqid_mutating_err(status))
2121 open_sop->so_seqid--;
2123 release_state_owner(lock_stp, &lock->lk_stateowner, LOCK_STATE);
2133 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2135 struct inode *inode;
2136 struct nfs4_stateowner *sop;
2138 struct file_lock file_lock;
2139 struct file_lock *conflicting_lock;
2140 unsigned int strhashval;
2143 if (nfs4_in_grace())
2144 return nfserr_grace;
2146 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2147 return nfserr_inval;
2149 lockt->lt_stateowner = NULL;
2152 status = nfserr_stale_clientid;
2153 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2154 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
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;
2165 inode = current_fh->fh_dentry->d_inode;
2166 switch (lockt->lt_type) {
2169 file_lock.fl_type = F_RDLCK;
2172 case NFS4_WRITEW_LT:
2173 file_lock.fl_type = F_WRLCK;
2176 printk("NFSD: nfs4_lockt: bad lock type!\n");
2177 status = nfserr_inval;
2181 strhashval = lock_ownerstr_hashval(inode,
2182 lockt->lt_clientid.cl_id, lockt->lt_owner);
2184 find_lockstateowner_str(strhashval, &lockt->lt_owner,
2185 &lockt->lt_clientid,
2186 &lockt->lt_stateowner);
2187 sop = lockt->lt_stateowner;
2189 file_lock.fl_owner = (fl_owner_t) sop;
2190 file_lock.fl_pid = lockownerid_hashval(sop->so_id);
2192 file_lock.fl_owner = NULL;
2193 file_lock.fl_pid = 0;
2195 file_lock.fl_flags = FL_POSIX;
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;
2201 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2203 nfs4_transform_lock_offset(&file_lock);
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.
2210 memset(&file, 0, sizeof (struct file));
2211 file.f_dentry = current_fh->fh_dentry;
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);
2220 nfs4_unlock_state();
2225 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2227 struct nfs4_stateid *stp;
2228 struct file *filp = NULL;
2229 struct file_lock file_lock;
2232 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2233 (long long) locku->lu_offset,
2234 (long long) locku->lu_length);
2236 if (check_lock_length(locku->lu_offset, locku->lu_length))
2237 return nfserr_inval;
2239 locku->lu_stateowner = NULL;
2242 if ((status = nfs4_preprocess_seqid_op(current_fh,
2245 CHECK_FH | LOCK_STATE,
2246 &locku->lu_stateowner, &stp, NULL)))
2249 filp = &stp->st_vfs_file;
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;
2261 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2262 file_lock.fl_end = ~(u64)0;
2264 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2265 nfs4_transform_lock_offset(&file_lock);
2268 * Try to unlock the file in the VFS.
2270 status = posix_lock_file(filp, &file_lock);
2272 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2276 * OK, unlock succeeded; the only thing left to do is update the stateid.
2278 update_stateid(&stp->st_stateid);
2279 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2285 status = nfserrno(status);
2291 * 1: locks held by lockowner
2292 * 0: no locks held by lockowner
2295 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2297 struct file_lock **flpp;
2298 struct inode *inode = filp->f_dentry->d_inode;
2302 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2303 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
2313 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2315 clientid_t *clid = &rlockowner->rl_clientid;
2316 struct nfs4_stateowner *local = NULL;
2317 struct xdr_netobj *owner = &rlockowner->rl_owner;
2320 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2321 clid->cl_boot, clid->cl_id);
2323 /* XXX check for lease expiration */
2325 status = nfserr_stale_clientid;
2326 if (STALE_CLIENTID(clid)) {
2327 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
2333 /* find the lockowner */
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;
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,
2345 if(stp->st_vfs_set) {
2346 if (check_for_locks(&stp->st_vfs_file,
2351 /* no locks held by (lock) stateowner */
2353 release_stateowner(local);
2357 nfs4_unlock_state();
2362 * Start and stop routines
2366 nfs4_state_init(void)
2369 time_t start = get_seconds();
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]);
2379 for (i = 0; i < FILE_HASH_SIZE; i++) {
2380 INIT_LIST_HEAD(&file_hashtbl[i]);
2382 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2383 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
2384 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
2386 for (i = 0; i < STATEID_HASH_SIZE; i++) {
2387 INIT_LIST_HEAD(&stateid_hashtbl[i]);
2388 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
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]);
2394 memset(&zerostateid, 0, sizeof(stateid_t));
2395 memset(&onestateid, ~0, sizeof(stateid_t));
2397 INIT_LIST_HEAD(&close_lru);
2398 INIT_LIST_HEAD(&client_lru);
2399 init_MUTEX(&client_sema);
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);
2411 return time_before(get_seconds(), (unsigned long)grace_end);
2415 nfs4_in_no_grace(void)
2417 return (grace_end < get_seconds());
2422 __nfs4_state_shutdown(void)
2425 struct nfs4_client *clp = NULL;
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);
2432 while (!list_empty(&unconf_str_hashtbl[i])) {
2433 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
2437 release_all_files();
2438 cancel_delayed_work(&laundromat_work);
2439 flush_scheduled_work();
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",
2454 nfs4_state_shutdown(void)
2457 __nfs4_state_shutdown();
2458 nfs4_unlock_state();