[PATCH v3 12/17] dm: use new hashtable implementation

2012-08-21 Thread Sasha Levin
Switch dm to use the new hashtable implementation. This reduces the amount of
generic unrelated code in the dm.

Signed-off-by: Sasha Levin 
---
 drivers/md/dm-snap.c   |   24 ---
 drivers/md/persistent-data/dm-block-manager.c  |1 -
 .../persistent-data/dm-persistent-data-internal.h  |   19 
 .../md/persistent-data/dm-transaction-manager.c|   30 ++--
 4 files changed, 16 insertions(+), 58 deletions(-)
 delete mode 100644 drivers/md/persistent-data/dm-persistent-data-internal.h

diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c
index a143921..7ac121f 100644
--- a/drivers/md/dm-snap.c
+++ b/drivers/md/dm-snap.c
@@ -34,9 +34,7 @@ static const char dm_snapshot_merge_target_name[] = 
"snapshot-merge";
  */
 #define MIN_IOS 256
 
-#define DM_TRACKED_CHUNK_HASH_SIZE 16
-#define DM_TRACKED_CHUNK_HASH(x)   ((unsigned long)(x) & \
-(DM_TRACKED_CHUNK_HASH_SIZE - 1))
+#define DM_TRACKED_CHUNK_HASH_BITS 4
 
 struct dm_exception_table {
uint32_t hash_mask;
@@ -80,7 +78,7 @@ struct dm_snapshot {
/* Chunks with outstanding reads */
spinlock_t tracked_chunk_lock;
mempool_t *tracked_chunk_pool;
-   struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
+   DEFINE_HASHTABLE(tracked_chunk_hash, DM_TRACKED_CHUNK_HASH_BITS);
 
/* The on disk metadata handler */
struct dm_exception_store *store;
@@ -203,8 +201,7 @@ static struct dm_snap_tracked_chunk *track_chunk(struct 
dm_snapshot *s,
c->chunk = chunk;
 
spin_lock_irqsave(>tracked_chunk_lock, flags);
-   hlist_add_head(>node,
-  >tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
+   hash_add(s->tracked_chunk_hash, >node, chunk);
spin_unlock_irqrestore(>tracked_chunk_lock, flags);
 
return c;
@@ -216,7 +213,7 @@ static void stop_tracking_chunk(struct dm_snapshot *s,
unsigned long flags;
 
spin_lock_irqsave(>tracked_chunk_lock, flags);
-   hlist_del(>node);
+   hash_del(>node);
spin_unlock_irqrestore(>tracked_chunk_lock, flags);
 
mempool_free(c, s->tracked_chunk_pool);
@@ -230,8 +227,7 @@ static int __chunk_is_tracked(struct dm_snapshot *s, 
chunk_t chunk)
 
spin_lock_irq(>tracked_chunk_lock);
 
-   hlist_for_each_entry(c, hn,
-   >tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
+   hash_for_each_possible(s->tracked_chunk_hash, c, hn, node, chunk) {
if (c->chunk == chunk) {
found = 1;
break;
@@ -1033,7 +1029,6 @@ static void stop_merge(struct dm_snapshot *s)
 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 {
struct dm_snapshot *s;
-   int i;
int r = -EINVAL;
char *origin_path, *cow_path;
unsigned args_used, num_flush_requests = 1;
@@ -1128,8 +1123,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned 
int argc, char **argv)
goto bad_tracked_chunk_pool;
}
 
-   for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
-   INIT_HLIST_HEAD(>tracked_chunk_hash[i]);
+   hash_init(s->tracked_chunk_hash);
 
spin_lock_init(>tracked_chunk_lock);
 
@@ -1253,9 +1247,6 @@ static void __handover_exceptions(struct dm_snapshot 
*snap_src,
 
 static void snapshot_dtr(struct dm_target *ti)
 {
-#ifdef CONFIG_DM_DEBUG
-   int i;
-#endif
struct dm_snapshot *s = ti->private;
struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
 
@@ -1286,8 +1277,7 @@ static void snapshot_dtr(struct dm_target *ti)
smp_mb();
 
 #ifdef CONFIG_DM_DEBUG
-   for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
-   BUG_ON(!hlist_empty(>tracked_chunk_hash[i]));
+   BUG_ON(!hash_empty(s->tracked_chunk_hash));
 #endif
 
mempool_destroy(s->tracked_chunk_pool);
diff --git a/drivers/md/persistent-data/dm-block-manager.c 
b/drivers/md/persistent-data/dm-block-manager.c
index 5ba2777..31edaf13 100644
--- a/drivers/md/persistent-data/dm-block-manager.c
+++ b/drivers/md/persistent-data/dm-block-manager.c
@@ -4,7 +4,6 @@
  * This file is released under the GPL.
  */
 #include "dm-block-manager.h"
-#include "dm-persistent-data-internal.h"
 #include "../dm-bufio.h"
 
 #include 
diff --git a/drivers/md/persistent-data/dm-persistent-data-internal.h 
b/drivers/md/persistent-data/dm-persistent-data-internal.h
deleted file mode 100644
index c49e26f..000
--- a/drivers/md/persistent-data/dm-persistent-data-internal.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2011 Red Hat, Inc.
- *
- * This file is released under the GPL.
- */
-
-#ifndef _DM_PERSISTENT_DATA_INTERNAL_H
-#define _DM_PERSISTENT_DATA_INTERNAL_H
-
-#include "dm-block-manager.h"
-
-static inline unsigned dm_hash_block(dm_block_t b, unsigned hash_mask)
-{
-   const unsigned BIG_PRIME = 

[PATCH v3 12/17] dm: use new hashtable implementation

2012-08-21 Thread Sasha Levin
Switch dm to use the new hashtable implementation. This reduces the amount of
generic unrelated code in the dm.

Signed-off-by: Sasha Levin levinsasha...@gmail.com
---
 drivers/md/dm-snap.c   |   24 ---
 drivers/md/persistent-data/dm-block-manager.c  |1 -
 .../persistent-data/dm-persistent-data-internal.h  |   19 
 .../md/persistent-data/dm-transaction-manager.c|   30 ++--
 4 files changed, 16 insertions(+), 58 deletions(-)
 delete mode 100644 drivers/md/persistent-data/dm-persistent-data-internal.h

diff --git a/drivers/md/dm-snap.c b/drivers/md/dm-snap.c
index a143921..7ac121f 100644
--- a/drivers/md/dm-snap.c
+++ b/drivers/md/dm-snap.c
@@ -34,9 +34,7 @@ static const char dm_snapshot_merge_target_name[] = 
snapshot-merge;
  */
 #define MIN_IOS 256
 
-#define DM_TRACKED_CHUNK_HASH_SIZE 16
-#define DM_TRACKED_CHUNK_HASH(x)   ((unsigned long)(x)  \
-(DM_TRACKED_CHUNK_HASH_SIZE - 1))
+#define DM_TRACKED_CHUNK_HASH_BITS 4
 
 struct dm_exception_table {
uint32_t hash_mask;
@@ -80,7 +78,7 @@ struct dm_snapshot {
/* Chunks with outstanding reads */
spinlock_t tracked_chunk_lock;
mempool_t *tracked_chunk_pool;
-   struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
+   DEFINE_HASHTABLE(tracked_chunk_hash, DM_TRACKED_CHUNK_HASH_BITS);
 
/* The on disk metadata handler */
struct dm_exception_store *store;
@@ -203,8 +201,7 @@ static struct dm_snap_tracked_chunk *track_chunk(struct 
dm_snapshot *s,
c-chunk = chunk;
 
spin_lock_irqsave(s-tracked_chunk_lock, flags);
-   hlist_add_head(c-node,
-  s-tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
+   hash_add(s-tracked_chunk_hash, c-node, chunk);
spin_unlock_irqrestore(s-tracked_chunk_lock, flags);
 
return c;
@@ -216,7 +213,7 @@ static void stop_tracking_chunk(struct dm_snapshot *s,
unsigned long flags;
 
spin_lock_irqsave(s-tracked_chunk_lock, flags);
-   hlist_del(c-node);
+   hash_del(c-node);
spin_unlock_irqrestore(s-tracked_chunk_lock, flags);
 
mempool_free(c, s-tracked_chunk_pool);
@@ -230,8 +227,7 @@ static int __chunk_is_tracked(struct dm_snapshot *s, 
chunk_t chunk)
 
spin_lock_irq(s-tracked_chunk_lock);
 
-   hlist_for_each_entry(c, hn,
-   s-tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
+   hash_for_each_possible(s-tracked_chunk_hash, c, hn, node, chunk) {
if (c-chunk == chunk) {
found = 1;
break;
@@ -1033,7 +1029,6 @@ static void stop_merge(struct dm_snapshot *s)
 static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
 {
struct dm_snapshot *s;
-   int i;
int r = -EINVAL;
char *origin_path, *cow_path;
unsigned args_used, num_flush_requests = 1;
@@ -1128,8 +1123,7 @@ static int snapshot_ctr(struct dm_target *ti, unsigned 
int argc, char **argv)
goto bad_tracked_chunk_pool;
}
 
-   for (i = 0; i  DM_TRACKED_CHUNK_HASH_SIZE; i++)
-   INIT_HLIST_HEAD(s-tracked_chunk_hash[i]);
+   hash_init(s-tracked_chunk_hash);
 
spin_lock_init(s-tracked_chunk_lock);
 
@@ -1253,9 +1247,6 @@ static void __handover_exceptions(struct dm_snapshot 
*snap_src,
 
 static void snapshot_dtr(struct dm_target *ti)
 {
-#ifdef CONFIG_DM_DEBUG
-   int i;
-#endif
struct dm_snapshot *s = ti-private;
struct dm_snapshot *snap_src = NULL, *snap_dest = NULL;
 
@@ -1286,8 +1277,7 @@ static void snapshot_dtr(struct dm_target *ti)
smp_mb();
 
 #ifdef CONFIG_DM_DEBUG
-   for (i = 0; i  DM_TRACKED_CHUNK_HASH_SIZE; i++)
-   BUG_ON(!hlist_empty(s-tracked_chunk_hash[i]));
+   BUG_ON(!hash_empty(s-tracked_chunk_hash));
 #endif
 
mempool_destroy(s-tracked_chunk_pool);
diff --git a/drivers/md/persistent-data/dm-block-manager.c 
b/drivers/md/persistent-data/dm-block-manager.c
index 5ba2777..31edaf13 100644
--- a/drivers/md/persistent-data/dm-block-manager.c
+++ b/drivers/md/persistent-data/dm-block-manager.c
@@ -4,7 +4,6 @@
  * This file is released under the GPL.
  */
 #include dm-block-manager.h
-#include dm-persistent-data-internal.h
 #include ../dm-bufio.h
 
 #include linux/crc32c.h
diff --git a/drivers/md/persistent-data/dm-persistent-data-internal.h 
b/drivers/md/persistent-data/dm-persistent-data-internal.h
deleted file mode 100644
index c49e26f..000
--- a/drivers/md/persistent-data/dm-persistent-data-internal.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * Copyright (C) 2011 Red Hat, Inc.
- *
- * This file is released under the GPL.
- */
-
-#ifndef _DM_PERSISTENT_DATA_INTERNAL_H
-#define _DM_PERSISTENT_DATA_INTERNAL_H
-
-#include dm-block-manager.h
-
-static inline unsigned dm_hash_block(dm_block_t b, unsigned hash_mask)
-{
-   const