虛擬文件系統[4]

 

     VFS把目錄當作文件對待,所以在路徑/bin/vi中,bin和vi都屬於文件---bin是特殊的目錄文件而vi是一個普通文件,路徑中的每個組成部分都由一個索引節點對象表示。雖然它們可以統一由索引節點表示,但是VFS經常需要執行目錄相關的操作。比如路徑名查找等。路徑名查找需要解析路徑中的每一個組成部分,不但要確保它有效,還要進一步尋址路徑中的下一個部分。爲了方便查找操作,VFS引入了目錄項的概念。而每個dentry代表路徑中的一個特定部分。必須明確一點:在路徑中,包括普通文件在內,每一個部分都是目錄項對象。目錄項也可包括安裝點。在路徑/mnt/cdrom/foo中,/、mnt、cdrom和foo都屬於目錄項對象。

    目錄項對象由dentry結構體表示:

  1. 在<Dcache.h(include/linux)>中
  2. struct dentry {
  3.     atomic_t d_count;
  4.     unsigned int d_flags;       /* protected by d_lock */
  5.     spinlock_t d_lock;      /* per dentry lock */
  6.     struct inode *d_inode;      /* Where the name belongs to - NULL is
  7.                      * negative */
  8.     /*
  9.      * The next three fields are touched by __d_lookup.  Place them here
  10.      * so they all fit in a cache line.
  11.      */
  12.     struct hlist_node d_hash;   /* lookup hash list */
  13.     struct dentry *d_parent;    /* parent directory */
  14.     struct qstr d_name;
  15.     struct list_head d_lru;     /* LRU list */
  16.     /*
  17.      * d_child and d_rcu can share memory
  18.      */
  19.     union {
  20.         struct list_head d_child;   /* child of parent list */
  21.         struct rcu_head d_rcu;
  22.     } d_u;
  23.     struct list_head d_subdirs; /* our children */
  24.     struct list_head d_alias;   /* inode alias list */
  25.     unsigned long d_time;       /* used by d_revalidate */
  26.     struct dentry_operations *d_op;
  27.     struct super_block *d_sb;   /* The root of the dentry tree */
  28.     void *d_fsdata;         /* fs-specific data */
  29. #ifdef CONFIG_PROFILING
  30.     struct dcookie_struct *d_cookie; /* cookie, if any */
  31. #endif
  32.     int d_mounted;
  33.     unsigned char d_iname[DNAME_INLINE_LEN_MIN];    /* small names */
  34. };

   不同於super_block和inode兩個對象,目錄項對象沒有對應的磁盤數據結構,VFS根據字符串形式的路徑名現場創建它。而且由於目錄項對象並非真正保存在磁盤上,所以目錄項結構體沒有是否被修改的標誌。

  • 目錄項狀態

   目錄項對象有三種有效狀態:被使用,未被使用和負狀態。

   一個被使用的目錄項對應一個有效的索引節點並且表明該對象存在一個或多個使用者。一個目錄項處於被使用狀態,意味着它正被VFS使用並且指向有效的索引節點,因此不能被丟棄。

   一個未被使用的目錄項對應一個有效的索引節點,但是應該指明VFS當前並未使用它。該目錄項對象仍然指向一個有效對象,而且被保存到緩存中以便需要時再使用它。由於該目錄項不會過早的被銷燬,所以在以後再需要用到它時,不必重新創建,從而使路徑查找更迅速。如果要收回內存,可以銷燬未使用的目錄項。

   一個負狀態的目錄項沒有對應的有效索引節點,因爲索引節點已被刪除,或路徑不再正確了,但是目錄項仍然保留,以便快速解析以後的路徑查詢。雖然負狀態的目錄項有些用處,但是如果需要的話,可以銷燬它。目錄項對象釋放後也可以保存到slab對象緩存中去。

  •    目錄項緩存

   如果VFS層遍歷路徑名中所有的元素並將它們逐個地解析成目錄項對象,效率很低,所以內核將目錄項對象緩存在目錄項緩存(簡稱dcache)中。

   目錄項緩存包括三個部分:

1. “被使用的”目錄項鍊表。該鏈表通過索引節點對象折哦你哦個的i_dentry項連接相關的索引節點,因爲一個給定的索引節點可能有多個鏈接,所以就可能有多個目錄項對象,因此用一個鏈表來連接它們。

2. “最近被使用的”雙向鏈表。該鏈表包含有未被使用的和負狀態的目錄項對象。由於該鏈以時間順序插入,所以鏈頭的的節點是最新數據。當內核必須通過刪除節點項回收內存時,會從鏈表刪除節點項,因爲尾部的節點最舊,它們在近期內再次被使用的可能性最小。

3. 散列表和相應的散列函數用來快速地將給定路徑解析爲相關目錄項對象。

   散列表由dentry_hashtable表示,其中每一個元素都是一個指向具有相同鍵值的目錄項對象鏈表的指針。數組的大小取決於系統中物理內存的大小。

 

  1. 在<Dcache.c(fs)>中
  2. static struct hlist_head *dentry_hashtable __read_mostly;

   實際的散列值有d_hash()函數技術,它是內核提供給文件系統的唯一的一個散列函數。

  1. 在<Dcache.c(fs)>中
  2. static inline struct hlist_head *d_hash(struct dentry *parent,
  3.                     unsigned long hash)
  4. {
  5.     hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
  6.     hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
  7.     return dentry_hashtable + (hash & D_HASHMASK);
  8. }

   查找散列表要通過d_lookup()函數計算,如果該函數在dcache中發現了與其相匹配的目錄項對象,則匹配的對象被返回;否則返回NULL。

  1. /**
  2.  * d_lookup - search for a dentry
  3.  * @parent: parent dentry
  4.  * @name: qstr of name we wish to find
  5.  *
  6.  * Searches the children of the parent dentry for the name in question. If
  7.  * the dentry is found its reference count is incremented and the dentry
  8.  * is returned. The caller must use d_put to free the entry when it has
  9.  * finished using it. %NULL is returned on failure.
  10.  *
  11.  * __d_lookup is dcache_lock free. The hash list is protected using RCU.
  12.  * Memory barriers are used while updating and doing lockless traversal. 
  13.  * To avoid races with d_move while rename is happening, d_lock is used.
  14.  *
  15.  * Overflows in memcmp(), while d_move, are avoided by keeping the length
  16.  * and name pointer in one structure pointed by d_qstr.
  17.  *
  18.  * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
  19.  * lookup is going on.
  20.  *
  21.  * dentry_unused list is not updated even if lookup finds the required dentry
  22.  * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
  23.  * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
  24.  * acquisition.
  25.  *
  26.  * d_lookup() is protected against the concurrent renames in some unrelated
  27.  * directory using the seqlockt_t rename_lock.
  28.  */
  29. struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
  30. {
  31.     struct dentry * dentry = NULL;
  32.     unsigned long seq;
  33.         do {
  34.                 seq = read_seqbegin(&rename_lock);
  35.                 dentry = __d_lookup(parent, name);
  36.                 if (dentry)
  37.             break;
  38.     } while (read_seqretry(&rename_lock, seq));
  39.     return dentry;
  40. }
  41. struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
  42. {
  43.     unsigned int len = name->len;
  44.     unsigned int hash = name->hash;
  45.     const unsigned char *str = name->name;
  46.     struct hlist_head *head = d_hash(parent,hash);
  47.     struct dentry *found = NULL;
  48.     struct hlist_node *node;
  49.     struct dentry *dentry;
  50.     rcu_read_lock();
  51.     
  52.     hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
  53.         struct qstr *qstr;
  54.         if (dentry->d_name.hash != hash)
  55.             continue;
  56.         if (dentry->d_parent != parent)
  57.             continue;
  58.         spin_lock(&dentry->d_lock);
  59.         /*
  60.          * Recheck the dentry after taking the lock - d_move may have
  61.          * changed things.  Don't bother checking the hash because we're
  62.          * about to compare the whole name anyway.
  63.          */
  64.         if (dentry->d_parent != parent)
  65.             goto next;
  66.         /*
  67.          * It is safe to compare names since d_move() cannot
  68.          * change the qstr (protected by d_lock).
  69.          */
  70.         qstr = &dentry->d_name;
  71.         if (parent->d_op && parent->d_op->d_compare) {
  72.             if (parent->d_op->d_compare(parent, qstr, name))
  73.                 goto next;
  74.         } else {
  75.             if (qstr->len != len)
  76.                 goto next;
  77.             if (memcmp(qstr->name, str, len))
  78.                 goto next;
  79.         }
  80.         if (!d_unhashed(dentry)) {
  81.             atomic_inc(&dentry->d_count);
  82.             found = dentry;
  83.         }
  84.         spin_unlock(&dentry->d_lock);
  85.         break;
  86. next:
  87.         spin_unlock(&dentry->d_lock);
  88.     }
  89.     rcu_read_unlock();
  90.     return found;
  91. }

   而dcache在一定意義上也提供對索引節點的緩存。和目錄項對象相關的索引節點對象不會被釋放,因爲目錄項會讓相關索引節點的使用計數爲正,這樣就可以確保索引節點留在內存中。主要目錄項被緩存,其相應的索引節點也就被緩存了。

目錄項操作

    dentry_operation結構體指明瞭VFS操作目錄項的所有方法:

  1. 在<Dcache.h(include/linux)>中
  2. struct dentry_operations {
  3. /*該函數判斷目錄對象是否郵箱。VFS準備從dcache中使用一個目錄項時,會調用該函數。大部分文件系統將該方法設置爲NULL,因爲它們認爲dcache中的目錄項對象總是有效的*/
  4.     int (*d_revalidate)(struct dentry *, struct nameidata *);
  5. /*該函數爲目錄項生成散列值,當目錄項需要加入到散列表中時,VFS調用該函數*/
  6.     int (*d_hash) (struct dentry *, struct qstr *);
  7. /*VFS調用該函數來比較兩個文件名。注意,使用該函數時要加dcache_lock鎖*/
  8.     int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
  9. /*當目錄項對象的d_count計數值等於零時,VFS調用該函數。注意,使用該函數時要加dcache_lock鎖*/
  10.     int (*d_delete)(struct dentry *);
  11. /* 當目錄項對象將要被釋放時,VFS調用該函數,默認情況下,它什麼也不做 */
  12.     void (*d_release)(struct dentry *);
  13. /* 當一個目錄項對象丟失了其相關的索引節點時(也就是說磁盤索引節點被刪除了),VFS調用該函數。默認情況下VFS會調用iput()函數釋放索引節點。如果文件系統重載了該函數,那麼除了執行文件系統特殊的工作外,還必須調用iput()函數 */
  14.     void (*d_iput)(struct dentry *, struct inode *);
  15. };

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章