How does Linux, the kernel, mount filesystems? What actually does this

filesystems

My question concerns the Linux source, and if it actually contains the code necessary, natively to mount, parse, and use a filesystem, or if that's added to the source.

If the Linux source contains code to parse and access/organize files through a file system and such, is it safe to say that the filesystem is controlled by a device driver coded in to Linux, or is this procedure differ based on things like distro, releases, etc.

How does Linux access files using a filesystem on other storage devices, i.e., how do userspace programs access proc, ext3, ext4, etc., and are ext3, ext4 considered "device drivers", since they operate directly with hardware, or do I have it wrong?

Best Answer

There is a component in the kernel (called the virtual file system, or VFS for short) that provides a generic interface to all filesystems. It understands things like file types (regular files, directories, symbolic links, …), metadata (times, permissions, …) and file contents.

Each Linux process lives in a namespace that specifies where filesystems are mounted. Often all the processes live in the same namespace; namespaces are mainly intended to support virtualization. A namespace is essentially a collection of paths, with an internal filesystem reference associated with each path. Mounting and unmounting consists of changing that namespace.

When the process access a file, the VFS component parses the path based on the process's namespace and current directory and determines under which mount point the file is located. The VFS then dispatches the file-related command to the appropriate filesystem driver based on the internal filesystem reference associated with the mount point.

It's the job of the filesystem driver to translate the command into data storage or retrieval. Each filesystem type. Most filesystem drivers don't interact directly with hardware but only with other drivers. A driver for a disk-backed filesystem (ext4, btrfs, vfat, …) translates the command into block storage operations (read and write sectors from a partition or other block device). A driver for a network-backed filesystem (nfs, cifs, …) translates the command into communication over a network channel. A driver for an internal kernel filesystem (proc, sysfs, …) works on its own. The FUSE filesystem driver passes the command on to a userland process.

Related Question