Chinaunix首页 | 论坛 | 博客
  • 博客访问: 616156
  • 博文数量: 120
  • 博客积分: 2284
  • 博客等级: 大尉
  • 技术积分: 1330
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-25 10:49
个人简介

http://guliqun1983.blog.163.com/blog/static/501116852011730535314/

文章分类
文章存档

2013年(23)

2012年(23)

2011年(74)

分类: LINUX

2013-04-23 11:19:59

dentry - 术语的定义

 
   的中文名称是,是文件系统中某个索引(inode)的链接。这个索引节点可以是文件,也可以是目录。

dentry - 目录项的结构

以下是dentry的结构体
struct dentry {
     atomic_t d_count; 目录项使用

     unsigned int d_flags; 目录项
     struct inode * d_inode; 与文件名关联的索引节点
     struct dentry * d_parent; 父目录的目录项对象
     struct list_head d_hash; 散列表表项的指针
     struct list_head d_lru; 未使用链表的

     struct list_head d_child; 父目录中目录项对象的链表的指针
     struct list_head d_subdirs;对目录而言,表示子目录目录项对象的链表
     struct list_head d_alias; 相关索引节点(别名)的链表
     int d_mounted; 对于安装点而言,表示被安装文件系统根项
     struct qstr d_name; 文件名
     unsigned long d_time; /* used by d_revalidate */
     struct dentry_operations *d_op; 目录项
     struct super_block * d_sb; 文件的超级块对象
     vunsigned long d_vfs_flags;
     void * d_fsdata;与文件系统相关的数据
     unsigned char d_iname [DNAME_INLINE_LEN]; 存放短文件名
};

dentry - 详细资料

 inode(可理解为ext2 inode)对应于物理磁盘上的具体对象,dentry是一个内存实体,其中的d_inode成员指向对应的inode。也就是说,一个inode可以在运行的时候链接多个dentry,而d_count记录了这个链接的数量。
    按照d_count的值,dentry分为以下三种状态:
    1、未使用(unused)状态:该dentry对象的引用计数d_count的值为0,但其d_inode指针仍然指向相关的的索引节点。该目录项仍然包含有效的信息,只是当前没有人引用他。这种dentry对象在回收内存时可能会被释放。
  2、正在使用(inuse)状态:处于该状态下的dentry对象的引用计数d_count大于0,且其d_inode指向相关的inode对象。这种dentry对象不能被释放。
  3、负(negative)状态:与目录项相关的inode对象不复存在(相应的磁盘索引节点可能已经被删除),dentry对象的d_inode指针为NULL。但这种dentry对象仍然保存在dcache中,以便后续对同一文件名的查找能够快速完成。这种dentry对象在回收内存时将首先被释放。

四、dentry与dentry_cache
    dentry_cache简称dcache,中文名称是目录项高速缓存,是Linux为了提高目录项对象的处理效率而设计的。它主要由两个数据结构组成:
    1、哈希链表dentry_hashtable:dcache中的所有dentry对象都通过d_hash指针域链到相应的dentry哈希链表中。
  2、未使用的dentry对象链表dentry_unused:dcache中所有处于unused状态和negative状态的dentry对象都通过其d_lru指针域链入dentry_unused链表中。该链表也称为LRU链表。
  目录项高速缓存dcache是索引节点缓存icache的主控器(master),也即dcache中的dentry对象控制着icache中的inode对象的生命期转换。无论何时,只要一个目录项对象存在于dcache中(非negative状态),则相应的inode就将总是存在,因为 inode的引用计数i_count总是大于0。当dcache中的一个dentry被释放时,针对相应inode对象的iput()方法就会被调用。

五、dentry_operations *d_op
struct dentry_operations {
     int (*d_revalidate)(struct dentry *);
     int (*d_hash) (struct dentry *, struct qstr *);
     int (*d_compare) (struct dentry *, struct qstr *, struct qstr *);
     void (*d_delete)(struct dentry *);
     void (*d_release)(struct dentry *);
     void (*d_iput)(struct dentry *, struct inode *);
};

d_revalidate:用于使一个dentry重新生效。
d_hash:用于VFS向哈希表中加入一个dentry。
d_compare:dentry的最后一个inode被释放时(d_count等于零),此方法被调用,因为这意味这没有inode再使用此dentry;当然,此dentry仍然有效,并且仍然在dcache中。
d_release: 用于清除一个dentry。
d_iput:用于一个dentry释放它的inode(d_count不等于零)

六、d_parent和d_child
    每个dentry都有一个指向其父目录的指针(d_parent),一个子dentry的哈希列表(d_child)。其中,子dentry基本上就是目录中的文件。

七、怎样从inode值得到?
    函数得到当前文件或目录的inode值后,进入dcache查找对应的dentry,然后顺着父目录指针d_parent得到父目录的dentry,这样逐级向上直到dentry= root,就得到全部目录名称。

阅读(3388) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~