From patchwork Mon Nov 4 03:45:42 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anand Jain X-Patchwork-Id: 3134521 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 69185BEEB2 for ; Mon, 4 Nov 2013 03:37:35 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id E9517201F7 for ; Mon, 4 Nov 2013 03:37:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BAF8320347 for ; Mon, 4 Nov 2013 03:37:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752572Ab3KDDh1 (ORCPT ); Sun, 3 Nov 2013 22:37:27 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:38141 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752432Ab3KDDh0 (ORCPT ); Sun, 3 Nov 2013 22:37:26 -0500 Received: from ucsinet21.oracle.com (ucsinet21.oracle.com [156.151.31.93]) by aserp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id rA43bIrn020992 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 4 Nov 2013 03:37:19 GMT Received: from userz7021.oracle.com (userz7021.oracle.com [156.151.31.85]) by ucsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id rA43bINc012577 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Mon, 4 Nov 2013 03:37:18 GMT Received: from abhmt104.oracle.com (abhmt104.oracle.com [141.146.116.56]) by userz7021.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id rA43bH0o012560; Mon, 4 Nov 2013 03:37:17 GMT Received: from wish.sg.oracle.com (/10.186.101.18) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 03 Nov 2013 19:37:17 -0800 From: Anand Jain To: linux-btrfs@vger.kernel.org Cc: jbacik@fusionio.com, zab@redhat.com, dsterba@suse.cz Subject: [PATCH 1/4] btrfs-progs: make filesystem show by label work Date: Mon, 4 Nov 2013 11:45:42 +0800 Message-Id: <1383536745-4635-1-git-send-email-anand.jain@oracle.com> X-Mailer: git-send-email 1.8.1.164.g2d0029e X-Source-IP: ucsinet21.oracle.com [156.151.31.93] 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.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable 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 with design revamp around filesystem show the fsid filter by label wasn't planned. but apparently that seemed to be necessary. this patch will fix it. Signed-off-by: Anand Jain --- cmds-filesystem.c | 120 ++++++++++++++++++++++++++++++++--------------------- 1 files changed, 73 insertions(+), 47 deletions(-) diff --git a/cmds-filesystem.c b/cmds-filesystem.c index d08007e..d2cad81 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -179,6 +179,26 @@ static int cmd_df(int argc, char **argv) return !!ret; } +static int match_search_item_kernel(__u8 *fsid, char *mnt, char *label, + char *search) +{ + char uuidbuf[37]; + int search_len = strlen(search); + + search_len = min(search_len, 37); + uuid_unparse(fsid, uuidbuf); + if (!strncmp(uuidbuf, search, search_len)) + return 1; + + if (strlen(label) && strcmp(label, search) == 0) + return 1; + + if (strcmp(mnt, search) == 0) + return 1; + + return 0; +} + static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search) { char uuidbuf[37]; @@ -275,16 +295,18 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info, struct btrfs_ioctl_dev_info_args *tmp_dev_info; uuid_unparse(fs_info->fsid, uuidbuf); - printf("Label: %s uuid: %s\n", - strlen(label) ? label : "none", uuidbuf); + if (label && strlen(label)) + printf("Label: '%s' ", label); + else + printf("Label: none "); - printf("\tTotal devices %llu FS bytes used %s\n", - fs_info->num_devices, + printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, + fs_info->num_devices, pretty_size(calc_used_bytes(space_info))); for (i = 0; i < fs_info->num_devices; i++) { tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i]; - printf("\tdevid %llu size %s used %s path %s\n", + printf("\tdevid %4llu size %s used %s path %s\n", tmp_dev_info->devid, pretty_size(tmp_dev_info->total_bytes), pretty_size(tmp_dev_info->bytes_used), @@ -308,7 +330,7 @@ static int check_arg_type(char *input) char path[PATH_MAX]; if (!input) - return BTRFS_ARG_UNKNOWN; + return -EINVAL; if (realpath(input, path)) { if (is_block_device(input) == 1) @@ -320,7 +342,7 @@ static int check_arg_type(char *input) return BTRFS_ARG_UNKNOWN; } - if (!uuid_parse(input, out)) + if (strlen(input) == 36 && !uuid_parse(input, out)) return BTRFS_ARG_UUID; return BTRFS_ARG_UNKNOWN; @@ -328,23 +350,19 @@ static int check_arg_type(char *input) static int btrfs_scan_kernel(void *search) { - int ret = 0, fd, type; + int ret = 0, fd; FILE *f; struct mntent *mnt; struct btrfs_ioctl_fs_info_args fs_info_arg; struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL; struct btrfs_ioctl_space_args *space_info_arg; char label[BTRFS_LABEL_SIZE]; - uuid_t uuid; f = setmntent("/proc/self/mounts", "r"); if (f == NULL) return 1; - type = check_arg_type(search); - if (type == BTRFS_ARG_BLKDEV) - return 1; - + memset(label, 0, sizeof(label)); while ((mnt = getmntent(f)) != NULL) { if (strcmp(mnt->mnt_type, "btrfs")) continue; @@ -353,38 +371,36 @@ static int btrfs_scan_kernel(void *search) if (ret) return ret; - switch (type) { - case BTRFS_ARG_UUID: - ret = uuid_parse(search, uuid); - if (ret) - return 1; - if (uuid_compare(fs_info_arg.fsid, uuid)) - continue; - break; - case BTRFS_ARG_MNTPOINT: - if (strcmp(search, mnt->mnt_dir)) - continue; - break; - case BTRFS_ARG_UNKNOWN: - break; + if (get_label_mounted(mnt->mnt_dir, label)) { + kfree(dev_info_arg); + return 1; + } + if (search && !match_search_item_kernel(fs_info_arg.fsid, + mnt->mnt_dir, label, search)) { + kfree(dev_info_arg); + continue; } fd = open(mnt->mnt_dir, O_RDONLY); if (fd > 0 && !get_df(fd, &space_info_arg)) { - get_label_mounted(mnt->mnt_dir, label); print_one_fs(&fs_info_arg, dev_info_arg, space_info_arg, label, mnt->mnt_dir); - free(space_info_arg); + kfree(space_info_arg); + memset(label, 0, sizeof(label)); } if (fd > 0) close(fd); - free(dev_info_arg); + kfree(dev_info_arg); + if (search) + return 0; } - return ret; + if (search) + return 1; + return 0; } static const char * const cmd_show_usage[] = { - "btrfs filesystem show [options] [||]", + "btrfs filesystem show [options] [|||label]", "Show the structure of a filesystem", "-d|--all-devices show only disks under /dev containing btrfs filesystem", "-m|--mounted show only mounted btrfs", @@ -402,6 +418,7 @@ static int cmd_show(int argc, char **argv) int where = BTRFS_SCAN_LBLKID; int type = 0; char mp[BTRFS_PATH_NAME_MAX + 1]; + char path[PATH_MAX]; while (1) { int long_index; @@ -426,24 +443,31 @@ static int cmd_show(int argc, char **argv) } } - if (where == BTRFS_SCAN_LBLKID) { - if (check_argc_max(argc, optind + 1)) - usage(cmd_show_usage); - } else { - if (check_argc_max(argc, optind)) - usage(cmd_show_usage); - } + if (check_argc_max(argc, optind + 1)) + usage(cmd_show_usage); + if (argc > optind) { search = argv[optind]; - type = check_arg_type(search); - if (type == BTRFS_ARG_UNKNOWN) { - fprintf(stderr, "ERROR: arg type unknown\n"); + if (strlen(search) == 0) usage(cmd_show_usage); - } + type = check_arg_type(search); if (type == BTRFS_ARG_BLKDEV) { - ret = get_btrfs_mount(search, mp, sizeof(mp)); - if (ret == 0) - search = mp; + if (where == BTRFS_SCAN_DEV) { + /* we need to do this because + * legacy BTRFS_SCAN_DEV + * provides /dev/dm-x paths + */ + if (realpath(search, path)) + search = path; + } else { + ret = get_btrfs_mount(search, + mp, sizeof(mp)); + if (!ret) + /* given block dev is mounted*/ + search = mp; + else + goto devs_only; + } } } @@ -451,7 +475,9 @@ static int cmd_show(int argc, char **argv) goto devs_only; /* show mounted btrfs */ - btrfs_scan_kernel(search); + ret = btrfs_scan_kernel(search); + if (search && !ret) + return 0; /* shows mounted only */ if (where == BTRFS_SCAN_MOUNTED)