虚拟文件系统[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. };

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值