From patchwork Fri Jan 6 07:07:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 9500163 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 067BF60413 for ; Fri, 6 Jan 2017 07:08:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E85341FF0B for ; Fri, 6 Jan 2017 07:08:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DD0C928420; Fri, 6 Jan 2017 07:08:15 +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=unavailable 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 9679A1FF0B for ; Fri, 6 Jan 2017 07:08:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756668AbdAFHIM (ORCPT ); Fri, 6 Jan 2017 02:08:12 -0500 Received: from userp1040.oracle.com ([156.151.31.81]:18044 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753592AbdAFHIK (ORCPT ); Fri, 6 Jan 2017 02:08:10 -0500 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v0677cLR002894 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 6 Jan 2017 07:07:39 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.14.4) with ESMTP id v0677cck017248 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 6 Jan 2017 07:07:38 GMT Received: from abhmp0014.oracle.com (abhmp0014.oracle.com [141.146.116.20]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id v0677Ye6002697; Fri, 6 Jan 2017 07:07:35 GMT Received: from localhost (/24.21.211.40) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 05 Jan 2017 23:07:33 -0800 Date: Thu, 5 Jan 2017 23:07:32 -0800 From: "Darrick J. Wong" To: mfasheh@versity.com Cc: Christoph Hellwig , xfs , linux-btrfs Subject: [PATCH v2] duperemove: test presence of dedupe ioctl Message-ID: <20170106070732.GA14020@birch.djwong.org> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Source-IP: aserv0021.oracle.com [141.146.126.233] 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: declare variables on the stack instead of introducing fake types --- 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; }