Commit 34264ae3 authored by Al Viro's avatar Al Viro

don't bother with explicit length argument for __lookup_constant()

Have the arrays of constant_table self-terminated (by NULL ->name
in the final entry).  Simplifies lookup_constant() and allows to
reuse the search for enum params as well.
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 5eede625
...@@ -45,6 +45,7 @@ static const struct constant_table common_set_sb_flag[] = { ...@@ -45,6 +45,7 @@ static const struct constant_table common_set_sb_flag[] = {
{ "posixacl", SB_POSIXACL }, { "posixacl", SB_POSIXACL },
{ "ro", SB_RDONLY }, { "ro", SB_RDONLY },
{ "sync", SB_SYNCHRONOUS }, { "sync", SB_SYNCHRONOUS },
{ },
}; };
static const struct constant_table common_clear_sb_flag[] = { static const struct constant_table common_clear_sb_flag[] = {
...@@ -53,6 +54,7 @@ static const struct constant_table common_clear_sb_flag[] = { ...@@ -53,6 +54,7 @@ static const struct constant_table common_clear_sb_flag[] = {
{ "nomand", SB_MANDLOCK }, { "nomand", SB_MANDLOCK },
{ "rw", SB_RDONLY }, { "rw", SB_RDONLY },
{ "silent", SB_SILENT }, { "silent", SB_SILENT },
{ },
}; };
static const char *const forbidden_sb_flag[] = { static const char *const forbidden_sb_flag[] = {
......
...@@ -20,27 +20,31 @@ static const struct constant_table bool_names[] = { ...@@ -20,27 +20,31 @@ static const struct constant_table bool_names[] = {
{ "no", false }, { "no", false },
{ "true", true }, { "true", true },
{ "yes", true }, { "yes", true },
{ },
}; };
static const struct constant_table *
__lookup_constant(const struct constant_table *tbl, const char *name)
{
for ( ; tbl->name; tbl++)
if (strcmp(name, tbl->name) == 0)
return tbl;
return NULL;
}
/** /**
* lookup_constant - Look up a constant by name in an ordered table * lookup_constant - Look up a constant by name in an ordered table
* @tbl: The table of constants to search. * @tbl: The table of constants to search.
* @tbl_size: The size of the table.
* @name: The name to look up. * @name: The name to look up.
* @not_found: The value to return if the name is not found. * @not_found: The value to return if the name is not found.
*/ */
int __lookup_constant(const struct constant_table *tbl, size_t tbl_size, int lookup_constant(const struct constant_table *tbl, const char *name, int not_found)
const char *name, int not_found)
{ {
unsigned int i; const struct constant_table *p = __lookup_constant(tbl, name);
for (i = 0; i < tbl_size; i++) return p ? p->value : not_found;
if (strcmp(name, tbl[i].name) == 0)
return tbl[i].value;
return not_found;
} }
EXPORT_SYMBOL(__lookup_constant); EXPORT_SYMBOL(lookup_constant);
static const struct fs_parameter_spec *fs_lookup_key( static const struct fs_parameter_spec *fs_lookup_key(
const struct fs_parameter_description *desc, const struct fs_parameter_description *desc,
...@@ -189,11 +193,10 @@ int fs_parse(struct fs_context *fc, ...@@ -189,11 +193,10 @@ int fs_parse(struct fs_context *fc,
goto maybe_okay; goto maybe_okay;
case fs_param_is_enum: case fs_param_is_enum:
for (e = p->data; e->name; e++) { e = __lookup_constant(p->data, param->string);
if (strcmp(e->name, param->string) == 0) { if (e) {
result->uint_32 = e->value; result->uint_32 = e->value;
goto okay; goto okay;
}
} }
goto bad_value; goto bad_value;
......
...@@ -83,9 +83,7 @@ extern int fs_lookup_param(struct fs_context *fc, ...@@ -83,9 +83,7 @@ extern int fs_lookup_param(struct fs_context *fc,
bool want_bdev, bool want_bdev,
struct path *_path); struct path *_path);
extern int __lookup_constant(const struct constant_table tbl[], size_t tbl_size, extern int lookup_constant(const struct constant_table tbl[], const char *name, int not_found);
const char *name, int not_found);
#define lookup_constant(t, n, nf) __lookup_constant(t, ARRAY_SIZE(t), (n), (nf))
#ifdef CONFIG_VALIDATE_FS_PARSER #ifdef CONFIG_VALIDATE_FS_PARSER
extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size, extern bool validate_constant_table(const struct constant_table *tbl, size_t tbl_size,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment