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/kthread.h>
48 #include <linux/nfs4.h>
49 #include <linux/nfsd/state.h>
50 #include <linux/nfsd/xdr4.h>
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 static time_t lease_time = 90; /* default lease time */
56 static time_t old_lease_time = 90; /* past incarnation lease time */
57 static u32 nfs4_reclaim_init = 0;
59 static time_t grace_end = 0;
60 static u32 current_clientid = 1;
61 static u32 current_ownerid = 1;
62 static u32 current_fileid = 1;
63 static u32 current_delegid = 1;
65 stateid_t zerostateid; /* bits all 0 */
66 stateid_t onestateid; /* bits all 1 */
69 u32 list_add_perfile = 0;
70 u32 list_del_perfile = 0;
71 u32 add_perclient = 0;
72 u32 del_perclient = 0;
76 /* forward declarations */
77 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
78 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
79 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
84 * protects clientid_hashtbl[], clientstr_hashtbl[],
85 * unconfstr_hashtbl[], uncofid_hashtbl[].
87 static DECLARE_MUTEX(client_sema);
89 kmem_cache_t *stateowner_slab = NULL;
90 kmem_cache_t *file_slab = NULL;
91 kmem_cache_t *stateid_slab = NULL;
92 kmem_cache_t *deleg_slab = NULL;
101 nfs4_unlock_state(void)
107 opaque_hashval(const void *ptr, int nbytes)
109 unsigned char *cptr = (unsigned char *) ptr;
119 /* forward declarations */
120 static void release_stateowner(struct nfs4_stateowner *sop);
121 static void release_stateid(struct nfs4_stateid *stp, int flags);
122 static void release_file(struct nfs4_file *fp);
128 /* recall_lock protects the del_recall_lru */
129 spinlock_t recall_lock;
130 static struct list_head del_recall_lru;
132 static struct nfs4_delegation *
133 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
135 struct nfs4_delegation *dp;
136 struct nfs4_file *fp = stp->st_file;
137 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
139 dprintk("NFSD alloc_init_deleg\n");
140 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
143 INIT_LIST_HEAD(&dp->dl_del_perfile);
144 INIT_LIST_HEAD(&dp->dl_del_perclnt);
145 INIT_LIST_HEAD(&dp->dl_recall_lru);
149 get_file(stp->st_vfs_file);
150 dp->dl_vfs_file = stp->st_vfs_file;
152 dp->dl_recall.cbr_dp = NULL;
153 dp->dl_recall.cbr_ident = cb->cb_ident;
154 dp->dl_recall.cbr_trunc = 0;
155 dp->dl_stateid.si_boot = boot_time;
156 dp->dl_stateid.si_stateownerid = current_delegid++;
157 dp->dl_stateid.si_fileid = 0;
158 dp->dl_stateid.si_generation = 0;
159 dp->dl_fhlen = current_fh->fh_handle.fh_size;
160 memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base,
161 current_fh->fh_handle.fh_size);
163 atomic_set(&dp->dl_count, 1);
164 list_add(&dp->dl_del_perfile, &fp->fi_del_perfile);
165 list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
170 nfs4_put_delegation(struct nfs4_delegation *dp)
172 if (atomic_dec_and_test(&dp->dl_count)) {
173 dprintk("NFSD: freeing dp %p\n",dp);
174 kmem_cache_free(deleg_slab, dp);
178 /* Remove the associated file_lock first, then remove the delegation.
179 * lease_modify() is called to remove the FS_LEASE file_lock from
180 * the i_flock list, eventually calling nfsd's lock_manager
181 * fl_release_callback.
184 nfs4_close_delegation(struct nfs4_delegation *dp)
186 struct file *filp = dp->dl_vfs_file;
188 dprintk("NFSD: close_delegation dp %p\n",dp);
189 dp->dl_vfs_file = NULL;
190 /* The following nfsd_close may not actually close the file,
191 * but we want to remove the lease in any case. */
193 setlease(filp, F_UNLCK, &dp->dl_flock);
198 /* Called under the state lock. */
200 unhash_delegation(struct nfs4_delegation *dp)
202 list_del_init(&dp->dl_del_perfile);
203 list_del_init(&dp->dl_del_perclnt);
204 spin_lock(&recall_lock);
205 list_del_init(&dp->dl_recall_lru);
206 spin_unlock(&recall_lock);
207 nfs4_close_delegation(dp);
208 nfs4_put_delegation(dp);
215 /* Hash tables for nfs4_clientid state */
216 #define CLIENT_HASH_BITS 4
217 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
218 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
220 #define clientid_hashval(id) \
221 ((id) & CLIENT_HASH_MASK)
222 #define clientstr_hashval(name, namelen) \
223 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
225 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
226 * used in reboot/reset lease grace period processing
228 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
229 * setclientid_confirmed info.
231 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
234 * client_lru holds client queue ordered by nfs4_client.cl_time
237 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
238 * for last close replay.
240 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
241 static int reclaim_str_hashtbl_size = 0;
242 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
243 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
244 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
245 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
246 static struct list_head client_lru;
247 static struct list_head close_lru;
250 renew_client(struct nfs4_client *clp)
253 * Move client to the end to the LRU list.
255 dprintk("renewing client (clientid %08x/%08x)\n",
256 clp->cl_clientid.cl_boot,
257 clp->cl_clientid.cl_id);
258 list_move_tail(&clp->cl_lru, &client_lru);
259 clp->cl_time = get_seconds();
262 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
264 STALE_CLIENTID(clientid_t *clid)
266 if (clid->cl_boot == boot_time)
268 dprintk("NFSD stale clientid (%08x/%08x)\n",
269 clid->cl_boot, clid->cl_id);
274 * XXX Should we use a slab cache ?
275 * This type of memory management is somewhat inefficient, but we use it
276 * anyway since SETCLIENTID is not a common operation.
278 static inline struct nfs4_client *
279 alloc_client(struct xdr_netobj name)
281 struct nfs4_client *clp;
283 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
284 memset(clp, 0, sizeof(*clp));
285 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
286 memcpy(clp->cl_name.data, name.data, name.len);
287 clp->cl_name.len = name.len;
298 free_client(struct nfs4_client *clp)
300 if (clp->cl_cred.cr_group_info)
301 put_group_info(clp->cl_cred.cr_group_info);
302 kfree(clp->cl_name.data);
307 put_nfs4_client(struct nfs4_client *clp)
309 if (atomic_dec_and_test(&clp->cl_count))
314 expire_client(struct nfs4_client *clp)
316 struct nfs4_stateowner *sop;
317 struct nfs4_delegation *dp;
318 struct nfs4_callback *cb = &clp->cl_callback;
319 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
320 struct list_head reaplist;
322 dprintk("NFSD: expire_client cl_count %d\n",
323 atomic_read(&clp->cl_count));
325 /* shutdown rpc client, ending any outstanding recall rpcs */
326 if (atomic_read(&cb->cb_set) == 1 && clnt) {
327 rpc_shutdown_client(clnt);
328 clnt = clp->cl_callback.cb_client = NULL;
331 INIT_LIST_HEAD(&reaplist);
332 spin_lock(&recall_lock);
333 while (!list_empty(&clp->cl_del_perclnt)) {
334 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
335 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
337 list_del_init(&dp->dl_del_perclnt);
338 list_move(&dp->dl_recall_lru, &reaplist);
340 spin_unlock(&recall_lock);
341 while (!list_empty(&reaplist)) {
342 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
343 list_del_init(&dp->dl_recall_lru);
344 unhash_delegation(dp);
346 list_del(&clp->cl_idhash);
347 list_del(&clp->cl_strhash);
348 list_del(&clp->cl_lru);
349 while (!list_empty(&clp->cl_perclient)) {
350 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
351 release_stateowner(sop);
353 put_nfs4_client(clp);
356 static struct nfs4_client *
357 create_client(struct xdr_netobj name) {
358 struct nfs4_client *clp;
360 if (!(clp = alloc_client(name)))
362 atomic_set(&clp->cl_count, 1);
363 atomic_set(&clp->cl_callback.cb_set, 0);
364 clp->cl_callback.cb_parsed = 0;
365 INIT_LIST_HEAD(&clp->cl_idhash);
366 INIT_LIST_HEAD(&clp->cl_strhash);
367 INIT_LIST_HEAD(&clp->cl_perclient);
368 INIT_LIST_HEAD(&clp->cl_del_perclnt);
369 INIT_LIST_HEAD(&clp->cl_lru);
375 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
376 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
380 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
381 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
382 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
386 copy_cred(struct svc_cred *target, struct svc_cred *source) {
388 target->cr_uid = source->cr_uid;
389 target->cr_gid = source->cr_gid;
390 target->cr_group_info = source->cr_group_info;
391 get_group_info(target->cr_group_info);
395 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
398 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
402 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
403 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
407 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
408 return((cl1->cl_boot == cl2->cl_boot) &&
409 (cl1->cl_id == cl2->cl_id));
412 /* XXX what about NGROUP */
414 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
415 return(cr1->cr_uid == cr2->cr_uid);
420 gen_clid(struct nfs4_client *clp) {
421 clp->cl_clientid.cl_boot = boot_time;
422 clp->cl_clientid.cl_id = current_clientid++;
426 gen_confirm(struct nfs4_client *clp) {
431 p = (u32 *)clp->cl_confirm.data;
437 check_name(struct xdr_netobj name) {
441 if (name.len > NFS4_OPAQUE_LIMIT) {
442 printk("NFSD: check_name: name too long(%d)!\n", name.len);
449 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
451 unsigned int idhashval;
453 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
454 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
455 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
456 list_add_tail(&clp->cl_lru, &client_lru);
457 clp->cl_time = get_seconds();
461 move_to_confirmed(struct nfs4_client *clp)
463 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
464 unsigned int strhashval;
466 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
467 list_del_init(&clp->cl_strhash);
468 list_del_init(&clp->cl_idhash);
469 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
470 strhashval = clientstr_hashval(clp->cl_name.data,
472 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
476 static struct nfs4_client *
477 find_confirmed_client(clientid_t *clid)
479 struct nfs4_client *clp;
480 unsigned int idhashval = clientid_hashval(clid->cl_id);
482 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
483 if (cmp_clid(&clp->cl_clientid, clid))
489 static struct nfs4_client *
490 find_unconfirmed_client(clientid_t *clid)
492 struct nfs4_client *clp;
493 unsigned int idhashval = clientid_hashval(clid->cl_id);
495 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
496 if (cmp_clid(&clp->cl_clientid, clid))
502 /* a helper function for parse_callback */
504 parse_octet(unsigned int *lenp, char **addrp)
506 unsigned int len = *lenp;
518 if ((c < '0') || (c > '9')) {
524 n = (n * 10) + (c - '0');
535 /* parse and set the setclientid ipv4 callback address */
537 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
542 u32 addrlen = addr_len;
543 char *addr = addr_val;
548 for(i = 4; i > 0 ; i--) {
549 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
552 cbaddr |= (temp << shift);
560 for(i = 2; i > 0 ; i--) {
561 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
564 cbport |= (temp << shift);
573 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
575 struct nfs4_callback *cb = &clp->cl_callback;
577 /* Currently, we only support tcp for the callback channel */
578 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
581 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
582 &cb->cb_addr, &cb->cb_port)))
584 cb->cb_prog = se->se_callback_prog;
585 cb->cb_ident = se->se_callback_ident;
589 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
590 "will not receive delegations\n",
591 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
598 * RFC 3010 has a complex implmentation description of processing a
599 * SETCLIENTID request consisting of 5 bullets, labeled as
600 * CASE0 - CASE4 below.
603 * callback information will be processed in a future patch
605 * an unconfirmed record is added when:
606 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
607 * CASE 1: confirmed record found with matching name, principal,
608 * verifier, and clientid.
609 * CASE 2: confirmed record found with matching name, principal,
610 * and there is no unconfirmed record with matching
613 * an unconfirmed record is replaced when:
614 * CASE 3: confirmed record found with matching name, principal,
615 * and an unconfirmed record is found with matching
616 * name, principal, and with clientid and
617 * confirm that does not match the confirmed record.
618 * CASE 4: there is no confirmed record with matching name and
619 * principal. there is an unconfirmed record with
620 * matching name, principal.
622 * an unconfirmed record is deleted when:
623 * CASE 1: an unconfirmed record that matches input name, verifier,
624 * and confirmed clientid.
625 * CASE 4: any unconfirmed records with matching name and principal
626 * that exist after an unconfirmed record has been replaced
627 * as described above.
631 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
633 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
634 struct xdr_netobj clname = {
635 .len = setclid->se_namelen,
636 .data = setclid->se_name,
638 nfs4_verifier clverifier = setclid->se_verf;
639 unsigned int strhashval;
640 struct nfs4_client * conf, * unconf, * new, * clp;
643 status = nfserr_inval;
644 if (!check_name(clname))
648 * XXX The Duplicate Request Cache (DRC) has been checked (??)
649 * We get here on a DRC miss.
652 strhashval = clientstr_hashval(clname.data, clname.len);
656 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
657 if (!cmp_name(&clp->cl_name, &clname))
661 * clname match, confirmed, different principal
662 * or different ip_address
664 status = nfserr_clid_inuse;
665 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
666 printk("NFSD: setclientid: string in use by client"
667 "(clientid %08x/%08x)\n",
668 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
671 if (clp->cl_addr != ip_addr) {
672 printk("NFSD: setclientid: string in use by client"
673 "(clientid %08x/%08x)\n",
674 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
679 * cl_name match from a previous SETCLIENTID operation
680 * XXX check for additional matches?
686 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
687 if (!cmp_name(&clp->cl_name, &clname))
689 /* cl_name match from a previous SETCLIENTID operation */
693 status = nfserr_resource;
697 * placed first, because it is the normal case.
700 expire_client(unconf);
701 if (!(new = create_client(clname)))
703 copy_verf(new, &clverifier);
704 new->cl_addr = ip_addr;
705 copy_cred(&new->cl_cred,&rqstp->rq_cred);
708 gen_callback(new, setclid);
709 add_to_unconfirmed(new, strhashval);
710 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
713 * cl_name match, confirmed, principal match
714 * verifier match: probable callback update
716 * remove any unconfirmed nfs4_client with
717 * matching cl_name, cl_verifier, and cl_clientid
719 * create and insert an unconfirmed nfs4_client with same
720 * cl_name, cl_verifier, and cl_clientid as existing
721 * nfs4_client, but with the new callback info and a
725 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
726 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
727 expire_client(unconf);
729 if (!(new = create_client(clname)))
731 copy_verf(new,&conf->cl_verifier);
732 new->cl_addr = ip_addr;
733 copy_cred(&new->cl_cred,&rqstp->rq_cred);
734 copy_clid(new, conf);
736 gen_callback(new, setclid);
737 add_to_unconfirmed(new,strhashval);
738 } else if (!unconf) {
741 * clname match, confirmed, principal match
742 * verfier does not match
743 * no unconfirmed. create a new unconfirmed nfs4_client
744 * using input clverifier, clname, and callback info
745 * and generate a new cl_clientid and cl_confirm.
747 if (!(new = create_client(clname)))
749 copy_verf(new,&clverifier);
750 new->cl_addr = ip_addr;
751 copy_cred(&new->cl_cred,&rqstp->rq_cred);
754 gen_callback(new, setclid);
755 add_to_unconfirmed(new, strhashval);
756 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
759 * confirmed found (name, principal match)
760 * confirmed verifier does not match input clverifier
762 * unconfirmed found (name match)
763 * confirmed->cl_confirm != unconfirmed->cl_confirm
765 * remove unconfirmed.
767 * create an unconfirmed nfs4_client
768 * with same cl_name as existing confirmed nfs4_client,
769 * but with new callback info, new cl_clientid,
770 * new cl_verifier and a new cl_confirm
772 expire_client(unconf);
773 if (!(new = create_client(clname)))
775 copy_verf(new,&clverifier);
776 new->cl_addr = ip_addr;
777 copy_cred(&new->cl_cred,&rqstp->rq_cred);
780 gen_callback(new, setclid);
781 add_to_unconfirmed(new, strhashval);
783 /* No cases hit !!! */
784 status = nfserr_inval;
788 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
789 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
790 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
799 * RFC 3010 has a complex implmentation description of processing a
800 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
801 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
803 * NOTE: callback information will be processed here in a future patch
806 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
808 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
809 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
810 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
811 clientid_t * clid = &setclientid_confirm->sc_clientid;
814 if (STALE_CLIENTID(clid))
815 return nfserr_stale_clientid;
817 * XXX The Duplicate Request Cache (DRC) has been checked (??)
818 * We get here on a DRC miss.
822 clp = find_confirmed_client(clid);
824 status = nfserr_inval;
826 * Found a record for this clientid. If the IP addresses
827 * don't match, return ERR_INVAL just as if the record had
830 if (clp->cl_addr != ip_addr) {
831 printk("NFSD: setclientid: string in use by client"
832 "(clientid %08x/%08x)\n",
833 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
838 clp = find_unconfirmed_client(clid);
840 status = nfserr_inval;
841 if (clp->cl_addr != ip_addr) {
842 printk("NFSD: setclientid: string in use by client"
843 "(clientid %08x/%08x)\n",
844 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
850 * unconf record that matches input clientid and input confirm.
851 * conf record that matches input clientid.
852 * conf and unconf records match names, verifiers
854 if ((conf && unconf) &&
855 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
856 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
857 (cmp_name(&conf->cl_name,&unconf->cl_name)) &&
858 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
859 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
860 status = nfserr_clid_inuse;
864 move_to_confirmed(unconf);
870 * conf record that matches input clientid.
871 * if unconf record that matches input clientid, then unconf->cl_name
872 * or unconf->cl_verifier don't match the conf record.
874 if ((conf && !unconf) ||
876 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
877 !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
878 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
879 status = nfserr_clid_inuse;
887 * conf record not found.
888 * unconf record found.
889 * unconf->cl_confirm matches input confirm
891 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
892 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
893 status = nfserr_clid_inuse;
897 move_to_confirmed(unconf);
902 * conf record not found, or if conf, then conf->cl_confirm does not
903 * match input confirm.
904 * unconf record not found, or if unconf, then unconf->cl_confirm
905 * does not match input confirm.
907 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
908 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
909 status = nfserr_stale_clientid;
912 /* check that we have hit one of the cases...*/
913 status = nfserr_inval;
917 nfsd4_probe_callback(clp);
923 * Open owner state (share locks)
926 /* hash tables for nfs4_stateowner */
927 #define OWNER_HASH_BITS 8
928 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
929 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
931 #define ownerid_hashval(id) \
932 ((id) & OWNER_HASH_MASK)
933 #define ownerstr_hashval(clientid, ownername) \
934 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
936 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
937 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
939 /* hash table for nfs4_file */
940 #define FILE_HASH_BITS 8
941 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
942 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
943 /* hash table for (open)nfs4_stateid */
944 #define STATEID_HASH_BITS 10
945 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
946 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
948 #define file_hashval(x) \
949 hash_ptr(x, FILE_HASH_BITS)
950 #define stateid_hashval(owner_id, file_id) \
951 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
953 static struct list_head file_hashtbl[FILE_HASH_SIZE];
954 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
956 /* OPEN Share state helper functions */
957 static inline struct nfs4_file *
958 alloc_init_file(struct inode *ino)
960 struct nfs4_file *fp;
961 unsigned int hashval = file_hashval(ino);
963 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
965 INIT_LIST_HEAD(&fp->fi_hash);
966 INIT_LIST_HEAD(&fp->fi_perfile);
967 INIT_LIST_HEAD(&fp->fi_del_perfile);
968 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
969 fp->fi_inode = igrab(ino);
970 fp->fi_id = current_fileid++;
977 release_all_files(void)
980 struct nfs4_file *fp;
982 for (i=0;i<FILE_HASH_SIZE;i++) {
983 while (!list_empty(&file_hashtbl[i])) {
984 fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
985 /* this should never be more than once... */
986 if (!list_empty(&fp->fi_perfile) || !list_empty(&fp->fi_del_perfile)) {
987 printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
995 nfsd4_free_slab(kmem_cache_t **slab)
1001 status = kmem_cache_destroy(*slab);
1007 nfsd4_free_slabs(void)
1009 nfsd4_free_slab(&stateowner_slab);
1010 nfsd4_free_slab(&file_slab);
1011 nfsd4_free_slab(&stateid_slab);
1012 nfsd4_free_slab(&deleg_slab);
1016 nfsd4_init_slabs(void)
1018 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1019 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1020 if (stateowner_slab == NULL)
1022 file_slab = kmem_cache_create("nfsd4_files",
1023 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1024 if (file_slab == NULL)
1026 stateid_slab = kmem_cache_create("nfsd4_stateids",
1027 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1028 if (stateid_slab == NULL)
1030 deleg_slab = kmem_cache_create("nfsd4_delegations",
1031 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1032 if (deleg_slab == NULL)
1037 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1042 nfs4_free_stateowner(struct kref *kref)
1044 struct nfs4_stateowner *sop =
1045 container_of(kref, struct nfs4_stateowner, so_ref);
1046 kfree(sop->so_owner.data);
1047 kmem_cache_free(stateowner_slab, sop);
1050 static inline struct nfs4_stateowner *
1051 alloc_stateowner(struct xdr_netobj *owner)
1053 struct nfs4_stateowner *sop;
1055 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1056 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1057 memcpy(sop->so_owner.data, owner->data, owner->len);
1058 sop->so_owner.len = owner->len;
1059 kref_init(&sop->so_ref);
1062 kmem_cache_free(stateowner_slab, sop);
1067 static struct nfs4_stateowner *
1068 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1069 struct nfs4_stateowner *sop;
1070 struct nfs4_replay *rp;
1071 unsigned int idhashval;
1073 if (!(sop = alloc_stateowner(&open->op_owner)))
1075 idhashval = ownerid_hashval(current_ownerid);
1076 INIT_LIST_HEAD(&sop->so_idhash);
1077 INIT_LIST_HEAD(&sop->so_strhash);
1078 INIT_LIST_HEAD(&sop->so_perclient);
1079 INIT_LIST_HEAD(&sop->so_perfilestate);
1080 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1081 INIT_LIST_HEAD(&sop->so_close_lru);
1083 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1084 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1085 list_add(&sop->so_perclient, &clp->cl_perclient);
1087 sop->so_is_open_owner = 1;
1088 sop->so_id = current_ownerid++;
1089 sop->so_client = clp;
1090 sop->so_seqid = open->op_seqid;
1091 sop->so_confirmed = 0;
1092 rp = &sop->so_replay;
1093 rp->rp_status = NFSERR_SERVERFAULT;
1095 rp->rp_buf = rp->rp_ibuf;
1100 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1102 struct nfs4_stateowner *lock_sop;
1104 while (!list_empty(&open_stp->st_perlockowner)) {
1105 lock_sop = list_entry(open_stp->st_perlockowner.next,
1106 struct nfs4_stateowner, so_perlockowner);
1107 /* list_del(&open_stp->st_perlockowner); */
1108 BUG_ON(lock_sop->so_is_open_owner);
1109 release_stateowner(lock_sop);
1114 unhash_stateowner(struct nfs4_stateowner *sop)
1116 struct nfs4_stateid *stp;
1118 list_del(&sop->so_idhash);
1119 list_del(&sop->so_strhash);
1120 if (sop->so_is_open_owner) {
1121 list_del(&sop->so_perclient);
1124 list_del(&sop->so_perlockowner);
1125 while (!list_empty(&sop->so_perfilestate)) {
1126 stp = list_entry(sop->so_perfilestate.next,
1127 struct nfs4_stateid, st_perfilestate);
1128 if (sop->so_is_open_owner)
1129 release_stateid(stp, OPEN_STATE);
1131 release_stateid(stp, LOCK_STATE);
1136 release_stateowner(struct nfs4_stateowner *sop)
1138 unhash_stateowner(sop);
1139 list_del(&sop->so_close_lru);
1140 nfs4_put_stateowner(sop);
1144 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1145 struct nfs4_stateowner *sop = open->op_stateowner;
1146 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1148 INIT_LIST_HEAD(&stp->st_hash);
1149 INIT_LIST_HEAD(&stp->st_perfilestate);
1150 INIT_LIST_HEAD(&stp->st_perlockowner);
1151 INIT_LIST_HEAD(&stp->st_perfile);
1152 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1153 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1155 list_add(&stp->st_perfile, &fp->fi_perfile);
1156 stp->st_stateowner = sop;
1158 stp->st_stateid.si_boot = boot_time;
1159 stp->st_stateid.si_stateownerid = sop->so_id;
1160 stp->st_stateid.si_fileid = fp->fi_id;
1161 stp->st_stateid.si_generation = 0;
1162 stp->st_access_bmap = 0;
1163 stp->st_deny_bmap = 0;
1164 __set_bit(open->op_share_access, &stp->st_access_bmap);
1165 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1169 release_stateid(struct nfs4_stateid *stp, int flags)
1171 struct file *filp = stp->st_vfs_file;
1173 list_del(&stp->st_hash);
1175 list_del(&stp->st_perfile);
1176 list_del(&stp->st_perfilestate);
1177 if (flags & OPEN_STATE) {
1178 release_stateid_lockowners(stp);
1179 stp->st_vfs_file = NULL;
1182 } else if (flags & LOCK_STATE)
1183 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1184 kmem_cache_free(stateid_slab, stp);
1189 release_file(struct nfs4_file *fp)
1191 list_del(&fp->fi_hash);
1193 kmem_cache_free(file_slab, fp);
1197 move_to_close_lru(struct nfs4_stateowner *sop)
1199 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1201 unhash_stateowner(sop);
1202 list_add_tail(&sop->so_close_lru, &close_lru);
1203 sop->so_time = get_seconds();
1207 release_state_owner(struct nfs4_stateid *stp, int flag)
1209 struct nfs4_stateowner *sop = stp->st_stateowner;
1210 struct nfs4_file *fp = stp->st_file;
1212 dprintk("NFSD: release_state_owner\n");
1213 release_stateid(stp, flag);
1215 /* place unused nfs4_stateowners on so_close_lru list to be
1216 * released by the laundromat service after the lease period
1217 * to enable us to handle CLOSE replay
1219 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1220 move_to_close_lru(sop);
1221 /* unused nfs4_file's are releseed. XXX slab cache? */
1222 if (list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile)) {
1228 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1229 return ((sop->so_owner.len == owner->len) &&
1230 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1231 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1234 static struct nfs4_stateowner *
1235 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1237 struct nfs4_stateowner *so = NULL;
1239 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1240 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1246 /* search file_hashtbl[] for file */
1247 static struct nfs4_file *
1248 find_file(struct inode *ino)
1250 unsigned int hashval = file_hashval(ino);
1251 struct nfs4_file *fp;
1253 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1254 if (fp->fi_inode == ino)
1260 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1261 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1264 set_access(unsigned int *access, unsigned long bmap) {
1268 for (i = 1; i < 4; i++) {
1269 if (test_bit(i, &bmap))
1275 set_deny(unsigned int *deny, unsigned long bmap) {
1279 for (i = 0; i < 4; i++) {
1280 if (test_bit(i, &bmap))
1286 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1287 unsigned int access, deny;
1289 set_access(&access, stp->st_access_bmap);
1290 set_deny(&deny, stp->st_deny_bmap);
1291 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1297 * Called to check deny when READ with all zero stateid or
1298 * WRITE with all zero or all one stateid
1301 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1303 struct inode *ino = current_fh->fh_dentry->d_inode;
1304 struct nfs4_file *fp;
1305 struct nfs4_stateid *stp;
1307 dprintk("NFSD: nfs4_share_conflict\n");
1309 fp = find_file(ino);
1311 /* Search for conflicting share reservations */
1312 list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1313 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1314 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1315 return nfserr_share_denied;
1322 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1324 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1325 put_write_access(filp->f_dentry->d_inode);
1326 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1331 * Recall a delegation
1334 do_recall(void *__dp)
1336 struct nfs4_delegation *dp = __dp;
1338 daemonize("nfsv4-recall");
1340 nfsd4_cb_recall(dp);
1345 * Spawn a thread to perform a recall on the delegation represented
1346 * by the lease (file_lock)
1348 * Called from break_lease() with lock_kernel() held.
1349 * Note: we assume break_lease will only call this *once* for any given
1353 void nfsd_break_deleg_cb(struct file_lock *fl)
1355 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1356 struct task_struct *t;
1358 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1362 /* We're assuming the state code never drops its reference
1363 * without first removing the lease. Since we're in this lease
1364 * callback (and since the lease code is serialized by the kernel
1365 * lock) we know the server hasn't removed the lease yet, we know
1366 * it's safe to take a reference: */
1367 atomic_inc(&dp->dl_count);
1369 spin_lock(&recall_lock);
1370 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1371 spin_unlock(&recall_lock);
1373 /* only place dl_time is set. protected by lock_kernel*/
1374 dp->dl_time = get_seconds();
1376 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1377 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1379 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1381 struct nfs4_client *clp = dp->dl_client;
1383 printk(KERN_INFO "NFSD: Callback thread failed for "
1384 "for client (clientid %08x/%08x)\n",
1385 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1386 nfs4_put_delegation(dp);
1391 * The file_lock is being reapd.
1393 * Called by locks_free_lock() with lock_kernel() held.
1396 void nfsd_release_deleg_cb(struct file_lock *fl)
1398 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1400 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1402 if (!(fl->fl_flags & FL_LEASE) || !dp)
1404 dp->dl_flock = NULL;
1408 * Set the delegation file_lock back pointer.
1410 * Called from __setlease() with lock_kernel() held.
1413 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1415 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1417 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1424 * Called from __setlease() with lock_kernel() held
1427 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1429 struct nfs4_delegation *onlistd =
1430 (struct nfs4_delegation *)onlist->fl_owner;
1431 struct nfs4_delegation *tryd =
1432 (struct nfs4_delegation *)try->fl_owner;
1434 if (onlist->fl_lmops != try->fl_lmops)
1437 return onlistd->dl_client == tryd->dl_client;
1442 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1445 return lease_modify(onlist, arg);
1450 struct lock_manager_operations nfsd_lease_mng_ops = {
1451 .fl_break = nfsd_break_deleg_cb,
1452 .fl_release_private = nfsd_release_deleg_cb,
1453 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1454 .fl_mylease = nfsd_same_client_deleg_cb,
1455 .fl_change = nfsd_change_deleg_cb,
1460 * nfsd4_process_open1()
1461 * lookup stateowner.
1473 * called with nfs4_lock_state() held.
1476 nfsd4_process_open1(struct nfsd4_open *open)
1479 clientid_t *clientid = &open->op_clientid;
1480 struct nfs4_client *clp = NULL;
1481 unsigned int strhashval;
1482 struct nfs4_stateowner *sop = NULL;
1484 status = nfserr_inval;
1485 if (!check_name(open->op_owner))
1488 if (STALE_CLIENTID(&open->op_clientid))
1489 return nfserr_stale_clientid;
1491 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1492 sop = find_openstateowner_str(strhashval, open);
1494 open->op_stateowner = sop;
1495 /* check for replay */
1496 if (open->op_seqid == sop->so_seqid){
1497 if (sop->so_replay.rp_buflen)
1498 return NFSERR_REPLAY_ME;
1500 /* The original OPEN failed so spectacularly
1501 * that we don't even have replay data saved!
1502 * Therefore, we have no choice but to continue
1503 * processing this OPEN; presumably, we'll
1504 * fail again for the same reason.
1506 dprintk("nfsd4_process_open1:"
1507 " replay with no replay cache\n");
1510 } else if (sop->so_confirmed) {
1511 if (open->op_seqid == sop->so_seqid + 1)
1513 status = nfserr_bad_seqid;
1516 /* If we get here, we received an OPEN for an
1517 * unconfirmed nfs4_stateowner. Since the seqid's are
1518 * different, purge the existing nfs4_stateowner, and
1519 * instantiate a new one.
1521 clp = sop->so_client;
1522 release_stateowner(sop);
1525 /* nfs4_stateowner not found.
1526 * Verify clientid and instantiate new nfs4_stateowner.
1527 * If verify fails this is presumably the result of the
1528 * client's lease expiring.
1530 status = nfserr_expired;
1531 clp = find_confirmed_client(clientid);
1535 status = nfserr_resource;
1536 sop = alloc_init_open_stateowner(strhashval, clp, open);
1539 open->op_stateowner = sop;
1542 renew_client(sop->so_client);
1544 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1545 status = nfserr_reclaim_bad;
1550 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1552 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1553 return nfserr_openmode;
1558 static struct nfs4_delegation *
1559 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1561 struct nfs4_delegation *dp;
1563 list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
1564 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1571 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1572 struct nfs4_delegation **dp)
1575 int status = nfserr_bad_stateid;
1577 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1580 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1581 RD_STATE : WR_STATE;
1582 status = nfs4_check_delegmode(*dp, flags);
1586 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1590 open->op_stateowner->so_confirmed = 1;
1595 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1597 struct nfs4_stateid *local;
1598 int status = nfserr_share_denied;
1599 struct nfs4_stateowner *sop = open->op_stateowner;
1601 list_for_each_entry(local, &fp->fi_perfile, st_perfile) {
1602 /* ignore lock owners */
1603 if (local->st_stateowner->so_is_open_owner == 0)
1605 /* remember if we have seen this open owner */
1606 if (local->st_stateowner == sop)
1608 /* check for conflicting share reservations */
1609 if (!test_share(local, open))
1617 static inline struct nfs4_stateid *
1618 nfs4_alloc_stateid(void)
1620 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1624 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1625 struct nfs4_delegation *dp,
1626 struct svc_fh *cur_fh, int flags)
1628 struct nfs4_stateid *stp;
1630 stp = nfs4_alloc_stateid();
1632 return nfserr_resource;
1635 get_file(dp->dl_vfs_file);
1636 stp->st_vfs_file = dp->dl_vfs_file;
1639 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1642 if (status == nfserr_dropit)
1643 status = nfserr_jukebox;
1644 kmem_cache_free(stateid_slab, stp);
1654 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1655 struct nfsd4_open *open)
1657 struct iattr iattr = {
1658 .ia_valid = ATTR_SIZE,
1661 if (!open->op_truncate)
1663 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1665 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1669 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1671 struct file *filp = stp->st_vfs_file;
1672 struct inode *inode = filp->f_dentry->d_inode;
1673 unsigned int share_access;
1676 set_access(&share_access, stp->st_access_bmap);
1677 share_access = ~share_access;
1678 share_access &= open->op_share_access;
1680 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1681 return nfsd4_truncate(rqstp, cur_fh, open);
1683 status = get_write_access(inode);
1685 return nfserrno(status);
1686 status = nfsd4_truncate(rqstp, cur_fh, open);
1688 put_write_access(inode);
1691 /* remember the open */
1692 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1693 set_bit(open->op_share_access, &stp->st_access_bmap);
1694 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1700 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1702 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1704 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1706 *status = nfserr_reclaim_bad;
1708 open->op_stateowner->so_confirmed = 1;
1709 open->op_stateowner->so_seqid--;
1715 * Attempt to hand out a delegation.
1718 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1720 struct nfs4_delegation *dp;
1721 struct nfs4_stateowner *sop = stp->st_stateowner;
1722 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1723 struct file_lock fl, *flp = &fl;
1724 int status, flag = 0;
1726 flag = NFS4_OPEN_DELEGATE_NONE;
1727 if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1728 || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1731 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1732 flag = NFS4_OPEN_DELEGATE_WRITE;
1734 flag = NFS4_OPEN_DELEGATE_READ;
1736 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1738 flag = NFS4_OPEN_DELEGATE_NONE;
1741 locks_init_lock(&fl);
1742 fl.fl_lmops = &nfsd_lease_mng_ops;
1743 fl.fl_flags = FL_LEASE;
1744 fl.fl_end = OFFSET_MAX;
1745 fl.fl_owner = (fl_owner_t)dp;
1746 fl.fl_file = stp->st_vfs_file;
1747 fl.fl_pid = current->tgid;
1749 /* setlease checks to see if delegation should be handed out.
1750 * the lock_manager callbacks fl_mylease and fl_change are used
1752 if ((status = setlease(stp->st_vfs_file,
1753 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1754 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1755 unhash_delegation(dp);
1756 flag = NFS4_OPEN_DELEGATE_NONE;
1760 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1762 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1763 dp->dl_stateid.si_boot,
1764 dp->dl_stateid.si_stateownerid,
1765 dp->dl_stateid.si_fileid,
1766 dp->dl_stateid.si_generation);
1768 open->op_delegate_type = flag;
1772 * called with nfs4_lock_state() held.
1775 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1777 struct nfs4_file *fp = NULL;
1778 struct inode *ino = current_fh->fh_dentry->d_inode;
1779 struct nfs4_stateid *stp = NULL;
1780 struct nfs4_delegation *dp = NULL;
1783 status = nfserr_inval;
1784 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1787 * Lookup file; if found, lookup stateid and check open request,
1788 * and check for delegations in the process of being recalled.
1789 * If not found, create the nfs4_file struct
1791 fp = find_file(ino);
1793 if ((status = nfs4_check_open(fp, open, &stp)))
1795 status = nfs4_check_deleg(fp, open, &dp);
1799 status = nfserr_bad_stateid;
1800 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1802 status = nfserr_resource;
1803 fp = alloc_init_file(ino);
1809 * OPEN the file, or upgrade an existing OPEN.
1810 * If truncate fails, the OPEN fails.
1813 /* Stateid was found, this is an OPEN upgrade */
1814 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1818 /* Stateid was not found, this is a new OPEN */
1820 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1824 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1827 init_stateid(stp, fp, open);
1828 status = nfsd4_truncate(rqstp, current_fh, open);
1830 release_stateid(stp, OPEN_STATE);
1834 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1837 * Attempt to hand out a delegation. No error return, because the
1838 * OPEN succeeds even if we fail.
1840 nfs4_open_delegation(current_fh, open, stp);
1844 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1845 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1846 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1848 /* take the opportunity to clean up unused state */
1849 if (fp && list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile))
1852 /* CLAIM_PREVIOUS has different error returns */
1853 nfs4_set_claim_prev(open, &status);
1855 * To finish the open response, we just need to set the rflags.
1857 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1858 if (!open->op_stateowner->so_confirmed)
1859 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1864 static struct work_struct laundromat_work;
1865 static void laundromat_main(void *);
1866 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1869 nfsd4_renew(clientid_t *clid)
1871 struct nfs4_client *clp;
1875 dprintk("process_renew(%08x/%08x): starting\n",
1876 clid->cl_boot, clid->cl_id);
1877 status = nfserr_stale_clientid;
1878 if (STALE_CLIENTID(clid))
1880 clp = find_confirmed_client(clid);
1881 status = nfserr_expired;
1883 /* We assume the client took too long to RENEW. */
1884 dprintk("nfsd4_renew: clientid not found!\n");
1888 status = nfserr_cb_path_down;
1889 if (!list_empty(&clp->cl_del_perclnt)
1890 && !atomic_read(&clp->cl_callback.cb_set))
1894 nfs4_unlock_state();
1899 nfs4_laundromat(void)
1901 struct nfs4_client *clp;
1902 struct nfs4_stateowner *sop;
1903 struct nfs4_delegation *dp;
1904 struct list_head *pos, *next, reaplist;
1905 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1906 time_t t, clientid_val = NFSD_LEASE_TIME;
1907 time_t u, test_val = NFSD_LEASE_TIME;
1911 dprintk("NFSD: laundromat service - starting\n");
1912 list_for_each_safe(pos, next, &client_lru) {
1913 clp = list_entry(pos, struct nfs4_client, cl_lru);
1914 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1915 t = clp->cl_time - cutoff;
1916 if (clientid_val > t)
1920 dprintk("NFSD: purging unused client (clientid %08x)\n",
1921 clp->cl_clientid.cl_id);
1924 INIT_LIST_HEAD(&reaplist);
1925 spin_lock(&recall_lock);
1926 list_for_each_safe(pos, next, &del_recall_lru) {
1927 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1928 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1929 u = dp->dl_time - cutoff;
1934 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1936 list_move(&dp->dl_recall_lru, &reaplist);
1938 spin_unlock(&recall_lock);
1939 list_for_each_safe(pos, next, &reaplist) {
1940 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1941 list_del_init(&dp->dl_recall_lru);
1942 unhash_delegation(dp);
1944 test_val = NFSD_LEASE_TIME;
1945 list_for_each_safe(pos, next, &close_lru) {
1946 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1947 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1948 u = sop->so_time - cutoff;
1953 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1955 list_del(&sop->so_close_lru);
1956 nfs4_put_stateowner(sop);
1958 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1959 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1960 nfs4_unlock_state();
1961 return clientid_val;
1965 laundromat_main(void *not_used)
1969 t = nfs4_laundromat();
1970 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1971 schedule_delayed_work(&laundromat_work, t*HZ);
1974 /* search ownerid_hashtbl[] and close_lru for stateid owner
1975 * (stateid->si_stateownerid)
1977 struct nfs4_stateowner *
1978 find_openstateowner_id(u32 st_id, int flags) {
1979 struct nfs4_stateowner *local = NULL;
1981 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1982 if (flags & CLOSE_STATE) {
1983 list_for_each_entry(local, &close_lru, so_close_lru) {
1984 if (local->so_id == st_id)
1992 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1994 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1998 STALE_STATEID(stateid_t *stateid)
2000 if (stateid->si_boot == boot_time)
2002 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2003 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2004 stateid->si_generation);
2009 access_permit_read(unsigned long access_bmap)
2011 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2012 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2013 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2017 access_permit_write(unsigned long access_bmap)
2019 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2020 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2024 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2026 int status = nfserr_openmode;
2028 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2030 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2038 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2040 /* Trying to call delegreturn with a special stateid? Yuch: */
2041 if (!(flags & (RD_STATE | WR_STATE)))
2042 return nfserr_bad_stateid;
2043 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2045 else if (nfs4_in_grace()) {
2046 /* Answer in remaining cases depends on existance of
2047 * conflicting state; so we must wait out the grace period. */
2048 return nfserr_grace;
2049 } else if (flags & WR_STATE)
2050 return nfs4_share_conflict(current_fh,
2051 NFS4_SHARE_DENY_WRITE);
2052 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2053 return nfs4_share_conflict(current_fh,
2054 NFS4_SHARE_DENY_READ);
2058 * Allow READ/WRITE during grace period on recovered state only for files
2059 * that are not able to provide mandatory locking.
2062 io_during_grace_disallowed(struct inode *inode, int flags)
2064 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2065 && MANDATORY_LOCK(inode);
2069 * Checks for stateid operations
2072 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2074 struct nfs4_stateid *stp = NULL;
2075 struct nfs4_delegation *dp = NULL;
2077 struct inode *ino = current_fh->fh_dentry->d_inode;
2080 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2081 stateid->si_boot, stateid->si_stateownerid,
2082 stateid->si_fileid, stateid->si_generation);
2086 if (io_during_grace_disallowed(ino, flags))
2087 return nfserr_grace;
2089 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2090 return check_special_stateids(current_fh, stateid, flags);
2093 status = nfserr_stale_stateid;
2094 if (STALE_STATEID(stateid))
2098 status = nfserr_bad_stateid;
2099 if (!stateid->si_fileid) { /* delegation stateid */
2100 if(!(dp = find_delegation_stateid(ino, stateid))) {
2101 dprintk("NFSD: delegation stateid not found\n");
2102 if (nfs4_in_grace())
2103 status = nfserr_grace;
2106 stidp = &dp->dl_stateid;
2107 } else { /* open or lock stateid */
2108 if (!(stp = find_stateid(stateid, flags))) {
2109 dprintk("NFSD: open or lock stateid not found\n");
2110 if (nfs4_in_grace())
2111 status = nfserr_grace;
2114 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2116 if (!stp->st_stateowner->so_confirmed)
2118 stidp = &stp->st_stateid;
2120 if (stateid->si_generation > stidp->si_generation)
2124 status = nfserr_old_stateid;
2125 if (stateid->si_generation < stidp->si_generation)
2128 if ((status = nfs4_check_openmode(stp,flags)))
2130 renew_client(stp->st_stateowner->so_client);
2132 *filpp = stp->st_vfs_file;
2134 if ((status = nfs4_check_delegmode(dp, flags)))
2136 renew_client(dp->dl_client);
2137 if (flags & DELEG_RET)
2138 unhash_delegation(dp);
2140 *filpp = dp->dl_vfs_file;
2149 * Checks for sequence id mutating operations.
2152 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)
2155 struct nfs4_stateid *stp;
2156 struct nfs4_stateowner *sop;
2158 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2159 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2160 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2161 stateid->si_generation);
2166 status = nfserr_bad_stateid;
2167 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2168 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2172 status = nfserr_stale_stateid;
2173 if (STALE_STATEID(stateid))
2176 * We return BAD_STATEID if filehandle doesn't match stateid,
2177 * the confirmed flag is incorrecly set, or the generation
2178 * number is incorrect.
2179 * If there is no entry in the openfile table for this id,
2180 * we can't always return BAD_STATEID;
2181 * this might be a retransmitted CLOSE which has arrived after
2182 * the openfile has been released.
2184 if (!(stp = find_stateid(stateid, flags)))
2185 goto no_nfs4_stateid;
2187 status = nfserr_bad_stateid;
2189 /* for new lock stateowners:
2190 * check that the lock->v.new.open_stateid
2191 * refers to an open stateowner
2193 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2194 * as the open_stateid->st_stateowner->so_client->clientid
2197 struct nfs4_stateowner *sop = stp->st_stateowner;
2198 struct nfs4_client *clp = sop->so_client;
2200 if (!sop->so_is_open_owner)
2202 if (!cmp_clid(&clp->cl_clientid, lockclid))
2206 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2207 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2212 *sopp = sop = stp->st_stateowner;
2215 * We now validate the seqid and stateid generation numbers.
2216 * For the moment, we ignore the possibility of
2217 * generation number wraparound.
2219 if (seqid != sop->so_seqid + 1)
2222 if (sop->so_confirmed) {
2223 if (flags & CONFIRM) {
2224 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2229 if (!(flags & CONFIRM)) {
2230 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2234 if (stateid->si_generation > stp->st_stateid.si_generation) {
2235 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2239 status = nfserr_old_stateid;
2240 if (stateid->si_generation < stp->st_stateid.si_generation) {
2241 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2244 /* XXX renew the client lease here */
2253 * We determine whether this is a bad stateid or a replay,
2254 * starting by trying to look up the stateowner.
2255 * If stateowner is not found - stateid is bad.
2257 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2258 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2259 status = nfserr_bad_stateid;
2265 if (seqid == sop->so_seqid) {
2266 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2267 /* indicate replay to calling function */
2268 status = NFSERR_REPLAY_ME;
2270 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2273 status = nfserr_bad_seqid;
2279 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2282 struct nfs4_stateowner *sop;
2283 struct nfs4_stateid *stp;
2285 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2286 (int)current_fh->fh_dentry->d_name.len,
2287 current_fh->fh_dentry->d_name.name);
2289 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2294 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2295 &oc->oc_req_stateid,
2296 CHECK_FH | CONFIRM | OPEN_STATE,
2297 &oc->oc_stateowner, &stp, NULL)))
2300 sop = oc->oc_stateowner;
2301 sop->so_confirmed = 1;
2302 update_stateid(&stp->st_stateid);
2303 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2304 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2305 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2306 stp->st_stateid.si_boot,
2307 stp->st_stateid.si_stateownerid,
2308 stp->st_stateid.si_fileid,
2309 stp->st_stateid.si_generation);
2311 if (oc->oc_stateowner)
2312 nfs4_get_stateowner(oc->oc_stateowner);
2313 nfs4_unlock_state();
2319 * unset all bits in union bitmap (bmap) that
2320 * do not exist in share (from successful OPEN_DOWNGRADE)
2323 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2326 for (i = 1; i < 4; i++) {
2327 if ((i & access) != i)
2328 __clear_bit(i, bmap);
2333 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2336 for (i = 0; i < 4; i++) {
2337 if ((i & deny) != i)
2338 __clear_bit(i, bmap);
2343 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2346 struct nfs4_stateid *stp;
2347 unsigned int share_access;
2349 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2350 (int)current_fh->fh_dentry->d_name.len,
2351 current_fh->fh_dentry->d_name.name);
2353 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2354 return nfserr_inval;
2357 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2359 CHECK_FH | OPEN_STATE,
2360 &od->od_stateowner, &stp, NULL)))
2363 status = nfserr_inval;
2364 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2365 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2366 stp->st_access_bmap, od->od_share_access);
2369 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2370 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2371 stp->st_deny_bmap, od->od_share_deny);
2374 set_access(&share_access, stp->st_access_bmap);
2375 nfs4_file_downgrade(stp->st_vfs_file,
2376 share_access & ~od->od_share_access);
2378 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2379 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2381 update_stateid(&stp->st_stateid);
2382 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2385 if (od->od_stateowner)
2386 nfs4_get_stateowner(od->od_stateowner);
2387 nfs4_unlock_state();
2392 * nfs4_unlock_state() called after encode
2395 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2398 struct nfs4_stateid *stp;
2400 dprintk("NFSD: nfsd4_close on file %.*s\n",
2401 (int)current_fh->fh_dentry->d_name.len,
2402 current_fh->fh_dentry->d_name.name);
2405 /* check close_lru for replay */
2406 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2408 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2409 &close->cl_stateowner, &stp, NULL)))
2412 * Return success, but first update the stateid.
2415 update_stateid(&stp->st_stateid);
2416 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2418 /* release_state_owner() calls nfsd_close() if needed */
2419 release_state_owner(stp, OPEN_STATE);
2421 if (close->cl_stateowner)
2422 nfs4_get_stateowner(close->cl_stateowner);
2423 nfs4_unlock_state();
2428 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2432 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2436 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2437 nfs4_unlock_state();
2444 * Lock owner state (byte-range locks)
2446 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2447 #define LOCK_HASH_BITS 8
2448 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2449 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2451 #define lockownerid_hashval(id) \
2452 ((id) & LOCK_HASH_MASK)
2454 static inline unsigned int
2455 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2456 struct xdr_netobj *ownername)
2458 return (file_hashval(inode) + cl_id
2459 + opaque_hashval(ownername->data, ownername->len))
2463 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2464 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2465 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2467 struct nfs4_stateid *
2468 find_stateid(stateid_t *stid, int flags)
2470 struct nfs4_stateid *local = NULL;
2471 u32 st_id = stid->si_stateownerid;
2472 u32 f_id = stid->si_fileid;
2473 unsigned int hashval;
2475 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2476 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2477 hashval = stateid_hashval(st_id, f_id);
2478 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2479 if ((local->st_stateid.si_stateownerid == st_id) &&
2480 (local->st_stateid.si_fileid == f_id))
2484 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2485 hashval = stateid_hashval(st_id, f_id);
2486 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2487 if ((local->st_stateid.si_stateownerid == st_id) &&
2488 (local->st_stateid.si_fileid == f_id))
2492 printk("NFSD: find_stateid: ERROR: no state flag\n");
2496 static struct nfs4_delegation *
2497 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2499 struct nfs4_file *fp = NULL;
2501 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2502 stid->si_boot, stid->si_stateownerid,
2503 stid->si_fileid, stid->si_generation);
2505 fp = find_file(ino);
2507 return find_delegation_file(fp, stid);
2512 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2513 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2514 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2515 * locking, this prevents us from being completely protocol-compliant. The
2516 * real solution to this problem is to start using unsigned file offsets in
2517 * the VFS, but this is a very deep change!
2520 nfs4_transform_lock_offset(struct file_lock *lock)
2522 if (lock->fl_start < 0)
2523 lock->fl_start = OFFSET_MAX;
2524 if (lock->fl_end < 0)
2525 lock->fl_end = OFFSET_MAX;
2529 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2531 struct nfs4_stateowner *local = NULL;
2534 if (hashval >= LOCK_HASH_SIZE)
2536 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2548 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2550 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2551 unsigned int hval = lockownerid_hashval(sop->so_id);
2553 deny->ld_sop = NULL;
2554 if (nfs4_verify_lock_stateowner(sop, hval)) {
2555 kref_get(&sop->so_ref);
2557 deny->ld_clientid = sop->so_client->cl_clientid;
2559 deny->ld_start = fl->fl_start;
2560 deny->ld_length = ~(u64)0;
2561 if (fl->fl_end != ~(u64)0)
2562 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2563 deny->ld_type = NFS4_READ_LT;
2564 if (fl->fl_type != F_RDLCK)
2565 deny->ld_type = NFS4_WRITE_LT;
2568 static struct nfs4_stateowner *
2569 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2571 struct nfs4_stateowner *local = NULL;
2574 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2575 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2576 if (!cmp_owner_str(local, owner, clid))
2584 static struct nfs4_stateowner *
2585 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2586 struct xdr_netobj *owner)
2588 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2589 struct nfs4_stateowner *op;
2591 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2592 if (cmp_owner_str(op, owner, clid))
2599 * Alloc a lock owner structure.
2600 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2603 * strhashval = lock_ownerstr_hashval
2604 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2607 static struct nfs4_stateowner *
2608 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2609 struct nfs4_stateowner *sop;
2610 struct nfs4_replay *rp;
2611 unsigned int idhashval;
2613 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2615 idhashval = lockownerid_hashval(current_ownerid);
2616 INIT_LIST_HEAD(&sop->so_idhash);
2617 INIT_LIST_HEAD(&sop->so_strhash);
2618 INIT_LIST_HEAD(&sop->so_perclient);
2619 INIT_LIST_HEAD(&sop->so_perfilestate);
2620 INIT_LIST_HEAD(&sop->so_perlockowner);
2621 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2623 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2624 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2625 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2626 sop->so_is_open_owner = 0;
2627 sop->so_id = current_ownerid++;
2628 sop->so_client = clp;
2629 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2630 sop->so_confirmed = 1;
2631 rp = &sop->so_replay;
2632 rp->rp_status = NFSERR_SERVERFAULT;
2634 rp->rp_buf = rp->rp_ibuf;
2638 struct nfs4_stateid *
2639 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2641 struct nfs4_stateid *stp;
2642 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2644 stp = nfs4_alloc_stateid();
2647 INIT_LIST_HEAD(&stp->st_hash);
2648 INIT_LIST_HEAD(&stp->st_perfile);
2649 INIT_LIST_HEAD(&stp->st_perfilestate);
2650 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2651 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2652 list_add(&stp->st_perfile, &fp->fi_perfile);
2654 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2655 stp->st_stateowner = sop;
2657 stp->st_stateid.si_boot = boot_time;
2658 stp->st_stateid.si_stateownerid = sop->so_id;
2659 stp->st_stateid.si_fileid = fp->fi_id;
2660 stp->st_stateid.si_generation = 0;
2661 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2662 stp->st_access_bmap = open_stp->st_access_bmap;
2663 stp->st_deny_bmap = open_stp->st_deny_bmap;
2670 check_lock_length(u64 offset, u64 length)
2672 return ((length == 0) || ((length != ~(u64)0) &&
2673 LOFF_OVERFLOW(offset, length)));
2680 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2682 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2683 struct nfs4_stateid *lock_stp;
2685 struct file_lock file_lock;
2686 struct file_lock *conflock;
2688 unsigned int strhashval;
2690 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2691 (long long) lock->lk_offset,
2692 (long long) lock->lk_length);
2694 if (nfs4_in_grace() && !lock->lk_reclaim)
2695 return nfserr_grace;
2696 if (!nfs4_in_grace() && lock->lk_reclaim)
2697 return nfserr_no_grace;
2699 if (check_lock_length(lock->lk_offset, lock->lk_length))
2700 return nfserr_inval;
2704 if (lock->lk_is_new) {
2706 * Client indicates that this is a new lockowner.
2707 * Use open owner and open stateid to create lock owner and lock
2710 struct nfs4_stateid *open_stp = NULL;
2711 struct nfs4_file *fp;
2713 status = nfserr_stale_clientid;
2714 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2715 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2719 /* is the new lock seqid presented by the client zero? */
2720 status = nfserr_bad_seqid;
2721 if (lock->v.new.lock_seqid != 0)
2724 /* validate and update open stateid and open seqid */
2725 status = nfs4_preprocess_seqid_op(current_fh,
2726 lock->lk_new_open_seqid,
2727 &lock->lk_new_open_stateid,
2728 CHECK_FH | OPEN_STATE,
2729 &open_sop, &open_stp,
2730 &lock->v.new.clientid);
2732 if (lock->lk_reclaim)
2733 status = nfserr_reclaim_bad;
2736 /* create lockowner and lock stateid */
2737 fp = open_stp->st_file;
2738 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2739 open_sop->so_client->cl_clientid.cl_id,
2740 &lock->v.new.owner);
2742 * If we already have this lock owner, the client is in
2743 * error (or our bookeeping is wrong!)
2744 * for asking for a 'new lock'.
2746 status = nfserr_bad_stateid;
2747 lock_sop = find_lockstateowner(&lock->v.new.owner,
2748 &lock->v.new.clientid);
2751 status = nfserr_resource;
2752 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2754 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2755 fp, open_stp)) == NULL) {
2756 release_stateowner(lock->lk_stateowner);
2757 lock->lk_stateowner = NULL;
2760 /* bump the open seqid used to create the lock */
2761 open_sop->so_seqid++;
2763 /* lock (lock owner + lock stateid) already exists */
2764 status = nfs4_preprocess_seqid_op(current_fh,
2765 lock->lk_old_lock_seqid,
2766 &lock->lk_old_lock_stateid,
2767 CHECK_FH | LOCK_STATE,
2768 &lock->lk_stateowner, &lock_stp, NULL);
2772 /* lock->lk_stateowner and lock_stp have been created or found */
2773 filp = lock_stp->st_vfs_file;
2775 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2776 printk("NFSD: nfsd4_lock: permission denied!\n");
2780 locks_init_lock(&file_lock);
2781 switch (lock->lk_type) {
2784 file_lock.fl_type = F_RDLCK;
2787 case NFS4_WRITEW_LT:
2788 file_lock.fl_type = F_WRLCK;
2791 status = nfserr_inval;
2794 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2795 file_lock.fl_pid = current->tgid;
2796 file_lock.fl_file = filp;
2797 file_lock.fl_flags = FL_POSIX;
2799 file_lock.fl_start = lock->lk_offset;
2800 if ((lock->lk_length == ~(u64)0) ||
2801 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2802 file_lock.fl_end = ~(u64)0;
2804 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2805 nfs4_transform_lock_offset(&file_lock);
2808 * Try to lock the file in the VFS.
2809 * Note: locks.c uses the BKL to protect the inode's lock list.
2812 status = posix_lock_file(filp, &file_lock);
2813 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2814 file_lock.fl_ops->fl_release_private(&file_lock);
2815 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2817 case 0: /* success! */
2818 update_stateid(&lock_stp->st_stateid);
2819 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2823 goto conflicting_lock;
2825 status = nfserr_deadlock;
2827 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2828 goto out_destroy_new_stateid;
2832 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2833 status = nfserr_denied;
2834 /* XXX There is a race here. Future patch needed to provide
2835 * an atomic posix_lock_and_test_file
2837 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2838 status = nfserr_serverfault;
2841 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2843 out_destroy_new_stateid:
2844 if (lock->lk_is_new) {
2845 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2847 * An error encountered after instantiation of the new
2848 * stateid has forced us to destroy it.
2850 if (!seqid_mutating_err(status))
2851 open_sop->so_seqid--;
2853 release_state_owner(lock_stp, LOCK_STATE);
2856 if (lock->lk_stateowner)
2857 nfs4_get_stateowner(lock->lk_stateowner);
2858 nfs4_unlock_state();
2866 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2868 struct inode *inode;
2870 struct file_lock file_lock;
2871 struct file_lock *conflicting_lock;
2874 if (nfs4_in_grace())
2875 return nfserr_grace;
2877 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2878 return nfserr_inval;
2880 lockt->lt_stateowner = NULL;
2883 status = nfserr_stale_clientid;
2884 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2885 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2889 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2890 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2891 if (status == nfserr_symlink)
2892 status = nfserr_inval;
2896 inode = current_fh->fh_dentry->d_inode;
2897 locks_init_lock(&file_lock);
2898 switch (lockt->lt_type) {
2901 file_lock.fl_type = F_RDLCK;
2904 case NFS4_WRITEW_LT:
2905 file_lock.fl_type = F_WRLCK;
2908 printk("NFSD: nfs4_lockt: bad lock type!\n");
2909 status = nfserr_inval;
2913 lockt->lt_stateowner = find_lockstateowner_str(inode,
2914 &lockt->lt_clientid, &lockt->lt_owner);
2915 if (lockt->lt_stateowner)
2916 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2917 file_lock.fl_pid = current->tgid;
2918 file_lock.fl_flags = FL_POSIX;
2920 file_lock.fl_start = lockt->lt_offset;
2921 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2922 file_lock.fl_end = ~(u64)0;
2924 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2926 nfs4_transform_lock_offset(&file_lock);
2928 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2929 * since LOCKT doesn't require an OPEN, and therefore a struct
2930 * file may not exist, pass posix_test_lock a struct file with
2931 * only the dentry:inode set.
2933 memset(&file, 0, sizeof (struct file));
2934 file.f_dentry = current_fh->fh_dentry;
2937 conflicting_lock = posix_test_lock(&file, &file_lock);
2938 if (conflicting_lock) {
2939 status = nfserr_denied;
2940 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2943 nfs4_unlock_state();
2948 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2950 struct nfs4_stateid *stp;
2951 struct file *filp = NULL;
2952 struct file_lock file_lock;
2955 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2956 (long long) locku->lu_offset,
2957 (long long) locku->lu_length);
2959 if (check_lock_length(locku->lu_offset, locku->lu_length))
2960 return nfserr_inval;
2964 if ((status = nfs4_preprocess_seqid_op(current_fh,
2967 CHECK_FH | LOCK_STATE,
2968 &locku->lu_stateowner, &stp, NULL)))
2971 filp = stp->st_vfs_file;
2973 locks_init_lock(&file_lock);
2974 file_lock.fl_type = F_UNLCK;
2975 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2976 file_lock.fl_pid = current->tgid;
2977 file_lock.fl_file = filp;
2978 file_lock.fl_flags = FL_POSIX;
2979 file_lock.fl_start = locku->lu_offset;
2981 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2982 file_lock.fl_end = ~(u64)0;
2984 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2985 nfs4_transform_lock_offset(&file_lock);
2988 * Try to unlock the file in the VFS.
2990 status = posix_lock_file(filp, &file_lock);
2991 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2992 file_lock.fl_ops->fl_release_private(&file_lock);
2994 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2998 * OK, unlock succeeded; the only thing left to do is update the stateid.
3000 update_stateid(&stp->st_stateid);
3001 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3004 if (locku->lu_stateowner)
3005 nfs4_get_stateowner(locku->lu_stateowner);
3006 nfs4_unlock_state();
3010 status = nfserrno(status);
3016 * 1: locks held by lockowner
3017 * 0: no locks held by lockowner
3020 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3022 struct file_lock **flpp;
3023 struct inode *inode = filp->f_dentry->d_inode;
3027 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3028 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3038 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3040 clientid_t *clid = &rlockowner->rl_clientid;
3041 struct nfs4_stateowner *local = NULL;
3042 struct xdr_netobj *owner = &rlockowner->rl_owner;
3045 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3046 clid->cl_boot, clid->cl_id);
3048 /* XXX check for lease expiration */
3050 status = nfserr_stale_clientid;
3051 if (STALE_CLIENTID(clid)) {
3052 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3059 local = find_lockstateowner(owner, clid);
3061 struct nfs4_stateid *stp;
3063 /* check for any locks held by any stateid
3064 * associated with the (lock) stateowner */
3065 status = nfserr_locks_held;
3066 list_for_each_entry(stp, &local->so_perfilestate,
3068 if (check_for_locks(stp->st_vfs_file, local))
3071 /* no locks held by (lock) stateowner */
3073 release_stateowner(local);
3076 nfs4_unlock_state();
3080 static inline struct nfs4_client_reclaim *
3081 alloc_reclaim(int namelen)
3083 struct nfs4_client_reclaim *crp = NULL;
3085 crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3088 crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3089 if (!crp->cr_name.data) {
3097 * failure => all reset bets are off, nfserr_no_grace...
3100 nfs4_client_to_reclaim(char *name, int namlen)
3102 unsigned int strhashval;
3103 struct nfs4_client_reclaim *crp = NULL;
3105 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3106 crp = alloc_reclaim(namlen);
3109 strhashval = clientstr_hashval(name, namlen);
3110 INIT_LIST_HEAD(&crp->cr_strhash);
3111 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3112 memcpy(crp->cr_name.data, name, namlen);
3113 crp->cr_name.len = namlen;
3114 reclaim_str_hashtbl_size++;
3119 nfs4_release_reclaim(void)
3121 struct nfs4_client_reclaim *crp = NULL;
3124 BUG_ON(!nfs4_reclaim_init);
3125 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3126 while (!list_empty(&reclaim_str_hashtbl[i])) {
3127 crp = list_entry(reclaim_str_hashtbl[i].next,
3128 struct nfs4_client_reclaim, cr_strhash);
3129 list_del(&crp->cr_strhash);
3130 kfree(crp->cr_name.data);
3132 reclaim_str_hashtbl_size--;
3135 BUG_ON(reclaim_str_hashtbl_size);
3139 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3140 struct nfs4_client_reclaim *
3141 nfs4_find_reclaim_client(clientid_t *clid)
3143 unsigned int strhashval;
3144 struct nfs4_client *clp;
3145 struct nfs4_client_reclaim *crp = NULL;
3148 /* find clientid in conf_id_hashtbl */
3149 clp = find_confirmed_client(clid);
3153 dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3154 clp->cl_name.len, clp->cl_name.data);
3156 /* find clp->cl_name in reclaim_str_hashtbl */
3157 strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3158 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3159 if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3167 * Called from OPEN. Look for clientid in reclaim list.
3170 nfs4_check_open_reclaim(clientid_t *clid)
3172 struct nfs4_client_reclaim *crp;
3174 if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
3175 return nfserr_reclaim_bad;
3181 * Start and stop routines
3185 __nfs4_state_init(void)
3190 if (!nfs4_reclaim_init) {
3191 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3192 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3193 reclaim_str_hashtbl_size = 0;
3194 nfs4_reclaim_init = 1;
3196 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3197 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3198 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3199 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3200 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3202 for (i = 0; i < FILE_HASH_SIZE; i++) {
3203 INIT_LIST_HEAD(&file_hashtbl[i]);
3205 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3206 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3207 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3209 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3210 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3211 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3213 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3214 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3215 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3217 memset(&zerostateid, 0, sizeof(stateid_t));
3218 memset(&onestateid, ~0, sizeof(stateid_t));
3220 INIT_LIST_HEAD(&close_lru);
3221 INIT_LIST_HEAD(&client_lru);
3222 INIT_LIST_HEAD(&del_recall_lru);
3223 spin_lock_init(&recall_lock);
3224 boot_time = get_seconds();
3225 grace_time = max(old_lease_time, lease_time);
3226 if (reclaim_str_hashtbl_size == 0)
3229 printk("NFSD: starting %ld-second grace period\n", grace_time);
3230 grace_end = boot_time + grace_time;
3231 INIT_WORK(&laundromat_work,laundromat_main, NULL);
3232 schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3236 nfs4_state_init(void)
3242 status = nfsd4_init_slabs();
3245 __nfs4_state_init();
3253 return get_seconds() < grace_end;
3259 printk("NFSD: ERROR in reboot recovery. State reclaims will fail.\n");
3260 grace_end = get_seconds();
3264 nfs4_lease_time(void)
3270 __nfs4_state_shutdown(void)
3273 struct nfs4_client *clp = NULL;
3274 struct nfs4_delegation *dp = NULL;
3275 struct nfs4_stateowner *sop = NULL;
3276 struct list_head *pos, *next, reaplist;
3278 list_for_each_safe(pos, next, &close_lru) {
3279 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3280 list_del(&sop->so_close_lru);
3281 nfs4_put_stateowner(sop);
3284 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3285 while (!list_empty(&conf_id_hashtbl[i])) {
3286 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3289 while (!list_empty(&unconf_str_hashtbl[i])) {
3290 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3294 INIT_LIST_HEAD(&reaplist);
3295 spin_lock(&recall_lock);
3296 list_for_each_safe(pos, next, &del_recall_lru) {
3297 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3298 list_move(&dp->dl_recall_lru, &reaplist);
3300 spin_unlock(&recall_lock);
3301 list_for_each_safe(pos, next, &reaplist) {
3302 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3303 list_del_init(&dp->dl_recall_lru);
3304 unhash_delegation(dp);
3307 release_all_files();
3308 cancel_delayed_work(&laundromat_work);
3309 flush_scheduled_work();
3311 dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
3312 list_add_perfile, list_del_perfile);
3313 dprintk("NFSD: add_perclient %d del_perclient %d\n",
3314 add_perclient, del_perclient);
3315 dprintk("NFSD: vfsopen %d vfsclose %d\n",
3320 nfs4_state_shutdown(void)
3323 nfs4_release_reclaim();
3324 __nfs4_state_shutdown();
3326 nfs4_unlock_state();
3330 * Called when leasetime is changed.
3332 * if nfsd is not started, simply set the global lease.
3334 * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3335 * e.g: boot_time is reset, existing nfs4_client structs are
3336 * used to fill reclaim_str_hashtbl, then all state (except for the
3337 * reclaim_str_hashtbl) is re-initialized.
3339 * if the old lease time is greater than the new lease time, the grace
3340 * period needs to be set to the old lease time to allow clients to reclaim
3341 * their state. XXX - we may want to set the grace period == lease time
3342 * after an initial grace period == old lease time
3344 * if an error occurs in this process, the new lease is set, but the server
3345 * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3346 * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3348 * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3349 * OPEN and LOCK reclaims.
3352 nfs4_reset_lease(time_t leasetime)
3354 struct nfs4_client *clp;
3357 printk("NFSD: New leasetime %ld\n",leasetime);
3361 old_lease_time = lease_time;
3362 lease_time = leasetime;
3364 nfs4_release_reclaim();
3366 /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3367 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3368 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3369 if (!nfs4_client_to_reclaim(clp->cl_name.data,
3370 clp->cl_name.len)) {
3371 nfs4_release_reclaim();
3377 __nfs4_state_shutdown();
3378 __nfs4_state_init();
3379 nfs4_unlock_state();