From patchwork Thu Feb 15 19:05:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10223413 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 23EEE602CB for ; Thu, 15 Feb 2018 19:06:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12B3829485 for ; Thu, 15 Feb 2018 19:06:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0682B2948F; Thu, 15 Feb 2018 19:06:23 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4989529485 for ; Thu, 15 Feb 2018 19:06:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161145AbeBOTGU (ORCPT ); Thu, 15 Feb 2018 14:06:20 -0500 Received: from mail-pg0-f67.google.com ([74.125.83.67]:39546 "EHLO mail-pg0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161161AbeBOTFu (ORCPT ); Thu, 15 Feb 2018 14:05:50 -0500 Received: by mail-pg0-f67.google.com with SMTP id w17so502811pgv.6 for ; Thu, 15 Feb 2018 11:05:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=osandov-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=SLRdT6pSUd+9geAZzXeSjVf6iRh6XSVR5TOl59fC0RY=; b=ya/SKWe+v+m/HIR14js5xipW27cEF5179J8y8vsOHvMij/+dxyQAVbwKZku1ixoMO7 BBTB5QoWxiyMjyW7c0NsHw4s/5gVv/NKxUmiDtzM/Il7bbyzTqauaqN19Zr30iBMulL5 8NlkVqGjc/vyPO9LmLC5aw9VBCChz+AGOskX7P3SHDd3BsS4yPFC94EXutQ35Z42ngfF com4CioVf9K7czc14gF3xLXDWHC+meomvD2w1A0Umeo69T5VrZ0LEcQAW0CpkAP10JcZ EqpOjckkosLvn/N4POswjO8D8rBAviRfSXWLpxYW/hz5y1ykZHPahNIT/MW6sBzIilkS qn9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=SLRdT6pSUd+9geAZzXeSjVf6iRh6XSVR5TOl59fC0RY=; b=pAObS/pxY/Q5+Ua+5wkSQNHyAV+DG/tuDNQPFoBtztCrOdwdJoAgAF9nGNxPviz9an IKbasD8esOqvI4UdDB2qxhf6BGjLsDuI4wyjfnKfaPMg4OkLUcozFzA3xKCLVKgPXZYY OjbHJDgQhKjC3IPjm/yYxtDftReQp3gPqgDmMFg4R4rfGqeEtlCtbKkSD9dAwPpA8Tbg PSkOFOVs7c/0zjdjXf+UwRavRWU/D8UgXQJcJ7ISSd4LWew/qd1pbYCqzcXys/AgIkQi +1guiPxe8bJIyDkYLj6nossElzemdoDv5ufpX2J3THEnllB/6s6v/f1SnSNRPstG3UE2 OD/Q== X-Gm-Message-State: APf1xPDqyw2U7KF5a+cdvdnH2feTBrxZx00unx8cIDEPJ1H7twx4gfu+ rOw3e4S+k2saTBjwl/u0uw5zlbq9AQU= X-Google-Smtp-Source: AH8x226M7Ca5Do8Qpg3lVaGsag1VCiSO1rClMFHcc/8D1bzbZnaYtLbCBfKdmgxJ62nySKYNpXuccg== X-Received: by 10.98.198.143 with SMTP id x15mr3555348pfk.22.1518721549341; Thu, 15 Feb 2018 11:05:49 -0800 (PST) Received: from vader.thefacebook.com ([2620:10d:c090:200::6:4a19]) by smtp.gmail.com with ESMTPSA id p1sm40467428pgr.44.2018.02.15.11.05.48 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Feb 2018 11:05:48 -0800 (PST) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com Subject: [PATCH v2 20/27] btrfs-progs: use libbtrfsutil for subvol create and snapshot Date: Thu, 15 Feb 2018 11:05:05 -0800 Message-Id: <27f66cec6698af6902c2e89c52c52d9032e83738.1518720598.git.osandov@fb.com> X-Mailer: git-send-email 2.16.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Omar Sandoval These become trivial calls to the libbtrfsutil helpers, and we can get rid of the qgroup inherit code in progs. Signed-off-by: Omar Sandoval --- cmds-subvolume.c | 225 ++++++++++++++----------------------------------------- qgroup.c | 64 ---------------- qgroup.h | 2 - 3 files changed, 58 insertions(+), 233 deletions(-) diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 42cc30ce..ea436ba0 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -103,6 +103,35 @@ static int wait_for_subvolume_cleaning(int fd, int count, u64 *ids, return 0; } +static int qgroup_inherit_add_group(struct btrfs_util_qgroup_inherit **inherit, + const char *arg) +{ + enum btrfs_util_error err; + u64 qgroupid; + + if (!*inherit) { + err = btrfs_util_create_qgroup_inherit(0, inherit); + if (err) { + error_btrfs_util(err); + return -1; + } + } + + qgroupid = parse_qgroupid(optarg); + if (qgroupid == 0) { + error("invalid qgroup specification, qgroupid must not be 0"); + return -1; + } + + err = btrfs_util_qgroup_inherit_add_group(inherit, qgroupid); + if (err) { + error_btrfs_util(err); + return -1; + } + + return 0; +} + static const char * const subvolume_cmd_group_usage[] = { "btrfs subvolume ", NULL @@ -121,15 +150,9 @@ static const char * const cmd_subvol_create_usage[] = { static int cmd_subvol_create(int argc, char **argv) { - int retval, res, len; - int fddst = -1; - char *dupname = NULL; - char *dupdir = NULL; - char *newname; - char *dstdir; - char *dst; - struct btrfs_qgroup_inherit *inherit = NULL; - DIR *dirstream = NULL; + struct btrfs_util_qgroup_inherit *inherit = NULL; + enum btrfs_util_error err; + int retval = 1; while (1) { int c = getopt(argc, argv, "i:"); @@ -138,11 +161,8 @@ static int cmd_subvol_create(int argc, char **argv) switch (c) { case 'i': - res = qgroup_inherit_add_group(&inherit, optarg); - if (res) { - retval = res; + if (qgroup_inherit_add_group(&inherit, optarg) == -1) goto out; - } break; default: usage(cmd_subvol_create_usage); @@ -152,70 +172,18 @@ static int cmd_subvol_create(int argc, char **argv) if (check_argc_exact(argc - optind, 1)) usage(cmd_subvol_create_usage); - dst = argv[optind]; - - retval = 1; /* failure */ - res = test_isdir(dst); - if (res < 0 && res != -ENOENT) { - error("cannot access %s: %s", dst, strerror(-res)); - goto out; - } - if (res >= 0) { - error("target path already exists: %s", dst); - goto out; - } - - dupname = strdup(dst); - newname = basename(dupname); - dupdir = strdup(dst); - dstdir = dirname(dupdir); - - if (!test_issubvolname(newname)) { - error("invalid subvolume name: %s", newname); - goto out; - } - - len = strlen(newname); - if (len == 0 || len >= BTRFS_VOL_NAME_MAX) { - error("subvolume name too long: %s", newname); - goto out; - } - - fddst = btrfs_open_dir(dstdir, &dirstream, 1); - if (fddst < 0) - goto out; - - printf("Create subvolume '%s/%s'\n", dstdir, newname); - if (inherit) { - struct btrfs_ioctl_vol_args_v2 args; - - memset(&args, 0, sizeof(args)); - strncpy_null(args.name, newname); - args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT; - args.size = qgroup_inherit_size(inherit); - args.qgroup_inherit = inherit; - - res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE_V2, &args); - } else { - struct btrfs_ioctl_vol_args args; - - memset(&args, 0, sizeof(args)); - strncpy_null(args.name, newname); - - res = ioctl(fddst, BTRFS_IOC_SUBVOL_CREATE, &args); - } + printf("Create subvolume '%s'\n", argv[optind]); - if (res < 0) { - error("cannot create subvolume: %m"); + err = btrfs_util_create_subvolume(argv[optind], 0, NULL, inherit); + if (err) { + error_btrfs_util(err); goto out; } - retval = 0; /* success */ + retval = 0; out: - close_file_or_dir(fddst, dirstream); - free(inherit); - free(dupname); - free(dupdir); + if (inherit) + btrfs_util_destroy_qgroup_inherit(inherit); return retval; } @@ -650,19 +618,11 @@ static const char * const cmd_subvol_snapshot_usage[] = { static int cmd_subvol_snapshot(int argc, char **argv) { - char *subvol, *dst; - int res, retval; - int fd = -1, fddst = -1; - int len, readonly = 0; - char *dupname = NULL; - char *dupdir = NULL; - char *newname; - char *dstdir; - struct btrfs_ioctl_vol_args_v2 args; - struct btrfs_qgroup_inherit *inherit = NULL; - DIR *dirstream1 = NULL, *dirstream2 = NULL; + struct btrfs_util_qgroup_inherit *inherit = NULL; + enum btrfs_util_error err; + int flags = 0; + int retval = 1; - memset(&args, 0, sizeof(args)); while (1) { int c = getopt(argc, argv, "i:r"); if (c < 0) @@ -670,14 +630,11 @@ static int cmd_subvol_snapshot(int argc, char **argv) switch (c) { case 'i': - res = qgroup_inherit_add_group(&inherit, optarg); - if (res) { - retval = res; + if (qgroup_inherit_add_group(&inherit, optarg) == -1) goto out; - } break; case 'r': - readonly = 1; + flags |= BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY; break; default: usage(cmd_subvol_snapshot_usage); @@ -687,92 +644,26 @@ static int cmd_subvol_snapshot(int argc, char **argv) if (check_argc_exact(argc - optind, 2)) usage(cmd_subvol_snapshot_usage); - subvol = argv[optind]; - dst = argv[optind + 1]; - - retval = 1; /* failure */ - res = test_issubvolume(subvol); - if (res < 0) { - error("cannot access subvolume %s: %s", subvol, strerror(-res)); - goto out; - } - if (!res) { - error("not a subvolume: %s", subvol); - goto out; - } - - res = test_isdir(dst); - if (res < 0 && res != -ENOENT) { - error("cannot access %s: %s", dst, strerror(-res)); - goto out; - } - if (res == 0) { - error("'%s' exists and it is not a directory", dst); - goto out; - } - - if (res > 0) { - dupname = strdup(subvol); - newname = basename(dupname); - dstdir = dst; + if (flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY) { + printf("Create a readonly snapshot of '%s' in '%s'\n", + argv[optind], argv[optind + 1]); } else { - dupname = strdup(dst); - newname = basename(dupname); - dupdir = strdup(dst); - dstdir = dirname(dupdir); + printf("Create a snapshot of '%s' in '%s'\n", argv[optind], + argv[optind + 1]); } - if (!test_issubvolname(newname)) { - error("invalid snapshot name '%s'", newname); - goto out; - } - - len = strlen(newname); - if (len == 0 || len >= BTRFS_VOL_NAME_MAX) { - error("snapshot name too long '%s'", newname); - goto out; - } - - fddst = btrfs_open_dir(dstdir, &dirstream1, 1); - if (fddst < 0) - goto out; - - fd = btrfs_open_dir(subvol, &dirstream2, 1); - if (fd < 0) - goto out; - - if (readonly) { - args.flags |= BTRFS_SUBVOL_RDONLY; - printf("Create a readonly snapshot of '%s' in '%s/%s'\n", - subvol, dstdir, newname); - } else { - printf("Create a snapshot of '%s' in '%s/%s'\n", - subvol, dstdir, newname); - } - - args.fd = fd; - if (inherit) { - args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT; - args.size = qgroup_inherit_size(inherit); - args.qgroup_inherit = inherit; - } - strncpy_null(args.name, newname); - - res = ioctl(fddst, BTRFS_IOC_SNAP_CREATE_V2, &args); - - if (res < 0) { - error("cannot snapshot '%s': %m", subvol); + err = btrfs_util_create_snapshot(argv[optind], argv[optind + 1], flags, + NULL, inherit); + if (err) { + error_btrfs_util(err); goto out; } - retval = 0; /* success */ + retval = 0; out: - close_file_or_dir(fddst, dirstream1); - close_file_or_dir(fd, dirstream2); - free(inherit); - free(dupname); - free(dupdir); + if (inherit) + btrfs_util_destroy_qgroup_inherit(inherit); return retval; } diff --git a/qgroup.c b/qgroup.c index b107a683..69395939 100644 --- a/qgroup.c +++ b/qgroup.c @@ -1260,67 +1260,3 @@ out: free(opt_tmp); return ret; } - -int 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(sizeof(*out) + sizeof(out->qgroups[0]) * (nitems + n), 1); - if (out == NULL) { - error("not enough memory"); - 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 qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg) -{ - int ret; - u64 qgroupid = parse_qgroupid(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; -} diff --git a/qgroup.h b/qgroup.h index bb6610d7..97c6cd9e 100644 --- a/qgroup.h +++ b/qgroup.h @@ -90,7 +90,5 @@ struct btrfs_qgroup_comparer_set *btrfs_qgroup_alloc_comparer_set(void); int btrfs_qgroup_setup_comparer(struct btrfs_qgroup_comparer_set **comp_set, enum btrfs_qgroup_comp_enum comparer, int is_descending); -int qgroup_inherit_size(struct btrfs_qgroup_inherit *p); -int qgroup_inherit_add_group(struct btrfs_qgroup_inherit **inherit, char *arg); #endif