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;
77 u32 alloc_delegation= 0;
78 u32 free_delegation= 0;
80 /* forward declarations */
81 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
82 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
83 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
88 * protects clientid_hashtbl[], clientstr_hashtbl[],
89 * unconfstr_hashtbl[], uncofid_hashtbl[].
91 static DECLARE_MUTEX(client_sema);
100 nfs4_unlock_state(void)
106 opaque_hashval(const void *ptr, int nbytes)
108 unsigned char *cptr = (unsigned char *) ptr;
118 /* forward declarations */
119 static void release_stateowner(struct nfs4_stateowner *sop);
120 static void release_stateid(struct nfs4_stateid *stp, int flags);
121 static void release_file(struct nfs4_file *fp);
127 /* recall_lock protects the del_recall_lru */
128 spinlock_t recall_lock;
129 static struct list_head del_recall_lru;
131 static struct nfs4_delegation *
132 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
134 struct nfs4_delegation *dp;
135 struct nfs4_file *fp = stp->st_file;
136 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
138 dprintk("NFSD alloc_init_deleg\n");
139 if ((dp = kmalloc(sizeof(struct nfs4_delegation),
140 GFP_KERNEL)) == NULL)
142 INIT_LIST_HEAD(&dp->dl_del_perfile);
143 INIT_LIST_HEAD(&dp->dl_del_perclnt);
144 INIT_LIST_HEAD(&dp->dl_recall_lru);
148 get_file(stp->st_vfs_file);
149 dp->dl_vfs_file = stp->st_vfs_file;
151 dp->dl_recall.cbr_dp = NULL;
152 dp->dl_recall.cbr_ident = cb->cb_ident;
153 dp->dl_recall.cbr_trunc = 0;
154 dp->dl_stateid.si_boot = boot_time;
155 dp->dl_stateid.si_stateownerid = current_delegid++;
156 dp->dl_stateid.si_fileid = 0;
157 dp->dl_stateid.si_generation = 0;
158 dp->dl_fhlen = current_fh->fh_handle.fh_size;
159 memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base,
160 current_fh->fh_handle.fh_size);
162 atomic_set(&dp->dl_count, 1);
163 list_add(&dp->dl_del_perfile, &fp->fi_del_perfile);
164 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);
179 /* Remove the associated file_lock first, then remove the delegation.
180 * lease_modify() is called to remove the FS_LEASE file_lock from
181 * the i_flock list, eventually calling nfsd's lock_manager
182 * fl_release_callback.
185 nfs4_close_delegation(struct nfs4_delegation *dp)
187 struct file *filp = dp->dl_vfs_file;
189 dprintk("NFSD: close_delegation dp %p\n",dp);
190 dp->dl_vfs_file = NULL;
191 /* The following nfsd_close may not actually close the file,
192 * but we want to remove the lease in any case. */
194 setlease(filp, F_UNLCK, &dp->dl_flock);
199 /* Called under the state lock. */
201 unhash_delegation(struct nfs4_delegation *dp)
203 list_del_init(&dp->dl_del_perfile);
204 list_del_init(&dp->dl_del_perclnt);
205 spin_lock(&recall_lock);
206 list_del_init(&dp->dl_recall_lru);
207 spin_unlock(&recall_lock);
208 nfs4_close_delegation(dp);
209 nfs4_put_delegation(dp);
216 /* Hash tables for nfs4_clientid state */
217 #define CLIENT_HASH_BITS 4
218 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
219 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
221 #define clientid_hashval(id) \
222 ((id) & CLIENT_HASH_MASK)
223 #define clientstr_hashval(name, namelen) \
224 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
226 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
227 * used in reboot/reset lease grace period processing
229 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
230 * setclientid_confirmed info.
232 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
235 * client_lru holds client queue ordered by nfs4_client.cl_time
238 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
239 * for last close replay.
241 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
242 static int reclaim_str_hashtbl_size = 0;
243 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
244 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
245 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
246 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
247 static struct list_head client_lru;
248 static struct list_head close_lru;
251 renew_client(struct nfs4_client *clp)
254 * Move client to the end to the LRU list.
256 dprintk("renewing client (clientid %08x/%08x)\n",
257 clp->cl_clientid.cl_boot,
258 clp->cl_clientid.cl_id);
259 list_move_tail(&clp->cl_lru, &client_lru);
260 clp->cl_time = get_seconds();
263 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
265 STALE_CLIENTID(clientid_t *clid)
267 if (clid->cl_boot == boot_time)
269 dprintk("NFSD stale clientid (%08x/%08x)\n",
270 clid->cl_boot, clid->cl_id);
275 * XXX Should we use a slab cache ?
276 * This type of memory management is somewhat inefficient, but we use it
277 * anyway since SETCLIENTID is not a common operation.
279 static inline struct nfs4_client *
280 alloc_client(struct xdr_netobj name)
282 struct nfs4_client *clp;
284 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
285 memset(clp, 0, sizeof(*clp));
286 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
287 memcpy(clp->cl_name.data, name.data, name.len);
288 clp->cl_name.len = name.len;
299 free_client(struct nfs4_client *clp)
301 if (clp->cl_cred.cr_group_info)
302 put_group_info(clp->cl_cred.cr_group_info);
303 kfree(clp->cl_name.data);
308 put_nfs4_client(struct nfs4_client *clp)
310 if (atomic_dec_and_test(&clp->cl_count))
315 expire_client(struct nfs4_client *clp)
317 struct nfs4_stateowner *sop;
318 struct nfs4_delegation *dp;
319 struct nfs4_callback *cb = &clp->cl_callback;
320 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
321 struct list_head reaplist;
323 dprintk("NFSD: expire_client cl_count %d\n",
324 atomic_read(&clp->cl_count));
326 /* shutdown rpc client, ending any outstanding recall rpcs */
327 if (atomic_read(&cb->cb_set) == 1 && clnt) {
328 rpc_shutdown_client(clnt);
329 clnt = clp->cl_callback.cb_client = NULL;
332 INIT_LIST_HEAD(&reaplist);
333 spin_lock(&recall_lock);
334 while (!list_empty(&clp->cl_del_perclnt)) {
335 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
336 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
338 list_del_init(&dp->dl_del_perclnt);
339 list_move(&dp->dl_recall_lru, &reaplist);
341 spin_unlock(&recall_lock);
342 while (!list_empty(&reaplist)) {
343 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
344 list_del_init(&dp->dl_recall_lru);
345 unhash_delegation(dp);
347 list_del(&clp->cl_idhash);
348 list_del(&clp->cl_strhash);
349 list_del(&clp->cl_lru);
350 while (!list_empty(&clp->cl_perclient)) {
351 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
352 release_stateowner(sop);
354 put_nfs4_client(clp);
357 static struct nfs4_client *
358 create_client(struct xdr_netobj name) {
359 struct nfs4_client *clp;
361 if (!(clp = alloc_client(name)))
363 atomic_set(&clp->cl_count, 1);
364 atomic_set(&clp->cl_callback.cb_set, 0);
365 clp->cl_callback.cb_parsed = 0;
366 INIT_LIST_HEAD(&clp->cl_idhash);
367 INIT_LIST_HEAD(&clp->cl_strhash);
368 INIT_LIST_HEAD(&clp->cl_perclient);
369 INIT_LIST_HEAD(&clp->cl_del_perclnt);
370 INIT_LIST_HEAD(&clp->cl_lru);
376 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
377 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
381 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
382 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
383 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
387 copy_cred(struct svc_cred *target, struct svc_cred *source) {
389 target->cr_uid = source->cr_uid;
390 target->cr_gid = source->cr_gid;
391 target->cr_group_info = source->cr_group_info;
392 get_group_info(target->cr_group_info);
396 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
399 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
403 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
404 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
408 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
409 return((cl1->cl_boot == cl2->cl_boot) &&
410 (cl1->cl_id == cl2->cl_id));
413 /* XXX what about NGROUP */
415 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
416 return(cr1->cr_uid == cr2->cr_uid);
421 gen_clid(struct nfs4_client *clp) {
422 clp->cl_clientid.cl_boot = boot_time;
423 clp->cl_clientid.cl_id = current_clientid++;
427 gen_confirm(struct nfs4_client *clp) {
432 p = (u32 *)clp->cl_confirm.data;
438 check_name(struct xdr_netobj name) {
442 if (name.len > NFS4_OPAQUE_LIMIT) {
443 printk("NFSD: check_name: name too long(%d)!\n", name.len);
450 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
452 unsigned int idhashval;
454 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
455 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
456 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
457 list_add_tail(&clp->cl_lru, &client_lru);
458 clp->cl_time = get_seconds();
462 move_to_confirmed(struct nfs4_client *clp)
464 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
465 unsigned int strhashval;
467 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
468 list_del_init(&clp->cl_strhash);
469 list_del_init(&clp->cl_idhash);
470 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
471 strhashval = clientstr_hashval(clp->cl_name.data,
473 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
477 static struct nfs4_client *
478 find_confirmed_client(clientid_t *clid)
480 struct nfs4_client *clp;
481 unsigned int idhashval = clientid_hashval(clid->cl_id);
483 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
484 if (cmp_clid(&clp->cl_clientid, clid))
490 static struct nfs4_client *
491 find_unconfirmed_client(clientid_t *clid)
493 struct nfs4_client *clp;
494 unsigned int idhashval = clientid_hashval(clid->cl_id);
496 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
497 if (cmp_clid(&clp->cl_clientid, clid))
503 /* a helper function for parse_callback */
505 parse_octet(unsigned int *lenp, char **addrp)
507 unsigned int len = *lenp;
519 if ((c < '0') || (c > '9')) {
525 n = (n * 10) + (c - '0');
536 /* parse and set the setclientid ipv4 callback address */
538 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
543 u32 addrlen = addr_len;
544 char *addr = addr_val;
549 for(i = 4; i > 0 ; i--) {
550 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
553 cbaddr |= (temp << shift);
561 for(i = 2; i > 0 ; i--) {
562 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
565 cbport |= (temp << shift);
574 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
576 struct nfs4_callback *cb = &clp->cl_callback;
578 /* Currently, we only support tcp for the callback channel */
579 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
582 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
583 &cb->cb_addr, &cb->cb_port)))
585 cb->cb_prog = se->se_callback_prog;
586 cb->cb_ident = se->se_callback_ident;
590 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
591 "will not receive delegations\n",
592 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
599 * RFC 3010 has a complex implmentation description of processing a
600 * SETCLIENTID request consisting of 5 bullets, labeled as
601 * CASE0 - CASE4 below.
604 * callback information will be processed in a future patch
606 * an unconfirmed record is added when:
607 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
608 * CASE 1: confirmed record found with matching name, principal,
609 * verifier, and clientid.
610 * CASE 2: confirmed record found with matching name, principal,
611 * and there is no unconfirmed record with matching
614 * an unconfirmed record is replaced when:
615 * CASE 3: confirmed record found with matching name, principal,
616 * and an unconfirmed record is found with matching
617 * name, principal, and with clientid and
618 * confirm that does not match the confirmed record.
619 * CASE 4: there is no confirmed record with matching name and
620 * principal. there is an unconfirmed record with
621 * matching name, principal.
623 * an unconfirmed record is deleted when:
624 * CASE 1: an unconfirmed record that matches input name, verifier,
625 * and confirmed clientid.
626 * CASE 4: any unconfirmed records with matching name and principal
627 * that exist after an unconfirmed record has been replaced
628 * as described above.
632 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
634 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
635 struct xdr_netobj clname = {
636 .len = setclid->se_namelen,
637 .data = setclid->se_name,
639 nfs4_verifier clverifier = setclid->se_verf;
640 unsigned int strhashval;
641 struct nfs4_client * conf, * unconf, * new, * clp;
644 status = nfserr_inval;
645 if (!check_name(clname))
649 * XXX The Duplicate Request Cache (DRC) has been checked (??)
650 * We get here on a DRC miss.
653 strhashval = clientstr_hashval(clname.data, clname.len);
657 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
658 if (!cmp_name(&clp->cl_name, &clname))
662 * clname match, confirmed, different principal
663 * or different ip_address
665 status = nfserr_clid_inuse;
666 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
667 printk("NFSD: setclientid: string in use by client"
668 "(clientid %08x/%08x)\n",
669 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
672 if (clp->cl_addr != ip_addr) {
673 printk("NFSD: setclientid: string in use by client"
674 "(clientid %08x/%08x)\n",
675 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
680 * cl_name match from a previous SETCLIENTID operation
681 * XXX check for additional matches?
687 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
688 if (!cmp_name(&clp->cl_name, &clname))
690 /* cl_name match from a previous SETCLIENTID operation */
694 status = nfserr_resource;
698 * placed first, because it is the normal case.
701 expire_client(unconf);
702 if (!(new = create_client(clname)))
704 copy_verf(new, &clverifier);
705 new->cl_addr = ip_addr;
706 copy_cred(&new->cl_cred,&rqstp->rq_cred);
709 gen_callback(new, setclid);
710 add_to_unconfirmed(new, strhashval);
711 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
714 * cl_name match, confirmed, principal match
715 * verifier match: probable callback update
717 * remove any unconfirmed nfs4_client with
718 * matching cl_name, cl_verifier, and cl_clientid
720 * create and insert an unconfirmed nfs4_client with same
721 * cl_name, cl_verifier, and cl_clientid as existing
722 * nfs4_client, but with the new callback info and a
726 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
727 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
728 expire_client(unconf);
730 if (!(new = create_client(clname)))
732 copy_verf(new,&conf->cl_verifier);
733 new->cl_addr = ip_addr;
734 copy_cred(&new->cl_cred,&rqstp->rq_cred);
735 copy_clid(new, conf);
737 gen_callback(new, setclid);
738 add_to_unconfirmed(new,strhashval);
739 } else if (!unconf) {
742 * clname match, confirmed, principal match
743 * verfier does not match
744 * no unconfirmed. create a new unconfirmed nfs4_client
745 * using input clverifier, clname, and callback info
746 * and generate a new cl_clientid and cl_confirm.
748 if (!(new = create_client(clname)))
750 copy_verf(new,&clverifier);
751 new->cl_addr = ip_addr;
752 copy_cred(&new->cl_cred,&rqstp->rq_cred);
755 gen_callback(new, setclid);
756 add_to_unconfirmed(new, strhashval);
757 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
760 * confirmed found (name, principal match)
761 * confirmed verifier does not match input clverifier
763 * unconfirmed found (name match)
764 * confirmed->cl_confirm != unconfirmed->cl_confirm
766 * remove unconfirmed.
768 * create an unconfirmed nfs4_client
769 * with same cl_name as existing confirmed nfs4_client,
770 * but with new callback info, new cl_clientid,
771 * new cl_verifier and a new cl_confirm
773 expire_client(unconf);
774 if (!(new = create_client(clname)))
776 copy_verf(new,&clverifier);
777 new->cl_addr = ip_addr;
778 copy_cred(&new->cl_cred,&rqstp->rq_cred);
781 gen_callback(new, setclid);
782 add_to_unconfirmed(new, strhashval);
784 /* No cases hit !!! */
785 status = nfserr_inval;
789 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
790 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
791 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
800 * RFC 3010 has a complex implmentation description of processing a
801 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
802 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
804 * NOTE: callback information will be processed here in a future patch
807 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
809 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
810 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
811 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
812 clientid_t * clid = &setclientid_confirm->sc_clientid;
815 if (STALE_CLIENTID(clid))
816 return nfserr_stale_clientid;
818 * XXX The Duplicate Request Cache (DRC) has been checked (??)
819 * We get here on a DRC miss.
823 clp = find_confirmed_client(clid);
825 status = nfserr_inval;
827 * Found a record for this clientid. If the IP addresses
828 * don't match, return ERR_INVAL just as if the record had
831 if (clp->cl_addr != ip_addr) {
832 printk("NFSD: setclientid: string in use by client"
833 "(clientid %08x/%08x)\n",
834 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
839 clp = find_unconfirmed_client(clid);
841 status = nfserr_inval;
842 if (clp->cl_addr != ip_addr) {
843 printk("NFSD: setclientid: string in use by client"
844 "(clientid %08x/%08x)\n",
845 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
851 * unconf record that matches input clientid and input confirm.
852 * conf record that matches input clientid.
853 * conf and unconf records match names, verifiers
855 if ((conf && unconf) &&
856 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
857 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
858 (cmp_name(&conf->cl_name,&unconf->cl_name)) &&
859 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
860 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
861 status = nfserr_clid_inuse;
865 move_to_confirmed(unconf);
871 * conf record that matches input clientid.
872 * if unconf record that matches input clientid, then unconf->cl_name
873 * or unconf->cl_verifier don't match the conf record.
875 if ((conf && !unconf) ||
877 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
878 !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
879 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
880 status = nfserr_clid_inuse;
888 * conf record not found.
889 * unconf record found.
890 * unconf->cl_confirm matches input confirm
892 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
893 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
894 status = nfserr_clid_inuse;
898 move_to_confirmed(unconf);
903 * conf record not found, or if conf, then conf->cl_confirm does not
904 * match input confirm.
905 * unconf record not found, or if unconf, then unconf->cl_confirm
906 * does not match input confirm.
908 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
909 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
910 status = nfserr_stale_clientid;
913 /* check that we have hit one of the cases...*/
914 status = nfserr_inval;
918 nfsd4_probe_callback(clp);
924 * Open owner state (share locks)
927 /* hash tables for nfs4_stateowner */
928 #define OWNER_HASH_BITS 8
929 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
930 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
932 #define ownerid_hashval(id) \
933 ((id) & OWNER_HASH_MASK)
934 #define ownerstr_hashval(clientid, ownername) \
935 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
937 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
938 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
940 /* hash table for nfs4_file */
941 #define FILE_HASH_BITS 8
942 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
943 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
944 /* hash table for (open)nfs4_stateid */
945 #define STATEID_HASH_BITS 10
946 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
947 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
949 #define file_hashval(x) \
950 hash_ptr(x, FILE_HASH_BITS)
951 #define stateid_hashval(owner_id, file_id) \
952 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
954 static struct list_head file_hashtbl[FILE_HASH_SIZE];
955 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
957 /* OPEN Share state helper functions */
958 static inline struct nfs4_file *
959 alloc_init_file(struct inode *ino)
961 struct nfs4_file *fp;
962 unsigned int hashval = file_hashval(ino);
964 if ((fp = kmalloc(sizeof(struct nfs4_file),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++;
978 release_all_files(void)
981 struct nfs4_file *fp;
983 for (i=0;i<FILE_HASH_SIZE;i++) {
984 while (!list_empty(&file_hashtbl[i])) {
985 fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
986 /* this should never be more than once... */
987 if (!list_empty(&fp->fi_perfile) || !list_empty(&fp->fi_del_perfile)) {
988 printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
995 kmem_cache_t *stateowner_slab = NULL;
998 nfsd4_init_slabs(void)
1000 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1001 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1002 if (stateowner_slab == NULL) {
1003 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1010 nfsd4_free_slabs(void)
1014 if (stateowner_slab)
1015 status = kmem_cache_destroy(stateowner_slab);
1016 stateowner_slab = NULL;
1021 nfs4_free_stateowner(struct kref *kref)
1023 struct nfs4_stateowner *sop =
1024 container_of(kref, struct nfs4_stateowner, so_ref);
1025 kfree(sop->so_owner.data);
1026 kmem_cache_free(stateowner_slab, sop);
1029 static inline struct nfs4_stateowner *
1030 alloc_stateowner(struct xdr_netobj *owner)
1032 struct nfs4_stateowner *sop;
1034 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1035 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1036 memcpy(sop->so_owner.data, owner->data, owner->len);
1037 sop->so_owner.len = owner->len;
1038 kref_init(&sop->so_ref);
1041 kmem_cache_free(stateowner_slab, sop);
1046 static struct nfs4_stateowner *
1047 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1048 struct nfs4_stateowner *sop;
1049 struct nfs4_replay *rp;
1050 unsigned int idhashval;
1052 if (!(sop = alloc_stateowner(&open->op_owner)))
1054 idhashval = ownerid_hashval(current_ownerid);
1055 INIT_LIST_HEAD(&sop->so_idhash);
1056 INIT_LIST_HEAD(&sop->so_strhash);
1057 INIT_LIST_HEAD(&sop->so_perclient);
1058 INIT_LIST_HEAD(&sop->so_perfilestate);
1059 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1060 INIT_LIST_HEAD(&sop->so_close_lru);
1062 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1063 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1064 list_add(&sop->so_perclient, &clp->cl_perclient);
1066 sop->so_is_open_owner = 1;
1067 sop->so_id = current_ownerid++;
1068 sop->so_client = clp;
1069 sop->so_seqid = open->op_seqid;
1070 sop->so_confirmed = 0;
1071 rp = &sop->so_replay;
1072 rp->rp_status = NFSERR_SERVERFAULT;
1074 rp->rp_buf = rp->rp_ibuf;
1079 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1081 struct nfs4_stateowner *lock_sop;
1083 while (!list_empty(&open_stp->st_perlockowner)) {
1084 lock_sop = list_entry(open_stp->st_perlockowner.next,
1085 struct nfs4_stateowner, so_perlockowner);
1086 /* list_del(&open_stp->st_perlockowner); */
1087 BUG_ON(lock_sop->so_is_open_owner);
1088 release_stateowner(lock_sop);
1093 unhash_stateowner(struct nfs4_stateowner *sop)
1095 struct nfs4_stateid *stp;
1097 list_del(&sop->so_idhash);
1098 list_del(&sop->so_strhash);
1099 if (sop->so_is_open_owner) {
1100 list_del(&sop->so_perclient);
1103 list_del(&sop->so_perlockowner);
1104 while (!list_empty(&sop->so_perfilestate)) {
1105 stp = list_entry(sop->so_perfilestate.next,
1106 struct nfs4_stateid, st_perfilestate);
1107 if (sop->so_is_open_owner)
1108 release_stateid(stp, OPEN_STATE);
1110 release_stateid(stp, LOCK_STATE);
1115 release_stateowner(struct nfs4_stateowner *sop)
1117 unhash_stateowner(sop);
1118 list_del(&sop->so_close_lru);
1119 nfs4_put_stateowner(sop);
1123 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1124 struct nfs4_stateowner *sop = open->op_stateowner;
1125 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1127 INIT_LIST_HEAD(&stp->st_hash);
1128 INIT_LIST_HEAD(&stp->st_perfilestate);
1129 INIT_LIST_HEAD(&stp->st_perlockowner);
1130 INIT_LIST_HEAD(&stp->st_perfile);
1131 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1132 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1134 list_add(&stp->st_perfile, &fp->fi_perfile);
1135 stp->st_stateowner = sop;
1137 stp->st_stateid.si_boot = boot_time;
1138 stp->st_stateid.si_stateownerid = sop->so_id;
1139 stp->st_stateid.si_fileid = fp->fi_id;
1140 stp->st_stateid.si_generation = 0;
1141 stp->st_access_bmap = 0;
1142 stp->st_deny_bmap = 0;
1143 __set_bit(open->op_share_access, &stp->st_access_bmap);
1144 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1148 release_stateid(struct nfs4_stateid *stp, int flags)
1150 struct file *filp = stp->st_vfs_file;
1152 list_del(&stp->st_hash);
1154 list_del(&stp->st_perfile);
1155 list_del(&stp->st_perfilestate);
1156 if (flags & OPEN_STATE) {
1157 release_stateid_lockowners(stp);
1158 stp->st_vfs_file = NULL;
1161 } else if (flags & LOCK_STATE)
1162 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1168 release_file(struct nfs4_file *fp)
1171 list_del(&fp->fi_hash);
1177 move_to_close_lru(struct nfs4_stateowner *sop)
1179 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1181 unhash_stateowner(sop);
1182 list_add_tail(&sop->so_close_lru, &close_lru);
1183 sop->so_time = get_seconds();
1187 release_state_owner(struct nfs4_stateid *stp, int flag)
1189 struct nfs4_stateowner *sop = stp->st_stateowner;
1190 struct nfs4_file *fp = stp->st_file;
1192 dprintk("NFSD: release_state_owner\n");
1193 release_stateid(stp, flag);
1195 /* place unused nfs4_stateowners on so_close_lru list to be
1196 * released by the laundromat service after the lease period
1197 * to enable us to handle CLOSE replay
1199 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1200 move_to_close_lru(sop);
1201 /* unused nfs4_file's are releseed. XXX slab cache? */
1202 if (list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile)) {
1208 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1209 return ((sop->so_owner.len == owner->len) &&
1210 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1211 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1214 static struct nfs4_stateowner *
1215 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1217 struct nfs4_stateowner *so = NULL;
1219 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1220 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1226 /* search file_hashtbl[] for file */
1227 static struct nfs4_file *
1228 find_file(struct inode *ino)
1230 unsigned int hashval = file_hashval(ino);
1231 struct nfs4_file *fp;
1233 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1234 if (fp->fi_inode == ino)
1240 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1241 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1244 set_access(unsigned int *access, unsigned long bmap) {
1248 for (i = 1; i < 4; i++) {
1249 if (test_bit(i, &bmap))
1255 set_deny(unsigned int *deny, unsigned long bmap) {
1259 for (i = 0; i < 4; i++) {
1260 if (test_bit(i, &bmap))
1266 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1267 unsigned int access, deny;
1269 set_access(&access, stp->st_access_bmap);
1270 set_deny(&deny, stp->st_deny_bmap);
1271 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1277 * Called to check deny when READ with all zero stateid or
1278 * WRITE with all zero or all one stateid
1281 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1283 struct inode *ino = current_fh->fh_dentry->d_inode;
1284 struct nfs4_file *fp;
1285 struct nfs4_stateid *stp;
1287 dprintk("NFSD: nfs4_share_conflict\n");
1289 fp = find_file(ino);
1291 /* Search for conflicting share reservations */
1292 list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1293 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1294 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1295 return nfserr_share_denied;
1302 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1304 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1305 put_write_access(filp->f_dentry->d_inode);
1306 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1311 * Recall a delegation
1314 do_recall(void *__dp)
1316 struct nfs4_delegation *dp = __dp;
1318 daemonize("nfsv4-recall");
1320 nfsd4_cb_recall(dp);
1325 * Spawn a thread to perform a recall on the delegation represented
1326 * by the lease (file_lock)
1328 * Called from break_lease() with lock_kernel() held.
1329 * Note: we assume break_lease will only call this *once* for any given
1333 void nfsd_break_deleg_cb(struct file_lock *fl)
1335 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1336 struct task_struct *t;
1338 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1342 /* We're assuming the state code never drops its reference
1343 * without first removing the lease. Since we're in this lease
1344 * callback (and since the lease code is serialized by the kernel
1345 * lock) we know the server hasn't removed the lease yet, we know
1346 * it's safe to take a reference: */
1347 atomic_inc(&dp->dl_count);
1349 spin_lock(&recall_lock);
1350 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1351 spin_unlock(&recall_lock);
1353 /* only place dl_time is set. protected by lock_kernel*/
1354 dp->dl_time = get_seconds();
1356 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1357 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1359 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1361 struct nfs4_client *clp = dp->dl_client;
1363 printk(KERN_INFO "NFSD: Callback thread failed for "
1364 "for client (clientid %08x/%08x)\n",
1365 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1366 nfs4_put_delegation(dp);
1371 * The file_lock is being reapd.
1373 * Called by locks_free_lock() with lock_kernel() held.
1376 void nfsd_release_deleg_cb(struct file_lock *fl)
1378 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1380 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1382 if (!(fl->fl_flags & FL_LEASE) || !dp)
1384 dp->dl_flock = NULL;
1388 * Set the delegation file_lock back pointer.
1390 * Called from __setlease() with lock_kernel() held.
1393 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1395 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1397 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1404 * Called from __setlease() with lock_kernel() held
1407 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1409 struct nfs4_delegation *onlistd =
1410 (struct nfs4_delegation *)onlist->fl_owner;
1411 struct nfs4_delegation *tryd =
1412 (struct nfs4_delegation *)try->fl_owner;
1414 if (onlist->fl_lmops != try->fl_lmops)
1417 return onlistd->dl_client == tryd->dl_client;
1422 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1425 return lease_modify(onlist, arg);
1430 struct lock_manager_operations nfsd_lease_mng_ops = {
1431 .fl_break = nfsd_break_deleg_cb,
1432 .fl_release_private = nfsd_release_deleg_cb,
1433 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1434 .fl_mylease = nfsd_same_client_deleg_cb,
1435 .fl_change = nfsd_change_deleg_cb,
1440 * nfsd4_process_open1()
1441 * lookup stateowner.
1453 * called with nfs4_lock_state() held.
1456 nfsd4_process_open1(struct nfsd4_open *open)
1459 clientid_t *clientid = &open->op_clientid;
1460 struct nfs4_client *clp = NULL;
1461 unsigned int strhashval;
1462 struct nfs4_stateowner *sop = NULL;
1464 status = nfserr_inval;
1465 if (!check_name(open->op_owner))
1468 if (STALE_CLIENTID(&open->op_clientid))
1469 return nfserr_stale_clientid;
1471 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1472 sop = find_openstateowner_str(strhashval, open);
1474 open->op_stateowner = sop;
1475 /* check for replay */
1476 if (open->op_seqid == sop->so_seqid){
1477 if (sop->so_replay.rp_buflen)
1478 return NFSERR_REPLAY_ME;
1480 /* The original OPEN failed so spectacularly
1481 * that we don't even have replay data saved!
1482 * Therefore, we have no choice but to continue
1483 * processing this OPEN; presumably, we'll
1484 * fail again for the same reason.
1486 dprintk("nfsd4_process_open1:"
1487 " replay with no replay cache\n");
1490 } else if (sop->so_confirmed) {
1491 if (open->op_seqid == sop->so_seqid + 1)
1493 status = nfserr_bad_seqid;
1496 /* If we get here, we received an OPEN for an
1497 * unconfirmed nfs4_stateowner. Since the seqid's are
1498 * different, purge the existing nfs4_stateowner, and
1499 * instantiate a new one.
1501 clp = sop->so_client;
1502 release_stateowner(sop);
1505 /* nfs4_stateowner not found.
1506 * Verify clientid and instantiate new nfs4_stateowner.
1507 * If verify fails this is presumably the result of the
1508 * client's lease expiring.
1510 status = nfserr_expired;
1511 clp = find_confirmed_client(clientid);
1515 status = nfserr_resource;
1516 sop = alloc_init_open_stateowner(strhashval, clp, open);
1519 open->op_stateowner = sop;
1522 renew_client(sop->so_client);
1524 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1525 status = nfserr_reclaim_bad;
1530 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1532 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1533 return nfserr_openmode;
1538 static struct nfs4_delegation *
1539 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1541 struct nfs4_delegation *dp;
1543 list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
1544 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1551 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1552 struct nfs4_delegation **dp)
1555 int status = nfserr_bad_stateid;
1557 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1560 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1561 RD_STATE : WR_STATE;
1562 status = nfs4_check_delegmode(*dp, flags);
1566 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1570 open->op_stateowner->so_confirmed = 1;
1575 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1577 struct nfs4_stateid *local;
1578 int status = nfserr_share_denied;
1579 struct nfs4_stateowner *sop = open->op_stateowner;
1581 list_for_each_entry(local, &fp->fi_perfile, st_perfile) {
1582 /* ignore lock owners */
1583 if (local->st_stateowner->so_is_open_owner == 0)
1585 /* remember if we have seen this open owner */
1586 if (local->st_stateowner == sop)
1588 /* check for conflicting share reservations */
1589 if (!test_share(local, open))
1598 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1599 struct nfs4_delegation *dp,
1600 struct svc_fh *cur_fh, int flags)
1602 struct nfs4_stateid *stp;
1604 stp = kmalloc(sizeof(struct nfs4_stateid), GFP_KERNEL);
1606 return nfserr_resource;
1609 get_file(dp->dl_vfs_file);
1610 stp->st_vfs_file = dp->dl_vfs_file;
1613 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1616 if (status == nfserr_dropit)
1617 status = nfserr_jukebox;
1628 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1629 struct nfsd4_open *open)
1631 struct iattr iattr = {
1632 .ia_valid = ATTR_SIZE,
1635 if (!open->op_truncate)
1637 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1639 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1643 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1645 struct file *filp = stp->st_vfs_file;
1646 struct inode *inode = filp->f_dentry->d_inode;
1647 unsigned int share_access;
1650 set_access(&share_access, stp->st_access_bmap);
1651 share_access = ~share_access;
1652 share_access &= open->op_share_access;
1654 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1655 return nfsd4_truncate(rqstp, cur_fh, open);
1657 status = get_write_access(inode);
1659 return nfserrno(status);
1660 status = nfsd4_truncate(rqstp, cur_fh, open);
1662 put_write_access(inode);
1665 /* remember the open */
1666 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1667 set_bit(open->op_share_access, &stp->st_access_bmap);
1668 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1674 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1676 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1678 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1680 *status = nfserr_reclaim_bad;
1682 open->op_stateowner->so_confirmed = 1;
1683 open->op_stateowner->so_seqid--;
1689 * Attempt to hand out a delegation.
1692 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1694 struct nfs4_delegation *dp;
1695 struct nfs4_stateowner *sop = stp->st_stateowner;
1696 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1697 struct file_lock fl, *flp = &fl;
1698 int status, flag = 0;
1700 flag = NFS4_OPEN_DELEGATE_NONE;
1701 if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1702 || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1705 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1706 flag = NFS4_OPEN_DELEGATE_WRITE;
1708 flag = NFS4_OPEN_DELEGATE_READ;
1710 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1712 flag = NFS4_OPEN_DELEGATE_NONE;
1715 locks_init_lock(&fl);
1716 fl.fl_lmops = &nfsd_lease_mng_ops;
1717 fl.fl_flags = FL_LEASE;
1718 fl.fl_end = OFFSET_MAX;
1719 fl.fl_owner = (fl_owner_t)dp;
1720 fl.fl_file = stp->st_vfs_file;
1721 fl.fl_pid = current->tgid;
1723 /* setlease checks to see if delegation should be handed out.
1724 * the lock_manager callbacks fl_mylease and fl_change are used
1726 if ((status = setlease(stp->st_vfs_file,
1727 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1728 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1729 unhash_delegation(dp);
1730 flag = NFS4_OPEN_DELEGATE_NONE;
1734 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1736 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1737 dp->dl_stateid.si_boot,
1738 dp->dl_stateid.si_stateownerid,
1739 dp->dl_stateid.si_fileid,
1740 dp->dl_stateid.si_generation);
1742 open->op_delegate_type = flag;
1746 * called with nfs4_lock_state() held.
1749 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1751 struct nfs4_file *fp = NULL;
1752 struct inode *ino = current_fh->fh_dentry->d_inode;
1753 struct nfs4_stateid *stp = NULL;
1754 struct nfs4_delegation *dp = NULL;
1757 status = nfserr_inval;
1758 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1761 * Lookup file; if found, lookup stateid and check open request,
1762 * and check for delegations in the process of being recalled.
1763 * If not found, create the nfs4_file struct
1765 fp = find_file(ino);
1767 if ((status = nfs4_check_open(fp, open, &stp)))
1769 status = nfs4_check_deleg(fp, open, &dp);
1773 status = nfserr_bad_stateid;
1774 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1776 status = nfserr_resource;
1777 fp = alloc_init_file(ino);
1783 * OPEN the file, or upgrade an existing OPEN.
1784 * If truncate fails, the OPEN fails.
1787 /* Stateid was found, this is an OPEN upgrade */
1788 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1792 /* Stateid was not found, this is a new OPEN */
1794 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1798 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1801 init_stateid(stp, fp, open);
1802 status = nfsd4_truncate(rqstp, current_fh, open);
1804 release_stateid(stp, OPEN_STATE);
1808 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1811 * Attempt to hand out a delegation. No error return, because the
1812 * OPEN succeeds even if we fail.
1814 nfs4_open_delegation(current_fh, open, stp);
1818 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1819 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1820 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1822 /* take the opportunity to clean up unused state */
1823 if (fp && list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile))
1826 /* CLAIM_PREVIOUS has different error returns */
1827 nfs4_set_claim_prev(open, &status);
1829 * To finish the open response, we just need to set the rflags.
1831 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1832 if (!open->op_stateowner->so_confirmed)
1833 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1838 static struct work_struct laundromat_work;
1839 static void laundromat_main(void *);
1840 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1843 nfsd4_renew(clientid_t *clid)
1845 struct nfs4_client *clp;
1849 dprintk("process_renew(%08x/%08x): starting\n",
1850 clid->cl_boot, clid->cl_id);
1851 status = nfserr_stale_clientid;
1852 if (STALE_CLIENTID(clid))
1854 clp = find_confirmed_client(clid);
1855 status = nfserr_expired;
1857 /* We assume the client took too long to RENEW. */
1858 dprintk("nfsd4_renew: clientid not found!\n");
1862 status = nfserr_cb_path_down;
1863 if (!list_empty(&clp->cl_del_perclnt)
1864 && !atomic_read(&clp->cl_callback.cb_set))
1868 nfs4_unlock_state();
1873 nfs4_laundromat(void)
1875 struct nfs4_client *clp;
1876 struct nfs4_stateowner *sop;
1877 struct nfs4_delegation *dp;
1878 struct list_head *pos, *next, reaplist;
1879 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1880 time_t t, clientid_val = NFSD_LEASE_TIME;
1881 time_t u, test_val = NFSD_LEASE_TIME;
1885 dprintk("NFSD: laundromat service - starting\n");
1886 list_for_each_safe(pos, next, &client_lru) {
1887 clp = list_entry(pos, struct nfs4_client, cl_lru);
1888 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1889 t = clp->cl_time - cutoff;
1890 if (clientid_val > t)
1894 dprintk("NFSD: purging unused client (clientid %08x)\n",
1895 clp->cl_clientid.cl_id);
1898 INIT_LIST_HEAD(&reaplist);
1899 spin_lock(&recall_lock);
1900 list_for_each_safe(pos, next, &del_recall_lru) {
1901 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1902 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1903 u = dp->dl_time - cutoff;
1908 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1910 list_move(&dp->dl_recall_lru, &reaplist);
1912 spin_unlock(&recall_lock);
1913 list_for_each_safe(pos, next, &reaplist) {
1914 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1915 list_del_init(&dp->dl_recall_lru);
1916 unhash_delegation(dp);
1918 test_val = NFSD_LEASE_TIME;
1919 list_for_each_safe(pos, next, &close_lru) {
1920 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1921 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1922 u = sop->so_time - cutoff;
1927 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1929 list_del(&sop->so_close_lru);
1930 nfs4_put_stateowner(sop);
1932 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1933 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1934 nfs4_unlock_state();
1935 return clientid_val;
1939 laundromat_main(void *not_used)
1943 t = nfs4_laundromat();
1944 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1945 schedule_delayed_work(&laundromat_work, t*HZ);
1948 /* search ownerid_hashtbl[] and close_lru for stateid owner
1949 * (stateid->si_stateownerid)
1951 struct nfs4_stateowner *
1952 find_openstateowner_id(u32 st_id, int flags) {
1953 struct nfs4_stateowner *local = NULL;
1955 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1956 if (flags & CLOSE_STATE) {
1957 list_for_each_entry(local, &close_lru, so_close_lru) {
1958 if (local->so_id == st_id)
1966 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1968 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1972 STALE_STATEID(stateid_t *stateid)
1974 if (stateid->si_boot == boot_time)
1976 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1977 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1978 stateid->si_generation);
1983 access_permit_read(unsigned long access_bmap)
1985 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1986 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1987 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1991 access_permit_write(unsigned long access_bmap)
1993 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1994 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1998 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2000 int status = nfserr_openmode;
2002 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2004 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2012 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2014 /* Trying to call delegreturn with a special stateid? Yuch: */
2015 if (!(flags & (RD_STATE | WR_STATE)))
2016 return nfserr_bad_stateid;
2017 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2019 else if (nfs4_in_grace()) {
2020 /* Answer in remaining cases depends on existance of
2021 * conflicting state; so we must wait out the grace period. */
2022 return nfserr_grace;
2023 } else if (flags & WR_STATE)
2024 return nfs4_share_conflict(current_fh,
2025 NFS4_SHARE_DENY_WRITE);
2026 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2027 return nfs4_share_conflict(current_fh,
2028 NFS4_SHARE_DENY_READ);
2032 * Allow READ/WRITE during grace period on recovered state only for files
2033 * that are not able to provide mandatory locking.
2036 io_during_grace_disallowed(struct inode *inode, int flags)
2038 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2039 && MANDATORY_LOCK(inode);
2043 * Checks for stateid operations
2046 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2048 struct nfs4_stateid *stp = NULL;
2049 struct nfs4_delegation *dp = NULL;
2051 struct inode *ino = current_fh->fh_dentry->d_inode;
2054 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2055 stateid->si_boot, stateid->si_stateownerid,
2056 stateid->si_fileid, stateid->si_generation);
2060 if (io_during_grace_disallowed(ino, flags))
2061 return nfserr_grace;
2063 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2064 return check_special_stateids(current_fh, stateid, flags);
2067 status = nfserr_stale_stateid;
2068 if (STALE_STATEID(stateid))
2072 status = nfserr_bad_stateid;
2073 if (!stateid->si_fileid) { /* delegation stateid */
2074 if(!(dp = find_delegation_stateid(ino, stateid))) {
2075 dprintk("NFSD: delegation stateid not found\n");
2076 if (nfs4_in_grace())
2077 status = nfserr_grace;
2080 stidp = &dp->dl_stateid;
2081 } else { /* open or lock stateid */
2082 if (!(stp = find_stateid(stateid, flags))) {
2083 dprintk("NFSD: open or lock stateid not found\n");
2084 if (nfs4_in_grace())
2085 status = nfserr_grace;
2088 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2090 if (!stp->st_stateowner->so_confirmed)
2092 stidp = &stp->st_stateid;
2094 if (stateid->si_generation > stidp->si_generation)
2098 status = nfserr_old_stateid;
2099 if (stateid->si_generation < stidp->si_generation)
2102 if ((status = nfs4_check_openmode(stp,flags)))
2104 renew_client(stp->st_stateowner->so_client);
2106 *filpp = stp->st_vfs_file;
2108 if ((status = nfs4_check_delegmode(dp, flags)))
2110 renew_client(dp->dl_client);
2111 if (flags & DELEG_RET)
2112 unhash_delegation(dp);
2114 *filpp = dp->dl_vfs_file;
2123 * Checks for sequence id mutating operations.
2126 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)
2129 struct nfs4_stateid *stp;
2130 struct nfs4_stateowner *sop;
2132 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2133 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2134 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2135 stateid->si_generation);
2140 status = nfserr_bad_stateid;
2141 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2142 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2146 status = nfserr_stale_stateid;
2147 if (STALE_STATEID(stateid))
2150 * We return BAD_STATEID if filehandle doesn't match stateid,
2151 * the confirmed flag is incorrecly set, or the generation
2152 * number is incorrect.
2153 * If there is no entry in the openfile table for this id,
2154 * we can't always return BAD_STATEID;
2155 * this might be a retransmitted CLOSE which has arrived after
2156 * the openfile has been released.
2158 if (!(stp = find_stateid(stateid, flags)))
2159 goto no_nfs4_stateid;
2161 status = nfserr_bad_stateid;
2163 /* for new lock stateowners:
2164 * check that the lock->v.new.open_stateid
2165 * refers to an open stateowner
2167 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2168 * as the open_stateid->st_stateowner->so_client->clientid
2171 struct nfs4_stateowner *sop = stp->st_stateowner;
2172 struct nfs4_client *clp = sop->so_client;
2174 if (!sop->so_is_open_owner)
2176 if (!cmp_clid(&clp->cl_clientid, lockclid))
2180 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2181 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2186 *sopp = sop = stp->st_stateowner;
2189 * We now validate the seqid and stateid generation numbers.
2190 * For the moment, we ignore the possibility of
2191 * generation number wraparound.
2193 if (seqid != sop->so_seqid + 1)
2196 if (sop->so_confirmed) {
2197 if (flags & CONFIRM) {
2198 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2203 if (!(flags & CONFIRM)) {
2204 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2208 if (stateid->si_generation > stp->st_stateid.si_generation) {
2209 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2213 status = nfserr_old_stateid;
2214 if (stateid->si_generation < stp->st_stateid.si_generation) {
2215 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2218 /* XXX renew the client lease here */
2227 * We determine whether this is a bad stateid or a replay,
2228 * starting by trying to look up the stateowner.
2229 * If stateowner is not found - stateid is bad.
2231 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2232 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2233 status = nfserr_bad_stateid;
2239 if (seqid == sop->so_seqid) {
2240 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2241 /* indicate replay to calling function */
2242 status = NFSERR_REPLAY_ME;
2244 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2247 status = nfserr_bad_seqid;
2253 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2256 struct nfs4_stateowner *sop;
2257 struct nfs4_stateid *stp;
2259 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2260 (int)current_fh->fh_dentry->d_name.len,
2261 current_fh->fh_dentry->d_name.name);
2263 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2268 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2269 &oc->oc_req_stateid,
2270 CHECK_FH | CONFIRM | OPEN_STATE,
2271 &oc->oc_stateowner, &stp, NULL)))
2274 sop = oc->oc_stateowner;
2275 sop->so_confirmed = 1;
2276 update_stateid(&stp->st_stateid);
2277 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2278 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2279 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2280 stp->st_stateid.si_boot,
2281 stp->st_stateid.si_stateownerid,
2282 stp->st_stateid.si_fileid,
2283 stp->st_stateid.si_generation);
2285 if (oc->oc_stateowner)
2286 nfs4_get_stateowner(oc->oc_stateowner);
2287 nfs4_unlock_state();
2293 * unset all bits in union bitmap (bmap) that
2294 * do not exist in share (from successful OPEN_DOWNGRADE)
2297 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2300 for (i = 1; i < 4; i++) {
2301 if ((i & access) != i)
2302 __clear_bit(i, bmap);
2307 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2310 for (i = 0; i < 4; i++) {
2311 if ((i & deny) != i)
2312 __clear_bit(i, bmap);
2317 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2320 struct nfs4_stateid *stp;
2321 unsigned int share_access;
2323 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2324 (int)current_fh->fh_dentry->d_name.len,
2325 current_fh->fh_dentry->d_name.name);
2327 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2328 return nfserr_inval;
2331 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2333 CHECK_FH | OPEN_STATE,
2334 &od->od_stateowner, &stp, NULL)))
2337 status = nfserr_inval;
2338 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2339 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2340 stp->st_access_bmap, od->od_share_access);
2343 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2344 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2345 stp->st_deny_bmap, od->od_share_deny);
2348 set_access(&share_access, stp->st_access_bmap);
2349 nfs4_file_downgrade(stp->st_vfs_file,
2350 share_access & ~od->od_share_access);
2352 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2353 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2355 update_stateid(&stp->st_stateid);
2356 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2359 if (od->od_stateowner)
2360 nfs4_get_stateowner(od->od_stateowner);
2361 nfs4_unlock_state();
2366 * nfs4_unlock_state() called after encode
2369 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2372 struct nfs4_stateid *stp;
2374 dprintk("NFSD: nfsd4_close on file %.*s\n",
2375 (int)current_fh->fh_dentry->d_name.len,
2376 current_fh->fh_dentry->d_name.name);
2379 /* check close_lru for replay */
2380 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2382 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2383 &close->cl_stateowner, &stp, NULL)))
2386 * Return success, but first update the stateid.
2389 update_stateid(&stp->st_stateid);
2390 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2392 /* release_state_owner() calls nfsd_close() if needed */
2393 release_state_owner(stp, OPEN_STATE);
2395 if (close->cl_stateowner)
2396 nfs4_get_stateowner(close->cl_stateowner);
2397 nfs4_unlock_state();
2402 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2406 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2410 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2411 nfs4_unlock_state();
2418 * Lock owner state (byte-range locks)
2420 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2421 #define LOCK_HASH_BITS 8
2422 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2423 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2425 #define lockownerid_hashval(id) \
2426 ((id) & LOCK_HASH_MASK)
2428 static inline unsigned int
2429 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2430 struct xdr_netobj *ownername)
2432 return (file_hashval(inode) + cl_id
2433 + opaque_hashval(ownername->data, ownername->len))
2437 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2438 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2439 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2441 struct nfs4_stateid *
2442 find_stateid(stateid_t *stid, int flags)
2444 struct nfs4_stateid *local = NULL;
2445 u32 st_id = stid->si_stateownerid;
2446 u32 f_id = stid->si_fileid;
2447 unsigned int hashval;
2449 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2450 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2451 hashval = stateid_hashval(st_id, f_id);
2452 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2453 if ((local->st_stateid.si_stateownerid == st_id) &&
2454 (local->st_stateid.si_fileid == f_id))
2458 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2459 hashval = stateid_hashval(st_id, f_id);
2460 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2461 if ((local->st_stateid.si_stateownerid == st_id) &&
2462 (local->st_stateid.si_fileid == f_id))
2466 printk("NFSD: find_stateid: ERROR: no state flag\n");
2470 static struct nfs4_delegation *
2471 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2473 struct nfs4_file *fp = NULL;
2475 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2476 stid->si_boot, stid->si_stateownerid,
2477 stid->si_fileid, stid->si_generation);
2479 fp = find_file(ino);
2481 return find_delegation_file(fp, stid);
2486 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2487 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2488 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2489 * locking, this prevents us from being completely protocol-compliant. The
2490 * real solution to this problem is to start using unsigned file offsets in
2491 * the VFS, but this is a very deep change!
2494 nfs4_transform_lock_offset(struct file_lock *lock)
2496 if (lock->fl_start < 0)
2497 lock->fl_start = OFFSET_MAX;
2498 if (lock->fl_end < 0)
2499 lock->fl_end = OFFSET_MAX;
2503 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2505 struct nfs4_stateowner *local = NULL;
2508 if (hashval >= LOCK_HASH_SIZE)
2510 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2522 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2524 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2525 unsigned int hval = lockownerid_hashval(sop->so_id);
2527 deny->ld_sop = NULL;
2528 if (nfs4_verify_lock_stateowner(sop, hval)) {
2529 kref_get(&sop->so_ref);
2531 deny->ld_clientid = sop->so_client->cl_clientid;
2533 deny->ld_start = fl->fl_start;
2534 deny->ld_length = ~(u64)0;
2535 if (fl->fl_end != ~(u64)0)
2536 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2537 deny->ld_type = NFS4_READ_LT;
2538 if (fl->fl_type != F_RDLCK)
2539 deny->ld_type = NFS4_WRITE_LT;
2542 static struct nfs4_stateowner *
2543 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2545 struct nfs4_stateowner *local = NULL;
2548 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2549 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2550 if (!cmp_owner_str(local, owner, clid))
2558 static struct nfs4_stateowner *
2559 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2560 struct xdr_netobj *owner)
2562 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2563 struct nfs4_stateowner *op;
2565 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2566 if (cmp_owner_str(op, owner, clid))
2573 * Alloc a lock owner structure.
2574 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2577 * strhashval = lock_ownerstr_hashval
2578 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2581 static struct nfs4_stateowner *
2582 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2583 struct nfs4_stateowner *sop;
2584 struct nfs4_replay *rp;
2585 unsigned int idhashval;
2587 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2589 idhashval = lockownerid_hashval(current_ownerid);
2590 INIT_LIST_HEAD(&sop->so_idhash);
2591 INIT_LIST_HEAD(&sop->so_strhash);
2592 INIT_LIST_HEAD(&sop->so_perclient);
2593 INIT_LIST_HEAD(&sop->so_perfilestate);
2594 INIT_LIST_HEAD(&sop->so_perlockowner);
2595 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2597 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2598 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2599 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2600 sop->so_is_open_owner = 0;
2601 sop->so_id = current_ownerid++;
2602 sop->so_client = clp;
2603 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2604 sop->so_confirmed = 1;
2605 rp = &sop->so_replay;
2606 rp->rp_status = NFSERR_SERVERFAULT;
2608 rp->rp_buf = rp->rp_ibuf;
2612 struct nfs4_stateid *
2613 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2615 struct nfs4_stateid *stp;
2616 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2618 if ((stp = kmalloc(sizeof(struct nfs4_stateid),
2619 GFP_KERNEL)) == NULL)
2621 INIT_LIST_HEAD(&stp->st_hash);
2622 INIT_LIST_HEAD(&stp->st_perfile);
2623 INIT_LIST_HEAD(&stp->st_perfilestate);
2624 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2625 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2626 list_add(&stp->st_perfile, &fp->fi_perfile);
2628 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2629 stp->st_stateowner = sop;
2631 stp->st_stateid.si_boot = boot_time;
2632 stp->st_stateid.si_stateownerid = sop->so_id;
2633 stp->st_stateid.si_fileid = fp->fi_id;
2634 stp->st_stateid.si_generation = 0;
2635 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2636 stp->st_access_bmap = open_stp->st_access_bmap;
2637 stp->st_deny_bmap = open_stp->st_deny_bmap;
2644 check_lock_length(u64 offset, u64 length)
2646 return ((length == 0) || ((length != ~(u64)0) &&
2647 LOFF_OVERFLOW(offset, length)));
2654 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2656 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2657 struct nfs4_stateid *lock_stp;
2659 struct file_lock file_lock;
2660 struct file_lock *conflock;
2662 unsigned int strhashval;
2664 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2665 (long long) lock->lk_offset,
2666 (long long) lock->lk_length);
2668 if (nfs4_in_grace() && !lock->lk_reclaim)
2669 return nfserr_grace;
2670 if (!nfs4_in_grace() && lock->lk_reclaim)
2671 return nfserr_no_grace;
2673 if (check_lock_length(lock->lk_offset, lock->lk_length))
2674 return nfserr_inval;
2678 if (lock->lk_is_new) {
2680 * Client indicates that this is a new lockowner.
2681 * Use open owner and open stateid to create lock owner and lock
2684 struct nfs4_stateid *open_stp = NULL;
2685 struct nfs4_file *fp;
2687 status = nfserr_stale_clientid;
2688 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2689 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2693 /* is the new lock seqid presented by the client zero? */
2694 status = nfserr_bad_seqid;
2695 if (lock->v.new.lock_seqid != 0)
2698 /* validate and update open stateid and open seqid */
2699 status = nfs4_preprocess_seqid_op(current_fh,
2700 lock->lk_new_open_seqid,
2701 &lock->lk_new_open_stateid,
2702 CHECK_FH | OPEN_STATE,
2703 &open_sop, &open_stp,
2704 &lock->v.new.clientid);
2706 if (lock->lk_reclaim)
2707 status = nfserr_reclaim_bad;
2710 /* create lockowner and lock stateid */
2711 fp = open_stp->st_file;
2712 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2713 open_sop->so_client->cl_clientid.cl_id,
2714 &lock->v.new.owner);
2716 * If we already have this lock owner, the client is in
2717 * error (or our bookeeping is wrong!)
2718 * for asking for a 'new lock'.
2720 status = nfserr_bad_stateid;
2721 lock_sop = find_lockstateowner(&lock->v.new.owner,
2722 &lock->v.new.clientid);
2725 status = nfserr_resource;
2726 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2728 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2729 fp, open_stp)) == NULL) {
2730 release_stateowner(lock->lk_stateowner);
2731 lock->lk_stateowner = NULL;
2734 /* bump the open seqid used to create the lock */
2735 open_sop->so_seqid++;
2737 /* lock (lock owner + lock stateid) already exists */
2738 status = nfs4_preprocess_seqid_op(current_fh,
2739 lock->lk_old_lock_seqid,
2740 &lock->lk_old_lock_stateid,
2741 CHECK_FH | LOCK_STATE,
2742 &lock->lk_stateowner, &lock_stp, NULL);
2746 /* lock->lk_stateowner and lock_stp have been created or found */
2747 filp = lock_stp->st_vfs_file;
2749 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2750 printk("NFSD: nfsd4_lock: permission denied!\n");
2754 locks_init_lock(&file_lock);
2755 switch (lock->lk_type) {
2758 file_lock.fl_type = F_RDLCK;
2761 case NFS4_WRITEW_LT:
2762 file_lock.fl_type = F_WRLCK;
2765 status = nfserr_inval;
2768 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2769 file_lock.fl_pid = current->tgid;
2770 file_lock.fl_file = filp;
2771 file_lock.fl_flags = FL_POSIX;
2773 file_lock.fl_start = lock->lk_offset;
2774 if ((lock->lk_length == ~(u64)0) ||
2775 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2776 file_lock.fl_end = ~(u64)0;
2778 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2779 nfs4_transform_lock_offset(&file_lock);
2782 * Try to lock the file in the VFS.
2783 * Note: locks.c uses the BKL to protect the inode's lock list.
2786 status = posix_lock_file(filp, &file_lock);
2787 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2788 file_lock.fl_ops->fl_release_private(&file_lock);
2789 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2791 case 0: /* success! */
2792 update_stateid(&lock_stp->st_stateid);
2793 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2797 goto conflicting_lock;
2799 status = nfserr_deadlock;
2801 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2802 goto out_destroy_new_stateid;
2806 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2807 status = nfserr_denied;
2808 /* XXX There is a race here. Future patch needed to provide
2809 * an atomic posix_lock_and_test_file
2811 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2812 status = nfserr_serverfault;
2815 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2817 out_destroy_new_stateid:
2818 if (lock->lk_is_new) {
2819 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2821 * An error encountered after instantiation of the new
2822 * stateid has forced us to destroy it.
2824 if (!seqid_mutating_err(status))
2825 open_sop->so_seqid--;
2827 release_state_owner(lock_stp, LOCK_STATE);
2830 if (lock->lk_stateowner)
2831 nfs4_get_stateowner(lock->lk_stateowner);
2832 nfs4_unlock_state();
2840 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2842 struct inode *inode;
2844 struct file_lock file_lock;
2845 struct file_lock *conflicting_lock;
2848 if (nfs4_in_grace())
2849 return nfserr_grace;
2851 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2852 return nfserr_inval;
2854 lockt->lt_stateowner = NULL;
2857 status = nfserr_stale_clientid;
2858 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2859 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2863 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2864 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2865 if (status == nfserr_symlink)
2866 status = nfserr_inval;
2870 inode = current_fh->fh_dentry->d_inode;
2871 locks_init_lock(&file_lock);
2872 switch (lockt->lt_type) {
2875 file_lock.fl_type = F_RDLCK;
2878 case NFS4_WRITEW_LT:
2879 file_lock.fl_type = F_WRLCK;
2882 printk("NFSD: nfs4_lockt: bad lock type!\n");
2883 status = nfserr_inval;
2887 lockt->lt_stateowner = find_lockstateowner_str(inode,
2888 &lockt->lt_clientid, &lockt->lt_owner);
2889 if (lockt->lt_stateowner)
2890 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2891 file_lock.fl_pid = current->tgid;
2892 file_lock.fl_flags = FL_POSIX;
2894 file_lock.fl_start = lockt->lt_offset;
2895 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2896 file_lock.fl_end = ~(u64)0;
2898 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2900 nfs4_transform_lock_offset(&file_lock);
2902 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2903 * since LOCKT doesn't require an OPEN, and therefore a struct
2904 * file may not exist, pass posix_test_lock a struct file with
2905 * only the dentry:inode set.
2907 memset(&file, 0, sizeof (struct file));
2908 file.f_dentry = current_fh->fh_dentry;
2911 conflicting_lock = posix_test_lock(&file, &file_lock);
2912 if (conflicting_lock) {
2913 status = nfserr_denied;
2914 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2917 nfs4_unlock_state();
2922 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2924 struct nfs4_stateid *stp;
2925 struct file *filp = NULL;
2926 struct file_lock file_lock;
2929 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2930 (long long) locku->lu_offset,
2931 (long long) locku->lu_length);
2933 if (check_lock_length(locku->lu_offset, locku->lu_length))
2934 return nfserr_inval;
2938 if ((status = nfs4_preprocess_seqid_op(current_fh,
2941 CHECK_FH | LOCK_STATE,
2942 &locku->lu_stateowner, &stp, NULL)))
2945 filp = stp->st_vfs_file;
2947 locks_init_lock(&file_lock);
2948 file_lock.fl_type = F_UNLCK;
2949 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2950 file_lock.fl_pid = current->tgid;
2951 file_lock.fl_file = filp;
2952 file_lock.fl_flags = FL_POSIX;
2953 file_lock.fl_start = locku->lu_offset;
2955 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2956 file_lock.fl_end = ~(u64)0;
2958 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2959 nfs4_transform_lock_offset(&file_lock);
2962 * Try to unlock the file in the VFS.
2964 status = posix_lock_file(filp, &file_lock);
2965 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2966 file_lock.fl_ops->fl_release_private(&file_lock);
2968 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2972 * OK, unlock succeeded; the only thing left to do is update the stateid.
2974 update_stateid(&stp->st_stateid);
2975 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2978 if (locku->lu_stateowner)
2979 nfs4_get_stateowner(locku->lu_stateowner);
2980 nfs4_unlock_state();
2984 status = nfserrno(status);
2990 * 1: locks held by lockowner
2991 * 0: no locks held by lockowner
2994 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2996 struct file_lock **flpp;
2997 struct inode *inode = filp->f_dentry->d_inode;
3001 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3002 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3012 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3014 clientid_t *clid = &rlockowner->rl_clientid;
3015 struct nfs4_stateowner *local = NULL;
3016 struct xdr_netobj *owner = &rlockowner->rl_owner;
3019 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3020 clid->cl_boot, clid->cl_id);
3022 /* XXX check for lease expiration */
3024 status = nfserr_stale_clientid;
3025 if (STALE_CLIENTID(clid)) {
3026 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3033 local = find_lockstateowner(owner, clid);
3035 struct nfs4_stateid *stp;
3037 /* check for any locks held by any stateid
3038 * associated with the (lock) stateowner */
3039 status = nfserr_locks_held;
3040 list_for_each_entry(stp, &local->so_perfilestate,
3042 if (check_for_locks(stp->st_vfs_file, local))
3045 /* no locks held by (lock) stateowner */
3047 release_stateowner(local);
3050 nfs4_unlock_state();
3054 static inline struct nfs4_client_reclaim *
3055 alloc_reclaim(int namelen)
3057 struct nfs4_client_reclaim *crp = NULL;
3059 crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3062 crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3063 if (!crp->cr_name.data) {
3071 * failure => all reset bets are off, nfserr_no_grace...
3074 nfs4_client_to_reclaim(char *name, int namlen)
3076 unsigned int strhashval;
3077 struct nfs4_client_reclaim *crp = NULL;
3079 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3080 crp = alloc_reclaim(namlen);
3083 strhashval = clientstr_hashval(name, namlen);
3084 INIT_LIST_HEAD(&crp->cr_strhash);
3085 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3086 memcpy(crp->cr_name.data, name, namlen);
3087 crp->cr_name.len = namlen;
3088 reclaim_str_hashtbl_size++;
3093 nfs4_release_reclaim(void)
3095 struct nfs4_client_reclaim *crp = NULL;
3098 BUG_ON(!nfs4_reclaim_init);
3099 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3100 while (!list_empty(&reclaim_str_hashtbl[i])) {
3101 crp = list_entry(reclaim_str_hashtbl[i].next,
3102 struct nfs4_client_reclaim, cr_strhash);
3103 list_del(&crp->cr_strhash);
3104 kfree(crp->cr_name.data);
3106 reclaim_str_hashtbl_size--;
3109 BUG_ON(reclaim_str_hashtbl_size);
3113 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3114 struct nfs4_client_reclaim *
3115 nfs4_find_reclaim_client(clientid_t *clid)
3117 unsigned int strhashval;
3118 struct nfs4_client *clp;
3119 struct nfs4_client_reclaim *crp = NULL;
3122 /* find clientid in conf_id_hashtbl */
3123 clp = find_confirmed_client(clid);
3127 dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3128 clp->cl_name.len, clp->cl_name.data);
3130 /* find clp->cl_name in reclaim_str_hashtbl */
3131 strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3132 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3133 if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3141 * Called from OPEN. Look for clientid in reclaim list.
3144 nfs4_check_open_reclaim(clientid_t *clid)
3146 struct nfs4_client_reclaim *crp;
3148 if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
3149 return nfserr_reclaim_bad;
3155 * Start and stop routines
3159 __nfs4_state_init(void)
3164 if (!nfs4_reclaim_init) {
3165 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3166 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3167 reclaim_str_hashtbl_size = 0;
3168 nfs4_reclaim_init = 1;
3170 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3171 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3172 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3173 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3174 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3176 for (i = 0; i < FILE_HASH_SIZE; i++) {
3177 INIT_LIST_HEAD(&file_hashtbl[i]);
3179 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3180 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3181 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3183 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3184 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3185 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3187 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3188 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3189 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3191 memset(&zerostateid, 0, sizeof(stateid_t));
3192 memset(&onestateid, ~0, sizeof(stateid_t));
3194 INIT_LIST_HEAD(&close_lru);
3195 INIT_LIST_HEAD(&client_lru);
3196 INIT_LIST_HEAD(&del_recall_lru);
3197 spin_lock_init(&recall_lock);
3198 boot_time = get_seconds();
3199 grace_time = max(old_lease_time, lease_time);
3200 if (reclaim_str_hashtbl_size == 0)
3203 printk("NFSD: starting %ld-second grace period\n", grace_time);
3204 grace_end = boot_time + grace_time;
3205 INIT_WORK(&laundromat_work,laundromat_main, NULL);
3206 schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3210 nfs4_state_init(void)
3216 status = nfsd4_init_slabs();
3219 __nfs4_state_init();
3227 return get_seconds() < grace_end;
3233 printk("NFSD: ERROR in reboot recovery. State reclaims will fail.\n");
3234 grace_end = get_seconds();
3238 nfs4_lease_time(void)
3244 __nfs4_state_shutdown(void)
3247 struct nfs4_client *clp = NULL;
3248 struct nfs4_delegation *dp = NULL;
3249 struct nfs4_stateowner *sop = NULL;
3250 struct list_head *pos, *next, reaplist;
3252 list_for_each_safe(pos, next, &close_lru) {
3253 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3254 list_del(&sop->so_close_lru);
3255 nfs4_put_stateowner(sop);
3258 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3259 while (!list_empty(&conf_id_hashtbl[i])) {
3260 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3263 while (!list_empty(&unconf_str_hashtbl[i])) {
3264 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3268 INIT_LIST_HEAD(&reaplist);
3269 spin_lock(&recall_lock);
3270 list_for_each_safe(pos, next, &del_recall_lru) {
3271 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3272 list_move(&dp->dl_recall_lru, &reaplist);
3274 spin_unlock(&recall_lock);
3275 list_for_each_safe(pos, next, &reaplist) {
3276 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3277 list_del_init(&dp->dl_recall_lru);
3278 unhash_delegation(dp);
3281 release_all_files();
3282 cancel_delayed_work(&laundromat_work);
3283 flush_scheduled_work();
3285 dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
3286 list_add_perfile, list_del_perfile);
3287 dprintk("NFSD: add_perclient %d del_perclient %d\n",
3288 add_perclient, del_perclient);
3289 dprintk("NFSD: alloc_file %d free_file %d\n",
3290 alloc_file, free_file);
3291 dprintk("NFSD: vfsopen %d vfsclose %d\n",
3293 dprintk("NFSD: alloc_delegation %d free_delegation %d\n",
3294 alloc_delegation, free_delegation);
3299 nfs4_state_shutdown(void)
3302 nfs4_release_reclaim();
3303 __nfs4_state_shutdown();
3305 nfs4_unlock_state();
3309 * Called when leasetime is changed.
3311 * if nfsd is not started, simply set the global lease.
3313 * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3314 * e.g: boot_time is reset, existing nfs4_client structs are
3315 * used to fill reclaim_str_hashtbl, then all state (except for the
3316 * reclaim_str_hashtbl) is re-initialized.
3318 * if the old lease time is greater than the new lease time, the grace
3319 * period needs to be set to the old lease time to allow clients to reclaim
3320 * their state. XXX - we may want to set the grace period == lease time
3321 * after an initial grace period == old lease time
3323 * if an error occurs in this process, the new lease is set, but the server
3324 * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3325 * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3327 * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3328 * OPEN and LOCK reclaims.
3331 nfs4_reset_lease(time_t leasetime)
3333 struct nfs4_client *clp;
3336 printk("NFSD: New leasetime %ld\n",leasetime);
3340 old_lease_time = lease_time;
3341 lease_time = leasetime;
3343 nfs4_release_reclaim();
3345 /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3346 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3347 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3348 if (!nfs4_client_to_reclaim(clp->cl_name.data,
3349 clp->cl_name.len)) {
3350 nfs4_release_reclaim();
3356 __nfs4_state_shutdown();
3357 __nfs4_state_init();
3358 nfs4_unlock_state();