From patchwork Tue Mar 22 01:35:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 8637331 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 68AC39F8A8 for ; Tue, 22 Mar 2016 01:45:40 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5C6892034E for ; Tue, 22 Mar 2016 01:45:39 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 597DE2034B for ; Tue, 22 Mar 2016 01:45:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758089AbcCVBpe (ORCPT ); Mon, 21 Mar 2016 21:45:34 -0400 Received: from cn.fujitsu.com ([222.73.24.84]:51827 "EHLO song.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-OK) by vger.kernel.org with ESMTP id S1751929AbcCVBpc (ORCPT ); Mon, 21 Mar 2016 21:45:32 -0400 X-IronPort-AV: E=Sophos;i="5.20,367,1444665600"; d="scan'208";a="405212" Received: from unknown (HELO cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 22 Mar 2016 09:45:26 +0800 Received: from localhost.localdomain (unknown [10.167.226.34]) by cn.fujitsu.com (Postfix) with ESMTP id ADEBD4056411; Tue, 22 Mar 2016 09:37:55 +0800 (CST) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: Wang Xiaoguang Subject: [PATCH v8 13/27] btrfs: dedupe: Add support to delete hash for on-disk backend Date: Tue, 22 Mar 2016 09:35:38 +0800 Message-Id: <1458610552-9845-14-git-send-email-quwenruo@cn.fujitsu.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1458610552-9845-1-git-send-email-quwenruo@cn.fujitsu.com> References: <1458610552-9845-1-git-send-email-quwenruo@cn.fujitsu.com> MIME-Version: 1.0 X-yoursite-MailScanner-ID: ADEBD4056411.ADCC7 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 Now on-disk backend can delete hash now. Signed-off-by: Wang Xiaoguang Signed-off-by: Qu Wenruo --- fs/btrfs/dedupe.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/fs/btrfs/dedupe.c b/fs/btrfs/dedupe.c index f73a4c7..c38137e 100644 --- a/fs/btrfs/dedupe.c +++ b/fs/btrfs/dedupe.c @@ -468,6 +468,104 @@ static int inmem_del(struct btrfs_dedupe_info *dedupe_info, u64 bytenr) return 0; } +/* + * If prepare_del is given, this will setup search_slot() for delete. + * Caller needs to do proper locking. + * + * Return > 0 for found. + * Return 0 for not found. + * Return < 0 for error. + */ +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) +{ + struct btrfs_key key; + struct btrfs_root *dedupe_root = dedupe_info->dedupe_root; + int ret; + int ins_len = 0; + int cow = 0; + + if (prepare_del) { + if (WARN_ON(trans == NULL)) + return -EINVAL; + cow = 1; + ins_len = -1; + } + + key.objectid = bytenr; + key.type = BTRFS_DEDUPE_BYTENR_ITEM_KEY; + key.offset = (u64)-1; + + ret = btrfs_search_slot(trans, dedupe_root, &key, path, + ins_len, cow); + + if (ret < 0) + return ret; + /* + * Although it's almost impossible, it's still possible that + * the last 64bits are all 1. + */ + if (ret == 0) + return 1; + + ret = btrfs_previous_item(dedupe_root, path, bytenr, + BTRFS_DEDUPE_BYTENR_ITEM_KEY); + if (ret < 0) + return ret; + if (ret > 0) + return 0; + return 1; +} + +static int ondisk_del(struct btrfs_trans_handle *trans, + struct btrfs_dedupe_info *dedupe_info, u64 bytenr) +{ + struct btrfs_root *dedupe_root = dedupe_info->dedupe_root; + struct btrfs_path *path; + struct btrfs_key key; + int ret; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + key.objectid = bytenr; + key.type = BTRFS_DEDUPE_BYTENR_ITEM_KEY; + key.offset = 0; + + mutex_lock(&dedupe_info->lock); + + ret = ondisk_search_bytenr(trans, dedupe_info, path, bytenr, 1); + if (ret <= 0) + goto out; + + btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); + ret = btrfs_del_item(trans, dedupe_root, path); + btrfs_release_path(path); + if (ret < 0) + goto out; + /* Search for hash item and delete it */ + key.objectid = key.offset; + key.type = BTRFS_DEDUPE_HASH_ITEM_KEY; + key.offset = bytenr; + + ret = btrfs_search_slot(trans, dedupe_root, &key, path, -1, 1); + if (WARN_ON(ret > 0)) { + ret = -ENOENT; + goto out; + } + if (ret < 0) + goto out; + ret = btrfs_del_item(trans, dedupe_root, path); + +out: + btrfs_free_path(path); + mutex_unlock(&dedupe_info->lock); + return ret; +} + /* Remove a dedupe hash from dedupe tree */ int btrfs_dedupe_del(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, u64 bytenr) @@ -482,6 +580,8 @@ int btrfs_dedupe_del(struct btrfs_trans_handle *trans, if (dedupe_info->backend == BTRFS_DEDUPE_BACKEND_INMEMORY) return inmem_del(dedupe_info, bytenr); + if (dedupe_info->backend == BTRFS_DEDUPE_BACKEND_ONDISK) + return ondisk_del(trans, dedupe_info, bytenr); return -EINVAL; }