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