Skip to content

Commit 6967399

Browse files
loemrawkdave
authored andcommitted
btrfs: push cleanup into btrfs_read_locked_inode()
Move btrfs_add_inode_to_root() so it can be called from btrfs_read_locked_inode(), no changes were made to the function. Move cleanup code from btrfs_iget_path() to btrfs_read_locked_inode. This improves readability and improves a leaky abstraction. Previously btrfs_iget_path() had to handle a positive error case as a result of a call to btrfs_search_slot(), but it makes more sense to handle this closer to the source of the call. Signed-off-by: Leo Martins <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent df3b8ca commit 6967399

File tree

1 file changed

+53
-48
lines changed

1 file changed

+53
-48
lines changed

fs/btrfs/inode.c

Lines changed: 53 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3759,8 +3759,41 @@ static int btrfs_init_file_extent_tree(struct btrfs_inode *inode)
37593759
return 0;
37603760
}
37613761

3762+
static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc)
3763+
{
3764+
struct btrfs_root *root = inode->root;
3765+
struct btrfs_inode *existing;
3766+
const u64 ino = btrfs_ino(inode);
3767+
int ret;
3768+
3769+
if (inode_unhashed(&inode->vfs_inode))
3770+
return 0;
3771+
3772+
if (prealloc) {
3773+
ret = xa_reserve(&root->inodes, ino, GFP_NOFS);
3774+
if (ret)
3775+
return ret;
3776+
}
3777+
3778+
existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC);
3779+
3780+
if (xa_is_err(existing)) {
3781+
ret = xa_err(existing);
3782+
ASSERT(ret != -EINVAL);
3783+
ASSERT(ret != -ENOMEM);
3784+
return ret;
3785+
} else if (existing) {
3786+
WARN_ON(!(existing->vfs_inode.i_state & (I_WILL_FREE | I_FREEING)));
3787+
}
3788+
3789+
return 0;
3790+
}
3791+
37623792
/*
3763-
* read an inode from the btree into the in-memory inode
3793+
* Read a locked inode from the btree into the in-memory inode and add it to
3794+
* its root list/tree.
3795+
*
3796+
* On failure clean up the inode.
37643797
*/
37653798
static int btrfs_read_locked_inode(struct inode *inode,
37663799
struct btrfs_path *in_path)
@@ -3780,7 +3813,7 @@ static int btrfs_read_locked_inode(struct inode *inode,
37803813

37813814
ret = btrfs_init_file_extent_tree(BTRFS_I(inode));
37823815
if (ret)
3783-
return ret;
3816+
goto out;
37843817

37853818
ret = btrfs_fill_inode(inode, &rdev);
37863819
if (!ret)
@@ -3789,7 +3822,7 @@ static int btrfs_read_locked_inode(struct inode *inode,
37893822
if (!path) {
37903823
path = btrfs_alloc_path();
37913824
if (!path)
3792-
return -ENOMEM;
3825+
goto out;
37933826
}
37943827

37953828
btrfs_get_inode_key(BTRFS_I(inode), &location);
@@ -3798,7 +3831,13 @@ static int btrfs_read_locked_inode(struct inode *inode,
37983831
if (ret) {
37993832
if (path != in_path)
38003833
btrfs_free_path(path);
3801-
return ret;
3834+
/*
3835+
* ret > 0 can come from btrfs_search_slot called by
3836+
* btrfs_lookup_inode(), this means the inode was not found.
3837+
*/
3838+
if (ret > 0)
3839+
ret = -ENOENT;
3840+
goto out;
38023841
}
38033842

38043843
leaf = path->nodes[0];
@@ -3961,7 +4000,15 @@ static int btrfs_read_locked_inode(struct inode *inode,
39614000
}
39624001

39634002
btrfs_sync_inode_flags_to_i_flags(inode);
4003+
4004+
ret = btrfs_add_inode_to_root(BTRFS_I(inode), true);
4005+
if (ret)
4006+
goto out;
4007+
39644008
return 0;
4009+
out:
4010+
iget_failed(inode);
4011+
return ret;
39654012
}
39664013

39674014
/*
@@ -5470,35 +5517,7 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
54705517
return err;
54715518
}
54725519

5473-
static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc)
5474-
{
5475-
struct btrfs_root *root = inode->root;
5476-
struct btrfs_inode *existing;
5477-
const u64 ino = btrfs_ino(inode);
5478-
int ret;
54795520

5480-
if (inode_unhashed(&inode->vfs_inode))
5481-
return 0;
5482-
5483-
if (prealloc) {
5484-
ret = xa_reserve(&root->inodes, ino, GFP_NOFS);
5485-
if (ret)
5486-
return ret;
5487-
}
5488-
5489-
existing = xa_store(&root->inodes, ino, inode, GFP_ATOMIC);
5490-
5491-
if (xa_is_err(existing)) {
5492-
ret = xa_err(existing);
5493-
ASSERT(ret != -EINVAL);
5494-
ASSERT(ret != -ENOMEM);
5495-
return ret;
5496-
} else if (existing) {
5497-
WARN_ON(!(existing->vfs_inode.i_state & (I_WILL_FREE | I_FREEING)));
5498-
}
5499-
5500-
return 0;
5501-
}
55025521

55035522
static void btrfs_del_inode_from_root(struct btrfs_inode *inode)
55045523
{
@@ -5579,25 +5598,11 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
55795598
return inode;
55805599

55815600
ret = btrfs_read_locked_inode(inode, path);
5582-
/*
5583-
* ret > 0 can come from btrfs_search_slot called by
5584-
* btrfs_read_locked_inode(), this means the inode item was not found.
5585-
*/
5586-
if (ret > 0)
5587-
ret = -ENOENT;
5588-
if (ret < 0)
5589-
goto error;
5590-
5591-
ret = btrfs_add_inode_to_root(BTRFS_I(inode), true);
5592-
if (ret < 0)
5593-
goto error;
5601+
if (ret)
5602+
return ERR_PTR(ret);
55945603

55955604
unlock_new_inode(inode);
5596-
55975605
return inode;
5598-
error:
5599-
iget_failed(inode);
5600-
return ERR_PTR(ret);
56015606
}
56025607

56035608
struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)

0 commit comments

Comments
 (0)