From patchwork Fri Jan 26 18:41:07 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Omar Sandoval X-Patchwork-Id: 10186751 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 0E2CE601D5 for ; Fri, 26 Jan 2018 18:41:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1BB929ECA for ; Fri, 26 Jan 2018 18:41:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E674D29EE1; Fri, 26 Jan 2018 18:41:48 +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 3525C29ECA for ; Fri, 26 Jan 2018 18:41:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752082AbeAZSlq (ORCPT ); Fri, 26 Jan 2018 13:41:46 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:36736 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751956AbeAZSlm (ORCPT ); Fri, 26 Jan 2018 13:41:42 -0500 Received: by mail-pf0-f196.google.com with SMTP id 23so821869pfp.3 for ; Fri, 26 Jan 2018 10:41:41 -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=N0FbM49DFVNvgkrjOLrC4qCbTB1QTe68dPzBzRtEOuc=; b=k181T/Zd4iH7Ki7X+hPIFeCJzpdRoD4yGltpNhNJHNck0t17EdnuduoVxwkNzHmh2g tFAMzi6rydbtuxtJKQJwDCcV3/G7XV0Ku+RyzNG/+bB6ZqIuxp/A/pai/9MONhEibBfQ XsOqUAiBjzrq9A4eM4Gh09qvvqThOXDL2cvmsmFZowX17vEbkSK19cgPIRhBrabcwM3J afc4xf6hhRat34Z96CR0lhmYLZWrKLM6aFxLV+ROnAW+LPpHtLN7PtiWEPdN/Sm9cFs8 eF2v8hCQD4e/MxNKdTsBSs5TeAb4gvhf9KdJfbTl3UTS4FV/94GQNxV3aI5Aq5p5HH7/ zJ1g== 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=N0FbM49DFVNvgkrjOLrC4qCbTB1QTe68dPzBzRtEOuc=; b=JJiMuzdqQwpR9fJkxLPWW9x/6vWGebQlOgOTQtyPzrFstBGpSh3TTd3dEj6HGyD2pz akzeXcfDfXzVVtaIG9mveodb5uFeqZCnwgnXmAIXaEtu1H62vx4ZKaAAM833F5DDOHno o5qqnJrYIWUpEjbRLI7HjxpEpWoR5cx3Dx6YxYygrTEz5AJKp3kdloj+TjDTvlhUULa7 XpfGPHsl+HcoRzZeIfT9lpBkwsoKlf0BjBR3jk7JTd6lam5d1RN45a/gQDzk383sJ2A9 5tpLAJUZgfvMwxigtSrqYVosN1XC0zSnQNTKAAe/chN1YoinCa/5/x4RTBTU2B3JaBzU p9rQ== X-Gm-Message-State: AKwxytfqPvftOtu3GuS5O0YtGdYbvCGkW1Sq7LDcf8Tiuns7vgtiEiX6 7IBuXx0z+ZJWEP6FgXg838k11thdbJc= X-Google-Smtp-Source: AH8x224MtWWKvcf9VsjslO+vkj4kwxNUVGAi9cPWZtYZIoS8ec/nRWbXmCA18mCDQqVFPt1WeSbUdQ== X-Received: by 2002:a17:902:8f87:: with SMTP id z7-v6mr14977763plo.242.1516992100920; Fri, 26 Jan 2018 10:41:40 -0800 (PST) Received: from vader.thefacebook.com ([2620:10d:c090:200::6:7f96]) by smtp.gmail.com with ESMTPSA id y29sm19627400pff.24.2018.01.26.10.41.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Jan 2018 10:41:40 -0800 (PST) From: Omar Sandoval To: linux-btrfs@vger.kernel.org Cc: kernel-team@fb.com Subject: [PATCH 19/26] btrfs-progs: use libbtrfsutil for subvol create and snapshot Date: Fri, 26 Jan 2018 10:41:07 -0800 Message-Id: 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 557108c1..6b474fe4 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: %s", strerror(errno)); + 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; } @@ -653,19 +621,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) @@ -673,14 +633,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); @@ -690,92 +647,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': %s", subvol, strerror(errno)); + 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 e9158a26..6e5fdb01 100644 --- a/qgroup.c +++ b/qgroup.c @@ -1246,67 +1246,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