Whamcloud - gitweb
1227ec873075edef37de3197025558b0dc0dc37d
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-hash.h
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 #ifndef __LIBCFS_LINUX_HASH_H__
24 #define __LIBCFS_LINUX_HASH_H__
25
26 #include <linux/dcache.h>
27 #include <linux/rhashtable.h>
28
29 u64 cfs_hashlen_string(const void *salt, const char *name);
30
31 #ifndef hashlen_hash
32 #define hashlen_hash(hashlen) ((u32)(hashlen))
33 #endif
34
35 #ifndef HAVE_STRINGHASH
36 #ifndef hashlen_create
37 #define hashlen_create(hash, len) ((u64)(len)<<32 | (u32)(hash))
38 #endif
39 #endif /* !HAVE_STRINGHASH */
40
41 #ifndef HAVE_RHASHTABLE_LOOKUP_GET_INSERT_FAST
42 /**
43  * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
44  * @ht:         hash table
45  * @obj:        pointer to hash head inside object
46  * @params:     hash table parameters
47  *
48  * Just like rhashtable_lookup_insert_fast(), but this function returns the
49  * object if it exists, NULL if it did not and the insertion was successful,
50  * and an ERR_PTR otherwise.
51  */
52 static inline void *rhashtable_lookup_get_insert_fast(
53         struct rhashtable *ht, struct rhash_head *obj,
54         const struct rhashtable_params params)
55 {
56         const char *key;
57         void *ret;
58         int rc;
59
60         rc = rhashtable_lookup_insert_fast(ht, obj, params);
61         switch (rc) {
62         case -EEXIST:
63                 key = rht_obj(ht, obj);
64                 ret = rhashtable_lookup_fast(ht, key, params);
65                 break;
66         case 0:
67                 ret = NULL;
68                 break;
69         default:
70                 ret = ERR_PTR(rc);
71                 break;
72         }
73         return ret;
74 }
75 #endif /* !HAVE_RHASHTABLE_LOOKUP_GET_INSERT_FAST */
76
77 #endif /* __LIBCFS_LINUX_MISC_H__ */