@@ -3840,6 +3840,19 @@ static void open_ctree_block_groups_exit(struct btrfs_fs_info *fs_info)
btrfs_free_block_groups(fs_info);
}
+static int open_ctree_fs_root_init(struct btrfs_fs_info *fs_info)
+{
+ fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
+ if (IS_ERR(fs_info->fs_root)) {
+ int ret = PTR_ERR(fs_info->fs_root);
+
+ btrfs_warn(fs_info, "failed to read fs tree: %d", ret);
+ fs_info->fs_root = NULL;
+ return ret;
+ }
+ return 0;
+}
+
struct init_sequence {
int (*init_func)(struct btrfs_fs_info *fs_info);
void (*exit_func)(struct btrfs_fs_info *fs_info);
@@ -3873,6 +3886,17 @@ static const struct init_sequence open_ctree_seq[] = {
}, {
.init_func = open_ctree_block_groups_init,
.exit_func = open_ctree_block_groups_exit,
+ }, {
+ /*
+ * This fs roots related code should be called before anything
+ * which may try to read a subvolume, including cleanup/commit
+ * kthread, qgroup rescan, log replay etc.
+ *
+ * The main reason is for the exit function to be called for
+ * any stage which may read some subvolume trees.
+ */
+ .init_func = open_ctree_fs_root_init,
+ .exit_func = btrfs_free_fs_roots,
}
};
@@ -3933,14 +3957,6 @@ int __cold open_ctree(struct super_block *sb, char *options)
}
}
- fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
- if (IS_ERR(fs_info->fs_root)) {
- err = PTR_ERR(fs_info->fs_root);
- btrfs_warn(fs_info, "failed to read fs tree: %d", err);
- fs_info->fs_root = NULL;
- goto fail_qgroup;
- }
-
if (sb_rdonly(fs_info->sb))
goto clear_oneshot;
@@ -3980,7 +3996,6 @@ int __cold open_ctree(struct super_block *sb, char *options)
fail_trans_kthread:
kthread_stop(fs_info->transaction_kthread);
btrfs_cleanup_transaction(fs_info);
- btrfs_free_fs_roots(fs_info);
fail_cleaner:
kthread_stop(fs_info->cleaner_kthread);
The most important change in this patch is the timing change. The existing code put fs root read very late, after kthread/qgroup-rescan/log-replay, but put btrfs_free_fs_roots() very early, as kthread/qgroup/log-replacey can all populate the fs roots. Thus this patch will change the timing, by reading fs root early. The fs root read part is not that important, but the cleanup part is. After the timing change, the fs root would be the first subvolume to be read, and its exit call can be ensured to cover all later possible subvolume loads. Signed-off-by: Qu Wenruo <wqu@suse.com> --- fs/btrfs/disk-io.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-)