Whamcloud - gitweb
LU-8130 libcfs: prepare rhashtable support
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-hash.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 #define DEBUG_SUBSYSTEM S_LNET
24
25 #include <linux/module.h>
26 #ifdef HAVE_STRINGHASH
27 #include <linux/stringhash.h>
28 #else
29 #include <linux/dcache.h>
30 #endif
31 #include <linux/hash.h>
32
33 #include <libcfs/linux/linux-hash.h>
34
35 /* Return the "hash_len" (hash and length) of a null-terminated string */
36 /* The kernel equivalent is in fs/namei.c but for some strange reason
37  * RHEL7.5 stuck it in dax/super.c instead. This placement never existed
38  * upstream so to make life easier we just have the equavilent
39  */
40 u64 cfs_hashlen_string(const void *salt, const char *name)
41 {
42 #ifdef HAVE_FULL_NAME_HASH_3ARGS
43         unsigned long hash = init_name_hash(salt);
44 #else
45         unsigned long hash = init_name_hash();
46 #endif
47         unsigned long len = 0, c;
48
49         c = (unsigned char)*name;
50         while (c) {
51                 len++;
52                 hash = partial_name_hash(c, hash);
53                 c = (unsigned char)name[len];
54         }
55         return hashlen_create(end_name_hash(hash), len);
56 }
57 EXPORT_SYMBOL(cfs_hashlen_string);