diff mbox series

[3/7] btrfs-progs: cmds/tune: add set support for free-space-tree feature

Message ID 5bee36f6d2bac339edb47d6c7dede061fa3a12f3.1693900169.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: cmds/tune: add set/clear features | expand

Commit Message

Qu Wenruo Sept. 5, 2023, 7:51 a.m. UTC
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 mbox series

Patch

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)