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;
93 kmem_cache_t *stateid_slab = NULL;
102 nfs4_unlock_state(void)
108 opaque_hashval(const void *ptr, int nbytes)
110 unsigned char *cptr = (unsigned char *) ptr;
120 /* forward declarations */
121 static void release_stateowner(struct nfs4_stateowner *sop);
122 static void release_stateid(struct nfs4_stateid *stp, int flags);
123 static void release_file(struct nfs4_file *fp);
129 /* recall_lock protects the del_recall_lru */
130 spinlock_t recall_lock;
131 static struct list_head del_recall_lru;
133 static struct nfs4_delegation *
134 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
136 struct nfs4_delegation *dp;
137 struct nfs4_file *fp = stp->st_file;
138 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
140 dprintk("NFSD alloc_init_deleg\n");
141 if ((dp = kmalloc(sizeof(struct nfs4_delegation),
142 GFP_KERNEL)) == NULL)
144 INIT_LIST_HEAD(&dp->dl_del_perfile);
145 INIT_LIST_HEAD(&dp->dl_del_perclnt);
146 INIT_LIST_HEAD(&dp->dl_recall_lru);
150 get_file(stp->st_vfs_file);
151 dp->dl_vfs_file = stp->st_vfs_file;
153 dp->dl_recall.cbr_dp = NULL;
154 dp->dl_recall.cbr_ident = cb->cb_ident;
155 dp->dl_recall.cbr_trunc = 0;
156 dp->dl_stateid.si_boot = boot_time;
157 dp->dl_stateid.si_stateownerid = current_delegid++;
158 dp->dl_stateid.si_fileid = 0;
159 dp->dl_stateid.si_generation = 0;
160 dp->dl_fhlen = current_fh->fh_handle.fh_size;
161 memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base,
162 current_fh->fh_handle.fh_size);
164 atomic_set(&dp->dl_count, 1);
165 list_add(&dp->dl_del_perfile, &fp->fi_del_perfile);
166 list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
172 nfs4_put_delegation(struct nfs4_delegation *dp)
174 if (atomic_dec_and_test(&dp->dl_count)) {
175 dprintk("NFSD: freeing dp %p\n",dp);
181 /* Remove the associated file_lock first, then remove the delegation.
182 * lease_modify() is called to remove the FS_LEASE file_lock from
183 * the i_flock list, eventually calling nfsd's lock_manager
184 * fl_release_callback.
187 nfs4_close_delegation(struct nfs4_delegation *dp)
189 struct file *filp = dp->dl_vfs_file;
191 dprintk("NFSD: close_delegation dp %p\n",dp);
192 dp->dl_vfs_file = NULL;
193 /* The following nfsd_close may not actually close the file,
194 * but we want to remove the lease in any case. */
196 setlease(filp, F_UNLCK, &dp->dl_flock);
201 /* Called under the state lock. */
203 unhash_delegation(struct nfs4_delegation *dp)
205 list_del_init(&dp->dl_del_perfile);
206 list_del_init(&dp->dl_del_perclnt);
207 spin_lock(&recall_lock);
208 list_del_init(&dp->dl_recall_lru);
209 spin_unlock(&recall_lock);
210 nfs4_close_delegation(dp);
211 nfs4_put_delegation(dp);
218 /* Hash tables for nfs4_clientid state */
219 #define CLIENT_HASH_BITS 4
220 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
221 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
223 #define clientid_hashval(id) \
224 ((id) & CLIENT_HASH_MASK)
225 #define clientstr_hashval(name, namelen) \
226 (opaque_hashval((name), (namelen)) & CLIENT_HASH_MASK)
228 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
229 * used in reboot/reset lease grace period processing
231 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
232 * setclientid_confirmed info.
234 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
237 * client_lru holds client queue ordered by nfs4_client.cl_time
240 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
241 * for last close replay.
243 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
244 static int reclaim_str_hashtbl_size = 0;
245 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
246 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
247 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
248 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
249 static struct list_head client_lru;
250 static struct list_head close_lru;
253 renew_client(struct nfs4_client *clp)
256 * Move client to the end to the LRU list.
258 dprintk("renewing client (clientid %08x/%08x)\n",
259 clp->cl_clientid.cl_boot,
260 clp->cl_clientid.cl_id);
261 list_move_tail(&clp->cl_lru, &client_lru);
262 clp->cl_time = get_seconds();
265 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
267 STALE_CLIENTID(clientid_t *clid)
269 if (clid->cl_boot == boot_time)
271 dprintk("NFSD stale clientid (%08x/%08x)\n",
272 clid->cl_boot, clid->cl_id);
277 * XXX Should we use a slab cache ?
278 * This type of memory management is somewhat inefficient, but we use it
279 * anyway since SETCLIENTID is not a common operation.
281 static inline struct nfs4_client *
282 alloc_client(struct xdr_netobj name)
284 struct nfs4_client *clp;
286 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
287 memset(clp, 0, sizeof(*clp));
288 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
289 memcpy(clp->cl_name.data, name.data, name.len);
290 clp->cl_name.len = name.len;
301 free_client(struct nfs4_client *clp)
303 if (clp->cl_cred.cr_group_info)
304 put_group_info(clp->cl_cred.cr_group_info);
305 kfree(clp->cl_name.data);
310 put_nfs4_client(struct nfs4_client *clp)
312 if (atomic_dec_and_test(&clp->cl_count))
317 expire_client(struct nfs4_client *clp)
319 struct nfs4_stateowner *sop;
320 struct nfs4_delegation *dp;
321 struct nfs4_callback *cb = &clp->cl_callback;
322 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
323 struct list_head reaplist;
325 dprintk("NFSD: expire_client cl_count %d\n",
326 atomic_read(&clp->cl_count));
328 /* shutdown rpc client, ending any outstanding recall rpcs */
329 if (atomic_read(&cb->cb_set) == 1 && clnt) {
330 rpc_shutdown_client(clnt);
331 clnt = clp->cl_callback.cb_client = NULL;
334 INIT_LIST_HEAD(&reaplist);
335 spin_lock(&recall_lock);
336 while (!list_empty(&clp->cl_del_perclnt)) {
337 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
338 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
340 list_del_init(&dp->dl_del_perclnt);
341 list_move(&dp->dl_recall_lru, &reaplist);
343 spin_unlock(&recall_lock);
344 while (!list_empty(&reaplist)) {
345 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
346 list_del_init(&dp->dl_recall_lru);
347 unhash_delegation(dp);
349 list_del(&clp->cl_idhash);
350 list_del(&clp->cl_strhash);
351 list_del(&clp->cl_lru);
352 while (!list_empty(&clp->cl_perclient)) {
353 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
354 release_stateowner(sop);
356 put_nfs4_client(clp);
359 static struct nfs4_client *
360 create_client(struct xdr_netobj name) {
361 struct nfs4_client *clp;
363 if (!(clp = alloc_client(name)))
365 atomic_set(&clp->cl_count, 1);
366 atomic_set(&clp->cl_callback.cb_set, 0);
367 clp->cl_callback.cb_parsed = 0;
368 INIT_LIST_HEAD(&clp->cl_idhash);
369 INIT_LIST_HEAD(&clp->cl_strhash);
370 INIT_LIST_HEAD(&clp->cl_perclient);
371 INIT_LIST_HEAD(&clp->cl_del_perclnt);
372 INIT_LIST_HEAD(&clp->cl_lru);
378 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
379 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
383 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
384 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
385 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
389 copy_cred(struct svc_cred *target, struct svc_cred *source) {
391 target->cr_uid = source->cr_uid;
392 target->cr_gid = source->cr_gid;
393 target->cr_group_info = source->cr_group_info;
394 get_group_info(target->cr_group_info);
398 cmp_name(struct xdr_netobj *n1, struct xdr_netobj *n2) {
401 return((n1->len == n2->len) && !memcmp(n1->data, n2->data, n2->len));
405 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
406 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
410 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
411 return((cl1->cl_boot == cl2->cl_boot) &&
412 (cl1->cl_id == cl2->cl_id));
415 /* XXX what about NGROUP */
417 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
418 return(cr1->cr_uid == cr2->cr_uid);
423 gen_clid(struct nfs4_client *clp) {
424 clp->cl_clientid.cl_boot = boot_time;
425 clp->cl_clientid.cl_id = current_clientid++;
429 gen_confirm(struct nfs4_client *clp) {
434 p = (u32 *)clp->cl_confirm.data;
440 check_name(struct xdr_netobj name) {
444 if (name.len > NFS4_OPAQUE_LIMIT) {
445 printk("NFSD: check_name: name too long(%d)!\n", name.len);
452 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
454 unsigned int idhashval;
456 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
457 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
458 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
459 list_add_tail(&clp->cl_lru, &client_lru);
460 clp->cl_time = get_seconds();
464 move_to_confirmed(struct nfs4_client *clp)
466 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
467 unsigned int strhashval;
469 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
470 list_del_init(&clp->cl_strhash);
471 list_del_init(&clp->cl_idhash);
472 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
473 strhashval = clientstr_hashval(clp->cl_name.data,
475 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
479 static struct nfs4_client *
480 find_confirmed_client(clientid_t *clid)
482 struct nfs4_client *clp;
483 unsigned int idhashval = clientid_hashval(clid->cl_id);
485 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
486 if (cmp_clid(&clp->cl_clientid, clid))
492 static struct nfs4_client *
493 find_unconfirmed_client(clientid_t *clid)
495 struct nfs4_client *clp;
496 unsigned int idhashval = clientid_hashval(clid->cl_id);
498 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
499 if (cmp_clid(&clp->cl_clientid, clid))
505 /* a helper function for parse_callback */
507 parse_octet(unsigned int *lenp, char **addrp)
509 unsigned int len = *lenp;
521 if ((c < '0') || (c > '9')) {
527 n = (n * 10) + (c - '0');
538 /* parse and set the setclientid ipv4 callback address */
540 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
545 u32 addrlen = addr_len;
546 char *addr = addr_val;
551 for(i = 4; i > 0 ; i--) {
552 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
555 cbaddr |= (temp << shift);
563 for(i = 2; i > 0 ; i--) {
564 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
567 cbport |= (temp << shift);
576 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
578 struct nfs4_callback *cb = &clp->cl_callback;
580 /* Currently, we only support tcp for the callback channel */
581 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
584 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
585 &cb->cb_addr, &cb->cb_port)))
587 cb->cb_prog = se->se_callback_prog;
588 cb->cb_ident = se->se_callback_ident;
592 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
593 "will not receive delegations\n",
594 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
601 * RFC 3010 has a complex implmentation description of processing a
602 * SETCLIENTID request consisting of 5 bullets, labeled as
603 * CASE0 - CASE4 below.
606 * callback information will be processed in a future patch
608 * an unconfirmed record is added when:
609 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
610 * CASE 1: confirmed record found with matching name, principal,
611 * verifier, and clientid.
612 * CASE 2: confirmed record found with matching name, principal,
613 * and there is no unconfirmed record with matching
616 * an unconfirmed record is replaced when:
617 * CASE 3: confirmed record found with matching name, principal,
618 * and an unconfirmed record is found with matching
619 * name, principal, and with clientid and
620 * confirm that does not match the confirmed record.
621 * CASE 4: there is no confirmed record with matching name and
622 * principal. there is an unconfirmed record with
623 * matching name, principal.
625 * an unconfirmed record is deleted when:
626 * CASE 1: an unconfirmed record that matches input name, verifier,
627 * and confirmed clientid.
628 * CASE 4: any unconfirmed records with matching name and principal
629 * that exist after an unconfirmed record has been replaced
630 * as described above.
634 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
636 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
637 struct xdr_netobj clname = {
638 .len = setclid->se_namelen,
639 .data = setclid->se_name,
641 nfs4_verifier clverifier = setclid->se_verf;
642 unsigned int strhashval;
643 struct nfs4_client * conf, * unconf, * new, * clp;
646 status = nfserr_inval;
647 if (!check_name(clname))
651 * XXX The Duplicate Request Cache (DRC) has been checked (??)
652 * We get here on a DRC miss.
655 strhashval = clientstr_hashval(clname.data, clname.len);
659 list_for_each_entry(clp, &conf_str_hashtbl[strhashval], cl_strhash) {
660 if (!cmp_name(&clp->cl_name, &clname))
664 * clname match, confirmed, different principal
665 * or different ip_address
667 status = nfserr_clid_inuse;
668 if (!cmp_creds(&clp->cl_cred,&rqstp->rq_cred)) {
669 printk("NFSD: setclientid: string in use by client"
670 "(clientid %08x/%08x)\n",
671 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
674 if (clp->cl_addr != ip_addr) {
675 printk("NFSD: setclientid: string in use by client"
676 "(clientid %08x/%08x)\n",
677 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
682 * cl_name match from a previous SETCLIENTID operation
683 * XXX check for additional matches?
689 list_for_each_entry(clp, &unconf_str_hashtbl[strhashval], cl_strhash) {
690 if (!cmp_name(&clp->cl_name, &clname))
692 /* cl_name match from a previous SETCLIENTID operation */
696 status = nfserr_resource;
700 * placed first, because it is the normal case.
703 expire_client(unconf);
704 if (!(new = create_client(clname)))
706 copy_verf(new, &clverifier);
707 new->cl_addr = ip_addr;
708 copy_cred(&new->cl_cred,&rqstp->rq_cred);
711 gen_callback(new, setclid);
712 add_to_unconfirmed(new, strhashval);
713 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
716 * cl_name match, confirmed, principal match
717 * verifier match: probable callback update
719 * remove any unconfirmed nfs4_client with
720 * matching cl_name, cl_verifier, and cl_clientid
722 * create and insert an unconfirmed nfs4_client with same
723 * cl_name, cl_verifier, and cl_clientid as existing
724 * nfs4_client, but with the new callback info and a
728 cmp_verf(&unconf->cl_verifier, &conf->cl_verifier) &&
729 cmp_clid(&unconf->cl_clientid, &conf->cl_clientid)) {
730 expire_client(unconf);
732 if (!(new = create_client(clname)))
734 copy_verf(new,&conf->cl_verifier);
735 new->cl_addr = ip_addr;
736 copy_cred(&new->cl_cred,&rqstp->rq_cred);
737 copy_clid(new, conf);
739 gen_callback(new, setclid);
740 add_to_unconfirmed(new,strhashval);
741 } else if (!unconf) {
744 * clname match, confirmed, principal match
745 * verfier does not match
746 * no unconfirmed. create a new unconfirmed nfs4_client
747 * using input clverifier, clname, and callback info
748 * and generate a new cl_clientid and cl_confirm.
750 if (!(new = create_client(clname)))
752 copy_verf(new,&clverifier);
753 new->cl_addr = ip_addr;
754 copy_cred(&new->cl_cred,&rqstp->rq_cred);
757 gen_callback(new, setclid);
758 add_to_unconfirmed(new, strhashval);
759 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
762 * confirmed found (name, principal match)
763 * confirmed verifier does not match input clverifier
765 * unconfirmed found (name match)
766 * confirmed->cl_confirm != unconfirmed->cl_confirm
768 * remove unconfirmed.
770 * create an unconfirmed nfs4_client
771 * with same cl_name as existing confirmed nfs4_client,
772 * but with new callback info, new cl_clientid,
773 * new cl_verifier and a new cl_confirm
775 expire_client(unconf);
776 if (!(new = create_client(clname)))
778 copy_verf(new,&clverifier);
779 new->cl_addr = ip_addr;
780 copy_cred(&new->cl_cred,&rqstp->rq_cred);
783 gen_callback(new, setclid);
784 add_to_unconfirmed(new, strhashval);
786 /* No cases hit !!! */
787 status = nfserr_inval;
791 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
792 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
793 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
802 * RFC 3010 has a complex implmentation description of processing a
803 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
804 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
806 * NOTE: callback information will be processed here in a future patch
809 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
811 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
812 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
813 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
814 clientid_t * clid = &setclientid_confirm->sc_clientid;
817 if (STALE_CLIENTID(clid))
818 return nfserr_stale_clientid;
820 * XXX The Duplicate Request Cache (DRC) has been checked (??)
821 * We get here on a DRC miss.
825 clp = find_confirmed_client(clid);
827 status = nfserr_inval;
829 * Found a record for this clientid. If the IP addresses
830 * don't match, return ERR_INVAL just as if the record had
833 if (clp->cl_addr != ip_addr) {
834 printk("NFSD: setclientid: string in use by client"
835 "(clientid %08x/%08x)\n",
836 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
841 clp = find_unconfirmed_client(clid);
843 status = nfserr_inval;
844 if (clp->cl_addr != ip_addr) {
845 printk("NFSD: setclientid: string in use by client"
846 "(clientid %08x/%08x)\n",
847 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
853 * unconf record that matches input clientid and input confirm.
854 * conf record that matches input clientid.
855 * conf and unconf records match names, verifiers
857 if ((conf && unconf) &&
858 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
859 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
860 (cmp_name(&conf->cl_name,&unconf->cl_name)) &&
861 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
862 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
863 status = nfserr_clid_inuse;
867 move_to_confirmed(unconf);
873 * conf record that matches input clientid.
874 * if unconf record that matches input clientid, then unconf->cl_name
875 * or unconf->cl_verifier don't match the conf record.
877 if ((conf && !unconf) ||
879 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
880 !cmp_name(&conf->cl_name, &unconf->cl_name)))) {
881 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
882 status = nfserr_clid_inuse;
890 * conf record not found.
891 * unconf record found.
892 * unconf->cl_confirm matches input confirm
894 if (!conf && unconf && cmp_verf(&unconf->cl_confirm, &confirm)) {
895 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
896 status = nfserr_clid_inuse;
900 move_to_confirmed(unconf);
905 * conf record not found, or if conf, then conf->cl_confirm does not
906 * match input confirm.
907 * unconf record not found, or if unconf, then unconf->cl_confirm
908 * does not match input confirm.
910 if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm))) &&
911 (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm, &confirm)))) {
912 status = nfserr_stale_clientid;
915 /* check that we have hit one of the cases...*/
916 status = nfserr_inval;
920 nfsd4_probe_callback(clp);
926 * Open owner state (share locks)
929 /* hash tables for nfs4_stateowner */
930 #define OWNER_HASH_BITS 8
931 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
932 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
934 #define ownerid_hashval(id) \
935 ((id) & OWNER_HASH_MASK)
936 #define ownerstr_hashval(clientid, ownername) \
937 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
939 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
940 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
942 /* hash table for nfs4_file */
943 #define FILE_HASH_BITS 8
944 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
945 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
946 /* hash table for (open)nfs4_stateid */
947 #define STATEID_HASH_BITS 10
948 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
949 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
951 #define file_hashval(x) \
952 hash_ptr(x, FILE_HASH_BITS)
953 #define stateid_hashval(owner_id, file_id) \
954 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
956 static struct list_head file_hashtbl[FILE_HASH_SIZE];
957 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
959 /* OPEN Share state helper functions */
960 static inline struct nfs4_file *
961 alloc_init_file(struct inode *ino)
963 struct nfs4_file *fp;
964 unsigned int hashval = file_hashval(ino);
966 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
968 INIT_LIST_HEAD(&fp->fi_hash);
969 INIT_LIST_HEAD(&fp->fi_perfile);
970 INIT_LIST_HEAD(&fp->fi_del_perfile);
971 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
972 fp->fi_inode = igrab(ino);
973 fp->fi_id = current_fileid++;
980 release_all_files(void)
983 struct nfs4_file *fp;
985 for (i=0;i<FILE_HASH_SIZE;i++) {
986 while (!list_empty(&file_hashtbl[i])) {
987 fp = list_entry(file_hashtbl[i].next, struct nfs4_file, fi_hash);
988 /* this should never be more than once... */
989 if (!list_empty(&fp->fi_perfile) || !list_empty(&fp->fi_del_perfile)) {
990 printk("ERROR: release_all_files: file %p is open, creating dangling state !!!\n",fp);
998 nfsd4_free_slab(kmem_cache_t **slab)
1004 status = kmem_cache_destroy(*slab);
1010 nfsd4_free_slabs(void)
1012 nfsd4_free_slab(&stateowner_slab);
1013 nfsd4_free_slab(&file_slab);
1014 nfsd4_free_slab(&stateid_slab);
1018 nfsd4_init_slabs(void)
1020 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1021 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1022 if (stateowner_slab == NULL)
1024 file_slab = kmem_cache_create("nfsd4_files",
1025 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1026 if (file_slab == NULL)
1028 stateid_slab = kmem_cache_create("nfsd4_stateids",
1029 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1030 if (stateid_slab == NULL)
1035 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1040 nfs4_free_stateowner(struct kref *kref)
1042 struct nfs4_stateowner *sop =
1043 container_of(kref, struct nfs4_stateowner, so_ref);
1044 kfree(sop->so_owner.data);
1045 kmem_cache_free(stateowner_slab, sop);
1048 static inline struct nfs4_stateowner *
1049 alloc_stateowner(struct xdr_netobj *owner)
1051 struct nfs4_stateowner *sop;
1053 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1054 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1055 memcpy(sop->so_owner.data, owner->data, owner->len);
1056 sop->so_owner.len = owner->len;
1057 kref_init(&sop->so_ref);
1060 kmem_cache_free(stateowner_slab, sop);
1065 static struct nfs4_stateowner *
1066 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1067 struct nfs4_stateowner *sop;
1068 struct nfs4_replay *rp;
1069 unsigned int idhashval;
1071 if (!(sop = alloc_stateowner(&open->op_owner)))
1073 idhashval = ownerid_hashval(current_ownerid);
1074 INIT_LIST_HEAD(&sop->so_idhash);
1075 INIT_LIST_HEAD(&sop->so_strhash);
1076 INIT_LIST_HEAD(&sop->so_perclient);
1077 INIT_LIST_HEAD(&sop->so_perfilestate);
1078 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1079 INIT_LIST_HEAD(&sop->so_close_lru);
1081 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1082 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1083 list_add(&sop->so_perclient, &clp->cl_perclient);
1085 sop->so_is_open_owner = 1;
1086 sop->so_id = current_ownerid++;
1087 sop->so_client = clp;
1088 sop->so_seqid = open->op_seqid;
1089 sop->so_confirmed = 0;
1090 rp = &sop->so_replay;
1091 rp->rp_status = NFSERR_SERVERFAULT;
1093 rp->rp_buf = rp->rp_ibuf;
1098 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1100 struct nfs4_stateowner *lock_sop;
1102 while (!list_empty(&open_stp->st_perlockowner)) {
1103 lock_sop = list_entry(open_stp->st_perlockowner.next,
1104 struct nfs4_stateowner, so_perlockowner);
1105 /* list_del(&open_stp->st_perlockowner); */
1106 BUG_ON(lock_sop->so_is_open_owner);
1107 release_stateowner(lock_sop);
1112 unhash_stateowner(struct nfs4_stateowner *sop)
1114 struct nfs4_stateid *stp;
1116 list_del(&sop->so_idhash);
1117 list_del(&sop->so_strhash);
1118 if (sop->so_is_open_owner) {
1119 list_del(&sop->so_perclient);
1122 list_del(&sop->so_perlockowner);
1123 while (!list_empty(&sop->so_perfilestate)) {
1124 stp = list_entry(sop->so_perfilestate.next,
1125 struct nfs4_stateid, st_perfilestate);
1126 if (sop->so_is_open_owner)
1127 release_stateid(stp, OPEN_STATE);
1129 release_stateid(stp, LOCK_STATE);
1134 release_stateowner(struct nfs4_stateowner *sop)
1136 unhash_stateowner(sop);
1137 list_del(&sop->so_close_lru);
1138 nfs4_put_stateowner(sop);
1142 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1143 struct nfs4_stateowner *sop = open->op_stateowner;
1144 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1146 INIT_LIST_HEAD(&stp->st_hash);
1147 INIT_LIST_HEAD(&stp->st_perfilestate);
1148 INIT_LIST_HEAD(&stp->st_perlockowner);
1149 INIT_LIST_HEAD(&stp->st_perfile);
1150 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1151 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1153 list_add(&stp->st_perfile, &fp->fi_perfile);
1154 stp->st_stateowner = sop;
1156 stp->st_stateid.si_boot = boot_time;
1157 stp->st_stateid.si_stateownerid = sop->so_id;
1158 stp->st_stateid.si_fileid = fp->fi_id;
1159 stp->st_stateid.si_generation = 0;
1160 stp->st_access_bmap = 0;
1161 stp->st_deny_bmap = 0;
1162 __set_bit(open->op_share_access, &stp->st_access_bmap);
1163 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1167 release_stateid(struct nfs4_stateid *stp, int flags)
1169 struct file *filp = stp->st_vfs_file;
1171 list_del(&stp->st_hash);
1173 list_del(&stp->st_perfile);
1174 list_del(&stp->st_perfilestate);
1175 if (flags & OPEN_STATE) {
1176 release_stateid_lockowners(stp);
1177 stp->st_vfs_file = NULL;
1180 } else if (flags & LOCK_STATE)
1181 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1182 kmem_cache_free(stateid_slab, stp);
1187 release_file(struct nfs4_file *fp)
1189 list_del(&fp->fi_hash);
1191 kmem_cache_free(file_slab, fp);
1195 move_to_close_lru(struct nfs4_stateowner *sop)
1197 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1199 unhash_stateowner(sop);
1200 list_add_tail(&sop->so_close_lru, &close_lru);
1201 sop->so_time = get_seconds();
1205 release_state_owner(struct nfs4_stateid *stp, int flag)
1207 struct nfs4_stateowner *sop = stp->st_stateowner;
1208 struct nfs4_file *fp = stp->st_file;
1210 dprintk("NFSD: release_state_owner\n");
1211 release_stateid(stp, flag);
1213 /* place unused nfs4_stateowners on so_close_lru list to be
1214 * released by the laundromat service after the lease period
1215 * to enable us to handle CLOSE replay
1217 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1218 move_to_close_lru(sop);
1219 /* unused nfs4_file's are releseed. XXX slab cache? */
1220 if (list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile)) {
1226 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1227 return ((sop->so_owner.len == owner->len) &&
1228 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1229 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1232 static struct nfs4_stateowner *
1233 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1235 struct nfs4_stateowner *so = NULL;
1237 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1238 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1244 /* search file_hashtbl[] for file */
1245 static struct nfs4_file *
1246 find_file(struct inode *ino)
1248 unsigned int hashval = file_hashval(ino);
1249 struct nfs4_file *fp;
1251 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1252 if (fp->fi_inode == ino)
1258 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1259 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1262 set_access(unsigned int *access, unsigned long bmap) {
1266 for (i = 1; i < 4; i++) {
1267 if (test_bit(i, &bmap))
1273 set_deny(unsigned int *deny, unsigned long bmap) {
1277 for (i = 0; i < 4; i++) {
1278 if (test_bit(i, &bmap))
1284 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1285 unsigned int access, deny;
1287 set_access(&access, stp->st_access_bmap);
1288 set_deny(&deny, stp->st_deny_bmap);
1289 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1295 * Called to check deny when READ with all zero stateid or
1296 * WRITE with all zero or all one stateid
1299 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1301 struct inode *ino = current_fh->fh_dentry->d_inode;
1302 struct nfs4_file *fp;
1303 struct nfs4_stateid *stp;
1305 dprintk("NFSD: nfs4_share_conflict\n");
1307 fp = find_file(ino);
1309 /* Search for conflicting share reservations */
1310 list_for_each_entry(stp, &fp->fi_perfile, st_perfile) {
1311 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1312 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1313 return nfserr_share_denied;
1320 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1322 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1323 put_write_access(filp->f_dentry->d_inode);
1324 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1329 * Recall a delegation
1332 do_recall(void *__dp)
1334 struct nfs4_delegation *dp = __dp;
1336 daemonize("nfsv4-recall");
1338 nfsd4_cb_recall(dp);
1343 * Spawn a thread to perform a recall on the delegation represented
1344 * by the lease (file_lock)
1346 * Called from break_lease() with lock_kernel() held.
1347 * Note: we assume break_lease will only call this *once* for any given
1351 void nfsd_break_deleg_cb(struct file_lock *fl)
1353 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1354 struct task_struct *t;
1356 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1360 /* We're assuming the state code never drops its reference
1361 * without first removing the lease. Since we're in this lease
1362 * callback (and since the lease code is serialized by the kernel
1363 * lock) we know the server hasn't removed the lease yet, we know
1364 * it's safe to take a reference: */
1365 atomic_inc(&dp->dl_count);
1367 spin_lock(&recall_lock);
1368 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1369 spin_unlock(&recall_lock);
1371 /* only place dl_time is set. protected by lock_kernel*/
1372 dp->dl_time = get_seconds();
1374 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1375 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1377 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1379 struct nfs4_client *clp = dp->dl_client;
1381 printk(KERN_INFO "NFSD: Callback thread failed for "
1382 "for client (clientid %08x/%08x)\n",
1383 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1384 nfs4_put_delegation(dp);
1389 * The file_lock is being reapd.
1391 * Called by locks_free_lock() with lock_kernel() held.
1394 void nfsd_release_deleg_cb(struct file_lock *fl)
1396 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1398 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1400 if (!(fl->fl_flags & FL_LEASE) || !dp)
1402 dp->dl_flock = NULL;
1406 * Set the delegation file_lock back pointer.
1408 * Called from __setlease() with lock_kernel() held.
1411 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1413 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1415 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1422 * Called from __setlease() with lock_kernel() held
1425 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1427 struct nfs4_delegation *onlistd =
1428 (struct nfs4_delegation *)onlist->fl_owner;
1429 struct nfs4_delegation *tryd =
1430 (struct nfs4_delegation *)try->fl_owner;
1432 if (onlist->fl_lmops != try->fl_lmops)
1435 return onlistd->dl_client == tryd->dl_client;
1440 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1443 return lease_modify(onlist, arg);
1448 struct lock_manager_operations nfsd_lease_mng_ops = {
1449 .fl_break = nfsd_break_deleg_cb,
1450 .fl_release_private = nfsd_release_deleg_cb,
1451 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1452 .fl_mylease = nfsd_same_client_deleg_cb,
1453 .fl_change = nfsd_change_deleg_cb,
1458 * nfsd4_process_open1()
1459 * lookup stateowner.
1471 * called with nfs4_lock_state() held.
1474 nfsd4_process_open1(struct nfsd4_open *open)
1477 clientid_t *clientid = &open->op_clientid;
1478 struct nfs4_client *clp = NULL;
1479 unsigned int strhashval;
1480 struct nfs4_stateowner *sop = NULL;
1482 status = nfserr_inval;
1483 if (!check_name(open->op_owner))
1486 if (STALE_CLIENTID(&open->op_clientid))
1487 return nfserr_stale_clientid;
1489 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1490 sop = find_openstateowner_str(strhashval, open);
1492 open->op_stateowner = sop;
1493 /* check for replay */
1494 if (open->op_seqid == sop->so_seqid){
1495 if (sop->so_replay.rp_buflen)
1496 return NFSERR_REPLAY_ME;
1498 /* The original OPEN failed so spectacularly
1499 * that we don't even have replay data saved!
1500 * Therefore, we have no choice but to continue
1501 * processing this OPEN; presumably, we'll
1502 * fail again for the same reason.
1504 dprintk("nfsd4_process_open1:"
1505 " replay with no replay cache\n");
1508 } else if (sop->so_confirmed) {
1509 if (open->op_seqid == sop->so_seqid + 1)
1511 status = nfserr_bad_seqid;
1514 /* If we get here, we received an OPEN for an
1515 * unconfirmed nfs4_stateowner. Since the seqid's are
1516 * different, purge the existing nfs4_stateowner, and
1517 * instantiate a new one.
1519 clp = sop->so_client;
1520 release_stateowner(sop);
1523 /* nfs4_stateowner not found.
1524 * Verify clientid and instantiate new nfs4_stateowner.
1525 * If verify fails this is presumably the result of the
1526 * client's lease expiring.
1528 status = nfserr_expired;
1529 clp = find_confirmed_client(clientid);
1533 status = nfserr_resource;
1534 sop = alloc_init_open_stateowner(strhashval, clp, open);
1537 open->op_stateowner = sop;
1540 renew_client(sop->so_client);
1542 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1543 status = nfserr_reclaim_bad;
1548 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1550 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1551 return nfserr_openmode;
1556 static struct nfs4_delegation *
1557 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1559 struct nfs4_delegation *dp;
1561 list_for_each_entry(dp, &fp->fi_del_perfile, dl_del_perfile) {
1562 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1569 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1570 struct nfs4_delegation **dp)
1573 int status = nfserr_bad_stateid;
1575 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1578 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1579 RD_STATE : WR_STATE;
1580 status = nfs4_check_delegmode(*dp, flags);
1584 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1588 open->op_stateowner->so_confirmed = 1;
1593 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1595 struct nfs4_stateid *local;
1596 int status = nfserr_share_denied;
1597 struct nfs4_stateowner *sop = open->op_stateowner;
1599 list_for_each_entry(local, &fp->fi_perfile, st_perfile) {
1600 /* ignore lock owners */
1601 if (local->st_stateowner->so_is_open_owner == 0)
1603 /* remember if we have seen this open owner */
1604 if (local->st_stateowner == sop)
1606 /* check for conflicting share reservations */
1607 if (!test_share(local, open))
1615 static inline struct nfs4_stateid *
1616 nfs4_alloc_stateid(void)
1618 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1622 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1623 struct nfs4_delegation *dp,
1624 struct svc_fh *cur_fh, int flags)
1626 struct nfs4_stateid *stp;
1628 stp = nfs4_alloc_stateid();
1630 return nfserr_resource;
1633 get_file(dp->dl_vfs_file);
1634 stp->st_vfs_file = dp->dl_vfs_file;
1637 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1640 if (status == nfserr_dropit)
1641 status = nfserr_jukebox;
1642 kmem_cache_free(stateid_slab, stp);
1652 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1653 struct nfsd4_open *open)
1655 struct iattr iattr = {
1656 .ia_valid = ATTR_SIZE,
1659 if (!open->op_truncate)
1661 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1663 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1667 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1669 struct file *filp = stp->st_vfs_file;
1670 struct inode *inode = filp->f_dentry->d_inode;
1671 unsigned int share_access;
1674 set_access(&share_access, stp->st_access_bmap);
1675 share_access = ~share_access;
1676 share_access &= open->op_share_access;
1678 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1679 return nfsd4_truncate(rqstp, cur_fh, open);
1681 status = get_write_access(inode);
1683 return nfserrno(status);
1684 status = nfsd4_truncate(rqstp, cur_fh, open);
1686 put_write_access(inode);
1689 /* remember the open */
1690 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1691 set_bit(open->op_share_access, &stp->st_access_bmap);
1692 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1698 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1700 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1702 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1704 *status = nfserr_reclaim_bad;
1706 open->op_stateowner->so_confirmed = 1;
1707 open->op_stateowner->so_seqid--;
1713 * Attempt to hand out a delegation.
1716 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1718 struct nfs4_delegation *dp;
1719 struct nfs4_stateowner *sop = stp->st_stateowner;
1720 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1721 struct file_lock fl, *flp = &fl;
1722 int status, flag = 0;
1724 flag = NFS4_OPEN_DELEGATE_NONE;
1725 if (open->op_claim_type != NFS4_OPEN_CLAIM_NULL
1726 || !atomic_read(&cb->cb_set) || !sop->so_confirmed)
1729 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1730 flag = NFS4_OPEN_DELEGATE_WRITE;
1732 flag = NFS4_OPEN_DELEGATE_READ;
1734 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1736 flag = NFS4_OPEN_DELEGATE_NONE;
1739 locks_init_lock(&fl);
1740 fl.fl_lmops = &nfsd_lease_mng_ops;
1741 fl.fl_flags = FL_LEASE;
1742 fl.fl_end = OFFSET_MAX;
1743 fl.fl_owner = (fl_owner_t)dp;
1744 fl.fl_file = stp->st_vfs_file;
1745 fl.fl_pid = current->tgid;
1747 /* setlease checks to see if delegation should be handed out.
1748 * the lock_manager callbacks fl_mylease and fl_change are used
1750 if ((status = setlease(stp->st_vfs_file,
1751 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1752 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1753 unhash_delegation(dp);
1754 flag = NFS4_OPEN_DELEGATE_NONE;
1758 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1760 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1761 dp->dl_stateid.si_boot,
1762 dp->dl_stateid.si_stateownerid,
1763 dp->dl_stateid.si_fileid,
1764 dp->dl_stateid.si_generation);
1766 open->op_delegate_type = flag;
1770 * called with nfs4_lock_state() held.
1773 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1775 struct nfs4_file *fp = NULL;
1776 struct inode *ino = current_fh->fh_dentry->d_inode;
1777 struct nfs4_stateid *stp = NULL;
1778 struct nfs4_delegation *dp = NULL;
1781 status = nfserr_inval;
1782 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1785 * Lookup file; if found, lookup stateid and check open request,
1786 * and check for delegations in the process of being recalled.
1787 * If not found, create the nfs4_file struct
1789 fp = find_file(ino);
1791 if ((status = nfs4_check_open(fp, open, &stp)))
1793 status = nfs4_check_deleg(fp, open, &dp);
1797 status = nfserr_bad_stateid;
1798 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1800 status = nfserr_resource;
1801 fp = alloc_init_file(ino);
1807 * OPEN the file, or upgrade an existing OPEN.
1808 * If truncate fails, the OPEN fails.
1811 /* Stateid was found, this is an OPEN upgrade */
1812 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1816 /* Stateid was not found, this is a new OPEN */
1818 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1822 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1825 init_stateid(stp, fp, open);
1826 status = nfsd4_truncate(rqstp, current_fh, open);
1828 release_stateid(stp, OPEN_STATE);
1832 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1835 * Attempt to hand out a delegation. No error return, because the
1836 * OPEN succeeds even if we fail.
1838 nfs4_open_delegation(current_fh, open, stp);
1842 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1843 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1844 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1846 /* take the opportunity to clean up unused state */
1847 if (fp && list_empty(&fp->fi_perfile) && list_empty(&fp->fi_del_perfile))
1850 /* CLAIM_PREVIOUS has different error returns */
1851 nfs4_set_claim_prev(open, &status);
1853 * To finish the open response, we just need to set the rflags.
1855 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1856 if (!open->op_stateowner->so_confirmed)
1857 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1862 static struct work_struct laundromat_work;
1863 static void laundromat_main(void *);
1864 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1867 nfsd4_renew(clientid_t *clid)
1869 struct nfs4_client *clp;
1873 dprintk("process_renew(%08x/%08x): starting\n",
1874 clid->cl_boot, clid->cl_id);
1875 status = nfserr_stale_clientid;
1876 if (STALE_CLIENTID(clid))
1878 clp = find_confirmed_client(clid);
1879 status = nfserr_expired;
1881 /* We assume the client took too long to RENEW. */
1882 dprintk("nfsd4_renew: clientid not found!\n");
1886 status = nfserr_cb_path_down;
1887 if (!list_empty(&clp->cl_del_perclnt)
1888 && !atomic_read(&clp->cl_callback.cb_set))
1892 nfs4_unlock_state();
1897 nfs4_laundromat(void)
1899 struct nfs4_client *clp;
1900 struct nfs4_stateowner *sop;
1901 struct nfs4_delegation *dp;
1902 struct list_head *pos, *next, reaplist;
1903 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1904 time_t t, clientid_val = NFSD_LEASE_TIME;
1905 time_t u, test_val = NFSD_LEASE_TIME;
1909 dprintk("NFSD: laundromat service - starting\n");
1910 list_for_each_safe(pos, next, &client_lru) {
1911 clp = list_entry(pos, struct nfs4_client, cl_lru);
1912 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1913 t = clp->cl_time - cutoff;
1914 if (clientid_val > t)
1918 dprintk("NFSD: purging unused client (clientid %08x)\n",
1919 clp->cl_clientid.cl_id);
1922 INIT_LIST_HEAD(&reaplist);
1923 spin_lock(&recall_lock);
1924 list_for_each_safe(pos, next, &del_recall_lru) {
1925 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1926 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1927 u = dp->dl_time - cutoff;
1932 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1934 list_move(&dp->dl_recall_lru, &reaplist);
1936 spin_unlock(&recall_lock);
1937 list_for_each_safe(pos, next, &reaplist) {
1938 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1939 list_del_init(&dp->dl_recall_lru);
1940 unhash_delegation(dp);
1942 test_val = NFSD_LEASE_TIME;
1943 list_for_each_safe(pos, next, &close_lru) {
1944 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1945 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1946 u = sop->so_time - cutoff;
1951 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1953 list_del(&sop->so_close_lru);
1954 nfs4_put_stateowner(sop);
1956 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1957 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1958 nfs4_unlock_state();
1959 return clientid_val;
1963 laundromat_main(void *not_used)
1967 t = nfs4_laundromat();
1968 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1969 schedule_delayed_work(&laundromat_work, t*HZ);
1972 /* search ownerid_hashtbl[] and close_lru for stateid owner
1973 * (stateid->si_stateownerid)
1975 struct nfs4_stateowner *
1976 find_openstateowner_id(u32 st_id, int flags) {
1977 struct nfs4_stateowner *local = NULL;
1979 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
1980 if (flags & CLOSE_STATE) {
1981 list_for_each_entry(local, &close_lru, so_close_lru) {
1982 if (local->so_id == st_id)
1990 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1992 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1996 STALE_STATEID(stateid_t *stateid)
1998 if (stateid->si_boot == boot_time)
2000 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2001 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2002 stateid->si_generation);
2007 access_permit_read(unsigned long access_bmap)
2009 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2010 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2011 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2015 access_permit_write(unsigned long access_bmap)
2017 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2018 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2022 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2024 int status = nfserr_openmode;
2026 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2028 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2036 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2038 /* Trying to call delegreturn with a special stateid? Yuch: */
2039 if (!(flags & (RD_STATE | WR_STATE)))
2040 return nfserr_bad_stateid;
2041 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2043 else if (nfs4_in_grace()) {
2044 /* Answer in remaining cases depends on existance of
2045 * conflicting state; so we must wait out the grace period. */
2046 return nfserr_grace;
2047 } else if (flags & WR_STATE)
2048 return nfs4_share_conflict(current_fh,
2049 NFS4_SHARE_DENY_WRITE);
2050 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2051 return nfs4_share_conflict(current_fh,
2052 NFS4_SHARE_DENY_READ);
2056 * Allow READ/WRITE during grace period on recovered state only for files
2057 * that are not able to provide mandatory locking.
2060 io_during_grace_disallowed(struct inode *inode, int flags)
2062 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2063 && MANDATORY_LOCK(inode);
2067 * Checks for stateid operations
2070 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2072 struct nfs4_stateid *stp = NULL;
2073 struct nfs4_delegation *dp = NULL;
2075 struct inode *ino = current_fh->fh_dentry->d_inode;
2078 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2079 stateid->si_boot, stateid->si_stateownerid,
2080 stateid->si_fileid, stateid->si_generation);
2084 if (io_during_grace_disallowed(ino, flags))
2085 return nfserr_grace;
2087 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2088 return check_special_stateids(current_fh, stateid, flags);
2091 status = nfserr_stale_stateid;
2092 if (STALE_STATEID(stateid))
2096 status = nfserr_bad_stateid;
2097 if (!stateid->si_fileid) { /* delegation stateid */
2098 if(!(dp = find_delegation_stateid(ino, stateid))) {
2099 dprintk("NFSD: delegation stateid not found\n");
2100 if (nfs4_in_grace())
2101 status = nfserr_grace;
2104 stidp = &dp->dl_stateid;
2105 } else { /* open or lock stateid */
2106 if (!(stp = find_stateid(stateid, flags))) {
2107 dprintk("NFSD: open or lock stateid not found\n");
2108 if (nfs4_in_grace())
2109 status = nfserr_grace;
2112 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2114 if (!stp->st_stateowner->so_confirmed)
2116 stidp = &stp->st_stateid;
2118 if (stateid->si_generation > stidp->si_generation)
2122 status = nfserr_old_stateid;
2123 if (stateid->si_generation < stidp->si_generation)
2126 if ((status = nfs4_check_openmode(stp,flags)))
2128 renew_client(stp->st_stateowner->so_client);
2130 *filpp = stp->st_vfs_file;
2132 if ((status = nfs4_check_delegmode(dp, flags)))
2134 renew_client(dp->dl_client);
2135 if (flags & DELEG_RET)
2136 unhash_delegation(dp);
2138 *filpp = dp->dl_vfs_file;
2147 * Checks for sequence id mutating operations.
2150 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)
2153 struct nfs4_stateid *stp;
2154 struct nfs4_stateowner *sop;
2156 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2157 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2158 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2159 stateid->si_generation);
2164 status = nfserr_bad_stateid;
2165 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2166 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2170 status = nfserr_stale_stateid;
2171 if (STALE_STATEID(stateid))
2174 * We return BAD_STATEID if filehandle doesn't match stateid,
2175 * the confirmed flag is incorrecly set, or the generation
2176 * number is incorrect.
2177 * If there is no entry in the openfile table for this id,
2178 * we can't always return BAD_STATEID;
2179 * this might be a retransmitted CLOSE which has arrived after
2180 * the openfile has been released.
2182 if (!(stp = find_stateid(stateid, flags)))
2183 goto no_nfs4_stateid;
2185 status = nfserr_bad_stateid;
2187 /* for new lock stateowners:
2188 * check that the lock->v.new.open_stateid
2189 * refers to an open stateowner
2191 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2192 * as the open_stateid->st_stateowner->so_client->clientid
2195 struct nfs4_stateowner *sop = stp->st_stateowner;
2196 struct nfs4_client *clp = sop->so_client;
2198 if (!sop->so_is_open_owner)
2200 if (!cmp_clid(&clp->cl_clientid, lockclid))
2204 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2205 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2210 *sopp = sop = stp->st_stateowner;
2213 * We now validate the seqid and stateid generation numbers.
2214 * For the moment, we ignore the possibility of
2215 * generation number wraparound.
2217 if (seqid != sop->so_seqid + 1)
2220 if (sop->so_confirmed) {
2221 if (flags & CONFIRM) {
2222 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2227 if (!(flags & CONFIRM)) {
2228 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2232 if (stateid->si_generation > stp->st_stateid.si_generation) {
2233 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2237 status = nfserr_old_stateid;
2238 if (stateid->si_generation < stp->st_stateid.si_generation) {
2239 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2242 /* XXX renew the client lease here */
2251 * We determine whether this is a bad stateid or a replay,
2252 * starting by trying to look up the stateowner.
2253 * If stateowner is not found - stateid is bad.
2255 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2256 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2257 status = nfserr_bad_stateid;
2263 if (seqid == sop->so_seqid) {
2264 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2265 /* indicate replay to calling function */
2266 status = NFSERR_REPLAY_ME;
2268 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2271 status = nfserr_bad_seqid;
2277 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2280 struct nfs4_stateowner *sop;
2281 struct nfs4_stateid *stp;
2283 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2284 (int)current_fh->fh_dentry->d_name.len,
2285 current_fh->fh_dentry->d_name.name);
2287 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2292 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2293 &oc->oc_req_stateid,
2294 CHECK_FH | CONFIRM | OPEN_STATE,
2295 &oc->oc_stateowner, &stp, NULL)))
2298 sop = oc->oc_stateowner;
2299 sop->so_confirmed = 1;
2300 update_stateid(&stp->st_stateid);
2301 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2302 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2303 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2304 stp->st_stateid.si_boot,
2305 stp->st_stateid.si_stateownerid,
2306 stp->st_stateid.si_fileid,
2307 stp->st_stateid.si_generation);
2309 if (oc->oc_stateowner)
2310 nfs4_get_stateowner(oc->oc_stateowner);
2311 nfs4_unlock_state();
2317 * unset all bits in union bitmap (bmap) that
2318 * do not exist in share (from successful OPEN_DOWNGRADE)
2321 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2324 for (i = 1; i < 4; i++) {
2325 if ((i & access) != i)
2326 __clear_bit(i, bmap);
2331 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2334 for (i = 0; i < 4; i++) {
2335 if ((i & deny) != i)
2336 __clear_bit(i, bmap);
2341 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2344 struct nfs4_stateid *stp;
2345 unsigned int share_access;
2347 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2348 (int)current_fh->fh_dentry->d_name.len,
2349 current_fh->fh_dentry->d_name.name);
2351 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2352 return nfserr_inval;
2355 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2357 CHECK_FH | OPEN_STATE,
2358 &od->od_stateowner, &stp, NULL)))
2361 status = nfserr_inval;
2362 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2363 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2364 stp->st_access_bmap, od->od_share_access);
2367 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2368 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2369 stp->st_deny_bmap, od->od_share_deny);
2372 set_access(&share_access, stp->st_access_bmap);
2373 nfs4_file_downgrade(stp->st_vfs_file,
2374 share_access & ~od->od_share_access);
2376 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2377 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2379 update_stateid(&stp->st_stateid);
2380 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2383 if (od->od_stateowner)
2384 nfs4_get_stateowner(od->od_stateowner);
2385 nfs4_unlock_state();
2390 * nfs4_unlock_state() called after encode
2393 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2396 struct nfs4_stateid *stp;
2398 dprintk("NFSD: nfsd4_close on file %.*s\n",
2399 (int)current_fh->fh_dentry->d_name.len,
2400 current_fh->fh_dentry->d_name.name);
2403 /* check close_lru for replay */
2404 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2406 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2407 &close->cl_stateowner, &stp, NULL)))
2410 * Return success, but first update the stateid.
2413 update_stateid(&stp->st_stateid);
2414 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2416 /* release_state_owner() calls nfsd_close() if needed */
2417 release_state_owner(stp, OPEN_STATE);
2419 if (close->cl_stateowner)
2420 nfs4_get_stateowner(close->cl_stateowner);
2421 nfs4_unlock_state();
2426 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2430 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2434 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2435 nfs4_unlock_state();
2442 * Lock owner state (byte-range locks)
2444 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2445 #define LOCK_HASH_BITS 8
2446 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2447 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2449 #define lockownerid_hashval(id) \
2450 ((id) & LOCK_HASH_MASK)
2452 static inline unsigned int
2453 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2454 struct xdr_netobj *ownername)
2456 return (file_hashval(inode) + cl_id
2457 + opaque_hashval(ownername->data, ownername->len))
2461 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2462 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2463 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2465 struct nfs4_stateid *
2466 find_stateid(stateid_t *stid, int flags)
2468 struct nfs4_stateid *local = NULL;
2469 u32 st_id = stid->si_stateownerid;
2470 u32 f_id = stid->si_fileid;
2471 unsigned int hashval;
2473 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2474 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2475 hashval = stateid_hashval(st_id, f_id);
2476 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2477 if ((local->st_stateid.si_stateownerid == st_id) &&
2478 (local->st_stateid.si_fileid == f_id))
2482 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2483 hashval = stateid_hashval(st_id, f_id);
2484 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2485 if ((local->st_stateid.si_stateownerid == st_id) &&
2486 (local->st_stateid.si_fileid == f_id))
2490 printk("NFSD: find_stateid: ERROR: no state flag\n");
2494 static struct nfs4_delegation *
2495 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2497 struct nfs4_file *fp = NULL;
2499 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2500 stid->si_boot, stid->si_stateownerid,
2501 stid->si_fileid, stid->si_generation);
2503 fp = find_file(ino);
2505 return find_delegation_file(fp, stid);
2510 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2511 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2512 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2513 * locking, this prevents us from being completely protocol-compliant. The
2514 * real solution to this problem is to start using unsigned file offsets in
2515 * the VFS, but this is a very deep change!
2518 nfs4_transform_lock_offset(struct file_lock *lock)
2520 if (lock->fl_start < 0)
2521 lock->fl_start = OFFSET_MAX;
2522 if (lock->fl_end < 0)
2523 lock->fl_end = OFFSET_MAX;
2527 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2529 struct nfs4_stateowner *local = NULL;
2532 if (hashval >= LOCK_HASH_SIZE)
2534 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2546 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2548 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2549 unsigned int hval = lockownerid_hashval(sop->so_id);
2551 deny->ld_sop = NULL;
2552 if (nfs4_verify_lock_stateowner(sop, hval)) {
2553 kref_get(&sop->so_ref);
2555 deny->ld_clientid = sop->so_client->cl_clientid;
2557 deny->ld_start = fl->fl_start;
2558 deny->ld_length = ~(u64)0;
2559 if (fl->fl_end != ~(u64)0)
2560 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2561 deny->ld_type = NFS4_READ_LT;
2562 if (fl->fl_type != F_RDLCK)
2563 deny->ld_type = NFS4_WRITE_LT;
2566 static struct nfs4_stateowner *
2567 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2569 struct nfs4_stateowner *local = NULL;
2572 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2573 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2574 if (!cmp_owner_str(local, owner, clid))
2582 static struct nfs4_stateowner *
2583 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2584 struct xdr_netobj *owner)
2586 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2587 struct nfs4_stateowner *op;
2589 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2590 if (cmp_owner_str(op, owner, clid))
2597 * Alloc a lock owner structure.
2598 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2601 * strhashval = lock_ownerstr_hashval
2602 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2605 static struct nfs4_stateowner *
2606 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2607 struct nfs4_stateowner *sop;
2608 struct nfs4_replay *rp;
2609 unsigned int idhashval;
2611 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2613 idhashval = lockownerid_hashval(current_ownerid);
2614 INIT_LIST_HEAD(&sop->so_idhash);
2615 INIT_LIST_HEAD(&sop->so_strhash);
2616 INIT_LIST_HEAD(&sop->so_perclient);
2617 INIT_LIST_HEAD(&sop->so_perfilestate);
2618 INIT_LIST_HEAD(&sop->so_perlockowner);
2619 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2621 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2622 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2623 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2624 sop->so_is_open_owner = 0;
2625 sop->so_id = current_ownerid++;
2626 sop->so_client = clp;
2627 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2628 sop->so_confirmed = 1;
2629 rp = &sop->so_replay;
2630 rp->rp_status = NFSERR_SERVERFAULT;
2632 rp->rp_buf = rp->rp_ibuf;
2636 struct nfs4_stateid *
2637 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2639 struct nfs4_stateid *stp;
2640 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2642 stp = nfs4_alloc_stateid();
2645 INIT_LIST_HEAD(&stp->st_hash);
2646 INIT_LIST_HEAD(&stp->st_perfile);
2647 INIT_LIST_HEAD(&stp->st_perfilestate);
2648 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2649 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2650 list_add(&stp->st_perfile, &fp->fi_perfile);
2652 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2653 stp->st_stateowner = sop;
2655 stp->st_stateid.si_boot = boot_time;
2656 stp->st_stateid.si_stateownerid = sop->so_id;
2657 stp->st_stateid.si_fileid = fp->fi_id;
2658 stp->st_stateid.si_generation = 0;
2659 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2660 stp->st_access_bmap = open_stp->st_access_bmap;
2661 stp->st_deny_bmap = open_stp->st_deny_bmap;
2668 check_lock_length(u64 offset, u64 length)
2670 return ((length == 0) || ((length != ~(u64)0) &&
2671 LOFF_OVERFLOW(offset, length)));
2678 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2680 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2681 struct nfs4_stateid *lock_stp;
2683 struct file_lock file_lock;
2684 struct file_lock *conflock;
2686 unsigned int strhashval;
2688 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2689 (long long) lock->lk_offset,
2690 (long long) lock->lk_length);
2692 if (nfs4_in_grace() && !lock->lk_reclaim)
2693 return nfserr_grace;
2694 if (!nfs4_in_grace() && lock->lk_reclaim)
2695 return nfserr_no_grace;
2697 if (check_lock_length(lock->lk_offset, lock->lk_length))
2698 return nfserr_inval;
2702 if (lock->lk_is_new) {
2704 * Client indicates that this is a new lockowner.
2705 * Use open owner and open stateid to create lock owner and lock
2708 struct nfs4_stateid *open_stp = NULL;
2709 struct nfs4_file *fp;
2711 status = nfserr_stale_clientid;
2712 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2713 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2717 /* is the new lock seqid presented by the client zero? */
2718 status = nfserr_bad_seqid;
2719 if (lock->v.new.lock_seqid != 0)
2722 /* validate and update open stateid and open seqid */
2723 status = nfs4_preprocess_seqid_op(current_fh,
2724 lock->lk_new_open_seqid,
2725 &lock->lk_new_open_stateid,
2726 CHECK_FH | OPEN_STATE,
2727 &open_sop, &open_stp,
2728 &lock->v.new.clientid);
2730 if (lock->lk_reclaim)
2731 status = nfserr_reclaim_bad;
2734 /* create lockowner and lock stateid */
2735 fp = open_stp->st_file;
2736 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2737 open_sop->so_client->cl_clientid.cl_id,
2738 &lock->v.new.owner);
2740 * If we already have this lock owner, the client is in
2741 * error (or our bookeeping is wrong!)
2742 * for asking for a 'new lock'.
2744 status = nfserr_bad_stateid;
2745 lock_sop = find_lockstateowner(&lock->v.new.owner,
2746 &lock->v.new.clientid);
2749 status = nfserr_resource;
2750 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2752 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2753 fp, open_stp)) == NULL) {
2754 release_stateowner(lock->lk_stateowner);
2755 lock->lk_stateowner = NULL;
2758 /* bump the open seqid used to create the lock */
2759 open_sop->so_seqid++;
2761 /* lock (lock owner + lock stateid) already exists */
2762 status = nfs4_preprocess_seqid_op(current_fh,
2763 lock->lk_old_lock_seqid,
2764 &lock->lk_old_lock_stateid,
2765 CHECK_FH | LOCK_STATE,
2766 &lock->lk_stateowner, &lock_stp, NULL);
2770 /* lock->lk_stateowner and lock_stp have been created or found */
2771 filp = lock_stp->st_vfs_file;
2773 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2774 printk("NFSD: nfsd4_lock: permission denied!\n");
2778 locks_init_lock(&file_lock);
2779 switch (lock->lk_type) {
2782 file_lock.fl_type = F_RDLCK;
2785 case NFS4_WRITEW_LT:
2786 file_lock.fl_type = F_WRLCK;
2789 status = nfserr_inval;
2792 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2793 file_lock.fl_pid = current->tgid;
2794 file_lock.fl_file = filp;
2795 file_lock.fl_flags = FL_POSIX;
2797 file_lock.fl_start = lock->lk_offset;
2798 if ((lock->lk_length == ~(u64)0) ||
2799 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2800 file_lock.fl_end = ~(u64)0;
2802 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2803 nfs4_transform_lock_offset(&file_lock);
2806 * Try to lock the file in the VFS.
2807 * Note: locks.c uses the BKL to protect the inode's lock list.
2810 status = posix_lock_file(filp, &file_lock);
2811 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2812 file_lock.fl_ops->fl_release_private(&file_lock);
2813 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2815 case 0: /* success! */
2816 update_stateid(&lock_stp->st_stateid);
2817 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2821 goto conflicting_lock;
2823 status = nfserr_deadlock;
2825 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2826 goto out_destroy_new_stateid;
2830 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2831 status = nfserr_denied;
2832 /* XXX There is a race here. Future patch needed to provide
2833 * an atomic posix_lock_and_test_file
2835 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2836 status = nfserr_serverfault;
2839 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2841 out_destroy_new_stateid:
2842 if (lock->lk_is_new) {
2843 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2845 * An error encountered after instantiation of the new
2846 * stateid has forced us to destroy it.
2848 if (!seqid_mutating_err(status))
2849 open_sop->so_seqid--;
2851 release_state_owner(lock_stp, LOCK_STATE);
2854 if (lock->lk_stateowner)
2855 nfs4_get_stateowner(lock->lk_stateowner);
2856 nfs4_unlock_state();
2864 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2866 struct inode *inode;
2868 struct file_lock file_lock;
2869 struct file_lock *conflicting_lock;
2872 if (nfs4_in_grace())
2873 return nfserr_grace;
2875 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2876 return nfserr_inval;
2878 lockt->lt_stateowner = NULL;
2881 status = nfserr_stale_clientid;
2882 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2883 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2887 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2888 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2889 if (status == nfserr_symlink)
2890 status = nfserr_inval;
2894 inode = current_fh->fh_dentry->d_inode;
2895 locks_init_lock(&file_lock);
2896 switch (lockt->lt_type) {
2899 file_lock.fl_type = F_RDLCK;
2902 case NFS4_WRITEW_LT:
2903 file_lock.fl_type = F_WRLCK;
2906 printk("NFSD: nfs4_lockt: bad lock type!\n");
2907 status = nfserr_inval;
2911 lockt->lt_stateowner = find_lockstateowner_str(inode,
2912 &lockt->lt_clientid, &lockt->lt_owner);
2913 if (lockt->lt_stateowner)
2914 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2915 file_lock.fl_pid = current->tgid;
2916 file_lock.fl_flags = FL_POSIX;
2918 file_lock.fl_start = lockt->lt_offset;
2919 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2920 file_lock.fl_end = ~(u64)0;
2922 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2924 nfs4_transform_lock_offset(&file_lock);
2926 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2927 * since LOCKT doesn't require an OPEN, and therefore a struct
2928 * file may not exist, pass posix_test_lock a struct file with
2929 * only the dentry:inode set.
2931 memset(&file, 0, sizeof (struct file));
2932 file.f_dentry = current_fh->fh_dentry;
2935 conflicting_lock = posix_test_lock(&file, &file_lock);
2936 if (conflicting_lock) {
2937 status = nfserr_denied;
2938 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2941 nfs4_unlock_state();
2946 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2948 struct nfs4_stateid *stp;
2949 struct file *filp = NULL;
2950 struct file_lock file_lock;
2953 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2954 (long long) locku->lu_offset,
2955 (long long) locku->lu_length);
2957 if (check_lock_length(locku->lu_offset, locku->lu_length))
2958 return nfserr_inval;
2962 if ((status = nfs4_preprocess_seqid_op(current_fh,
2965 CHECK_FH | LOCK_STATE,
2966 &locku->lu_stateowner, &stp, NULL)))
2969 filp = stp->st_vfs_file;
2971 locks_init_lock(&file_lock);
2972 file_lock.fl_type = F_UNLCK;
2973 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2974 file_lock.fl_pid = current->tgid;
2975 file_lock.fl_file = filp;
2976 file_lock.fl_flags = FL_POSIX;
2977 file_lock.fl_start = locku->lu_offset;
2979 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2980 file_lock.fl_end = ~(u64)0;
2982 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2983 nfs4_transform_lock_offset(&file_lock);
2986 * Try to unlock the file in the VFS.
2988 status = posix_lock_file(filp, &file_lock);
2989 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2990 file_lock.fl_ops->fl_release_private(&file_lock);
2992 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2996 * OK, unlock succeeded; the only thing left to do is update the stateid.
2998 update_stateid(&stp->st_stateid);
2999 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3002 if (locku->lu_stateowner)
3003 nfs4_get_stateowner(locku->lu_stateowner);
3004 nfs4_unlock_state();
3008 status = nfserrno(status);
3014 * 1: locks held by lockowner
3015 * 0: no locks held by lockowner
3018 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3020 struct file_lock **flpp;
3021 struct inode *inode = filp->f_dentry->d_inode;
3025 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3026 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3036 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3038 clientid_t *clid = &rlockowner->rl_clientid;
3039 struct nfs4_stateowner *local = NULL;
3040 struct xdr_netobj *owner = &rlockowner->rl_owner;
3043 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3044 clid->cl_boot, clid->cl_id);
3046 /* XXX check for lease expiration */
3048 status = nfserr_stale_clientid;
3049 if (STALE_CLIENTID(clid)) {
3050 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3057 local = find_lockstateowner(owner, clid);
3059 struct nfs4_stateid *stp;
3061 /* check for any locks held by any stateid
3062 * associated with the (lock) stateowner */
3063 status = nfserr_locks_held;
3064 list_for_each_entry(stp, &local->so_perfilestate,
3066 if (check_for_locks(stp->st_vfs_file, local))
3069 /* no locks held by (lock) stateowner */
3071 release_stateowner(local);
3074 nfs4_unlock_state();
3078 static inline struct nfs4_client_reclaim *
3079 alloc_reclaim(int namelen)
3081 struct nfs4_client_reclaim *crp = NULL;
3083 crp = kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3086 crp->cr_name.data = kmalloc(namelen, GFP_KERNEL);
3087 if (!crp->cr_name.data) {
3095 * failure => all reset bets are off, nfserr_no_grace...
3098 nfs4_client_to_reclaim(char *name, int namlen)
3100 unsigned int strhashval;
3101 struct nfs4_client_reclaim *crp = NULL;
3103 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", namlen, name);
3104 crp = alloc_reclaim(namlen);
3107 strhashval = clientstr_hashval(name, namlen);
3108 INIT_LIST_HEAD(&crp->cr_strhash);
3109 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3110 memcpy(crp->cr_name.data, name, namlen);
3111 crp->cr_name.len = namlen;
3112 reclaim_str_hashtbl_size++;
3117 nfs4_release_reclaim(void)
3119 struct nfs4_client_reclaim *crp = NULL;
3122 BUG_ON(!nfs4_reclaim_init);
3123 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3124 while (!list_empty(&reclaim_str_hashtbl[i])) {
3125 crp = list_entry(reclaim_str_hashtbl[i].next,
3126 struct nfs4_client_reclaim, cr_strhash);
3127 list_del(&crp->cr_strhash);
3128 kfree(crp->cr_name.data);
3130 reclaim_str_hashtbl_size--;
3133 BUG_ON(reclaim_str_hashtbl_size);
3137 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3138 struct nfs4_client_reclaim *
3139 nfs4_find_reclaim_client(clientid_t *clid)
3141 unsigned int strhashval;
3142 struct nfs4_client *clp;
3143 struct nfs4_client_reclaim *crp = NULL;
3146 /* find clientid in conf_id_hashtbl */
3147 clp = find_confirmed_client(clid);
3151 dprintk("NFSD: nfs4_find_reclaim_client for %.*s\n",
3152 clp->cl_name.len, clp->cl_name.data);
3154 /* find clp->cl_name in reclaim_str_hashtbl */
3155 strhashval = clientstr_hashval(clp->cl_name.data, clp->cl_name.len);
3156 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3157 if (cmp_name(&crp->cr_name, &clp->cl_name)) {
3165 * Called from OPEN. Look for clientid in reclaim list.
3168 nfs4_check_open_reclaim(clientid_t *clid)
3170 struct nfs4_client_reclaim *crp;
3172 if ((crp = nfs4_find_reclaim_client(clid)) == NULL)
3173 return nfserr_reclaim_bad;
3179 * Start and stop routines
3183 __nfs4_state_init(void)
3188 if (!nfs4_reclaim_init) {
3189 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3190 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3191 reclaim_str_hashtbl_size = 0;
3192 nfs4_reclaim_init = 1;
3194 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3195 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3196 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3197 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3198 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3200 for (i = 0; i < FILE_HASH_SIZE; i++) {
3201 INIT_LIST_HEAD(&file_hashtbl[i]);
3203 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3204 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3205 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3207 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3208 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3209 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3211 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3212 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3213 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3215 memset(&zerostateid, 0, sizeof(stateid_t));
3216 memset(&onestateid, ~0, sizeof(stateid_t));
3218 INIT_LIST_HEAD(&close_lru);
3219 INIT_LIST_HEAD(&client_lru);
3220 INIT_LIST_HEAD(&del_recall_lru);
3221 spin_lock_init(&recall_lock);
3222 boot_time = get_seconds();
3223 grace_time = max(old_lease_time, lease_time);
3224 if (reclaim_str_hashtbl_size == 0)
3227 printk("NFSD: starting %ld-second grace period\n", grace_time);
3228 grace_end = boot_time + grace_time;
3229 INIT_WORK(&laundromat_work,laundromat_main, NULL);
3230 schedule_delayed_work(&laundromat_work, NFSD_LEASE_TIME*HZ);
3234 nfs4_state_init(void)
3240 status = nfsd4_init_slabs();
3243 __nfs4_state_init();
3251 return get_seconds() < grace_end;
3257 printk("NFSD: ERROR in reboot recovery. State reclaims will fail.\n");
3258 grace_end = get_seconds();
3262 nfs4_lease_time(void)
3268 __nfs4_state_shutdown(void)
3271 struct nfs4_client *clp = NULL;
3272 struct nfs4_delegation *dp = NULL;
3273 struct nfs4_stateowner *sop = NULL;
3274 struct list_head *pos, *next, reaplist;
3276 list_for_each_safe(pos, next, &close_lru) {
3277 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3278 list_del(&sop->so_close_lru);
3279 nfs4_put_stateowner(sop);
3282 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3283 while (!list_empty(&conf_id_hashtbl[i])) {
3284 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3287 while (!list_empty(&unconf_str_hashtbl[i])) {
3288 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3292 INIT_LIST_HEAD(&reaplist);
3293 spin_lock(&recall_lock);
3294 list_for_each_safe(pos, next, &del_recall_lru) {
3295 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3296 list_move(&dp->dl_recall_lru, &reaplist);
3298 spin_unlock(&recall_lock);
3299 list_for_each_safe(pos, next, &reaplist) {
3300 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3301 list_del_init(&dp->dl_recall_lru);
3302 unhash_delegation(dp);
3305 release_all_files();
3306 cancel_delayed_work(&laundromat_work);
3307 flush_scheduled_work();
3309 dprintk("NFSD: list_add_perfile %d list_del_perfile %d\n",
3310 list_add_perfile, list_del_perfile);
3311 dprintk("NFSD: add_perclient %d del_perclient %d\n",
3312 add_perclient, del_perclient);
3313 dprintk("NFSD: vfsopen %d vfsclose %d\n",
3315 dprintk("NFSD: alloc_delegation %d free_delegation %d\n",
3316 alloc_delegation, free_delegation);
3321 nfs4_state_shutdown(void)
3324 nfs4_release_reclaim();
3325 __nfs4_state_shutdown();
3327 nfs4_unlock_state();
3331 * Called when leasetime is changed.
3333 * if nfsd is not started, simply set the global lease.
3335 * if nfsd(s) are running, lease change requires nfsv4 state to be reset.
3336 * e.g: boot_time is reset, existing nfs4_client structs are
3337 * used to fill reclaim_str_hashtbl, then all state (except for the
3338 * reclaim_str_hashtbl) is re-initialized.
3340 * if the old lease time is greater than the new lease time, the grace
3341 * period needs to be set to the old lease time to allow clients to reclaim
3342 * their state. XXX - we may want to set the grace period == lease time
3343 * after an initial grace period == old lease time
3345 * if an error occurs in this process, the new lease is set, but the server
3346 * will not honor OPEN or LOCK reclaims, and will return nfserr_no_grace
3347 * which means OPEN/LOCK/READ/WRITE will fail during grace period.
3349 * clients will attempt to reset all state with SETCLIENTID/CONFIRM, and
3350 * OPEN and LOCK reclaims.
3353 nfs4_reset_lease(time_t leasetime)
3355 struct nfs4_client *clp;
3358 printk("NFSD: New leasetime %ld\n",leasetime);
3362 old_lease_time = lease_time;
3363 lease_time = leasetime;
3365 nfs4_release_reclaim();
3367 /* populate reclaim_str_hashtbl with current confirmed nfs4_clientid */
3368 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3369 list_for_each_entry(clp, &conf_id_hashtbl[i], cl_idhash) {
3370 if (!nfs4_client_to_reclaim(clp->cl_name.data,
3371 clp->cl_name.len)) {
3372 nfs4_release_reclaim();
3378 __nfs4_state_shutdown();
3379 __nfs4_state_init();
3380 nfs4_unlock_state();