An overlooked part in the Linux VFS: filesystem mounting

intro

We've all heard of that the VFS is like the polymorphism in C++: every filesystem can be regarded as a plugin which fits in the VFS. And that's partially the reason there are so many types of filesystem right now.

From users' perspective, all files live uniformly in one single tree. It also perfectly embodies Unix's philosophy of "Everything is a file". As a side-effect, different processes can be set different root directories, which is exactly what chroot and container do.

superblock instantiating

In Linux, every filesystem needs to be mounted to the root directory tree before it can be used: A formated hard disk can be only accessed as a block device which has no way to read/write the files inside it. Obviously, that's because it's not mounted yet. Then the question is: what does mount do under the hood?

In my opinion, the decisive event is the creation of super_block.

///@file:  fs/namespace.c
/// mount=>do_mount=>path_mount=>do_new_mount=>do_new_mount_fc=>vfs_create_mount
/**
 * vfs_create_mount - Create a mount for a configured superblock
 * @fc: The configuration context with the superblock attached
 *
 * Create a mount to an already configured superblock.  If necessary, the
 * caller should invoke vfs_get_tree() before calling this.
 *
 * Note that this does not attach the mount to anything.
 */
struct vfsmount *vfs_create_mount(struct fs_context *fc)
{
///...
///...
}

As we can see, the two most important fields sit at the beginning of the return type of vfs_create_mount: struct dentry *mnt_root and struct super_block *mnt_sb.

///@file: include/linux/mount.h
struct vfsmount {
	struct dentry *mnt_root;	/* root of the mounted tree */
	struct super_block *mnt_sb;	/* pointer to superblock */
	int mnt_flags;
	struct mnt_idmap *mnt_idmap;
} __randomize_layout;

pathname to inode

Technically, a mount point could be anywhere, so the VFS must provide its fundamental infrastructure: being aware of the mount point and switched smoothly to the underlying filesystem automatically when accessing a file path.

///@file: fs/namespace.c
path_openat=>open_last_lookups=>step_into=>handle_mounts=>traverse_mounts=>__traverse_mounts=>lookup_mnt=>__lookup_mnt

/**
 * __lookup_mnt - find first child mount
 * @mnt:	parent mount
 * @dentry:	mountpoint
 *
 * If @mnt has a child mount @c mounted @dentry find and return it.
 *
 * Note that the child mount @c need not be unique. There are cases
 * where shadow mounts are created. For example, during mount
 * propagation when a source mount @mnt whose root got overmounted by a
 * mount @o after path lookup but before @namespace_sem could be
 * acquired gets copied and propagated. So @mnt gets copied including
 * @o. When @mnt is propagated to a destination mount @d that already
 * has another mount @n mounted at the same mountpoint then the source
 * mount @mnt will be tucked beneath @n, i.e., @n will be mounted on
 * @mnt and @mnt mounted on @d. Now both @n and @o are mounted at @mnt
 * on @dentry.
 *
 * Return: The first child of @mnt mounted @dentry or NULL.
 */
struct mount *__lookup_mnt(struct vfsmount *mnt, struct dentry *dentry)
{
	struct hlist_head *head = m_hash(mnt, dentry);
	struct mount *p;

	hlist_for_each_entry_rcu(p, head, mnt_hash)
		if (&p->mnt_parent->mnt == mnt && p->mnt_mountpoint == dentry)
			return p;
	return NULL;
}

The procedure is straightforward:

  1. Check if a dentry is a mount point(based on a flag inside the dentry structre).
  2. Look up a global hash to find the mounted filesystem(the key is parent filesystem and the mount point in it).

outro

The VFS is a beautiful Unix architecture where system mount plays a vital role and the mount-handling in path to inode is a concrete workhorse to implement it.

posted on 2026-10-31 11:58  tsecer  阅读(7)  评论(0)    收藏  举报

导航