Whamcloud - gitweb
LU-14550 libcfs: fix setting of debug_path
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_hash.h
index 28bb503..d0e452c 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
@@ -43,6 +39,8 @@
 #define __LIBCFS_HASH_H__
 
 #include <linux/hash.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
 
 /*
  * Knuth recommends primes in approximately golden ratio to the maximum
@@ -76,6 +74,7 @@ struct cfs_hash_hlist_ops;
 union cfs_hash_lock {
        rwlock_t                rw;             /**< rwlock */
        spinlock_t              spin;           /**< spinlock */
+       struct rw_semaphore     rw_sem;         /**< rwsem */
 };
 
 /**
@@ -119,46 +118,49 @@ struct cfs_hash_bd {
  * common hash attributes.
  */
 enum cfs_hash_tag {
-        /**
-         * don't need any lock, caller will protect operations with it's
-         * own lock. With this flag:
-         *  . CFS_HASH_NO_BKTLOCK, CFS_HASH_RW_BKTLOCK, CFS_HASH_SPIN_BKTLOCK
-         *    will be ignored.
-         *  . Some functions will be disabled with this flag, i.e:
-         *    cfs_hash_for_each_empty, cfs_hash_rehash
-         */
-        CFS_HASH_NO_LOCK        = 1 << 0,
-        /** no bucket lock, use one spinlock to protect the whole hash */
-        CFS_HASH_NO_BKTLOCK     = 1 << 1,
-        /** rwlock to protect bucket */
-        CFS_HASH_RW_BKTLOCK     = 1 << 2,
+       /**
+        * don't need any lock, caller will protect operations with it's
+        * own lock. With this flag:
+        *  . CFS_HASH_NO_BKTLOCK, CFS_HASH_RW_BKTLOCK, CFS_HASH_SPIN_BKTLOCK
+        *    will be ignored.
+        *  . Some functions will be disabled with this flag, i.e:
+        *    cfs_hash_for_each_empty, cfs_hash_rehash
+        */
+       CFS_HASH_NO_LOCK        = BIT(0),
+       /** no bucket lock, use one spinlock to protect the whole hash */
+       CFS_HASH_NO_BKTLOCK     = BIT(1),
+       /** rwlock to protect bucket */
+       CFS_HASH_RW_BKTLOCK     = BIT(2),
        /** spinlock to protect bucket */
-        CFS_HASH_SPIN_BKTLOCK   = 1 << 3,
-        /** always add new item to tail */
-        CFS_HASH_ADD_TAIL       = 1 << 4,
-        /** hash-table doesn't have refcount on item */
-        CFS_HASH_NO_ITEMREF     = 1 << 5,
-        /** big name for param-tree */
-        CFS_HASH_BIGNAME        = 1 << 6,
-        /** track global count */
-        CFS_HASH_COUNTER        = 1 << 7,
-        /** rehash item by new key */
-        CFS_HASH_REHASH_KEY     = 1 << 8,
-        /** Enable dynamic hash resizing */
-        CFS_HASH_REHASH         = 1 << 9,
-        /** can shrink hash-size */
-        CFS_HASH_SHRINK         = 1 << 10,
-        /** assert hash is empty on exit */
-        CFS_HASH_ASSERT_EMPTY   = 1 << 11,
-        /** record hlist depth */
-        CFS_HASH_DEPTH          = 1 << 12,
-        /**
-         * rehash is always scheduled in a different thread, so current
-         * change on hash table is non-blocking
-         */
-        CFS_HASH_NBLK_CHANGE    = 1 << 13,
-        /** NB, we typed hs_flags as  __u16, please change it
-         * if you need to extend >=16 flags */
+       CFS_HASH_SPIN_BKTLOCK   = BIT(3),
+       /** always add new item to tail */
+       CFS_HASH_ADD_TAIL       = BIT(4),
+       /** hash-table doesn't have refcount on item */
+       CFS_HASH_NO_ITEMREF     = BIT(5),
+       /** big name for param-tree */
+       CFS_HASH_BIGNAME        = BIT(6),
+       /** track global count */
+       CFS_HASH_COUNTER        = BIT(7),
+       /** rehash item by new key */
+       CFS_HASH_REHASH_KEY     = BIT(8),
+       /** Enable dynamic hash resizing */
+       CFS_HASH_REHASH         = BIT(9),
+       /** can shrink hash-size */
+       CFS_HASH_SHRINK         = BIT(10),
+       /** assert hash is empty on exit */
+       CFS_HASH_ASSERT_EMPTY   = BIT(11),
+       /** record hlist depth */
+       CFS_HASH_DEPTH          = BIT(12),
+       /**
+        * rehash is always scheduled in a different thread, so current
+        * change on hash table is non-blocking
+        */
+       CFS_HASH_NBLK_CHANGE    = BIT(13),
+       /** rw semaphore lock to protect bucket */
+       CFS_HASH_RW_SEM_BKTLOCK = BIT(14),
+       /** NB, we typed hs_flags as  __u16, please change it
+        * if you need to extend >=16 flags
+        */
 };
 
 /** most used attributes */
@@ -245,7 +247,7 @@ struct cfs_hash {
         /** # of iterators (caller of cfs_hash_for_each_*) */
         __u32                       hs_iterators;
        /** rehash workitem */
-       struct cfs_workitem             hs_rehash_wi;
+       struct work_struct              hs_rehash_work;
        /** refcount on this hash table */
        atomic_t                        hs_refcount;
        /** rehash buckets-table */
@@ -262,7 +264,7 @@ struct cfs_hash {
         /** bits when we found the max depth */
         unsigned int                hs_dep_bits;
         /** workitem to output max depth */
-       struct cfs_workitem         hs_dep_wi;
+       struct work_struct              hs_dep_work;
 #endif
         /** name of htable */
         char                        hs_name[0];
@@ -362,6 +364,13 @@ cfs_hash_with_spin_bktlock(struct cfs_hash *hs)
 }
 
 static inline int
+cfs_hash_with_rw_sem_bktlock(struct cfs_hash *hs)
+{
+       /* rw sem lock to protect hash bucket */
+       return (hs->hs_flags & CFS_HASH_RW_SEM_BKTLOCK) != 0;
+}
+
+static inline int
 cfs_hash_with_add_tail(struct cfs_hash *hs)
 {
         return (hs->hs_flags & CFS_HASH_ADD_TAIL) != 0;
@@ -733,7 +742,7 @@ __u64 cfs_hash_size_get(struct cfs_hash *hs);
  */
 void cfs_hash_rehash_cancel_locked(struct cfs_hash *hs);
 void cfs_hash_rehash_cancel(struct cfs_hash *hs);
-int  cfs_hash_rehash(struct cfs_hash *hs, int do_rehash);
+void cfs_hash_rehash(struct cfs_hash *hs, int do_rehash);
 void cfs_hash_rehash_key(struct cfs_hash *hs, const void *old_key,
                        void *new_key, struct hlist_node *hnode);
 
@@ -803,8 +812,8 @@ __cfs_hash_set_theta(struct cfs_hash *hs, int min, int max)
 
 /* Generic debug formatting routines mainly for proc handler */
 struct seq_file;
-int cfs_hash_debug_header(struct seq_file *m);
-int cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m);
+void cfs_hash_debug_header(struct seq_file *m);
+void cfs_hash_debug_str(struct cfs_hash *hs, struct seq_file *m);
 
 /*
  * Generic djb2 hash algorithm for character arrays.