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;
75 u32 alloc_delegation= 0;
76 u32 free_delegation= 0;
78 /* forward declarations */
79 struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
80 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
81 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
86 * protects clientid_hashtbl[], clientstr_hashtbl[],
87 * unconfstr_hashtbl[], uncofid_hashtbl[].
89 static DECLARE_MUTEX(client_sema);
91 kmem_cache_t *stateowner_slab = NULL;
92 kmem_cache_t *file_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 if ((dp = kmalloc(sizeof(struct nfs4_delegation),
141 GFP_KERNEL)) == NULL)
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);
171 nfs4_put_delegation(struct nfs4_delegation *dp)
173 if (atomic_dec_and_test(&dp->dl_count)) {
174 dprintk("NFSD: freeing dp %p\n",dp);
180 /* Remove the associated file_lock first, then remove the delegation.
181 * lease_modify() is called to remove the FS_LEASE file_lock from
182 * the i_flock list, eventually calling nfsd's lock_manager
183 * fl_release_callback.
186 nfs4_close_delegation(struct nfs4_delegation *dp)
188 struct file *filp = dp->dl_vfs_file;
190 dprintk("NFSD: close_delegation dp %p\n",dp);
191 dp->dl_vfs_file = NULL;
192 /* The following nfsd_close may not actually close the file,
193 * but we want to remove the lease in any case. */
195 setlease(filp, F_UNLCK, &dp->dl_flock);
200 /* Called under the state lock. */
202 unhash_delegation(struct nfs4_delegation *dp)
204 list_del_init(&dp->dl_del_perfile);
205 list_del_init(&dp->dl_del_perclnt);
206 spin_lock(&recall_lock);
207 list_del_init(&dp->dl_recall_lru);
208 spin_unlock(&recall_lock);
209 nfs4_close_delegation(dp);
210 nfs4_put_delegation(dp);
217 /* Hash tables for nfs4_clientid state */
218 #define CLIENT_HASH_BITS 4
219 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
220 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
222 #define clientid_hashval(id) \
223 ((id) & CLIENT_HASH_MASK)
224 #define clientstr_hashval(name, namelen) \
225 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
227 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
228 * used in reboot/reset lease grace period processing
230 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
231 * setclientid_confirmed info.
233 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
236 * client_lru holds client queue ordered by nfs4_client.cl_time
239 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
240 * for last close replay.
242 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
243 static int reclaim_str_hashtbl_size = 0;
244 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
245 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
246 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
247 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
248 static struct list_head client_lru;
249 static struct list_head close_lru;
252 renew_client(struct nfs4_client *clp)
255 * Move client to the end to the LRU list.
257 dprintk("renewing client (clientid %08x/%08x)\n",
258 clp->cl_clientid.cl_boot,
259 clp->cl_clientid.cl_id);
260 list_move_tail(&clp->cl_lru, &client_lru);
261 clp->cl_time = get_seconds();
264 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
266 STALE_CLIENTID(clientid_t *clid)
268 if (clid->cl_boot == boot_time)
270 dprintk("NFSD stale clientid (%08x/%08x)\n",
271 clid->cl_boot, clid->cl_id);
276 * XXX Should we use a slab cache ?
277 * This type of memory management is somewhat inefficient, but we use it
278 * anyway since SETCLIENTID is not a common operation.
280 static inline struct nfs4_client *
281 alloc_client(struct xdr_netobj name)
283 struct nfs4_client *clp;
285 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
286 memset(clp, 0, sizeof(*clp));
287 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
288 memcpy(clp->cl_name.data, name.data, name.len);
289 clp->cl_name.len = name.len;
300 free_client(struct nfs4_client *clp)
302 if (clp->cl_cred.cr_group_info)
303 put_group_info(clp->cl_cred.cr_group_info);
304 kfree(clp->cl_name.data);
309 put_nfs4_client(struct nfs4_client *clp)
311 if (atomic_dec_and_test(&clp->cl_count))
316 expire_client(struct nfs4_client *clp)
318 struct nfs4_stateowner *sop;
319 struct nfs4_delegation *dp;
320 struct nfs4_callback *cb = &clp->cl_callback;
321 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
322 struct list_head reaplist;
324 dprintk("NFSD: expire_client cl_count %d\n",
325 atomic_read(&clp->cl_count));
327 /* shutdown rpc client, ending any outstanding recall rpcs */
328 if (atomic_read(&cb->cb_set) == 1 && clnt) {
329 rpc_shutdown_client(clnt);
330 clnt = clp->cl_callback.cb_client = NULL;
333 INIT_LIST_HEAD(&reaplist);
334 spin_lock(&recall_lock);
335 while (!list_empty(&clp->cl_del_perclnt)) {
336 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
337 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
339 list_del_init(&dp->dl_del_perclnt);
340 list_move(&dp->dl_recall_lru, &reaplist);
342 spin_unlock(&recall_lock);
343 while (!list_empty(&reaplist)) {
344 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
345 list_del_init(&dp->dl_recall_lru);
346 unhash_delegation(dp);
348 list_del(&clp->cl_idhash);
349 list_del(&clp->cl_strhash);
350 list_del(&clp->cl_lru);
351 while (!list_empty(&clp->cl_perclient)) {
352 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
353 release_stateowner(sop);
355 put_nfs4_client(clp);
358 static struct nfs4_client *
359 create_client(struct xdr_netobj name) {
360 struct nfs4_client *clp;
362 if (!(clp = alloc_client(name)))
364 atomic_set(&clp->cl_count, 1);
365 atomic_set(&clp->cl_callback.cb_set, 0);
366 clp->cl_callback.cb_parsed = 0;
367 INIT_LIST_HEAD(&clp->cl_idhash);
368 INIT_LIST_HEAD(&clp->cl_strhash);
369 INIT_LIST_HEAD(&clp->cl_perclient);
370 INIT_LIST_HEAD(&clp->cl_del_perclnt);
371 INIT_LIST_HEAD(&clp->cl_lru);
377 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
378 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
382 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
383 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
384 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
388 copy_cred(struct svc_cred *target, struct svc_cred *source) {
390 target->cr_uid = source->cr_uid;
391 target->cr_gid = source->cr_gid;
392 target->cr_group_info = source->cr_group_info;
393 get_group_info(target->cr_group_info);
397 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
400 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
404 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
405 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
409 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
410 return((cl1->cl_boot == cl2->cl_boot) &&
411 (cl1->cl_id == cl2->cl_id));
414 /* XXX what about NGROUP */
416 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
417 return(cr1->cr_uid == cr2->cr_uid);
422 gen_clid(struct nfs4_client *clp) {
423 clp->cl_clientid.cl_boot = boot_time;
424 clp->cl_clientid.cl_id = current_clientid++;
428 gen_confirm(struct nfs4_client *clp) {
433 p = (u32 *)clp->cl_confirm.data;
439 check_name(struct xdr_netobj name) {
443 if (name.len > NFS4_OPAQUE_LIMIT) {
444 printk("NFSD: check_name: name too long(%d)!\n", name.len);
451 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
453 unsigned int idhashval;
455 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
456 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
457 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
458 list_add_tail(&clp->cl_lru, &client_lru);
459 clp->cl_time = get_seconds();
463 move_to_confirmed(struct nfs4_client *clp)
465 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
466 unsigned int strhashval;
468 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
469 list_del_init(&clp->cl_strhash);
470 list_del_init(&clp->cl_idhash);
471 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
472 strhashval = clientstr_hashval(clp->cl_name.data,
474 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
478 static struct nfs4_client *
479 find_confirmed_client(clientid_t *clid)
481 struct nfs4_client *clp;
482 unsigned int idhashval = clientid_hashval(clid->cl_id);
484 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
485 if (cmp_clid(&clp->cl_clientid, clid))
491 static struct nfs4_client *
492 find_unconfirmed_client(clientid_t *clid)
494 struct nfs4_client *clp;
495 unsigned int idhashval = clientid_hashval(clid->cl_id);
497 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
498 if (cmp_clid(&clp->cl_clientid, clid))
504 /* a helper function for parse_callback */
506 parse_octet(unsigned int *lenp, char **addrp)
508 unsigned int len = *lenp;
520 if ((c < '0') || (c > '9')) {
526 n = (n * 10) + (c - '0');
537 /* parse and set the setclientid ipv4 callback address */
539 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
544 u32 addrlen = addr_len;
545 char *addr = addr_val;
550 for(i = 4; i > 0 ; i--) {
551 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
554 cbaddr |= (temp << shift);
562 for(i = 2; i > 0 ; i--) {
563 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
566 cbport |= (temp << shift);
575 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
577 struct nfs4_callback *cb = &clp->cl_callback;
579 /* Currently, we only support tcp for the callback channel */
580 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
583 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
584 &cb->cb_addr, &cb->cb_port)))
586 cb->cb_prog = se->se_callback_prog;
587 cb->cb_ident = se->se_callback_ident;
591 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
592 "will not receive delegations\n",
593 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
600 * RFC 3010 has a complex implmentation description of processing a
601 * SETCLIENTID request consisting of 5 bullets, labeled as
602 * CASE0 - CASE4 below.
605 * callback information will be processed in a future patch
607 * an unconfirmed record is added when:
608 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
609 * CASE 1: confirmed record found with matching name, principal,
610 * verifier, and clientid.
611 * CASE 2: confirmed record found with matching name, principal,
612 * and there is no unconfirmed record with matching
615 * an unconfirmed record is replaced when:
616 * CASE 3: confirmed record found with matching name, principal,
617 * and an unconfirmed record is found with matching
618 * name, principal, and with clientid and
619 * confirm that does not match the confirmed record.
620 * CASE 4: there is no confirmed record with matching name and
621 * principal. there is an unconfirmed record with
622 * matching name, principal.
624 * an unconfirmed record is deleted when:
625 * CASE 1: an unconfirmed record that matches input name, verifier,
626 * and confirmed clientid.
627 * CASE 4: any unconfirmed records with matching name and principal
628 * that exist after an unconfirmed record has been replaced
629 * as described above.
633 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
635 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
636 struct xdr_netobj clname = {
637 .len = setclid->se_namelen,
638 .data = setclid->se_name,
640 nfs4_verifier clverifier = setclid->se_verf;
641 unsigned int strhashval;
642 struct nfs4_client * conf, * unconf, * new, * clp;
645 status = nfserr_inval;
646 if (!check_name(clname))
650 * XXX The Duplicate Request Cache (DRC) has been checked (??)
651 * We get here on a DRC miss.
654 strhashval = clientstr_hashval(clname.data, clname.len);
658 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
659 if (!cmp_name(&clp->cl_name, &clname))
663 * clname match, confirmed, different principal
664 * or different ip_address
666 status = nfserr_clid_inuse;
667 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
668 printk("NFSD: setclientid: string in use by client"
669 "(clientid %08x/%08x)\n",
670 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
673 if (clp->cl_addr != ip_addr) {
674 printk("NFSD: setclientid: string in use by client"
675 "(clientid %08x/%08x)\n",
676 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
681 * cl_name match from a previous SETCLIENTID operation
682 * XXX check for additional matches?
688 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
689 if (!cmp_name(&clp->cl_name, &clname))
691 /* cl_name match from a previous SETCLIENTID operation */
695 status = nfserr_resource;
699 * placed first, because it is the normal case.
702 expire_client(unconf);
703 if (!(new = create_client(clname)))
705 copy_verf(new, &clverifier);
706 new->cl_addr = ip_addr;
707 copy_cred(&new->cl_cred,&rqstp->rq_cred);
710 gen_callback(new, setclid);
711 add_to_unconfirmed(new, strhashval);
712 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
715 * cl_name match, confirmed, principal match
716 * verifier match: probable callback update
718 * remove any unconfirmed nfs4_client with
719 * matching cl_name, cl_verifier, and cl_clientid
721 * create and insert an unconfirmed nfs4_client with same
722 * cl_name, cl_verifier, and cl_clientid as existing
723 * nfs4_client, but with the new callback info and a
727 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
728 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
729 expire_client(unconf);
731 if (!(new = create_client(clname)))
733 copy_verf(new,&conf->cl_verifier);
734 new->cl_addr = ip_addr;
735 copy_cred(&new->cl_cred,&rqstp->rq_cred);
736 copy_clid(new, conf);
738 gen_callback(new, setclid);
739 add_to_unconfirmed(new,strhashval);
740 } else if (!unconf) {
743 * clname match, confirmed, principal match
744 * verfier does not match
745 * no unconfirmed. create a new unconfirmed nfs4_client
746 * using input clverifier, clname, and callback info
747 * and generate a new cl_clientid and cl_confirm.
749 if (!(new = create_client(clname)))
751 copy_verf(new,&clverifier);
752 new->cl_addr = ip_addr;
753 copy_cred(&new->cl_cred,&rqstp->rq_cred);
756 gen_callback(new, setclid);
757 add_to_unconfirmed(new, strhashval);
758 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
761 * confirmed found (name, principal match)
762 * confirmed verifier does not match input clverifier
764 * unconfirmed found (name match)
765 * confirmed->cl_confirm != unconfirmed->cl_confirm
767 * remove unconfirmed.
769 * create an unconfirmed nfs4_client
770 * with same cl_name as existing confirmed nfs4_client,
771 * but with new callback info, new cl_clientid,
772 * new cl_verifier and a new cl_confirm
774 expire_client(unconf);
775 if (!(new = create_client(clname)))
777 copy_verf(new,&clverifier);
778 new->cl_addr = ip_addr;
779 copy_cred(&new->cl_cred,&rqstp->rq_cred);
782 gen_callback(new, setclid);
783 add_to_unconfirmed(new, strhashval);
785 /* No cases hit !!! */
786 status = nfserr_inval;
790 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
791 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
792 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
801 * RFC 3010 has a complex implmentation description of processing a
802 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
803 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
805 * NOTE: callback information will be processed here in a future patch
808 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
810 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
811 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
812 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
813 clientid_t * clid = &setclientid_confirm->sc_clientid;
816 if (STALE_CLIENTID(clid))
817 return nfserr_stale_clientid;
819 * XXX The Duplicate Request Cache (DRC) has been checked (??)
820 * We get here on a DRC miss.
824 clp = find_confirmed_client(clid);
826 status = nfserr_inval;
828 * Found a record for this clientid. If the IP addresses
829 * don't match, return ERR_INVAL just as if the record had
832 if (clp->cl_addr != ip_addr) {
833 printk("NFSD: setclientid: string in use by client"
834 "(clientid %08x/%08x)\n",
835 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
840 clp = find_unconfirmed_client(clid);
842 status = nfserr_inval;
843 if (clp->cl_addr != ip_addr) {
844 printk("NFSD: setclientid: string in use by client"
845 "(clientid %08x/%08x)\n",
846 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
852 * unconf record that matches input clientid and input confirm.
853 * conf record that matches input clientid.
854 * conf and unconf records match names, verifiers
856 if ((conf && unconf) &&
857 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
858 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
859 (cmp_name(&conf->cl_name,&unconf->cl_name)) &&
860 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
861 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
862 status = nfserr_clid_inuse;
866 move_to_confirmed(unconf);
872 * conf record that matches input clientid.
873 * if unconf record that matches input clientid, then unconf->cl_name
874 * or unconf->cl_verifier don't match the conf record.
876 if ((conf && !unconf) ||
878 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
879 !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
880 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
881 status = nfserr_clid_inuse;
889 * conf record not found.
890 * unconf record found.
891 * unconf->cl_confirm matches input confirm
893 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
894 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
895 status = nfserr_clid_inuse;
899 move_to_confirmed(unconf);
904 * conf record not found, or if conf, then conf->cl_confirm does not
905 * match input confirm.
906 * unconf record not found, or if unconf, then unconf->cl_confirm
907 * does not match input confirm.
909 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
910 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
911 status = nfserr_stale_clientid;
914 /* check that we have hit one of the cases...*/
915 status = nfserr_inval;
919 nfsd4_probe_callback(clp);
925 * Open owner state (share locks)
928 /* hash tables for nfs4_stateowner */
929 #define OWNER_HASH_BITS 8
930 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
931 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
933 #define ownerid_hashval(id) \
934 ((id) & OWNER_HASH_MASK)
935 #define ownerstr_hashval(clientid, ownername) \
936 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
938 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
939 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
941 /* hash table for nfs4_file */
942 #define FILE_HASH_BITS 8
943 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
944 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
945 /* hash table for (open)nfs4_stateid */
946 #define STATEID_HASH_BITS 10
947 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
948 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
950 #define file_hashval(x) \
951 hash_ptr(x, FILE_HASH_BITS)
952 #define stateid_hashval(owner_id, file_id) \
953 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
955 static struct list_head file_hashtbl[FILE_HASH_SIZE];
956 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
958 /* OPEN Share state helper functions */
959 static inline struct nfs4_file *
960 alloc_init_file(struct inode *ino)
962 struct nfs4_file *fp;
963 unsigned int hashval = file_hashval(ino);
965 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
967 INIT_LIST_HEAD(&fp->fi_hash);
968 INIT_LIST_HEAD(&fp->fi_perfile);
969 INIT_LIST_HEAD(&fp->fi_del_perfile);
970 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
971 fp->fi_inode = igrab(ino);
972 fp->fi_id = current_fileid++;
979 release_all_files(void)
982 struct nfs4_file *fp;
984 for (i=0;i<FILE_HASH_SIZE;i++) {
985 while (!list_empty(&file_hashtbl[i])) {
986 fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
987 /* this should never be more than once... */
988 if (!list_empty(&fp->fi_perfile) || !list_empty(&fp->fi_del_perfile)) {
989 printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
997 nfsd4_free_slab(kmem_cache_t **slab)
1003 status = kmem_cache_destroy(*slab);
1009 nfsd4_free_slabs(void)
1011 nfsd4_free_slab(&stateowner_slab);
1012 nfsd4_free_slab(&file_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)
1029 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1034 nfs4_free_stateowner(struct kref *kref)
1036 struct nfs4_stateowner *sop =
1037 container_of(kref, struct nfs4_stateowner, so_ref);
1038 kfree(sop->so_owner.data);
1039 kmem_cache_free(stateowner_slab, sop);
1042 static inline struct nfs4_stateowner *
1043 alloc_stateowner(struct xdr_netobj *owner)
1045 struct nfs4_stateowner *sop;
1047 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1048 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1049 memcpy(sop->so_owner.data, owner->data, owner->len);
1050 sop->so_owner.len = owner->len;
1051 kref_init(&sop->so_ref);
1054 kmem_cache_free(stateowner_slab, sop);
1059 static struct nfs4_stateowner *
1060 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1061 struct nfs4_stateowner *sop;
1062 struct nfs4_replay *rp;
1063 unsigned int idhashval;
1065 if (!(sop = alloc_stateowner(&open->op_owner)))
1067 idhashval = ownerid_hashval(current_ownerid);
1068 INIT_LIST_HEAD(&sop->so_idhash);
1069 INIT_LIST_HEAD(&sop->so_strhash);
1070 INIT_LIST_HEAD(&sop->so_perclient);
1071 INIT_LIST_HEAD(&sop->so_perfilestate);
1072 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1073 INIT_LIST_HEAD(&sop->so_close_lru);
1075 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1076 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1077 list_add(&sop->so_perclient, &clp->cl_perclient);
1079 sop->so_is_open_owner = 1;
1080 sop->so_id = current_ownerid++;
1081 sop->so_client = clp;
1082 sop->so_seqid = open->op_seqid;
1083 sop->so_confirmed = 0;
1084 rp = &sop->so_replay;
1085 rp->rp_status = NFSERR_SERVERFAULT;
1087 rp->rp_buf = rp->rp_ibuf;
1092 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1094 struct nfs4_stateowner *lock_sop;
1096 while (!list_empty(&open_stp->st_perlockowner)) {
1097 lock_sop = list_entry(open_stp->st_perlockowner.next,
1098 struct nfs4_stateowner, so_perlockowner);
1099 /* list_del(&open_stp->st_perlockowner); */
1100 BUG_ON(lock_sop->so_is_open_owner);
1101 release_stateowner(lock_sop);
1106 unhash_stateowner(struct nfs4_stateowner *sop)
1108 struct nfs4_stateid *stp;
1110 list_del(&sop->so_idhash);
1111 list_del(&sop->so_strhash);
1112 if (sop->so_is_open_owner) {
1113 list_del(&sop->so_perclient);
1116 list_del(&sop->so_perlockowner);
1117 while (!list_empty(&sop->so_perfilestate)) {
1118 stp = list_entry(sop->so_perfilestate.next,
1119 struct nfs4_stateid, st_perfilestate);
1120 if (sop->so_is_open_owner)
1121 release_stateid(stp, OPEN_STATE);
1123 release_stateid(stp, LOCK_STATE);
1128 release_stateowner(struct nfs4_stateowner *sop)
1130 unhash_stateowner(sop);
1131 list_del(&sop->so_close_lru);
1132 nfs4_put_stateowner(sop);
1136 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1137 struct nfs4_stateowner *sop = open->op_stateowner;
1138 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1140 INIT_LIST_HEAD(&stp->st_hash);
1141 INIT_LIST_HEAD(&stp->st_perfilestate);
1142 INIT_LIST_HEAD(&stp->st_perlockowner);
1143 INIT_LIST_HEAD(&stp->st_perfile);
1144 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1145 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1147 list_add(&stp->st_perfile, &fp->fi_perfile);
1148 stp->st_stateowner = sop;
1150 stp->st_stateid.si_boot = boot_time;
1151 stp->st_stateid.si_stateownerid = sop->so_id;
1152 stp->st_stateid.si_fileid = fp->fi_id;
1153 stp->st_stateid.si_generation = 0;
1154 stp->st_access_bmap = 0;
1155 stp->st_deny_bmap = 0;
1156 __set_bit(open->op_share_access, &stp->st_access_bmap);
1157 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1161 release_stateid(struct nfs4_stateid *stp, int flags)
1163 struct file *filp = stp->st_vfs_file;
1165 list_del(&stp->st_hash);
1167 list_del(&stp->st_perfile);
1168 list_del(&stp->st_perfilestate);
1169 if (flags & OPEN_STATE) {
1170 release_stateid_lockowners(stp);
1171 stp->st_vfs_file = NULL;
1174 } else if (flags & LOCK_STATE)
1175 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1181 release_file(struct nfs4_file *fp)
1183 list_del(&fp->fi_hash);
1185 kmem_cache_free(file_slab, fp);
1189 move_to_close_lru(struct nfs4_stateowner *sop)
1191 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1193 unhash_stateowner(sop);
1194 list_add_tail(&sop->so_close_lru, &close_lru);
1195 sop->so_time = get_seconds();
1199 release_state_owner(struct nfs4_stateid *stp, int flag)
1201 struct nfs4_stateowner *sop = stp->st_stateowner;
1202 struct nfs4_file *fp = stp->st_file;
1204 dprintk("NFSD: release_state_owner\n");
1205 release_stateid(stp, flag);
1207 /* place unused nfs4_stateowners on so_close_lru list to be
1208 * released by the laundromat service after the lease period
1209 * to enable us to handle CLOSE replay
1211 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1212 move_to_close_lru(sop);
1213 /* unused nfs4_file's are releseed. XXX slab cache? */
1214 if (list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile)) {
1220 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1221 return ((sop->so_owner.len == owner->len) &&
1222 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1223 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1226 static struct nfs4_stateowner *
1227 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1229 struct nfs4_stateowner *so = NULL;
1231 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1232 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1238 /* search file_hashtbl[] for file */
1239 static struct nfs4_file *
1240 find_file(struct inode *ino)
1242 unsigned int hashval = file_hashval(ino);
1243 struct nfs4_file *fp;
1245 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1246 if (fp->fi_inode == ino)
1252 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1253 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1256 set_access(unsigned int *access, unsigned long bmap) {
1260 for (i = 1; i < 4; i++) {
1261 if (test_bit(i, &bmap))
1267 set_deny(unsigned int *deny, unsigned long bmap) {
1271 for (i = 0; i < 4; i++) {
1272 if (test_bit(i, &bmap))
1278 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1279 unsigned int access, deny;
1281 set_access(&access, stp->st_access_bmap);
1282 set_deny(&deny, stp->st_deny_bmap);
1283 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1289 * Called to check deny when READ with all zero stateid or
1290 * WRITE with all zero or all one stateid
1293 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1295 struct inode *ino = current_fh->fh_dentry->d_inode;
1296 struct nfs4_file *fp;
1297 struct nfs4_stateid *stp;
1299 dprintk("NFSD: nfs4_share_conflict\n");
1301 fp = find_file(ino);
1303 /* Search for conflicting share reservations */
1304 list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1305 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1306 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1307 return nfserr_share_denied;
1314 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1316 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1317 put_write_access(filp->f_dentry->d_inode);
1318 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1323 * Recall a delegation
1326 do_recall(void *__dp)
1328 struct nfs4_delegation *dp = __dp;
1330 daemonize("nfsv4-recall");
1332 nfsd4_cb_recall(dp);
1337 * Spawn a thread to perform a recall on the delegation represented
1338 * by the lease (file_lock)
1340 * Called from break_lease() with lock_kernel() held.
1341 * Note: we assume break_lease will only call this *once* for any given
1345 void nfsd_break_deleg_cb(struct file_lock *fl)
1347 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1348 struct task_struct *t;
1350 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1354 /* We're assuming the state code never drops its reference
1355 * without first removing the lease. Since we're in this lease
1356 * callback (and since the lease code is serialized by the kernel
1357 * lock) we know the server hasn't removed the lease yet, we know
1358 * it's safe to take a reference: */
1359 atomic_inc(&dp->dl_count);
1361 spin_lock(&recall_lock);
1362 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1363 spin_unlock(&recall_lock);
1365 /* only place dl_time is set. protected by lock_kernel*/
1366 dp->dl_time = get_seconds();
1368 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1369 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1371 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1373 struct nfs4_client *clp = dp->dl_client;
1375 printk(KERN_INFO "NFSD: Callback thread failed for "
1376 "for client (clientid %08x/%08x)\n",
1377 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1378 nfs4_put_delegation(dp);
1383 * The file_lock is being reapd.
1385 * Called by locks_free_lock() with lock_kernel() held.
1388 void nfsd_release_deleg_cb(struct file_lock *fl)
1390 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1392 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1394 if (!(fl->fl_flags & FL_LEASE) || !dp)
1396 dp->dl_flock = NULL;
1400 * Set the delegation file_lock back pointer.
1402 * Called from __setlease() with lock_kernel() held.
1405 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1407 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1409 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1416 * Called from __setlease() with lock_kernel() held
1419 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1421 struct nfs4_delegation *onlistd =
1422 (struct nfs4_delegation *)onlist->fl_owner;
1423 struct nfs4_delegation *tryd =
1424 (struct nfs4_delegation *)try->fl_owner;
1426 if (onlist->fl_lmops != try->fl_lmops)
1429 return onlistd->dl_client == tryd->dl_client;
1434 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1437 return lease_modify(onlist, arg);
1442 struct lock_manager_operations nfsd_lease_mng_ops = {
1443 .fl_break = nfsd_break_deleg_cb,
1444 .fl_release_private = nfsd_release_deleg_cb,
1445 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1446 .fl_mylease = nfsd_same_client_deleg_cb,
1447 .fl_change = nfsd_change_deleg_cb,
1452 * nfsd4_process_open1()
1453 * lookup stateowner.
1465 * called with nfs4_lock_state() held.
1468 nfsd4_process_open1(struct nfsd4_open *open)
1471 clientid_t *clientid = &open->op_clientid;
1472 struct nfs4_client *clp = NULL;
1473 unsigned int strhashval;
1474 struct nfs4_stateowner *sop = NULL;
1476 status = nfserr_inval;
1477 if (!check_name(open->op_owner))
1480 if (STALE_CLIENTID(&open->op_clientid))
1481 return nfserr_stale_clientid;
1483 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1484 sop = find_openstateowner_str(strhashval, open);
1486 open->op_stateowner = sop;
1487 /* check for replay */
1488 if (open->op_seqid == sop->so_seqid){
1489 if (sop->so_replay.rp_buflen)
1490 return NFSERR_REPLAY_ME;
1492 /* The original OPEN failed so spectacularly
1493 * that we don't even have replay data saved!
1494 * Therefore, we have no choice but to continue
1495 * processing this OPEN; presumably, we'll
1496 * fail again for the same reason.
1498 dprintk("nfsd4_process_open1:"
1499 " replay with no replay cache\n");
1502 } else if (sop->so_confirmed) {
1503 if (open->op_seqid == sop->so_seqid + 1)
1505 status = nfserr_bad_seqid;
1508 /* If we get here, we received an OPEN for an
1509 * unconfirmed nfs4_stateowner. Since the seqid's are
1510 * different, purge the existing nfs4_stateowner, and
1511 * instantiate a new one.
1513 clp = sop->so_client;
1514 release_stateowner(sop);
1517 /* nfs4_stateowner not found.
1518 * Verify clientid and instantiate new nfs4_stateowner.
1519 * If verify fails this is presumably the result of the
1520 * client's lease expiring.
1522 status = nfserr_expired;
1523 clp = find_confirmed_client(clientid);
1527 status = nfserr_resource;
1528 sop = alloc_init_open_stateowner(strhashval, clp, open);
1531 open->op_stateowner = sop;
1534 renew_client(sop->so_client);
1536 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1537 status = nfserr_reclaim_bad;
1542 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1544 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1545 return nfserr_openmode;
1550 static struct nfs4_delegation *
1551 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1553 struct nfs4_delegation *dp;
1555 list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
1556 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1563 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1564 struct nfs4_delegation **dp)
1567 int status = nfserr_bad_stateid;
1569 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1572 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1573 RD_STATE : WR_STATE;
1574 status = nfs4_check_delegmode(*dp, flags);
1578 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1582 open->op_stateowner->so_confirmed = 1;
1587 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1589 struct nfs4_stateid *local;
1590 int status = nfserr_share_denied;
1591 struct nfs4_stateowner *sop = open->op_stateowner;
1593 list_for_each_entry(local, &fp->fi_perfile, st_perfile) {
1594 /* ignore lock owners */
1595 if (local->st_stateowner->so_is_open_owner == 0)
1597 /* remember if we have seen this open owner */
1598 if (local->st_stateowner == sop)
1600 /* check for conflicting share reservations */
1601 if (!test_share(local, open))
1610 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1611 struct nfs4_delegation *dp,
1612 struct svc_fh *cur_fh, int flags)
1614 struct nfs4_stateid *stp;
1616 stp = kmalloc(sizeof(struct nfs4_stateid), GFP_KERNEL);
1618 return nfserr_resource;
1621 get_file(dp->dl_vfs_file);
1622 stp->st_vfs_file = dp->dl_vfs_file;
1625 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1628 if (status == nfserr_dropit)
1629 status = nfserr_jukebox;
1640 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1641 struct nfsd4_open *open)
1643 struct iattr iattr = {
1644 .ia_valid = ATTR_SIZE,
1647 if (!open->op_truncate)
1649 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1651 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1655 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1657 struct file *filp = stp->st_vfs_file;
1658 struct inode *inode = filp->f_dentry->d_inode;
1659 unsigned int share_access;
1662 set_access(&share_access, stp->st_access_bmap);
1663 share_access = ~share_access;
1664 share_access &= open->op_share_access;
1666 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1667 return nfsd4_truncate(rqstp, cur_fh, open);
1669 status = get_write_access(inode);
1671 return nfserrno(status);
1672 status = nfsd4_truncate(rqstp, cur_fh, open);
1674 put_write_access(inode);
1677 /* remember the open */
1678 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1679 set_bit(open->op_share_access, &stp->st_access_bmap);
1680 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1686 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1688 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1690 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1692 *status = nfserr_reclaim_bad;
1694 open->op_stateowner->so_confirmed = 1;
1695 open->op_stateowner->so_seqid--;
1701 * Attempt to hand out a delegation.
1704 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1706 struct nfs4_delegation *dp;
1707 struct nfs4_stateowner *sop = stp->st_stateowner;
1708 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1709 struct file_lock fl, *flp = &fl;
1710 int status, flag = 0;
1712 flag = NFS4_OPEN_DELEGATE_NONE;
1713 if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1714 || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1717 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1718 flag = NFS4_OPEN_DELEGATE_WRITE;
1720 flag = NFS4_OPEN_DELEGATE_READ;
1722 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1724 flag = NFS4_OPEN_DELEGATE_NONE;
1727 locks_init_lock(&fl);
1728 fl.fl_lmops = &nfsd_lease_mng_ops;
1729 fl.fl_flags = FL_LEASE;
1730 fl.fl_end = OFFSET_MAX;
1731 fl.fl_owner = (fl_owner_t)dp;
1732 fl.fl_file = stp->st_vfs_file;
1733 fl.fl_pid = current->tgid;
1735 /* setlease checks to see if delegation should be handed out.
1736 * the lock_manager callbacks fl_mylease and fl_change are used
1738 if ((status = setlease(stp->st_vfs_file,
1739 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1740 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1741 unhash_delegation(dp);
1742 flag = NFS4_OPEN_DELEGATE_NONE;
1746 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1748 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1749 dp->dl_stateid.si_boot,
1750 dp->dl_stateid.si_stateownerid,
1751 dp->dl_stateid.si_fileid,
1752 dp->dl_stateid.si_generation);
1754 open->op_delegate_type = flag;
1758 * called with nfs4_lock_state() held.
1761 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1763 struct nfs4_file *fp = NULL;
1764 struct inode *ino = current_fh->fh_dentry->d_inode;
1765 struct nfs4_stateid *stp = NULL;
1766 struct nfs4_delegation *dp = NULL;
1769 status = nfserr_inval;
1770 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1773 * Lookup file; if found, lookup stateid and check open request,
1774 * and check for delegations in the process of being recalled.
1775 * If not found, create the nfs4_file struct
1777 fp = find_file(ino);
1779 if ((status = nfs4_check_open(fp, open, &stp)))
1781 status = nfs4_check_deleg(fp, open, &dp);
1785 status = nfserr_bad_stateid;
1786 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1788 status = nfserr_resource;
1789 fp = alloc_init_file(ino);
1795 * OPEN the file, or upgrade an existing OPEN.
1796 * If truncate fails, the OPEN fails.
1799 /* Stateid was found, this is an OPEN upgrade */
1800 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1804 /* Stateid was not found, this is a new OPEN */
1806 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1810 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1813 init_stateid(stp, fp, open);
1814 status = nfsd4_truncate(rqstp, current_fh, open);
1816 release_stateid(stp, OPEN_STATE);
1820 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1823 * Attempt to hand out a delegation. No error return, because the
1824 * OPEN succeeds even if we fail.
1826 nfs4_open_delegation(current_fh, open, stp);
1830 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1831 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1832 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1834 /* take the opportunity to clean up unused state */
1835 if (fp && list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile))
1838 /* CLAIM_PREVIOUS has different error returns */
1839 nfs4_set_claim_prev(open, &status);
1841 * To finish the open response, we just need to set the rflags.
1843 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1844 if (!open->op_stateowner->so_confirmed)
1845 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1850 static struct work_struct laundromat_work;
1851 static void laundromat_main(void *);
1852 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1855 nfsd4_renew(clientid_t *clid)
1857 struct nfs4_client *clp;
1861 dprintk("process_renew(%08x/%08x): starting\n",
1862 clid->cl_boot, clid->cl_id);
1863 status = nfserr_stale_clientid;
1864 if (STALE_CLIENTID(clid))
1866 clp = find_confirmed_client(clid);
1867 status = nfserr_expired;
1869 /* We assume the client took too long to RENEW. */
1870 dprintk("nfsd4_renew: clientid not found!\n");
1874 status = nfserr_cb_path_down;
1875 if (!list_empty(&clp->cl_del_perclnt)
1876 && !atomic_read(&clp->cl_callback.cb_set))
1880 nfs4_unlock_state();
1885 nfs4_laundromat(void)
1887 struct nfs4_client *clp;
1888 struct nfs4_stateowner *sop;
1889 struct nfs4_delegation *dp;
1890 struct list_head *pos, *next, reaplist;
1891 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1892 time_t t, clientid_val = NFSD_LEASE_TIME;
1893 time_t u, test_val = NFSD_LEASE_TIME;
1897 dprintk("NFSD: laundromat service - starting\n");
1898 list_for_each_safe(pos, next, &client_lru) {
1899 clp = list_entry(pos, struct nfs4_client, cl_lru);
1900 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1901 t = clp->cl_time - cutoff;
1902 if (clientid_val > t)
1906 dprintk("NFSD: purging unused client (clientid %08x)\n",
1907 clp->cl_clientid.cl_id);
1910 INIT_LIST_HEAD(&reaplist);
1911 spin_lock(&recall_lock);
1912 list_for_each_safe(pos, next, &del_recall_lru) {
1913 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1914 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1915 u = dp->dl_time - cutoff;
1920 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1922 list_move(&dp->dl_recall_lru, &reaplist);
1924 spin_unlock(&recall_lock);
1925 list_for_each_safe(pos, next, &reaplist) {
1926 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1927 list_del_init(&dp->dl_recall_lru);
1928 unhash_delegation(dp);
1930 test_val = NFSD_LEASE_TIME;
1931 list_for_each_safe(pos, next, &close_lru) {
1932 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1933 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1934 u = sop->so_time - cutoff;
1939 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1941 list_del(&sop->so_close_lru);
1942 nfs4_put_stateowner(sop);
1944 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1945 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1946 nfs4_unlock_state();
1947 return clientid_val;
1951 laundromat_main(void *not_used)
1955 t = nfs4_laundromat();
1956 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1957 schedule_delayed_work(&laundromat_work, t*HZ);
1960 /* search ownerid_hashtbl[] and close_lru for stateid owner
1961 * (stateid->si_stateownerid)
1963 struct nfs4_stateowner *
1964 find_openstateowner_id(u32 st_id, int flags) {
1965 struct nfs4_stateowner *local = NULL;
1967 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1968 if (flags & CLOSE_STATE) {
1969 list_for_each_entry(local, &close_lru, so_close_lru) {
1970 if (local->so_id == st_id)
1978 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1980 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1984 STALE_STATEID(stateid_t *stateid)
1986 if (stateid->si_boot == boot_time)
1988 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1989 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1990 stateid->si_generation);
1995 access_permit_read(unsigned long access_bmap)
1997 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1998 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1999 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2003 access_permit_write(unsigned long access_bmap)
2005 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2006 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2010 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2012 int status = nfserr_openmode;
2014 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2016 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2024 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2026 /* Trying to call delegreturn with a special stateid? Yuch: */
2027 if (!(flags & (RD_STATE | WR_STATE)))
2028 return nfserr_bad_stateid;
2029 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2031 else if (nfs4_in_grace()) {
2032 /* Answer in remaining cases depends on existance of
2033 * conflicting state; so we must wait out the grace period. */
2034 return nfserr_grace;
2035 } else if (flags & WR_STATE)
2036 return nfs4_share_conflict(current_fh,
2037 NFS4_SHARE_DENY_WRITE);
2038 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2039 return nfs4_share_conflict(current_fh,
2040 NFS4_SHARE_DENY_READ);
2044 * Allow READ/WRITE during grace period on recovered state only for files
2045 * that are not able to provide mandatory locking.
2048 io_during_grace_disallowed(struct inode *inode, int flags)
2050 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2051 && MANDATORY_LOCK(inode);
2055 * Checks for stateid operations
2058 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2060 struct nfs4_stateid *stp = NULL;
2061 struct nfs4_delegation *dp = NULL;
2063 struct inode *ino = current_fh->fh_dentry->d_inode;
2066 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2067 stateid->si_boot, stateid->si_stateownerid,
2068 stateid->si_fileid, stateid->si_generation);
2072 if (io_during_grace_disallowed(ino, flags))
2073 return nfserr_grace;
2075 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2076 return check_special_stateids(current_fh, stateid, flags);
2079 status = nfserr_stale_stateid;
2080 if (STALE_STATEID(stateid))
2084 status = nfserr_bad_stateid;
2085 if (!stateid->si_fileid) { /* delegation stateid */
2086 if(!(dp = find_delegation_stateid(ino, stateid))) {
2087 dprintk("NFSD: delegation stateid not found\n");
2088 if (nfs4_in_grace())
2089 status = nfserr_grace;
2092 stidp = &dp->dl_stateid;
2093 } else { /* open or lock stateid */
2094 if (!(stp = find_stateid(stateid, flags))) {
2095 dprintk("NFSD: open or lock stateid not found\n");
2096 if (nfs4_in_grace())
2097 status = nfserr_grace;
2100 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2102 if (!stp->st_stateowner->so_confirmed)
2104 stidp = &stp->st_stateid;
2106 if (stateid->si_generation > stidp->si_generation)
2110 status = nfserr_old_stateid;
2111 if (stateid->si_generation < stidp->si_generation)
2114 if ((status = nfs4_check_openmode(stp,flags)))
2116 renew_client(stp->st_stateowner->so_client);
2118 *filpp = stp->st_vfs_file;
2120 if ((status = nfs4_check_delegmode(dp, flags)))
2122 renew_client(dp->dl_client);
2123 if (flags & DELEG_RET)
2124 unhash_delegation(dp);
2126 *filpp = dp->dl_vfs_file;
2135 * Checks for sequence id mutating operations.
2138 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)
2141 struct nfs4_stateid *stp;
2142 struct nfs4_stateowner *sop;
2144 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2145 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2146 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2147 stateid->si_generation);
2152 status = nfserr_bad_stateid;
2153 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2154 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2158 status = nfserr_stale_stateid;
2159 if (STALE_STATEID(stateid))
2162 * We return BAD_STATEID if filehandle doesn't match stateid,
2163 * the confirmed flag is incorrecly set, or the generation
2164 * number is incorrect.
2165 * If there is no entry in the openfile table for this id,
2166 * we can't always return BAD_STATEID;
2167 * this might be a retransmitted CLOSE which has arrived after
2168 * the openfile has been released.
2170 if (!(stp = find_stateid(stateid, flags)))
2171 goto no_nfs4_stateid;
2173 status = nfserr_bad_stateid;
2175 /* for new lock stateowners:
2176 * check that the lock->v.new.open_stateid
2177 * refers to an open stateowner
2179 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2180 * as the open_stateid->st_stateowner->so_client->clientid
2183 struct nfs4_stateowner *sop = stp->st_stateowner;
2184 struct nfs4_client *clp = sop->so_client;
2186 if (!sop->so_is_open_owner)
2188 if (!cmp_clid(&clp->cl_clientid, lockclid))
2192 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2193 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2198 *sopp = sop = stp->st_stateowner;
2201 * We now validate the seqid and stateid generation numbers.
2202 * For the moment, we ignore the possibility of
2203 * generation number wraparound.
2205 if (seqid != sop->so_seqid + 1)
2208 if (sop->so_confirmed) {
2209 if (flags & CONFIRM) {
2210 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2215 if (!(flags & CONFIRM)) {
2216 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2220 if (stateid->si_generation > stp->st_stateid.si_generation) {
2221 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2225 status = nfserr_old_stateid;
2226 if (stateid->si_generation < stp->st_stateid.si_generation) {
2227 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2230 /* XXX renew the client lease here */
2239 * We determine whether this is a bad stateid or a replay,
2240 * starting by trying to look up the stateowner.
2241 * If stateowner is not found - stateid is bad.
2243 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2244 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2245 status = nfserr_bad_stateid;
2251 if (seqid == sop->so_seqid) {
2252 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2253 /* indicate replay to calling function */
2254 status = NFSERR_REPLAY_ME;
2256 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2259 status = nfserr_bad_seqid;
2265 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2268 struct nfs4_stateowner *sop;
2269 struct nfs4_stateid *stp;
2271 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2272 (int)current_fh->fh_dentry->d_name.len,
2273 current_fh->fh_dentry->d_name.name);
2275 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2280 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2281 &oc->oc_req_stateid,
2282 CHECK_FH | CONFIRM | OPEN_STATE,
2283 &oc->oc_stateowner, &stp, NULL)))
2286 sop = oc->oc_stateowner;
2287 sop->so_confirmed = 1;
2288 update_stateid(&stp->st_stateid);
2289 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2290 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2291 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2292 stp->st_stateid.si_boot,
2293 stp->st_stateid.si_stateownerid,
2294 stp->st_stateid.si_fileid,
2295 stp->st_stateid.si_generation);
2297 if (oc->oc_stateowner)
2298 nfs4_get_stateowner(oc->oc_stateowner);
2299 nfs4_unlock_state();
2305 * unset all bits in union bitmap (bmap) that
2306 * do not exist in share (from successful OPEN_DOWNGRADE)
2309 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2312 for (i = 1; i < 4; i++) {
2313 if ((i & access) != i)
2314 __clear_bit(i, bmap);
2319 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2322 for (i = 0; i < 4; i++) {
2323 if ((i & deny) != i)
2324 __clear_bit(i, bmap);
2329 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2332 struct nfs4_stateid *stp;
2333 unsigned int share_access;
2335 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2336 (int)current_fh->fh_dentry->d_name.len,
2337 current_fh->fh_dentry->d_name.name);
2339 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2340 return nfserr_inval;
2343 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2345 CHECK_FH | OPEN_STATE,
2346 &od->od_stateowner, &stp, NULL)))
2349 status = nfserr_inval;
2350 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2351 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2352 stp->st_access_bmap, od->od_share_access);
2355 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2356 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2357 stp->st_deny_bmap, od->od_share_deny);
2360 set_access(&share_access, stp->st_access_bmap);
2361 nfs4_file_downgrade(stp->st_vfs_file,
2362 share_access & ~od->od_share_access);
2364 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2365 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2367 update_stateid(&stp->st_stateid);
2368 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2371 if (od->od_stateowner)
2372 nfs4_get_stateowner(od->od_stateowner);
2373 nfs4_unlock_state();
2378 * nfs4_unlock_state() called after encode
2381 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2384 struct nfs4_stateid *stp;
2386 dprintk("NFSD: nfsd4_close on file %.*s\n",
2387 (int)current_fh->fh_dentry->d_name.len,
2388 current_fh->fh_dentry->d_name.name);
2391 /* check close_lru for replay */
2392 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2394 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2395 &close->cl_stateowner, &stp, NULL)))
2398 * Return success, but first update the stateid.
2401 update_stateid(&stp->st_stateid);
2402 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2404 /* release_state_owner() calls nfsd_close() if needed */
2405 release_state_owner(stp, OPEN_STATE);
2407 if (close->cl_stateowner)
2408 nfs4_get_stateowner(close->cl_stateowner);
2409 nfs4_unlock_state();
2414 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2418 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2422 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2423 nfs4_unlock_state();
2430 * Lock owner state (byte-range locks)
2432 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2433 #define LOCK_HASH_BITS 8
2434 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2435 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2437 #define lockownerid_hashval(id) \
2438 ((id) & LOCK_HASH_MASK)
2440 static inline unsigned int
2441 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2442 struct xdr_netobj *ownername)
2444 return (file_hashval(inode) + cl_id
2445 + opaque_hashval(ownername->data, ownername->len))
2449 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2450 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2451 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2453 struct nfs4_stateid *
2454 find_stateid(stateid_t *stid, int flags)
2456 struct nfs4_stateid *local = NULL;
2457 u32 st_id = stid->si_stateownerid;
2458 u32 f_id = stid->si_fileid;
2459 unsigned int hashval;
2461 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2462 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2463 hashval = stateid_hashval(st_id, f_id);
2464 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2465 if ((local->st_stateid.si_stateownerid == st_id) &&
2466 (local->st_stateid.si_fileid == f_id))
2470 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2471 hashval = stateid_hashval(st_id, f_id);
2472 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2473 if ((local->st_stateid.si_stateownerid == st_id) &&
2474 (local->st_stateid.si_fileid == f_id))
2478 printk("NFSD: find_stateid: ERROR: no state flag\n");
2482 static struct nfs4_delegation *
2483 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2485 struct nfs4_file *fp = NULL;
2487 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2488 stid->si_boot, stid->si_stateownerid,
2489 stid->si_fileid, stid->si_generation);
2491 fp = find_file(ino);
2493 return find_delegation_file(fp, stid);
2498 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2499 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2500 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2501 * locking, this prevents us from being completely protocol-compliant. The
2502 * real solution to this problem is to start using unsigned file offsets in
2503 * the VFS, but this is a very deep change!
2506 nfs4_transform_lock_offset(struct file_lock *lock)
2508 if (lock->fl_start < 0)
2509 lock->fl_start = OFFSET_MAX;
2510 if (lock->fl_end < 0)
2511 lock->fl_end = OFFSET_MAX;
2515 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2517 struct nfs4_stateowner *local = NULL;
2520 if (hashval >= LOCK_HASH_SIZE)
2522 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2534 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2536 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2537 unsigned int hval = lockownerid_hashval(sop->so_id);
2539 deny->ld_sop = NULL;
2540 if (nfs4_verify_lock_stateowner(sop, hval)) {
2541 kref_get(&sop->so_ref);
2543 deny->ld_clientid = sop->so_client->cl_clientid;
2545 deny->ld_start = fl->fl_start;
2546 deny->ld_length = ~(u64)0;
2547 if (fl->fl_end != ~(u64)0)
2548 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2549 deny->ld_type = NFS4_READ_LT;
2550 if (fl->fl_type != F_RDLCK)
2551 deny->ld_type = NFS4_WRITE_LT;
2554 static struct nfs4_stateowner *
2555 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2557 struct nfs4_stateowner *local = NULL;
2560 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2561 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2562 if (!cmp_owner_str(local, owner, clid))
2570 static struct nfs4_stateowner *
2571 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2572 struct xdr_netobj *owner)
2574 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2575 struct nfs4_stateowner *op;
2577 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2578 if (cmp_owner_str(op, owner, clid))
2585 * Alloc a lock owner structure.
2586 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2589 * strhashval = lock_ownerstr_hashval
2590 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2593 static struct nfs4_stateowner *
2594 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2595 struct nfs4_stateowner *sop;
2596 struct nfs4_replay *rp;
2597 unsigned int idhashval;
2599 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2601 idhashval = lockownerid_hashval(current_ownerid);
2602 INIT_LIST_HEAD(&sop->so_idhash);
2603 INIT_LIST_HEAD(&sop->so_strhash);
2604 INIT_LIST_HEAD(&sop->so_perclient);
2605 INIT_LIST_HEAD(&sop->so_perfilestate);
2606 INIT_LIST_HEAD(&sop->so_perlockowner);
2607 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2609 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2610 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2611 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2612 sop->so_is_open_owner = 0;
2613 sop->so_id = current_ownerid++;
2614 sop->so_client = clp;
2615 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2616 sop->so_confirmed = 1;
2617 rp = &sop->so_replay;
2618 rp->rp_status = NFSERR_SERVERFAULT;
2620 rp->rp_buf = rp->rp_ibuf;
2624 struct nfs4_stateid *
2625 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2627 struct nfs4_stateid *stp;
2628 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2630 if ((stp = kmalloc(sizeof(struct nfs4_stateid),
2631 GFP_KERNEL)) == NULL)
2633 INIT_LIST_HEAD(&stp->st_hash);
2634 INIT_LIST_HEAD(&stp->st_perfile);
2635 INIT_LIST_HEAD(&stp->st_perfilestate);
2636 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2637 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2638 list_add(&stp->st_perfile, &fp->fi_perfile);
2640 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2641 stp->st_stateowner = sop;
2643 stp->st_stateid.si_boot = boot_time;
2644 stp->st_stateid.si_stateownerid = sop->so_id;
2645 stp->st_stateid.si_fileid = fp->fi_id;
2646 stp->st_stateid.si_generation = 0;
2647 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2648 stp->st_access_bmap = open_stp->st_access_bmap;
2649 stp->st_deny_bmap = open_stp->st_deny_bmap;
2656 check_lock_length(u64 offset, u64 length)
2658 return ((length == 0) || ((length != ~(u64)0) &&
2659 LOFF_OVERFLOW(offset, length)));
2666 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2668 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2669 struct nfs4_stateid *lock_stp;
2671 struct file_lock file_lock;
2672 struct file_lock *conflock;
2674 unsigned int strhashval;
2676 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2677 (long long) lock->lk_offset,
2678 (long long) lock->lk_length);
2680 if (nfs4_in_grace() && !lock->lk_reclaim)
2681 return nfserr_grace;
2682 if (!nfs4_in_grace() && lock->lk_reclaim)
2683 return nfserr_no_grace;
2685 if (check_lock_length(lock->lk_offset, lock->lk_length))
2686 return nfserr_inval;
2690 if (lock->lk_is_new) {
2692 * Client indicates that this is a new lockowner.
2693 * Use open owner and open stateid to create lock owner and lock
2696 struct nfs4_stateid *open_stp = NULL;
2697 struct nfs4_file *fp;
2699 status = nfserr_stale_clientid;
2700 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2701 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2705 /* is the new lock seqid presented by the client zero? */
2706 status = nfserr_bad_seqid;
2707 if (lock->v.new.lock_seqid != 0)
2710 /* validate and update open stateid and open seqid */
2711 status = nfs4_preprocess_seqid_op(current_fh,
2712 lock->lk_new_open_seqid,
2713 &lock->lk_new_open_stateid,
2714 CHECK_FH | OPEN_STATE,
2715 &open_sop, &open_stp,
2716 &lock->v.new.clientid);
2718 if (lock->lk_reclaim)
2719 status = nfserr_reclaim_bad;
2722 /* create lockowner and lock stateid */
2723 fp = open_stp->st_file;
2724 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2725 open_sop->so_client->cl_clientid.cl_id,
2726 &lock->v.new.owner);
2728 * If we already have this lock owner, the client is in
2729 * error (or our bookeeping is wrong!)
2730 * for asking for a 'new lock'.
2732 status = nfserr_bad_stateid;
2733 lock_sop = find_lockstateowner(&lock->v.new.owner,
2734 &lock->v.new.clientid);
2737 status = nfserr_resource;
2738 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2740 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2741 fp, open_stp)) == NULL) {
2742 release_stateowner(lock->lk_stateowner);
2743 lock->lk_stateowner = NULL;
2746 /* bump the open seqid used to create the lock */
2747 open_sop->so_seqid++;
2749 /* lock (lock owner + lock stateid) already exists */
2750 status = nfs4_preprocess_seqid_op(current_fh,
2751 lock->lk_old_lock_seqid,
2752 &lock->lk_old_lock_stateid,
2753 CHECK_FH | LOCK_STATE,
2754 &lock->lk_stateowner, &lock_stp, NULL);
2758 /* lock->lk_stateowner and lock_stp have been created or found */
2759 filp = lock_stp->st_vfs_file;
2761 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2762 printk("NFSD: nfsd4_lock: permission denied!\n");
2766 locks_init_lock(&file_lock);
2767 switch (lock->lk_type) {
2770 file_lock.fl_type = F_RDLCK;
2773 case NFS4_WRITEW_LT:
2774 file_lock.fl_type = F_WRLCK;
2777 status = nfserr_inval;
2780 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2781 file_lock.fl_pid = current->tgid;
2782 file_lock.fl_file = filp;
2783 file_lock.fl_flags = FL_POSIX;
2785 file_lock.fl_start = lock->lk_offset;
2786 if ((lock->lk_length == ~(u64)0) ||
2787 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2788 file_lock.fl_end = ~(u64)0;
2790 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2791 nfs4_transform_lock_offset(&file_lock);
2794 * Try to lock the file in the VFS.
2795 * Note: locks.c uses the BKL to protect the inode's lock list.
2798 status = posix_lock_file(filp, &file_lock);
2799 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2800 file_lock.fl_ops->fl_release_private(&file_lock);
2801 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2803 case 0: /* success! */
2804 update_stateid(&lock_stp->st_stateid);
2805 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2809 goto conflicting_lock;
2811 status = nfserr_deadlock;
2813 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2814 goto out_destroy_new_stateid;
2818 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2819 status = nfserr_denied;
2820 /* XXX There is a race here. Future patch needed to provide
2821 * an atomic posix_lock_and_test_file
2823 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2824 status = nfserr_serverfault;
2827 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2829 out_destroy_new_stateid:
2830 if (lock->lk_is_new) {
2831 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2833 * An error encountered after instantiation of the new
2834 * stateid has forced us to destroy it.
2836 if (!seqid_mutating_err(status))
2837 open_sop->so_seqid--;
2839 release_state_owner(lock_stp, LOCK_STATE);
2842 if (lock->lk_stateowner)
2843 nfs4_get_stateowner(lock->lk_stateowner);
2844 nfs4_unlock_state();
2852 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2854 struct inode *inode;
2856 struct file_lock file_lock;
2857 struct file_lock *conflicting_lock;
2860 if (nfs4_in_grace())
2861 return nfserr_grace;
2863 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2864 return nfserr_inval;
2866 lockt->lt_stateowner = NULL;
2869 status = nfserr_stale_clientid;
2870 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2871 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2875 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2876 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2877 if (status == nfserr_symlink)
2878 status = nfserr_inval;
2882 inode = current_fh->fh_dentry->d_inode;
2883 locks_init_lock(&file_lock);
2884 switch (lockt->lt_type) {
2887 file_lock.fl_type = F_RDLCK;
2890 case NFS4_WRITEW_LT:
2891 file_lock.fl_type = F_WRLCK;
2894 printk("NFSD: nfs4_lockt: bad lock type!\n");
2895 status = nfserr_inval;
2899 lockt->lt_stateowner = find_lockstateowner_str(inode,
2900 &lockt->lt_clientid, &lockt->lt_owner);
2901 if (lockt->lt_stateowner)
2902 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2903 file_lock.fl_pid = current->tgid;
2904 file_lock.fl_flags = FL_POSIX;
2906 file_lock.fl_start = lockt->lt_offset;
2907 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2908 file_lock.fl_end = ~(u64)0;
2910 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2912 nfs4_transform_lock_offset(&file_lock);
2914 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2915 * since LOCKT doesn't require an OPEN, and therefore a struct
2916 * file may not exist, pass posix_test_lock a struct file with
2917 * only the dentry:inode set.
2919 memset(&file, 0, sizeof (struct file));
2920 file.f_dentry = current_fh->fh_dentry;
2923 conflicting_lock = posix_test_lock(&file, &file_lock);
2924 if (conflicting_lock) {
2925 status = nfserr_denied;
2926 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2929 nfs4_unlock_state();
2934 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2936 struct nfs4_stateid *stp;
2937 struct file *filp = NULL;
2938 struct file_lock file_lock;
2941 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2942 (long long) locku->lu_offset,
2943 (long long) locku->lu_length);
2945 if (check_lock_length(locku->lu_offset, locku->lu_length))
2946 return nfserr_inval;
2950 if ((status = nfs4_preprocess_seqid_op(current_fh,
2953 CHECK_FH | LOCK_STATE,
2954 &locku->lu_stateowner, &stp, NULL)))
2957 filp = stp->st_vfs_file;
2959 locks_init_lock(&file_lock);
2960 file_lock.fl_type = F_UNLCK;
2961 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2962 file_lock.fl_pid = current->tgid;
2963 file_lock.fl_file = filp;
2964 file_lock.fl_flags = FL_POSIX;
2965 file_lock.fl_start = locku->lu_offset;
2967 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2968 file_lock.fl_end = ~(u64)0;
2970 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2971 nfs4_transform_lock_offset(&file_lock);
2974 * Try to unlock the file in the VFS.
2976 status = posix_lock_file(filp, &file_lock);
2977 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2978 file_lock.fl_ops->fl_release_private(&file_lock);
2980 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2984 * OK, unlock succeeded; the only thing left to do is update the stateid.
2986 update_stateid(&stp->st_stateid);
2987 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2990 if (locku->lu_stateowner)
2991 nfs4_get_stateowner(locku->lu_stateowner);
2992 nfs4_unlock_state();
2996 status = nfserrno(status);
3002 * 1: locks held by lockowner
3003 * 0: no locks held by lockowner
3006 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3008 struct file_lock **flpp;
3009 struct inode *inode = filp->f_dentry->d_inode;
3013 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3014 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3024 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3026 clientid_t *clid = &rlockowner->rl_clientid;
3027 struct nfs4_stateowner *local = NULL;
3028 struct xdr_netobj *owner = &rlockowner->rl_owner;
3031 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3032 clid->cl_boot, clid->cl_id);
3034 /* XXX check for lease expiration */
3036 status = nfserr_stale_clientid;
3037 if (STALE_CLIENTID(clid)) {
3038 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3045 local = find_lockstateowner(owner, clid);
3047 struct nfs4_stateid *stp;
3049 /* check for any locks held by any stateid
3050 * associated with the (lock) stateowner */
3051 status = nfserr_locks_held;
3052 list_for_each_entry(stp, &local->so_perfilestate,
3054 if (check_for_locks(stp->st_vfs_file, local))
3057 /* no locks held by (lock) stateowner */
3059 release_stateowner(local);
3062 nfs4_unlock_state();
3066 static inline struct nfs4_client_reclaim *
3067 alloc_reclaim(int namelen)
3069 struct nfs4_client_reclaim *crp = NULL;
3071 crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3074 crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3075 if (!crp->cr_name.data) {
3083 * failure => all reset bets are off, nfserr_no_grace...
3086 nfs4_client_to_reclaim(char *name, int namlen)
3088 unsigned int strhashval;
3089 struct nfs4_client_reclaim *crp = NULL;
3091 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3092 crp = alloc_reclaim(namlen);
3095 strhashval = clientstr_hashval(name, namlen);
3096 INIT_LIST_HEAD(&crp->cr_strhash);
3097 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3098 memcpy(crp->cr_name.data, name, namlen);
3099 crp->cr_name.len = namlen;
3100 reclaim_str_hashtbl_size++;
3105 nfs4_release_reclaim(void)
3107 struct nfs4_client_reclaim *crp = NULL;
3110 BUG_ON(!nfs4_reclaim_init);
3111 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3112 while (!list_empty(&reclaim_str_hashtbl[i])) {
3113 crp = list_entry(reclaim_str_hashtbl[i].next,
3114 struct nfs4_client_reclaim, cr_strhash);
3115 list_del(&crp->cr_strhash);
3116 kfree(crp->cr_name.data);
3118 reclaim_str_hashtbl_size--;
3121 BUG_ON(reclaim_str_hashtbl_size);
3125 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3126 struct nfs4_client_reclaim *
3127 nfs4_find_reclaim_client(clientid_t *clid)
3129 unsigned int strhashval;
3130 struct nfs4_client *clp;
3131 struct nfs4_client_reclaim *crp = NULL;
3134 /* find clientid in conf_id_hashtbl */
3135 clp = find_confirmed_client(clid);
3139 dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3140 clp->cl_name.len, clp->cl_name.data);
3142 /* find clp->cl_name in reclaim_str_hashtbl */
3143 strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3144 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3145 if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3153 * Called from OPEN. Look for clientid in reclaim list.
3156 nfs4_check_open_reclaim(clientid_t *clid)
3158 struct nfs4_client_reclaim *crp;
3160 if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
3161 return nfserr_reclaim_bad;
3167 * Start and stop routines
3171 __nfs4_state_init(void)
3176 if (!nfs4_reclaim_init) {
3177 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3178 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3179 reclaim_str_hashtbl_size = 0;
3180 nfs4_reclaim_init = 1;
3182 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3183 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3184 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3185 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3186 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3188 for (i = 0; i < FILE_HASH_SIZE; i++) {
3189 INIT_LIST_HEAD(&file_hashtbl[i]);
3191 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3192 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3193 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3195 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3196 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3197 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3199 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3200 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3201 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3203 memset(&zerostateid, 0, sizeof(stateid_t));
3204 memset(&onestateid, ~0, sizeof(stateid_t));
3206 INIT_LIST_HEAD(&close_lru);
3207 INIT_LIST_HEAD(&client_lru);
3208 INIT_LIST_HEAD(&del_recall_lru);
3209 spin_lock_init(&recall_lock);
3210 boot_time = get_seconds();
3211 grace_time = max(old_lease_time, lease_time);
3212 if (reclaim_str_hashtbl_size == 0)
3215 printk("NFSD: starting %ld-second grace period\n", grace_time);
3216 grace_end = boot_time + grace_time;
3217 INIT_WORK(&laundromat_work,laundromat_main, NULL);
3218 schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3222 nfs4_state_init(void)
3228 status = nfsd4_init_slabs();
3231 __nfs4_state_init();
3239 return get_seconds() < grace_end;
3245 printk("NFSD: ERROR in reboot recovery. State reclaims will fail.\n");
3246 grace_end = get_seconds();
3250 nfs4_lease_time(void)
3256 __nfs4_state_shutdown(void)
3259 struct nfs4_client *clp = NULL;
3260 struct nfs4_delegation *dp = NULL;
3261 struct nfs4_stateowner *sop = NULL;
3262 struct list_head *pos, *next, reaplist;
3264 list_for_each_safe(pos, next, &close_lru) {
3265 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3266 list_del(&sop->so_close_lru);
3267 nfs4_put_stateowner(sop);
3270 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3271 while (!list_empty(&conf_id_hashtbl[i])) {
3272 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3275 while (!list_empty(&unconf_str_hashtbl[i])) {
3276 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3280 INIT_LIST_HEAD(&reaplist);
3281 spin_lock(&recall_lock);
3282 list_for_each_safe(pos, next, &del_recall_lru) {
3283 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3284 list_move(&dp->dl_recall_lru, &reaplist);
3286 spin_unlock(&recall_lock);
3287 list_for_each_safe(pos, next, &reaplist) {
3288 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3289 list_del_init(&dp->dl_recall_lru);
3290 unhash_delegation(dp);
3293 release_all_files();
3294 cancel_delayed_work(&laundromat_work);
3295 flush_scheduled_work();
3297 dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
3298 list_add_perfile, list_del_perfile);
3299 dprintk("NFSD: add_perclient %d del_perclient %d\n",
3300 add_perclient, del_perclient);
3301 dprintk("NFSD: vfsopen %d vfsclose %d\n",
3303 dprintk("NFSD: alloc_delegation %d free_delegation %d\n",
3304 alloc_delegation, free_delegation);
3309 nfs4_state_shutdown(void)
3312 nfs4_release_reclaim();
3313 __nfs4_state_shutdown();
3315 nfs4_unlock_state();
3319 * Called when leasetime is changed.
3321 * if nfsd is not started, simply set the global lease.
3323 * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3324 * e.g: boot_time is reset, existing nfs4_client structs are
3325 * used to fill reclaim_str_hashtbl, then all state (except for the
3326 * reclaim_str_hashtbl) is re-initialized.
3328 * if the old lease time is greater than the new lease time, the grace
3329 * period needs to be set to the old lease time to allow clients to reclaim
3330 * their state. XXX - we may want to set the grace period == lease time
3331 * after an initial grace period == old lease time
3333 * if an error occurs in this process, the new lease is set, but the server
3334 * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3335 * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3337 * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3338 * OPEN and LOCK reclaims.
3341 nfs4_reset_lease(time_t leasetime)
3343 struct nfs4_client *clp;
3346 printk("NFSD: New leasetime %ld\n",leasetime);
3350 old_lease_time = lease_time;
3351 lease_time = leasetime;
3353 nfs4_release_reclaim();
3355 /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3356 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3357 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3358 if (!nfs4_client_to_reclaim(clp->cl_name.data,
3359 clp->cl_name.len)) {
3360 nfs4_release_reclaim();
3366 __nfs4_state_shutdown();
3367 __nfs4_state_init();
3368 nfs4_unlock_state();