1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "filesystems-gperf.h"
4 #include "stat-util.h"
5 
fs_type_to_string(statfs_f_type_t magic)6 const char *fs_type_to_string(statfs_f_type_t magic) {
7 
8         switch (magic) {
9 #include "filesystem-switch-case.h"
10         }
11 
12         return NULL;
13 }
14 
15 
fs_type_from_string(const char * name,const statfs_f_type_t ** ret)16 int fs_type_from_string(const char *name, const statfs_f_type_t **ret) {
17         const struct FilesystemMagic *fs_magic;
18 
19         assert(name);
20         assert(ret);
21 
22         fs_magic = filesystems_gperf_lookup(name, strlen(name));
23         if (!fs_magic)
24                 return -EINVAL;
25 
26         *ret = fs_magic->magic;
27         return 0;
28 }
29 
fs_in_group(const struct statfs * s,FilesystemGroups fs_group)30 int fs_in_group(const struct statfs *s, FilesystemGroups fs_group) {
31         const char *fs;
32         int r;
33 
34         NULSTR_FOREACH(fs, filesystem_sets[fs_group].value) {
35                 const statfs_f_type_t *magic;
36 
37                 r = fs_type_from_string(fs, &magic);
38                 if (r == 0) {
39                         for (size_t i = 0; i < FILESYSTEM_MAGIC_MAX; i++) {
40                                 if (magic[i] == 0)
41                                         break;
42 
43                                 if (is_fs_type(s, magic[i]))
44                                         return true;
45                         }
46                 }
47         }
48 
49         return false;
50 }
51 
52 const FilesystemSet filesystem_sets[_FILESYSTEM_SET_MAX] = {
53         [FILESYSTEM_SET_BASIC_API] = {
54                 .name = "@basic-api",
55                 .help = "Basic filesystem API",
56                 .value =
57                 "cgroup\0"
58                 "cgroup2\0"
59                 "devpts\0"
60                 "devtmpfs\0"
61                 "mqueue\0"
62                 "proc\0"
63                 "sysfs\0"
64         },
65         [FILESYSTEM_SET_ANONYMOUS] = {
66                 .name = "@anonymous",
67                 .help = "Anonymous inodes",
68                 .value =
69                 "anon_inodefs\0"
70                 "pipefs\0"
71                 "sockfs\0"
72         },
73         [FILESYSTEM_SET_APPLICATION] = {
74                 .name = "@application",
75                 .help = "Application virtual filesystems",
76                 .value =
77                 "autofs\0"
78                 "fuse\0"
79                 "overlay\0"
80         },
81         [FILESYSTEM_SET_AUXILIARY_API] = {
82                 .name = "@auxiliary-api",
83                 .help = "Auxiliary filesystem API",
84                 .value =
85                 "binfmt_misc\0"
86                 "configfs\0"
87                 "efivarfs\0"
88                 "fusectl\0"
89                 "hugetlbfs\0"
90                 "rpc_pipefs\0"
91                 "securityfs\0"
92         },
93         [FILESYSTEM_SET_COMMON_BLOCK] = {
94                 .name = "@common-block",
95                 .help = "Common block device filesystems",
96                 .value =
97                 "btrfs\0"
98                 "erofs\0"
99                 "exfat\0"
100                 "ext4\0"
101                 "f2fs\0"
102                 "iso9660\0"
103                 "ntfs3\0"
104                 "squashfs\0"
105                 "udf\0"
106                 "vfat\0"
107                 "xfs\0"
108         },
109         [FILESYSTEM_SET_HISTORICAL_BLOCK] = {
110                 .name = "@historical-block",
111                 .help = "Historical block device filesystems",
112                 .value =
113                 "ext2\0"
114                 "ext3\0"
115                 "minix\0"
116         },
117         [FILESYSTEM_SET_NETWORK] = {
118                 .name = "@network",
119                 .help = "Well-known network filesystems",
120                 .value =
121                 "afs\0"
122                 "ceph\0"
123                 "cifs\0"
124                 "gfs\0"
125                 "gfs2\0"
126                 "ncp\0"
127                 "ncpfs\0"
128                 "nfs\0"
129                 "nfs4\0"
130                 "ocfs2\0"
131                 "orangefs\0"
132                 "pvfs2\0"
133                 "smb3\0"
134                 "smbfs\0"
135         },
136         [FILESYSTEM_SET_PRIVILEGED_API] = {
137                 .name = "@privileged-api",
138                 .help = "Privileged filesystem API",
139                 .value =
140                 "bpf\0"
141                 "debugfs\0"
142                 "pstore\0"
143                 "tracefs\0"
144         },
145         [FILESYSTEM_SET_SECURITY] = {
146                 .name = "@security",
147                 .help = "Security/MAC API VFS",
148                 .value =
149                 "apparmorfs\0"
150                 "selinuxfs\0"
151                 "smackfs\0"
152         },
153         [FILESYSTEM_SET_TEMPORARY] = {
154                 .name = "@temporary",
155                 .help = "Temporary filesystems",
156                 .value =
157                 "ramfs\0"
158                 "tmpfs\0"
159         },
160         [FILESYSTEM_SET_KNOWN] = {
161                 .name = "@known",
162                 .help = "All known filesystems declared in the kernel",
163                 .value =
164 #include "filesystem-list.h"
165         },
166 };
167 
filesystem_set_find(const char * name)168 const FilesystemSet *filesystem_set_find(const char *name) {
169         if (isempty(name) || name[0] != '@')
170                 return NULL;
171 
172         for (FilesystemGroups i = 0; i < _FILESYSTEM_SET_MAX; i++)
173                 if (streq(filesystem_sets[i].name, name))
174                         return filesystem_sets + i;
175 
176         return NULL;
177 }
178