Skip to content
systemcoding.dev
Kernels

Register a Filesystem Type With register_filesystem

Learn what register_filesystem does, which file_system_type fields matter, how mount finds your type by name, and how to unregister it safely on module unload.

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 Rust logos.

A filesystem driver is invisible to mount until it tells the kernel its name. That announcement is one call, register_filesystem(), which adds a struct file_system_type to a global list the VFS searches whenever someone runs mount -t name. The call is short, but the structure it takes decides how mounts are created, how superblocks are torn down, and whether your module can be unloaded safely. This post walks through the structure's fields, the registration and unregistration rules, and the mistakes that cause oopses on module removal. It follows Linux 6.12, where the mount path uses fs_context, and the examples compile as an out-of-tree module with GCC 14.

What the call does

register_filesystem(struct file_system_type *fs) lives in fs/filesystems.c. It links the structure into a singly linked list called file_systems under a read-write lock, and it returns 0 on success. It fails with -EBUSY if the structure is already on a list (its next pointer is not NULL) or if a type with the same name is already registered, and the kernel treats a period in the name as a programming error, because the period is reserved for the type.subtype mount syntax. The list is what /proc/filesystems prints, one line per entry.

Nothing is allocated by the call. The structure is yours and must stay valid until unregister_filesystem() returns, which is why it is almost always a static object at file scope.

The file_system_type fields

fstype_fields.cc
static struct file_system_type examplefs_type = {
    .owner            = THIS_MODULE,
    .name             = "examplefs",
    .init_fs_context  = examplefs_init_fs_context,
    .parameters       = examplefs_param_specs,
    .kill_sb          = kill_anon_super,
    .fs_flags         = FS_USERNS_MOUNT,
};

The fields that matter:

FieldPurposeNotes
ownerThe module that provides the typePins the module while a mount exists; set it to THIS_MODULE
nameWhat users pass to mount -tAppears in /proc/filesystems
init_fs_contextCreates the fs_context for a new mountRequired on modern kernels
parametersThe table of mount options the type acceptsOptional, used by fs_parse
kill_sbDestroys a superblock when the last reference goesRequired; choose the helper that matches your get_tree
fs_flagsBehavior bitsFS_REQUIRES_DEV for block filesystems, FS_USERNS_MOUNT to allow unprivileged user-namespace mounts

The older .mount callback still exists for unconverted filesystems, but new code uses init_fs_context. The kernel's mount API document explains the split.

Matching get_tree with kill_sb

The get_tree function you install and the kill_sb you name must be a pair, because they decide what a superblock is. The common pairs are:

  • get_tree_bdev with kill_block_super, for a filesystem on a block device such as ext4

  • get_tree_nodev with kill_anon_super or kill_litter_super, for an in-memory filesystem with a fresh instance per mount

  • get_tree_single with kill_litter_super, for a filesystem with one global instance, like debugfs

Choosing kill_litter_super matters when fill_super pins dentries by a helper such as simple_fill_super: it drops those references before tearing down the superblock. If you take kill_anon_super there, you leak them.

A registration skeleton

This module registers a type and does nothing else, which is enough to see the machinery:

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

static int regfs_get_tree(struct fs_context *fc)
{
    return -ENOSYS;
}

static const struct fs_context_operations regfs_ctx_ops = {
    .get_tree = regfs_get_tree,
};

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

static struct file_system_type regfs_type = {
    .owner           = THIS_MODULE,
    .name            = "regfs",
    .init_fs_context = regfs_init_fs_context,
    .kill_sb         = kill_anon_super,
};

static int __init regfs_init(void)
{
    int err = register_filesystem(&regfs_type);

    if (err)
        pr_err("regfs: register_filesystem failed: %d\n", err);
    else
        pr_info("regfs: registered\n");
    return err;
}

static void __exit regfs_exit(void)
{
    int err = unregister_filesystem(&regfs_type);

    if (err)
        pr_err("regfs: unregister_filesystem failed: %d\n", err);
}

module_init(regfs_init);
module_exit(regfs_exit);
MODULE_LICENSE("GPL");

Load it and look at the registry:

Shell
sudo insmod regfs.ko
grep regfs /proc/filesystems
sudo mount -t regfs none /mnt
sudo rmmod regfs
dmesg | tail -2
Text
nodev	regfs
mount: /mnt: mount(2) system call failed: Function not implemented.
[ 2210.481203] regfs: registered

The type is listed with nodev because FS_REQUIRES_DEV is not set. The mount fails with ENOSYS, the error our get_tree returned, which shows that the VFS found the type by name, built a context, and called into it. Real filesystems replace regfs_get_tree with get_tree_bdev, get_tree_nodev or get_tree_single. Your timestamp in dmesg will differ.

Unregistering safely

unregister_filesystem() removes the type from the list and returns -EINVAL if it was not found. It does not check for live mounts. The protection against unloading a module with mounts is the module reference count: a mounted superblock holds a reference on fs->owner through the VFS, so rmmod fails with "Module is in use" until you unmount. This is why .owner = THIS_MODULE is not optional. Without it the module could vanish while a superblock still points at its function tables, and the next file access would jump into freed memory.

A second subtle rule: do not return the superblock's resources in exit assuming everything is gone. The order is that kill_sb runs when the last mount ends, which is always before the module can be removed, so module exit only needs to remove the type. Any caches you created at init, such as a kmem_cache for inodes, must be destroyed after unregister_filesystem(), and the kernel documentation recommends calling rcu_barrier() first, since inodes are freed through RCU and may still be in flight.

What the VFS does with the type afterwards

Registration is only the first step in a type's life. When a user runs mount -t regfs, the VFS calls get_fs_type("regfs"), which walks the list under the read lock, compares names, and tries to take a reference on the owning module with try_module_get. If the name is not found it asks modprobe for a module called fs-regfs, which is the reason for the MODULE_ALIAS_FS macro. If the module is being unloaded at that moment, the reference fails and the mount reports an unknown type instead of racing the removal.

That reference is held for the whole life of the mount. Each superblock records the type that created it in s_type, and the VFS drops the module reference only after kill_sb returns. This is also why a type's kill_sb runs in the module's own code: the function pointer cannot dangle, because the module cannot go away while the superblock exists. If you ever see a stack trace pointing into freed module memory after rmmod, look first for a missing owner or a work item that outlived the superblock.

The nodev marker in /proc/filesystems comes from the absence of the FS_REQUIRES_DEV flag in fs_flags, so a block filesystem such as ext4 is listed without it.

Registering several types

A module can register more than one type, for example a foofs and a foofs2. Check each return value and unwind in reverse order on failure, as in any kernel init function. A single type can also serve a type.subtype mount: mount -t fuse.myfs finds fuse, and the subtype is passed to the driver as part of the context. That is the reason periods are banned in names.

Debugging registration problems

The failures are few and distinct:

  • -EBUSY from register_filesystem means a type with that name exists. Check /proc/filesystems, and look for a built-in or a previously loaded copy of your module.

  • mount: unknown filesystem type 'regfs' means the module is not loaded and the kernel's automatic module request, which loads a module named fs-regfs, found nothing. Add MODULE_ALIAS_FS("regfs") so modprobe can find the module by filesystem name.

  • A crash at rmmod after a successful mount usually means a missing .owner.

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.