Skip to content

Commit 0202e83

Browse files
committed
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 <[email protected]>
1 parent a820feb commit 0202e83

File tree

11 files changed

+44
-81
lines changed

11 files changed

+44
-81
lines changed

fs/btrfs/ctree.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,10 +2898,9 @@ void btrfs_free_inode(struct inode *inode);
28982898
int btrfs_drop_inode(struct inode *inode);
28992899
int __init btrfs_init_cachep(void);
29002900
void __cold btrfs_destroy_cachep(void);
2901-
struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
2901+
struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
29022902
struct btrfs_root *root, struct btrfs_path *path);
2903-
struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2904-
struct btrfs_root *root);
2903+
struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root);
29052904
struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
29062905
struct page *page, size_t pg_offset,
29072906
u64 start, u64 end);

fs/btrfs/export.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
6464
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
6565
struct btrfs_root *root;
6666
struct inode *inode;
67-
struct btrfs_key key;
6867

6968
if (objectid < BTRFS_FIRST_FREE_OBJECTID)
7069
return ERR_PTR(-ESTALE);
@@ -73,11 +72,7 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
7372
if (IS_ERR(root))
7473
return ERR_CAST(root);
7574

76-
key.objectid = objectid;
77-
key.type = BTRFS_INODE_ITEM_KEY;
78-
key.offset = 0;
79-
80-
inode = btrfs_iget(sb, &key, root);
75+
inode = btrfs_iget(sb, objectid, root);
8176
btrfs_put_root(root);
8277
if (IS_ERR(inode))
8378
return ERR_CAST(inode);
@@ -196,9 +191,7 @@ struct dentry *btrfs_get_parent(struct dentry *child)
196191
found_key.offset, 0, 0);
197192
}
198193

199-
key.type = BTRFS_INODE_ITEM_KEY;
200-
key.offset = 0;
201-
return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root));
194+
return d_obtain_alias(btrfs_iget(fs_info->sb, key.objectid, root));
202195
fail:
203196
btrfs_free_path(path);
204197
return ERR_PTR(ret);

fs/btrfs/file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
275275
{
276276
struct btrfs_root *inode_root;
277277
struct inode *inode;
278-
struct btrfs_key key;
279278
struct btrfs_ioctl_defrag_range_args range;
280279
int num_defrag;
281280
int ret;
@@ -287,10 +286,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
287286
goto cleanup;
288287
}
289288

290-
key.objectid = defrag->ino;
291-
key.type = BTRFS_INODE_ITEM_KEY;
292-
key.offset = 0;
293-
inode = btrfs_iget(fs_info->sb, &key, inode_root);
289+
inode = btrfs_iget(fs_info->sb, defrag->ino, inode_root);
294290
btrfs_put_root(inode_root);
295291
if (IS_ERR(inode)) {
296292
ret = PTR_ERR(inode);

fs/btrfs/free-space-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
8282
* sure NOFS is set to keep us from deadlocking.
8383
*/
8484
nofs_flag = memalloc_nofs_save();
85-
inode = btrfs_iget_path(fs_info->sb, &location, root, path);
85+
inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
8686
btrfs_release_path(path);
8787
memalloc_nofs_restore(nofs_flag);
8888
if (IS_ERR(inode))

fs/btrfs/inode.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#include "block-group.h"
5252

5353
struct btrfs_iget_args {
54-
struct btrfs_key *location;
54+
u64 ino;
5555
struct btrfs_root *root;
5656
};
5757

@@ -2978,7 +2978,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
29782978
found_key.objectid = found_key.offset;
29792979
found_key.type = BTRFS_INODE_ITEM_KEY;
29802980
found_key.offset = 0;
2981-
inode = btrfs_iget(fs_info->sb, &found_key, root);
2981+
inode = btrfs_iget(fs_info->sb, last_objectid, root);
29822982
ret = PTR_ERR_OR_ZERO(inode);
29832983
if (ret && ret != -ENOENT)
29842984
goto out;
@@ -5223,9 +5223,11 @@ static void inode_tree_del(struct inode *inode)
52235223
static int btrfs_init_locked_inode(struct inode *inode, void *p)
52245224
{
52255225
struct btrfs_iget_args *args = p;
5226-
inode->i_ino = args->location->objectid;
5227-
memcpy(&BTRFS_I(inode)->location, args->location,
5228-
sizeof(*args->location));
5226+
5227+
inode->i_ino = args->ino;
5228+
BTRFS_I(inode)->location.objectid = args->ino;
5229+
BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5230+
BTRFS_I(inode)->location.offset = 0;
52295231
BTRFS_I(inode)->root = btrfs_grab_root(args->root);
52305232
BUG_ON(args->root && !BTRFS_I(inode)->root);
52315233
return 0;
@@ -5234,19 +5236,19 @@ static int btrfs_init_locked_inode(struct inode *inode, void *p)
52345236
static int btrfs_find_actor(struct inode *inode, void *opaque)
52355237
{
52365238
struct btrfs_iget_args *args = opaque;
5237-
return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5239+
5240+
return args->ino == BTRFS_I(inode)->location.objectid &&
52385241
args->root == BTRFS_I(inode)->root;
52395242
}
52405243

5241-
static struct inode *btrfs_iget_locked(struct super_block *s,
5242-
struct btrfs_key *location,
5244+
static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
52435245
struct btrfs_root *root)
52445246
{
52455247
struct inode *inode;
52465248
struct btrfs_iget_args args;
5247-
unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5249+
unsigned long hashval = btrfs_inode_hash(ino, root);
52485250

5249-
args.location = location;
5251+
args.ino = ino;
52505252
args.root = root;
52515253

52525254
inode = iget5_locked(s, hashval, btrfs_find_actor,
@@ -5256,17 +5258,17 @@ static struct inode *btrfs_iget_locked(struct super_block *s,
52565258
}
52575259

52585260
/*
5259-
* Get an inode object given its location and corresponding root.
5261+
* Get an inode object given its inode number and corresponding root.
52605262
* Path can be preallocated to prevent recursing back to iget through
52615263
* allocator. NULL is also valid but may require an additional allocation
52625264
* later.
52635265
*/
5264-
struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5266+
struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
52655267
struct btrfs_root *root, struct btrfs_path *path)
52665268
{
52675269
struct inode *inode;
52685270

5269-
inode = btrfs_iget_locked(s, location, root);
5271+
inode = btrfs_iget_locked(s, ino, root);
52705272
if (!inode)
52715273
return ERR_PTR(-ENOMEM);
52725274

@@ -5293,10 +5295,9 @@ struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
52935295
return inode;
52945296
}
52955297

5296-
struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5297-
struct btrfs_root *root)
5298+
struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
52985299
{
5299-
return btrfs_iget_path(s, location, root, NULL);
5300+
return btrfs_iget_path(s, ino, root, NULL);
53005301
}
53015302

53025303
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)
53655366
return ERR_PTR(ret);
53665367

53675368
if (location.type == BTRFS_INODE_ITEM_KEY) {
5368-
inode = btrfs_iget(dir->i_sb, &location, root);
5369+
inode = btrfs_iget(dir->i_sb, location.objectid, root);
53695370
if (IS_ERR(inode))
53705371
return inode;
53715372

@@ -5389,7 +5390,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
53895390
else
53905391
inode = new_simple_dir(dir->i_sb, &location, sub_root);
53915392
} else {
5392-
inode = btrfs_iget(dir->i_sb, &location, sub_root);
5393+
inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
53935394
}
53945395
if (root != sub_root)
53955396
btrfs_put_root(sub_root);
@@ -5770,7 +5771,8 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
57705771
static int btrfs_insert_inode_locked(struct inode *inode)
57715772
{
57725773
struct btrfs_iget_args args;
5773-
args.location = &BTRFS_I(inode)->location;
5774+
5775+
args.ino = BTRFS_I(inode)->location.objectid;
57745776
args.root = BTRFS_I(inode)->root;
57755777

57765778
return insert_inode_locked4(inode,

fs/btrfs/ioctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2424,7 +2424,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
24242424
goto out_put;
24252425
}
24262426

2427-
temp_inode = btrfs_iget(sb, &key2, root);
2427+
temp_inode = btrfs_iget(sb, key2.objectid, root);
24282428
if (IS_ERR(temp_inode)) {
24292429
ret = PTR_ERR(temp_inode);
24302430
goto out_put;

fs/btrfs/props.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -408,19 +408,14 @@ int btrfs_subvol_inherit_props(struct btrfs_trans_handle *trans,
408408
struct btrfs_root *parent_root)
409409
{
410410
struct super_block *sb = root->fs_info->sb;
411-
struct btrfs_key key;
412411
struct inode *parent_inode, *child_inode;
413412
int ret;
414413

415-
key.objectid = BTRFS_FIRST_FREE_OBJECTID;
416-
key.type = BTRFS_INODE_ITEM_KEY;
417-
key.offset = 0;
418-
419-
parent_inode = btrfs_iget(sb, &key, parent_root);
414+
parent_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, parent_root);
420415
if (IS_ERR(parent_inode))
421416
return PTR_ERR(parent_inode);
422417

423-
child_inode = btrfs_iget(sb, &key, root);
418+
child_inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, root);
424419
if (IS_ERR(child_inode)) {
425420
iput(parent_inode);
426421
return PTR_ERR(child_inode);

fs/btrfs/relocation.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,19 +2970,14 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
29702970
struct inode *inode,
29712971
u64 ino)
29722972
{
2973-
struct btrfs_key key;
29742973
struct btrfs_root *root = fs_info->tree_root;
29752974
struct btrfs_trans_handle *trans;
29762975
int ret = 0;
29772976

29782977
if (inode)
29792978
goto truncate;
29802979

2981-
key.objectid = ino;
2982-
key.type = BTRFS_INODE_ITEM_KEY;
2983-
key.offset = 0;
2984-
2985-
inode = btrfs_iget(fs_info->sb, &key, root);
2980+
inode = btrfs_iget(fs_info->sb, ino, root);
29862981
if (IS_ERR(inode))
29872982
return -ENOENT;
29882983

@@ -3470,7 +3465,6 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
34703465
struct inode *inode = NULL;
34713466
struct btrfs_trans_handle *trans;
34723467
struct btrfs_root *root;
3473-
struct btrfs_key key;
34743468
u64 objectid;
34753469
int err = 0;
34763470

@@ -3488,10 +3482,7 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
34883482
err = __insert_orphan_inode(trans, root, objectid);
34893483
BUG_ON(err);
34903484

3491-
key.objectid = objectid;
3492-
key.type = BTRFS_INODE_ITEM_KEY;
3493-
key.offset = 0;
3494-
inode = btrfs_iget(fs_info->sb, &key, root);
3485+
inode = btrfs_iget(fs_info->sb, objectid, root);
34953486
BUG_ON(IS_ERR(inode));
34963487
BTRFS_I(inode)->index_cnt = group->start;
34973488

fs/btrfs/send.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4806,17 +4806,12 @@ static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len)
48064806
struct inode *inode;
48074807
struct page *page;
48084808
char *addr;
4809-
struct btrfs_key key;
48104809
pgoff_t index = offset >> PAGE_SHIFT;
48114810
pgoff_t last_index;
48124811
unsigned pg_offset = offset_in_page(offset);
48134812
ssize_t ret = 0;
48144813

4815-
key.objectid = sctx->cur_ino;
4816-
key.type = BTRFS_INODE_ITEM_KEY;
4817-
key.offset = 0;
4818-
4819-
inode = btrfs_iget(fs_info->sb, &key, root);
4814+
inode = btrfs_iget(fs_info->sb, sctx->cur_ino, root);
48204815
if (IS_ERR(inode))
48214816
return PTR_ERR(inode);
48224817

fs/btrfs/super.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,6 @@ static int btrfs_fill_super(struct super_block *sb,
12171217
{
12181218
struct inode *inode;
12191219
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
1220-
struct btrfs_key key;
12211220
int err;
12221221

12231222
sb->s_maxbytes = MAX_LFS_FILESIZE;
@@ -1245,10 +1244,7 @@ static int btrfs_fill_super(struct super_block *sb,
12451244
return err;
12461245
}
12471246

1248-
key.objectid = BTRFS_FIRST_FREE_OBJECTID;
1249-
key.type = BTRFS_INODE_ITEM_KEY;
1250-
key.offset = 0;
1251-
inode = btrfs_iget(sb, &key, fs_info->fs_root);
1247+
inode = btrfs_iget(sb, BTRFS_FIRST_FREE_OBJECTID, fs_info->fs_root);
12521248
if (IS_ERR(inode)) {
12531249
err = PTR_ERR(inode);
12541250
goto fail_close;

0 commit comments

Comments
 (0)