Linux kernel device driver virtual file system note collation

  • 2020-12-21 18:18:41
  • OfStack


/********************
 *  Virtual file system VFS
 ********************/

(1) VFS is introduced

As a subsystem of the kernel, virtual file system VFS provides a file system interface for user-space programs.

VFS enables users to use system calls such as open() directly without regard to the specific file system and actual physical media.

VFS provides a generic file system model that encapsulates the common functionality and behavior of a file system as we might expect. This abstraction layer enables the implementation to manipulate all the new file systems of classes using a common interface.

a. Invoke the model

write(): User space -- >

sys_write(): VFS -- >

File system write method: file system - >

Physical media

(2) The main object adopted by VFS

VFS adopts an object-oriented approach and uses a set of data structures to represent common file objects.

These structures contain data as well as Pointers to manipulate it.

There are four main object types included in VFS.

a. Super block object super_block

All file systems must implement a super block, an object that stores information about a particular file system, usually in a particular sector of the disk, with only one super block per file system.

For non-disk-based file systems, such as the memory-based file system sysfs, linux creates super blocks in the field and stores them in memory.

The structure of the super block is super_block, defined in < linux/fs.h > .

The operation method structure of the super block is super_operations, also defined in ES62en.h.

The code to create, manage, and destroy super block objects is located at /fs/ super.c.

At file system installation, the kernel calls the alloc_super() function to read the file system super block from disk and populate its information into the super block object in memory.

inode. Index node object inode

An INOde object contains all the information the kernel needs to manipulate a file or directory, such as the file's access control permissions, size, owner, creation time, and so on.

The system stores this information in a separate data structure called an inindex node.

A file has only 1 INodes in memory, and special files (such as pipe and device files) have their inodes as well.

The inode structure is defined in < linux/fs.h > , the corresponding operation function structure is inode_operations

c. Directory entry object dentry

Each directory entry object represents a specific part of a path, such as path /bin/vi, /, bin, and vi, which belong to the directory entry object.

The directory entry object has no corresponding disk structure, and VFS is created live from a pathname in string form. There are only 1 dentry object per file.

The dentry structure is defined in < linux/dcache.h > , the corresponding directory entry manipulation function structure dentry_operations is also defined in < linux/dcache.h > In the.

d. File object file

The file object represents the file that the process has opened. This object is created for open and destroyed for close.

Because multiple processes can open and manipulate a file at the same time, a file may have multiple file objects in memory.

File objects are represented by the file structure, defined in < linux/fs.h > In the. The operation function structure of the file object is file_operations, defined in < linux/fs.h > In the.

This set of functions is very important and includes the actual manipulation functions on the file, which call write from user space and eventually call write in file_operations.

We want to implement a character device of type char, that is, we want to implement the functions supported in file_operations.

conclusion


Related articles: