xref: /OK3568_Linux_fs/kernel/fs/nfs/nfs4renewd.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  fs/nfs/nfs4renewd.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Copyright (c) 2002 The Regents of the University of Michigan.
5*4882a593Smuzhiyun  *  All rights reserved.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  *  Kendrick Smith <kmsmith@umich.edu>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  *  Redistribution and use in source and binary forms, with or without
10*4882a593Smuzhiyun  *  modification, are permitted provided that the following conditions
11*4882a593Smuzhiyun  *  are met:
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  *  1. Redistributions of source code must retain the above copyright
14*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer.
15*4882a593Smuzhiyun  *  2. Redistributions in binary form must reproduce the above copyright
16*4882a593Smuzhiyun  *     notice, this list of conditions and the following disclaimer in the
17*4882a593Smuzhiyun  *     documentation and/or other materials provided with the distribution.
18*4882a593Smuzhiyun  *  3. Neither the name of the University nor the names of its
19*4882a593Smuzhiyun  *     contributors may be used to endorse or promote products derived
20*4882a593Smuzhiyun  *     from this software without specific prior written permission.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
23*4882a593Smuzhiyun  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24*4882a593Smuzhiyun  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25*4882a593Smuzhiyun  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*4882a593Smuzhiyun  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27*4882a593Smuzhiyun  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28*4882a593Smuzhiyun  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29*4882a593Smuzhiyun  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30*4882a593Smuzhiyun  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31*4882a593Smuzhiyun  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*4882a593Smuzhiyun  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * Implementation of the NFSv4 "renew daemon", which wakes up periodically to
35*4882a593Smuzhiyun  * send a RENEW, to keep state alive on the server.  The daemon is implemented
36*4882a593Smuzhiyun  * as an rpc_task, not a real kernel thread, so it always runs in rpciod's
37*4882a593Smuzhiyun  * context.  There is one renewd per nfs_server.
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #include <linux/mm.h>
42*4882a593Smuzhiyun #include <linux/pagemap.h>
43*4882a593Smuzhiyun #include <linux/sunrpc/sched.h>
44*4882a593Smuzhiyun #include <linux/sunrpc/clnt.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include <linux/nfs.h>
47*4882a593Smuzhiyun #include <linux/nfs4.h>
48*4882a593Smuzhiyun #include <linux/nfs_fs.h>
49*4882a593Smuzhiyun #include "nfs4_fs.h"
50*4882a593Smuzhiyun #include "delegation.h"
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define NFSDBG_FACILITY		NFSDBG_STATE
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun void
nfs4_renew_state(struct work_struct * work)55*4882a593Smuzhiyun nfs4_renew_state(struct work_struct *work)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun 	const struct nfs4_state_maintenance_ops *ops;
58*4882a593Smuzhiyun 	struct nfs_client *clp =
59*4882a593Smuzhiyun 		container_of(work, struct nfs_client, cl_renewd.work);
60*4882a593Smuzhiyun 	const struct cred *cred;
61*4882a593Smuzhiyun 	long lease;
62*4882a593Smuzhiyun 	unsigned long last, now;
63*4882a593Smuzhiyun 	unsigned renew_flags = 0;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	ops = clp->cl_mvops->state_renewal_ops;
66*4882a593Smuzhiyun 	dprintk("%s: start\n", __func__);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (test_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state))
69*4882a593Smuzhiyun 		goto out;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	lease = clp->cl_lease_time;
72*4882a593Smuzhiyun 	last = clp->cl_last_renewal;
73*4882a593Smuzhiyun 	now = jiffies;
74*4882a593Smuzhiyun 	/* Are we close to a lease timeout? */
75*4882a593Smuzhiyun 	if (time_after(now, last + lease/3))
76*4882a593Smuzhiyun 		renew_flags |= NFS4_RENEW_TIMEOUT;
77*4882a593Smuzhiyun 	if (nfs_delegations_present(clp))
78*4882a593Smuzhiyun 		renew_flags |= NFS4_RENEW_DELEGATION_CB;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (renew_flags != 0) {
81*4882a593Smuzhiyun 		cred = ops->get_state_renewal_cred(clp);
82*4882a593Smuzhiyun 		if (cred == NULL) {
83*4882a593Smuzhiyun 			if (!(renew_flags & NFS4_RENEW_DELEGATION_CB)) {
84*4882a593Smuzhiyun 				set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
85*4882a593Smuzhiyun 				goto out;
86*4882a593Smuzhiyun 			}
87*4882a593Smuzhiyun 			nfs_expire_all_delegations(clp);
88*4882a593Smuzhiyun 		} else {
89*4882a593Smuzhiyun 			int ret;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 			/* Queue an asynchronous RENEW. */
92*4882a593Smuzhiyun 			ret = ops->sched_state_renewal(clp, cred, renew_flags);
93*4882a593Smuzhiyun 			put_cred(cred);
94*4882a593Smuzhiyun 			switch (ret) {
95*4882a593Smuzhiyun 			default:
96*4882a593Smuzhiyun 				goto out_exp;
97*4882a593Smuzhiyun 			case -EAGAIN:
98*4882a593Smuzhiyun 			case -ENOMEM:
99*4882a593Smuzhiyun 				break;
100*4882a593Smuzhiyun 			}
101*4882a593Smuzhiyun 		}
102*4882a593Smuzhiyun 	} else {
103*4882a593Smuzhiyun 		dprintk("%s: failed to call renewd. Reason: lease not expired \n",
104*4882a593Smuzhiyun 				__func__);
105*4882a593Smuzhiyun 	}
106*4882a593Smuzhiyun 	nfs4_schedule_state_renewal(clp);
107*4882a593Smuzhiyun out_exp:
108*4882a593Smuzhiyun 	nfs_expire_unreferenced_delegations(clp);
109*4882a593Smuzhiyun out:
110*4882a593Smuzhiyun 	dprintk("%s: done\n", __func__);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun void
nfs4_schedule_state_renewal(struct nfs_client * clp)114*4882a593Smuzhiyun nfs4_schedule_state_renewal(struct nfs_client *clp)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	long timeout;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
119*4882a593Smuzhiyun 	timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal
120*4882a593Smuzhiyun 		- (long)jiffies;
121*4882a593Smuzhiyun 	if (timeout < 5 * HZ)
122*4882a593Smuzhiyun 		timeout = 5 * HZ;
123*4882a593Smuzhiyun 	dprintk("%s: requeueing work. Lease period = %ld\n",
124*4882a593Smuzhiyun 			__func__, (timeout + HZ - 1) / HZ);
125*4882a593Smuzhiyun 	mod_delayed_work(system_wq, &clp->cl_renewd, timeout);
126*4882a593Smuzhiyun 	set_bit(NFS_CS_RENEWD, &clp->cl_res_state);
127*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun void
nfs4_kill_renewd(struct nfs_client * clp)131*4882a593Smuzhiyun nfs4_kill_renewd(struct nfs_client *clp)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	cancel_delayed_work_sync(&clp->cl_renewd);
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /**
137*4882a593Smuzhiyun  * nfs4_set_lease_period - Sets the lease period on a nfs_client
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * @clp: pointer to nfs_client
140*4882a593Smuzhiyun  * @lease: new value for lease period
141*4882a593Smuzhiyun  */
nfs4_set_lease_period(struct nfs_client * clp,unsigned long lease)142*4882a593Smuzhiyun void nfs4_set_lease_period(struct nfs_client *clp,
143*4882a593Smuzhiyun 		unsigned long lease)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun 	spin_lock(&clp->cl_lock);
146*4882a593Smuzhiyun 	clp->cl_lease_time = lease;
147*4882a593Smuzhiyun 	spin_unlock(&clp->cl_lock);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	/* Cap maximum reconnect timeout at 1/2 lease period */
150*4882a593Smuzhiyun 	rpc_set_connect_timeout(clp->cl_rpcclient, lease, lease >> 1);
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun  * Local variables:
155*4882a593Smuzhiyun  *   c-basic-offset: 8
156*4882a593Smuzhiyun  * End:
157*4882a593Smuzhiyun  */
158