GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / lustre / include / linux / 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 /*
61  * record hash depth and output to console when it's too deep,
62  * computing overhead is low but consume more memory
63  */
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        = BIT(0),
129         /** no bucket lock, use one spinlock to protect the whole hash */
130         CFS_HASH_NO_BKTLOCK     = BIT(1),
131         /** rwlock to protect bucket */
132         CFS_HASH_RW_BKTLOCK     = BIT(2),
133         /** spinlock to protect bucket */
134         CFS_HASH_SPIN_BKTLOCK   = BIT(3),
135         /** always add new item to tail */
136         CFS_HASH_ADD_TAIL       = BIT(4),
137         /** hash-table doesn't have refcount on item */
138         CFS_HASH_NO_ITEMREF     = BIT(5),
139         /** big name for param-tree */
140         CFS_HASH_BIGNAME        = BIT(6),
141         /** track global count */
142         CFS_HASH_COUNTER        = BIT(7),
143         /** rehash item by new key */
144         CFS_HASH_REHASH_KEY     = BIT(8),
145         /** Enable dynamic hash resizing */
146         CFS_HASH_REHASH         = BIT(9),
147         /** can shrink hash-size */
148         CFS_HASH_SHRINK         = BIT(10),
149         /** assert hash is empty on exit */
150         CFS_HASH_ASSERT_EMPTY   = BIT(11),
151         /** record hlist depth */
152         CFS_HASH_DEPTH          = BIT(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    = BIT(13),
158         /**
159          * NB, we typed hs_flags as  u16, please change it
160          * if you need to extend >=16 flags
161          */
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         /**
209          * serialize with rehash, or serialize all operations if
210          * the hash-table has CFS_HASH_NO_BKTLOCK
211          */
212         union cfs_hash_lock             hs_lock;
213         /** hash operations */
214         struct cfs_hash_ops             *hs_ops;
215         /** hash lock operations */
216         struct cfs_hash_lock_ops        *hs_lops;
217         /** hash list operations */
218         struct cfs_hash_hlist_ops       *hs_hops;
219         /** hash buckets-table */
220         struct cfs_hash_bucket          **hs_buckets;
221         /** total number of items on this hash-table */
222         atomic_t                        hs_count;
223         /** hash flags, see cfs_hash_tag for detail */
224         u16                             hs_flags;
225         /** # of extra-bytes for bucket, for user saving extended attributes */
226         u16                             hs_extra_bytes;
227         /** wants to iterate */
228         u8                              hs_iterating;
229         /** hash-table is dying */
230         u8                              hs_exiting;
231         /** current hash bits */
232         u8                              hs_cur_bits;
233         /** min hash bits */
234         u8                              hs_min_bits;
235         /** max hash bits */
236         u8                              hs_max_bits;
237         /** bits for rehash */
238         u8                              hs_rehash_bits;
239         /** bits for each bucket */
240         u8                              hs_bkt_bits;
241         /** resize min threshold */
242         u16                             hs_min_theta;
243         /** resize max threshold */
244         u16                             hs_max_theta;
245         /** resize count */
246         u32                             hs_rehash_count;
247         /** # of iterators (caller of cfs_hash_for_each_*) */
248         u32                             hs_iterators;
249         /** rehash workitem */
250         struct cfs_workitem             hs_rehash_wi;
251         /** refcount on this hash table */
252         atomic_t                        hs_refcount;
253         /** rehash buckets-table */
254         struct cfs_hash_bucket          **hs_rehash_buckets;
255 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
256         /** serialize debug members */
257         spinlock_t                      hs_dep_lock;
258         /** max depth */
259         unsigned int                    hs_dep_max;
260         /** id of the deepest bucket */
261         unsigned int                    hs_dep_bkt;
262         /** offset in the deepest bucket */
263         unsigned int                    hs_dep_off;
264         /** bits when we found the max depth */
265         unsigned int                    hs_dep_bits;
266         /** workitem to output max depth */
267         struct cfs_workitem             hs_dep_wi;
268 #endif
269         /** name of htable */
270         char                            hs_name[0];
271 };
272
273 struct cfs_hash_lock_ops {
274         /** lock the hash table */
275         void    (*hs_lock)(union cfs_hash_lock *lock, int exclusive);
276         /** unlock the hash table */
277         void    (*hs_unlock)(union cfs_hash_lock *lock, int exclusive);
278         /** lock the hash bucket */
279         void    (*hs_bkt_lock)(union cfs_hash_lock *lock, int exclusive);
280         /** unlock the hash bucket */
281         void    (*hs_bkt_unlock)(union cfs_hash_lock *lock, int exclusive);
282 };
283
284 struct cfs_hash_hlist_ops {
285         /** return hlist_head of hash-head of @bd */
286         struct hlist_head *(*hop_hhead)(struct cfs_hash *hs,
287                                         struct cfs_hash_bd *bd);
288         /** return hash-head size */
289         int (*hop_hhead_size)(struct cfs_hash *hs);
290         /** add @hnode to hash-head of @bd */
291         int (*hop_hnode_add)(struct cfs_hash *hs, struct cfs_hash_bd *bd,
292                              struct hlist_node *hnode);
293         /** remove @hnode from hash-head of @bd */
294         int (*hop_hnode_del)(struct cfs_hash *hs, struct cfs_hash_bd *bd,
295                              struct hlist_node *hnode);
296 };
297
298 struct cfs_hash_ops {
299         /** return hashed value from @key */
300         unsigned int (*hs_hash)(struct cfs_hash *hs, const void *key,
301                                 unsigned int mask);
302         /** return key address of @hnode */
303         void *   (*hs_key)(struct hlist_node *hnode);
304         /** copy key from @hnode to @key */
305         void     (*hs_keycpy)(struct hlist_node *hnode, void *key);
306         /**
307          *  compare @key with key of @hnode
308          *  returns 1 on a match
309          */
310         int      (*hs_keycmp)(const void *key, struct hlist_node *hnode);
311         /** return object address of @hnode, i.e: container_of(...hnode) */
312         void *   (*hs_object)(struct hlist_node *hnode);
313         /** get refcount of item, always called with holding bucket-lock */
314         void     (*hs_get)(struct cfs_hash *hs, struct hlist_node *hnode);
315         /** release refcount of item */
316         void     (*hs_put)(struct cfs_hash *hs, struct hlist_node *hnode);
317         /** release refcount of item, always called with holding bucket-lock */
318         void     (*hs_put_locked)(struct cfs_hash *hs,
319                                   struct hlist_node *hnode);
320         /** it's called before removing of @hnode */
321         void     (*hs_exit)(struct cfs_hash *hs, struct hlist_node *hnode);
322 };
323
324 /** total number of buckets in @hs */
325 #define CFS_HASH_NBKT(hs)       \
326         BIT((hs)->hs_cur_bits - (hs)->hs_bkt_bits)
327
328 /** total number of buckets in @hs while rehashing */
329 #define CFS_HASH_RH_NBKT(hs)    \
330         BIT((hs)->hs_rehash_bits - (hs)->hs_bkt_bits)
331
332 /** number of hlist for in bucket */
333 #define CFS_HASH_BKT_NHLIST(hs) BIT((hs)->hs_bkt_bits)
334
335 /** total number of hlist in @hs */
336 #define CFS_HASH_NHLIST(hs)     BIT((hs)->hs_cur_bits)
337
338 /** total number of hlist in @hs while rehashing */
339 #define CFS_HASH_RH_NHLIST(hs)  BIT((hs)->hs_rehash_bits)
340
341 static inline int
342 cfs_hash_with_no_lock(struct cfs_hash *hs)
343 {
344         /* caller will serialize all operations for this hash-table */
345         return hs->hs_flags & CFS_HASH_NO_LOCK;
346 }
347
348 static inline int
349 cfs_hash_with_no_bktlock(struct cfs_hash *hs)
350 {
351         /* no bucket lock, one single lock to protect the hash-table */
352         return hs->hs_flags & CFS_HASH_NO_BKTLOCK;
353 }
354
355 static inline int
356 cfs_hash_with_rw_bktlock(struct cfs_hash *hs)
357 {
358         /* rwlock to protect hash bucket */
359         return hs->hs_flags & CFS_HASH_RW_BKTLOCK;
360 }
361
362 static inline int
363 cfs_hash_with_spin_bktlock(struct cfs_hash *hs)
364 {
365         /* spinlock to protect hash bucket */
366         return hs->hs_flags & CFS_HASH_SPIN_BKTLOCK;
367 }
368
369 static inline int
370 cfs_hash_with_add_tail(struct cfs_hash *hs)
371 {
372         return hs->hs_flags & CFS_HASH_ADD_TAIL;
373 }
374
375 static inline int
376 cfs_hash_with_no_itemref(struct cfs_hash *hs)
377 {
378         /*
379          * hash-table doesn't keep refcount on item,
380          * item can't be removed from hash unless it's
381          * ZERO refcount
382          */
383         return hs->hs_flags & CFS_HASH_NO_ITEMREF;
384 }
385
386 static inline int
387 cfs_hash_with_bigname(struct cfs_hash *hs)
388 {
389         return hs->hs_flags & CFS_HASH_BIGNAME;
390 }
391
392 static inline int
393 cfs_hash_with_counter(struct cfs_hash *hs)
394 {
395         return hs->hs_flags & CFS_HASH_COUNTER;
396 }
397
398 static inline int
399 cfs_hash_with_rehash(struct cfs_hash *hs)
400 {
401         return hs->hs_flags & CFS_HASH_REHASH;
402 }
403
404 static inline int
405 cfs_hash_with_rehash_key(struct cfs_hash *hs)
406 {
407         return hs->hs_flags & CFS_HASH_REHASH_KEY;
408 }
409
410 static inline int
411 cfs_hash_with_shrink(struct cfs_hash *hs)
412 {
413         return hs->hs_flags & CFS_HASH_SHRINK;
414 }
415
416 static inline int
417 cfs_hash_with_assert_empty(struct cfs_hash *hs)
418 {
419         return hs->hs_flags & CFS_HASH_ASSERT_EMPTY;
420 }
421
422 static inline int
423 cfs_hash_with_depth(struct cfs_hash *hs)
424 {
425         return hs->hs_flags & CFS_HASH_DEPTH;
426 }
427
428 static inline int
429 cfs_hash_with_nblk_change(struct cfs_hash *hs)
430 {
431         return hs->hs_flags & CFS_HASH_NBLK_CHANGE;
432 }
433
434 static inline int
435 cfs_hash_is_exiting(struct cfs_hash *hs)
436 {
437         /* cfs_hash_destroy is called */
438         return hs->hs_exiting;
439 }
440
441 static inline int
442 cfs_hash_is_rehashing(struct cfs_hash *hs)
443 {
444         /* rehash is launched */
445         return !!hs->hs_rehash_bits;
446 }
447
448 static inline int
449 cfs_hash_is_iterating(struct cfs_hash *hs)
450 {
451         /* someone is calling cfs_hash_for_each_* */
452         return hs->hs_iterating || hs->hs_iterators;
453 }
454
455 static inline int
456 cfs_hash_bkt_size(struct cfs_hash *hs)
457 {
458         return offsetof(struct cfs_hash_bucket, hsb_head[0]) +
459                hs->hs_hops->hop_hhead_size(hs) * CFS_HASH_BKT_NHLIST(hs) +
460                hs->hs_extra_bytes;
461 }
462
463 static inline unsigned
464 cfs_hash_id(struct cfs_hash *hs, const void *key, unsigned int mask)
465 {
466         return hs->hs_ops->hs_hash(hs, key, mask);
467 }
468
469 static inline void *
470 cfs_hash_key(struct cfs_hash *hs, struct hlist_node *hnode)
471 {
472         return hs->hs_ops->hs_key(hnode);
473 }
474
475 static inline void
476 cfs_hash_keycpy(struct cfs_hash *hs, struct hlist_node *hnode, void *key)
477 {
478         if (hs->hs_ops->hs_keycpy)
479                 hs->hs_ops->hs_keycpy(hnode, key);
480 }
481
482 /**
483  * Returns 1 on a match,
484  */
485 static inline int
486 cfs_hash_keycmp(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
487 {
488         return hs->hs_ops->hs_keycmp(key, hnode);
489 }
490
491 static inline void *
492 cfs_hash_object(struct cfs_hash *hs, struct hlist_node *hnode)
493 {
494         return hs->hs_ops->hs_object(hnode);
495 }
496
497 static inline void
498 cfs_hash_get(struct cfs_hash *hs, struct hlist_node *hnode)
499 {
500         return hs->hs_ops->hs_get(hs, hnode);
501 }
502
503 static inline void
504 cfs_hash_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
505 {
506         return hs->hs_ops->hs_put_locked(hs, hnode);
507 }
508
509 static inline void
510 cfs_hash_put(struct cfs_hash *hs, struct hlist_node *hnode)
511 {
512         return hs->hs_ops->hs_put(hs, hnode);
513 }
514
515 static inline void
516 cfs_hash_exit(struct cfs_hash *hs, struct hlist_node *hnode)
517 {
518         if (hs->hs_ops->hs_exit)
519                 hs->hs_ops->hs_exit(hs, hnode);
520 }
521
522 static inline void cfs_hash_lock(struct cfs_hash *hs, int excl)
523 {
524         hs->hs_lops->hs_lock(&hs->hs_lock, excl);
525 }
526
527 static inline void cfs_hash_unlock(struct cfs_hash *hs, int excl)
528 {
529         hs->hs_lops->hs_unlock(&hs->hs_lock, excl);
530 }
531
532 static inline int cfs_hash_dec_and_lock(struct cfs_hash *hs,
533                                         atomic_t *condition)
534 {
535         LASSERT(cfs_hash_with_no_bktlock(hs));
536         return atomic_dec_and_lock(condition, &hs->hs_lock.spin);
537 }
538
539 static inline void cfs_hash_bd_lock(struct cfs_hash *hs,
540                                     struct cfs_hash_bd *bd, int excl)
541 {
542         hs->hs_lops->hs_bkt_lock(&bd->bd_bucket->hsb_lock, excl);
543 }
544
545 static inline void cfs_hash_bd_unlock(struct cfs_hash *hs,
546                                       struct cfs_hash_bd *bd, int excl)
547 {
548         hs->hs_lops->hs_bkt_unlock(&bd->bd_bucket->hsb_lock, excl);
549 }
550
551 /**
552  * operations on cfs_hash bucket (bd: bucket descriptor),
553  * they are normally for hash-table without rehash
554  */
555 void cfs_hash_bd_get(struct cfs_hash *hs, const void *key,
556                      struct cfs_hash_bd *bd);
557
558 static inline void
559 cfs_hash_bd_get_and_lock(struct cfs_hash *hs, const void *key,
560                          struct cfs_hash_bd *bd, int excl)
561 {
562         cfs_hash_bd_get(hs, key, bd);
563         cfs_hash_bd_lock(hs, bd, excl);
564 }
565
566 static inline unsigned
567 cfs_hash_bd_index_get(struct cfs_hash *hs, struct cfs_hash_bd *bd)
568 {
569         return bd->bd_offset | (bd->bd_bucket->hsb_index << hs->hs_bkt_bits);
570 }
571
572 static inline void
573 cfs_hash_bd_index_set(struct cfs_hash *hs, unsigned int index,
574                       struct cfs_hash_bd *bd)
575 {
576         bd->bd_bucket = hs->hs_buckets[index >> hs->hs_bkt_bits];
577         bd->bd_offset = index & (CFS_HASH_BKT_NHLIST(hs) - 1U);
578 }
579
580 static inline void *
581 cfs_hash_bd_extra_get(struct cfs_hash *hs, struct cfs_hash_bd *bd)
582 {
583         return (void *)bd->bd_bucket +
584                cfs_hash_bkt_size(hs) - hs->hs_extra_bytes;
585 }
586
587 static inline u32
588 cfs_hash_bd_version_get(struct cfs_hash_bd *bd)
589 {
590         /* need hold cfs_hash_bd_lock */
591         return bd->bd_bucket->hsb_version;
592 }
593
594 static inline u32
595 cfs_hash_bd_count_get(struct cfs_hash_bd *bd)
596 {
597         /* need hold cfs_hash_bd_lock */
598         return bd->bd_bucket->hsb_count;
599 }
600
601 static inline int
602 cfs_hash_bd_depmax_get(struct cfs_hash_bd *bd)
603 {
604         return bd->bd_bucket->hsb_depmax;
605 }
606
607 static inline int
608 cfs_hash_bd_compare(struct cfs_hash_bd *bd1, struct cfs_hash_bd *bd2)
609 {
610         if (bd1->bd_bucket->hsb_index != bd2->bd_bucket->hsb_index)
611                 return bd1->bd_bucket->hsb_index - bd2->bd_bucket->hsb_index;
612
613         if (bd1->bd_offset != bd2->bd_offset)
614                 return bd1->bd_offset - bd2->bd_offset;
615
616         return 0;
617 }
618
619 void cfs_hash_bd_add_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
620                             struct hlist_node *hnode);
621 void cfs_hash_bd_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
622                             struct hlist_node *hnode);
623 void cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
624                              struct cfs_hash_bd *bd_new,
625                              struct hlist_node *hnode);
626
627 static inline int
628 cfs_hash_bd_dec_and_lock(struct cfs_hash *hs, struct cfs_hash_bd *bd,
629                          atomic_t *condition)
630 {
631         LASSERT(cfs_hash_with_spin_bktlock(hs));
632         return atomic_dec_and_lock(condition, &bd->bd_bucket->hsb_lock.spin);
633 }
634
635 static inline struct hlist_head *
636 cfs_hash_bd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
637 {
638         return hs->hs_hops->hop_hhead(hs, bd);
639 }
640
641 struct hlist_node *
642 cfs_hash_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
643                           const void *key);
644 struct hlist_node *
645 cfs_hash_bd_peek_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
646                         const void *key);
647
648 /**
649  * operations on cfs_hash bucket (bd: bucket descriptor),
650  * they are safe for hash-table with rehash
651  */
652 void cfs_hash_dual_bd_get(struct cfs_hash *hs, const void *key,
653                           struct cfs_hash_bd *bds);
654 void cfs_hash_dual_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
655                            int excl);
656 void cfs_hash_dual_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
657                              int excl);
658
659 static inline void
660 cfs_hash_dual_bd_get_and_lock(struct cfs_hash *hs, const void *key,
661                               struct cfs_hash_bd *bds, int excl)
662 {
663         cfs_hash_dual_bd_get(hs, key, bds);
664         cfs_hash_dual_bd_lock(hs, bds, excl);
665 }
666
667 struct hlist_node *
668 cfs_hash_dual_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
669                                const void *key);
670 struct hlist_node *
671 cfs_hash_dual_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
672                                 const void *key, struct hlist_node *hnode,
673                                 int insist_add);
674 struct hlist_node *
675 cfs_hash_dual_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
676                                 const void *key, struct hlist_node *hnode);
677
678 /* Hash init/cleanup functions */
679 struct cfs_hash *
680 cfs_hash_create(char *name, unsigned int cur_bits, unsigned int max_bits,
681                 unsigned int bkt_bits, unsigned int extra_bytes,
682                 unsigned int min_theta, unsigned int max_theta,
683                 struct cfs_hash_ops *ops, unsigned int flags);
684
685 struct cfs_hash *cfs_hash_getref(struct cfs_hash *hs);
686 void cfs_hash_putref(struct cfs_hash *hs);
687
688 /* Hash addition functions */
689 void cfs_hash_add(struct cfs_hash *hs, const void *key,
690                   struct hlist_node *hnode);
691 int cfs_hash_add_unique(struct cfs_hash *hs, const void *key,
692                         struct hlist_node *hnode);
693 void *cfs_hash_findadd_unique(struct cfs_hash *hs, const void *key,
694                               struct hlist_node *hnode);
695
696 /* Hash deletion functions */
697 void *cfs_hash_del(struct cfs_hash *hs, const void *key,
698                    struct hlist_node *hnode);
699 void *cfs_hash_del_key(struct cfs_hash *hs, const void *key);
700
701 /* Hash lookup/for_each functions */
702 #define CFS_HASH_LOOP_HOG       1024
703
704 typedef int (*cfs_hash_for_each_cb_t)(struct cfs_hash *hs,
705                                       struct cfs_hash_bd *bd,
706                                       struct hlist_node *node,
707                                       void *data);
708 void *
709 cfs_hash_lookup(struct cfs_hash *hs, const void *key);
710 void
711 cfs_hash_for_each(struct cfs_hash *hs, cfs_hash_for_each_cb_t cb, void *data);
712 void
713 cfs_hash_for_each_safe(struct cfs_hash *hs, cfs_hash_for_each_cb_t cb,
714                        void *data);
715 int
716 cfs_hash_for_each_nolock(struct cfs_hash *hs, cfs_hash_for_each_cb_t cb,
717                          void *data, int start);
718 int
719 cfs_hash_for_each_empty(struct cfs_hash *hs, cfs_hash_for_each_cb_t cb,
720                         void *data);
721 void
722 cfs_hash_for_each_key(struct cfs_hash *hs, const void *key,
723                       cfs_hash_for_each_cb_t cb, void *data);
724 typedef int (*cfs_hash_cond_opt_cb_t)(void *obj, void *data);
725 void
726 cfs_hash_cond_del(struct cfs_hash *hs, cfs_hash_cond_opt_cb_t cb, void *data);
727
728 void
729 cfs_hash_hlist_for_each(struct cfs_hash *hs, unsigned int hindex,
730                         cfs_hash_for_each_cb_t cb, void *data);
731 int  cfs_hash_is_empty(struct cfs_hash *hs);
732 u64 cfs_hash_size_get(struct cfs_hash *hs);
733
734 /*
735  * Rehash - Theta is calculated to be the average chained
736  * hash depth assuming a perfectly uniform hash function.
737  */
738 void cfs_hash_rehash_cancel_locked(struct cfs_hash *hs);
739 void cfs_hash_rehash_cancel(struct cfs_hash *hs);
740 int  cfs_hash_rehash(struct cfs_hash *hs, int do_rehash);
741 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
742                          void *new_key, struct hlist_node *hnode);
743
744 #if CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1
745 /* Validate hnode references the correct key */
746 static inline void
747 cfs_hash_key_validate(struct cfs_hash *hs, const void *key,
748                       struct hlist_node *hnode)
749 {
750         LASSERT(cfs_hash_keycmp(hs, key, hnode));
751 }
752
753 /* Validate hnode is in the correct bucket */
754 static inline void
755 cfs_hash_bucket_validate(struct cfs_hash *hs, struct cfs_hash_bd *bd,
756                          struct hlist_node *hnode)
757 {
758         struct cfs_hash_bd bds[2];
759
760         cfs_hash_dual_bd_get(hs, cfs_hash_key(hs, hnode), bds);
761         LASSERT(bds[0].bd_bucket == bd->bd_bucket ||
762                 bds[1].bd_bucket == bd->bd_bucket);
763 }
764
765 #else /* CFS_HASH_DEBUG_LEVEL > CFS_HASH_DEBUG_1 */
766
767 static inline void
768 cfs_hash_key_validate(struct cfs_hash *hs, const void *key,
769                       struct hlist_node *hnode) {}
770
771 static inline void
772 cfs_hash_bucket_validate(struct cfs_hash *hs, struct cfs_hash_bd *bd,
773                          struct hlist_node *hnode) {}
774
775 #endif /* CFS_HASH_DEBUG_LEVEL */
776
777 #define CFS_HASH_THETA_BITS     10
778 #define CFS_HASH_MIN_THETA      BIT(CFS_HASH_THETA_BITS - 1)
779 #define CFS_HASH_MAX_THETA      BIT(CFS_HASH_THETA_BITS + 1)
780
781 /* Return integer component of theta */
782 static inline int __cfs_hash_theta_int(int theta)
783 {
784         return (theta >> CFS_HASH_THETA_BITS);
785 }
786
787 /* Return a fractional value between 0 and 999 */
788 static inline int __cfs_hash_theta_frac(int theta)
789 {
790         return ((theta * 1000) >> CFS_HASH_THETA_BITS) -
791                (__cfs_hash_theta_int(theta) * 1000);
792 }
793
794 static inline int __cfs_hash_theta(struct cfs_hash *hs)
795 {
796         return (atomic_read(&hs->hs_count) <<
797                 CFS_HASH_THETA_BITS) >> hs->hs_cur_bits;
798 }
799
800 static inline void
801 __cfs_hash_set_theta(struct cfs_hash *hs, int min, int max)
802 {
803         LASSERT(min < max);
804         hs->hs_min_theta = (u16)min;
805         hs->hs_max_theta = (u16)max;
806 }
807
808 /* Generic debug formatting routines mainly for proc handler */
809 struct seq_file;
810 void cfs_hash_debug_header(struct seq_file *m);
811 void cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m);
812
813 /*
814  * Generic djb2 hash algorithm for character arrays.
815  */
816 static inline unsigned
817 cfs_hash_djb2_hash(const void *key, size_t size, unsigned int mask)
818 {
819         unsigned int i, hash = 5381;
820
821         LASSERT(key);
822
823         for (i = 0; i < size; i++)
824                 hash = hash * 33 + ((char *)key)[i];
825
826         return (hash & mask);
827 }
828
829 /*
830  * Generic u32 hash algorithm.
831  */
832 static inline unsigned
833 cfs_hash_u32_hash(const u32 key, unsigned int mask)
834 {
835         return ((key * CFS_GOLDEN_RATIO_PRIME_32) & mask);
836 }
837
838 /*
839  * Generic u64 hash algorithm.
840  */
841 static inline unsigned
842 cfs_hash_u64_hash(const u64 key, unsigned int mask)
843 {
844         return ((unsigned int)(key * CFS_GOLDEN_RATIO_PRIME_64) & mask);
845 }
846
847 /** iterate over all buckets in @bds (array of struct cfs_hash_bd) */
848 #define cfs_hash_for_each_bd(bds, n, i) \
849         for (i = 0; i < n && (bds)[i].bd_bucket != NULL; i++)
850
851 /** iterate over all buckets of @hs */
852 #define cfs_hash_for_each_bucket(hs, bd, pos)                   \
853         for (pos = 0;                                           \
854              pos < CFS_HASH_NBKT(hs) &&                         \
855              ((bd)->bd_bucket = (hs)->hs_buckets[pos]) != NULL; pos++)
856
857 /** iterate over all hlist of bucket @bd */
858 #define cfs_hash_bd_for_each_hlist(hs, bd, hlist)               \
859         for ((bd)->bd_offset = 0;                               \
860              (bd)->bd_offset < CFS_HASH_BKT_NHLIST(hs) &&       \
861              (hlist = cfs_hash_bd_hhead(hs, bd)) != NULL;       \
862              (bd)->bd_offset++)
863
864 /* !__LIBCFS__HASH_H__ */
865 #endif