From patchwork Fri Dec 9 17:56:45 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: 9468827 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 46E0E60586 for ; Fri, 9 Dec 2016 17:57:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3D44C28680 for ; Fri, 9 Dec 2016 17:57:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 30E5F28683; Fri, 9 Dec 2016 17:57:09 +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 6298D28680 for ; Fri, 9 Dec 2016 17:57:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752657AbcLIR4x (ORCPT ); Fri, 9 Dec 2016 12:56:53 -0500 Received: from userp1040.oracle.com ([156.151.31.81]:36012 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751756AbcLIR4v (ORCPT ); Fri, 9 Dec 2016 12:56:51 -0500 Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id uB9HuntR026213 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 9 Dec 2016 17:56:49 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id uB9HumcZ018481 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 9 Dec 2016 17:56:49 GMT Received: from abhmp0005.oracle.com (abhmp0005.oracle.com [141.146.116.11]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id uB9Hullf026031; Fri, 9 Dec 2016 17:56:47 GMT Received: from localhost (/24.21.211.40) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 09 Dec 2016 09:56:46 -0800 Date: Fri, 9 Dec 2016 09:56:45 -0800 From: "Darrick J. Wong" To: mfasheh@versity.com Cc: xfs , linux-btrfs Subject: [PATCH] duperemove: test presence of dedupe ioctl Message-ID: <20161209175645.GB16813@birch.djwong.org> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-Source-IP: aserv0022.oracle.com [141.146.126.234] 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 --- file_scan.c | 47 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 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..a34453e 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,39 @@ static int walk_dir(const char *name) return ret; } +struct fake_btrfs_ioctl_same_args { + struct btrfs_ioctl_same_args args; + struct btrfs_ioctl_same_extent_info info; +}; + +/* + * 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 fake_btrfs_ioctl_same_args 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 != 0; +} + static int __add_file(const char *name, struct stat *st, struct filerec **ret_file) { @@ -235,12 +264,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; }