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