From patchwork Mon Nov 18 19:33:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chris Mason X-Patchwork-Id: 3198691 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 A4E1A9F26C for ; Mon, 18 Nov 2013 19:34:47 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id F28C620131 for ; Mon, 18 Nov 2013 19:34:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D12E120502 for ; Mon, 18 Nov 2013 19:34:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751887Ab3KRTeI (ORCPT ); Mon, 18 Nov 2013 14:34:08 -0500 Received: from dkim2.fusionio.com ([66.114.96.54]:44898 "EHLO dkim2.fusionio.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751939Ab3KRTeG convert rfc822-to-8bit (ORCPT ); Mon, 18 Nov 2013 14:34:06 -0500 Received: from mx2.fusionio.com (unknown [10.101.1.160]) by dkim2.fusionio.com (Postfix) with ESMTP id 81A639A06AE for ; Mon, 18 Nov 2013 12:34:03 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=fusionio.com; s=default; t=1384803243; bh=2gwspSljd5gKD68wERQmFd2f8vMO9d9PLhnVrhgM45c=; h=To:From:Subject:Date; b=Hdh3dBBY4lmRyt8cXCT1zr1sT59LD/lQ+lG5lMdAYn1HirY/lOVrisu9XJMOUJz/E FLS4qBar+qCeqqGgDwHHmnt7PStspHlD8OKziomlVhLHiaFKLACg13y1nLFebFNJav X8h7IygZLnuYRrXdnua8gIuceTRbsbC3U35wvTgI= X-ASG-Debug-ID: 1384803242-0421b529b8083e0001-6jHSXT Received: from CAS1.int.fusionio.com (cas1.int.fusionio.com [10.101.1.40]) by mx2.fusionio.com with ESMTP id xUNSyLrUEXsqNXPK (version=TLSv1 cipher=AES128-SHA bits=128 verify=NO); Mon, 18 Nov 2013 12:34:02 -0700 (MST) X-Barracuda-Envelope-From: clmason@fusionio.com Received: from localhost (10.101.1.160) by mail.fusionio.com (10.101.1.40) with Microsoft SMTP Server (TLS) id 14.3.158.1; Mon, 18 Nov 2013 12:34:02 -0700 MIME-Version: 1.0 To: linux-btrfs , Eric Sandeen , Zach Brown Message-ID: <20131118193348.5312.89618@localhost.localdomain> From: Chris Mason User-Agent: alot/0.3.4 Subject: [PATCH] btrfs filesystem show: skip duplicate fsids Date: Mon, 18 Nov 2013 14:33:48 -0500 X-ASG-Orig-Subj: [PATCH] btrfs filesystem show: skip duplicate fsids X-Originating-IP: [10.101.1.160] X-Barracuda-Connect: cas1.int.fusionio.com[10.101.1.40] X-Barracuda-Start-Time: 1384803242 X-Barracuda-Encrypted: AES128-SHA X-Barracuda-URL: http://10.101.1.181:8000/cgi-mod/mark.cgi X-Virus-Scanned: by bsmtpd at fusionio.com X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.00 X-Barracuda-Spam-Status: No, SCORE=0.00 using global 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 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. Anand has other patches that solve this by pulling information in from new kernel interfaces. For now, we'll do the duplicate searching. 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; }