dcache.c中的d_hash函数和dentry操作中的d_hash之间有调用关系吗
时间: 2024-05-31 21:07:46 浏览: 85
是的,dcache.c中的d_hash函数计算dentry的哈希值,而dentry操作中的d_hash则是将dentry插入哈希表或从哈希表中删除。在插入或删除dentry时,需要先通过d_hash函数计算出dentry的哈希值,然后根据哈希值将dentry插入或删除相应的哈希桶中。因此,dcache.c中的d_hash函数和dentry操作中的d_hash之间存在调用关系。
相关问题
dcache.c中的d_hash函数和dentry操作中的d_hash之间的调用关系展示一下
d_hash函数是一个在dentry缓存中用于计算哈希值的函数,它将dentry对象的名字作为输入,在哈希表中找到相应的槽位,并返回该槽位的索引。d_hash函数的调用关系如下:
1. 在dentry缓存中创建一个新的dentry对象时,会调用d_hash函数计算其哈希值。
2. 在查找dentry对象时,会先调用d_hash函数计算其哈希值,然后在哈希表中查找相应的槽位。
3. 在向dentry缓存中添加或删除dentry对象时,也会先调用d_hash函数计算其哈希值,然后在哈希表中找到相应的槽位,对相应的dentry对象进行添加或删除操作。
dentry操作中的d_hash函数则是一个宏定义,它是通过调用dentry对象的d_op->d_hash方法实现的。d_op是一个指向dentry_operations结构体的指针,其中包含了dentry对象的操作函数,包括d_hash函数。
因此,dentry操作中的d_hash函数和dcache.c中的d_hash函数是相互关联的,它们通过d_op->d_hash方法实现了相同的功能,即计算dentry对象的哈希值。
dcache.c中的d_hash函数和dentry操作中的d_hash之间的调用路径展示一下
1. 在dcache.c中定义了d_hash函数,该函数用于计算给定dentry的哈希值:
```c
unsigned int d_hash(const struct dentry *dentry, const struct qstr *name)
{
return full_name_hash(name->name, name->len);
}
```
2. 在dentry操作中,需要将dentry插入到dentry缓存中,因此需要调用d_hash函数来计算dentry的哈希值。这个过程发生在d_alloc和d_instantiate函数中。
```c
struct dentry *d_alloc(struct dentry *parent, const struct qstr *name)
{
struct dentry *dentry;
unsigned int len = name->len;
unsigned int hash = d_hash(parent, name); // 调用d_hash计算哈希值
// 分配dentry对象并初始化
dentry = __d_alloc(parent->d_sb, len);
dentry->d_name = *name;
dentry->d_parent = parent;
dentry->d_flags |= DCACHE_UNHASHED;
return dentry;
}
void d_instantiate(struct dentry *entry, struct inode *inode)
{
spin_lock(&dcache_lock);
// 设置dentry的inode和哈希值
entry->d_inode = inode;
entry->d_flags &= ~DCACHE_UNHASHED;
entry->d_hash = inode->i_ino; // inode的编号作为哈希值
// 将dentry插入到哈希表中
d_hash_and_lookup(entry, NULL);
spin_unlock(&dcache_lock);
}
```
3. 在d_instantiate函数中,还调用了d_hash_and_lookup函数来将dentry插入到哈希表中。
```c
static inline void d_hash_and_lookup(struct dentry *entry, struct qstr *str)
{
struct hlist_head *head = dentry_hash(entry->d_sb, entry->d_hash);
spin_lock(&dcache_lock);
// 将dentry插入到哈希表中
hlist_add_head(&entry->d_hash, head);
spin_unlock(&dcache_lock);
}
```
4. d_hash_and_lookup函数中调用了dentry_hash函数来获取哈希表中对应哈希值的链表头。
```c
static inline struct hlist_head *dentry_hash(const struct super_block *sb, unsigned long hashval)
{
return &sb->s_dentry_hashtable[hashval & (sb->s_dentry_hashtable_size - 1)];
}
```
5. 在dentry缓存中查找dentry时,也需要调用d_hash函数来计算dentry的哈希值。这个过程发生在d_lookup函数中。
```c
struct dentry *d_lookup(const struct dentry * parent, const struct qstr * name)
{
struct dentry *dentry;
unsigned int len = name->len;
unsigned int hash = d_hash(parent, name); // 调用d_hash计算哈希值
spin_lock(&dcache_lock);
// 在哈希表中查找dentry
dentry = __d_lookup(parent, name, hash);
spin_unlock(&dcache_lock);
return dentry;
}
```
6. 在__d_lookup函数中,也需要调用dentry_hash函数来获取哈希表中对应哈希值的链表头。
```c
static struct dentry *__d_lookup(const struct dentry * parent, const struct qstr * name, unsigned int hash)
{
struct dentry *dentry;
struct hlist_head *head = dentry_hash(parent->d_sb, hash); // 调用dentry_hash获取哈希表中对应的链表头
// 遍历链表查找dentry
hlist_for_each_entry(dentry, head, d_hash)
{
if (dentry->d_parent == parent && dentry->d_name.hash == hash && dentry_cmp(dentry, name) == 0)
return dentry;
}
return NULL;
}
```
阅读全文