Pull video into test branch
[pandora-kernel.git] / fs / lockd / host.c
index 0bf4afb..3d4610c 100644 (file)
@@ -27,7 +27,7 @@
 #define NLM_HOST_EXPIRE                ((nrhosts > NLM_HOST_MAX)? 300 * HZ : 120 * HZ)
 #define NLM_HOST_COLLECT       ((nrhosts > NLM_HOST_MAX)? 120 * HZ :  60 * HZ)
 
-static struct nlm_host *       nlm_hosts[NLM_HOST_NRHASH];
+static struct hlist_head       nlm_hosts[NLM_HOST_NRHASH];
 static unsigned long           next_gc;
 static int                     nrhosts;
 static DEFINE_MUTEX(nlm_host_mutex);
@@ -36,40 +36,22 @@ static DEFINE_MUTEX(nlm_host_mutex);
 static void                    nlm_gc_hosts(void);
 static struct nsm_handle *     __nsm_find(const struct sockaddr_in *,
                                        const char *, int, int);
-
-/*
- * Find an NLM server handle in the cache. If there is none, create it.
- */
-struct nlm_host *
-nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
-                       const char *hostname, int hostname_len)
-{
-       return nlm_lookup_host(0, sin, proto, version,
-                              hostname, hostname_len);
-}
-
-/*
- * Find an NLM client handle in the cache. If there is none, create it.
- */
-struct nlm_host *
-nlmsvc_lookup_host(struct svc_rqst *rqstp,
-                       const char *hostname, int hostname_len)
-{
-       return nlm_lookup_host(1, &rqstp->rq_addr,
-                              rqstp->rq_prot, rqstp->rq_vers,
-                              hostname, hostname_len);
-}
+static struct nsm_handle *     nsm_find(const struct sockaddr_in *sin,
+                                        const char *hostname,
+                                        int hostname_len);
 
 /*
  * Common host lookup routine for server & client
  */
-struct nlm_host *
+static struct nlm_host *
 nlm_lookup_host(int server, const struct sockaddr_in *sin,
                                        int proto, int version,
                                        const char *hostname,
                                        int hostname_len)
 {
-       struct nlm_host *host, **hp;
+       struct hlist_head *chain;
+       struct hlist_node *pos;
+       struct nlm_host *host;
        struct nsm_handle *nsm = NULL;
        int             hash;
 
@@ -95,13 +77,14 @@ nlm_lookup_host(int server, const struct sockaddr_in *sin,
         * different NLM rpc_clients into one single nlm_host object.
         * This would allow us to have one nlm_host per address.
         */
-       for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
+       chain = &nlm_hosts[hash];
+       hlist_for_each_entry(host, pos, chain, h_hash) {
                if (!nlm_cmp_addr(&host->h_addr, sin))
                        continue;
 
                /* See if we have an NSM handle for this client */
-               if (!nsm && (nsm = host->h_nsmhandle) != 0)
-                       atomic_inc(&nsm->sm_count);
+               if (!nsm)
+                       nsm = host->h_nsmhandle;
 
                if (host->h_proto != proto)
                        continue;
@@ -110,17 +93,17 @@ nlm_lookup_host(int server, const struct sockaddr_in *sin,
                if (host->h_server != server)
                        continue;
 
-               {
-                       if (hp != nlm_hosts + hash) {
-                               *hp = host->h_next;
-                               host->h_next = nlm_hosts[hash];
-                               nlm_hosts[hash] = host;
-                       }
-                       nlm_get_host(host);
-                       mutex_unlock(&nlm_host_mutex);
-                       return host;
-               }
+               /* Move to head of hash chain. */
+               hlist_del(&host->h_hash);
+               hlist_add_head(&host->h_hash, chain);
+
+               nlm_get_host(host);
+               goto out;
        }
+       if (nsm)
+               atomic_inc(&nsm->sm_count);
+
+       host = NULL;
 
        /* Sadly, the host isn't in our hash table yet. See if
         * we have an NSM handle for it. If not, create one.
@@ -149,8 +132,7 @@ nlm_lookup_host(int server, const struct sockaddr_in *sin,
        host->h_nsmstate   = 0;                 /* real NSM state */
        host->h_nsmhandle  = nsm;
        host->h_server     = server;
-       host->h_next       = nlm_hosts[hash];
-       nlm_hosts[hash]    = host;
+       hlist_add_head(&host->h_hash, chain);
        INIT_LIST_HEAD(&host->h_lockowners);
        spin_lock_init(&host->h_lock);
        INIT_LIST_HEAD(&host->h_granted);
@@ -164,30 +146,57 @@ out:
        return host;
 }
 
-struct nlm_host *
-nlm_find_client(void)
+/*
+ * Destroy a host
+ */
+static void
+nlm_destroy_host(struct nlm_host *host)
 {
-       /* find a nlm_host for a client for which h_killed == 0.
-        * and return it
+       struct rpc_clnt *clnt;
+
+       BUG_ON(!list_empty(&host->h_lockowners));
+       BUG_ON(atomic_read(&host->h_count));
+
+       /*
+        * Release NSM handle and unmonitor host.
         */
-       int hash;
-       mutex_lock(&nlm_host_mutex);
-       for (hash = 0 ; hash < NLM_HOST_NRHASH; hash++) {
-               struct nlm_host *host, **hp;
-               for (hp = &nlm_hosts[hash]; (host = *hp) != 0; hp = &host->h_next) {
-                       if (host->h_server &&
-                           host->h_killed == 0) {
-                               nlm_get_host(host);
-                               mutex_unlock(&nlm_host_mutex);
-                               return host;
-                       }
+       nsm_unmonitor(host);
+
+       if ((clnt = host->h_rpcclnt) != NULL) {
+               if (atomic_read(&clnt->cl_users)) {
+                       printk(KERN_WARNING
+                               "lockd: active RPC handle\n");
+                       clnt->cl_dead = 1;
+               } else {
+                       rpc_destroy_client(host->h_rpcclnt);
                }
        }
-       mutex_unlock(&nlm_host_mutex);
-       return NULL;
+       kfree(host);
+}
+
+/*
+ * Find an NLM server handle in the cache. If there is none, create it.
+ */
+struct nlm_host *
+nlmclnt_lookup_host(const struct sockaddr_in *sin, int proto, int version,
+                       const char *hostname, int hostname_len)
+{
+       return nlm_lookup_host(0, sin, proto, version,
+                              hostname, hostname_len);
+}
+
+/*
+ * Find an NLM client handle in the cache. If there is none, create it.
+ */
+struct nlm_host *
+nlmsvc_lookup_host(struct svc_rqst *rqstp,
+                       const char *hostname, int hostname_len)
+{
+       return nlm_lookup_host(1, &rqstp->rq_addr,
+                              rqstp->rq_prot, rqstp->rq_vers,
+                              hostname, hostname_len);
 }
 
-                               
 /*
  * Create the NLM RPC client for an NLM peer
  */
@@ -293,28 +302,58 @@ void nlm_release_host(struct nlm_host *host)
  * has rebooted.
  * Release all resources held by that peer.
  */
-void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *argp)
+void nlm_host_rebooted(const struct sockaddr_in *sin,
+                               const char *hostname, int hostname_len,
+                               u32 new_state)
 {
-       struct nlm_host *host;
-       int server;
+       struct hlist_head *chain;
+       struct hlist_node *pos;
+       struct nsm_handle *nsm;
+       struct nlm_host *host;
 
-       /* Obtain the host pointer for this NFS server and try to
-        * reclaim all locks we hold on this server.
-        */
-       server = (argp->proto & 1)? 1 : 0;
-       host = nlm_lookup_host(server, sin, argp->proto >> 1, argp->vers,
-                       argp->mon, argp->len);
-       if (host == NULL)
+       dprintk("lockd: nlm_host_rebooted(%s, %u.%u.%u.%u)\n",
+                       hostname, NIPQUAD(sin->sin_addr));
+
+       /* Find the NSM handle for this peer */
+       if (!(nsm = __nsm_find(sin, hostname, hostname_len, 0)))
                return;
 
-       if (server == 0) {
-               /* We are client, he's the server: try to reclaim all locks. */
-               nlmclnt_recovery(host, argp->state);
-       } else {
-               /* He's the client, we're the server: delete all locks held by the client */
-               nlmsvc_free_host_resources(host);
+       /* When reclaiming locks on this peer, make sure that
+        * we set up a new notification */
+       nsm->sm_monitored = 0;
+
+       /* Mark all hosts tied to this NSM state as having rebooted.
+        * We run the loop repeatedly, because we drop the host table
+        * lock for this.
+        * To avoid processing a host several times, we match the nsmstate.
+        */
+again: mutex_lock(&nlm_host_mutex);
+       for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
+               hlist_for_each_entry(host, pos, chain, h_hash) {
+                       if (host->h_nsmhandle == nsm
+                        && host->h_nsmstate != new_state) {
+                               host->h_nsmstate = new_state;
+                               host->h_state++;
+
+                               nlm_get_host(host);
+                               mutex_unlock(&nlm_host_mutex);
+
+                               if (host->h_server) {
+                                       /* We're server for this guy, just ditch
+                                        * all the locks he held. */
+                                       nlmsvc_free_host_resources(host);
+                               } else {
+                                       /* He's the server, initiate lock recovery. */
+                                       nlmclnt_recovery(host);
+                               }
+
+                               nlm_release_host(host);
+                               goto again;
+                       }
+               }
        }
-       nlm_release_host(host);
+
+       mutex_unlock(&nlm_host_mutex);
 }
 
 /*
@@ -324,16 +363,17 @@ void nlm_host_rebooted(const struct sockaddr_in *sin, const struct nlm_reboot *a
 void
 nlm_shutdown_hosts(void)
 {
+       struct hlist_head *chain;
+       struct hlist_node *pos;
        struct nlm_host *host;
-       int             i;
 
        dprintk("lockd: shutting down host module\n");
        mutex_lock(&nlm_host_mutex);
 
        /* First, make all hosts eligible for gc */
        dprintk("lockd: nuking all hosts...\n");
-       for (i = 0; i < NLM_HOST_NRHASH; i++) {
-               for (host = nlm_hosts[i]; host; host = host->h_next)
+       for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
+               hlist_for_each_entry(host, pos, chain, h_hash)
                        host->h_expires = jiffies - 1;
        }
 
@@ -345,8 +385,8 @@ nlm_shutdown_hosts(void)
        if (nrhosts) {
                printk(KERN_WARNING "lockd: couldn't shutdown host module!\n");
                dprintk("lockd: %d hosts left:\n", nrhosts);
-               for (i = 0; i < NLM_HOST_NRHASH; i++) {
-                       for (host = nlm_hosts[i]; host; host = host->h_next) {
+               for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
+                       hlist_for_each_entry(host, pos, chain, h_hash) {
                                dprintk("       %s (cnt %d use %d exp %ld)\n",
                                        host->h_name, atomic_read(&host->h_count),
                                        host->h_inuse, host->h_expires);
@@ -363,48 +403,32 @@ nlm_shutdown_hosts(void)
 static void
 nlm_gc_hosts(void)
 {
-       struct nlm_host **q, *host;
-       struct rpc_clnt *clnt;
-       int             i;
+       struct hlist_head *chain;
+       struct hlist_node *pos, *next;
+       struct nlm_host *host;
 
        dprintk("lockd: host garbage collection\n");
-       for (i = 0; i < NLM_HOST_NRHASH; i++) {
-               for (host = nlm_hosts[i]; host; host = host->h_next)
+       for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
+               hlist_for_each_entry(host, pos, chain, h_hash)
                        host->h_inuse = 0;
        }
 
        /* Mark all hosts that hold locks, blocks or shares */
        nlmsvc_mark_resources();
 
-       for (i = 0; i < NLM_HOST_NRHASH; i++) {
-               q = &nlm_hosts[i];
-               while ((host = *q) != NULL) {
+       for (chain = nlm_hosts; chain < nlm_hosts + NLM_HOST_NRHASH; ++chain) {
+               hlist_for_each_entry_safe(host, pos, next, chain, h_hash) {
                        if (atomic_read(&host->h_count) || host->h_inuse
                         || time_before(jiffies, host->h_expires)) {
                                dprintk("nlm_gc_hosts skipping %s (cnt %d use %d exp %ld)\n",
                                        host->h_name, atomic_read(&host->h_count),
                                        host->h_inuse, host->h_expires);
-                               q = &host->h_next;
                                continue;
                        }
                        dprintk("lockd: delete host %s\n", host->h_name);
-                       *q = host->h_next;
-
-                       /*
-                        * Unmonitor unless host was invalidated (i.e. lockd restarted)
-                        */
-                       nsm_unmonitor(host);
-
-                       if ((clnt = host->h_rpcclnt) != NULL) {
-                               if (atomic_read(&clnt->cl_users)) {
-                                       printk(KERN_WARNING
-                                               "lockd: active RPC handle\n");
-                                       clnt->cl_dead = 1;
-                               } else {
-                                       rpc_destroy_client(host->h_rpcclnt);
-                               }
-                       }
-                       kfree(host);
+                       hlist_del_init(&host->h_hash);
+
+                       nlm_destroy_host(host);
                        nrhosts--;
                }
        }
@@ -417,7 +441,7 @@ nlm_gc_hosts(void)
  * Manage NSM handles
  */
 static LIST_HEAD(nsm_handles);
-static DECLARE_MUTEX(nsm_sema);
+static DEFINE_MUTEX(nsm_mutex);
 
 static struct nsm_handle *
 __nsm_find(const struct sockaddr_in *sin,
@@ -439,11 +463,15 @@ __nsm_find(const struct sockaddr_in *sin,
                return NULL;
        }
 
-       down(&nsm_sema);
+       mutex_lock(&nsm_mutex);
        list_for_each(pos, &nsm_handles) {
                nsm = list_entry(pos, struct nsm_handle, sm_link);
 
-               if (!nlm_cmp_addr(&nsm->sm_addr, sin))
+               if (hostname && nsm_use_hostnames) {
+                       if (strlen(nsm->sm_name) != hostname_len
+                        || memcmp(nsm->sm_name, hostname, hostname_len))
+                               continue;
+               } else if (!nlm_cmp_addr(&nsm->sm_addr, sin))
                        continue;
                atomic_inc(&nsm->sm_count);
                goto out;
@@ -465,11 +493,12 @@ __nsm_find(const struct sockaddr_in *sin,
                list_add(&nsm->sm_link, &nsm_handles);
        }
 
-out:   up(&nsm_sema);
+out:
+       mutex_unlock(&nsm_mutex);
        return nsm;
 }
 
-struct nsm_handle *
+static struct nsm_handle *
 nsm_find(const struct sockaddr_in *sin, const char *hostname, int hostname_len)
 {
        return __nsm_find(sin, hostname, hostname_len, 1);
@@ -484,11 +513,11 @@ nsm_release(struct nsm_handle *nsm)
        if (!nsm)
                return;
        if (atomic_dec_and_test(&nsm->sm_count)) {
-               down(&nsm_sema);
+               mutex_lock(&nsm_mutex);
                if (atomic_read(&nsm->sm_count) == 0) {
                        list_del(&nsm->sm_link);
                        kfree(nsm);
                }
-               up(&nsm_sema);
+               mutex_unlock(&nsm_mutex);
        }
 }