Skip to content

Commit 7c855e1

Browse files
loemrawkdave
authored andcommitted
btrfs: remove conditional path allocation in btrfs_read_locked_inode()
Remove conditional path allocation from btrfs_read_locked_inode(). Add an ASSERT(path) to indicate it should never be called with a NULL path. Call btrfs_read_locked_inode() directly from btrfs_iget(). This causes code duplication between btrfs_iget() and btrfs_iget_path(), but I think this is justifiable as it removes the need for conditionally allocating the path inside of btrfs_read_locked_inode(). This makes the code easier to reason about and makes it clear who has the responsibility of allocating and freeing the path. Signed-off-by: Leo Martins <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 6967399 commit 7c855e1

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

fs/btrfs/inode.c

+29-17
Original file line numberDiff line numberDiff line change
@@ -3795,11 +3795,9 @@ static int btrfs_add_inode_to_root(struct btrfs_inode *inode, bool prealloc)
37953795
*
37963796
* On failure clean up the inode.
37973797
*/
3798-
static int btrfs_read_locked_inode(struct inode *inode,
3799-
struct btrfs_path *in_path)
3798+
static int btrfs_read_locked_inode(struct inode *inode, struct btrfs_path *path)
38003799
{
38013800
struct btrfs_fs_info *fs_info = inode_to_fs_info(inode);
3802-
struct btrfs_path *path = in_path;
38033801
struct extent_buffer *leaf;
38043802
struct btrfs_inode_item *inode_item;
38053803
struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -3819,18 +3817,12 @@ static int btrfs_read_locked_inode(struct inode *inode,
38193817
if (!ret)
38203818
filled = true;
38213819

3822-
if (!path) {
3823-
path = btrfs_alloc_path();
3824-
if (!path)
3825-
goto out;
3826-
}
3820+
ASSERT(path);
38273821

38283822
btrfs_get_inode_key(BTRFS_I(inode), &location);
38293823

38303824
ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
38313825
if (ret) {
3832-
if (path != in_path)
3833-
btrfs_free_path(path);
38343826
/*
38353827
* ret > 0 can come from btrfs_search_slot called by
38363828
* btrfs_lookup_inode(), this means the inode was not found.
@@ -3972,8 +3964,6 @@ static int btrfs_read_locked_inode(struct inode *inode,
39723964
btrfs_ino(BTRFS_I(inode)),
39733965
btrfs_root_id(root), ret);
39743966
}
3975-
if (path != in_path)
3976-
btrfs_free_path(path);
39773967

39783968
if (!maybe_acls)
39793969
cache_no_acl(inode);
@@ -5579,10 +5569,8 @@ static struct inode *btrfs_iget_locked(u64 ino, struct btrfs_root *root)
55795569
}
55805570

55815571
/*
5582-
* Get an inode object given its inode number and corresponding root.
5583-
* Path can be preallocated to prevent recursing back to iget through
5584-
* allocator. NULL is also valid but may require an additional allocation
5585-
* later.
5572+
* Get an inode object given its inode number and corresponding root. Path is
5573+
* preallocated to prevent recursing back to iget through allocator.
55865574
*/
55875575
struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
55885576
struct btrfs_path *path)
@@ -5605,9 +5593,33 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
56055593
return inode;
56065594
}
56075595

5596+
/*
5597+
* Get an inode object given its inode number and corresponding root.
5598+
*/
56085599
struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
56095600
{
5610-
return btrfs_iget_path(ino, root, NULL);
5601+
struct inode *inode;
5602+
struct btrfs_path *path;
5603+
int ret;
5604+
5605+
inode = btrfs_iget_locked(ino, root);
5606+
if (!inode)
5607+
return ERR_PTR(-ENOMEM);
5608+
5609+
if (!(inode->i_state & I_NEW))
5610+
return inode;
5611+
5612+
path = btrfs_alloc_path();
5613+
if (!path)
5614+
return ERR_PTR(-ENOMEM);
5615+
5616+
ret = btrfs_read_locked_inode(inode, path);
5617+
btrfs_free_path(path);
5618+
if (ret)
5619+
return ERR_PTR(ret);
5620+
5621+
unlock_new_inode(inode);
5622+
return inode;
56115623
}
56125624

56135625
static struct inode *new_simple_dir(struct inode *dir,

0 commit comments

Comments
 (0)