blob: a0a0fdb8580513215a59f971dd199aa00dc10d0f [file] [log] [blame]
Sven Eckelmann0046b042016-01-01 00:01:03 +01001/* Copyright (C) 2006-2016 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Simon Wunderlich, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "hash.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
21#include <linux/fs.h>
22#include <linux/lockdep.h>
23#include <linux/slab.h>
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000024
25/* clears the hash */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020026static void batadv_hash_init(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000027{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020028 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029
Sven Eckelmanncb4cca72012-06-17 16:27:22 +020030 for (i = 0; i < hash->size; i++) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031 INIT_HLIST_HEAD(&hash->table[i]);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000032 spin_lock_init(&hash->list_locks[i]);
33 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034}
35
36/* free only the hashtable and the hash itself. */
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020037void batadv_hash_destroy(struct batadv_hashtable *hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
Marek Lindnerfb778ea2011-01-19 20:01:40 +000039 kfree(hash->list_locks);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040 kfree(hash->table);
41 kfree(hash);
42}
43
44/* allocates and clears the hash */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020045struct batadv_hashtable *batadv_hash_new(u32 size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020047 struct batadv_hashtable *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048
Sven Eckelmann704509b2011-05-14 23:14:54 +020049 hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050 if (!hash)
51 return NULL;
52
Antonio Quartulli0185dda2014-05-24 06:43:47 +020053 hash->table = kmalloc_array(size, sizeof(*hash->table), GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000054 if (!hash->table)
55 goto free_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056
Antonio Quartulli0185dda2014-05-24 06:43:47 +020057 hash->list_locks = kmalloc_array(size, sizeof(*hash->list_locks),
58 GFP_ATOMIC);
Marek Lindnerfb778ea2011-01-19 20:01:40 +000059 if (!hash->list_locks)
60 goto free_table;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061
Marek Lindnerfb778ea2011-01-19 20:01:40 +000062 hash->size = size;
Sven Eckelmann7f9f02c2012-05-12 18:33:58 +020063 batadv_hash_init(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064 return hash;
Marek Lindnerfb778ea2011-01-19 20:01:40 +000065
66free_table:
67 kfree(hash->table);
68free_hash:
69 kfree(hash);
70 return NULL;
71}
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020072
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020073void batadv_hash_set_lock_class(struct batadv_hashtable *hash,
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020074 struct lock_class_key *key)
75{
Sven Eckelmann6b5e9712015-05-26 18:34:26 +020076 u32 i;
Sven Eckelmann5d52dad2012-03-29 12:38:20 +020077
78 for (i = 0; i < hash->size; i++)
79 lockdep_set_class(&hash->list_locks[i], key);
80}