Skip to content

Commit

Permalink
btrfs: make btrfs_iget() return a btrfs inode instead
Browse files Browse the repository at this point in the history
It's an internal function and most of the time the callers are doing a lot
of BTRFS_I() calls on the returned VFS inode to get the btrfs inode, so
change the return type to struct btrfs_inode instead.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
  • Loading branch information
Filipe Manana authored and David Sterba committed Mar 18, 2025
1 parent 14d063e commit b204e5c
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 71 deletions.
2 changes: 1 addition & 1 deletion fs/btrfs/btrfs_inode.h
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ int __init btrfs_init_cachep(void);
void __cold btrfs_destroy_cachep(void);
struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
struct btrfs_path *path);
struct inode *btrfs_iget(u64 ino, struct btrfs_root *root);
struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root);
struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
struct folio *folio, u64 start, u64 len);
int btrfs_update_inode(struct btrfs_trans_handle *trans,
Expand Down
14 changes: 7 additions & 7 deletions fs/btrfs/defrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ static int btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
struct file_ra_state *ra)
{
struct btrfs_root *inode_root;
struct inode *inode;
struct btrfs_inode *inode;
struct btrfs_ioctl_defrag_range_args range;
int ret = 0;
u64 cur = 0;
Expand All @@ -250,24 +250,24 @@ static int btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
goto cleanup;
}

if (cur >= i_size_read(inode)) {
iput(inode);
if (cur >= i_size_read(&inode->vfs_inode)) {
iput(&inode->vfs_inode);
goto cleanup;
}

/* Do a chunk of defrag */
clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
clear_bit(BTRFS_INODE_IN_DEFRAG, &inode->runtime_flags);
memset(&range, 0, sizeof(range));
range.len = (u64)-1;
range.start = cur;
range.extent_thresh = defrag->extent_thresh;
file_ra_state_init(ra, inode->i_mapping);
file_ra_state_init(ra, inode->vfs_inode.i_mapping);

sb_start_write(fs_info->sb);
ret = btrfs_defrag_file(BTRFS_I(inode), ra, &range, defrag->transid,
ret = btrfs_defrag_file(inode, ra, &range, defrag->transid,
BTRFS_DEFRAG_BATCH);
sb_end_write(fs_info->sb);
iput(inode);
iput(&inode->vfs_inode);

if (ret < 0)
goto cleanup;
Expand Down
15 changes: 10 additions & 5 deletions fs/btrfs/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
{
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
struct btrfs_root *root;
struct inode *inode;
struct btrfs_inode *inode;

if (objectid < BTRFS_FIRST_FREE_OBJECTID)
return ERR_PTR(-ESTALE);
Expand All @@ -89,12 +89,12 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
if (IS_ERR(inode))
return ERR_CAST(inode);

if (generation != 0 && generation != inode->i_generation) {
iput(inode);
if (generation != 0 && generation != inode->vfs_inode.i_generation) {
iput(&inode->vfs_inode);
return ERR_PTR(-ESTALE);
}

return d_obtain_alias(inode);
return d_obtain_alias(&inode->vfs_inode);
}

static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
Expand Down Expand Up @@ -146,6 +146,7 @@ static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
struct dentry *btrfs_get_parent(struct dentry *child)
{
struct btrfs_inode *dir = BTRFS_I(d_inode(child));
struct btrfs_inode *inode;
struct btrfs_root *root = dir->root;
struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_path *path;
Expand Down Expand Up @@ -210,7 +211,11 @@ struct dentry *btrfs_get_parent(struct dentry *child)
found_key.offset, 0);
}

return d_obtain_alias(btrfs_iget(key.objectid, root));
inode = btrfs_iget(key.objectid, root);
if (IS_ERR(inode))
return ERR_CAST(inode);

return d_obtain_alias(&inode->vfs_inode);
fail:
btrfs_free_path(path);
return ERR_PTR(ret);
Expand Down
56 changes: 28 additions & 28 deletions fs/btrfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3547,7 +3547,6 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
struct extent_buffer *leaf;
struct btrfs_key key, found_key;
struct btrfs_trans_handle *trans;
struct inode *inode;
u64 last_objectid = 0;
int ret = 0, nr_unlink = 0;

Expand All @@ -3566,6 +3565,8 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
key.offset = (u64)-1;

while (1) {
struct btrfs_inode *inode;

ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0)
goto out;
Expand Down Expand Up @@ -3689,10 +3690,10 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
* deleted but wasn't. The inode number may have been reused,
* but either way, we can delete the orphan item.
*/
if (!inode || inode->i_nlink) {
if (!inode || inode->vfs_inode.i_nlink) {
if (inode) {
ret = btrfs_drop_verity_items(BTRFS_I(inode));
iput(inode);
ret = btrfs_drop_verity_items(inode);
iput(&inode->vfs_inode);
inode = NULL;
if (ret)
goto out;
Expand All @@ -3715,7 +3716,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
nr_unlink++;

/* this will do delete_inode and everything for us */
iput(inode);
iput(&inode->vfs_inode);
}
/* release the path since we're done with it */
btrfs_release_path(path);
Expand Down Expand Up @@ -5665,7 +5666,7 @@ struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
/*
* Get an inode object given its inode number and corresponding root.
*/
struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
struct btrfs_inode *btrfs_iget(u64 ino, struct btrfs_root *root)
{
struct btrfs_inode *inode;
struct btrfs_path *path;
Expand All @@ -5676,7 +5677,7 @@ struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
return ERR_PTR(-ENOMEM);

if (!(inode->vfs_inode.i_state & I_NEW))
return &inode->vfs_inode;
return inode;

path = btrfs_alloc_path();
if (!path)
Expand All @@ -5688,7 +5689,7 @@ struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
return ERR_PTR(ret);

unlock_new_inode(&inode->vfs_inode);
return &inode->vfs_inode;
return inode;
}

static struct btrfs_inode *new_simple_dir(struct inode *dir,
Expand Down Expand Up @@ -5748,7 +5749,7 @@ static inline u8 btrfs_inode_type(const struct btrfs_inode *inode)
struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
{
struct btrfs_fs_info *fs_info = inode_to_fs_info(dir);
struct inode *inode;
struct btrfs_inode *inode;
struct btrfs_root *root = BTRFS_I(dir)->root;
struct btrfs_root *sub_root = root;
struct btrfs_key location = { 0 };
Expand All @@ -5765,49 +5766,48 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
if (location.type == BTRFS_INODE_ITEM_KEY) {
inode = btrfs_iget(location.objectid, root);
if (IS_ERR(inode))
return inode;
return ERR_CAST(inode);

/* Do extra check against inode mode with di_type */
if (btrfs_inode_type(BTRFS_I(inode)) != di_type) {
if (btrfs_inode_type(inode) != di_type) {
btrfs_crit(fs_info,
"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
inode->i_mode, btrfs_inode_type(BTRFS_I(inode)),
inode->vfs_inode.i_mode, btrfs_inode_type(inode),
di_type);
iput(inode);
iput(&inode->vfs_inode);
return ERR_PTR(-EUCLEAN);
}
return inode;
return &inode->vfs_inode;
}

ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry,
&location, &sub_root);
if (ret < 0) {
if (ret != -ENOENT) {
if (ret != -ENOENT)
inode = ERR_PTR(ret);
} else {
struct btrfs_inode *b_inode;

b_inode = new_simple_dir(dir, &location, root);
inode = &b_inode->vfs_inode;
}
else
inode = new_simple_dir(dir, &location, root);
} else {
inode = btrfs_iget(location.objectid, sub_root);
btrfs_put_root(sub_root);

if (IS_ERR(inode))
return inode;
return ERR_CAST(inode);

down_read(&fs_info->cleanup_work_sem);
if (!sb_rdonly(inode->i_sb))
if (!sb_rdonly(inode->vfs_inode.i_sb))
ret = btrfs_orphan_cleanup(sub_root);
up_read(&fs_info->cleanup_work_sem);
if (ret) {
iput(inode);
iput(&inode->vfs_inode);
inode = ERR_PTR(ret);
}
}

return inode;
if (IS_ERR(inode))
return ERR_CAST(inode);

return &inode->vfs_inode;
}

static int btrfs_dentry_delete(const struct dentry *dentry)
Expand Down Expand Up @@ -6460,7 +6460,7 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
path = NULL;

if (args->subvol) {
struct inode *parent;
struct btrfs_inode *parent;

/*
* Subvolumes inherit properties from their parent subvolume,
Expand All @@ -6471,8 +6471,8 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
ret = PTR_ERR(parent);
} else {
ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode),
BTRFS_I(parent));
iput(parent);
parent);
iput(&parent->vfs_inode);
}
} else {
ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode),
Expand Down
7 changes: 4 additions & 3 deletions fs/btrfs/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,6 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
struct btrfs_path *path;
struct btrfs_key key, key2;
struct extent_buffer *leaf;
struct inode *temp_inode;
char *ptr;
int slot;
int len;
Expand Down Expand Up @@ -1860,6 +1859,8 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
key.type = BTRFS_INODE_REF_KEY;
key.offset = (u64)-1;
while (1) {
struct btrfs_inode *temp_inode;

ret = btrfs_search_backwards(root, &key, path);
if (ret < 0)
goto out_put;
Expand Down Expand Up @@ -1914,9 +1915,9 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
ret = PTR_ERR(temp_inode);
goto out_put;
}
ret = inode_permission(idmap, temp_inode,
ret = inode_permission(idmap, &temp_inode->vfs_inode,
MAY_READ | MAY_EXEC);
iput(temp_inode);
iput(&temp_inode->vfs_inode);
if (ret) {
ret = -EACCES;
goto out_put;
Expand Down
17 changes: 10 additions & 7 deletions fs/btrfs/relocation.c
Original file line number Diff line number Diff line change
Expand Up @@ -3246,14 +3246,16 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
{
struct btrfs_root *root = fs_info->tree_root;
struct btrfs_trans_handle *trans;
struct btrfs_inode *btrfs_inode;
int ret = 0;

if (inode)
goto truncate;

inode = btrfs_iget(ino, root);
if (IS_ERR(inode))
btrfs_inode = btrfs_iget(ino, root);
if (IS_ERR(btrfs_inode))
return -ENOENT;
inode = &btrfs_inode->vfs_inode;

truncate:
ret = btrfs_check_trunc_cache_free_space(fs_info,
Expand Down Expand Up @@ -3764,7 +3766,7 @@ static noinline_for_stack struct inode *create_reloc_inode(
struct btrfs_fs_info *fs_info,
const struct btrfs_block_group *group)
{
struct inode *inode = NULL;
struct btrfs_inode *inode = NULL;
struct btrfs_trans_handle *trans;
struct btrfs_root *root;
u64 objectid;
Expand Down Expand Up @@ -3792,18 +3794,19 @@ static noinline_for_stack struct inode *create_reloc_inode(
inode = NULL;
goto out;
}
BTRFS_I(inode)->reloc_block_group_start = group->start;
inode->reloc_block_group_start = group->start;

ret = btrfs_orphan_add(trans, BTRFS_I(inode));
ret = btrfs_orphan_add(trans, inode);
out:
btrfs_put_root(root);
btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info);
if (ret) {
iput(inode);
if (inode)
iput(&inode->vfs_inode);
inode = ERR_PTR(ret);
}
return inode;
return &inode->vfs_inode;
}

/*
Expand Down
Loading

0 comments on commit b204e5c

Please sign in to comment.