batman-adv: Add SPDX license identifier above copyright header
[linux-block.git] / net / batman-adv / hash.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2006-2017  B.A.T.M.A.N. contributors:
3  *
4  * Simon Wunderlich, Marek Lindner
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef _NET_BATMAN_ADV_HASH_H_
20 #define _NET_BATMAN_ADV_HASH_H_
21
22 #include "main.h"
23
24 #include <linux/compiler.h>
25 #include <linux/list.h>
26 #include <linux/rculist.h>
27 #include <linux/spinlock.h>
28 #include <linux/stddef.h>
29 #include <linux/types.h>
30
31 struct lock_class_key;
32
33 /* callback to a compare function.  should compare 2 element datas for their
34  * keys
35  *
36  * Return: true if same and false if not same
37  */
38 typedef bool (*batadv_hashdata_compare_cb)(const struct hlist_node *,
39                                            const void *);
40
41 /* the hashfunction
42  *
43  * Return: an index based on the key in the data of the first argument and the
44  * size the second
45  */
46 typedef u32 (*batadv_hashdata_choose_cb)(const void *, u32);
47 typedef void (*batadv_hashdata_free_cb)(struct hlist_node *, void *);
48
49 struct batadv_hashtable {
50         struct hlist_head *table;   /* the hashtable itself with the buckets */
51         spinlock_t *list_locks;     /* spinlock for each hash list entry */
52         u32 size;                   /* size of hashtable */
53 };
54
55 /* allocates and clears the hash */
56 struct batadv_hashtable *batadv_hash_new(u32 size);
57
58 /* set class key for all locks */
59 void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
60                                 struct lock_class_key *key);
61
62 /* free only the hashtable and the hash itself. */
63 void batadv_hash_destroy(struct batadv_hashtable *hash);
64
65 /**
66  *      batadv_hash_add - adds data to the hashtable
67  *      @hash: storage hash table
68  *      @compare: callback to determine if 2 hash elements are identical
69  *      @choose: callback calculating the hash index
70  *      @data: data passed to the aforementioned callbacks as argument
71  *      @data_node: to be added element
72  *
73  *      Return: 0 on success, 1 if the element already is in the hash
74  *      and -1 on error.
75  */
76 static inline int batadv_hash_add(struct batadv_hashtable *hash,
77                                   batadv_hashdata_compare_cb compare,
78                                   batadv_hashdata_choose_cb choose,
79                                   const void *data,
80                                   struct hlist_node *data_node)
81 {
82         u32 index;
83         int ret = -1;
84         struct hlist_head *head;
85         struct hlist_node *node;
86         spinlock_t *list_lock; /* spinlock to protect write access */
87
88         if (!hash)
89                 goto out;
90
91         index = choose(data, hash->size);
92         head = &hash->table[index];
93         list_lock = &hash->list_locks[index];
94
95         spin_lock_bh(list_lock);
96
97         hlist_for_each(node, head) {
98                 if (!compare(node, data))
99                         continue;
100
101                 ret = 1;
102                 goto unlock;
103         }
104
105         /* no duplicate found in list, add new element */
106         hlist_add_head_rcu(data_node, head);
107
108         ret = 0;
109
110 unlock:
111         spin_unlock_bh(list_lock);
112 out:
113         return ret;
114 }
115
116 /* removes data from hash, if found. data could be the structure you use with
117  * just the key filled, we just need the key for comparing.
118  *
119  * Return: returns pointer do data on success, so you can remove the used
120  * structure yourself, or NULL on error
121  */
122 static inline void *batadv_hash_remove(struct batadv_hashtable *hash,
123                                        batadv_hashdata_compare_cb compare,
124                                        batadv_hashdata_choose_cb choose,
125                                        void *data)
126 {
127         u32 index;
128         struct hlist_node *node;
129         struct hlist_head *head;
130         void *data_save = NULL;
131
132         index = choose(data, hash->size);
133         head = &hash->table[index];
134
135         spin_lock_bh(&hash->list_locks[index]);
136         hlist_for_each(node, head) {
137                 if (!compare(node, data))
138                         continue;
139
140                 data_save = node;
141                 hlist_del_rcu(node);
142                 break;
143         }
144         spin_unlock_bh(&hash->list_locks[index]);
145
146         return data_save;
147 }
148
149 #endif /* _NET_BATMAN_ADV_HASH_H_ */