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