From patchwork Thu Jan 14 05:57:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8029681 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 356419FE72 for ; Thu, 14 Jan 2016 06:00:19 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 41A30204A9 for ; Thu, 14 Jan 2016 06:00:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3C0A7204A2 for ; Thu, 14 Jan 2016 06:00:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752124AbcANGAO (ORCPT ); Thu, 14 Jan 2016 01:00:14 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:18653 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751878AbcANGAI (ORCPT ); Thu, 14 Jan 2016 01:00:08 -0500 X-IronPort-AV: E=Sophos;i="5.20,346,1444665600"; d="scan'208";a="2575584" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 14 Jan 2016 14:00:01 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 81E8E41896CA for ; Thu, 14 Jan 2016 13:59:39 +0800 (CST) Received: from localhost.localdomain (10.167.226.34) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.181.6; Thu, 14 Jan 2016 13:59:38 +0800 From: Qu Wenruo To: CC: Wang Xiaoguang Subject: [PATCH v4 06/18] btrfs: dedup: Introduce function to search for an existing hash Date: Thu, 14 Jan 2016 13:57:22 +0800 Message-ID: <1452751054-2365-7-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1452751054-2365-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1452751054-2365-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.34] X-yoursite-MailScanner-ID: 81E8E41896CA.AF6CD X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: quwenruo@cn.fujitsu.com X-Spam-Status: No, score=-6.9 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 Introduce static function inmem_search() to handle the job for in-memory hash tree. The trick is, we must ensure the delayed ref head is not being run at the time we search the for the hash. With inmem_search(), we can implement the btrfs_dedup_search() interface. Signed-off-by: Qu Wenruo Signed-off-by: Wang Xiaoguang --- fs/btrfs/dedup.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/fs/btrfs/dedup.c b/fs/btrfs/dedup.c index 39391b8..d6ee576 100644 --- a/fs/btrfs/dedup.c +++ b/fs/btrfs/dedup.c @@ -326,3 +326,142 @@ int btrfs_dedup_disable(struct btrfs_fs_info *fs_info) inmem_destroy(fs_info); return 0; } + +/* + * Caller must ensure the corresponding ref head is not being run. + */ +static struct inmem_hash * +inmem_search_hash(struct btrfs_dedup_info *dedup_info, u8 *hash) +{ + struct rb_node **p = &dedup_info->hash_root.rb_node; + struct rb_node *parent = NULL; + struct inmem_hash *entry = NULL; + u16 hash_type = dedup_info->hash_type; + int hash_len = btrfs_dedup_sizes[hash_type]; + + while (*p) { + parent = *p; + entry = rb_entry(parent, struct inmem_hash, hash_node); + + if (memcmp(hash, entry->hash, hash_len) < 0) { + p = &(*p)->rb_left; + } else if (memcmp(hash, entry->hash, hash_len) > 0) { + p = &(*p)->rb_right; + } else { + /* Found, need to re-add it to LRU list head */ + list_del(&entry->lru_list); + list_add(&entry->lru_list, &dedup_info->lru_list); + return entry; + } + } + return NULL; +} + +static int inmem_search(struct inode *inode, u64 file_pos, + struct btrfs_dedup_hash *hash) +{ + int ret; + struct btrfs_root *root = BTRFS_I(inode)->root; + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_trans_handle *trans; + struct btrfs_delayed_ref_root *delayed_refs; + struct btrfs_delayed_ref_head *head; + struct inmem_hash *found_hash; + struct btrfs_dedup_info *dedup_info = fs_info->dedup_info; + u64 bytenr; + u32 num_bytes; + + trans = btrfs_join_transaction(root); + if (IS_ERR(trans)) + return PTR_ERR(trans); + +again: + mutex_lock(&dedup_info->lock); + found_hash = inmem_search_hash(dedup_info, hash->hash); + /* If we don't find a duplicated extent, just return. */ + if (!found_hash) { + ret = 0; + goto out; + } + bytenr = found_hash->bytenr; + num_bytes = found_hash->num_bytes; + + delayed_refs = &trans->transaction->delayed_refs; + + spin_lock(&delayed_refs->lock); + head = btrfs_find_delayed_ref_head(trans, bytenr); + if (!head) { + /* + * We can safely insert a new delayed_ref as long as we + * hold delayed_refs->lock. + * Only need to use atomic inc_extent_ref() + */ + ret = btrfs_inc_extent_ref_atomic(trans, root, bytenr, + num_bytes, 0, root->root_key.objectid, + btrfs_ino(inode), file_pos); + spin_unlock(&delayed_refs->lock); + + if (ret == 0) { + hash->bytenr = bytenr; + hash->num_bytes = num_bytes; + ret = 1; + } + goto out; + } + + /* + * We can't lock ref head with dedup_info->lock hold or we will cause + * ABBA dead lock. + */ + mutex_unlock(&dedup_info->lock); + ret = btrfs_delayed_ref_lock(trans, head); + spin_unlock(&delayed_refs->lock); + if (ret == -EAGAIN) + goto again; + + mutex_lock(&dedup_info->lock); + /* Search again to ensure the hash is still here */ + found_hash = inmem_search_hash(dedup_info, hash->hash); + if (!found_hash) { + ret = 0; + mutex_unlock(&head->mutex); + goto out; + } + hash->bytenr = bytenr; + hash->num_bytes = num_bytes; + + /* + * Increase the extent ref right now, to avoid delayed ref run + * Or we may increase ref on non-exist extent. + */ + btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, + root->root_key.objectid, + btrfs_ino(inode), file_pos); + mutex_unlock(&head->mutex); +out: + mutex_unlock(&dedup_info->lock); + btrfs_end_transaction(trans, root); + + return ret; +} + +int btrfs_dedup_search(struct inode *inode, u64 file_pos, + struct btrfs_dedup_hash *hash) +{ + struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; + struct btrfs_dedup_info *dedup_info = fs_info->dedup_info; + int ret = 0; + + if (WARN_ON(!dedup_info || !hash)) + return 0; + + if (dedup_info->backend == BTRFS_DEDUP_BACKEND_INMEMORY) + ret = inmem_search(inode, file_pos, hash); + + /* It's possible hash->bytenr/num_bytenr already changed */ + if (ret == 0) { + hash->num_bytes = 0; + hash->bytenr = 0; + } + return ret; +}