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 3/7] btrfs-progs: cmds/tune: add set support for free-space-tree feature
Date: Tue,  5 Sep 2023 15:51:45 +0800	[thread overview]
Message-ID: <5bee36f6d2bac339edb47d6c7dede061fa3a12f3.1693900169.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1693900169.git.wqu@suse.com>

This patch allows "btrfs tune set" to enable free-space-tree feature,
using pretty much the same code from btrfstune.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 cmds/tune.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)

diff --git a/cmds/tune.c b/cmds/tune.c
index 92c7b9f1502c..9906cde393f4 100644
--- a/cmds/tune.c
+++ b/cmds/tune.c
@@ -17,11 +17,13 @@
 #include <unistd.h>
 #include "kerncompat.h"
 #include "cmds/commands.h"
+#include "check/clear-cache.h"
 #include "common/help.h"
 #include "common/fsfeatures.h"
 #include "kernel-shared/messages.h"
 #include "kernel-shared/disk-io.h"
 #include "kernel-shared/transaction.h"
+#include "kernel-shared/free-space-tree.h"
 
 static const char * const cmd_tune_set_usage[] = {
 	"btrfs tune set <feature> [<device>]",
@@ -57,6 +59,15 @@ static const struct btrfs_feature set_features[] = {
 		VERSION_TO_STRING2(safe, 4,0),
 		VERSION_TO_STRING2(default, 5,15),
 		.desc		= "no explicit hole extents for files"
+	}, {
+		.name		= "free-space-tree",
+		.compat_ro_flag	= BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE |
+				  BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID,
+		.sysfs_name = "free_space_tree",
+		VERSION_TO_STRING2(compat, 4,5),
+		VERSION_TO_STRING2(safe, 4,9),
+		VERSION_TO_STRING2(default, 5,15),
+		.desc		= "free space tree (space_cache=v2)"
 	},
 	/* Keep this one last */
 	{
@@ -134,6 +145,36 @@ static int set_super_incompat_flags(struct btrfs_fs_info *fs_info, u64 flags)
 	return ret;
 }
 
+static int convert_to_fst(struct btrfs_fs_info *fs_info)
+{
+	int ret;
+
+	/* We may have invalid old v2 cache, clear them first. */
+	if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
+		ret = btrfs_clear_free_space_tree(fs_info);
+		if (ret < 0) {
+			errno = -ret;
+			error("failed to clear stale v2 free space cache: %m");
+			return ret;
+		}
+	}
+	ret = btrfs_clear_v1_cache(fs_info);
+	if (ret < 0) {
+		errno = -ret;
+		error("failed to clear v1 free space cache: %m");
+		return ret;
+	}
+
+	ret = btrfs_create_free_space_tree(fs_info);
+	if (ret < 0) {
+		errno = -ret;
+		error("failed to create free space tree: %m");
+		return ret;
+	}
+	pr_verbose(LOG_DEFAULT, "Converted to free space tree feature\n");
+	return ret;
+}
+
 static int cmd_tune_set(const struct cmd_struct *cmd, int argc, char **argv)
 {
 	struct btrfs_fs_info *fs_info;
@@ -202,6 +243,17 @@ static int cmd_tune_set(const struct cmd_struct *cmd, int argc, char **argv)
 		}
 		goto out;
 	}
+	if (!strcmp(feature, "free-space-tree")) {
+		if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
+			error("filesystem already has free-space-tree feature");
+			ret = -EINVAL;
+			goto out;
+		}
+		ret = convert_to_fst(fs_info);
+		if (ret < 0)
+			error("failed to convert the filesystem to free-space-tree feature");
+		goto out;
+	}
 
 out:
 	if (fs_info)
-- 
2.42.0


  parent reply	other threads:[~2023-09-05 16:02 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-05  7:51 [PATCH 0/7] btrfs-progs: cmds/tune: add set/clear features Qu Wenruo
2023-09-05  7:51 ` [PATCH 1/7] btrfs-progs: export btrfs_feature structure Qu Wenruo
2023-09-21  0:32   ` Anand Jain
2023-09-21 21:35     ` Qu Wenruo
2023-09-05  7:51 ` [PATCH 2/7] btrfs-progs: cmds: add "btrfs tune set" subcommand group Qu Wenruo
2023-09-21  0:53   ` Anand Jain
2023-09-21  2:13     ` Qu Wenruo
2023-10-13 17:47     ` David Sterba
2023-10-30  8:26       ` Anand Jain
2023-09-05  7:51 ` Qu Wenruo [this message]
2023-09-05  7:51 ` [PATCH 4/7] btrfs-progs: cmds/tune: add set support for block-group-tree feature Qu Wenruo
2023-09-05  7:51 ` [PATCH 5/7] btrfs-progs: cmds/tune: add set support for seeding device Qu Wenruo
2023-09-05  7:51 ` [PATCH 6/7] btrfs-progs: cmds/tune: add "btrfs tune clear" subcommand Qu Wenruo
2023-09-05  7:51 ` [PATCH 7/7] btrfs-progs: tests/cli: add a test case for "btrfs tune" subcommand Qu Wenruo
2023-09-20 23:03 ` [PATCH 0/7] btrfs-progs: cmds/tune: add set/clear features Qu Wenruo
2023-09-21 22:33 ` waxhead
2023-09-21 22:53   ` Qu Wenruo
2023-10-13 17:55   ` David Sterba
2023-10-13 18:50 ` David Sterba
2023-10-13 20:53   ` 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=5bee36f6d2bac339edb47d6c7dede061fa3a12f3.1693900169.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).