1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 * Portions of this code from linux/fs/ext3/hash.c
10 * Copyright (C) 2002 by Theodore Ts'o
12 #include <linux/types.h>
14 #include <linux/f2fs_fs.h>
15 #include <linux/cryptohash.h>
16 #include <linux/pagemap.h>
17 #include <linux/unicode.h>
22 * Hashing code copied from ext3
24 #define DELTA 0x9E3779B9
26 static void TEA_transform(unsigned int buf[4], unsigned int const in[])
29 __u32 b0 = buf[0], b1 = buf[1];
30 __u32 a = in[0], b = in[1], c = in[2], d = in[3];
35 b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b);
36 b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d);
43 static void str2hashbuf(const unsigned char *msg, size_t len,
44 unsigned int *buf, int num)
49 pad = (__u32)len | ((__u32)len << 8);
55 for (i = 0; i < len; i++) {
58 val = msg[i] + (val << 8);
71 static f2fs_hash_t __f2fs_dentry_hash(const struct qstr *name_info,
72 struct fscrypt_name *fname)
75 f2fs_hash_t f2fs_hash;
76 const unsigned char *p;
78 const unsigned char *name = name_info->name;
79 size_t len = name_info->len;
81 /* encrypted bigname case */
82 if (fname && !fname->disk_name.name)
83 return cpu_to_le32(fname->hash);
85 if (is_dot_dotdot(name_info))
88 /* Initialize the default seed for the hash checksum functions */
96 str2hashbuf(p, len, in, 4);
97 TEA_transform(buf, in);
104 f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
108 f2fs_hash_t f2fs_dentry_hash(const struct inode *dir,
109 const struct qstr *name_info, struct fscrypt_name *fname)
111 #ifdef CONFIG_UNICODE
112 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
113 const struct unicode_map *um = sbi->s_encoding;
118 if (!name_info->len || !IS_CASEFOLDED(dir))
121 buff = f2fs_kzalloc(sbi, sizeof(char) * PATH_MAX, GFP_KERNEL);
125 dlen = utf8_casefold(um, name_info, buff, PATH_MAX);
132 r = __f2fs_dentry_hash(&folded, fname);
139 return __f2fs_dentry_hash(name_info, fname);