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