btrfs: make btrfs_iget() return a btrfs inode instead

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>
This commit is contained in:
Filipe Manana 2025-03-07 12:16:10 +00:00 committed by David Sterba
parent 14d063ec85
commit b204e5c7d4
9 changed files with 76 additions and 71 deletions

View file

@ -595,7 +595,7 @@ int __init btrfs_init_cachep(void);
void __cold btrfs_destroy_cachep(void); void __cold btrfs_destroy_cachep(void);
struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root, struct inode *btrfs_iget_path(u64 ino, struct btrfs_root *root,
struct btrfs_path *path); 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 extent_map *btrfs_get_extent(struct btrfs_inode *inode,
struct folio *folio, u64 start, u64 len); struct folio *folio, u64 start, u64 len);
int btrfs_update_inode(struct btrfs_trans_handle *trans, int btrfs_update_inode(struct btrfs_trans_handle *trans,

View file

@ -225,7 +225,7 @@ static int btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
struct file_ra_state *ra) struct file_ra_state *ra)
{ {
struct btrfs_root *inode_root; struct btrfs_root *inode_root;
struct inode *inode; struct btrfs_inode *inode;
struct btrfs_ioctl_defrag_range_args range; struct btrfs_ioctl_defrag_range_args range;
int ret = 0; int ret = 0;
u64 cur = 0; u64 cur = 0;
@ -250,24 +250,24 @@ again:
goto cleanup; goto cleanup;
} }
if (cur >= i_size_read(inode)) { if (cur >= i_size_read(&inode->vfs_inode)) {
iput(inode); iput(&inode->vfs_inode);
goto cleanup; goto cleanup;
} }
/* Do a chunk of defrag */ /* 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)); memset(&range, 0, sizeof(range));
range.len = (u64)-1; range.len = (u64)-1;
range.start = cur; range.start = cur;
range.extent_thresh = defrag->extent_thresh; 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); 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); BTRFS_DEFRAG_BATCH);
sb_end_write(fs_info->sb); sb_end_write(fs_info->sb);
iput(inode); iput(&inode->vfs_inode);
if (ret < 0) if (ret < 0)
goto cleanup; goto cleanup;

View file

@ -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_fs_info *fs_info = btrfs_sb(sb);
struct btrfs_root *root; struct btrfs_root *root;
struct inode *inode; struct btrfs_inode *inode;
if (objectid < BTRFS_FIRST_FREE_OBJECTID) if (objectid < BTRFS_FIRST_FREE_OBJECTID)
return ERR_PTR(-ESTALE); return ERR_PTR(-ESTALE);
@ -89,12 +89,12 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
if (IS_ERR(inode)) if (IS_ERR(inode))
return ERR_CAST(inode); return ERR_CAST(inode);
if (generation != 0 && generation != inode->i_generation) { if (generation != 0 && generation != inode->vfs_inode.i_generation) {
iput(inode); iput(&inode->vfs_inode);
return ERR_PTR(-ESTALE); 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, static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
@ -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 dentry *btrfs_get_parent(struct dentry *child)
{ {
struct btrfs_inode *dir = BTRFS_I(d_inode(child)); struct btrfs_inode *dir = BTRFS_I(d_inode(child));
struct btrfs_inode *inode;
struct btrfs_root *root = dir->root; struct btrfs_root *root = dir->root;
struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_fs_info *fs_info = root->fs_info;
struct btrfs_path *path; struct btrfs_path *path;
@ -210,7 +211,11 @@ struct dentry *btrfs_get_parent(struct dentry *child)
found_key.offset, 0); 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: fail:
btrfs_free_path(path); btrfs_free_path(path);
return ERR_PTR(ret); return ERR_PTR(ret);

View file

@ -3547,7 +3547,6 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct btrfs_key key, found_key; struct btrfs_key key, found_key;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct inode *inode;
u64 last_objectid = 0; u64 last_objectid = 0;
int ret = 0, nr_unlink = 0; int ret = 0, nr_unlink = 0;
@ -3566,6 +3565,8 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
key.offset = (u64)-1; key.offset = (u64)-1;
while (1) { while (1) {
struct btrfs_inode *inode;
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
if (ret < 0) if (ret < 0)
goto out; goto out;
@ -3689,10 +3690,10 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
* deleted but wasn't. The inode number may have been reused, * deleted but wasn't. The inode number may have been reused,
* but either way, we can delete the orphan item. * but either way, we can delete the orphan item.
*/ */
if (!inode || inode->i_nlink) { if (!inode || inode->vfs_inode.i_nlink) {
if (inode) { if (inode) {
ret = btrfs_drop_verity_items(BTRFS_I(inode)); ret = btrfs_drop_verity_items(inode);
iput(inode); iput(&inode->vfs_inode);
inode = NULL; inode = NULL;
if (ret) if (ret)
goto out; goto out;
@ -3715,7 +3716,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
nr_unlink++; nr_unlink++;
/* this will do delete_inode and everything for us */ /* this will do delete_inode and everything for us */
iput(inode); iput(&inode->vfs_inode);
} }
/* release the path since we're done with it */ /* release the path since we're done with it */
btrfs_release_path(path); btrfs_release_path(path);
@ -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. * 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_inode *inode;
struct btrfs_path *path; struct btrfs_path *path;
@ -5676,7 +5677,7 @@ struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
if (!(inode->vfs_inode.i_state & I_NEW)) if (!(inode->vfs_inode.i_state & I_NEW))
return &inode->vfs_inode; return inode;
path = btrfs_alloc_path(); path = btrfs_alloc_path();
if (!path) if (!path)
@ -5688,7 +5689,7 @@ struct inode *btrfs_iget(u64 ino, struct btrfs_root *root)
return ERR_PTR(ret); return ERR_PTR(ret);
unlock_new_inode(&inode->vfs_inode); unlock_new_inode(&inode->vfs_inode);
return &inode->vfs_inode; return inode;
} }
static struct btrfs_inode *new_simple_dir(struct inode *dir, static struct btrfs_inode *new_simple_dir(struct inode *dir,
@ -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 inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
{ {
struct btrfs_fs_info *fs_info = inode_to_fs_info(dir); 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 *root = BTRFS_I(dir)->root;
struct btrfs_root *sub_root = root; struct btrfs_root *sub_root = root;
struct btrfs_key location = { 0 }; struct btrfs_key location = { 0 };
@ -5765,49 +5766,48 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
if (location.type == BTRFS_INODE_ITEM_KEY) { if (location.type == BTRFS_INODE_ITEM_KEY) {
inode = btrfs_iget(location.objectid, root); inode = btrfs_iget(location.objectid, root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return inode; return ERR_CAST(inode);
/* Do extra check against inode mode with di_type */ /* 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, btrfs_crit(fs_info,
"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u", "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); di_type);
iput(inode); iput(&inode->vfs_inode);
return ERR_PTR(-EUCLEAN); return ERR_PTR(-EUCLEAN);
} }
return inode; return &inode->vfs_inode;
} }
ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry, ret = fixup_tree_root_location(fs_info, BTRFS_I(dir), dentry,
&location, &sub_root); &location, &sub_root);
if (ret < 0) { if (ret < 0) {
if (ret != -ENOENT) { if (ret != -ENOENT)
inode = ERR_PTR(ret); inode = ERR_PTR(ret);
} else { else
struct btrfs_inode *b_inode; inode = new_simple_dir(dir, &location, root);
b_inode = new_simple_dir(dir, &location, root);
inode = &b_inode->vfs_inode;
}
} else { } else {
inode = btrfs_iget(location.objectid, sub_root); inode = btrfs_iget(location.objectid, sub_root);
btrfs_put_root(sub_root); btrfs_put_root(sub_root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return inode; return ERR_CAST(inode);
down_read(&fs_info->cleanup_work_sem); 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); ret = btrfs_orphan_cleanup(sub_root);
up_read(&fs_info->cleanup_work_sem); up_read(&fs_info->cleanup_work_sem);
if (ret) { if (ret) {
iput(inode); iput(&inode->vfs_inode);
inode = ERR_PTR(ret); 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) static int btrfs_dentry_delete(const struct dentry *dentry)
@ -6460,7 +6460,7 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
path = NULL; path = NULL;
if (args->subvol) { if (args->subvol) {
struct inode *parent; struct btrfs_inode *parent;
/* /*
* Subvolumes inherit properties from their parent subvolume, * Subvolumes inherit properties from their parent subvolume,
@ -6471,8 +6471,8 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans,
ret = PTR_ERR(parent); ret = PTR_ERR(parent);
} else { } else {
ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode), ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode),
BTRFS_I(parent)); parent);
iput(parent); iput(&parent->vfs_inode);
} }
} else { } else {
ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode), ret = btrfs_inode_inherit_props(trans, BTRFS_I(inode),

View file

@ -1832,7 +1832,6 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
struct btrfs_path *path; struct btrfs_path *path;
struct btrfs_key key, key2; struct btrfs_key key, key2;
struct extent_buffer *leaf; struct extent_buffer *leaf;
struct inode *temp_inode;
char *ptr; char *ptr;
int slot; int slot;
int len; int len;
@ -1860,6 +1859,8 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
key.type = BTRFS_INODE_REF_KEY; key.type = BTRFS_INODE_REF_KEY;
key.offset = (u64)-1; key.offset = (u64)-1;
while (1) { while (1) {
struct btrfs_inode *temp_inode;
ret = btrfs_search_backwards(root, &key, path); ret = btrfs_search_backwards(root, &key, path);
if (ret < 0) if (ret < 0)
goto out_put; goto out_put;
@ -1914,9 +1915,9 @@ static int btrfs_search_path_in_tree_user(struct mnt_idmap *idmap,
ret = PTR_ERR(temp_inode); ret = PTR_ERR(temp_inode);
goto out_put; goto out_put;
} }
ret = inode_permission(idmap, temp_inode, ret = inode_permission(idmap, &temp_inode->vfs_inode,
MAY_READ | MAY_EXEC); MAY_READ | MAY_EXEC);
iput(temp_inode); iput(&temp_inode->vfs_inode);
if (ret) { if (ret) {
ret = -EACCES; ret = -EACCES;
goto out_put; goto out_put;

View file

@ -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_root *root = fs_info->tree_root;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct btrfs_inode *btrfs_inode;
int ret = 0; int ret = 0;
if (inode) if (inode)
goto truncate; goto truncate;
inode = btrfs_iget(ino, root); btrfs_inode = btrfs_iget(ino, root);
if (IS_ERR(inode)) if (IS_ERR(btrfs_inode))
return -ENOENT; return -ENOENT;
inode = &btrfs_inode->vfs_inode;
truncate: truncate:
ret = btrfs_check_trunc_cache_free_space(fs_info, ret = btrfs_check_trunc_cache_free_space(fs_info,
@ -3764,7 +3766,7 @@ static noinline_for_stack struct inode *create_reloc_inode(
struct btrfs_fs_info *fs_info, struct btrfs_fs_info *fs_info,
const struct btrfs_block_group *group) const struct btrfs_block_group *group)
{ {
struct inode *inode = NULL; struct btrfs_inode *inode = NULL;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct btrfs_root *root; struct btrfs_root *root;
u64 objectid; u64 objectid;
@ -3792,18 +3794,19 @@ static noinline_for_stack struct inode *create_reloc_inode(
inode = NULL; inode = NULL;
goto out; 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: out:
btrfs_put_root(root); btrfs_put_root(root);
btrfs_end_transaction(trans); btrfs_end_transaction(trans);
btrfs_btree_balance_dirty(fs_info); btrfs_btree_balance_dirty(fs_info);
if (ret) { if (ret) {
iput(inode); if (inode)
iput(&inode->vfs_inode);
inode = ERR_PTR(ret); inode = ERR_PTR(ret);
} }
return inode; return &inode->vfs_inode;
} }
/* /*

View file

@ -5187,14 +5187,14 @@ tlv_put_failure:
static int process_verity(struct send_ctx *sctx) static int process_verity(struct send_ctx *sctx)
{ {
int ret = 0; int ret = 0;
struct inode *inode; struct btrfs_inode *inode;
struct fs_path *p; struct fs_path *p;
inode = btrfs_iget(sctx->cur_ino, sctx->send_root); inode = btrfs_iget(sctx->cur_ino, sctx->send_root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return PTR_ERR(inode); return PTR_ERR(inode);
ret = btrfs_get_verity_descriptor(inode, NULL, 0); ret = btrfs_get_verity_descriptor(&inode->vfs_inode, NULL, 0);
if (ret < 0) if (ret < 0)
goto iput; goto iput;
@ -5211,7 +5211,7 @@ static int process_verity(struct send_ctx *sctx)
} }
} }
ret = btrfs_get_verity_descriptor(inode, sctx->verity_descriptor, ret); ret = btrfs_get_verity_descriptor(&inode->vfs_inode, sctx->verity_descriptor, ret);
if (ret < 0) if (ret < 0)
goto iput; goto iput;
@ -5223,7 +5223,7 @@ static int process_verity(struct send_ctx *sctx)
ret = send_verity(sctx, p, sctx->verity_descriptor); ret = send_verity(sctx, p, sctx->verity_descriptor);
iput: iput:
iput(inode); iput(&inode->vfs_inode);
return ret; return ret;
} }
@ -5573,7 +5573,7 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
{ {
struct btrfs_root *root = sctx->send_root; struct btrfs_root *root = sctx->send_root;
struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_fs_info *fs_info = root->fs_info;
struct inode *inode; struct btrfs_inode *inode;
struct fs_path *fspath; struct fs_path *fspath;
struct extent_buffer *leaf = path->nodes[0]; struct extent_buffer *leaf = path->nodes[0];
struct btrfs_key key; struct btrfs_key key;
@ -5639,7 +5639,7 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
* Note that send_buf is a mapping of send_buf_pages, so this is really * Note that send_buf is a mapping of send_buf_pages, so this is really
* reading into send_buf. * reading into send_buf.
*/ */
ret = btrfs_encoded_read_regular_fill_pages(BTRFS_I(inode), ret = btrfs_encoded_read_regular_fill_pages(inode,
disk_bytenr, disk_num_bytes, disk_bytenr, disk_num_bytes,
sctx->send_buf_pages + sctx->send_buf_pages +
(data_offset >> PAGE_SHIFT), (data_offset >> PAGE_SHIFT),
@ -5665,7 +5665,7 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
tlv_put_failure: tlv_put_failure:
out: out:
iput(inode); iput(&inode->vfs_inode);
return ret; return ret;
} }
@ -5707,15 +5707,14 @@ static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
} }
if (sctx->cur_inode == NULL) { if (sctx->cur_inode == NULL) {
struct btrfs_inode *btrfs_inode;
struct btrfs_root *root = sctx->send_root; struct btrfs_root *root = sctx->send_root;
sctx->cur_inode = btrfs_iget(sctx->cur_ino, root); btrfs_inode = btrfs_iget(sctx->cur_ino, root);
if (IS_ERR(sctx->cur_inode)) { if (IS_ERR(btrfs_inode))
int err = PTR_ERR(sctx->cur_inode); return PTR_ERR(btrfs_inode);
sctx->cur_inode = NULL; sctx->cur_inode = &btrfs_inode->vfs_inode;
return err;
}
memset(&sctx->ra, 0, sizeof(struct file_ra_state)); memset(&sctx->ra, 0, sizeof(struct file_ra_state));
file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping); file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);

View file

@ -947,7 +947,7 @@ static int get_default_subvol_objectid(struct btrfs_fs_info *fs_info, u64 *objec
static int btrfs_fill_super(struct super_block *sb, static int btrfs_fill_super(struct super_block *sb,
struct btrfs_fs_devices *fs_devices) struct btrfs_fs_devices *fs_devices)
{ {
struct inode *inode; struct btrfs_inode *inode;
struct btrfs_fs_info *fs_info = btrfs_sb(sb); struct btrfs_fs_info *fs_info = btrfs_sb(sb);
int err; int err;
@ -982,7 +982,7 @@ static int btrfs_fill_super(struct super_block *sb,
goto fail_close; goto fail_close;
} }
sb->s_root = d_make_root(inode); sb->s_root = d_make_root(&inode->vfs_inode);
if (!sb->s_root) { if (!sb->s_root) {
err = -ENOMEM; err = -ENOMEM;
goto fail_close; goto fail_close;

View file

@ -141,7 +141,7 @@ static void wait_log_commit(struct btrfs_root *root, int transid);
static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root) static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *root)
{ {
unsigned int nofs_flag; unsigned int nofs_flag;
struct inode *inode; struct btrfs_inode *inode;
/* /*
* We're holding a transaction handle whether we are logging or * We're holding a transaction handle whether we are logging or
@ -154,10 +154,7 @@ static struct btrfs_inode *btrfs_iget_logging(u64 objectid, struct btrfs_root *r
inode = btrfs_iget(objectid, root); inode = btrfs_iget(objectid, root);
memalloc_nofs_restore(nofs_flag); memalloc_nofs_restore(nofs_flag);
if (IS_ERR(inode)) return inode;
return ERR_CAST(inode);
return BTRFS_I(inode);
} }
/* /*