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