Skip to contentSkip to conversation

btrfs-progs: use libbtrfsutil for subvolume creation

PATCH Superseded Mark Harmstone 3 participants 8 messages Jun 28, 2024 – Jul 26, 2024 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover btrfs-progs: use libbtrfsutil for subvolume creation

From: Mark Harmstone <[email protected]>

These patches are a resending of Omar Sandoval's patch from 2018, which appears to have been overlooked [0], split up and rebased against the current code.

We change btrfs subvol create and btrfs subvol snapshot so that they use libbtrfsutil rather than calling the ioctl directly.

[0] https://lore.kernel.org/linux-btrfs/ab09ba595157b7fb6606814730508cae4da48caf.1516991902.git.osandov@fb.com/

Omar Sandoval (3):
btrfs-progs: remove unused qgroup functions
btrfs-progs: use libbtrfsutil for btrfs subvolume create
btrfs-progs: use libbtrfsutil for btrfs subvolume snapshot

 cmds/qgroup.c    |  64 -------------
 cmds/qgroup.h    |   2 -
 cmds/subvolume.c | 227 ++++++++++++++++++-----------------------------
 3 files changed, 86 insertions(+), 207 deletions(-)
Signature · 1 line
2.44.2
#1 lore raw

PATCH 1/3 btrfs-progs: use libbtrfsutil for btrfs subvolume create

From: Omar Sandoval <[email protected]>

Call btrfs_util_create_subvolume in create_one_subvolume rather than calling the ioctl directly.

---
 cmds/subvolume.c | 133 +++++++++++++++++++----------------------------
 1 file changed, 53 insertions(+), 80 deletions(-)
cmds/subvolume.c +53 -80
index b4151af2..8fa0d407 100644
@@ -46,6 +46,7 @@
 #include "common/units.h"
 #include "common/format-output.h"
 #include "common/tree-search.h"
+#include "common/parse-utils.h"
 #include "cmds/commands.h"
 #include "cmds/qgroup.h"
 
@@ -140,63 +141,27 @@ static const char * const cmd_subvolume_create_usage[] = {
 	NULL
 };
 
-static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *inherit,
+static int create_one_subvolume(const char *dst, struct btrfs_util_qgroup_inherit *inherit,
 				bool create_parents)
 {
 	int ret;
-	int len;
-	int	fddst = -1;
-	char	*dupname = NULL;
-	char	*dupdir = NULL;
-	const char *newname;
-	char	*dstdir;
-
-	ret = path_is_dir(dst);
-	if (ret < 0 && ret != -ENOENT) {
-		errno = -ret;
-		error("cannot access %s: %m", dst);
-		goto out;
-	}
-	if (ret >= 0) {
-		error("target path already exists: %s", dst);
-		ret = -EEXIST;
-		goto out;
-	}
-
-	dupname = strdup(dst);
-	if (!dupname) {
-		error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
-		ret = -ENOMEM;
-		goto out;
-	}
-	newname = path_basename(dupname);
-
-	dupdir = strdup(dst);
-	if (!dupdir) {
-		error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
-		ret = -ENOMEM;
-		goto out;
-	}
-	dstdir = path_dirname(dupdir);
-
-	if (!test_issubvolname(newname)) {
-		error("invalid subvolume name: %s", newname);
-		ret = -EINVAL;
-		goto out;
-	}
-
-	len = strlen(newname);
-	if (len > BTRFS_VOL_NAME_MAX) {
-		error("subvolume name too long: %s", newname);
-		ret = -EINVAL;
-		goto out;
-	}
+	enum btrfs_util_error err;
 
 	if (create_parents) {
 		char p[PATH_MAX] = { 0 };
 		char dstdir_dup[PATH_MAX];
+		char *dupdir = NULL;
+		char *dstdir;
 		char *token;
 
+		dupdir = strdup(dst);
+		if (!dupdir) {
+			error_msg(ERROR_MSG_MEMORY, "duplicating %s", dst);
+			free(dupdir);
+			return -ENOMEM;
+		}
+		dstdir = path_dirname(dupdir);
+
 		strncpy_null(dstdir_dup, dstdir, sizeof(dstdir_dup));
 		if (dstdir_dup[0] == '/')
 			strcat(p, "/");
@@ -209,61 +174,68 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in
 				ret = mkdir(p, 0777);
 				if (ret < 0) {
 					error("failed to create directory %s: %m", p);
-					goto out;
+					free(dupdir);
+					return ret;
 				}
 			} else if (ret <= 0) {
 				if (ret == 0)
 					ret = -EEXIST;
 				errno = ret ;
 				error("failed to check directory %s before creation: %m", p);
-				goto out;
+				free(dupdir);
+				return ret;
 			}
 			strcat(p, "/");
 			token = strtok(NULL, "/");
 		}
-	}
 
-	fddst = btrfs_open_dir(dstdir);
-	if (fddst < 0) {
-		ret = fddst;
-		goto out;
+		free(dupdir);
 	}
 
-	if (inherit) {
-		struct btrfs_ioctl_vol_args_v2	args;
+	pr_verbose(LOG_DEFAULT, "Create subvolume '%s'\n", dst);
 
-		memset(&args, 0, sizeof(args));
-		strncpy_null(args.name, newname, sizeof(args.name));
-		args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
-		args.size = btrfs_qgroup_inherit_size(inherit);
-		args.qgroup_inherit = inherit;
+	err = btrfs_util_create_subvolume(dst, 0, NULL, inherit);
+	if (err) {
+		error_btrfs_util(err);
+		return 1;
+	}
 
-		ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args);
-	} else {
-		struct btrfs_ioctl_vol_args	args;
+	return 0;
+}
+
+static int qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit,
+				    const char *arg)
+{
+	enum btrfs_util_error err;
+	u64 qgroupid;
 
-		memset(&args, 0, sizeof(args));
-		strncpy_null(args.name, newname, sizeof(args.name));
-		ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
+	if (!*inherit) {
+		err = btrfs_util_create_qgroup_inherit(0, inherit);
+		if (err) {
+			error_btrfs_util(err);
+			return -1;
+		}
 	}
 
-	if (ret < 0) {
-		error("cannot create subvolume: %m");
-		goto out;
+	qgroupid = parse_qgroupid_or_path(optarg);
+	if (qgroupid == 0) {
+		error("invalid qgroup specification, qgroupid must not be 0");
+		return -1;
 	}
-	pr_verbose(LOG_DEFAULT, "Create subvolume '%s/%s'\n", dstdir, newname);
 
-out:
-	close(fddst);
-	free(dupname);
-	free(dupdir);
+	err = btrfs_util_qgroup_inherit_add_group(inherit, qgroupid);
+	if (err) {
+		error_btrfs_util(err);
+		return -1;
+	}
 
-	return ret;
+	return 0;
 }
+
 static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **argv)
 {
 	int retval, ret;
-	struct btrfs_qgroup_inherit *inherit = NULL;
+	struct btrfs_util_qgroup_inherit *inherit = NULL;
 	bool has_error = false;
 	bool create_parents = false;
 
@@ -281,7 +253,7 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
 
 		switch (c) {
 		case 'i':
-			ret = btrfs_qgroup_inherit_add_group(&inherit, optarg);
+			ret = qgroup_inherit_add_group(&inherit, optarg);
 			if (ret) {
 				retval = ret;
 				goto out;
@@ -310,7 +282,8 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a
 	if (!has_error)
 		retval = 0;
 out:
-	free(inherit);
+	if (inherit)
+		btrfs_util_destroy_qgroup_inherit(inherit);
 
 	return retval;
 }
Signature · 1 line
2.44.2
#2 lore raw

Re: [PATCH 1/3] btrfs-progs: use libbtrfsutil for btrfs subvolume create

cmds/subvolume.c static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in · line 197

Quoted 1 line · Mark Harmstone
cmds/subvolume.c +1 -0
+	err = btrfs_util_create_subvolume(dst, 0, NULL, inherit);

Please use functions with the new naming scheme,
btrfs_util_subvolume_create()

cmds/subvolume.c static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in · line 213

Quoted 12 lines
cmds/subvolume.c +8 -3
+
+static int qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit,
+				    const char *arg)
+{
+	enum btrfs_util_error err;
+	u64 qgroupid;
 
-		memset(&args, 0, sizeof(args));
-		strncpy_null(args.name, newname, sizeof(args.name));
-		ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args);
+	if (!*inherit) {
+		err = btrfs_util_create_qgroup_inherit(0, inherit);

btrfs_util_qgroup_inherit_create()

Quoted 5 lines
cmds/subvolume.c +4 -0
+		if (err) {
+			error_btrfs_util(err);
+			return -1;
+		}
 	}
#3 lore raw

PATCH 2/3 btrfs-progs: use libbtrfsutil for btrfs subvolume snapshot

In reply to Mark Harmstone

From: Omar Sandoval <[email protected]>

Call btrfs_util_create_snapshot in cmd_subvolume_snapshot rather than calling the ioctl directly.

---
 cmds/subvolume.c | 94 +++++++++++++++++-------------------------------
 1 file changed, 33 insertions(+), 61 deletions(-)
cmds/subvolume.c +33 -61
index 8fa0d407..c668ae91 100644
@@ -622,18 +622,11 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
 {
 	char	*subvol, *dst;
 	int	res, retval;
-	int	fd = -1, fddst = -1;
-	int	len;
-	bool readonly = false;
-	char	*dupname = NULL;
-	char	*dupdir = NULL;
-	const char *newname;
-	char	*dstdir;
+	char	*dstdir = NULL;
 	enum btrfs_util_error err;
-	struct btrfs_ioctl_vol_args_v2	args;
-	struct btrfs_qgroup_inherit *inherit = NULL;
+	struct btrfs_util_qgroup_inherit *inherit = NULL;
+	int flags = 0;
 
-	memset(&args, 0, sizeof(args));
 	optind = 0;
 	while (1) {
 		int c = getopt(argc, argv, "i:r");
@@ -642,14 +635,14 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
 
 		switch (c) {
 		case 'i':
-			res = btrfs_qgroup_inherit_add_group(&inherit, optarg);
+			res = qgroup_inherit_add_group(&inherit, optarg);
 			if (res) {
 				retval = res;
 				goto out;
 			}
 			break;
 		case 'r':
-			readonly = true;
+			flags |= BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY;
 			break;
 		default:
 			usage_unknown_option(cmd, argv);
@@ -683,72 +676,51 @@ static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char *
 	}
 
 	if (res > 0) {
+		char *dupname;
+		const char *newname;
+
 		dupname = strdup(subvol);
 		newname = path_basename(dupname);
-		dstdir = dst;
-	} else {
-		dupname = strdup(dst);
-		newname = path_basename(dupname);
-		dupdir = strdup(dst);
-		dstdir = path_dirname(dupdir);
-	}
-
-	if (!test_issubvolname(newname)) {
-		error("invalid snapshot name '%s'", newname);
-		goto out;
-	}
-
-	len = strlen(newname);
-	if (len > BTRFS_VOL_NAME_MAX) {
-		error("snapshot name too long '%s'", newname);
-		goto out;
-	}
 
-	fddst = btrfs_open_dir(dstdir);
-	if (fddst < 0)
-		goto out;
-
-	fd = btrfs_open_dir(subvol);
-	if (fd < 0)
-		goto out;
+		dstdir = malloc(strlen(dst) + 1 + strlen(newname) + 1);
+		if (!dstdir) {
+			error("out of memory");
+			free(dupname);
+			goto out;
+		}
 
-	if (readonly)
-		args.flags |= BTRFS_SUBVOL_RDONLY;
+		dstdir[0] = 0;
+		strcpy(dstdir, dst);
+		strcat(dstdir, "/");
+		strcat(dstdir, newname);
 
-	args.fd = fd;
-	if (inherit) {
-		args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
-		args.size = btrfs_qgroup_inherit_size(inherit);
-		args.qgroup_inherit = inherit;
+		free(dupname);
+	} else {
+		dstdir = strdup(dst);
 	}
-	strncpy_null(args.name, newname, sizeof(args.name));
 
-	res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args);
-	if (res < 0) {
-		if (errno == ETXTBSY)
-			error("cannot snapshot '%s': source subvolume contains an active swapfile (%m)", subvol);
-		else
-			error("cannot snapshot '%s': %m", subvol);
+	err = btrfs_util_create_snapshot(subvol, dstdir, flags, NULL, inherit);
+	if (err) {
+		error_btrfs_util(err);
 		goto out;
 	}
 
 	retval = 0;	/* success */
 
-	if (readonly)
+	if (flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY)
 		pr_verbose(LOG_DEFAULT,
-			   "Create readonly snapshot of '%s' in '%s/%s'\n",
-			   subvol, dstdir, newname);
+			   "Create readonly snapshot of '%s' in '%s'\n",
+			   subvol, dstdir);
 	else
 		pr_verbose(LOG_DEFAULT,
-			   "Create snapshot of '%s' in '%s/%s'\n",
-			   subvol, dstdir, newname);
+			   "Create snapshot of '%s' in '%s'\n",
+			   subvol, dstdir);
 
 out:
-	close(fddst);
-	close(fd);
-	free(inherit);
-	free(dupname);
-	free(dupdir);
+	free(dstdir);
+
+	if (inherit)
+		btrfs_util_destroy_qgroup_inherit(inherit);
 
 	return retval;
 }
Signature · 1 line
2.44.2
#4 lore raw

Re: [PATCH 2/3] btrfs-progs: use libbtrfsutil for btrfs subvolume snapshot

cmds/subvolume.c static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char * · line 702

Quoted 1 line · Mark Harmstone
cmds/subvolume.c +1 -0
+	err = btrfs_util_create_snapshot(subvol, dstdir, flags, NULL, inherit);

btrfs_util_subvolume_snapshot()

Quoted 21 lines
cmds/subvolume.c +7 -5
+	if (err) {
+		error_btrfs_util(err);
 		goto out;
 	}
 
 	retval = 0;	/* success */
 
-	if (readonly)
+	if (flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY)
 		pr_verbose(LOG_DEFAULT,
-			   "Create readonly snapshot of '%s' in '%s/%s'\n",
-			   subvol, dstdir, newname);
+			   "Create readonly snapshot of '%s' in '%s'\n",
+			   subvol, dstdir);
 	else
 		pr_verbose(LOG_DEFAULT,
-			   "Create snapshot of '%s' in '%s/%s'\n",
-			   subvol, dstdir, newname);
+			   "Create snapshot of '%s' in '%s'\n",
+			   subvol, dstdir);
 

cmds/subvolume.c static int cmd_subvolume_snapshot(const struct cmd_struct *cmd, int argc, char * · line 723

Quoted 10 lines
cmds/subvolume.c +4 -5
 out:
-	close(fddst);
-	close(fd);
-	free(inherit);
-	free(dupname);
-	free(dupdir);
+	free(dstdir);
+
+	if (inherit)
+		btrfs_util_destroy_qgroup_inherit(inherit);

btrfs_util_qgroup_inherit_destroy()

#5 lore raw

PATCH 3/3 btrfs-progs: remove unused qgroup functions

In reply to Mark Harmstone

From: Omar Sandoval <[email protected]>

Remove functions that after the previous two patches are no longer referenced.

---
 cmds/qgroup.c | 64 ---------------------------------------------------
 cmds/qgroup.h |  2 --
 2 files changed, 66 deletions(-)
cmds/qgroup.c +0 -64
index bffe942b..35b8c7b8 100644
@@ -1688,70 +1688,6 @@ out:
 	return ret;
 }
 
-int btrfs_qgroup_inherit_size(struct btrfs_qgroup_inherit *p)
-{
-	return sizeof(*p) + sizeof(p->qgroups[0]) *
-			    (p->num_qgroups + 2 * p->num_ref_copies +
-			     2 * p->num_excl_copies);
-}
-
-static int qgroup_inherit_realloc(struct btrfs_qgroup_inherit **inherit, int n,
-		int pos)
-{
-	struct btrfs_qgroup_inherit *out;
-	int nitems = 0;
-
-	if (*inherit) {
-		nitems = (*inherit)->num_qgroups +
-			 (*inherit)->num_ref_copies +
-			 (*inherit)->num_excl_copies;
-	}
-
-	out = calloc(1, sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n));
-	if (out == NULL) {
-		error_msg(ERROR_MSG_MEMORY, NULL);
-		return -ENOMEM;
-	}
-
-	if (*inherit) {
-		struct btrfs_qgroup_inherit *i = *inherit;
-		int s = sizeof(out->qgroups[0]);
-
-		out->num_qgroups = i->num_qgroups;
-		out->num_ref_copies = i->num_ref_copies;
-		out->num_excl_copies = i->num_excl_copies;
-		memcpy(out->qgroups, i->qgroups, pos * s);
-		memcpy(out->qgroups + pos + n, i->qgroups + pos,
-		       (nitems - pos) * s);
-	}
-	free(*inherit);
-	*inherit = out;
-
-	return 0;
-}
-
-int btrfs_qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg)
-{
-	int ret;
-	u64 qgroupid = parse_qgroupid_or_path(arg);
-	int pos = 0;
-
-	if (qgroupid == 0) {
-		error("invalid qgroup specification, qgroupid must not 0");
-		return -EINVAL;
-	}
-
-	if (*inherit)
-		pos = (*inherit)->num_qgroups;
-	ret = qgroup_inherit_realloc(inherit, 1, pos);
-	if (ret)
-		return ret;
-
-	(*inherit)->qgroups[(*inherit)->num_qgroups++] = qgroupid;
-
-	return 0;
-}
-
 static const char * const qgroup_cmd_group_usage[] = {
 	"btrfs qgroup <command> [options] <path>",
 	NULL
cmds/qgroup.h +0 -2
index 1fc10722..32309ce4 100644
@@ -36,8 +36,6 @@ struct btrfs_qgroup_stats {
 	struct btrfs_qgroup_limit limit;
 };
 
-int btrfs_qgroup_inherit_size(struct btrfs_qgroup_inherit *p);
-int btrfs_qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg);
 int btrfs_qgroup_query(int fd, u64 qgroupid, struct btrfs_qgroup_stats *stats);
 
 #endif
Signature · 1 line
2.44.2
Reviewed-by on the v1 series
#6 lore raw
In reply to Mark Harmstone
Quoted 10 lines · Mark Harmstone

From: Mark Harmstone <[email protected]>

These patches are a resending of Omar Sandoval's patch from 2018, which appears to have been overlooked [0], split up and rebased against the current code.

We change btrfs subvol create and btrfs subvol snapshot so that they use libbtrfsutil rather than calling the ioctl directly.

[0] https://lore.kernel.org/linux-btrfs/ab09ba595157b7fb6606814730508cae4da48caf.1516991902.git.osandov@fb.com/

The series looks good to me, you can add

to it.

Two high level questions:
1. I think you put duplicate Signed-off-by: and Co-authored-by: lines on each patch. Did you mean to put Omar as the Co-authored-by: ?

2. Have you run fstests with this patch applied? We have recently had some test failures from stdout in create subvol/snapshot changing, so I would like to avoid that fiasco again.

Thanks,
Boris

Quoted 14 lines

Omar Sandoval (3):
btrfs-progs: remove unused qgroup functions
btrfs-progs: use libbtrfsutil for btrfs subvolume create
btrfs-progs: use libbtrfsutil for btrfs subvolume snapshot

 cmds/qgroup.c    |  64 -------------
 cmds/qgroup.h    |   2 -
 cmds/subvolume.c | 227 ++++++++++++++++++-----------------------------
 3 files changed, 86 insertions(+), 207 deletions(-)
Signature · 1 line
2.44.2
#7 lore raw
Quoted 12 lines · Boris Burkov
Quoted 10 lines · Mark Harmstone

From: Mark Harmstone <[email protected]>

These patches are a resending of Omar Sandoval's patch from 2018, which appears to have been overlooked [0], split up and rebased against the current code.

We change btrfs subvol create and btrfs subvol snapshot so that they use libbtrfsutil rather than calling the ioctl directly.

[0] https://lore.kernel.org/linux-btrfs/ab09ba595157b7fb6606814730508cae4da48caf.1516991902.git.osandov@fb.com/

Quoted 7 lines

The series looks good to me, you can add

to it.

Two high level questions:
1. I think you put duplicate Signed-off-by: and Co-authored-by: lines on each patch. Did you mean to put Omar as the Co-authored-by: ?

I think Omar's signed-off could be there in case the code is the same except some minor adjustments to fix conflicts, or co-authored-by.

Quoted 3 lines

2. Have you run fstests with this patch applied? We have recently had some test failures from stdout in create subvol/snapshot changing, so I would like to avoid that fiasco again.

If the output changed then a filter needs to be added to fstests. I don't see any for subvolume creation, only _filter_btrfs_subvol_delete.

Besides, the tests of btrfs-progs don't pass,
https://github.com/kdave/btrfs-progs/actions/runs/10114811661

misc test 033-filename-length-limit fails, subvolume length, "unexpected success: subvolume with name 256 bytes long succeeded"

and cli test 019-subvolume-create-parents
"unexpected success: was able to create subvolume without an intermediate directory"