4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/hash.c
38 * Implement a hash class for hash process in lustre system.
40 * Author: YuZhangyong <yzy@clusterfs.com>
42 * 2008-08-15: Brian Behlendorf <behlendorf1@llnl.gov>
43 * - Simplified API and improved documentation
44 * - Added per-hash feature flags:
45 * * CFS_HASH_DEBUG additional validation
46 * * CFS_HASH_REHASH dynamic rehashing
47 * - Added per-hash statistics
48 * - General performance enhancements
50 * 2009-07-31: Liang Zhen <zhen.liang@sun.com>
51 * - move all stuff to libcfs
52 * - don't allow cur_bits != max_bits without setting of CFS_HASH_REHASH
53 * - ignore hs_rwlock if without CFS_HASH_REHASH setting
54 * - buckets are allocated one by one(intead of contiguous memory),
55 * to avoid unnecessary cacheline conflict
57 * 2010-03-01: Liang Zhen <zhen.liang@sun.com>
58 * - "bucket" is a group of hlist_head now, user can speicify bucket size
59 * by bkt_bits of cfs_hash_create(), all hlist_heads in a bucket share
60 * one lock for reducing memory overhead.
62 * - support lockless hash, caller will take care of locks:
63 * avoid lock overhead for hash tables that are already protected
64 * by locking in the caller for another reason
66 * - support both spin_lock/rwlock for bucket:
67 * overhead of spinlock contention is lower than read/write
68 * contention of rwlock, so using spinlock to serialize operations on
69 * bucket is more reasonable for those frequently changed hash tables
71 * - support one-single lock mode:
72 * one lock to protect all hash operations to avoid overhead of
73 * multiple locks if hash table is always small
75 * - removed a lot of unnecessary addref & decref on hash element:
76 * addref & decref are atomic operations in many use-cases which
79 * - support non-blocking cfs_hash_add() and cfs_hash_findadd():
80 * some lustre use-cases require these functions to be strictly
81 * non-blocking, we need to schedule required rehash on a different
82 * thread on those cases.
84 * - safer rehash on large hash table
85 * In old implementation, rehash function will exclusively lock the
86 * hash table and finish rehash in one batch, it's dangerous on SMP
87 * system because rehash millions of elements could take long time.
88 * New implemented rehash can release lock and relax CPU in middle
89 * of rehash, it's safe for another thread to search/change on the
90 * hash table even it's in rehasing.
92 * - support two different refcount modes
93 * . hash table has refcount on element
94 * . hash table doesn't change refcount on adding/removing element
96 * - support long name hash table (for param-tree)
98 * - fix a bug for cfs_hash_rehash_key:
99 * in old implementation, cfs_hash_rehash_key could screw up the
100 * hash-table because @key is overwritten without any protection.
101 * Now we need user to define hs_keycpy for those rehash enabled
102 * hash tables, cfs_hash_rehash_key will overwrite hash-key
103 * inside lock by calling hs_keycpy.
105 * - better hash iteration:
106 * Now we support both locked iteration & lockless iteration of hash
107 * table. Also, user can break the iteration by return 1 in callback.
110 #include <linux/libcfs/libcfs.h>
111 #include <linux/seq_file.h>
113 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
114 static unsigned int warn_on_depth = 8;
115 CFS_MODULE_PARM(warn_on_depth, "i", uint, 0644,
116 "warning when hash depth is high.");
119 struct cfs_wi_sched *cfs_sched_rehash;
122 cfs_hash_nl_lock(union cfs_hash_lock *lock, int exclusive) {}
125 cfs_hash_nl_unlock(union cfs_hash_lock *lock, int exclusive) {}
128 cfs_hash_spin_lock(union cfs_hash_lock *lock, int exclusive)
130 spin_lock(&lock->spin);
134 cfs_hash_spin_unlock(union cfs_hash_lock *lock, int exclusive)
136 spin_unlock(&lock->spin);
140 cfs_hash_rw_lock(union cfs_hash_lock *lock, int exclusive)
143 read_lock(&lock->rw);
145 write_lock(&lock->rw);
149 cfs_hash_rw_unlock(union cfs_hash_lock *lock, int exclusive)
152 read_unlock(&lock->rw);
154 write_unlock(&lock->rw);
158 static cfs_hash_lock_ops_t cfs_hash_nl_lops =
160 .hs_lock = cfs_hash_nl_lock,
161 .hs_unlock = cfs_hash_nl_unlock,
162 .hs_bkt_lock = cfs_hash_nl_lock,
163 .hs_bkt_unlock = cfs_hash_nl_unlock,
166 /** no bucket lock, one spinlock to protect everything */
167 static cfs_hash_lock_ops_t cfs_hash_nbl_lops =
169 .hs_lock = cfs_hash_spin_lock,
170 .hs_unlock = cfs_hash_spin_unlock,
171 .hs_bkt_lock = cfs_hash_nl_lock,
172 .hs_bkt_unlock = cfs_hash_nl_unlock,
175 /** spin bucket lock, rehash is enabled */
176 static cfs_hash_lock_ops_t cfs_hash_bkt_spin_lops =
178 .hs_lock = cfs_hash_rw_lock,
179 .hs_unlock = cfs_hash_rw_unlock,
180 .hs_bkt_lock = cfs_hash_spin_lock,
181 .hs_bkt_unlock = cfs_hash_spin_unlock,
184 /** rw bucket lock, rehash is enabled */
185 static cfs_hash_lock_ops_t cfs_hash_bkt_rw_lops =
187 .hs_lock = cfs_hash_rw_lock,
188 .hs_unlock = cfs_hash_rw_unlock,
189 .hs_bkt_lock = cfs_hash_rw_lock,
190 .hs_bkt_unlock = cfs_hash_rw_unlock,
193 /** spin bucket lock, rehash is disabled */
194 static cfs_hash_lock_ops_t cfs_hash_nr_bkt_spin_lops =
196 .hs_lock = cfs_hash_nl_lock,
197 .hs_unlock = cfs_hash_nl_unlock,
198 .hs_bkt_lock = cfs_hash_spin_lock,
199 .hs_bkt_unlock = cfs_hash_spin_unlock,
202 /** rw bucket lock, rehash is disabled */
203 static cfs_hash_lock_ops_t cfs_hash_nr_bkt_rw_lops =
205 .hs_lock = cfs_hash_nl_lock,
206 .hs_unlock = cfs_hash_nl_unlock,
207 .hs_bkt_lock = cfs_hash_rw_lock,
208 .hs_bkt_unlock = cfs_hash_rw_unlock,
212 cfs_hash_lock_setup(struct cfs_hash *hs)
214 if (cfs_hash_with_no_lock(hs)) {
215 hs->hs_lops = &cfs_hash_nl_lops;
217 } else if (cfs_hash_with_no_bktlock(hs)) {
218 hs->hs_lops = &cfs_hash_nbl_lops;
219 spin_lock_init(&hs->hs_lock.spin);
221 } else if (cfs_hash_with_rehash(hs)) {
222 rwlock_init(&hs->hs_lock.rw);
224 if (cfs_hash_with_rw_bktlock(hs))
225 hs->hs_lops = &cfs_hash_bkt_rw_lops;
226 else if (cfs_hash_with_spin_bktlock(hs))
227 hs->hs_lops = &cfs_hash_bkt_spin_lops;
231 if (cfs_hash_with_rw_bktlock(hs))
232 hs->hs_lops = &cfs_hash_nr_bkt_rw_lops;
233 else if (cfs_hash_with_spin_bktlock(hs))
234 hs->hs_lops = &cfs_hash_nr_bkt_spin_lops;
241 * Simple hash head without depth tracking
242 * new element is always added to head of hlist
245 struct hlist_head hh_head; /**< entries list */
249 cfs_hash_hh_hhead_size(struct cfs_hash *hs)
251 return sizeof(cfs_hash_head_t);
254 static struct hlist_head *
255 cfs_hash_hh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
257 cfs_hash_head_t *head = (cfs_hash_head_t *)&bd->bd_bucket->hsb_head[0];
259 return &head[bd->bd_offset].hh_head;
263 cfs_hash_hh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
264 struct hlist_node *hnode)
266 hlist_add_head(hnode, cfs_hash_hh_hhead(hs, bd));
267 return -1; /* unknown depth */
271 cfs_hash_hh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
272 struct hlist_node *hnode)
274 hlist_del_init(hnode);
275 return -1; /* unknown depth */
279 * Simple hash head with depth tracking
280 * new element is always added to head of hlist
283 struct hlist_head hd_head; /**< entries list */
284 unsigned int hd_depth; /**< list length */
285 } cfs_hash_head_dep_t;
288 cfs_hash_hd_hhead_size(struct cfs_hash *hs)
290 return sizeof(cfs_hash_head_dep_t);
293 static struct hlist_head *
294 cfs_hash_hd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
296 cfs_hash_head_dep_t *head;
298 head = (cfs_hash_head_dep_t *)&bd->bd_bucket->hsb_head[0];
299 return &head[bd->bd_offset].hd_head;
303 cfs_hash_hd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
304 struct hlist_node *hnode)
306 cfs_hash_head_dep_t *hh = container_of(cfs_hash_hd_hhead(hs, bd),
307 cfs_hash_head_dep_t, hd_head);
308 hlist_add_head(hnode, &hh->hd_head);
309 return ++hh->hd_depth;
313 cfs_hash_hd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
314 struct hlist_node *hnode)
316 cfs_hash_head_dep_t *hh = container_of(cfs_hash_hd_hhead(hs, bd),
317 cfs_hash_head_dep_t, hd_head);
318 hlist_del_init(hnode);
319 return --hh->hd_depth;
323 * double links hash head without depth tracking
324 * new element is always added to tail of hlist
327 struct hlist_head dh_head; /**< entries list */
328 struct hlist_node *dh_tail; /**< the last entry */
332 cfs_hash_dh_hhead_size(struct cfs_hash *hs)
334 return sizeof(cfs_hash_dhead_t);
337 static struct hlist_head *
338 cfs_hash_dh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
340 cfs_hash_dhead_t *head;
342 head = (cfs_hash_dhead_t *)&bd->bd_bucket->hsb_head[0];
343 return &head[bd->bd_offset].dh_head;
347 cfs_hash_dh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
348 struct hlist_node *hnode)
350 cfs_hash_dhead_t *dh = container_of(cfs_hash_dh_hhead(hs, bd),
351 cfs_hash_dhead_t, dh_head);
353 if (dh->dh_tail != NULL) /* not empty */
354 hlist_add_after(dh->dh_tail, hnode);
355 else /* empty list */
356 hlist_add_head(hnode, &dh->dh_head);
358 return -1; /* unknown depth */
362 cfs_hash_dh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
363 struct hlist_node *hnd)
365 cfs_hash_dhead_t *dh = container_of(cfs_hash_dh_hhead(hs, bd),
366 cfs_hash_dhead_t, dh_head);
368 if (hnd->next == NULL) { /* it's the tail */
369 dh->dh_tail = (hnd->pprev == &dh->dh_head.first) ? NULL :
370 container_of(hnd->pprev, struct hlist_node, next);
373 return -1; /* unknown depth */
377 * double links hash head with depth tracking
378 * new element is always added to tail of hlist
381 struct hlist_head dd_head; /**< entries list */
382 struct hlist_node *dd_tail; /**< the last entry */
383 unsigned int dd_depth; /**< list length */
384 } cfs_hash_dhead_dep_t;
387 cfs_hash_dd_hhead_size(struct cfs_hash *hs)
389 return sizeof(cfs_hash_dhead_dep_t);
392 static struct hlist_head *
393 cfs_hash_dd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
395 cfs_hash_dhead_dep_t *head;
397 head = (cfs_hash_dhead_dep_t *)&bd->bd_bucket->hsb_head[0];
398 return &head[bd->bd_offset].dd_head;
402 cfs_hash_dd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
403 struct hlist_node *hnode)
405 cfs_hash_dhead_dep_t *dh = container_of(cfs_hash_dd_hhead(hs, bd),
406 cfs_hash_dhead_dep_t, dd_head);
408 if (dh->dd_tail != NULL) /* not empty */
409 hlist_add_after(dh->dd_tail, hnode);
410 else /* empty list */
411 hlist_add_head(hnode, &dh->dd_head);
413 return ++dh->dd_depth;
417 cfs_hash_dd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
418 struct hlist_node *hnd)
420 cfs_hash_dhead_dep_t *dh = container_of(cfs_hash_dd_hhead(hs, bd),
421 cfs_hash_dhead_dep_t, dd_head);
423 if (hnd->next == NULL) { /* it's the tail */
424 dh->dd_tail = (hnd->pprev == &dh->dd_head.first) ? NULL :
425 container_of(hnd->pprev, struct hlist_node, next);
428 return --dh->dd_depth;
431 static cfs_hash_hlist_ops_t cfs_hash_hh_hops = {
432 .hop_hhead = cfs_hash_hh_hhead,
433 .hop_hhead_size = cfs_hash_hh_hhead_size,
434 .hop_hnode_add = cfs_hash_hh_hnode_add,
435 .hop_hnode_del = cfs_hash_hh_hnode_del,
438 static cfs_hash_hlist_ops_t cfs_hash_hd_hops = {
439 .hop_hhead = cfs_hash_hd_hhead,
440 .hop_hhead_size = cfs_hash_hd_hhead_size,
441 .hop_hnode_add = cfs_hash_hd_hnode_add,
442 .hop_hnode_del = cfs_hash_hd_hnode_del,
445 static cfs_hash_hlist_ops_t cfs_hash_dh_hops = {
446 .hop_hhead = cfs_hash_dh_hhead,
447 .hop_hhead_size = cfs_hash_dh_hhead_size,
448 .hop_hnode_add = cfs_hash_dh_hnode_add,
449 .hop_hnode_del = cfs_hash_dh_hnode_del,
452 static cfs_hash_hlist_ops_t cfs_hash_dd_hops = {
453 .hop_hhead = cfs_hash_dd_hhead,
454 .hop_hhead_size = cfs_hash_dd_hhead_size,
455 .hop_hnode_add = cfs_hash_dd_hnode_add,
456 .hop_hnode_del = cfs_hash_dd_hnode_del,
460 cfs_hash_hlist_setup(struct cfs_hash *hs)
462 if (cfs_hash_with_add_tail(hs)) {
463 hs->hs_hops = cfs_hash_with_depth(hs) ?
464 &cfs_hash_dd_hops : &cfs_hash_dh_hops;
466 hs->hs_hops = cfs_hash_with_depth(hs) ?
467 &cfs_hash_hd_hops : &cfs_hash_hh_hops;
472 cfs_hash_bd_from_key(struct cfs_hash *hs, struct cfs_hash_bucket **bkts,
473 unsigned int bits, const void *key, struct cfs_hash_bd *bd)
475 unsigned int index = cfs_hash_id(hs, key, (1U << bits) - 1);
477 LASSERT(bits == hs->hs_cur_bits || bits == hs->hs_rehash_bits);
479 bd->bd_bucket = bkts[index & ((1U << (bits - hs->hs_bkt_bits)) - 1)];
480 bd->bd_offset = index >> (bits - hs->hs_bkt_bits);
484 cfs_hash_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bd)
486 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
487 if (likely(hs->hs_rehash_buckets == NULL)) {
488 cfs_hash_bd_from_key(hs, hs->hs_buckets,
489 hs->hs_cur_bits, key, bd);
491 LASSERT(hs->hs_rehash_bits != 0);
492 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
493 hs->hs_rehash_bits, key, bd);
496 EXPORT_SYMBOL(cfs_hash_bd_get);
499 cfs_hash_bd_dep_record(struct cfs_hash *hs, struct cfs_hash_bd *bd, int dep_cur)
501 if (likely(dep_cur <= bd->bd_bucket->hsb_depmax))
504 bd->bd_bucket->hsb_depmax = dep_cur;
505 # if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
506 if (likely(warn_on_depth == 0 ||
507 max(warn_on_depth, hs->hs_dep_max) >= dep_cur))
510 spin_lock(&hs->hs_dep_lock);
511 hs->hs_dep_max = dep_cur;
512 hs->hs_dep_bkt = bd->bd_bucket->hsb_index;
513 hs->hs_dep_off = bd->bd_offset;
514 hs->hs_dep_bits = hs->hs_cur_bits;
515 spin_unlock(&hs->hs_dep_lock);
517 cfs_wi_schedule(cfs_sched_rehash, &hs->hs_dep_wi);
522 cfs_hash_bd_add_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
523 struct hlist_node *hnode)
527 rc = hs->hs_hops->hop_hnode_add(hs, bd, hnode);
528 cfs_hash_bd_dep_record(hs, bd, rc);
529 bd->bd_bucket->hsb_version++;
530 if (unlikely(bd->bd_bucket->hsb_version == 0))
531 bd->bd_bucket->hsb_version++;
532 bd->bd_bucket->hsb_count++;
534 if (cfs_hash_with_counter(hs))
535 atomic_inc(&hs->hs_count);
536 if (!cfs_hash_with_no_itemref(hs))
537 cfs_hash_get(hs, hnode);
539 EXPORT_SYMBOL(cfs_hash_bd_add_locked);
542 cfs_hash_bd_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
543 struct hlist_node *hnode)
545 hs->hs_hops->hop_hnode_del(hs, bd, hnode);
547 LASSERT(bd->bd_bucket->hsb_count > 0);
548 bd->bd_bucket->hsb_count--;
549 bd->bd_bucket->hsb_version++;
550 if (unlikely(bd->bd_bucket->hsb_version == 0))
551 bd->bd_bucket->hsb_version++;
553 if (cfs_hash_with_counter(hs)) {
554 LASSERT(atomic_read(&hs->hs_count) > 0);
555 atomic_dec(&hs->hs_count);
557 if (!cfs_hash_with_no_itemref(hs))
558 cfs_hash_put_locked(hs, hnode);
560 EXPORT_SYMBOL(cfs_hash_bd_del_locked);
563 cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
564 struct cfs_hash_bd *bd_new, struct hlist_node *hnode)
566 struct cfs_hash_bucket *obkt = bd_old->bd_bucket;
567 struct cfs_hash_bucket *nbkt = bd_new->bd_bucket;
570 if (cfs_hash_bd_compare(bd_old, bd_new) == 0)
573 /* use cfs_hash_bd_hnode_add/del, to avoid atomic & refcount ops
574 * in cfs_hash_bd_del/add_locked */
575 hs->hs_hops->hop_hnode_del(hs, bd_old, hnode);
576 rc = hs->hs_hops->hop_hnode_add(hs, bd_new, hnode);
577 cfs_hash_bd_dep_record(hs, bd_new, rc);
579 LASSERT(obkt->hsb_count > 0);
582 if (unlikely(obkt->hsb_version == 0))
586 if (unlikely(nbkt->hsb_version == 0))
589 EXPORT_SYMBOL(cfs_hash_bd_move_locked);
592 /** always set, for sanity (avoid ZERO intent) */
593 CFS_HS_LOOKUP_MASK_FIND = 1 << 0,
594 /** return entry with a ref */
595 CFS_HS_LOOKUP_MASK_REF = 1 << 1,
596 /** add entry if not existing */
597 CFS_HS_LOOKUP_MASK_ADD = 1 << 2,
598 /** delete entry, ignore other masks */
599 CFS_HS_LOOKUP_MASK_DEL = 1 << 3,
602 typedef enum cfs_hash_lookup_intent {
603 /** return item w/o refcount */
604 CFS_HS_LOOKUP_IT_PEEK = CFS_HS_LOOKUP_MASK_FIND,
605 /** return item with refcount */
606 CFS_HS_LOOKUP_IT_FIND = (CFS_HS_LOOKUP_MASK_FIND |
607 CFS_HS_LOOKUP_MASK_REF),
608 /** return item w/o refcount if existed, otherwise add */
609 CFS_HS_LOOKUP_IT_ADD = (CFS_HS_LOOKUP_MASK_FIND |
610 CFS_HS_LOOKUP_MASK_ADD),
611 /** return item with refcount if existed, otherwise add */
612 CFS_HS_LOOKUP_IT_FINDADD = (CFS_HS_LOOKUP_IT_FIND |
613 CFS_HS_LOOKUP_MASK_ADD),
614 /** delete if existed */
615 CFS_HS_LOOKUP_IT_FINDDEL = (CFS_HS_LOOKUP_MASK_FIND |
616 CFS_HS_LOOKUP_MASK_DEL)
617 } cfs_hash_lookup_intent_t;
619 static struct hlist_node *
620 cfs_hash_bd_lookup_intent(struct cfs_hash *hs, struct cfs_hash_bd *bd,
621 const void *key, struct hlist_node *hnode,
622 cfs_hash_lookup_intent_t intent)
625 struct hlist_head *hhead = cfs_hash_bd_hhead(hs, bd);
626 struct hlist_node *ehnode;
627 struct hlist_node *match;
628 int intent_add = (intent & CFS_HS_LOOKUP_MASK_ADD) != 0;
630 /* with this function, we can avoid a lot of useless refcount ops,
631 * which are expensive atomic operations most time. */
632 match = intent_add ? NULL : hnode;
633 hlist_for_each(ehnode, hhead) {
634 if (!cfs_hash_keycmp(hs, key, ehnode))
637 if (match != NULL && match != ehnode) /* can't match */
641 if ((intent & CFS_HS_LOOKUP_MASK_DEL) != 0) {
642 cfs_hash_bd_del_locked(hs, bd, ehnode);
646 /* caller wants refcount? */
647 if ((intent & CFS_HS_LOOKUP_MASK_REF) != 0)
648 cfs_hash_get(hs, ehnode);
655 LASSERT(hnode != NULL);
656 cfs_hash_bd_add_locked(hs, bd, hnode);
661 cfs_hash_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd, const void *key)
663 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
664 CFS_HS_LOOKUP_IT_FIND);
666 EXPORT_SYMBOL(cfs_hash_bd_lookup_locked);
669 cfs_hash_bd_peek_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd, const void *key)
671 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
672 CFS_HS_LOOKUP_IT_PEEK);
674 EXPORT_SYMBOL(cfs_hash_bd_peek_locked);
677 cfs_hash_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
678 const void *key, struct hlist_node *hnode,
681 return cfs_hash_bd_lookup_intent(hs, bd, key, hnode,
682 CFS_HS_LOOKUP_IT_ADD |
683 (!noref * CFS_HS_LOOKUP_MASK_REF));
685 EXPORT_SYMBOL(cfs_hash_bd_findadd_locked);
688 cfs_hash_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
689 const void *key, struct hlist_node *hnode)
691 /* hnode can be NULL, we find the first item with @key */
692 return cfs_hash_bd_lookup_intent(hs, bd, key, hnode,
693 CFS_HS_LOOKUP_IT_FINDDEL);
695 EXPORT_SYMBOL(cfs_hash_bd_finddel_locked);
698 cfs_hash_multi_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
699 unsigned n, int excl)
701 struct cfs_hash_bucket *prev = NULL;
705 * bds must be ascendantly ordered by bd->bd_bucket->hsb_index.
706 * NB: it's possible that several bds point to the same bucket but
707 * have different bd::bd_offset, so need take care of deadlock.
709 cfs_hash_for_each_bd(bds, n, i) {
710 if (prev == bds[i].bd_bucket)
713 LASSERT(prev == NULL ||
714 prev->hsb_index < bds[i].bd_bucket->hsb_index);
715 cfs_hash_bd_lock(hs, &bds[i], excl);
716 prev = bds[i].bd_bucket;
721 cfs_hash_multi_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
722 unsigned n, int excl)
724 struct cfs_hash_bucket *prev = NULL;
727 cfs_hash_for_each_bd(bds, n, i) {
728 if (prev != bds[i].bd_bucket) {
729 cfs_hash_bd_unlock(hs, &bds[i], excl);
730 prev = bds[i].bd_bucket;
735 static struct hlist_node *
736 cfs_hash_multi_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
737 unsigned n, const void *key)
739 struct hlist_node *ehnode;
742 cfs_hash_for_each_bd(bds, n, i) {
743 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, NULL,
744 CFS_HS_LOOKUP_IT_FIND);
751 static struct hlist_node *
752 cfs_hash_multi_bd_findadd_locked(struct cfs_hash *hs,
753 struct cfs_hash_bd *bds, unsigned n, const void *key,
754 struct hlist_node *hnode, int noref)
756 struct hlist_node *ehnode;
760 LASSERT(hnode != NULL);
761 intent = CFS_HS_LOOKUP_IT_PEEK | (!noref * CFS_HS_LOOKUP_MASK_REF);
763 cfs_hash_for_each_bd(bds, n, i) {
764 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key,
770 if (i == 1) { /* only one bucket */
771 cfs_hash_bd_add_locked(hs, &bds[0], hnode);
773 struct cfs_hash_bd mybd;
775 cfs_hash_bd_get(hs, key, &mybd);
776 cfs_hash_bd_add_locked(hs, &mybd, hnode);
782 static struct hlist_node *
783 cfs_hash_multi_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
784 unsigned n, const void *key,
785 struct hlist_node *hnode)
787 struct hlist_node *ehnode;
790 cfs_hash_for_each_bd(bds, n, i) {
791 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, hnode,
792 CFS_HS_LOOKUP_IT_FINDDEL);
800 cfs_hash_bd_order(struct cfs_hash_bd *bd1, struct cfs_hash_bd *bd2)
804 if (bd2->bd_bucket == NULL)
807 if (bd1->bd_bucket == NULL) {
809 bd2->bd_bucket = NULL;
813 rc = cfs_hash_bd_compare(bd1, bd2);
815 bd2->bd_bucket = NULL;
817 } else if (rc > 0) { /* swab bd1 and bd2 */
818 struct cfs_hash_bd tmp;
827 cfs_hash_dual_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bds)
829 /* NB: caller should hold hs_lock.rw if REHASH is set */
830 cfs_hash_bd_from_key(hs, hs->hs_buckets,
831 hs->hs_cur_bits, key, &bds[0]);
832 if (likely(hs->hs_rehash_buckets == NULL)) {
833 /* no rehash or not rehashing */
834 bds[1].bd_bucket = NULL;
838 LASSERT(hs->hs_rehash_bits != 0);
839 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
840 hs->hs_rehash_bits, key, &bds[1]);
842 cfs_hash_bd_order(&bds[0], &bds[1]);
844 EXPORT_SYMBOL(cfs_hash_dual_bd_get);
847 cfs_hash_dual_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
849 cfs_hash_multi_bd_lock(hs, bds, 2, excl);
851 EXPORT_SYMBOL(cfs_hash_dual_bd_lock);
854 cfs_hash_dual_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
856 cfs_hash_multi_bd_unlock(hs, bds, 2, excl);
858 EXPORT_SYMBOL(cfs_hash_dual_bd_unlock);
861 cfs_hash_dual_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
864 return cfs_hash_multi_bd_lookup_locked(hs, bds, 2, key);
866 EXPORT_SYMBOL(cfs_hash_dual_bd_lookup_locked);
869 cfs_hash_dual_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
870 const void *key, struct hlist_node *hnode,
873 return cfs_hash_multi_bd_findadd_locked(hs, bds, 2, key,
876 EXPORT_SYMBOL(cfs_hash_dual_bd_findadd_locked);
879 cfs_hash_dual_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
880 const void *key, struct hlist_node *hnode)
882 return cfs_hash_multi_bd_finddel_locked(hs, bds, 2, key, hnode);
884 EXPORT_SYMBOL(cfs_hash_dual_bd_finddel_locked);
887 cfs_hash_buckets_free(struct cfs_hash_bucket **buckets,
888 int bkt_size, int prev_size, int size)
892 for (i = prev_size; i < size; i++) {
893 if (buckets[i] != NULL)
894 LIBCFS_FREE(buckets[i], bkt_size);
897 LIBCFS_FREE(buckets, sizeof(buckets[0]) * size);
901 * Create or grow bucket memory. Return old_buckets if no allocation was
902 * needed, the newly allocated buckets if allocation was needed and
903 * successful, and NULL on error.
905 static struct cfs_hash_bucket **
906 cfs_hash_buckets_realloc(struct cfs_hash *hs, struct cfs_hash_bucket **old_bkts,
907 unsigned int old_size, unsigned int new_size)
909 struct cfs_hash_bucket **new_bkts;
912 LASSERT(old_size == 0 || old_bkts != NULL);
914 if (old_bkts != NULL && old_size == new_size)
917 LIBCFS_ALLOC(new_bkts, sizeof(new_bkts[0]) * new_size);
918 if (new_bkts == NULL)
921 if (old_bkts != NULL) {
922 memcpy(new_bkts, old_bkts,
923 min(old_size, new_size) * sizeof(*old_bkts));
926 for (i = old_size; i < new_size; i++) {
927 struct hlist_head *hhead;
928 struct cfs_hash_bd bd;
930 LIBCFS_ALLOC(new_bkts[i], cfs_hash_bkt_size(hs));
931 if (new_bkts[i] == NULL) {
932 cfs_hash_buckets_free(new_bkts, cfs_hash_bkt_size(hs),
937 new_bkts[i]->hsb_index = i;
938 new_bkts[i]->hsb_version = 1; /* shouldn't be zero */
939 new_bkts[i]->hsb_depmax = -1; /* unknown */
940 bd.bd_bucket = new_bkts[i];
941 cfs_hash_bd_for_each_hlist(hs, &bd, hhead)
942 INIT_HLIST_HEAD(hhead);
944 if (cfs_hash_with_no_lock(hs) ||
945 cfs_hash_with_no_bktlock(hs))
948 if (cfs_hash_with_rw_bktlock(hs))
949 rwlock_init(&new_bkts[i]->hsb_lock.rw);
950 else if (cfs_hash_with_spin_bktlock(hs))
951 spin_lock_init(&new_bkts[i]->hsb_lock.spin);
953 LBUG(); /* invalid use-case */
959 * Initialize new libcfs hash, where:
960 * @name - Descriptive hash name
961 * @cur_bits - Initial hash table size, in bits
962 * @max_bits - Maximum allowed hash table resize, in bits
963 * @ops - Registered hash table operations
964 * @flags - CFS_HASH_REHASH enable synamic hash resizing
965 * - CFS_HASH_SORT enable chained hash sort
967 static int cfs_hash_rehash_worker(cfs_workitem_t *wi);
969 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
970 static int cfs_hash_dep_print(cfs_workitem_t *wi)
972 struct cfs_hash *hs = container_of(wi, struct cfs_hash, hs_dep_wi);
978 spin_lock(&hs->hs_dep_lock);
979 dep = hs->hs_dep_max;
980 bkt = hs->hs_dep_bkt;
981 off = hs->hs_dep_off;
982 bits = hs->hs_dep_bits;
983 spin_unlock(&hs->hs_dep_lock);
985 LCONSOLE_WARN("#### HASH %s (bits: %d): max depth %d at bucket %d/%d\n",
986 hs->hs_name, bits, dep, bkt, off);
987 spin_lock(&hs->hs_dep_lock);
988 hs->hs_dep_bits = 0; /* mark as workitem done */
989 spin_unlock(&hs->hs_dep_lock);
993 static void cfs_hash_depth_wi_init(struct cfs_hash *hs)
995 spin_lock_init(&hs->hs_dep_lock);
996 cfs_wi_init(&hs->hs_dep_wi, hs, cfs_hash_dep_print);
999 static void cfs_hash_depth_wi_cancel(struct cfs_hash *hs)
1001 if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_dep_wi))
1004 spin_lock(&hs->hs_dep_lock);
1005 while (hs->hs_dep_bits != 0) {
1006 spin_unlock(&hs->hs_dep_lock);
1008 spin_lock(&hs->hs_dep_lock);
1010 spin_unlock(&hs->hs_dep_lock);
1013 #else /* CFS_HASH_DEBUG_LEVEL < CFS_HASH_DEBUG_1 */
1015 static inline void cfs_hash_depth_wi_init(struct cfs_hash *hs) {}
1016 static inline void cfs_hash_depth_wi_cancel(struct cfs_hash *hs) {}
1018 #endif /* CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1 */
1021 cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
1022 unsigned bkt_bits, unsigned extra_bytes,
1023 unsigned min_theta, unsigned max_theta,
1024 cfs_hash_ops_t *ops, unsigned flags)
1026 struct cfs_hash *hs;
1029 CLASSERT(CFS_HASH_THETA_BITS < 15);
1031 LASSERT(name != NULL);
1032 LASSERT(ops != NULL);
1033 LASSERT(ops->hs_key);
1034 LASSERT(ops->hs_hash);
1035 LASSERT(ops->hs_object);
1036 LASSERT(ops->hs_keycmp);
1037 LASSERT(ops->hs_get != NULL);
1038 LASSERT(ops->hs_put_locked != NULL);
1040 if ((flags & CFS_HASH_REHASH) != 0)
1041 flags |= CFS_HASH_COUNTER; /* must have counter */
1043 LASSERT(cur_bits > 0);
1044 LASSERT(cur_bits >= bkt_bits);
1045 LASSERT(max_bits >= cur_bits && max_bits < 31);
1046 LASSERT(ergo((flags & CFS_HASH_REHASH) == 0, cur_bits == max_bits));
1047 LASSERT(ergo((flags & CFS_HASH_REHASH) != 0,
1048 (flags & CFS_HASH_NO_LOCK) == 0));
1049 LASSERT(ergo((flags & CFS_HASH_REHASH_KEY) != 0,
1050 ops->hs_keycpy != NULL));
1052 len = (flags & CFS_HASH_BIGNAME) == 0 ?
1053 CFS_HASH_NAME_LEN : CFS_HASH_BIGNAME_LEN;
1054 LIBCFS_ALLOC(hs, offsetof(struct cfs_hash, hs_name[len]));
1058 strncpy(hs->hs_name, name, len);
1059 hs->hs_name[len - 1] = '\0';
1060 hs->hs_flags = flags;
1062 atomic_set(&hs->hs_refcount, 1);
1063 atomic_set(&hs->hs_count, 0);
1065 cfs_hash_lock_setup(hs);
1066 cfs_hash_hlist_setup(hs);
1068 hs->hs_cur_bits = (__u8)cur_bits;
1069 hs->hs_min_bits = (__u8)cur_bits;
1070 hs->hs_max_bits = (__u8)max_bits;
1071 hs->hs_bkt_bits = (__u8)bkt_bits;
1074 hs->hs_extra_bytes = extra_bytes;
1075 hs->hs_rehash_bits = 0;
1076 cfs_wi_init(&hs->hs_rehash_wi, hs, cfs_hash_rehash_worker);
1077 cfs_hash_depth_wi_init(hs);
1079 if (cfs_hash_with_rehash(hs))
1080 __cfs_hash_set_theta(hs, min_theta, max_theta);
1082 hs->hs_buckets = cfs_hash_buckets_realloc(hs, NULL, 0,
1084 if (hs->hs_buckets != NULL)
1087 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[len]));
1090 EXPORT_SYMBOL(cfs_hash_create);
1093 * Cleanup libcfs hash @hs.
1096 cfs_hash_destroy(struct cfs_hash *hs)
1098 struct hlist_node *hnode;
1099 struct hlist_node *pos;
1100 struct cfs_hash_bd bd;
1103 LASSERT(hs != NULL);
1104 LASSERT(!cfs_hash_is_exiting(hs) &&
1105 !cfs_hash_is_iterating(hs));
1108 * prohibit further rehashes, don't need any lock because
1109 * I'm the only (last) one can change it.
1112 if (cfs_hash_with_rehash(hs))
1113 cfs_hash_rehash_cancel(hs);
1115 cfs_hash_depth_wi_cancel(hs);
1116 /* rehash should be done/canceled */
1117 LASSERT(hs->hs_buckets != NULL &&
1118 hs->hs_rehash_buckets == NULL);
1120 cfs_hash_for_each_bucket(hs, &bd, i) {
1121 struct hlist_head *hhead;
1123 LASSERT(bd.bd_bucket != NULL);
1124 /* no need to take this lock, just for consistent code */
1125 cfs_hash_bd_lock(hs, &bd, 1);
1127 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1128 hlist_for_each_safe(hnode, pos, hhead) {
1129 LASSERTF(!cfs_hash_with_assert_empty(hs),
1130 "hash %s bucket %u(%u) is not "
1131 " empty: %u items left\n",
1132 hs->hs_name, bd.bd_bucket->hsb_index,
1133 bd.bd_offset, bd.bd_bucket->hsb_count);
1134 /* can't assert key valicate, because we
1135 * can interrupt rehash */
1136 cfs_hash_bd_del_locked(hs, &bd, hnode);
1137 cfs_hash_exit(hs, hnode);
1140 LASSERT(bd.bd_bucket->hsb_count == 0);
1141 cfs_hash_bd_unlock(hs, &bd, 1);
1145 LASSERT(atomic_read(&hs->hs_count) == 0);
1147 cfs_hash_buckets_free(hs->hs_buckets, cfs_hash_bkt_size(hs),
1148 0, CFS_HASH_NBKT(hs));
1149 i = cfs_hash_with_bigname(hs) ?
1150 CFS_HASH_BIGNAME_LEN : CFS_HASH_NAME_LEN;
1151 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[i]));
1154 struct cfs_hash *cfs_hash_getref(struct cfs_hash *hs)
1156 if (atomic_inc_not_zero(&hs->hs_refcount))
1160 EXPORT_SYMBOL(cfs_hash_getref);
1162 void cfs_hash_putref(struct cfs_hash *hs)
1164 if (atomic_dec_and_test(&hs->hs_refcount))
1165 cfs_hash_destroy(hs);
1167 EXPORT_SYMBOL(cfs_hash_putref);
1170 cfs_hash_rehash_bits(struct cfs_hash *hs)
1172 if (cfs_hash_with_no_lock(hs) ||
1173 !cfs_hash_with_rehash(hs))
1176 if (unlikely(cfs_hash_is_exiting(hs)))
1179 if (unlikely(cfs_hash_is_rehashing(hs)))
1182 if (unlikely(cfs_hash_is_iterating(hs)))
1185 /* XXX: need to handle case with max_theta != 2.0
1186 * and the case with min_theta != 0.5 */
1187 if ((hs->hs_cur_bits < hs->hs_max_bits) &&
1188 (__cfs_hash_theta(hs) > hs->hs_max_theta))
1189 return hs->hs_cur_bits + 1;
1191 if (!cfs_hash_with_shrink(hs))
1194 if ((hs->hs_cur_bits > hs->hs_min_bits) &&
1195 (__cfs_hash_theta(hs) < hs->hs_min_theta))
1196 return hs->hs_cur_bits - 1;
1202 * don't allow inline rehash if:
1203 * - user wants non-blocking change (add/del) on hash table
1204 * - too many elements
1207 cfs_hash_rehash_inline(struct cfs_hash *hs)
1209 return !cfs_hash_with_nblk_change(hs) &&
1210 atomic_read(&hs->hs_count) < CFS_HASH_LOOP_HOG;
1214 * Add item @hnode to libcfs hash @hs using @key. The registered
1215 * ops->hs_get function will be called when the item is added.
1218 cfs_hash_add(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1220 struct cfs_hash_bd bd;
1223 LASSERT(hlist_unhashed(hnode));
1225 cfs_hash_lock(hs, 0);
1226 cfs_hash_bd_get_and_lock(hs, key, &bd, 1);
1228 cfs_hash_key_validate(hs, key, hnode);
1229 cfs_hash_bd_add_locked(hs, &bd, hnode);
1231 cfs_hash_bd_unlock(hs, &bd, 1);
1233 bits = cfs_hash_rehash_bits(hs);
1234 cfs_hash_unlock(hs, 0);
1236 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1238 EXPORT_SYMBOL(cfs_hash_add);
1240 static struct hlist_node *
1241 cfs_hash_find_or_add(struct cfs_hash *hs, const void *key,
1242 struct hlist_node *hnode, int noref)
1244 struct hlist_node *ehnode;
1245 struct cfs_hash_bd bds[2];
1248 LASSERT(hlist_unhashed(hnode));
1250 cfs_hash_lock(hs, 0);
1251 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1253 cfs_hash_key_validate(hs, key, hnode);
1254 ehnode = cfs_hash_dual_bd_findadd_locked(hs, bds, key,
1256 cfs_hash_dual_bd_unlock(hs, bds, 1);
1258 if (ehnode == hnode) /* new item added */
1259 bits = cfs_hash_rehash_bits(hs);
1260 cfs_hash_unlock(hs, 0);
1262 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1268 * Add item @hnode to libcfs hash @hs using @key. The registered
1269 * ops->hs_get function will be called if the item was added.
1270 * Returns 0 on success or -EALREADY on key collisions.
1273 cfs_hash_add_unique(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1275 return cfs_hash_find_or_add(hs, key, hnode, 1) != hnode ?
1278 EXPORT_SYMBOL(cfs_hash_add_unique);
1281 * Add item @hnode to libcfs hash @hs using @key. If this @key
1282 * already exists in the hash then ops->hs_get will be called on the
1283 * conflicting entry and that entry will be returned to the caller.
1284 * Otherwise ops->hs_get is called on the item which was added.
1287 cfs_hash_findadd_unique(struct cfs_hash *hs, const void *key,
1288 struct hlist_node *hnode)
1290 hnode = cfs_hash_find_or_add(hs, key, hnode, 0);
1292 return cfs_hash_object(hs, hnode);
1294 EXPORT_SYMBOL(cfs_hash_findadd_unique);
1297 * Delete item @hnode from the libcfs hash @hs using @key. The @key
1298 * is required to ensure the correct hash bucket is locked since there
1299 * is no direct linkage from the item to the bucket. The object
1300 * removed from the hash will be returned and obs->hs_put is called
1301 * on the removed object.
1304 cfs_hash_del(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1308 struct cfs_hash_bd bds[2];
1310 cfs_hash_lock(hs, 0);
1311 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1313 /* NB: do nothing if @hnode is not in hash table */
1314 if (hnode == NULL || !hlist_unhashed(hnode)) {
1315 if (bds[1].bd_bucket == NULL && hnode != NULL) {
1316 cfs_hash_bd_del_locked(hs, &bds[0], hnode);
1318 hnode = cfs_hash_dual_bd_finddel_locked(hs, bds,
1323 if (hnode != NULL) {
1324 obj = cfs_hash_object(hs, hnode);
1325 bits = cfs_hash_rehash_bits(hs);
1328 cfs_hash_dual_bd_unlock(hs, bds, 1);
1329 cfs_hash_unlock(hs, 0);
1331 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1335 EXPORT_SYMBOL(cfs_hash_del);
1338 * Delete item given @key in libcfs hash @hs. The first @key found in
1339 * the hash will be removed, if the key exists multiple times in the hash
1340 * @hs this function must be called once per key. The removed object
1341 * will be returned and ops->hs_put is called on the removed object.
1344 cfs_hash_del_key(struct cfs_hash *hs, const void *key)
1346 return cfs_hash_del(hs, key, NULL);
1348 EXPORT_SYMBOL(cfs_hash_del_key);
1351 * Lookup an item using @key in the libcfs hash @hs and return it.
1352 * If the @key is found in the hash hs->hs_get() is called and the
1353 * matching objects is returned. It is the callers responsibility
1354 * to call the counterpart ops->hs_put using the cfs_hash_put() macro
1355 * when when finished with the object. If the @key was not found
1356 * in the hash @hs NULL is returned.
1359 cfs_hash_lookup(struct cfs_hash *hs, const void *key)
1362 struct hlist_node *hnode;
1363 struct cfs_hash_bd bds[2];
1365 cfs_hash_lock(hs, 0);
1366 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1368 hnode = cfs_hash_dual_bd_lookup_locked(hs, bds, key);
1370 obj = cfs_hash_object(hs, hnode);
1372 cfs_hash_dual_bd_unlock(hs, bds, 0);
1373 cfs_hash_unlock(hs, 0);
1377 EXPORT_SYMBOL(cfs_hash_lookup);
1380 cfs_hash_for_each_enter(struct cfs_hash *hs)
1382 LASSERT(!cfs_hash_is_exiting(hs));
1384 if (!cfs_hash_with_rehash(hs))
1387 * NB: it's race on cfs_has_t::hs_iterating, but doesn't matter
1388 * because it's just an unreliable signal to rehash-thread,
1389 * rehash-thread will try to finsih rehash ASAP when seeing this.
1391 hs->hs_iterating = 1;
1393 cfs_hash_lock(hs, 1);
1396 /* NB: iteration is mostly called by service thread,
1397 * we tend to cancel pending rehash-requst, instead of
1398 * blocking service thread, we will relaunch rehash request
1399 * after iteration */
1400 if (cfs_hash_is_rehashing(hs))
1401 cfs_hash_rehash_cancel_locked(hs);
1402 cfs_hash_unlock(hs, 1);
1406 cfs_hash_for_each_exit(struct cfs_hash *hs)
1411 if (!cfs_hash_with_rehash(hs))
1413 cfs_hash_lock(hs, 1);
1414 remained = --hs->hs_iterators;
1415 bits = cfs_hash_rehash_bits(hs);
1416 cfs_hash_unlock(hs, 1);
1417 /* NB: it's race on cfs_has_t::hs_iterating, see above */
1419 hs->hs_iterating = 0;
1421 cfs_hash_rehash(hs, atomic_read(&hs->hs_count) <
1427 * For each item in the libcfs hash @hs call the passed callback @func
1428 * and pass to it as an argument each hash item and the private @data.
1430 * a) the function may sleep!
1431 * b) during the callback:
1432 * . the bucket lock is held so the callback must never sleep.
1433 * . if @removal_safe is true, use can remove current item by
1434 * cfs_hash_bd_del_locked
1437 cfs_hash_for_each_tight(struct cfs_hash *hs, cfs_hash_for_each_cb_t func,
1438 void *data, int remove_safe)
1440 struct hlist_node *hnode;
1441 struct hlist_node *pos;
1442 struct cfs_hash_bd bd;
1444 int excl = !!remove_safe;
1448 cfs_hash_for_each_enter(hs);
1450 cfs_hash_lock(hs, 0);
1451 LASSERT(!cfs_hash_is_rehashing(hs));
1453 cfs_hash_for_each_bucket(hs, &bd, i) {
1454 struct hlist_head *hhead;
1456 cfs_hash_bd_lock(hs, &bd, excl);
1457 if (func == NULL) { /* only glimpse size */
1458 count += bd.bd_bucket->hsb_count;
1459 cfs_hash_bd_unlock(hs, &bd, excl);
1463 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1464 hlist_for_each_safe(hnode, pos, hhead) {
1465 cfs_hash_bucket_validate(hs, &bd, hnode);
1468 if (func(hs, &bd, hnode, data)) {
1469 cfs_hash_bd_unlock(hs, &bd, excl);
1474 cfs_hash_bd_unlock(hs, &bd, excl);
1475 if (loop < CFS_HASH_LOOP_HOG)
1478 cfs_hash_unlock(hs, 0);
1480 cfs_hash_lock(hs, 0);
1483 cfs_hash_unlock(hs, 0);
1485 cfs_hash_for_each_exit(hs);
1490 cfs_hash_cond_opt_cb_t func;
1492 } cfs_hash_cond_arg_t;
1495 cfs_hash_cond_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1496 struct hlist_node *hnode, void *data)
1498 cfs_hash_cond_arg_t *cond = data;
1500 if (cond->func(cfs_hash_object(hs, hnode), cond->arg))
1501 cfs_hash_bd_del_locked(hs, bd, hnode);
1506 * Delete item from the libcfs hash @hs when @func return true.
1507 * The write lock being hold during loop for each bucket to avoid
1508 * any object be reference.
1511 cfs_hash_cond_del(struct cfs_hash *hs, cfs_hash_cond_opt_cb_t func, void *data)
1513 cfs_hash_cond_arg_t arg = {
1518 cfs_hash_for_each_tight(hs, cfs_hash_cond_del_locked, &arg, 1);
1520 EXPORT_SYMBOL(cfs_hash_cond_del);
1523 cfs_hash_for_each(struct cfs_hash *hs,
1524 cfs_hash_for_each_cb_t func, void *data)
1526 cfs_hash_for_each_tight(hs, func, data, 0);
1528 EXPORT_SYMBOL(cfs_hash_for_each);
1531 cfs_hash_for_each_safe(struct cfs_hash *hs,
1532 cfs_hash_for_each_cb_t func, void *data)
1534 cfs_hash_for_each_tight(hs, func, data, 1);
1536 EXPORT_SYMBOL(cfs_hash_for_each_safe);
1539 cfs_hash_peek(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1540 struct hlist_node *hnode, void *data)
1543 return 1; /* return 1 to break the loop */
1547 cfs_hash_is_empty(struct cfs_hash *hs)
1551 cfs_hash_for_each_tight(hs, cfs_hash_peek, &empty, 0);
1554 EXPORT_SYMBOL(cfs_hash_is_empty);
1557 cfs_hash_size_get(struct cfs_hash *hs)
1559 return cfs_hash_with_counter(hs) ?
1560 atomic_read(&hs->hs_count) :
1561 cfs_hash_for_each_tight(hs, NULL, NULL, 0);
1563 EXPORT_SYMBOL(cfs_hash_size_get);
1566 * cfs_hash_for_each_relax:
1567 * Iterate the hash table and call @func on each item without
1568 * any lock. This function can't guarantee to finish iteration
1569 * if these features are enabled:
1571 * a. if rehash_key is enabled, an item can be moved from
1572 * one bucket to another bucket
1573 * b. user can remove non-zero-ref item from hash-table,
1574 * so the item can be removed from hash-table, even worse,
1575 * it's possible that user changed key and insert to another
1577 * there's no way for us to finish iteration correctly on previous
1578 * two cases, so iteration has to be stopped on change.
1581 cfs_hash_for_each_relax(struct cfs_hash *hs, cfs_hash_for_each_cb_t func, void *data)
1583 struct hlist_node *hnode;
1584 struct hlist_node *tmp;
1585 struct cfs_hash_bd bd;
1592 stop_on_change = cfs_hash_with_rehash_key(hs) ||
1593 !cfs_hash_with_no_itemref(hs) ||
1594 CFS_HOP(hs, put_locked) == NULL;
1595 cfs_hash_lock(hs, 0);
1596 LASSERT(!cfs_hash_is_rehashing(hs));
1598 cfs_hash_for_each_bucket(hs, &bd, i) {
1599 struct hlist_head *hhead;
1601 cfs_hash_bd_lock(hs, &bd, 0);
1602 version = cfs_hash_bd_version_get(&bd);
1604 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1605 for (hnode = hhead->first; hnode != NULL;) {
1606 cfs_hash_bucket_validate(hs, &bd, hnode);
1607 cfs_hash_get(hs, hnode);
1608 cfs_hash_bd_unlock(hs, &bd, 0);
1609 cfs_hash_unlock(hs, 0);
1611 rc = func(hs, &bd, hnode, data);
1613 cfs_hash_put(hs, hnode);
1617 cfs_hash_lock(hs, 0);
1618 cfs_hash_bd_lock(hs, &bd, 0);
1619 if (!stop_on_change) {
1621 cfs_hash_put_locked(hs, hnode);
1623 } else { /* bucket changed? */
1625 cfs_hash_bd_version_get(&bd))
1627 /* safe to continue because no change */
1628 hnode = hnode->next;
1630 if (rc) /* callback wants to break iteration */
1634 cfs_hash_bd_unlock(hs, &bd, 0);
1636 cfs_hash_unlock(hs, 0);
1642 cfs_hash_for_each_nolock(struct cfs_hash *hs,
1643 cfs_hash_for_each_cb_t func, void *data)
1645 if (cfs_hash_with_no_lock(hs) ||
1646 cfs_hash_with_rehash_key(hs) ||
1647 !cfs_hash_with_no_itemref(hs))
1650 if (CFS_HOP(hs, get) == NULL ||
1651 (CFS_HOP(hs, put) == NULL &&
1652 CFS_HOP(hs, put_locked) == NULL))
1655 cfs_hash_for_each_enter(hs);
1656 cfs_hash_for_each_relax(hs, func, data);
1657 cfs_hash_for_each_exit(hs);
1661 EXPORT_SYMBOL(cfs_hash_for_each_nolock);
1664 * For each hash bucket in the libcfs hash @hs call the passed callback
1665 * @func until all the hash buckets are empty. The passed callback @func
1666 * or the previously registered callback hs->hs_put must remove the item
1667 * from the hash. You may either use the cfs_hash_del() or hlist_del()
1668 * functions. No rwlocks will be held during the callback @func it is
1669 * safe to sleep if needed. This function will not terminate until the
1670 * hash is empty. Note it is still possible to concurrently add new
1671 * items in to the hash. It is the callers responsibility to ensure
1672 * the required locking is in place to prevent concurrent insertions.
1675 cfs_hash_for_each_empty(struct cfs_hash *hs,
1676 cfs_hash_for_each_cb_t func, void *data)
1680 if (cfs_hash_with_no_lock(hs))
1683 if (CFS_HOP(hs, get) == NULL ||
1684 (CFS_HOP(hs, put) == NULL &&
1685 CFS_HOP(hs, put_locked) == NULL))
1688 cfs_hash_for_each_enter(hs);
1689 while (cfs_hash_for_each_relax(hs, func, data)) {
1690 CDEBUG(D_INFO, "Try to empty hash: %s, loop: %u\n",
1693 cfs_hash_for_each_exit(hs);
1696 EXPORT_SYMBOL(cfs_hash_for_each_empty);
1699 cfs_hash_hlist_for_each(struct cfs_hash *hs, unsigned hindex,
1700 cfs_hash_for_each_cb_t func, void *data)
1702 struct hlist_head *hhead;
1703 struct hlist_node *hnode;
1704 struct cfs_hash_bd bd;
1706 cfs_hash_for_each_enter(hs);
1707 cfs_hash_lock(hs, 0);
1708 if (hindex >= CFS_HASH_NHLIST(hs))
1711 cfs_hash_bd_index_set(hs, hindex, &bd);
1713 cfs_hash_bd_lock(hs, &bd, 0);
1714 hhead = cfs_hash_bd_hhead(hs, &bd);
1715 hlist_for_each(hnode, hhead) {
1716 if (func(hs, &bd, hnode, data))
1719 cfs_hash_bd_unlock(hs, &bd, 0);
1721 cfs_hash_unlock(hs, 0);
1722 cfs_hash_for_each_exit(hs);
1725 EXPORT_SYMBOL(cfs_hash_hlist_for_each);
1728 * For each item in the libcfs hash @hs which matches the @key call
1729 * the passed callback @func and pass to it as an argument each hash
1730 * item and the private @data. During the callback the bucket lock
1731 * is held so the callback must never sleep.
1734 cfs_hash_for_each_key(struct cfs_hash *hs, const void *key,
1735 cfs_hash_for_each_cb_t func, void *data)
1737 struct hlist_node *hnode;
1738 struct cfs_hash_bd bds[2];
1741 cfs_hash_lock(hs, 0);
1743 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1745 cfs_hash_for_each_bd(bds, 2, i) {
1746 struct hlist_head *hlist = cfs_hash_bd_hhead(hs, &bds[i]);
1748 hlist_for_each(hnode, hlist) {
1749 cfs_hash_bucket_validate(hs, &bds[i], hnode);
1751 if (cfs_hash_keycmp(hs, key, hnode)) {
1752 if (func(hs, &bds[i], hnode, data))
1758 cfs_hash_dual_bd_unlock(hs, bds, 0);
1759 cfs_hash_unlock(hs, 0);
1761 EXPORT_SYMBOL(cfs_hash_for_each_key);
1764 * Rehash the libcfs hash @hs to the given @bits. This can be used
1765 * to grow the hash size when excessive chaining is detected, or to
1766 * shrink the hash when it is larger than needed. When the CFS_HASH_REHASH
1767 * flag is set in @hs the libcfs hash may be dynamically rehashed
1768 * during addition or removal if the hash's theta value exceeds
1769 * either the hs->hs_min_theta or hs->max_theta values. By default
1770 * these values are tuned to keep the chained hash depth small, and
1771 * this approach assumes a reasonably uniform hashing function. The
1772 * theta thresholds for @hs are tunable via cfs_hash_set_theta().
1775 cfs_hash_rehash_cancel_locked(struct cfs_hash *hs)
1779 /* need hold cfs_hash_lock(hs, 1) */
1780 LASSERT(cfs_hash_with_rehash(hs) &&
1781 !cfs_hash_with_no_lock(hs));
1783 if (!cfs_hash_is_rehashing(hs))
1786 if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_rehash_wi)) {
1787 hs->hs_rehash_bits = 0;
1791 for (i = 2; cfs_hash_is_rehashing(hs); i++) {
1792 cfs_hash_unlock(hs, 1);
1793 /* raise console warning while waiting too long */
1794 CDEBUG(IS_PO2(i >> 3) ? D_WARNING : D_INFO,
1795 "hash %s is still rehashing, rescheded %d\n",
1796 hs->hs_name, i - 1);
1798 cfs_hash_lock(hs, 1);
1801 EXPORT_SYMBOL(cfs_hash_rehash_cancel_locked);
1804 cfs_hash_rehash_cancel(struct cfs_hash *hs)
1806 cfs_hash_lock(hs, 1);
1807 cfs_hash_rehash_cancel_locked(hs);
1808 cfs_hash_unlock(hs, 1);
1810 EXPORT_SYMBOL(cfs_hash_rehash_cancel);
1813 cfs_hash_rehash(struct cfs_hash *hs, int do_rehash)
1817 LASSERT(cfs_hash_with_rehash(hs) && !cfs_hash_with_no_lock(hs));
1819 cfs_hash_lock(hs, 1);
1821 rc = cfs_hash_rehash_bits(hs);
1823 cfs_hash_unlock(hs, 1);
1827 hs->hs_rehash_bits = rc;
1829 /* launch and return */
1830 cfs_wi_schedule(cfs_sched_rehash, &hs->hs_rehash_wi);
1831 cfs_hash_unlock(hs, 1);
1835 /* rehash right now */
1836 cfs_hash_unlock(hs, 1);
1838 return cfs_hash_rehash_worker(&hs->hs_rehash_wi);
1840 EXPORT_SYMBOL(cfs_hash_rehash);
1843 cfs_hash_rehash_bd(struct cfs_hash *hs, struct cfs_hash_bd *old)
1845 struct cfs_hash_bd new;
1846 struct hlist_head *hhead;
1847 struct hlist_node *hnode;
1848 struct hlist_node *pos;
1852 /* hold cfs_hash_lock(hs, 1), so don't need any bucket lock */
1853 cfs_hash_bd_for_each_hlist(hs, old, hhead) {
1854 hlist_for_each_safe(hnode, pos, hhead) {
1855 key = cfs_hash_key(hs, hnode);
1856 LASSERT(key != NULL);
1857 /* Validate hnode is in the correct bucket. */
1858 cfs_hash_bucket_validate(hs, old, hnode);
1860 * Delete from old hash bucket; move to new bucket.
1861 * ops->hs_key must be defined.
1863 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
1864 hs->hs_rehash_bits, key, &new);
1865 cfs_hash_bd_move_locked(hs, old, &new, hnode);
1874 cfs_hash_rehash_worker(cfs_workitem_t *wi)
1876 struct cfs_hash *hs = container_of(wi, struct cfs_hash, hs_rehash_wi);
1877 struct cfs_hash_bucket **bkts;
1878 struct cfs_hash_bd bd;
1879 unsigned int old_size;
1880 unsigned int new_size;
1886 LASSERT (hs != NULL && cfs_hash_with_rehash(hs));
1888 cfs_hash_lock(hs, 0);
1889 LASSERT(cfs_hash_is_rehashing(hs));
1891 old_size = CFS_HASH_NBKT(hs);
1892 new_size = CFS_HASH_RH_NBKT(hs);
1894 cfs_hash_unlock(hs, 0);
1897 * don't need hs::hs_rwlock for hs::hs_buckets,
1898 * because nobody can change bkt-table except me.
1900 bkts = cfs_hash_buckets_realloc(hs, hs->hs_buckets,
1901 old_size, new_size);
1902 cfs_hash_lock(hs, 1);
1908 if (bkts == hs->hs_buckets) {
1909 bkts = NULL; /* do nothing */
1913 rc = __cfs_hash_theta(hs);
1914 if ((rc >= hs->hs_min_theta) && (rc <= hs->hs_max_theta)) {
1915 /* free the new allocated bkt-table */
1916 old_size = new_size;
1917 new_size = CFS_HASH_NBKT(hs);
1922 LASSERT(hs->hs_rehash_buckets == NULL);
1923 hs->hs_rehash_buckets = bkts;
1926 cfs_hash_for_each_bucket(hs, &bd, i) {
1927 if (cfs_hash_is_exiting(hs)) {
1929 /* someone wants to destroy the hash, abort now */
1930 if (old_size < new_size) /* OK to free old bkt-table */
1932 /* it's shrinking, need free new bkt-table */
1933 hs->hs_rehash_buckets = NULL;
1934 old_size = new_size;
1935 new_size = CFS_HASH_NBKT(hs);
1939 count += cfs_hash_rehash_bd(hs, &bd);
1940 if (count < CFS_HASH_LOOP_HOG ||
1941 cfs_hash_is_iterating(hs)) { /* need to finish ASAP */
1946 cfs_hash_unlock(hs, 1);
1948 cfs_hash_lock(hs, 1);
1951 hs->hs_rehash_count++;
1953 bkts = hs->hs_buckets;
1954 hs->hs_buckets = hs->hs_rehash_buckets;
1955 hs->hs_rehash_buckets = NULL;
1957 hs->hs_cur_bits = hs->hs_rehash_bits;
1959 hs->hs_rehash_bits = 0;
1960 if (rc == -ESRCH) /* never be scheduled again */
1961 cfs_wi_exit(cfs_sched_rehash, wi);
1962 bsize = cfs_hash_bkt_size(hs);
1963 cfs_hash_unlock(hs, 1);
1964 /* can't refer to @hs anymore because it could be destroyed */
1966 cfs_hash_buckets_free(bkts, bsize, new_size, old_size);
1968 CDEBUG(D_INFO, "early quit of rehashing: %d\n", rc);
1969 /* return 1 only if cfs_wi_exit is called */
1970 return rc == -ESRCH;
1974 * Rehash the object referenced by @hnode in the libcfs hash @hs. The
1975 * @old_key must be provided to locate the objects previous location
1976 * in the hash, and the @new_key will be used to reinsert the object.
1977 * Use this function instead of a cfs_hash_add() + cfs_hash_del()
1978 * combo when it is critical that there is no window in time where the
1979 * object is missing from the hash. When an object is being rehashed
1980 * the registered cfs_hash_get() and cfs_hash_put() functions will
1983 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
1984 void *new_key, struct hlist_node *hnode)
1986 struct cfs_hash_bd bds[3];
1987 struct cfs_hash_bd old_bds[2];
1988 struct cfs_hash_bd new_bd;
1990 LASSERT(!hlist_unhashed(hnode));
1992 cfs_hash_lock(hs, 0);
1994 cfs_hash_dual_bd_get(hs, old_key, old_bds);
1995 cfs_hash_bd_get(hs, new_key, &new_bd);
1997 bds[0] = old_bds[0];
1998 bds[1] = old_bds[1];
2001 /* NB: bds[0] and bds[1] are ordered already */
2002 cfs_hash_bd_order(&bds[1], &bds[2]);
2003 cfs_hash_bd_order(&bds[0], &bds[1]);
2005 cfs_hash_multi_bd_lock(hs, bds, 3, 1);
2006 if (likely(old_bds[1].bd_bucket == NULL)) {
2007 cfs_hash_bd_move_locked(hs, &old_bds[0], &new_bd, hnode);
2009 cfs_hash_dual_bd_finddel_locked(hs, old_bds, old_key, hnode);
2010 cfs_hash_bd_add_locked(hs, &new_bd, hnode);
2012 /* overwrite key inside locks, otherwise may screw up with
2013 * other operations, i.e: rehash */
2014 cfs_hash_keycpy(hs, new_key, hnode);
2016 cfs_hash_multi_bd_unlock(hs, bds, 3, 1);
2017 cfs_hash_unlock(hs, 0);
2019 EXPORT_SYMBOL(cfs_hash_rehash_key);
2021 int cfs_hash_debug_header(struct seq_file *m)
2023 return seq_printf(m, "%-*s%6s%6s%6s%6s%6s%6s%6s%7s%8s%8s%8s%s\n",
2024 CFS_HASH_BIGNAME_LEN,
2025 "name", "cur", "min", "max", "theta", "t-min", "t-max",
2026 "flags", "rehash", "count", "maxdep", "maxdepb",
2029 EXPORT_SYMBOL(cfs_hash_debug_header);
2031 static struct cfs_hash_bucket **
2032 cfs_hash_full_bkts(struct cfs_hash *hs)
2034 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2035 if (hs->hs_rehash_buckets == NULL)
2036 return hs->hs_buckets;
2038 LASSERT(hs->hs_rehash_bits != 0);
2039 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2040 hs->hs_rehash_buckets : hs->hs_buckets;
2044 cfs_hash_full_nbkt(struct cfs_hash *hs)
2046 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2047 if (hs->hs_rehash_buckets == NULL)
2048 return CFS_HASH_NBKT(hs);
2050 LASSERT(hs->hs_rehash_bits != 0);
2051 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2052 CFS_HASH_RH_NBKT(hs) : CFS_HASH_NBKT(hs);
2055 int cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m)
2057 int dist[8] = { 0, };
2064 cfs_hash_lock(hs, 0);
2065 theta = __cfs_hash_theta(hs);
2067 seq_printf(m, "%-*s %5d %5d %5d %d.%03d %d.%03d %d.%03d 0x%02x %6d ",
2068 CFS_HASH_BIGNAME_LEN, hs->hs_name,
2069 1 << hs->hs_cur_bits, 1 << hs->hs_min_bits,
2070 1 << hs->hs_max_bits,
2071 __cfs_hash_theta_int(theta), __cfs_hash_theta_frac(theta),
2072 __cfs_hash_theta_int(hs->hs_min_theta),
2073 __cfs_hash_theta_frac(hs->hs_min_theta),
2074 __cfs_hash_theta_int(hs->hs_max_theta),
2075 __cfs_hash_theta_frac(hs->hs_max_theta),
2076 hs->hs_flags, hs->hs_rehash_count);
2079 * The distribution is a summary of the chained hash depth in
2080 * each of the libcfs hash buckets. Each buckets hsb_count is
2081 * divided by the hash theta value and used to generate a
2082 * histogram of the hash distribution. A uniform hash will
2083 * result in all hash buckets being close to the average thus
2084 * only the first few entries in the histogram will be non-zero.
2085 * If you hash function results in a non-uniform hash the will
2086 * be observable by outlier bucks in the distribution histogram.
2088 * Uniform hash distribution: 128/128/0/0/0/0/0/0
2089 * Non-Uniform hash distribution: 128/125/0/0/0/0/2/1
2091 for (i = 0; i < cfs_hash_full_nbkt(hs); i++) {
2092 struct cfs_hash_bd bd;
2094 bd.bd_bucket = cfs_hash_full_bkts(hs)[i];
2095 cfs_hash_bd_lock(hs, &bd, 0);
2096 if (maxdep < bd.bd_bucket->hsb_depmax) {
2097 maxdep = bd.bd_bucket->hsb_depmax;
2098 maxdepb = ffz(~maxdep);
2100 total += bd.bd_bucket->hsb_count;
2101 dist[min(__cfs_fls(bd.bd_bucket->hsb_count/max(theta,1)),7)]++;
2102 cfs_hash_bd_unlock(hs, &bd, 0);
2105 seq_printf(m, "%7d %7d %7d ", total, maxdep, maxdepb);
2106 for (i = 0; i < 8; i++)
2107 seq_printf(m, "%d%c", dist[i], (i == 7) ? '\n' : '/');
2109 cfs_hash_unlock(hs, 0);
2113 EXPORT_SYMBOL(cfs_hash_debug_str);