Skip to content

Commit 56e9357

Browse files
committed
btrfs: simplify root lookup by id
The main function to lookup a root by its id btrfs_get_fs_root takes the whole key, while only using the objectid. The value of offset is preset to (u64)-1 but not actually used until btrfs_find_root that does the actual search. Switch btrfs_get_fs_root to use only objectid and remove all local variables that existed just for the lookup. The actual key for search is set up in btrfs_get_fs_root, reusing another key variable. Signed-off-by: David Sterba <[email protected]>
1 parent 1dae7e0 commit 56e9357

File tree

15 files changed

+46
-113
lines changed

15 files changed

+46
-113
lines changed

fs/btrfs/backref.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,13 @@ static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
538538
const u64 *extent_item_pos, bool ignore_offset)
539539
{
540540
struct btrfs_root *root;
541-
struct btrfs_key root_key;
542541
struct extent_buffer *eb;
543542
int ret = 0;
544543
int root_level;
545544
int level = ref->level;
546545
struct btrfs_key search_key = ref->key_for_search;
547546

548-
root_key.objectid = ref->root_id;
549-
root_key.type = BTRFS_ROOT_ITEM_KEY;
550-
root_key.offset = (u64)-1;
551-
552-
root = btrfs_get_fs_root(fs_info, &root_key, false);
547+
root = btrfs_get_fs_root(fs_info, ref->root_id, false);
553548
if (IS_ERR(root)) {
554549
ret = PTR_ERR(root);
555550
goto out_free;
@@ -2690,16 +2685,12 @@ static int handle_indirect_tree_backref(struct btrfs_backref_cache *cache,
26902685
struct btrfs_backref_edge *edge;
26912686
struct extent_buffer *eb;
26922687
struct btrfs_root *root;
2693-
struct btrfs_key root_key;
26942688
struct rb_node *rb_node;
26952689
int level;
26962690
bool need_check = true;
26972691
int ret;
26982692

2699-
root_key.objectid = ref_key->offset;
2700-
root_key.type = BTRFS_ROOT_ITEM_KEY;
2701-
root_key.offset = (u64)-1;
2702-
root = btrfs_get_fs_root(fs_info, &root_key, false);
2693+
root = btrfs_get_fs_root(fs_info, ref_key->offset, false);
27032694
if (IS_ERR(root))
27042695
return PTR_ERR(root);
27052696
if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state))

fs/btrfs/disk-io.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,35 +1534,34 @@ void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
15341534

15351535

15361536
struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
1537-
struct btrfs_key *location,
1538-
bool check_ref)
1537+
u64 objectid, bool check_ref)
15391538
{
15401539
struct btrfs_root *root;
15411540
struct btrfs_path *path;
15421541
struct btrfs_key key;
15431542
int ret;
15441543

1545-
if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1544+
if (objectid == BTRFS_ROOT_TREE_OBJECTID)
15461545
return btrfs_grab_root(fs_info->tree_root);
1547-
if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1546+
if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
15481547
return btrfs_grab_root(fs_info->extent_root);
1549-
if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1548+
if (objectid == BTRFS_CHUNK_TREE_OBJECTID)
15501549
return btrfs_grab_root(fs_info->chunk_root);
1551-
if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1550+
if (objectid == BTRFS_DEV_TREE_OBJECTID)
15521551
return btrfs_grab_root(fs_info->dev_root);
1553-
if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1552+
if (objectid == BTRFS_CSUM_TREE_OBJECTID)
15541553
return btrfs_grab_root(fs_info->csum_root);
1555-
if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1554+
if (objectid == BTRFS_QUOTA_TREE_OBJECTID)
15561555
return btrfs_grab_root(fs_info->quota_root) ?
15571556
fs_info->quota_root : ERR_PTR(-ENOENT);
1558-
if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
1557+
if (objectid == BTRFS_UUID_TREE_OBJECTID)
15591558
return btrfs_grab_root(fs_info->uuid_root) ?
15601559
fs_info->uuid_root : ERR_PTR(-ENOENT);
1561-
if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1560+
if (objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
15621561
return btrfs_grab_root(fs_info->free_space_root) ?
15631562
fs_info->free_space_root : ERR_PTR(-ENOENT);
15641563
again:
1565-
root = btrfs_lookup_fs_root(fs_info, location->objectid);
1564+
root = btrfs_lookup_fs_root(fs_info, objectid);
15661565
if (root) {
15671566
if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
15681567
btrfs_put_root(root);
@@ -1571,7 +1570,10 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
15711570
return root;
15721571
}
15731572

1574-
root = btrfs_read_tree_root(fs_info->tree_root, location);
1573+
key.objectid = objectid;
1574+
key.type = BTRFS_ROOT_ITEM_KEY;
1575+
key.offset = (u64)-1;
1576+
root = btrfs_read_tree_root(fs_info->tree_root, &key);
15751577
if (IS_ERR(root))
15761578
return root;
15771579

@@ -1591,7 +1593,7 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
15911593
}
15921594
key.objectid = BTRFS_ORPHAN_OBJECTID;
15931595
key.type = BTRFS_ORPHAN_ITEM_KEY;
1594-
key.offset = location->objectid;
1596+
key.offset = objectid;
15951597

15961598
ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
15971599
btrfs_free_path(path);
@@ -2293,8 +2295,8 @@ static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
22932295
* This tree can share blocks with some other fs tree during relocation
22942296
* and we need a proper setup by btrfs_get_fs_root
22952297
*/
2296-
location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
2297-
root = btrfs_get_fs_root(tree_root->fs_info, &location, true);
2298+
root = btrfs_get_fs_root(tree_root->fs_info,
2299+
BTRFS_DATA_RELOC_TREE_OBJECTID, true);
22982300
if (IS_ERR(root)) {
22992301
ret = PTR_ERR(root);
23002302
goto out;
@@ -2839,7 +2841,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
28392841
u64 generation;
28402842
u64 features;
28412843
u16 csum_type;
2842-
struct btrfs_key location;
28432844
struct btrfs_super_block *disk_super;
28442845
struct btrfs_fs_info *fs_info = btrfs_sb(sb);
28452846
struct btrfs_root *tree_root;
@@ -3253,11 +3254,7 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
32533254
}
32543255
}
32553256

3256-
location.objectid = BTRFS_FS_TREE_OBJECTID;
3257-
location.type = BTRFS_ROOT_ITEM_KEY;
3258-
location.offset = 0;
3259-
3260-
fs_info->fs_root = btrfs_get_fs_root(fs_info, &location, true);
3257+
fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
32613258
if (IS_ERR(fs_info->fs_root)) {
32623259
err = PTR_ERR(fs_info->fs_root);
32633260
btrfs_warn(fs_info, "failed to read fs tree: %d", err);

fs/btrfs/disk-io.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
6666
void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info);
6767

6868
struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
69-
struct btrfs_key *key,
70-
bool check_ref);
69+
u64 objectid, bool check_ref);
7170

7271
void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);
7372
int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);

fs/btrfs/export.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,7 @@ struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
6969
if (objectid < BTRFS_FIRST_FREE_OBJECTID)
7070
return ERR_PTR(-ESTALE);
7171

72-
key.objectid = root_objectid;
73-
key.type = BTRFS_ROOT_ITEM_KEY;
74-
key.offset = (u64)-1;
75-
76-
root = btrfs_get_fs_root(fs_info, &key, true);
72+
root = btrfs_get_fs_root(fs_info, root_objectid, true);
7773
if (IS_ERR(root))
7874
return ERR_CAST(root);
7975

fs/btrfs/file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,7 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
281281
int ret;
282282

283283
/* get the inode */
284-
key.objectid = defrag->root;
285-
key.type = BTRFS_ROOT_ITEM_KEY;
286-
key.offset = (u64)-1;
287-
288-
inode_root = btrfs_get_fs_root(fs_info, &key, true);
284+
inode_root = btrfs_get_fs_root(fs_info, defrag->root, true);
289285
if (IS_ERR(inode_root)) {
290286
ret = PTR_ERR(inode_root);
291287
goto cleanup;

fs/btrfs/inode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5145,7 +5145,7 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
51455145

51465146
btrfs_release_path(path);
51475147

5148-
new_root = btrfs_get_fs_root(fs_info, location, true);
5148+
new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
51495149
if (IS_ERR(new_root)) {
51505150
err = PTR_ERR(new_root);
51515151
goto out;

fs/btrfs/ioctl.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ static noinline int create_subvol(struct inode *dir,
660660
goto fail;
661661

662662
key.offset = (u64)-1;
663-
new_root = btrfs_get_fs_root(fs_info, &key, true);
663+
new_root = btrfs_get_fs_root(fs_info, objectid, true);
664664
if (IS_ERR(new_root)) {
665665
ret = PTR_ERR(new_root);
666666
btrfs_abort_transaction(trans, ret);
@@ -2139,10 +2139,7 @@ static noinline int search_ioctl(struct inode *inode,
21392139
/* search the root of the inode that was passed */
21402140
root = btrfs_grab_root(BTRFS_I(inode)->root);
21412141
} else {
2142-
key.objectid = sk->tree_id;
2143-
key.type = BTRFS_ROOT_ITEM_KEY;
2144-
key.offset = (u64)-1;
2145-
root = btrfs_get_fs_root(info, &key, true);
2142+
root = btrfs_get_fs_root(info, sk->tree_id, true);
21462143
if (IS_ERR(root)) {
21472144
btrfs_free_path(path);
21482145
return PTR_ERR(root);
@@ -2275,10 +2272,7 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
22752272

22762273
ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1];
22772274

2278-
key.objectid = tree_id;
2279-
key.type = BTRFS_ROOT_ITEM_KEY;
2280-
key.offset = (u64)-1;
2281-
root = btrfs_get_fs_root(info, &key, true);
2275+
root = btrfs_get_fs_root(info, tree_id, true);
22822276
if (IS_ERR(root)) {
22832277
ret = PTR_ERR(root);
22842278
root = NULL;
@@ -2371,10 +2365,7 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
23712365
if (dirid != upper_limit.objectid) {
23722366
ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1];
23732367

2374-
key.objectid = treeid;
2375-
key.type = BTRFS_ROOT_ITEM_KEY;
2376-
key.offset = (u64)-1;
2377-
root = btrfs_get_fs_root(fs_info, &key, true);
2368+
root = btrfs_get_fs_root(fs_info, treeid, true);
23782369
if (IS_ERR(root)) {
23792370
ret = PTR_ERR(root);
23802371
goto out;
@@ -2620,9 +2611,7 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
26202611

26212612
/* Get root_item of inode's subvolume */
26222613
key.objectid = BTRFS_I(inode)->root->root_key.objectid;
2623-
key.type = BTRFS_ROOT_ITEM_KEY;
2624-
key.offset = (u64)-1;
2625-
root = btrfs_get_fs_root(fs_info, &key, true);
2614+
root = btrfs_get_fs_root(fs_info, key.objectid, true);
26262615
if (IS_ERR(root)) {
26272616
ret = PTR_ERR(root);
26282617
goto out_free;
@@ -3290,7 +3279,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
32903279
struct btrfs_dir_item *di;
32913280
struct btrfs_trans_handle *trans;
32923281
struct btrfs_path *path = NULL;
3293-
struct btrfs_key location;
32943282
struct btrfs_disk_key disk_key;
32953283
u64 objectid = 0;
32963284
u64 dir_id;
@@ -3311,11 +3299,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
33113299
if (!objectid)
33123300
objectid = BTRFS_FS_TREE_OBJECTID;
33133301

3314-
location.objectid = objectid;
3315-
location.type = BTRFS_ROOT_ITEM_KEY;
3316-
location.offset = (u64)-1;
3317-
3318-
new_root = btrfs_get_fs_root(fs_info, &location, true);
3302+
new_root = btrfs_get_fs_root(fs_info, objectid, true);
33193303
if (IS_ERR(new_root)) {
33203304
ret = PTR_ERR(new_root);
33213305
goto out;

fs/btrfs/relocation.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,7 @@ struct btrfs_root *find_reloc_root(struct btrfs_fs_info *fs_info, u64 bytenr)
368368
static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
369369
u64 root_objectid)
370370
{
371-
struct btrfs_key key;
372-
373-
key.objectid = root_objectid;
374-
key.type = BTRFS_ROOT_ITEM_KEY;
375-
key.offset = (u64)-1;
376-
377-
return btrfs_get_fs_root(fs_info, &key, false);
371+
return btrfs_get_fs_root(fs_info, root_objectid, false);
378372
}
379373

380374
/*

fs/btrfs/root-tree.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
210210
struct extent_buffer *leaf;
211211
struct btrfs_path *path;
212212
struct btrfs_key key;
213-
struct btrfs_key root_key;
214213
struct btrfs_root *root;
215214
int err = 0;
216215
int ret;
@@ -223,10 +222,9 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
223222
key.type = BTRFS_ORPHAN_ITEM_KEY;
224223
key.offset = 0;
225224

226-
root_key.type = BTRFS_ROOT_ITEM_KEY;
227-
root_key.offset = (u64)-1;
228-
229225
while (1) {
226+
u64 root_objectid;
227+
230228
ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
231229
if (ret < 0) {
232230
err = ret;
@@ -250,10 +248,10 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
250248
key.type != BTRFS_ORPHAN_ITEM_KEY)
251249
break;
252250

253-
root_key.objectid = key.offset;
251+
root_objectid = key.offset;
254252
key.offset++;
255253

256-
root = btrfs_get_fs_root(fs_info, &root_key, false);
254+
root = btrfs_get_fs_root(fs_info, root_objectid, false);
257255
err = PTR_ERR_OR_ZERO(root);
258256
if (err && err != -ENOENT) {
259257
break;
@@ -270,7 +268,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
270268
break;
271269
}
272270
err = btrfs_del_orphan_item(trans, tree_root,
273-
root_key.objectid);
271+
root_objectid);
274272
btrfs_end_transaction(trans);
275273
if (err) {
276274
btrfs_handle_fs_error(fs_info, err,

fs/btrfs/scrub.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -647,13 +647,9 @@ static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
647647
struct btrfs_fs_info *fs_info = swarn->dev->fs_info;
648648
struct inode_fs_paths *ipath = NULL;
649649
struct btrfs_root *local_root;
650-
struct btrfs_key root_key;
651650
struct btrfs_key key;
652651

653-
root_key.objectid = root;
654-
root_key.type = BTRFS_ROOT_ITEM_KEY;
655-
root_key.offset = (u64)-1;
656-
local_root = btrfs_get_fs_root(fs_info, &root_key, true);
652+
local_root = btrfs_get_fs_root(fs_info, root, true);
657653
if (IS_ERR(local_root)) {
658654
ret = PTR_ERR(local_root);
659655
goto err;

0 commit comments

Comments
 (0)