Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / net / tipc / node.c
index 598f4d3..20d98c5 100644 (file)
 #include "bearer.h"
 #include "name_distr.h"
 
-void node_print(struct print_buf *buf, struct node *n_ptr, char *str);
-static void node_lost_contact(struct node *n_ptr);
-static void node_established_contact(struct node *n_ptr);
+void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str);
+static void node_lost_contact(struct tipc_node *n_ptr);
+static void node_established_contact(struct tipc_node *n_ptr);
 
-struct node *tipc_nodes = NULL;        /* sorted list of nodes within cluster */
+struct tipc_node *tipc_nodes = NULL;   /* sorted list of nodes within cluster */
+
+static DEFINE_SPINLOCK(node_create_lock);
 
 u32 tipc_own_tag = 0;
 
-struct node *tipc_node_create(u32 addr)
+/**
+ * tipc_node_create - create neighboring node
+ *
+ * Currently, this routine is called by neighbor discovery code, which holds
+ * net_lock for reading only.  We must take node_create_lock to ensure a node
+ * isn't created twice if two different bearers discover the node at the same
+ * time.  (It would be preferable to switch to holding net_lock in write mode,
+ * but this is a non-trivial change.)
+ */
+
+struct tipc_node *tipc_node_create(u32 addr)
 {
        struct cluster *c_ptr;
-       struct node *n_ptr;
-       struct node **curr_node;
+       struct tipc_node *n_ptr;
+       struct tipc_node **curr_node;
+
+       spin_lock_bh(&node_create_lock);
+
+       for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
+               if (addr < n_ptr->addr)
+                       break;
+               if (addr == n_ptr->addr) {
+                       spin_unlock_bh(&node_create_lock);
+                       return n_ptr;
+               }
+       }
 
        n_ptr = kzalloc(sizeof(*n_ptr),GFP_ATOMIC);
        if (!n_ptr) {
+               spin_unlock_bh(&node_create_lock);
                warn("Node creation failed, no memory\n");
                return NULL;
        }
@@ -71,6 +95,7 @@ struct node *tipc_node_create(u32 addr)
                c_ptr = tipc_cltr_create(addr);
        }
        if (!c_ptr) {
+               spin_unlock_bh(&node_create_lock);
                kfree(n_ptr);
                return NULL;
        }
@@ -91,10 +116,11 @@ struct node *tipc_node_create(u32 addr)
                }
        }
        (*curr_node) = n_ptr;
+       spin_unlock_bh(&node_create_lock);
        return n_ptr;
 }
 
-void tipc_node_delete(struct node *n_ptr)
+void tipc_node_delete(struct tipc_node *n_ptr)
 {
        if (!n_ptr)
                return;
@@ -120,7 +146,7 @@ void tipc_node_delete(struct node *n_ptr)
  * Link becomes active (alone or shared) or standby, depending on its priority.
  */
 
-void tipc_node_link_up(struct node *n_ptr, struct link *l_ptr)
+void tipc_node_link_up(struct tipc_node *n_ptr, struct link *l_ptr)
 {
        struct link **active = &n_ptr->active_links[0];
 
@@ -154,7 +180,7 @@ void tipc_node_link_up(struct node *n_ptr, struct link *l_ptr)
  * node_select_active_links - select active link
  */
 
-static void node_select_active_links(struct node *n_ptr)
+static void node_select_active_links(struct tipc_node *n_ptr)
 {
        struct link **active = &n_ptr->active_links[0];
        u32 i;
@@ -182,7 +208,7 @@ static void node_select_active_links(struct node *n_ptr)
  * tipc_node_link_down - handle loss of link
  */
 
-void tipc_node_link_down(struct node *n_ptr, struct link *l_ptr)
+void tipc_node_link_down(struct tipc_node *n_ptr, struct link *l_ptr)
 {
        struct link **active;
 
@@ -209,30 +235,30 @@ void tipc_node_link_down(struct node *n_ptr, struct link *l_ptr)
                node_lost_contact(n_ptr);
 }
 
-int tipc_node_has_active_links(struct node *n_ptr)
+int tipc_node_has_active_links(struct tipc_node *n_ptr)
 {
        return (n_ptr &&
                ((n_ptr->active_links[0]) || (n_ptr->active_links[1])));
 }
 
-int tipc_node_has_redundant_links(struct node *n_ptr)
+int tipc_node_has_redundant_links(struct tipc_node *n_ptr)
 {
        return (n_ptr->working_links > 1);
 }
 
-static int tipc_node_has_active_routes(struct node *n_ptr)
+static int tipc_node_has_active_routes(struct tipc_node *n_ptr)
 {
        return (n_ptr && (n_ptr->last_router >= 0));
 }
 
-int tipc_node_is_up(struct node *n_ptr)
+int tipc_node_is_up(struct tipc_node *n_ptr)
 {
        return (tipc_node_has_active_links(n_ptr) || tipc_node_has_active_routes(n_ptr));
 }
 
-struct node *tipc_node_attach_link(struct link *l_ptr)
+struct tipc_node *tipc_node_attach_link(struct link *l_ptr)
 {
-       struct node *n_ptr = tipc_node_find(l_ptr->addr);
+       struct tipc_node *n_ptr = tipc_node_find(l_ptr->addr);
 
        if (!n_ptr)
                n_ptr = tipc_node_create(l_ptr->addr);
@@ -259,7 +285,7 @@ struct node *tipc_node_attach_link(struct link *l_ptr)
        return NULL;
 }
 
-void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
+void tipc_node_detach_link(struct tipc_node *n_ptr, struct link *l_ptr)
 {
        n_ptr->links[l_ptr->b_ptr->identity] = NULL;
        tipc_net.zones[tipc_zone(l_ptr->addr)]->links--;
@@ -312,7 +338,7 @@ void tipc_node_detach_link(struct node *n_ptr, struct link *l_ptr)
  *
  */
 
-static void node_established_contact(struct node *n_ptr)
+static void node_established_contact(struct tipc_node *n_ptr)
 {
        struct cluster *c_ptr;
 
@@ -358,10 +384,10 @@ static void node_established_contact(struct node *n_ptr)
                                  tipc_highest_allowed_slave);
 }
 
-static void node_lost_contact(struct node *n_ptr)
+static void node_lost_contact(struct tipc_node *n_ptr)
 {
        struct cluster *c_ptr;
-       struct node_subscr *ns, *tns;
+       struct tipc_node_subscr *ns, *tns;
        char addr_string[16];
        u32 i;
 
@@ -440,9 +466,9 @@ static void node_lost_contact(struct node *n_ptr)
  * Called by when cluster local lookup has failed.
  */
 
-struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
+struct tipc_node *tipc_node_select_next_hop(u32 addr, u32 selector)
 {
-       struct node *n_ptr;
+       struct tipc_node *n_ptr;
        u32 router_addr;
 
        if (!tipc_addr_domain_valid(addr))
@@ -487,7 +513,7 @@ struct node *tipc_node_select_next_hop(u32 addr, u32 selector)
  * Uses a deterministic and fair algorithm for selecting router node.
  */
 
-u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
+u32 tipc_node_select_router(struct tipc_node *n_ptr, u32 ref)
 {
        u32 ulim;
        u32 mask;
@@ -525,7 +551,7 @@ u32 tipc_node_select_router(struct node *n_ptr, u32 ref)
        return tipc_addr(own_zone(), own_cluster(), r);
 }
 
-void tipc_node_add_router(struct node *n_ptr, u32 router)
+void tipc_node_add_router(struct tipc_node *n_ptr, u32 router)
 {
        u32 r_num = tipc_node(router);
 
@@ -536,7 +562,7 @@ void tipc_node_add_router(struct node *n_ptr, u32 router)
               !n_ptr->routers[n_ptr->last_router]);
 }
 
-void tipc_node_remove_router(struct node *n_ptr, u32 router)
+void tipc_node_remove_router(struct tipc_node *n_ptr, u32 router)
 {
        u32 r_num = tipc_node(router);
 
@@ -554,7 +580,7 @@ void tipc_node_remove_router(struct node *n_ptr, u32 router)
 }
 
 #if 0
-void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
+void node_print(struct print_buf *buf, struct tipc_node *n_ptr, char *str)
 {
        u32 i;
 
@@ -571,15 +597,17 @@ void node_print(struct print_buf *buf, struct node *n_ptr, char *str)
 
 u32 tipc_available_nodes(const u32 domain)
 {
-       struct node *n_ptr;
+       struct tipc_node *n_ptr;
        u32 cnt = 0;
 
+       read_lock_bh(&tipc_net_lock);
        for (n_ptr = tipc_nodes; n_ptr; n_ptr = n_ptr->next) {
                if (!in_scope(domain, n_ptr->addr))
                        continue;
                if (tipc_node_is_up(n_ptr))
                        cnt++;
        }
+       read_unlock_bh(&tipc_net_lock);
        return cnt;
 }
 
@@ -587,7 +615,7 @@ struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
 {
        u32 domain;
        struct sk_buff *buf;
-       struct node *n_ptr;
+       struct tipc_node *n_ptr;
        struct tipc_node_info node_info;
        u32 payload_size;
 
@@ -599,19 +627,26 @@ struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
                return tipc_cfg_reply_error_string(TIPC_CFG_INVALID_VALUE
                                                   " (network address)");
 
-       if (!tipc_nodes)
+       read_lock_bh(&tipc_net_lock);
+       if (!tipc_nodes) {
+               read_unlock_bh(&tipc_net_lock);
                return tipc_cfg_reply_none();
+       }
 
        /* For now, get space for all other nodes
           (will need to modify this when slave nodes are supported */
 
        payload_size = TLV_SPACE(sizeof(node_info)) * (tipc_max_nodes - 1);
-       if (payload_size > 32768u)
+       if (payload_size > 32768u) {
+               read_unlock_bh(&tipc_net_lock);
                return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
                                                   " (too many nodes)");
+       }
        buf = tipc_cfg_reply_alloc(payload_size);
-       if (!buf)
+       if (!buf) {
+               read_unlock_bh(&tipc_net_lock);
                return NULL;
+       }
 
        /* Add TLVs for all nodes in scope */
 
@@ -624,6 +659,7 @@ struct sk_buff *tipc_node_get_nodes(const void *req_tlv_area, int req_tlv_space)
                                    &node_info, sizeof(node_info));
        }
 
+       read_unlock_bh(&tipc_net_lock);
        return buf;
 }
 
@@ -631,7 +667,7 @@ struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
 {
        u32 domain;
        struct sk_buff *buf;
-       struct node *n_ptr;
+       struct tipc_node *n_ptr;
        struct tipc_link_info link_info;
        u32 payload_size;
 
@@ -646,16 +682,22 @@ struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
        if (tipc_mode != TIPC_NET_MODE)
                return tipc_cfg_reply_none();
 
+       read_lock_bh(&tipc_net_lock);
+
        /* Get space for all unicast links + multicast link */
 
        payload_size = TLV_SPACE(sizeof(link_info)) *
                (tipc_net.zones[tipc_zone(tipc_own_addr)]->links + 1);
-       if (payload_size > 32768u)
+       if (payload_size > 32768u) {
+               read_unlock_bh(&tipc_net_lock);
                return tipc_cfg_reply_error_string(TIPC_CFG_NOT_SUPPORTED
                                                   " (too many links)");
+       }
        buf = tipc_cfg_reply_alloc(payload_size);
-       if (!buf)
+       if (!buf) {
+               read_unlock_bh(&tipc_net_lock);
                return NULL;
+       }
 
        /* Add TLV for broadcast link */
 
@@ -671,6 +713,7 @@ struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
 
                if (!in_scope(domain, n_ptr->addr))
                        continue;
+               tipc_node_lock(n_ptr);
                for (i = 0; i < MAX_BEARERS; i++) {
                        if (!n_ptr->links[i])
                                continue;
@@ -680,7 +723,9 @@ struct sk_buff *tipc_node_get_links(const void *req_tlv_area, int req_tlv_space)
                        tipc_cfg_append_tlv(buf, TIPC_TLV_LINK_INFO,
                                            &link_info, sizeof(link_info));
                }
+               tipc_node_unlock(n_ptr);
        }
 
+       read_unlock_bh(&tipc_net_lock);
        return buf;
 }