... so they can be used without the rest of <linux/dcache.h> The hashlen_* macros will make sense next patch.
Unified · Split
| @@ -10,6 +10,7 @@ · 9 unmodified lines | ||
| 10 | 10 | #include <linux/cache.h> |
| 11 | 11 | #include <linux/rcupdate.h> |
| 12 | 12 | #include <linux/lockref.h> |
| 13 | +#include <linux/stringhash.h> | |
| 13 | 14 | |
| 14 | 15 | struct path; |
| 15 | 16 | struct vfsmount; |
| @@ -52,9 +53,6 @@ struct qstr { · 36 unmodified lines | ||
| 52 | 53 | }; |
| 53 | 54 | |
| 54 | 55 | #define QSTR_INIT(n,l) { { { .len = l } }, .name = n } |
| 55 | −#define hashlen_hash(hashlen) ((u32) (hashlen)) | |
| 56 | −#define hashlen_len(hashlen) ((u32)((hashlen) >> 32)) | |
| 57 | −#define hashlen_create(hash,len) (((u64)(len)<<32)|(u32)(hash)) | |
| 58 | 56 | |
| 59 | 57 | struct dentry_stat_t { |
| 60 | 58 | long nr_dentry; |
| @@ -65,29 +63,6 @@ struct dentry_stat_t { · 4 unmodified lines | ||
| 65 | 63 | }; |
| 66 | 64 | extern struct dentry_stat_t dentry_stat; |
| 67 | 65 | |
| 68 | −/* Name hashing routines. Initial hash value */ | |
| 69 | −/* Hash courtesy of the R5 hash in reiserfs modulo sign bits */ | |
| 70 | −#define init_name_hash() 0 | |
| 71 | − | |
| 72 | −/* partial hash update function. Assume roughly 4 bits per character */ | |
| 73 | −static inline unsigned long | |
| 74 | −partial_name_hash(unsigned long c, unsigned long prevhash) | |
| 75 | −{ | |
| 76 | − return (prevhash + (c << 4) + (c >> 4)) * 11; | |
| 77 | −} | |
| 78 | − | |
| 79 | −/* | |
| 80 | − * Finally: cut down the number of bits to a int value (and try to avoid | |
| 81 | − * losing bits) | |
| 82 | − */ | |
| 83 | −static inline unsigned long end_name_hash(unsigned long hash) | |
| 84 | −{ | |
| 85 | − return (unsigned int) hash; | |
| 86 | −} | |
| 87 | − | |
| 88 | −/* Compute the hash for a name string. */ | |
| 89 | −extern unsigned int full_name_hash(const unsigned char *, unsigned int); | |
| 90 | − | |
| 91 | 66 | /* |
| 92 | 67 | * Try to keep struct dentry aligned on 64 byte cachelines (this will |
| 93 | 68 | * give reasonable cacheline footprint with larger lines without the |
| @@ -0,0 +1,72 @@ | ||
| 1 | +#ifndef __LINUX_STRINGHASH_H | |
| 2 | +#define __LINUX_STRINGHASH_H | |
| 3 | + | |
| 4 | +#include <linux/types.h> | |
| 5 | + | |
| 6 | +/* | |
| 7 | + * Routines for hashing strings of bytes to a 32-bit hash value. | |
| 8 | + * | |
| 9 | + * These hash functions are NOT GUARANTEED STABLE between kernel | |
| 10 | + * versions, architectures, or even repeated boots of the same kernel. | |
| 11 | + * (E.g. they may depend on boot-time hardware detection or be | |
| 12 | + * deliberately randomized.) | |
| 13 | + * | |
| 14 | + * They are also not intended to be secure against collisions caused by | |
| 15 | + * malicious inputs; much slower hash functions are required for that. | |
| 16 | + * | |
| 17 | + * They are optimized for pathname components, meaning short strings. | |
| 18 | + * Even if a majority of files have longer names, the dynamic profile of | |
| 19 | + * pathname components skews short due to short directory names. | |
| 20 | + * (E.g. /usr/lib/libsesquipedalianism.so.3.141.) | |
| 21 | + */ | |
| 22 | + | |
| 23 | +/* | |
| 24 | + * Version 1: one byte at a time. Example of use: | |
| 25 | + * | |
| 26 | + * unsigned long hash = init_name_hash; | |
| 27 | + * while (*p) | |
| 28 | + * hash = partial_name_hash(tolower(*p++), hash); | |
| 29 | + * hash = end_name_hash(hash); | |
| 30 | + * | |
| 31 | + * Although this is designed for bytes, fs/hfsplus/unicode.c | |
| 32 | + * abuses it to hash 16-bit values. | |
| 33 | + */ | |
| 34 | + | |
| 35 | +/* Hash courtesy of the R5 hash in reiserfs modulo sign bits */ | |
| 36 | +#define init_name_hash() 0 | |
| 37 | + | |
| 38 | +/* partial hash update function. Assume roughly 4 bits per character */ | |
| 39 | +static inline unsigned long | |
| 40 | +partial_name_hash(unsigned long c, unsigned long prevhash) | |
| 41 | +{ | |
| 42 | + return (prevhash + (c << 4) + (c >> 4)) * 11; | |
| 43 | +} | |
| 44 | + | |
| 45 | +/* | |
| 46 | + * Finally: cut down the number of bits to a int value (and try to avoid | |
| 47 | + * losing bits) | |
| 48 | + */ | |
| 49 | +static inline unsigned long end_name_hash(unsigned long hash) | |
| 50 | +{ | |
| 51 | + return (unsigned int)hash; | |
| 52 | +} | |
| 53 | + | |
| 54 | +/* | |
| 55 | + * Version 2: One word (32 or 64 bits) at a time. | |
| 56 | + * If CONFIG_DCACHE_WORD_ACCESS is defined (meaning <asm/word-at-a-time.h> | |
| 57 | + * exists, which describes major Linux platforms like x86 and ARM), then | |
| 58 | + * this computes a different hash function much faster. | |
| 59 | + * | |
| 60 | + * If not set, this falls back to a wrapper around the preceding. | |
| 61 | + */ | |
| 62 | +extern unsigned int full_name_hash(const unsigned char *, unsigned int); | |
| 63 | + | |
| 64 | +/* | |
| 65 | + * A hash_len is a u64 with the hash of a string in the low | |
| 66 | + * half and the length in the high half. | |
| 67 | + */ | |
| 68 | +#define hashlen_hash(hashlen) ((u32)(hashlen)) | |
| 69 | +#define hashlen_len(hashlen) ((u32)((hashlen) >> 32)) | |
| 70 | +#define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash)) | |
| 71 | + | |
| 72 | +#endif /* __LINUX_STRINGHASH_H */ | |