knfsd: lockd: nfsd4: use same grace period for lockd and nfsd4
[linux-2.6-block.git] / fs / lockd / svc.c
index 5e85bde6c123e89fb271706ffeaf2e99080dca32..82e2192a0d5c873a72b867702c511b0c8872f6a7 100644 (file)
@@ -12,7 +12,6 @@
  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/sysctl.h>
 #include <linux/slab.h>
 #include <linux/smp.h>
 #include <linux/smp_lock.h>
+#include <linux/mutex.h>
+#include <linux/freezer.h>
 
 #include <linux/sunrpc/types.h>
 #include <linux/sunrpc/stats.h>
 #include <linux/sunrpc/clnt.h>
 #include <linux/sunrpc/svc.h>
 #include <linux/sunrpc/svcsock.h>
+#include <net/ip.h>
 #include <linux/lockd/lockd.h>
+#include <linux/lockd/sm_inter.h>
 #include <linux/nfs.h>
 
 #define NLMDBG_FACILITY                NLMDBG_SVC
@@ -43,13 +46,14 @@ static struct svc_program   nlmsvc_program;
 struct nlmsvc_binding *                nlmsvc_ops;
 EXPORT_SYMBOL(nlmsvc_ops);
 
-static DECLARE_MUTEX(nlmsvc_sema);
+static DEFINE_MUTEX(nlmsvc_mutex);
 static unsigned int            nlmsvc_users;
 static pid_t                   nlmsvc_pid;
+static struct svc_serv         *nlmsvc_serv;
 int                            nlmsvc_grace_period;
 unsigned long                  nlmsvc_timeout;
 
-static DECLARE_MUTEX_LOCKED(lockd_start);
+static DECLARE_COMPLETION(lockd_start_done);
 static DECLARE_WAIT_QUEUE_HEAD(lockd_exit);
 
 /*
@@ -59,6 +63,7 @@ static DECLARE_WAIT_QUEUE_HEAD(lockd_exit);
 static unsigned long           nlm_grace_period;
 static unsigned long           nlm_timeout = LOCKD_DFLT_TIMEO;
 static int                     nlm_udpport, nlm_tcpport;
+int                            nsm_use_hostnames = 0;
 
 /*
  * Constants needed for the sysctl interface.
@@ -71,18 +76,31 @@ static const int            nlm_port_min = 0, nlm_port_max = 65535;
 
 static struct ctl_table_header * nlm_sysctl_table;
 
-static unsigned long set_grace_period(void)
+static unsigned long get_lockd_grace_period(void)
 {
-       unsigned long grace_period;
-
        /* Note: nlm_timeout should always be nonzero */
        if (nlm_grace_period)
-               grace_period = ((nlm_grace_period + nlm_timeout - 1)
-                               / nlm_timeout) * nlm_timeout * HZ;
+               return roundup(nlm_grace_period, nlm_timeout) * HZ;
        else
-               grace_period = nlm_timeout * 5 * HZ;
+               return nlm_timeout * 5 * HZ;
+}
+
+unsigned long get_nfs_grace_period(void)
+{
+       unsigned long lockdgrace = get_lockd_grace_period();
+       unsigned long nfsdgrace = 0;
+
+       if (nlmsvc_ops)
+               nfsdgrace = nlmsvc_ops->get_grace_period();
+
+       return max(lockdgrace, nfsdgrace);
+}
+EXPORT_SYMBOL(get_nfs_grace_period);
+
+static unsigned long set_grace_period(void)
+{
        nlmsvc_grace_period = 1;
-       return grace_period + jiffies;
+       return get_nfs_grace_period() + jiffies;
 }
 
 static inline void clear_grace_period(void)
@@ -96,7 +114,6 @@ static inline void clear_grace_period(void)
 static void
 lockd(struct svc_rqst *rqstp)
 {
-       struct svc_serv *serv = rqstp->rq_server;
        int             err = 0;
        unsigned long grace_period_expire;
 
@@ -112,16 +129,15 @@ lockd(struct svc_rqst *rqstp)
         * Let our maker know we're running.
         */
        nlmsvc_pid = current->pid;
-       up(&lockd_start);
+       nlmsvc_serv = rqstp->rq_server;
+       complete(&lockd_start_done);
 
        daemonize("lockd");
+       set_freezable();
 
        /* Process request with signals blocked, but allow SIGKILL.  */
        allow_signal(SIGKILL);
 
-       /* kick rpciod */
-       rpciod_up();
-
        dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
 
        if (!nlm_timeout)
@@ -137,6 +153,7 @@ lockd(struct svc_rqst *rqstp)
         */
        while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
                long timeout = MAX_SCHEDULE_TIMEOUT;
+               char buf[RPC_MAX_ADDRBUFLEN];
 
                if (signalled()) {
                        flush_signals(current);
@@ -161,7 +178,7 @@ lockd(struct svc_rqst *rqstp)
                 * Find a socket with data available and call its
                 * recvfrom routine.
                 */
-               err = svc_recv(serv, rqstp, timeout);
+               err = svc_recv(rqstp, timeout);
                if (err == -EAGAIN || err == -EINTR)
                        continue;
                if (err < 0) {
@@ -171,11 +188,10 @@ lockd(struct svc_rqst *rqstp)
                        break;
                }
 
-               dprintk("lockd: request from %08x\n",
-                       (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
-
-               svc_process(serv, rqstp);
+               dprintk("lockd: request from %s\n",
+                               svc_print_addr(rqstp, buf, sizeof(buf)));
 
+               svc_process(rqstp);
        }
 
        flush_signals(current);
@@ -189,6 +205,7 @@ lockd(struct svc_rqst *rqstp)
                        nlmsvc_invalidate_all();
                nlm_shutdown_hosts();
                nlmsvc_pid = 0;
+               nlmsvc_serv = NULL;
        } else
                printk(KERN_DEBUG
                        "lockd: new process, skipping host shutdown\n");
@@ -197,62 +214,88 @@ lockd(struct svc_rqst *rqstp)
        /* Exit the RPC thread */
        svc_exit_thread(rqstp);
 
-       /* release rpciod */
-       rpciod_down();
-
        /* Release module */
        unlock_kernel();
        module_put_and_exit(0);
 }
 
+
+static int find_socket(struct svc_serv *serv, int proto)
+{
+       struct svc_sock *svsk;
+       int found = 0;
+       list_for_each_entry(svsk, &serv->sv_permsocks, sk_list)
+               if (svsk->sk_sk->sk_protocol == proto) {
+                       found = 1;
+                       break;
+               }
+       return found;
+}
+
+/*
+ * Make any sockets that are needed but not present.
+ * If nlm_udpport or nlm_tcpport were set as module
+ * options, make those sockets unconditionally
+ */
+static int make_socks(struct svc_serv *serv, int proto)
+{
+       static int warned;
+       int err = 0;
+
+       if (proto == IPPROTO_UDP || nlm_udpport)
+               if (!find_socket(serv, IPPROTO_UDP))
+                       err = svc_makesock(serv, IPPROTO_UDP, nlm_udpport,
+                                               SVC_SOCK_DEFAULTS);
+       if (err >= 0 && (proto == IPPROTO_TCP || nlm_tcpport))
+               if (!find_socket(serv, IPPROTO_TCP))
+                       err = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport,
+                                               SVC_SOCK_DEFAULTS);
+
+       if (err >= 0) {
+               warned = 0;
+               err = 0;
+       } else if (warned++ == 0)
+               printk(KERN_WARNING
+                      "lockd_up: makesock failed, error=%d\n", err);
+       return err;
+}
+
 /*
  * Bring up the lockd process if it's not already up.
  */
 int
-lockd_up(void)
+lockd_up(int proto) /* Maybe add a 'family' option when IPv6 is supported ?? */
 {
-       static int              warned;
        struct svc_serv *       serv;
        int                     error = 0;
 
-       down(&nlmsvc_sema);
-       /*
-        * Unconditionally increment the user count ... this is
-        * the number of clients who _want_ a lockd process.
-        */
-       nlmsvc_users++; 
+       mutex_lock(&nlmsvc_mutex);
        /*
         * Check whether we're already up and running.
         */
-       if (nlmsvc_pid)
+       if (nlmsvc_pid) {
+               if (proto)
+                       error = make_socks(nlmsvc_serv, proto);
                goto out;
+       }
 
        /*
         * Sanity check: if there's no pid,
         * we should be the first user ...
         */
-       if (nlmsvc_users > 1)
+       if (nlmsvc_users)
                printk(KERN_WARNING
                        "lockd_up: no pid, %d users??\n", nlmsvc_users);
 
        error = -ENOMEM;
-       serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE);
+       serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, NULL);
        if (!serv) {
                printk(KERN_WARNING "lockd_up: create service failed\n");
                goto out;
        }
 
-       if ((error = svc_makesock(serv, IPPROTO_UDP, nlm_udpport)) < 0 
-#ifdef CONFIG_NFSD_TCP
-        || (error = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport)) < 0
-#endif
-               ) {
-               if (warned++ == 0) 
-                       printk(KERN_WARNING
-                               "lockd_up: makesock failed, error=%d\n", error);
+       if ((error = make_socks(serv, proto)) < 0)
                goto destroy_and_out;
-       } 
-       warned = 0;
 
        /*
         * Create the kernel thread and wait for it to start.
@@ -263,7 +306,7 @@ lockd_up(void)
                        "lockd_up: create thread failed, error=%d\n", error);
                goto destroy_and_out;
        }
-       down(&lockd_start);
+       wait_for_completion(&lockd_start_done);
 
        /*
         * Note: svc_serv structures have an initial use count of 1,
@@ -272,7 +315,9 @@ lockd_up(void)
 destroy_and_out:
        svc_destroy(serv);
 out:
-       up(&nlmsvc_sema);
+       if (!error)
+               nlmsvc_users++;
+       mutex_unlock(&nlmsvc_mutex);
        return error;
 }
 EXPORT_SYMBOL(lockd_up);
@@ -285,7 +330,7 @@ lockd_down(void)
 {
        static int warned;
 
-       down(&nlmsvc_sema);
+       mutex_lock(&nlmsvc_mutex);
        if (nlmsvc_users) {
                if (--nlmsvc_users)
                        goto out;
@@ -315,7 +360,7 @@ lockd_down(void)
        recalc_sigpending();
        spin_unlock_irq(&current->sighand->siglock);
 out:
-       up(&nlmsvc_sema);
+       mutex_unlock(&nlmsvc_mutex);
 }
 EXPORT_SYMBOL(lockd_down);
 
@@ -323,9 +368,6 @@ EXPORT_SYMBOL(lockd_down);
  * Sysctl parameters (same as module parameters, different interface).
  */
 
-/* Something that isn't CTL_ANY, CTL_NONE or a value that may clash. */
-#define CTL_UNNUMBERED         -2
-
 static ctl_table nlm_sysctls[] = {
        {
                .ctl_name       = CTL_UNNUMBERED,
@@ -367,6 +409,22 @@ static ctl_table nlm_sysctls[] = {
                .extra1         = (int *) &nlm_port_min,
                .extra2         = (int *) &nlm_port_max,
        },
+       {
+               .ctl_name       = CTL_UNNUMBERED,
+               .procname       = "nsm_use_hostnames",
+               .data           = &nsm_use_hostnames,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec,
+       },
+       {
+               .ctl_name       = CTL_UNNUMBERED,
+               .procname       = "nsm_local_state",
+               .data           = &nsm_local_state,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = &proc_dointvec,
+       },
        { .ctl_name = 0 }
 };
 
@@ -391,7 +449,7 @@ static ctl_table nlm_sysctl_root[] = {
 };
 
 /*
- * Module (and driverfs) parameters.
+ * Module (and sysfs) parameters.
  */
 
 #define param_set_min_max(name, type, which_strtol, min, max)          \
@@ -455,6 +513,7 @@ module_param_call(nlm_udpport, param_set_port, param_get_int,
                  &nlm_udpport, 0644);
 module_param_call(nlm_tcpport, param_set_port, param_get_int,
                  &nlm_tcpport, 0644);
+module_param(nsm_use_hostnames, bool, 0644);
 
 /*
  * Initialising and terminating the module.
@@ -462,7 +521,7 @@ module_param_call(nlm_tcpport, param_set_port, param_get_int,
 
 static int __init init_nlm(void)
 {
-       nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root, 0);
+       nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
        return nlm_sysctl_table ? 0 : -ENOMEM;
 }