NSM: More clean up of nsm_get_handle()
[safe/jmp/linux-2.6] / fs / lockd / mon.c
index 3825545..99aec74 100644 (file)
@@ -31,7 +31,7 @@ enum {
 };
 
 struct nsm_args {
-       __be32                  addr;           /* remote address */
+       struct nsm_private      *priv;
        u32                     prog;           /* RPC callback info */
        u32                     vers;
        u32                     proc;
@@ -101,7 +101,7 @@ nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
        struct rpc_clnt *clnt;
        int             status;
        struct nsm_args args = {
-               .addr           = nsm_addr_in(nsm)->sin_addr.s_addr,
+               .priv           = &nsm->sm_priv,
                .prog           = NLM_PROGRAM,
                .vers           = 3,
                .proc           = NLMPROC_NSM_NOTIFY,
@@ -201,31 +201,97 @@ void nsm_unmonitor(const struct nlm_host *host)
        }
 }
 
+static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
+                                             const size_t len)
+{
+       struct nsm_handle *nsm;
+
+       list_for_each_entry(nsm, &nsm_handles, sm_link)
+               if (strlen(nsm->sm_name) == len &&
+                   memcmp(nsm->sm_name, hostname, len) == 0)
+                       return nsm;
+       return NULL;
+}
+
+static struct nsm_handle *nsm_lookup_addr(const struct sockaddr *sap)
+{
+       struct nsm_handle *nsm;
+
+       list_for_each_entry(nsm, &nsm_handles, sm_link)
+               if (nlm_cmp_addr(nsm_addr(nsm), sap))
+                       return nsm;
+       return NULL;
+}
+
+static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
+{
+       struct nsm_handle *nsm;
+
+       list_for_each_entry(nsm, &nsm_handles, sm_link)
+               if (memcmp(nsm->sm_priv.data, priv->data,
+                                       sizeof(priv->data)) == 0)
+                       return nsm;
+       return NULL;
+}
+
+/*
+ * Construct a unique cookie to match this nsm_handle to this monitored
+ * host.  It is passed to the local rpc.statd via NSMPROC_MON, and
+ * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
+ * requests.
+ *
+ * Linux provides the raw IP address of the monitored host,
+ * left in network byte order.
+ */
+static void nsm_init_private(struct nsm_handle *nsm)
+{
+       __be32 *p = (__be32 *)&nsm->sm_priv.data;
+       *p = nsm_addr_in(nsm)->sin_addr.s_addr;
+}
+
+static struct nsm_handle *nsm_create_handle(const struct sockaddr *sap,
+                                           const size_t salen,
+                                           const char *hostname,
+                                           const size_t hostname_len)
+{
+       struct nsm_handle *new;
+
+       new = kzalloc(sizeof(*new) + hostname_len + 1, GFP_KERNEL);
+       if (unlikely(new == NULL))
+               return NULL;
+
+       atomic_set(&new->sm_count, 1);
+       new->sm_name = (char *)(new + 1);
+       memcpy(nsm_addr(new), sap, salen);
+       new->sm_addrlen = salen;
+       nsm_init_private(new);
+       nsm_display_address((const struct sockaddr *)&new->sm_addr,
+                               new->sm_addrbuf, sizeof(new->sm_addrbuf));
+       memcpy(new->sm_name, hostname, hostname_len);
+       new->sm_name[hostname_len] = '\0';
+
+       return new;
+}
+
 /**
- * nsm_find - Find or create a cached nsm_handle
+ * nsm_get_handle - Find or create a cached nsm_handle
  * @sap: pointer to socket address of handle to find
  * @salen: length of socket address
  * @hostname: pointer to C string containing hostname to find
  * @hostname_len: length of C string
- * @create: one means create new handle if not found in cache
  *
- * Behavior is modulated by the global nsm_use_hostnames variable
- * and by the @create argument.
+ * Behavior is modulated by the global nsm_use_hostnames variable.
  *
- * Returns a cached nsm_handle after bumping its ref count, or if
- * @create is set, returns a fresh nsm_handle if a handle that
- * matches @sap and/or @hostname cannot be found in the handle cache.
- * Returns NULL if an error occurs.
+ * Returns a cached nsm_handle after bumping its ref count, or
+ * returns a fresh nsm_handle if a handle that matches @sap and/or
+ * @hostname cannot be found in the handle cache.  Returns NULL if
+ * an error occurs.
  */
-struct nsm_handle *nsm_find(const struct sockaddr *sap, const size_t salen,
-                           const char *hostname, const size_t hostname_len,
-                           const int create)
+struct nsm_handle *nsm_get_handle(const struct sockaddr *sap,
+                                 const size_t salen, const char *hostname,
+                                 const size_t hostname_len)
 {
-       struct nsm_handle *nsm = NULL;
-       struct nsm_handle *pos;
-
-       if (!sap)
-               return NULL;
+       struct nsm_handle *cached, *new = NULL;
 
        if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
                if (printk_ratelimit()) {
@@ -238,50 +304,78 @@ struct nsm_handle *nsm_find(const struct sockaddr *sap, const size_t salen,
 
 retry:
        spin_lock(&nsm_lock);
-       list_for_each_entry(pos, &nsm_handles, sm_link) {
-
-               if (hostname && nsm_use_hostnames) {
-                       if (strlen(pos->sm_name) != hostname_len
-                        || memcmp(pos->sm_name, hostname, hostname_len))
-                               continue;
-               } else if (!nlm_cmp_addr(nsm_addr(pos), sap))
-                       continue;
-               atomic_inc(&pos->sm_count);
-               kfree(nsm);
-               nsm = pos;
-               dprintk("lockd: found nsm_handle for %s (%s), cnt %d\n",
-                               pos->sm_name, pos->sm_addrbuf,
-                               atomic_read(&pos->sm_count));
-               goto found;
+
+       if (nsm_use_hostnames && hostname != NULL)
+               cached = nsm_lookup_hostname(hostname, hostname_len);
+       else
+               cached = nsm_lookup_addr(sap);
+
+       if (cached != NULL) {
+               atomic_inc(&cached->sm_count);
+               spin_unlock(&nsm_lock);
+               kfree(new);
+               dprintk("lockd: found nsm_handle for %s (%s), "
+                               "cnt %d\n", cached->sm_name,
+                               cached->sm_addrbuf,
+                               atomic_read(&cached->sm_count));
+               return cached;
        }
-       if (nsm) {
-               list_add(&nsm->sm_link, &nsm_handles);
+
+       if (new != NULL) {
+               list_add(&new->sm_link, &nsm_handles);
+               spin_unlock(&nsm_lock);
                dprintk("lockd: created nsm_handle for %s (%s)\n",
-                               nsm->sm_name, nsm->sm_addrbuf);
-               goto found;
+                               new->sm_name, new->sm_addrbuf);
+               return new;
        }
+
        spin_unlock(&nsm_lock);
 
-       if (!create)
+       new = nsm_create_handle(sap, salen, hostname, hostname_len);
+       if (unlikely(new == NULL))
                return NULL;
+       goto retry;
+}
 
-       nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
-       if (nsm == NULL)
-               return NULL;
+/**
+ * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
+ * @info: pointer to NLMPROC_SM_NOTIFY arguments
+ *
+ * Returns a matching nsm_handle if found in the nsm cache; the returned
+ * nsm_handle's reference count is bumped and sm_monitored is cleared.
+ * Otherwise returns NULL if some error occurred.
+ */
+struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
+{
+       struct nsm_handle *cached;
 
-       memcpy(nsm_addr(nsm), sap, salen);
-       nsm->sm_addrlen = salen;
-       nsm->sm_name = (char *) (nsm + 1);
-       memcpy(nsm->sm_name, hostname, hostname_len);
-       nsm->sm_name[hostname_len] = '\0';
-       nsm_display_address((struct sockaddr *)&nsm->sm_addr,
-                               nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
-       atomic_set(&nsm->sm_count, 1);
-       goto retry;
+       spin_lock(&nsm_lock);
+
+       if (nsm_use_hostnames && info->mon != NULL)
+               cached = nsm_lookup_hostname(info->mon, info->len);
+       else
+               cached = nsm_lookup_priv(&info->priv);
+
+       if (unlikely(cached == NULL)) {
+               spin_unlock(&nsm_lock);
+               dprintk("lockd: never saw rebooted peer '%.*s' before\n",
+                               info->len, info->mon);
+               return cached;
+       }
 
-found:
+       atomic_inc(&cached->sm_count);
        spin_unlock(&nsm_lock);
-       return nsm;
+
+       /*
+        * During subsequent lock activity, force a fresh
+        * notification to be set up for this host.
+        */
+       cached->sm_monitored = 0;
+
+       dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
+                       cached->sm_name, cached->sm_addrbuf,
+                       atomic_read(&cached->sm_count));
+       return cached;
 }
 
 /**
@@ -291,8 +385,6 @@ found:
  */
 void nsm_release(struct nsm_handle *nsm)
 {
-       if (!nsm)
-               return;
        if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
                list_del(&nsm->sm_link);
                spin_unlock(&nsm_lock);
@@ -396,9 +488,6 @@ static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
  * The "priv" argument may contain private information required
  * by the NSMPROC_MON call. This information will be supplied in the
  * NLMPROC_SM_NOTIFY call.
- *
- * Linux provides the raw IP address of the monitored host,
- * left in network byte order.
  */
 static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
 {
@@ -407,10 +496,7 @@ static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
        p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
        if (unlikely(p == NULL))
                return -EIO;
-       *p++ = argp->addr;
-       *p++ = 0;
-       *p++ = 0;
-       *p++ = 0;
+       xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
        return 0;
 }