public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Youling Tang <youling.tang@linux.dev>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
	Christian Brauner <brauner@kernel.org>, Jan Kara <jack@suse.cz>,
	Chris Mason <clm@fb.com>, Josef Bacik <josef@toxicpanda.com>,
	David Sterba <dsterba@suse.com>,
	tytso@mit.edu, Andreas Dilger <adilger.kernel@dilger.ca>,
	Jaegeuk Kim <jaegeuk@kernel.org>, Chao Yu <chao@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, linux-btrfs@vger.kernel.org,
	linux-ext4@vger.kernel.org,
	linux-f2fs-devel@lists.sourceforge.net,
	linux-kernel@vger.kernel.org, youling.tang@linux.dev,
	Youling Tang <tangyouling@kylinos.cn>
Subject: [PATCH 2/3] ext4: make ext4 init/exit match their sequence
Date: Thu, 11 Jul 2024 15:48:58 +0800	[thread overview]
Message-ID: <20240711074859.366088-3-youling.tang@linux.dev> (raw)
In-Reply-To: <20240711074859.366088-1-youling.tang@linux.dev>

From: Youling Tang <tangyouling@kylinos.cn>

Use init_sequence to ensure that modules init and exit are in sequence
and to simplify the code.

Signed-off-by: Youling Tang <tangyouling@kylinos.cn>
---
 fs/ext4/super.c | 175 +++++++++++++++++++++++++-----------------------
 1 file changed, 93 insertions(+), 82 deletions(-)

diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index c682fb927b64..ec1e63facb10 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -7314,103 +7314,114 @@ static struct file_system_type ext4_fs_type = {
 };
 MODULE_ALIAS_FS("ext4");
 
-/* Shared across all ext4 file systems */
-wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
-
-static int __init ext4_init_fs(void)
+static int register_ext(void)
 {
-	int i, err;
-
-	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
-	ext4_li_info = NULL;
+	register_as_ext3();
+	register_as_ext2();
+	return register_filesystem(&ext4_fs_type);
+}
 
-	/* Build-time check for flags consistency */
-	ext4_check_flag_values();
+static void unregister_ext(void)
+{
+	unregister_as_ext2();
+	unregister_as_ext3();
+	unregister_filesystem(&ext4_fs_type);
+}
 
-	for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
-		init_waitqueue_head(&ext4__ioend_wq[i]);
+/* Helper structure for long init/exit functions. */
+struct init_sequence {
+	int (*init_func)(void);
+	/* Can be NULL if the init_func doesn't need cleanup. */
+	void (*exit_func)(void);
+};
 
-	err = ext4_init_es();
-	if (err)
-		return err;
+static const struct init_sequence mod_init_seq[] = {
+	{
+		.init_func = ext4_init_es,
+		.exit_func = ext4_exit_es,
+	}, {
+		.init_func = ext4_init_pending,
+		.exit_func = ext4_exit_pending,
+	}, {
+		.init_func = ext4_init_post_read_processing,
+		.exit_func = ext4_exit_post_read_processing,
+	}, {
+		.init_func = ext4_init_pageio,
+		.exit_func = ext4_exit_pageio,
+	}, {
+		.init_func = ext4_init_system_zone,
+		.exit_func = ext4_exit_system_zone,
+	}, {
+		.init_func = ext4_init_sysfs,
+		.exit_func = ext4_exit_sysfs,
+	}, {
+		.init_func = ext4_init_mballoc,
+		.exit_func = ext4_exit_mballoc,
+	}, {
+		.init_func = init_inodecache,
+		.exit_func = destroy_inodecache,
+	}, {
+		.init_func = ext4_fc_init_dentry_cache,
+		.exit_func = ext4_fc_destroy_dentry_cache,
+	}, {
+		.init_func = register_ext,
+		.exit_func = unregister_ext,
+	}
+};
 
-	err = ext4_init_pending();
-	if (err)
-		goto out7;
+static bool mod_init_result[ARRAY_SIZE(mod_init_seq)];
 
-	err = ext4_init_post_read_processing();
-	if (err)
-		goto out6;
+static __always_inline void ext4_exit_ext4_fs(void)
+{
+	int i;
 
-	err = ext4_init_pageio();
-	if (err)
-		goto out5;
+	for (i = ARRAY_SIZE(mod_init_seq) - 1; i >= 0; i--) {
+		if (!mod_init_result[i])
+			continue;
+		if (mod_init_seq[i].exit_func)
+			mod_init_seq[i].exit_func();
+		mod_init_result[i] = false;
+	}
+}
 
-	err = ext4_init_system_zone();
-	if (err)
-		goto out4;
+static void __exit ext4_exit_fs(void)
+{
+	ext4_destroy_lazyinit_thread();
+	ext4_exit_ext4_fs();
+}
 
-	err = ext4_init_sysfs();
-	if (err)
-		goto out3;
+static __always_inline int ext4_init_ext4_fs(void)
+{
+	int ret;
+	int i;
 
-	err = ext4_init_mballoc();
-	if (err)
-		goto out2;
-	err = init_inodecache();
-	if (err)
-		goto out1;
+	for (i = 0; i < ARRAY_SIZE(mod_init_seq); i++) {
+		ASSERT(!mod_init_result[i]);
+		ret = mod_init_seq[i].init_func();
+		if (ret < 0) {
+			ext4_exit_ext4_fs();
+			return ret;
+		}
+		mod_init_result[i] = true;
+	}
+	return 0;
+}
 
-	err = ext4_fc_init_dentry_cache();
-	if (err)
-		goto out05;
+/* Shared across all ext4 file systems */
+wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
 
-	register_as_ext3();
-	register_as_ext2();
-	err = register_filesystem(&ext4_fs_type);
-	if (err)
-		goto out;
+static int __init ext4_init_fs(void)
+{
+	ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
+	ext4_li_info = NULL;
 
-	return 0;
-out:
-	unregister_as_ext2();
-	unregister_as_ext3();
-	ext4_fc_destroy_dentry_cache();
-out05:
-	destroy_inodecache();
-out1:
-	ext4_exit_mballoc();
-out2:
-	ext4_exit_sysfs();
-out3:
-	ext4_exit_system_zone();
-out4:
-	ext4_exit_pageio();
-out5:
-	ext4_exit_post_read_processing();
-out6:
-	ext4_exit_pending();
-out7:
-	ext4_exit_es();
+	/* Build-time check for flags consistency */
+	ext4_check_flag_values();
 
-	return err;
-}
+	for (int i = 0; i < EXT4_WQ_HASH_SZ; i++)
+		init_waitqueue_head(&ext4__ioend_wq[i]);
 
-static void __exit ext4_exit_fs(void)
-{
-	ext4_destroy_lazyinit_thread();
-	unregister_as_ext2();
-	unregister_as_ext3();
-	unregister_filesystem(&ext4_fs_type);
-	ext4_fc_destroy_dentry_cache();
-	destroy_inodecache();
-	ext4_exit_mballoc();
-	ext4_exit_sysfs();
-	ext4_exit_system_zone();
-	ext4_exit_pageio();
-	ext4_exit_post_read_processing();
-	ext4_exit_es();
-	ext4_exit_pending();
+	return ext4_init_ext4_fs();
 }
 
 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
-- 
2.34.1


  parent reply	other threads:[~2024-07-11  7:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-11  7:48 [PATCH 0/3] Add {init, exit}_sequence_fs() helper function Youling Tang
2024-07-11  7:48 ` [PATCH 1/3] f2fs: make module init/exit match their sequence Youling Tang
2024-07-11  7:48 ` Youling Tang [this message]
2024-07-11  7:48 ` [PATCH 3/3] fs: Add {init, exit}_sequence_fs() helper functions Youling Tang
2024-07-11  8:26 ` [PATCH 0/3] Add {init, exit}_sequence_fs() helper function Christoph Hellwig
2024-07-23  8:44   ` Youling Tang
2024-07-23 13:37     ` Christoph Hellwig

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=20240711074859.366088-3-youling.tang@linux.dev \
    --to=youling.tang@linux.dev \
    --cc=adilger.kernel@dilger.ca \
    --cc=brauner@kernel.org \
    --cc=chao@kernel.org \
    --cc=clm@fb.com \
    --cc=dsterba@suse.com \
    --cc=jack@suse.cz \
    --cc=jaegeuk@kernel.org \
    --cc=josef@toxicpanda.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tangyouling@kylinos.cn \
    --cc=tytso@mit.edu \
    --cc=viro@zeniv.linux.org.uk \
    /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