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