From patchwork Thu Jul 20 06:56:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gu Jinxiang X-Patchwork-Id: 9853941 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 555A860388 for ; Thu, 20 Jul 2017 06:57:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 462902876B for ; Thu, 20 Jul 2017 06:57:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3B79F28770; Thu, 20 Jul 2017 06:57:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AA2B12876B for ; Thu, 20 Jul 2017 06:57:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934045AbdGTG5C (ORCPT ); Thu, 20 Jul 2017 02:57:02 -0400 Received: from cn.fujitsu.com ([59.151.112.132]:24333 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753185AbdGTG47 (ORCPT ); Thu, 20 Jul 2017 02:56:59 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="21556368" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 20 Jul 2017 14:56:53 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 992BA46B4C6A; Thu, 20 Jul 2017 14:56:47 +0800 (CST) Received: from localhost.localdomain (10.167.226.132) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 20 Jul 2017 14:56:47 +0800 From: Gu Jinxiang To: CC: Qu Wenruo Subject: [PATCH v6 10/15] btrfs-progs: extent-tree: Introduce function to check if there is any extent in given range. Date: Thu, 20 Jul 2017 14:56:03 +0800 Message-ID: <20170720065608.27563-11-gujx@cn.fujitsu.com> X-Mailer: git-send-email 2.9.4 In-Reply-To: <20170720065608.27563-1-gujx@cn.fujitsu.com> References: <20170720065608.27563-1-gujx@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.132] X-yoursite-MailScanner-ID: 992BA46B4C6A.A7C79 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: gujx@cn.fujitsu.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Qu Wenruo Introduce a new function, btrfs_check_extent_exists(), to check if there is any extent in the range specified by user. The parameter can be a large range, and if any extent exists in the range, it will return >0 (in fact it will return 1). Or return 0 if no extent is found. Signed-off-by: Qu Wenruo Signed-off-by: Gu Jinxiang --- ctree.h | 2 ++ extent-tree.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/ctree.h b/ctree.h index 19ba64e..8ebff7a 100644 --- a/ctree.h +++ b/ctree.h @@ -2520,6 +2520,8 @@ int exclude_super_stripes(struct btrfs_root *root, u64 add_new_free_space(struct btrfs_block_group_cache *block_group, struct btrfs_fs_info *info, u64 start, u64 end); u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset); +int btrfs_check_extent_exists(struct btrfs_fs_info *fs_info, u64 start, + u64 len); /* ctree.c */ int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2); diff --git a/extent-tree.c b/extent-tree.c index 46b8a05..2ece767 100644 --- a/extent-tree.c +++ b/extent-tree.c @@ -4254,3 +4254,63 @@ u64 add_new_free_space(struct btrfs_block_group_cache *block_group, return total_added; } + +/* + * Check if there is any extent(both data and metadata) in the range + * [@start, @start + @len) + * + * Return 0 for no extent found. + * Return >0 for found extent. + * Return <0 for fatal error. + */ +int btrfs_check_extent_exists(struct btrfs_fs_info *fs_info, u64 start, + u64 len) +{ + struct btrfs_path *path; + struct btrfs_key key; + u64 extent_start; + u64 extent_len; + int ret; + + path = btrfs_alloc_path(); + if (!path) + return -ENOMEM; + + key.objectid = start + len; + key.type = 0; + key.offset = 0; + + ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); + if (ret < 0) + goto out; + /* + * Now we're pointing at slot whose key.object >= end, skip to previous + * extent. + */ + ret = btrfs_previous_extent_item(fs_info->extent_root, path, 0); + if (ret < 0) + goto out; + if (ret > 0) { + ret = 0; + goto out; + } + btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); + extent_start = key.objectid; + if (key.type == BTRFS_METADATA_ITEM_KEY) + extent_len = fs_info->nodesize; + else + extent_len = key.offset; + + /* + * search_slot() and previous_extent_item() has ensured that our + * extent_start < start + len, we only need to care extent end. + */ + if (extent_start + extent_len <= start) + ret = 0; + else + ret = 1; + +out: + btrfs_free_path(path); + return ret; +}