当前位置:网站首页>[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing

[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing

2022-06-26 15:29:00 bandaoyu

brief

recommend :《VFS、 Superblock 、inode、dentry、file》https://www.itdaan.com/blog/2014/08/31/f99ae6e6e574449076a385cd07f0957b.html

https://www.itdaan.com/blog/2014/08/31/b60d5d86d0e61607dc9f1becfe0f5101.html

Linux Four objects of virtual file system :

1) Superblock (super block)

2) The index node (inode)

3) Catalog items (dentry)

4) File object (file)

The index node inode: Save some information about the actual data , This information is called “ Metadata ”( That is, the description of file attributes ). for example : file size , Device identifier , User identifier , User group identifier , File mode , Extended attributes .

Catalog items dentry: A directory entry is a logical property that describes a file (dentry Contains the file name , Of documents inode Number and other information .), Only in memory , More specifically, the cache of directory entries in memory , Designed to improve search performance . Notice whether it's the folder or the final file , All of them belong to catalog items , All the entries together form a huge directory tree . for example :open A file /home/xxx/yyy.txt, that /、home、xxx、yyy.txt It's all a catalog entry ,VFS When searching , According to the level by level directory entries, find the corresponding... Of each directory entry inode, Then follow the directory entry to find the final file .

dentry Structure is a bi-directional structure with pointers to parent and child nodes , Multiple such bidirectional structures form a tree structure in memory , That is, the directory structure of the file system is cached in memory . With this cache , When we access the file system , Usually very fast .

Be careful : A directory is also a kind of file ( So there is a corresponding inode). Open Directory , It's actually opening a directory file .

File object : Note that the file object describes the file that the process has opened . Because a file can be opened by multiple processes , So a file can have multiple file objects . But because the file is unique , that inode It's the only one , Directory entries are also fixed !

A process actually operates a file through a file descriptor , Note that each file has a 32 The number of bits to indicate the position of the next read and write byte , This number is called file location . Generally, after opening a file , All open positions are from 0 Start , Except for some special circumstances .Linux use file Structure to save the location of the open file , therefore file Called the open file description . This needs to be well understood !file The structure forms a double linked list , be called The system opens the file table .

Superblock, Inode, Dentry and File All belong to metadata (Metadata),

Linux System from ext2 Start , Is to store file attributes and file contents separately ,inode Used to store the attributes of the file ,block  Used to store the contents of a file .inode Point to block( At least one ).

Super block Super block , It is the beginning of the hard disk partition , The data in the superblock is the volume resource table , Most of the information about file volumes is saved here .

 for example : Each in the hard disk partition block Size 、 How many are there on the hard disk partition block group、 And each block group How many of them inode. It defines a file system similar to 、 size 、 state , And other metadata structures ( Metadata of metadata ).

 

 vfs Four objects : Superblock 、inode、dentry、file The relationship between - https://zhuanlan.zhihu.com/p/354100369

detailed

Superblock, Inode, Dentry  and  File  All belong to metadata (Metadata),
1、Inode  and  Block

(1) background

    because Linux System from ext2 Start , It stores file attributes and file contents separately , By inode and block To be responsible for .

(2)inode   Used to store the attributes of the file

    Used to store the attributes of the file , Include :

    -  Owner information : Of documents owner,group;

    -  Permission information :read、write and excite;

    - Time information : Establish or change time (ctime)、 Last read time (atime)、 Last modified (mtime);

    -  Logo information : some flags;

    -  Content information :type,size, And corresponding block Location information for .

    Be careful : Do not record file or directory names , The file name or directory name is recorded in the corresponding file directory block in .

(3)block  Used to store the contents of a file .

(4) Create a directory or file

    When creating a directory , The file system assigns a to the directory inode And at least one block. The inode Record the properties of this directory , And point to that block. The block Record the relevance and name of the associated file or directory under the directory .

    When creating a file , The file system assigns at least one to the file inode And the number corresponding to the file size block. The inode Record the attributes of the file , And point to block.

    If there are too many files in a directory , So much so that 1 individual block When you can't hold so many files ,Linux Your file system will allocate more to this directory block.

   ( various block Form a linked list ?)

(5) Read directories or files

    When we tell the operating system the path of a file , How does the operating system find this file ? First, the operating system will call the corresponding interface of the file system , Next :

    Recursion : When reading a file or directory X when , The file system is provided with a path P. The file system reads first X directory D Of inode_d( Note that there , It is actually a recursive process of this operation ), adopt inode_d Get its corresponding block_d, stay block_d Through known X The name of , Query to X Of inode_x.

     Iterations ; If you read /x1/x2/x3/x4/x5 This file , Read the root directory first inode_root, And then find inode_root Corresponding block_root, stay block_root According to the x1 The name found x1 Corresponding inode_x1, And then find inode_x1 Corresponding block_x1, stay block_x1 According to the x2 The name found inode_x2, And find block_x2, And then according to x3 find inode_x3, Again block_x3, according to x4 find inode_x4, Again block_x4, According to x5 find inode_x5, Again block_x5, Just read what we want x5 Content. .

 Linux The foundation of file system inode and dentry

VFS:Linux There are many file systems in the system ext2,ext3,ext4,sysfs,rootfs,proc.., In order to use them transparently, you need VFS As the middle layer , Users directly communicate with VFS Dealing with , for example read,write.

VFS stay Linux The structure of the system is :

inode Is the metadata of the kernel file object .inode File data and file name information are not included in .inode Only the attribute information of the file object is saved , Include : jurisdiction 、 Generic group 、 The location of the data block 、 Time stamp and other information . But it doesn't contain the file name , The location information of the file in the directory tree of the file system .

How does the kernel manage the directory tree of the file system ?

dentry It connects different file objects in the kernel inode The role of , Then it plays the role of maintaining the directory tree of the file system .dentry Is a pure memory structure , The file system directly establishes in memory in the process of providing file access .dentry Contains the file name , Of documents inode Number and other information .

When reading a file , Always read from the root directory , Every directory or file , stay VFS in , Is a file object , Each file object has a unique inode With the corresponding . Root directory inode Number is 0, stay superblock in , Can be quickly based on inode Index to specific inode, therefore The first read inode Is the root directory . After reading the directory , The kernel object creates a for the file object dentry, And cache it , It is convenient to fetch directly from the memory for the next reading . The directory itself is a file , The contents of the directory file are the name and... Of the files in the directory inode Number , The contents of the directory file are like a table , The file name of the record is the same as inode no. Mapping between . According to the path, you can find the name and... Of the next level file you currently need to read inode, At the same time, continue to create dentry,dentry Structure is a bi-directional structure with pointers to parent and child nodes , Multiple such bidirectional structures form a tree structure in memory , That is, the directory structure of the file system is cached in memory . With this cache , When we access the file system , Usually very fast .

With inode and dentry, It is very easy to understand the connection of files . We know soft connection , It's a special document , This file points to the target file through its contents . So soft connect has its own inode, Have their own content . The contents record the contents of the target file inode Number and your own name . Soft connect is a special kind of file . Hard links are different , Hard links are aliases for files , Hard link is not a complete file object , Hard links are just the contents of the parent directory where you write your name ( File name and inode no. Mapping table ) in . And its inode No. is the... Of the target file inode. In this way, the hard connection shares one with the target file inode, Use reference counting to manage hard connections .

dentry
dentry It's a Memory Entity , Among them d_inode The member points to the corresponding inode

struct dentry {
  atomic_t d_count;
  struct inode * d_inode; // Point to one inode structure . This inode and dentry Common description of a common file or directory file 
  struct dentry * d_parent; // The directory entry object of the parent directory 
  struct list_head d_hash; // link to dentry cache Of hash Linked list .
struct list_head d_lru; // Unused pointer to linked list 
  struct list_head d_child;//dentry Own chain header , Need to link to parent dentry Of d_subdirs member 
  struct list_head d_subdirs;// term ( The subitem may be a directory , It could be a file ) The chain head of , All sub items should be linked to the linked list 
  int d_mounted;//  instructions dentry Is it a mount point . If it is a mount point , The member is not zero .
  struct qstr d_name;//  Members save the name of a file or directory . When opening a file , Search for the target file by comparing this member with the name entered by the user 
...
  };

When moving files , Need to put a dentry Structure from the old parent dentry From the list of , Then link to the new parent dentry Of d_subdirs member . such dentry Structures form a directory tree
Every dentry Each has a pointer to its parent directory (d_parent), A dime dentry Hash list of (d_child). among , Son dentry It is basically the files in the directory

dentry state :
1.d_count=0, not used (unused) state ,d_inode Point to the relevant inode . The directory entry still contains valid information , But at present, no one quotes him . such dentry Objects may be freed when memory is reclaimed .
2.d_count>0, Being used (inuse) state ,d_inode Point to the relevant inode object . such dentry Object cannot be released .
3.d_count<0,y negative (negative) state ,inode The object no longer exists and may be deleted ,dentry Object's d_inode Pointer for NULL. But this dentry The object is still stored in dcache in , So that the subsequent search for the same file name can be completed quickly . such dentry Objects will be freed first when they reclaim memory .

dentry And dentry_cache
  dentry_cache(dcache, Directory entry cache ) It consists of two data structures :
  1、 Hash list dentry_hashtable:dcache All in dentry All objects pass d_hash The pointer field is chained to the corresponding dentry Hash linked list .
  2、 That is not used dentry Object linked list dentry_unused:dcache All in unused State and negative State of dentry Objects all pass through their d_lru Pointer fields are chained into dentry_unused In the list . This linked list is also called LRU Linked list .
   dcache Medium dentry The object controls icache Medium inode Life cycle transformation of objects . No matter when , As long as a directory entry object exists in dcache in ( Not negative state ), The corresponding inode Will always exist , because inode Reference count of i_count Always greater than 0. When dcache One of them dentry When released , For the corresponding inode Object's iput() Method will be called

dentry and inode_ Er Rong You Xia's blog -CSDN Blog _dentry and inode

Linux The biggest routine is “ Everything is a document ”?

inode The source of the ,file Running water

We think of the document as a object, that inode It describes the origin , And finally object One-to-one correspondence ;dentry yes inode A path vest of , For example, we can go through "ln" The order is the same inode Create a lot of hard link vests ; and file It's living water , The process is right object Primary “open”, To obtain a file, Cause the user state to get a "fd" To manipulate this object.

classical inode、dentry、file The model that no one is absent is like this :

Above picture , We have a inode, This inode Yes 2 individual dentry, process A、B open It's the first dentry; And the process C、D open What I want is the second one dentry. What has changed is file and fd, What remains unchanged is inode, In the middle of the dentry The vest is not that important .

But in inode、dentry、file In this classic iron triangle , There can always be an absentee , That's it dentry, because , Sometimes users want to get the convenience of walking inside and outside the Great Wall , But I don't want this inode Leave a trail of path in the file system . Simply speaking , I want to have a fd, But this fd, You are from "/" Search any path below , You can't even find it , It doesn't have a path below the root file system at all , It's anonymous , It doesn't have a vest , It's a legend .

Linux The biggest routine is “ Everything is a document ”?- Electronic enthusiast network




 


 

 https://i.stack.imgur.com/daHCZ.gif

 https://www.apriorit.com/images/articles/hidedriverlinux1.png

原网站

版权声明
本文为[bandaoyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261456117585.html