@@ -3815,6 +3815,31 @@ static void open_ctree_sysfs_exit(struct btrfs_fs_info *fs_info)
btrfs_sysfs_remove_fsid(fs_info->fs_devices);
}
+static int open_ctree_block_groups_init(struct btrfs_fs_info *fs_info)
+{
+ int ret;
+
+ ret = btrfs_read_block_groups(fs_info);
+
+ /*
+ * Even if btrfs_read_block_groups() failed, we may still have
+ * inserted some block groups.
+ * Thus we have to do cleanup here manually for error path, as
+ * our exit function won't be executed for error path.
+ */
+ if (ret < 0) {
+ btrfs_put_block_group_cache(fs_info);
+ btrfs_free_block_groups(fs_info);
+ }
+ return ret;
+}
+
+static void open_ctree_block_groups_exit(struct btrfs_fs_info *fs_info)
+{
+ btrfs_put_block_group_cache(fs_info);
+ btrfs_free_block_groups(fs_info);
+}
+
struct init_sequence {
int (*init_func)(struct btrfs_fs_info *fs_info);
void (*exit_func)(struct btrfs_fs_info *fs_info);
@@ -3845,6 +3870,9 @@ static const struct init_sequence open_ctree_seq[] = {
}, {
.init_func = open_ctree_sysfs_init,
.exit_func = open_ctree_sysfs_exit,
+ }, {
+ .init_func = open_ctree_block_groups_init,
+ .exit_func = open_ctree_block_groups_exit,
}
};
@@ -3870,16 +3898,10 @@ int __cold open_ctree(struct super_block *sb, char *options)
open_ctree_res[i] = true;
}
- ret = btrfs_read_block_groups(fs_info);
- if (ret) {
- btrfs_err(fs_info, "failed to read block groups: %d", ret);
- goto fail_sysfs;
- }
-
fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
"btrfs-cleaner");
if (IS_ERR(fs_info->cleaner_kthread))
- goto fail_sysfs;
+ goto fail;
fs_info->transaction_kthread = kthread_run(transaction_kthread,
fs_info->tree_root,
@@ -3968,9 +3990,6 @@ int __cold open_ctree(struct super_block *sb, char *options)
*/
filemap_write_and_wait(fs_info->btree_inode->i_mapping);
-fail_sysfs:
- btrfs_put_block_group_cache(fs_info);
- btrfs_free_block_groups(fs_info);
fail:
for (i = ARRAY_SIZE(open_ctree_seq) - 1; i >= 0; i--) {
if (!open_ctree_res[i] || !open_ctree_seq[i].exit_func)
The only special handling is: - Need error cleanup even in open_ctree_block_group_init() As btrfs_read_block_groups() can error out with some block groups already inserted. Thus here we have to do the cleanup manually, as exit helper will not be called if the init helper failed. Signed-off-by: Qu Wenruo <wqu@suse.com> --- fs/btrfs/disk-io.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-)