From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Cc: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
Subject: [PATCH v9 02/19] btrfs: dedupe: Introduce function to initialize dedupe info
Date: Wed, 30 Mar 2016 15:55:57 +0800 [thread overview]
Message-ID: <1459324574-28063-3-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1459324574-28063-1-git-send-email-quwenruo@cn.fujitsu.com>
From: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
Add generic function to initialize dedupe info.
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Signed-off-by: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
---
fs/btrfs/Makefile | 2 +-
fs/btrfs/dedupe.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/dedupe.h | 16 +++++-
3 files changed, 169 insertions(+), 3 deletions(-)
create mode 100644 fs/btrfs/dedupe.c
diff --git a/fs/btrfs/Makefile b/fs/btrfs/Makefile
index 128ce17..1b8c627 100644
--- a/fs/btrfs/Makefile
+++ b/fs/btrfs/Makefile
@@ -9,7 +9,7 @@ btrfs-y += super.o ctree.o extent-tree.o print-tree.o root-tree.o dir-item.o \
export.o tree-log.o free-space-cache.o zlib.o lzo.o \
compression.o delayed-ref.o relocation.o delayed-inode.o scrub.o \
reada.o backref.o ulist.o qgroup.o send.o dev-replace.o raid56.o \
- uuid-tree.o props.o hash.o free-space-tree.o
+ uuid-tree.o props.o hash.o free-space-tree.o dedupe.o
btrfs-$(CONFIG_BTRFS_FS_POSIX_ACL) += acl.o
btrfs-$(CONFIG_BTRFS_FS_CHECK_INTEGRITY) += check-integrity.o
diff --git a/fs/btrfs/dedupe.c b/fs/btrfs/dedupe.c
new file mode 100644
index 0000000..2211588
--- /dev/null
+++ b/fs/btrfs/dedupe.c
@@ -0,0 +1,154 @@
+/*
+ * Copyright (C) 2016 Fujitsu. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public
+ * License v2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 021110-1307, USA.
+ */
+#include "ctree.h"
+#include "dedupe.h"
+#include "btrfs_inode.h"
+#include "transaction.h"
+#include "delayed-ref.h"
+
+struct inmem_hash {
+ struct rb_node hash_node;
+ struct rb_node bytenr_node;
+ struct list_head lru_list;
+
+ u64 bytenr;
+ u32 num_bytes;
+
+ u8 hash[];
+};
+
+static int init_dedupe_info(struct btrfs_dedupe_info **ret_info, u16 type,
+ u16 backend, u64 blocksize, u64 limit)
+{
+ struct btrfs_dedupe_info *dedupe_info;
+
+ dedupe_info = kzalloc(sizeof(*dedupe_info), GFP_NOFS);
+ if (!dedupe_info)
+ return -ENOMEM;
+
+ dedupe_info->hash_type = type;
+ dedupe_info->backend = backend;
+ dedupe_info->blocksize = blocksize;
+ dedupe_info->limit_nr = limit;
+
+ /* only support SHA256 yet */
+ dedupe_info->dedupe_driver = crypto_alloc_shash("sha256", 0, 0);
+ if (IS_ERR(dedupe_info->dedupe_driver)) {
+ int ret;
+
+ ret = PTR_ERR(dedupe_info->dedupe_driver);
+ kfree(dedupe_info);
+ return ret;
+ }
+
+ dedupe_info->hash_root = RB_ROOT;
+ dedupe_info->bytenr_root = RB_ROOT;
+ dedupe_info->current_nr = 0;
+ INIT_LIST_HEAD(&dedupe_info->lru_list);
+ mutex_init(&dedupe_info->lock);
+
+ *ret_info = dedupe_info;
+ return 0;
+}
+
+static int check_dedupe_parameter(struct btrfs_fs_info *fs_info, u16 hash_type,
+ u16 backend, u64 blocksize, u64 limit_nr,
+ u64 limit_mem, u64 *ret_limit)
+{
+ if (blocksize > BTRFS_DEDUPE_BLOCKSIZE_MAX ||
+ blocksize < BTRFS_DEDUPE_BLOCKSIZE_MIN ||
+ blocksize < fs_info->tree_root->sectorsize ||
+ !is_power_of_2(blocksize))
+ return -EINVAL;
+ /*
+ * For new backend and hash type, we return special return code
+ * as they can be easily expended.
+ */
+ if (hash_type >= ARRAY_SIZE(btrfs_dedupe_sizes))
+ return -EOPNOTSUPP;
+ if (backend >= BTRFS_DEDUPE_BACKEND_COUNT)
+ return -EOPNOTSUPP;
+
+ /* Backend specific check */
+ if (backend == BTRFS_DEDUPE_BACKEND_INMEMORY) {
+ if (!limit_nr && !limit_mem)
+ *ret_limit = BTRFS_DEDUPE_LIMIT_NR_DEFAULT;
+ else {
+ u64 tmp = (u64)-1;
+
+ if (limit_mem) {
+ tmp = limit_mem / (sizeof(struct inmem_hash) +
+ btrfs_dedupe_hash_size(hash_type));
+ /* Too small limit_mem to fill a hash item */
+ if (!tmp)
+ return -EINVAL;
+ }
+ if (!limit_nr)
+ limit_nr = (u64)-1;
+
+ *ret_limit = min(tmp, limit_nr);
+ }
+ }
+ if (backend == BTRFS_DEDUPE_BACKEND_ONDISK)
+ *ret_limit = 0;
+ return 0;
+}
+
+int btrfs_dedupe_enable(struct btrfs_fs_info *fs_info, u16 type, u16 backend,
+ u64 blocksize, u64 limit_nr, u64 limit_mem)
+{
+ struct btrfs_dedupe_info *dedupe_info;
+ u64 limit = 0;
+ int ret = 0;
+
+ /* only one limit is accepted for enable*/
+ if (limit_nr && limit_mem)
+ return -EINVAL;
+
+ ret = check_dedupe_parameter(fs_info, type, backend, blocksize,
+ limit_nr, limit_mem, &limit);
+ if (ret < 0)
+ return ret;
+
+ dedupe_info = fs_info->dedupe_info;
+ if (dedupe_info) {
+ /* Check if we are re-enable for different dedupe config */
+ if (dedupe_info->blocksize != blocksize ||
+ dedupe_info->hash_type != type ||
+ dedupe_info->backend != backend) {
+ btrfs_dedupe_disable(fs_info);
+ goto enable;
+ }
+
+ /* On-fly limit change is OK */
+ mutex_lock(&dedupe_info->lock);
+ fs_info->dedupe_info->limit_nr = limit;
+ mutex_unlock(&dedupe_info->lock);
+ return 0;
+ }
+
+enable:
+ ret = init_dedupe_info(&dedupe_info, type, backend, blocksize, limit);
+ if (ret < 0)
+ return ret;
+ fs_info->dedupe_info = dedupe_info;
+ /* We must ensure dedupe_enabled is modified after dedupe_info */
+ smp_wmb();
+ fs_info->dedupe_enabled = 1;
+ return ret;
+}
diff --git a/fs/btrfs/dedupe.h b/fs/btrfs/dedupe.h
index 40f4808..e5d0d34 100644
--- a/fs/btrfs/dedupe.h
+++ b/fs/btrfs/dedupe.h
@@ -39,6 +39,9 @@
#define BTRFS_DEDUPE_BLOCKSIZE_MIN (16 * 1024)
#define BTRFS_DEDUPE_BLOCKSIZE_DEFAULT (128 * 1024)
+/* Default dedupe limit on number of hash */
+#define BTRFS_DEDUPE_LIMIT_NR_DEFAULT (32 * 1024)
+
/* Hash algorithm, only support SHA256 yet */
#define BTRFS_DEDUPE_HASH_SHA256 0
@@ -81,8 +84,17 @@ static inline int btrfs_dedupe_hash_hit(struct btrfs_dedupe_hash *hash)
return (hash && hash->bytenr);
}
-int btrfs_dedupe_hash_size(u16 type);
-struct btrfs_dedupe_hash *btrfs_dedupe_alloc_hash(u16 type);
+static inline int btrfs_dedupe_hash_size(u16 type)
+{
+ if (WARN_ON(type >= ARRAY_SIZE(btrfs_dedupe_sizes)))
+ return -EINVAL;
+ return sizeof(struct btrfs_dedupe_hash) + btrfs_dedupe_sizes[type];
+}
+
+static inline struct btrfs_dedupe_hash *btrfs_dedupe_alloc_hash(u16 type)
+{
+ return kzalloc(btrfs_dedupe_hash_size(type), GFP_NOFS);
+}
/*
* Initial inband dedupe info
--
2.7.4
next prev parent reply other threads:[~2016-03-30 8:05 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-03-30 7:55 [PATCH v9 00/19] Btrfs dedupe framework Qu Wenruo
2016-03-30 7:55 ` [PATCH v9 01/19] btrfs: dedupe: Introduce dedupe framework and its header Qu Wenruo
2016-03-30 7:55 ` Qu Wenruo [this message]
2016-03-30 10:10 ` [PATCH v9 02/19] btrfs: dedupe: Introduce function to initialize dedupe info kbuild test robot
2016-03-30 7:55 ` [PATCH v9 03/19] btrfs: dedupe: Introduce function to add hash into in-memory tree Qu Wenruo
2016-03-30 7:55 ` [PATCH v9 04/19] btrfs: dedupe: Introduce function to remove hash from " Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 05/19] btrfs: delayed-ref: Add support for increasing data ref under spinlock Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 06/19] btrfs: dedupe: Introduce function to search for an existing hash Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 07/19] btrfs: dedupe: Implement btrfs_dedupe_calc_hash interface Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 08/19] btrfs: ordered-extent: Add support for dedupe Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 09/19] btrfs: dedupe: Inband in-memory only de-duplication implement Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 10/19] btrfs: dedupe: Add ioctl for inband dedupelication Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 11/19] btrfs: dedupe: add an inode nodedupe flag Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 12/19] btrfs: dedupe: add a property handler for online dedupe Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 13/19] btrfs: dedupe: add per-file online dedupe control Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 14/19] btrfs: dedupe: Add basic tree structure for on-disk dedupe method Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 15/19] btrfs: dedupe: Introduce interfaces to resume and cleanup dedupe info Qu Wenruo
2016-03-30 9:45 ` kbuild test robot
2016-03-30 7:56 ` [PATCH v9 16/19] btrfs: dedupe: Add support for on-disk hash search Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 17/19] btrfs: dedupe: Add support to delete hash for on-disk backend Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 18/19] btrfs: dedupe: Add support for adding " Qu Wenruo
2016-03-30 7:56 ` [PATCH v9 19/19] btrfs: dedupe: Preparation for compress-dedupe co-work Qu Wenruo
2016-03-31 16:12 ` [PATCH v9 00/19] Btrfs dedupe framework David Sterba
2016-04-01 0:26 ` Qu Wenruo
2016-04-01 16:41 ` David Sterba
2016-04-01 2:01 ` Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1459324574-28063-3-git-send-email-quwenruo@cn.fujitsu.com \
--to=quwenruo@cn.fujitsu.com \
--cc=linux-btrfs@vger.kernel.org \
--cc=wangxg.fnst@cn.fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).