From patchwork Sat Nov 16 17:09:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Goffredo Baroncelli X-Patchwork-Id: 3192941 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id CC0829F3AE for ; Sat, 16 Nov 2013 17:09:34 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id BE16D207BF for ; Sat, 16 Nov 2013 17:09:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BA66F20233 for ; Sat, 16 Nov 2013 17:09:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752380Ab3KPRJa (ORCPT ); Sat, 16 Nov 2013 12:09:30 -0500 Received: from mail-wg0-f45.google.com ([74.125.82.45]:58661 "EHLO mail-wg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751034Ab3KPRJO (ORCPT ); Sat, 16 Nov 2013 12:09:14 -0500 Received: by mail-wg0-f45.google.com with SMTP id a1so4748314wgh.12 for ; Sat, 16 Nov 2013 09:09:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=cMzE+MsTHLLTY7ZHOlPIIEh/OcOTCJ7jd5Plv3cgu0U=; b=kPvZqkgiIVOJdyIsTvETp5tRJrmsWTd13Joh0M+HFctbdf03oBF36ykJP+SO09Jqlr kiNvSF9x92kr46f/qNid4uMX4A7wlxCKVuaVvgwWBUd8YWW/fRMVt5D56t8JOC5xN8BZ E2HffAYnFvBjdGtwZGsl9l+cunZ02DvAeZRZ241yBcDaaCD5VLAx4ryX9SEwlrBLn62g U4Up2ygydFtB9ReiFva90POvCg8fWPn9EQauRoE1LINA4F3QMPdk4/25dbZgPm20Twnf WQRDRT7ZzSOq8fJYl/hPZY7/24P8PZwUtvhLx5ZM0dwGJQGzU2NxWMD7nXLaJ+CX70/5 Rr9Q== X-Received: by 10.181.5.40 with SMTP id cj8mr11183215wid.18.1384621753683; Sat, 16 Nov 2013 09:09:13 -0800 (PST) Received: from venice.bhome (ppp-80-52.24-151.libero.it. [151.24.52.80]) by mx.google.com with ESMTPSA id e1sm6584799wij.6.2013.11.16.09.09.13 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 16 Nov 2013 09:09:13 -0800 (PST) From: Goffredo Baroncelli To: linux-btrfs@vger.kernel.org Cc: Goffredo Baroncelli Subject: [PATCH 1/7] Recursive btrfs sub snapshot/delete: create get_root_info() function Date: Sat, 16 Nov 2013 18:09:01 +0100 Message-Id: <1384621747-25441-2-git-send-email-kreijack@inwind.it> X-Mailer: git-send-email 1.8.4.3 In-Reply-To: <1384621747-25441-1-git-send-email-kreijack@inwind.it> References: <1384621747-25441-1-git-send-email-kreijack@inwind.it> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Move some code from cmd_subvol_show() to get_root_info(). This is a preparation for the introducing of traverse_list_subvol_rec() function. Signed-off-by: Goffredo Baroncelli --- btrfs-list.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ btrfs-list.h | 7 +++++ cmds-subvolume.c | 71 ++++-------------------------------------- 3 files changed, 108 insertions(+), 65 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index f3618b9..66f3127 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -33,6 +33,8 @@ #include "utils.h" #include #include "btrfs-list.h" +#include "commands.h" +#include "utils.h" #define BTRFS_LIST_NFILTERS_INCREASE (2 * BTRFS_LIST_FILTER_MAX) #define BTRFS_LIST_NCOMPS_INCREASE (2 * BTRFS_LIST_COMP_MAX) @@ -1917,3 +1919,96 @@ int btrfs_list_get_path_rootid(int fd, u64 *treeid) *treeid = args.treeid; return 0; } + +/* + * Fill the root_info struct + */ +int get_root_info(char *path, struct root_info *get_ri) +{ + + char *svpath = NULL, *mnt = NULL, *fullpath=NULL; + u64 sv_id, mntid; + int fd = -1, mntfd = -1; + DIR *fddir, *mntfddir; + int ret = -1; + + fullpath = realpath(path, 0); + + ret = test_issubvolume(fullpath); + if (ret < 0) { + fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath); + goto out; + } + if (!ret) { + fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath); + ret = -1; + goto out; + } + + ret = find_mount_root(fullpath, &mnt); + if (ret < 0) { + fprintf(stderr, "ERROR: find_mount_root failed on %s: " + "%s\n", fullpath, strerror(-ret)); + goto out; + } + ret = -1; + svpath = get_subvol_name(mnt, fullpath); + + fd = open_file_or_dir(fullpath, &fddir); + if (fd < 0) { + fprintf(stderr, "ERROR: can't access '%s'\n", fullpath); + goto out; + } + + ret = btrfs_list_get_path_rootid(fd, &sv_id); + if (ret) { + fprintf(stderr, "ERROR: can't get rootid for '%s'\n", + fullpath); + goto out; + } + + mntfd = open_file_or_dir(mnt, &mntfddir); + if (mntfd < 0) { + fprintf(stderr, "ERROR: can't access '%s'\n", mnt); + goto out; + } + + ret = btrfs_list_get_path_rootid(mntfd, &mntid); + if (ret) { + fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt); + goto out; + } + + if (sv_id == BTRFS_FS_TREE_OBJECTID) { + printf("ERROR: %s is btrfs root\n", fullpath); + goto out; + } + + memset(get_ri, 0, sizeof(*get_ri)); + get_ri->root_id = sv_id; + + if (btrfs_get_subvol(mntfd, get_ri)) { + fprintf(stderr, "ERROR: can't find '%s'\n", + svpath); + goto out; + } + + ret = 0; + +out: + if (mntfd >= 0) + close_file_or_dir(mntfd, mntfddir); + if (fd >= 0) + close_file_or_dir(fd, fddir); + free(mnt); + free(fullpath); + + return ret; +} + +void free_root_info(struct root_info *ri) +{ + free(ri->path); + free(ri->name); + free(ri->full_path); +} diff --git a/btrfs-list.h b/btrfs-list.h index 724e973..db32805 100644 --- a/btrfs-list.h +++ b/btrfs-list.h @@ -16,6 +16,9 @@ * Boston, MA 021110-1307, USA. */ +#ifndef __BTRFS_LIST__ +#define __BTRFS_LIST__ + #if BTRFS_FLAT_INCLUDES #include "kerncompat.h" #else @@ -163,3 +166,7 @@ int btrfs_list_get_default_subvolume(int fd, u64 *default_id); char *btrfs_list_path_for_root(int fd, u64 root); int btrfs_list_get_path_rootid(int fd, u64 *treeid); int btrfs_get_subvol(int fd, struct root_info *the_ri); +int get_root_info(char *path, struct root_info *get_ri); +void free_root_info(struct root_info *ri); + +#endif diff --git a/cmds-subvolume.c b/cmds-subvolume.c index f57694a..477919c 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -827,12 +827,11 @@ static int cmd_subvol_show(int argc, char **argv) struct btrfs_list_filter_set *filter_set; char tstr[256]; char uuidparse[37]; - char *fullpath = NULL, *svpath = NULL, *mnt = NULL; + char *fullpath = NULL; char raw_prefix[] = "\t\t\t\t"; - u64 sv_id, mntid; - int fd = -1, mntfd = -1; + int fd = -1; int ret = 1; - DIR *dirstream1 = NULL, *dirstream2 = NULL; + DIR *dirstream1 = NULL; if (check_argc_exact(argc, 2)) usage(cmd_subvol_show_usage); @@ -844,63 +843,15 @@ static int cmd_subvol_show(int argc, char **argv) goto out; } - ret = test_issubvolume(fullpath); - if (ret < 0) { - fprintf(stderr, "ERROR: error accessing '%s'\n", fullpath); - goto out; - } - if (!ret) { - fprintf(stderr, "ERROR: '%s' is not a subvolume\n", fullpath); - goto out; - } - - ret = find_mount_root(fullpath, &mnt); - if (ret < 0) { - fprintf(stderr, "ERROR: find_mount_root failed on %s: " - "%s\n", fullpath, strerror(-ret)); - goto out; - } - ret = 1; - svpath = get_subvol_name(mnt, fullpath); - fd = open_file_or_dir(fullpath, &dirstream1); if (fd < 0) { fprintf(stderr, "ERROR: can't access '%s'\n", fullpath); goto out; } - ret = btrfs_list_get_path_rootid(fd, &sv_id); - if (ret) { - fprintf(stderr, "ERROR: can't get rootid for '%s'\n", - fullpath); - goto out; - } - - mntfd = open_file_or_dir(mnt, &dirstream2); - if (mntfd < 0) { - fprintf(stderr, "ERROR: can't access '%s'\n", mnt); - goto out; - } - - ret = btrfs_list_get_path_rootid(mntfd, &mntid); - if (ret) { - fprintf(stderr, "ERROR: can't get rootid for '%s'\n", mnt); + ret = get_root_info(fullpath, &get_ri); + if (ret) goto out; - } - - if (sv_id == BTRFS_FS_TREE_OBJECTID) { - printf("%s is btrfs root\n", fullpath); - goto out; - } - - memset(&get_ri, 0, sizeof(get_ri)); - get_ri.root_id = sv_id; - - if (btrfs_get_subvol(mntfd, &get_ri)) { - fprintf(stderr, "ERROR: can't find '%s'\n", - svpath); - goto out; - } ret = 0; /* print the info */ @@ -949,22 +900,12 @@ static int cmd_subvol_show(int argc, char **argv) 1, raw_prefix); /* clean up */ - if (get_ri.path) - free(get_ri.path); - if (get_ri.name) - free(get_ri.name); - if (get_ri.full_path) - free(get_ri.full_path); + free_root_info(&get_ri); if (filter_set) btrfs_list_free_filter_set(filter_set); out: close_file_or_dir(fd, dirstream1); - close_file_or_dir(mntfd, dirstream2); - if (mnt) - free(mnt); - if (fullpath) - free(fullpath); return !!ret; }