Skip to content

Commit 1a1f37d

Browse files
Antonio QuartulliMarek Lindner
authored andcommitted
batman-adv: hash_add() has to discriminate on the return value
hash_add() returns 0 on success while returns -1 either on error and on entry already present. The caller could use such information to select its behaviour. For this reason it is useful that hash_add() returns -1 in case on error and returns 1 in case of entry already present. Signed-off-by: Antonio Quartulli <ordex@autistici.org> Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
1 parent 322a8b0 commit 1a1f37d

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎net/batman-adv/hash.h‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,30 @@ static inline void hash_delete(struct hashtable_t *hash,
7676
hash_destroy(hash);
7777
}
7878

79-
/* adds data to the hashtable. returns 0 on success, -1 on error */
79+
/**
80+
* hash_add - adds data to the hashtable
81+
* @hash: storage hash table
82+
* @compare: callback to determine if 2 hash elements are identical
83+
* @choose: callback calculating the hash index
84+
* @data: data passed to the aforementioned callbacks as argument
85+
* @data_node: to be added element
86+
*
87+
* Returns 0 on success, 1 if the element already is in the hash
88+
* and -1 on error.
89+
*/
90+
8091
static inline int hash_add(struct hashtable_t *hash,
8192
hashdata_compare_cb compare,
8293
hashdata_choose_cb choose,
8394
const void *data, struct hlist_node *data_node)
8495
{
85-
int index;
96+
int index, ret = -1;
8697
struct hlist_head *head;
8798
struct hlist_node *node;
8899
spinlock_t *list_lock; /* spinlock to protect write access */
89100

90101
if (!hash)
91-
goto err;
102+
goto out;
92103

93104
index = choose(data, hash->size);
94105
head = &hash->table[index];
@@ -99,6 +110,7 @@ static inline int hash_add(struct hashtable_t *hash,
99110
if (!compare(node, data))
100111
continue;
101112

113+
ret = 1;
102114
goto err_unlock;
103115
}
104116
rcu_read_unlock();
@@ -108,12 +120,13 @@ static inline int hash_add(struct hashtable_t *hash,
108120
hlist_add_head_rcu(data_node, head);
109121
spin_unlock_bh(list_lock);
110122

111-
return 0;
123+
ret = 0;
124+
goto out;
112125

113126
err_unlock:
114127
rcu_read_unlock();
115-
err:
116-
return -1;
128+
out:
129+
return ret;
117130
}
118131

119132
/* removes data from hash, if found. returns pointer do data on success, so you

‎net/batman-adv/originator.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, const uint8_t *addr)
252252

253253
hash_added = hash_add(bat_priv->orig_hash, compare_orig,
254254
choose_orig, orig_node, &orig_node->hash_entry);
255-
if (hash_added < 0)
255+
if (hash_added != 0)
256256
goto free_bcast_own_sum;
257257

258258
return orig_node;

‎net/batman-adv/vis.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ static struct vis_info *add_packet(struct bat_priv *bat_priv,
465465
/* try to add it */
466466
hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
467467
info, &info->hash_entry);
468-
if (hash_added < 0) {
468+
if (hash_added != 0) {
469469
/* did not work (for some reason) */
470470
kref_put(&info->refcount, free_info);
471471
info = NULL;
@@ -920,7 +920,7 @@ int vis_init(struct bat_priv *bat_priv)
920920
hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
921921
bat_priv->my_vis_info,
922922
&bat_priv->my_vis_info->hash_entry);
923-
if (hash_added < 0) {
923+
if (hash_added != 0) {
924924
pr_err("Can't add own vis packet into hash\n");
925925
/* not in hash, need to remove it manually. */
926926
kref_put(&bat_priv->my_vis_info->refcount, free_info);

0 commit comments

Comments
 (0)