4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/libcfs_hash.h
42 #ifndef __LIBCFS_HASH_H__
43 #define __LIBCFS_HASH_H__
45 * Knuth recommends primes in approximately golden ratio to the maximum
46 * integer representable by a machine word for multiplicative hashing.
47 * Chuck Lever verified the effectiveness of this technique:
48 * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
50 * These primes are chosen to be bit-sparse, that is operations on
51 * them can use shifts and additions instead of multiplications for
52 * machines where multiplications are slow.
54 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
55 #define CFS_GOLDEN_RATIO_PRIME_32 0x9e370001UL
56 /* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
57 #define CFS_GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001ULL
60 * Ideally we would use HAVE_HASH_LONG for this, but on linux we configure
61 * the linux kernel and user space at the same time, so we need to differentiate
62 * between them explicitely. If this is not needed on other architectures, then
63 * we'll need to move the functions to archi specific headers.
67 # include <linux/hash.h>
68 #else /* __KERNEL__ */
69 /* Fast hashing routine for a long.
70 (C) 2002 William Lee Irwin III, IBM */
72 # if BITS_PER_LONG == 32
73 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
74 # define CFS_GOLDEN_RATIO_PRIME CFS_GOLDEN_RATIO_PRIME_32
75 # elif BITS_PER_LONG == 64
76 /* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
77 # define CFS_GOLDEN_RATIO_PRIME CFS_GOLDEN_RATIO_PRIME_64
79 # error Define CFS_GOLDEN_RATIO_PRIME for your wordsize.
80 # endif /* BITS_PER_LONG == 64 */
82 static inline unsigned long hash_long(unsigned long val, unsigned int bits)
84 unsigned long hash = val;
86 # if BITS_PER_LONG == 64
87 /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
88 unsigned long n = hash;
101 # else /* BITS_PER_LONG == 64 */
102 /* On some cpus multiply is faster, on others gcc will do shifts */
103 hash *= CFS_GOLDEN_RATIO_PRIME;
104 # endif /* BITS_PER_LONG != 64 */
106 /* High bits are more random, so use them. */
107 return hash >> (BITS_PER_LONG - bits);
109 #endif /* !__KERNEL__ */
112 #define CFS_HASH_DEBUG_NONE 0
113 /** record hash depth and output to console when it's too deep,
114 * computing overhead is low but consume more memory */
115 #define CFS_HASH_DEBUG_1 1
116 /** expensive, check key validation */
117 #define CFS_HASH_DEBUG_2 2
119 #define CFS_HASH_DEBUG_LEVEL CFS_HASH_DEBUG_NONE
122 struct cfs_hash_lock_ops;
123 struct cfs_hash_hlist_ops;
126 rwlock_t rw; /**< rwlock */
127 spinlock_t spin; /**< spinlock */
131 * cfs_hash_bucket is a container of:
133 * - array of hash-head starting from hsb_head[0], hash-head can be one of
135 * . cfs_hash_head_dep_t
137 * . cfs_hash_dhead_dep_t
138 * which depends on requirement of user
139 * - some extra bytes (caller can require it while creating hash)
141 typedef struct cfs_hash_bucket {
142 cfs_hash_lock_t hsb_lock; /**< bucket lock */
143 __u32 hsb_count; /**< current entries */
144 __u32 hsb_version; /**< change version */
145 unsigned int hsb_index; /**< index of bucket */
146 int hsb_depmax; /**< max depth on bucket */
147 long hsb_head[0]; /**< hash-head array */
151 * cfs_hash bucket descriptor, it's normally in stack of caller
153 typedef struct cfs_hash_bd {
154 cfs_hash_bucket_t *bd_bucket; /**< address of bucket */
155 unsigned int bd_offset; /**< offset in bucket */
158 #define CFS_HASH_NAME_LEN 16 /**< default name length */
159 #define CFS_HASH_BIGNAME_LEN 64 /**< bigname for param tree */
161 #define CFS_HASH_BKT_BITS 3 /**< default bits of bucket */
162 #define CFS_HASH_BITS_MAX 30 /**< max bits of bucket */
163 #define CFS_HASH_BITS_MIN CFS_HASH_BKT_BITS
166 * common hash attributes.
170 * don't need any lock, caller will protect operations with it's
171 * own lock. With this flag:
172 * . CFS_HASH_NO_BKTLOCK, CFS_HASH_RW_BKTLOCK, CFS_HASH_SPIN_BKTLOCK
174 * . Some functions will be disabled with this flag, i.e:
175 * cfs_hash_for_each_empty, cfs_hash_rehash
177 CFS_HASH_NO_LOCK = 1 << 0,
178 /** no bucket lock, use one spinlock to protect the whole hash */
179 CFS_HASH_NO_BKTLOCK = 1 << 1,
180 /** rwlock to protect bucket */
181 CFS_HASH_RW_BKTLOCK = 1 << 2,
182 /** spinlcok to protect bucket */
183 CFS_HASH_SPIN_BKTLOCK = 1 << 3,
184 /** always add new item to tail */
185 CFS_HASH_ADD_TAIL = 1 << 4,
186 /** hash-table doesn't have refcount on item */
187 CFS_HASH_NO_ITEMREF = 1 << 5,
188 /** big name for param-tree */
189 CFS_HASH_BIGNAME = 1 << 6,
190 /** track global count */
191 CFS_HASH_COUNTER = 1 << 7,
192 /** rehash item by new key */
193 CFS_HASH_REHASH_KEY = 1 << 8,
194 /** Enable dynamic hash resizing */
195 CFS_HASH_REHASH = 1 << 9,
196 /** can shrink hash-size */
197 CFS_HASH_SHRINK = 1 << 10,
198 /** assert hash is empty on exit */
199 CFS_HASH_ASSERT_EMPTY = 1 << 11,
200 /** record hlist depth */
201 CFS_HASH_DEPTH = 1 << 12,
203 * rehash is always scheduled in a different thread, so current
204 * change on hash table is non-blocking
206 CFS_HASH_NBLK_CHANGE = 1 << 13,
207 /** NB, we typed hs_flags as __u16, please change it
208 * if you need to extend >=16 flags */
211 /** most used attributes */
212 #define CFS_HASH_DEFAULT (CFS_HASH_RW_BKTLOCK | \
213 CFS_HASH_COUNTER | CFS_HASH_REHASH)
216 * cfs_hash is a hash-table implementation for general purpose, it can support:
217 * . two refcount modes
218 * hash-table with & without refcount
220 * nolock, one-spinlock, rw-bucket-lock, spin-bucket-lock
221 * . general operations
222 * lookup, add(add_tail or add_head), delete
226 * locked iteration and unlocked iteration
228 * support long name hash
230 * trace max searching depth
233 * When the htable grows or shrinks, a separate task (cfs_hash_rehash_worker)
234 * is spawned to handle the rehash in the background, it's possible that other
235 * processes can concurrently perform additions, deletions, and lookups
236 * without being blocked on rehash completion, because rehash will release
237 * the global wrlock for each bucket.
239 * rehash and iteration can't run at the same time because it's too tricky
240 * to keep both of them safe and correct.
241 * As they are relatively rare operations, so:
242 * . if iteration is in progress while we try to launch rehash, then
243 * it just giveup, iterator will launch rehash at the end.
244 * . if rehash is in progress while we try to iterate the hash table,
245 * then we just wait (shouldn't be very long time), anyway, nobody
246 * should expect iteration of whole hash-table to be non-blocking.
248 * During rehashing, a (key,object) pair may be in one of two buckets,
249 * depending on whether the worker task has yet to transfer the object
250 * to its new location in the table. Lookups and deletions need to search both
251 * locations; additions must take care to only insert into the new bucket.
254 typedef struct cfs_hash {
255 /** serialize with rehash, or serialize all operations if
256 * the hash-table has CFS_HASH_NO_BKTLOCK */
257 cfs_hash_lock_t hs_lock;
258 /** hash operations */
259 struct cfs_hash_ops *hs_ops;
260 /** hash lock operations */
261 struct cfs_hash_lock_ops *hs_lops;
262 /** hash list operations */
263 struct cfs_hash_hlist_ops *hs_hops;
264 /** hash buckets-table */
265 cfs_hash_bucket_t **hs_buckets;
266 /** total number of items on this hash-table */
268 /** hash flags, see cfs_hash_tag for detail */
270 /** # of extra-bytes for bucket, for user saving extended attributes */
271 __u16 hs_extra_bytes;
272 /** wants to iterate */
274 /** hash-table is dying */
276 /** current hash bits */
282 /** bits for rehash */
284 /** bits for each bucket */
286 /** resize min threshold */
288 /** resize max threshold */
291 __u32 hs_rehash_count;
292 /** # of iterators (caller of cfs_hash_for_each_*) */
294 /** rehash workitem */
295 cfs_workitem_t hs_rehash_wi;
296 /** refcount on this hash table */
297 atomic_t hs_refcount;
298 /** rehash buckets-table */
299 cfs_hash_bucket_t **hs_rehash_buckets;
300 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
301 /** serialize debug members */
302 spinlock_t hs_dep_lock;
304 unsigned int hs_dep_max;
305 /** id of the deepest bucket */
306 unsigned int hs_dep_bkt;
307 /** offset in the deepest bucket */
308 unsigned int hs_dep_off;
309 /** bits when we found the max depth */
310 unsigned int hs_dep_bits;
311 /** workitem to output max depth */
312 cfs_workitem_t hs_dep_wi;
314 /** name of htable */
318 typedef struct cfs_hash_lock_ops {
319 /** lock the hash table */
320 void (*hs_lock)(cfs_hash_lock_t *lock, int exclusive);
321 /** unlock the hash table */
322 void (*hs_unlock)(cfs_hash_lock_t *lock, int exclusive);
323 /** lock the hash bucket */
324 void (*hs_bkt_lock)(cfs_hash_lock_t *lock, int exclusive);
325 /** unlock the hash bucket */
326 void (*hs_bkt_unlock)(cfs_hash_lock_t *lock, int exclusive);
327 } cfs_hash_lock_ops_t;
329 typedef struct cfs_hash_hlist_ops {
330 /** return hlist_head of hash-head of @bd */
331 struct hlist_head *(*hop_hhead)(cfs_hash_t *hs, cfs_hash_bd_t *bd);
332 /** return hash-head size */
333 int (*hop_hhead_size)(cfs_hash_t *hs);
334 /** add @hnode to hash-head of @bd */
335 int (*hop_hnode_add)(cfs_hash_t *hs, cfs_hash_bd_t *bd,
336 struct hlist_node *hnode);
337 /** remove @hnode from hash-head of @bd */
338 int (*hop_hnode_del)(cfs_hash_t *hs, cfs_hash_bd_t *bd,
339 struct hlist_node *hnode);
340 } cfs_hash_hlist_ops_t;
342 typedef struct cfs_hash_ops {
343 /** return hashed value from @key */
344 unsigned (*hs_hash)(cfs_hash_t *hs, const void *key, unsigned mask);
345 /** return key address of @hnode */
346 void * (*hs_key)(struct hlist_node *hnode);
347 /** copy key from @hnode to @key */
348 void (*hs_keycpy)(struct hlist_node *hnode, void *key);
350 * compare @key with key of @hnode
351 * returns 1 on a match
353 int (*hs_keycmp)(const void *key, struct hlist_node *hnode);
354 /** return object address of @hnode, i.e: container_of(...hnode) */
355 void * (*hs_object)(struct hlist_node *hnode);
356 /** get refcount of item, always called with holding bucket-lock */
357 void (*hs_get)(cfs_hash_t *hs, struct hlist_node *hnode);
358 /** release refcount of item */
359 void (*hs_put)(cfs_hash_t *hs, struct hlist_node *hnode);
360 /** release refcount of item, always called with holding bucket-lock */
361 void (*hs_put_locked)(cfs_hash_t *hs, struct hlist_node *hnode);
362 /** it's called before removing of @hnode */
363 void (*hs_exit)(cfs_hash_t *hs, struct hlist_node *hnode);
366 /** total number of buckets in @hs */
367 #define CFS_HASH_NBKT(hs) \
368 (1U << ((hs)->hs_cur_bits - (hs)->hs_bkt_bits))
370 /** total number of buckets in @hs while rehashing */
371 #define CFS_HASH_RH_NBKT(hs) \
372 (1U << ((hs)->hs_rehash_bits - (hs)->hs_bkt_bits))
374 /** number of hlist for in bucket */
375 #define CFS_HASH_BKT_NHLIST(hs) (1U << (hs)->hs_bkt_bits)
377 /** total number of hlist in @hs */
378 #define CFS_HASH_NHLIST(hs) (1U << (hs)->hs_cur_bits)
380 /** total number of hlist in @hs while rehashing */
381 #define CFS_HASH_RH_NHLIST(hs) (1U << (hs)->hs_rehash_bits)
384 cfs_hash_with_no_lock(cfs_hash_t *hs)
386 /* caller will serialize all operations for this hash-table */
387 return (hs->hs_flags & CFS_HASH_NO_LOCK) != 0;
391 cfs_hash_with_no_bktlock(cfs_hash_t *hs)
393 /* no bucket lock, one single lock to protect the hash-table */
394 return (hs->hs_flags & CFS_HASH_NO_BKTLOCK) != 0;
398 cfs_hash_with_rw_bktlock(cfs_hash_t *hs)
400 /* rwlock to protect hash bucket */
401 return (hs->hs_flags & CFS_HASH_RW_BKTLOCK) != 0;
405 cfs_hash_with_spin_bktlock(cfs_hash_t *hs)
407 /* spinlock to protect hash bucket */
408 return (hs->hs_flags & CFS_HASH_SPIN_BKTLOCK) != 0;
412 cfs_hash_with_add_tail(cfs_hash_t *hs)
414 return (hs->hs_flags & CFS_HASH_ADD_TAIL) != 0;
418 cfs_hash_with_no_itemref(cfs_hash_t *hs)
420 /* hash-table doesn't keep refcount on item,
421 * item can't be removed from hash unless it's
423 return (hs->hs_flags & CFS_HASH_NO_ITEMREF) != 0;
427 cfs_hash_with_bigname(cfs_hash_t *hs)
429 return (hs->hs_flags & CFS_HASH_BIGNAME) != 0;
433 cfs_hash_with_counter(cfs_hash_t *hs)
435 return (hs->hs_flags & CFS_HASH_COUNTER) != 0;
439 cfs_hash_with_rehash(cfs_hash_t *hs)
441 return (hs->hs_flags & CFS_HASH_REHASH) != 0;
445 cfs_hash_with_rehash_key(cfs_hash_t *hs)
447 return (hs->hs_flags & CFS_HASH_REHASH_KEY) != 0;
451 cfs_hash_with_shrink(cfs_hash_t *hs)
453 return (hs->hs_flags & CFS_HASH_SHRINK) != 0;
457 cfs_hash_with_assert_empty(cfs_hash_t *hs)
459 return (hs->hs_flags & CFS_HASH_ASSERT_EMPTY) != 0;
463 cfs_hash_with_depth(cfs_hash_t *hs)
465 return (hs->hs_flags & CFS_HASH_DEPTH) != 0;
469 cfs_hash_with_nblk_change(cfs_hash_t *hs)
471 return (hs->hs_flags & CFS_HASH_NBLK_CHANGE) != 0;
475 cfs_hash_is_exiting(cfs_hash_t *hs)
476 { /* cfs_hash_destroy is called */
477 return hs->hs_exiting;
481 cfs_hash_is_rehashing(cfs_hash_t *hs)
482 { /* rehash is launched */
483 return hs->hs_rehash_bits != 0;
487 cfs_hash_is_iterating(cfs_hash_t *hs)
488 { /* someone is calling cfs_hash_for_each_* */
489 return hs->hs_iterating || hs->hs_iterators != 0;
493 cfs_hash_bkt_size(cfs_hash_t *hs)
495 return offsetof(cfs_hash_bucket_t, hsb_head[0]) +
496 hs->hs_hops->hop_hhead_size(hs) * CFS_HASH_BKT_NHLIST(hs) +
500 #define CFS_HOP(hs, op) (hs)->hs_ops->hs_ ## op
502 static inline unsigned
503 cfs_hash_id(cfs_hash_t *hs, const void *key, unsigned mask)
505 return CFS_HOP(hs, hash)(hs, key, mask);
509 cfs_hash_key(cfs_hash_t *hs, struct hlist_node *hnode)
511 return CFS_HOP(hs, key)(hnode);
515 cfs_hash_keycpy(cfs_hash_t *hs, struct hlist_node *hnode, void *key)
517 if (CFS_HOP(hs, keycpy) != NULL)
518 CFS_HOP(hs, keycpy)(hnode, key);
522 * Returns 1 on a match,
525 cfs_hash_keycmp(cfs_hash_t *hs, const void *key, struct hlist_node *hnode)
527 return CFS_HOP(hs, keycmp)(key, hnode);
531 cfs_hash_object(cfs_hash_t *hs, struct hlist_node *hnode)
533 return CFS_HOP(hs, object)(hnode);
537 cfs_hash_get(cfs_hash_t *hs, struct hlist_node *hnode)
539 return CFS_HOP(hs, get)(hs, hnode);
543 cfs_hash_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
545 LASSERT(CFS_HOP(hs, put_locked) != NULL);
547 return CFS_HOP(hs, put_locked)(hs, hnode);
551 cfs_hash_put(cfs_hash_t *hs, struct hlist_node *hnode)
553 LASSERT(CFS_HOP(hs, put) != NULL);
555 return CFS_HOP(hs, put)(hs, hnode);
559 cfs_hash_exit(cfs_hash_t *hs, struct hlist_node *hnode)
561 if (CFS_HOP(hs, exit))
562 CFS_HOP(hs, exit)(hs, hnode);
565 static inline void cfs_hash_lock(cfs_hash_t *hs, int excl)
567 hs->hs_lops->hs_lock(&hs->hs_lock, excl);
570 static inline void cfs_hash_unlock(cfs_hash_t *hs, int excl)
572 hs->hs_lops->hs_unlock(&hs->hs_lock, excl);
575 static inline int cfs_hash_dec_and_lock(cfs_hash_t *hs,
578 LASSERT(cfs_hash_with_no_bktlock(hs));
579 return atomic_dec_and_lock(condition, &hs->hs_lock.spin);
582 static inline void cfs_hash_bd_lock(cfs_hash_t *hs,
583 cfs_hash_bd_t *bd, int excl)
585 hs->hs_lops->hs_bkt_lock(&bd->bd_bucket->hsb_lock, excl);
588 static inline void cfs_hash_bd_unlock(cfs_hash_t *hs,
589 cfs_hash_bd_t *bd, int excl)
591 hs->hs_lops->hs_bkt_unlock(&bd->bd_bucket->hsb_lock, excl);
595 * operations on cfs_hash bucket (bd: bucket descriptor),
596 * they are normally for hash-table without rehash
598 void cfs_hash_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bd);
600 static inline void cfs_hash_bd_get_and_lock(cfs_hash_t *hs, const void *key,
601 cfs_hash_bd_t *bd, int excl)
603 cfs_hash_bd_get(hs, key, bd);
604 cfs_hash_bd_lock(hs, bd, excl);
607 static inline unsigned cfs_hash_bd_index_get(cfs_hash_t *hs, cfs_hash_bd_t *bd)
609 return bd->bd_offset | (bd->bd_bucket->hsb_index << hs->hs_bkt_bits);
612 static inline void cfs_hash_bd_index_set(cfs_hash_t *hs,
613 unsigned index, cfs_hash_bd_t *bd)
615 bd->bd_bucket = hs->hs_buckets[index >> hs->hs_bkt_bits];
616 bd->bd_offset = index & (CFS_HASH_BKT_NHLIST(hs) - 1U);
620 cfs_hash_bd_extra_get(cfs_hash_t *hs, cfs_hash_bd_t *bd)
622 return (void *)bd->bd_bucket +
623 cfs_hash_bkt_size(hs) - hs->hs_extra_bytes;
627 cfs_hash_bd_version_get(cfs_hash_bd_t *bd)
629 /* need hold cfs_hash_bd_lock */
630 return bd->bd_bucket->hsb_version;
634 cfs_hash_bd_count_get(cfs_hash_bd_t *bd)
636 /* need hold cfs_hash_bd_lock */
637 return bd->bd_bucket->hsb_count;
641 cfs_hash_bd_depmax_get(cfs_hash_bd_t *bd)
643 return bd->bd_bucket->hsb_depmax;
647 cfs_hash_bd_compare(cfs_hash_bd_t *bd1, cfs_hash_bd_t *bd2)
649 if (bd1->bd_bucket->hsb_index != bd2->bd_bucket->hsb_index)
650 return bd1->bd_bucket->hsb_index - bd2->bd_bucket->hsb_index;
652 if (bd1->bd_offset != bd2->bd_offset)
653 return bd1->bd_offset - bd2->bd_offset;
658 void cfs_hash_bd_add_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
659 struct hlist_node *hnode);
660 void cfs_hash_bd_del_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
661 struct hlist_node *hnode);
662 void cfs_hash_bd_move_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd_old,
663 cfs_hash_bd_t *bd_new, struct hlist_node *hnode);
665 static inline int cfs_hash_bd_dec_and_lock(cfs_hash_t *hs, cfs_hash_bd_t *bd,
668 LASSERT(cfs_hash_with_spin_bktlock(hs));
669 return atomic_dec_and_lock(condition, &bd->bd_bucket->hsb_lock.spin);
672 static inline struct hlist_head *cfs_hash_bd_hhead(cfs_hash_t *hs,
675 return hs->hs_hops->hop_hhead(hs, bd);
678 struct hlist_node *cfs_hash_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
680 struct hlist_node *cfs_hash_bd_peek_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
682 struct hlist_node *cfs_hash_bd_findadd_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
684 struct hlist_node *hnode,
686 struct hlist_node *cfs_hash_bd_finddel_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
688 struct hlist_node *hnode);
691 * operations on cfs_hash bucket (bd: bucket descriptor),
692 * they are safe for hash-table with rehash
694 void cfs_hash_dual_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bds);
695 void cfs_hash_dual_bd_lock(cfs_hash_t *hs, cfs_hash_bd_t *bds, int excl);
696 void cfs_hash_dual_bd_unlock(cfs_hash_t *hs, cfs_hash_bd_t *bds, int excl);
698 static inline void cfs_hash_dual_bd_get_and_lock(cfs_hash_t *hs, const void *key,
699 cfs_hash_bd_t *bds, int excl)
701 cfs_hash_dual_bd_get(hs, key, bds);
702 cfs_hash_dual_bd_lock(hs, bds, excl);
706 cfs_hash_dual_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
709 cfs_hash_dual_bd_findadd_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
710 const void *key, struct hlist_node *hnode,
713 cfs_hash_dual_bd_finddel_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
714 const void *key, struct hlist_node *hnode);
716 /* Hash init/cleanup functions */
717 cfs_hash_t *cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
718 unsigned bkt_bits, unsigned extra_bytes,
719 unsigned min_theta, unsigned max_theta,
720 cfs_hash_ops_t *ops, unsigned flags);
722 cfs_hash_t *cfs_hash_getref(cfs_hash_t *hs);
723 void cfs_hash_putref(cfs_hash_t *hs);
725 /* Hash addition functions */
726 void cfs_hash_add(cfs_hash_t *hs, const void *key,
727 struct hlist_node *hnode);
728 int cfs_hash_add_unique(cfs_hash_t *hs, const void *key,
729 struct hlist_node *hnode);
730 void *cfs_hash_findadd_unique(cfs_hash_t *hs, const void *key,
731 struct hlist_node *hnode);
733 /* Hash deletion functions */
734 void *cfs_hash_del(cfs_hash_t *hs, const void *key, struct hlist_node *hnode);
735 void *cfs_hash_del_key(cfs_hash_t *hs, const void *key);
737 /* Hash lookup/for_each functions */
738 #define CFS_HASH_LOOP_HOG 1024
740 typedef int (*cfs_hash_for_each_cb_t)(cfs_hash_t *hs, cfs_hash_bd_t *bd,
741 struct hlist_node *node, void *data);
742 void *cfs_hash_lookup(cfs_hash_t *hs, const void *key);
743 void cfs_hash_for_each(cfs_hash_t *hs, cfs_hash_for_each_cb_t, void *data);
744 void cfs_hash_for_each_safe(cfs_hash_t *hs, cfs_hash_for_each_cb_t, void *data);
745 int cfs_hash_for_each_nolock(cfs_hash_t *hs, cfs_hash_for_each_cb_t,
747 int cfs_hash_for_each_empty(cfs_hash_t *hs, cfs_hash_for_each_cb_t,
749 void cfs_hash_for_each_key(cfs_hash_t *hs, const void *key,
750 cfs_hash_for_each_cb_t, void *data);
751 typedef int (*cfs_hash_cond_opt_cb_t)(void *obj, void *data);
752 void cfs_hash_cond_del(cfs_hash_t *hs, cfs_hash_cond_opt_cb_t, void *data);
754 void cfs_hash_hlist_for_each(cfs_hash_t *hs, unsigned hindex,
755 cfs_hash_for_each_cb_t, void *data);
756 int cfs_hash_is_empty(cfs_hash_t *hs);
757 __u64 cfs_hash_size_get(cfs_hash_t *hs);
760 * Rehash - Theta is calculated to be the average chained
761 * hash depth assuming a perfectly uniform hash funcion.
763 void cfs_hash_rehash_cancel_locked(cfs_hash_t *hs);
764 void cfs_hash_rehash_cancel(cfs_hash_t *hs);
765 int cfs_hash_rehash(cfs_hash_t *hs, int do_rehash);
766 void cfs_hash_rehash_key(cfs_hash_t *hs, const void *old_key,
767 void *new_key, struct hlist_node *hnode);
769 #if CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1
770 /* Validate hnode references the correct key */
772 cfs_hash_key_validate(cfs_hash_t *hs, const void *key,
773 struct hlist_node *hnode)
775 LASSERT(cfs_hash_keycmp(hs, key, hnode));
778 /* Validate hnode is in the correct bucket */
780 cfs_hash_bucket_validate(cfs_hash_t *hs, cfs_hash_bd_t *bd,
781 struct hlist_node *hnode)
783 cfs_hash_bd_t bds[2];
785 cfs_hash_dual_bd_get(hs, cfs_hash_key(hs, hnode), bds);
786 LASSERT(bds[0].bd_bucket == bd->bd_bucket ||
787 bds[1].bd_bucket == bd->bd_bucket);
790 #else /* CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1 */
793 cfs_hash_key_validate(cfs_hash_t *hs, const void *key,
794 struct hlist_node *hnode) {}
797 cfs_hash_bucket_validate(cfs_hash_t *hs, cfs_hash_bd_t *bd,
798 struct hlist_node *hnode) {}
800 #endif /* CFS_HASH_DEBUG_LEVEL */
802 #define CFS_HASH_THETA_BITS 10
803 #define CFS_HASH_MIN_THETA (1U << (CFS_HASH_THETA_BITS - 1))
804 #define CFS_HASH_MAX_THETA (1U << (CFS_HASH_THETA_BITS + 1))
806 /* Return integer component of theta */
807 static inline int __cfs_hash_theta_int(int theta)
809 return (theta >> CFS_HASH_THETA_BITS);
812 /* Return a fractional value between 0 and 999 */
813 static inline int __cfs_hash_theta_frac(int theta)
815 return ((theta * 1000) >> CFS_HASH_THETA_BITS) -
816 (__cfs_hash_theta_int(theta) * 1000);
819 static inline int __cfs_hash_theta(cfs_hash_t *hs)
821 return (atomic_read(&hs->hs_count) <<
822 CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
825 static inline void __cfs_hash_set_theta(cfs_hash_t *hs, int min, int max)
828 hs->hs_min_theta = (__u16)min;
829 hs->hs_max_theta = (__u16)max;
832 /* Generic debug formatting routines mainly for proc handler */
834 int cfs_hash_debug_header(struct seq_file *m);
835 int cfs_hash_debug_str(cfs_hash_t *hs, struct seq_file *m);
838 * Generic djb2 hash algorithm for character arrays.
840 static inline unsigned
841 cfs_hash_djb2_hash(const void *key, size_t size, unsigned mask)
843 unsigned i, hash = 5381;
845 LASSERT(key != NULL);
847 for (i = 0; i < size; i++)
848 hash = hash * 33 + ((char *)key)[i];
850 return (hash & mask);
854 * Generic u32 hash algorithm.
856 static inline unsigned
857 cfs_hash_u32_hash(const __u32 key, unsigned mask)
859 return ((key * CFS_GOLDEN_RATIO_PRIME_32) & mask);
863 * Generic u64 hash algorithm.
865 static inline unsigned
866 cfs_hash_u64_hash(const __u64 key, unsigned mask)
868 return ((unsigned)(key * CFS_GOLDEN_RATIO_PRIME_64) & mask);
871 /** iterate over all buckets in @bds (array of cfs_hash_bd_t) */
872 #define cfs_hash_for_each_bd(bds, n, i) \
873 for (i = 0; i < n && (bds)[i].bd_bucket != NULL; i++)
875 /** iterate over all buckets of @hs */
876 #define cfs_hash_for_each_bucket(hs, bd, pos) \
878 pos < CFS_HASH_NBKT(hs) && \
879 ((bd)->bd_bucket = (hs)->hs_buckets[pos]) != NULL; pos++)
881 /** iterate over all hlist of bucket @bd */
882 #define cfs_hash_bd_for_each_hlist(hs, bd, hlist) \
883 for ((bd)->bd_offset = 0; \
884 (bd)->bd_offset < CFS_HASH_BKT_NHLIST(hs) && \
885 (hlist = cfs_hash_bd_hhead(hs, bd)) != NULL; \
888 /* !__LIBCFS__HASH_H__ */