xref: /linux/fs/nfs/nfs4renewd.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  fs/nfs/nfs4renewd.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 2002 The Regents of the University of Michigan.
51da177e4SLinus Torvalds  *  All rights reserved.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Kendrick Smith <kmsmith@umich.edu>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  Redistribution and use in source and binary forms, with or without
101da177e4SLinus Torvalds  *  modification, are permitted provided that the following conditions
111da177e4SLinus Torvalds  *  are met:
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  1. Redistributions of source code must retain the above copyright
141da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer.
151da177e4SLinus Torvalds  *  2. Redistributions in binary form must reproduce the above copyright
161da177e4SLinus Torvalds  *     notice, this list of conditions and the following disclaimer in the
171da177e4SLinus Torvalds  *     documentation and/or other materials provided with the distribution.
181da177e4SLinus Torvalds  *  3. Neither the name of the University nor the names of its
191da177e4SLinus Torvalds  *     contributors may be used to endorse or promote products derived
201da177e4SLinus Torvalds  *     from this software without specific prior written permission.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
231da177e4SLinus Torvalds  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
241da177e4SLinus Torvalds  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
251da177e4SLinus Torvalds  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261da177e4SLinus Torvalds  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
271da177e4SLinus Torvalds  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
281da177e4SLinus Torvalds  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
291da177e4SLinus Torvalds  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
301da177e4SLinus Torvalds  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
311da177e4SLinus Torvalds  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
321da177e4SLinus Torvalds  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  * Implementation of the NFSv4 "renew daemon", which wakes up periodically to
351da177e4SLinus Torvalds  * send a RENEW, to keep state alive on the server.  The daemon is implemented
361da177e4SLinus Torvalds  * as an rpc_task, not a real kernel thread, so it always runs in rpciod's
371da177e4SLinus Torvalds  * context.  There is one renewd per nfs_server.
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #include <linux/mm.h>
421da177e4SLinus Torvalds #include <linux/pagemap.h>
431da177e4SLinus Torvalds #include <linux/sunrpc/sched.h>
441da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h>
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #include <linux/nfs.h>
471da177e4SLinus Torvalds #include <linux/nfs4.h>
481da177e4SLinus Torvalds #include <linux/nfs_fs.h>
494ce79717STrond Myklebust #include "nfs4_fs.h"
5058d9714aSTrond Myklebust #include "delegation.h"
511da177e4SLinus Torvalds 
52e3c0fb7eSChuck Lever #define NFSDBG_FACILITY		NFSDBG_STATE
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds void
nfs4_renew_state(struct work_struct * work)5565f27f38SDavid Howells nfs4_renew_state(struct work_struct *work)
561da177e4SLinus Torvalds {
57c48f4f35STrond Myklebust 	const struct nfs4_state_maintenance_ops *ops;
5865f27f38SDavid Howells 	struct nfs_client *clp =
5965f27f38SDavid Howells 		container_of(work, struct nfs_client, cl_renewd.work);
60a52458b4SNeilBrown 	const struct cred *cred;
61dc96aef9SAlexandros Batsakis 	long lease;
621da177e4SLinus Torvalds 	unsigned long last, now;
632f60ea6bSTrond Myklebust 	unsigned renew_flags = 0;
641da177e4SLinus Torvalds 
65c48f4f35STrond Myklebust 	ops = clp->cl_mvops->state_renewal_ops;
663110ff80SHarvey Harrison 	dprintk("%s: start\n", __func__);
67fca5238eSChuck Lever 
68d3b4c9d7SAndy Adamson 	if (test_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state))
691da177e4SLinus Torvalds 		goto out;
70fca5238eSChuck Lever 
711da177e4SLinus Torvalds 	lease = clp->cl_lease_time;
721da177e4SLinus Torvalds 	last = clp->cl_last_renewal;
731da177e4SLinus Torvalds 	now = jiffies;
741da177e4SLinus Torvalds 	/* Are we close to a lease timeout? */
752f60ea6bSTrond Myklebust 	if (time_after(now, last + lease/3))
762f60ea6bSTrond Myklebust 		renew_flags |= NFS4_RENEW_TIMEOUT;
772f60ea6bSTrond Myklebust 	if (nfs_delegations_present(clp))
782f60ea6bSTrond Myklebust 		renew_flags |= NFS4_RENEW_DELEGATION_CB;
792f60ea6bSTrond Myklebust 
802f60ea6bSTrond Myklebust 	if (renew_flags != 0) {
81f15e1e8bSNeilBrown 		cred = ops->get_state_renewal_cred(clp);
82b0d3ded1STrond Myklebust 		if (cred == NULL) {
832f60ea6bSTrond Myklebust 			if (!(renew_flags & NFS4_RENEW_DELEGATION_CB)) {
84b0d3ded1STrond Myklebust 				set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
8558d9714aSTrond Myklebust 				goto out;
8658d9714aSTrond Myklebust 			}
87b0d3ded1STrond Myklebust 			nfs_expire_all_delegations(clp);
88b0d3ded1STrond Myklebust 		} else {
89d1f456b0SAndy Adamson 			int ret;
90d1f456b0SAndy Adamson 
911da177e4SLinus Torvalds 			/* Queue an asynchronous RENEW. */
92d1f456b0SAndy Adamson 			ret = ops->sched_state_renewal(clp, cred, renew_flags);
93a52458b4SNeilBrown 			put_cred(cred);
94d1f456b0SAndy Adamson 			switch (ret) {
95d1f456b0SAndy Adamson 			default:
96dc96aef9SAlexandros Batsakis 				goto out_exp;
97d1f456b0SAndy Adamson 			case -EAGAIN:
98d1f456b0SAndy Adamson 			case -ENOMEM:
99d1f456b0SAndy Adamson 				break;
100d1f456b0SAndy Adamson 			}
101b0d3ded1STrond Myklebust 		}
102dc96aef9SAlexandros Batsakis 	} else {
1031da177e4SLinus Torvalds 		dprintk("%s: failed to call renewd. Reason: lease not expired \n",
1043110ff80SHarvey Harrison 				__func__);
105dc96aef9SAlexandros Batsakis 	}
106dc96aef9SAlexandros Batsakis 	nfs4_schedule_state_renewal(clp);
107dc96aef9SAlexandros Batsakis out_exp:
108b7391f44STrond Myklebust 	nfs_expire_unreferenced_delegations(clp);
1091da177e4SLinus Torvalds out:
1103110ff80SHarvey Harrison 	dprintk("%s: done\n", __func__);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds void
nfs4_schedule_state_renewal(struct nfs_client * clp)114adfa6f98SDavid Howells nfs4_schedule_state_renewal(struct nfs_client *clp)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	long timeout;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	spin_lock(&clp->cl_lock);
1191da177e4SLinus Torvalds 	timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal
1201da177e4SLinus Torvalds 		- (long)jiffies;
1211da177e4SLinus Torvalds 	if (timeout < 5 * HZ)
1221da177e4SLinus Torvalds 		timeout = 5 * HZ;
1231da177e4SLinus Torvalds 	dprintk("%s: requeueing work. Lease period = %ld\n",
1243110ff80SHarvey Harrison 			__func__, (timeout + HZ - 1) / HZ);
12541f63c53STejun Heo 	mod_delayed_work(system_wq, &clp->cl_renewd, timeout);
1265dd3177aSTrond Myklebust 	set_bit(NFS_CS_RENEWD, &clp->cl_res_state);
1271da177e4SLinus Torvalds 	spin_unlock(&clp->cl_lock);
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds void
nfs4_kill_renewd(struct nfs_client * clp)131adfa6f98SDavid Howells nfs4_kill_renewd(struct nfs_client *clp)
1321da177e4SLinus Torvalds {
1333d39c691STrond Myklebust 	cancel_delayed_work_sync(&clp->cl_renewd);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
136fb10fb67STrond Myklebust /**
137fb10fb67STrond Myklebust  * nfs4_set_lease_period - Sets the lease period on a nfs_client
138fb10fb67STrond Myklebust  *
139fb10fb67STrond Myklebust  * @clp: pointer to nfs_client
140fb10fb67STrond Myklebust  * @lease: new value for lease period
141fb10fb67STrond Myklebust  */
nfs4_set_lease_period(struct nfs_client * clp,unsigned long lease)142fb10fb67STrond Myklebust void nfs4_set_lease_period(struct nfs_client *clp,
143*7dc2993aSRobert Milkowski 		unsigned long lease)
144fb10fb67STrond Myklebust {
145fb10fb67STrond Myklebust 	spin_lock(&clp->cl_lock);
146fb10fb67STrond Myklebust 	clp->cl_lease_time = lease;
147fb10fb67STrond Myklebust 	spin_unlock(&clp->cl_lock);
1488d480326STrond Myklebust 
1498d480326STrond Myklebust 	/* Cap maximum reconnect timeout at 1/2 lease period */
15026ae102fSTrond Myklebust 	rpc_set_connect_timeout(clp->cl_rpcclient, lease, lease >> 1);
151fb10fb67STrond Myklebust }
152