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, 2015, 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(instead 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 specify 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.
109 #include <linux/seq_file.h>
111 #include <libcfs/libcfs.h>
113 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
114 static unsigned int warn_on_depth = 8;
115 module_param(warn_on_depth, uint, 0644);
116 MODULE_PARM_DESC(warn_on_depth, "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)
129 __acquires(&lock->spin)
131 spin_lock(&lock->spin);
135 cfs_hash_spin_unlock(union cfs_hash_lock *lock, int exclusive)
136 __releases(&lock->spin)
138 spin_unlock(&lock->spin);
142 cfs_hash_rw_lock(union cfs_hash_lock *lock, int exclusive)
143 __acquires(&lock->rw)
146 read_lock(&lock->rw);
148 write_lock(&lock->rw);
152 cfs_hash_rw_unlock(union cfs_hash_lock *lock, int exclusive)
153 __releases(&lock->rw)
156 read_unlock(&lock->rw);
158 write_unlock(&lock->rw);
162 static struct cfs_hash_lock_ops cfs_hash_nl_lops = {
163 .hs_lock = cfs_hash_nl_lock,
164 .hs_unlock = cfs_hash_nl_unlock,
165 .hs_bkt_lock = cfs_hash_nl_lock,
166 .hs_bkt_unlock = cfs_hash_nl_unlock,
169 /** no bucket lock, one spinlock to protect everything */
170 static struct cfs_hash_lock_ops cfs_hash_nbl_lops = {
171 .hs_lock = cfs_hash_spin_lock,
172 .hs_unlock = cfs_hash_spin_unlock,
173 .hs_bkt_lock = cfs_hash_nl_lock,
174 .hs_bkt_unlock = cfs_hash_nl_unlock,
177 /** spin bucket lock, rehash is enabled */
178 static struct cfs_hash_lock_ops cfs_hash_bkt_spin_lops = {
179 .hs_lock = cfs_hash_rw_lock,
180 .hs_unlock = cfs_hash_rw_unlock,
181 .hs_bkt_lock = cfs_hash_spin_lock,
182 .hs_bkt_unlock = cfs_hash_spin_unlock,
185 /** rw bucket lock, rehash is enabled */
186 static struct cfs_hash_lock_ops 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 struct cfs_hash_lock_ops cfs_hash_nr_bkt_spin_lops = {
195 .hs_lock = cfs_hash_nl_lock,
196 .hs_unlock = cfs_hash_nl_unlock,
197 .hs_bkt_lock = cfs_hash_spin_lock,
198 .hs_bkt_unlock = cfs_hash_spin_unlock,
201 /** rw bucket lock, rehash is disabled */
202 static struct cfs_hash_lock_ops cfs_hash_nr_bkt_rw_lops = {
203 .hs_lock = cfs_hash_nl_lock,
204 .hs_unlock = cfs_hash_nl_unlock,
205 .hs_bkt_lock = cfs_hash_rw_lock,
206 .hs_bkt_unlock = cfs_hash_rw_unlock,
210 cfs_hash_lock_setup(struct cfs_hash *hs)
212 if (cfs_hash_with_no_lock(hs)) {
213 hs->hs_lops = &cfs_hash_nl_lops;
215 } else if (cfs_hash_with_no_bktlock(hs)) {
216 hs->hs_lops = &cfs_hash_nbl_lops;
217 spin_lock_init(&hs->hs_lock.spin);
219 } else if (cfs_hash_with_rehash(hs)) {
220 rwlock_init(&hs->hs_lock.rw);
222 if (cfs_hash_with_rw_bktlock(hs))
223 hs->hs_lops = &cfs_hash_bkt_rw_lops;
224 else if (cfs_hash_with_spin_bktlock(hs))
225 hs->hs_lops = &cfs_hash_bkt_spin_lops;
229 if (cfs_hash_with_rw_bktlock(hs))
230 hs->hs_lops = &cfs_hash_nr_bkt_rw_lops;
231 else if (cfs_hash_with_spin_bktlock(hs))
232 hs->hs_lops = &cfs_hash_nr_bkt_spin_lops;
239 * Simple hash head without depth tracking
240 * new element is always added to head of hlist
242 struct cfs_hash_head {
243 struct hlist_head hh_head; /**< entries list */
247 cfs_hash_hh_hhead_size(struct cfs_hash *hs)
249 return sizeof(struct cfs_hash_head);
252 static struct hlist_head *
253 cfs_hash_hh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
255 struct cfs_hash_head *head;
257 head = (struct cfs_hash_head *)&bd->bd_bucket->hsb_head[0];
258 return &head[bd->bd_offset].hh_head;
262 cfs_hash_hh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
263 struct hlist_node *hnode)
265 hlist_add_head(hnode, cfs_hash_hh_hhead(hs, bd));
266 return -1; /* unknown depth */
270 cfs_hash_hh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
271 struct hlist_node *hnode)
273 hlist_del_init(hnode);
274 return -1; /* unknown depth */
278 * Simple hash head with depth tracking
279 * new element is always added to head of hlist
281 struct cfs_hash_head_dep {
282 struct hlist_head hd_head; /**< entries list */
283 unsigned int hd_depth; /**< list length */
287 cfs_hash_hd_hhead_size(struct cfs_hash *hs)
289 return sizeof(struct cfs_hash_head_dep);
292 static struct hlist_head *
293 cfs_hash_hd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
295 struct cfs_hash_head_dep *head;
297 head = (struct cfs_hash_head_dep *)&bd->bd_bucket->hsb_head[0];
298 return &head[bd->bd_offset].hd_head;
302 cfs_hash_hd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
303 struct hlist_node *hnode)
305 struct cfs_hash_head_dep *hh;
307 hh = container_of(cfs_hash_hd_hhead(hs, bd),
308 struct cfs_hash_head_dep, hd_head);
309 hlist_add_head(hnode, &hh->hd_head);
310 return ++hh->hd_depth;
314 cfs_hash_hd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
315 struct hlist_node *hnode)
317 struct cfs_hash_head_dep *hh;
319 hh = container_of(cfs_hash_hd_hhead(hs, bd),
320 struct cfs_hash_head_dep, hd_head);
321 hlist_del_init(hnode);
322 return --hh->hd_depth;
326 * double links hash head without depth tracking
327 * new element is always added to tail of hlist
329 struct cfs_hash_dhead {
330 struct hlist_head dh_head; /**< entries list */
331 struct hlist_node *dh_tail; /**< the last entry */
335 cfs_hash_dh_hhead_size(struct cfs_hash *hs)
337 return sizeof(struct cfs_hash_dhead);
340 static struct hlist_head *
341 cfs_hash_dh_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
343 struct cfs_hash_dhead *head;
345 head = (struct cfs_hash_dhead *)&bd->bd_bucket->hsb_head[0];
346 return &head[bd->bd_offset].dh_head;
350 cfs_hash_dh_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
351 struct hlist_node *hnode)
353 struct cfs_hash_dhead *dh;
355 dh = container_of(cfs_hash_dh_hhead(hs, bd),
356 struct cfs_hash_dhead, dh_head);
357 if (dh->dh_tail != NULL) /* not empty */
358 hlist_add_behind(hnode, dh->dh_tail);
359 else /* empty list */
360 hlist_add_head(hnode, &dh->dh_head);
362 return -1; /* unknown depth */
366 cfs_hash_dh_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
367 struct hlist_node *hnd)
369 struct cfs_hash_dhead *dh;
371 dh = container_of(cfs_hash_dh_hhead(hs, bd),
372 struct cfs_hash_dhead, dh_head);
373 if (hnd->next == NULL) { /* it's the tail */
374 dh->dh_tail = (hnd->pprev == &dh->dh_head.first) ? NULL :
375 container_of(hnd->pprev, struct hlist_node, next);
378 return -1; /* unknown depth */
382 * double links hash head with depth tracking
383 * new element is always added to tail of hlist
385 struct cfs_hash_dhead_dep {
386 struct hlist_head dd_head; /**< entries list */
387 struct hlist_node *dd_tail; /**< the last entry */
388 unsigned int dd_depth; /**< list length */
392 cfs_hash_dd_hhead_size(struct cfs_hash *hs)
394 return sizeof(struct cfs_hash_dhead_dep);
397 static struct hlist_head *
398 cfs_hash_dd_hhead(struct cfs_hash *hs, struct cfs_hash_bd *bd)
400 struct cfs_hash_dhead_dep *head;
402 head = (struct cfs_hash_dhead_dep *)&bd->bd_bucket->hsb_head[0];
403 return &head[bd->bd_offset].dd_head;
407 cfs_hash_dd_hnode_add(struct cfs_hash *hs, struct cfs_hash_bd *bd,
408 struct hlist_node *hnode)
410 struct cfs_hash_dhead_dep *dh;
412 dh = container_of(cfs_hash_dd_hhead(hs, bd),
413 struct cfs_hash_dhead_dep, dd_head);
414 if (dh->dd_tail != NULL) /* not empty */
415 hlist_add_behind(hnode, dh->dd_tail);
416 else /* empty list */
417 hlist_add_head(hnode, &dh->dd_head);
419 return ++dh->dd_depth;
423 cfs_hash_dd_hnode_del(struct cfs_hash *hs, struct cfs_hash_bd *bd,
424 struct hlist_node *hnd)
426 struct cfs_hash_dhead_dep *dh;
428 dh = container_of(cfs_hash_dd_hhead(hs, bd),
429 struct cfs_hash_dhead_dep, dd_head);
430 if (hnd->next == NULL) { /* it's the tail */
431 dh->dd_tail = (hnd->pprev == &dh->dd_head.first) ? NULL :
432 container_of(hnd->pprev, struct hlist_node, next);
435 return --dh->dd_depth;
438 static struct cfs_hash_hlist_ops cfs_hash_hh_hops = {
439 .hop_hhead = cfs_hash_hh_hhead,
440 .hop_hhead_size = cfs_hash_hh_hhead_size,
441 .hop_hnode_add = cfs_hash_hh_hnode_add,
442 .hop_hnode_del = cfs_hash_hh_hnode_del,
445 static struct cfs_hash_hlist_ops cfs_hash_hd_hops = {
446 .hop_hhead = cfs_hash_hd_hhead,
447 .hop_hhead_size = cfs_hash_hd_hhead_size,
448 .hop_hnode_add = cfs_hash_hd_hnode_add,
449 .hop_hnode_del = cfs_hash_hd_hnode_del,
452 static struct cfs_hash_hlist_ops cfs_hash_dh_hops = {
453 .hop_hhead = cfs_hash_dh_hhead,
454 .hop_hhead_size = cfs_hash_dh_hhead_size,
455 .hop_hnode_add = cfs_hash_dh_hnode_add,
456 .hop_hnode_del = cfs_hash_dh_hnode_del,
459 static struct cfs_hash_hlist_ops cfs_hash_dd_hops = {
460 .hop_hhead = cfs_hash_dd_hhead,
461 .hop_hhead_size = cfs_hash_dd_hhead_size,
462 .hop_hnode_add = cfs_hash_dd_hnode_add,
463 .hop_hnode_del = cfs_hash_dd_hnode_del,
467 cfs_hash_hlist_setup(struct cfs_hash *hs)
469 if (cfs_hash_with_add_tail(hs)) {
470 hs->hs_hops = cfs_hash_with_depth(hs) ?
471 &cfs_hash_dd_hops : &cfs_hash_dh_hops;
473 hs->hs_hops = cfs_hash_with_depth(hs) ?
474 &cfs_hash_hd_hops : &cfs_hash_hh_hops;
479 cfs_hash_bd_from_key(struct cfs_hash *hs, struct cfs_hash_bucket **bkts,
480 unsigned int bits, const void *key, struct cfs_hash_bd *bd)
482 unsigned int index = cfs_hash_id(hs, key, (1U << bits) - 1);
484 LASSERT(bits == hs->hs_cur_bits || bits == hs->hs_rehash_bits);
486 bd->bd_bucket = bkts[index & ((1U << (bits - hs->hs_bkt_bits)) - 1)];
487 bd->bd_offset = index >> (bits - hs->hs_bkt_bits);
491 cfs_hash_bd_get(struct cfs_hash *hs, const void *key, struct cfs_hash_bd *bd)
493 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
494 if (likely(hs->hs_rehash_buckets == NULL)) {
495 cfs_hash_bd_from_key(hs, hs->hs_buckets,
496 hs->hs_cur_bits, key, bd);
498 LASSERT(hs->hs_rehash_bits != 0);
499 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
500 hs->hs_rehash_bits, key, bd);
503 EXPORT_SYMBOL(cfs_hash_bd_get);
506 cfs_hash_bd_dep_record(struct cfs_hash *hs, struct cfs_hash_bd *bd, int dep_cur)
508 if (likely(dep_cur <= bd->bd_bucket->hsb_depmax))
511 bd->bd_bucket->hsb_depmax = dep_cur;
512 # if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
513 if (likely(warn_on_depth == 0 ||
514 max(warn_on_depth, hs->hs_dep_max) >= dep_cur))
517 spin_lock(&hs->hs_dep_lock);
518 hs->hs_dep_max = dep_cur;
519 hs->hs_dep_bkt = bd->bd_bucket->hsb_index;
520 hs->hs_dep_off = bd->bd_offset;
521 hs->hs_dep_bits = hs->hs_cur_bits;
522 spin_unlock(&hs->hs_dep_lock);
524 cfs_wi_schedule(cfs_sched_rehash, &hs->hs_dep_wi);
529 cfs_hash_bd_add_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
530 struct hlist_node *hnode)
534 rc = hs->hs_hops->hop_hnode_add(hs, bd, hnode);
535 cfs_hash_bd_dep_record(hs, bd, rc);
536 bd->bd_bucket->hsb_version++;
537 if (unlikely(bd->bd_bucket->hsb_version == 0))
538 bd->bd_bucket->hsb_version++;
539 bd->bd_bucket->hsb_count++;
541 if (cfs_hash_with_counter(hs))
542 atomic_inc(&hs->hs_count);
543 if (!cfs_hash_with_no_itemref(hs))
544 cfs_hash_get(hs, hnode);
546 EXPORT_SYMBOL(cfs_hash_bd_add_locked);
549 cfs_hash_bd_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
550 struct hlist_node *hnode)
552 hs->hs_hops->hop_hnode_del(hs, bd, hnode);
554 LASSERT(bd->bd_bucket->hsb_count > 0);
555 bd->bd_bucket->hsb_count--;
556 bd->bd_bucket->hsb_version++;
557 if (unlikely(bd->bd_bucket->hsb_version == 0))
558 bd->bd_bucket->hsb_version++;
560 if (cfs_hash_with_counter(hs)) {
561 LASSERT(atomic_read(&hs->hs_count) > 0);
562 atomic_dec(&hs->hs_count);
564 if (!cfs_hash_with_no_itemref(hs))
565 cfs_hash_put_locked(hs, hnode);
567 EXPORT_SYMBOL(cfs_hash_bd_del_locked);
570 cfs_hash_bd_move_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd_old,
571 struct cfs_hash_bd *bd_new, struct hlist_node *hnode)
573 struct cfs_hash_bucket *obkt = bd_old->bd_bucket;
574 struct cfs_hash_bucket *nbkt = bd_new->bd_bucket;
577 if (cfs_hash_bd_compare(bd_old, bd_new) == 0)
580 /* use cfs_hash_bd_hnode_add/del, to avoid atomic & refcount ops
581 * in cfs_hash_bd_del/add_locked */
582 hs->hs_hops->hop_hnode_del(hs, bd_old, hnode);
583 rc = hs->hs_hops->hop_hnode_add(hs, bd_new, hnode);
584 cfs_hash_bd_dep_record(hs, bd_new, rc);
586 LASSERT(obkt->hsb_count > 0);
589 if (unlikely(obkt->hsb_version == 0))
593 if (unlikely(nbkt->hsb_version == 0))
598 /** always set, for sanity (avoid ZERO intent) */
599 CFS_HS_LOOKUP_MASK_FIND = 1 << 0,
600 /** return entry with a ref */
601 CFS_HS_LOOKUP_MASK_REF = 1 << 1,
602 /** add entry if not existing */
603 CFS_HS_LOOKUP_MASK_ADD = 1 << 2,
604 /** delete entry, ignore other masks */
605 CFS_HS_LOOKUP_MASK_DEL = 1 << 3,
608 enum cfs_hash_lookup_intent {
609 /** return item w/o refcount */
610 CFS_HS_LOOKUP_IT_PEEK = CFS_HS_LOOKUP_MASK_FIND,
611 /** return item with refcount */
612 CFS_HS_LOOKUP_IT_FIND = (CFS_HS_LOOKUP_MASK_FIND |
613 CFS_HS_LOOKUP_MASK_REF),
614 /** return item w/o refcount if existed, otherwise add */
615 CFS_HS_LOOKUP_IT_ADD = (CFS_HS_LOOKUP_MASK_FIND |
616 CFS_HS_LOOKUP_MASK_ADD),
617 /** return item with refcount if existed, otherwise add */
618 CFS_HS_LOOKUP_IT_FINDADD = (CFS_HS_LOOKUP_IT_FIND |
619 CFS_HS_LOOKUP_MASK_ADD),
620 /** delete if existed */
621 CFS_HS_LOOKUP_IT_FINDDEL = (CFS_HS_LOOKUP_MASK_FIND |
622 CFS_HS_LOOKUP_MASK_DEL)
625 static struct hlist_node *
626 cfs_hash_bd_lookup_intent(struct cfs_hash *hs, struct cfs_hash_bd *bd,
627 const void *key, struct hlist_node *hnode,
628 enum cfs_hash_lookup_intent intent)
631 struct hlist_head *hhead = cfs_hash_bd_hhead(hs, bd);
632 struct hlist_node *ehnode;
633 struct hlist_node *match;
634 int intent_add = (intent & CFS_HS_LOOKUP_MASK_ADD) != 0;
636 /* with this function, we can avoid a lot of useless refcount ops,
637 * which are expensive atomic operations most time. */
638 match = intent_add ? NULL : hnode;
639 hlist_for_each(ehnode, hhead) {
640 if (!cfs_hash_keycmp(hs, key, ehnode))
643 if (match != NULL && match != ehnode) /* can't match */
647 if ((intent & CFS_HS_LOOKUP_MASK_DEL) != 0) {
648 cfs_hash_bd_del_locked(hs, bd, ehnode);
652 /* caller wants refcount? */
653 if ((intent & CFS_HS_LOOKUP_MASK_REF) != 0)
654 cfs_hash_get(hs, ehnode);
661 LASSERT(hnode != NULL);
662 cfs_hash_bd_add_locked(hs, bd, hnode);
667 cfs_hash_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
670 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
671 CFS_HS_LOOKUP_IT_FIND);
673 EXPORT_SYMBOL(cfs_hash_bd_lookup_locked);
676 cfs_hash_bd_peek_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
679 return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
680 CFS_HS_LOOKUP_IT_PEEK);
682 EXPORT_SYMBOL(cfs_hash_bd_peek_locked);
685 cfs_hash_multi_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
686 unsigned n, int excl)
688 struct cfs_hash_bucket *prev = NULL;
692 * bds must be ascendantly ordered by bd->bd_bucket->hsb_index.
693 * NB: it's possible that several bds point to the same bucket but
694 * have different bd::bd_offset, so need take care of deadlock.
696 cfs_hash_for_each_bd(bds, n, i) {
697 if (prev == bds[i].bd_bucket)
700 LASSERT(prev == NULL ||
701 prev->hsb_index < bds[i].bd_bucket->hsb_index);
702 cfs_hash_bd_lock(hs, &bds[i], excl);
703 prev = bds[i].bd_bucket;
708 cfs_hash_multi_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds,
709 unsigned n, int excl)
711 struct cfs_hash_bucket *prev = NULL;
714 cfs_hash_for_each_bd(bds, n, i) {
715 if (prev != bds[i].bd_bucket) {
716 cfs_hash_bd_unlock(hs, &bds[i], excl);
717 prev = bds[i].bd_bucket;
722 static struct hlist_node *
723 cfs_hash_multi_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
724 unsigned n, const void *key)
726 struct hlist_node *ehnode;
729 cfs_hash_for_each_bd(bds, n, i) {
730 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, NULL,
731 CFS_HS_LOOKUP_IT_FIND);
738 static struct hlist_node *
739 cfs_hash_multi_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
740 unsigned n, const void *key,
741 struct hlist_node *hnode, int noref)
743 struct hlist_node *ehnode;
747 LASSERT(hnode != NULL);
748 intent = CFS_HS_LOOKUP_IT_PEEK | (!noref * CFS_HS_LOOKUP_MASK_REF);
750 cfs_hash_for_each_bd(bds, n, i) {
751 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key,
757 if (i == 1) { /* only one bucket */
758 cfs_hash_bd_add_locked(hs, &bds[0], hnode);
760 struct cfs_hash_bd mybd;
762 cfs_hash_bd_get(hs, key, &mybd);
763 cfs_hash_bd_add_locked(hs, &mybd, hnode);
769 static struct hlist_node *
770 cfs_hash_multi_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
771 unsigned n, const void *key,
772 struct hlist_node *hnode)
774 struct hlist_node *ehnode;
777 cfs_hash_for_each_bd(bds, n, i) {
778 ehnode = cfs_hash_bd_lookup_intent(hs, &bds[i], key, hnode,
779 CFS_HS_LOOKUP_IT_FINDDEL);
787 cfs_hash_bd_order(struct cfs_hash_bd *bd1, struct cfs_hash_bd *bd2)
791 if (bd2->bd_bucket == NULL)
794 if (bd1->bd_bucket == NULL) {
796 bd2->bd_bucket = NULL;
800 rc = cfs_hash_bd_compare(bd1, bd2);
802 bd2->bd_bucket = NULL;
804 } else if (rc > 0) { /* swab bd1 and bd2 */
805 struct cfs_hash_bd tmp;
814 cfs_hash_dual_bd_get(struct cfs_hash *hs, const void *key,
815 struct cfs_hash_bd *bds)
817 /* NB: caller should hold hs_lock.rw if REHASH is set */
818 cfs_hash_bd_from_key(hs, hs->hs_buckets,
819 hs->hs_cur_bits, key, &bds[0]);
820 if (likely(hs->hs_rehash_buckets == NULL)) {
821 /* no rehash or not rehashing */
822 bds[1].bd_bucket = NULL;
826 LASSERT(hs->hs_rehash_bits != 0);
827 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
828 hs->hs_rehash_bits, key, &bds[1]);
830 cfs_hash_bd_order(&bds[0], &bds[1]);
834 cfs_hash_dual_bd_lock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
836 cfs_hash_multi_bd_lock(hs, bds, 2, excl);
840 cfs_hash_dual_bd_unlock(struct cfs_hash *hs, struct cfs_hash_bd *bds, int excl)
842 cfs_hash_multi_bd_unlock(hs, bds, 2, excl);
846 cfs_hash_dual_bd_lookup_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
849 return cfs_hash_multi_bd_lookup_locked(hs, bds, 2, key);
853 cfs_hash_dual_bd_findadd_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
854 const void *key, struct hlist_node *hnode,
857 return cfs_hash_multi_bd_findadd_locked(hs, bds, 2, key,
862 cfs_hash_dual_bd_finddel_locked(struct cfs_hash *hs, struct cfs_hash_bd *bds,
863 const void *key, struct hlist_node *hnode)
865 return cfs_hash_multi_bd_finddel_locked(hs, bds, 2, key, hnode);
869 cfs_hash_buckets_free(struct cfs_hash_bucket **buckets,
870 int bkt_size, int prev_size, int size)
874 for (i = prev_size; i < size; i++) {
875 if (buckets[i] != NULL)
876 LIBCFS_FREE(buckets[i], bkt_size);
879 LIBCFS_FREE(buckets, sizeof(buckets[0]) * size);
883 * Create or grow bucket memory. Return old_buckets if no allocation was
884 * needed, the newly allocated buckets if allocation was needed and
885 * successful, and NULL on error.
887 static struct cfs_hash_bucket **
888 cfs_hash_buckets_realloc(struct cfs_hash *hs, struct cfs_hash_bucket **old_bkts,
889 unsigned int old_size, unsigned int new_size)
891 struct cfs_hash_bucket **new_bkts;
894 LASSERT(old_size == 0 || old_bkts != NULL);
896 if (old_bkts != NULL && old_size == new_size)
899 LIBCFS_ALLOC(new_bkts, sizeof(new_bkts[0]) * new_size);
900 if (new_bkts == NULL)
903 if (old_bkts != NULL) {
904 memcpy(new_bkts, old_bkts,
905 min(old_size, new_size) * sizeof(*old_bkts));
908 for (i = old_size; i < new_size; i++) {
909 struct hlist_head *hhead;
910 struct cfs_hash_bd bd;
912 LIBCFS_ALLOC(new_bkts[i], cfs_hash_bkt_size(hs));
913 if (new_bkts[i] == NULL) {
914 cfs_hash_buckets_free(new_bkts, cfs_hash_bkt_size(hs),
919 new_bkts[i]->hsb_index = i;
920 new_bkts[i]->hsb_version = 1; /* shouldn't be zero */
921 new_bkts[i]->hsb_depmax = -1; /* unknown */
922 bd.bd_bucket = new_bkts[i];
923 cfs_hash_bd_for_each_hlist(hs, &bd, hhead)
924 INIT_HLIST_HEAD(hhead);
926 if (cfs_hash_with_no_lock(hs) ||
927 cfs_hash_with_no_bktlock(hs))
930 if (cfs_hash_with_rw_bktlock(hs))
931 rwlock_init(&new_bkts[i]->hsb_lock.rw);
932 else if (cfs_hash_with_spin_bktlock(hs))
933 spin_lock_init(&new_bkts[i]->hsb_lock.spin);
935 LBUG(); /* invalid use-case */
941 * Initialize new libcfs hash, where:
942 * @name - Descriptive hash name
943 * @cur_bits - Initial hash table size, in bits
944 * @max_bits - Maximum allowed hash table resize, in bits
945 * @ops - Registered hash table operations
946 * @flags - CFS_HASH_REHASH enable synamic hash resizing
947 * - CFS_HASH_SORT enable chained hash sort
949 static int cfs_hash_rehash_worker(struct cfs_workitem *wi);
951 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
952 static int cfs_hash_dep_print(struct cfs_workitem *wi)
954 struct cfs_hash *hs = container_of(wi, struct cfs_hash, hs_dep_wi);
960 spin_lock(&hs->hs_dep_lock);
961 dep = hs->hs_dep_max;
962 bkt = hs->hs_dep_bkt;
963 off = hs->hs_dep_off;
964 bits = hs->hs_dep_bits;
965 spin_unlock(&hs->hs_dep_lock);
967 LCONSOLE_WARN("#### HASH %s (bits: %d): max depth %d at bucket %d/%d\n",
968 hs->hs_name, bits, dep, bkt, off);
969 spin_lock(&hs->hs_dep_lock);
970 hs->hs_dep_bits = 0; /* mark as workitem done */
971 spin_unlock(&hs->hs_dep_lock);
975 static void cfs_hash_depth_wi_init(struct cfs_hash *hs)
977 spin_lock_init(&hs->hs_dep_lock);
978 cfs_wi_init(&hs->hs_dep_wi, hs, cfs_hash_dep_print);
981 static void cfs_hash_depth_wi_cancel(struct cfs_hash *hs)
983 if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_dep_wi))
986 spin_lock(&hs->hs_dep_lock);
987 while (hs->hs_dep_bits != 0) {
988 spin_unlock(&hs->hs_dep_lock);
990 spin_lock(&hs->hs_dep_lock);
992 spin_unlock(&hs->hs_dep_lock);
995 #else /* CFS_HASH_DEBUG_LEVEL < CFS_HASH_DEBUG_1 */
997 static inline void cfs_hash_depth_wi_init(struct cfs_hash *hs) {}
998 static inline void cfs_hash_depth_wi_cancel(struct cfs_hash *hs) {}
1000 #endif /* CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1 */
1003 cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
1004 unsigned bkt_bits, unsigned extra_bytes,
1005 unsigned min_theta, unsigned max_theta,
1006 struct cfs_hash_ops *ops, unsigned flags)
1008 struct cfs_hash *hs;
1013 CLASSERT(CFS_HASH_THETA_BITS < 15);
1015 LASSERT(name != NULL);
1016 LASSERT(ops != NULL);
1017 LASSERT(ops->hs_key);
1018 LASSERT(ops->hs_hash);
1019 LASSERT(ops->hs_object);
1020 LASSERT(ops->hs_keycmp);
1021 LASSERT(ops->hs_get != NULL);
1022 LASSERT(ops->hs_put != NULL || ops->hs_put_locked != NULL);
1024 if ((flags & CFS_HASH_REHASH) != 0)
1025 flags |= CFS_HASH_COUNTER; /* must have counter */
1027 LASSERT(cur_bits > 0);
1028 LASSERT(cur_bits >= bkt_bits);
1029 LASSERT(max_bits >= cur_bits && max_bits < 31);
1030 LASSERT(ergo((flags & CFS_HASH_REHASH) == 0, cur_bits == max_bits));
1031 LASSERT(ergo((flags & CFS_HASH_REHASH) != 0,
1032 (flags & CFS_HASH_NO_LOCK) == 0));
1033 LASSERT(ergo((flags & CFS_HASH_REHASH_KEY) != 0,
1034 ops->hs_keycpy != NULL));
1036 len = (flags & CFS_HASH_BIGNAME) == 0 ?
1037 CFS_HASH_NAME_LEN : CFS_HASH_BIGNAME_LEN;
1038 LIBCFS_ALLOC(hs, offsetof(struct cfs_hash, hs_name[len]));
1042 strlcpy(hs->hs_name, name, len);
1043 hs->hs_flags = flags;
1045 atomic_set(&hs->hs_refcount, 1);
1046 atomic_set(&hs->hs_count, 0);
1048 cfs_hash_lock_setup(hs);
1049 cfs_hash_hlist_setup(hs);
1051 hs->hs_cur_bits = (__u8)cur_bits;
1052 hs->hs_min_bits = (__u8)cur_bits;
1053 hs->hs_max_bits = (__u8)max_bits;
1054 hs->hs_bkt_bits = (__u8)bkt_bits;
1057 hs->hs_extra_bytes = extra_bytes;
1058 hs->hs_rehash_bits = 0;
1059 cfs_wi_init(&hs->hs_rehash_wi, hs, cfs_hash_rehash_worker);
1060 cfs_hash_depth_wi_init(hs);
1062 if (cfs_hash_with_rehash(hs))
1063 __cfs_hash_set_theta(hs, min_theta, max_theta);
1065 hs->hs_buckets = cfs_hash_buckets_realloc(hs, NULL, 0,
1067 if (hs->hs_buckets != NULL)
1070 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[len]));
1073 EXPORT_SYMBOL(cfs_hash_create);
1076 * Cleanup libcfs hash @hs.
1079 cfs_hash_destroy(struct cfs_hash *hs)
1081 struct hlist_node *hnode;
1082 struct hlist_node *pos;
1083 struct cfs_hash_bd bd;
1087 LASSERT(hs != NULL);
1088 LASSERT(!cfs_hash_is_exiting(hs) &&
1089 !cfs_hash_is_iterating(hs));
1092 * prohibit further rehashes, don't need any lock because
1093 * I'm the only (last) one can change it.
1096 if (cfs_hash_with_rehash(hs))
1097 cfs_hash_rehash_cancel(hs);
1099 cfs_hash_depth_wi_cancel(hs);
1100 /* rehash should be done/canceled */
1101 LASSERT(hs->hs_buckets != NULL &&
1102 hs->hs_rehash_buckets == NULL);
1104 cfs_hash_for_each_bucket(hs, &bd, i) {
1105 struct hlist_head *hhead;
1107 LASSERT(bd.bd_bucket != NULL);
1108 /* no need to take this lock, just for consistent code */
1109 cfs_hash_bd_lock(hs, &bd, 1);
1111 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1112 hlist_for_each_safe(hnode, pos, hhead) {
1113 LASSERTF(!cfs_hash_with_assert_empty(hs),
1114 "hash %s bucket %u(%u) is not "
1115 " empty: %u items left\n",
1116 hs->hs_name, bd.bd_bucket->hsb_index,
1117 bd.bd_offset, bd.bd_bucket->hsb_count);
1118 /* can't assert key valicate, because we
1119 * can interrupt rehash */
1120 cfs_hash_bd_del_locked(hs, &bd, hnode);
1121 cfs_hash_exit(hs, hnode);
1124 LASSERT(bd.bd_bucket->hsb_count == 0);
1125 cfs_hash_bd_unlock(hs, &bd, 1);
1129 LASSERT(atomic_read(&hs->hs_count) == 0);
1131 cfs_hash_buckets_free(hs->hs_buckets, cfs_hash_bkt_size(hs),
1132 0, CFS_HASH_NBKT(hs));
1133 i = cfs_hash_with_bigname(hs) ?
1134 CFS_HASH_BIGNAME_LEN : CFS_HASH_NAME_LEN;
1135 LIBCFS_FREE(hs, offsetof(struct cfs_hash, hs_name[i]));
1140 struct cfs_hash *cfs_hash_getref(struct cfs_hash *hs)
1142 if (atomic_inc_not_zero(&hs->hs_refcount))
1146 EXPORT_SYMBOL(cfs_hash_getref);
1148 void cfs_hash_putref(struct cfs_hash *hs)
1150 if (atomic_dec_and_test(&hs->hs_refcount))
1151 cfs_hash_destroy(hs);
1153 EXPORT_SYMBOL(cfs_hash_putref);
1156 cfs_hash_rehash_bits(struct cfs_hash *hs)
1158 if (cfs_hash_with_no_lock(hs) ||
1159 !cfs_hash_with_rehash(hs))
1162 if (unlikely(cfs_hash_is_exiting(hs)))
1165 if (unlikely(cfs_hash_is_rehashing(hs)))
1168 if (unlikely(cfs_hash_is_iterating(hs)))
1171 /* XXX: need to handle case with max_theta != 2.0
1172 * and the case with min_theta != 0.5 */
1173 if ((hs->hs_cur_bits < hs->hs_max_bits) &&
1174 (__cfs_hash_theta(hs) > hs->hs_max_theta))
1175 return hs->hs_cur_bits + 1;
1177 if (!cfs_hash_with_shrink(hs))
1180 if ((hs->hs_cur_bits > hs->hs_min_bits) &&
1181 (__cfs_hash_theta(hs) < hs->hs_min_theta))
1182 return hs->hs_cur_bits - 1;
1188 * don't allow inline rehash if:
1189 * - user wants non-blocking change (add/del) on hash table
1190 * - too many elements
1193 cfs_hash_rehash_inline(struct cfs_hash *hs)
1195 return !cfs_hash_with_nblk_change(hs) &&
1196 atomic_read(&hs->hs_count) < CFS_HASH_LOOP_HOG;
1200 * Add item @hnode to libcfs hash @hs using @key. The registered
1201 * ops->hs_get function will be called when the item is added.
1204 cfs_hash_add(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1206 struct cfs_hash_bd bd;
1209 LASSERT(hlist_unhashed(hnode));
1211 cfs_hash_lock(hs, 0);
1212 cfs_hash_bd_get_and_lock(hs, key, &bd, 1);
1214 cfs_hash_key_validate(hs, key, hnode);
1215 cfs_hash_bd_add_locked(hs, &bd, hnode);
1217 cfs_hash_bd_unlock(hs, &bd, 1);
1219 bits = cfs_hash_rehash_bits(hs);
1220 cfs_hash_unlock(hs, 0);
1222 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1224 EXPORT_SYMBOL(cfs_hash_add);
1226 static struct hlist_node *
1227 cfs_hash_find_or_add(struct cfs_hash *hs, const void *key,
1228 struct hlist_node *hnode, int noref)
1230 struct hlist_node *ehnode;
1231 struct cfs_hash_bd bds[2];
1234 LASSERTF(hlist_unhashed(hnode), "hnode = %p\n", hnode);
1236 cfs_hash_lock(hs, 0);
1237 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1239 cfs_hash_key_validate(hs, key, hnode);
1240 ehnode = cfs_hash_dual_bd_findadd_locked(hs, bds, key,
1242 cfs_hash_dual_bd_unlock(hs, bds, 1);
1244 if (ehnode == hnode) /* new item added */
1245 bits = cfs_hash_rehash_bits(hs);
1246 cfs_hash_unlock(hs, 0);
1248 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1254 * Add item @hnode to libcfs hash @hs using @key. The registered
1255 * ops->hs_get function will be called if the item was added.
1256 * Returns 0 on success or -EALREADY on key collisions.
1259 cfs_hash_add_unique(struct cfs_hash *hs, const void *key,
1260 struct hlist_node *hnode)
1262 return cfs_hash_find_or_add(hs, key, hnode, 1) != hnode ?
1265 EXPORT_SYMBOL(cfs_hash_add_unique);
1268 * Add item @hnode to libcfs hash @hs using @key. If this @key
1269 * already exists in the hash then ops->hs_get will be called on the
1270 * conflicting entry and that entry will be returned to the caller.
1271 * Otherwise ops->hs_get is called on the item which was added.
1274 cfs_hash_findadd_unique(struct cfs_hash *hs, const void *key,
1275 struct hlist_node *hnode)
1277 hnode = cfs_hash_find_or_add(hs, key, hnode, 0);
1279 return cfs_hash_object(hs, hnode);
1281 EXPORT_SYMBOL(cfs_hash_findadd_unique);
1284 * Delete item @hnode from the libcfs hash @hs using @key. The @key
1285 * is required to ensure the correct hash bucket is locked since there
1286 * is no direct linkage from the item to the bucket. The object
1287 * removed from the hash will be returned and obs->hs_put is called
1288 * on the removed object.
1291 cfs_hash_del(struct cfs_hash *hs, const void *key, struct hlist_node *hnode)
1295 struct cfs_hash_bd bds[2];
1297 cfs_hash_lock(hs, 0);
1298 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
1300 /* NB: do nothing if @hnode is not in hash table */
1301 if (hnode == NULL || !hlist_unhashed(hnode)) {
1302 if (bds[1].bd_bucket == NULL && hnode != NULL) {
1303 cfs_hash_bd_del_locked(hs, &bds[0], hnode);
1305 hnode = cfs_hash_dual_bd_finddel_locked(hs, bds,
1310 if (hnode != NULL) {
1311 obj = cfs_hash_object(hs, hnode);
1312 bits = cfs_hash_rehash_bits(hs);
1315 cfs_hash_dual_bd_unlock(hs, bds, 1);
1316 cfs_hash_unlock(hs, 0);
1318 cfs_hash_rehash(hs, cfs_hash_rehash_inline(hs));
1322 EXPORT_SYMBOL(cfs_hash_del);
1325 * Delete item given @key in libcfs hash @hs. The first @key found in
1326 * the hash will be removed, if the key exists multiple times in the hash
1327 * @hs this function must be called once per key. The removed object
1328 * will be returned and ops->hs_put is called on the removed object.
1331 cfs_hash_del_key(struct cfs_hash *hs, const void *key)
1333 return cfs_hash_del(hs, key, NULL);
1335 EXPORT_SYMBOL(cfs_hash_del_key);
1338 * Lookup an item using @key in the libcfs hash @hs and return it.
1339 * If the @key is found in the hash hs->hs_get() is called and the
1340 * matching objects is returned. It is the callers responsibility
1341 * to call the counterpart ops->hs_put using the cfs_hash_put() macro
1342 * when when finished with the object. If the @key was not found
1343 * in the hash @hs NULL is returned.
1346 cfs_hash_lookup(struct cfs_hash *hs, const void *key)
1349 struct hlist_node *hnode;
1350 struct cfs_hash_bd bds[2];
1352 cfs_hash_lock(hs, 0);
1353 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1355 hnode = cfs_hash_dual_bd_lookup_locked(hs, bds, key);
1357 obj = cfs_hash_object(hs, hnode);
1359 cfs_hash_dual_bd_unlock(hs, bds, 0);
1360 cfs_hash_unlock(hs, 0);
1364 EXPORT_SYMBOL(cfs_hash_lookup);
1367 cfs_hash_for_each_enter(struct cfs_hash *hs)
1369 LASSERT(!cfs_hash_is_exiting(hs));
1371 if (!cfs_hash_with_rehash(hs))
1374 * NB: it's race on cfs_has_t::hs_iterating, but doesn't matter
1375 * because it's just an unreliable signal to rehash-thread,
1376 * rehash-thread will try to finish rehash ASAP when seeing this.
1378 hs->hs_iterating = 1;
1380 cfs_hash_lock(hs, 1);
1383 /* NB: iteration is mostly called by service thread,
1384 * we tend to cancel pending rehash-request, instead of
1385 * blocking service thread, we will relaunch rehash request
1386 * after iteration */
1387 if (cfs_hash_is_rehashing(hs))
1388 cfs_hash_rehash_cancel_locked(hs);
1389 cfs_hash_unlock(hs, 1);
1393 cfs_hash_for_each_exit(struct cfs_hash *hs)
1398 if (!cfs_hash_with_rehash(hs))
1400 cfs_hash_lock(hs, 1);
1401 remained = --hs->hs_iterators;
1402 bits = cfs_hash_rehash_bits(hs);
1403 cfs_hash_unlock(hs, 1);
1404 /* NB: it's race on cfs_has_t::hs_iterating, see above */
1406 hs->hs_iterating = 0;
1408 cfs_hash_rehash(hs, atomic_read(&hs->hs_count) <
1414 * For each item in the libcfs hash @hs call the passed callback @func
1415 * and pass to it as an argument each hash item and the private @data.
1417 * a) the function may sleep!
1418 * b) during the callback:
1419 * . the bucket lock is held so the callback must never sleep.
1420 * . if @removal_safe is true, use can remove current item by
1421 * cfs_hash_bd_del_locked
1424 cfs_hash_for_each_tight(struct cfs_hash *hs, cfs_hash_for_each_cb_t func,
1425 void *data, int remove_safe)
1427 struct hlist_node *hnode;
1428 struct hlist_node *pos;
1429 struct cfs_hash_bd bd;
1431 int excl = !!remove_safe;
1436 cfs_hash_for_each_enter(hs);
1438 cfs_hash_lock(hs, 0);
1439 LASSERT(!cfs_hash_is_rehashing(hs));
1441 cfs_hash_for_each_bucket(hs, &bd, i) {
1442 struct hlist_head *hhead;
1444 cfs_hash_bd_lock(hs, &bd, excl);
1445 if (func == NULL) { /* only glimpse size */
1446 count += bd.bd_bucket->hsb_count;
1447 cfs_hash_bd_unlock(hs, &bd, excl);
1451 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1452 hlist_for_each_safe(hnode, pos, hhead) {
1453 cfs_hash_bucket_validate(hs, &bd, hnode);
1456 if (func(hs, &bd, hnode, data)) {
1457 cfs_hash_bd_unlock(hs, &bd, excl);
1462 cfs_hash_bd_unlock(hs, &bd, excl);
1463 if (loop < CFS_HASH_LOOP_HOG)
1466 cfs_hash_unlock(hs, 0);
1468 cfs_hash_lock(hs, 0);
1471 cfs_hash_unlock(hs, 0);
1473 cfs_hash_for_each_exit(hs);
1477 struct cfs_hash_cond_arg {
1478 cfs_hash_cond_opt_cb_t func;
1483 cfs_hash_cond_del_locked(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1484 struct hlist_node *hnode, void *data)
1486 struct cfs_hash_cond_arg *cond = data;
1488 if (cond->func(cfs_hash_object(hs, hnode), cond->arg))
1489 cfs_hash_bd_del_locked(hs, bd, hnode);
1494 * Delete item from the libcfs hash @hs when @func return true.
1495 * The write lock being hold during loop for each bucket to avoid
1496 * any object be reference.
1499 cfs_hash_cond_del(struct cfs_hash *hs, cfs_hash_cond_opt_cb_t func, void *data)
1501 struct cfs_hash_cond_arg arg = {
1506 cfs_hash_for_each_tight(hs, cfs_hash_cond_del_locked, &arg, 1);
1508 EXPORT_SYMBOL(cfs_hash_cond_del);
1511 cfs_hash_for_each(struct cfs_hash *hs,
1512 cfs_hash_for_each_cb_t func, void *data)
1514 cfs_hash_for_each_tight(hs, func, data, 0);
1516 EXPORT_SYMBOL(cfs_hash_for_each);
1519 cfs_hash_for_each_safe(struct cfs_hash *hs,
1520 cfs_hash_for_each_cb_t func, void *data)
1522 cfs_hash_for_each_tight(hs, func, data, 1);
1524 EXPORT_SYMBOL(cfs_hash_for_each_safe);
1527 cfs_hash_peek(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1528 struct hlist_node *hnode, void *data)
1531 return 1; /* return 1 to break the loop */
1535 cfs_hash_is_empty(struct cfs_hash *hs)
1539 cfs_hash_for_each_tight(hs, cfs_hash_peek, &empty, 0);
1542 EXPORT_SYMBOL(cfs_hash_is_empty);
1545 cfs_hash_size_get(struct cfs_hash *hs)
1547 return cfs_hash_with_counter(hs) ?
1548 atomic_read(&hs->hs_count) :
1549 cfs_hash_for_each_tight(hs, NULL, NULL, 0);
1551 EXPORT_SYMBOL(cfs_hash_size_get);
1554 * cfs_hash_for_each_relax:
1555 * Iterate the hash table and call @func on each item without
1556 * any lock. This function can't guarantee to finish iteration
1557 * if these features are enabled:
1559 * a. if rehash_key is enabled, an item can be moved from
1560 * one bucket to another bucket
1561 * b. user can remove non-zero-ref item from hash-table,
1562 * so the item can be removed from hash-table, even worse,
1563 * it's possible that user changed key and insert to another
1565 * there's no way for us to finish iteration correctly on previous
1566 * two cases, so iteration has to be stopped on change.
1569 cfs_hash_for_each_relax(struct cfs_hash *hs, cfs_hash_for_each_cb_t func,
1570 void *data, int start)
1572 struct hlist_node *hnode;
1573 struct hlist_node *next = NULL;
1574 struct cfs_hash_bd bd;
1583 stop_on_change = cfs_hash_with_rehash_key(hs) ||
1584 !cfs_hash_with_no_itemref(hs);
1585 has_put_locked = hs->hs_ops->hs_put_locked != NULL;
1586 cfs_hash_lock(hs, 0);
1588 LASSERT(!cfs_hash_is_rehashing(hs));
1590 cfs_hash_for_each_bucket(hs, &bd, i) {
1591 struct hlist_head *hhead;
1595 else if (end > 0 && i >= end)
1598 cfs_hash_bd_lock(hs, &bd, 0);
1599 version = cfs_hash_bd_version_get(&bd);
1601 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1602 hnode = hhead->first;
1605 cfs_hash_get(hs, hnode);
1606 for (; hnode != NULL; hnode = next) {
1607 cfs_hash_bucket_validate(hs, &bd, hnode);
1610 cfs_hash_get(hs, next);
1611 cfs_hash_bd_unlock(hs, &bd, 0);
1612 cfs_hash_unlock(hs, 0);
1614 rc = func(hs, &bd, hnode, data);
1615 if (stop_on_change || !has_put_locked)
1616 cfs_hash_put(hs, hnode);
1621 cfs_hash_lock(hs, 0);
1622 cfs_hash_bd_lock(hs, &bd, 0);
1623 if (stop_on_change) {
1625 cfs_hash_bd_version_get(&bd))
1627 } else if (has_put_locked) {
1628 cfs_hash_put_locked(hs, hnode);
1630 if (rc) /* callback wants to break iteration */
1634 if (has_put_locked) {
1635 cfs_hash_put_locked(hs, next);
1639 } else if (rc != 0) {
1643 cfs_hash_bd_unlock(hs, &bd, 0);
1644 if (next != NULL && !has_put_locked) {
1645 cfs_hash_put(hs, next);
1648 if (rc) /* callback wants to break iteration */
1652 if (start > 0 && rc == 0) {
1658 cfs_hash_unlock(hs, 0);
1663 cfs_hash_for_each_nolock(struct cfs_hash *hs,
1664 cfs_hash_for_each_cb_t func, void *data, int start)
1668 if (cfs_hash_with_no_lock(hs) ||
1669 cfs_hash_with_rehash_key(hs) ||
1670 !cfs_hash_with_no_itemref(hs))
1671 RETURN(-EOPNOTSUPP);
1673 if (hs->hs_ops->hs_get == NULL ||
1674 (hs->hs_ops->hs_put == NULL &&
1675 hs->hs_ops->hs_put_locked == NULL))
1676 RETURN(-EOPNOTSUPP);
1678 cfs_hash_for_each_enter(hs);
1679 cfs_hash_for_each_relax(hs, func, data, start);
1680 cfs_hash_for_each_exit(hs);
1684 EXPORT_SYMBOL(cfs_hash_for_each_nolock);
1687 * For each hash bucket in the libcfs hash @hs call the passed callback
1688 * @func until all the hash buckets are empty. The passed callback @func
1689 * or the previously registered callback hs->hs_put must remove the item
1690 * from the hash. You may either use the cfs_hash_del() or hlist_del()
1691 * functions. No rwlocks will be held during the callback @func it is
1692 * safe to sleep if needed. This function will not terminate until the
1693 * hash is empty. Note it is still possible to concurrently add new
1694 * items in to the hash. It is the callers responsibility to ensure
1695 * the required locking is in place to prevent concurrent insertions.
1698 cfs_hash_for_each_empty(struct cfs_hash *hs,
1699 cfs_hash_for_each_cb_t func, void *data)
1704 if (cfs_hash_with_no_lock(hs))
1707 if (hs->hs_ops->hs_get == NULL ||
1708 (hs->hs_ops->hs_put == NULL &&
1709 hs->hs_ops->hs_put_locked == NULL))
1712 cfs_hash_for_each_enter(hs);
1713 while (cfs_hash_for_each_relax(hs, func, data, 0)) {
1714 CDEBUG(D_INFO, "Try to empty hash: %s, loop: %u\n",
1717 cfs_hash_for_each_exit(hs);
1720 EXPORT_SYMBOL(cfs_hash_for_each_empty);
1723 cfs_hash_hlist_for_each(struct cfs_hash *hs, unsigned hindex,
1724 cfs_hash_for_each_cb_t func, void *data)
1726 struct hlist_head *hhead;
1727 struct hlist_node *hnode;
1728 struct cfs_hash_bd bd;
1730 cfs_hash_for_each_enter(hs);
1731 cfs_hash_lock(hs, 0);
1732 if (hindex >= CFS_HASH_NHLIST(hs))
1735 cfs_hash_bd_index_set(hs, hindex, &bd);
1737 cfs_hash_bd_lock(hs, &bd, 0);
1738 hhead = cfs_hash_bd_hhead(hs, &bd);
1739 hlist_for_each(hnode, hhead) {
1740 if (func(hs, &bd, hnode, data))
1743 cfs_hash_bd_unlock(hs, &bd, 0);
1745 cfs_hash_unlock(hs, 0);
1746 cfs_hash_for_each_exit(hs);
1749 EXPORT_SYMBOL(cfs_hash_hlist_for_each);
1752 * For each item in the libcfs hash @hs which matches the @key call
1753 * the passed callback @func and pass to it as an argument each hash
1754 * item and the private @data. During the callback the bucket lock
1755 * is held so the callback must never sleep.
1758 cfs_hash_for_each_key(struct cfs_hash *hs, const void *key,
1759 cfs_hash_for_each_cb_t func, void *data)
1761 struct hlist_node *hnode;
1762 struct cfs_hash_bd bds[2];
1765 cfs_hash_lock(hs, 0);
1767 cfs_hash_dual_bd_get_and_lock(hs, key, bds, 0);
1769 cfs_hash_for_each_bd(bds, 2, i) {
1770 struct hlist_head *hlist = cfs_hash_bd_hhead(hs, &bds[i]);
1772 hlist_for_each(hnode, hlist) {
1773 cfs_hash_bucket_validate(hs, &bds[i], hnode);
1775 if (cfs_hash_keycmp(hs, key, hnode)) {
1776 if (func(hs, &bds[i], hnode, data))
1782 cfs_hash_dual_bd_unlock(hs, bds, 0);
1783 cfs_hash_unlock(hs, 0);
1785 EXPORT_SYMBOL(cfs_hash_for_each_key);
1788 * Rehash the libcfs hash @hs to the given @bits. This can be used
1789 * to grow the hash size when excessive chaining is detected, or to
1790 * shrink the hash when it is larger than needed. When the CFS_HASH_REHASH
1791 * flag is set in @hs the libcfs hash may be dynamically rehashed
1792 * during addition or removal if the hash's theta value exceeds
1793 * either the hs->hs_min_theta or hs->max_theta values. By default
1794 * these values are tuned to keep the chained hash depth small, and
1795 * this approach assumes a reasonably uniform hashing function. The
1796 * theta thresholds for @hs are tunable via cfs_hash_set_theta().
1799 cfs_hash_rehash_cancel_locked(struct cfs_hash *hs)
1803 /* need hold cfs_hash_lock(hs, 1) */
1804 LASSERT(cfs_hash_with_rehash(hs) &&
1805 !cfs_hash_with_no_lock(hs));
1807 if (!cfs_hash_is_rehashing(hs))
1810 if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_rehash_wi)) {
1811 hs->hs_rehash_bits = 0;
1815 for (i = 2; cfs_hash_is_rehashing(hs); i++) {
1816 cfs_hash_unlock(hs, 1);
1817 /* raise console warning while waiting too long */
1818 CDEBUG(IS_PO2(i >> 3) ? D_WARNING : D_INFO,
1819 "hash %s is still rehashing, rescheded %d\n",
1820 hs->hs_name, i - 1);
1822 cfs_hash_lock(hs, 1);
1827 cfs_hash_rehash_cancel(struct cfs_hash *hs)
1829 cfs_hash_lock(hs, 1);
1830 cfs_hash_rehash_cancel_locked(hs);
1831 cfs_hash_unlock(hs, 1);
1835 cfs_hash_rehash(struct cfs_hash *hs, int do_rehash)
1839 LASSERT(cfs_hash_with_rehash(hs) && !cfs_hash_with_no_lock(hs));
1841 cfs_hash_lock(hs, 1);
1843 rc = cfs_hash_rehash_bits(hs);
1845 cfs_hash_unlock(hs, 1);
1849 hs->hs_rehash_bits = rc;
1851 /* launch and return */
1852 cfs_wi_schedule(cfs_sched_rehash, &hs->hs_rehash_wi);
1853 cfs_hash_unlock(hs, 1);
1857 /* rehash right now */
1858 cfs_hash_unlock(hs, 1);
1860 return cfs_hash_rehash_worker(&hs->hs_rehash_wi);
1864 cfs_hash_rehash_bd(struct cfs_hash *hs, struct cfs_hash_bd *old)
1866 struct cfs_hash_bd new;
1867 struct hlist_head *hhead;
1868 struct hlist_node *hnode;
1869 struct hlist_node *pos;
1873 /* hold cfs_hash_lock(hs, 1), so don't need any bucket lock */
1874 cfs_hash_bd_for_each_hlist(hs, old, hhead) {
1875 hlist_for_each_safe(hnode, pos, hhead) {
1876 key = cfs_hash_key(hs, hnode);
1877 LASSERT(key != NULL);
1878 /* Validate hnode is in the correct bucket. */
1879 cfs_hash_bucket_validate(hs, old, hnode);
1881 * Delete from old hash bucket; move to new bucket.
1882 * ops->hs_key must be defined.
1884 cfs_hash_bd_from_key(hs, hs->hs_rehash_buckets,
1885 hs->hs_rehash_bits, key, &new);
1886 cfs_hash_bd_move_locked(hs, old, &new, hnode);
1894 cfs_hash_rehash_worker(struct cfs_workitem *wi)
1896 struct cfs_hash *hs =
1897 container_of(wi, struct cfs_hash, hs_rehash_wi);
1898 struct cfs_hash_bucket **bkts;
1899 struct cfs_hash_bd bd;
1900 unsigned int old_size;
1901 unsigned int new_size;
1907 LASSERT(hs != NULL && cfs_hash_with_rehash(hs));
1909 cfs_hash_lock(hs, 0);
1910 LASSERT(cfs_hash_is_rehashing(hs));
1912 old_size = CFS_HASH_NBKT(hs);
1913 new_size = CFS_HASH_RH_NBKT(hs);
1915 cfs_hash_unlock(hs, 0);
1918 * don't need hs::hs_rwlock for hs::hs_buckets,
1919 * because nobody can change bkt-table except me.
1921 bkts = cfs_hash_buckets_realloc(hs, hs->hs_buckets,
1922 old_size, new_size);
1923 cfs_hash_lock(hs, 1);
1929 if (bkts == hs->hs_buckets) {
1930 bkts = NULL; /* do nothing */
1934 rc = __cfs_hash_theta(hs);
1935 if ((rc >= hs->hs_min_theta) && (rc <= hs->hs_max_theta)) {
1936 /* free the new allocated bkt-table */
1937 old_size = new_size;
1938 new_size = CFS_HASH_NBKT(hs);
1943 LASSERT(hs->hs_rehash_buckets == NULL);
1944 hs->hs_rehash_buckets = bkts;
1947 cfs_hash_for_each_bucket(hs, &bd, i) {
1948 if (cfs_hash_is_exiting(hs)) {
1950 /* someone wants to destroy the hash, abort now */
1951 if (old_size < new_size) /* OK to free old bkt-table */
1953 /* it's shrinking, need free new bkt-table */
1954 hs->hs_rehash_buckets = NULL;
1955 old_size = new_size;
1956 new_size = CFS_HASH_NBKT(hs);
1960 count += cfs_hash_rehash_bd(hs, &bd);
1961 if (count < CFS_HASH_LOOP_HOG ||
1962 cfs_hash_is_iterating(hs)) { /* need to finish ASAP */
1967 cfs_hash_unlock(hs, 1);
1969 cfs_hash_lock(hs, 1);
1972 hs->hs_rehash_count++;
1974 bkts = hs->hs_buckets;
1975 hs->hs_buckets = hs->hs_rehash_buckets;
1976 hs->hs_rehash_buckets = NULL;
1978 hs->hs_cur_bits = hs->hs_rehash_bits;
1980 hs->hs_rehash_bits = 0;
1981 if (rc == -ESRCH) /* never be scheduled again */
1982 cfs_wi_exit(cfs_sched_rehash, wi);
1983 bsize = cfs_hash_bkt_size(hs);
1984 cfs_hash_unlock(hs, 1);
1985 /* can't refer to @hs anymore because it could be destroyed */
1987 cfs_hash_buckets_free(bkts, bsize, new_size, old_size);
1989 CDEBUG(D_INFO, "early quit of rehashing: %d\n", rc);
1990 /* return 1 only if cfs_wi_exit is called */
1991 return rc == -ESRCH;
1995 * Rehash the object referenced by @hnode in the libcfs hash @hs. The
1996 * @old_key must be provided to locate the objects previous location
1997 * in the hash, and the @new_key will be used to reinsert the object.
1998 * Use this function instead of a cfs_hash_add() + cfs_hash_del()
1999 * combo when it is critical that there is no window in time where the
2000 * object is missing from the hash. When an object is being rehashed
2001 * the registered cfs_hash_get() and cfs_hash_put() functions will
2004 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
2005 void *new_key, struct hlist_node *hnode)
2007 struct cfs_hash_bd bds[3];
2008 struct cfs_hash_bd old_bds[2];
2009 struct cfs_hash_bd new_bd;
2011 LASSERT(!hlist_unhashed(hnode));
2013 cfs_hash_lock(hs, 0);
2015 cfs_hash_dual_bd_get(hs, old_key, old_bds);
2016 cfs_hash_bd_get(hs, new_key, &new_bd);
2018 bds[0] = old_bds[0];
2019 bds[1] = old_bds[1];
2022 /* NB: bds[0] and bds[1] are ordered already */
2023 cfs_hash_bd_order(&bds[1], &bds[2]);
2024 cfs_hash_bd_order(&bds[0], &bds[1]);
2026 cfs_hash_multi_bd_lock(hs, bds, 3, 1);
2027 if (likely(old_bds[1].bd_bucket == NULL)) {
2028 cfs_hash_bd_move_locked(hs, &old_bds[0], &new_bd, hnode);
2030 cfs_hash_dual_bd_finddel_locked(hs, old_bds, old_key, hnode);
2031 cfs_hash_bd_add_locked(hs, &new_bd, hnode);
2033 /* overwrite key inside locks, otherwise may screw up with
2034 * other operations, i.e: rehash */
2035 cfs_hash_keycpy(hs, hnode, new_key);
2037 cfs_hash_multi_bd_unlock(hs, bds, 3, 1);
2038 cfs_hash_unlock(hs, 0);
2040 EXPORT_SYMBOL(cfs_hash_rehash_key);
2042 int cfs_hash_debug_header(struct seq_file *m)
2044 return seq_printf(m, "%-*s%6s%6s%6s%6s%6s%6s%6s%7s%8s%8s%8s%s\n",
2045 CFS_HASH_BIGNAME_LEN,
2046 "name", "cur", "min", "max", "theta", "t-min", "t-max",
2047 "flags", "rehash", "count", "maxdep", "maxdepb",
2050 EXPORT_SYMBOL(cfs_hash_debug_header);
2052 static struct cfs_hash_bucket **
2053 cfs_hash_full_bkts(struct cfs_hash *hs)
2055 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2056 if (hs->hs_rehash_buckets == NULL)
2057 return hs->hs_buckets;
2059 LASSERT(hs->hs_rehash_bits != 0);
2060 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2061 hs->hs_rehash_buckets : hs->hs_buckets;
2065 cfs_hash_full_nbkt(struct cfs_hash *hs)
2067 /* NB: caller should hold hs->hs_rwlock if REHASH is set */
2068 if (hs->hs_rehash_buckets == NULL)
2069 return CFS_HASH_NBKT(hs);
2071 LASSERT(hs->hs_rehash_bits != 0);
2072 return hs->hs_rehash_bits > hs->hs_cur_bits ?
2073 CFS_HASH_RH_NBKT(hs) : CFS_HASH_NBKT(hs);
2076 int cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m)
2078 int dist[8] = { 0, };
2086 cfs_hash_lock(hs, 0);
2087 theta = __cfs_hash_theta(hs);
2089 c += seq_printf(m, "%-*s ", CFS_HASH_BIGNAME_LEN, hs->hs_name);
2090 c += seq_printf(m, "%5d ", 1 << hs->hs_cur_bits);
2091 c += seq_printf(m, "%5d ", 1 << hs->hs_min_bits);
2092 c += seq_printf(m, "%5d ", 1 << hs->hs_max_bits);
2093 c += seq_printf(m, "%d.%03d ", __cfs_hash_theta_int(theta),
2094 __cfs_hash_theta_frac(theta));
2095 c += seq_printf(m, "%d.%03d ", __cfs_hash_theta_int(hs->hs_min_theta),
2096 __cfs_hash_theta_frac(hs->hs_min_theta));
2097 c += seq_printf(m, "%d.%03d ", __cfs_hash_theta_int(hs->hs_max_theta),
2098 __cfs_hash_theta_frac(hs->hs_max_theta));
2099 c += seq_printf(m, " 0x%02x ", hs->hs_flags);
2100 c += seq_printf(m, "%6d ", hs->hs_rehash_count);
2103 * The distribution is a summary of the chained hash depth in
2104 * each of the libcfs hash buckets. Each buckets hsb_count is
2105 * divided by the hash theta value and used to generate a
2106 * histogram of the hash distribution. A uniform hash will
2107 * result in all hash buckets being close to the average thus
2108 * only the first few entries in the histogram will be non-zero.
2109 * If you hash function results in a non-uniform hash the will
2110 * be observable by outlier bucks in the distribution histogram.
2112 * Uniform hash distribution: 128/128/0/0/0/0/0/0
2113 * Non-Uniform hash distribution: 128/125/0/0/0/0/2/1
2115 for (i = 0; i < cfs_hash_full_nbkt(hs); i++) {
2116 struct cfs_hash_bd bd;
2118 bd.bd_bucket = cfs_hash_full_bkts(hs)[i];
2119 cfs_hash_bd_lock(hs, &bd, 0);
2120 if (maxdep < bd.bd_bucket->hsb_depmax) {
2121 maxdep = bd.bd_bucket->hsb_depmax;
2122 maxdepb = ffz(~maxdep);
2124 total += bd.bd_bucket->hsb_count;
2125 dist[min(fls(bd.bd_bucket->hsb_count/max(theta,1)),7)]++;
2126 cfs_hash_bd_unlock(hs, &bd, 0);
2129 c += seq_printf(m, "%7d ", total);
2130 c += seq_printf(m, "%7d ", maxdep);
2131 c += seq_printf(m, "%7d ", maxdepb);
2132 for (i = 0; i < 8; i++)
2133 c += seq_printf(m, "%d%c", dist[i], (i == 7) ? '\n' : '/');
2135 cfs_hash_unlock(hs, 0);
2138 EXPORT_SYMBOL(cfs_hash_debug_str);