From patchwork Mon Nov 18 19:32:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Mason X-Patchwork-Id: 3198681 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 4F6D4C045C for ; Mon, 18 Nov 2013 19:32:29 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 1BB0D20155 for ; Mon, 18 Nov 2013 19:32:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id C2EFD20131 for ; Mon, 18 Nov 2013 19:32:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751557Ab3KRTcX (ORCPT ); Mon, 18 Nov 2013 14:32:23 -0500 Received: from dkim1.fusionio.com ([66.114.96.53]:36328 "EHLO dkim1.fusionio.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751341Ab3KRTcW convert rfc822-to-8bit (ORCPT ); Mon, 18 Nov 2013 14:32:22 -0500 Received: from mx1.fusionio.com (unknown [10.101.1.160]) by dkim1.fusionio.com (Postfix) with ESMTP id A485F7C06A4 for ; Mon, 18 Nov 2013 12:32:21 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fusionio.com; s=default; t=1384803141; bh=Rlt8yWO3sNkl5MhHl4ik1NBjkYO4UjNBKbAz4qDbZRw=; h=From:To:References:In-Reply-To:Subject:Date; b=AfikDu7Rl1AnCs6jgyZBvZx8bVAUS8HVUC833154XmppV5ZIyZfU6sqoH2ILuZykE CPCEkLtAOt6iI9Lwu+NP9Qth5msCzo3NkoUz4BGX5yjbe+P2j8KAgEvwItisepKbV9 J8Ic/Cun7hA8FAc73UX5L+f87urw4z1iLt7N3Q+4= X-ASG-Debug-ID: 1384803141-03d6a568cc19c30001-6jHSXT Received: from CAS2.int.fusionio.com (cas2.int.fusionio.com [10.101.1.41]) by mx1.fusionio.com with ESMTP id Zi87NJjQyiD3dveV (version=TLSv1 cipher=AES128-SHA bits=128 verify=NO); Mon, 18 Nov 2013 12:32:21 -0700 (MST) X-Barracuda-Envelope-From: clmason@fusionio.com Received: from localhost (10.101.1.160) by mail.fusionio.com (10.101.1.41) with Microsoft SMTP Server (TLS) id 14.3.158.1; Mon, 18 Nov 2013 12:32:20 -0700 MIME-Version: 1.0 From: Chris Mason User-Agent: alot/0.3.4 To: Anand Jain , Gene Czarcinski , , Eric Sandeen References: <528787FB.4040100@czarc.net> <528978AF.3020806@oracle.com> In-Reply-To: <528978AF.3020806@oracle.com> Message-ID: <20131118193206.5312.78304@localhost.localdomain> Subject: Re: btrfs fi show Date: Mon, 18 Nov 2013 14:32:06 -0500 X-ASG-Orig-Subj: Re: btrfs fi show X-Originating-IP: [10.101.1.160] X-Barracuda-Connect: cas2.int.fusionio.com[10.101.1.41] X-Barracuda-Start-Time: 1384803141 X-Barracuda-Encrypted: AES128-SHA X-Barracuda-URL: http://10.101.1.180:8000/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at fusionio.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Bayes: INNOCENT GLOBAL 0.0000 1.0000 -2.0210 X-Barracuda-Spam-Score: -2.02 X-Barracuda-Spam-Status: No, SCORE=-2.02 using per-user scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=9.0 tests= X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.2.142419 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, 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 Quoting Anand Jain (2013-11-17 21:17:19) > There is a fix for this (which David has integrated into the branch > recently). This also needs the kernel side patch which is listed below. > > [PATCH v4 1/3] btrfs-progs: mechanism to fetch fsinfo from btrfs-control > [PATCH v4 2/3] btrfs-progs: fs show should handle if subvol(s) mounted > > [PATCH v2] btrfs: add framework to read fs info from btrfs-control Anand's updated patches are a better way to solve this, but until we get the kernel feature in, I'm about to commit this patch: From 5aff090a3951e7d787b32bb5c49adfec65091385 Mon Sep 17 00:00:00 2001 From: Chris Mason Date: Mon, 18 Nov 2013 14:18:08 -0500 Subject: [PATCH] btrfs filesystem show: skip duplicate fsids If a given filesystem is mounted more than once, btrfs fi show will print dups. This adds a quick and dirty hash table of fsids it has already printed and makes sure we don't print any fsid more than once. Signed-off-by: Chris Mason --- cmds-filesystem.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/cmds-filesystem.c b/cmds-filesystem.c index aa361d6..1c1926b 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -38,6 +38,74 @@ #include "commands.h" #include "list_sort.h" + +/* + * for btrfs fi show, we maintain a hash of fsids we've already printed. + * This way we don't print dups if a given FS is mounted more than once. + */ +#define SEEN_FSID_HASH_SIZE 256 + +struct seen_fsid { + u8 fsid[BTRFS_FSID_SIZE]; + struct seen_fsid *next; +}; + +static struct seen_fsid *seen_fsid_hash[SEEN_FSID_HASH_SIZE] = {NULL,}; + +static int add_seen_fsid(u8 *fsid) +{ + u8 hash = fsid[0]; + int slot = hash % SEEN_FSID_HASH_SIZE; + struct seen_fsid *seen = seen_fsid_hash[slot]; + struct seen_fsid *alloc; + + if (!seen) + goto insert; + + while (1) { + if (memcmp(seen->fsid, fsid, BTRFS_FSID_SIZE) == 0) + return -EEXIST; + + if (!seen->next) + break; + + seen = seen->next; + } + +insert: + + alloc = malloc(sizeof(*alloc)); + if (!alloc) + return -ENOMEM; + + alloc->next = NULL; + memcpy(alloc->fsid, fsid, BTRFS_FSID_SIZE); + + if (seen) + seen->next = alloc; + else + seen_fsid_hash[slot] = alloc; + + return 0; +} + +static void free_seen_fsid(void) +{ + int slot; + struct seen_fsid *seen; + struct seen_fsid *next; + + for (slot = 0; slot < SEEN_FSID_HASH_SIZE; slot++) { + seen = seen_fsid_hash[slot]; + while (seen) { + next = seen->next; + free(seen); + seen = next; + } + seen_fsid_hash[slot] = NULL; + } +} + static const char * const filesystem_cmd_group_usage[] = { "btrfs filesystem [] []", NULL @@ -224,6 +292,9 @@ static void print_one_uuid(struct btrfs_fs_devices *fs_devices) u64 devs_found = 0; u64 total; + if (add_seen_fsid(fs_devices->fsid)) + return; + uuid_unparse(fs_devices->fsid, uuidbuf); device = list_entry(fs_devices->devices.next, struct btrfs_device, dev_list); @@ -274,6 +345,13 @@ static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info, int i; char uuidbuf[37]; struct btrfs_ioctl_dev_info_args *tmp_dev_info; + int ret; + + ret = add_seen_fsid(fs_info->fsid); + if (ret == -EEXIST) + return 0; + else if (ret) + return ret; uuid_unparse(fs_info->fsid, uuidbuf); printf("Label: %s uuid: %s\n", @@ -478,6 +556,7 @@ devs_only: out: printf("%s\n", BTRFS_BUILD_VERSION); + free_seen_fsid(); return 0; }