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