From patchwork Fri Feb 5 01:22:58 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8229711 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 5C0EFBEEE5 for ; Fri, 5 Feb 2016 01:25:43 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 194FF2038E for ; Fri, 5 Feb 2016 01:25:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D4BBA20373 for ; Fri, 5 Feb 2016 01:25:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751594AbcBEBZZ (ORCPT ); Thu, 4 Feb 2016 20:25:25 -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 S1751550AbcBEBZO (ORCPT ); Thu, 4 Feb 2016 20:25:14 -0500 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="314610" 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 96AFB4056414 for ; Fri, 5 Feb 2016 09:25:04 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH v5 2/8] btrfs-progs: dedup: Add enable command for dedup command group Date: Fri, 5 Feb 2016 09:22:58 +0800 Message-Id: <1454635384-10106-3-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: 96AFB4056414.ABCFE 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 Add enable subcommand for dedup commmand group. Signed-off-by: Qu Wenruo --- Documentation/btrfs-dedup.asciidoc | 81 +++++++++++++++++++++- cmds-dedup.c | 138 +++++++++++++++++++++++++++++++++++++ ioctl.h | 2 + 3 files changed, 220 insertions(+), 1 deletion(-) diff --git a/Documentation/btrfs-dedup.asciidoc b/Documentation/btrfs-dedup.asciidoc index 917977b..aa378df 100644 --- a/Documentation/btrfs-dedup.asciidoc +++ b/Documentation/btrfs-dedup.asciidoc @@ -21,7 +21,86 @@ use with caution. SUBCOMMAND ---------- -Nothing yet +*enable* [options] :: +Enable in-band de-duplication for a filesystem. ++ +`Options` ++ +-s|--storage-backend :::: +Specify de-duplication hash storage backend. +Supported backends are 'ondisk' and 'inmemory'. +If not specified, default value is 'inmemory'. ++ +Refer to *BACKENDS* sector for more information. + +-b|--blocksize :::: +Specify dedup block size. +Supported values are power of 2 from '16K' to '8M'. +Default value is '128K'. ++ +Refer to *BLOCKSIZE* sector for more information. + +-a|--hash-algorithm :::: +Specify hash algorithm. +Only 'sha256' is supported yet. + +-l|--limit-hash :::: +Specify maximum number of hashes stored in memory. +Only works for 'inmemory' backend. +Conflicts with '-m' option. +Default value is '32K' if using 'inmemory' backend. + +-m|--limit-memory :::: +Specify maximum memory used for hashes. +Only works for 'inmemory' backend. +Conflicts with '-l' option. +No default value. + +BACKENDS +-------- +Btrfs in-band de-duplication support two different backends with their own +features. + +In-memory backend:: +This backend can be used on old btrfs(without '-O dedup' mkfs option) with +newer kernel(4.6+). +When used on old btrfs, this backend must be re-enabled after umount. ++ +Designed for speed, in-memory backend will keep all dedup hash in memory. +And only keeps a limit of number of hash in memory. +Hashes over the limit will be dropped following Last-Recent-Use behavior. +So this backend has a consistent overhead for given limit but can't ensure +any all duplicated blocks will be de-duplicated. ++ +After umount and mount, in-memory backend need to refill its hash table. + +On-disk backend:: +This backend needs '-O dedup' mkfs option to enable. ++ +Designed for de-duplication rate, on-disk backend will keep dedup hash on disk. +This behavior may cause extra disk IO for de-duplication under high memory +pressure, but will have a much higher dedup rate. ++ +After umount and mount, on-disk backend still has its hash on disk, no need to +refill its dedup hash table. + +BLOCKSIZE +--------- +Block in-band de-duplication is done at block size unit. +Any data smaller than dedup block size won't go through the dedup backends. + +Smaller block size will cause more fragments and lower performance, but a +higher dedup rate. + +Larger block size will cause less fragments and higher performance, but a +lower dedup rate. + +In-band de-duplication rate is highly related to the workload pattern. +So it's highly recommended to align de-duplication blocksize to the workload +blocksize to make full use of de-duplication. + +And blocksize larger than 128K will cause compression unavailable, as +compression only support maximum extent size of 128K. EXIT STATUS ----------- diff --git a/cmds-dedup.c b/cmds-dedup.c index 800df34..1da416f 100644 --- a/cmds-dedup.c +++ b/cmds-dedup.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "ctree.h" #include "ioctl.h" @@ -36,8 +37,145 @@ static const char * const dedup_cmd_group_usage[] = { static const char dedup_cmd_group_info[] = "manage inband(write time) de-duplication"; +static const char * const cmd_dedup_enable_usage[] = { + "btrfs dedup enable [options] ", + "Enable in-band(write time) de-duplication of a btrfs.", + "", + "-s|--storage-backend ", + " specify dedup hash storage backend", + " supported backend: 'ondisk', 'inmemory'", + " inmemory is the default backend", + "-b|--blocksize ", + " specify dedup block size", + " default value is 128K", + "-a|--hash-algorithm ", + " specify hash algorithm", + " only 'sha256' is supported yet", + "-l|--limit-hash ", + " specify maximum number of hashes stored in memory", + " only for 'inmemory' backend", + " default value is 32K if using 'inmemory' backend", + "-m|--limit-mem ", + " specify maximum memory used for hashes", + " only for 'inmemory' backend", + " only one of '-m' and '-l' is allowed", + NULL +}; + +static int cmd_dedup_enable(int argc, char **argv) +{ + int ret; + int fd; + char *path; + u64 blocksize = BTRFS_DEDUP_BLOCKSIZE_DEFAULT; + u16 hash_type = BTRFS_DEDUP_HASH_SHA256; + u16 backend = BTRFS_DEDUP_BACKEND_INMEMORY; + u64 limit_nr = 0; + u64 limit_mem = 0; + struct btrfs_ioctl_dedup_args dargs; + DIR *dirstream; + + while (1) { + int c; + static const struct option long_options[] = { + { "storage-backend", required_argument, NULL, 's'}, + { "blocksize", required_argument, NULL, 'b'}, + { "hash-algorithm", required_argument, NULL, 'a'}, + { "limit-hash", required_argument, NULL, 'l'}, + { "limit-memory", required_argument, NULL, 'm'}, + { NULL, 0, NULL, 0} + }; + + c = getopt_long(argc, argv, "s:b:a:l:m:", long_options, NULL); + if (c < 0) + break; + switch (c) { + case 's': + if (!strcmp("ondisk", optarg)) + backend = BTRFS_DEDUP_BACKEND_ONDISK; + else if (!strcmp("inmemory", optarg)) + backend = BTRFS_DEDUP_BACKEND_INMEMORY; + else { + error("unsupported dedup backend: %s", optarg); + exit(1); + } + break; + case 'b': + blocksize = parse_size(optarg); + break; + case 'a': + if (strcmp("sha256", optarg)) { + error("unsupported dedup hash algorithm: %s", + optarg); + return 1; + } + break; + case 'l': + limit_nr = parse_size(optarg); + break; + case 'm': + limit_mem = parse_size(optarg); + break; + } + } + + path = argv[optind]; + if (check_argc_exact(argc - optind, 1)) + usage(cmd_dedup_enable_usage); + + /* Validation check */ + if (!is_power_of_2(blocksize) || + blocksize > BTRFS_DEDUP_BLOCKSIZE_MAX || + blocksize < BTRFS_DEDUP_BLOCKSIZE_MIN) { + error("invalid dedup blocksize: %llu, not in range [%u,%u] or power of 2", + blocksize, BTRFS_DEDUP_BLOCKSIZE_MIN, + BTRFS_DEDUP_BLOCKSIZE_MAX); + return 1; + } + if ((limit_nr || limit_mem) && backend == BTRFS_DEDUP_BACKEND_ONDISK) { + error("limit is only valid for 'inmemory' backend"); + return 1; + } + if (limit_nr && limit_mem) { + error("limit-memory and limit-hash can't be given at the same time"); + return 1; + } + + fd = open_file_or_dir(path, &dirstream); + if (fd < 0) { + error("failed to open file or directory: %s", path); + return 1; + } + memset(&dargs, 0, sizeof(dargs)); + dargs.cmd = BTRFS_DEDUP_CTL_ENABLE; + dargs.blocksize = blocksize; + dargs.hash_type = hash_type; + dargs.limit_nr = limit_nr; + dargs.limit_mem = limit_mem; + dargs.backend = backend; + + ret = ioctl(fd, BTRFS_IOC_DEDUP_CTL, &dargs); + if (ret < 0) { + char *error_message = NULL; + /* Special case, provide better error message */ + if (backend == BTRFS_DEDUP_BACKEND_ONDISK && + errno == -EOPNOTSUPP) + error_message = "Need 'dedup' mkfs feature to enable ondisk backend"; + error("failed to enable inband deduplication: %s", + error_message ? error_message : strerror(errno)); + ret = 1; + goto out; + } + ret = 0; + +out: + close_file_or_dir(fd, dirstream); + return ret; +} + const struct cmd_group dedup_cmd_group = { dedup_cmd_group_usage, dedup_cmd_group_info, { + { "enable", cmd_dedup_enable, cmd_dedup_enable_usage, NULL, 0}, NULL_CMD_STRUCT } }; diff --git a/ioctl.h b/ioctl.h index b19d4e6..4de92d5 100644 --- a/ioctl.h +++ b/ioctl.h @@ -724,6 +724,8 @@ static inline char *btrfs_err_str(enum btrfs_err_code err_code) struct btrfs_ioctl_dev_replace_args) #define BTRFS_IOC_FILE_EXTENT_SAME _IOWR(BTRFS_IOCTL_MAGIC, 54, \ struct btrfs_ioctl_same_args) +#define BTRFS_IOC_DEDUP_CTL _IOWR(BTRFS_IOCTL_MAGIC, 55, \ + struct btrfs_ioctl_dedup_args) #define BTRFS_IOC_GET_FEATURES _IOR(BTRFS_IOCTL_MAGIC, 57, \ struct btrfs_ioctl_feature_flags) #define BTRFS_IOC_SET_FEATURES _IOW(BTRFS_IOCTL_MAGIC, 57, \