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, 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 explicitly. If this is not needed on other architectures, then
63 * we'll need to move the functions to architecture specific headers.
66 #include <linux/hash.h>
69 #define CFS_HASH_DEBUG_NONE 0
70 /** record hash depth and output to console when it's too deep,
71 * computing overhead is low but consume more memory */
72 #define CFS_HASH_DEBUG_1 1
73 /** expensive, check key validation */
74 #define CFS_HASH_DEBUG_2 2
76 #define CFS_HASH_DEBUG_LEVEL CFS_HASH_DEBUG_NONE
79 struct cfs_hash_lock_ops;
80 struct cfs_hash_hlist_ops;
83 rwlock_t rw; /**< rwlock */
84 spinlock_t spin; /**< spinlock */
88 * cfs_hash_bucket is a container of:
90 * - array of hash-head starting from hsb_head[0], hash-head can be one of
92 * . cfs_hash_head_dep_t
94 * . cfs_hash_dhead_dep_t
95 * which depends on requirement of user
96 * - some extra bytes (caller can require it while creating hash)
98 struct cfs_hash_bucket {
99 union cfs_hash_lock hsb_lock; /**< bucket lock */
100 __u32 hsb_count; /**< current entries */
101 __u32 hsb_version; /**< change version */
102 unsigned int hsb_index; /**< index of bucket */
103 int hsb_depmax; /**< max depth on bucket */
104 long hsb_head[0]; /**< hash-head array */
108 * cfs_hash bucket descriptor, it's normally in stack of caller
111 struct cfs_hash_bucket *bd_bucket; /**< address of bucket */
112 unsigned int bd_offset; /**< offset in bucket */
115 #define CFS_HASH_NAME_LEN 16 /**< default name length */
116 #define CFS_HASH_BIGNAME_LEN 64 /**< bigname for param tree */
118 #define CFS_HASH_BKT_BITS 3 /**< default bits of bucket */
119 #define CFS_HASH_BITS_MAX 30 /**< max bits of bucket */
120 #define CFS_HASH_BITS_MIN CFS_HASH_BKT_BITS
123 * common hash attributes.
127 * don't need any lock, caller will protect operations with it's
128 * own lock. With this flag:
129 * . CFS_HASH_NO_BKTLOCK, CFS_HASH_RW_BKTLOCK, CFS_HASH_SPIN_BKTLOCK
131 * . Some functions will be disabled with this flag, i.e:
132 * cfs_hash_for_each_empty, cfs_hash_rehash
134 CFS_HASH_NO_LOCK = 1 << 0,
135 /** no bucket lock, use one spinlock to protect the whole hash */
136 CFS_HASH_NO_BKTLOCK = 1 << 1,
137 /** rwlock to protect bucket */
138 CFS_HASH_RW_BKTLOCK = 1 << 2,
139 /** spinlock to protect bucket */
140 CFS_HASH_SPIN_BKTLOCK = 1 << 3,
141 /** always add new item to tail */
142 CFS_HASH_ADD_TAIL = 1 << 4,
143 /** hash-table doesn't have refcount on item */
144 CFS_HASH_NO_ITEMREF = 1 << 5,
145 /** big name for param-tree */
146 CFS_HASH_BIGNAME = 1 << 6,
147 /** track global count */
148 CFS_HASH_COUNTER = 1 << 7,
149 /** rehash item by new key */
150 CFS_HASH_REHASH_KEY = 1 << 8,
151 /** Enable dynamic hash resizing */
152 CFS_HASH_REHASH = 1 << 9,
153 /** can shrink hash-size */
154 CFS_HASH_SHRINK = 1 << 10,
155 /** assert hash is empty on exit */
156 CFS_HASH_ASSERT_EMPTY = 1 << 11,
157 /** record hlist depth */
158 CFS_HASH_DEPTH = 1 << 12,
160 * rehash is always scheduled in a different thread, so current
161 * change on hash table is non-blocking
163 CFS_HASH_NBLK_CHANGE = 1 << 13,
164 /** NB, we typed hs_flags as __u16, please change it
165 * if you need to extend >=16 flags */
168 /** most used attributes */
169 #define CFS_HASH_DEFAULT (CFS_HASH_RW_BKTLOCK | \
170 CFS_HASH_COUNTER | CFS_HASH_REHASH)
173 * cfs_hash is a hash-table implementation for general purpose, it can support:
174 * . two refcount modes
175 * hash-table with & without refcount
177 * nolock, one-spinlock, rw-bucket-lock, spin-bucket-lock
178 * . general operations
179 * lookup, add(add_tail or add_head), delete
183 * locked iteration and unlocked iteration
185 * support long name hash
187 * trace max searching depth
190 * When the htable grows or shrinks, a separate task (cfs_hash_rehash_worker)
191 * is spawned to handle the rehash in the background, it's possible that other
192 * processes can concurrently perform additions, deletions, and lookups
193 * without being blocked on rehash completion, because rehash will release
194 * the global wrlock for each bucket.
196 * rehash and iteration can't run at the same time because it's too tricky
197 * to keep both of them safe and correct.
198 * As they are relatively rare operations, so:
199 * . if iteration is in progress while we try to launch rehash, then
200 * it just giveup, iterator will launch rehash at the end.
201 * . if rehash is in progress while we try to iterate the hash table,
202 * then we just wait (shouldn't be very long time), anyway, nobody
203 * should expect iteration of whole hash-table to be non-blocking.
205 * During rehashing, a (key,object) pair may be in one of two buckets,
206 * depending on whether the worker task has yet to transfer the object
207 * to its new location in the table. Lookups and deletions need to search both
208 * locations; additions must take care to only insert into the new bucket.
212 /** serialize with rehash, or serialize all operations if
213 * the hash-table has CFS_HASH_NO_BKTLOCK */
214 union cfs_hash_lock hs_lock;
215 /** hash operations */
216 struct cfs_hash_ops *hs_ops;
217 /** hash lock operations */
218 struct cfs_hash_lock_ops *hs_lops;
219 /** hash list operations */
220 struct cfs_hash_hlist_ops *hs_hops;
221 /** hash buckets-table */
222 struct cfs_hash_bucket **hs_buckets;
223 /** total number of items on this hash-table */
225 /** hash flags, see cfs_hash_tag for detail */
227 /** # of extra-bytes for bucket, for user saving extended attributes */
228 __u16 hs_extra_bytes;
229 /** wants to iterate */
231 /** hash-table is dying */
233 /** current hash bits */
239 /** bits for rehash */
241 /** bits for each bucket */
243 /** resize min threshold */
245 /** resize max threshold */
248 __u32 hs_rehash_count;
249 /** # of iterators (caller of cfs_hash_for_each_*) */
251 /** rehash workitem */
252 cfs_workitem_t hs_rehash_wi;
253 /** refcount on this hash table */
254 atomic_t hs_refcount;
255 /** rehash buckets-table */
256 struct cfs_hash_bucket **hs_rehash_buckets;
257 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
258 /** serialize debug members */
259 spinlock_t hs_dep_lock;
261 unsigned int hs_dep_max;
262 /** id of the deepest bucket */
263 unsigned int hs_dep_bkt;
264 /** offset in the deepest bucket */
265 unsigned int hs_dep_off;
266 /** bits when we found the max depth */
267 unsigned int hs_dep_bits;
268 /** workitem to output max depth */
269 cfs_workitem_t hs_dep_wi;
271 /** name of htable */
275 typedef struct cfs_hash_lock_ops {
276 /** lock the hash table */
277 void (*hs_lock)(union cfs_hash_lock *lock, int exclusive);
278 /** unlock the hash table */
279 void (*hs_unlock)(union cfs_hash_lock *lock, int exclusive);
280 /** lock the hash bucket */
281 void (*hs_bkt_lock)(union cfs_hash_lock *lock, int exclusive);
282 /** unlock the hash bucket */
283 void (*hs_bkt_unlock)(union cfs_hash_lock *lock, int exclusive);
284 } cfs_hash_lock_ops_t;
286 typedef struct cfs_hash_hlist_ops {
287 /** return hlist_head of hash-head of @bd */
288 struct hlist_head *(*hop_hhead)(struct cfs_hash *hs, struct cfs_hash_bd *bd);
289 /** return hash-head size */
290 int (*hop_hhead_size)(struct cfs_hash *hs);
291 /** add @hnode to hash-head of @bd */
292 int (*hop_hnode_add)(struct cfs_hash *hs,
293 struct cfs_hash_bd *bd, struct hlist_node *hnode);
294 /** remove @hnode from hash-head of @bd */
295 int (*hop_hnode_del)(struct cfs_hash *hs,
296 struct cfs_hash_bd *bd, struct hlist_node *hnode);
297 } cfs_hash_hlist_ops_t;
299 typedef struct cfs_hash_ops {
300 /** return hashed value from @key */
301 unsigned (*hs_hash)(struct cfs_hash *hs, const void *key, unsigned mask);
302 /** return key address of @hnode */
303 void * (*hs_key)(struct hlist_node *hnode);
304 /** copy key from @hnode to @key */
305 void (*hs_keycpy)(struct hlist_node *hnode, void *key);
307 * compare @key with key of @hnode
308 * returns 1 on a match
310 int (*hs_keycmp)(const void *key, struct hlist_node *hnode);
311 /** return object address of @hnode, i.e: container_of(...hnode) */
312 void * (*hs_object)(struct hlist_node *hnode);
313 /** get refcount of item, always called with holding bucket-lock */
314 void (*hs_get)(struct cfs_hash *hs, struct hlist_node *hnode);
315 /** release refcount of item */
316 void (*hs_put)(struct cfs_hash *hs, struct hlist_node *hnode);
317 /** release refcount of item, always called with holding bucket-lock */
318 void (*hs_put_locked)(struct cfs_hash *hs, struct hlist_node *hnode);
319 /** it's called before removing of @hnode */
320 void (*hs_exit)(struct cfs_hash *hs, struct hlist_node *hnode);
323 /** total number of buckets in @hs */
324 #define CFS_HASH_NBKT(hs) \
325 (1U << ((hs)->hs_cur_bits - (hs)->hs_bkt_bits))
327 /** total number of buckets in @hs while rehashing */
328 #define CFS_HASH_RH_NBKT(hs) \
329 (1U << ((hs)->hs_rehash_bits - (hs)->hs_bkt_bits))
331 /** number of hlist for in bucket */
332 #define CFS_HASH_BKT_NHLIST(hs) (1U << (hs)->hs_bkt_bits)
334 /** total number of hlist in @hs */
335 #define CFS_HASH_NHLIST(hs) (1U << (hs)->hs_cur_bits)
337 /** total number of hlist in @hs while rehashing */
338 #define CFS_HASH_RH_NHLIST(hs) (1U << (hs)->hs_rehash_bits)
341 cfs_hash_with_no_lock(struct cfs_hash *hs)
343 /* caller will serialize all operations for this hash-table */
344 return (hs->hs_flags & CFS_HASH_NO_LOCK) != 0;
348 cfs_hash_with_no_bktlock(struct cfs_hash *hs)
350 /* no bucket lock, one single lock to protect the hash-table */
351 return (hs->hs_flags & CFS_HASH_NO_BKTLOCK) != 0;
355 cfs_hash_with_rw_bktlock(struct cfs_hash *hs)
357 /* rwlock to protect hash bucket */
358 return (hs->hs_flags & CFS_HASH_RW_BKTLOCK) != 0;
362 cfs_hash_with_spin_bktlock(struct cfs_hash *hs)
364 /* spinlock to protect hash bucket */
365 return (hs->hs_flags & CFS_HASH_SPIN_BKTLOCK) != 0;
369 cfs_hash_with_add_tail(struct cfs_hash *hs)
371 return (hs->hs_flags & CFS_HASH_ADD_TAIL) != 0;
375 cfs_hash_with_no_itemref(struct cfs_hash *hs)
377 /* hash-table doesn't keep refcount on item,
378 * item can't be removed from hash unless it's
380 return (hs->hs_flags & CFS_HASH_NO_ITEMREF) != 0;
384 cfs_hash_with_bigname(struct cfs_hash *hs)
386 return (hs->hs_flags & CFS_HASH_BIGNAME) != 0;
390 cfs_hash_with_counter(struct cfs_hash *hs)
392 return (hs->hs_flags & CFS_HASH_COUNTER) != 0;
396 cfs_hash_with_rehash(struct cfs_hash *hs)
398 return (hs->hs_flags & CFS_HASH_REHASH) != 0;
402 cfs_hash_with_rehash_key(struct cfs_hash *hs)
404 return (hs->hs_flags & CFS_HASH_REHASH_KEY) != 0;
408 cfs_hash_with_shrink(struct cfs_hash *hs)
410 return (hs->hs_flags & CFS_HASH_SHRINK) != 0;
414 cfs_hash_with_assert_empty(struct cfs_hash *hs)
416 return (hs->hs_flags & CFS_HASH_ASSERT_EMPTY) != 0;
420 cfs_hash_with_depth(struct cfs_hash *hs)
422 return (hs->hs_flags & CFS_HASH_DEPTH) != 0;
426 cfs_hash_with_nblk_change(struct cfs_hash *hs)
428 return (hs->hs_flags & CFS_HASH_NBLK_CHANGE) != 0;
432 cfs_hash_is_exiting(struct cfs_hash *hs)
433 { /* cfs_hash_destroy is called */
434 return hs->hs_exiting;
438 cfs_hash_is_rehashing(struct cfs_hash *hs)
439 { /* rehash is launched */
440 return hs->hs_rehash_bits != 0;
444 cfs_hash_is_iterating(struct cfs_hash *hs)
445 { /* someone is calling cfs_hash_for_each_* */
446 return hs->hs_iterating || hs->hs_iterators != 0;
450 cfs_hash_bkt_size(struct cfs_hash *hs)
452 return offsetof(struct cfs_hash_bucket, hsb_head[0]) +
453 hs->hs_hops->hop_hhead_size(hs) * CFS_HASH_BKT_NHLIST(hs) +
457 static inline unsigned
458 cfs_hash_id(struct cfs_hash *hs, const void *key, unsigned mask)
460 return hs->hs_ops->hs_hash(hs, key, mask);
464 cfs_hash_key(struct cfs_hash *hs, struct hlist_node *hnode)
466 return hs->hs_ops->hs_key(hnode);
470 cfs_hash_keycpy(struct cfs_hash *hs, struct hlist_node *hnode, void *key)
472 if (hs->hs_ops->hs_keycpy)
473 hs->hs_ops->hs_keycpy(hnode, key);
477 * Returns 1 on a match,
480 cfs_hash_keycmp(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
482 return hs->hs_ops->hs_keycmp(key, hnode);
486 cfs_hash_object(struct cfs_hash *hs, struct hlist_node *hnode)
488 return hs->hs_ops->hs_object(hnode);
492 cfs_hash_get(struct cfs_hash *hs, struct hlist_node *hnode)
494 return hs->hs_ops->hs_get(hs, hnode);
498 cfs_hash_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
500 return hs->hs_ops->hs_put_locked(hs, hnode);
504 cfs_hash_put(struct cfs_hash *hs, struct hlist_node *hnode)
506 return hs->hs_ops->hs_put(hs, hnode);
510 cfs_hash_exit(struct cfs_hash *hs, struct hlist_node *hnode)
512 if (hs->hs_ops->hs_exit)
513 hs->hs_ops->hs_exit(hs, hnode);
516 static inline void cfs_hash_lock(struct cfs_hash *hs, int excl)
518 hs->hs_lops->hs_lock(&hs->hs_lock, excl);
521 static inline void cfs_hash_unlock(struct cfs_hash *hs, int excl)
523 hs->hs_lops->hs_unlock(&hs->hs_lock, excl);
526 static inline int cfs_hash_dec_and_lock(struct cfs_hash *hs,
529 LASSERT(cfs_hash_with_no_bktlock(hs));
530 return atomic_dec_and_lock(condition, &hs->hs_lock.spin);
533 static inline void cfs_hash_bd_lock(struct cfs_hash *hs,
534 struct cfs_hash_bd *bd, int excl)
536 hs->hs_lops->hs_bkt_lock(&bd->bd_bucket->hsb_lock, excl);
539 static inline void cfs_hash_bd_unlock(struct cfs_hash *hs,
540 struct cfs_hash_bd *bd, int excl)
542 hs->hs_lops->hs_bkt_unlock(&bd->bd_bucket->hsb_lock, excl);
546 * operations on cfs_hash bucket (bd: bucket descriptor),
547 * they are normally for hash-table without rehash
549 void cfs_hash_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bd);
551 static inline void cfs_hash_bd_get_and_lock(struct cfs_hash *hs, const void *key,
552 struct cfs_hash_bd *bd, int excl)
554 cfs_hash_bd_get(hs, key, bd);
555 cfs_hash_bd_lock(hs, bd, excl);
558 static inline unsigned cfs_hash_bd_index_get(struct cfs_hash *hs, struct cfs_hash_bd *bd)
560 return bd->bd_offset | (bd->bd_bucket->hsb_index << hs->hs_bkt_bits);
563 static inline void cfs_hash_bd_index_set(struct cfs_hash *hs,
564 unsigned index, struct cfs_hash_bd *bd)
566 bd->bd_bucket = hs->hs_buckets[index >> hs->hs_bkt_bits];
567 bd->bd_offset = index & (CFS_HASH_BKT_NHLIST(hs) - 1U);
571 cfs_hash_bd_extra_get(struct cfs_hash *hs, struct cfs_hash_bd *bd)
573 return (void *)bd->bd_bucket +
574 cfs_hash_bkt_size(hs) - hs->hs_extra_bytes;
578 cfs_hash_bd_version_get(struct cfs_hash_bd *bd)
580 /* need hold cfs_hash_bd_lock */
581 return bd->bd_bucket->hsb_version;
585 cfs_hash_bd_count_get(struct cfs_hash_bd *bd)
587 /* need hold cfs_hash_bd_lock */
588 return bd->bd_bucket->hsb_count;
592 cfs_hash_bd_depmax_get(struct cfs_hash_bd *bd)
594 return bd->bd_bucket->hsb_depmax;
598 cfs_hash_bd_compare(struct cfs_hash_bd *bd1, struct cfs_hash_bd *bd2)
600 if (bd1->bd_bucket->hsb_index != bd2->bd_bucket->hsb_index)
601 return bd1->bd_bucket->hsb_index - bd2->bd_bucket->hsb_index;
603 if (bd1->bd_offset != bd2->bd_offset)
604 return bd1->bd_offset - bd2->bd_offset;
609 void cfs_hash_bd_add_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
610 struct hlist_node *hnode);
611 void cfs_hash_bd_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
612 struct hlist_node *hnode);
613 void cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
614 struct cfs_hash_bd *bd_new, struct hlist_node *hnode);
616 static inline int cfs_hash_bd_dec_and_lock(struct cfs_hash *hs, struct cfs_hash_bd *bd,
619 LASSERT(cfs_hash_with_spin_bktlock(hs));
620 return atomic_dec_and_lock(condition,
621 &bd->bd_bucket->hsb_lock.spin);
624 static inline struct hlist_head *cfs_hash_bd_hhead(struct cfs_hash *hs,
625 struct cfs_hash_bd *bd)
627 return hs->hs_hops->hop_hhead(hs, bd);
630 struct hlist_node *cfs_hash_bd_lookup_locked(struct cfs_hash *hs,
631 struct cfs_hash_bd *bd, const void *key);
632 struct hlist_node *cfs_hash_bd_peek_locked(struct cfs_hash *hs,
633 struct cfs_hash_bd *bd, const void *key);
634 struct hlist_node *cfs_hash_bd_findadd_locked(struct cfs_hash *hs,
635 struct cfs_hash_bd *bd, const void *key,
636 struct hlist_node *hnode,
638 struct hlist_node *cfs_hash_bd_finddel_locked(struct cfs_hash *hs,
639 struct cfs_hash_bd *bd, const void *key,
640 struct hlist_node *hnode);
643 * operations on cfs_hash bucket (bd: bucket descriptor),
644 * they are safe for hash-table with rehash
646 void cfs_hash_dual_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bds);
647 void cfs_hash_dual_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl);
648 void cfs_hash_dual_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl);
650 static inline void cfs_hash_dual_bd_get_and_lock(struct cfs_hash *hs, const void *key,
651 struct cfs_hash_bd *bds, int excl)
653 cfs_hash_dual_bd_get(hs, key, bds);
654 cfs_hash_dual_bd_lock(hs, bds, excl);
657 struct hlist_node *cfs_hash_dual_bd_lookup_locked(struct cfs_hash *hs,
658 struct cfs_hash_bd *bds,
660 struct hlist_node *cfs_hash_dual_bd_findadd_locked(struct cfs_hash *hs,
661 struct cfs_hash_bd *bds,
663 struct hlist_node *hnode,
665 struct hlist_node *cfs_hash_dual_bd_finddel_locked(struct cfs_hash *hs,
666 struct cfs_hash_bd *bds,
668 struct hlist_node *hnode);
670 /* Hash init/cleanup functions */
671 struct cfs_hash *cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
672 unsigned bkt_bits, unsigned extra_bytes,
673 unsigned min_theta, unsigned max_theta,
674 cfs_hash_ops_t *ops, unsigned flags);
676 struct cfs_hash *cfs_hash_getref(struct cfs_hash *hs);
677 void cfs_hash_putref(struct cfs_hash *hs);
679 /* Hash addition functions */
680 void cfs_hash_add(struct cfs_hash *hs, const void *key,
681 struct hlist_node *hnode);
682 int cfs_hash_add_unique(struct cfs_hash *hs, const void *key,
683 struct hlist_node *hnode);
684 void *cfs_hash_findadd_unique(struct cfs_hash *hs, const void *key,
685 struct hlist_node *hnode);
687 /* Hash deletion functions */
688 void *cfs_hash_del(struct cfs_hash *hs, const void *key, struct hlist_node *hnode);
689 void *cfs_hash_del_key(struct cfs_hash *hs, const void *key);
691 /* Hash lookup/for_each functions */
692 #define CFS_HASH_LOOP_HOG 1024
694 typedef int (*cfs_hash_for_each_cb_t)(struct cfs_hash *hs, struct cfs_hash_bd *bd,
695 struct hlist_node *node, void *data);
696 void *cfs_hash_lookup(struct cfs_hash *hs, const void *key);
697 void cfs_hash_for_each(struct cfs_hash *hs, cfs_hash_for_each_cb_t, void *data);
698 void cfs_hash_for_each_safe(struct cfs_hash *hs, cfs_hash_for_each_cb_t, void *data);
699 int cfs_hash_for_each_nolock(struct cfs_hash *hs,
700 cfs_hash_for_each_cb_t, void *data);
701 int cfs_hash_for_each_empty(struct cfs_hash *hs,
702 cfs_hash_for_each_cb_t, void *data);
703 void cfs_hash_for_each_key(struct cfs_hash *hs, const void *key,
704 cfs_hash_for_each_cb_t, void *data);
705 typedef int (*cfs_hash_cond_opt_cb_t)(void *obj, void *data);
706 void cfs_hash_cond_del(struct cfs_hash *hs, cfs_hash_cond_opt_cb_t, void *data);
708 void cfs_hash_hlist_for_each(struct cfs_hash *hs, unsigned hindex,
709 cfs_hash_for_each_cb_t, void *data);
710 int cfs_hash_is_empty(struct cfs_hash *hs);
711 __u64 cfs_hash_size_get(struct cfs_hash *hs);
714 * Rehash - Theta is calculated to be the average chained
715 * hash depth assuming a perfectly uniform hash function.
717 void cfs_hash_rehash_cancel_locked(struct cfs_hash *hs);
718 void cfs_hash_rehash_cancel(struct cfs_hash *hs);
719 int cfs_hash_rehash(struct cfs_hash *hs, int do_rehash);
720 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
721 void *new_key, struct hlist_node *hnode);
723 #if CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1
724 /* Validate hnode references the correct key */
726 cfs_hash_key_validate(struct cfs_hash *hs, const void *key,
727 struct hlist_node *hnode)
729 LASSERT(cfs_hash_keycmp(hs, key, hnode));
732 /* Validate hnode is in the correct bucket */
734 cfs_hash_bucket_validate(struct cfs_hash *hs, struct cfs_hash_bd *bd,
735 struct hlist_node *hnode)
737 struct cfs_hash_bd bds[2];
739 cfs_hash_dual_bd_get(hs, cfs_hash_key(hs, hnode), bds);
740 LASSERT(bds[0].bd_bucket == bd->bd_bucket ||
741 bds[1].bd_bucket == bd->bd_bucket);
744 #else /* CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1 */
747 cfs_hash_key_validate(struct cfs_hash *hs, const void *key,
748 struct hlist_node *hnode) {}
751 cfs_hash_bucket_validate(struct cfs_hash *hs, struct cfs_hash_bd *bd,
752 struct hlist_node *hnode) {}
754 #endif /* CFS_HASH_DEBUG_LEVEL */
756 #define CFS_HASH_THETA_BITS 10
757 #define CFS_HASH_MIN_THETA (1U << (CFS_HASH_THETA_BITS - 1))
758 #define CFS_HASH_MAX_THETA (1U << (CFS_HASH_THETA_BITS + 1))
760 /* Return integer component of theta */
761 static inline int __cfs_hash_theta_int(int theta)
763 return (theta >> CFS_HASH_THETA_BITS);
766 /* Return a fractional value between 0 and 999 */
767 static inline int __cfs_hash_theta_frac(int theta)
769 return ((theta * 1000) >> CFS_HASH_THETA_BITS) -
770 (__cfs_hash_theta_int(theta) * 1000);
773 static inline int __cfs_hash_theta(struct cfs_hash *hs)
775 return (atomic_read(&hs->hs_count) <<
776 CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
779 static inline void __cfs_hash_set_theta(struct cfs_hash *hs, int min, int max)
782 hs->hs_min_theta = (__u16)min;
783 hs->hs_max_theta = (__u16)max;
786 /* Generic debug formatting routines mainly for proc handler */
788 void cfs_hash_debug_header(struct seq_file *m);
789 void cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m);
792 * Generic djb2 hash algorithm for character arrays.
794 static inline unsigned
795 cfs_hash_djb2_hash(const void *key, size_t size, unsigned mask)
797 unsigned i, hash = 5381;
799 LASSERT(key != NULL);
801 for (i = 0; i < size; i++)
802 hash = hash * 33 + ((char *)key)[i];
804 return (hash & mask);
808 * Generic u32 hash algorithm.
810 static inline unsigned
811 cfs_hash_u32_hash(const __u32 key, unsigned mask)
813 return ((key * CFS_GOLDEN_RATIO_PRIME_32) & mask);
817 * Generic u64 hash algorithm.
819 static inline unsigned
820 cfs_hash_u64_hash(const __u64 key, unsigned mask)
822 return ((unsigned)(key * CFS_GOLDEN_RATIO_PRIME_64) & mask);
825 /** iterate over all buckets in @bds (array of struct cfs_hash_bd) */
826 #define cfs_hash_for_each_bd(bds, n, i) \
827 for (i = 0; i < n && (bds)[i].bd_bucket != NULL; i++)
829 /** iterate over all buckets of @hs */
830 #define cfs_hash_for_each_bucket(hs, bd, pos) \
832 pos < CFS_HASH_NBKT(hs) && \
833 ((bd)->bd_bucket = (hs)->hs_buckets[pos]) != NULL; pos++)
835 /** iterate over all hlist of bucket @bd */
836 #define cfs_hash_bd_for_each_hlist(hs, bd, hlist) \
837 for ((bd)->bd_offset = 0; \
838 (bd)->bd_offset < CFS_HASH_BKT_NHLIST(hs) && \
839 (hlist = cfs_hash_bd_hhead(hs, bd)) != NULL; \
842 /* !__LIBCFS__HASH_H__ */