| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import os |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
|
| 8 |
NAME_TO_MAGIC = { |
| 9 |
'apparmorfs': ['AAFS_MAGIC'], |
| 10 |
'adfs': ['ADFS_SUPER_MAGIC'], |
| 11 |
'affs': ['AFFS_SUPER_MAGIC'], |
| 12 |
'afs': ['AFS_FS_MAGIC', |
| 13 |
'AFS_SUPER_MAGIC'], |
| 14 |
'anon_inodefs': ['ANON_INODE_FS_MAGIC'], |
| 15 |
'autofs': ['AUTOFS_SUPER_MAGIC'], |
| 16 |
'balloon-kvm': ['BALLOON_KVM_MAGIC'], |
| 17 |
'bcachefs': ['BCACHEFS_SUPER_MAGIC'], |
| 18 |
'bdev': ['BDEVFS_MAGIC'], |
| 19 |
'binder': ['BINDERFS_SUPER_MAGIC'], |
| 20 |
'binfmt_misc': ['BINFMTFS_MAGIC'], |
| 21 |
'bpf': ['BPF_FS_MAGIC'], |
| 22 |
'btrfs': ['BTRFS_SUPER_MAGIC'], |
| 23 |
'btrfs_test_fs': ['BTRFS_TEST_MAGIC'], |
| 24 |
# cpuset's magic got reassigned to cgroupfs |
| 25 |
'cpuset': ['CGROUP_SUPER_MAGIC'], |
| 26 |
'ceph': ['CEPH_SUPER_MAGIC'], |
| 27 |
'cgroup2': ['CGROUP2_SUPER_MAGIC'], |
| 28 |
# note that the cgroupfs magic got reassigned from cpuset |
| 29 |
'cgroup': ['CGROUP_SUPER_MAGIC'], |
| 30 |
'cifs': ['CIFS_SUPER_MAGIC', |
| 31 |
'SMB2_SUPER_MAGIC'], |
| 32 |
'coda': ['CODA_SUPER_MAGIC'], |
| 33 |
'configfs': ['CONFIGFS_MAGIC'], |
| 34 |
'cramfs': ['CRAMFS_MAGIC'], |
| 35 |
'dax': ['DAXFS_MAGIC'], |
| 36 |
'debugfs': ['DEBUGFS_MAGIC'], |
| 37 |
'devmem': ['DEVMEM_MAGIC'], |
| 38 |
'devpts': ['DEVPTS_SUPER_MAGIC'], |
| 39 |
# devtmpfs is just a special instance of tmpfs, hence it reports its magic |
| 40 |
'devtmpfs': ['TMPFS_MAGIC'], |
| 41 |
'dmabuf': ['DMA_BUF_MAGIC'], |
| 42 |
'ecryptfs': ['ECRYPTFS_SUPER_MAGIC'], |
| 43 |
'efivarfs': ['EFIVARFS_MAGIC'], |
| 44 |
'efs': ['EFS_SUPER_MAGIC'], |
| 45 |
'erofs': ['EROFS_SUPER_MAGIC_V1'], |
| 46 |
# ext2 + ext3 + ext4 use the same magic |
| 47 |
'ext2': ['EXT2_SUPER_MAGIC'], |
| 48 |
'ext3': ['EXT3_SUPER_MAGIC'], |
| 49 |
'ext4': ['EXT4_SUPER_MAGIC'], |
| 50 |
'exfat': ['EXFAT_SUPER_MAGIC'], |
| 51 |
'f2fs': ['F2FS_SUPER_MAGIC'], |
| 52 |
# fuseblk is so closely related to fuse that it shares the same magic |
| 53 |
'fuseblk': ['FUSE_SUPER_MAGIC'], |
| 54 |
'fuse': ['FUSE_SUPER_MAGIC'], |
| 55 |
'fusectl': ['FUSE_CTL_SUPER_MAGIC'], |
| 56 |
# gfs is an old version of gfs2 and reuses the magic |
| 57 |
'gfs': ['GFS2_MAGIC'], |
| 58 |
'gfs2': ['GFS2_MAGIC'], |
| 59 |
'gmem': ['GUEST_MEMFD_MAGIC'], |
| 60 |
'hostfs': ['HOSTFS_SUPER_MAGIC'], |
| 61 |
'hpfs': ['HPFS_SUPER_MAGIC'], |
| 62 |
'hugetlbfs': ['HUGETLBFS_MAGIC'], |
| 63 |
'iso9660': ['ISOFS_SUPER_MAGIC'], |
| 64 |
'jffs2': ['JFFS2_SUPER_MAGIC'], |
| 65 |
'minix': ['MINIX_SUPER_MAGIC', |
| 66 |
'MINIX_SUPER_MAGIC2', |
| 67 |
'MINIX2_SUPER_MAGIC', |
| 68 |
'MINIX2_SUPER_MAGIC2', |
| 69 |
'MINIX3_SUPER_MAGIC'], |
| 70 |
'mqueue': ['MQUEUE_MAGIC'], |
| 71 |
# msdos is an older legacy version of vfat, shares the magic |
| 72 |
'msdos': ['MSDOS_SUPER_MAGIC'], |
| 73 |
# ncp/ncpfs have been removed from the kernel, but ncpfs was the official name |
| 74 |
'ncp': ['NCP_SUPER_MAGIC'], |
| 75 |
'ncpfs': ['NCP_SUPER_MAGIC'], |
| 76 |
# nfs is the old version of nfs4, and they share the same magic |
| 77 |
'nfs': ['NFS_SUPER_MAGIC'], |
| 78 |
'nfs4': ['NFS_SUPER_MAGIC'], |
| 79 |
'nilfs2': ['NILFS_SUPER_MAGIC'], |
| 80 |
'nsfs': ['NSFS_MAGIC'], |
| 81 |
'ntfs': ['NTFS_SB_MAGIC'], |
| 82 |
'ntfs3': ['NTFS3_SUPER_MAGIC'], |
| 83 |
'nullfs': ['NULL_FS_MAGIC'], |
| 84 |
'ocfs2': ['OCFS2_SUPER_MAGIC'], |
| 85 |
'openpromfs': ['OPENPROM_SUPER_MAGIC'], |
| 86 |
'orangefs': ['ORANGEFS_DEVREQ_MAGIC'], |
| 87 |
'overlay': ['OVERLAYFS_SUPER_MAGIC'], |
| 88 |
'pidfs': ['PID_FS_MAGIC'], |
| 89 |
'pipefs': ['PIPEFS_MAGIC'], |
| 90 |
'ppc-cmm': ['PPC_CMM_MAGIC'], |
| 91 |
'proc': ['PROC_SUPER_MAGIC'], |
| 92 |
'pstore': ['PSTOREFS_MAGIC'], |
| 93 |
# pvfs2 is the old version of orangefs |
| 94 |
'pvfs2': ['ORANGEFS_DEVREQ_MAGIC'], |
| 95 |
'qnx4': ['QNX4_SUPER_MAGIC'], |
| 96 |
'qnx6': ['QNX6_SUPER_MAGIC'], |
| 97 |
'ramfs': ['RAMFS_MAGIC'], |
| 98 |
'resctrl': ['RDTGROUP_SUPER_MAGIC'], |
| 99 |
'reiserfs': ['REISERFS_SUPER_MAGIC'], |
| 100 |
'rpc_pipefs': ['RPC_PIPEFS_SUPER_MAGIC'], |
| 101 |
'secretmem': ['SECRETMEM_MAGIC'], |
| 102 |
'securityfs': ['SECURITYFS_MAGIC'], |
| 103 |
'selinuxfs': ['SELINUX_MAGIC'], |
| 104 |
'shiftfs': ['SHIFTFS_MAGIC'], |
| 105 |
'smackfs': ['SMACK_MAGIC'], |
| 106 |
# smb3 is an alias for cifs |
| 107 |
'smb3': ['CIFS_SUPER_MAGIC'], |
| 108 |
# smbfs was removed from the kernel in 2010, the magic remains |
| 109 |
'smbfs': ['SMB_SUPER_MAGIC'], |
| 110 |
'sockfs': ['SOCKFS_MAGIC'], |
| 111 |
'squashfs': ['SQUASHFS_MAGIC'], |
| 112 |
'sysfs': ['SYSFS_MAGIC'], |
| 113 |
# note that devtmpfs shares the same magic with tmpfs, given it is just a special named instance of it. |
| 114 |
'tmpfs': ['TMPFS_MAGIC'], |
| 115 |
'tracefs': ['TRACEFS_MAGIC'], |
| 116 |
'udf': ['UDF_SUPER_MAGIC'], |
| 117 |
'usbdevfs': ['USBDEVICE_SUPER_MAGIC'], |
| 118 |
'vboxsf': ['VBOXSF_SUPER_MAGIC'], |
| 119 |
# note that msdos shares the same magic (and is the older version) |
| 120 |
'vfat': ['MSDOS_SUPER_MAGIC'], |
| 121 |
'v9fs': ['V9FS_MAGIC'], |
| 122 |
'xenfs': ['XENFS_SUPER_MAGIC'], |
| 123 |
'xfs': ['XFS_SUPER_MAGIC'], |
| 124 |
'z3fold': ['Z3FOLD_MAGIC'], |
| 125 |
'zonefs': ['ZONEFS_MAGIC'], |
| 126 |
'zsmalloc': ['ZSMALLOC_MAGIC'], |
| 127 |
} # fmt: skip |
| 128 |
|
| 129 |
# System magics are sometimes not unique, because file systems got new |
| 130 |
# revisions or got renamed. Let's prefer newer over older here, and thus ignore |
| 131 |
# the old names. |
| 132 |
OBSOLETE_NAMES = { |
| 133 |
'cpuset', # magic taken over by cgroupfs |
| 134 |
'devtmpfs', # not a file system of its own, but just a "named superblock" of tmpfs |
| 135 |
'ext2', # ext4 is the newest revision of ext2 + ext3 |
| 136 |
'ext3', |
| 137 |
'fuseblk', # closely related to fuse; they share a single magic, but the latter is more common |
| 138 |
'gfs', # magic taken over by gfs2 |
| 139 |
'msdos', # vfat is the newest revision of msdos |
| 140 |
'ncp', # ncpfs (not ncp) was the last name of the netware 'file_system_type' |
| 141 |
# name before it was removed in 2018 |
| 142 |
'nfs', # nfs4 is the newest revision of nfs |
| 143 |
'pvfs2', # orangefs is the new name of pvfs2 |
| 144 |
'smb3', # smb3 is an alias for cifs |
| 145 |
} # fmt: skip |
| 146 |
|
| 147 |
FILESYSTEM_SETS = [ |
| 148 |
( |
| 149 |
'@basic-api', |
| 150 |
'Basic filesystem API', |
| 151 |
'cgroup', |
| 152 |
'cgroup2', |
| 153 |
'devpts', |
| 154 |
'devtmpfs', |
| 155 |
'mqueue', |
| 156 |
'proc', |
| 157 |
'sysfs', |
| 158 |
), |
| 159 |
( |
| 160 |
'@anonymous', |
| 161 |
'Anonymous inodes', |
| 162 |
'anon_inodefs', |
| 163 |
'pipefs', |
| 164 |
'sockfs', |
| 165 |
), |
| 166 |
( |
| 167 |
'@application', |
| 168 |
'Application virtual filesystems', |
| 169 |
'autofs', |
| 170 |
'fuse', |
| 171 |
'overlay', |
| 172 |
), |
| 173 |
( |
| 174 |
'@auxiliary-api', |
| 175 |
'Auxiliary filesystem API', |
| 176 |
'binfmt_misc', |
| 177 |
'configfs', |
| 178 |
'efivarfs', |
| 179 |
'fusectl', |
| 180 |
'hugetlbfs', |
| 181 |
'rpc_pipefs', |
| 182 |
'securityfs', |
| 183 |
), |
| 184 |
( |
| 185 |
'@common-block', |
| 186 |
'Common block device filesystems', |
| 187 |
'btrfs', |
| 188 |
'erofs', |
| 189 |
'exfat', |
| 190 |
'ext4', |
| 191 |
'f2fs', |
| 192 |
'iso9660', |
| 193 |
'ntfs3', |
| 194 |
'squashfs', |
| 195 |
'udf', |
| 196 |
'vfat', |
| 197 |
'xfs', |
| 198 |
), |
| 199 |
( |
| 200 |
'@historical-block', |
| 201 |
'Historical block device filesystems', |
| 202 |
'ext2', |
| 203 |
'ext3', |
| 204 |
'minix', |
| 205 |
), |
| 206 |
( |
| 207 |
'@network', |
| 208 |
'Well-known network filesystems', |
| 209 |
'afs', |
| 210 |
'ceph', |
| 211 |
'cifs', |
| 212 |
'gfs', |
| 213 |
'gfs2', |
| 214 |
'ncp', |
| 215 |
'ncpfs', |
| 216 |
'nfs', |
| 217 |
'nfs4', |
| 218 |
'ocfs2', |
| 219 |
'orangefs', |
| 220 |
'pvfs2', |
| 221 |
'smb3', |
| 222 |
'smbfs', |
| 223 |
), |
| 224 |
( |
| 225 |
'@privileged-api', |
| 226 |
'Privileged filesystem API', |
| 227 |
'bpf', |
| 228 |
'debugfs', |
| 229 |
'pstore', |
| 230 |
'tracefs', |
| 231 |
), |
| 232 |
( |
| 233 |
'@security', |
| 234 |
'Security/MAC API VFS', |
| 235 |
'apparmorfs', |
| 236 |
'selinuxfs', |
| 237 |
'smackfs', |
| 238 |
), |
| 239 |
( |
| 240 |
'@temporary', |
| 241 |
'Temporary filesystems', |
| 242 |
'ramfs', |
| 243 |
'tmpfs', |
| 244 |
), |
| 245 |
( |
| 246 |
'@known', |
| 247 |
'All known filesystems declared in the kernel', |
| 248 |
*NAME_TO_MAGIC.keys(), |
| 249 |
), |
| 250 |
] |
| 251 |
|
| 252 |
|
| 253 |
def generate_gperf(): |
| 254 |
print('''\ |
| 255 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 256 |
%{ |
| 257 |
#if __GNUC__ >= 15 |
| 258 |
_Pragma("GCC diagnostic ignored \\"-Wzero-as-null-pointer-constant\\"") |
| 259 |
#endif |
| 260 |
#include <linux/magic.h> |
| 261 |
|
| 262 |
#include "filesystems.h" |
| 263 |
#include "stat-util.h" |
| 264 |
|
| 265 |
struct FilesystemMagic { |
| 266 |
const char *name; |
| 267 |
statfs_f_type_t magic[FILESYSTEM_MAGIC_MAX]; |
| 268 |
}; |
| 269 |
%} |
| 270 |
struct FilesystemMagic; |
| 271 |
%language=ANSI-C |
| 272 |
%define hash-function-name filesystems_gperf_hash |
| 273 |
%define lookup-function-name filesystems_gperf_lookup |
| 274 |
%define slot-name name |
| 275 |
%readonly-tables |
| 276 |
%omit-struct-type |
| 277 |
%struct-type |
| 278 |
%includes |
| 279 |
%%''') |
| 280 |
for name, magics in NAME_TO_MAGIC.items(): |
| 281 |
print(f'{name + ",":16} {{{", ".join(magics)}}}') |
| 282 |
|
| 283 |
|
| 284 |
def generate_fs_type_to_string(): |
| 285 |
print('''\ |
| 286 |
#include <linux/magic.h> |
| 287 |
#include "filesystems.h" |
| 288 |
|
| 289 |
/* PROJECT_FILE, which is used by log_xyz() thus also used by assert_not_reached(), cannot be used in |
| 290 |
* generated files, as the build directory may be outside of the source directory. */ |
| 291 |
#undef PROJECT_FILE |
| 292 |
#define PROJECT_FILE __FILE__ |
| 293 |
|
| 294 |
const char* fs_type_to_string(statfs_f_type_t magic) { |
| 295 |
switch (magic) {''') |
| 296 |
|
| 297 |
for name, magics in NAME_TO_MAGIC.items(): |
| 298 |
if name in OBSOLETE_NAMES: |
| 299 |
continue |
| 300 |
for magic in magics: |
| 301 |
print(f' case (statfs_f_type_t) {magic}:') |
| 302 |
print(f' return "{name}";') |
| 303 |
|
| 304 |
print('''\ |
| 305 |
} |
| 306 |
return NULL; |
| 307 |
}''') |
| 308 |
|
| 309 |
|
| 310 |
def generate_fs_in_group(): |
| 311 |
print('bool fs_in_group(const struct statfs *st, FilesystemGroups fs_group) {') |
| 312 |
print(' switch (fs_group) {') |
| 313 |
|
| 314 |
for name, _, *filesystems in FILESYSTEM_SETS: |
| 315 |
magics = sorted(set(sum((NAME_TO_MAGIC[fs] for fs in filesystems), start=[]))) |
| 316 |
enum = 'FILESYSTEM_SET_' + name[1:].upper().replace('-', '_') |
| 317 |
print(f' case {enum}:') |
| 318 |
opts = '\n || '.join(f'F_TYPE_EQUAL(st->f_type, {magic})' for magic in magics) |
| 319 |
print(f' return {opts};') |
| 320 |
|
| 321 |
print(' default: assert_not_reached();') |
| 322 |
print(' }') |
| 323 |
print('}') |
| 324 |
|
| 325 |
|
| 326 |
def generate_filesystem_sets(): |
| 327 |
print('const FilesystemSet filesystem_sets[_FILESYSTEM_SET_MAX] = {') |
| 328 |
|
| 329 |
for name, desc, *filesystems in FILESYSTEM_SETS: |
| 330 |
enum = 'FILESYSTEM_SET_' + name[1:].upper().replace('-', '_') |
| 331 |
|
| 332 |
print(f' [{enum}] = {{') |
| 333 |
print(f' .name = "{name}",') |
| 334 |
print(f' .help = "{desc}",') |
| 335 |
print(' .value =') |
| 336 |
for filesystem in filesystems: |
| 337 |
print(f' "{filesystem}\\0"') |
| 338 |
print(' },') |
| 339 |
|
| 340 |
print('};') |
| 341 |
|
| 342 |
|
| 343 |
def magic_defines(): |
| 344 |
cpp = os.environ['CPP'].split() |
| 345 |
out = subprocess.check_output( |
| 346 |
[*cpp, '-dM', '-include', 'linux/magic.h', '-'], |
| 347 |
stdin=subprocess.DEVNULL, |
| 348 |
text=True, |
| 349 |
) |
| 350 |
for line in out.splitlines(): |
| 351 |
_, name, *rest = line.split() |
| 352 |
if ( |
| 353 |
'_MAGIC' in name |
| 354 |
and rest |
| 355 |
and rest[0].startswith('0x') |
| 356 |
and name |
| 357 |
not in { |
| 358 |
'STACK_END_MAGIC', |
| 359 |
'MTD_INODE_FS_MAGIC', |
| 360 |
'FUTEXFS_SUPER_MAGIC', |
| 361 |
'CRAMFS_MAGIC_WEND', |
| 362 |
} |
| 363 |
): |
| 364 |
yield name |
| 365 |
|
| 366 |
|
| 367 |
def check(): |
| 368 |
kernel_magics = set(magic_defines()) |
| 369 |
our_magics = set(sum(NAME_TO_MAGIC.values(), start=[])) |
| 370 |
extra = kernel_magics - our_magics |
| 371 |
if extra: |
| 372 |
sys.exit(f'kernel knows additional filesystem magics: {", ".join(sorted(extra))}') |
| 373 |
|
| 374 |
|
| 375 |
if __name__ == '__main__': |
| 376 |
for arg in sys.argv[1:]: |
| 377 |
if arg == 'gperf': |
| 378 |
generate_gperf() |
| 379 |
elif arg == 'fs-type-to-string': |
| 380 |
generate_fs_type_to_string() |
| 381 |
elif arg == 'filesystem-sets': |
| 382 |
generate_filesystem_sets() |
| 383 |
elif arg == 'fs-in-group': |
| 384 |
generate_fs_in_group() |
| 385 |
elif arg == 'check': |
| 386 |
check() |
| 387 |
else: |
| 388 |
raise ValueError |