Table of Contents
Introduction to Virtual File System" >Introduction to Virtual File System
Principle of virtual file system" >Principle of virtual file system
A Java example" >A Java example
虚拟文件系统原理" >虚拟文件系统原理
1. file结构" >1. file结构
2. file_operations结构" >2. file_operations结构
3. dentry结构" >3. dentry结构
总结" >总结
Home System Tutorial LINUX Describe the principles of Linux virtual file system in detail

Describe the principles of Linux virtual file system in detail

Feb 15, 2024 am 09:21 AM
linux linux tutorial linux system linux command shell script embeddedlinux Getting started with linux linux learning

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:

细说 Linux 虚拟文件系统原理

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

public interface VFSFile {

  int open(String file, int mode);

  int read(int fd, byte[] buffer, int size);

  int write(int fd, byte[] buffer, int size);

  ...

}

Copy after login

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

public class Ext3File implements VFSFile {

  @Override

  public int open(String file, int mode) {

    ...

  }

   

  @Override

  public int read(int fd, byte[] buffer, int size) {

    ...

  }

   

  @Override

  public int write(int fd, byte[] buffer, int size) {

    ...

  }

   

  ...

}

Copy after login

Now we can use the VFSFile interface to operate the Ext3File object, as follows:

1

2

3

4

5

6

7

8

public class Main() {

  public static void main(String[] args) {

    VFSFile file = new Ext3File();

     

    int fd = file.open("/tmp/file.txt", 0);

    ...

  }

}

Copy after login

从上面的例子可以看出,底层对象只需要实现 VFSFile 接口,就可以使用 VFSFile 接口相关的方法来操作对象,用户完全不需要了解底层对象的实现过程。

虚拟文件系统原理

上面的 Java 例子已经大概说明虚拟文件系统的原理,但由于 Linux 是使用 C 语言来编写的,而 C 语言并没有接口这个概念。所以,Linux 内核使用了一些技巧来模拟接口这个概念。

下面来介绍一下 Linux 内核是如何实现的。

1. file结构

为了模拟接口,Linux 内核定义了一个名为 file 的结构体,其定义如下:

1

2

3

4

5

struct file {

    ...

    const struct file_operations *f_op;

    ...

};

Copy after login

在 file 结构中,最为重要的一个字段就是 f_op,其类型为 file_operations 结构。而 file_operations 结构是由一组函数指针组成,其定义如下:

1

2

3

4

5

6

7

8

9

struct file_operations {

    ...

    loff_t (*llseek) (struct file *, loff_t, int);

    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

    ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

    ...

    int (*open) (struct inode *, struct file *);

    ...

};

Copy after login

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

static struct file *

__dentry_open(struct dentry *dentry, 

              struct vfsmount *mnt, 

              truct file *f, 

              int (*open)(struct inode *, struct file *), 

              const struct cred *cred)

{

    ...

    inode = dentry->d_inode;

    ...

    // 设置file结构的f_op字段为底层文件系统实现的方法集

    f->f_op = fops_get(inode->i_fop);

    ...

    return f;

}

Copy after login

设置好 file 结构的 f_op 字段后,虚拟文件系统就能够使用通用的接口来操作此文件了。调用过程如下:

细说 Linux 虚拟文件系统原理

2. file_operations结构

底层文件系统需要实现虚拟文件系统的接口,才能被虚拟文件系统使用。也就是说,底层文件系统需要实现 file_operations 结构中的方法集。

一般底层文件系统会在其内部定义好 file_operations 结构,并且填充好其方法集中的函数指针。如 minix文件系统 就定义了一个名为 minix_file_operationsfile_operations 结构。其定义如下:

1

2

3

4

5

6

7

8

9

10

11

12

// 文件:fs/minix/file.c

 

const struct file_operations minix_file_operations = {

    .llseek         = generic_file_llseek,

    .read           = do_sync_read,

    .aio_read       = generic_file_aio_read,

    .write          = do_sync_write,

    .aio_write      = generic_file_aio_write,

    .mmap           = generic_file_mmap,

    .fsync          = generic_file_fsync,

    .splice_read    = generic_file_splice_read,

};

Copy after login

也就是说,如果当前使用的是 minix 文件系统,当使用 read() 函数读取其文件的内容时,那么最终将会调用 do_sync_read() 函数来读取文件的内容。

3. dentry结构

到这里,虚拟文件系统的原理基本分析完毕,但还有两个非常重要的结构要介绍一下的:dentryinode

dentry 结构表示一个打开的目录项,当我们打开文件 /usr/local/lib/libc.so 文件时,内核会为文件路径中的每个目录创建一个 dentry 结构。如下图所示:

细说 Linux 虚拟文件系统原理

由于 /usr/local/lib/libc.so/tmp/libc.so 指向同一个文件,所以它们都使用同一个 inode 对象。

inode 结构保存了文件的所有属性值,如文件的创建时间、文件所属用户和文件的大小等。其定义如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

struct inode {

    ...

    uid_t           i_uid;               // 文件所属用户

    gid_t           i_gid;               // 文件所属组

    ...

    struct timespec i_atime;             // 最后访问时间

    struct timespec i_mtime;             // 最后修改时间

    struct timespec i_ctime;             // 文件创建时间

    ...

    unsigned short  i_bytes;             // 文件大小

    ...

    const struct file_operations *i_fop; // 文件操作方法集(用于设置file结构)

    ...

};

Copy after login

我们注意到 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

static struct file *

__dentry_open(struct dentry *dentry, 

              struct vfsmount *mnt, 

              truct file *f, 

              int (*open)(struct inode *, struct file *), 

              const struct cred *cred)

{

    ...

    // 文件对应的inode对象

    inode = dentry->d_inode;

    ...

    // 使用inode结构的i_fop字段赋值给file结构的f_op字段

    f->f_op = fops_get(inode->i_fop);

    ...

    return f;

}

Copy after login

总结

本文主要介绍了 虚拟文件系统 的基本原理,从分析中可以发现,虚拟文件系统使用了类似于面向对象编程语言中的接口概念。正是有了 虚拟文件系统,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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

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 stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

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.

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

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.

How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

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 How to use docker desktop Apr 15, 2025 am 11:45 AM

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).

What are the backup methods for GitLab on CentOS What are the backup methods for GitLab on CentOS Apr 14, 2025 pm 05:33 PM

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

How to mount hard disk in centos How to mount hard disk in centos Apr 14, 2025 pm 08:15 PM

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.

What to do after centos stops maintenance What to do after centos stops maintenance Apr 14, 2025 pm 08:48 PM

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.

See all articles