btrfs: simplify iget helpers

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>
This commit is contained in:
David Sterba 2020-05-15 19:35:59 +02:00
parent a820feb546
commit 0202e83fda
11 changed files with 44 additions and 81 deletions

View File

@ -2898,10 +2898,9 @@ void btrfs_free_inode(struct inode *inode);
int btrfs_drop_inode(struct inode *inode); int btrfs_drop_inode(struct inode *inode);
int __init btrfs_init_cachep(void); int __init btrfs_init_cachep(void);
void __cold btrfs_destroy_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 btrfs_root *root, struct btrfs_path *path);
struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
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 page *page, size_t pg_offset, struct page *page, size_t pg_offset,
u64 start, u64 end); u64 start, u64 end);

View 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_fs_info *fs_info = btrfs_sb(sb);
struct btrfs_root *root; struct btrfs_root *root;
struct inode *inode; struct inode *inode;
struct btrfs_key key;
if (objectid < BTRFS_FIRST_FREE_OBJECTID) if (objectid < BTRFS_FIRST_FREE_OBJECTID)
return ERR_PTR(-ESTALE); return ERR_PTR(-ESTALE);
@ -73,11 +72,7 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
if (IS_ERR(root)) if (IS_ERR(root))
return ERR_CAST(root); return ERR_CAST(root);
key.objectid = objectid; inode = btrfs_iget(sb, objectid, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(sb, &key, root);
btrfs_put_root(root); btrfs_put_root(root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return ERR_CAST(inode); return ERR_CAST(inode);
@ -196,9 +191,7 @@ struct dentry *btrfs_get_parent(struct dentry *child)
found_key.offset, 0, 0); found_key.offset, 0, 0);
} }
key.type = BTRFS_INODE_ITEM_KEY; return d_obtain_alias(btrfs_iget(fs_info->sb, key.objectid, root));
key.offset = 0;
return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
fail: fail:
btrfs_free_path(path); btrfs_free_path(path);
return ERR_PTR(ret); return ERR_PTR(ret);

View File

@ -275,7 +275,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
{ {
struct btrfs_root *inode_root; struct btrfs_root *inode_root;
struct inode *inode; struct inode *inode;
struct btrfs_key key;
struct btrfs_ioctl_defrag_range_args range; struct btrfs_ioctl_defrag_range_args range;
int num_defrag; int num_defrag;
int ret; int ret;
@ -287,10 +286,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
goto cleanup; goto cleanup;
} }
key.objectid = defrag->ino; inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(fs_info->sb, &key, inode_root);
btrfs_put_root(inode_root); btrfs_put_root(inode_root);
if (IS_ERR(inode)) { if (IS_ERR(inode)) {
ret = PTR_ERR(inode); ret = PTR_ERR(inode);

View 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. * sure NOFS is set to keep us from deadlocking.
*/ */
nofs_flag = memalloc_nofs_save(); 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); btrfs_release_path(path);
memalloc_nofs_restore(nofs_flag); memalloc_nofs_restore(nofs_flag);
if (IS_ERR(inode)) if (IS_ERR(inode))

View File

@ -51,7 +51,7 @@
#include "block-group.h" #include "block-group.h"
struct btrfs_iget_args { struct btrfs_iget_args {
struct btrfs_key *location; u64 ino;
struct btrfs_root *root; struct btrfs_root *root;
}; };
@ -2978,7 +2978,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
found_key.objectid = found_key.offset; found_key.objectid = found_key.offset;
found_key.type = BTRFS_INODE_ITEM_KEY; found_key.type = BTRFS_INODE_ITEM_KEY;
found_key.offset = 0; 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); ret = PTR_ERR_OR_ZERO(inode);
if (ret && ret != -ENOENT) if (ret && ret != -ENOENT)
goto out; 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) static int btrfs_init_locked_inode(struct inode *inode, void *p)
{ {
struct btrfs_iget_args *args = p; struct btrfs_iget_args *args = p;
inode->i_ino = args->location->objectid;
memcpy(&BTRFS_I(inode)->location, args->location, inode->i_ino = args->ino;
sizeof(*args->location)); 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); BTRFS_I(inode)->root = btrfs_grab_root(args->root);
BUG_ON(args->root && !BTRFS_I(inode)->root); BUG_ON(args->root && !BTRFS_I(inode)->root);
return 0; 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) static int btrfs_find_actor(struct inode *inode, void *opaque)
{ {
struct btrfs_iget_args *args = 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; args->root == BTRFS_I(inode)->root;
} }
static struct inode *btrfs_iget_locked(struct super_block *s, static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
struct btrfs_key *location,
struct btrfs_root *root) struct btrfs_root *root)
{ {
struct inode *inode; struct inode *inode;
struct btrfs_iget_args args; 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; args.root = root;
inode = iget5_locked(s, hashval, btrfs_find_actor, 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 * Path can be preallocated to prevent recursing back to iget through
* allocator. NULL is also valid but may require an additional allocation * allocator. NULL is also valid but may require an additional allocation
* later. * 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 btrfs_root *root, struct btrfs_path *path)
{ {
struct inode *inode; struct inode *inode;
inode = btrfs_iget_locked(s, location, root); inode = btrfs_iget_locked(s, ino, root);
if (!inode) if (!inode)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
@ -5293,10 +5295,9 @@ struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
return inode; return inode;
} }
struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
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, 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); return ERR_PTR(ret);
if (location.type == BTRFS_INODE_ITEM_KEY) { 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)) if (IS_ERR(inode))
return inode; return inode;
@ -5389,7 +5390,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
else else
inode = new_simple_dir(dir->i_sb, &location, sub_root); inode = new_simple_dir(dir->i_sb, &location, sub_root);
} else { } else {
inode = btrfs_iget(dir->i_sb, &location, sub_root); inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
} }
if (root != sub_root) if (root != sub_root)
btrfs_put_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) static int btrfs_insert_inode_locked(struct inode *inode)
{ {
struct btrfs_iget_args args; struct btrfs_iget_args args;
args.location = &BTRFS_I(inode)->location;
args.ino = BTRFS_I(inode)->location.objectid;
args.root = BTRFS_I(inode)->root; args.root = BTRFS_I(inode)->root;
return insert_inode_locked4(inode, return insert_inode_locked4(inode,

View File

@ -2424,7 +2424,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
goto out_put; goto out_put;
} }
temp_inode = btrfs_iget(sb, &key2, root); temp_inode = btrfs_iget(sb, key2.objectid, root);
if (IS_ERR(temp_inode)) { if (IS_ERR(temp_inode)) {
ret = PTR_ERR(temp_inode); ret = PTR_ERR(temp_inode);
goto out_put; goto out_put;

View File

@ -408,19 +408,14 @@ int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
struct btrfs_root *parent_root) struct btrfs_root *parent_root)
{ {
struct super_block *sb = root->fs_info->sb; struct super_block *sb = root->fs_info->sb;
struct btrfs_key key;
struct inode *parent_inode, *child_inode; struct inode *parent_inode, *child_inode;
int ret; int ret;
key.objectid = BTRFS_FIRST_FREE_OBJECTID; parent_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, parent_root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
parent_inode = btrfs_iget(sb, &key, parent_root);
if (IS_ERR(parent_inode)) if (IS_ERR(parent_inode))
return PTR_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)) { if (IS_ERR(child_inode)) {
iput(parent_inode); iput(parent_inode);
return PTR_ERR(child_inode); return PTR_ERR(child_inode);

View File

@ -2970,7 +2970,6 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
struct inode *inode, struct inode *inode,
u64 ino) u64 ino)
{ {
struct btrfs_key key;
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;
int ret = 0; int ret = 0;
@ -2978,11 +2977,7 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
if (inode) if (inode)
goto truncate; goto truncate;
key.objectid = ino; inode = btrfs_iget(fs_info->sb, ino, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(fs_info->sb, &key, root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return -ENOENT; return -ENOENT;
@ -3470,7 +3465,6 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
struct inode *inode = NULL; struct inode *inode = NULL;
struct btrfs_trans_handle *trans; struct btrfs_trans_handle *trans;
struct btrfs_root *root; struct btrfs_root *root;
struct btrfs_key key;
u64 objectid; u64 objectid;
int err = 0; 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); err = __insert_orphan_inode(trans, root, objectid);
BUG_ON(err); BUG_ON(err);
key.objectid = objectid; inode = btrfs_iget(fs_info->sb, objectid, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(fs_info->sb, &key, root);
BUG_ON(IS_ERR(inode)); BUG_ON(IS_ERR(inode));
BTRFS_I(inode)->index_cnt = group->start; BTRFS_I(inode)->index_cnt = group->start;

View File

@ -4806,17 +4806,12 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
struct inode *inode; struct inode *inode;
struct page *page; struct page *page;
char *addr; char *addr;
struct btrfs_key key;
pgoff_t index = offset >> PAGE_SHIFT; pgoff_t index = offset >> PAGE_SHIFT;
pgoff_t last_index; pgoff_t last_index;
unsigned pg_offset = offset_in_page(offset); unsigned pg_offset = offset_in_page(offset);
ssize_t ret = 0; ssize_t ret = 0;
key.objectid = sctx->cur_ino; inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(fs_info->sb, &key, root);
if (IS_ERR(inode)) if (IS_ERR(inode))
return PTR_ERR(inode); return PTR_ERR(inode);

View File

@ -1217,7 +1217,6 @@ static int btrfs_fill_super(struct super_block *sb,
{ {
struct inode *inode; struct inode *inode;
struct btrfs_fs_info *fs_info = btrfs_sb(sb); struct btrfs_fs_info *fs_info = btrfs_sb(sb);
struct btrfs_key key;
int err; int err;
sb->s_maxbytes = MAX_LFS_FILESIZE; sb->s_maxbytes = MAX_LFS_FILESIZE;
@ -1245,10 +1244,7 @@ static int btrfs_fill_super(struct super_block *sb,
return err; return err;
} }
key.objectid = BTRFS_FIRST_FREE_OBJECTID; inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(sb, &key, fs_info->fs_root);
if (IS_ERR(inode)) { if (IS_ERR(inode)) {
err = PTR_ERR(inode); err = PTR_ERR(inode);
goto fail_close; goto fail_close;

View File

@ -550,13 +550,9 @@ no_copy:
static noinline struct inode *read_one_inode(struct btrfs_root *root, static noinline struct inode *read_one_inode(struct btrfs_root *root,
u64 objectid) u64 objectid)
{ {
struct btrfs_key key;
struct inode *inode; struct inode *inode;
key.objectid = objectid; inode = btrfs_iget(root->fs_info->sb, objectid, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(root->fs_info->sb, &key, root);
if (IS_ERR(inode)) if (IS_ERR(inode))
inode = NULL; inode = NULL;
return inode; return inode;
@ -4815,10 +4811,7 @@ static int log_conflicting_inodes(struct btrfs_trans_handle *trans,
btrfs_release_path(path); btrfs_release_path(path);
key.objectid = ino; inode = btrfs_iget(fs_info->sb, ino, root);
key.type = BTRFS_INODE_ITEM_KEY;
key.offset = 0;
inode = btrfs_iget(fs_info->sb, &key, root);
/* /*
* If the other inode that had a conflicting dir entry was * If the other inode that had a conflicting dir entry was
* deleted in the current transaction, we need to log its parent * 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)) { if (IS_ERR(inode)) {
ret = PTR_ERR(inode); ret = PTR_ERR(inode);
if (ret == -ENOENT) { if (ret == -ENOENT) {
key.objectid = parent; inode = btrfs_iget(fs_info->sb, parent, root);
inode = btrfs_iget(fs_info->sb, &key, root);
if (IS_ERR(inode)) { if (IS_ERR(inode)) {
ret = PTR_ERR(inode); ret = PTR_ERR(inode);
} else { } else {
@ -5567,7 +5559,7 @@ process_leaf:
continue; continue;
btrfs_release_path(path); 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)) { if (IS_ERR(di_inode)) {
ret = PTR_ERR(di_inode); ret = PTR_ERR(di_inode);
goto next_dir_inode; goto next_dir_inode;
@ -5693,7 +5685,8 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
cur_offset = item_size; 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 * If the parent inode was deleted, return an error to
* fallback to a transaction commit. This is to prevent * 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]; int slot = path->slots[0];
struct btrfs_key search_key; struct btrfs_key search_key;
struct inode *inode; struct inode *inode;
u64 ino;
int ret = 0; int ret = 0;
btrfs_release_path(path); btrfs_release_path(path);
ino = found_key.offset;
search_key.objectid = found_key.offset; search_key.objectid = found_key.offset;
search_key.type = BTRFS_INODE_ITEM_KEY; search_key.type = BTRFS_INODE_ITEM_KEY;
search_key.offset = 0; 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)) if (IS_ERR(inode))
return PTR_ERR(inode); return PTR_ERR(inode);