1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
7 #include "plugin_header.h"
12 #include <linux/types.h>
14 /* old rupasov (yura) hash */
15 static __u64
hash_rupasov(const unsigned char *name
/* name to hash */ ,
16 int len
/* @name's length */ )
24 assert("nikita-672", name
!= NULL
);
25 assert("nikita-673", len
>= 0);
27 for (pow
= 1, i
= 1; i
< len
; ++i
)
33 a
= (name
[0] - 48) * pow
;
35 for (i
= 1; i
< len
; ++i
) {
37 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
43 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
48 for (; i
< 256; ++i
) {
50 for (pow
= 1, j
= i
; j
< len
- 1; ++j
)
60 static __u64
hash_r5(const unsigned char *name
/* name to hash */ ,
61 int len UNUSED_ARG
/* @name's length */ )
65 assert("nikita-674", name
!= NULL
);
66 assert("nikita-675", len
>= 0);
77 /* Keyed 32-bit hash function using TEA in a Davis-Meyer function
79 Hi = E Mi(Hi-1) + Hi-1
81 (see Applied Cryptography, 2nd edition, p448).
83 Jeremy Fitzhardinge <jeremy@zip.com.au> 1998
85 Jeremy has agreed to the contents of reiserfs/README. -Hans
87 This code was blindly upgraded to __u64 by s/__u32/__u64/g.
89 static __u64
hash_tea(const unsigned char *name
/* name to hash */ ,
90 int len
/* @name's length */ )
92 __u64 k
[] = { 0x9464a485u
, 0x542e1a94u
, 0x3e846bffu
, 0xb75bcfc3u
};
94 __u64 h0
= k
[0], h1
= k
[1];
99 assert("nikita-676", name
!= NULL
);
100 assert("nikita-677", len
>= 0);
102 #define DELTA 0x9E3779B9u
103 #define FULLROUNDS 10 /* 32 is overkill, 16 is strong crypto */
104 #define PARTROUNDS 6 /* 6 gets complete mixing */
106 /* a, b, c, d - data; h0, h1 - accumulated hash */
107 #define TEACORE(rounds) \
119 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); \
120 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); \
127 pad
= (__u64
) len
| ((__u64
) len
<< 8);
131 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
132 16 | (__u64
) name
[3] << 24;
133 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
134 16 | (__u64
) name
[7] << 24;
135 c
= (__u64
) name
[8] | (__u64
) name
[9] << 8 | (__u64
) name
[10] <<
136 16 | (__u64
) name
[11] << 24;
137 d
= (__u64
) name
[12] | (__u64
) name
[13] << 8 | (__u64
) name
[14]
138 << 16 | (__u64
) name
[15] << 24;
151 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
152 16 | (__u64
) name
[3] << 24;
153 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
154 16 | (__u64
) name
[7] << 24;
155 c
= (__u64
) name
[8] | (__u64
) name
[9] << 8 | (__u64
) name
[10] <<
156 16 | (__u64
) name
[11] << 24;
159 for (i
= 12; i
< len
; i
++) {
163 } else if (len
>= 8) {
167 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
168 16 | (__u64
) name
[3] << 24;
169 b
= (__u64
) name
[4] | (__u64
) name
[5] << 8 | (__u64
) name
[6] <<
170 16 | (__u64
) name
[7] << 24;
173 for (i
= 8; i
< len
; i
++) {
177 } else if (len
>= 4) {
181 a
= (__u64
) name
[0] | (__u64
) name
[1] << 8 | (__u64
) name
[2] <<
182 16 | (__u64
) name
[3] << 24;
185 for (i
= 4; i
< len
; i
++) {
194 for (i
= 0; i
< len
; i
++) {
207 /* classical 64 bit Fowler/Noll/Vo-1 (FNV-1) hash.
209 See http://www.isthe.com/chongo/tech/comp/fnv/ for details.
213 FNV hashes are designed to be fast while maintaining a low collision
216 [This version also seems to preserve lexicographical order locally.]
218 FNV hash algorithms and source code have been released into the public
222 static __u64
hash_fnv1(const unsigned char *name
/* name to hash */ ,
223 int len UNUSED_ARG
/* @name's length */ )
225 unsigned long long a
= 0xcbf29ce484222325ull
;
226 const unsigned long long fnv_64_prime
= 0x100000001b3ull
;
228 assert("nikita-678", name
!= NULL
);
229 assert("nikita-679", len
>= 0);
231 /* FNV-1 hash each octet in the buffer */
232 for (; *name
; ++name
) {
233 /* multiply by the 32 bit FNV magic prime mod 2^64 */
235 /* xor the bottom with the current octet */
236 a
^= (unsigned long long)(*name
);
238 /* return our new hash value */
242 /* degenerate hash function used to simplify testing of non-unique key
244 static __u64
hash_deg(const unsigned char *name UNUSED_ARG
/* name to hash */ ,
245 int len UNUSED_ARG
/* @name's length */ )
247 return 0xc0c0c0c010101010ull
;
250 static int change_hash(struct inode
*inode
,
251 reiser4_plugin
* plugin
,
256 assert("nikita-3503", inode
!= NULL
);
257 assert("nikita-3504", plugin
!= NULL
);
259 assert("nikita-3505", is_reiser4_inode(inode
));
260 assert("nikita-3507", plugin
->h
.type_id
== REISER4_HASH_PLUGIN_TYPE
);
262 if (!plugin_of_group(inode_file_plugin(inode
), REISER4_DIRECTORY_FILE
))
263 return RETERR(-EINVAL
);
266 if (inode_hash_plugin(inode
) == NULL
||
267 inode_hash_plugin(inode
)->h
.id
!= plugin
->h
.id
) {
268 if (is_dir_empty(inode
) == 0)
269 result
= aset_set_unsafe(&reiser4_inode_data(inode
)->pset
,
272 result
= RETERR(-ENOTEMPTY
);
278 static reiser4_plugin_ops hash_plugin_ops
= {
283 .change
= change_hash
287 hash_plugin hash_plugins
[LAST_HASH_ID
] = {
288 [RUPASOV_HASH_ID
] = {
290 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
291 .id
= RUPASOV_HASH_ID
,
292 .pops
= &hash_plugin_ops
,
294 .desc
= "Original Yura's hash",
295 .linkage
= {NULL
, NULL
}
301 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
303 .pops
= &hash_plugin_ops
,
306 .linkage
= {NULL
, NULL
}
312 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
314 .pops
= &hash_plugin_ops
,
317 .linkage
= {NULL
, NULL
}
323 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
325 .pops
= &hash_plugin_ops
,
328 .linkage
= {NULL
, NULL
}
332 [DEGENERATE_HASH_ID
] = {
334 .type_id
= REISER4_HASH_PLUGIN_TYPE
,
335 .id
= DEGENERATE_HASH_ID
,
336 .pops
= &hash_plugin_ops
,
337 .label
= "degenerate hash",
338 .desc
= "Degenerate hash: only for testing",
339 .linkage
= {NULL
, NULL
}
347 c-indentation-style: "K&R"