Skip to contentSkip to conversation

btrfs: re-define btrfs_raid_types

PATCH Superseded Qu Wenruo 1 participant 3 messages Oct 27, 2021 Btrfs file system

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: re-define btrfs_raid_types

By the nature of BTRFS_BLOCK_GROUP_* profiles, converting the flag into an index should only need one bits AND, one if () check for SINGLE profile, one right shift to align the values, one ilog2() call which is normally converted into ffs() assembly code.

But we're using a lot of if () branches to do the convert.

This patch will re-define btrfs_raid_types by:

- Move it to volumes.h
btrfs_raid_types are only used internally, no need to be exposed through UAPI.

- Re-order btrfs_raid_types
To make them match their value order

- Use ilog2() to convert them into index

- Inline btrfs_bg_flags_to_raid_index()
It's just 5 assembly commands now.

Qu Wenruo (2):
btrfs: move definition of btrfs_raid_types to volumes.h
btrfs: use ilog2() to replace if () branches for
btrfs_bg_flags_to_raid_index()

 fs/btrfs/space-info.h           |  2 ++
 fs/btrfs/volumes.c              | 26 -----------------------
 fs/btrfs/volumes.h              | 37 ++++++++++++++++++++++++++++++++-
 include/uapi/linux/btrfs_tree.h | 13 ------------
 4 files changed, 38 insertions(+), 40 deletions(-)
Signature · 1 line
2.33.1
author
#1 lore raw

PATCH 1/2 btrfs: move definition of btrfs_raid_types to volumes.h

It's only internally used as another way to represent btrfs profiles, it's not exposed through any on-disk format.

Furthermore, since it's internal structure, its definition can change in the future.

---
 fs/btrfs/volumes.h              | 13 +++++++++++++
 include/uapi/linux/btrfs_tree.h | 13 -------------
 2 files changed, 13 insertions(+), 13 deletions(-)
fs/btrfs/volumes.h +13 -0
index 3b8130680749..e0c374a7c30b 100644
@@ -17,6 +17,19 @@ extern struct mutex uuid_mutex;
 
 #define BTRFS_STRIPE_LEN	SZ_64K
 
+enum btrfs_raid_types {
+	BTRFS_RAID_RAID10,
+	BTRFS_RAID_RAID1,
+	BTRFS_RAID_DUP,
+	BTRFS_RAID_RAID0,
+	BTRFS_RAID_SINGLE,
+	BTRFS_RAID_RAID5,
+	BTRFS_RAID_RAID6,
+	BTRFS_RAID_RAID1C3,
+	BTRFS_RAID_RAID1C4,
+	BTRFS_NR_RAID_TYPES
+};
+
 struct btrfs_io_geometry {
 	/* remaining bytes before crossing a stripe */
 	u64 len;
include/uapi/linux/btrfs_tree.h +0 -13
index e1c4c732aaba..819dec72f232 100644
@@ -875,19 +875,6 @@ struct btrfs_dev_replace_item {
 #define BTRFS_BLOCK_GROUP_RESERVED	(BTRFS_AVAIL_ALLOC_BIT_SINGLE | \
 					 BTRFS_SPACE_INFO_GLOBAL_RSV)
 
-enum btrfs_raid_types {
-	BTRFS_RAID_RAID10,
-	BTRFS_RAID_RAID1,
-	BTRFS_RAID_DUP,
-	BTRFS_RAID_RAID0,
-	BTRFS_RAID_SINGLE,
-	BTRFS_RAID_RAID5,
-	BTRFS_RAID_RAID6,
-	BTRFS_RAID_RAID1C3,
-	BTRFS_RAID_RAID1C4,
-	BTRFS_NR_RAID_TYPES
-};
-
 #define BTRFS_BLOCK_GROUP_TYPE_MASK	(BTRFS_BLOCK_GROUP_DATA |    \
 					 BTRFS_BLOCK_GROUP_SYSTEM |  \
 					 BTRFS_BLOCK_GROUP_METADATA)
Signature · 1 line
2.33.1
author
#2 lore raw

PATCH 2/2 btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()

In reply to Qu Wenruo

In function btrfs_bg_flags_to_raid_index(), we use quite some if () to convert the BTRFS_BLOCK_GROUP_* bits to a index number.

But the truth is, there is really no such need for so many branches at all.
Since all BTRFS_BLOCK_GROUP_* flags are just one single bit set inside BTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily use ilog2() to calculate their values.

Only one fixed offset is needed to make the index sequential (the lowest profile bit starts at ilog2(1 << 3) while we have 0 reserved for SINGLE).

Even with that calculation involved (one if(), one ilog2(), one minus), it should still be way faster than the if () branches, and now it is definitely small enough to be inlined.

---
 fs/btrfs/space-info.h |  2 ++
 fs/btrfs/volumes.c    | 26 --------------------------
 fs/btrfs/volumes.h    | 42 ++++++++++++++++++++++++++++++++----------
 3 files changed, 34 insertions(+), 36 deletions(-)
fs/btrfs/space-info.h +2 -0
index cb5056472e79..5a0686ab9679 100644
@@ -3,6 +3,8 @@
 #ifndef BTRFS_SPACE_INFO_H
 #define BTRFS_SPACE_INFO_H
 
+#include "volumes.h"
+
 struct btrfs_space_info {
 	spinlock_t lock;
 
fs/btrfs/volumes.c +0 -26
index a8ea3f88c4db..94a3dfe709e8 100644
@@ -154,32 +154,6 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
 	},
 };
 
-/*
- * Convert block group flags (BTRFS_BLOCK_GROUP_*) to btrfs_raid_types, which
- * can be used as index to access btrfs_raid_array[].
- */
-enum btrfs_raid_types __attribute_const__ btrfs_bg_flags_to_raid_index(u64 flags)
-{
-	if (flags & BTRFS_BLOCK_GROUP_RAID10)
-		return BTRFS_RAID_RAID10;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID1)
-		return BTRFS_RAID_RAID1;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID1C3)
-		return BTRFS_RAID_RAID1C3;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID1C4)
-		return BTRFS_RAID_RAID1C4;
-	else if (flags & BTRFS_BLOCK_GROUP_DUP)
-		return BTRFS_RAID_DUP;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID0)
-		return BTRFS_RAID_RAID0;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID5)
-		return BTRFS_RAID_RAID5;
-	else if (flags & BTRFS_BLOCK_GROUP_RAID6)
-		return BTRFS_RAID_RAID6;
-
-	return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
-}
-
 const char *btrfs_bg_type_to_raid_name(u64 flags)
 {
 	const int index = btrfs_bg_flags_to_raid_index(flags);
fs/btrfs/volumes.h +32 -10

58 lines not on this page · raw message

Signature · 1 line
2.33.1