um: remove file pointer from ioctl
[pandora-kernel.git] / net / batman-adv / originator.c
index 61299da..0b91330 100644 (file)
@@ -44,18 +44,15 @@ int originator_init(struct bat_priv *bat_priv)
        if (bat_priv->orig_hash)
                return 1;
 
-       spin_lock_bh(&bat_priv->orig_hash_lock);
        bat_priv->orig_hash = hash_new(1024);
 
        if (!bat_priv->orig_hash)
                goto err;
 
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
        start_purge_timer(bat_priv);
        return 1;
 
 err:
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
        return 0;
 }
 
@@ -76,7 +73,7 @@ void neigh_node_free_ref(struct neigh_node *neigh_node)
 struct neigh_node *create_neighbor(struct orig_node *orig_node,
                                   struct orig_node *orig_neigh_node,
                                   uint8_t *neigh,
-                                  struct batman_if *if_incoming)
+                                  struct hard_iface *if_incoming)
 {
        struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
        struct neigh_node *neigh_node;
@@ -94,7 +91,9 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
        memcpy(neigh_node->addr, neigh, ETH_ALEN);
        neigh_node->orig_node = orig_neigh_node;
        neigh_node->if_incoming = if_incoming;
-       atomic_set(&neigh_node->refcount, 1);
+
+       /* extra reference for return */
+       atomic_set(&neigh_node->refcount, 2);
 
        spin_lock_bh(&orig_node->neigh_list_lock);
        hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
@@ -102,13 +101,13 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
        return neigh_node;
 }
 
-void orig_node_free_ref(struct kref *refcount)
+static void orig_node_free_rcu(struct rcu_head *rcu)
 {
        struct hlist_node *node, *node_tmp;
        struct neigh_node *neigh_node, *tmp_neigh_node;
        struct orig_node *orig_node;
 
-       orig_node = container_of(refcount, struct orig_node, refcount);
+       orig_node = container_of(rcu, struct orig_node, rcu);
 
        spin_lock_bh(&orig_node->neigh_list_lock);
 
@@ -137,12 +136,17 @@ void orig_node_free_ref(struct kref *refcount)
        kfree(orig_node);
 }
 
+void orig_node_free_ref(struct orig_node *orig_node)
+{
+       if (atomic_dec_and_test(&orig_node->refcount))
+               call_rcu(&orig_node->rcu, orig_node_free_rcu);
+}
+
 void originator_free(struct bat_priv *bat_priv)
 {
        struct hashtable_t *hash = bat_priv->orig_hash;
-       struct hlist_node *walk, *safe;
+       struct hlist_node *node, *node_tmp;
        struct hlist_head *head;
-       struct element_t *bucket;
        spinlock_t *list_lock; /* spinlock to protect write access */
        struct orig_node *orig_node;
        int i;
@@ -152,7 +156,6 @@ void originator_free(struct bat_priv *bat_priv)
 
        cancel_delayed_work_sync(&bat_priv->orig_work);
 
-       spin_lock_bh(&bat_priv->orig_hash_lock);
        bat_priv->orig_hash = NULL;
 
        for (i = 0; i < hash->size; i++) {
@@ -160,30 +163,16 @@ void originator_free(struct bat_priv *bat_priv)
                list_lock = &hash->list_locks[i];
 
                spin_lock_bh(list_lock);
-               hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
-                       orig_node = bucket->data;
+               hlist_for_each_entry_safe(orig_node, node, node_tmp,
+                                         head, hash_entry) {
 
-                       hlist_del_rcu(walk);
-                       call_rcu(&bucket->rcu, bucket_free_rcu);
-                       kref_put(&orig_node->refcount, orig_node_free_ref);
+                       hlist_del_rcu(node);
+                       orig_node_free_ref(orig_node);
                }
                spin_unlock_bh(list_lock);
        }
 
        hash_destroy(hash);
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
-}
-
-static void bucket_free_orig_rcu(struct rcu_head *rcu)
-{
-       struct element_t *bucket;
-       struct orig_node *orig_node;
-
-       bucket = container_of(rcu, struct element_t, rcu);
-       orig_node = bucket->data;
-
-       kref_put(&orig_node->refcount, orig_node_free_ref);
-       kfree(bucket);
 }
 
 /* this function finds or creates an originator entry for the given
@@ -194,16 +183,9 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
        int size;
        int hash_added;
 
-       rcu_read_lock();
-       orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
-                                                  compare_orig, choose_orig,
-                                                  addr));
-       rcu_read_unlock();
-
-       if (orig_node) {
-               kref_get(&orig_node->refcount);
+       orig_node = orig_hash_find(bat_priv, addr);
+       if (orig_node)
                return orig_node;
-       }
 
        bat_dbg(DBG_BATMAN, bat_priv,
                "Creating new originator: %pM\n", addr);
@@ -215,8 +197,11 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
        INIT_HLIST_HEAD(&orig_node->neigh_list);
        INIT_LIST_HEAD(&orig_node->bond_list);
        spin_lock_init(&orig_node->ogm_cnt_lock);
+       spin_lock_init(&orig_node->bcast_seqno_lock);
        spin_lock_init(&orig_node->neigh_list_lock);
-       kref_init(&orig_node->refcount);
+
+       /* extra reference for return */
+       atomic_set(&orig_node->refcount, 2);
 
        orig_node->bat_priv = bat_priv;
        memcpy(orig_node->orig, addr, ETH_ALEN);
@@ -244,13 +229,11 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
        if (!orig_node->bcast_own_sum)
                goto free_bcast_own;
 
-       hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
-                             orig_node);
+       hash_added = hash_add(bat_priv->orig_hash, compare_orig,
+                             choose_orig, orig_node, &orig_node->hash_entry);
        if (hash_added < 0)
                goto free_bcast_own_sum;
 
-       /* extra reference for return */
-       kref_get(&orig_node->refcount);
        return orig_node;
 free_bcast_own_sum:
        kfree(orig_node->bcast_own_sum);
@@ -345,9 +328,8 @@ static bool purge_orig_node(struct bat_priv *bat_priv,
 static void _purge_orig(struct bat_priv *bat_priv)
 {
        struct hashtable_t *hash = bat_priv->orig_hash;
-       struct hlist_node *walk, *safe;
+       struct hlist_node *node, *node_tmp;
        struct hlist_head *head;
-       struct element_t *bucket;
        spinlock_t *list_lock; /* spinlock to protect write access */
        struct orig_node *orig_node;
        int i;
@@ -355,22 +337,19 @@ static void _purge_orig(struct bat_priv *bat_priv)
        if (!hash)
                return;
 
-       spin_lock_bh(&bat_priv->orig_hash_lock);
-
        /* for all origins... */
        for (i = 0; i < hash->size; i++) {
                head = &hash->table[i];
                list_lock = &hash->list_locks[i];
 
                spin_lock_bh(list_lock);
-               hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
-                       orig_node = bucket->data;
-
+               hlist_for_each_entry_safe(orig_node, node, node_tmp,
+                                         head, hash_entry) {
                        if (purge_orig_node(bat_priv, orig_node)) {
                                if (orig_node->gw_flags)
                                        gw_node_delete(bat_priv, orig_node);
-                               hlist_del_rcu(walk);
-                               call_rcu(&bucket->rcu, bucket_free_orig_rcu);
+                               hlist_del_rcu(node);
+                               orig_node_free_ref(orig_node);
                                continue;
                        }
 
@@ -381,8 +360,6 @@ static void _purge_orig(struct bat_priv *bat_priv)
                spin_unlock_bh(list_lock);
        }
 
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
-
        gw_node_purge(bat_priv);
        gw_election(bat_priv);
 
@@ -410,9 +387,8 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
        struct net_device *net_dev = (struct net_device *)seq->private;
        struct bat_priv *bat_priv = netdev_priv(net_dev);
        struct hashtable_t *hash = bat_priv->orig_hash;
-       struct hlist_node *walk, *node;
+       struct hlist_node *node, *node_tmp;
        struct hlist_head *head;
-       struct element_t *bucket;
        struct orig_node *orig_node;
        struct neigh_node *neigh_node;
        int batman_count = 0;
@@ -440,15 +416,11 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
                   "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
                   "outgoingIF", "Potential nexthops");
 
-       spin_lock_bh(&bat_priv->orig_hash_lock);
-
        for (i = 0; i < hash->size; i++) {
                head = &hash->table[i];
 
                rcu_read_lock();
-               hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
-                       orig_node = bucket->data;
-
+               hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
                        if (!orig_node->router)
                                continue;
 
@@ -467,7 +439,7 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
                                   neigh_node->addr,
                                   neigh_node->if_incoming->net_dev->name);
 
-                       hlist_for_each_entry_rcu(neigh_node, node,
+                       hlist_for_each_entry_rcu(neigh_node, node_tmp,
                                                 &orig_node->neigh_list, list) {
                                seq_printf(seq, " %pM (%3i)", neigh_node->addr,
                                                neigh_node->tq_avg);
@@ -479,8 +451,6 @@ int orig_seq_print_text(struct seq_file *seq, void *offset)
                rcu_read_unlock();
        }
 
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
-
        if ((batman_count == 0))
                seq_printf(seq, "No batman nodes in range ...\n");
 
@@ -517,27 +487,22 @@ static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
        return 0;
 }
 
-int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
+int orig_hash_add_if(struct hard_iface *hard_iface, int max_if_num)
 {
-       struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
+       struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
        struct hashtable_t *hash = bat_priv->orig_hash;
-       struct hlist_node *walk;
+       struct hlist_node *node;
        struct hlist_head *head;
-       struct element_t *bucket;
        struct orig_node *orig_node;
        int i, ret;
 
        /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
         * if_num */
-       spin_lock_bh(&bat_priv->orig_hash_lock);
-
        for (i = 0; i < hash->size; i++) {
                head = &hash->table[i];
 
                rcu_read_lock();
-               hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
-                       orig_node = bucket->data;
-
+               hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
                        spin_lock_bh(&orig_node->ogm_cnt_lock);
                        ret = orig_node_add_if(orig_node, max_if_num);
                        spin_unlock_bh(&orig_node->ogm_cnt_lock);
@@ -548,12 +513,10 @@ int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
                rcu_read_unlock();
        }
 
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
        return 0;
 
 err:
        rcu_read_unlock();
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
        return -ENOMEM;
 }
 
@@ -609,31 +572,26 @@ free_own_sum:
        return 0;
 }
 
-int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
+int orig_hash_del_if(struct hard_iface *hard_iface, int max_if_num)
 {
-       struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
+       struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
        struct hashtable_t *hash = bat_priv->orig_hash;
-       struct hlist_node *walk;
+       struct hlist_node *node;
        struct hlist_head *head;
-       struct element_t *bucket;
-       struct batman_if *batman_if_tmp;
+       struct hard_iface *hard_iface_tmp;
        struct orig_node *orig_node;
        int i, ret;
 
        /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
         * if_num */
-       spin_lock_bh(&bat_priv->orig_hash_lock);
-
        for (i = 0; i < hash->size; i++) {
                head = &hash->table[i];
 
                rcu_read_lock();
-               hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
-                       orig_node = bucket->data;
-
+               hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
                        spin_lock_bh(&orig_node->ogm_cnt_lock);
                        ret = orig_node_del_if(orig_node, max_if_num,
-                                       batman_if->if_num);
+                                       hard_iface->if_num);
                        spin_unlock_bh(&orig_node->ogm_cnt_lock);
 
                        if (ret == -1)
@@ -644,27 +602,25 @@ int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
 
        /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
        rcu_read_lock();
-       list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
-               if (batman_if_tmp->if_status == IF_NOT_IN_USE)
+       list_for_each_entry_rcu(hard_iface_tmp, &hardif_list, list) {
+               if (hard_iface_tmp->if_status == IF_NOT_IN_USE)
                        continue;
 
-               if (batman_if == batman_if_tmp)
+               if (hard_iface == hard_iface_tmp)
                        continue;
 
-               if (batman_if->soft_iface != batman_if_tmp->soft_iface)
+               if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
                        continue;
 
-               if (batman_if_tmp->if_num > batman_if->if_num)
-                       batman_if_tmp->if_num--;
+               if (hard_iface_tmp->if_num > hard_iface->if_num)
+                       hard_iface_tmp->if_num--;
        }
        rcu_read_unlock();
 
-       batman_if->if_num = -1;
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
+       hard_iface->if_num = -1;
        return 0;
 
 err:
        rcu_read_unlock();
-       spin_unlock_bh(&bat_priv->orig_hash_lock);
        return -ENOMEM;
 }