All of lore.kernel.org
 help / color / mirror / Atom feed
From: damenly.su@gmail.com
To: linux-btrfs@vger.kernel.org
Cc: Su Yue <Damenly_Su@gmx.com>
Subject: [PATCH 03/10] btrfs-progs: port block group cache tree insertion and lookup functions
Date: Thu,  5 Dec 2019 12:29:14 +0800	[thread overview]
Message-ID: <20191205042921.25316-4-Damenly_Su@gmx.com> (raw)
In-Reply-To: <20191205042921.25316-1-Damenly_Su@gmx.com>

From: Su Yue <Damenly_Su@gmx.com>

Simple copy and paste codes, remove useless lock operantions in progs.
Th new coming lookup functions are named with suffix _kernel in
temporary.

Signed-off-by: Su Yue <Damenly_Su@gmx.com>
---
 extent-tree.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/extent-tree.c b/extent-tree.c
index 4a3db029e811..ab576f8732a2 100644
--- a/extent-tree.c
+++ b/extent-tree.c
@@ -164,6 +164,92 @@ err:
 	return 0;
 }
 
+/*
+ * This adds the block group to the fs_info rb tree for the block group cache
+ */
+static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
+				struct btrfs_block_group_cache *block_group)
+{
+	struct rb_node **p;
+	struct rb_node *parent = NULL;
+	struct btrfs_block_group_cache *cache;
+
+	p = &info->block_group_cache_tree.rb_node;
+
+	while (*p) {
+		parent = *p;
+		cache = rb_entry(parent, struct btrfs_block_group_cache,
+				 cache_node);
+		if (block_group->key.objectid < cache->key.objectid)
+			p = &(*p)->rb_left;
+		else if (block_group->key.objectid > cache->key.objectid)
+			p = &(*p)->rb_right;
+		else
+			return -EEXIST;
+	}
+
+	rb_link_node(&block_group->cache_node, parent, p);
+	rb_insert_color(&block_group->cache_node,
+			&info->block_group_cache_tree);
+
+	return 0;
+}
+
+/*
+ * This will return the block group at or after bytenr if contains is 0, else
+ * it will return the block group that contains the bytenr
+ */
+static struct btrfs_block_group_cache *block_group_cache_tree_search(
+		struct btrfs_fs_info *info, u64 bytenr, int contains)
+{
+	struct btrfs_block_group_cache *cache, *ret = NULL;
+	struct rb_node *n;
+	u64 end, start;
+
+	n = info->block_group_cache_tree.rb_node;
+
+	while (n) {
+		cache = rb_entry(n, struct btrfs_block_group_cache,
+				 cache_node);
+		end = cache->key.objectid + cache->key.offset - 1;
+		start = cache->key.objectid;
+
+		if (bytenr < start) {
+			if (!contains && (!ret || start < ret->key.objectid))
+				ret = cache;
+			n = n->rb_left;
+		} else if (bytenr > start) {
+			if (contains && bytenr <= end) {
+				ret = cache;
+				break;
+			}
+			n = n->rb_right;
+		} else {
+			ret = cache;
+			break;
+		}
+	}
+	return ret;
+}
+
+/*
+ * Return the block group that starts at or after bytenr
+ */
+struct btrfs_block_group_cache *btrfs_lookup_first_block_group_kernel(
+		struct btrfs_fs_info *info, u64 bytenr)
+{
+	return block_group_cache_tree_search(info, bytenr, 0);
+}
+
+/*
+ * Return the block group that contains the given bytenr
+ */
+struct btrfs_block_group_cache *btrfs_lookup_block_group_kernel(
+		struct btrfs_fs_info *info, u64 bytenr)
+{
+	return block_group_cache_tree_search(info, bytenr, 1);
+}
+
 /*
  * Return the block group that contains @bytenr, otherwise return the next one
  * that starts after @bytenr
-- 
2.21.0 (Apple Git-122)


  parent reply	other threads:[~2019-12-05  4:29 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-05  4:29 [PATCH 00/10] unify origanization structure of block group cache damenly.su
2019-12-05  4:29 ` [PATCH 01/10] btrfs-progs: handle error if btrfs_write_one_block_group() failed damenly.su
2019-12-05  7:21   ` Qu Wenruo
2019-12-05  4:29 ` [PATCH 02/10] btrfs-progs: block_group: add rb tree related memebers damenly.su
2019-12-05  7:22   ` Qu Wenruo
2019-12-05  4:29 ` damenly.su [this message]
2019-12-05  7:29   ` [PATCH 03/10] btrfs-progs: port block group cache tree insertion and lookup functions Qu Wenruo
2019-12-05  8:32     ` Su Yue
2019-12-05 13:41       ` David Sterba
2019-12-05  4:29 ` [PATCH 04/10] btrfs-progs: reform the function block_group_cache_tree_search() damenly.su
2019-12-05  7:38   ` Qu Wenruo
2019-12-05  8:08     ` Su Yue
2019-12-05  4:29 ` [PATCH 05/10] btrfs-progs: adjust function btrfs_lookup_first_block_group_kernel damenly.su
2019-12-05  7:40   ` Qu Wenruo
2019-12-05  8:20     ` Su Yue
2019-12-05  4:29 ` [PATCH 06/10] btrfs-progs: abstract function btrfs_add_block_group_cache() damenly.su
2019-12-05  7:42   ` Qu Wenruo
2019-12-05  4:29 ` [PATCH 07/10] block-progs: block_group: add dirty_bgs list related memebers damenly.su
2019-12-05  7:43   ` Qu Wenruo
2019-12-05  4:29 ` [PATCH 08/10] btrfs-progs: pass @trans to functions touch dirty block groups damenly.su
2019-12-05  7:44   ` Qu Wenruo
2019-12-05  4:29 ` [PATCH 09/10] btrfs-progs: refrom block groups caches structure damenly.su
2019-12-05  7:51   ` Qu Wenruo
2019-12-05  8:09     ` Su Yue
2019-12-05  4:29 ` [PATCH 10/10] btrfs-progs: cleanups after block group cache reform damenly.su
2019-12-05  7:52   ` 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=20191205042921.25316-4-Damenly_Su@gmx.com \
    --to=damenly.su@gmail.com \
    --cc=Damenly_Su@gmx.com \
    --cc=linux-btrfs@vger.kernel.org \
    /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.