4 * Copyright (C) 2002 by Theodore Ts'o
6 * This file is released under the GPL v2.
8 * This file may be redistributed under the terms of the GNU Public
13 #include <linux/jbd2.h>
14 #include <linux/sched.h>
15 #include <linux/ext4_fs.h>
16 #include <linux/cryptohash.h>
18 #define DELTA 0x9E3779B9
20 static void TEA_transform(__u32 buf[4], __u32 const in[])
23 __u32 b0 = buf[0], b1 = buf[1];
24 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
29 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
30 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
38 /* The old legacy hash */
39 static __u32 dx_hack_hash (const char *name, int len)
41 __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
43 __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
45 if (hash & 0x80000000) hash -= 0x7fffffff;
52 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
57 pad = (__u32)len | ((__u32)len << 8);
63 for (i=0; i < len; i++) {
66 val = msg[i] + (val << 8);
80 * Returns the hash of a filename. If len is 0 and name is NULL, then
81 * this function can be used to test whether or not a hash version is
84 * The seed is an 4 longword (32 bits) "secret" which can be used to
85 * uniquify a hash. If the seed is all zero's, then some default seed
88 * A particular hash version specifies whether or not the seed is
89 * represented, and whether or not the returned hash is 32 bits or 64
90 * bits. 32 bit hashes will return 0 for the minor hash.
92 int ext4fs_dirhash(const char *name, int len, struct dx_hash_info *hinfo)
100 /* Initialize the default seed for the hash checksum functions */
106 /* Check to see if the seed is all zero's */
108 for (i=0; i < 4; i++) {
113 memcpy(buf, hinfo->seed, sizeof(buf));
116 switch (hinfo->hash_version) {
118 hash = dx_hack_hash(name, len);
120 case DX_HASH_HALF_MD4:
123 str2hashbuf(p, len, in, 8);
124 half_md4_transform(buf, in);
134 str2hashbuf(p, len, in, 4);
135 TEA_transform(buf, in);
147 if (hash == (EXT4_HTREE_EOF << 1))
148 hash = (EXT4_HTREE_EOF-1) << 1;
150 hinfo->minor_hash = minor_hash;