Blame view

fs/f2fs/hash.c 2.05 KB
0a8165d7c   Jaegeuk Kim   f2fs: adjust kern...
1
  /*
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
2
3
4
5
6
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
38
39
40
41
42
43
   * fs/f2fs/hash.c
   *
   * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   *             http://www.samsung.com/
   *
   * Portions of this code from linux/fs/ext3/hash.c
   *
   * Copyright (C) 2002 by Theodore Ts'o
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
  #include <linux/types.h>
  #include <linux/fs.h>
  #include <linux/f2fs_fs.h>
  #include <linux/cryptohash.h>
  #include <linux/pagemap.h>
  
  #include "f2fs.h"
  
  /*
   * Hashing code copied from ext3
   */
  #define DELTA 0x9E3779B9
  
  static void TEA_transform(unsigned int buf[4], unsigned int const in[])
  {
  	__u32 sum = 0;
  	__u32 b0 = buf[0], b1 = buf[1];
  	__u32 a = in[0], b = in[1], c = in[2], d = in[3];
  	int n = 16;
  
  	do {
  		sum += DELTA;
  		b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
  		b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
  	} while (--n);
  
  	buf[0] += b0;
  	buf[1] += b1;
  }
3304b5640   Jaegeuk Kim   f2fs: fix wrong c...
44
45
  static void str2hashbuf(const unsigned char *msg, size_t len,
  				unsigned int *buf, int num)
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  {
  	unsigned pad, val;
  	int i;
  
  	pad = (__u32)len | ((__u32)len << 8);
  	pad |= pad << 16;
  
  	val = pad;
  	if (len > num * 4)
  		len = num * 4;
  	for (i = 0; i < len; i++) {
  		if ((i % 4) == 0)
  			val = pad;
  		val = msg[i] + (val << 8);
  		if ((i % 4) == 3) {
  			*buf++ = val;
  			val = pad;
  			num--;
  		}
  	}
  	if (--num >= 0)
  		*buf++ = val;
  	while (--num >= 0)
  		*buf++ = pad;
  }
eee6160f2   Gu Zheng   f2fs: arguments c...
71
  f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info)
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
72
  {
2b50638de   Jaegeuk Kim   f2fs: clean up un...
73
  	__u32 hash;
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
74
  	f2fs_hash_t f2fs_hash;
3304b5640   Jaegeuk Kim   f2fs: fix wrong c...
75
  	const unsigned char *p;
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
76
  	__u32 in[8], buf[4];
3304b5640   Jaegeuk Kim   f2fs: fix wrong c...
77
  	const unsigned char *name = name_info->name;
eee6160f2   Gu Zheng   f2fs: arguments c...
78
  	size_t len = name_info->len;
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
79

eaa693f4d   Jaegeuk Kim   f2fs: introduce d...
80
  	if (is_dot_dotdot(name_info))
38e0abdcf   Namjae Jeon   f2fs: fix up f2fs...
81
  		return 0;
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
82
83
84
85
86
87
88
  	/* Initialize the default seed for the hash checksum functions */
  	buf[0] = 0x67452301;
  	buf[1] = 0xefcdab89;
  	buf[2] = 0x98badcfe;
  	buf[3] = 0x10325476;
  
  	p = name;
9836b8b94   Leon Romanovsky   f2fs: unify strin...
89
  	while (1) {
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
90
91
  		str2hashbuf(p, len, in, 4);
  		TEA_transform(buf, in);
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
92
  		p += 16;
9836b8b94   Leon Romanovsky   f2fs: unify strin...
93
94
95
  		if (len <= 16)
  			break;
  		len -= 16;
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
96
97
  	}
  	hash = buf[0];
25ca923b2   Jaegeuk Kim   f2fs: fix endian ...
98
  	f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
6b4ea0160   Jaegeuk Kim   f2fs: add core di...
99
100
  	return f2fs_hash;
  }