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