Rework file system group lookups

We want to check if the magic we got from statfs() is one of the magics listed
for one of the file systems in the given group. To do this, we'd iteratate over
the file system names, convert each name to an array of magics, and compare
those to the one we got. We were using gperf-generated lookup table for this,
so the string lookups were quick, but still this seems unnecessarily complex.
Let's just generate a simple lookup function, because we can:

$ src/basic/filesystem-sets.py fs-in-group
bool fs_in_group(const struct statfs *st, FilesystemGroups fs_group) {
        switch (fs_group) {
        case FILESYSTEM_SET_BASIC_API:
                return F_TYPE_EQUAL(st->f_type, CGROUP2_SUPER_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, CGROUP_SUPER_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, DEVPTS_SUPER_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, MQUEUE_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, PROC_SUPER_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, SYSFS_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, TMPFS_MAGIC);
        case FILESYSTEM_SET_ANONYMOUS:
                return F_TYPE_EQUAL(st->f_type, ANON_INODE_FS_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, PIPEFS_MAGIC)
                    || F_TYPE_EQUAL(st->f_type, SOCKFS_MAGIC);
...

We flatten the nested lookup of group=>fs=>magic into a single level.
The compiler can work its magic here to make the lookup quick.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2025-07-27 13:14:05 +02:00
parent b0123576f4
commit aca4353ab2
3 changed files with 21 additions and 21 deletions
+19
View File
@@ -297,6 +297,23 @@ const char* fs_type_to_string(statfs_f_type_t magic) {
return NULL;
}""")
def generate_fs_in_group():
print('bool fs_in_group(const struct statfs *st, FilesystemGroups fs_group) {')
print(' switch (fs_group) {')
for name, _, *filesystems in FILESYSTEM_SETS:
magics = sorted(set(sum((NAME_TO_MAGIC[fs] for fs in filesystems),
start=[])))
enum = 'FILESYSTEM_SET_' + name[1:].upper().replace('-', '_')
print(f' case {enum}:')
opts = '\n || '.join(f'F_TYPE_EQUAL(st->f_type, {magic})'
for magic in magics)
print(f' return {opts};')
print(' default: assert_not_reached();')
print(' }')
print('}')
def generate_filesystem_sets():
print('const FilesystemSet filesystem_sets[_FILESYSTEM_SET_MAX] = {')
@@ -346,6 +363,8 @@ if __name__ == '__main__':
generate_fs_type_to_string()
elif arg == 'filesystem-sets':
generate_filesystem_sets()
elif arg == 'fs-in-group':
generate_fs_in_group()
elif arg == 'check':
check()
else:
-20
View File
@@ -19,26 +19,6 @@ int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
return 0;
}
bool fs_in_group(const struct statfs *s, FilesystemGroups fs_group) {
int r;
NULSTR_FOREACH(fs, filesystem_sets[fs_group].value) {
const statfs_f_type_t *magic;
r = fs_type_from_string(fs, &magic);
if (r >= 0)
for (size_t i = 0; i < FILESYSTEM_MAGIC_MAX; i++) {
if (magic[i] == 0)
break;
if (is_fs_type(s, magic[i]))
return true;
}
}
return false;
}
const FilesystemSet* filesystem_set_find(const char *name) {
if (isempty(name) || name[0] != '@')
return NULL;
+2 -1
View File
@@ -173,7 +173,8 @@ filesystem_sets_c = custom_target(
output : 'filesystem-sets.c',
command : [filesystem_sets_py,
'fs-type-to-string',
'filesystem-sets'],
'filesystem-sets',
'fs-in-group'],
capture : true)
filesystems_gperf_gperf = custom_target(