Skip to contentSkip to conversation

bcachefs: allow initializing a bcachefs filesystem from a source directory

PATCH New Ariel Miculas 1 participant 4 messages Jun 14, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 bcachefs: allow initializing a bcachefs filesystem from a source directory

Add a new source command line argument which specifies the directory tree that will be copied onto the newly formatted bcachefs filesystem.

This commit also fixes an issue in copy_link where uninitialized data is copied into the symlink because of a round_up of the buffer size.

---
 c_src/cmd_format.c        |  29 +++
 c_src/cmd_migrate.c       | 472 +-------------------------------------
 c_src/libbcachefs.h       |   2 +
 c_src/posix_to_bcachefs.c | 460 +++++++++++++++++++++++++++++++++++++
 c_src/posix_to_bcachefs.h |  54 +++++
 libbcachefs/bkey.h        |   1 +
 libbcachefs/fs-common.h   |   1 +
 libbcachefs/inode.h       |   1 +
 8 files changed, 557 insertions(+), 463 deletions(-)
 create mode 100644 c_src/posix_to_bcachefs.c
 create mode 100644 c_src/posix_to_bcachefs.h
c_src/cmd_format.c +29 -0
index 4bafa171..d0c8e197 100644
@@ -21,6 +21,7 @@
 #include <uuid/uuid.h>
 
 #include "cmds.h"
+#include "posix_to_bcachefs.h"
 #include "libbcachefs.h"
 #include "crypto.h"
 #include "libbcachefs/errcode.h"
@@ -45,6 +46,7 @@ x(0,	data_allowed,		required_argument)	\
 x(0,	durability,		required_argument)	\
 x(0,	version,		required_argument)	\
 x(0,	no_initialize,		no_argument)		\
+x(0,	source,			required_argument)	\
 x('f',	force,			no_argument)		\
 x('q',	quiet,			no_argument)		\
 x('v',	verbose,		no_argument)		\
@@ -66,6 +68,7 @@ static void usage(void)
 	     "  -L, --fs_label=label\n"
 	     "  -U, --uuid=uuid\n"
 	     "      --superblock_size=size\n"
+	     "      --source=path           Initialize the bcachefs filesystem from this root directory\n"
 	     "\n"
 	     "Device specific options:");
 
@@ -113,6 +116,18 @@ u64 read_flag_list_or_die(char *opt, const char * const list[],
 	return v;
 }
 
+void build_fs(struct bch_fs *c, const char *src_path)
+{
+	struct copy_fs_state s = {};
+	int src_fd = xopen(src_path, O_RDONLY|O_NOATIME);
+	struct stat stat = xfstat(src_fd);
+
+	if (!S_ISDIR(stat.st_mode))
+		die("%s is not a directory", src_path);
+
+	copy_fs(c, src_fd, src_path, &s);
+}
+
 int cmd_format(int argc, char *argv[])
 {
 	DARRAY(struct dev_opts) devices = { 0 };
@@ -145,6 +160,9 @@ int cmd_format(int argc, char *argv[])
 			opt_set(fs_opts, metadata_replicas, v);
 			opt_set(fs_opts, data_replicas, v);
 			break;
+		case O_source:
+			opts.source = optarg;
+			break;
 		case O_encrypted:
 			opts.encrypted = true;
 			break;
@@ -277,6 +295,12 @@ int cmd_format(int argc, char *argv[])
 
 	darray_exit(&devices);
 
+	/* don't skip initialization when we have to build an image from a source */
+	if (opts.source && !initialize) {
+		printf("Warning: Forcing the initialization because the source flag was supplied\n");
+		initialize = 1;
+	}
+
 	if (initialize) {
 		struct bch_opts mount_opts = bch2_opts_empty();
 
@@ -294,6 +318,11 @@ int cmd_format(int argc, char *argv[])
 			die("error opening %s: %s", device_paths.data[0],
 			    bch2_err_str(PTR_ERR(c)));
 
+		if (opts.source) {
+			build_fs(c, opts.source);
+		}
+
+
 		bch2_fs_stop(c);
 	}
 
c_src/cmd_migrate.c +9 -463

514 lines not on this page · raw message

c_src/libbcachefs.h +2 -0
index 5c7ef6c7..60332bb8 100644
@@ -6,6 +6,7 @@
 
 #include "libbcachefs/bcachefs_format.h"
 #include "libbcachefs/bcachefs_ioctl.h"
+#include "libbcachefs/inode.h"
 #include "libbcachefs/opts.h"
 #include "libbcachefs/vstructs.h"
 #include "tools-util.h"
@@ -37,6 +38,7 @@ struct format_opts {
 	unsigned	superblock_size;
 	bool		encrypted;
 	char		*passphrase;
+	char		*source;
 };
 
 static inline struct format_opts format_opts_default()
c_src/posix_to_bcachefs.c new file +460 -0

460 lines not on this page · raw message

c_src/posix_to_bcachefs.h new file +54 -0

54 lines not on this page · raw message

libbcachefs/bkey.h +1 -0
index fcd43915..3dc4cf69 100644
@@ -8,6 +8,7 @@
 #include "btree_types.h"
 #include "util.h"
 #include "vstructs.h"
+#include "bcachefs.h"
 
 enum bch_validate_flags {
 	BCH_VALIDATE_write		= (1U << 0),
libbcachefs/fs-common.h +1 -0
index dde23785..2064ef5b 100644
@@ -2,6 +2,7 @@
 #ifndef _BCACHEFS_FS_COMMON_H
 #define _BCACHEFS_FS_COMMON_H
 
+#include "libbcachefs/dirent.h"
 struct posix_acl;
 
 #define BCH_CREATE_TMPFILE		(1U << 0)
libbcachefs/inode.h +1 -0
index 679f5f5e..b62111bf 100644
@@ -5,6 +5,7 @@
 #include "bkey.h"
 #include "bkey_methods.h"
 #include "opts.h"
+#include "subvolume_types.h"
 
 enum bch_validate_flags;
 extern const char * const bch2_inode_opts[];
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 2/4 Fix performance regression of update_inode

This kernel commit: 65bd442397274347e721a89c2c4882a392bae982 removes BTREE_ITER_cached from bch2_btree_insert_trans, which causes the update_inode function to take a long time (~20s). Add this flag back in the update_inode function.

---
 c_src/posix_to_bcachefs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
c_src/posix_to_bcachefs.c +1 -1
index ee0e300f..131913d1 100644
@@ -19,7 +19,7 @@ void update_inode(struct bch_fs *c,
 	bch2_inode_pack(&packed, inode);
 	packed.inode.k.p.snapshot = U32_MAX;
 	ret = bch2_btree_insert(c, BTREE_ID_inodes, &packed.inode.k_i,
-				NULL, 0, 0);
+				NULL, 0, BTREE_ITER_cached);
 	if (ret)
 		die("error updating inode: %s", bch2_err_str(ret));
 }
Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 3/4 Fix incomplete file copy due to copy_data misuse

In reply to Ariel Miculas

The copy_data function takes a start and an end parameter as the range of bytes to copy, but it was called with a start and a length parameter. This resulted in incomplete file copies. Fix it by passing the end of the range instead of the length.

---
 c_src/posix_to_bcachefs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
c_src/posix_to_bcachefs.c +2 -2
index 131913d1..c66b9734 100644
@@ -288,7 +288,7 @@ static void copy_file(struct bch_fs *c, struct bch_inode_unpacked *dst,
 				  FIEMAP_EXTENT_NOT_ALIGNED|
 				  FIEMAP_EXTENT_DATA_INLINE))) {
 			copy_data(c, dst, src_fd, e.fe_logical,
-				  min(src_size - e.fe_logical,
+				  e.fe_logical + min(src_size - e.fe_logical,
 				      e.fe_length));
 			continue;
 		}
@@ -299,7 +299,7 @@ static void copy_file(struct bch_fs *c, struct bch_inode_unpacked *dst,
 		 */
 		if (e.fe_physical < 1 << 20) {
 			copy_data(c, dst, src_fd, e.fe_logical,
-				  min(src_size - e.fe_logical,
+				  e.fe_logical + min(src_size - e.fe_logical,
 				      e.fe_length));
 			continue;
 		}
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 4/4 Preserve directory {a,m,c} times by calling copy_times() after the recursion

In reply to Ariel Miculas

Otherwise the directory {a,m,c} times will be modified by the recursive copy of the directory tree.

---
 c_src/posix_to_bcachefs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
c_src/posix_to_bcachefs.c +1 -1
index c66b9734..714eb47d 100644
@@ -360,7 +360,6 @@ static void copy_dir(struct copy_fs_state *s,
 		if (dst_inum)
 			*dst_inum = inode.bi_inum;
 
-		copy_times(c, &inode, &stat);
 		copy_xattrs(c, &inode, d->d_name);
 
 		/* copy xattrs */
@@ -395,6 +394,7 @@ static void copy_dir(struct copy_fs_state *s,
 			BUG();
 		}
 
+		copy_times(c, &inode, &stat);
 		update_inode(c, &inode);
 next:
 		free(child_path);
Signature · 1 line
2.34.1