tools/lib/api/fs/fs.c
Source file repositories/reference/linux-study-clean/tools/lib/api/fs/fs.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/api/fs/fs.c- Extension
.c- Size
- 10085 bytes
- Lines
- 509
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
assert.hctype.herrno.hlimits.hstdbool.hstdio.hstdlib.hstring.hsys/vfs.hsys/types.hsys/stat.hfcntl.hpthread.hunistd.hsys/mount.hfs.h../io.hdebug-internal.h
Detected Declarations
struct fsfunction fs__read_mountsfunction fs__valid_mountfunction fs__check_mountsfunction mem_toupperfunction fs__env_overridefunction fs__init_oncefunction filename__read_intfunction filename__read_ull_basefunction filename__read_xllfunction automaticallyfunction filename__read_strfunction filename__write_intfunction procfs__read_strfunction sysfs__read_ull_basefunction sysfs__read_xllfunction sysfs__read_ullfunction sysfs__read_intfunction sysfs__read_strfunction sysfs__read_boolfunction sysctl__read_intfunction sysfs__write_int
Annotated Snippet
struct fs {
const char * const name;
const char * const * const mounts;
char *path;
pthread_mutex_t mount_mutex;
const long magic;
};
#ifndef TRACEFS_MAGIC
#define TRACEFS_MAGIC 0x74726163
#endif
static void fs__init_once(struct fs *fs);
static const char *fs__mountpoint(const struct fs *fs);
static const char *fs__mount(struct fs *fs);
#define FS(lower_name, fs_name, upper_name) \
static struct fs fs__##lower_name = { \
.name = #fs_name, \
.mounts = lower_name##__known_mountpoints, \
.magic = upper_name##_MAGIC, \
.mount_mutex = PTHREAD_MUTEX_INITIALIZER, \
}; \
\
static void lower_name##_init_once(void) \
{ \
struct fs *fs = &fs__##lower_name; \
\
fs__init_once(fs); \
} \
\
const char *lower_name##__mountpoint(void) \
{ \
static pthread_once_t init_once = PTHREAD_ONCE_INIT; \
struct fs *fs = &fs__##lower_name; \
\
pthread_once(&init_once, lower_name##_init_once); \
return fs__mountpoint(fs); \
} \
\
const char *lower_name##__mount(void) \
{ \
const char *mountpoint = lower_name##__mountpoint(); \
struct fs *fs = &fs__##lower_name; \
\
if (mountpoint) \
return mountpoint; \
\
return fs__mount(fs); \
} \
\
bool lower_name##__configured(void) \
{ \
return lower_name##__mountpoint() != NULL; \
}
FS(sysfs, sysfs, SYSFS);
FS(procfs, procfs, PROC_SUPER);
FS(debugfs, debugfs, DEBUGFS);
FS(tracefs, tracefs, TRACEFS);
FS(hugetlbfs, hugetlbfs, HUGETLBFS);
FS(bpf_fs, bpf, BPF_FS);
static bool fs__read_mounts(struct fs *fs)
{
char type[100];
FILE *fp;
char path[PATH_MAX + 1];
fp = fopen("/proc/mounts", "r");
if (fp == NULL)
return false;
while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
path, type) == 2) {
if (strcmp(type, fs->name) == 0) {
fs->path = strdup(path);
fclose(fp);
return fs->path != NULL;
}
}
fclose(fp);
return false;
}
static int fs__valid_mount(const char *fs, long magic)
{
struct statfs st_fs;
Annotation
- Immediate include surface: `assert.h`, `ctype.h`, `errno.h`, `limits.h`, `stdbool.h`, `stdio.h`, `stdlib.h`, `string.h`.
- Detected declarations: `struct fs`, `function fs__read_mounts`, `function fs__valid_mount`, `function fs__check_mounts`, `function mem_toupper`, `function fs__env_override`, `function fs__init_once`, `function filename__read_int`, `function filename__read_ull_base`, `function filename__read_xll`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.