11654dd0cSSage Weil
23d14c5d2SYehuda Sadeh #include <linux/ceph/types.h>
36c0f3af7SSage Weil #include <linux/module.h>
41654dd0cSSage Weil
51654dd0cSSage Weil /*
61654dd0cSSage Weil * Robert Jenkin's hash function.
794f17c00SAlexander A. Klimov * https://burtleburtle.net/bob/hash/evahash.html
81654dd0cSSage Weil * This is in the public domain.
91654dd0cSSage Weil */
101654dd0cSSage Weil #define mix(a, b, c) \
111654dd0cSSage Weil do { \
121654dd0cSSage Weil a = a - b; a = a - c; a = a ^ (c >> 13); \
131654dd0cSSage Weil b = b - c; b = b - a; b = b ^ (a << 8); \
141654dd0cSSage Weil c = c - a; c = c - b; c = c ^ (b >> 13); \
151654dd0cSSage Weil a = a - b; a = a - c; a = a ^ (c >> 12); \
161654dd0cSSage Weil b = b - c; b = b - a; b = b ^ (a << 16); \
171654dd0cSSage Weil c = c - a; c = c - b; c = c ^ (b >> 5); \
181654dd0cSSage Weil a = a - b; a = a - c; a = a ^ (c >> 3); \
191654dd0cSSage Weil b = b - c; b = b - a; b = b ^ (a << 10); \
201654dd0cSSage Weil c = c - a; c = c - b; c = c ^ (b >> 15); \
211654dd0cSSage Weil } while (0)
221654dd0cSSage Weil
ceph_str_hash_rjenkins(const char * str,unsigned int length)2395c96174SEric Dumazet unsigned int ceph_str_hash_rjenkins(const char *str, unsigned int length)
241654dd0cSSage Weil {
251654dd0cSSage Weil const unsigned char *k = (const unsigned char *)str;
261654dd0cSSage Weil __u32 a, b, c; /* the internal state */
271654dd0cSSage Weil __u32 len; /* how many key bytes still need mixing */
281654dd0cSSage Weil
291654dd0cSSage Weil /* Set up the internal state */
301654dd0cSSage Weil len = length;
311654dd0cSSage Weil a = 0x9e3779b9; /* the golden ratio; an arbitrary value */
321654dd0cSSage Weil b = a;
331654dd0cSSage Weil c = 0; /* variable initialization of internal state */
341654dd0cSSage Weil
351654dd0cSSage Weil /* handle most of the key */
361654dd0cSSage Weil while (len >= 12) {
371654dd0cSSage Weil a = a + (k[0] + ((__u32)k[1] << 8) + ((__u32)k[2] << 16) +
381654dd0cSSage Weil ((__u32)k[3] << 24));
391654dd0cSSage Weil b = b + (k[4] + ((__u32)k[5] << 8) + ((__u32)k[6] << 16) +
401654dd0cSSage Weil ((__u32)k[7] << 24));
411654dd0cSSage Weil c = c + (k[8] + ((__u32)k[9] << 8) + ((__u32)k[10] << 16) +
421654dd0cSSage Weil ((__u32)k[11] << 24));
431654dd0cSSage Weil mix(a, b, c);
441654dd0cSSage Weil k = k + 12;
451654dd0cSSage Weil len = len - 12;
461654dd0cSSage Weil }
471654dd0cSSage Weil
481654dd0cSSage Weil /* handle the last 11 bytes */
491654dd0cSSage Weil c = c + length;
5018370b36SGustavo A. R. Silva switch (len) {
511654dd0cSSage Weil case 11:
521654dd0cSSage Weil c = c + ((__u32)k[10] << 24);
53*df561f66SGustavo A. R. Silva fallthrough;
541654dd0cSSage Weil case 10:
551654dd0cSSage Weil c = c + ((__u32)k[9] << 16);
56*df561f66SGustavo A. R. Silva fallthrough;
571654dd0cSSage Weil case 9:
581654dd0cSSage Weil c = c + ((__u32)k[8] << 8);
591654dd0cSSage Weil /* the first byte of c is reserved for the length */
60*df561f66SGustavo A. R. Silva fallthrough;
611654dd0cSSage Weil case 8:
621654dd0cSSage Weil b = b + ((__u32)k[7] << 24);
63*df561f66SGustavo A. R. Silva fallthrough;
641654dd0cSSage Weil case 7:
651654dd0cSSage Weil b = b + ((__u32)k[6] << 16);
66*df561f66SGustavo A. R. Silva fallthrough;
671654dd0cSSage Weil case 6:
681654dd0cSSage Weil b = b + ((__u32)k[5] << 8);
69*df561f66SGustavo A. R. Silva fallthrough;
701654dd0cSSage Weil case 5:
711654dd0cSSage Weil b = b + k[4];
72*df561f66SGustavo A. R. Silva fallthrough;
731654dd0cSSage Weil case 4:
741654dd0cSSage Weil a = a + ((__u32)k[3] << 24);
75*df561f66SGustavo A. R. Silva fallthrough;
761654dd0cSSage Weil case 3:
771654dd0cSSage Weil a = a + ((__u32)k[2] << 16);
78*df561f66SGustavo A. R. Silva fallthrough;
791654dd0cSSage Weil case 2:
801654dd0cSSage Weil a = a + ((__u32)k[1] << 8);
81*df561f66SGustavo A. R. Silva fallthrough;
821654dd0cSSage Weil case 1:
831654dd0cSSage Weil a = a + k[0];
841654dd0cSSage Weil /* case 0: nothing left to add */
851654dd0cSSage Weil }
861654dd0cSSage Weil mix(a, b, c);
871654dd0cSSage Weil
881654dd0cSSage Weil return c;
891654dd0cSSage Weil }
901654dd0cSSage Weil
911654dd0cSSage Weil /*
921654dd0cSSage Weil * linux dcache hash
931654dd0cSSage Weil */
ceph_str_hash_linux(const char * str,unsigned int length)9495c96174SEric Dumazet unsigned int ceph_str_hash_linux(const char *str, unsigned int length)
951654dd0cSSage Weil {
961654dd0cSSage Weil unsigned long hash = 0;
971654dd0cSSage Weil unsigned char c;
981654dd0cSSage Weil
99cfea1cf4SSage Weil while (length--) {
1001654dd0cSSage Weil c = *str++;
1011654dd0cSSage Weil hash = (hash + (c << 4) + (c >> 4)) * 11;
1021654dd0cSSage Weil }
1031654dd0cSSage Weil return hash;
1041654dd0cSSage Weil }
1051654dd0cSSage Weil
1061654dd0cSSage Weil
ceph_str_hash(int type,const char * s,unsigned int len)10795c96174SEric Dumazet unsigned int ceph_str_hash(int type, const char *s, unsigned int len)
1081654dd0cSSage Weil {
1091654dd0cSSage Weil switch (type) {
1101654dd0cSSage Weil case CEPH_STR_HASH_LINUX:
1111654dd0cSSage Weil return ceph_str_hash_linux(s, len);
1121654dd0cSSage Weil case CEPH_STR_HASH_RJENKINS:
1131654dd0cSSage Weil return ceph_str_hash_rjenkins(s, len);
1141654dd0cSSage Weil default:
1151654dd0cSSage Weil return -1;
1161654dd0cSSage Weil }
1171654dd0cSSage Weil }
1186c0f3af7SSage Weil EXPORT_SYMBOL(ceph_str_hash);
1191654dd0cSSage Weil
ceph_str_hash_name(int type)1201654dd0cSSage Weil const char *ceph_str_hash_name(int type)
1211654dd0cSSage Weil {
1221654dd0cSSage Weil switch (type) {
1231654dd0cSSage Weil case CEPH_STR_HASH_LINUX:
1241654dd0cSSage Weil return "linux";
1251654dd0cSSage Weil case CEPH_STR_HASH_RJENKINS:
1261654dd0cSSage Weil return "rjenkins";
1271654dd0cSSage Weil default:
1281654dd0cSSage Weil return "unknown";
1291654dd0cSSage Weil }
1301654dd0cSSage Weil }
1316c0f3af7SSage Weil EXPORT_SYMBOL(ceph_str_hash_name);
132