Whamcloud - gitweb
LU-1346 libcfs: cleanup macros in portals_compat25.h
[fs/lustre-release.git] / libcfs / libcfs / hash.c
index 738e4f9..f23440d 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -28,6 +26,8 @@
 /*
  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 #include <libcfs/libcfs.h>
 
 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
-static unsigned int warn_on_depth = 0;
+static unsigned int warn_on_depth = 8;
 CFS_MODULE_PARM(warn_on_depth, "i", uint, 0644,
                 "warning when hash depth is high.");
 #endif
 
+struct cfs_wi_sched *cfs_sched_rehash;
+
 static inline void
 cfs_hash_nl_lock(cfs_hash_lock_t *lock, int exclusive) {}
 
@@ -124,31 +126,31 @@ cfs_hash_nl_unlock(cfs_hash_lock_t *lock, int exclusive) {}
 static inline void
 cfs_hash_spin_lock(cfs_hash_lock_t *lock, int exclusive)
 {
-        cfs_spin_lock(&lock->spin);
+       spin_lock(&lock->spin);
 }
 
 static inline void
 cfs_hash_spin_unlock(cfs_hash_lock_t *lock, int exclusive)
 {
-        cfs_spin_unlock(&lock->spin);
+       spin_unlock(&lock->spin);
 }
 
 static inline void
 cfs_hash_rw_lock(cfs_hash_lock_t *lock, int exclusive)
 {
-        if (!exclusive)
-                cfs_read_lock(&lock->rw);
-        else
-                cfs_write_lock(&lock->rw);
+       if (!exclusive)
+               read_lock(&lock->rw);
+       else
+               write_lock(&lock->rw);
 }
 
 static inline void
 cfs_hash_rw_unlock(cfs_hash_lock_t *lock, int exclusive)
 {
-        if (!exclusive)
-                cfs_read_unlock(&lock->rw);
-        else
-                cfs_write_unlock(&lock->rw);
+       if (!exclusive)
+               read_unlock(&lock->rw);
+       else
+               write_unlock(&lock->rw);
 }
 
 /** No lock hash */
@@ -208,15 +210,15 @@ static cfs_hash_lock_ops_t cfs_hash_nr_bkt_rw_lops =
 static void
 cfs_hash_lock_setup(cfs_hash_t *hs)
 {
-        if (cfs_hash_with_no_lock(hs)) {
-                hs->hs_lops = &cfs_hash_nl_lops;
+       if (cfs_hash_with_no_lock(hs)) {
+               hs->hs_lops = &cfs_hash_nl_lops;
 
-        } else if (cfs_hash_with_no_bktlock(hs)) {
-                hs->hs_lops = &cfs_hash_nbl_lops;
-                cfs_spin_lock_init(&hs->hs_lock.spin);
+       } else if (cfs_hash_with_no_bktlock(hs)) {
+               hs->hs_lops = &cfs_hash_nbl_lops;
+               spin_lock_init(&hs->hs_lock.spin);
 
-        } else if (cfs_hash_with_rehash(hs)) {
-                cfs_rwlock_init(&hs->hs_lock.rw);
+       } else if (cfs_hash_with_rehash(hs)) {
+               rwlock_init(&hs->hs_lock.rw);
 
                 if (cfs_hash_with_rw_bktlock(hs))
                         hs->hs_lops = &cfs_hash_bkt_rw_lops;
@@ -467,7 +469,7 @@ cfs_hash_hlist_setup(cfs_hash_t *hs)
 
 static void
 cfs_hash_bd_from_key(cfs_hash_t *hs, cfs_hash_bucket_t **bkts,
-                     unsigned int bits, void *key, cfs_hash_bd_t *bd)
+                     unsigned int bits, const void *key, cfs_hash_bd_t *bd)
 {
         unsigned int index = cfs_hash_id(hs, key, (1U << bits) - 1);
 
@@ -478,7 +480,7 @@ cfs_hash_bd_from_key(cfs_hash_t *hs, cfs_hash_bucket_t **bkts,
 }
 
 void
-cfs_hash_bd_get(cfs_hash_t *hs, void *key, cfs_hash_bd_t *bd)
+cfs_hash_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bd)
 {
         /* NB: caller should hold hs->hs_rwlock if REHASH is set */
         if (likely(hs->hs_rehash_buckets == NULL)) {
@@ -504,14 +506,14 @@ cfs_hash_bd_dep_record(cfs_hash_t *hs, cfs_hash_bd_t *bd, int dep_cur)
                    max(warn_on_depth, hs->hs_dep_max) >= dep_cur))
                 return;
 
-        cfs_spin_lock(&hs->hs_dep_lock);
-        hs->hs_dep_max  = dep_cur;
-        hs->hs_dep_bkt  = bd->bd_bucket->hsb_index;
-        hs->hs_dep_off  = bd->bd_offset;
-        hs->hs_dep_bits = hs->hs_cur_bits;
-        cfs_spin_unlock(&hs->hs_dep_lock);
+       spin_lock(&hs->hs_dep_lock);
+       hs->hs_dep_max  = dep_cur;
+       hs->hs_dep_bkt  = bd->bd_bucket->hsb_index;
+       hs->hs_dep_off  = bd->bd_offset;
+       hs->hs_dep_bits = hs->hs_cur_bits;
+       spin_unlock(&hs->hs_dep_lock);
 
-        cfs_wi_schedule(&hs->hs_dep_wi);
+       cfs_wi_schedule(cfs_sched_rehash, &hs->hs_dep_wi);
 # endif
 }
 
@@ -615,7 +617,7 @@ typedef enum cfs_hash_lookup_intent {
 
 static cfs_hlist_node_t *
 cfs_hash_bd_lookup_intent(cfs_hash_t *hs, cfs_hash_bd_t *bd,
-                          void *key, cfs_hlist_node_t *hnode,
+                          const void *key, cfs_hlist_node_t *hnode,
                           cfs_hash_lookup_intent_t intent)
 
 {
@@ -655,7 +657,7 @@ cfs_hash_bd_lookup_intent(cfs_hash_t *hs, cfs_hash_bd_t *bd,
 }
 
 cfs_hlist_node_t *
-cfs_hash_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd, void *key)
+cfs_hash_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd, const void *key)
 {
         return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
                                          CFS_HS_LOOKUP_IT_FIND);
@@ -663,8 +665,16 @@ cfs_hash_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd, void *key)
 CFS_EXPORT_SYMBOL(cfs_hash_bd_lookup_locked);
 
 cfs_hlist_node_t *
+cfs_hash_bd_peek_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd, const void *key)
+{
+       return cfs_hash_bd_lookup_intent(hs, bd, key, NULL,
+                                        CFS_HS_LOOKUP_IT_PEEK);
+}
+CFS_EXPORT_SYMBOL(cfs_hash_bd_peek_locked);
+
+cfs_hlist_node_t *
 cfs_hash_bd_findadd_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
-                           void *key, cfs_hlist_node_t *hnode,
+                           const void *key, cfs_hlist_node_t *hnode,
                            int noref)
 {
         return cfs_hash_bd_lookup_intent(hs, bd, key, hnode,
@@ -675,7 +685,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_bd_findadd_locked);
 
 cfs_hlist_node_t *
 cfs_hash_bd_finddel_locked(cfs_hash_t *hs, cfs_hash_bd_t *bd,
-                           void *key, cfs_hlist_node_t *hnode)
+                           const void *key, cfs_hlist_node_t *hnode)
 {
         /* hnode can be NULL, we find the first item with @key */
         return cfs_hash_bd_lookup_intent(hs, bd, key, hnode,
@@ -723,7 +733,7 @@ cfs_hash_multi_bd_unlock(cfs_hash_t *hs, cfs_hash_bd_t *bds,
 
 static cfs_hlist_node_t *
 cfs_hash_multi_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
-                                unsigned n, void *key)
+                                unsigned n, const void *key)
 {
         cfs_hlist_node_t  *ehnode;
         unsigned           i;
@@ -739,7 +749,7 @@ cfs_hash_multi_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
 
 static cfs_hlist_node_t *
 cfs_hash_multi_bd_findadd_locked(cfs_hash_t *hs,
-                                 cfs_hash_bd_t *bds, unsigned n, void *key,
+                                 cfs_hash_bd_t *bds, unsigned n, const void *key,
                                  cfs_hlist_node_t *hnode, int noref)
 {
         cfs_hlist_node_t  *ehnode;
@@ -770,7 +780,8 @@ cfs_hash_multi_bd_findadd_locked(cfs_hash_t *hs,
 
 static cfs_hlist_node_t *
 cfs_hash_multi_bd_finddel_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
-                                 unsigned n, void *key, cfs_hlist_node_t *hnode)
+                                 unsigned n, const void *key,
+                                 cfs_hlist_node_t *hnode)
 {
         cfs_hlist_node_t  *ehnode;
         unsigned           i;
@@ -812,7 +823,7 @@ cfs_hash_bd_order(cfs_hash_bd_t *bd1, cfs_hash_bd_t *bd2)
 }
 
 void
-cfs_hash_dual_bd_get(cfs_hash_t *hs, void *key, cfs_hash_bd_t *bds)
+cfs_hash_dual_bd_get(cfs_hash_t *hs, const void *key, cfs_hash_bd_t *bds)
 {
         /* NB: caller should hold hs_lock.rw if REHASH is set */
         cfs_hash_bd_from_key(hs, hs->hs_buckets,
@@ -846,7 +857,8 @@ cfs_hash_dual_bd_unlock(cfs_hash_t *hs, cfs_hash_bd_t *bds, int excl)
 CFS_EXPORT_SYMBOL(cfs_hash_dual_bd_unlock);
 
 cfs_hlist_node_t *
-cfs_hash_dual_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds, void *key)
+cfs_hash_dual_bd_lookup_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
+                               const void *key)
 {
         return cfs_hash_multi_bd_lookup_locked(hs, bds, 2, key);
 }
@@ -854,7 +866,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_dual_bd_lookup_locked);
 
 cfs_hlist_node_t *
 cfs_hash_dual_bd_findadd_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
-                                void *key, cfs_hlist_node_t *hnode,
+                                const void *key, cfs_hlist_node_t *hnode,
                                 int noref)
 {
         return cfs_hash_multi_bd_findadd_locked(hs, bds, 2, key,
@@ -864,7 +876,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_dual_bd_findadd_locked);
 
 cfs_hlist_node_t *
 cfs_hash_dual_bd_finddel_locked(cfs_hash_t *hs, cfs_hash_bd_t *bds,
-                                void *key, cfs_hlist_node_t *hnode)
+                                const void *key, cfs_hlist_node_t *hnode)
 {
         return cfs_hash_multi_bd_finddel_locked(hs, bds, 2, key, hnode);
 }
@@ -932,14 +944,14 @@ cfs_hash_buckets_realloc(cfs_hash_t *hs, cfs_hash_bucket_t **old_bkts,
                     cfs_hash_with_no_bktlock(hs))
                         continue;
 
-                if (cfs_hash_with_rw_bktlock(hs))
-                        cfs_rwlock_init(&new_bkts[i]->hsb_lock.rw);
-                else if (cfs_hash_with_spin_bktlock(hs))
-                        cfs_spin_lock_init(&new_bkts[i]->hsb_lock.spin);
-                else
-                        LBUG(); /* invalid use-case */
-        }
-        return new_bkts;
+               if (cfs_hash_with_rw_bktlock(hs))
+                       rwlock_init(&new_bkts[i]->hsb_lock.rw);
+               else if (cfs_hash_with_spin_bktlock(hs))
+                       spin_lock_init(&new_bkts[i]->hsb_lock.spin);
+               else
+                       LBUG(); /* invalid use-case */
+       }
+       return new_bkts;
 }
 
 /**
@@ -956,46 +968,45 @@ static int cfs_hash_rehash_worker(cfs_workitem_t *wi);
 #if CFS_HASH_DEBUG_LEVEL >= CFS_HASH_DEBUG_1
 static int cfs_hash_dep_print(cfs_workitem_t *wi)
 {
-        cfs_hash_t *hs = container_of(wi, cfs_hash_t, hs_dep_wi);
-        int         dep;
-        int         bkt;
-        int         off;
-        int         bits;
-
-        cfs_spin_lock(&hs->hs_dep_lock);
-        dep  = hs->hs_dep_max;
-        bkt  = hs->hs_dep_bkt;
-        off  = hs->hs_dep_off;
-        bits = hs->hs_dep_bits;
-        cfs_spin_unlock(&hs->hs_dep_lock);
-
-        LCONSOLE_WARN("#### HASH %s (bits: %d): max depth %d at bucket %d/%d\n",
-                      hs->hs_name, bits, dep, bkt, off);
-        cfs_spin_lock(&hs->hs_dep_lock);
-        hs->hs_dep_bits = 0; /* mark as workitem done */
-        cfs_spin_unlock(&hs->hs_dep_lock);
-        return 0;
+       cfs_hash_t *hs = container_of(wi, cfs_hash_t, hs_dep_wi);
+       int         dep;
+       int         bkt;
+       int         off;
+       int         bits;
+
+       spin_lock(&hs->hs_dep_lock);
+       dep  = hs->hs_dep_max;
+       bkt  = hs->hs_dep_bkt;
+       off  = hs->hs_dep_off;
+       bits = hs->hs_dep_bits;
+       spin_unlock(&hs->hs_dep_lock);
+
+       LCONSOLE_WARN("#### HASH %s (bits: %d): max depth %d at bucket %d/%d\n",
+                     hs->hs_name, bits, dep, bkt, off);
+       spin_lock(&hs->hs_dep_lock);
+       hs->hs_dep_bits = 0; /* mark as workitem done */
+       spin_unlock(&hs->hs_dep_lock);
+       return 0;
 }
 
 static void cfs_hash_depth_wi_init(cfs_hash_t *hs)
 {
-        cfs_spin_lock_init(&hs->hs_dep_lock);
-        cfs_wi_init(&hs->hs_dep_wi, hs,
-                    cfs_hash_dep_print, CFS_WI_SCHED_ANY);
+       spin_lock_init(&hs->hs_dep_lock);
+       cfs_wi_init(&hs->hs_dep_wi, hs, cfs_hash_dep_print);
 }
 
 static void cfs_hash_depth_wi_cancel(cfs_hash_t *hs)
 {
-        if (cfs_wi_cancel(&hs->hs_dep_wi))
-                return;
+       if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_dep_wi))
+               return;
 
-        cfs_spin_lock(&hs->hs_dep_lock);
-        while (hs->hs_dep_bits != 0) {
-                cfs_spin_unlock(&hs->hs_dep_lock);
-                cfs_cond_resched();
-                cfs_spin_lock(&hs->hs_dep_lock);
-        }
-        cfs_spin_unlock(&hs->hs_dep_lock);
+       spin_lock(&hs->hs_dep_lock);
+       while (hs->hs_dep_bits != 0) {
+               spin_unlock(&hs->hs_dep_lock);
+               cfs_cond_resched();
+               spin_lock(&hs->hs_dep_lock);
+       }
+       spin_unlock(&hs->hs_dep_lock);
 }
 
 #else /* CFS_HASH_DEBUG_LEVEL < CFS_HASH_DEBUG_1 */
@@ -1063,8 +1074,7 @@ cfs_hash_create(char *name, unsigned cur_bits, unsigned max_bits,
         hs->hs_ops         = ops;
         hs->hs_extra_bytes = extra_bytes;
         hs->hs_rehash_bits = 0;
-        cfs_wi_init(&hs->hs_rehash_wi, hs,
-                    cfs_hash_rehash_worker, CFS_WI_SCHED_ANY);
+       cfs_wi_init(&hs->hs_rehash_wi, hs, cfs_hash_rehash_worker);
         cfs_hash_depth_wi_init(hs);
 
         if (cfs_hash_with_rehash(hs))
@@ -1209,7 +1219,7 @@ cfs_hash_rehash_inline(cfs_hash_t *hs)
  * ops->hs_get function will be called when the item is added.
  */
 void
-cfs_hash_add(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
+cfs_hash_add(cfs_hash_t *hs, const void *key, cfs_hlist_node_t *hnode)
 {
         cfs_hash_bd_t   bd;
         int             bits;
@@ -1232,7 +1242,7 @@ cfs_hash_add(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
 CFS_EXPORT_SYMBOL(cfs_hash_add);
 
 static cfs_hlist_node_t *
-cfs_hash_find_or_add(cfs_hash_t *hs, void *key,
+cfs_hash_find_or_add(cfs_hash_t *hs, const void *key,
                      cfs_hlist_node_t *hnode, int noref)
 {
         cfs_hlist_node_t *ehnode;
@@ -1264,10 +1274,10 @@ cfs_hash_find_or_add(cfs_hash_t *hs, void *key,
  * Returns 0 on success or -EALREADY on key collisions.
  */
 int
-cfs_hash_add_unique(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
+cfs_hash_add_unique(cfs_hash_t *hs, const void *key, cfs_hlist_node_t *hnode)
 {
-        RETURN(cfs_hash_find_or_add(hs, key, hnode, 1) != hnode ?
-               -EALREADY : 0);
+        return cfs_hash_find_or_add(hs, key, hnode, 1) != hnode ?
+               -EALREADY : 0;
 }
 CFS_EXPORT_SYMBOL(cfs_hash_add_unique);
 
@@ -1278,7 +1288,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_add_unique);
  * Otherwise ops->hs_get is called on the item which was added.
  */
 void *
-cfs_hash_findadd_unique(cfs_hash_t *hs, void *key,
+cfs_hash_findadd_unique(cfs_hash_t *hs, const void *key,
                         cfs_hlist_node_t *hnode)
 {
         hnode = cfs_hash_find_or_add(hs, key, hnode, 0);
@@ -1295,7 +1305,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_findadd_unique);
  * on the removed object.
  */
 void *
-cfs_hash_del(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
+cfs_hash_del(cfs_hash_t *hs, const void *key, cfs_hlist_node_t *hnode)
 {
         void           *obj  = NULL;
         int             bits = 0;
@@ -1304,10 +1314,15 @@ cfs_hash_del(cfs_hash_t *hs, void *key, cfs_hlist_node_t *hnode)
         cfs_hash_lock(hs, 0);
         cfs_hash_dual_bd_get_and_lock(hs, key, bds, 1);
 
-        if (bds[1].bd_bucket == NULL && hnode != NULL)
-                cfs_hash_bd_del_locked(hs, &bds[0], hnode);
-        else
-                hnode = cfs_hash_dual_bd_finddel_locked(hs, bds, key, hnode);
+       /* NB: do nothing if @hnode is not in hash table */
+       if (hnode == NULL || !cfs_hlist_unhashed(hnode)) {
+               if (bds[1].bd_bucket == NULL && hnode != NULL) {
+                       cfs_hash_bd_del_locked(hs, &bds[0], hnode);
+               } else {
+                       hnode = cfs_hash_dual_bd_finddel_locked(hs, bds,
+                                                               key, hnode);
+               }
+       }
 
         if (hnode != NULL) {
                 obj  = cfs_hash_object(hs, hnode);
@@ -1330,7 +1345,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_del);
  * will be returned and ops->hs_put is called on the removed object.
  */
 void *
-cfs_hash_del_key(cfs_hash_t *hs, void *key)
+cfs_hash_del_key(cfs_hash_t *hs, const void *key)
 {
         return cfs_hash_del(hs, key, NULL);
 }
@@ -1345,7 +1360,7 @@ CFS_EXPORT_SYMBOL(cfs_hash_del_key);
  * in the hash @hs NULL is returned.
  */
 void *
-cfs_hash_lookup(cfs_hash_t *hs, void *key)
+cfs_hash_lookup(cfs_hash_t *hs, const void *key)
 {
         void                 *obj = NULL;
         cfs_hlist_node_t     *hnode;
@@ -1572,7 +1587,6 @@ cfs_hash_for_each_relax(cfs_hash_t *hs, cfs_hash_for_each_cb_t func, void *data)
 {
         cfs_hlist_node_t *hnode;
         cfs_hlist_node_t *tmp;
-        void             *obj;
         cfs_hash_bd_t     bd;
         __u32             version;
         int               count = 0;
@@ -1596,13 +1610,13 @@ cfs_hash_for_each_relax(cfs_hash_t *hs, cfs_hash_for_each_cb_t func, void *data)
                 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
                         for (hnode = hhead->first; hnode != NULL;) {
                                 cfs_hash_bucket_validate(hs, &bd, hnode);
-                                obj = cfs_hash_get(hs, hnode);
+                                cfs_hash_get(hs, hnode);
                                 cfs_hash_bd_unlock(hs, &bd, 0);
                                 cfs_hash_unlock(hs, 0);
 
                                 rc = func(hs, &bd, hnode, data);
                                 if (stop_on_change)
-                                        (void)cfs_hash_put(hs, hnode);
+                                        cfs_hash_put(hs, hnode);
                                 cfs_cond_resched();
                                 count++;
 
@@ -1697,15 +1711,13 @@ cfs_hash_hlist_for_each(cfs_hash_t *hs, unsigned hindex,
         cfs_hlist_head_t   *hhead;
         cfs_hlist_node_t   *hnode;
         cfs_hash_bd_t       bd;
-        ENTRY;
 
         cfs_hash_for_each_enter(hs);
         cfs_hash_lock(hs, 0);
         if (hindex >= CFS_HASH_NHLIST(hs))
                 goto out;
 
-        bd.bd_bucket = hs->hs_buckets[hindex >> hs->hs_bkt_bits];
-        bd.bd_offset = hindex & (CFS_HASH_BKT_NHLIST(hs) - 1);
+        cfs_hash_bd_index_set(hs, hindex, &bd);
 
         cfs_hash_bd_lock(hs, &bd, 0);
         hhead = cfs_hash_bd_hhead(hs, &bd);
@@ -1717,7 +1729,6 @@ cfs_hash_hlist_for_each(cfs_hash_t *hs, unsigned hindex,
  out:
         cfs_hash_unlock(hs, 0);
         cfs_hash_for_each_exit(hs);
-        EXIT;
 }
 
 CFS_EXPORT_SYMBOL(cfs_hash_hlist_for_each);
@@ -1729,13 +1740,12 @@ CFS_EXPORT_SYMBOL(cfs_hash_hlist_for_each);
  * is held so the callback must never sleep.
    */
 void
-cfs_hash_for_each_key(cfs_hash_t *hs, void *key,
+cfs_hash_for_each_key(cfs_hash_t *hs, const void *key,
                       cfs_hash_for_each_cb_t func, void *data)
 {
         cfs_hlist_node_t   *hnode;
         cfs_hash_bd_t       bds[2];
         unsigned            i;
-        ENTRY;
 
         cfs_hash_lock(hs, 0);
 
@@ -1756,8 +1766,6 @@ cfs_hash_for_each_key(cfs_hash_t *hs, void *key,
 
         cfs_hash_dual_bd_unlock(hs, bds, 0);
         cfs_hash_unlock(hs, 0);
-
-        EXIT;
 }
 CFS_EXPORT_SYMBOL(cfs_hash_for_each_key);
 
@@ -1784,7 +1792,7 @@ cfs_hash_rehash_cancel_locked(cfs_hash_t *hs)
         if (!cfs_hash_is_rehashing(hs))
                 return;
 
-        if (cfs_wi_cancel(&hs->hs_rehash_wi)) {
+       if (cfs_wi_deschedule(cfs_sched_rehash, &hs->hs_rehash_wi)) {
                 hs->hs_rehash_bits = 0;
                 return;
         }
@@ -1814,7 +1822,6 @@ int
 cfs_hash_rehash(cfs_hash_t *hs, int do_rehash)
 {
         int     rc;
-        ENTRY;
 
         LASSERT(cfs_hash_with_rehash(hs) && !cfs_hash_with_no_lock(hs));
 
@@ -1829,7 +1836,7 @@ cfs_hash_rehash(cfs_hash_t *hs, int do_rehash)
         hs->hs_rehash_bits = rc;
         if (!do_rehash) {
                 /* launch and return */
-                cfs_wi_schedule(&hs->hs_rehash_wi);
+               cfs_wi_schedule(cfs_sched_rehash, &hs->hs_rehash_wi);
                 cfs_hash_unlock(hs, 1);
                 return 0;
         }
@@ -1884,7 +1891,6 @@ cfs_hash_rehash_worker(cfs_workitem_t *wi)
         int                 count = 0;
         int                 rc = 0;
         int                 i;
-        ENTRY;
 
         LASSERT (hs != NULL && cfs_hash_with_rehash(hs));
 
@@ -1960,8 +1966,8 @@ cfs_hash_rehash_worker(cfs_workitem_t *wi)
         hs->hs_cur_bits = hs->hs_rehash_bits;
  out:
         hs->hs_rehash_bits = 0;
-        if (rc == -ESRCH)
-                cfs_wi_exit(wi); /* never be scheduled again */
+       if (rc == -ESRCH) /* never be scheduled again */
+               cfs_wi_exit(cfs_sched_rehash, wi);
         bsize = cfs_hash_bkt_size(hs);
         cfs_hash_unlock(hs, 1);
         /* can't refer to @hs anymore because it could be destroyed */
@@ -1969,8 +1975,8 @@ cfs_hash_rehash_worker(cfs_workitem_t *wi)
                 cfs_hash_buckets_free(bkts, bsize, new_size, old_size);
         if (rc != 0)
                 CDEBUG(D_INFO, "early quit of of rehashing: %d\n", rc);
-        /* cfs_workitem require us to always return 0 */
-        RETURN(0);
+       /* return 1 only if cfs_wi_exit is called */
+       return rc == -ESRCH;
 }
 
 /**
@@ -1983,7 +1989,7 @@ cfs_hash_rehash_worker(cfs_workitem_t *wi)
  * the registered cfs_hash_get() and cfs_hash_put() functions will
  * not be called.
  */
-void cfs_hash_rehash_key(cfs_hash_t *hs, void *old_key,
+void cfs_hash_rehash_key(cfs_hash_t *hs, const void *old_key,
                          void *new_key, cfs_hlist_node_t *hnode)
 {
         cfs_hash_bd_t        bds[3];
@@ -2109,11 +2115,11 @@ int cfs_hash_debug_str(cfs_hash_t *hs, char *str, int size)
                 if (maxdep < bd.bd_bucket->hsb_depmax) {
                         maxdep  = bd.bd_bucket->hsb_depmax;
 #ifdef __KERNEL__
-                        maxdepb = cfs_ffz(~maxdep);
+                       maxdepb = ffz(~maxdep);
 #endif
                 }
                 total += bd.bd_bucket->hsb_count;
-                dist[min(__cfs_fls(bd.bd_bucket->hsb_count/max(theta,1)),7)]++;
+                dist[min(fls(bd.bd_bucket->hsb_count/max(theta,1)),7)]++;
                 cfs_hash_bd_unlock(hs, &bd, 0);
         }