btrfs: re-define btrfs_raid_types
v3 cover btrfs: re-define btrfs_raid_types
[CHANGELOG]
Changelog:
v2:
- Fix the start value of BTRFS_RAID_RAID0
v3:
- Introduce more static sanity checks
They are kinda overkilled, but they are only compile time checks, it should be fine.
- Keep btrfs_bg_flags_to_raid_index() as regular function
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
- Extra static_assert()s to make sure RAID0 is always the least significant bit in PROFILE_MASK
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 | 24 ++++++---------------- fs/btrfs/volumes.h | 35 +++++++++++++++++++++++++++++++++ include/uapi/linux/btrfs_tree.h | 13 ------------ 4 files changed, 43 insertions(+), 31 deletions(-)
Signature · 1 line
2.35.1
PATCH v3 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, in fact this btrfs_raid_types is diverted from the on-disk format values.
Furthermore, since it's internal structure, its definition can change in the future.
--- fs/btrfs/space-info.h | 2 ++ fs/btrfs/volumes.h | 13 +++++++++++++ include/uapi/linux/btrfs_tree.h | 13 ------------- 3 files changed, 15 insertions(+), 13 deletions(-)
fs/btrfs/space-info.h +2 -0
@@ -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.h +13 -0
@@ -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
@@ -880,19 +880,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.35.1
Re: [PATCH v3 1/2] btrfs: move definition of btrfs_raid_types to volumes.h
Looks good,
PATCH v3 2/2 btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
In reply to Qu WenruoIn 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.
This calculation has an anchor point, the lowest PROFILE bit, which is RAID0.
Even it's fixed on-disk format and should never change, here I added extra compile time checks to make it super safe:
1. Make sure RAID0 is always the lowest bit in PROFILE_MASK This is done by finding the first (least significant) bit set of RAID0 and PROFILE_MASK & ~RAID0.
2. Make sure RAID0 bit set beyond the highest bit of TYPE_MASK
--- fs/btrfs/volumes.c | 24 ++++++------------------ fs/btrfs/volumes.h | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 27 deletions(-)
fs/btrfs/volumes.c +6 -18
@@ -164,24 +164,12 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {*/ 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 */ +u64 profile = flags &BTRFS_BLOCK_GROUP_PROFILE_MASK ; + +if (!profile) +return BTRFS_RAID_SINGLE ; + +return BTRFS_BG_FLAG_TO_INDEX (profile); }const char *btrfs_bg_type_to_raid_name (u64 flags)
fs/btrfs/volumes.h +31 -9
@@ -17,16 +17,38 @@ extern struct mutex uuid_mutex;#define BTRFS_STRIPE_LEN SZ_64K +/* Used by sanity check for btrfs_raid_types. */ +#define const_ffs (n) (__builtin_ctzll(n) + 1) + +/* +* The conversion from BTRFS_BLOCK_GROUP_* bits to btrfs_raid_type requires +* RAID0 always to be the lowest profile bit. +* Although it's part of on-disk format and should never change, just do extra +* compile time sanity check on it. +*/ +static_assert (const_ffs (BTRFS_BLOCK_GROUP_RAID0 ) < +const_ffs (BTRFS_BLOCK_GROUP_PROFILE_MASK & + ~BTRFS_BLOCK_GROUP_RAID0 )); +static_assert (const_ilog2 (BTRFS_BLOCK_GROUP_RAID0 ) > +ilog2 (BTRFS_BLOCK_GROUP_TYPE_MASK )); + +/* ilog2() can handle both constants and variables */ +#define BTRFS_BG_FLAG_TO_INDEX (profile) \ + ilog2((profile) >> (ilog2(BTRFS_BLOCK_GROUP_RAID0) - 1)) +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 , +/* SINGLE is the special one as it doesn't have on-disk bit. */ +BTRFS_RAID_SINGLE =0 , + +BTRFS_RAID_RAID0 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID0 ), +BTRFS_RAID_RAID1 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID1 ), +BTRFS_RAID_DUP =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_DUP ), +BTRFS_RAID_RAID10 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID10 ), +BTRFS_RAID_RAID5 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID5 ), +BTRFS_RAID_RAID6 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID6 ), +BTRFS_RAID_RAID1C3 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID1C3 ), +BTRFS_RAID_RAID1C4 =BTRFS_BG_FLAG_TO_INDEX (BTRFS_BLOCK_GROUP_RAID1C4 ), +BTRFS_NR_RAID_TYPES };
Signature · 1 line
2.35.1
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 12 lines · 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 insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.This calculation has an anchor point, the lowest PROFILE bit, which is RAID0.
Quoted 8 lines
Even it's fixed on-disk format and should never change, here I added extra compile time checks to make it super safe:
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASK
TBH I think this change obscures the code more than it improves it.
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 16 lines · Johannes Thumshirn
Quoted 15 lines · 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 insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.This calculation has an anchor point, the lowest PROFILE bit, which is RAID0.
Even it's fixed on-disk format and should never change, here I added extra compile time checks to make it super safe:
Quoted 8 lines
Quoted 5 lines
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASKTBH I think this change obscures the code more than it improves it.
Right, that kinda makes sense.
Will update the patchset to remove that line if needed.
Thanks,
Qu
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 23 lines · Qu Wenruo
Quoted 20 lines · Johannes Thumshirn
Quoted 19 lines · 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 insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.This calculation has an anchor point, the lowest PROFILE bit, which is RAID0.
Even it's fixed on-disk format and should never change, here I added extra compile time checks to make it super safe:
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASKTBH I think this change obscures the code more than it improves it.
Right, that kinda makes sense.
Will update the patchset to remove that line if needed.
I think the whole patch makes the code harder to follow. As of now you can just look it up, now you have to look how the calculation is done etc.
If you want to get rid of the branches (which I still don't see a reason for) have you considered creating a lookup table?
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 26 lines · Johannes Thumshirn
Quoted 25 lines · Qu Wenruo
Quoted 22 lines · Johannes Thumshirn
Quoted 20 lines · 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 insideBTRFS_BLOCK_GROUP_PROFILES_MASK, we can easily useilog2()to calculate their values.This calculation has an anchor point, the lowest PROFILE bit, which is RAID0.
Even it's fixed on-disk format and should never change, here I added extra compile time checks to make it super safe:
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASK
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
TBH I think this change obscures the code more than it improves it.
Right, that kinda makes sense.
Will update the patchset to remove that line if needed.
I think the whole patch makes the code harder to follow. As of now you can just look it up, now you have to look how the calculation is done etc.
The problem is, why you need to look the index up?
The index is pretty straight forward, just a enum for each profile, one should not really bother whatever the value is.
Quoted 2 lines
If you want to get rid of the branches (which I still don't see a reason for)
The new code is just 5 asm commands on x86_64.
Quoted 1 line
have you considered creating a lookup table?
The index is used for the lookup table of btrfs_raid_type.
Thanks,
Qu
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
In reply to Johannes ThumshirnQuoted 7 lines · Johannes Thumshirn
Quoted 6 lines · Qu Wenruo
Quoted 6 lines
Quoted 5 lines
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASK
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
TBH I think this change obscures the code more than it improves it.
Right, that kinda makes sense.
Will update the patchset to remove that line if needed.
I think the whole patch makes the code harder to follow. As of now you can just look it up, now you have to look how the calculation is done etc.
I think the index is the least useful information about the profiles, it's there just that we have a sequence representing the profile flags that's usable for arrays, like the space infos. The on-disk definition is the bit and that's the source, how exactly it's converted to the index is IMHO just a detail.
Quoted 2 lines
If you want to get rid of the branches (which I still don't see a reason for) have you considered creating a lookup table?
It's yet another place that keeps the mapping of the values open coded.
Possibly if we would want to take it farther, a single definition of the index enums could be like
#define DEFINE_RAID_INDEX(profile) \
BTRFS_RAID_##profile = BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_##profile)
and then used like
enum raid_types {
DEFINE_RAID_INDEX(RAID0),
DEFINE_RAID_INDEX(RAID10),
...
};But that's obscuring what exactly does it define and we do use the plain indexes like BTRFS_RAID_DUP in several places. It's sometimes annoying that ctags don't locate all the setget helpers because of all the BTRFS_SETGET_FUNCS macros.
Re: [PATCH v3 2/2] btrfs: use ilog2() to replace if () branches for btrfs_bg_flags_to_raid_index()
Quoted 42 lines · David Sterba
Quoted 7 lines · Johannes Thumshirn
Quoted 6 lines · Qu Wenruo
Quoted 6 lines
Quoted 5 lines
1. Make sure RAID0 is always the lowest bit in
PROFILE_MASKThis is done by finding the first (least significant) bit set of RAID0 andPROFILE_MASK& ~RAID0.2. Make sure RAID0 bit set beyond the highest bit of
TYPE_MASKQuoted 8 lines
Quoted 5 lines
Quoted 2 lines
TBH I think this change obscures the code more than it improves it.
Right, that kinda makes sense.
Will update the patchset to remove that line if needed.
I think the whole patch makes the code harder to follow. As of now you can just look it up, now you have to look how the calculation is done etc.
I think the index is the least useful information about the profiles, it's there just that we have a sequence representing the profile flags that's usable for arrays, like the space infos. The on-disk definition is the bit and that's the source, how exactly it's converted to the index is IMHO just a detail.
Quoted 2 lines
If you want to get rid of the branches (which I still don't see a reason for) have you considered creating a lookup table?
It's yet another place that keeps the mapping of the values open coded.
Possibly if we would want to take it farther, a single definition of the index enums could be like
#define
DEFINE_RAID_INDEX(profile)\
BTRFS_RAID_##profile =BTRFS_BG_FLAG_TO_INDEX(BTRFS_BLOCK_GROUP_##profile)and then used like
enum raid_types { DEFINE_RAID_INDEX(RAID0), DEFINE_RAID_INDEX(RAID10), ... };
Quoted 4 lines
But that's obscuring what exactly does it define and we do use the plain indexes like
BTRFS_RAID_DUPin several places. It's sometimes annoying that ctags don't locate all the setget helpers because of all theBTRFS_SETGET_FUNCSmacros.
Ctags are really out-of-date now.
LSP is the future, just try clangd, handling macros is just a piece of cake.
Thanks,
Qu
Quoted 30 lines · Qu Wenruo
[CHANGELOG]
Changelog:
v2:
- Fix the start value ofBTRFS_RAID_RAID0v3:
- Introduce more static sanity checks
They are kinda overkilled, but they are only compile time checks, it should be fine.- Keep
btrfs_bg_flags_to_raid_index()as regular functionBy 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 intoffs()assembly code.But we're using a lot of if () branches to do the convert.
This patch will re-define
btrfs_raid_typesby:- Move it to
volumes.h
btrfs_raid_typesare 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
Quoted 8 lines
- Extra static_assert()s to make sure RAID0 is always the least significant bit in
PROFILE_MASKQu Wenruo (2):
btrfs: move definition ofbtrfs_raid_typestovolumes.h
btrfs: useilog2()to replace if () branches for
btrfs_bg_flags_to_raid_index()
I've added the patches to for-next as it looks good as it is now, feel free to send an update, either an incremental fixup or full resend.