Skip to content

Commit bc44d7c

Browse files
josefbacikkdave
authored andcommitted
btrfs: push btrfs_grab_fs_root into btrfs_get_fs_root
Now that all callers of btrfs_get_fs_root are subsequently calling btrfs_grab_fs_root and handling dropping the ref when they are done appropriately, go ahead and push btrfs_grab_fs_root up into btrfs_get_fs_root. Signed-off-by: Josef Bacik <josef@toxicpanda.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 81f096e commit bc44d7c

14 files changed

Lines changed: 27 additions & 106 deletions

File tree

‎fs/btrfs/backref.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,12 +527,6 @@ static int resolve_indirect_ref(struct btrfs_fs_info *fs_info,
527527
goto out_free;
528528
}
529529

530-
if (!btrfs_grab_fs_root(root)) {
531-
srcu_read_unlock(&fs_info->subvol_srcu, index);
532-
ret = -ENOENT;
533-
goto out_free;
534-
}
535-
536530
if (btrfs_is_testing(fs_info)) {
537531
srcu_read_unlock(&fs_info->subvol_srcu, index);
538532
ret = -ENOENT;

‎fs/btrfs/disk-io.c‎

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,8 @@ static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
14961496
spin_lock(&fs_info->fs_roots_radix_lock);
14971497
root = radix_tree_lookup(&fs_info->fs_roots_radix,
14981498
(unsigned long)root_id);
1499+
if (root)
1500+
root = btrfs_grab_fs_root(root);
14991501
spin_unlock(&fs_info->fs_roots_radix_lock);
15001502
return root;
15011503
}
@@ -1552,29 +1554,31 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
15521554
int ret;
15531555

15541556
if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1555-
return fs_info->tree_root;
1557+
return btrfs_grab_fs_root(fs_info->tree_root);
15561558
if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
1557-
return fs_info->extent_root;
1559+
return btrfs_grab_fs_root(fs_info->extent_root);
15581560
if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
1559-
return fs_info->chunk_root;
1561+
return btrfs_grab_fs_root(fs_info->chunk_root);
15601562
if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
1561-
return fs_info->dev_root;
1563+
return btrfs_grab_fs_root(fs_info->dev_root);
15621564
if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
1563-
return fs_info->csum_root;
1565+
return btrfs_grab_fs_root(fs_info->csum_root);
15641566
if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
1565-
return fs_info->quota_root ? fs_info->quota_root :
1566-
ERR_PTR(-ENOENT);
1567+
return btrfs_grab_fs_root(fs_info->quota_root) ?
1568+
fs_info->quota_root : ERR_PTR(-ENOENT);
15671569
if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
1568-
return fs_info->uuid_root ? fs_info->uuid_root :
1569-
ERR_PTR(-ENOENT);
1570+
return btrfs_grab_fs_root(fs_info->uuid_root) ?
1571+
fs_info->uuid_root : ERR_PTR(-ENOENT);
15701572
if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
1571-
return fs_info->free_space_root ? fs_info->free_space_root :
1572-
ERR_PTR(-ENOENT);
1573+
return btrfs_grab_fs_root(fs_info->free_space_root) ?
1574+
fs_info->free_space_root : ERR_PTR(-ENOENT);
15731575
again:
15741576
root = btrfs_lookup_fs_root(fs_info, location->objectid);
15751577
if (root) {
1576-
if (check_ref && btrfs_root_refs(&root->root_item) == 0)
1578+
if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
1579+
btrfs_put_fs_root(root);
15771580
return ERR_PTR(-ENOENT);
1581+
}
15781582
return root;
15791583
}
15801584

@@ -1607,8 +1611,18 @@ struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
16071611
if (ret == 0)
16081612
set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
16091613

1614+
/*
1615+
* All roots have two refs on them at all times, one for the mounted fs,
1616+
* and one for being in the radix tree. This way we only free the root
1617+
* when we are unmounting or deleting the subvolume. We get one ref
1618+
* from __setup_root, one for inserting it into the radix tree, and then
1619+
* we have the third for returning it, and the caller will put it when
1620+
* it's done with the root.
1621+
*/
1622+
btrfs_grab_fs_root(root);
16101623
ret = btrfs_insert_fs_root(fs_info, root);
16111624
if (ret) {
1625+
btrfs_put_fs_root(root);
16121626
if (ret == -EEXIST) {
16131627
btrfs_free_fs_root(root);
16141628
goto again;
@@ -3207,13 +3221,6 @@ int __cold open_ctree(struct super_block *sb,
32073221
goto fail_qgroup;
32083222
}
32093223

3210-
if (!btrfs_grab_fs_root(fs_info->fs_root)) {
3211-
fs_info->fs_root = NULL;
3212-
err = -ENOENT;
3213-
btrfs_warn(fs_info, "failed to grab a ref on the fs tree");
3214-
goto fail_qgroup;
3215-
}
3216-
32173224
if (sb_rdonly(sb))
32183225
return 0;
32193226

‎fs/btrfs/export.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
8282
err = PTR_ERR(root);
8383
goto fail;
8484
}
85-
if (!btrfs_grab_fs_root(root)) {
86-
err = -ENOENT;
87-
goto fail;
88-
}
8985

9086
key.objectid = objectid;
9187
key.type = BTRFS_INODE_ITEM_KEY;

‎fs/btrfs/file.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,6 @@ static int __btrfs_run_defrag_inode(struct btrfs_fs_info *fs_info,
292292
ret = PTR_ERR(inode_root);
293293
goto cleanup;
294294
}
295-
if (!btrfs_grab_fs_root(inode_root)) {
296-
ret = -ENOENT;
297-
goto cleanup;
298-
}
299295

300296
key.objectid = defrag->ino;
301297
key.type = BTRFS_INODE_ITEM_KEY;

‎fs/btrfs/inode.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5158,10 +5158,6 @@ static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
51585158
err = PTR_ERR(new_root);
51595159
goto out;
51605160
}
5161-
if (!btrfs_grab_fs_root(new_root)) {
5162-
err = -ENOENT;
5163-
goto out;
5164-
}
51655161

51665162
*sub_root = new_root;
51675163
location->objectid = btrfs_root_dirid(&new_root->root_item);

‎fs/btrfs/ioctl.c‎

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -672,11 +672,6 @@ static noinline int create_subvol(struct inode *dir,
672672
btrfs_abort_transaction(trans, ret);
673673
goto fail;
674674
}
675-
if (!btrfs_grab_fs_root(new_root)) {
676-
ret = -ENOENT;
677-
btrfs_abort_transaction(trans, ret);
678-
goto fail;
679-
}
680675

681676
btrfs_record_root_in_trans(trans, new_root);
682677

@@ -2191,10 +2186,6 @@ static noinline int search_ioctl(struct inode *inode,
21912186
btrfs_free_path(path);
21922187
return PTR_ERR(root);
21932188
}
2194-
if (!btrfs_grab_fs_root(root)) {
2195-
btrfs_free_path(path);
2196-
return -ENOENT;
2197-
}
21982189
}
21992190

22002191
key.objectid = sk->min_objectid;
@@ -2332,11 +2323,6 @@ static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
23322323
root = NULL;
23332324
goto out;
23342325
}
2335-
if (!btrfs_grab_fs_root(root)) {
2336-
ret = -ENOENT;
2337-
root = NULL;
2338-
goto out;
2339-
}
23402326

23412327
key.objectid = dirid;
23422328
key.type = BTRFS_INODE_REF_KEY;
@@ -2432,10 +2418,6 @@ static int btrfs_search_path_in_tree_user(struct inode *inode,
24322418
ret = PTR_ERR(root);
24332419
goto out;
24342420
}
2435-
if (!btrfs_grab_fs_root(root)) {
2436-
ret = -ENOENT;
2437-
goto out;
2438-
}
24392421

24402422
key.objectid = dirid;
24412423
key.type = BTRFS_INODE_REF_KEY;
@@ -2684,10 +2666,6 @@ static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp)
26842666
ret = PTR_ERR(root);
26852667
goto out_free;
26862668
}
2687-
if (!btrfs_grab_fs_root(root)) {
2688-
ret = -ENOENT;
2689-
goto out;
2690-
}
26912669
root_item = &root->root_item;
26922670

26932671
subvol_info->treeid = key.objectid;
@@ -4018,10 +3996,6 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
40183996
ret = PTR_ERR(new_root);
40193997
goto out;
40203998
}
4021-
if (!btrfs_grab_fs_root(new_root)) {
4022-
ret = -ENOENT;
4023-
goto out;
4024-
}
40253999
if (!is_fstree(new_root->root_key.objectid)) {
40264000
ret = -ENOENT;
40274001
goto out_free;

‎fs/btrfs/relocation.c‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
659659
u64 root_objectid)
660660
{
661661
struct btrfs_key key;
662-
struct btrfs_root *root;
663662

664663
key.objectid = root_objectid;
665664
key.type = BTRFS_ROOT_ITEM_KEY;
@@ -668,12 +667,7 @@ static struct btrfs_root *read_fs_root(struct btrfs_fs_info *fs_info,
668667
else
669668
key.offset = (u64)-1;
670669

671-
root = btrfs_get_fs_root(fs_info, &key, false);
672-
if (IS_ERR(root))
673-
return root;
674-
if (!btrfs_grab_fs_root(root))
675-
return ERR_PTR(-ENOENT);
676-
return root;
670+
return btrfs_get_fs_root(fs_info, &key, false);
677671
}
678672

679673
static noinline_for_stack

‎fs/btrfs/root-tree.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,6 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
257257

258258
root = btrfs_get_fs_root(fs_info, &root_key, false);
259259
err = PTR_ERR_OR_ZERO(root);
260-
if (!err && !btrfs_grab_fs_root(root))
261-
err = -ENOENT;
262260
if (err && err != -ENOENT) {
263261
break;
264262
} else if (err == -ENOENT) {

‎fs/btrfs/scrub.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,6 @@ static int scrub_print_warning_inode(u64 inum, u64 offset, u64 root,
658658
ret = PTR_ERR(local_root);
659659
goto err;
660660
}
661-
if (!btrfs_grab_fs_root(local_root)) {
662-
ret = -ENOENT;
663-
goto err;
664-
}
665661

666662
/*
667663
* this makes the path point to (inum INODE_ITEM ioff)

‎fs/btrfs/send.c‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7201,11 +7201,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
72017201
ret = PTR_ERR(clone_root);
72027202
goto out;
72037203
}
7204-
if (!btrfs_grab_fs_root(clone_root)) {
7205-
srcu_read_unlock(&fs_info->subvol_srcu, index);
7206-
ret = -ENOENT;
7207-
goto out;
7208-
}
72097204
spin_lock(&clone_root->root_item_lock);
72107205
if (!btrfs_root_readonly(clone_root) ||
72117206
btrfs_root_dead(clone_root)) {
@@ -7247,12 +7242,6 @@ long btrfs_ioctl_send(struct file *mnt_file, struct btrfs_ioctl_send_args *arg)
72477242
ret = PTR_ERR(sctx->parent_root);
72487243
goto out;
72497244
}
7250-
if (!btrfs_grab_fs_root(sctx->parent_root)) {
7251-
srcu_read_unlock(&fs_info->subvol_srcu, index);
7252-
ret = -ENOENT;
7253-
sctx->parent_root = ERR_PTR(ret);
7254-
goto out;
7255-
}
72567245

72577246
spin_lock(&sctx->parent_root->root_item_lock);
72587247
sctx->parent_root->send_in_progress++;

0 commit comments

Comments
 (0)