btrfs-progs: move parse_qgroupid to parse utils

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2021-09-07 13:56:33 +02:00
parent cb5a542871
commit 47b04b747d
3 changed files with 33 additions and 31 deletions

View File

@ -276,3 +276,34 @@ int parse_bg_profile(const char *profile, u64 *flags)
}
return 1;
}
/*
* Parse qgroupid of format LEVEL/ID, level and id are numerical, nothing must
* follow after the last character of ID.
*/
int parse_qgroupid(const char *str, u64 *qgroupid)
{
char *end = NULL;
u64 level;
u64 id;
level = strtoull(str, &end, 10);
if (str == end)
return -EINVAL;
if (end[0] != '/')
return -EINVAL;
str = end + 1;
end = NULL;
id = strtoull(str, &end, 10);
if (str == end)
return -EINVAL;
if (end[0])
return -EINVAL;
if (id >= (1ULL << BTRFS_QGROUP_LEVEL_SHIFT))
return -ERANGE;
if (level >= (1ULL << (64 - BTRFS_QGROUP_LEVEL_SHIFT)))
return -ERANGE;
*qgroupid = (level << BTRFS_QGROUP_LEVEL_SHIFT) | id;
return 0;
}

View File

@ -29,6 +29,7 @@ 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 parse_compress_type(const char *type);
int parse_qgroupid(const char *str, u64 *qgroupid);
int fls64(u64 x);
#endif

View File

@ -48,6 +48,7 @@
#include "common/utils.h"
#include "common/path-utils.h"
#include "common/device-scan.h"
#include "common/parse-utils.h"
#include "kernel-shared/volumes.h"
#include "ioctl.h"
#include "cmds/commands.h"
@ -227,37 +228,6 @@ int set_label(const char *btrfs_dev, const char *label)
return ret;
}
/*
* Parse qgroupid of format LEVEL/ID, level and id are numerical, nothing must
* follow after the last character of ID.
*/
int parse_qgroupid(const char *str, u64 *qgroupid)
{
char *end = NULL;
u64 level;
u64 id;
level = strtoull(str, &end, 10);
if (str == end)
return -EINVAL;
if (end[0] != '/')
return -EINVAL;
str = end + 1;
end = NULL;
id = strtoull(str, &end, 10);
if (str == end)
return -EINVAL;
if (end[0])
return -EINVAL;
if (id >= (1ULL << BTRFS_QGROUP_LEVEL_SHIFT))
return -ERANGE;
if (level >= (1ULL << (64 - BTRFS_QGROUP_LEVEL_SHIFT)))
return -ERANGE;
*qgroupid = (level << BTRFS_QGROUP_LEVEL_SHIFT) | id;
return 0;
}
u64 parse_qgroupid_or_path(const char *p)
{
enum btrfs_util_error err;