From patchwork Fri Dec 16 01:21:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 9477159 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 D134860825 for ; Fri, 16 Dec 2016 01:22:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CB6D128780 for ; Fri, 16 Dec 2016 01:22:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE398288A7; Fri, 16 Dec 2016 01:22:28 +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.9 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY 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 24EE328780 for ; Fri, 16 Dec 2016 01:22:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932820AbcLPBWQ (ORCPT ); Thu, 15 Dec 2016 20:22:16 -0500 Received: from aserp1040.oracle.com ([141.146.126.69]:25008 "EHLO aserp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754581AbcLPBWP (ORCPT ); Thu, 15 Dec 2016 20:22:15 -0500 Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by aserp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id uBG1Ln3o010767 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Dec 2016 01:21:50 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by userv0021.oracle.com (8.14.4/8.14.4) with ESMTP id uBG1LneW014520 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 16 Dec 2016 01:21:49 GMT Received: from abhmp0001.oracle.com (abhmp0001.oracle.com [141.146.116.7]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id uBG1LnDV009163; Fri, 16 Dec 2016 01:21:49 GMT Received: from localhost (/24.21.211.40) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 15 Dec 2016 17:21:48 -0800 Date: Thu, 15 Dec 2016 17:21:47 -0800 From: "Darrick J. Wong" To: mfasheh@versity.com Cc: xfs , Christoph Hellwig , linux-btrfs Subject: [PATCH v2] duperemove: test presence of dedupe ioctl Message-ID: <20161216012147.GH5357@birch.djwong.org> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Source-IP: userv0021.oracle.com [156.151.31.71] 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 Since a zero-length dedupe operation is guaranteed to succeed, use that to test whether or not this filesystem supports dedupe. Signed-off-by: Darrick J. Wong Reviewed-by: Christoph Hellwig --- v2: Don't declare a new type; just declare the struct on the stack. --- file_scan.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/file_scan.c b/file_scan.c index 617f166..2708bfe 100644 --- a/file_scan.c +++ b/file_scan.c @@ -45,11 +45,7 @@ #include "file_scan.h" #include "dbfile.h" #include "util.h" - -/* This is not in linux/magic.h */ -#ifndef XFS_SB_MAGIC -#define XFS_SB_MAGIC 0x58465342 /* 'XFSB' */ -#endif +#include "btrfs-ioctl.h" static char path[PATH_MAX] = { 0, }; static char *pathp = path; @@ -189,6 +185,37 @@ static int walk_dir(const char *name) return ret; } +/* + * A zero-length dedupe between two files should always succeed, + * so we can use this to test the presence of dedupe functionality. + */ +static bool check_ioctl_works(int fd) +{ + struct { + struct btrfs_ioctl_same_args args; + struct btrfs_ioctl_same_extent_info info; + } sa = {0}; + struct stat sb; + static int cached = -1; + int ret; + + if (cached >= 0) + return cached != 0; + + ret = fstat(fd, &sb); + if (ret) + return false; + + sa.args.dest_count = 1; + sa.args.length = 0; + sa.info.fd = fd; + sa.info.logical_offset = 0; + errno = 0; + ret = btrfs_extent_same(fd, &sa.args); + cached = !ret && !errno && !sa.info.status; + return cached; +} + static int __add_file(const char *name, struct stat *st, struct filerec **ret_file) { @@ -235,12 +262,10 @@ static int __add_file(const char *name, struct stat *st, goto out; } - if (run_dedupe && - ((fs.f_type != BTRFS_SUPER_MAGIC && - fs.f_type != XFS_SB_MAGIC))) { + if (run_dedupe && !check_ioctl_works(fd)) { close(fd); - fprintf(stderr, "\"%s\": Can only dedupe files on btrfs or xfs " - "(experimental)\n", name); + fprintf(stderr, "\"%s\": dedupe ioctl not supported on this " + "filesystem.\n", name); return ENOSYS; }