All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Cc: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
Subject: [PATCH v10 20/21] btrfs: dedupe: Add support for adding hash for on-disk backend
Date: Fri,  1 Apr 2016 14:35:11 +0800	[thread overview]
Message-ID: <1459492512-31435-21-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1459492512-31435-1-git-send-email-quwenruo@cn.fujitsu.com>

Now on-disk backend can add hash now.

Since all needed on-disk backend functions are added, also allow on-disk
backend to be used, by changing DEDUPE_BACKEND_COUNT from 1(inmemory
only) to 2 (inmemory + ondisk).

Signed-off-by: Wang Xiaoguang <wangxg.fnst@cn.fujitsu.com>
Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 fs/btrfs/dedupe.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/btrfs/dedupe.h |  3 +-
 2 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/dedupe.c b/fs/btrfs/dedupe.c
index 7c5d58a..1f0178e 100644
--- a/fs/btrfs/dedupe.c
+++ b/fs/btrfs/dedupe.c
@@ -437,6 +437,87 @@ out:
 	return 0;
 }
 
+static int ondisk_search_bytenr(struct btrfs_trans_handle *trans,
+				struct btrfs_dedupe_info *dedupe_info,
+				struct btrfs_path *path, u64 bytenr,
+				int prepare_del);
+static int ondisk_search_hash(struct btrfs_dedupe_info *dedupe_info, u8 *hash,
+			      u64 *bytenr_ret, u32 *num_bytes_ret);
+static int ondisk_add(struct btrfs_trans_handle *trans,
+		      struct btrfs_dedupe_info *dedupe_info,
+		      struct btrfs_dedupe_hash *hash)
+{
+	struct btrfs_path *path;
+	struct btrfs_root *dedupe_root = dedupe_info->dedupe_root;
+	struct btrfs_key key;
+	u64 hash_offset;
+	u64 bytenr;
+	u32 num_bytes;
+	int hash_len = btrfs_dedupe_sizes[dedupe_info->hash_type];
+	int ret;
+
+	if (WARN_ON(hash_len <= 8 ||
+	    !IS_ALIGNED(hash->bytenr, dedupe_root->sectorsize)))
+		return -EINVAL;
+
+	path = btrfs_alloc_path();
+	if (!path)
+		return -ENOMEM;
+
+	mutex_lock(&dedupe_info->lock);
+
+	ret = ondisk_search_bytenr(NULL, dedupe_info, path, hash->bytenr, 0);
+	if (ret < 0)
+		goto out;
+	if (ret > 0) {
+		ret = 0;
+		goto out;
+	}
+	btrfs_release_path(path);
+
+	ret = ondisk_search_hash(dedupe_info, hash->hash, &bytenr, &num_bytes);
+	if (ret < 0)
+		goto out;
+	/* Same hash found, don't re-add to save dedupe tree space */
+	if (ret > 0) {
+		ret = 0;
+		goto out;
+	}
+
+	/* Insert hash->bytenr item */
+	memcpy(&key.objectid, hash->hash + hash_len - 8, 8);
+	key.type = BTRFS_DEDUPE_HASH_ITEM_KEY;
+	key.offset = hash->bytenr;
+
+	/* The last 8 bit will not be included into hash */
+	ret = btrfs_insert_empty_item(trans, dedupe_root, path, &key,
+				      hash_len - 8);
+	WARN_ON(ret == -EEXIST);
+	if (ret < 0)
+		goto out;
+	hash_offset = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
+	write_extent_buffer(path->nodes[0], hash->hash,
+			    hash_offset, hash_len - 8);
+	btrfs_mark_buffer_dirty(path->nodes[0]);
+	btrfs_release_path(path);
+
+	/* Then bytenr->hash item */
+	key.objectid = hash->bytenr;
+	key.type = BTRFS_DEDUPE_BYTENR_ITEM_KEY;
+	memcpy(&key.offset, hash->hash + hash_len - 8, 8);
+
+	ret = btrfs_insert_empty_item(trans, dedupe_root, path, &key, 0);
+	WARN_ON(ret == -EEXIST);
+	if (ret < 0)
+		goto out;
+	btrfs_mark_buffer_dirty(path->nodes[0]);
+
+out:
+	mutex_unlock(&dedupe_info->lock);
+	btrfs_free_path(path);
+	return ret;
+}
+
 int btrfs_dedupe_add(struct btrfs_trans_handle *trans,
 		     struct btrfs_fs_info *fs_info,
 		     struct btrfs_dedupe_hash *hash)
@@ -458,6 +539,8 @@ int btrfs_dedupe_add(struct btrfs_trans_handle *trans,
 
 	if (dedupe_info->backend == BTRFS_DEDUPE_BACKEND_INMEMORY)
 		return inmem_add(dedupe_info, hash);
+	if (dedupe_info->backend == BTRFS_DEDUPE_BACKEND_ONDISK)
+		return ondisk_add(trans, dedupe_info, hash);
 	return -EINVAL;
 }
 
diff --git a/fs/btrfs/dedupe.h b/fs/btrfs/dedupe.h
index bfcacd7..1573456 100644
--- a/fs/btrfs/dedupe.h
+++ b/fs/btrfs/dedupe.h
@@ -31,8 +31,7 @@
 #define BTRFS_DEDUPE_BACKEND_INMEMORY		0
 #define BTRFS_DEDUPE_BACKEND_ONDISK		1
 
-/* Only support inmemory yet, so count is still only 1 */
-#define BTRFS_DEDUPE_BACKEND_COUNT		1
+#define BTRFS_DEDUPE_BACKEND_COUNT		2
 
 /* Dedup block size limit and default value */
 #define BTRFS_DEDUPE_BLOCKSIZE_MAX	(8 * 1024 * 1024)
-- 
2.7.4




  parent reply	other threads:[~2016-04-01  6:35 UTC|newest]

Thread overview: 54+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-01  6:34 [PATCH v10 00/21] Btrfs dedupe framework Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 01/21] btrfs: dedupe: Introduce dedupe framework and its header Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 02/21] btrfs: dedupe: Introduce function to initialize dedupe info Qu Wenruo
2016-04-01  9:59   ` kbuild test robot
2016-05-11  0:00     ` Mark Fasheh
2016-05-11  0:21       ` Qu Wenruo
2016-05-11  2:24         ` Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 03/21] btrfs: dedupe: Introduce function to add hash into in-memory tree Qu Wenruo
2016-06-01 19:37   ` Mark Fasheh
2016-06-02  0:49     ` Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 04/21] btrfs: dedupe: Introduce function to remove hash from " Qu Wenruo
2016-06-01 19:40   ` Mark Fasheh
2016-06-02  1:01     ` Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 05/21] btrfs: delayed-ref: Add support for increasing data ref under spinlock Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 06/21] btrfs: dedupe: Introduce function to search for an existing hash Qu Wenruo
2016-04-01  6:34 ` [PATCH v10 07/21] btrfs: dedupe: Implement btrfs_dedupe_calc_hash interface Qu Wenruo
2016-05-17 13:15   ` David Sterba
2016-04-01  6:34 ` [PATCH v10 08/21] btrfs: ordered-extent: Add support for dedupe Qu Wenruo
2016-06-01 22:06   ` Mark Fasheh
2016-06-02  1:08     ` Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 09/21] btrfs: dedupe: Inband in-memory only de-duplication implement Qu Wenruo
2016-06-01 22:08   ` Mark Fasheh
2016-06-02  1:12     ` Qu Wenruo
2016-06-03 14:27       ` Josef Bacik
2016-06-04 10:26         ` Qu Wenruo
2016-06-06 19:54           ` Mark Fasheh
2016-06-07  0:42             ` Qu Wenruo
2016-06-07 16:55               ` Mark Fasheh
2016-06-03 14:43   ` Josef Bacik
2016-06-04 10:28     ` Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 10/21] btrfs: try more times to alloc metadata reserve space Qu Wenruo
2016-05-17 13:20   ` David Sterba
2016-05-18  0:57     ` Qu Wenruo
2016-06-01 22:14     ` Mark Fasheh
2016-04-01  6:35 ` [PATCH v10 11/21] btrfs: dedupe: Add ioctl for inband dedupelication Qu Wenruo
2016-04-27  1:29   ` Qu Wenruo
2016-05-17 13:14     ` David Sterba
2016-05-18  0:54       ` Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 12/21] btrfs: dedupe: add an inode nodedupe flag Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 13/21] btrfs: dedupe: add a property handler for online dedupe Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 14/21] btrfs: dedupe: add per-file online dedupe control Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 15/21] btrfs: relocation: Enhance error handling to avoid BUG_ON Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 16/21] btrfs: dedupe: Add basic tree structure for on-disk dedupe method Qu Wenruo
2016-04-01  6:35 ` [PATCH v10 17/21] btrfs: dedupe: Introduce interfaces to resume and cleanup dedupe info Qu Wenruo
2016-06-03 14:54   ` Josef Bacik
2016-04-01  6:35 ` [PATCH v10 18/21] btrfs: dedupe: Add support for on-disk hash search Qu Wenruo
2016-06-03 14:57   ` Josef Bacik
2016-04-01  6:35 ` [PATCH v10 19/21] btrfs: dedupe: Add support to delete hash for on-disk backend Qu Wenruo
2016-04-01  6:35 ` Qu Wenruo [this message]
2016-06-03 15:03   ` [PATCH v10 20/21] btrfs: dedupe: Add support for adding " Josef Bacik
2016-04-01  6:35 ` [PATCH v10 21/21] btrfs: dedupe: Preparation for compress-dedupe co-work Qu Wenruo
2016-04-01  8:53 ` [PATCH v10 16/21] btrfs: dedupe: Add basic tree structure for on-disk dedupe method Qu Wenruo
2016-06-03 15:20 ` [PATCH v10 00/21] Btrfs dedupe framework Josef Bacik
2016-06-04 10:37   ` 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=1459492512-31435-21-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.