Describe the principles of Linux virtual file system in detail
In the world of Unix, there is a classic saying: everything is a file. What this means is that in the Unix operating system, all objects can be treated as files, and they can be manipulated using the interface for operating files. As a Unix-like operating system, Linux also strives to achieve this goal.
Introduction to Virtual File System
In order to achieve the goal of Everything is a file
, the Linux kernel provides an intermediate layer: Virtual File System (Virtual File System)
.
If you have ever used an object-oriented programming language (such as C/Java, etc.), then you should be familiar with the concept of Interface
. The virtual file system is similar to the interface in object-oriented programming, which defines a set of standard interfaces. Developers only need to implement this set of interfaces, and then they can use the interface for operating files to operate objects. As shown below:

The blue part in the above picture is the location of the virtual file system.
As can be seen from the above figure, the virtual file system provides a unified interface for upper-layer applications. If a file system implements the virtual file system interface, then the upper-layer application can use functions such as open()
, read()
and write()
etc. functions to operate them.
Today, we will introduce the principle and implementation of virtual file system.
Principle of virtual file system
Before explaining the principles of virtual file systems, let us first introduce a Java example. Through this Java example, we can more easily understand the principles of virtual file systems.
A Java example
If you have used Java to write programs, it is easy to understand the virtual file system. We use Java's interface to simulate the definition of a virtual file system:
1 2 3 4 5 6 |
|
The above defines an interface named VFSFile
, which defines some methods, such as open()
, read()
and write()
etc. Now we define an object named Ext3File
to implement this interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Now we can use the VFSFile
interface to operate the Ext3File
object, as follows:
1 2 3 4 5 6 7 8 |
|
从上面的例子可以看出,底层对象只需要实现 VFSFile
接口,就可以使用 VFSFile
接口相关的方法来操作对象,用户完全不需要了解底层对象的实现过程。
虚拟文件系统原理
上面的 Java 例子已经大概说明虚拟文件系统的原理,但由于 Linux 是使用 C 语言来编写的,而 C 语言并没有接口这个概念。所以,Linux 内核使用了一些技巧来模拟接口这个概念。
下面来介绍一下 Linux 内核是如何实现的。
1. file结构
为了模拟接口,Linux 内核定义了一个名为 file
的结构体,其定义如下:
1 2 3 4 5 |
|
在 file 结构中,最为重要的一个字段就是 f_op
,其类型为 file_operations
结构。而 file_operations
结构是由一组函数指针组成,其定义如下:
1 2 3 4 5 6 7 8 9 |
|
从 file_operations
结构的定义可以隐约看到接口的影子,所以可以猜想出,如果实现了 file_operations
结构中的方法,应该就能接入到虚拟文件系统中。
在 Linux 内核中,file
结构代表着一个被打开的文件。所以,只需要将 file
结构的 f_op
字段设置成不同文件系统实现好的方法集,那么就能够使用不同文件系统的功能。
这个过程在 __dentry_open()
函数中实现,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
设置好 file
结构的 f_op
字段后,虚拟文件系统就能够使用通用的接口来操作此文件了。调用过程如下:

2. file_operations结构
底层文件系统需要实现虚拟文件系统的接口,才能被虚拟文件系统使用。也就是说,底层文件系统需要实现 file_operations
结构中的方法集。
一般底层文件系统会在其内部定义好 file_operations
结构,并且填充好其方法集中的函数指针。如 minix文件系统
就定义了一个名为 minix_file_operations
的 file_operations
结构。其定义如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
也就是说,如果当前使用的是 minix 文件系统,当使用 read()
函数读取其文件的内容时,那么最终将会调用 do_sync_read()
函数来读取文件的内容。
3. dentry结构
到这里,虚拟文件系统的原理基本分析完毕,但还有两个非常重要的结构要介绍一下的:dentry
和 inode
。
dentry
结构表示一个打开的目录项,当我们打开文件 /usr/local/lib/libc.so
文件时,内核会为文件路径中的每个目录创建一个 dentry
结构。如下图所示:

由于 /usr/local/lib/libc.so
和 /tmp/libc.so
指向同一个文件,所以它们都使用同一个 inode
对象。
inode 结构保存了文件的所有属性值,如文件的创建时间、文件所属用户和文件的大小等。其定义如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
我们注意到 inode 结构有个类型为 file_operations
结构的字段 i_fop
,这个字段保存了文件的操作方法集。当用户调用 open()
系统调用打开文件时,内核将会使用 inode
结构的 i_fop
字段赋值给 file
结构的 f_op
字段。我们再来重温下赋值过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
总结
本文主要介绍了 虚拟文件系统
的基本原理,从分析中可以发现,虚拟文件系统使用了类似于面向对象编程语言中的接口概念。正是有了 虚拟文件系统
,Linux 才能支持各种各样的文件系统。
The above is the detailed content of Describe the principles of Linux virtual file system in detail. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Backup and Recovery Policy of GitLab under CentOS System In order to ensure data security and recoverability, GitLab on CentOS provides a variety of backup methods. This article will introduce several common backup methods, configuration parameters and recovery processes in detail to help you establish a complete GitLab backup and recovery strategy. 1. Manual backup Use the gitlab-rakegitlab:backup:create command to execute manual backup. This command backs up key information such as GitLab repository, database, users, user groups, keys, and permissions. The default backup file is stored in the /var/opt/gitlab/backups directory. You can modify /etc/gitlab

CentOS hard disk mount is divided into the following steps: determine the hard disk device name (/dev/sdX); create a mount point (it is recommended to use /mnt/newdisk); execute the mount command (mount /dev/sdX1 /mnt/newdisk); edit the /etc/fstab file to add a permanent mount configuration; use the umount command to uninstall the device to ensure that no process uses the device.

After CentOS is stopped, users can take the following measures to deal with it: Select a compatible distribution: such as AlmaLinux, Rocky Linux, and CentOS Stream. Migrate to commercial distributions: such as Red Hat Enterprise Linux, Oracle Linux. Upgrade to CentOS 9 Stream: Rolling distribution, providing the latest technology. Select other Linux distributions: such as Ubuntu, Debian. Evaluate other options such as containers, virtual machines, or cloud platforms.
