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