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 user_lease_time = 90;
57 static time_t boot_time;
58 static int in_grace = 1;
59 static u32 current_clientid = 1;
60 static u32 current_ownerid = 1;
61 static u32 current_fileid = 1;
62 static u32 current_delegid = 1;
64 static stateid_t zerostateid; /* bits all 0 */
65 static stateid_t onestateid; /* bits all 1 */
67 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
68 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
70 /* forward declarations */
71 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
72 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
73 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
78 * protects clientid_hashtbl[], clientstr_hashtbl[],
79 * unconfstr_hashtbl[], uncofid_hashtbl[].
81 static DECLARE_MUTEX(client_sema);
83 static kmem_cache_t *stateowner_slab = NULL;
84 static kmem_cache_t *file_slab = NULL;
85 static kmem_cache_t *stateid_slab = NULL;
86 static kmem_cache_t *deleg_slab = NULL;
95 nfs4_unlock_state(void)
101 opaque_hashval(const void *ptr, int nbytes)
103 unsigned char *cptr = (unsigned char *) ptr;
113 /* forward declarations */
114 static void release_stateowner(struct nfs4_stateowner *sop);
115 static void release_stateid(struct nfs4_stateid *stp, int flags);
121 /* recall_lock protects the del_recall_lru */
122 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
123 static struct list_head del_recall_lru;
126 free_nfs4_file(struct kref *kref)
128 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
129 list_del(&fp->fi_hash);
131 kmem_cache_free(file_slab, fp);
135 put_nfs4_file(struct nfs4_file *fi)
137 kref_put(&fi->fi_ref, free_nfs4_file);
141 get_nfs4_file(struct nfs4_file *fi)
143 kref_get(&fi->fi_ref);
146 static struct nfs4_delegation *
147 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
149 struct nfs4_delegation *dp;
150 struct nfs4_file *fp = stp->st_file;
151 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
153 dprintk("NFSD alloc_init_deleg\n");
154 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
157 INIT_LIST_HEAD(&dp->dl_del_perfile);
158 INIT_LIST_HEAD(&dp->dl_del_perclnt);
159 INIT_LIST_HEAD(&dp->dl_recall_lru);
164 get_file(stp->st_vfs_file);
165 dp->dl_vfs_file = stp->st_vfs_file;
167 dp->dl_recall.cbr_dp = NULL;
168 dp->dl_recall.cbr_ident = cb->cb_ident;
169 dp->dl_recall.cbr_trunc = 0;
170 dp->dl_stateid.si_boot = boot_time;
171 dp->dl_stateid.si_stateownerid = current_delegid++;
172 dp->dl_stateid.si_fileid = 0;
173 dp->dl_stateid.si_generation = 0;
174 dp->dl_fhlen = current_fh->fh_handle.fh_size;
175 memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base,
176 current_fh->fh_handle.fh_size);
178 atomic_set(&dp->dl_count, 1);
179 list_add(&dp->dl_del_perfile, &fp->fi_delegations);
180 list_add(&dp->dl_del_perclnt, &clp->cl_del_perclnt);
185 nfs4_put_delegation(struct nfs4_delegation *dp)
187 if (atomic_dec_and_test(&dp->dl_count)) {
188 dprintk("NFSD: freeing dp %p\n",dp);
189 put_nfs4_file(dp->dl_file);
190 kmem_cache_free(deleg_slab, dp);
194 /* Remove the associated file_lock first, then remove the delegation.
195 * lease_modify() is called to remove the FS_LEASE file_lock from
196 * the i_flock list, eventually calling nfsd's lock_manager
197 * fl_release_callback.
200 nfs4_close_delegation(struct nfs4_delegation *dp)
202 struct file *filp = dp->dl_vfs_file;
204 dprintk("NFSD: close_delegation dp %p\n",dp);
205 dp->dl_vfs_file = NULL;
206 /* The following nfsd_close may not actually close the file,
207 * but we want to remove the lease in any case. */
209 setlease(filp, F_UNLCK, &dp->dl_flock);
213 /* Called under the state lock. */
215 unhash_delegation(struct nfs4_delegation *dp)
217 list_del_init(&dp->dl_del_perfile);
218 list_del_init(&dp->dl_del_perclnt);
219 spin_lock(&recall_lock);
220 list_del_init(&dp->dl_recall_lru);
221 spin_unlock(&recall_lock);
222 nfs4_close_delegation(dp);
223 nfs4_put_delegation(dp);
230 /* Hash tables for nfs4_clientid state */
231 #define CLIENT_HASH_BITS 4
232 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
233 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
235 #define clientid_hashval(id) \
236 ((id) & CLIENT_HASH_MASK)
237 #define clientstr_hashval(name) \
238 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
240 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
241 * used in reboot/reset lease grace period processing
243 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
244 * setclientid_confirmed info.
246 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
249 * client_lru holds client queue ordered by nfs4_client.cl_time
252 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
253 * for last close replay.
255 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
256 static int reclaim_str_hashtbl_size = 0;
257 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
258 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
259 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
260 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
261 static struct list_head client_lru;
262 static struct list_head close_lru;
265 renew_client(struct nfs4_client *clp)
268 * Move client to the end to the LRU list.
270 dprintk("renewing client (clientid %08x/%08x)\n",
271 clp->cl_clientid.cl_boot,
272 clp->cl_clientid.cl_id);
273 list_move_tail(&clp->cl_lru, &client_lru);
274 clp->cl_time = get_seconds();
277 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
279 STALE_CLIENTID(clientid_t *clid)
281 if (clid->cl_boot == boot_time)
283 dprintk("NFSD stale clientid (%08x/%08x)\n",
284 clid->cl_boot, clid->cl_id);
289 * XXX Should we use a slab cache ?
290 * This type of memory management is somewhat inefficient, but we use it
291 * anyway since SETCLIENTID is not a common operation.
293 static inline struct nfs4_client *
294 alloc_client(struct xdr_netobj name)
296 struct nfs4_client *clp;
298 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
299 memset(clp, 0, sizeof(*clp));
300 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
301 memcpy(clp->cl_name.data, name.data, name.len);
302 clp->cl_name.len = name.len;
313 free_client(struct nfs4_client *clp)
315 if (clp->cl_cred.cr_group_info)
316 put_group_info(clp->cl_cred.cr_group_info);
317 kfree(clp->cl_name.data);
322 put_nfs4_client(struct nfs4_client *clp)
324 if (atomic_dec_and_test(&clp->cl_count))
329 expire_client(struct nfs4_client *clp)
331 struct nfs4_stateowner *sop;
332 struct nfs4_delegation *dp;
333 struct nfs4_callback *cb = &clp->cl_callback;
334 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
335 struct list_head reaplist;
337 dprintk("NFSD: expire_client cl_count %d\n",
338 atomic_read(&clp->cl_count));
340 /* shutdown rpc client, ending any outstanding recall rpcs */
341 if (atomic_read(&cb->cb_set) == 1 && clnt) {
342 rpc_shutdown_client(clnt);
343 clnt = clp->cl_callback.cb_client = NULL;
346 INIT_LIST_HEAD(&reaplist);
347 spin_lock(&recall_lock);
348 while (!list_empty(&clp->cl_del_perclnt)) {
349 dp = list_entry(clp->cl_del_perclnt.next, struct nfs4_delegation, dl_del_perclnt);
350 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
352 list_del_init(&dp->dl_del_perclnt);
353 list_move(&dp->dl_recall_lru, &reaplist);
355 spin_unlock(&recall_lock);
356 while (!list_empty(&reaplist)) {
357 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
358 list_del_init(&dp->dl_recall_lru);
359 unhash_delegation(dp);
361 list_del(&clp->cl_idhash);
362 list_del(&clp->cl_strhash);
363 list_del(&clp->cl_lru);
364 while (!list_empty(&clp->cl_perclient)) {
365 sop = list_entry(clp->cl_perclient.next, struct nfs4_stateowner, so_perclient);
366 release_stateowner(sop);
368 put_nfs4_client(clp);
371 static struct nfs4_client *
372 create_client(struct xdr_netobj name, char *recdir) {
373 struct nfs4_client *clp;
375 if (!(clp = alloc_client(name)))
377 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
378 atomic_set(&clp->cl_count, 1);
379 atomic_set(&clp->cl_callback.cb_set, 0);
380 clp->cl_callback.cb_parsed = 0;
381 INIT_LIST_HEAD(&clp->cl_idhash);
382 INIT_LIST_HEAD(&clp->cl_strhash);
383 INIT_LIST_HEAD(&clp->cl_perclient);
384 INIT_LIST_HEAD(&clp->cl_del_perclnt);
385 INIT_LIST_HEAD(&clp->cl_lru);
391 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
392 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
396 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
397 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
398 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
402 copy_cred(struct svc_cred *target, struct svc_cred *source) {
404 target->cr_uid = source->cr_uid;
405 target->cr_gid = source->cr_gid;
406 target->cr_group_info = source->cr_group_info;
407 get_group_info(target->cr_group_info);
411 same_name(const char *n1, const char *n2) {
412 return 0 == memcmp(n1, n2, HEXDIR_LEN);
416 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
417 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
421 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
422 return((cl1->cl_boot == cl2->cl_boot) &&
423 (cl1->cl_id == cl2->cl_id));
426 /* XXX what about NGROUP */
428 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
429 return(cr1->cr_uid == cr2->cr_uid);
434 gen_clid(struct nfs4_client *clp) {
435 clp->cl_clientid.cl_boot = boot_time;
436 clp->cl_clientid.cl_id = current_clientid++;
440 gen_confirm(struct nfs4_client *clp) {
445 p = (u32 *)clp->cl_confirm.data;
451 check_name(struct xdr_netobj name) {
455 if (name.len > NFS4_OPAQUE_LIMIT) {
456 printk("NFSD: check_name: name too long(%d)!\n", name.len);
463 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
465 unsigned int idhashval;
467 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
468 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
469 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
470 list_add_tail(&clp->cl_lru, &client_lru);
471 clp->cl_time = get_seconds();
475 move_to_confirmed(struct nfs4_client *clp)
477 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
478 unsigned int strhashval;
480 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
481 list_del_init(&clp->cl_strhash);
482 list_del_init(&clp->cl_idhash);
483 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
484 strhashval = clientstr_hashval(clp->cl_recdir);
485 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
489 static struct nfs4_client *
490 find_confirmed_client(clientid_t *clid)
492 struct nfs4_client *clp;
493 unsigned int idhashval = clientid_hashval(clid->cl_id);
495 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
496 if (cmp_clid(&clp->cl_clientid, clid))
502 static struct nfs4_client *
503 find_unconfirmed_client(clientid_t *clid)
505 struct nfs4_client *clp;
506 unsigned int idhashval = clientid_hashval(clid->cl_id);
508 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
509 if (cmp_clid(&clp->cl_clientid, clid))
515 static struct nfs4_client *
516 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
518 struct nfs4_client *clp;
520 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
521 if (same_name(clp->cl_recdir, dname))
527 static struct nfs4_client *
528 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
530 struct nfs4_client *clp;
532 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
533 if (same_name(clp->cl_recdir, dname))
539 /* a helper function for parse_callback */
541 parse_octet(unsigned int *lenp, char **addrp)
543 unsigned int len = *lenp;
555 if ((c < '0') || (c > '9')) {
561 n = (n * 10) + (c - '0');
572 /* parse and set the setclientid ipv4 callback address */
574 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
579 u32 addrlen = addr_len;
580 char *addr = addr_val;
585 for(i = 4; i > 0 ; i--) {
586 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
589 cbaddr |= (temp << shift);
597 for(i = 2; i > 0 ; i--) {
598 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
601 cbport |= (temp << shift);
610 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
612 struct nfs4_callback *cb = &clp->cl_callback;
614 /* Currently, we only support tcp for the callback channel */
615 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
618 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
619 &cb->cb_addr, &cb->cb_port)))
621 cb->cb_prog = se->se_callback_prog;
622 cb->cb_ident = se->se_callback_ident;
626 printk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
627 "will not receive delegations\n",
628 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
635 * RFC 3010 has a complex implmentation description of processing a
636 * SETCLIENTID request consisting of 5 bullets, labeled as
637 * CASE0 - CASE4 below.
640 * callback information will be processed in a future patch
642 * an unconfirmed record is added when:
643 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
644 * CASE 1: confirmed record found with matching name, principal,
645 * verifier, and clientid.
646 * CASE 2: confirmed record found with matching name, principal,
647 * and there is no unconfirmed record with matching
650 * an unconfirmed record is replaced when:
651 * CASE 3: confirmed record found with matching name, principal,
652 * and an unconfirmed record is found with matching
653 * name, principal, and with clientid and
654 * confirm that does not match the confirmed record.
655 * CASE 4: there is no confirmed record with matching name and
656 * principal. there is an unconfirmed record with
657 * matching name, principal.
659 * an unconfirmed record is deleted when:
660 * CASE 1: an unconfirmed record that matches input name, verifier,
661 * and confirmed clientid.
662 * CASE 4: any unconfirmed records with matching name and principal
663 * that exist after an unconfirmed record has been replaced
664 * as described above.
668 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
670 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
671 struct xdr_netobj clname = {
672 .len = setclid->se_namelen,
673 .data = setclid->se_name,
675 nfs4_verifier clverifier = setclid->se_verf;
676 unsigned int strhashval;
677 struct nfs4_client *conf, *unconf, *new;
679 char dname[HEXDIR_LEN];
681 status = nfserr_inval;
682 if (!check_name(clname))
685 status = nfs4_make_rec_clidname(dname, &clname);
690 * XXX The Duplicate Request Cache (DRC) has been checked (??)
691 * We get here on a DRC miss.
694 strhashval = clientstr_hashval(dname);
697 conf = find_confirmed_client_by_str(dname, strhashval);
701 * clname match, confirmed, different principal
702 * or different ip_address
704 status = nfserr_clid_inuse;
705 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
706 || conf->cl_addr != ip_addr) {
707 printk("NFSD: setclientid: string in use by client"
708 "(clientid %08x/%08x)\n",
709 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
713 unconf = find_unconfirmed_client_by_str(dname, strhashval);
714 status = nfserr_resource;
718 * placed first, because it is the normal case.
721 expire_client(unconf);
722 new = create_client(clname, dname);
725 copy_verf(new, &clverifier);
726 new->cl_addr = ip_addr;
727 copy_cred(&new->cl_cred,&rqstp->rq_cred);
730 gen_callback(new, setclid);
731 add_to_unconfirmed(new, strhashval);
732 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
735 * cl_name match, confirmed, principal match
736 * verifier match: probable callback update
738 * remove any unconfirmed nfs4_client with
739 * matching cl_name, cl_verifier, and cl_clientid
741 * create and insert an unconfirmed nfs4_client with same
742 * cl_name, cl_verifier, and cl_clientid as existing
743 * nfs4_client, but with the new callback info and a
747 /* Note this is removing unconfirmed {*x***},
748 * which is stronger than RFC recommended {vxc**}.
749 * This has the advantage that there is at most
750 * one {*x***} in either list at any time.
752 expire_client(unconf);
754 new = create_client(clname, dname);
757 copy_verf(new,&conf->cl_verifier);
758 new->cl_addr = ip_addr;
759 copy_cred(&new->cl_cred,&rqstp->rq_cred);
760 copy_clid(new, conf);
762 gen_callback(new, setclid);
763 add_to_unconfirmed(new,strhashval);
764 } else if (!unconf) {
767 * clname match, confirmed, principal match
768 * verfier does not match
769 * no unconfirmed. create a new unconfirmed nfs4_client
770 * using input clverifier, clname, and callback info
771 * and generate a new cl_clientid and cl_confirm.
773 new = create_client(clname, dname);
776 copy_verf(new,&clverifier);
777 new->cl_addr = ip_addr;
778 copy_cred(&new->cl_cred,&rqstp->rq_cred);
781 gen_callback(new, setclid);
782 add_to_unconfirmed(new, strhashval);
783 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
786 * confirmed found (name, principal match)
787 * confirmed verifier does not match input clverifier
789 * unconfirmed found (name match)
790 * confirmed->cl_confirm != unconfirmed->cl_confirm
792 * remove unconfirmed.
794 * create an unconfirmed nfs4_client
795 * with same cl_name as existing confirmed nfs4_client,
796 * but with new callback info, new cl_clientid,
797 * new cl_verifier and a new cl_confirm
799 expire_client(unconf);
800 new = create_client(clname, dname);
803 copy_verf(new,&clverifier);
804 new->cl_addr = ip_addr;
805 copy_cred(&new->cl_cred,&rqstp->rq_cred);
808 gen_callback(new, setclid);
809 add_to_unconfirmed(new, strhashval);
811 /* No cases hit !!! */
812 status = nfserr_inval;
816 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
817 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
818 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
827 * RFC 3010 has a complex implmentation description of processing a
828 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
829 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
831 * NOTE: callback information will be processed here in a future patch
834 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
836 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
837 struct nfs4_client *clp, *conf = NULL, *unconf = NULL;
838 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
839 clientid_t * clid = &setclientid_confirm->sc_clientid;
842 if (STALE_CLIENTID(clid))
843 return nfserr_stale_clientid;
845 * XXX The Duplicate Request Cache (DRC) has been checked (??)
846 * We get here on a DRC miss.
850 clp = find_confirmed_client(clid);
852 status = nfserr_clid_inuse;
853 if (clp->cl_addr != ip_addr) {
854 printk("NFSD: setclientid: string in use by client"
855 "(clientid %08x/%08x)\n",
856 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
861 clp = find_unconfirmed_client(clid);
863 status = nfserr_clid_inuse;
864 if (clp->cl_addr != ip_addr) {
865 printk("NFSD: setclientid: string in use by client"
866 "(clientid %08x/%08x)\n",
867 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
872 if ((conf && unconf) &&
873 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
874 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
875 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
876 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
878 * unconf record that matches input clientid and input confirm.
879 * conf record that matches input clientid.
880 * conf and unconf records match names, verifiers
882 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
883 status = nfserr_clid_inuse;
885 /* XXX: We just turn off callbacks until we can handle
886 * change request correctly. */
888 clp->cl_callback.cb_parsed = 0;
890 expire_client(unconf);
894 } else if ((conf && !unconf) ||
896 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
897 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
899 * conf record that matches input clientid.
900 * if unconf record matches input clientid, then
901 * unconf->cl_name or unconf->cl_verifier don't match the
904 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred)) {
905 status = nfserr_clid_inuse;
910 } else if (!conf && unconf
911 && cmp_verf(&unconf->cl_confirm, &confirm)) {
913 * conf record not found.
914 * unconf record found.
915 * unconf->cl_confirm matches input confirm
917 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
918 status = nfserr_clid_inuse;
921 clientstr_hashval(unconf->cl_recdir);
922 conf = find_confirmed_client_by_str(unconf->cl_recdir,
928 move_to_confirmed(unconf);
931 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
932 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
935 * conf record not found, or if conf, conf->cl_confirm does not
936 * match input confirm.
937 * unconf record not found, or if unconf, unconf->cl_confirm
938 * does not match input confirm.
940 status = nfserr_stale_clientid;
942 /* check that we have hit one of the cases...*/
943 status = nfserr_clid_inuse;
947 nfsd4_probe_callback(clp);
953 * Open owner state (share locks)
956 /* hash tables for nfs4_stateowner */
957 #define OWNER_HASH_BITS 8
958 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
959 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
961 #define ownerid_hashval(id) \
962 ((id) & OWNER_HASH_MASK)
963 #define ownerstr_hashval(clientid, ownername) \
964 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
966 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
967 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
969 /* hash table for nfs4_file */
970 #define FILE_HASH_BITS 8
971 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
972 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
973 /* hash table for (open)nfs4_stateid */
974 #define STATEID_HASH_BITS 10
975 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
976 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
978 #define file_hashval(x) \
979 hash_ptr(x, FILE_HASH_BITS)
980 #define stateid_hashval(owner_id, file_id) \
981 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
983 static struct list_head file_hashtbl[FILE_HASH_SIZE];
984 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
986 /* OPEN Share state helper functions */
987 static inline struct nfs4_file *
988 alloc_init_file(struct inode *ino)
990 struct nfs4_file *fp;
991 unsigned int hashval = file_hashval(ino);
993 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
995 kref_init(&fp->fi_ref);
996 INIT_LIST_HEAD(&fp->fi_hash);
997 INIT_LIST_HEAD(&fp->fi_stateids);
998 INIT_LIST_HEAD(&fp->fi_delegations);
999 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
1000 fp->fi_inode = igrab(ino);
1001 fp->fi_id = current_fileid++;
1008 nfsd4_free_slab(kmem_cache_t **slab)
1014 status = kmem_cache_destroy(*slab);
1020 nfsd4_free_slabs(void)
1022 nfsd4_free_slab(&stateowner_slab);
1023 nfsd4_free_slab(&file_slab);
1024 nfsd4_free_slab(&stateid_slab);
1025 nfsd4_free_slab(&deleg_slab);
1029 nfsd4_init_slabs(void)
1031 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1032 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1033 if (stateowner_slab == NULL)
1035 file_slab = kmem_cache_create("nfsd4_files",
1036 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1037 if (file_slab == NULL)
1039 stateid_slab = kmem_cache_create("nfsd4_stateids",
1040 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1041 if (stateid_slab == NULL)
1043 deleg_slab = kmem_cache_create("nfsd4_delegations",
1044 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1045 if (deleg_slab == NULL)
1050 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1055 nfs4_free_stateowner(struct kref *kref)
1057 struct nfs4_stateowner *sop =
1058 container_of(kref, struct nfs4_stateowner, so_ref);
1059 kfree(sop->so_owner.data);
1060 kmem_cache_free(stateowner_slab, sop);
1063 static inline struct nfs4_stateowner *
1064 alloc_stateowner(struct xdr_netobj *owner)
1066 struct nfs4_stateowner *sop;
1068 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1069 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1070 memcpy(sop->so_owner.data, owner->data, owner->len);
1071 sop->so_owner.len = owner->len;
1072 kref_init(&sop->so_ref);
1075 kmem_cache_free(stateowner_slab, sop);
1080 static struct nfs4_stateowner *
1081 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1082 struct nfs4_stateowner *sop;
1083 struct nfs4_replay *rp;
1084 unsigned int idhashval;
1086 if (!(sop = alloc_stateowner(&open->op_owner)))
1088 idhashval = ownerid_hashval(current_ownerid);
1089 INIT_LIST_HEAD(&sop->so_idhash);
1090 INIT_LIST_HEAD(&sop->so_strhash);
1091 INIT_LIST_HEAD(&sop->so_perclient);
1092 INIT_LIST_HEAD(&sop->so_perfilestate);
1093 INIT_LIST_HEAD(&sop->so_perlockowner); /* not used */
1094 INIT_LIST_HEAD(&sop->so_close_lru);
1096 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1097 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1098 list_add(&sop->so_perclient, &clp->cl_perclient);
1099 sop->so_is_open_owner = 1;
1100 sop->so_id = current_ownerid++;
1101 sop->so_client = clp;
1102 sop->so_seqid = open->op_seqid;
1103 sop->so_confirmed = 0;
1104 rp = &sop->so_replay;
1105 rp->rp_status = NFSERR_SERVERFAULT;
1107 rp->rp_buf = rp->rp_ibuf;
1112 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1114 struct nfs4_stateowner *lock_sop;
1116 while (!list_empty(&open_stp->st_perlockowner)) {
1117 lock_sop = list_entry(open_stp->st_perlockowner.next,
1118 struct nfs4_stateowner, so_perlockowner);
1119 /* list_del(&open_stp->st_perlockowner); */
1120 BUG_ON(lock_sop->so_is_open_owner);
1121 release_stateowner(lock_sop);
1126 unhash_stateowner(struct nfs4_stateowner *sop)
1128 struct nfs4_stateid *stp;
1130 list_del(&sop->so_idhash);
1131 list_del(&sop->so_strhash);
1132 if (sop->so_is_open_owner)
1133 list_del(&sop->so_perclient);
1134 list_del(&sop->so_perlockowner);
1135 while (!list_empty(&sop->so_perfilestate)) {
1136 stp = list_entry(sop->so_perfilestate.next,
1137 struct nfs4_stateid, st_perfilestate);
1138 if (sop->so_is_open_owner)
1139 release_stateid(stp, OPEN_STATE);
1141 release_stateid(stp, LOCK_STATE);
1146 release_stateowner(struct nfs4_stateowner *sop)
1148 unhash_stateowner(sop);
1149 list_del(&sop->so_close_lru);
1150 nfs4_put_stateowner(sop);
1154 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1155 struct nfs4_stateowner *sop = open->op_stateowner;
1156 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1158 INIT_LIST_HEAD(&stp->st_hash);
1159 INIT_LIST_HEAD(&stp->st_perfilestate);
1160 INIT_LIST_HEAD(&stp->st_perlockowner);
1161 INIT_LIST_HEAD(&stp->st_perfile);
1162 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1163 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
1164 list_add(&stp->st_perfile, &fp->fi_stateids);
1165 stp->st_stateowner = sop;
1168 stp->st_stateid.si_boot = boot_time;
1169 stp->st_stateid.si_stateownerid = sop->so_id;
1170 stp->st_stateid.si_fileid = fp->fi_id;
1171 stp->st_stateid.si_generation = 0;
1172 stp->st_access_bmap = 0;
1173 stp->st_deny_bmap = 0;
1174 __set_bit(open->op_share_access, &stp->st_access_bmap);
1175 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1179 release_stateid(struct nfs4_stateid *stp, int flags)
1181 struct file *filp = stp->st_vfs_file;
1183 list_del(&stp->st_hash);
1184 list_del(&stp->st_perfile);
1185 list_del(&stp->st_perfilestate);
1186 if (flags & OPEN_STATE) {
1187 release_stateid_lockowners(stp);
1188 stp->st_vfs_file = NULL;
1190 } else if (flags & LOCK_STATE)
1191 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1192 put_nfs4_file(stp->st_file);
1193 kmem_cache_free(stateid_slab, stp);
1198 move_to_close_lru(struct nfs4_stateowner *sop)
1200 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1202 unhash_stateowner(sop);
1203 list_add_tail(&sop->so_close_lru, &close_lru);
1204 sop->so_time = get_seconds();
1208 release_state_owner(struct nfs4_stateid *stp, int flag)
1210 struct nfs4_stateowner *sop = stp->st_stateowner;
1212 dprintk("NFSD: release_state_owner\n");
1213 release_stateid(stp, flag);
1215 /* place unused nfs4_stateowners on so_close_lru list to be
1216 * released by the laundromat service after the lease period
1217 * to enable us to handle CLOSE replay
1219 if (sop->so_confirmed && list_empty(&sop->so_perfilestate))
1220 move_to_close_lru(sop);
1224 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1225 return ((sop->so_owner.len == owner->len) &&
1226 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1227 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1230 static struct nfs4_stateowner *
1231 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1233 struct nfs4_stateowner *so = NULL;
1235 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1236 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1242 /* search file_hashtbl[] for file */
1243 static struct nfs4_file *
1244 find_file(struct inode *ino)
1246 unsigned int hashval = file_hashval(ino);
1247 struct nfs4_file *fp;
1249 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1250 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;
1306 dprintk("NFSD: nfs4_share_conflict\n");
1308 fp = find_file(ino);
1311 ret = nfserr_share_denied;
1312 /* Search for conflicting share reservations */
1313 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1314 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1315 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1325 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1327 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1328 put_write_access(filp->f_dentry->d_inode);
1329 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1334 * Recall a delegation
1337 do_recall(void *__dp)
1339 struct nfs4_delegation *dp = __dp;
1341 daemonize("nfsv4-recall");
1343 nfsd4_cb_recall(dp);
1348 * Spawn a thread to perform a recall on the delegation represented
1349 * by the lease (file_lock)
1351 * Called from break_lease() with lock_kernel() held.
1352 * Note: we assume break_lease will only call this *once* for any given
1356 void nfsd_break_deleg_cb(struct file_lock *fl)
1358 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1359 struct task_struct *t;
1361 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1365 /* We're assuming the state code never drops its reference
1366 * without first removing the lease. Since we're in this lease
1367 * callback (and since the lease code is serialized by the kernel
1368 * lock) we know the server hasn't removed the lease yet, we know
1369 * it's safe to take a reference: */
1370 atomic_inc(&dp->dl_count);
1372 spin_lock(&recall_lock);
1373 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1374 spin_unlock(&recall_lock);
1376 /* only place dl_time is set. protected by lock_kernel*/
1377 dp->dl_time = get_seconds();
1379 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1380 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1382 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1384 struct nfs4_client *clp = dp->dl_client;
1386 printk(KERN_INFO "NFSD: Callback thread failed for "
1387 "for client (clientid %08x/%08x)\n",
1388 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1389 nfs4_put_delegation(dp);
1394 * The file_lock is being reapd.
1396 * Called by locks_free_lock() with lock_kernel() held.
1399 void nfsd_release_deleg_cb(struct file_lock *fl)
1401 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1403 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1405 if (!(fl->fl_flags & FL_LEASE) || !dp)
1407 dp->dl_flock = NULL;
1411 * Set the delegation file_lock back pointer.
1413 * Called from __setlease() with lock_kernel() held.
1416 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1418 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1420 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1427 * Called from __setlease() with lock_kernel() held
1430 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1432 struct nfs4_delegation *onlistd =
1433 (struct nfs4_delegation *)onlist->fl_owner;
1434 struct nfs4_delegation *tryd =
1435 (struct nfs4_delegation *)try->fl_owner;
1437 if (onlist->fl_lmops != try->fl_lmops)
1440 return onlistd->dl_client == tryd->dl_client;
1445 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1448 return lease_modify(onlist, arg);
1453 static struct lock_manager_operations nfsd_lease_mng_ops = {
1454 .fl_break = nfsd_break_deleg_cb,
1455 .fl_release_private = nfsd_release_deleg_cb,
1456 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1457 .fl_mylease = nfsd_same_client_deleg_cb,
1458 .fl_change = nfsd_change_deleg_cb,
1463 * nfsd4_process_open1()
1464 * lookup stateowner.
1476 * called with nfs4_lock_state() held.
1479 nfsd4_process_open1(struct nfsd4_open *open)
1482 clientid_t *clientid = &open->op_clientid;
1483 struct nfs4_client *clp = NULL;
1484 unsigned int strhashval;
1485 struct nfs4_stateowner *sop = NULL;
1487 status = nfserr_inval;
1488 if (!check_name(open->op_owner))
1491 if (STALE_CLIENTID(&open->op_clientid))
1492 return nfserr_stale_clientid;
1494 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1495 sop = find_openstateowner_str(strhashval, open);
1497 open->op_stateowner = sop;
1498 /* check for replay */
1499 if (open->op_seqid == sop->so_seqid){
1500 if (sop->so_replay.rp_buflen)
1501 return NFSERR_REPLAY_ME;
1503 /* The original OPEN failed so spectacularly
1504 * that we don't even have replay data saved!
1505 * Therefore, we have no choice but to continue
1506 * processing this OPEN; presumably, we'll
1507 * fail again for the same reason.
1509 dprintk("nfsd4_process_open1:"
1510 " replay with no replay cache\n");
1513 } else if (sop->so_confirmed) {
1514 if (open->op_seqid == sop->so_seqid + 1)
1516 status = nfserr_bad_seqid;
1519 /* If we get here, we received an OPEN for an
1520 * unconfirmed nfs4_stateowner. Since the seqid's are
1521 * different, purge the existing nfs4_stateowner, and
1522 * instantiate a new one.
1524 clp = sop->so_client;
1525 release_stateowner(sop);
1528 /* nfs4_stateowner not found.
1529 * Verify clientid and instantiate new nfs4_stateowner.
1530 * If verify fails this is presumably the result of the
1531 * client's lease expiring.
1533 status = nfserr_expired;
1534 clp = find_confirmed_client(clientid);
1538 status = nfserr_resource;
1539 sop = alloc_init_open_stateowner(strhashval, clp, open);
1542 open->op_stateowner = sop;
1545 renew_client(sop->so_client);
1547 if (status && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1548 status = nfserr_reclaim_bad;
1553 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1555 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1556 return nfserr_openmode;
1561 static struct nfs4_delegation *
1562 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1564 struct nfs4_delegation *dp;
1566 list_for_each_entry(dp, &fp->fi_delegations, dl_del_perfile) {
1567 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1574 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1575 struct nfs4_delegation **dp)
1578 int status = nfserr_bad_stateid;
1580 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1583 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1584 RD_STATE : WR_STATE;
1585 status = nfs4_check_delegmode(*dp, flags);
1589 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1593 open->op_stateowner->so_confirmed = 1;
1598 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1600 struct nfs4_stateid *local;
1601 int status = nfserr_share_denied;
1602 struct nfs4_stateowner *sop = open->op_stateowner;
1604 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1605 /* ignore lock owners */
1606 if (local->st_stateowner->so_is_open_owner == 0)
1608 /* remember if we have seen this open owner */
1609 if (local->st_stateowner == sop)
1611 /* check for conflicting share reservations */
1612 if (!test_share(local, open))
1620 static inline struct nfs4_stateid *
1621 nfs4_alloc_stateid(void)
1623 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1627 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1628 struct nfs4_delegation *dp,
1629 struct svc_fh *cur_fh, int flags)
1631 struct nfs4_stateid *stp;
1633 stp = nfs4_alloc_stateid();
1635 return nfserr_resource;
1638 get_file(dp->dl_vfs_file);
1639 stp->st_vfs_file = dp->dl_vfs_file;
1642 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1645 if (status == nfserr_dropit)
1646 status = nfserr_jukebox;
1647 kmem_cache_free(stateid_slab, stp);
1656 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1657 struct nfsd4_open *open)
1659 struct iattr iattr = {
1660 .ia_valid = ATTR_SIZE,
1663 if (!open->op_truncate)
1665 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1667 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1671 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1673 struct file *filp = stp->st_vfs_file;
1674 struct inode *inode = filp->f_dentry->d_inode;
1675 unsigned int share_access;
1678 set_access(&share_access, stp->st_access_bmap);
1679 share_access = ~share_access;
1680 share_access &= open->op_share_access;
1682 if (!(share_access & NFS4_SHARE_ACCESS_WRITE))
1683 return nfsd4_truncate(rqstp, cur_fh, open);
1685 status = get_write_access(inode);
1687 return nfserrno(status);
1688 status = nfsd4_truncate(rqstp, cur_fh, open);
1690 put_write_access(inode);
1693 /* remember the open */
1694 filp->f_mode = (filp->f_mode | FMODE_WRITE) & ~FMODE_READ;
1695 set_bit(open->op_share_access, &stp->st_access_bmap);
1696 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1702 /* decrement seqid on successful reclaim, it will be bumped in encode_open */
1704 nfs4_set_claim_prev(struct nfsd4_open *open, int *status)
1706 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) {
1708 *status = nfserr_reclaim_bad;
1710 open->op_stateowner->so_confirmed = 1;
1711 open->op_stateowner->so_seqid--;
1717 * Attempt to hand out a delegation.
1720 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1722 struct nfs4_delegation *dp;
1723 struct nfs4_stateowner *sop = stp->st_stateowner;
1724 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1725 struct file_lock fl, *flp = &fl;
1726 int status, flag = 0;
1728 flag = NFS4_OPEN_DELEGATE_NONE;
1729 open->op_recall = 0;
1730 switch (open->op_claim_type) {
1731 case NFS4_OPEN_CLAIM_PREVIOUS:
1732 if (!atomic_read(&cb->cb_set))
1733 open->op_recall = 1;
1734 flag = open->op_delegate_type;
1735 if (flag == NFS4_OPEN_DELEGATE_NONE)
1738 case NFS4_OPEN_CLAIM_NULL:
1739 /* Let's not give out any delegations till everyone's
1740 * had the chance to reclaim theirs.... */
1741 if (nfs4_in_grace())
1743 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1745 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1746 flag = NFS4_OPEN_DELEGATE_WRITE;
1748 flag = NFS4_OPEN_DELEGATE_READ;
1754 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1756 flag = NFS4_OPEN_DELEGATE_NONE;
1759 locks_init_lock(&fl);
1760 fl.fl_lmops = &nfsd_lease_mng_ops;
1761 fl.fl_flags = FL_LEASE;
1762 fl.fl_end = OFFSET_MAX;
1763 fl.fl_owner = (fl_owner_t)dp;
1764 fl.fl_file = stp->st_vfs_file;
1765 fl.fl_pid = current->tgid;
1767 /* setlease checks to see if delegation should be handed out.
1768 * the lock_manager callbacks fl_mylease and fl_change are used
1770 if ((status = setlease(stp->st_vfs_file,
1771 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1772 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1773 unhash_delegation(dp);
1774 flag = NFS4_OPEN_DELEGATE_NONE;
1778 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1780 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1781 dp->dl_stateid.si_boot,
1782 dp->dl_stateid.si_stateownerid,
1783 dp->dl_stateid.si_fileid,
1784 dp->dl_stateid.si_generation);
1786 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1787 && flag == NFS4_OPEN_DELEGATE_NONE
1788 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1789 printk("NFSD: WARNING: refusing delegation reclaim\n");
1790 open->op_delegate_type = flag;
1794 * called with nfs4_lock_state() held.
1797 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1799 struct nfs4_file *fp = NULL;
1800 struct inode *ino = current_fh->fh_dentry->d_inode;
1801 struct nfs4_stateid *stp = NULL;
1802 struct nfs4_delegation *dp = NULL;
1805 status = nfserr_inval;
1806 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1809 * Lookup file; if found, lookup stateid and check open request,
1810 * and check for delegations in the process of being recalled.
1811 * If not found, create the nfs4_file struct
1813 fp = find_file(ino);
1815 if ((status = nfs4_check_open(fp, open, &stp)))
1817 status = nfs4_check_deleg(fp, open, &dp);
1821 status = nfserr_bad_stateid;
1822 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1824 status = nfserr_resource;
1825 fp = alloc_init_file(ino);
1831 * OPEN the file, or upgrade an existing OPEN.
1832 * If truncate fails, the OPEN fails.
1835 /* Stateid was found, this is an OPEN upgrade */
1836 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1840 /* Stateid was not found, this is a new OPEN */
1842 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1846 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1849 init_stateid(stp, fp, open);
1850 status = nfsd4_truncate(rqstp, current_fh, open);
1852 release_stateid(stp, OPEN_STATE);
1856 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1859 * Attempt to hand out a delegation. No error return, because the
1860 * OPEN succeeds even if we fail.
1862 nfs4_open_delegation(current_fh, open, stp);
1866 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1867 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1868 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1872 /* CLAIM_PREVIOUS has different error returns */
1873 nfs4_set_claim_prev(open, &status);
1875 * To finish the open response, we just need to set the rflags.
1877 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1878 if (!open->op_stateowner->so_confirmed)
1879 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1884 static struct workqueue_struct *laundry_wq;
1885 static struct work_struct laundromat_work;
1886 static void laundromat_main(void *);
1887 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1890 nfsd4_renew(clientid_t *clid)
1892 struct nfs4_client *clp;
1896 dprintk("process_renew(%08x/%08x): starting\n",
1897 clid->cl_boot, clid->cl_id);
1898 status = nfserr_stale_clientid;
1899 if (STALE_CLIENTID(clid))
1901 clp = find_confirmed_client(clid);
1902 status = nfserr_expired;
1904 /* We assume the client took too long to RENEW. */
1905 dprintk("nfsd4_renew: clientid not found!\n");
1909 status = nfserr_cb_path_down;
1910 if (!list_empty(&clp->cl_del_perclnt)
1911 && !atomic_read(&clp->cl_callback.cb_set))
1915 nfs4_unlock_state();
1922 dprintk("NFSD: end of grace period\n");
1927 nfs4_laundromat(void)
1929 struct nfs4_client *clp;
1930 struct nfs4_stateowner *sop;
1931 struct nfs4_delegation *dp;
1932 struct list_head *pos, *next, reaplist;
1933 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1934 time_t t, clientid_val = NFSD_LEASE_TIME;
1935 time_t u, test_val = NFSD_LEASE_TIME;
1939 dprintk("NFSD: laundromat service - starting\n");
1942 list_for_each_safe(pos, next, &client_lru) {
1943 clp = list_entry(pos, struct nfs4_client, cl_lru);
1944 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1945 t = clp->cl_time - cutoff;
1946 if (clientid_val > t)
1950 dprintk("NFSD: purging unused client (clientid %08x)\n",
1951 clp->cl_clientid.cl_id);
1954 INIT_LIST_HEAD(&reaplist);
1955 spin_lock(&recall_lock);
1956 list_for_each_safe(pos, next, &del_recall_lru) {
1957 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1958 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1959 u = dp->dl_time - cutoff;
1964 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1966 list_move(&dp->dl_recall_lru, &reaplist);
1968 spin_unlock(&recall_lock);
1969 list_for_each_safe(pos, next, &reaplist) {
1970 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1971 list_del_init(&dp->dl_recall_lru);
1972 unhash_delegation(dp);
1974 test_val = NFSD_LEASE_TIME;
1975 list_for_each_safe(pos, next, &close_lru) {
1976 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1977 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1978 u = sop->so_time - cutoff;
1983 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1985 list_del(&sop->so_close_lru);
1986 nfs4_put_stateowner(sop);
1988 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1989 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1990 nfs4_unlock_state();
1991 return clientid_val;
1995 laundromat_main(void *not_used)
1999 t = nfs4_laundromat();
2000 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
2001 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
2004 /* search ownerid_hashtbl[] and close_lru for stateid owner
2005 * (stateid->si_stateownerid)
2007 static struct nfs4_stateowner *
2008 find_openstateowner_id(u32 st_id, int flags) {
2009 struct nfs4_stateowner *local = NULL;
2011 dprintk("NFSD: find_openstateowner_id %d\n", st_id);
2012 if (flags & CLOSE_STATE) {
2013 list_for_each_entry(local, &close_lru, so_close_lru) {
2014 if (local->so_id == st_id)
2022 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
2024 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
2028 STALE_STATEID(stateid_t *stateid)
2030 if (stateid->si_boot == boot_time)
2032 printk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
2033 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2034 stateid->si_generation);
2039 access_permit_read(unsigned long access_bmap)
2041 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
2042 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
2043 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
2047 access_permit_write(unsigned long access_bmap)
2049 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
2050 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
2054 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
2056 int status = nfserr_openmode;
2058 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
2060 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2068 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2070 /* Trying to call delegreturn with a special stateid? Yuch: */
2071 if (!(flags & (RD_STATE | WR_STATE)))
2072 return nfserr_bad_stateid;
2073 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2075 else if (nfs4_in_grace()) {
2076 /* Answer in remaining cases depends on existance of
2077 * conflicting state; so we must wait out the grace period. */
2078 return nfserr_grace;
2079 } else if (flags & WR_STATE)
2080 return nfs4_share_conflict(current_fh,
2081 NFS4_SHARE_DENY_WRITE);
2082 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2083 return nfs4_share_conflict(current_fh,
2084 NFS4_SHARE_DENY_READ);
2088 * Allow READ/WRITE during grace period on recovered state only for files
2089 * that are not able to provide mandatory locking.
2092 io_during_grace_disallowed(struct inode *inode, int flags)
2094 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2095 && MANDATORY_LOCK(inode);
2099 * Checks for stateid operations
2102 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2104 struct nfs4_stateid *stp = NULL;
2105 struct nfs4_delegation *dp = NULL;
2107 struct inode *ino = current_fh->fh_dentry->d_inode;
2110 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2111 stateid->si_boot, stateid->si_stateownerid,
2112 stateid->si_fileid, stateid->si_generation);
2116 if (io_during_grace_disallowed(ino, flags))
2117 return nfserr_grace;
2119 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2120 return check_special_stateids(current_fh, stateid, flags);
2123 status = nfserr_stale_stateid;
2124 if (STALE_STATEID(stateid))
2128 status = nfserr_bad_stateid;
2129 if (!stateid->si_fileid) { /* delegation stateid */
2130 if(!(dp = find_delegation_stateid(ino, stateid))) {
2131 dprintk("NFSD: delegation stateid not found\n");
2132 if (nfs4_in_grace())
2133 status = nfserr_grace;
2136 stidp = &dp->dl_stateid;
2137 } else { /* open or lock stateid */
2138 if (!(stp = find_stateid(stateid, flags))) {
2139 dprintk("NFSD: open or lock stateid not found\n");
2140 if (nfs4_in_grace())
2141 status = nfserr_grace;
2144 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2146 if (!stp->st_stateowner->so_confirmed)
2148 stidp = &stp->st_stateid;
2150 if (stateid->si_generation > stidp->si_generation)
2154 status = nfserr_old_stateid;
2155 if (stateid->si_generation < stidp->si_generation)
2158 if ((status = nfs4_check_openmode(stp,flags)))
2160 renew_client(stp->st_stateowner->so_client);
2162 *filpp = stp->st_vfs_file;
2164 if ((status = nfs4_check_delegmode(dp, flags)))
2166 renew_client(dp->dl_client);
2167 if (flags & DELEG_RET)
2168 unhash_delegation(dp);
2170 *filpp = dp->dl_vfs_file;
2179 * Checks for sequence id mutating operations.
2182 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)
2185 struct nfs4_stateid *stp;
2186 struct nfs4_stateowner *sop;
2188 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2189 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2190 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2191 stateid->si_generation);
2196 status = nfserr_bad_stateid;
2197 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2198 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2202 status = nfserr_stale_stateid;
2203 if (STALE_STATEID(stateid))
2206 * We return BAD_STATEID if filehandle doesn't match stateid,
2207 * the confirmed flag is incorrecly set, or the generation
2208 * number is incorrect.
2209 * If there is no entry in the openfile table for this id,
2210 * we can't always return BAD_STATEID;
2211 * this might be a retransmitted CLOSE which has arrived after
2212 * the openfile has been released.
2214 if (!(stp = find_stateid(stateid, flags)))
2215 goto no_nfs4_stateid;
2217 status = nfserr_bad_stateid;
2219 /* for new lock stateowners:
2220 * check that the lock->v.new.open_stateid
2221 * refers to an open stateowner
2223 * check that the lockclid (nfs4_lock->v.new.clientid) is the same
2224 * as the open_stateid->st_stateowner->so_client->clientid
2227 struct nfs4_stateowner *sop = stp->st_stateowner;
2228 struct nfs4_client *clp = sop->so_client;
2230 if (!sop->so_is_open_owner)
2232 if (!cmp_clid(&clp->cl_clientid, lockclid))
2236 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2237 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2242 *sopp = sop = stp->st_stateowner;
2245 * We now validate the seqid and stateid generation numbers.
2246 * For the moment, we ignore the possibility of
2247 * generation number wraparound.
2249 if (seqid != sop->so_seqid + 1)
2252 if (sop->so_confirmed) {
2253 if (flags & CONFIRM) {
2254 printk("NFSD: preprocess_seqid_op: expected unconfirmed stateowner!\n");
2259 if (!(flags & CONFIRM)) {
2260 printk("NFSD: preprocess_seqid_op: stateowner not confirmed yet!\n");
2264 if (stateid->si_generation > stp->st_stateid.si_generation) {
2265 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2269 status = nfserr_old_stateid;
2270 if (stateid->si_generation < stp->st_stateid.si_generation) {
2271 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2274 /* XXX renew the client lease here */
2283 * We determine whether this is a bad stateid or a replay,
2284 * starting by trying to look up the stateowner.
2285 * If stateowner is not found - stateid is bad.
2287 if (!(sop = find_openstateowner_id(stateid->si_stateownerid, flags))) {
2288 printk("NFSD: preprocess_seqid_op: no stateowner or nfs4_stateid!\n");
2289 status = nfserr_bad_stateid;
2295 if (seqid == sop->so_seqid) {
2296 printk("NFSD: preprocess_seqid_op: retransmission?\n");
2297 /* indicate replay to calling function */
2298 status = NFSERR_REPLAY_ME;
2300 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d\n", sop->so_seqid +1, seqid);
2303 status = nfserr_bad_seqid;
2309 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc)
2312 struct nfs4_stateowner *sop;
2313 struct nfs4_stateid *stp;
2315 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2316 (int)current_fh->fh_dentry->d_name.len,
2317 current_fh->fh_dentry->d_name.name);
2319 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2324 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2325 &oc->oc_req_stateid,
2326 CHECK_FH | CONFIRM | OPEN_STATE,
2327 &oc->oc_stateowner, &stp, NULL)))
2330 sop = oc->oc_stateowner;
2331 sop->so_confirmed = 1;
2332 update_stateid(&stp->st_stateid);
2333 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2334 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2335 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2336 stp->st_stateid.si_boot,
2337 stp->st_stateid.si_stateownerid,
2338 stp->st_stateid.si_fileid,
2339 stp->st_stateid.si_generation);
2341 if (oc->oc_stateowner)
2342 nfs4_get_stateowner(oc->oc_stateowner);
2343 nfs4_unlock_state();
2349 * unset all bits in union bitmap (bmap) that
2350 * do not exist in share (from successful OPEN_DOWNGRADE)
2353 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2356 for (i = 1; i < 4; i++) {
2357 if ((i & access) != i)
2358 __clear_bit(i, bmap);
2363 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2366 for (i = 0; i < 4; i++) {
2367 if ((i & deny) != i)
2368 __clear_bit(i, bmap);
2373 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od)
2376 struct nfs4_stateid *stp;
2377 unsigned int share_access;
2379 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2380 (int)current_fh->fh_dentry->d_name.len,
2381 current_fh->fh_dentry->d_name.name);
2383 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2384 return nfserr_inval;
2387 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2389 CHECK_FH | OPEN_STATE,
2390 &od->od_stateowner, &stp, NULL)))
2393 status = nfserr_inval;
2394 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2395 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2396 stp->st_access_bmap, od->od_share_access);
2399 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2400 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2401 stp->st_deny_bmap, od->od_share_deny);
2404 set_access(&share_access, stp->st_access_bmap);
2405 nfs4_file_downgrade(stp->st_vfs_file,
2406 share_access & ~od->od_share_access);
2408 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2409 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2411 update_stateid(&stp->st_stateid);
2412 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2415 if (od->od_stateowner)
2416 nfs4_get_stateowner(od->od_stateowner);
2417 nfs4_unlock_state();
2422 * nfs4_unlock_state() called after encode
2425 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close)
2428 struct nfs4_stateid *stp;
2430 dprintk("NFSD: nfsd4_close on file %.*s\n",
2431 (int)current_fh->fh_dentry->d_name.len,
2432 current_fh->fh_dentry->d_name.name);
2435 /* check close_lru for replay */
2436 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2438 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2439 &close->cl_stateowner, &stp, NULL)))
2442 * Return success, but first update the stateid.
2445 update_stateid(&stp->st_stateid);
2446 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2448 /* release_state_owner() calls nfsd_close() if needed */
2449 release_state_owner(stp, OPEN_STATE);
2451 if (close->cl_stateowner)
2452 nfs4_get_stateowner(close->cl_stateowner);
2453 nfs4_unlock_state();
2458 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2462 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2466 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2467 nfs4_unlock_state();
2474 * Lock owner state (byte-range locks)
2476 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2477 #define LOCK_HASH_BITS 8
2478 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2479 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2481 #define lockownerid_hashval(id) \
2482 ((id) & LOCK_HASH_MASK)
2484 static inline unsigned int
2485 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2486 struct xdr_netobj *ownername)
2488 return (file_hashval(inode) + cl_id
2489 + opaque_hashval(ownername->data, ownername->len))
2493 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2494 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2495 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2497 static struct nfs4_stateid *
2498 find_stateid(stateid_t *stid, int flags)
2500 struct nfs4_stateid *local = NULL;
2501 u32 st_id = stid->si_stateownerid;
2502 u32 f_id = stid->si_fileid;
2503 unsigned int hashval;
2505 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2506 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2507 hashval = stateid_hashval(st_id, f_id);
2508 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2509 if ((local->st_stateid.si_stateownerid == st_id) &&
2510 (local->st_stateid.si_fileid == f_id))
2514 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2515 hashval = stateid_hashval(st_id, f_id);
2516 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2517 if ((local->st_stateid.si_stateownerid == st_id) &&
2518 (local->st_stateid.si_fileid == f_id))
2522 printk("NFSD: find_stateid: ERROR: no state flag\n");
2526 static struct nfs4_delegation *
2527 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2529 struct nfs4_file *fp;
2530 struct nfs4_delegation *dl;
2532 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2533 stid->si_boot, stid->si_stateownerid,
2534 stid->si_fileid, stid->si_generation);
2536 fp = find_file(ino);
2539 dl = find_delegation_file(fp, stid);
2545 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2546 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2547 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2548 * locking, this prevents us from being completely protocol-compliant. The
2549 * real solution to this problem is to start using unsigned file offsets in
2550 * the VFS, but this is a very deep change!
2553 nfs4_transform_lock_offset(struct file_lock *lock)
2555 if (lock->fl_start < 0)
2556 lock->fl_start = OFFSET_MAX;
2557 if (lock->fl_end < 0)
2558 lock->fl_end = OFFSET_MAX;
2562 nfs4_verify_lock_stateowner(struct nfs4_stateowner *sop, unsigned int hashval)
2564 struct nfs4_stateowner *local = NULL;
2567 if (hashval >= LOCK_HASH_SIZE)
2569 list_for_each_entry(local, &lock_ownerid_hashtbl[hashval], so_idhash) {
2581 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2583 struct nfs4_stateowner *sop = (struct nfs4_stateowner *) fl->fl_owner;
2584 unsigned int hval = lockownerid_hashval(sop->so_id);
2586 deny->ld_sop = NULL;
2587 if (nfs4_verify_lock_stateowner(sop, hval)) {
2588 kref_get(&sop->so_ref);
2590 deny->ld_clientid = sop->so_client->cl_clientid;
2592 deny->ld_start = fl->fl_start;
2593 deny->ld_length = ~(u64)0;
2594 if (fl->fl_end != ~(u64)0)
2595 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2596 deny->ld_type = NFS4_READ_LT;
2597 if (fl->fl_type != F_RDLCK)
2598 deny->ld_type = NFS4_WRITE_LT;
2601 static struct nfs4_stateowner *
2602 find_lockstateowner(struct xdr_netobj *owner, clientid_t *clid)
2604 struct nfs4_stateowner *local = NULL;
2607 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2608 list_for_each_entry(local, &lock_ownerid_hashtbl[i], so_idhash) {
2609 if (!cmp_owner_str(local, owner, clid))
2617 static struct nfs4_stateowner *
2618 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2619 struct xdr_netobj *owner)
2621 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2622 struct nfs4_stateowner *op;
2624 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2625 if (cmp_owner_str(op, owner, clid))
2632 * Alloc a lock owner structure.
2633 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2636 * strhashval = lock_ownerstr_hashval
2637 * so_seqid = lock->lk_new_lock_seqid - 1: it gets bumped in encode
2640 static struct nfs4_stateowner *
2641 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2642 struct nfs4_stateowner *sop;
2643 struct nfs4_replay *rp;
2644 unsigned int idhashval;
2646 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2648 idhashval = lockownerid_hashval(current_ownerid);
2649 INIT_LIST_HEAD(&sop->so_idhash);
2650 INIT_LIST_HEAD(&sop->so_strhash);
2651 INIT_LIST_HEAD(&sop->so_perclient);
2652 INIT_LIST_HEAD(&sop->so_perfilestate);
2653 INIT_LIST_HEAD(&sop->so_perlockowner);
2654 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2656 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2657 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2658 list_add(&sop->so_perlockowner, &open_stp->st_perlockowner);
2659 sop->so_is_open_owner = 0;
2660 sop->so_id = current_ownerid++;
2661 sop->so_client = clp;
2662 sop->so_seqid = lock->lk_new_lock_seqid - 1;
2663 sop->so_confirmed = 1;
2664 rp = &sop->so_replay;
2665 rp->rp_status = NFSERR_SERVERFAULT;
2667 rp->rp_buf = rp->rp_ibuf;
2671 static struct nfs4_stateid *
2672 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2674 struct nfs4_stateid *stp;
2675 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2677 stp = nfs4_alloc_stateid();
2680 INIT_LIST_HEAD(&stp->st_hash);
2681 INIT_LIST_HEAD(&stp->st_perfile);
2682 INIT_LIST_HEAD(&stp->st_perfilestate);
2683 INIT_LIST_HEAD(&stp->st_perlockowner); /* not used */
2684 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2685 list_add(&stp->st_perfile, &fp->fi_stateids);
2686 list_add(&stp->st_perfilestate, &sop->so_perfilestate);
2687 stp->st_stateowner = sop;
2690 stp->st_stateid.si_boot = boot_time;
2691 stp->st_stateid.si_stateownerid = sop->so_id;
2692 stp->st_stateid.si_fileid = fp->fi_id;
2693 stp->st_stateid.si_generation = 0;
2694 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2695 stp->st_access_bmap = open_stp->st_access_bmap;
2696 stp->st_deny_bmap = open_stp->st_deny_bmap;
2703 check_lock_length(u64 offset, u64 length)
2705 return ((length == 0) || ((length != ~(u64)0) &&
2706 LOFF_OVERFLOW(offset, length)));
2713 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock)
2715 struct nfs4_stateowner *lock_sop = NULL, *open_sop = NULL;
2716 struct nfs4_stateid *lock_stp;
2718 struct file_lock file_lock;
2719 struct file_lock *conflock;
2721 unsigned int strhashval;
2723 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2724 (long long) lock->lk_offset,
2725 (long long) lock->lk_length);
2727 if (nfs4_in_grace() && !lock->lk_reclaim)
2728 return nfserr_grace;
2729 if (!nfs4_in_grace() && lock->lk_reclaim)
2730 return nfserr_no_grace;
2732 if (check_lock_length(lock->lk_offset, lock->lk_length))
2733 return nfserr_inval;
2737 if (lock->lk_is_new) {
2739 * Client indicates that this is a new lockowner.
2740 * Use open owner and open stateid to create lock owner and lock
2743 struct nfs4_stateid *open_stp = NULL;
2744 struct nfs4_file *fp;
2746 status = nfserr_stale_clientid;
2747 if (STALE_CLIENTID(&lock->lk_new_clientid)) {
2748 printk("NFSD: nfsd4_lock: clientid is stale!\n");
2752 /* is the new lock seqid presented by the client zero? */
2753 status = nfserr_bad_seqid;
2754 if (lock->v.new.lock_seqid != 0)
2757 /* validate and update open stateid and open seqid */
2758 status = nfs4_preprocess_seqid_op(current_fh,
2759 lock->lk_new_open_seqid,
2760 &lock->lk_new_open_stateid,
2761 CHECK_FH | OPEN_STATE,
2762 &open_sop, &open_stp,
2763 &lock->v.new.clientid);
2765 if (lock->lk_reclaim)
2766 status = nfserr_reclaim_bad;
2769 /* create lockowner and lock stateid */
2770 fp = open_stp->st_file;
2771 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2772 open_sop->so_client->cl_clientid.cl_id,
2773 &lock->v.new.owner);
2775 * If we already have this lock owner, the client is in
2776 * error (or our bookeeping is wrong!)
2777 * for asking for a 'new lock'.
2779 status = nfserr_bad_stateid;
2780 lock_sop = find_lockstateowner(&lock->v.new.owner,
2781 &lock->v.new.clientid);
2784 status = nfserr_resource;
2785 if (!(lock->lk_stateowner = alloc_init_lock_stateowner(strhashval, open_sop->so_client, open_stp, lock)))
2787 if ((lock_stp = alloc_init_lock_stateid(lock->lk_stateowner,
2788 fp, open_stp)) == NULL) {
2789 release_stateowner(lock->lk_stateowner);
2790 lock->lk_stateowner = NULL;
2793 /* bump the open seqid used to create the lock */
2794 open_sop->so_seqid++;
2796 /* lock (lock owner + lock stateid) already exists */
2797 status = nfs4_preprocess_seqid_op(current_fh,
2798 lock->lk_old_lock_seqid,
2799 &lock->lk_old_lock_stateid,
2800 CHECK_FH | LOCK_STATE,
2801 &lock->lk_stateowner, &lock_stp, NULL);
2805 /* lock->lk_stateowner and lock_stp have been created or found */
2806 filp = lock_stp->st_vfs_file;
2808 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2809 printk("NFSD: nfsd4_lock: permission denied!\n");
2813 locks_init_lock(&file_lock);
2814 switch (lock->lk_type) {
2817 file_lock.fl_type = F_RDLCK;
2820 case NFS4_WRITEW_LT:
2821 file_lock.fl_type = F_WRLCK;
2824 status = nfserr_inval;
2827 file_lock.fl_owner = (fl_owner_t) lock->lk_stateowner;
2828 file_lock.fl_pid = current->tgid;
2829 file_lock.fl_file = filp;
2830 file_lock.fl_flags = FL_POSIX;
2832 file_lock.fl_start = lock->lk_offset;
2833 if ((lock->lk_length == ~(u64)0) ||
2834 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2835 file_lock.fl_end = ~(u64)0;
2837 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2838 nfs4_transform_lock_offset(&file_lock);
2841 * Try to lock the file in the VFS.
2842 * Note: locks.c uses the BKL to protect the inode's lock list.
2845 status = posix_lock_file(filp, &file_lock);
2846 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
2847 file_lock.fl_ops->fl_release_private(&file_lock);
2848 dprintk("NFSD: nfsd4_lock: posix_lock_file status %d\n",status);
2850 case 0: /* success! */
2851 update_stateid(&lock_stp->st_stateid);
2852 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2856 goto conflicting_lock;
2858 status = nfserr_deadlock;
2860 dprintk("NFSD: nfsd4_lock: posix_lock_file() failed! status %d\n",status);
2861 goto out_destroy_new_stateid;
2865 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2866 status = nfserr_denied;
2867 /* XXX There is a race here. Future patch needed to provide
2868 * an atomic posix_lock_and_test_file
2870 if (!(conflock = posix_test_lock(filp, &file_lock))) {
2871 status = nfserr_serverfault;
2874 nfs4_set_lock_denied(conflock, &lock->lk_denied);
2876 out_destroy_new_stateid:
2877 if (lock->lk_is_new) {
2878 dprintk("NFSD: nfsd4_lock: destroy new stateid!\n");
2880 * An error encountered after instantiation of the new
2881 * stateid has forced us to destroy it.
2883 if (!seqid_mutating_err(status))
2884 open_sop->so_seqid--;
2886 release_state_owner(lock_stp, LOCK_STATE);
2889 if (lock->lk_stateowner)
2890 nfs4_get_stateowner(lock->lk_stateowner);
2891 nfs4_unlock_state();
2899 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2901 struct inode *inode;
2903 struct file_lock file_lock;
2904 struct file_lock *conflicting_lock;
2907 if (nfs4_in_grace())
2908 return nfserr_grace;
2910 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2911 return nfserr_inval;
2913 lockt->lt_stateowner = NULL;
2916 status = nfserr_stale_clientid;
2917 if (STALE_CLIENTID(&lockt->lt_clientid)) {
2918 printk("NFSD: nfsd4_lockt: clientid is stale!\n");
2922 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2923 printk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2924 if (status == nfserr_symlink)
2925 status = nfserr_inval;
2929 inode = current_fh->fh_dentry->d_inode;
2930 locks_init_lock(&file_lock);
2931 switch (lockt->lt_type) {
2934 file_lock.fl_type = F_RDLCK;
2937 case NFS4_WRITEW_LT:
2938 file_lock.fl_type = F_WRLCK;
2941 printk("NFSD: nfs4_lockt: bad lock type!\n");
2942 status = nfserr_inval;
2946 lockt->lt_stateowner = find_lockstateowner_str(inode,
2947 &lockt->lt_clientid, &lockt->lt_owner);
2948 if (lockt->lt_stateowner)
2949 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2950 file_lock.fl_pid = current->tgid;
2951 file_lock.fl_flags = FL_POSIX;
2953 file_lock.fl_start = lockt->lt_offset;
2954 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2955 file_lock.fl_end = ~(u64)0;
2957 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2959 nfs4_transform_lock_offset(&file_lock);
2961 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2962 * since LOCKT doesn't require an OPEN, and therefore a struct
2963 * file may not exist, pass posix_test_lock a struct file with
2964 * only the dentry:inode set.
2966 memset(&file, 0, sizeof (struct file));
2967 file.f_dentry = current_fh->fh_dentry;
2970 conflicting_lock = posix_test_lock(&file, &file_lock);
2971 if (conflicting_lock) {
2972 status = nfserr_denied;
2973 nfs4_set_lock_denied(conflicting_lock, &lockt->lt_denied);
2976 nfs4_unlock_state();
2981 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku)
2983 struct nfs4_stateid *stp;
2984 struct file *filp = NULL;
2985 struct file_lock file_lock;
2988 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2989 (long long) locku->lu_offset,
2990 (long long) locku->lu_length);
2992 if (check_lock_length(locku->lu_offset, locku->lu_length))
2993 return nfserr_inval;
2997 if ((status = nfs4_preprocess_seqid_op(current_fh,
3000 CHECK_FH | LOCK_STATE,
3001 &locku->lu_stateowner, &stp, NULL)))
3004 filp = stp->st_vfs_file;
3006 locks_init_lock(&file_lock);
3007 file_lock.fl_type = F_UNLCK;
3008 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
3009 file_lock.fl_pid = current->tgid;
3010 file_lock.fl_file = filp;
3011 file_lock.fl_flags = FL_POSIX;
3012 file_lock.fl_start = locku->lu_offset;
3014 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
3015 file_lock.fl_end = ~(u64)0;
3017 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
3018 nfs4_transform_lock_offset(&file_lock);
3021 * Try to unlock the file in the VFS.
3023 status = posix_lock_file(filp, &file_lock);
3024 if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
3025 file_lock.fl_ops->fl_release_private(&file_lock);
3027 printk("NFSD: nfs4_locku: posix_lock_file failed!\n");
3031 * OK, unlock succeeded; the only thing left to do is update the stateid.
3033 update_stateid(&stp->st_stateid);
3034 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
3037 if (locku->lu_stateowner)
3038 nfs4_get_stateowner(locku->lu_stateowner);
3039 nfs4_unlock_state();
3043 status = nfserrno(status);
3049 * 1: locks held by lockowner
3050 * 0: no locks held by lockowner
3053 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
3055 struct file_lock **flpp;
3056 struct inode *inode = filp->f_dentry->d_inode;
3060 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
3061 if ((*flpp)->fl_owner == (fl_owner_t)lowner)
3071 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
3073 clientid_t *clid = &rlockowner->rl_clientid;
3074 struct nfs4_stateowner *local = NULL;
3075 struct xdr_netobj *owner = &rlockowner->rl_owner;
3078 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
3079 clid->cl_boot, clid->cl_id);
3081 /* XXX check for lease expiration */
3083 status = nfserr_stale_clientid;
3084 if (STALE_CLIENTID(clid)) {
3085 printk("NFSD: nfsd4_release_lockowner: clientid is stale!\n");
3092 local = find_lockstateowner(owner, clid);
3094 struct nfs4_stateid *stp;
3096 /* check for any locks held by any stateid
3097 * associated with the (lock) stateowner */
3098 status = nfserr_locks_held;
3099 list_for_each_entry(stp, &local->so_perfilestate,
3101 if (check_for_locks(stp->st_vfs_file, local))
3104 /* no locks held by (lock) stateowner */
3106 release_stateowner(local);
3109 nfs4_unlock_state();
3113 static inline struct nfs4_client_reclaim *
3116 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3120 * failure => all reset bets are off, nfserr_no_grace...
3123 nfs4_client_to_reclaim(char *name)
3125 unsigned int strhashval;
3126 struct nfs4_client_reclaim *crp = NULL;
3128 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3129 crp = alloc_reclaim();
3132 strhashval = clientstr_hashval(name);
3133 INIT_LIST_HEAD(&crp->cr_strhash);
3134 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3135 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3136 reclaim_str_hashtbl_size++;
3141 nfs4_release_reclaim(void)
3143 struct nfs4_client_reclaim *crp = NULL;
3146 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3147 while (!list_empty(&reclaim_str_hashtbl[i])) {
3148 crp = list_entry(reclaim_str_hashtbl[i].next,
3149 struct nfs4_client_reclaim, cr_strhash);
3150 list_del(&crp->cr_strhash);
3152 reclaim_str_hashtbl_size--;
3155 BUG_ON(reclaim_str_hashtbl_size);
3159 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3160 static struct nfs4_client_reclaim *
3161 nfs4_find_reclaim_client(clientid_t *clid)
3163 unsigned int strhashval;
3164 struct nfs4_client *clp;
3165 struct nfs4_client_reclaim *crp = NULL;
3168 /* find clientid in conf_id_hashtbl */
3169 clp = find_confirmed_client(clid);
3173 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3174 clp->cl_name.len, clp->cl_name.data,
3177 /* find clp->cl_name in reclaim_str_hashtbl */
3178 strhashval = clientstr_hashval(clp->cl_recdir);
3179 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3180 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3188 * Called from OPEN. Look for clientid in reclaim list.
3191 nfs4_check_open_reclaim(clientid_t *clid)
3193 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3196 /* initialization to perform at module load time: */
3199 nfs4_state_init(void)
3203 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3204 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3205 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3206 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3207 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3209 for (i = 0; i < FILE_HASH_SIZE; i++) {
3210 INIT_LIST_HEAD(&file_hashtbl[i]);
3212 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3213 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3214 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3216 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3217 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3218 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3220 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3221 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3222 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3224 memset(&onestateid, ~0, sizeof(stateid_t));
3225 INIT_LIST_HEAD(&close_lru);
3226 INIT_LIST_HEAD(&client_lru);
3227 INIT_LIST_HEAD(&del_recall_lru);
3228 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3229 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3230 reclaim_str_hashtbl_size = 0;
3233 /* initialization to perform when the nfsd service is started: */
3236 __nfs4_state_start(void)
3240 boot_time = get_seconds();
3241 grace_time = max(user_lease_time, lease_time);
3242 lease_time = user_lease_time;
3244 printk("NFSD: starting %ld-second grace period\n", grace_time);
3245 laundry_wq = create_singlethread_workqueue("nfsd4");
3246 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3250 nfs4_state_start(void)
3256 status = nfsd4_init_slabs();
3259 __nfs4_state_start();
3271 nfs4_lease_time(void)
3277 __nfs4_state_shutdown(void)
3280 struct nfs4_client *clp = NULL;
3281 struct nfs4_delegation *dp = NULL;
3282 struct nfs4_stateowner *sop = NULL;
3283 struct list_head *pos, *next, reaplist;
3285 list_for_each_safe(pos, next, &close_lru) {
3286 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3287 list_del(&sop->so_close_lru);
3288 nfs4_put_stateowner(sop);
3291 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3292 while (!list_empty(&conf_id_hashtbl[i])) {
3293 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3296 while (!list_empty(&unconf_str_hashtbl[i])) {
3297 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3301 INIT_LIST_HEAD(&reaplist);
3302 spin_lock(&recall_lock);
3303 list_for_each_safe(pos, next, &del_recall_lru) {
3304 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3305 list_move(&dp->dl_recall_lru, &reaplist);
3307 spin_unlock(&recall_lock);
3308 list_for_each_safe(pos, next, &reaplist) {
3309 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3310 list_del_init(&dp->dl_recall_lru);
3311 unhash_delegation(dp);
3314 cancel_delayed_work(&laundromat_work);
3315 flush_workqueue(laundry_wq);
3316 destroy_workqueue(laundry_wq);
3321 nfs4_state_shutdown(void)
3324 nfs4_release_reclaim();
3325 __nfs4_state_shutdown();
3327 nfs4_unlock_state();
3331 * Called when leasetime is changed.
3333 * The only way the protocol gives us to handle on-the-fly lease changes is to
3334 * simulate a reboot. Instead of doing that, we just wait till the next time
3335 * we start to register any changes in lease time. If the administrator
3336 * really wants to change the lease time *now*, they can go ahead and bring
3337 * nfsd down and then back up again after changing the lease time.
3340 nfs4_reset_lease(time_t leasetime)
3343 user_lease_time = leasetime;