10eb71a9dSNeilBrown /* SPDX-License-Identifier: GPL-2.0 */
20eb71a9dSNeilBrown /*
30eb71a9dSNeilBrown  * Resizable, Scalable, Concurrent Hash Table
40eb71a9dSNeilBrown  *
50eb71a9dSNeilBrown  * Simple structures that might be needed in include
60eb71a9dSNeilBrown  * files.
70eb71a9dSNeilBrown  */
80eb71a9dSNeilBrown 
90eb71a9dSNeilBrown #ifndef _LINUX_RHASHTABLE_TYPES_H
100eb71a9dSNeilBrown #define _LINUX_RHASHTABLE_TYPES_H
110eb71a9dSNeilBrown 
120eb71a9dSNeilBrown #include <linux/atomic.h>
130eb71a9dSNeilBrown #include <linux/compiler.h>
140eb71a9dSNeilBrown #include <linux/mutex.h>
150eb71a9dSNeilBrown #include <linux/workqueue.h>
160eb71a9dSNeilBrown 
170eb71a9dSNeilBrown struct rhash_head {
180eb71a9dSNeilBrown 	struct rhash_head __rcu		*next;
190eb71a9dSNeilBrown };
200eb71a9dSNeilBrown 
210eb71a9dSNeilBrown struct rhlist_head {
220eb71a9dSNeilBrown 	struct rhash_head		rhead;
230eb71a9dSNeilBrown 	struct rhlist_head __rcu	*next;
240eb71a9dSNeilBrown };
250eb71a9dSNeilBrown 
260eb71a9dSNeilBrown struct bucket_table;
270eb71a9dSNeilBrown 
280eb71a9dSNeilBrown /**
290eb71a9dSNeilBrown  * struct rhashtable_compare_arg - Key for the function rhashtable_compare
300eb71a9dSNeilBrown  * @ht: Hash table
310eb71a9dSNeilBrown  * @key: Key to compare against
320eb71a9dSNeilBrown  */
330eb71a9dSNeilBrown struct rhashtable_compare_arg {
340eb71a9dSNeilBrown 	struct rhashtable *ht;
350eb71a9dSNeilBrown 	const void *key;
360eb71a9dSNeilBrown };
370eb71a9dSNeilBrown 
380eb71a9dSNeilBrown typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
390eb71a9dSNeilBrown typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
400eb71a9dSNeilBrown typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
410eb71a9dSNeilBrown 			       const void *obj);
420eb71a9dSNeilBrown 
430eb71a9dSNeilBrown /**
440eb71a9dSNeilBrown  * struct rhashtable_params - Hash table construction parameters
450eb71a9dSNeilBrown  * @nelem_hint: Hint on number of elements, should be 75% of desired size
460eb71a9dSNeilBrown  * @key_len: Length of key
470eb71a9dSNeilBrown  * @key_offset: Offset of key in struct to be hashed
480eb71a9dSNeilBrown  * @head_offset: Offset of rhash_head in struct to be hashed
490eb71a9dSNeilBrown  * @max_size: Maximum size while expanding
500eb71a9dSNeilBrown  * @min_size: Minimum size while shrinking
510eb71a9dSNeilBrown  * @automatic_shrinking: Enable automatic shrinking of tables
520eb71a9dSNeilBrown  * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
530eb71a9dSNeilBrown  * @obj_hashfn: Function to hash object
540eb71a9dSNeilBrown  * @obj_cmpfn: Function to compare key with object
550eb71a9dSNeilBrown  */
560eb71a9dSNeilBrown struct rhashtable_params {
570eb71a9dSNeilBrown 	u16			nelem_hint;
580eb71a9dSNeilBrown 	u16			key_len;
590eb71a9dSNeilBrown 	u16			key_offset;
600eb71a9dSNeilBrown 	u16			head_offset;
610eb71a9dSNeilBrown 	unsigned int		max_size;
620eb71a9dSNeilBrown 	u16			min_size;
630eb71a9dSNeilBrown 	bool			automatic_shrinking;
640eb71a9dSNeilBrown 	rht_hashfn_t		hashfn;
650eb71a9dSNeilBrown 	rht_obj_hashfn_t	obj_hashfn;
660eb71a9dSNeilBrown 	rht_obj_cmpfn_t		obj_cmpfn;
670eb71a9dSNeilBrown };
680eb71a9dSNeilBrown 
690eb71a9dSNeilBrown /**
700eb71a9dSNeilBrown  * struct rhashtable - Hash table handle
710eb71a9dSNeilBrown  * @tbl: Bucket table
720eb71a9dSNeilBrown  * @key_len: Key length for hashfn
730eb71a9dSNeilBrown  * @max_elems: Maximum number of elements in table
740eb71a9dSNeilBrown  * @p: Configuration parameters
750eb71a9dSNeilBrown  * @rhlist: True if this is an rhltable
760eb71a9dSNeilBrown  * @run_work: Deferred worker to expand/shrink asynchronously
770eb71a9dSNeilBrown  * @mutex: Mutex to protect current/future table swapping
780eb71a9dSNeilBrown  * @lock: Spin lock to protect walker list
790eb71a9dSNeilBrown  * @nelems: Number of elements in table
800eb71a9dSNeilBrown  */
810eb71a9dSNeilBrown struct rhashtable {
820eb71a9dSNeilBrown 	struct bucket_table __rcu	*tbl;
830eb71a9dSNeilBrown 	unsigned int			key_len;
840eb71a9dSNeilBrown 	unsigned int			max_elems;
850eb71a9dSNeilBrown 	struct rhashtable_params	p;
860eb71a9dSNeilBrown 	bool				rhlist;
870eb71a9dSNeilBrown 	struct work_struct		run_work;
880eb71a9dSNeilBrown 	struct mutex                    mutex;
890eb71a9dSNeilBrown 	spinlock_t			lock;
900eb71a9dSNeilBrown 	atomic_t			nelems;
910eb71a9dSNeilBrown };
920eb71a9dSNeilBrown 
930eb71a9dSNeilBrown /**
940eb71a9dSNeilBrown  * struct rhltable - Hash table with duplicate objects in a list
950eb71a9dSNeilBrown  * @ht: Underlying rhtable
960eb71a9dSNeilBrown  */
970eb71a9dSNeilBrown struct rhltable {
980eb71a9dSNeilBrown 	struct rhashtable ht;
990eb71a9dSNeilBrown };
1000eb71a9dSNeilBrown 
1010eb71a9dSNeilBrown /**
1020eb71a9dSNeilBrown  * struct rhashtable_walker - Hash table walker
1030eb71a9dSNeilBrown  * @list: List entry on list of walkers
1040eb71a9dSNeilBrown  * @tbl: The table that we were walking over
1050eb71a9dSNeilBrown  */
1060eb71a9dSNeilBrown struct rhashtable_walker {
1070eb71a9dSNeilBrown 	struct list_head list;
1080eb71a9dSNeilBrown 	struct bucket_table *tbl;
1090eb71a9dSNeilBrown };
1100eb71a9dSNeilBrown 
1110eb71a9dSNeilBrown /**
1120eb71a9dSNeilBrown  * struct rhashtable_iter - Hash table iterator
1130eb71a9dSNeilBrown  * @ht: Table to iterate through
1140eb71a9dSNeilBrown  * @p: Current pointer
1150eb71a9dSNeilBrown  * @list: Current hash list pointer
1160eb71a9dSNeilBrown  * @walker: Associated rhashtable walker
1170eb71a9dSNeilBrown  * @slot: Current slot
1180eb71a9dSNeilBrown  * @skip: Number of entries to skip in slot
1190eb71a9dSNeilBrown  */
1200eb71a9dSNeilBrown struct rhashtable_iter {
1210eb71a9dSNeilBrown 	struct rhashtable *ht;
1220eb71a9dSNeilBrown 	struct rhash_head *p;
1230eb71a9dSNeilBrown 	struct rhlist_head *list;
1240eb71a9dSNeilBrown 	struct rhashtable_walker walker;
1250eb71a9dSNeilBrown 	unsigned int slot;
1260eb71a9dSNeilBrown 	unsigned int skip;
1270eb71a9dSNeilBrown 	bool end_of_table;
1280eb71a9dSNeilBrown };
1290eb71a9dSNeilBrown 
1300eb71a9dSNeilBrown int rhashtable_init(struct rhashtable *ht,
1310eb71a9dSNeilBrown 		    const struct rhashtable_params *params);
1320eb71a9dSNeilBrown int rhltable_init(struct rhltable *hlt,
1330eb71a9dSNeilBrown 		  const struct rhashtable_params *params);
1340eb71a9dSNeilBrown 
1350eb71a9dSNeilBrown #endif /* _LINUX_RHASHTABLE_TYPES_H */
136