Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 1 | /* |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 2 | * fs/f2fs/hash.c |
| 3 | * |
| 4 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 5 | * http://www.samsung.com/ |
| 6 | * |
| 7 | * Portions of this code from linux/fs/ext3/hash.c |
| 8 | * |
| 9 | * Copyright (C) 2002 by Theodore Ts'o |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/f2fs_fs.h> |
| 18 | #include <linux/cryptohash.h> |
| 19 | #include <linux/pagemap.h> |
| 20 | |
| 21 | #include "f2fs.h" |
| 22 | |
| 23 | /* |
| 24 | * Hashing code copied from ext3 |
| 25 | */ |
| 26 | #define DELTA 0x9E3779B9 |
| 27 | |
| 28 | static void TEA_transform(unsigned int buf[4], unsigned int const in[]) |
| 29 | { |
| 30 | __u32 sum = 0; |
| 31 | __u32 b0 = buf[0], b1 = buf[1]; |
| 32 | __u32 a = in[0], b = in[1], c = in[2], d = in[3]; |
| 33 | int n = 16; |
| 34 | |
| 35 | do { |
| 36 | sum += DELTA; |
| 37 | b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); |
| 38 | b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); |
| 39 | } while (--n); |
| 40 | |
| 41 | buf[0] += b0; |
| 42 | buf[1] += b1; |
| 43 | } |
| 44 | |
Jaegeuk Kim | 3304b56 | 2014-08-29 00:26:50 -0700 | [diff] [blame] | 45 | static void str2hashbuf(const unsigned char *msg, size_t len, |
| 46 | unsigned int *buf, int num) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 47 | { |
| 48 | unsigned pad, val; |
| 49 | int i; |
| 50 | |
| 51 | pad = (__u32)len | ((__u32)len << 8); |
| 52 | pad |= pad << 16; |
| 53 | |
| 54 | val = pad; |
| 55 | if (len > num * 4) |
| 56 | len = num * 4; |
| 57 | for (i = 0; i < len; i++) { |
| 58 | if ((i % 4) == 0) |
| 59 | val = pad; |
| 60 | val = msg[i] + (val << 8); |
| 61 | if ((i % 4) == 3) { |
| 62 | *buf++ = val; |
| 63 | val = pad; |
| 64 | num--; |
| 65 | } |
| 66 | } |
| 67 | if (--num >= 0) |
| 68 | *buf++ = val; |
| 69 | while (--num >= 0) |
| 70 | *buf++ = pad; |
| 71 | } |
| 72 | |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 73 | f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 74 | { |
Jaegeuk Kim | 2b50638 | 2012-12-26 14:39:50 +0900 | [diff] [blame] | 75 | __u32 hash; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 76 | f2fs_hash_t f2fs_hash; |
Jaegeuk Kim | 3304b56 | 2014-08-29 00:26:50 -0700 | [diff] [blame] | 77 | const unsigned char *p; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 78 | __u32 in[8], buf[4]; |
Jaegeuk Kim | 3304b56 | 2014-08-29 00:26:50 -0700 | [diff] [blame] | 79 | const unsigned char *name = name_info->name; |
Gu Zheng | eee6160 | 2014-06-24 18:21:23 +0800 | [diff] [blame] | 80 | size_t len = name_info->len; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 81 | |
Jaegeuk Kim | eaa693f | 2015-04-26 00:15:29 -0700 | [diff] [blame] | 82 | if (is_dot_dotdot(name_info)) |
Namjae Jeon | 38e0abd | 2012-12-13 23:44:11 +0900 | [diff] [blame] | 83 | return 0; |
| 84 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 85 | /* Initialize the default seed for the hash checksum functions */ |
| 86 | buf[0] = 0x67452301; |
| 87 | buf[1] = 0xefcdab89; |
| 88 | buf[2] = 0x98badcfe; |
| 89 | buf[3] = 0x10325476; |
| 90 | |
| 91 | p = name; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 92 | while (1) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 93 | str2hashbuf(p, len, in, 4); |
| 94 | TEA_transform(buf, in); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 95 | p += 16; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 96 | if (len <= 16) |
| 97 | break; |
| 98 | len -= 16; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 99 | } |
| 100 | hash = buf[0]; |
Jaegeuk Kim | 25ca923 | 2012-11-28 16:12:41 +0900 | [diff] [blame] | 101 | f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 102 | return f2fs_hash; |
| 103 | } |