内核文件系统API之new_inode

struct inode *new_inode(struct super_block *sb)用于在形参给定的super_block上申请一个新的inode
其源码分析如下:
struct inode *new_inode(struct super_block *sb)
{
	struct inode *inode;

	spin_lock_prefetch(&sb->s_inode_list_lock);
	#申请一个新的inode节点
	inode = new_inode_pseudo(sb);
	#inode 不为null,说明申请成功,将其加到sb的list中
	if (inode)
		inode_sb_list_add(inode);
	return inode;
}
struct inode *new_inode_pseudo(struct super_block *sb)
{
	#在sb上申请inode
	struct inode *inode = alloc_inode(sb);
	#申请成功后初始化inode的结构成员
	if (inode) {
		spin_lock(&inode->i_lock);
		inode->i_state = 0;
		spin_unlock(&inode->i_lock);
		INIT_LIST_HEAD(&inode->i_sb_list);
	}
	return inode;
}
其中alloc_inode的实现如下L可以看到主要是申请memory,形参sb如果提供申请
memory的话的话,就通过sb提供的函数申请memory,否则通过kmem_cache_alloc申请
static struct inode *alloc_inode(struct super_block *sb)
{
	struct inode *inode;

	if (sb->s_op->alloc_inode)
		inode = sb->s_op->alloc_inode(sb);
	else
		inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);

	if (!inode)
		return NULL;

	if (unlikely(inode_init_always(sb, inode))) {
		if (inode->i_sb->s_op->destroy_inode)
			inode->i_sb->s_op->destroy_inode(inode);
		else
			kmem_cache_free(inode_cachep, inode);
		return NULL;
	}

	return inode;
}

你可能感兴趣的:(Linux,源码分析,kernel常用API源码分析)