From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 14/16] btrfs: extract kthread code into its own init/exit helpers
Date: Thu, 22 Sep 2022 08:06:31 +0800 [thread overview]
Message-ID: <90da76842c5fe2562299af8493aa429777709dbb.1663804335.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1663804335.git.wqu@suse.com>
There are several changes involved:
- Change the timing of btrfs_cleanup_transaction()
That call is to address any unfinished transaction mostly caused by
the cleaner/commit kthread.
Thus at exit function and error handling path, we should stop all
kthread, then cleanup the unfinished transaction.
Not calling it before stopping cleaner thread.
- Remove the filemap_write_and_wait() call
Now we have open_ctree_btree_inode_exit() call, which will invalidate
all dirty pages of btree inode.
Thus there is no need to writeback those dirtied tree blocks anymore.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
fs/btrfs/disk-io.c | 79 +++++++++++++++++++++++++++++-----------------
1 file changed, 50 insertions(+), 29 deletions(-)
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index a152899fa21a..7d46c9442b0f 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3804,6 +3804,52 @@ static int open_ctree_fs_root_init(struct btrfs_fs_info *fs_info)
return 0;
}
+static int open_ctree_kthread_init(struct btrfs_fs_info *fs_info)
+{
+ int ret;
+
+ fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
+ "btrfs-cleaner");
+ if (IS_ERR(fs_info->cleaner_kthread)) {
+ ret = PTR_ERR(fs_info->cleaner_kthread);
+ return ret;
+ }
+
+ fs_info->transaction_kthread = kthread_run(transaction_kthread,
+ fs_info->tree_root,
+ "btrfs-transaction");
+ if (IS_ERR(fs_info->transaction_kthread)) {
+ kthread_stop(fs_info->cleaner_kthread);
+
+ /*
+ * Cleanup thread may have already started a trans.
+ * The dirtied tree blocks will be invalidated at
+ * open_ctree_btree_inode_exit() thus we don't need to bother.
+ */
+ btrfs_cleanup_transaction(fs_info);
+
+ ret = PTR_ERR(fs_info->cleaner_kthread);
+ return ret;
+ }
+ /*
+ * Mount does not set all options immediately, we can do it now and do
+ * not have to wait for transaction commit
+ */
+ btrfs_apply_pending_changes(fs_info);
+ return 0;
+}
+
+static void open_ctree_kthread_exit(struct btrfs_fs_info *fs_info)
+{
+ kthread_stop(fs_info->transaction_kthread);
+ kthread_stop(fs_info->cleaner_kthread);
+ /*
+ * Cleanup any unfinished transaction started by transaction/cleaner
+ * kthread.
+ */
+ btrfs_cleanup_transaction(fs_info);
+}
+
struct init_sequence {
int (*init_func)(struct btrfs_fs_info *fs_info);
void (*exit_func)(struct btrfs_fs_info *fs_info);
@@ -3848,6 +3894,9 @@ static const struct init_sequence open_ctree_seq[] = {
*/
.init_func = open_ctree_fs_root_init,
.exit_func = btrfs_free_fs_roots,
+ }, {
+ .init_func = open_ctree_kthread_init,
+ .exit_func = open_ctree_kthread_exit,
}
};
@@ -3873,26 +3922,9 @@ int __cold open_ctree(struct super_block *sb, char *options)
open_ctree_res[i] = true;
}
- fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
- "btrfs-cleaner");
- if (IS_ERR(fs_info->cleaner_kthread))
- goto fail;
-
- fs_info->transaction_kthread = kthread_run(transaction_kthread,
- fs_info->tree_root,
- "btrfs-transaction");
- if (IS_ERR(fs_info->transaction_kthread))
- goto fail_cleaner;
-
- /*
- * Mount does not set all options immediately, we can do it now and do
- * not have to wait for transaction commit
- */
- btrfs_apply_pending_changes(fs_info);
-
ret = btrfs_read_qgroup_config(fs_info);
if (ret)
- goto fail_trans_kthread;
+ goto fail;
if (btrfs_build_ref_tree(fs_info))
btrfs_err(fs_info, "couldn't build ref tree");
@@ -3944,17 +3976,6 @@ int __cold open_ctree(struct super_block *sb, char *options)
fail_qgroup:
btrfs_free_qgroup_config(fs_info);
-fail_trans_kthread:
- kthread_stop(fs_info->transaction_kthread);
- btrfs_cleanup_transaction(fs_info);
-fail_cleaner:
- kthread_stop(fs_info->cleaner_kthread);
-
- /*
- * make sure we're done with the btree inode before we stop our
- * kthreads
- */
- filemap_write_and_wait(fs_info->btree_inode->i_mapping);
fail:
for (i = ARRAY_SIZE(open_ctree_seq) - 1; i >= 0; i--) {
--
2.37.3
next prev parent reply other threads:[~2022-09-22 0:07 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-22 0:06 [PATCH 00/16] btrfs: make open_ctree() init/exit sequence strictly matched Qu Wenruo
2022-09-22 0:06 ` [PATCH 01/16] btrfs: make btrfs module init/exit match their sequence Qu Wenruo
2022-09-22 0:06 ` [PATCH 02/16] btrfs: initialize fs_info->sb at the very beginning of open_ctree() Qu Wenruo
2022-09-22 0:06 ` [PATCH 03/16] btrfs: remove @fs_devices argument from open_ctree() Qu Wenruo
2022-09-22 0:06 ` [PATCH 04/16] btrfs: extract btree inode init code into its own init/exit helpers Qu Wenruo
2022-09-22 0:06 ` [PATCH 05/16] btrfs: extract super block read code into its own init helper Qu Wenruo
2022-09-22 0:06 ` [PATCH 06/16] btrfs: extract mount options and features init " Qu Wenruo
2022-09-22 0:28 ` Qu Wenruo
2022-09-22 0:06 ` [PATCH 07/16] btrfs: move btrfs_init_workqueus() and btrfs_stop_all_workers() into open_ctree_seq[] Qu Wenruo
2022-09-22 0:06 ` [PATCH 08/16] btrfs: extract chunk tree read code into its own init/exit helpers Qu Wenruo
2022-09-22 0:06 ` [PATCH 09/16] btrfs: extract tree roots and zone info initialization into " Qu Wenruo
2022-09-22 0:06 ` [PATCH 10/16] btrfs: extract mount time checks and items load code into its init helper Qu Wenruo
2022-09-22 0:06 ` [PATCH 11/16] btrfs: extract sysfs init into its own helper Qu Wenruo
2022-09-22 0:06 ` [PATCH 12/16] btrfs: extra block groups read code into its own init/exit helpers Qu Wenruo
2022-09-22 0:06 ` [PATCH 13/16] btrfs: move the fs root related " Qu Wenruo
2022-09-22 0:06 ` Qu Wenruo [this message]
2022-09-22 0:06 ` [PATCH 15/16] btrfs: move qgroup init/exit code into open_ctree_seq[] array Qu Wenruo
2022-09-22 0:06 ` [PATCH 16/16] btrfs: introduce a debug mount option to do error injection for each stage of open_ctree() Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=90da76842c5fe2562299af8493aa429777709dbb.1663804335.git.wqu@suse.com \
--to=wqu@suse.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).