btrfs: make btrfs_iget() return a btrfs inode instead
authorFilipe Manana <fdmanana@suse.com>
Fri, 7 Mar 2025 12:16:10 +0000 (12:16 +0000)
committerDavid Sterba <dsterba@suse.com>
Tue, 18 Mar 2025 19:35:50 +0000 (20:35 +0100)
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>
fs/btrfs/btrfs_inode.h
fs/btrfs/defrag.c
fs/btrfs/export.c
fs/btrfs/inode.c
fs/btrfs/ioctl.c
fs/btrfs/relocation.c
fs/btrfs/send.c
fs/btrfs/super.c
fs/btrfs/tree-log.c

index cd5c8b595ebb62a996e9b3f4f34dd36c5c13745f..b5a94b961663bc02b1dcf2d0032ff0d291a1ecf6 100644 (file)
@@ -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,
index ae0b92b96345607bd8abb87834850f649835c09a..d4310d93f53285c1673dc6e0bc3ccb419eef14d6 100644 (file)
@@ -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;
@@ -250,24 +250,24 @@ again:
                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;
index a91eaf0ca34e10923d88e119efe3d923f9a0158f..7fc8a3200b4005784f4f6621db58c42ec4fe965b 100644 (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_root *root;
-       struct inode *inode;
+       struct btrfs_inode *inode;
 
        if (objectid < BTRFS_FIRST_FREE_OBJECTID)
                return ERR_PTR(-ESTALE);
@@ -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,
@@ -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;
@@ -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);
index 3a8e0858803c147e93a3e4b2f1ed864fd708d5fa..d48da09e53f3836827287b6cea10533f098e8c52 100644 (file)
@@ -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;
 
@@ -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;
@@ -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;
@@ -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);
@@ -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;
@@ -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)
@@ -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,
@@ -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 };
@@ -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)
@@ -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,
@@ -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),
index fffa2868f3296d148cd4f2fec79d18e5c2df00f5..c68e505710afb5055f9fd5c87ebb745d602e800f 100644 (file)
@@ -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;
@@ -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;
@@ -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;
index af0969b70b532025e73a8bcdeb84f2e43bd33eb1..5359cf2b79b5b200986e0c15d47c5ed8b1542e21 100644 (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_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,
@@ -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;
@@ -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;
 }
 
 /*
index 17a6ed3691e77677a42fa0459f1e14e81f31638c..0c8c58c4f29b78198fad9d19f7d1ebac27700e65 100644 (file)
@@ -5187,14 +5187,14 @@ tlv_put_failure:
 static int process_verity(struct send_ctx *sctx)
 {
        int ret = 0;
-       struct inode *inode;
+       struct btrfs_inode *inode;
        struct fs_path *p;
 
        inode = btrfs_iget(sctx->cur_ino, sctx->send_root);
        if (IS_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)
                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)
                goto iput;
 
@@ -5223,7 +5223,7 @@ static int process_verity(struct send_ctx *sctx)
 
        ret = send_verity(sctx, p, sctx->verity_descriptor);
 iput:
-       iput(inode);
+       iput(&inode->vfs_inode);
        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_fs_info *fs_info = root->fs_info;
-       struct inode *inode;
+       struct btrfs_inode *inode;
        struct fs_path *fspath;
        struct extent_buffer *leaf = path->nodes[0];
        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
         * 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,
                                                    sctx->send_buf_pages +
                                                    (data_offset >> PAGE_SHIFT),
@@ -5665,7 +5665,7 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
 
 tlv_put_failure:
 out:
-       iput(inode);
+       iput(&inode->vfs_inode);
        return ret;
 }
 
@@ -5707,15 +5707,14 @@ static int send_extent_data(struct send_ctx *sctx, struct btrfs_path *path,
        }
 
        if (sctx->cur_inode == NULL) {
+               struct btrfs_inode *btrfs_inode;
                struct btrfs_root *root = sctx->send_root;
 
-               sctx->cur_inode = btrfs_iget(sctx->cur_ino, root);
-               if (IS_ERR(sctx->cur_inode)) {
-                       int err = PTR_ERR(sctx->cur_inode);
+               btrfs_inode = btrfs_iget(sctx->cur_ino, root);
+               if (IS_ERR(btrfs_inode))
+                       return PTR_ERR(btrfs_inode);
 
-                       sctx->cur_inode = NULL;
-                       return err;
-               }
+               sctx->cur_inode = &btrfs_inode->vfs_inode;
                memset(&sctx->ra, 0, sizeof(struct file_ra_state));
                file_ra_state_init(&sctx->ra, sctx->cur_inode->i_mapping);
 
index fdec546a87f399a46cf5e5b9514a87f2cdfae7d3..40709e2a44fceca747b45fc219d6f87aa50d4d2b 100644 (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,
                            struct btrfs_fs_devices *fs_devices)
 {
-       struct inode *inode;
+       struct btrfs_inode *inode;
        struct btrfs_fs_info *fs_info = btrfs_sb(sb);
        int err;
 
@@ -982,7 +982,7 @@ static int btrfs_fill_super(struct super_block *sb,
                goto fail_close;
        }
 
-       sb->s_root = d_make_root(inode);
+       sb->s_root = d_make_root(&inode->vfs_inode);
        if (!sb->s_root) {
                err = -ENOMEM;
                goto fail_close;
index 349c9482e9b9dc9d8454cad0864f6ed3ed7432f4..2d23223f476b6a968cf7b3794b78ef63015907ea 100644 (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)
 {
        unsigned int nofs_flag;
-       struct inode *inode;
+       struct btrfs_inode *inode;
 
        /*
         * 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);
        memalloc_nofs_restore(nofs_flag);
 
-       if (IS_ERR(inode))
-               return ERR_CAST(inode);
-
-       return BTRFS_I(inode);
+       return inode;
 }
 
 /*