public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Mark Harmstone <maharmstone@fb.com>
To: <linux-btrfs@vger.kernel.org>
Cc: Omar Sandoval <osandov@fb.com>, Mark Harmstone <maharmstone@meta.com>
Subject: [PATCH 1/3] btrfs-progs: use libbtrfsutil for btrfs subvolume create
Date: Fri, 28 Jun 2024 15:56:47 +0100	[thread overview]
Message-ID: <20240628145807.1800474-2-maharmstone@fb.com> (raw)
In-Reply-To: <20240628145807.1800474-1-maharmstone@fb.com>

From: Omar Sandoval <osandov@fb.com>

Call btrfs_util_create_subvolume in create_one_subvolume rather than
calling the ioctl directly.

Signed-off-by: Mark Harmstone <maharmstone@meta.com>
Co-authored-by: Mark Harmstone <maharmstone@meta.com>

---
 cmds/subvolume.c | 133 +++++++++++++++++++----------------------------
 1 file changed, 53 insertions(+), 80 deletions(-)

diff --git a/cmds/subvolume.c b/cmds/subvolume.c
index b4151af2..8fa0d407 100644
--- a/cmds/subvolume.c
+++ b/cmds/subvolume.c
@@ -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;
 }
-- 
2.44.2


  reply	other threads:[~2024-06-28 14:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-28 14:56 [PATCH 0/3] btrfs-progs: use libbtrfsutil for subvolume creation Mark Harmstone
2024-06-28 14:56 ` Mark Harmstone [this message]
2024-07-26 17:19   ` [PATCH 1/3] btrfs-progs: use libbtrfsutil for btrfs subvolume create David Sterba
2024-06-28 14:56 ` [PATCH 2/3] btrfs-progs: use libbtrfsutil for btrfs subvolume snapshot Mark Harmstone
2024-07-26 17:22   ` David Sterba
2024-06-28 14:56 ` [PATCH 3/3] btrfs-progs: remove unused qgroup functions Mark Harmstone
2024-07-24 18:05 ` [PATCH 0/3] btrfs-progs: use libbtrfsutil for subvolume creation Boris Burkov
2024-07-26 17:17   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240628145807.1800474-2-maharmstone@fb.com \
    --to=maharmstone@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=maharmstone@meta.com \
    --cc=osandov@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox