OSDN Git Service

btrfs: simplify iget helpers
authorDavid Sterba <dsterba@suse.com>
Fri, 15 May 2020 17:35:59 +0000 (19:35 +0200)
committerDavid Sterba <dsterba@suse.com>
Mon, 25 May 2020 09:25:37 +0000 (11:25 +0200)
The inode lookup starting at btrfs_iget takes the full location key,
while only the objectid is used to match the inode, because the lookup
happens inside the given root thus the inode number is unique.
The entire location key is properly set up in btrfs_init_locked_inode.

Simplify the helpers and pass only inode number, renaming it to 'ino'
instead of 'objectid'. This allows to remove temporary variables key,
saving some stack space.

Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/ctree.h
fs/btrfs/export.c
fs/btrfs/file.c
fs/btrfs/free-space-cache.c
fs/btrfs/inode.c
fs/btrfs/ioctl.c
fs/btrfs/props.c
fs/btrfs/relocation.c
fs/btrfs/send.c
fs/btrfs/super.c
fs/btrfs/tree-log.c

index 616fdbc..5afeb17 100644 (file)
@@ -2898,10 +2898,9 @@ void btrfs_free_inode(struct inode *inode);
 int btrfs_drop_inode(struct inode *inode);
 int __init btrfs_init_cachep(void);
 void __cold btrfs_destroy_cachep(void);
-struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
+struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
                              struct btrfs_root *root, struct btrfs_path *path);
-struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
-                        struct btrfs_root *root);
+struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
                                    struct page *page, size_t pg_offset,
                                    u64 start, u64 end);
index e7cc98b..1a8d419 100644 (file)
@@ -64,7 +64,6 @@ 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_key key;
 
        if (objectid < BTRFS_FIRST_FREE_OBJECTID)
                return ERR_PTR(-ESTALE);
@@ -73,11 +72,7 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
        if (IS_ERR(root))
                return ERR_CAST(root);
 
-       key.objectid = objectid;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-
-       inode = btrfs_iget(sb, &key, root);
+       inode = btrfs_iget(sb, objectid, root);
        btrfs_put_root(root);
        if (IS_ERR(inode))
                return ERR_CAST(inode);
@@ -196,9 +191,7 @@ struct dentry *btrfs_get_parent(struct dentry *child)
                                        found_key.offset, 0, 0);
        }
 
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-       return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
+       return d_obtain_alias(btrfs_iget(fs_info->sb, key.objectid, root));
 fail:
        btrfs_free_path(path);
        return ERR_PTR(ret);
index 14e1464..2c14312 100644 (file)
@@ -275,7 +275,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
 {
        struct btrfs_root *inode_root;
        struct inode *inode;
-       struct btrfs_key key;
        struct btrfs_ioctl_defrag_range_args range;
        int num_defrag;
        int ret;
@@ -287,10 +286,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
                goto cleanup;
        }
 
-       key.objectid = defrag->ino;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-       inode = btrfs_iget(fs_info->sb, &key, inode_root);
+       inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
        btrfs_put_root(inode_root);
        if (IS_ERR(inode)) {
                ret = PTR_ERR(inode);
index 3c353a3..525bc5a 100644 (file)
@@ -82,7 +82,7 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
         * sure NOFS is set to keep us from deadlocking.
         */
        nofs_flag = memalloc_nofs_save();
-       inode = btrfs_iget_path(fs_info->sb, &location, root, path);
+       inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
        btrfs_release_path(path);
        memalloc_nofs_restore(nofs_flag);
        if (IS_ERR(inode))
index e0e8f74..4ec7f34 100644 (file)
@@ -51,7 +51,7 @@
 #include "block-group.h"
 
 struct btrfs_iget_args {
-       struct btrfs_key *location;
+       u64 ino;
        struct btrfs_root *root;
 };
 
@@ -2978,7 +2978,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
                found_key.objectid = found_key.offset;
                found_key.type = BTRFS_INODE_ITEM_KEY;
                found_key.offset = 0;
-               inode = btrfs_iget(fs_info->sb, &found_key, root);
+               inode = btrfs_iget(fs_info->sb, last_objectid, root);
                ret = PTR_ERR_OR_ZERO(inode);
                if (ret && ret != -ENOENT)
                        goto out;
@@ -5223,9 +5223,11 @@ static void inode_tree_del(struct inode *inode)
 static int btrfs_init_locked_inode(struct inode *inode, void *p)
 {
        struct btrfs_iget_args *args = p;
-       inode->i_ino = args->location->objectid;
-       memcpy(&BTRFS_I(inode)->location, args->location,
-              sizeof(*args->location));
+
+       inode->i_ino = args->ino;
+       BTRFS_I(inode)->location.objectid = args->ino;
+       BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
+       BTRFS_I(inode)->location.offset = 0;
        BTRFS_I(inode)->root = btrfs_grab_root(args->root);
        BUG_ON(args->root && !BTRFS_I(inode)->root);
        return 0;
@@ -5234,19 +5236,19 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
 static int btrfs_find_actor(struct inode *inode, void *opaque)
 {
        struct btrfs_iget_args *args = opaque;
-       return args->location->objectid == BTRFS_I(inode)->location.objectid &&
+
+       return args->ino == BTRFS_I(inode)->location.objectid &&
                args->root == BTRFS_I(inode)->root;
 }
 
-static struct inode *btrfs_iget_locked(struct super_block *s,
-                                      struct btrfs_key *location,
+static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
                                       struct btrfs_root *root)
 {
        struct inode *inode;
        struct btrfs_iget_args args;
-       unsigned long hashval = btrfs_inode_hash(location->objectid, root);
+       unsigned long hashval = btrfs_inode_hash(ino, root);
 
-       args.location = location;
+       args.ino = ino;
        args.root = root;
 
        inode = iget5_locked(s, hashval, btrfs_find_actor,
@@ -5256,17 +5258,17 @@ static struct inode *btrfs_iget_locked(struct super_block *s,
 }
 
 /*
- * Get an inode object given its location and corresponding root.
+ * Get an inode object given its inode number and corresponding root.
  * Path can be preallocated to prevent recursing back to iget through
  * allocator. NULL is also valid but may require an additional allocation
  * later.
  */
-struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
+struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
                              struct btrfs_root *root, struct btrfs_path *path)
 {
        struct inode *inode;
 
-       inode = btrfs_iget_locked(s, location, root);
+       inode = btrfs_iget_locked(s, ino, root);
        if (!inode)
                return ERR_PTR(-ENOMEM);
 
@@ -5293,10 +5295,9 @@ struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
        return inode;
 }
 
-struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
-                        struct btrfs_root *root)
+struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
 {
-       return btrfs_iget_path(s, location, root, NULL);
+       return btrfs_iget_path(s, ino, root, NULL);
 }
 
 static struct inode *new_simple_dir(struct super_block *s,
@@ -5365,7 +5366,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
                return ERR_PTR(ret);
 
        if (location.type == BTRFS_INODE_ITEM_KEY) {
-               inode = btrfs_iget(dir->i_sb, &location, root);
+               inode = btrfs_iget(dir->i_sb, location.objectid, root);
                if (IS_ERR(inode))
                        return inode;
 
@@ -5389,7 +5390,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
                else
                        inode = new_simple_dir(dir->i_sb, &location, sub_root);
        } else {
-               inode = btrfs_iget(dir->i_sb, &location, sub_root);
+               inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
        }
        if (root != sub_root)
                btrfs_put_root(sub_root);
@@ -5770,7 +5771,8 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
 static int btrfs_insert_inode_locked(struct inode *inode)
 {
        struct btrfs_iget_args args;
-       args.location = &BTRFS_I(inode)->location;
+
+       args.ino = BTRFS_I(inode)->location.objectid;
        args.root = BTRFS_I(inode)->root;
 
        return insert_inode_locked4(inode,
index 05a8688..168deb8 100644 (file)
@@ -2424,7 +2424,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
                                goto out_put;
                        }
 
-                       temp_inode = btrfs_iget(sb, &key2, root);
+                       temp_inode = btrfs_iget(sb, key2.objectid, root);
                        if (IS_ERR(temp_inode)) {
                                ret = PTR_ERR(temp_inode);
                                goto out_put;
index ff1ff90..2dcb1cb 100644 (file)
@@ -408,19 +408,14 @@ int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
                               struct btrfs_root *parent_root)
 {
        struct super_block *sb = root->fs_info->sb;
-       struct btrfs_key key;
        struct inode *parent_inode, *child_inode;
        int ret;
 
-       key.objectid = BTRFS_FIRST_FREE_OBJECTID;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-
-       parent_inode = btrfs_iget(sb, &key, parent_root);
+       parent_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, parent_root);
        if (IS_ERR(parent_inode))
                return PTR_ERR(parent_inode);
 
-       child_inode = btrfs_iget(sb, &key, root);
+       child_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, root);
        if (IS_ERR(child_inode)) {
                iput(parent_inode);
                return PTR_ERR(child_inode);
index 018f830..3bbae80 100644 (file)
@@ -2970,7 +2970,6 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
                                    struct inode *inode,
                                    u64 ino)
 {
-       struct btrfs_key key;
        struct btrfs_root *root = fs_info->tree_root;
        struct btrfs_trans_handle *trans;
        int ret = 0;
@@ -2978,11 +2977,7 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
        if (inode)
                goto truncate;
 
-       key.objectid = ino;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-
-       inode = btrfs_iget(fs_info->sb, &key, root);
+       inode = btrfs_iget(fs_info->sb, ino, root);
        if (IS_ERR(inode))
                return -ENOENT;
 
@@ -3470,7 +3465,6 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
        struct inode *inode = NULL;
        struct btrfs_trans_handle *trans;
        struct btrfs_root *root;
-       struct btrfs_key key;
        u64 objectid;
        int err = 0;
 
@@ -3488,10 +3482,7 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
        err = __insert_orphan_inode(trans, root, objectid);
        BUG_ON(err);
 
-       key.objectid = objectid;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-       inode = btrfs_iget(fs_info->sb, &key, root);
+       inode = btrfs_iget(fs_info->sb, objectid, root);
        BUG_ON(IS_ERR(inode));
        BTRFS_I(inode)->index_cnt = group->start;
 
index 3ddd3b9..0f37660 100644 (file)
@@ -4806,17 +4806,12 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
        struct inode *inode;
        struct page *page;
        char *addr;
-       struct btrfs_key key;
        pgoff_t index = offset >> PAGE_SHIFT;
        pgoff_t last_index;
        unsigned pg_offset = offset_in_page(offset);
        ssize_t ret = 0;
 
-       key.objectid = sctx->cur_ino;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-
-       inode = btrfs_iget(fs_info->sb, &key, root);
+       inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
        if (IS_ERR(inode))
                return PTR_ERR(inode);
 
index 6bbf84a..bc73fd6 100644 (file)
@@ -1217,7 +1217,6 @@ static int btrfs_fill_super(struct super_block *sb,
 {
        struct inode *inode;
        struct btrfs_fs_info *fs_info = btrfs_sb(sb);
-       struct btrfs_key key;
        int err;
 
        sb->s_maxbytes = MAX_LFS_FILESIZE;
@@ -1245,10 +1244,7 @@ static int btrfs_fill_super(struct super_block *sb,
                return err;
        }
 
-       key.objectid = BTRFS_FIRST_FREE_OBJECTID;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-       inode = btrfs_iget(sb, &key, fs_info->fs_root);
+       inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
        if (IS_ERR(inode)) {
                err = PTR_ERR(inode);
                goto fail_close;
index d3662e1..67fa708 100644 (file)
@@ -550,13 +550,9 @@ no_copy:
 static noinline struct inode *read_one_inode(struct btrfs_root *root,
                                             u64 objectid)
 {
-       struct btrfs_key key;
        struct inode *inode;
 
-       key.objectid = objectid;
-       key.type = BTRFS_INODE_ITEM_KEY;
-       key.offset = 0;
-       inode = btrfs_iget(root->fs_info->sb, &key, root);
+       inode = btrfs_iget(root->fs_info->sb, objectid, root);
        if (IS_ERR(inode))
                inode = NULL;
        return inode;
@@ -4815,10 +4811,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
 
                btrfs_release_path(path);
 
-               key.objectid = ino;
-               key.type = BTRFS_INODE_ITEM_KEY;
-               key.offset = 0;
-               inode = btrfs_iget(fs_info->sb, &key, root);
+               inode = btrfs_iget(fs_info->sb, ino, root);
                /*
                 * If the other inode that had a conflicting dir entry was
                 * deleted in the current transaction, we need to log its parent
@@ -4827,8 +4820,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
                if (IS_ERR(inode)) {
                        ret = PTR_ERR(inode);
                        if (ret == -ENOENT) {
-                               key.objectid = parent;
-                               inode = btrfs_iget(fs_info->sb, &key, root);
+                               inode = btrfs_iget(fs_info->sb, parent, root);
                                if (IS_ERR(inode)) {
                                        ret = PTR_ERR(inode);
                                } else {
@@ -5567,7 +5559,7 @@ process_leaf:
                                continue;
 
                        btrfs_release_path(path);
-                       di_inode = btrfs_iget(fs_info->sb, &di_key, root);
+                       di_inode = btrfs_iget(fs_info->sb, di_key.objectid, root);
                        if (IS_ERR(di_inode)) {
                                ret = PTR_ERR(di_inode);
                                goto next_dir_inode;
@@ -5693,7 +5685,8 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
                                cur_offset = item_size;
                        }
 
-                       dir_inode = btrfs_iget(fs_info->sb, &inode_key, root);
+                       dir_inode = btrfs_iget(fs_info->sb, inode_key.objectid,
+                                              root);
                        /*
                         * If the parent inode was deleted, return an error to
                         * fallback to a transaction commit. This is to prevent
@@ -5760,14 +5753,17 @@ static int log_new_ancestors(struct btrfs_trans_handle *trans,
                int slot = path->slots[0];
                struct btrfs_key search_key;
                struct inode *inode;
+               u64 ino;
                int ret = 0;
 
                btrfs_release_path(path);
 
+               ino = found_key.offset;
+
                search_key.objectid = found_key.offset;
                search_key.type = BTRFS_INODE_ITEM_KEY;
                search_key.offset = 0;
-               inode = btrfs_iget(fs_info->sb, &search_key, root);
+               inode = btrfs_iget(fs_info->sb, ino, root);
                if (IS_ERR(inode))
                        return PTR_ERR(inode);