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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2021-09-06 15:57:27 +02:00
parent dd3b931b1c
commit 31df7dc295
3 changed files with 31 additions and 13 deletions

View File

@ -32,6 +32,7 @@
#include "common/open-utils.h" #include "common/open-utils.h"
#include "cmds/commands.h" #include "cmds/commands.h"
#include "common/utils.h" #include "common/utils.h"
#include "common/parse-utils.h"
#include "common/help.h" #include "common/help.h"
static const char * const balance_cmd_group_usage[] = { 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) 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++) { ret = parse_bg_profile(profile, &tmp);
if (strcasecmp(btrfs_raid_array[i].raid_name, profile) == 0) { if (ret) {
u64 tmp; error("unknown profile: %s", profile);
return 1;
tmp = btrfs_raid_array[i].bg_flag;
if (tmp == 0)
tmp = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
*flags |= tmp;
return 0;
}
} }
error("unknown profile: %s", profile); if (tmp == 0)
return 1; tmp = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
*flags |= tmp;
return ret;
} }
static int parse_profiles(char *profiles, u64 *flags) static int parse_profiles(char *profiles, u64 *flags)

View File

@ -19,6 +19,7 @@
#include <ctype.h> #include <ctype.h>
#include "common/parse-utils.h" #include "common/parse-utils.h"
#include "common/messages.h" #include "common/messages.h"
#include "kernel-shared/volumes.h"
int parse_u64(const char *str, u64 *result) int parse_u64(const char *str, u64 *result)
{ {
@ -241,3 +242,21 @@ int fls64(u64 x)
return 64 - i; 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;
}

View File

@ -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_u32(const char *range, u32 *start, u32 *end);
int parse_range(const char *range, u64 *start, u64 *end); int parse_range(const char *range, u64 *start, u64 *end);
int parse_range_strict(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); int fls64(u64 x);
#endif #endif