Chao Yu | 7c1a000 | 2018-09-12 09:16:07 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0a8165d | 2012-11-29 13:28:09 +0900 | [diff] [blame] | 2 | /* |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 3 | * fs/f2fs/hash.c |
| 4 | * |
| 5 | * Copyright (c) 2012 Samsung Electronics Co., Ltd. |
| 6 | * http://www.samsung.com/ |
| 7 | * |
| 8 | * Portions of this code from linux/fs/ext3/hash.c |
| 9 | * |
| 10 | * Copyright (C) 2002 by Theodore Ts'o |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/f2fs_fs.h> |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 15 | #include <linux/pagemap.h> |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 16 | #include <linux/unicode.h> |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 17 | |
| 18 | #include "f2fs.h" |
| 19 | |
| 20 | /* |
| 21 | * Hashing code copied from ext3 |
| 22 | */ |
| 23 | #define DELTA 0x9E3779B9 |
| 24 | |
| 25 | static void TEA_transform(unsigned int buf[4], unsigned int const in[]) |
| 26 | { |
| 27 | __u32 sum = 0; |
| 28 | __u32 b0 = buf[0], b1 = buf[1]; |
| 29 | __u32 a = in[0], b = in[1], c = in[2], d = in[3]; |
| 30 | int n = 16; |
| 31 | |
| 32 | do { |
| 33 | sum += DELTA; |
| 34 | b0 += ((b1 << 4)+a) ^ (b1+sum) ^ ((b1 >> 5)+b); |
| 35 | b1 += ((b0 << 4)+c) ^ (b0+sum) ^ ((b0 >> 5)+d); |
| 36 | } while (--n); |
| 37 | |
| 38 | buf[0] += b0; |
| 39 | buf[1] += b1; |
| 40 | } |
| 41 | |
Jaegeuk Kim | 3304b56 | 2014-08-29 00:26:50 -0700 | [diff] [blame] | 42 | static void str2hashbuf(const unsigned char *msg, size_t len, |
| 43 | unsigned int *buf, int num) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 44 | { |
| 45 | unsigned pad, val; |
| 46 | int i; |
| 47 | |
| 48 | pad = (__u32)len | ((__u32)len << 8); |
| 49 | pad |= pad << 16; |
| 50 | |
| 51 | val = pad; |
| 52 | if (len > num * 4) |
| 53 | len = num * 4; |
| 54 | for (i = 0; i < len; i++) { |
| 55 | if ((i % 4) == 0) |
| 56 | val = pad; |
| 57 | val = msg[i] + (val << 8); |
| 58 | if ((i % 4) == 3) { |
| 59 | *buf++ = val; |
| 60 | val = pad; |
| 61 | num--; |
| 62 | } |
| 63 | } |
| 64 | if (--num >= 0) |
| 65 | *buf++ = val; |
| 66 | while (--num >= 0) |
| 67 | *buf++ = pad; |
| 68 | } |
| 69 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 70 | static u32 TEA_hash_name(const u8 *p, size_t len) |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 71 | { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 72 | __u32 in[8], buf[4]; |
Namjae Jeon | 38e0abd | 2012-12-13 23:44:11 +0900 | [diff] [blame] | 73 | |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 74 | /* Initialize the default seed for the hash checksum functions */ |
| 75 | buf[0] = 0x67452301; |
| 76 | buf[1] = 0xefcdab89; |
| 77 | buf[2] = 0x98badcfe; |
| 78 | buf[3] = 0x10325476; |
| 79 | |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 80 | while (1) { |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 81 | str2hashbuf(p, len, in, 4); |
| 82 | TEA_transform(buf, in); |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 83 | p += 16; |
Leon Romanovsky | 9836b8b | 2012-12-27 19:55:46 +0200 | [diff] [blame] | 84 | if (len <= 16) |
| 85 | break; |
| 86 | len -= 16; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 87 | } |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 88 | return buf[0] & ~F2FS_HASH_COL_BIT; |
Jaegeuk Kim | 6b4ea01 | 2012-11-14 16:59:04 +0900 | [diff] [blame] | 89 | } |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 90 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 91 | /* |
| 92 | * Compute @fname->hash. For all directories, @fname->disk_name must be set. |
| 93 | * For casefolded directories, @fname->usr_fname must be set, and also |
| 94 | * @fname->cf_name if the filename is valid Unicode. |
| 95 | */ |
| 96 | void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname) |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 97 | { |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 98 | const u8 *name = fname->disk_name.name; |
| 99 | size_t len = fname->disk_name.len; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 100 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 101 | WARN_ON_ONCE(!name); |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 102 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 103 | if (is_dot_dotdot(name, len)) { |
| 104 | fname->hash = 0; |
| 105 | return; |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 106 | } |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 107 | |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 108 | #ifdef CONFIG_UNICODE |
| 109 | if (IS_CASEFOLDED(dir)) { |
| 110 | /* |
| 111 | * If the casefolded name is provided, hash it instead of the |
| 112 | * on-disk name. If the casefolded name is *not* provided, that |
| 113 | * should only be because the name wasn't valid Unicode, so fall |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 114 | * back to treating the name as an opaque byte sequence. Note |
| 115 | * that to handle encrypted directories, the fallback must use |
| 116 | * usr_fname (plaintext) rather than disk_name (ciphertext). |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 117 | */ |
| 118 | WARN_ON_ONCE(!fname->usr_fname->name); |
| 119 | if (fname->cf_name.name) { |
| 120 | name = fname->cf_name.name; |
| 121 | len = fname->cf_name.len; |
| 122 | } else { |
| 123 | name = fname->usr_fname->name; |
| 124 | len = fname->usr_fname->len; |
| 125 | } |
Daniel Rosenberg | 7ad08a5 | 2020-11-19 06:09:04 +0000 | [diff] [blame] | 126 | if (IS_ENCRYPTED(dir)) { |
| 127 | struct qstr tmp = QSTR_INIT(name, len); |
| 128 | |
| 129 | fname->hash = |
| 130 | cpu_to_le32(fscrypt_fname_siphash(dir, &tmp)); |
| 131 | return; |
| 132 | } |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 133 | } |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 134 | #endif |
Eric Biggers | 43c780b | 2020-05-07 00:59:04 -0700 | [diff] [blame] | 135 | fname->hash = cpu_to_le32(TEA_hash_name(name, len)); |
Daniel Rosenberg | 2c2eb7a | 2019-07-23 16:05:29 -0700 | [diff] [blame] | 136 | } |