linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 16/16] btrfs: introduce a debug mount option to do error injection for each stage of open_ctree()
Date: Thu, 22 Sep 2022 08:06:33 +0800	[thread overview]
Message-ID: <4cbeba91ff6429eb9f47e16280456db139572d1b.1663804335.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1663804335.git.wqu@suse.com>

With the new open_ctree_seq[] array, we can afford a debug mount option
to do all the error inject at different stages to have a much better
coverage for the error path.

The new "fail_mount=%u" mount option will be hidden behind
CONFIG_BTRFS_DEBUG option, and when enabled it will cause mount failure
just after the init function of specified stage.

This can be verified by the following script:

 mkfs.btrfs -f $dev
 for (( i=0;; i++ )) do
	mount -o fail_mount=$i $dev $mnt
	ret=$?
	if [ $ret -eq 0 ]; then
		umount $mnt
		exit
	fi
 done
 umount $mnt

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/ctree.h   |  1 +
 fs/btrfs/disk-io.c | 14 ++++++++++++++
 fs/btrfs/super.c   | 13 +++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 88dc3b2896d7..7908027a6bb6 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1123,6 +1123,7 @@ struct btrfs_fs_info {
 
 	spinlock_t eb_leak_lock;
 	struct list_head allocated_ebs;
+	int fail_stage;
 #endif
 };
 
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 14dcec9a23aa..66d45521a9df 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -3013,6 +3013,7 @@ void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
 	INIT_LIST_HEAD(&fs_info->allocated_roots);
 	INIT_LIST_HEAD(&fs_info->allocated_ebs);
 	spin_lock_init(&fs_info->eb_leak_lock);
+	fs_info->fail_stage = -1;
 #endif
 	extent_map_tree_init(&fs_info->mapping_tree);
 	btrfs_init_block_rsv(&fs_info->global_block_rsv,
@@ -3922,6 +3923,19 @@ int __cold open_ctree(struct super_block *sb, char *options)
 		if (ret < 0)
 			goto fail;
 		open_ctree_res[i] = true;
+#ifdef CONFIG_BTRFS_DEBUG
+		/*
+		 * This is not the best timing, as fail_stage will only be
+		 * initialized after open_ctree_features_init().
+		 * But this is still better to cover more error paths.
+		 */
+		if (fs_info->fail_stage >= 0 && i >= fs_info->fail_stage) {
+			btrfs_info(fs_info,
+				"error injected at open ctree stage %u", i);
+			ret = -ECANCELED;
+			goto fail;
+		}
+#endif
 	}
 
 	if (btrfs_build_ref_tree(fs_info))
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 9d86b7ef9e43..76e6ca9ea98d 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -447,6 +447,7 @@ enum {
 	Opt_enospc_debug, Opt_noenospc_debug,
 #ifdef CONFIG_BTRFS_DEBUG
 	Opt_fragment_data, Opt_fragment_metadata, Opt_fragment_all,
+	Opt_fail_mount,
 #endif
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	Opt_ref_verify,
@@ -521,6 +522,7 @@ static const match_table_t tokens = {
 	{Opt_fragment_data, "fragment=data"},
 	{Opt_fragment_metadata, "fragment=metadata"},
 	{Opt_fragment_all, "fragment=all"},
+	{Opt_fail_mount, "fail_mount=%u"},
 #endif
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 	{Opt_ref_verify, "ref_verify"},
@@ -1106,6 +1108,17 @@ int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
 			btrfs_info(info, "fragmenting data");
 			btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
 			break;
+		case Opt_fail_mount:
+			ret = match_int(&args[0], &intarg);
+			if (ret) {
+				btrfs_err(info, "unrecognized fail_mount value %s",
+					  args[0].from);
+				goto out;
+			}
+			btrfs_info(info, "fail mount at open_ctree() stage %u",
+				   intarg);
+			info->fail_stage = intarg;
+			break;
 #endif
 #ifdef CONFIG_BTRFS_FS_REF_VERIFY
 		case Opt_ref_verify:
-- 
2.37.3


      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 ` [PATCH 14/16] btrfs: extract kthread " Qu Wenruo
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 ` Qu Wenruo [this message]

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=4cbeba91ff6429eb9f47e16280456db139572d1b.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).