Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * libcfs/include/libcfs/libcfs_hash.h
35  *
36  * Hashing routines
37  *
38  */
39
40 #ifndef __LIBCFS_HASH_H__
41 #define __LIBCFS_HASH_H__
42 /*
43  * Knuth recommends primes in approximately golden ratio to the maximum
44  * integer representable by a machine word for multiplicative hashing.
45  * Chuck Lever verified the effectiveness of this technique:
46  * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
47  *
48  * These primes are chosen to be bit-sparse, that is operations on
49  * them can use shifts and additions instead of multiplications for
50  * machines where multiplications are slow.
51  */
52 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
53 #define CFS_GOLDEN_RATIO_PRIME_32 0x9e370001UL
54 /*  2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
55 #define CFS_GOLDEN_RATIO_PRIME_64 0x9e37fffffffc0001ULL
56
57 /*
58  * Ideally we would use HAVE_HASH_LONG for this, but on linux we configure
59  * the linux kernel and user space at the same time, so we need to differentiate
60  * between them explicitely. If this is not needed on other architectures, then
61  * we'll need to move the functions to archi specific headers.
62  */
63
64 #if (defined __linux__ && defined __KERNEL__)
65 #include <linux/hash.h>
66
67 #define cfs_hash_long(val, bits)    hash_long(val, bits)
68 #else
69 /* Fast hashing routine for a long.
70    (C) 2002 William Lee Irwin III, IBM */
71
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
78 #else
79 #error Define CFS_GOLDEN_RATIO_PRIME for your wordsize.
80 #endif
81
82 static inline unsigned long cfs_hash_long(unsigned long val, unsigned int bits)
83 {
84         unsigned long hash = val;
85
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;
89         n <<= 18;
90         hash -= n;
91         n <<= 33;
92         hash -= n;
93         n <<= 3;
94         hash += n;
95         n <<= 3;
96         hash -= n;
97         n <<= 4;
98         hash += n;
99         n <<= 2;
100         hash += n;
101 #else
102         /* On some cpus multiply is faster, on others gcc will do shifts */
103         hash *= CFS_GOLDEN_RATIO_PRIME;
104 #endif
105
106         /* High bits are more random, so use them. */
107         return hash >> (BITS_PER_LONG - bits);
108 }
109 #if 0
110 static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
111 {
112         return cfs_hash_long((unsigned long)ptr, bits);
113 }
114 #endif
115
116 /* !(__linux__ && __KERNEL__) */
117 #endif
118
119 /** disable debug */
120 #define CFS_HASH_DEBUG_NONE         0
121 /** record hash depth and output to console when it's too deep,
122  *  computing overhead is low but consume more memory */
123 #define CFS_HASH_DEBUG_1            1
124 /** expensive, check key validation */
125 #define CFS_HASH_DEBUG_2            2
126
127 #define CFS_HASH_DEBUG_LEVEL        CFS_HASH_DEBUG_NONE
128
129 struct cfs_hash_ops;
130 struct cfs_hash_lock_ops;
131 struct cfs_hash_hlist_ops;
132
133 typedef union {
134         cfs_rwlock_t                rw;             /**< rwlock */
135         cfs_spinlock_t              spin;           /**< spinlock */
136 } cfs_hash_lock_t;
137
138 /**
139  * cfs_hash_bucket is a container of:
140  * - lock, couter ...
141  * - array of hash-head starting from hsb_head[0], hash-head can be one of
142  *   . cfs_hash_head_t
143  *   . cfs_hash_head_dep_t
144  *   . cfs_hash_dhead_t
145  *   . cfs_hash_dhead_dep_t
146  *   which depends on requirement of user
147  * - some extra bytes (caller can require it while creating hash)
148  */
149 typedef struct cfs_hash_bucket {
150         cfs_hash_lock_t             hsb_lock;       /**< bucket lock */
151         __u32                       hsb_count;      /**< current entries */
152         __u32                       hsb_version;    /**< change version */
153         unsigned int                hsb_index;      /**< index of bucket */
154         int                         hsb_depmax;     /**< max depth on bucket */
155         char                        hsb_head[0];    /**< hash-head array */
156 } cfs_hash_bucket_t;
157
158 /**
159  * cfs_hash bucket descriptor, it's normally in stack of caller
160  */
161 typedef struct cfs_hash_bd {
162         cfs_hash_bucket_t          *bd_bucket;      /**< address of bucket */
163         unsigned int                bd_offset;      /**< offset in bucket */
164 } cfs_hash_bd_t;
165
166 #define CFS_HASH_NAME_LEN           16      /**< default name length */
167 #define CFS_HASH_BIGNAME_LEN        64      /**< bigname for param tree */
168
169 #define CFS_HASH_BKT_BITS           3       /**< default bits of bucket */
170 #define CFS_HASH_BITS_MAX           30      /**< max bits of bucket */
171 #define CFS_HASH_BITS_MIN           CFS_HASH_BKT_BITS
172
173 /**
174  * common hash attributes.
175  */
176 enum cfs_hash_tag {
177         /**
178          * don't need any lock, caller will protect operations with it's
179          * own lock. With this flag:
180          *  . CFS_HASH_NO_BKTLOCK, CFS_HASH_RW_BKTLOCK, CFS_HASH_SPIN_BKTLOCK
181          *    will be ignored.
182          *  . Some functions will be disabled with this flag, i.e:
183          *    cfs_hash_for_each_empty, cfs_hash_rehash
184          */
185         CFS_HASH_NO_LOCK        = 1 << 0,
186         /** no bucket lock, use one spinlock to protect the whole hash */
187         CFS_HASH_NO_BKTLOCK     = 1 << 1,
188         /** rwlock to protect bucket */
189         CFS_HASH_RW_BKTLOCK     = 1 << 2,
190         /** spinlcok to protect bucket */
191         CFS_HASH_SPIN_BKTLOCK   = 1 << 3,
192         /** always add new item to tail */
193         CFS_HASH_ADD_TAIL       = 1 << 4,
194         /** hash-table doesn't have refcount on item */
195         CFS_HASH_NO_ITEMREF     = 1 << 5,
196         /** big name for param-tree */
197         CFS_HASH_BIGNAME        = 1 << 6,
198         /** track global count */
199         CFS_HASH_COUNTER        = 1 << 7,
200         /** rehash item by new key */
201         CFS_HASH_REHASH_KEY     = 1 << 8,
202         /** Enable dynamic hash resizing */
203         CFS_HASH_REHASH         = 1 << 9,
204         /** can shrink hash-size */
205         CFS_HASH_SHRINK         = 1 << 10,
206         /** assert hash is empty on exit */
207         CFS_HASH_ASSERT_EMPTY   = 1 << 11,
208         /** record hlist depth */
209         CFS_HASH_DEPTH          = 1 << 12,
210         /**
211          * rehash is always scheduled in a different thread, so current
212          * change on hash table is non-blocking
213          */
214         CFS_HASH_NBLK_CHANGE    = 1 << 13,
215         /** NB, we typed hs_flags as  __u16, please change it
216          * if you need to extend >=16 flags */
217 };
218
219 /** most used attributes */
220 #define CFS_HASH_DEFAULT       (CFS_HASH_RW_BKTLOCK | \
221                                 CFS_HASH_COUNTER | CFS_HASH_REHASH)
222
223 /**
224  * cfs_hash is a hash-table implementation for general purpose, it can support:
225  *    . two refcount modes
226  *      hash-table with & without refcount
227  *    . four lock modes
228  *      nolock, one-spinlock, rw-bucket-lock, spin-bucket-lock
229  *    . general operations
230  *      lookup, add(add_tail or add_head), delete
231  *    . rehash
232  *      grows or shrink
233  *    . iteration
234  *      locked iteration and unlocked iteration
235  *    . bigname
236  *      support long name hash
237  *    . debug
238  *      trace max searching depth
239  *
240  * Rehash:
241  * When the htable grows or shrinks, a separate task (cfs_hash_rehash_worker)
242  * is spawned to handle the rehash in the background, it's possible that other
243  * processes can concurrently perform additions, deletions, and lookups
244  * without being blocked on rehash completion, because rehash will release
245  * the global wrlock for each bucket.
246  *
247  * rehash and iteration can't run at the same time because it's too tricky
248  * to keep both of them safe and correct.
249  * As they are relatively rare operations, so:
250  *   . if iteration is in progress while we try to launch rehash, then
251  *     it just giveup, iterator will launch rehash at the end.
252  *   . if rehash is in progress while we try to iterate the hash table,
253  *     then we just wait (shouldn't be very long time), anyway, nobody
254  *     should expect iteration of whole hash-table to be non-blocking.
255  *
256  * During rehashing, a (key,object) pair may be in one of two buckets,
257  * depending on whether the worker task has yet to transfer the object
258  * to its new location in the table. Lookups and deletions need to search both
259  * locations; additions must take care to only insert into the new bucket.
260  */
261
262 typedef struct cfs_hash {
263         /** serialize with rehash, or serialize all operations if
264          * the hash-table has CFS_HASH_NO_BKTLOCK */
265         cfs_hash_lock_t             hs_lock;
266         /** hash operations */
267         struct cfs_hash_ops        *hs_ops;
268         /** hash lock operations */
269         struct cfs_hash_lock_ops   *hs_lops;
270         /** hash list operations */
271         struct cfs_hash_hlist_ops  *hs_hops;
272         /** hash buckets-table */
273         cfs_hash_bucket_t         **hs_buckets;
274         /** total number of items on this hash-table */
275         cfs_atomic_t                hs_count;
276         /** hash flags, see cfs_hash_tag for detail */
277         __u16                       hs_flags;
278         /** # of extra-bytes for bucket, for user saving extended attributes */
279         __u16                       hs_extra_bytes;
280         /** wants to iterate */
281         __u8                        hs_iterating;
282         /** hash-table is dying */
283         __u8                        hs_exiting;
284         /** current hash bits */
285         __u8                        hs_cur_bits;
286         /** min hash bits */
287         __u8                        hs_min_bits;
288         /** max hash bits */
289         __u8                        hs_max_bits;
290         /** bits for rehash */
291         __u8                        hs_rehash_bits;
292         /** bits for each bucket */
293         __u8                        hs_bkt_bits;
294         /** resize min threshold */
295         __u16                       hs_min_theta;
296         /** resize max threshold */
297         __u16                       hs_max_theta;
298         /** resize count */
299         __u32                       hs_rehash_count;
300         /** # of iterators (caller of cfs_hash_for_each_*) */
301         __u32                       hs_iterators;
302         /** rehash workitem */
303         cfs_workitem_t              hs_rehash_wi;
304         /** refcount on this hash table */
305         cfs_atomic_t                hs_refcount;
306         /** rehash buckets-table */
307         cfs_hash_bucket_t         **hs_rehash_buckets;
308 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
309         /** serialize debug members */
310         cfs_spinlock_t              hs_dep_lock;
311         /** max depth */
312         unsigned int                hs_dep_max;
313         /** id of the deepest bucket */
314         unsigned int                hs_dep_bkt;
315         /** offset in the deepest bucket */
316         unsigned int                hs_dep_off;
317         /** bits when we found the max depth */
318         unsigned int                hs_dep_bits;
319         /** workitem to output max depth */
320         cfs_workitem_t              hs_dep_wi;
321 #endif
322         /** name of htable */
323         char                        hs_name[0];
324 } cfs_hash_t;
325
326 typedef struct cfs_hash_lock_ops {
327         /** lock the hash table */
328         void    (*hs_lock)(cfs_hash_lock_t *lock, int exclusive);
329         /** unlock the hash table */
330         void    (*hs_unlock)(cfs_hash_lock_t *lock, int exclusive);
331         /** lock the hash bucket */
332         void    (*hs_bkt_lock)(cfs_hash_lock_t *lock, int exclusive);
333         /** unlock the hash bucket */
334         void    (*hs_bkt_unlock)(cfs_hash_lock_t *lock, int exclusive);
335 } cfs_hash_lock_ops_t;
336
337 typedef struct cfs_hash_hlist_ops {
338         /** return hlist_head of hash-head of @bd */
339         cfs_hlist_head_t *(*hop_hhead)(cfs_hash_t *hs, cfs_hash_bd_t *bd);
340         /** return hash-head size */
341         int (*hop_hhead_size)(cfs_hash_t *hs);
342         /** add @hnode to hash-head of @bd */
343         int (*hop_hnode_add)(cfs_hash_t *hs,
344                              cfs_hash_bd_t *bd, cfs_hlist_node_t *hnode);
345         /** remove @hnode from hash-head of @bd */
346         int (*hop_hnode_del)(cfs_hash_t *hs,
347                              cfs_hash_bd_t *bd, cfs_hlist_node_t *hnode);
348 } cfs_hash_hlist_ops_t;
349
350 typedef struct cfs_hash_ops {
351         /** return hashed value from @key */
352         unsigned (*hs_hash)(cfs_hash_t *hs, const void *key, unsigned mask);
353         /** return key address of @hnode */
354         void *   (*hs_key)(cfs_hlist_node_t *hnode);
355         /** copy key from @hnode to @key */
356         void     (*hs_keycpy)(cfs_hlist_node_t *hnode, void *key);
357         /** compare @key with key of @hnode */
358         int      (*hs_keycmp)(const void *key, cfs_hlist_node_t *hnode);
359         /** return object address of @hnode, i.e: container_of(...hnode) */
360         void *   (*hs_object)(cfs_hlist_node_t *hnode);
361         /** get refcount of item, always called with holding bucket-lock */
362         void     (*hs_get)(cfs_hash_t *hs, cfs_hlist_node_t *hnode);
363         /** release refcount of item */
364         void     (*hs_put)(cfs_hash_t *hs, cfs_hlist_node_t *hnode);
365         /** release refcount of item, always called with holding bucket-lock */
366         void     (*hs_put_locked)(cfs_hash_t *hs, cfs_hlist_node_t *hnode);
367         /** it's called before removing of @hnode */
368         void     (*hs_exit)(cfs_hash_t *hs, cfs_hlist_node_t *hnode);
369 } cfs_hash_ops_t;
370
371 /** total number of buckets in @hs */
372 #define CFS_HASH_NBKT(hs)       \
373         (1U << ((hs)->hs_cur_bits - (hs)->hs_bkt_bits))
374
375 /** total number of buckets in @hs while rehashing */
376 #define CFS_HASH_RH_NBKT(hs)    \
377         (1U << ((hs)->hs_rehash_bits - (hs)->hs_bkt_bits))
378
379 /** number of hlist for in bucket */
380 #define CFS_HASH_BKT_NHLIST(hs) (1U << (hs)->hs_bkt_bits)
381
382 /** total number of hlist in @hs */
383 #define CFS_HASH_NHLIST(hs)     (1U << (hs)->hs_cur_bits)
384
385 /** total number of hlist in @hs while rehashing */
386 #define CFS_HASH_RH_NHLIST(hs)  (1U << (hs)->hs_rehash_bits)
387
388 static inline int
389 cfs_hash_with_no_lock(cfs_hash_t *hs)
390 {
391         /* caller will serialize all operations for this hash-table */
392         return (hs->hs_flags & CFS_HASH_NO_LOCK) != 0;
393 }
394
395 static inline int
396 cfs_hash_with_no_bktlock(cfs_hash_t *hs)
397 {
398         /* no bucket lock, one single lock to protect the hash-table */
399         return (hs->hs_flags & CFS_HASH_NO_BKTLOCK) != 0;
400 }
401
402 static inline int
403 cfs_hash_with_rw_bktlock(cfs_hash_t *hs)
404 {
405         /* rwlock to protect hash bucket */
406         return (hs->hs_flags & CFS_HASH_RW_BKTLOCK) != 0;
407 }
408
409 static inline int
410 cfs_hash_with_spin_bktlock(cfs_hash_t *hs)
411 {
412         /* spinlock to protect hash bucket */
413         return (hs->hs_flags & CFS_HASH_SPIN_BKTLOCK) != 0;
414 }
415
416 static inline int
417 cfs_hash_with_add_tail(cfs_hash_t *hs)
418 {
419         return (hs->hs_flags & CFS_HASH_ADD_TAIL) != 0;
420 }
421
422 static inline int
423 cfs_hash_with_no_itemref(cfs_hash_t *hs)
424 {
425         /* hash-table doesn't keep refcount on item,
426          * item can't be removed from hash unless it's
427          * ZERO refcount */
428         return (hs->hs_flags & CFS_HASH_NO_ITEMREF) != 0;
429 }
430
431 static inline int
432 cfs_hash_with_bigname(cfs_hash_t *hs)
433 {
434         return (hs->hs_flags & CFS_HASH_BIGNAME) != 0;
435 }
436
437 static inline int
438 cfs_hash_with_counter(cfs_hash_t *hs)
439 {
440         return (hs->hs_flags & CFS_HASH_COUNTER) != 0;
441 }
442
443 static inline int
444 cfs_hash_with_rehash(cfs_hash_t *hs)
445 {
446         return (hs->hs_flags & CFS_HASH_REHASH) != 0;
447 }
448
449 static inline int
450 cfs_hash_with_rehash_key(cfs_hash_t *hs)
451 {
452         return (hs->hs_flags & CFS_HASH_REHASH_KEY) != 0;
453 }
454
455 static inline int
456 cfs_hash_with_shrink(cfs_hash_t *hs)
457 {
458         return (hs->hs_flags & CFS_HASH_SHRINK) != 0;
459 }
460
461 static inline int
462 cfs_hash_with_assert_empty(cfs_hash_t *hs)
463 {
464         return (hs->hs_flags & CFS_HASH_ASSERT_EMPTY) != 0;
465 }
466
467 static inline int
468 cfs_hash_with_depth(cfs_hash_t *hs)
469 {
470         return (hs->hs_flags & CFS_HASH_DEPTH) != 0;
471 }
472
473 static inline int
474 cfs_hash_with_nblk_change(cfs_hash_t *hs)
475 {
476         return (hs->hs_flags & CFS_HASH_NBLK_CHANGE) != 0;
477 }
478
479 static inline int
480 cfs_hash_is_exiting(cfs_hash_t *hs)
481 {       /* cfs_hash_destroy is called */
482         return hs->hs_exiting;
483 }
484
485 static inline int
486 cfs_hash_is_rehashing(cfs_hash_t *hs)
487 {       /* rehash is launched */
488         return hs->hs_rehash_bits != 0;
489 }
490
491 static inline int
492 cfs_hash_is_iterating(cfs_hash_t *hs)
493 {       /* someone is calling cfs_hash_for_each_* */
494         return hs->hs_iterating || hs->hs_iterators != 0;
495 }
496
497 static inline int
498 cfs_hash_bkt_size(cfs_hash_t *hs)
499 {
500         return offsetof(cfs_hash_bucket_t, hsb_head[0]) +
501                hs->hs_hops->hop_hhead_size(hs) * CFS_HASH_BKT_NHLIST(hs) +
502                hs->hs_extra_bytes;
503 }
504
505 #define CFS_HOP(hs, op)           (hs)->hs_ops->hs_ ## op
506
507 static inline unsigned
508 cfs_hash_id(cfs_hash_t *hs, const void *key, unsigned mask)
509 {
510         return CFS_HOP(hs, hash)(hs, key, mask);
511 }
512
513 static inline void *
514 cfs_hash_key(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
515 {
516         return CFS_HOP(hs, key)(hnode);
517 }
518
519 static inline void
520 cfs_hash_keycpy(cfs_hash_t *hs, cfs_hlist_node_t *hnode, void *key)
521 {
522         if (CFS_HOP(hs, keycpy) != NULL)
523                 CFS_HOP(hs, keycpy)(hnode, key);
524 }
525
526 /**
527  * Returns 1 on a match,
528  */
529 static inline int
530 cfs_hash_keycmp(cfs_hash_t *hs, const void *key, cfs_hlist_node_t *hnode)
531 {
532         return CFS_HOP(hs, keycmp)(key, hnode);
533 }
534
535 static inline void *
536 cfs_hash_object(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
537 {
538         return CFS_HOP(hs, object)(hnode);
539 }
540
541 static inline void
542 cfs_hash_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
543 {
544         return CFS_HOP(hs, get)(hs, hnode);
545 }
546
547 static inline void
548 cfs_hash_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
549 {
550         LASSERT(CFS_HOP(hs, put_locked) != NULL);
551
552         return CFS_HOP(hs, put_locked)(hs, hnode);
553 }
554
555 static inline void
556 cfs_hash_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
557 {
558         LASSERT(CFS_HOP(hs, put) != NULL);
559
560         return CFS_HOP(hs, put)(hs, hnode);
561 }
562
563 static inline void
564 cfs_hash_exit(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
565 {
566         if (CFS_HOP(hs, exit))
567                 CFS_HOP(hs, exit)(hs, hnode);
568 }
569
570 static inline void cfs_hash_lock(cfs_hash_t *hs, int excl)
571 {
572         hs->hs_lops->hs_lock(&hs->hs_lock, excl);
573 }
574
575 static inline void cfs_hash_unlock(cfs_hash_t *hs, int excl)
576 {
577         hs->hs_lops->hs_unlock(&hs->hs_lock, excl);
578 }
579
580 static inline int cfs_hash_dec_and_lock(cfs_hash_t *hs,
581                                         cfs_atomic_t *condition)
582 {
583         LASSERT(cfs_hash_with_no_bktlock(hs));
584         return cfs_atomic_dec_and_lock(condition, &hs->hs_lock.spin);
585 }
586
587 static inline void cfs_hash_bd_lock(cfs_hash_t *hs,
588                                     cfs_hash_bd_t *bd, int excl)
589 {
590         hs->hs_lops->hs_bkt_lock(&bd->bd_bucket->hsb_lock, excl);
591 }
592
593 static inline void cfs_hash_bd_unlock(cfs_hash_t *hs,
594                                       cfs_hash_bd_t *bd, int excl)
595 {
596         hs->hs_lops->hs_bkt_unlock(&bd->bd_bucket->hsb_lock, excl);
597 }
598
599 /**
600  * operations on cfs_hash bucket (bd: bucket descriptor),
601  * they are normally for hash-table without rehash
602  */
603 void cfs_hash_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bd);
604
605 static inline void cfs_hash_bd_get_and_lock(cfs_hash_t *hs, const void *key,
606                                             cfs_hash_bd_t *bd, int excl)
607 {
608         cfs_hash_bd_get(hs, key, bd);
609         cfs_hash_bd_lock(hs, bd, excl);
610 }
611
612 static inline unsigned cfs_hash_bd_index_get(cfs_hash_t *hs, cfs_hash_bd_t *bd)
613 {
614         return bd->bd_offset | (bd->bd_bucket->hsb_index << hs->hs_bkt_bits);
615 }
616
617 static inline void cfs_hash_bd_index_set(cfs_hash_t *hs,
618                                          unsigned index, cfs_hash_bd_t *bd)
619 {
620         bd->bd_bucket = hs->hs_buckets[index >> hs->hs_bkt_bits];
621         bd->bd_offset = index & (CFS_HASH_BKT_NHLIST(hs) - 1U);
622 }
623
624 static inline void *
625 cfs_hash_bd_extra_get(cfs_hash_t *hs, cfs_hash_bd_t *bd)
626 {
627         return (void *)bd->bd_bucket +
628                cfs_hash_bkt_size(hs) - hs->hs_extra_bytes;
629 }
630
631 static inline __u32
632 cfs_hash_bd_version_get(cfs_hash_bd_t *bd)
633 {
634         /* need hold cfs_hash_bd_lock */
635         return bd->bd_bucket->hsb_version;
636 }
637
638 static inline __u32
639 cfs_hash_bd_count_get(cfs_hash_bd_t *bd)
640 {
641         /* need hold cfs_hash_bd_lock */
642         return bd->bd_bucket->hsb_count;
643 }
644
645 static inline int
646 cfs_hash_bd_depmax_get(cfs_hash_bd_t *bd)
647 {
648         return bd->bd_bucket->hsb_depmax;
649 }
650
651 static inline int
652 cfs_hash_bd_compare(cfs_hash_bd_t *bd1, cfs_hash_bd_t *bd2)
653 {
654         if (bd1->bd_bucket->hsb_index != bd2->bd_bucket->hsb_index)
655                 return bd1->bd_bucket->hsb_index - bd2->bd_bucket->hsb_index;
656
657         if (bd1->bd_offset != bd2->bd_offset)
658                 return bd1->bd_offset - bd2->bd_offset;
659
660         return 0;
661 }
662
663 void cfs_hash_bd_add_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
664                             cfs_hlist_node_t *hnode);
665 void cfs_hash_bd_del_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
666                             cfs_hlist_node_t *hnode);
667 void cfs_hash_bd_move_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd_old,
668                              cfs_hash_bd_t *bd_new, cfs_hlist_node_t *hnode);
669
670 static inline int cfs_hash_bd_dec_and_lock(cfs_hash_t *hs, cfs_hash_bd_t *bd,
671                                            cfs_atomic_t *condition)
672 {
673         LASSERT(cfs_hash_with_spin_bktlock(hs));
674         return cfs_atomic_dec_and_lock(condition,
675                                        &bd->bd_bucket->hsb_lock.spin);
676 }
677
678 static inline cfs_hlist_head_t *cfs_hash_bd_hhead(cfs_hash_t *hs,
679                                                   cfs_hash_bd_t *bd)
680 {
681         return hs->hs_hops->hop_hhead(hs, bd);
682 }
683
684 cfs_hlist_node_t *cfs_hash_bd_lookup_locked(cfs_hash_t *hs,
685                                             cfs_hash_bd_t *bd, const void *key);
686 cfs_hlist_node_t *cfs_hash_bd_findadd_locked(cfs_hash_t *hs,
687                                              cfs_hash_bd_t *bd, const void *key,
688                                              cfs_hlist_node_t *hnode,
689                                              int insist_add);
690 cfs_hlist_node_t *cfs_hash_bd_finddel_locked(cfs_hash_t *hs,
691                                              cfs_hash_bd_t *bd, const void *key,
692                                              cfs_hlist_node_t *hnode);
693
694 /**
695  * operations on cfs_hash bucket (bd: bucket descriptor),
696  * they are safe for hash-table with rehash
697  */
698 void cfs_hash_dual_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bds);
699 void cfs_hash_dual_bd_lock(cfs_hash_t *hs, cfs_hash_bd_t *bds, int excl);
700 void cfs_hash_dual_bd_unlock(cfs_hash_t *hs, cfs_hash_bd_t *bds, int excl);
701
702 static inline void cfs_hash_dual_bd_get_and_lock(cfs_hash_t *hs, const void *key,
703                                                  cfs_hash_bd_t *bds, int excl)
704 {
705         cfs_hash_dual_bd_get(hs, key, bds);
706         cfs_hash_dual_bd_lock(hs, bds, excl);
707 }
708
709 cfs_hlist_node_t *cfs_hash_dual_bd_lookup_locked(cfs_hash_t *hs,
710                                                  cfs_hash_bd_t *bds,
711                                                  const void *key);
712 cfs_hlist_node_t *cfs_hash_dual_bd_findadd_locked(cfs_hash_t *hs,
713                                                   cfs_hash_bd_t *bds,
714                                                   const void *key,
715                                                   cfs_hlist_node_t *hnode,
716                                                   int insist_add);
717 cfs_hlist_node_t *cfs_hash_dual_bd_finddel_locked(cfs_hash_t *hs,
718                                                   cfs_hash_bd_t *bds,
719                                                   const void *key,
720                                                   cfs_hlist_node_t *hnode);
721
722 /* Hash init/cleanup functions */
723 cfs_hash_t *cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
724                             unsigned bkt_bits, unsigned extra_bytes,
725                             unsigned min_theta, unsigned max_theta,
726                             cfs_hash_ops_t *ops, unsigned flags);
727
728 cfs_hash_t *cfs_hash_getref(cfs_hash_t *hs);
729 void cfs_hash_putref(cfs_hash_t *hs);
730
731 /* Hash addition functions */
732 void cfs_hash_add(cfs_hash_t *hs, const void *key,
733                   cfs_hlist_node_t *hnode);
734 int cfs_hash_add_unique(cfs_hash_t *hs, const void *key,
735                         cfs_hlist_node_t *hnode);
736 void *cfs_hash_findadd_unique(cfs_hash_t *hs, const void *key,
737                               cfs_hlist_node_t *hnode);
738
739 /* Hash deletion functions */
740 void *cfs_hash_del(cfs_hash_t *hs, const void *key, cfs_hlist_node_t *hnode);
741 void *cfs_hash_del_key(cfs_hash_t *hs, const void *key);
742
743 /* Hash lookup/for_each functions */
744 #define CFS_HASH_LOOP_HOG       1024
745
746 typedef int (*cfs_hash_for_each_cb_t)(cfs_hash_t *hs, cfs_hash_bd_t *bd,
747                                       cfs_hlist_node_t *node, void *data);
748 void *cfs_hash_lookup(cfs_hash_t *hs, const void *key);
749 void cfs_hash_for_each(cfs_hash_t *hs, cfs_hash_for_each_cb_t, void *data);
750 void cfs_hash_for_each_safe(cfs_hash_t *hs, cfs_hash_for_each_cb_t, void *data);
751 int  cfs_hash_for_each_nolock(cfs_hash_t *hs,
752                               cfs_hash_for_each_cb_t, void *data);
753 int  cfs_hash_for_each_empty(cfs_hash_t *hs,
754                              cfs_hash_for_each_cb_t, void *data);
755 void cfs_hash_for_each_key(cfs_hash_t *hs, const void *key,
756                            cfs_hash_for_each_cb_t, void *data);
757 typedef int (*cfs_hash_cond_opt_cb_t)(void *obj, void *data);
758 void cfs_hash_cond_del(cfs_hash_t *hs, cfs_hash_cond_opt_cb_t, void *data);
759
760 void cfs_hash_hlist_for_each(cfs_hash_t *hs, unsigned hindex,
761                              cfs_hash_for_each_cb_t, void *data);
762 int  cfs_hash_is_empty(cfs_hash_t *hs);
763 __u64 cfs_hash_size_get(cfs_hash_t *hs);
764
765 /*
766  * Rehash - Theta is calculated to be the average chained
767  * hash depth assuming a perfectly uniform hash funcion.
768  */
769 void cfs_hash_rehash_cancel_locked(cfs_hash_t *hs);
770 void cfs_hash_rehash_cancel(cfs_hash_t *hs);
771 int  cfs_hash_rehash(cfs_hash_t *hs, int do_rehash);
772 void cfs_hash_rehash_key(cfs_hash_t *hs, const void *old_key,
773                          void *new_key, cfs_hlist_node_t *hnode);
774
775 #if CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1
776 /* Validate hnode references the correct key */
777 static inline void
778 cfs_hash_key_validate(cfs_hash_t *hs, const void *key,
779                       cfs_hlist_node_t *hnode)
780 {
781         LASSERT(cfs_hash_keycmp(hs, key, hnode));
782 }
783
784 /* Validate hnode is in the correct bucket */
785 static inline void
786 cfs_hash_bucket_validate(cfs_hash_t *hs, cfs_hash_bd_t *bd,
787                          cfs_hlist_node_t *hnode)
788 {
789         cfs_hash_bd_t   bds[2];
790
791         cfs_hash_dual_bd_get(hs, cfs_hash_key(hs, hnode), bds);
792         LASSERT(bds[0].bd_bucket == bd->bd_bucket ||
793                 bds[1].bd_bucket == bd->bd_bucket);
794 }
795
796 #else /* CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1 */
797
798 static inline void
799 cfs_hash_key_validate(cfs_hash_t *hs, const void *key,
800                       cfs_hlist_node_t *hnode) {}
801
802 static inline void
803 cfs_hash_bucket_validate(cfs_hash_t *hs, cfs_hash_bd_t *bd,
804                          cfs_hlist_node_t *hnode) {}
805
806 #endif /* CFS_HASH_DEBUG_LEVEL */
807
808 #define CFS_HASH_THETA_BITS  10
809 #define CFS_HASH_MIN_THETA  (1U << (CFS_HASH_THETA_BITS - 1))
810 #define CFS_HASH_MAX_THETA  (1U << (CFS_HASH_THETA_BITS + 1))
811
812 /* Return integer component of theta */
813 static inline int __cfs_hash_theta_int(int theta)
814 {
815         return (theta >> CFS_HASH_THETA_BITS);
816 }
817
818 /* Return a fractional value between 0 and 999 */
819 static inline int __cfs_hash_theta_frac(int theta)
820 {
821         return ((theta * 1000) >> CFS_HASH_THETA_BITS) -
822                (__cfs_hash_theta_int(theta) * 1000);
823 }
824
825 static inline int __cfs_hash_theta(cfs_hash_t *hs)
826 {
827         return (cfs_atomic_read(&hs->hs_count) <<
828                 CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
829 }
830
831 static inline void __cfs_hash_set_theta(cfs_hash_t *hs, int min, int max)
832 {
833         LASSERT(min < max);
834         hs->hs_min_theta = (__u16)min;
835         hs->hs_max_theta = (__u16)max;
836 }
837
838 /* Generic debug formatting routines mainly for proc handler */
839 int cfs_hash_debug_header(char *str, int size);
840 int cfs_hash_debug_str(cfs_hash_t *hs, char *str, int size);
841
842 /*
843  * Generic djb2 hash algorithm for character arrays.
844  */
845 static inline unsigned
846 cfs_hash_djb2_hash(const void *key, size_t size, unsigned mask)
847 {
848         unsigned i, hash = 5381;
849
850         LASSERT(key != NULL);
851
852         for (i = 0; i < size; i++)
853                 hash = hash * 33 + ((char *)key)[i];
854
855         return (hash & mask);
856 }
857
858 /*
859  * Generic u32 hash algorithm.
860  */
861 static inline unsigned
862 cfs_hash_u32_hash(const __u32 key, unsigned mask)
863 {
864         return ((key * CFS_GOLDEN_RATIO_PRIME_32) & mask);
865 }
866
867 /*
868  * Generic u64 hash algorithm.
869  */
870 static inline unsigned
871 cfs_hash_u64_hash(const __u64 key, unsigned mask)
872 {
873         return ((unsigned)(key * CFS_GOLDEN_RATIO_PRIME_64) & mask);
874 }
875
876 /** iterate over all buckets in @bds (array of cfs_hash_bd_t) */
877 #define cfs_hash_for_each_bd(bds, n, i) \
878         for (i = 0; i < n && (bds)[i].bd_bucket != NULL; i++)
879
880 /** iterate over all buckets of @hs */
881 #define cfs_hash_for_each_bucket(hs, bd, pos)                   \
882         for (pos = 0;                                           \
883              pos < CFS_HASH_NBKT(hs) &&                         \
884              ((bd)->bd_bucket = (hs)->hs_buckets[pos]) != NULL; pos++)
885
886 /** iterate over all hlist of bucket @bd */
887 #define cfs_hash_bd_for_each_hlist(hs, bd, hlist)               \
888         for ((bd)->bd_offset = 0;                               \
889              (bd)->bd_offset < CFS_HASH_BKT_NHLIST(hs) &&       \
890              (hlist = cfs_hash_bd_hhead(hs, bd)) != NULL;       \
891              (bd)->bd_offset++)
892
893 /* !__LIBCFS__HASH_H__ */
894 #endif