Skip to content
systemcoding.dev
Kernels

Write a Minimal In-Memory Filesystem With libfs Helpers

Build a tiny kernel filesystem that exposes one read-only file, using simple_fill_super, get_tree_nodev and simple_read_from_buffer from libfs on Linux 6.12.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and FreeBSD logos.

The smallest useful kernel filesystem has no disk, no allocation tracking and no journal. It builds a root directory and a few files in memory when you mount it. The kernel ships a helper library for exactly this, fs/libfs.c, and filesystems such as debugfs, securityfs and configfs rely on its pieces. This post writes a module called tinyfs that mounts as a filesystem containing one read-only file named hello. It targets Linux 6.12, which uses the fs_context mount API, and the code compiles as an out-of-tree module with GCC 14. Nothing here was run: read it as carefully reviewed source, with the output worked out from the code.

The pieces a filesystem needs

A mountable in-memory filesystem needs four things:

  1. A struct file_system_type registered with the VFS, so mount -t tinyfs finds it.

  2. A mount entry point, which in the current API is init_fs_context setting up a struct fs_context_operations.

  3. A fill_super function that prepares the struct super_block and builds the root inode and dentry.

  4. File operations for each file it exposes.

libfs supplies simple_fill_super, which builds the root directory and a flat list of files from an array, and helpers such as simple_read_from_buffer. The overall API is described in the kernel's Overview of the Linux Virtual File System and the mount API documentation.

The complete module

tinyfs.cc
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/fs_context.h>
#include <linux/pagemap.h>

#define TINYFS_MAGIC 0x74696e79

static const char hello_text[] = "hello from tinyfs\n";

static ssize_t hello_read(struct file *file, char __user *buf,
                          size_t count, loff_t *ppos)
{
    return simple_read_from_buffer(buf, count, ppos, hello_text,
                                   sizeof(hello_text) - 1);
}

static const struct file_operations hello_fops = {
    .read   = hello_read,
    .llseek = default_llseek,
};

static int tinyfs_fill_super(struct super_block *sb, struct fs_context *fc)
{
    static const struct tree_descr files[] = {
        { "hello", &hello_fops, 0444 },
        { "" },
    };

    return simple_fill_super(sb, TINYFS_MAGIC, files);
}

static int tinyfs_get_tree(struct fs_context *fc)
{
    return get_tree_nodev(fc, tinyfs_fill_super);
}

static const struct fs_context_operations tinyfs_ctx_ops = {
    .get_tree = tinyfs_get_tree,
};

static int tinyfs_init_fs_context(struct fs_context *fc)
{
    fc->ops = &tinyfs_ctx_ops;
    return 0;
}

static struct file_system_type tinyfs_type = {
    .owner           = THIS_MODULE,
    .name            = "tinyfs",
    .init_fs_context = tinyfs_init_fs_context,
    .kill_sb         = kill_litter_super,
};

static int __init tinyfs_init(void)
{
    return register_filesystem(&tinyfs_type);
}

static void __exit tinyfs_exit(void)
{
    unregister_filesystem(&tinyfs_type);
}

module_init(tinyfs_init);
module_exit(tinyfs_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Minimal in-memory filesystem built on libfs");

Why each part is there

simple_fill_super(sb, magic, files) does most of the work. It sets the block size to PAGE_SIZE, installs default super_operations, creates the root inode as a directory with mode 0755, then walks the tree_descr array. For each entry it allocates an inode and a dentry named after the string. Entries are numbered from inode 2 upward, the root takes inode 1, and the loop stops at the first entry whose name is empty, which is why the array ends with { "" }.

The hello_read function is the whole data path. simple_read_from_buffer copies from a kernel buffer to the user buffer, honoring *ppos and returning 0 at end of file. Passing sizeof(hello_text) - 1 leaves out the terminating NUL, so cat prints text without a stray byte. Notice there is no open method: the VFS opens the file using the inode's default and the file_operations that simple_fill_super attached from the table.

get_tree_nodev is the right helper for a filesystem with no backing device. It creates a fresh superblock for every mount (each mount -t tinyfs gets an independent instance) and calls your fill_super. For a filesystem with one shared instance you would use get_tree_single instead. kill_litter_super is the matching teardown: it removes the dentries that simple_fill_super pinned with a reference, then destroys the superblock. Without it the module would leak those dentries on unmount.

Build and load

An out-of-tree module needs a Makefile against your installed kernel headers:

Makefilemakefile
obj-m += tinyfs.o

all:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Then load the module and mount it. A mount needs a source string, which get_tree_nodev ignores, so any word works:

Shell
make
sudo insmod tinyfs.ko
grep tinyfs /proc/filesystems
sudo mkdir -p /mnt/tiny
sudo mount -t tinyfs none /mnt/tiny
ls -l /mnt/tiny
cat /mnt/tiny/hello
Text
nodev	tinyfs
total 0
-r--r--r-- 1 root root 0 Nov 23 11:02 hello
hello from tinyfs

The size shows 0 because simple_fill_super creates inodes with size zero and we never set i_size. The data still reads correctly, since hello_read does not consult the inode size. A reader that trusts st_size, such as cp, copies zero bytes. To fix it, save the inode returned from the loop and set inode->i_size = sizeof(hello_text) - 1, which requires writing your own fill loop instead of the helper.

The nodev in /proc/filesystems confirms the type is registered without a block device. Unmount and remove the module in reverse order:

Shell
sudo umount /mnt/tiny
sudo rmmod tinyfs

How the mount reaches your code

It helps to follow one mount command through the kernel. The mount(8) program calls fsopen("tinyfs"), which asks the VFS to find a registered file_system_type by name and allocate a struct fs_context. The VFS calls your init_fs_context, where you attach the operations table. The program then calls fsconfig for each option and finally fsconfig with the create command, which invokes your get_tree. At that point get_tree_nodev allocates the superblock, calls tinyfs_fill_super, and stores the finished root dentry in the context. The last step, fsmount and move_mount, attaches it to /mnt/tiny.

This split matters because option parsing now happens before the superblock exists. Our module accepts no options, so the context operations table has only get_tree. If a user passed -o size=1M, the default parsing would reject it with EINVAL, which is the right behavior for a filesystem that supports nothing. A later post in this cluster adds a parse_param handler for real options.

The static tree_descr array is also worth a second look. Defining it inside the function is legal because simple_fill_super only reads it during the call, and the file operations it points at are global. If your table is large or shared, move it to file scope. Whichever you pick, the file_operations structures must outlive the mount, so they should be static const at file scope and never on the stack.

Extending it

Real in-memory filesystems grow in three directions. To add writable files, implement write with simple_write_to_buffer and a kernel buffer guarded by a mutex. To add directories, stop using the flat table and create inodes with new_inode(sb), set i_op = &simple_dir_inode_operations and i_fop = &simple_dir_operations, and link them with d_add or d_make_root. To support mkdir and create from user space, add inode_operations that allocate fresh inodes, which is the road to ramfs, whose source in fs/ramfs/inode.c is under 400 lines and the best next read.

Pitfalls

Two mistakes are common. First, forgetting .owner = THIS_MODULE lets a user rmmod the module while a mount is live, which crashes on the next access. Second, choosing kill_anon_super instead of kill_litter_super skips the step that drops the dentries simple_fill_super pinned, and the kernel then complains about busy inodes after unmount. Also keep in mind that the fs_context API replaced the older mount callback and mount_nodev helper, so examples written for kernels before 5.x will not compile on 6.12.

Share

Keep reading

Get new posts by email

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

Unsubscribe in one click. No spam, ever.