Skip to contentSkip to conversation

btrfs-progs: small cleanups related to subvolume creation

PATCH New Qu Wenruo 1 participant 4 messages Jul 15, 2024

Subsystems (2)
lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v2 cover btrfs-progs: small cleanups related to subvolume creation

[CHANGELOG]
v2:
- A new patch to cleanup btrfs_mksubvol()
We have a kernel function with the same name, but those two are doing completely different works.

The progs version is more like linking a subvolume to a destination.

The new patch removes btrfs_mksubvol(), and move the convert specific code to convert code basis.

Thanks to Mark's new effort to introduce subvolume creation ability, the long existing duplicated subvolume creation problem is exposed again.

The first patch to do a small cleanup for btrfs_create_tree() so that the parameter list matches the kernel one.

The second one is the main dish to fully merge the different functions to create a subvolume.

We have btrfs_create_tree() to properly create an empty tree, and btrfs_make_root_dir() to create the initial root dir.

So use them to create btrfs_make_subvolume():

- Calls btrfs_create_tree() to properly create an empty tree Unlike btrfs_copy_root() used in create_subvol(), which can be unsafe if the source subvolume is not empty.

- Calls btrfs_read_fs_root() to setup the cache and tracking Inside create_data_reloc_tree() we directly added the root to fs_root_tree, which is only safe for data reloc tree.

As we didn't properly set the correct tracking flags.

- Calls btrfs_make_root_dir() to setup the root directory

And finally cleanup a confusing function, btrfs_mksubvol(), which is not really creating a subvolume, but linking a subvolume to a parent inode.

Instead of the confusing name, create a new helper, btrfs_link_subvolume() to do the linkage, and move the convert specific retry behavior into convert code.

Qu Wenruo (3):
btrfs-progs: remove fs_info parameter from btrfs_create_tree()
btrfs-progs: introduce btrfs_make_subvolume()
btrfs-progs: use btrfs_link_subvolume() to replace btrfs_mksubvol()

 Makefile                        |   1 +
 check/main.c                    |   9 +-
 common/root-tree-utils.c        | 215 ++++++++++++++++++++++++++++++++
 common/root-tree-utils.h        |  30 +++++
 convert/main.c                  | 107 +++++++++-------
 kernel-shared/ctree.h           |   8 +-
 kernel-shared/disk-io.c         |   4 +-
 kernel-shared/disk-io.h         |   1 -
 kernel-shared/free-space-tree.c |   2 +-
 kernel-shared/inode.c           | 140 +--------------------
 mkfs/common.c                   |  39 ------
 mkfs/common.h                   |   2 -
 mkfs/main.c                     |  78 +-----------
 13 files changed, 331 insertions(+), 305 deletions(-)
 create mode 100644 common/root-tree-utils.c
 create mode 100644 common/root-tree-utils.h

--
2.45.2

author
#1 lore raw

PATCH v2 1/3 btrfs-progs: remove fs_info parameter from btrfs_create_tree()

The @fs_info parameter can be easily extracted from @trans, and kernel has already remove the parameter.

---
 kernel-shared/disk-io.c         | 4 ++--
 kernel-shared/disk-io.h         | 1 -
 kernel-shared/free-space-tree.c | 2 +-
 mkfs/main.c                     | 8 ++++----
 4 files changed, 7 insertions(+), 8 deletions(-)
kernel-shared/disk-io.c +2 -2
index 295bd50ad063..3e8cab1187f0 100644
@@ -2369,12 +2369,12 @@ int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans,
 }
 
 struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
-				     struct btrfs_fs_info *fs_info,
 				     struct btrfs_key *key)
 {
-	struct extent_buffer *leaf;
+	struct btrfs_fs_info *fs_info = trans->fs_info;
 	struct btrfs_root *tree_root = fs_info->tree_root;
 	struct btrfs_root *root;
+	struct extent_buffer *leaf;
 	int ret = 0;
 
 	root = kzalloc(sizeof(*root), GFP_KERNEL);
kernel-shared/disk-io.h +0 -1
index 9f848635fd69..0047db5e7c3e 100644
@@ -239,7 +239,6 @@ int write_tree_block(struct btrfs_trans_handle *trans,
 		     struct extent_buffer *eb);
 int btrfs_fs_roots_compare_roots(const struct rb_node *node1, const struct rb_node *node2);
 struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
-				     struct btrfs_fs_info *fs_info,
 				     struct btrfs_key *key);
 int btrfs_delete_and_free_root(struct btrfs_trans_handle *trans,
 			       struct btrfs_root *root);
kernel-shared/free-space-tree.c +1 -1
index 93806ca01162..81fd57b886d2 100644
@@ -1516,7 +1516,7 @@ int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
 	if (IS_ERR(trans))
 		return PTR_ERR(trans);
 
-	free_space_root = btrfs_create_tree(trans, fs_info, &root_key);
+	free_space_root = btrfs_create_tree(trans, &root_key);
 	if (IS_ERR(free_space_root)) {
 		ret = PTR_ERR(free_space_root);
 		goto abort;
mkfs/main.c +4 -4
index b40f7432bb74..077da1e33fc6 100644
@@ -748,7 +748,7 @@ static int create_data_reloc_tree(struct btrfs_trans_handle *trans)
 	char *name = "..";
 	int ret;
 
-	root = btrfs_create_tree(trans, fs_info, &key);
+	root = btrfs_create_tree(trans, &key);
 	if (IS_ERR(root)) {
 		ret = PTR_ERR(root);
 		goto out;
@@ -870,7 +870,7 @@ static int create_uuid_tree(struct btrfs_trans_handle *trans)
 	int ret = 0;
 
 	UASSERT(fs_info->uuid_root == NULL);
-	root = btrfs_create_tree(trans, fs_info, &key);
+	root = btrfs_create_tree(trans, &key);
 	if (IS_ERR(root)) {
 		ret = PTR_ERR(root);
 		goto out;
@@ -900,7 +900,7 @@ static int create_global_root(struct btrfs_trans_handle *trans, u64 objectid,
 	};
 	int ret = 0;
 
-	root = btrfs_create_tree(trans, fs_info, &key);
+	root = btrfs_create_tree(trans, &key);
 	if (IS_ERR(root)) {
 		ret = PTR_ERR(root);
 		goto out;
@@ -1127,7 +1127,7 @@ static int setup_raid_stripe_tree_root(struct btrfs_fs_info *fs_info)
 		return ret;
 	}
 
-	stripe_root = btrfs_create_tree(trans, fs_info, &key);
+	stripe_root = btrfs_create_tree(trans, &key);
 	if (IS_ERR(stripe_root))  {
 		ret = PTR_ERR(stripe_root);
 		btrfs_abort_transaction(trans, ret);
Signature · 1 line
2.45.2
author
#2 lore raw

PATCH v2 2/3 btrfs-progs: introduce btrfs_make_subvolume()

In reply to Qu Wenruo

There are two different subvolume/data reloc tree creation routines:

- create_subvol() from convert/main.c
* calls btrfs_copy_root() to create an empty root This is not safe, as it relies on the source root to be empty.
* calls btrfs_read_fs_root() to add it to the cache and trace it properly
* calls btrfs_make_root_dir() to initialize the empty new root

- create_data_reloc_tree() from mkfs/main.c
* calls btrfs_create_tree() to create an empty rooo
* Manually add the root to fs_root cache
This is only safe for data reloc tree as it's never updated inside btrfs-progs.
But not safe for other subvolume trees.
* manually setup the root dir

Both has its good and bad aspects, so here we introduce a new helper, btrfs_make_subvolume():

- Calls btrfs_create_tree() to create an empty root
- Calls btrfs_read_fs_root() to setup the cache and tracking properly
- Calls btrfs_make_root_dir() to initialize the root dir
- Calls btrfs_update_root() to reflect the rootdir change

So this new helper can replace both create_subvol() and create_data_reloc_tree().

---
 Makefile                 |   1 +
 check/main.c             |   1 +
 common/root-tree-utils.c | 108 +++++++++++++++++++++++++++++++++++++++
 common/root-tree-utils.h |  26 ++++++++++
 convert/main.c           |  43 ++--------------
 mkfs/common.c            |  39 --------------
 mkfs/common.h            |   2 -
 mkfs/main.c              |  72 +-------------------------
 8 files changed, 141 insertions(+), 151 deletions(-)
 create mode 100644 common/root-tree-utils.c
 create mode 100644 common/root-tree-utils.h
Makefile +1 -0
index 82dfb1b40cde..c0428c573933 100644
@@ -221,6 +221,7 @@ objects = \
 	common/device-utils.o	\
 	common/extent-cache.o	\
 	common/extent-tree-utils.o	\
+	common/root-tree-utils.o	\
 	common/filesystem-utils.o	\
 	common/format-output.o	\
 	common/fsfeatures.o	\
check/main.c +1 -0
index 693f77c3542d..8a572c22036e 100644
@@ -59,6 +59,7 @@
 #include "common/open-utils.h"
 #include "common/string-utils.h"
 #include "common/clear-cache.h"
+#include "common/root-tree-utils.h"
 #include "cmds/commands.h"
 #include "mkfs/common.h"
 #include "check/common.h"
common/root-tree-utils.c new file +108 -0

108 lines not on this page · raw message

common/root-tree-utils.h new file +26 -0
new file mode 100644
index 000000000000..f4e354c0bf3e
@@ -0,0 +1,26 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+
+#ifndef _ROOT_TREE_UTILS_H_
+#define _ROOT_TREE_UTILS_H_
+
+#include "kernel-shared/transaction.h"
+
+int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
+			struct btrfs_root *root, u64 objectid);
+int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid);
+
+#endif
convert/main.c +3 -40

66 lines not on this page · raw message

mkfs/common.c +0 -39
index 5de3e0b6d59c..e28b1e827ee7 100644
@@ -758,45 +758,6 @@ out:
 	return ret;
 }
 
-int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
-			struct btrfs_root *root, u64 objectid)
-{
-	int ret;
-	struct btrfs_inode_item inode_item;
-	time_t now = time(NULL);
-
-	memset(&inode_item, 0, sizeof(inode_item));
-	btrfs_set_stack_inode_generation(&inode_item, trans->transid);
-	btrfs_set_stack_inode_size(&inode_item, 0);
-	btrfs_set_stack_inode_nlink(&inode_item, 1);
-	btrfs_set_stack_inode_nbytes(&inode_item, root->fs_info->nodesize);
-	btrfs_set_stack_inode_mode(&inode_item, S_IFDIR | 0755);
-	btrfs_set_stack_timespec_sec(&inode_item.atime, now);
-	btrfs_set_stack_timespec_nsec(&inode_item.atime, 0);
-	btrfs_set_stack_timespec_sec(&inode_item.ctime, now);
-	btrfs_set_stack_timespec_nsec(&inode_item.ctime, 0);
-	btrfs_set_stack_timespec_sec(&inode_item.mtime, now);
-	btrfs_set_stack_timespec_nsec(&inode_item.mtime, 0);
-	btrfs_set_stack_timespec_sec(&inode_item.otime, now);
-	btrfs_set_stack_timespec_nsec(&inode_item.otime, 0);
-
-	if (root->fs_info->tree_root == root)
-		btrfs_set_super_root_dir(root->fs_info->super_copy, objectid);
-
-	ret = btrfs_insert_inode(trans, root, objectid, &inode_item);
-	if (ret)
-		goto error;
-
-	ret = btrfs_insert_inode_ref(trans, root, "..", 2, objectid, objectid, 0);
-	if (ret)
-		goto error;
-
-	btrfs_set_root_dirid(&root->root_item, objectid);
-	ret = 0;
-error:
-	return ret;
-}
-
 /*
  * Btrfs minimum size calculation is complicated, it should include at least:
  * 1. system group size
mkfs/common.h +0 -2
index de0ff57beee8..c600c16622fa 100644
@@ -103,8 +103,6 @@ struct btrfs_mkfs_config {
 };
 
 int make_btrfs(int fd, struct btrfs_mkfs_config *cfg);
-int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
-			struct btrfs_root *root, u64 objectid);
 u64 btrfs_min_dev_size(u32 nodesize, bool mixed, u64 zone_size, u64 meta_profile,
 		       u64 data_profile);
 int test_minimum_size(const char *file, u64 min_dev_size);
mkfs/main.c +2 -70

90 lines not on this page · raw message

Signature · 1 line
2.45.2
author
#3 lore raw

PATCH v2 3/3 btrfs-progs: use btrfs_link_subvolume() to replace btrfs_mksubvol()

In reply to Qu Wenruo

The function btrfs_mksubvol() is very different between btrfs-progs and kernel, the former version is really just linking a subvolume to another directory inode, but the kernel version is really to make a completely new subvolume.

Instead of same-named function, introduce btrfs_link_subvolume() and use it to replace the old btrfs_mksubvol().

This is done by:

- Introduce btrfs_link_subvolume()
Which would do extra checks before doing any modification:
* Make sure the target inode is a directory
* Make sure no filename conflict

Then do the linkage:
* Add the dir_item/dir_index into the parent inode
* Add the forward and backward root refs into tree root

- Introduce link_image_subvolume() helper
Currently btrfs_mksubvol() has a dedicated convert filename retry behavior, which is unnecessary and should be done by the convert code.

Now move the filename retry behavior into the helper.

- Remove btrfs_mksubvol()
Since there is only one caller utilizing btrfs_mksubvol(), and it's now gone, we can remove the old btrfs_mksubvol().

---
 check/main.c             |   8 +--
 common/root-tree-utils.c | 107 ++++++++++++++++++++++++++++++
 common/root-tree-utils.h |   4 ++
 convert/main.c           |  64 ++++++++++++++++--
 kernel-shared/ctree.h    |   8 +--
 kernel-shared/inode.c    | 140 ++-------------------------------------
 6 files changed, 184 insertions(+), 147 deletions(-)
check/main.c +4 -4
index 8a572c22036e..205bbb4a3c73 100644
@@ -2361,10 +2361,10 @@ static int repair_inode_backrefs(struct btrfs_root *root,
 			struct btrfs_trans_handle *trans;
 			struct btrfs_key location;
 
-			ret = check_dir_conflict(root, backref->name,
-						 backref->namelen,
-						 backref->dir,
-						 backref->index);
+			ret = btrfs_check_dir_conflict(root, backref->name,
+						       backref->namelen,
+						       backref->dir,
+						       backref->index);
 			if (ret) {
 				/*
 				 * let nlink fixing routine to handle it,
common/root-tree-utils.c +107 -0

110 lines not on this page · raw message

common/root-tree-utils.h +4 -0
index f4e354c0bf3e..c5318bd9b8db 100644
@@ -22,5 +22,9 @@
 int btrfs_make_root_dir(struct btrfs_trans_handle *trans,
 			struct btrfs_root *root, u64 objectid);
 int btrfs_make_subvolume(struct btrfs_trans_handle *trans, u64 objectid);
+int btrfs_link_subvolume(struct btrfs_trans_handle *trans,
+			 struct btrfs_root *parent_root,
+			 u64 parent_dir, const char *name,
+			 int namelen, struct btrfs_root *subvol);
 
 #endif
convert/main.c +60 -4

76 lines not on this page · raw message

kernel-shared/ctree.h +4 -4
index 1341a418726b..2388879d1db3 100644
@@ -1212,8 +1212,10 @@ static inline int is_fstree(u64 rootid)
 void btrfs_uuid_to_key(const u8 *uuid, u8 type, struct btrfs_key *key);
 
 /* inode.c */
-int check_dir_conflict(struct btrfs_root *root, char *name, int namelen,
-		u64 dir, u64 index);
+int btrfs_find_free_dir_index(struct btrfs_root *root, u64 dir_ino,
+			      u64 *ret_ino);
+int btrfs_check_dir_conflict(struct btrfs_root *root, const char *name,
+			     int namelen, u64 dir, u64 index);
 int btrfs_new_inode(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		u64 ino, u32 mode);
 int btrfs_change_inode_flags(struct btrfs_trans_handle *trans,
@@ -1229,8 +1231,6 @@ int btrfs_add_orphan_item(struct btrfs_trans_handle *trans,
 			  u64 ino);
 int btrfs_mkdir(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		char *name, int namelen, u64 parent_ino, u64 *ino, int mode);
-struct btrfs_root *btrfs_mksubvol(struct btrfs_root *root, const char *base,
-				  u64 root_objectid, bool convert);
 int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
 			     struct btrfs_root *fs_root,
 			     u64 dirid, u64 *objectid);
kernel-shared/inode.c +5 -135

164 lines not on this page · raw message

Signature · 1 line
2.45.2