@@ -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 );
15731575again :
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
0 commit comments