歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux/Unix inode、vnode、dentry、file、進程表、文件表(上)

Linux/Unix inode、vnode、dentry、file、進程表、文件表(上)

日期:2017/3/3 13:40:33   编辑:Linux技術
傳統的Unix既有v節點(vnode)也有i節點(inode),vnode的數據結構中包含了inode信息。但在Linux中沒有使用vnode,而使用了通用inode。“實現雖不同,但在概念上是一樣的。”
vnode (“virtual node”)僅在文件打開的時候,才出現的;而inode定位文件在磁盤的位置,它的信息本身是存儲在磁盤等上的,當打開文件的時候從磁盤上讀入內存。



inode信息就存儲在磁盤的某個分區上。下圖是上圖的一個擴展:inode指示了文件在數據塊中的物理位置。所以僅僅存在inode無法描述一個完整的文件系統,比如:目錄與目錄的樹狀結構,這一點在inode上無法體現。
延伸:
如果多個inode指向同一個數據塊的時候,是不是就可以實現熟悉的鏈接了?!這就是軟連接的原理,新建一個文件(一個符號鏈接文件,文件的屬性中有明確說明它是一個符號鏈接文件),為需要鏈接的文件分配一個新的inode,然後指向同一個文件。
多個文件共用一個inode,同樣可以實現鏈接?!這就是硬鏈接的原理,inode中有鏈接計數器,當增加一個文件指向這個inode時,計數器增1。特別的,當計數器為0時候,文件才真正從磁盤刪除。即ls -l 命令中的第二欄



ext3_inode上的數據結構如下:它記錄了很多關於文件的信息,比如文件長度,文件所在的設備,文件的物理位置,創建、修改和更新時間等等,特別的,它不包含文件名!目錄下的所有文件名和目錄名都存儲在目錄的數據塊中,即如上的目錄塊。
01
struct
ext3_inode
 {
02
__le16
 i_mode; File mode
03
__le16
 i_uid; Low 16 bits of Owner Uid
04
__le32
 i_size; Size in bytes
05
__le32
 i_atime; Access
time
06
__le32
 i_ctime; Creation
time
07
__le32
 i_mtime; Modification
time
08
09
__le32
 i_dtime; Deletion Time
10
__le16
 i_gid; Low 16 bits of Group Id
11
__le16
 i_links_count; Links count
12
......
13
__le32
 i_block[EXT2_N_BLOCKS]; Pointers to blocks
14
......
15
};
引入vnode:早期版本的Unix是這樣做的,但是Linux並沒有。vnode一般包含了文件類型和對此文件進行各種操作的函數的指針。


Linux上有dentry,中文的意思就是目錄項,它粘合了內存中文件和磁盤中文件,同時它保存是經常訪問的目錄信息。
http://unix.stackexchange.com/questions/4402/what-is-a-superblock-inode-dentry-and-a-file
A dentry is the glue that holds inodes and files together by relating inode numbers to file names. Dentries also play a role in directory caching which, ideally, keeps the most frequently used files on-hand for faster access. File system traversal is another
aspect of the dentry as it maintains a relationship between directories and their files.下面是一副很有趣的圖片:
Interaction between processes and VFS objects
The common file model consists of the following object types:
<1>The
superblock object
Stores information concerning a mounted filesystem. For disk-based filesystems, this object usually corresponds to a filesystem control block stored
on disk.
<2>The
inode object
Stores general information about a specific file. For disk-based filesystems, this object usually corresponds to a file control block stored on disk.
Each inode object is associated with an inode number, which uniquely identifies the file within the filesystem.
<3>The
file object
Stores information about the interaction between an open file and a process. This information exists only in kernel memory during the period when a process
has the file open.
<4>The
dentry object
Stores information about the linking of a directory entry (that is, a particular name of the file) with the corresponding file. Each disk-based filesystem
stores this information in its own particular way on disk.
Figure 12-2 illustrates with a simple example how processes interact with files. Three different processes have opened the same file, two of them using
the same hard link. In this case, each of the three processes uses its own file object, while only two dentry objects are requiredone for each hard link. Both dentry objects refer to the same inode object, which identifies the superblock object and, together
with the latter, the common disk file.



需要注意的幾點如下所示:
1)進程每打開一個文件,就會有一個file結構與之對應。同一個進程可以多次打開同一個文件而得到多個不同的file結構,file結構描述被打開文件的屬性,如文件的當前偏移量等信息。
2)兩個不同的file結構可以對應同一個dentry結構。進程多次打開同一個文件時,對應的只有一個dentry結構。Dentry結構存儲目錄項和對應文件(inode)的信息。
3)在存儲介質中,每個文件對應唯一的inode結點,但是每個文件又可以有多個文件名。即可以通過不同的文件名訪問同一個文件。這裡多個文件名對應一個文件的關系在數據結構中表示就是dentry和inode的關系。
4)Inode中不存儲文件的名字,它只存儲節點號;而dentry則保存有名字和與其對應的節點號,所以就可以通過不同的dentry訪問同一個inode。
5)不同的dentry則是同個文件鏈接(ln命令)來實現的。
01
struct
dentry
 {
02
atomic_t
 d_count; 目錄項對象使用計數器
03
unsigned
int
d_flags;
 目錄項標志
04
struct
inode
 * d_inode; 與文件名關聯的索引節點
05
struct
dentry
 * d_parent; 父目錄的目錄項對象
06
struct
list_head
 d_hash; 散列表表項的指針
07
struct
list_head
 d_lru; 未使用鏈表的指針
08
struct
list_head
 d_child; 父目錄中目錄項對象的鏈表的指針
09
struct
list_head
 d_subdirs;對目錄而言,表示子目錄目錄項對象的鏈表
10
struct
list_head
 d_alias; 相關索引節點(別名)的鏈表
11
int
d_mounted;
 對於安裝點而言,表示被安裝文件系統根項
12
struct
qstr
 d_name; 文件名
13
unsigned
long
d_time;
//
 used by d_revalidate
14
struct
dentry_operations
 *d_op; 目錄項方法
15
struct
super_block
 * d_sb; 文件的超級塊對象
16
vunsigned
long
d_vfs_flags;
17
void
*
 d_fsdata;與文件系統相關的數據
18
unsigned
char
d_iname
 [DNAME_INLINE_LEN]; 存放短文件名
19
};
諸如文件名,父目錄等。dentry可以描述目錄的樹狀結構。
http://daoluan.net/blog/inode、vnode和dentry/
Copyright © Linux教程網 All Rights Reserved