From patchwork Fri Aug 2 11:27:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 13751482 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F279A1E2125 for ; Fri, 2 Aug 2024 11:27:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722598063; cv=none; b=nDCneA9ZI31KhRSeGCf7U5jp0Hi3DdLTAAkgYo2M8donHPdIP+80VKShBl644wU4YweJGftcGMJ+RsSW8DCKIfXO+fgEzBC4TttaotNbBxcEJyehFQ2AfpNnmz/SQWDa/pqhiwyiQO11jjEq+kvmQHkBtpYsd2GD8Sul6zzKsKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722598063; c=relaxed/simple; bh=ua9JIwMhqL7tcx6ddbdeHkhCeUIjgq8pOeTC0vjFBuk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=DCTDGkkafYMYb3jOt0cl/Z72Ui2NSubLs3+1oJkMmAPg/6wSCWRtSH3gmlxr2lC15BoA7G7TLoz8KYv5r5sznV43P+K6XolYHxP4OoTAXS7Q0VQhlXxP0/z7oEu0R10tRcWupNXmDDm+KTznC6qkAzjAA6fcl2Fznbwhc8qhBlc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b=rrtPIDSN; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="rrtPIDSN" Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 472ANrLN011564 for ; Fri, 2 Aug 2024 04:27:41 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= facebook; bh=8HodEiQ7ExVKMpWnx+LtIdDeEi52lh29QrYCFpR74t8=; b=rrt PIDSNdYqvPyW9p+IZvQWPw9wZGrVcgZ3Qd1bQ6taK6NLYftsySrIteD0/ylIF+6U syDu0Kl+1JXp4b9AIVyZr8g0u7mfKXV9Rlqxos7+wyAXbY3b5ymKJCk26xkrATUc rZzU3gnBp1OAE5AJVvpwUt2q44EH7iFomLs1CqzI= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 40rjesm616-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 02 Aug 2024 04:27:41 -0700 (PDT) Received: from twshared18385.08.ash8.facebook.com (2620:10d:c0a8:1c::1b) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1544.11; Fri, 2 Aug 2024 11:27:39 +0000 Received: by devbig276.nha1.facebook.com (Postfix, from userid 660015) id A4A0F4FE0C0B; Fri, 2 Aug 2024 12:27:33 +0100 (BST) From: Mark Harmstone To: CC: Mark Harmstone , Mark Harmstone , Omar Sandoval Subject: [PATCH v2 1/3] btrfs-progs: use libbtrfsutil for btrfs subvolume create Date: Fri, 2 Aug 2024 12:27:22 +0100 Message-ID: <20240802112730.3575159-2-maharmstone@fb.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240802112730.3575159-1-maharmstone@fb.com> References: <20240802112730.3575159-1-maharmstone@fb.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: 1gDypF3yJtuB_nGE0sacWhwV42DDVLUG X-Proofpoint-ORIG-GUID: 1gDypF3yJtuB_nGE0sacWhwV42DDVLUG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-02_08,2024-08-01_01,2024-05-17_01 From: Mark Harmstone Call btrfs_util_subvolume_create in create_one_subvolume rather than calling the ioctl directly. Signed-off-by: Mark Harmstone Co-authored-by: Omar Sandoval Reviewed-by: Qu Wenruo --- cmds/subvolume.c | 100 ++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 57 deletions(-) diff --git a/cmds/subvolume.c b/cmds/subvolume.c index b4151af2..2a635fa2 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,28 +141,15 @@ 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; - } + enum btrfs_util_error err; dupname = strdup(dst); if (!dupname) { @@ -179,19 +167,6 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in } 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; - } - if (create_parents) { char p[PATH_MAX] = { 0 }; char dstdir_dup[PATH_MAX]; @@ -223,47 +198,57 @@ static int create_one_subvolume(const char *dst, struct btrfs_qgroup_inherit *in } } - fddst = btrfs_open_dir(dstdir); - if (fddst < 0) { - ret = fddst; + err = btrfs_util_subvolume_create(dst, 0, NULL, inherit); + if (err) { + error_btrfs_util(err); + ret = 1; goto out; } - if (inherit) { - struct btrfs_ioctl_vol_args_v2 args; + pr_verbose(LOG_DEFAULT, "Create subvolume '%s/%s'\n", dstdir, newname); - 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; + ret = 0; - ret = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args); - } else { - struct btrfs_ioctl_vol_args args; +out: + free(dupname); + free(dupdir); + + return ret; +} + +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_qgroup_inherit_create(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 +266,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; @@ -304,13 +289,14 @@ static int cmd_subvolume_create(const struct cmd_struct *cmd, int argc, char **a for (int i = optind; i < argc; i++) { ret = create_one_subvolume(argv[i], inherit, create_parents); - if (ret < 0) + if (ret) has_error = true; } if (!has_error) retval = 0; out: - free(inherit); + if (inherit) + btrfs_util_qgroup_inherit_destroy(inherit); return retval; }