Skip to content
systemcoding.dev
Kernels

Create a pseudo filesystem with init_pseudo

Create a tiny in-kernel filesystem for internal files with init_pseudo and kern_mount on Linux 6.12, the way sockfs and pipefs are built.

1 min read
Summarize in AI

Some kernel objects need inodes but no mount point that users see. A pseudo filesystem gives you a superblock with almost no code, and init_pseudo sets up the context for it.

c
#define DEMO_MAGIC 0x44454d4fUL

static int demo_init_fs_context(struct fs_context *fc)
{
	struct pseudo_fs_context *ctx = init_pseudo(fc, DEMO_MAGIC);

	if (!ctx)
		return -ENOMEM;
	return 0;
}

static struct file_system_type demo_fs_type = {
	.name            = "demofs",
	.init_fs_context = demo_init_fs_context,
	.kill_sb         = kill_anon_super,
};
Shell
grep demofs /proc/filesystems
Text
nodev	demofs

After register_filesystem(&demo_fs_type), the line appears in /proc/filesystems with nodev, since there is no block device. Call kern_mount(&demo_fs_type) to get an internal vfsmount, and allocate inodes from its superblock with new_inode.

The function fills in an empty root directory and sets the default operations. Set ctx->ops or ctx->dops before returning to override the superblock and dentry operations. Do not register the type if user space never needs to mount it: kern_mount works without it. The pattern is used for pipefs and sockfs, and the context API is described in the filesystem mount API.

Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.