From patchwork Fri Feb 5 01:23:04 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8229681 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.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 6F785BEEE5 for ; Fri, 5 Feb 2016 01:25:25 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 74726201BB for ; Fri, 5 Feb 2016 01:25:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 772A32017E for ; Fri, 5 Feb 2016 01:25:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751577AbcBEBZS (ORCPT ); Thu, 4 Feb 2016 20:25:18 -0500 Received: from cn.fujitsu.com ([222.73.24.84]:56740 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751561AbcBEBZP (ORCPT ); Thu, 4 Feb 2016 20:25:15 -0500 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208,217";a="314613" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 05 Feb 2016 09:25:10 +0800 Received: from localhost.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id 67F254056418; Fri, 5 Feb 2016 09:25:05 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: Wang Xiaoguang Subject: [PATCH v5 8/8] btrfs-progs: property: add a dedup property Date: Fri, 5 Feb 2016 09:23:04 +0800 Message-Id: <1454635384-10106-9-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1454635384-10106-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1454635384-10106-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: 67F254056418.AA458 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com X-Spam-Status: No, score=-7.4 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wang Xiaoguang Normally if we enable online dedup for a fs, it's filesystem wide de-duplication. With this property, we can explicitly disable data de-duplication for specified files. Signed-off-by: Wang Xiaoguang --- Documentation/btrfs-property.asciidoc | 2 + props.c | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/Documentation/btrfs-property.asciidoc b/Documentation/btrfs-property.asciidoc index 8b9b7f0..2fbecf6 100644 --- a/Documentation/btrfs-property.asciidoc +++ b/Documentation/btrfs-property.asciidoc @@ -44,6 +44,8 @@ label:::: label of device compression:::: compression setting for an inode: lzo, zlib, or "" (empty string) +dedup:::: +online dedup setting for an inode: disable or "" (empty string) *list* [-t ] :: Lists available properties with their descriptions for the given object. diff --git a/props.c b/props.c index 5b74932..f27eb8b 100644 --- a/props.c +++ b/props.c @@ -187,6 +187,77 @@ out: return ret; } +static int prop_dedup(enum prop_object_type type, const char *object, + const char *name, const char *value) +{ + int ret; + ssize_t sret; + int fd = -1; + DIR *dirstream = NULL; + char *buf = NULL; + char *xattr_name = NULL; + int open_flags = value ? O_RDWR : O_RDONLY; + + fd = open_file_or_dir3(object, &dirstream, open_flags); + if (fd == -1) { + ret = -errno; + fprintf(stderr, "ERROR: open %s failed. %s\n", + object, strerror(-ret)); + goto out; + } + + xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1); + if (!xattr_name) { + ret = -ENOMEM; + goto out; + } + memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN); + memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name)); + xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0'; + + if (value) + sret = fsetxattr(fd, xattr_name, value, strlen(value), 0); + else + sret = fgetxattr(fd, xattr_name, NULL, 0); + if (sret < 0) { + ret = -errno; + if (ret != -ENOATTR) + fprintf(stderr, + "ERROR: failed to %s dedup for %s. %s\n", + value ? "set" : "get", object, strerror(-ret)); + else + ret = 0; + goto out; + } + if (!value) { + size_t len = sret; + + buf = malloc(len); + if (!buf) { + ret = -ENOMEM; + goto out; + } + sret = fgetxattr(fd, xattr_name, buf, len); + if (sret < 0) { + ret = -errno; + fprintf(stderr, + "ERROR: failed to get dedup for %s. %s\n", + object, strerror(-ret)); + goto out; + } + fprintf(stdout, "dedup=%.*s\n", (int)len, buf); + } + + ret = 0; +out: + free(xattr_name); + free(buf); + if (fd >= 0) + close_file_or_dir(fd, dirstream); + + return ret; +} + const struct prop_handler prop_handlers[] = { {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol, prop_read_only}, @@ -194,5 +265,7 @@ const struct prop_handler prop_handlers[] = { prop_object_dev | prop_object_root, prop_label}, {"compression", "Set/get compression for a file or directory", 0, prop_object_inode, prop_compression}, + {"dedup", "Set/get dedup for a file or directory", 0, + prop_object_inode, prop_dedup}, {NULL, NULL, 0, 0, NULL} };