diff mbox series

[v2,29/30] btrfs-progs: mkfs: create the global root's

Message ID dc65e92db91bcdf238b17e53d3efdb8ec21a493e.1636575147.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: extent tree v2 support, global roots | expand

Commit Message

Josef Bacik Nov. 10, 2021, 8:14 p.m. UTC
Now that we have all of the supporting code, add the ability to create
all of the global roots for an extent tree v2 fs.  This will default to
nr_cpu's, but also allow the user to specify how many global roots they
would like.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 mkfs/main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 67 insertions(+), 1 deletion(-)

Comments

David Sterba Nov. 15, 2021, 8:51 p.m. UTC | #1
On Wed, Nov 10, 2021 at 03:14:41PM -0500, Josef Bacik wrote:
> @@ -966,13 +1010,18 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
>  	struct btrfs_mkfs_config mkfs_cfg;
>  	enum btrfs_csum_type csum_type = BTRFS_CSUM_TYPE_CRC32;
>  	u64 system_group_size;
> +	int nr_global_roots = sysconf(_SC_NPROCESSORS_ONLN);
>  
>  	crc32c_optimization_init();
>  	btrfs_config_init();
>  
>  	while(1) {
>  		int c;
> -		enum { GETOPT_VAL_SHRINK = 257, GETOPT_VAL_CHECKSUM };
> +		enum {
> +			GETOPT_VAL_SHRINK = 257,
> +			GETOPT_VAL_CHECKSUM,
> +			GETOPT_VAL_GLOBAL_ROOTS,
> +		};
>  		static const struct option long_options[] = {
>  			{ "byte-count", required_argument, NULL, 'b' },
>  			{ "csum", required_argument, NULL,
> @@ -996,6 +1045,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
>  			{ "quiet", 0, NULL, 'q' },
>  			{ "verbose", 0, NULL, 'v' },
>  			{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
> +			{ "num-global-roots", required_argument, NULL, GETOPT_VAL_GLOBAL_ROOTS },

This will be good under the #ifdef EXPERIMENTAL too, so we don't
accidentaly expose it, the rest of the option handling code can stay as
it would be unreachable.

No need to resend, a separate patch is ok for that.

>  			{ "help", no_argument, NULL, GETOPT_VAL_HELP },
>  			{ NULL, 0, NULL, 0}
>  		};
diff mbox series

Patch

diff --git a/mkfs/main.c b/mkfs/main.c
index fd40c70e..8959fd8b 100644
--- a/mkfs/main.c
+++ b/mkfs/main.c
@@ -810,6 +810,50 @@  out:
 	return ret;
 }
 
+static int create_global_root(struct btrfs_trans_handle *trans, u64 objectid,
+			      int root_id)
+{
+	struct btrfs_fs_info *fs_info = trans->fs_info;
+	struct btrfs_root *root;
+	struct btrfs_key key = {
+		.objectid = objectid,
+		.type = BTRFS_ROOT_ITEM_KEY,
+		.offset = root_id,
+	};
+	int ret = 0;
+
+	root = btrfs_create_tree(trans, fs_info, &key);
+	if (IS_ERR(root)) {
+		ret = PTR_ERR(root);
+		goto out;
+	}
+	ret = btrfs_global_root_insert(fs_info, root);
+out:
+	if (ret)
+		btrfs_abort_transaction(trans, ret);
+	return ret;
+}
+
+static int create_global_roots(struct btrfs_trans_handle *trans,
+			       int nr_global_roots)
+{
+	int ret, i;
+
+	for (i = 1; i < nr_global_roots; i++) {
+		ret = create_global_root(trans, BTRFS_EXTENT_TREE_OBJECTID, i);
+		if (ret)
+			return ret;
+		ret = create_global_root(trans, BTRFS_CSUM_TREE_OBJECTID, i);
+		if (ret)
+			return ret;
+		ret = create_global_root(trans, BTRFS_FREE_SPACE_TREE_OBJECTID, i);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int insert_qgroup_items(struct btrfs_trans_handle *trans,
 			       struct btrfs_fs_info *fs_info,
 			       u64 qgroupid)
@@ -966,13 +1010,18 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 	struct btrfs_mkfs_config mkfs_cfg;
 	enum btrfs_csum_type csum_type = BTRFS_CSUM_TYPE_CRC32;
 	u64 system_group_size;
+	int nr_global_roots = sysconf(_SC_NPROCESSORS_ONLN);
 
 	crc32c_optimization_init();
 	btrfs_config_init();
 
 	while(1) {
 		int c;
-		enum { GETOPT_VAL_SHRINK = 257, GETOPT_VAL_CHECKSUM };
+		enum {
+			GETOPT_VAL_SHRINK = 257,
+			GETOPT_VAL_CHECKSUM,
+			GETOPT_VAL_GLOBAL_ROOTS,
+		};
 		static const struct option long_options[] = {
 			{ "byte-count", required_argument, NULL, 'b' },
 			{ "csum", required_argument, NULL,
@@ -996,6 +1045,7 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 			{ "quiet", 0, NULL, 'q' },
 			{ "verbose", 0, NULL, 'v' },
 			{ "shrink", no_argument, NULL, GETOPT_VAL_SHRINK },
+			{ "num-global-roots", required_argument, NULL, GETOPT_VAL_GLOBAL_ROOTS },
 			{ "help", no_argument, NULL, GETOPT_VAL_HELP },
 			{ NULL, 0, NULL, 0}
 		};
@@ -1100,6 +1150,9 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 			case GETOPT_VAL_CHECKSUM:
 				csum_type = parse_csum_type(optarg);
 				break;
+			case GETOPT_VAL_GLOBAL_ROOTS:
+				nr_global_roots = (int)arg_strtou64(optarg);
+				break;
 			case GETOPT_VAL_HELP:
 			default:
 				print_usage(c != GETOPT_VAL_HELP);
@@ -1239,6 +1292,11 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 	if (features & BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2) {
 		features |= BTRFS_FEATURE_INCOMPAT_NO_HOLES;
 		runtime_features |= BTRFS_RUNTIME_FEATURE_FREE_SPACE_TREE;
+
+		if (!nr_global_roots) {
+			error("you must set a non-zero num-global-roots value");
+			exit(1);
+		}
 	}
 
 	if (zoned) {
@@ -1466,6 +1524,14 @@  int BOX_MAIN(mkfs)(int argc, char **argv)
 		goto error;
 	}
 
+	if (features & BTRFS_FEATURE_INCOMPAT_EXTENT_TREE_V2) {
+		ret = create_global_roots(trans, nr_global_roots);
+		if (ret) {
+			error("failed to create global roots: %d", ret);
+			goto error;
+		}
+	}
+
 	ret = make_root_dir(trans, root);
 	if (ret) {
 		error("failed to setup the root directory: %d", ret);