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.

Dependency Surface

Detected Declarations

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

Implementation Notes