inode节点hash处理

本文介绍了Linux内核中inode哈希表的初始化过程及inode的插入与删除操作。通过详细解析源代码,展示了如何利用哈希算法进行inode管理,为理解Linux文件系统的内部工作原理提供了深入洞见。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

static struct hlist_head *inode_hashtable __read_mostly;
/*
 * Initialize the waitqueues and inode hash table.
 */
void __init inode_init_early(void)
{
	unsigned int loop;


	/* If hashes are distributed across NUMA nodes, defer
	 * hash allocation until vmalloc space is available.
	 */
	if (hashdist)
		return;

	inode_hashtable =
		alloc_large_system_hash("Inode-cache",
					sizeof(struct hlist_head),
					ihash_entries,
					14,
					HASH_EARLY,
					&i_hash_shift,
					&i_hash_mask,
					0,
					0);
	for (loop = 0; loop < (1U << i_hash_shift); loop++)
		INIT_HLIST_HEAD(&inode_hashtable[loop]);
}
static inline void insert_inode_hash(struct inode *inode)
{
	__insert_inode_hash(inode, inode->i_ino);
}

static unsigned long hash(struct super_block *sb, unsigned long hashval)
{
	unsigned long tmp;
	tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
			L1_CACHE_BYTES;
	tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
	return tmp & i_hash_mask;
}
/**
 *	__insert_inode_hash - hash an inode
 *	@inode: unhashed inode
 *	@hashval: unsigned long value used to locate this object in the
 *		inode_hashtable.
 *
 *	Add an inode to the inode hash for this superblock.
 */
void __insert_inode_hash(struct inode *inode, unsigned long hashval)
{
	struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
	spin_lock(&inode_hash_lock);
	spin_lock(&inode->i_lock);
	hlist_add_head(&inode->i_hash, b);
	spin_unlock(&inode->i_lock);
	spin_unlock(&inode_hash_lock);
}
EXPORT_SYMBOL(__insert_inode_hash);

/** * __remove_inode_hash - remove an inode from the hash *@inode: inode to unhash * *
Remove an inode from the superblock. */void__remove_inode_hash(struct inode *inode){spin_lock(&inode_hash_lock);
spin_lock(&inode->i_lock);hlist_del_init(&inode->i_hash);spin_unlock(&inode->i_lock);
spin_unlock(&inode_hash_lock);}EXPORT_SYMBOL(__remove_inode_hash);


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值