George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 1 | #ifndef __LINUX_STRINGHASH_H |
| 2 | #define __LINUX_STRINGHASH_H |
| 3 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 4 | #include <linux/compiler.h> /* For __pure */ |
| 5 | #include <linux/types.h> /* For u32, u64 */ |
George Spelvin | 703b5fa | 2016-06-10 00:22:12 -0400 | [diff] [blame] | 6 | #include <linux/hash.h> |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 7 | |
| 8 | /* |
| 9 | * Routines for hashing strings of bytes to a 32-bit hash value. |
| 10 | * |
| 11 | * These hash functions are NOT GUARANTEED STABLE between kernel |
| 12 | * versions, architectures, or even repeated boots of the same kernel. |
| 13 | * (E.g. they may depend on boot-time hardware detection or be |
| 14 | * deliberately randomized.) |
| 15 | * |
| 16 | * They are also not intended to be secure against collisions caused by |
| 17 | * malicious inputs; much slower hash functions are required for that. |
| 18 | * |
| 19 | * They are optimized for pathname components, meaning short strings. |
| 20 | * Even if a majority of files have longer names, the dynamic profile of |
| 21 | * pathname components skews short due to short directory names. |
| 22 | * (E.g. /usr/lib/libsesquipedalianism.so.3.141.) |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * Version 1: one byte at a time. Example of use: |
| 27 | * |
| 28 | * unsigned long hash = init_name_hash; |
| 29 | * while (*p) |
| 30 | * hash = partial_name_hash(tolower(*p++), hash); |
| 31 | * hash = end_name_hash(hash); |
| 32 | * |
| 33 | * Although this is designed for bytes, fs/hfsplus/unicode.c |
| 34 | * abuses it to hash 16-bit values. |
| 35 | */ |
| 36 | |
| 37 | /* Hash courtesy of the R5 hash in reiserfs modulo sign bits */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 38 | #define init_name_hash(salt) (unsigned long)(salt) |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 39 | |
| 40 | /* partial hash update function. Assume roughly 4 bits per character */ |
| 41 | static inline unsigned long |
| 42 | partial_name_hash(unsigned long c, unsigned long prevhash) |
| 43 | { |
| 44 | return (prevhash + (c << 4) + (c >> 4)) * 11; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Finally: cut down the number of bits to a int value (and try to avoid |
George Spelvin | 703b5fa | 2016-06-10 00:22:12 -0400 | [diff] [blame] | 49 | * losing bits). This also has the property (wanted by the dcache) |
| 50 | * that the msbits make a good hash table index. |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 51 | */ |
| 52 | static inline unsigned long end_name_hash(unsigned long hash) |
| 53 | { |
George Spelvin | 703b5fa | 2016-06-10 00:22:12 -0400 | [diff] [blame] | 54 | return __hash_32((unsigned int)hash); |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Version 2: One word (32 or 64 bits) at a time. |
| 59 | * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h> |
| 60 | * exists, which describes major Linux platforms like x86 and ARM), then |
| 61 | * this computes a different hash function much faster. |
| 62 | * |
| 63 | * If not set, this falls back to a wrapper around the preceding. |
| 64 | */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 65 | extern unsigned int __pure full_name_hash(const void *salt, const char *, unsigned int); |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * A hash_len is a u64 with the hash of a string in the low |
| 69 | * half and the length in the high half. |
| 70 | */ |
| 71 | #define hashlen_hash(hashlen) ((u32)(hashlen)) |
| 72 | #define hashlen_len(hashlen) ((u32)((hashlen) >> 32)) |
| 73 | #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash)) |
| 74 | |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 75 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 76 | extern u64 __pure hashlen_string(const void *salt, const char *name); |
George Spelvin | fcfd2fb | 2016-05-20 08:41:37 -0400 | [diff] [blame] | 77 | |
George Spelvin | f4bcbe7 | 2016-05-20 07:26:00 -0400 | [diff] [blame] | 78 | #endif /* __LINUX_STRINGHASH_H */ |