Blame view

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

f4bcbe792   George Spelvin   Pull out string h...
77
  #endif	/* __LINUX_STRINGHASH_H */