Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *  fs/nfs/nfs4renewd.c
0003  *
0004  *  Copyright (c) 2002 The Regents of the University of Michigan.
0005  *  All rights reserved.
0006  *
0007  *  Kendrick Smith <kmsmith@umich.edu>
0008  *
0009  *  Redistribution and use in source and binary forms, with or without
0010  *  modification, are permitted provided that the following conditions
0011  *  are met:
0012  *
0013  *  1. Redistributions of source code must retain the above copyright
0014  *     notice, this list of conditions and the following disclaimer.
0015  *  2. Redistributions in binary form must reproduce the above copyright
0016  *     notice, this list of conditions and the following disclaimer in the
0017  *     documentation and/or other materials provided with the distribution.
0018  *  3. Neither the name of the University nor the names of its
0019  *     contributors may be used to endorse or promote products derived
0020  *     from this software without specific prior written permission.
0021  *
0022  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
0023  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0024  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
0025  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0026  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0027  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0028  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
0029  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0030  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0031  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0032  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0033  *
0034  * Implementation of the NFSv4 "renew daemon", which wakes up periodically to
0035  * send a RENEW, to keep state alive on the server.  The daemon is implemented
0036  * as an rpc_task, not a real kernel thread, so it always runs in rpciod's
0037  * context.  There is one renewd per nfs_server.
0038  *
0039  */
0040 
0041 #include <linux/mm.h>
0042 #include <linux/pagemap.h>
0043 #include <linux/sunrpc/sched.h>
0044 #include <linux/sunrpc/clnt.h>
0045 
0046 #include <linux/nfs.h>
0047 #include <linux/nfs4.h>
0048 #include <linux/nfs_fs.h>
0049 #include "nfs4_fs.h"
0050 #include "delegation.h"
0051 
0052 #define NFSDBG_FACILITY     NFSDBG_STATE
0053 
0054 void
0055 nfs4_renew_state(struct work_struct *work)
0056 {
0057     const struct nfs4_state_maintenance_ops *ops;
0058     struct nfs_client *clp =
0059         container_of(work, struct nfs_client, cl_renewd.work);
0060     const struct cred *cred;
0061     long lease;
0062     unsigned long last, now;
0063     unsigned renew_flags = 0;
0064 
0065     ops = clp->cl_mvops->state_renewal_ops;
0066     dprintk("%s: start\n", __func__);
0067 
0068     if (test_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state))
0069         goto out;
0070 
0071     lease = clp->cl_lease_time;
0072     last = clp->cl_last_renewal;
0073     now = jiffies;
0074     /* Are we close to a lease timeout? */
0075     if (time_after(now, last + lease/3))
0076         renew_flags |= NFS4_RENEW_TIMEOUT;
0077     if (nfs_delegations_present(clp))
0078         renew_flags |= NFS4_RENEW_DELEGATION_CB;
0079 
0080     if (renew_flags != 0) {
0081         cred = ops->get_state_renewal_cred(clp);
0082         if (cred == NULL) {
0083             if (!(renew_flags & NFS4_RENEW_DELEGATION_CB)) {
0084                 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
0085                 goto out;
0086             }
0087             nfs_expire_all_delegations(clp);
0088         } else {
0089             int ret;
0090 
0091             /* Queue an asynchronous RENEW. */
0092             ret = ops->sched_state_renewal(clp, cred, renew_flags);
0093             put_cred(cred);
0094             switch (ret) {
0095             default:
0096                 goto out_exp;
0097             case -EAGAIN:
0098             case -ENOMEM:
0099                 break;
0100             }
0101         }
0102     } else {
0103         dprintk("%s: failed to call renewd. Reason: lease not expired \n",
0104                 __func__);
0105     }
0106     nfs4_schedule_state_renewal(clp);
0107 out_exp:
0108     nfs_expire_unreferenced_delegations(clp);
0109 out:
0110     dprintk("%s: done\n", __func__);
0111 }
0112 
0113 void
0114 nfs4_schedule_state_renewal(struct nfs_client *clp)
0115 {
0116     long timeout;
0117 
0118     spin_lock(&clp->cl_lock);
0119     timeout = (2 * clp->cl_lease_time) / 3 + (long)clp->cl_last_renewal
0120         - (long)jiffies;
0121     if (timeout < 5 * HZ)
0122         timeout = 5 * HZ;
0123     dprintk("%s: requeueing work. Lease period = %ld\n",
0124             __func__, (timeout + HZ - 1) / HZ);
0125     mod_delayed_work(system_wq, &clp->cl_renewd, timeout);
0126     set_bit(NFS_CS_RENEWD, &clp->cl_res_state);
0127     spin_unlock(&clp->cl_lock);
0128 }
0129 
0130 void
0131 nfs4_kill_renewd(struct nfs_client *clp)
0132 {
0133     cancel_delayed_work_sync(&clp->cl_renewd);
0134 }
0135 
0136 /**
0137  * nfs4_set_lease_period - Sets the lease period on a nfs_client
0138  *
0139  * @clp: pointer to nfs_client
0140  * @lease: new value for lease period
0141  */
0142 void nfs4_set_lease_period(struct nfs_client *clp,
0143         unsigned long lease)
0144 {
0145     spin_lock(&clp->cl_lock);
0146     clp->cl_lease_time = lease;
0147     spin_unlock(&clp->cl_lock);
0148 
0149     /* Cap maximum reconnect timeout at 1/2 lease period */
0150     rpc_set_connect_timeout(clp->cl_rpcclient, lease, lease >> 1);
0151 }