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