From patchwork Tue Dec 29 08:01:12 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 7928971 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 23AE2BEEE5 for ; Tue, 29 Dec 2015 08:02:46 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 28CCC20221 for ; Tue, 29 Dec 2015 08:02:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 39A112021A for ; Tue, 29 Dec 2015 08:02:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753315AbbL2ICl (ORCPT ); Tue, 29 Dec 2015 03:02:41 -0500 Received: from cn.fujitsu.com ([59.151.112.132]:52918 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753257AbbL2ICT (ORCPT ); Tue, 29 Dec 2015 03:02:19 -0500 X-IronPort-AV: E=Sophos;i="5.20,346,1444665600"; d="scan'208";a="2059353" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 29 Dec 2015 16:01:42 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 6B6A4409256C for ; Tue, 29 Dec 2015 16:01:26 +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; Tue, 29 Dec 2015 16:01:25 +0800 From: Qu Wenruo To: CC: Wang Xiaoguang Subject: [PATCH 03/14] btrfs: dedup: Introduce function to add hash into in-memory tree Date: Tue, 29 Dec 2015 16:01:12 +0800 Message-ID: <1451376083-30474-4-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.6.4 In-Reply-To: <1451376083-30474-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1451376083-30474-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.34] X-yoursite-MailScanner-ID: 6B6A4409256C.AEF95 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_add() to add hash into in-memory tree. And now we can implement the btrfs_dedup_add() interface. Signed-off-by: Wang Xiaoguang --- fs/btrfs/dedup.c | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/fs/btrfs/dedup.c b/fs/btrfs/dedup.c index c1fadff..cafbce1 100644 --- a/fs/btrfs/dedup.c +++ b/fs/btrfs/dedup.c @@ -94,3 +94,119 @@ out: } return ret; } + +static int inmem_insert_hash(struct rb_root *root, + struct btrfs_dedup_hash *hash, int hash_len) +{ + struct rb_node **p = &root->rb_node; + struct rb_node *parent = NULL; + struct btrfs_dedup_hash *entry = NULL; + + while (*p) { + parent = *p; + entry = rb_entry(parent, struct btrfs_dedup_hash, + hash_node); + if (memcmp(hash->hash, entry->hash, hash_len) < 0) + p = &(*p)->rb_left; + else if (memcmp(hash->hash, entry->hash, hash_len) > 0) + p = &(*p)->rb_right; + else + return 1; + } + rb_link_node(&hash->hash_node, parent, p); + rb_insert_color(&hash->hash_node, root); + return 0; +} + +static int inmem_insert_bytenr(struct rb_root *root, + struct btrfs_dedup_hash *hash) +{ + struct rb_node **p = &root->rb_node; + struct rb_node *parent = NULL; + struct btrfs_dedup_hash *entry = NULL; + + while (*p) { + parent = *p; + entry = rb_entry(parent, struct btrfs_dedup_hash, bytenr_node); + if (hash->bytenr < entry->bytenr) + p = &(*p)->rb_left; + else if (hash->bytenr > entry->bytenr) + p = &(*p)->rb_right; + else + return 1; + } + rb_link_node(&hash->bytenr_node, parent, p); + rb_insert_color(&hash->bytenr_node, root); + return 0; +} + +static void __inmem_del(struct btrfs_dedup_info *dedup_info, + struct btrfs_dedup_hash *hash) +{ + list_del(&hash->lru_list); + rb_erase(&hash->hash_node, &dedup_info->hash_root); + rb_erase(&hash->bytenr_node, &dedup_info->bytenr_root); + + if (!WARN_ON(dedup_info->current_nr == 0)) + dedup_info->current_nr--; + + kfree(hash); +} + +/* + * Insert a hash into in-memory dedup tree + * Will remove exceeding last recent use hash. + * + * If the hash mathced with existing one, we won't insert it, to + * save memory + */ +static int inmem_add(struct btrfs_dedup_info *dedup_info, + struct btrfs_dedup_hash *hash) +{ + int ret = 0; + u16 type = dedup_info->hash_type; + + spin_lock(&dedup_info->lock); + ret = inmem_insert_hash(&dedup_info->hash_root, hash, + btrfs_dedup_sizes[type]); + if (ret > 0) { + /* Same hash already in tree, don't insert */ + kfree(hash); + ret = 0; + goto out; + } + + inmem_insert_bytenr(&dedup_info->bytenr_root, hash); + + list_add(&hash->lru_list, &dedup_info->lru_list); + dedup_info->current_nr++; + + /* Remove the last dedup hash if we exceed limit */ + while (dedup_info->current_nr > dedup_info->limit_nr) { + struct btrfs_dedup_hash *last; + + last = list_entry(dedup_info->lru_list.prev, + struct btrfs_dedup_hash, lru_list); + __inmem_del(dedup_info, last); + } +out: + spin_unlock(&dedup_info->lock); + return 0; +} + +int btrfs_dedup_add(struct btrfs_trans_handle *trans, struct btrfs_root *root, + struct btrfs_dedup_hash *hash) +{ + struct btrfs_fs_info *fs_info = root->fs_info; + struct btrfs_dedup_info *dedup_info = fs_info->dedup_info; + + if (!dedup_info || !hash) + return 0; + + if (WARN_ON(hash->bytenr == 0)) + return -EINVAL; + + if (dedup_info->backend == BTRFS_DEDUP_BACKEND_INMEMORY) + return inmem_add(dedup_info, hash); + return -EINVAL; +}