Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / obdclass / hash.c
1 /*
2  * Copyright (C) 2002 by Theodore Ts'o
3  *
4  * This file is released under the GPL v2.
5  *
6  * This file may be redistributed under the terms of the GNU Public
7  * License.
8  */
9
10 /*
11  * obdclass/hash.c is copied from ldiskfs/hash.c.
12  * ldiskfs is used by server only.
13  * obdclass is shared by both client and server, it should not depend on ldiskfs.
14  */
15
16 #include <linux/fs.h>
17
18 #ifdef HAVE_EXT4_LDISKFS
19 #include <ldiskfs/ldiskfs_jbd2.h>
20 #else
21 #include <linux/jbd.h>
22 #endif
23
24 #include <linux/sched.h>
25 #ifdef HAVE_SERVER_SUPPORT
26
27 #ifdef HAVE_EXT4_LDISKFS
28 #include <ldiskfs/ldiskfs.h>
29 #else
30 #include <linux/ldiskfs_fs.h>
31 #endif
32
33 #else
34 # include <obd_class.h>
35 #endif
36
37 #define DELTA 0x9E3779B9
38
39
40 static void TEA_transform(__u32 buf[4], __u32 const in[])
41 {
42         __u32   sum = 0;
43         __u32   b0 = buf[0], b1 = buf[1];
44         __u32   a = in[0], b = in[1], c = in[2], d = in[3];
45         int     n = 16;
46
47         do {
48                 sum += DELTA;
49                 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
50                 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
51         } while(--n);
52
53         buf[0] += b0;
54         buf[1] += b1;
55 }
56
57 /* F, G and H are basic MD4 functions: selection, majority, parity */
58 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
59 #define G(x, y, z) (((x) & (y)) + (((x) ^ (y)) & (z)))
60 #define H(x, y, z) ((x) ^ (y) ^ (z))
61
62 /*
63  * The generic round function.  The application is so specific that
64  * we don't bother protecting all the arguments with parens, as is generally
65  * good macro practice, in favor of extra legibility.
66  * Rotation is separate from addition to prevent recomputation
67  */
68 #define ROUND(f, a, b, c, d, x, s)      \
69         (a += f(b, c, d) + x, a = (a << s) | (a >> (32-s)))
70 #define K1 0
71 #define K2 013240474631UL
72 #define K3 015666365641UL
73
74 /*
75  * Basic cut-down MD4 transform.  Returns only 32 bits of result.
76  */
77 static void halfMD4Transform (__u32 buf[4], __u32 const in[])
78 {
79         __u32   a = buf[0], b = buf[1], c = buf[2], d = buf[3];
80
81         /* Round 1 */
82         ROUND(F, a, b, c, d, in[0] + K1,  3);
83         ROUND(F, d, a, b, c, in[1] + K1,  7);
84         ROUND(F, c, d, a, b, in[2] + K1, 11);
85         ROUND(F, b, c, d, a, in[3] + K1, 19);
86         ROUND(F, a, b, c, d, in[4] + K1,  3);
87         ROUND(F, d, a, b, c, in[5] + K1,  7);
88         ROUND(F, c, d, a, b, in[6] + K1, 11);
89         ROUND(F, b, c, d, a, in[7] + K1, 19);
90
91         /* Round 2 */
92         ROUND(G, a, b, c, d, in[1] + K2,  3);
93         ROUND(G, d, a, b, c, in[3] + K2,  5);
94         ROUND(G, c, d, a, b, in[5] + K2,  9);
95         ROUND(G, b, c, d, a, in[7] + K2, 13);
96         ROUND(G, a, b, c, d, in[0] + K2,  3);
97         ROUND(G, d, a, b, c, in[2] + K2,  5);
98         ROUND(G, c, d, a, b, in[4] + K2,  9);
99         ROUND(G, b, c, d, a, in[6] + K2, 13);
100
101         /* Round 3 */
102         ROUND(H, a, b, c, d, in[3] + K3,  3);
103         ROUND(H, d, a, b, c, in[7] + K3,  9);
104         ROUND(H, c, d, a, b, in[2] + K3, 11);
105         ROUND(H, b, c, d, a, in[6] + K3, 15);
106         ROUND(H, a, b, c, d, in[1] + K3,  3);
107         ROUND(H, d, a, b, c, in[5] + K3,  9);
108         ROUND(H, c, d, a, b, in[0] + K3, 11);
109         ROUND(H, b, c, d, a, in[4] + K3, 15);
110
111         buf[0] += a;
112         buf[1] += b;
113         buf[2] += c;
114         buf[3] += d;
115 }
116
117 #undef ROUND
118 #undef F
119 #undef G
120 #undef H
121 #undef K1
122 #undef K2
123 #undef K3
124
125 /* The old legacy hash */
126 static __u32 dx_hack_hash (const char *name, int len)
127 {
128         __u32 hash0 = 0x12a3fe2d, hash1 = 0x37abe8f9;
129         while (len--) {
130                 __u32 hash = hash1 + (hash0 ^ (*name++ * 7152373));
131
132                 if (hash & 0x80000000) hash -= 0x7fffffff;
133                 hash1 = hash0;
134                 hash0 = hash;
135         }
136         return (hash0 << 1);
137 }
138
139 static void str2hashbuf(const char *msg, int len, __u32 *buf, int num)
140 {
141         __u32   pad, val;
142         int     i;
143
144         pad = (__u32)len | ((__u32)len << 8);
145         pad |= pad << 16;
146
147         val = pad;
148         if (len > num*4)
149                 len = num * 4;
150         for (i=0; i < len; i++) {
151                 if ((i % 4) == 0)
152                         val = pad;
153                 val = msg[i] + (val << 8);
154                 if ((i % 4) == 3) {
155                         *buf++ = val;
156                         val = pad;
157                         num--;
158                 }
159         }
160         if (--num >= 0)
161                 *buf++ = val;
162         while (--num >= 0)
163                 *buf++ = pad;
164 }
165
166 /*
167  * Returns the hash of a filename.  If len is 0 and name is NULL, then
168  * this function can be used to test whether or not a hash version is
169  * supported.
170  *
171  * The seed is an 4 longword (32 bits) "secret" which can be used to
172  * uniquify a hash.  If the seed is all zero's, then some default seed
173  * may be used.
174  *
175  * A particular hash version specifies whether or not the seed is
176  * represented, and whether or not the returned hash is 32 bits or 64
177  * bits.  32 bit hashes will return 0 for the minor hash.
178  */
179 int ldiskfsfs_dirhash(const char *name, int len, struct ldiskfs_dx_hash_info *hinfo)
180 {
181         __u32   hash;
182         __u32   minor_hash = 0;
183         const char      *p;
184         int             i;
185         __u32           in[8], buf[4];
186
187         /* Initialize the default seed for the hash checksum functions */
188         buf[0] = 0x67452301;
189         buf[1] = 0xefcdab89;
190         buf[2] = 0x98badcfe;
191         buf[3] = 0x10325476;
192
193         /* Check to see if the seed is all zero's */
194         if (hinfo->seed) {
195                 for (i=0; i < 4; i++) {
196                         if (hinfo->seed[i])
197                                 break;
198                 }
199                 if (i < 4)
200                         memcpy(buf, hinfo->seed, sizeof(buf));
201         }
202
203         switch (hinfo->hash_version) {
204         case LDISKFS_DX_HASH_LEGACY:
205                 hash = dx_hack_hash(name, len);
206                 break;
207         case LDISKFS_DX_HASH_HALF_MD4:
208                 p = name;
209                 while (len > 0) {
210                         str2hashbuf(p, len, in, 8);
211                         halfMD4Transform(buf, in);
212                         len -= 32;
213                         p += 32;
214                 }
215                 minor_hash = buf[2];
216                 hash = buf[1];
217                 break;
218         case LDISKFS_DX_HASH_TEA:
219                 p = name;
220                 while (len > 0) {
221                         str2hashbuf(p, len, in, 4);
222                         TEA_transform(buf, in);
223                         len -= 16;
224                         p += 16;
225                 }
226                 hash = buf[0];
227                 minor_hash = buf[1];
228                 break;
229         default:
230                 hinfo->hash = 0;
231                 return -1;
232         }
233         hash = hash & ~1;
234         if (hash == (LDISKFS_HTREE_EOF << 1))
235                 hash = (LDISKFS_HTREE_EOF-1) << 1;
236         hinfo->hash = hash;
237         hinfo->minor_hash = minor_hash;
238         return 0;
239 }