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