>From 05ca99b07e28e1fe970e0ed547b51f93a9c0b94c Mon Sep 17 00:00:00 2001
From: Changman Lee <cm224.lee@samsung.com>
Date: Tue, 29 Oct 2013 16:34:43 +0900
Subject: [PATCH] f2fs: use pre-calculated value to get sum of valid blocks

Using pre-calculated value is more fast than looking into bitmap on
runnnig time.

Signed-off-by: Changman Lee <cm224.lee@samsung.com>
---
 fs/f2fs/f2fs.h    |    1 +
 fs/f2fs/hash.c    |   24 ++++++++++++++++++++++++
 fs/f2fs/segment.h |    4 ++--
 3 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index a61cc5f..288be6a 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -71,6 +71,7 @@ static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
 	return f2fs_crc32(buf, buf_size) == blk_crc;
 }
 
+extern int bit_count_byte(unsigned char);
 /*
  * For checkpoint manager
  */
diff --git a/fs/f2fs/hash.c b/fs/f2fs/hash.c
index 6eb8d26..2dd9b5a 100644
--- a/fs/f2fs/hash.c
+++ b/fs/f2fs/hash.c
@@ -99,3 +99,27 @@ f2fs_hash_t f2fs_dentry_hash(const char *name, size_t len)
 	f2fs_hash = cpu_to_le32(hash & ~F2FS_HASH_COL_BIT);
 	return f2fs_hash;
 }
+
+static const int bits_in_byte[256] = {
+	0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
+	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
+	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
+	1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
+	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
+	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
+	3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
+	4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
+};
+
+int bit_count_byte(unsigned char n)
+{
+	return bits_in_byte[n];
+}
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index abe7094..d5aa4cc 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -561,8 +561,8 @@ static inline void check_block_count(struct f2fs_sb_info *sbi,
 
 	/* check bitmap with valid block count */
 	for (i = 0; i < sbi->blocks_per_seg; i++)
-		if (f2fs_test_bit(i, raw_sit->valid_map))
-			valid_blocks++;
+		valid_blocks += bit_count_byte(raw_sit->valid_map[i]);
+
 	BUG_ON(GET_SIT_VBLOCKS(raw_sit) != valid_blocks);
 }
 
-- 
1.7.10.4

