VFS四大對象之三 struct dentry


繼上一篇文章介紹了inode結構體:繼續介紹目錄項dentry:

http://www.cnblogs.com/linhaostudy/p/7427794.html

 

三、dentry結構體

目錄項:目錄項是描述文件的邏輯屬性,只存在於內存中,並沒有實際對應的磁盤上的描述,更確切的說是存在於內存的目錄項緩存,為了提高查找性能而設計。注意不管是文件夾還是最終的文件,都是屬於目錄項,所有的目錄項在一起構成一顆龐大的目錄樹。例如:open一個文件/home/xxx/yyy.txt,那么/、home、xxx、yyy.txt都是一個目錄項,VFS在查找的時候,根據一層一層的目錄項找到對應的每個目錄項的inode,那么沿着目錄項進行操作就可以找到最終的文件。
注意:目錄也是一種文件(所以也存在對應的inode)。打開目錄,實際上就是打開目錄文件。

 1 struct dentry {
 2     /* RCU lookup touched fields */
 3     unsigned int d_flags;        /* protected by d_lock */
 4     seqcount_t d_seq;        /* per dentry seqlock */
 5     struct hlist_bl_node d_hash;    /* lookup hash list */
 6     struct dentry *d_parent;    /* parent directory */
 7     struct qstr d_name;
 8     struct inode *d_inode;        /* Where the name belongs to - NULL is
 9                      * negative */
10     unsigned char d_iname[DNAME_INLINE_LEN];    /* small names */
11 
12     /* Ref lookup also touches following */
13     unsigned int d_count;        /* protected by d_lock */
14     spinlock_t d_lock;        /* per dentry lock */
15     const struct dentry_operations *d_op;
16     struct super_block *d_sb;    /* The root of the dentry tree */
17     unsigned long d_time;        /* used by d_revalidate */
18     void *d_fsdata;            /* fs-specific data */
19 
20     struct list_head d_lru;        /* LRU list */
21     /*
22      * d_child and d_rcu can share memory
23      */
24     union {
25         struct list_head d_child;    /* child of parent list */
26          struct rcu_head d_rcu;
27     } d_u;
28     struct list_head d_subdirs;    /* our children */
29     struct list_head d_alias;    /* inode alias list */
30 };

 解釋一些字段:
d_count:引用計數

d_flags:目錄項緩存標識,可取DCACHE_UNUSED、DCACHE_REFERENCED等

d_inode:與該目錄項關聯的inode

d_parent:父目錄的目錄項

d_hash:內核使用dentry_hashtable對dentry進行管理,dentry_hashtable是由list_head組成的鏈表,一個dentry創建之后,就通過

d_hash鏈接進入對應的hash值的鏈表中。

d_lru:最近未使用的目錄項的鏈表

d_child:目錄項通過這個加入到父目錄的d_subdirs中

d_subdirs:本目錄的所有孩子目錄鏈表頭

d_alias:一個有效的dentry必然與一個inode關聯,但是一個inode可以對應多個dentry,因為一個文件可以被鏈接到其他文件,所以,這個dentry就是通過這個字段鏈接到屬於自己的inode結構中的i_dentry鏈表中的。(inode中講過)

d_mounted:安裝在該目錄的文件系統的數量!注意一個文件目錄下可以有不同的文件系統!

d_name:目錄項名稱

d_time:重新變為有效的時間!注意只要操作成功這個dentry就是有效的,否則無效。

d_op:目錄項操作

d_sb:這個目錄項所屬的文件系統的超級塊

d_vfs_flags:一些標志

d_fsdata:文件系統私有數據

d_iname:存放短的文件名


一些解釋:一個有效的dentry結構必定有一個inode結構,這是因為一個目錄項要么代表着一個文件,要么代表着一個目錄,而目錄實際上也是文件。所以,只要dentry結構是有效的,則其指針d_inode必定指向一個inode結構。但是inode卻可以對應多個

dentry,上面已經說過兩次了。

注意:整個結構其實就是一棵樹。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM