ext4_journal: forcibly flush data to disk when stop journalling.
[lwext4.git] / lwext4 / ext4_hash.c
1 /*
2  * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  *
4  * FreeBSD:
5  * Copyright (c) 2010, 2013 Zheng Liu <lz@freebsd.org>
6  * Copyright (c) 2012, Vyacheslav Matyushin
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31
32 /*
33  * The following notice applies to the code in ext2_half_md4():
34  *
35  * Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
36  *
37  * License to copy and use this software is granted provided that it
38  * is identified as the "RSA Data Security, Inc. MD4 Message-Digest
39  * Algorithm" in all material mentioning or referencing this software
40  * or this function.
41  *
42  * License is also granted to make and use derivative works provided
43  * that such works are identified as "derived from the RSA Data
44  * Security, Inc. MD4 Message-Digest Algorithm" in all material
45  * mentioning or referencing the derived work.
46  *
47  * RSA Data Security, Inc. makes no representations concerning either
48  * the merchantability of this software or the suitability of this
49  * software for any particular purpose. It is provided "as is"
50  * without express or implied warranty of any kind.
51  *
52  * These notices must be retained in any copies of any part of this
53  * documentation and/or software.
54  */
55
56 /** @addtogroup lwext4
57  * @{
58  */
59 /**
60  * @file  ext4_hash.c
61  * @brief Directory indexing hash functions.
62  */
63
64 #include "ext4_config.h"
65 #include "ext4_types.h"
66 #include "ext4_errno.h"
67
68 #include <string.h>
69
70 /* F, G, and H are MD4 functions */
71 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
72 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
73 #define H(x, y, z) ((x) ^ (y) ^ (z))
74
75 /* ROTATE_LEFT rotates x left n bits */
76 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
77
78 /*
79  * FF, GG, and HH are transformations for rounds 1, 2, and 3.
80  * Rotation is separated from addition to prevent recomputation.
81  */
82 #define FF(a, b, c, d, x, s)                                                   \
83         {                                                                      \
84                 (a) += F((b), (c), (d)) + (x);                                 \
85                 (a) = ROTATE_LEFT((a), (s));                                   \
86         \
87 }
88
89 #define GG(a, b, c, d, x, s)                                                   \
90         {                                                                      \
91                 (a) += G((b), (c), (d)) + (x) + (uint32_t)0x5A827999;          \
92                 (a) = ROTATE_LEFT((a), (s));                                   \
93         \
94 }
95
96 #define HH(a, b, c, d, x, s)                                                   \
97         {                                                                      \
98                 (a) += H((b), (c), (d)) + (x) + (uint32_t)0x6ED9EBA1;          \
99                 (a) = ROTATE_LEFT((a), (s));                                   \
100         \
101 }
102
103 /*
104  * MD4 basic transformation.  It transforms state based on block.
105  *
106  * This is a half md4 algorithm since Linux uses this algorithm for dir
107  * index.  This function is derived from the RSA Data Security, Inc. MD4
108  * Message-Digest Algorithm and was modified as necessary.
109  *
110  * The return value of this function is uint32_t in Linux, but actually we don't
111  * need to check this value, so in our version this function doesn't return any
112  * value.
113  */
114 static void ext2_half_md4(uint32_t hash[4], uint32_t data[8])
115 {
116         uint32_t a = hash[0], b = hash[1], c = hash[2], d = hash[3];
117
118         /* Round 1 */
119         FF(a, b, c, d, data[0], 3);
120         FF(d, a, b, c, data[1], 7);
121         FF(c, d, a, b, data[2], 11);
122         FF(b, c, d, a, data[3], 19);
123         FF(a, b, c, d, data[4], 3);
124         FF(d, a, b, c, data[5], 7);
125         FF(c, d, a, b, data[6], 11);
126         FF(b, c, d, a, data[7], 19);
127
128         /* Round 2 */
129         GG(a, b, c, d, data[1], 3);
130         GG(d, a, b, c, data[3], 5);
131         GG(c, d, a, b, data[5], 9);
132         GG(b, c, d, a, data[7], 13);
133         GG(a, b, c, d, data[0], 3);
134         GG(d, a, b, c, data[2], 5);
135         GG(c, d, a, b, data[4], 9);
136         GG(b, c, d, a, data[6], 13);
137
138         /* Round 3 */
139         HH(a, b, c, d, data[3], 3);
140         HH(d, a, b, c, data[7], 9);
141         HH(c, d, a, b, data[2], 11);
142         HH(b, c, d, a, data[6], 15);
143         HH(a, b, c, d, data[1], 3);
144         HH(d, a, b, c, data[5], 9);
145         HH(c, d, a, b, data[0], 11);
146         HH(b, c, d, a, data[4], 15);
147
148         hash[0] += a;
149         hash[1] += b;
150         hash[2] += c;
151         hash[3] += d;
152 }
153
154 /*
155  * Tiny Encryption Algorithm.
156  */
157 static void ext2_tea(uint32_t hash[4], uint32_t data[8])
158 {
159         uint32_t tea_delta = 0x9E3779B9;
160         uint32_t sum;
161         uint32_t x = hash[0], y = hash[1];
162         int n = 16;
163         int i = 1;
164
165         while (n-- > 0) {
166                 sum = i * tea_delta;
167                 x += ((y << 4) + data[0]) ^ (y + sum) ^ ((y >> 5) + data[1]);
168                 y += ((x << 4) + data[2]) ^ (x + sum) ^ ((x >> 5) + data[3]);
169                 i++;
170         }
171
172         hash[0] += x;
173         hash[1] += y;
174 }
175
176 static uint32_t ext2_legacy_hash(const char *name, int len, int unsigned_char)
177 {
178         uint32_t h0, h1 = 0x12A3FE2D, h2 = 0x37ABE8F9;
179         uint32_t multi = 0x6D22F5;
180         const unsigned char *uname = (const unsigned char *)name;
181         const signed char *sname = (const signed char *)name;
182         int val, i;
183
184         for (i = 0; i < len; i++) {
185                 if (unsigned_char)
186                         val = (unsigned int)*uname++;
187                 else
188                         val = (int)*sname++;
189
190                 h0 = h2 + (h1 ^ (val * multi));
191                 if (h0 & 0x80000000)
192                         h0 -= 0x7FFFFFFF;
193                 h2 = h1;
194                 h1 = h0;
195         }
196
197         return (h1 << 1);
198 }
199
200 static void ext2_prep_hashbuf(const char *src, uint32_t slen, uint32_t *dst,
201                               int dlen, int unsigned_char)
202 {
203         uint32_t padding = slen | (slen << 8) | (slen << 16) | (slen << 24);
204         uint32_t buf_val;
205         int len, i;
206         int buf_byte;
207         const unsigned char *ubuf = (const unsigned char *)src;
208         const signed char *sbuf = (const signed char *)src;
209
210         if (slen > (uint32_t)dlen)
211                 len = dlen;
212         else
213                 len = slen;
214
215         buf_val = padding;
216
217         for (i = 0; i < len; i++) {
218                 if (unsigned_char)
219                         buf_byte = (unsigned int)ubuf[i];
220                 else
221                         buf_byte = (int)sbuf[i];
222
223                 if ((i % 4) == 0)
224                         buf_val = padding;
225
226                 buf_val <<= 8;
227                 buf_val += buf_byte;
228
229                 if ((i % 4) == 3) {
230                         *dst++ = buf_val;
231                         dlen -= sizeof(uint32_t);
232                         buf_val = padding;
233                 }
234         }
235
236         dlen -= sizeof(uint32_t);
237         if (dlen >= 0)
238                 *dst++ = buf_val;
239
240         dlen -= sizeof(uint32_t);
241         while (dlen >= 0) {
242                 *dst++ = padding;
243                 dlen -= sizeof(uint32_t);
244         }
245 }
246
247 int ext2_htree_hash(const char *name, int len, const uint32_t *hash_seed,
248                     int hash_version, uint32_t *hash_major,
249                     uint32_t *hash_minor)
250 {
251         uint32_t hash[4];
252         uint32_t data[8];
253         uint32_t major = 0, minor = 0;
254         int unsigned_char = 0;
255
256         if (!name || !hash_major)
257                 return (-1);
258
259         if (len < 1 || len > 255)
260                 goto error;
261
262         hash[0] = 0x67452301;
263         hash[1] = 0xEFCDAB89;
264         hash[2] = 0x98BADCFE;
265         hash[3] = 0x10325476;
266
267         if (hash_seed)
268                 memcpy(hash, hash_seed, sizeof(hash));
269
270         switch (hash_version) {
271         case EXT2_HTREE_TEA_UNSIGNED:
272                 unsigned_char = 1;
273         case EXT2_HTREE_TEA:
274                 while (len > 0) {
275                         ext2_prep_hashbuf(name, len, data, 16, unsigned_char);
276                         ext2_tea(hash, data);
277                         len -= 16;
278                         name += 16;
279                 }
280                 major = hash[0];
281                 minor = hash[1];
282                 break;
283         case EXT2_HTREE_LEGACY_UNSIGNED:
284                 unsigned_char = 1;
285         case EXT2_HTREE_LEGACY:
286                 major = ext2_legacy_hash(name, len, unsigned_char);
287                 break;
288         case EXT2_HTREE_HALF_MD4_UNSIGNED:
289                 unsigned_char = 1;
290         case EXT2_HTREE_HALF_MD4:
291                 while (len > 0) {
292                         ext2_prep_hashbuf(name, len, data, 32, unsigned_char);
293                         ext2_half_md4(hash, data);
294                         len -= 32;
295                         name += 32;
296                 }
297                 major = hash[1];
298                 minor = hash[2];
299                 break;
300         default:
301                 goto error;
302         }
303
304         major &= ~1;
305         if (major == (EXT2_HTREE_EOF << 1))
306                 major = (EXT2_HTREE_EOF - 1) << 1;
307         *hash_major = major;
308         if (hash_minor)
309                 *hash_minor = minor;
310
311         return EOK;
312
313 error:
314         *hash_major = 0;
315         if (hash_minor)
316                 *hash_minor = 0;
317         return ENOTSUP;
318 }
319
320 /**
321  * @}
322  */