linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] Bit helpers cleanup
@ 2019-10-01 17:52 David Sterba
  2019-10-01 17:52 ` [PATCH 1/2] btrfs: add 64bit safe helper for power of two checks David Sterba
  2019-10-01 17:52 ` [PATCH 2/2] btrfs: use has_single_bit_set for clarity David Sterba
  0 siblings, 2 replies; 3+ messages in thread
From: David Sterba @ 2019-10-01 17:52 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Depends on patch "btrfs: fix balance convert to single on 32-bit host
CPUs" that's now in misc-next and cleans up the helpers as discussed at

https://lore.kernel.org/linux-btrfs/20190912235507.3DE794232AF@james.kirk.hungrycats.org/

David Sterba (2):
  btrfs: add 64bit safe helper for power of two checks
  btrfs: use has_single_bit_set for clarity

 fs/btrfs/misc.h         | 11 +++++++++++
 fs/btrfs/tree-checker.c | 14 +++++++-------
 fs/btrfs/volumes.c      |  7 +------
 3 files changed, 19 insertions(+), 13 deletions(-)

-- 
2.23.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] btrfs: add 64bit safe helper for power of two checks
  2019-10-01 17:52 [PATCH 0/2] Bit helpers cleanup David Sterba
@ 2019-10-01 17:52 ` David Sterba
  2019-10-01 17:52 ` [PATCH 2/2] btrfs: use has_single_bit_set for clarity David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2019-10-01 17:52 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

As is_power_of_two takes unsigned long, it's not safe on 32bit
architectures, but we could pass any u64 value in seveal places. Add a
separate helper and also an alias that better expresses the purpose for
which the helper is used.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/misc.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fs/btrfs/misc.h b/fs/btrfs/misc.h
index 7d564924dfeb..72bab64ecf60 100644
--- a/fs/btrfs/misc.h
+++ b/fs/btrfs/misc.h
@@ -47,4 +47,15 @@ static inline u64 div_factor_fine(u64 num, int factor)
 	return div_u64(num, 100);
 }
 
+/* Copy of is_power_of_two that is 64bit safe */
+static inline bool is_power_of_two_u64(u64 n)
+{
+	return n != 0 && (n & (n - 1)) == 0;
+}
+
+static inline bool has_single_bit_set(u64 n)
+{
+	return is_power_of_two_u64(n);
+}
+
 #endif
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] btrfs: use has_single_bit_set for clarity
  2019-10-01 17:52 [PATCH 0/2] Bit helpers cleanup David Sterba
  2019-10-01 17:52 ` [PATCH 1/2] btrfs: add 64bit safe helper for power of two checks David Sterba
@ 2019-10-01 17:52 ` David Sterba
  1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2019-10-01 17:52 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba

Replace is_power_of_2 with the helper that is self-documenting and
remove the open coded call in alloc_profile_is_valid.

Signed-off-by: David Sterba <dsterba@suse.com>
---
 fs/btrfs/tree-checker.c | 15 ++++++++-------
 fs/btrfs/volumes.c      |  7 +------
 2 files changed, 9 insertions(+), 13 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index f28f9725cef1..b8f82d9be9f0 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -23,6 +23,7 @@
 #include "disk-io.h"
 #include "compression.h"
 #include "volumes.h"
+#include "misc.h"
 
 /*
  * Error message should follow the following format:
@@ -630,7 +631,7 @@ int btrfs_check_chunk_valid(struct extent_buffer *leaf,
 		return -EUCLEAN;
 	}
 
-	if (!is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
+	if (!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
 	    (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
 		chunk_err(leaf, chunk, logical,
 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
@@ -814,11 +815,11 @@ static int check_inode_item(struct extent_buffer *leaf,
 	}
 
 	/*
-	 * S_IFMT is not bit mapped so we can't completely rely on is_power_of_2,
-	 * but is_power_of_2() can save us from checking FIFO/CHR/DIR/REG.
-	 * Only needs to check BLK, LNK and SOCKS
+	 * S_IFMT is not bit mapped so we can't completely rely on
+	 * is_power_of_2/has_single_bit_set, but it can save us from checking
+	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
 	 */
-	if (!is_power_of_2(mode & S_IFMT)) {
+	if (!has_single_bit_set(mode & S_IFMT)) {
 		if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
 			inode_item_err(fs_info, leaf, slot,
 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
@@ -1039,8 +1040,8 @@ static int check_extent_item(struct extent_buffer *leaf,
 			   btrfs_super_generation(fs_info->super_copy) + 1);
 		return -EUCLEAN;
 	}
-	if (!is_power_of_2(flags & (BTRFS_EXTENT_FLAG_DATA |
-				    BTRFS_EXTENT_FLAG_TREE_BLOCK))) {
+	if (!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
+					 BTRFS_EXTENT_FLAG_TREE_BLOCK))) {
 		extent_err(leaf, slot,
 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
 			flags, BTRFS_EXTENT_FLAG_DATA |
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index db13f7ae2c12..f0ff1c34eda1 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -3844,12 +3844,7 @@ static int alloc_profile_is_valid(u64 flags, int extended)
 	if (flags == 0)
 		return !extended; /* "0" is valid for usual profiles */
 
-	/* true if exactly one bit set */
-	/*
-	 * Don't use is_power_of_2(unsigned long) because it won't work
-	 * for the single profile (1ULL << 48) on 32-bit CPUs.
-	 */
-	return flags != 0 && (flags & (flags - 1)) == 0;
+	return has_single_bit_set(flags);
 }
 
 static inline int balance_need_close(struct btrfs_fs_info *fs_info)
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-10-01 17:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-01 17:52 [PATCH 0/2] Bit helpers cleanup David Sterba
2019-10-01 17:52 ` [PATCH 1/2] btrfs: add 64bit safe helper for power of two checks David Sterba
2019-10-01 17:52 ` [PATCH 2/2] btrfs: use has_single_bit_set for clarity David Sterba

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).