diff mbox series

[RFC,08/17] btrfs: priority alloc: add btrfs_update_block_group_priority()

Message ID 20181128031148.357-9-suy.fnst@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show
Series btrfs: implementation of priority aware allocator | expand

Commit Message

Su Yue Nov. 28, 2018, 3:11 a.m. UTC
Introduce btrfs_update_block_group_priority() to update
block_groups::priority. It will move block group from old tree
to new tree if need.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 fs/btrfs/extent-tree.c | 76 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
diff mbox series

Patch

diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index b559c9a9afc6..5ea1f2e40701 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -11466,3 +11466,79 @@  void btrfs_remove_block_group_priority(struct btrfs_block_group_cache *cache)
 	spin_unlock(&cache->lock);
 	up_write(&pt->groups_sem);
 }
+
+static inline struct btrfs_priority_tree *
+find_priority_tree(struct btrfs_space_info *sinfo, u64 flags, int level)
+{
+	int index = btrfs_bg_flags_to_raid_index(flags);
+
+	return search_priority_tree(&sinfo->priority_trees[index], level);
+}
+
+void btrfs_update_block_group_priority(struct btrfs_block_group_cache *cache)
+{
+	struct btrfs_priority_tree *old_tree, *new_tree;
+	struct rw_semaphore *front_sem, *back_sem;
+	long priority;
+	int old_level, new_level;
+
+	if (!is_priority_alloc_enabled(cache->fs_info))
+		return;
+
+	spin_lock(&cache->lock);
+	if (cache->priority != PRIORITY_BG_UPDATING) {
+		spin_unlock(&cache->lock);
+		return;
+	}
+
+	old_level = cache->priority_tree->level;
+	priority = compute_block_group_priority(cache);
+	new_level = compute_priority_level(cache->fs_info, priority);
+
+	/* no need to move the block group */
+	if (old_level == new_level) {
+		cache->priority = priority;
+		spin_unlock(&cache->lock);
+		return;
+	}
+	spin_unlock(&cache->lock);
+
+	old_tree = cache->priority_tree;
+	new_tree = find_priority_tree(cache->space_info, cache->flags,
+				      new_level);
+
+	if (!old_tree || !new_tree) {
+		btrfs_err(cache->fs_info,
+"can't found priority tree %d for block_group %llu old level %d new priority %ld",
+			  old_tree ? new_level : old_level,
+			  cache->key.objectid, old_level, priority);
+		BUG();
+	}
+
+	if (old_level > new_level) {
+		front_sem = &old_tree->groups_sem;
+		back_sem = &new_tree->groups_sem;
+	} else {
+		front_sem = &new_tree->groups_sem;
+		back_sem = &old_tree->groups_sem;
+	}
+
+	down_write(front_sem);
+	down_write(back_sem);
+	spin_lock(&cache->lock);
+
+	/* the block group was removed/is removing */
+	if (cache->priority != PRIORITY_BG_UPDATING ||
+	    priority != compute_block_group_priority(cache) ||
+	    old_tree != cache->priority_tree)
+		goto out;
+
+	unlink_block_group_priority(old_tree, cache);
+	cache->priority = priority;
+	link_block_group_priority(new_tree, cache);
+	cache->priority_tree = new_tree;
+out:
+	spin_unlock(&cache->lock);
+	up_write(front_sem);
+	up_write(back_sem);
+}