目录项对象
dentry结构体表示目录项对象,该对象只存在于内存中,不写在磁盘上,结构体的定义在linux/dcache.h中。包含了其父目录项,目录对应的inode,目录项名称。d_count变量记录了这个目录项对应的文件是否正在被使用,被多少个使用者使用。inode结构体中有一个i_dentry的链表,记录了指向该inode的所有目录项对象,dentry相当于一种缓存,它保存在dcache中,对应的inode对象也会保存在缓存中,在文件读取的过程中,首先查找dcache缓存查看是否有对应的目录项对象缓存,如果命中的话,就不需要重新创建dentry对象,否则就需要创建dentry对象,并创建inode对象。
struct dentry {
/* RCU lookup touched fields */
unsigned int d_flags; /* protected by d_lock */
seqcount_t d_seq; /* per dentry seqlock */
struct hlist_bl_node d_hash; /* lookup hash list */
struct dentry *d_parent; /* parent directory */
struct qstr d_name;
struct inode *d_inode; /* Where the name belongs to - NULL is
* negative */
unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */
/* Ref lookup also touches following */
unsigned int d_count; /* protected by d_lock */
spinlock_t d_lock; /* per dentry lock */
const struct dentry_operations *d_op;
struct super_block *d_sb; /* The root of the dentry tree */
unsigned long d_time; /* used by d_revalidate */
void *d_fsdata; /* fs-specific data */
struct list_head d_lru; /* LRU list */
/*
* d_child and d_rcu can share memory
*/
union {
struct list_head d_child; /* child of parent list */
struct rcu_head d_rcu;
} d_u;
struct list_head d_subdirs; /* our children */
struct list_head d_alias; /* inode alias list */
};
关于dentry的操作,保存在dentry_operations中,
struct dentry_operations {
int (*d_revalidate)(struct dentry *, struct nameidata *);
验证dentry是否有效,默认都是有效的
int (*d_hash)(const struct dentry *, const struct inode *,
struct qstr *);
为目录项生成哈希值
int (*d_compare)(const struct dentry *, const struct inode *,
const struct dentry *, const struct inode *,
unsigned int, const char *, const struct qstr *);
int (*d_delete)(const struct dentry *);
d_count=0时,调用删除方法,释放目录项对象
void (*d_release)(struct dentry *);
void (*d_iput)(struct dentry *, struct inode *);
char *(*d_dname)(struct dentry *, char *, int);
struct vfsmount *(*d_automount)(struct path *);
int (*d_manage)(struct dentry *, bool);
} ____cacheline_aligned;