@@ -1266,6 +1266,8 @@ static void free_global_roots(struct btrfs_fs_info *fs_info)
void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
{
+ if (fs_info->fs_devices)
+ btrfs_close_devices(fs_info->fs_devices);
percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
percpu_counter_destroy(&fs_info->delalloc_bytes);
percpu_counter_destroy(&fs_info->ordered_bytes);
@@ -3609,7 +3611,6 @@ int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_device
iput(fs_info->btree_inode);
fail:
- btrfs_close_devices(fs_info->fs_devices);
ASSERT(ret < 0);
return ret;
}
@@ -4389,7 +4390,6 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
iput(fs_info->btree_inode);
btrfs_mapping_tree_free(fs_info);
- btrfs_close_devices(fs_info->fs_devices);
}
void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
@@ -1822,10 +1822,8 @@ static int btrfs_get_tree_super(struct fs_context *fc)
if (ret)
return ret;
- if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0) {
- ret = -EACCES;
- goto error;
- }
+ if (!(fc->sb_flags & SB_RDONLY) && fs_devices->rw_devices == 0)
+ return -EACCES;
bdev = fs_devices->latest_dev->bdev;
@@ -1839,15 +1837,12 @@ static int btrfs_get_tree_super(struct fs_context *fc)
* otherwise it's tied to the lifetime of the super_block.
*/
sb = sget_fc(fc, btrfs_fc_test_super, set_anon_super_fc);
- if (IS_ERR(sb)) {
- ret = PTR_ERR(sb);
- goto error;
- }
+ if (IS_ERR(sb))
+ return PTR_ERR(sb);
set_device_specific_options(fs_info);
if (sb->s_root) {
- btrfs_close_devices(fs_devices);
if ((fc->sb_flags ^ sb->s_flags) & SB_RDONLY)
ret = -EBUSY;
} else {
@@ -1866,10 +1861,6 @@ static int btrfs_get_tree_super(struct fs_context *fc)
fc->root = dget(sb->s_root);
return 0;
-
-error:
- btrfs_close_devices(fs_devices);
- return ret;
}
/*
@@ -1962,10 +1953,20 @@ static int btrfs_get_tree_super(struct fs_context *fc)
*/
static struct vfsmount *btrfs_reconfigure_for_mount(struct fs_context *fc)
{
+ struct btrfs_fs_info *fs_info = fc->s_fs_info;
struct vfsmount *mnt;
int ret;
const bool ro2rw = !(fc->sb_flags & SB_RDONLY);
+ /*
+ * We got a reference to our fs_devices, so we need to close it here to
+ * make sure we don't leak our reference on the fs_devices.
+ */
+ if (fs_info->fs_devices) {
+ btrfs_close_devices(fs_info->fs_devices);
+ fs_info->fs_devices = NULL;
+ }
+
/*
* We got an EBUSY because our SB_RDONLY flag didn't match the existing
* super block, so invert our setting here and retry the mount so we