Blame SOURCES/autofs-5.1.6-add-hashtable-implementation.patch

beb904
autofs-5.1.6 - add hashtable implementation
beb904
beb904
From: Ian Kent <raven@themaw.net>
beb904
beb904
Include the (slightly modified) Linux kernel hashtable implementation.
beb904
beb904
Signed-off-by: Ian Kent <raven@themaw.net>
beb904
---
beb904
 CHANGELOG           |    1 
beb904
 include/automount.h |   19 -----
beb904
 include/hash.h      |  101 +++++++++++++++++++++++++++++++
beb904
 include/hashtable.h |  166 ++++++++++++++++++++++++++++++++++++++++++++++++++++
beb904
 4 files changed, 269 insertions(+), 18 deletions(-)
beb904
 create mode 100644 include/hash.h
beb904
 create mode 100644 include/hashtable.h
beb904
beb904
--- autofs-5.1.4.orig/CHANGELOG
beb904
+++ autofs-5.1.4/CHANGELOG
beb904
@@ -117,6 +117,7 @@ xx/xx/2018 autofs-5.1.5
beb904
 - make bind mounts propagation slave by default.
beb904
 - fix browse dir not re-created on symlink expire.
beb904
 - update list.h.
beb904
+- add hashtable implementation.
beb904
 
beb904
 19/12/2017 autofs-5.1.4
beb904
 - fix spec file url.
beb904
--- autofs-5.1.4.orig/include/automount.h
beb904
+++ autofs-5.1.4/include/automount.h
beb904
@@ -22,6 +22,7 @@
beb904
 #include <mntent.h>
beb904
 #include "config.h"
beb904
 #include "list.h"
beb904
+#include "hash.h"
beb904
 
beb904
 #include <linux/auto_fs4.h>
beb904
 
beb904
@@ -143,24 +144,6 @@ struct autofs_point;
beb904
 #define UMOUNT_RETRIES		8
beb904
 #define EXPIRE_RETRIES		3
beb904
 
beb904
-static u_int32_t inline hash(const char *key, unsigned int size)
beb904
-{
beb904
-	u_int32_t hashval;
beb904
-	char *s = (char *) key;
beb904
-
beb904
-	for (hashval = 0; *s != '\0';) {
beb904
-		hashval += (unsigned char) *s++;
beb904
-		hashval += (hashval << 10);
beb904
-		hashval ^= (hashval >> 6);
beb904
-	}
beb904
-
beb904
-	hashval += (hashval << 3);
beb904
-	hashval ^= (hashval >> 11);
beb904
-	hashval += (hashval << 15);
beb904
-
beb904
-	return hashval % size;
beb904
-}
beb904
-
beb904
 struct mapent_cache {
beb904
 	pthread_rwlock_t rwlock;
beb904
 	unsigned int size;
beb904
--- /dev/null
beb904
+++ autofs-5.1.4/include/hash.h
beb904
@@ -0,0 +1,101 @@
beb904
+#ifndef _LINUX_HASH_H
beb904
+#define _LINUX_HASH_H
beb904
+/* Fast hashing routine for ints,  longs and pointers.
beb904
+   (C) 2002 Nadia Yvette Chambers, IBM */
beb904
+
beb904
+#include <sys/types.h>
beb904
+#include <stdint.h>
beb904
+
beb904
+/*
beb904
+ * The "GOLDEN_RATIO_PRIME" is used in ifs/btrfs/brtfs_inode.h and
beb904
+ * fs/inode.c.  It's not actually prime any more (the previous primes
beb904
+ * were actively bad for hashing), but the name remains.
beb904
+ */
beb904
+#if __WORDSIZE == 32
beb904
+#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_32
beb904
+#define hash_long(val, bits) hash_32(val, bits)
beb904
+#elif __WORDSIZE == 64
beb904
+#define hash_long(val, bits) hash_64(val, bits)
beb904
+#define GOLDEN_RATIO_PRIME GOLDEN_RATIO_64
beb904
+#else
beb904
+#error Wordsize not 32 or 64
beb904
+#endif
beb904
+
beb904
+/* String based hash function */
beb904
+static uint32_t inline hash(const char *key, unsigned int size)
beb904
+{
beb904
+	u_int32_t hashval;
beb904
+	char *s = (char *) key;
beb904
+
beb904
+	for (hashval = 0; *s != '\0';) {
beb904
+		hashval += (unsigned char) *s++;
beb904
+		hashval += (hashval << 10);
beb904
+		hashval ^= (hashval >> 6);
beb904
+	}
beb904
+
beb904
+	hashval += (hashval << 3);
beb904
+	hashval ^= (hashval >> 11);
beb904
+	hashval += (hashval << 15);
beb904
+
beb904
+	return hashval % size;
beb904
+}
beb904
+
beb904
+/*
beb904
+ * This hash multiplies the input by a large odd number and takes the
beb904
+ * high bits.  Since multiplication propagates changes to the most
beb904
+ * significant end only, it is essential that the high bits of the
beb904
+ * product be used for the hash value.
beb904
+ *
beb904
+ * Chuck Lever verified the effectiveness of this technique:
beb904
+ * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
beb904
+ *
beb904
+ * Although a random odd number will do, it turns out that the golden
beb904
+ * ratio phi = (sqrt(5)-1)/2, or its negative, has particularly nice
beb904
+ * properties.  (See Knuth vol 3, section 6.4, exercise 9.)
beb904
+ *
beb904
+ * These are the negative, (1 - phi) = phi**2 = (3 - sqrt(5))/2,
beb904
+ * which is very slightly easier to multiply by and makes no
beb904
+ * difference to the hash distribution.
beb904
+ */
beb904
+#define GOLDEN_RATIO_32 0x61C88647
beb904
+#define GOLDEN_RATIO_64 0x61C8864680B583EBull
beb904
+
beb904
+static inline uint32_t __hash_32(uint32_t val)
beb904
+{
beb904
+	return val * GOLDEN_RATIO_32;
beb904
+}
beb904
+
beb904
+static inline uint32_t hash_32(uint32_t val, unsigned int bits)
beb904
+{
beb904
+	/* High bits are more random, so use them. */
beb904
+	return __hash_32(val) >> (32 - bits);
beb904
+}
beb904
+
beb904
+static __always_inline uint32_t hash_64(uint64_t val, unsigned int bits)
beb904
+{
beb904
+#if __WORDSIZE == 64
beb904
+	/* 64x64-bit multiply is efficient on all 64-bit processors */
beb904
+	return val * GOLDEN_RATIO_64 >> (64 - bits);
beb904
+#else
beb904
+	/* Hash 64 bits using only 32x32-bit multiply. */
beb904
+	return hash_32((uint32_t) val ^ __hash_32(val >> 32), bits);
beb904
+#endif
beb904
+}
beb904
+
beb904
+static inline uint32_t hash_ptr(const void *ptr, unsigned int bits)
beb904
+{
beb904
+	return hash_long((unsigned long) ptr, bits);
beb904
+}
beb904
+
beb904
+/* This really should be called fold32_ptr; it does no hashing to speak of. */
beb904
+static inline uint32_t hash32_ptr(const void *ptr)
beb904
+{
beb904
+	unsigned long val = (unsigned long) ptr;
beb904
+
beb904
+#if __WORDSIZE == 64
beb904
+	val ^= (val >> 32);
beb904
+#endif
beb904
+	return (uint32_t) val;
beb904
+}
beb904
+
beb904
+#endif /* _LINUX_HASH_H */
beb904
--- /dev/null
beb904
+++ autofs-5.1.4/include/hashtable.h
beb904
@@ -0,0 +1,166 @@
beb904
+/*
beb904
+ * Statically sized hash table implementation
beb904
+ * (C) 2012  Sasha Levin <levinsasha928@gmail.com>
beb904
+ */
beb904
+
beb904
+#ifndef _LINUX_HASHTABLE_H
beb904
+#define _LINUX_HASHTABLE_H
beb904
+
beb904
+#include "list.h"
beb904
+#include "hash.h"
beb904
+
beb904
+#ifndef ARRAY_SIZE
beb904
+#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
beb904
+#endif
beb904
+
beb904
+static inline unsigned int ilog2(unsigned long val) {
beb904
+	unsigned int ret = -1;
beb904
+
beb904
+	while (val != 0) {
beb904
+		val >>= 1;
beb904
+		ret++;
beb904
+	}
beb904
+	return ret;
beb904
+}
beb904
+
beb904
+#define DEFINE_HASHTABLE(name, bits)						\
beb904
+	struct hlist_head name[1 << (bits)] =					\
beb904
+			{ [0 ... ((1 << (bits)) - 1)] = HLIST_HEAD_INIT }
beb904
+
beb904
+#define DECLARE_HASHTABLE(name, bits)                                   	\
beb904
+	struct hlist_head name[1 << (bits)]
beb904
+
beb904
+#define HASH_SIZE(name) (ARRAY_SIZE(name))
beb904
+#define HASH_BITS(name) ilog2(HASH_SIZE(name))
beb904
+
beb904
+/* Use hash_32 when possible to allow for fast 32bit hashing in 64bit kernels. */
beb904
+#define hash_min(val, bits)							\
beb904
+	(sizeof(val) <= 4 ? hash_32(val, bits) : hash_long(val, bits))
beb904
+
beb904
+static inline void __hash_init(struct hlist_head *ht, unsigned int sz)
beb904
+{
beb904
+	unsigned int i;
beb904
+
beb904
+	for (i = 0; i < sz; i++)
beb904
+		INIT_HLIST_HEAD(&ht[i]);
beb904
+}
beb904
+
beb904
+/**
beb904
+ * hash_init - initialize a hash table
beb904
+ * @hashtable: hashtable to be initialized
beb904
+ *
beb904
+ * Calculates the size of the hashtable from the given parameter, otherwise
beb904
+ * same as hash_init_size.
beb904
+ *
beb904
+ * This has to be a macro since HASH_BITS() will not work on pointers since
beb904
+ * it calculates the size during preprocessing.
beb904
+ */
beb904
+#define hash_init(hashtable) __hash_init(hashtable, HASH_SIZE(hashtable))
beb904
+
beb904
+/**
beb904
+ * hash_add - add an object to a hashtable
beb904
+ * @hashtable: hashtable to add to
beb904
+ * @node: the &struct hlist_node of the object to be added
beb904
+ * @key: the key of the object to be added
beb904
+ */
beb904
+#define hash_add(hashtable, node, key)						\
beb904
+	hlist_add_head(node, &hashtable[hash_min(key, HASH_BITS(hashtable))])
beb904
+
beb904
+/**
beb904
+ * hash_add_str - add a string object to a hashtable
beb904
+ * @hashtable: hashtable to add to
beb904
+ * @node: the &struct hlist_node of the object to be added
beb904
+ * @key: the string key of the object to be added
beb904
+ */
beb904
+#define hash_add_str(hashtable, node, key)						\
beb904
+	hlist_add_head(node, &hashtable[hash(key, HASH_SIZE(hashtable))])
beb904
+
beb904
+/**
beb904
+ * hash_hashed - check whether an object is in any hashtable
beb904
+ * @node: the &struct hlist_node of the object to be checked
beb904
+ */
beb904
+static inline int hash_hashed(struct hlist_node *node)
beb904
+{
beb904
+	return !hlist_unhashed(node);
beb904
+}
beb904
+
beb904
+static inline int __hash_empty(struct hlist_head *ht, unsigned int sz)
beb904
+{
beb904
+	unsigned int i;
beb904
+
beb904
+	for (i = 0; i < sz; i++)
beb904
+		if (!hlist_empty(&ht[i]))
beb904
+			return 0;
beb904
+
beb904
+	return 1;
beb904
+}
beb904
+
beb904
+/**
beb904
+ * hash_empty - check whether a hashtable is empty
beb904
+ * @hashtable: hashtable to check
beb904
+ *
beb904
+ * This has to be a macro since HASH_BITS() will not work on pointers since
beb904
+ * it calculates the size during preprocessing.
beb904
+ */
beb904
+#define hash_empty(hashtable) __hash_empty(hashtable, HASH_SIZE(hashtable))
beb904
+
beb904
+/**
beb904
+ * hash_del - remove an object from a hashtable
beb904
+ * @node: &struct hlist_node of the object to remove
beb904
+ */
beb904
+static inline void hash_del(struct hlist_node *node)
beb904
+{
beb904
+	hlist_del_init(node);
beb904
+}
beb904
+
beb904
+/**
beb904
+ * hash_for_each - iterate over a hashtable
beb904
+ * @name: hashtable to iterate
beb904
+ * @bkt: integer to use as bucket loop cursor
beb904
+ * @obj: the type * to use as a loop cursor for each entry
beb904
+ * @member: the name of the hlist_node within the struct
beb904
+ */
beb904
+#define hash_for_each(name, bkt, obj, member)				\
beb904
+	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
beb904
+			(bkt)++)\
beb904
+		hlist_for_each_entry(obj, &name[bkt], member)
beb904
+
beb904
+/**
beb904
+ * hash_for_each_safe - iterate over a hashtable safe against removal of
beb904
+ * hash entry
beb904
+ * @name: hashtable to iterate
beb904
+ * @bkt: integer to use as bucket loop cursor
beb904
+ * @tmp: a &struct used for temporary storage
beb904
+ * @obj: the type * to use as a loop cursor for each entry
beb904
+ * @member: the name of the hlist_node within the struct
beb904
+ */
beb904
+#define hash_for_each_safe(name, bkt, tmp, obj, member)			\
beb904
+	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
beb904
+			(bkt)++)\
beb904
+		hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
beb904
+
beb904
+/**
beb904
+ * hash_for_each_possible - iterate over all possible objects hashing to the
beb904
+ * same bucket
beb904
+ * @name: hashtable to iterate
beb904
+ * @obj: the type * to use as a loop cursor for each entry
beb904
+ * @member: the name of the hlist_node within the struct
beb904
+ * @key: the key of the objects to iterate over
beb904
+ */
beb904
+#define hash_for_each_possible(name, obj, member, key)			\
beb904
+	hlist_for_each_entry(obj, &name[hash_min(key, HASH_BITS(name))], member)
beb904
+
beb904
+/**
beb904
+ * hash_for_each_possible_safe - iterate over all possible objects hashing to the
beb904
+ * same bucket safe against removals
beb904
+ * @name: hashtable to iterate
beb904
+ * @obj: the type * to use as a loop cursor for each entry
beb904
+ * @tmp: a &struct used for temporary storage
beb904
+ * @member: the name of the hlist_node within the struct
beb904
+ * @key: the key of the objects to iterate over
beb904
+ */
beb904
+#define hash_for_each_possible_safe(name, obj, tmp, member, key)	\
beb904
+	hlist_for_each_entry_safe(obj, tmp,\
beb904
+		&name[hash_min(key, HASH_BITS(name))], member)
beb904
+
beb904
+#endif