From 31df7dc295a520a9eaa4c98a5b5c20fc05360fce Mon Sep 17 00:00:00 2001 From: David Sterba Date: Mon, 6 Sep 2021 15:57:27 +0200 Subject: [PATCH] btrfs-progs: factor out profile parsing to common utils There are some duplicate parsers of the profile names, factor out the one from balance to the common code. Signed-off-by: David Sterba --- cmds/balance.c | 24 +++++++++++------------- common/parse-utils.c | 19 +++++++++++++++++++ common/parse-utils.h | 1 + 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/cmds/balance.c b/cmds/balance.c index 073a85f7..7abc69d9 100644 --- a/cmds/balance.c +++ b/cmds/balance.c @@ -32,6 +32,7 @@ #include "common/open-utils.h" #include "cmds/commands.h" #include "common/utils.h" +#include "common/parse-utils.h" #include "common/help.h" static const char * const balance_cmd_group_usage[] = { @@ -42,21 +43,18 @@ static const char * const balance_cmd_group_usage[] = { static int parse_one_profile(const char *profile, u64 *flags) { - int i; + int ret; + u64 tmp = 0; - for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { - if (strcasecmp(btrfs_raid_array[i].raid_name, profile) == 0) { - u64 tmp; - - tmp = btrfs_raid_array[i].bg_flag; - if (tmp == 0) - tmp = BTRFS_AVAIL_ALLOC_BIT_SINGLE; - *flags |= tmp; - return 0; - } + ret = parse_bg_profile(profile, &tmp); + if (ret) { + error("unknown profile: %s", profile); + return 1; } - error("unknown profile: %s", profile); - return 1; + if (tmp == 0) + tmp = BTRFS_AVAIL_ALLOC_BIT_SINGLE; + *flags |= tmp; + return ret; } static int parse_profiles(char *profiles, u64 *flags) diff --git a/common/parse-utils.c b/common/parse-utils.c index adefd006..53215833 100644 --- a/common/parse-utils.c +++ b/common/parse-utils.c @@ -19,6 +19,7 @@ #include #include "common/parse-utils.h" #include "common/messages.h" +#include "kernel-shared/volumes.h" int parse_u64(const char *str, u64 *result) { @@ -241,3 +242,21 @@ int fls64(u64 x) return 64 - i; } +/* + * Parse string description of block group profile and set that bit in @flags. + * Return 1 if the profile is not valid, otherwise 0. + * + * String matched against btrfs_raid_array, case insensitive. + */ +int parse_bg_profile(const char *profile, u64 *flags) +{ + int i; + + for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { + if (strcasecmp(btrfs_raid_array[i].raid_name, profile) == 0) { + *flags |= btrfs_raid_array[i].bg_flag; + return 0; + } + } + return 1; +} diff --git a/common/parse-utils.h b/common/parse-utils.h index 0eecebf2..ebfaa413 100644 --- a/common/parse-utils.h +++ b/common/parse-utils.h @@ -27,6 +27,7 @@ int parse_u64(const char *str, u64 *result); int parse_range_u32(const char *range, u32 *start, u32 *end); int parse_range(const char *range, u64 *start, u64 *end); int parse_range_strict(const char *range, u64 *start, u64 *end); +int parse_bg_profile(const char *profile, u64 *flags); int fls64(u64 x); #endif