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>
51 #include <linux/namei.h>
52 #include <linux/mutex.h>
54 #define NFSDDBG_FACILITY NFSDDBG_PROC
57 static time_t lease_time = 90; /* default lease time */
58 static time_t user_lease_time = 90;
59 static time_t boot_time;
60 static int in_grace = 1;
61 static u32 current_clientid = 1;
62 static u32 current_ownerid = 1;
63 static u32 current_fileid = 1;
64 static u32 current_delegid = 1;
66 static stateid_t zerostateid; /* bits all 0 */
67 static stateid_t onestateid; /* bits all 1 */
69 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zerostateid, sizeof(stateid_t)))
70 #define ONE_STATEID(stateid) (!memcmp((stateid), &onestateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static struct nfs4_stateid * find_stateid(stateid_t *stid, int flags);
74 static struct nfs4_delegation * find_delegation_stateid(struct inode *ino, stateid_t *stid);
75 static void release_stateid_lockowners(struct nfs4_stateid *open_stp);
76 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
77 static void nfs4_set_recdir(char *recdir);
82 * protects clientid_hashtbl[], clientstr_hashtbl[],
83 * unconfstr_hashtbl[], uncofid_hashtbl[].
85 static DEFINE_MUTEX(client_mutex);
87 static kmem_cache_t *stateowner_slab = NULL;
88 static kmem_cache_t *file_slab = NULL;
89 static kmem_cache_t *stateid_slab = NULL;
90 static kmem_cache_t *deleg_slab = NULL;
95 mutex_lock(&client_mutex);
99 nfs4_unlock_state(void)
101 mutex_unlock(&client_mutex);
105 opaque_hashval(const void *ptr, int nbytes)
107 unsigned char *cptr = (unsigned char *) ptr;
117 /* forward declarations */
118 static void release_stateowner(struct nfs4_stateowner *sop);
119 static void release_stateid(struct nfs4_stateid *stp, int flags);
125 /* recall_lock protects the del_recall_lru */
126 static spinlock_t recall_lock = SPIN_LOCK_UNLOCKED;
127 static struct list_head del_recall_lru;
130 free_nfs4_file(struct kref *kref)
132 struct nfs4_file *fp = container_of(kref, struct nfs4_file, fi_ref);
133 list_del(&fp->fi_hash);
135 kmem_cache_free(file_slab, fp);
139 put_nfs4_file(struct nfs4_file *fi)
141 kref_put(&fi->fi_ref, free_nfs4_file);
145 get_nfs4_file(struct nfs4_file *fi)
147 kref_get(&fi->fi_ref);
150 static struct nfs4_delegation *
151 alloc_init_deleg(struct nfs4_client *clp, struct nfs4_stateid *stp, struct svc_fh *current_fh, u32 type)
153 struct nfs4_delegation *dp;
154 struct nfs4_file *fp = stp->st_file;
155 struct nfs4_callback *cb = &stp->st_stateowner->so_client->cl_callback;
157 dprintk("NFSD alloc_init_deleg\n");
158 dp = kmem_cache_alloc(deleg_slab, GFP_KERNEL);
161 INIT_LIST_HEAD(&dp->dl_perfile);
162 INIT_LIST_HEAD(&dp->dl_perclnt);
163 INIT_LIST_HEAD(&dp->dl_recall_lru);
168 get_file(stp->st_vfs_file);
169 dp->dl_vfs_file = stp->st_vfs_file;
171 dp->dl_recall.cbr_dp = NULL;
172 dp->dl_recall.cbr_ident = cb->cb_ident;
173 dp->dl_recall.cbr_trunc = 0;
174 dp->dl_stateid.si_boot = boot_time;
175 dp->dl_stateid.si_stateownerid = current_delegid++;
176 dp->dl_stateid.si_fileid = 0;
177 dp->dl_stateid.si_generation = 0;
178 dp->dl_fhlen = current_fh->fh_handle.fh_size;
179 memcpy(dp->dl_fhval, ¤t_fh->fh_handle.fh_base,
180 current_fh->fh_handle.fh_size);
182 atomic_set(&dp->dl_count, 1);
183 list_add(&dp->dl_perfile, &fp->fi_delegations);
184 list_add(&dp->dl_perclnt, &clp->cl_delegations);
189 nfs4_put_delegation(struct nfs4_delegation *dp)
191 if (atomic_dec_and_test(&dp->dl_count)) {
192 dprintk("NFSD: freeing dp %p\n",dp);
193 put_nfs4_file(dp->dl_file);
194 kmem_cache_free(deleg_slab, dp);
198 /* Remove the associated file_lock first, then remove the delegation.
199 * lease_modify() is called to remove the FS_LEASE file_lock from
200 * the i_flock list, eventually calling nfsd's lock_manager
201 * fl_release_callback.
204 nfs4_close_delegation(struct nfs4_delegation *dp)
206 struct file *filp = dp->dl_vfs_file;
208 dprintk("NFSD: close_delegation dp %p\n",dp);
209 dp->dl_vfs_file = NULL;
210 /* The following nfsd_close may not actually close the file,
211 * but we want to remove the lease in any case. */
213 setlease(filp, F_UNLCK, &dp->dl_flock);
217 /* Called under the state lock. */
219 unhash_delegation(struct nfs4_delegation *dp)
221 list_del_init(&dp->dl_perfile);
222 list_del_init(&dp->dl_perclnt);
223 spin_lock(&recall_lock);
224 list_del_init(&dp->dl_recall_lru);
225 spin_unlock(&recall_lock);
226 nfs4_close_delegation(dp);
227 nfs4_put_delegation(dp);
234 /* Hash tables for nfs4_clientid state */
235 #define CLIENT_HASH_BITS 4
236 #define CLIENT_HASH_SIZE (1 << CLIENT_HASH_BITS)
237 #define CLIENT_HASH_MASK (CLIENT_HASH_SIZE - 1)
239 #define clientid_hashval(id) \
240 ((id) & CLIENT_HASH_MASK)
241 #define clientstr_hashval(name) \
242 (opaque_hashval((name), 8) & CLIENT_HASH_MASK)
244 * reclaim_str_hashtbl[] holds known client info from previous reset/reboot
245 * used in reboot/reset lease grace period processing
247 * conf_id_hashtbl[], and conf_str_hashtbl[] hold confirmed
248 * setclientid_confirmed info.
250 * unconf_str_hastbl[] and unconf_id_hashtbl[] hold unconfirmed
253 * client_lru holds client queue ordered by nfs4_client.cl_time
256 * close_lru holds (open) stateowner queue ordered by nfs4_stateowner.so_time
257 * for last close replay.
259 static struct list_head reclaim_str_hashtbl[CLIENT_HASH_SIZE];
260 static int reclaim_str_hashtbl_size = 0;
261 static struct list_head conf_id_hashtbl[CLIENT_HASH_SIZE];
262 static struct list_head conf_str_hashtbl[CLIENT_HASH_SIZE];
263 static struct list_head unconf_str_hashtbl[CLIENT_HASH_SIZE];
264 static struct list_head unconf_id_hashtbl[CLIENT_HASH_SIZE];
265 static struct list_head client_lru;
266 static struct list_head close_lru;
269 renew_client(struct nfs4_client *clp)
272 * Move client to the end to the LRU list.
274 dprintk("renewing client (clientid %08x/%08x)\n",
275 clp->cl_clientid.cl_boot,
276 clp->cl_clientid.cl_id);
277 list_move_tail(&clp->cl_lru, &client_lru);
278 clp->cl_time = get_seconds();
281 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
283 STALE_CLIENTID(clientid_t *clid)
285 if (clid->cl_boot == boot_time)
287 dprintk("NFSD stale clientid (%08x/%08x)\n",
288 clid->cl_boot, clid->cl_id);
293 * XXX Should we use a slab cache ?
294 * This type of memory management is somewhat inefficient, but we use it
295 * anyway since SETCLIENTID is not a common operation.
297 static inline struct nfs4_client *
298 alloc_client(struct xdr_netobj name)
300 struct nfs4_client *clp;
302 if ((clp = kmalloc(sizeof(struct nfs4_client), GFP_KERNEL))!= NULL) {
303 memset(clp, 0, sizeof(*clp));
304 if ((clp->cl_name.data = kmalloc(name.len, GFP_KERNEL)) != NULL) {
305 memcpy(clp->cl_name.data, name.data, name.len);
306 clp->cl_name.len = name.len;
317 free_client(struct nfs4_client *clp)
319 if (clp->cl_cred.cr_group_info)
320 put_group_info(clp->cl_cred.cr_group_info);
321 kfree(clp->cl_name.data);
326 put_nfs4_client(struct nfs4_client *clp)
328 if (atomic_dec_and_test(&clp->cl_count))
333 shutdown_callback_client(struct nfs4_client *clp)
335 struct rpc_clnt *clnt = clp->cl_callback.cb_client;
337 /* shutdown rpc client, ending any outstanding recall rpcs */
339 clp->cl_callback.cb_client = NULL;
340 rpc_shutdown_client(clnt);
346 expire_client(struct nfs4_client *clp)
348 struct nfs4_stateowner *sop;
349 struct nfs4_delegation *dp;
350 struct list_head reaplist;
352 dprintk("NFSD: expire_client cl_count %d\n",
353 atomic_read(&clp->cl_count));
355 shutdown_callback_client(clp);
357 INIT_LIST_HEAD(&reaplist);
358 spin_lock(&recall_lock);
359 while (!list_empty(&clp->cl_delegations)) {
360 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
361 dprintk("NFSD: expire client. dp %p, fp %p\n", dp,
363 list_del_init(&dp->dl_perclnt);
364 list_move(&dp->dl_recall_lru, &reaplist);
366 spin_unlock(&recall_lock);
367 while (!list_empty(&reaplist)) {
368 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
369 list_del_init(&dp->dl_recall_lru);
370 unhash_delegation(dp);
372 list_del(&clp->cl_idhash);
373 list_del(&clp->cl_strhash);
374 list_del(&clp->cl_lru);
375 while (!list_empty(&clp->cl_openowners)) {
376 sop = list_entry(clp->cl_openowners.next, struct nfs4_stateowner, so_perclient);
377 release_stateowner(sop);
379 put_nfs4_client(clp);
382 static struct nfs4_client *
383 create_client(struct xdr_netobj name, char *recdir) {
384 struct nfs4_client *clp;
386 if (!(clp = alloc_client(name)))
388 memcpy(clp->cl_recdir, recdir, HEXDIR_LEN);
389 atomic_set(&clp->cl_count, 1);
390 atomic_set(&clp->cl_callback.cb_set, 0);
391 INIT_LIST_HEAD(&clp->cl_idhash);
392 INIT_LIST_HEAD(&clp->cl_strhash);
393 INIT_LIST_HEAD(&clp->cl_openowners);
394 INIT_LIST_HEAD(&clp->cl_delegations);
395 INIT_LIST_HEAD(&clp->cl_lru);
401 copy_verf(struct nfs4_client *target, nfs4_verifier *source) {
402 memcpy(target->cl_verifier.data, source->data, sizeof(target->cl_verifier.data));
406 copy_clid(struct nfs4_client *target, struct nfs4_client *source) {
407 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
408 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
412 copy_cred(struct svc_cred *target, struct svc_cred *source) {
414 target->cr_uid = source->cr_uid;
415 target->cr_gid = source->cr_gid;
416 target->cr_group_info = source->cr_group_info;
417 get_group_info(target->cr_group_info);
421 same_name(const char *n1, const char *n2) {
422 return 0 == memcmp(n1, n2, HEXDIR_LEN);
426 cmp_verf(nfs4_verifier *v1, nfs4_verifier *v2) {
427 return(!memcmp(v1->data,v2->data,sizeof(v1->data)));
431 cmp_clid(clientid_t * cl1, clientid_t * cl2) {
432 return((cl1->cl_boot == cl2->cl_boot) &&
433 (cl1->cl_id == cl2->cl_id));
436 /* XXX what about NGROUP */
438 cmp_creds(struct svc_cred *cr1, struct svc_cred *cr2){
439 return(cr1->cr_uid == cr2->cr_uid);
444 gen_clid(struct nfs4_client *clp) {
445 clp->cl_clientid.cl_boot = boot_time;
446 clp->cl_clientid.cl_id = current_clientid++;
450 gen_confirm(struct nfs4_client *clp) {
455 p = (u32 *)clp->cl_confirm.data;
461 check_name(struct xdr_netobj name) {
465 if (name.len > NFS4_OPAQUE_LIMIT) {
466 printk("NFSD: check_name: name too long(%d)!\n", name.len);
473 add_to_unconfirmed(struct nfs4_client *clp, unsigned int strhashval)
475 unsigned int idhashval;
477 list_add(&clp->cl_strhash, &unconf_str_hashtbl[strhashval]);
478 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
479 list_add(&clp->cl_idhash, &unconf_id_hashtbl[idhashval]);
480 list_add_tail(&clp->cl_lru, &client_lru);
481 clp->cl_time = get_seconds();
485 move_to_confirmed(struct nfs4_client *clp)
487 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
488 unsigned int strhashval;
490 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
491 list_del_init(&clp->cl_strhash);
492 list_del_init(&clp->cl_idhash);
493 list_add(&clp->cl_idhash, &conf_id_hashtbl[idhashval]);
494 strhashval = clientstr_hashval(clp->cl_recdir);
495 list_add(&clp->cl_strhash, &conf_str_hashtbl[strhashval]);
499 static struct nfs4_client *
500 find_confirmed_client(clientid_t *clid)
502 struct nfs4_client *clp;
503 unsigned int idhashval = clientid_hashval(clid->cl_id);
505 list_for_each_entry(clp, &conf_id_hashtbl[idhashval], cl_idhash) {
506 if (cmp_clid(&clp->cl_clientid, clid))
512 static struct nfs4_client *
513 find_unconfirmed_client(clientid_t *clid)
515 struct nfs4_client *clp;
516 unsigned int idhashval = clientid_hashval(clid->cl_id);
518 list_for_each_entry(clp, &unconf_id_hashtbl[idhashval], cl_idhash) {
519 if (cmp_clid(&clp->cl_clientid, clid))
525 static struct nfs4_client *
526 find_confirmed_client_by_str(const char *dname, unsigned int hashval)
528 struct nfs4_client *clp;
530 list_for_each_entry(clp, &conf_str_hashtbl[hashval], cl_strhash) {
531 if (same_name(clp->cl_recdir, dname))
537 static struct nfs4_client *
538 find_unconfirmed_client_by_str(const char *dname, unsigned int hashval)
540 struct nfs4_client *clp;
542 list_for_each_entry(clp, &unconf_str_hashtbl[hashval], cl_strhash) {
543 if (same_name(clp->cl_recdir, dname))
549 /* a helper function for parse_callback */
551 parse_octet(unsigned int *lenp, char **addrp)
553 unsigned int len = *lenp;
565 if ((c < '0') || (c > '9')) {
571 n = (n * 10) + (c - '0');
582 /* parse and set the setclientid ipv4 callback address */
584 parse_ipv4(unsigned int addr_len, char *addr_val, unsigned int *cbaddrp, unsigned short *cbportp)
589 u32 addrlen = addr_len;
590 char *addr = addr_val;
595 for(i = 4; i > 0 ; i--) {
596 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
599 cbaddr |= (temp << shift);
607 for(i = 2; i > 0 ; i--) {
608 if ((temp = parse_octet(&addrlen, &addr)) < 0) {
611 cbport |= (temp << shift);
620 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se)
622 struct nfs4_callback *cb = &clp->cl_callback;
624 /* Currently, we only support tcp for the callback channel */
625 if ((se->se_callback_netid_len != 3) || memcmp((char *)se->se_callback_netid_val, "tcp", 3))
628 if ( !(parse_ipv4(se->se_callback_addr_len, se->se_callback_addr_val,
629 &cb->cb_addr, &cb->cb_port)))
631 cb->cb_prog = se->se_callback_prog;
632 cb->cb_ident = se->se_callback_ident;
635 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
636 "will not receive delegations\n",
637 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
643 * RFC 3010 has a complex implmentation description of processing a
644 * SETCLIENTID request consisting of 5 bullets, labeled as
645 * CASE0 - CASE4 below.
648 * callback information will be processed in a future patch
650 * an unconfirmed record is added when:
651 * NORMAL (part of CASE 4): there is no confirmed nor unconfirmed record.
652 * CASE 1: confirmed record found with matching name, principal,
653 * verifier, and clientid.
654 * CASE 2: confirmed record found with matching name, principal,
655 * and there is no unconfirmed record with matching
658 * an unconfirmed record is replaced when:
659 * CASE 3: confirmed record found with matching name, principal,
660 * and an unconfirmed record is found with matching
661 * name, principal, and with clientid and
662 * confirm that does not match the confirmed record.
663 * CASE 4: there is no confirmed record with matching name and
664 * principal. there is an unconfirmed record with
665 * matching name, principal.
667 * an unconfirmed record is deleted when:
668 * CASE 1: an unconfirmed record that matches input name, verifier,
669 * and confirmed clientid.
670 * CASE 4: any unconfirmed records with matching name and principal
671 * that exist after an unconfirmed record has been replaced
672 * as described above.
676 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_setclientid *setclid)
678 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
679 struct xdr_netobj clname = {
680 .len = setclid->se_namelen,
681 .data = setclid->se_name,
683 nfs4_verifier clverifier = setclid->se_verf;
684 unsigned int strhashval;
685 struct nfs4_client *conf, *unconf, *new;
687 char dname[HEXDIR_LEN];
689 if (!check_name(clname))
692 status = nfs4_make_rec_clidname(dname, &clname);
697 * XXX The Duplicate Request Cache (DRC) has been checked (??)
698 * We get here on a DRC miss.
701 strhashval = clientstr_hashval(dname);
704 conf = find_confirmed_client_by_str(dname, strhashval);
708 * clname match, confirmed, different principal
709 * or different ip_address
711 status = nfserr_clid_inuse;
712 if (!cmp_creds(&conf->cl_cred, &rqstp->rq_cred)
713 || conf->cl_addr != ip_addr) {
714 printk("NFSD: setclientid: string in use by client"
715 "(clientid %08x/%08x)\n",
716 conf->cl_clientid.cl_boot, conf->cl_clientid.cl_id);
720 unconf = find_unconfirmed_client_by_str(dname, strhashval);
721 status = nfserr_resource;
725 * placed first, because it is the normal case.
728 expire_client(unconf);
729 new = create_client(clname, dname);
732 copy_verf(new, &clverifier);
733 new->cl_addr = ip_addr;
734 copy_cred(&new->cl_cred,&rqstp->rq_cred);
737 gen_callback(new, setclid);
738 add_to_unconfirmed(new, strhashval);
739 } else if (cmp_verf(&conf->cl_verifier, &clverifier)) {
742 * cl_name match, confirmed, principal match
743 * verifier match: probable callback update
745 * remove any unconfirmed nfs4_client with
746 * matching cl_name, cl_verifier, and cl_clientid
748 * create and insert an unconfirmed nfs4_client with same
749 * cl_name, cl_verifier, and cl_clientid as existing
750 * nfs4_client, but with the new callback info and a
754 /* Note this is removing unconfirmed {*x***},
755 * which is stronger than RFC recommended {vxc**}.
756 * This has the advantage that there is at most
757 * one {*x***} in either list at any time.
759 expire_client(unconf);
761 new = create_client(clname, dname);
764 copy_verf(new,&conf->cl_verifier);
765 new->cl_addr = ip_addr;
766 copy_cred(&new->cl_cred,&rqstp->rq_cred);
767 copy_clid(new, conf);
769 gen_callback(new, setclid);
770 add_to_unconfirmed(new,strhashval);
771 } else if (!unconf) {
774 * clname match, confirmed, principal match
775 * verfier does not match
776 * no unconfirmed. create a new unconfirmed nfs4_client
777 * using input clverifier, clname, and callback info
778 * and generate a new cl_clientid and cl_confirm.
780 new = create_client(clname, dname);
783 copy_verf(new,&clverifier);
784 new->cl_addr = ip_addr;
785 copy_cred(&new->cl_cred,&rqstp->rq_cred);
788 gen_callback(new, setclid);
789 add_to_unconfirmed(new, strhashval);
790 } else if (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm)) {
793 * confirmed found (name, principal match)
794 * confirmed verifier does not match input clverifier
796 * unconfirmed found (name match)
797 * confirmed->cl_confirm != unconfirmed->cl_confirm
799 * remove unconfirmed.
801 * create an unconfirmed nfs4_client
802 * with same cl_name as existing confirmed nfs4_client,
803 * but with new callback info, new cl_clientid,
804 * new cl_verifier and a new cl_confirm
806 expire_client(unconf);
807 new = create_client(clname, dname);
810 copy_verf(new,&clverifier);
811 new->cl_addr = ip_addr;
812 copy_cred(&new->cl_cred,&rqstp->rq_cred);
815 gen_callback(new, setclid);
816 add_to_unconfirmed(new, strhashval);
818 /* No cases hit !!! */
819 status = nfserr_inval;
823 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
824 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
825 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
834 * RFC 3010 has a complex implmentation description of processing a
835 * SETCLIENTID_CONFIRM request consisting of 4 bullets describing
836 * processing on a DRC miss, labeled as CASE1 - CASE4 below.
838 * NOTE: callback information will be processed here in a future patch
841 nfsd4_setclientid_confirm(struct svc_rqst *rqstp, struct nfsd4_setclientid_confirm *setclientid_confirm)
843 u32 ip_addr = rqstp->rq_addr.sin_addr.s_addr;
844 struct nfs4_client *conf, *unconf;
845 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
846 clientid_t * clid = &setclientid_confirm->sc_clientid;
849 if (STALE_CLIENTID(clid))
850 return nfserr_stale_clientid;
852 * XXX The Duplicate Request Cache (DRC) has been checked (??)
853 * We get here on a DRC miss.
858 conf = find_confirmed_client(clid);
859 unconf = find_unconfirmed_client(clid);
861 status = nfserr_clid_inuse;
862 if (conf && conf->cl_addr != ip_addr)
864 if (unconf && unconf->cl_addr != ip_addr)
867 if ((conf && unconf) &&
868 (cmp_verf(&unconf->cl_confirm, &confirm)) &&
869 (cmp_verf(&conf->cl_verifier, &unconf->cl_verifier)) &&
870 (same_name(conf->cl_recdir,unconf->cl_recdir)) &&
871 (!cmp_verf(&conf->cl_confirm, &unconf->cl_confirm))) {
873 * unconf record that matches input clientid and input confirm.
874 * conf record that matches input clientid.
875 * conf and unconf records match names, verifiers
877 if (!cmp_creds(&conf->cl_cred, &unconf->cl_cred))
878 status = nfserr_clid_inuse;
880 /* XXX: We just turn off callbacks until we can handle
881 * change request correctly. */
882 atomic_set(&conf->cl_callback.cb_set, 0);
884 nfsd4_remove_clid_dir(unconf);
885 expire_client(unconf);
889 } else if ((conf && !unconf) ||
891 (!cmp_verf(&conf->cl_verifier, &unconf->cl_verifier) ||
892 !same_name(conf->cl_recdir, unconf->cl_recdir)))) {
894 * conf record that matches input clientid.
895 * if unconf record matches input clientid, then
896 * unconf->cl_name or unconf->cl_verifier don't match the
899 if (!cmp_creds(&conf->cl_cred,&rqstp->rq_cred))
900 status = nfserr_clid_inuse;
903 } else if (!conf && unconf
904 && cmp_verf(&unconf->cl_confirm, &confirm)) {
906 * conf record not found.
907 * unconf record found.
908 * unconf->cl_confirm matches input confirm
910 if (!cmp_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
911 status = nfserr_clid_inuse;
914 clientstr_hashval(unconf->cl_recdir);
915 conf = find_confirmed_client_by_str(unconf->cl_recdir,
918 nfsd4_remove_clid_dir(conf);
921 move_to_confirmed(unconf);
925 } else if ((!conf || (conf && !cmp_verf(&conf->cl_confirm, &confirm)))
926 && (!unconf || (unconf && !cmp_verf(&unconf->cl_confirm,
929 * conf record not found, or if conf, conf->cl_confirm does not
930 * match input confirm.
931 * unconf record not found, or if unconf, unconf->cl_confirm
932 * does not match input confirm.
934 status = nfserr_stale_clientid;
936 /* check that we have hit one of the cases...*/
937 status = nfserr_clid_inuse;
941 nfsd4_probe_callback(conf);
947 * Open owner state (share locks)
950 /* hash tables for nfs4_stateowner */
951 #define OWNER_HASH_BITS 8
952 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
953 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
955 #define ownerid_hashval(id) \
956 ((id) & OWNER_HASH_MASK)
957 #define ownerstr_hashval(clientid, ownername) \
958 (((clientid) + opaque_hashval((ownername.data), (ownername.len))) & OWNER_HASH_MASK)
960 static struct list_head ownerid_hashtbl[OWNER_HASH_SIZE];
961 static struct list_head ownerstr_hashtbl[OWNER_HASH_SIZE];
963 /* hash table for nfs4_file */
964 #define FILE_HASH_BITS 8
965 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
966 #define FILE_HASH_MASK (FILE_HASH_SIZE - 1)
967 /* hash table for (open)nfs4_stateid */
968 #define STATEID_HASH_BITS 10
969 #define STATEID_HASH_SIZE (1 << STATEID_HASH_BITS)
970 #define STATEID_HASH_MASK (STATEID_HASH_SIZE - 1)
972 #define file_hashval(x) \
973 hash_ptr(x, FILE_HASH_BITS)
974 #define stateid_hashval(owner_id, file_id) \
975 (((owner_id) + (file_id)) & STATEID_HASH_MASK)
977 static struct list_head file_hashtbl[FILE_HASH_SIZE];
978 static struct list_head stateid_hashtbl[STATEID_HASH_SIZE];
980 /* OPEN Share state helper functions */
981 static inline struct nfs4_file *
982 alloc_init_file(struct inode *ino)
984 struct nfs4_file *fp;
985 unsigned int hashval = file_hashval(ino);
987 fp = kmem_cache_alloc(file_slab, GFP_KERNEL);
989 kref_init(&fp->fi_ref);
990 INIT_LIST_HEAD(&fp->fi_hash);
991 INIT_LIST_HEAD(&fp->fi_stateids);
992 INIT_LIST_HEAD(&fp->fi_delegations);
993 list_add(&fp->fi_hash, &file_hashtbl[hashval]);
994 fp->fi_inode = igrab(ino);
995 fp->fi_id = current_fileid++;
1002 nfsd4_free_slab(kmem_cache_t **slab)
1008 status = kmem_cache_destroy(*slab);
1014 nfsd4_free_slabs(void)
1016 nfsd4_free_slab(&stateowner_slab);
1017 nfsd4_free_slab(&file_slab);
1018 nfsd4_free_slab(&stateid_slab);
1019 nfsd4_free_slab(&deleg_slab);
1023 nfsd4_init_slabs(void)
1025 stateowner_slab = kmem_cache_create("nfsd4_stateowners",
1026 sizeof(struct nfs4_stateowner), 0, 0, NULL, NULL);
1027 if (stateowner_slab == NULL)
1029 file_slab = kmem_cache_create("nfsd4_files",
1030 sizeof(struct nfs4_file), 0, 0, NULL, NULL);
1031 if (file_slab == NULL)
1033 stateid_slab = kmem_cache_create("nfsd4_stateids",
1034 sizeof(struct nfs4_stateid), 0, 0, NULL, NULL);
1035 if (stateid_slab == NULL)
1037 deleg_slab = kmem_cache_create("nfsd4_delegations",
1038 sizeof(struct nfs4_delegation), 0, 0, NULL, NULL);
1039 if (deleg_slab == NULL)
1044 dprintk("nfsd4: out of memory while initializing nfsv4\n");
1049 nfs4_free_stateowner(struct kref *kref)
1051 struct nfs4_stateowner *sop =
1052 container_of(kref, struct nfs4_stateowner, so_ref);
1053 kfree(sop->so_owner.data);
1054 kmem_cache_free(stateowner_slab, sop);
1057 static inline struct nfs4_stateowner *
1058 alloc_stateowner(struct xdr_netobj *owner)
1060 struct nfs4_stateowner *sop;
1062 if ((sop = kmem_cache_alloc(stateowner_slab, GFP_KERNEL))) {
1063 if ((sop->so_owner.data = kmalloc(owner->len, GFP_KERNEL))) {
1064 memcpy(sop->so_owner.data, owner->data, owner->len);
1065 sop->so_owner.len = owner->len;
1066 kref_init(&sop->so_ref);
1069 kmem_cache_free(stateowner_slab, sop);
1074 static struct nfs4_stateowner *
1075 alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
1076 struct nfs4_stateowner *sop;
1077 struct nfs4_replay *rp;
1078 unsigned int idhashval;
1080 if (!(sop = alloc_stateowner(&open->op_owner)))
1082 idhashval = ownerid_hashval(current_ownerid);
1083 INIT_LIST_HEAD(&sop->so_idhash);
1084 INIT_LIST_HEAD(&sop->so_strhash);
1085 INIT_LIST_HEAD(&sop->so_perclient);
1086 INIT_LIST_HEAD(&sop->so_stateids);
1087 INIT_LIST_HEAD(&sop->so_perstateid); /* not used */
1088 INIT_LIST_HEAD(&sop->so_close_lru);
1090 list_add(&sop->so_idhash, &ownerid_hashtbl[idhashval]);
1091 list_add(&sop->so_strhash, &ownerstr_hashtbl[strhashval]);
1092 list_add(&sop->so_perclient, &clp->cl_openowners);
1093 sop->so_is_open_owner = 1;
1094 sop->so_id = current_ownerid++;
1095 sop->so_client = clp;
1096 sop->so_seqid = open->op_seqid;
1097 sop->so_confirmed = 0;
1098 rp = &sop->so_replay;
1099 rp->rp_status = nfserr_serverfault;
1101 rp->rp_buf = rp->rp_ibuf;
1106 release_stateid_lockowners(struct nfs4_stateid *open_stp)
1108 struct nfs4_stateowner *lock_sop;
1110 while (!list_empty(&open_stp->st_lockowners)) {
1111 lock_sop = list_entry(open_stp->st_lockowners.next,
1112 struct nfs4_stateowner, so_perstateid);
1113 /* list_del(&open_stp->st_lockowners); */
1114 BUG_ON(lock_sop->so_is_open_owner);
1115 release_stateowner(lock_sop);
1120 unhash_stateowner(struct nfs4_stateowner *sop)
1122 struct nfs4_stateid *stp;
1124 list_del(&sop->so_idhash);
1125 list_del(&sop->so_strhash);
1126 if (sop->so_is_open_owner)
1127 list_del(&sop->so_perclient);
1128 list_del(&sop->so_perstateid);
1129 while (!list_empty(&sop->so_stateids)) {
1130 stp = list_entry(sop->so_stateids.next,
1131 struct nfs4_stateid, st_perstateowner);
1132 if (sop->so_is_open_owner)
1133 release_stateid(stp, OPEN_STATE);
1135 release_stateid(stp, LOCK_STATE);
1140 release_stateowner(struct nfs4_stateowner *sop)
1142 unhash_stateowner(sop);
1143 list_del(&sop->so_close_lru);
1144 nfs4_put_stateowner(sop);
1148 init_stateid(struct nfs4_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
1149 struct nfs4_stateowner *sop = open->op_stateowner;
1150 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
1152 INIT_LIST_HEAD(&stp->st_hash);
1153 INIT_LIST_HEAD(&stp->st_perstateowner);
1154 INIT_LIST_HEAD(&stp->st_lockowners);
1155 INIT_LIST_HEAD(&stp->st_perfile);
1156 list_add(&stp->st_hash, &stateid_hashtbl[hashval]);
1157 list_add(&stp->st_perstateowner, &sop->so_stateids);
1158 list_add(&stp->st_perfile, &fp->fi_stateids);
1159 stp->st_stateowner = sop;
1162 stp->st_stateid.si_boot = boot_time;
1163 stp->st_stateid.si_stateownerid = sop->so_id;
1164 stp->st_stateid.si_fileid = fp->fi_id;
1165 stp->st_stateid.si_generation = 0;
1166 stp->st_access_bmap = 0;
1167 stp->st_deny_bmap = 0;
1168 __set_bit(open->op_share_access, &stp->st_access_bmap);
1169 __set_bit(open->op_share_deny, &stp->st_deny_bmap);
1170 stp->st_openstp = NULL;
1174 release_stateid(struct nfs4_stateid *stp, int flags)
1176 struct file *filp = stp->st_vfs_file;
1178 list_del(&stp->st_hash);
1179 list_del(&stp->st_perfile);
1180 list_del(&stp->st_perstateowner);
1181 if (flags & OPEN_STATE) {
1182 release_stateid_lockowners(stp);
1183 stp->st_vfs_file = NULL;
1185 } else if (flags & LOCK_STATE)
1186 locks_remove_posix(filp, (fl_owner_t) stp->st_stateowner);
1187 put_nfs4_file(stp->st_file);
1188 kmem_cache_free(stateid_slab, stp);
1192 move_to_close_lru(struct nfs4_stateowner *sop)
1194 dprintk("NFSD: move_to_close_lru nfs4_stateowner %p\n", sop);
1196 unhash_stateowner(sop);
1197 list_add_tail(&sop->so_close_lru, &close_lru);
1198 sop->so_time = get_seconds();
1202 cmp_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner, clientid_t *clid) {
1203 return ((sop->so_owner.len == owner->len) &&
1204 !memcmp(sop->so_owner.data, owner->data, owner->len) &&
1205 (sop->so_client->cl_clientid.cl_id == clid->cl_id));
1208 static struct nfs4_stateowner *
1209 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open)
1211 struct nfs4_stateowner *so = NULL;
1213 list_for_each_entry(so, &ownerstr_hashtbl[hashval], so_strhash) {
1214 if (cmp_owner_str(so, &open->op_owner, &open->op_clientid))
1220 /* search file_hashtbl[] for file */
1221 static struct nfs4_file *
1222 find_file(struct inode *ino)
1224 unsigned int hashval = file_hashval(ino);
1225 struct nfs4_file *fp;
1227 list_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
1228 if (fp->fi_inode == ino) {
1236 #define TEST_ACCESS(x) ((x > 0 || x < 4)?1:0)
1237 #define TEST_DENY(x) ((x >= 0 || x < 5)?1:0)
1240 set_access(unsigned int *access, unsigned long bmap) {
1244 for (i = 1; i < 4; i++) {
1245 if (test_bit(i, &bmap))
1251 set_deny(unsigned int *deny, unsigned long bmap) {
1255 for (i = 0; i < 4; i++) {
1256 if (test_bit(i, &bmap))
1262 test_share(struct nfs4_stateid *stp, struct nfsd4_open *open) {
1263 unsigned int access, deny;
1265 set_access(&access, stp->st_access_bmap);
1266 set_deny(&deny, stp->st_deny_bmap);
1267 if ((access & open->op_share_deny) || (deny & open->op_share_access))
1273 * Called to check deny when READ with all zero stateid or
1274 * WRITE with all zero or all one stateid
1277 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
1279 struct inode *ino = current_fh->fh_dentry->d_inode;
1280 struct nfs4_file *fp;
1281 struct nfs4_stateid *stp;
1284 dprintk("NFSD: nfs4_share_conflict\n");
1286 fp = find_file(ino);
1289 ret = nfserr_locked;
1290 /* Search for conflicting share reservations */
1291 list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
1292 if (test_bit(deny_type, &stp->st_deny_bmap) ||
1293 test_bit(NFS4_SHARE_DENY_BOTH, &stp->st_deny_bmap))
1303 nfs4_file_downgrade(struct file *filp, unsigned int share_access)
1305 if (share_access & NFS4_SHARE_ACCESS_WRITE) {
1306 put_write_access(filp->f_dentry->d_inode);
1307 filp->f_mode = (filp->f_mode | FMODE_READ) & ~FMODE_WRITE;
1312 * Recall a delegation
1315 do_recall(void *__dp)
1317 struct nfs4_delegation *dp = __dp;
1319 daemonize("nfsv4-recall");
1321 nfsd4_cb_recall(dp);
1326 * Spawn a thread to perform a recall on the delegation represented
1327 * by the lease (file_lock)
1329 * Called from break_lease() with lock_kernel() held.
1330 * Note: we assume break_lease will only call this *once* for any given
1334 void nfsd_break_deleg_cb(struct file_lock *fl)
1336 struct nfs4_delegation *dp= (struct nfs4_delegation *)fl->fl_owner;
1337 struct task_struct *t;
1339 dprintk("NFSD nfsd_break_deleg_cb: dp %p fl %p\n",dp,fl);
1343 /* We're assuming the state code never drops its reference
1344 * without first removing the lease. Since we're in this lease
1345 * callback (and since the lease code is serialized by the kernel
1346 * lock) we know the server hasn't removed the lease yet, we know
1347 * it's safe to take a reference: */
1348 atomic_inc(&dp->dl_count);
1350 spin_lock(&recall_lock);
1351 list_add_tail(&dp->dl_recall_lru, &del_recall_lru);
1352 spin_unlock(&recall_lock);
1354 /* only place dl_time is set. protected by lock_kernel*/
1355 dp->dl_time = get_seconds();
1357 /* XXX need to merge NFSD_LEASE_TIME with fs/locks.c:lease_break_time */
1358 fl->fl_break_time = jiffies + NFSD_LEASE_TIME * HZ;
1360 t = kthread_run(do_recall, dp, "%s", "nfs4_cb_recall");
1362 struct nfs4_client *clp = dp->dl_client;
1364 printk(KERN_INFO "NFSD: Callback thread failed for "
1365 "for client (clientid %08x/%08x)\n",
1366 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
1367 nfs4_put_delegation(dp);
1372 * The file_lock is being reapd.
1374 * Called by locks_free_lock() with lock_kernel() held.
1377 void nfsd_release_deleg_cb(struct file_lock *fl)
1379 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
1381 dprintk("NFSD nfsd_release_deleg_cb: fl %p dp %p dl_count %d\n", fl,dp, atomic_read(&dp->dl_count));
1383 if (!(fl->fl_flags & FL_LEASE) || !dp)
1385 dp->dl_flock = NULL;
1389 * Set the delegation file_lock back pointer.
1391 * Called from __setlease() with lock_kernel() held.
1394 void nfsd_copy_lock_deleg_cb(struct file_lock *new, struct file_lock *fl)
1396 struct nfs4_delegation *dp = (struct nfs4_delegation *)new->fl_owner;
1398 dprintk("NFSD: nfsd_copy_lock_deleg_cb: new fl %p dp %p\n", new, dp);
1405 * Called from __setlease() with lock_kernel() held
1408 int nfsd_same_client_deleg_cb(struct file_lock *onlist, struct file_lock *try)
1410 struct nfs4_delegation *onlistd =
1411 (struct nfs4_delegation *)onlist->fl_owner;
1412 struct nfs4_delegation *tryd =
1413 (struct nfs4_delegation *)try->fl_owner;
1415 if (onlist->fl_lmops != try->fl_lmops)
1418 return onlistd->dl_client == tryd->dl_client;
1423 int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
1426 return lease_modify(onlist, arg);
1431 static struct lock_manager_operations nfsd_lease_mng_ops = {
1432 .fl_break = nfsd_break_deleg_cb,
1433 .fl_release_private = nfsd_release_deleg_cb,
1434 .fl_copy_lock = nfsd_copy_lock_deleg_cb,
1435 .fl_mylease = nfsd_same_client_deleg_cb,
1436 .fl_change = nfsd_change_deleg_cb,
1441 nfsd4_process_open1(struct nfsd4_open *open)
1443 clientid_t *clientid = &open->op_clientid;
1444 struct nfs4_client *clp = NULL;
1445 unsigned int strhashval;
1446 struct nfs4_stateowner *sop = NULL;
1448 if (!check_name(open->op_owner))
1449 return nfserr_inval;
1451 if (STALE_CLIENTID(&open->op_clientid))
1452 return nfserr_stale_clientid;
1454 strhashval = ownerstr_hashval(clientid->cl_id, open->op_owner);
1455 sop = find_openstateowner_str(strhashval, open);
1456 open->op_stateowner = sop;
1458 /* Make sure the client's lease hasn't expired. */
1459 clp = find_confirmed_client(clientid);
1461 return nfserr_expired;
1464 if (!sop->so_confirmed) {
1465 /* Replace unconfirmed owners without checking for replay. */
1466 clp = sop->so_client;
1467 release_stateowner(sop);
1468 open->op_stateowner = NULL;
1471 if (open->op_seqid == sop->so_seqid - 1) {
1472 if (sop->so_replay.rp_buflen)
1473 return NFSERR_REPLAY_ME;
1474 /* The original OPEN failed so spectacularly
1475 * that we don't even have replay data saved!
1476 * Therefore, we have no choice but to continue
1477 * processing this OPEN; presumably, we'll
1478 * fail again for the same reason.
1480 dprintk("nfsd4_process_open1: replay with no replay cache\n");
1483 if (open->op_seqid != sop->so_seqid)
1484 return nfserr_bad_seqid;
1486 if (open->op_stateowner == NULL) {
1487 sop = alloc_init_open_stateowner(strhashval, clp, open);
1489 return nfserr_resource;
1490 open->op_stateowner = sop;
1492 list_del_init(&sop->so_close_lru);
1493 renew_client(sop->so_client);
1498 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
1500 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
1501 return nfserr_openmode;
1506 static struct nfs4_delegation *
1507 find_delegation_file(struct nfs4_file *fp, stateid_t *stid)
1509 struct nfs4_delegation *dp;
1511 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile) {
1512 if (dp->dl_stateid.si_stateownerid == stid->si_stateownerid)
1519 nfs4_check_deleg(struct nfs4_file *fp, struct nfsd4_open *open,
1520 struct nfs4_delegation **dp)
1523 int status = nfserr_bad_stateid;
1525 *dp = find_delegation_file(fp, &open->op_delegate_stateid);
1528 flags = open->op_share_access == NFS4_SHARE_ACCESS_READ ?
1529 RD_STATE : WR_STATE;
1530 status = nfs4_check_delegmode(*dp, flags);
1534 if (open->op_claim_type != NFS4_OPEN_CLAIM_DELEGATE_CUR)
1538 open->op_stateowner->so_confirmed = 1;
1543 nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_stateid **stpp)
1545 struct nfs4_stateid *local;
1546 int status = nfserr_share_denied;
1547 struct nfs4_stateowner *sop = open->op_stateowner;
1549 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
1550 /* ignore lock owners */
1551 if (local->st_stateowner->so_is_open_owner == 0)
1553 /* remember if we have seen this open owner */
1554 if (local->st_stateowner == sop)
1556 /* check for conflicting share reservations */
1557 if (!test_share(local, open))
1565 static inline struct nfs4_stateid *
1566 nfs4_alloc_stateid(void)
1568 return kmem_cache_alloc(stateid_slab, GFP_KERNEL);
1572 nfs4_new_open(struct svc_rqst *rqstp, struct nfs4_stateid **stpp,
1573 struct nfs4_delegation *dp,
1574 struct svc_fh *cur_fh, int flags)
1576 struct nfs4_stateid *stp;
1578 stp = nfs4_alloc_stateid();
1580 return nfserr_resource;
1583 get_file(dp->dl_vfs_file);
1584 stp->st_vfs_file = dp->dl_vfs_file;
1587 status = nfsd_open(rqstp, cur_fh, S_IFREG, flags,
1590 if (status == nfserr_dropit)
1591 status = nfserr_jukebox;
1592 kmem_cache_free(stateid_slab, stp);
1601 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
1602 struct nfsd4_open *open)
1604 struct iattr iattr = {
1605 .ia_valid = ATTR_SIZE,
1608 if (!open->op_truncate)
1610 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
1611 return nfserr_inval;
1612 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
1616 nfs4_upgrade_open(struct svc_rqst *rqstp, struct svc_fh *cur_fh, struct nfs4_stateid *stp, struct nfsd4_open *open)
1618 struct file *filp = stp->st_vfs_file;
1619 struct inode *inode = filp->f_dentry->d_inode;
1620 unsigned int share_access, new_writer;
1623 set_access(&share_access, stp->st_access_bmap);
1624 new_writer = (~share_access) & open->op_share_access
1625 & NFS4_SHARE_ACCESS_WRITE;
1628 status = get_write_access(inode);
1630 return nfserrno(status);
1632 status = nfsd4_truncate(rqstp, cur_fh, open);
1635 put_write_access(inode);
1638 /* remember the open */
1639 filp->f_mode |= open->op_share_access;
1640 set_bit(open->op_share_access, &stp->st_access_bmap);
1641 set_bit(open->op_share_deny, &stp->st_deny_bmap);
1648 nfs4_set_claim_prev(struct nfsd4_open *open)
1650 open->op_stateowner->so_confirmed = 1;
1651 open->op_stateowner->so_client->cl_firststate = 1;
1655 * Attempt to hand out a delegation.
1658 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, struct nfs4_stateid *stp)
1660 struct nfs4_delegation *dp;
1661 struct nfs4_stateowner *sop = stp->st_stateowner;
1662 struct nfs4_callback *cb = &sop->so_client->cl_callback;
1663 struct file_lock fl, *flp = &fl;
1664 int status, flag = 0;
1666 flag = NFS4_OPEN_DELEGATE_NONE;
1667 open->op_recall = 0;
1668 switch (open->op_claim_type) {
1669 case NFS4_OPEN_CLAIM_PREVIOUS:
1670 if (!atomic_read(&cb->cb_set))
1671 open->op_recall = 1;
1672 flag = open->op_delegate_type;
1673 if (flag == NFS4_OPEN_DELEGATE_NONE)
1676 case NFS4_OPEN_CLAIM_NULL:
1677 /* Let's not give out any delegations till everyone's
1678 * had the chance to reclaim theirs.... */
1679 if (nfs4_in_grace())
1681 if (!atomic_read(&cb->cb_set) || !sop->so_confirmed)
1683 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1684 flag = NFS4_OPEN_DELEGATE_WRITE;
1686 flag = NFS4_OPEN_DELEGATE_READ;
1692 dp = alloc_init_deleg(sop->so_client, stp, fh, flag);
1694 flag = NFS4_OPEN_DELEGATE_NONE;
1697 locks_init_lock(&fl);
1698 fl.fl_lmops = &nfsd_lease_mng_ops;
1699 fl.fl_flags = FL_LEASE;
1700 fl.fl_end = OFFSET_MAX;
1701 fl.fl_owner = (fl_owner_t)dp;
1702 fl.fl_file = stp->st_vfs_file;
1703 fl.fl_pid = current->tgid;
1705 /* setlease checks to see if delegation should be handed out.
1706 * the lock_manager callbacks fl_mylease and fl_change are used
1708 if ((status = setlease(stp->st_vfs_file,
1709 flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK, &flp))) {
1710 dprintk("NFSD: setlease failed [%d], no delegation\n", status);
1711 unhash_delegation(dp);
1712 flag = NFS4_OPEN_DELEGATE_NONE;
1716 memcpy(&open->op_delegate_stateid, &dp->dl_stateid, sizeof(dp->dl_stateid));
1718 dprintk("NFSD: delegation stateid=(%08x/%08x/%08x/%08x)\n\n",
1719 dp->dl_stateid.si_boot,
1720 dp->dl_stateid.si_stateownerid,
1721 dp->dl_stateid.si_fileid,
1722 dp->dl_stateid.si_generation);
1724 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS
1725 && flag == NFS4_OPEN_DELEGATE_NONE
1726 && open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
1727 printk("NFSD: WARNING: refusing delegation reclaim\n");
1728 open->op_delegate_type = flag;
1732 * called with nfs4_lock_state() held.
1735 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
1737 struct nfs4_file *fp = NULL;
1738 struct inode *ino = current_fh->fh_dentry->d_inode;
1739 struct nfs4_stateid *stp = NULL;
1740 struct nfs4_delegation *dp = NULL;
1743 status = nfserr_inval;
1744 if (!TEST_ACCESS(open->op_share_access) || !TEST_DENY(open->op_share_deny))
1747 * Lookup file; if found, lookup stateid and check open request,
1748 * and check for delegations in the process of being recalled.
1749 * If not found, create the nfs4_file struct
1751 fp = find_file(ino);
1753 if ((status = nfs4_check_open(fp, open, &stp)))
1755 status = nfs4_check_deleg(fp, open, &dp);
1759 status = nfserr_bad_stateid;
1760 if (open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR)
1762 status = nfserr_resource;
1763 fp = alloc_init_file(ino);
1769 * OPEN the file, or upgrade an existing OPEN.
1770 * If truncate fails, the OPEN fails.
1773 /* Stateid was found, this is an OPEN upgrade */
1774 status = nfs4_upgrade_open(rqstp, current_fh, stp, open);
1777 update_stateid(&stp->st_stateid);
1779 /* Stateid was not found, this is a new OPEN */
1781 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
1785 status = nfs4_new_open(rqstp, &stp, dp, current_fh, flags);
1788 init_stateid(stp, fp, open);
1789 status = nfsd4_truncate(rqstp, current_fh, open);
1791 release_stateid(stp, OPEN_STATE);
1795 memcpy(&open->op_stateid, &stp->st_stateid, sizeof(stateid_t));
1798 * Attempt to hand out a delegation. No error return, because the
1799 * OPEN succeeds even if we fail.
1801 nfs4_open_delegation(current_fh, open, stp);
1805 dprintk("nfs4_process_open2: stateid=(%08x/%08x/%08x/%08x)\n",
1806 stp->st_stateid.si_boot, stp->st_stateid.si_stateownerid,
1807 stp->st_stateid.si_fileid, stp->st_stateid.si_generation);
1811 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
1812 nfs4_set_claim_prev(open);
1814 * To finish the open response, we just need to set the rflags.
1816 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
1817 if (!open->op_stateowner->so_confirmed)
1818 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
1823 static struct workqueue_struct *laundry_wq;
1824 static struct work_struct laundromat_work;
1825 static void laundromat_main(void *);
1826 static DECLARE_WORK(laundromat_work, laundromat_main, NULL);
1829 nfsd4_renew(clientid_t *clid)
1831 struct nfs4_client *clp;
1835 dprintk("process_renew(%08x/%08x): starting\n",
1836 clid->cl_boot, clid->cl_id);
1837 status = nfserr_stale_clientid;
1838 if (STALE_CLIENTID(clid))
1840 clp = find_confirmed_client(clid);
1841 status = nfserr_expired;
1843 /* We assume the client took too long to RENEW. */
1844 dprintk("nfsd4_renew: clientid not found!\n");
1848 status = nfserr_cb_path_down;
1849 if (!list_empty(&clp->cl_delegations)
1850 && !atomic_read(&clp->cl_callback.cb_set))
1854 nfs4_unlock_state();
1861 dprintk("NFSD: end of grace period\n");
1862 nfsd4_recdir_purge_old();
1867 nfs4_laundromat(void)
1869 struct nfs4_client *clp;
1870 struct nfs4_stateowner *sop;
1871 struct nfs4_delegation *dp;
1872 struct list_head *pos, *next, reaplist;
1873 time_t cutoff = get_seconds() - NFSD_LEASE_TIME;
1874 time_t t, clientid_val = NFSD_LEASE_TIME;
1875 time_t u, test_val = NFSD_LEASE_TIME;
1879 dprintk("NFSD: laundromat service - starting\n");
1882 list_for_each_safe(pos, next, &client_lru) {
1883 clp = list_entry(pos, struct nfs4_client, cl_lru);
1884 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
1885 t = clp->cl_time - cutoff;
1886 if (clientid_val > t)
1890 dprintk("NFSD: purging unused client (clientid %08x)\n",
1891 clp->cl_clientid.cl_id);
1892 nfsd4_remove_clid_dir(clp);
1895 INIT_LIST_HEAD(&reaplist);
1896 spin_lock(&recall_lock);
1897 list_for_each_safe(pos, next, &del_recall_lru) {
1898 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1899 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
1900 u = dp->dl_time - cutoff;
1905 dprintk("NFSD: purging unused delegation dp %p, fp %p\n",
1907 list_move(&dp->dl_recall_lru, &reaplist);
1909 spin_unlock(&recall_lock);
1910 list_for_each_safe(pos, next, &reaplist) {
1911 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
1912 list_del_init(&dp->dl_recall_lru);
1913 unhash_delegation(dp);
1915 test_val = NFSD_LEASE_TIME;
1916 list_for_each_safe(pos, next, &close_lru) {
1917 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
1918 if (time_after((unsigned long)sop->so_time, (unsigned long)cutoff)) {
1919 u = sop->so_time - cutoff;
1924 dprintk("NFSD: purging unused open stateowner (so_id %d)\n",
1926 list_del(&sop->so_close_lru);
1927 nfs4_put_stateowner(sop);
1929 if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
1930 clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
1931 nfs4_unlock_state();
1932 return clientid_val;
1936 laundromat_main(void *not_used)
1940 t = nfs4_laundromat();
1941 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
1942 queue_delayed_work(laundry_wq, &laundromat_work, t*HZ);
1945 static struct nfs4_stateowner *
1946 search_close_lru(u32 st_id, int flags)
1948 struct nfs4_stateowner *local = NULL;
1950 if (flags & CLOSE_STATE) {
1951 list_for_each_entry(local, &close_lru, so_close_lru) {
1952 if (local->so_id == st_id)
1960 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stateid *stp)
1962 return fhp->fh_dentry->d_inode != stp->st_vfs_file->f_dentry->d_inode;
1966 STALE_STATEID(stateid_t *stateid)
1968 if (stateid->si_boot == boot_time)
1970 dprintk("NFSD: stale stateid (%08x/%08x/%08x/%08x)!\n",
1971 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
1972 stateid->si_generation);
1977 access_permit_read(unsigned long access_bmap)
1979 return test_bit(NFS4_SHARE_ACCESS_READ, &access_bmap) ||
1980 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap) ||
1981 test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap);
1985 access_permit_write(unsigned long access_bmap)
1987 return test_bit(NFS4_SHARE_ACCESS_WRITE, &access_bmap) ||
1988 test_bit(NFS4_SHARE_ACCESS_BOTH, &access_bmap);
1992 int nfs4_check_openmode(struct nfs4_stateid *stp, int flags)
1994 int status = nfserr_openmode;
1996 if ((flags & WR_STATE) && (!access_permit_write(stp->st_access_bmap)))
1998 if ((flags & RD_STATE) && (!access_permit_read(stp->st_access_bmap)))
2006 check_special_stateids(svc_fh *current_fh, stateid_t *stateid, int flags)
2008 /* Trying to call delegreturn with a special stateid? Yuch: */
2009 if (!(flags & (RD_STATE | WR_STATE)))
2010 return nfserr_bad_stateid;
2011 else if (ONE_STATEID(stateid) && (flags & RD_STATE))
2013 else if (nfs4_in_grace()) {
2014 /* Answer in remaining cases depends on existance of
2015 * conflicting state; so we must wait out the grace period. */
2016 return nfserr_grace;
2017 } else if (flags & WR_STATE)
2018 return nfs4_share_conflict(current_fh,
2019 NFS4_SHARE_DENY_WRITE);
2020 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
2021 return nfs4_share_conflict(current_fh,
2022 NFS4_SHARE_DENY_READ);
2026 * Allow READ/WRITE during grace period on recovered state only for files
2027 * that are not able to provide mandatory locking.
2030 io_during_grace_disallowed(struct inode *inode, int flags)
2032 return nfs4_in_grace() && (flags & (RD_STATE | WR_STATE))
2033 && MANDATORY_LOCK(inode);
2037 * Checks for stateid operations
2040 nfs4_preprocess_stateid_op(struct svc_fh *current_fh, stateid_t *stateid, int flags, struct file **filpp)
2042 struct nfs4_stateid *stp = NULL;
2043 struct nfs4_delegation *dp = NULL;
2045 struct inode *ino = current_fh->fh_dentry->d_inode;
2048 dprintk("NFSD: preprocess_stateid_op: stateid = (%08x/%08x/%08x/%08x)\n",
2049 stateid->si_boot, stateid->si_stateownerid,
2050 stateid->si_fileid, stateid->si_generation);
2054 if (io_during_grace_disallowed(ino, flags))
2055 return nfserr_grace;
2057 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
2058 return check_special_stateids(current_fh, stateid, flags);
2061 status = nfserr_stale_stateid;
2062 if (STALE_STATEID(stateid))
2066 status = nfserr_bad_stateid;
2067 if (!stateid->si_fileid) { /* delegation stateid */
2068 if(!(dp = find_delegation_stateid(ino, stateid))) {
2069 dprintk("NFSD: delegation stateid not found\n");
2070 if (nfs4_in_grace())
2071 status = nfserr_grace;
2074 stidp = &dp->dl_stateid;
2075 } else { /* open or lock stateid */
2076 if (!(stp = find_stateid(stateid, flags))) {
2077 dprintk("NFSD: open or lock stateid not found\n");
2078 if (nfs4_in_grace())
2079 status = nfserr_grace;
2082 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp))
2084 if (!stp->st_stateowner->so_confirmed)
2086 stidp = &stp->st_stateid;
2088 if (stateid->si_generation > stidp->si_generation)
2092 status = nfserr_old_stateid;
2093 if (stateid->si_generation < stidp->si_generation)
2096 if ((status = nfs4_check_openmode(stp,flags)))
2098 renew_client(stp->st_stateowner->so_client);
2100 *filpp = stp->st_vfs_file;
2102 if ((status = nfs4_check_delegmode(dp, flags)))
2104 renew_client(dp->dl_client);
2105 if (flags & DELEG_RET)
2106 unhash_delegation(dp);
2108 *filpp = dp->dl_vfs_file;
2118 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
2119 RD_STATE : WR_STATE;
2123 * Checks for sequence id mutating operations.
2126 nfs4_preprocess_seqid_op(struct svc_fh *current_fh, u32 seqid, stateid_t *stateid, int flags, struct nfs4_stateowner **sopp, struct nfs4_stateid **stpp, struct nfsd4_lock *lock)
2128 struct nfs4_stateid *stp;
2129 struct nfs4_stateowner *sop;
2131 dprintk("NFSD: preprocess_seqid_op: seqid=%d "
2132 "stateid = (%08x/%08x/%08x/%08x)\n", seqid,
2133 stateid->si_boot, stateid->si_stateownerid, stateid->si_fileid,
2134 stateid->si_generation);
2139 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
2140 printk("NFSD: preprocess_seqid_op: magic stateid!\n");
2141 return nfserr_bad_stateid;
2144 if (STALE_STATEID(stateid))
2145 return nfserr_stale_stateid;
2147 * We return BAD_STATEID if filehandle doesn't match stateid,
2148 * the confirmed flag is incorrecly set, or the generation
2149 * number is incorrect.
2151 stp = find_stateid(stateid, flags);
2154 * Also, we should make sure this isn't just the result of
2157 sop = search_close_lru(stateid->si_stateownerid, flags);
2159 return nfserr_bad_stateid;
2165 struct nfs4_stateowner *sop = stp->st_stateowner;
2166 clientid_t *lockclid = &lock->v.new.clientid;
2167 struct nfs4_client *clp = sop->so_client;
2171 lkflg = setlkflg(lock->lk_type);
2173 if (lock->lk_is_new) {
2174 if (!sop->so_is_open_owner)
2175 return nfserr_bad_stateid;
2176 if (!cmp_clid(&clp->cl_clientid, lockclid))
2177 return nfserr_bad_stateid;
2178 /* stp is the open stateid */
2179 status = nfs4_check_openmode(stp, lkflg);
2183 /* stp is the lock stateid */
2184 status = nfs4_check_openmode(stp->st_openstp, lkflg);
2191 if ((flags & CHECK_FH) && nfs4_check_fh(current_fh, stp)) {
2192 printk("NFSD: preprocess_seqid_op: fh-stateid mismatch!\n");
2193 return nfserr_bad_stateid;
2197 *sopp = sop = stp->st_stateowner;
2200 * We now validate the seqid and stateid generation numbers.
2201 * For the moment, we ignore the possibility of
2202 * generation number wraparound.
2204 if (seqid != sop->so_seqid)
2207 if (sop->so_confirmed && flags & CONFIRM) {
2208 printk("NFSD: preprocess_seqid_op: expected"
2209 " unconfirmed stateowner!\n");
2210 return nfserr_bad_stateid;
2212 if (!sop->so_confirmed && !(flags & CONFIRM)) {
2213 printk("NFSD: preprocess_seqid_op: stateowner not"
2214 " confirmed yet!\n");
2215 return nfserr_bad_stateid;
2217 if (stateid->si_generation > stp->st_stateid.si_generation) {
2218 printk("NFSD: preprocess_seqid_op: future stateid?!\n");
2219 return nfserr_bad_stateid;
2222 if (stateid->si_generation < stp->st_stateid.si_generation) {
2223 printk("NFSD: preprocess_seqid_op: old stateid!\n");
2224 return nfserr_old_stateid;
2226 renew_client(sop->so_client);
2230 if (seqid == sop->so_seqid - 1) {
2231 dprintk("NFSD: preprocess_seqid_op: retransmission?\n");
2232 /* indicate replay to calling function */
2233 return NFSERR_REPLAY_ME;
2235 printk("NFSD: preprocess_seqid_op: bad seqid (expected %d, got %d)\n",
2236 sop->so_seqid, seqid);
2238 return nfserr_bad_seqid;
2242 nfsd4_open_confirm(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_confirm *oc, struct nfs4_stateowner **replay_owner)
2245 struct nfs4_stateowner *sop;
2246 struct nfs4_stateid *stp;
2248 dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
2249 (int)current_fh->fh_dentry->d_name.len,
2250 current_fh->fh_dentry->d_name.name);
2252 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2257 if ((status = nfs4_preprocess_seqid_op(current_fh, oc->oc_seqid,
2258 &oc->oc_req_stateid,
2259 CHECK_FH | CONFIRM | OPEN_STATE,
2260 &oc->oc_stateowner, &stp, NULL)))
2263 sop = oc->oc_stateowner;
2264 sop->so_confirmed = 1;
2265 update_stateid(&stp->st_stateid);
2266 memcpy(&oc->oc_resp_stateid, &stp->st_stateid, sizeof(stateid_t));
2267 dprintk("NFSD: nfsd4_open_confirm: success, seqid=%d "
2268 "stateid=(%08x/%08x/%08x/%08x)\n", oc->oc_seqid,
2269 stp->st_stateid.si_boot,
2270 stp->st_stateid.si_stateownerid,
2271 stp->st_stateid.si_fileid,
2272 stp->st_stateid.si_generation);
2274 nfsd4_create_clid_dir(sop->so_client);
2276 if (oc->oc_stateowner) {
2277 nfs4_get_stateowner(oc->oc_stateowner);
2278 *replay_owner = oc->oc_stateowner;
2280 nfs4_unlock_state();
2286 * unset all bits in union bitmap (bmap) that
2287 * do not exist in share (from successful OPEN_DOWNGRADE)
2290 reset_union_bmap_access(unsigned long access, unsigned long *bmap)
2293 for (i = 1; i < 4; i++) {
2294 if ((i & access) != i)
2295 __clear_bit(i, bmap);
2300 reset_union_bmap_deny(unsigned long deny, unsigned long *bmap)
2303 for (i = 0; i < 4; i++) {
2304 if ((i & deny) != i)
2305 __clear_bit(i, bmap);
2310 nfsd4_open_downgrade(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open_downgrade *od, struct nfs4_stateowner **replay_owner)
2313 struct nfs4_stateid *stp;
2314 unsigned int share_access;
2316 dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
2317 (int)current_fh->fh_dentry->d_name.len,
2318 current_fh->fh_dentry->d_name.name);
2320 if (!TEST_ACCESS(od->od_share_access) || !TEST_DENY(od->od_share_deny))
2321 return nfserr_inval;
2324 if ((status = nfs4_preprocess_seqid_op(current_fh, od->od_seqid,
2326 CHECK_FH | OPEN_STATE,
2327 &od->od_stateowner, &stp, NULL)))
2330 status = nfserr_inval;
2331 if (!test_bit(od->od_share_access, &stp->st_access_bmap)) {
2332 dprintk("NFSD:access not a subset current bitmap: 0x%lx, input access=%08x\n",
2333 stp->st_access_bmap, od->od_share_access);
2336 if (!test_bit(od->od_share_deny, &stp->st_deny_bmap)) {
2337 dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
2338 stp->st_deny_bmap, od->od_share_deny);
2341 set_access(&share_access, stp->st_access_bmap);
2342 nfs4_file_downgrade(stp->st_vfs_file,
2343 share_access & ~od->od_share_access);
2345 reset_union_bmap_access(od->od_share_access, &stp->st_access_bmap);
2346 reset_union_bmap_deny(od->od_share_deny, &stp->st_deny_bmap);
2348 update_stateid(&stp->st_stateid);
2349 memcpy(&od->od_stateid, &stp->st_stateid, sizeof(stateid_t));
2352 if (od->od_stateowner) {
2353 nfs4_get_stateowner(od->od_stateowner);
2354 *replay_owner = od->od_stateowner;
2356 nfs4_unlock_state();
2361 * nfs4_unlock_state() called after encode
2364 nfsd4_close(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_close *close, struct nfs4_stateowner **replay_owner)
2367 struct nfs4_stateid *stp;
2369 dprintk("NFSD: nfsd4_close on file %.*s\n",
2370 (int)current_fh->fh_dentry->d_name.len,
2371 current_fh->fh_dentry->d_name.name);
2374 /* check close_lru for replay */
2375 if ((status = nfs4_preprocess_seqid_op(current_fh, close->cl_seqid,
2377 CHECK_FH | OPEN_STATE | CLOSE_STATE,
2378 &close->cl_stateowner, &stp, NULL)))
2381 update_stateid(&stp->st_stateid);
2382 memcpy(&close->cl_stateid, &stp->st_stateid, sizeof(stateid_t));
2384 /* release_stateid() calls nfsd_close() if needed */
2385 release_stateid(stp, OPEN_STATE);
2387 /* place unused nfs4_stateowners on so_close_lru list to be
2388 * released by the laundromat service after the lease period
2389 * to enable us to handle CLOSE replay
2391 if (list_empty(&close->cl_stateowner->so_stateids))
2392 move_to_close_lru(close->cl_stateowner);
2394 if (close->cl_stateowner) {
2395 nfs4_get_stateowner(close->cl_stateowner);
2396 *replay_owner = close->cl_stateowner;
2398 nfs4_unlock_state();
2403 nfsd4_delegreturn(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_delegreturn *dr)
2407 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0)))
2411 status = nfs4_preprocess_stateid_op(current_fh, &dr->dr_stateid, DELEG_RET, NULL);
2412 nfs4_unlock_state();
2419 * Lock owner state (byte-range locks)
2421 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
2422 #define LOCK_HASH_BITS 8
2423 #define LOCK_HASH_SIZE (1 << LOCK_HASH_BITS)
2424 #define LOCK_HASH_MASK (LOCK_HASH_SIZE - 1)
2426 #define lockownerid_hashval(id) \
2427 ((id) & LOCK_HASH_MASK)
2429 static inline unsigned int
2430 lock_ownerstr_hashval(struct inode *inode, u32 cl_id,
2431 struct xdr_netobj *ownername)
2433 return (file_hashval(inode) + cl_id
2434 + opaque_hashval(ownername->data, ownername->len))
2438 static struct list_head lock_ownerid_hashtbl[LOCK_HASH_SIZE];
2439 static struct list_head lock_ownerstr_hashtbl[LOCK_HASH_SIZE];
2440 static struct list_head lockstateid_hashtbl[STATEID_HASH_SIZE];
2442 static struct nfs4_stateid *
2443 find_stateid(stateid_t *stid, int flags)
2445 struct nfs4_stateid *local = NULL;
2446 u32 st_id = stid->si_stateownerid;
2447 u32 f_id = stid->si_fileid;
2448 unsigned int hashval;
2450 dprintk("NFSD: find_stateid flags 0x%x\n",flags);
2451 if ((flags & LOCK_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2452 hashval = stateid_hashval(st_id, f_id);
2453 list_for_each_entry(local, &lockstateid_hashtbl[hashval], st_hash) {
2454 if ((local->st_stateid.si_stateownerid == st_id) &&
2455 (local->st_stateid.si_fileid == f_id))
2459 if ((flags & OPEN_STATE) || (flags & RD_STATE) || (flags & WR_STATE)) {
2460 hashval = stateid_hashval(st_id, f_id);
2461 list_for_each_entry(local, &stateid_hashtbl[hashval], st_hash) {
2462 if ((local->st_stateid.si_stateownerid == st_id) &&
2463 (local->st_stateid.si_fileid == f_id))
2470 static struct nfs4_delegation *
2471 find_delegation_stateid(struct inode *ino, stateid_t *stid)
2473 struct nfs4_file *fp;
2474 struct nfs4_delegation *dl;
2476 dprintk("NFSD:find_delegation_stateid stateid=(%08x/%08x/%08x/%08x)\n",
2477 stid->si_boot, stid->si_stateownerid,
2478 stid->si_fileid, stid->si_generation);
2480 fp = find_file(ino);
2483 dl = find_delegation_file(fp, stid);
2489 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
2490 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
2491 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
2492 * locking, this prevents us from being completely protocol-compliant. The
2493 * real solution to this problem is to start using unsigned file offsets in
2494 * the VFS, but this is a very deep change!
2497 nfs4_transform_lock_offset(struct file_lock *lock)
2499 if (lock->fl_start < 0)
2500 lock->fl_start = OFFSET_MAX;
2501 if (lock->fl_end < 0)
2502 lock->fl_end = OFFSET_MAX;
2505 /* Hack!: For now, we're defining this just so we can use a pointer to it
2506 * as a unique cookie to identify our (NFSv4's) posix locks. */
2507 static struct lock_manager_operations nfsd_posix_mng_ops = {
2511 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
2513 struct nfs4_stateowner *sop;
2516 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
2517 sop = (struct nfs4_stateowner *) fl->fl_owner;
2518 hval = lockownerid_hashval(sop->so_id);
2519 kref_get(&sop->so_ref);
2521 deny->ld_clientid = sop->so_client->cl_clientid;
2523 deny->ld_sop = NULL;
2524 deny->ld_clientid.cl_boot = 0;
2525 deny->ld_clientid.cl_id = 0;
2527 deny->ld_start = fl->fl_start;
2528 deny->ld_length = ~(u64)0;
2529 if (fl->fl_end != ~(u64)0)
2530 deny->ld_length = fl->fl_end - fl->fl_start + 1;
2531 deny->ld_type = NFS4_READ_LT;
2532 if (fl->fl_type != F_RDLCK)
2533 deny->ld_type = NFS4_WRITE_LT;
2536 static struct nfs4_stateowner *
2537 find_lockstateowner_str(struct inode *inode, clientid_t *clid,
2538 struct xdr_netobj *owner)
2540 unsigned int hashval = lock_ownerstr_hashval(inode, clid->cl_id, owner);
2541 struct nfs4_stateowner *op;
2543 list_for_each_entry(op, &lock_ownerstr_hashtbl[hashval], so_strhash) {
2544 if (cmp_owner_str(op, owner, clid))
2551 * Alloc a lock owner structure.
2552 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
2555 * strhashval = lock_ownerstr_hashval
2558 static struct nfs4_stateowner *
2559 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_stateid *open_stp, struct nfsd4_lock *lock) {
2560 struct nfs4_stateowner *sop;
2561 struct nfs4_replay *rp;
2562 unsigned int idhashval;
2564 if (!(sop = alloc_stateowner(&lock->lk_new_owner)))
2566 idhashval = lockownerid_hashval(current_ownerid);
2567 INIT_LIST_HEAD(&sop->so_idhash);
2568 INIT_LIST_HEAD(&sop->so_strhash);
2569 INIT_LIST_HEAD(&sop->so_perclient);
2570 INIT_LIST_HEAD(&sop->so_stateids);
2571 INIT_LIST_HEAD(&sop->so_perstateid);
2572 INIT_LIST_HEAD(&sop->so_close_lru); /* not used */
2574 list_add(&sop->so_idhash, &lock_ownerid_hashtbl[idhashval]);
2575 list_add(&sop->so_strhash, &lock_ownerstr_hashtbl[strhashval]);
2576 list_add(&sop->so_perstateid, &open_stp->st_lockowners);
2577 sop->so_is_open_owner = 0;
2578 sop->so_id = current_ownerid++;
2579 sop->so_client = clp;
2580 /* It is the openowner seqid that will be incremented in encode in the
2581 * case of new lockowners; so increment the lock seqid manually: */
2582 sop->so_seqid = lock->lk_new_lock_seqid + 1;
2583 sop->so_confirmed = 1;
2584 rp = &sop->so_replay;
2585 rp->rp_status = nfserr_serverfault;
2587 rp->rp_buf = rp->rp_ibuf;
2591 static struct nfs4_stateid *
2592 alloc_init_lock_stateid(struct nfs4_stateowner *sop, struct nfs4_file *fp, struct nfs4_stateid *open_stp)
2594 struct nfs4_stateid *stp;
2595 unsigned int hashval = stateid_hashval(sop->so_id, fp->fi_id);
2597 stp = nfs4_alloc_stateid();
2600 INIT_LIST_HEAD(&stp->st_hash);
2601 INIT_LIST_HEAD(&stp->st_perfile);
2602 INIT_LIST_HEAD(&stp->st_perstateowner);
2603 INIT_LIST_HEAD(&stp->st_lockowners); /* not used */
2604 list_add(&stp->st_hash, &lockstateid_hashtbl[hashval]);
2605 list_add(&stp->st_perfile, &fp->fi_stateids);
2606 list_add(&stp->st_perstateowner, &sop->so_stateids);
2607 stp->st_stateowner = sop;
2610 stp->st_stateid.si_boot = boot_time;
2611 stp->st_stateid.si_stateownerid = sop->so_id;
2612 stp->st_stateid.si_fileid = fp->fi_id;
2613 stp->st_stateid.si_generation = 0;
2614 stp->st_vfs_file = open_stp->st_vfs_file; /* FIXME refcount?? */
2615 stp->st_access_bmap = open_stp->st_access_bmap;
2616 stp->st_deny_bmap = open_stp->st_deny_bmap;
2617 stp->st_openstp = open_stp;
2624 check_lock_length(u64 offset, u64 length)
2626 return ((length == 0) || ((length != ~(u64)0) &&
2627 LOFF_OVERFLOW(offset, length)));
2634 nfsd4_lock(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lock *lock, struct nfs4_stateowner **replay_owner)
2636 struct nfs4_stateowner *open_sop = NULL;
2637 struct nfs4_stateowner *lock_sop = NULL;
2638 struct nfs4_stateid *lock_stp;
2640 struct file_lock file_lock;
2641 struct file_lock conflock;
2643 unsigned int strhashval;
2645 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
2646 (long long) lock->lk_offset,
2647 (long long) lock->lk_length);
2649 if (check_lock_length(lock->lk_offset, lock->lk_length))
2650 return nfserr_inval;
2652 if ((status = fh_verify(rqstp, current_fh, S_IFREG, MAY_LOCK))) {
2653 dprintk("NFSD: nfsd4_lock: permission denied!\n");
2659 if (lock->lk_is_new) {
2661 * Client indicates that this is a new lockowner.
2662 * Use open owner and open stateid to create lock owner and
2665 struct nfs4_stateid *open_stp = NULL;
2666 struct nfs4_file *fp;
2668 status = nfserr_stale_clientid;
2669 if (STALE_CLIENTID(&lock->lk_new_clientid))
2672 /* validate and update open stateid and open seqid */
2673 status = nfs4_preprocess_seqid_op(current_fh,
2674 lock->lk_new_open_seqid,
2675 &lock->lk_new_open_stateid,
2676 CHECK_FH | OPEN_STATE,
2677 &lock->lk_replay_owner, &open_stp,
2681 open_sop = lock->lk_replay_owner;
2682 /* create lockowner and lock stateid */
2683 fp = open_stp->st_file;
2684 strhashval = lock_ownerstr_hashval(fp->fi_inode,
2685 open_sop->so_client->cl_clientid.cl_id,
2686 &lock->v.new.owner);
2687 /* XXX: Do we need to check for duplicate stateowners on
2688 * the same file, or should they just be allowed (and
2689 * create new stateids)? */
2690 status = nfserr_resource;
2691 lock_sop = alloc_init_lock_stateowner(strhashval,
2692 open_sop->so_client, open_stp, lock);
2693 if (lock_sop == NULL)
2695 lock_stp = alloc_init_lock_stateid(lock_sop, fp, open_stp);
2696 if (lock_stp == NULL)
2699 /* lock (lock owner + lock stateid) already exists */
2700 status = nfs4_preprocess_seqid_op(current_fh,
2701 lock->lk_old_lock_seqid,
2702 &lock->lk_old_lock_stateid,
2703 CHECK_FH | LOCK_STATE,
2704 &lock->lk_replay_owner, &lock_stp, lock);
2707 lock_sop = lock->lk_replay_owner;
2709 /* lock->lk_replay_owner and lock_stp have been created or found */
2710 filp = lock_stp->st_vfs_file;
2712 status = nfserr_grace;
2713 if (nfs4_in_grace() && !lock->lk_reclaim)
2715 status = nfserr_no_grace;
2716 if (!nfs4_in_grace() && lock->lk_reclaim)
2719 locks_init_lock(&file_lock);
2720 switch (lock->lk_type) {
2723 file_lock.fl_type = F_RDLCK;
2726 case NFS4_WRITEW_LT:
2727 file_lock.fl_type = F_WRLCK;
2730 status = nfserr_inval;
2733 file_lock.fl_owner = (fl_owner_t)lock_sop;
2734 file_lock.fl_pid = current->tgid;
2735 file_lock.fl_file = filp;
2736 file_lock.fl_flags = FL_POSIX;
2737 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2739 file_lock.fl_start = lock->lk_offset;
2740 if ((lock->lk_length == ~(u64)0) ||
2741 LOFF_OVERFLOW(lock->lk_offset, lock->lk_length))
2742 file_lock.fl_end = ~(u64)0;
2744 file_lock.fl_end = lock->lk_offset + lock->lk_length - 1;
2745 nfs4_transform_lock_offset(&file_lock);
2748 * Try to lock the file in the VFS.
2749 * Note: locks.c uses the BKL to protect the inode's lock list.
2752 /* XXX?: Just to divert the locks_release_private at the start of
2753 * locks_copy_lock: */
2754 conflock.fl_ops = NULL;
2755 conflock.fl_lmops = NULL;
2756 status = posix_lock_file_conf(filp, &file_lock, &conflock);
2757 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf status %d\n",status);
2759 case 0: /* success! */
2760 update_stateid(&lock_stp->st_stateid);
2761 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stateid,
2764 case (EAGAIN): /* conflock holds conflicting lock */
2765 status = nfserr_denied;
2766 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
2767 nfs4_set_lock_denied(&conflock, &lock->lk_denied);
2770 status = nfserr_deadlock;
2773 dprintk("NFSD: nfsd4_lock: posix_lock_file_conf() failed! status %d\n",status);
2774 status = nfserr_resource;
2778 if (status && lock->lk_is_new && lock_sop)
2779 release_stateowner(lock_sop);
2780 if (lock->lk_replay_owner) {
2781 nfs4_get_stateowner(lock->lk_replay_owner);
2782 *replay_owner = lock->lk_replay_owner;
2784 nfs4_unlock_state();
2792 nfsd4_lockt(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_lockt *lockt)
2794 struct inode *inode;
2796 struct file_lock file_lock;
2797 struct file_lock conflock;
2800 if (nfs4_in_grace())
2801 return nfserr_grace;
2803 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
2804 return nfserr_inval;
2806 lockt->lt_stateowner = NULL;
2809 status = nfserr_stale_clientid;
2810 if (STALE_CLIENTID(&lockt->lt_clientid))
2813 if ((status = fh_verify(rqstp, current_fh, S_IFREG, 0))) {
2814 dprintk("NFSD: nfsd4_lockt: fh_verify() failed!\n");
2815 if (status == nfserr_symlink)
2816 status = nfserr_inval;
2820 inode = current_fh->fh_dentry->d_inode;
2821 locks_init_lock(&file_lock);
2822 switch (lockt->lt_type) {
2825 file_lock.fl_type = F_RDLCK;
2828 case NFS4_WRITEW_LT:
2829 file_lock.fl_type = F_WRLCK;
2832 printk("NFSD: nfs4_lockt: bad lock type!\n");
2833 status = nfserr_inval;
2837 lockt->lt_stateowner = find_lockstateowner_str(inode,
2838 &lockt->lt_clientid, &lockt->lt_owner);
2839 if (lockt->lt_stateowner)
2840 file_lock.fl_owner = (fl_owner_t)lockt->lt_stateowner;
2841 file_lock.fl_pid = current->tgid;
2842 file_lock.fl_flags = FL_POSIX;
2843 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2845 file_lock.fl_start = lockt->lt_offset;
2846 if ((lockt->lt_length == ~(u64)0) || LOFF_OVERFLOW(lockt->lt_offset, lockt->lt_length))
2847 file_lock.fl_end = ~(u64)0;
2849 file_lock.fl_end = lockt->lt_offset + lockt->lt_length - 1;
2851 nfs4_transform_lock_offset(&file_lock);
2853 /* posix_test_lock uses the struct file _only_ to resolve the inode.
2854 * since LOCKT doesn't require an OPEN, and therefore a struct
2855 * file may not exist, pass posix_test_lock a struct file with
2856 * only the dentry:inode set.
2858 memset(&file, 0, sizeof (struct file));
2859 file.f_dentry = current_fh->fh_dentry;
2862 if (posix_test_lock(&file, &file_lock, &conflock)) {
2863 status = nfserr_denied;
2864 nfs4_set_lock_denied(&conflock, &lockt->lt_denied);
2867 nfs4_unlock_state();
2872 nfsd4_locku(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_locku *locku, struct nfs4_stateowner **replay_owner)
2874 struct nfs4_stateid *stp;
2875 struct file *filp = NULL;
2876 struct file_lock file_lock;
2879 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
2880 (long long) locku->lu_offset,
2881 (long long) locku->lu_length);
2883 if (check_lock_length(locku->lu_offset, locku->lu_length))
2884 return nfserr_inval;
2888 if ((status = nfs4_preprocess_seqid_op(current_fh,
2891 CHECK_FH | LOCK_STATE,
2892 &locku->lu_stateowner, &stp, NULL)))
2895 filp = stp->st_vfs_file;
2897 locks_init_lock(&file_lock);
2898 file_lock.fl_type = F_UNLCK;
2899 file_lock.fl_owner = (fl_owner_t) locku->lu_stateowner;
2900 file_lock.fl_pid = current->tgid;
2901 file_lock.fl_file = filp;
2902 file_lock.fl_flags = FL_POSIX;
2903 file_lock.fl_lmops = &nfsd_posix_mng_ops;
2904 file_lock.fl_start = locku->lu_offset;
2906 if ((locku->lu_length == ~(u64)0) || LOFF_OVERFLOW(locku->lu_offset, locku->lu_length))
2907 file_lock.fl_end = ~(u64)0;
2909 file_lock.fl_end = locku->lu_offset + locku->lu_length - 1;
2910 nfs4_transform_lock_offset(&file_lock);
2913 * Try to unlock the file in the VFS.
2915 status = posix_lock_file(filp, &file_lock);
2917 dprintk("NFSD: nfs4_locku: posix_lock_file failed!\n");
2921 * OK, unlock succeeded; the only thing left to do is update the stateid.
2923 update_stateid(&stp->st_stateid);
2924 memcpy(&locku->lu_stateid, &stp->st_stateid, sizeof(stateid_t));
2927 if (locku->lu_stateowner) {
2928 nfs4_get_stateowner(locku->lu_stateowner);
2929 *replay_owner = locku->lu_stateowner;
2931 nfs4_unlock_state();
2935 status = nfserrno(status);
2941 * 1: locks held by lockowner
2942 * 0: no locks held by lockowner
2945 check_for_locks(struct file *filp, struct nfs4_stateowner *lowner)
2947 struct file_lock **flpp;
2948 struct inode *inode = filp->f_dentry->d_inode;
2952 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
2953 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
2964 nfsd4_release_lockowner(struct svc_rqst *rqstp, struct nfsd4_release_lockowner *rlockowner)
2966 clientid_t *clid = &rlockowner->rl_clientid;
2967 struct nfs4_stateowner *sop;
2968 struct nfs4_stateid *stp;
2969 struct xdr_netobj *owner = &rlockowner->rl_owner;
2970 struct list_head matches;
2974 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
2975 clid->cl_boot, clid->cl_id);
2977 /* XXX check for lease expiration */
2979 status = nfserr_stale_clientid;
2980 if (STALE_CLIENTID(clid))
2985 status = nfserr_locks_held;
2986 /* XXX: we're doing a linear search through all the lockowners.
2987 * Yipes! For now we'll just hope clients aren't really using
2988 * release_lockowner much, but eventually we have to fix these
2989 * data structures. */
2990 INIT_LIST_HEAD(&matches);
2991 for (i = 0; i < LOCK_HASH_SIZE; i++) {
2992 list_for_each_entry(sop, &lock_ownerid_hashtbl[i], so_idhash) {
2993 if (!cmp_owner_str(sop, owner, clid))
2995 list_for_each_entry(stp, &sop->so_stateids,
2997 if (check_for_locks(stp->st_vfs_file, sop))
2999 /* Note: so_perclient unused for lockowners,
3000 * so it's OK to fool with here. */
3001 list_add(&sop->so_perclient, &matches);
3005 /* Clients probably won't expect us to return with some (but not all)
3006 * of the lockowner state released; so don't release any until all
3007 * have been checked. */
3009 while (!list_empty(&matches)) {
3010 sop = list_entry(matches.next, struct nfs4_stateowner,
3012 /* unhash_stateowner deletes so_perclient only
3013 * for openowners. */
3014 list_del(&sop->so_perclient);
3015 release_stateowner(sop);
3018 nfs4_unlock_state();
3022 static inline struct nfs4_client_reclaim *
3025 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
3029 nfs4_has_reclaimed_state(const char *name)
3031 unsigned int strhashval = clientstr_hashval(name);
3032 struct nfs4_client *clp;
3034 clp = find_confirmed_client_by_str(name, strhashval);
3039 * failure => all reset bets are off, nfserr_no_grace...
3042 nfs4_client_to_reclaim(const char *name)
3044 unsigned int strhashval;
3045 struct nfs4_client_reclaim *crp = NULL;
3047 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
3048 crp = alloc_reclaim();
3051 strhashval = clientstr_hashval(name);
3052 INIT_LIST_HEAD(&crp->cr_strhash);
3053 list_add(&crp->cr_strhash, &reclaim_str_hashtbl[strhashval]);
3054 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
3055 reclaim_str_hashtbl_size++;
3060 nfs4_release_reclaim(void)
3062 struct nfs4_client_reclaim *crp = NULL;
3065 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3066 while (!list_empty(&reclaim_str_hashtbl[i])) {
3067 crp = list_entry(reclaim_str_hashtbl[i].next,
3068 struct nfs4_client_reclaim, cr_strhash);
3069 list_del(&crp->cr_strhash);
3071 reclaim_str_hashtbl_size--;
3074 BUG_ON(reclaim_str_hashtbl_size);
3078 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
3079 static struct nfs4_client_reclaim *
3080 nfs4_find_reclaim_client(clientid_t *clid)
3082 unsigned int strhashval;
3083 struct nfs4_client *clp;
3084 struct nfs4_client_reclaim *crp = NULL;
3087 /* find clientid in conf_id_hashtbl */
3088 clp = find_confirmed_client(clid);
3092 dprintk("NFSD: nfs4_find_reclaim_client for %.*s with recdir %s\n",
3093 clp->cl_name.len, clp->cl_name.data,
3096 /* find clp->cl_name in reclaim_str_hashtbl */
3097 strhashval = clientstr_hashval(clp->cl_recdir);
3098 list_for_each_entry(crp, &reclaim_str_hashtbl[strhashval], cr_strhash) {
3099 if (same_name(crp->cr_recdir, clp->cl_recdir)) {
3107 * Called from OPEN. Look for clientid in reclaim list.
3110 nfs4_check_open_reclaim(clientid_t *clid)
3112 return nfs4_find_reclaim_client(clid) ? nfs_ok : nfserr_reclaim_bad;
3115 /* initialization to perform at module load time: */
3118 nfs4_state_init(void)
3122 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3123 INIT_LIST_HEAD(&conf_id_hashtbl[i]);
3124 INIT_LIST_HEAD(&conf_str_hashtbl[i]);
3125 INIT_LIST_HEAD(&unconf_str_hashtbl[i]);
3126 INIT_LIST_HEAD(&unconf_id_hashtbl[i]);
3128 for (i = 0; i < FILE_HASH_SIZE; i++) {
3129 INIT_LIST_HEAD(&file_hashtbl[i]);
3131 for (i = 0; i < OWNER_HASH_SIZE; i++) {
3132 INIT_LIST_HEAD(&ownerstr_hashtbl[i]);
3133 INIT_LIST_HEAD(&ownerid_hashtbl[i]);
3135 for (i = 0; i < STATEID_HASH_SIZE; i++) {
3136 INIT_LIST_HEAD(&stateid_hashtbl[i]);
3137 INIT_LIST_HEAD(&lockstateid_hashtbl[i]);
3139 for (i = 0; i < LOCK_HASH_SIZE; i++) {
3140 INIT_LIST_HEAD(&lock_ownerid_hashtbl[i]);
3141 INIT_LIST_HEAD(&lock_ownerstr_hashtbl[i]);
3143 memset(&onestateid, ~0, sizeof(stateid_t));
3144 INIT_LIST_HEAD(&close_lru);
3145 INIT_LIST_HEAD(&client_lru);
3146 INIT_LIST_HEAD(&del_recall_lru);
3147 for (i = 0; i < CLIENT_HASH_SIZE; i++)
3148 INIT_LIST_HEAD(&reclaim_str_hashtbl[i]);
3149 reclaim_str_hashtbl_size = 0;
3153 nfsd4_load_reboot_recovery_data(void)
3158 nfsd4_init_recdir(user_recovery_dirname);
3159 status = nfsd4_recdir_load();
3160 nfs4_unlock_state();
3162 printk("NFSD: Failure reading reboot recovery data\n");
3165 /* initialization to perform when the nfsd service is started: */
3168 __nfs4_state_start(void)
3172 boot_time = get_seconds();
3173 grace_time = max(user_lease_time, lease_time);
3174 lease_time = user_lease_time;
3176 printk("NFSD: starting %ld-second grace period\n", grace_time);
3177 laundry_wq = create_singlethread_workqueue("nfsd4");
3178 queue_delayed_work(laundry_wq, &laundromat_work, grace_time*HZ);
3182 nfs4_state_start(void)
3188 status = nfsd4_init_slabs();
3191 nfsd4_load_reboot_recovery_data();
3192 __nfs4_state_start();
3204 nfs4_lease_time(void)
3210 __nfs4_state_shutdown(void)
3213 struct nfs4_client *clp = NULL;
3214 struct nfs4_delegation *dp = NULL;
3215 struct nfs4_stateowner *sop = NULL;
3216 struct list_head *pos, *next, reaplist;
3218 list_for_each_safe(pos, next, &close_lru) {
3219 sop = list_entry(pos, struct nfs4_stateowner, so_close_lru);
3220 list_del(&sop->so_close_lru);
3221 nfs4_put_stateowner(sop);
3224 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
3225 while (!list_empty(&conf_id_hashtbl[i])) {
3226 clp = list_entry(conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
3229 while (!list_empty(&unconf_str_hashtbl[i])) {
3230 clp = list_entry(unconf_str_hashtbl[i].next, struct nfs4_client, cl_strhash);
3234 INIT_LIST_HEAD(&reaplist);
3235 spin_lock(&recall_lock);
3236 list_for_each_safe(pos, next, &del_recall_lru) {
3237 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3238 list_move(&dp->dl_recall_lru, &reaplist);
3240 spin_unlock(&recall_lock);
3241 list_for_each_safe(pos, next, &reaplist) {
3242 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3243 list_del_init(&dp->dl_recall_lru);
3244 unhash_delegation(dp);
3247 cancel_delayed_work(&laundromat_work);
3248 nfsd4_shutdown_recdir();
3253 nfs4_state_shutdown(void)
3255 cancel_rearming_delayed_workqueue(laundry_wq, &laundromat_work);
3256 destroy_workqueue(laundry_wq);
3258 nfs4_release_reclaim();
3259 __nfs4_state_shutdown();
3261 nfs4_unlock_state();
3265 nfs4_set_recdir(char *recdir)
3268 strcpy(user_recovery_dirname, recdir);
3269 nfs4_unlock_state();
3273 * Change the NFSv4 recovery directory to recdir.
3276 nfs4_reset_recoverydir(char *recdir)
3279 struct nameidata nd;
3281 status = path_lookup(recdir, LOOKUP_FOLLOW, &nd);
3285 if (S_ISDIR(nd.dentry->d_inode->i_mode)) {
3286 nfs4_set_recdir(recdir);
3294 * Called when leasetime is changed.
3296 * The only way the protocol gives us to handle on-the-fly lease changes is to
3297 * simulate a reboot. Instead of doing that, we just wait till the next time
3298 * we start to register any changes in lease time. If the administrator
3299 * really wants to change the lease time *now*, they can go ahead and bring
3300 * nfsd down and then back up again after changing the lease time.
3303 nfs4_reset_lease(time_t leasetime)
3306 user_lease_time = leasetime;