struct inode *iget_locked(struct super_block *sb, unsigned long ino)用于根据super_block和inode id 找到inode
其源码分析如下:
struct inode *iget_locked(struct super_block *sb, unsigned long ino)
{
struct hlist_head *head = inode_hashtable + hash(sb, ino);
struct inode *inode;
again:
spin_lock(&inode_hash_lock);
#根据sb和ino尝试找inode,所有的inode都是在一个名为inode的list上
inode = find_inode_fast(sb, head, ino);
spin_unlock(&inode_hash_lock);
if (inode) {
#inode 不为null,则说明找到了,然后通过wait_on_inode来等待置位__I_NEW这个标志
wait_on_inode(inode);
if (unlikely(inode_unhashed(inode))) {
iput(inode);
goto again;
}
#找到inode的话,这里就直接返回给用户了
return inode;
}
#如果没有找到,则新申请一个inode
inode = alloc_inode(sb);
if (inode) {
struct inode *old;
spin_lock(&inode_hash_lock);
/* We released the lock, so.. */
#再次根据sb和ino 尝试第二次查找inode
old = find_inode_fast(sb, head, ino);
if (!old) {
#如果还是找不到,则给新申请的inode 赋值后返回给用户
inode->i_ino = ino;
spin_lock(&inode->i_lock);
inode->i_state = I_NEW;
hlist_add_head(&inode->i_hash, head);
spin_unlock(&inode->i_lock);
inode_sb_list_add(inode);
spin_unlock(&inode_hash_lock);
/* Return the locked inode with I_NEW set, the
* caller is responsible for filling in the contents
*/
#所以这里返回新申请的inode给用户
return inode;
}
/*
* Uhhuh, somebody else created the same inode under
* us. Use the old inode instead of the one we just
* allocated.
*/
#走到这里说明根据sb和ino再第二次查找inode的时候找到了,所以要将新申请的inode 释放掉
spin_unlock(&inode_hash_lock);
destroy_inode(inode);
inode = old;
wait_on_inode(inode);
if (unlikely(inode_unhashed(inode))) {
iput(inode);
goto again;
}
}
#这里就是第二次找到inode
return inode;
}