Commit 4ac81c38 authored by Filipe Manana's avatar Filipe Manana Committed by David Sterba
Browse files

btrfs: use the btrfs_block_group_end() helper everywhere



We have a helper to calculate a block group's exclusive end offset, but
we only use it in some places. Update every site that open codes the
calculation to use the helper.

Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: default avatarFilipe Manana <fdmanana@suse.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 57a4a863
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -239,7 +239,7 @@ static struct btrfs_block_group *block_group_cache_tree_search(

	while (n) {
		cache = rb_entry(n, struct btrfs_block_group, cache_node);
		end = cache->start + cache->length - 1;
		end = btrfs_block_group_end(cache) - 1;
		start = cache->start;

		if (bytenr < start) {
@@ -292,7 +292,7 @@ struct btrfs_block_group *btrfs_next_block_group(

	/* If our block group was removed, we need a full search. */
	if (RB_EMPTY_NODE(&cache->cache_node)) {
		const u64 next_bytenr = cache->start + cache->length;
		const u64 next_bytenr = btrfs_block_group_end(cache);

		read_unlock(&fs_info->block_group_cache_lock);
		btrfs_put_block_group(cache);
@@ -595,7 +595,7 @@ static int sample_block_group_extent_item(struct btrfs_caching_control *caching_
	struct btrfs_fs_info *fs_info = block_group->fs_info;
	struct btrfs_root *extent_root;
	u64 search_offset;
	u64 search_end = block_group->start + block_group->length;
	const u64 search_end = btrfs_block_group_end(block_group);
	BTRFS_PATH_AUTO_FREE(path);
	struct btrfs_key search_key;
	int ret = 0;
@@ -711,6 +711,7 @@ static int load_block_group_size_class(struct btrfs_caching_control *caching_ctl
static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
{
	struct btrfs_block_group *block_group = caching_ctl->block_group;
	const u64 block_group_end = btrfs_block_group_end(block_group);
	struct btrfs_fs_info *fs_info = block_group->fs_info;
	struct btrfs_root *extent_root;
	BTRFS_PATH_AUTO_FREE(path);
@@ -807,7 +808,7 @@ static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
			continue;
		}

		if (key.objectid >= block_group->start + block_group->length)
		if (key.objectid >= block_group_end)
			break;

		if (key.type == BTRFS_EXTENT_ITEM_KEY ||
@@ -836,9 +837,7 @@ static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
		path->slots[0]++;
	}

	ret = btrfs_add_new_free_space(block_group, last,
				       block_group->start + block_group->length,
				       NULL);
	ret = btrfs_add_new_free_space(block_group, last, block_group_end, NULL);
out:
	return ret;
}
@@ -846,7 +845,7 @@ static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
static inline void btrfs_free_excluded_extents(const struct btrfs_block_group *bg)
{
	btrfs_clear_extent_bit(&bg->fs_info->excluded_extents, bg->start,
			       bg->start + bg->length - 1, EXTENT_DIRTY, NULL);
			       btrfs_block_group_end(bg) - 1, EXTENT_DIRTY, NULL);
}

static noinline void caching_thread(struct btrfs_work *work)
@@ -2267,7 +2266,7 @@ static int exclude_super_stripes(struct btrfs_block_group *cache)

		while (nr--) {
			u64 len = min_t(u64, stripe_len,
				cache->start + cache->length - logical[nr]);
					btrfs_block_group_end(cache) - logical[nr]);

			cache->bytes_super += len;
			ret = btrfs_set_extent_bit(&fs_info->excluded_extents,
@@ -2470,7 +2469,7 @@ static int read_one_block_group(struct btrfs_fs_info *info,
	} else if (cache->used == 0 && cache->remap_bytes == 0) {
		cache->cached = BTRFS_CACHE_FINISHED;
		ret = btrfs_add_new_free_space(cache, cache->start,
					       cache->start + cache->length, NULL);
					       btrfs_block_group_end(cache), NULL);
		btrfs_free_excluded_extents(cache);
		if (ret)
			goto error;
@@ -3763,7 +3762,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
		return -ENOENT;

	/* An extent can not span multiple block groups. */
	ASSERT(bytenr + num_bytes <= cache->start + cache->length);
	ASSERT(bytenr + num_bytes <= btrfs_block_group_end(cache));

	space_info = cache->space_info;
	factor = btrfs_bg_type_to_factor(cache->flags);
+4 −5
Original line number Diff line number Diff line
@@ -2784,8 +2784,7 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
		u64 len;
		bool readonly;

		if (!cache ||
		    start >= cache->start + cache->length) {
		if (!cache || start >= btrfs_block_group_end(cache)) {
			if (cache)
				btrfs_put_block_group(cache);
			total_unpinned = 0;
@@ -2801,7 +2800,7 @@ static int unpin_extent_range(struct btrfs_fs_info *fs_info,
			empty_cluster <<= 1;
		}

		len = cache->start + cache->length - start;
		len = btrfs_block_group_end(cache) - start;
		len = min(len, end + 1 - start);

		if (return_free_space)
@@ -4683,7 +4682,7 @@ static noinline int find_free_extent(struct btrfs_root *root,

		/* move on to the next group */
		if (ffe_ctl->search_start + ffe_ctl->num_bytes >
		    block_group->start + block_group->length) {
		    btrfs_block_group_end(block_group)) {
			btrfs_add_free_space_unused(block_group,
					    ffe_ctl->found_offset,
					    ffe_ctl->num_bytes);
@@ -6651,7 +6650,7 @@ int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
		}

		start = max(range->start, cache->start);
		end = min(range_end, cache->start + cache->length);
		end = min(range_end, btrfs_block_group_end(cache));

		if (end - start >= range->minlen) {
			if (!btrfs_block_group_done(cache)) {
+4 −4
Original line number Diff line number Diff line
@@ -1201,6 +1201,7 @@ static noinline_for_stack int write_pinned_extent_entries(
			    int *entries)
{
	u64 start, extent_start, extent_end, len;
	const u64 block_group_end = btrfs_block_group_end(block_group);
	struct extent_io_tree *unpin = NULL;
	int ret;

@@ -1215,19 +1216,18 @@ static noinline_for_stack int write_pinned_extent_entries(

	start = block_group->start;

	while (start < block_group->start + block_group->length) {
	while (start < block_group_end) {
		if (!btrfs_find_first_extent_bit(unpin, start,
						 &extent_start, &extent_end,
						 EXTENT_DIRTY, NULL))
			return 0;

		/* This pinned extent is out of our range */
		if (extent_start >= block_group->start + block_group->length)
		if (extent_start >= block_group_end)
			return 0;

		extent_start = max(extent_start, start);
		extent_end = min(block_group->start + block_group->length,
				 extent_end + 1);
		extent_end = min(block_group_end, extent_end + 1);
		len = extent_end - extent_start;

		*entries += 1;
+13 −20
Original line number Diff line number Diff line
@@ -218,7 +218,7 @@ int btrfs_convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
		return 0;

	start = block_group->start;
	end = block_group->start + block_group->length;
	end = btrfs_block_group_end(block_group);

	key.objectid = end - 1;
	key.type = (u8)-1;
@@ -358,7 +358,7 @@ int btrfs_convert_free_space_to_extents(struct btrfs_trans_handle *trans,
		return 0;

	start = block_group->start;
	end = block_group->start + block_group->length;
	end = btrfs_block_group_end(block_group);

	key.objectid = end - 1;
	key.type = (u8)-1;
@@ -665,7 +665,7 @@ static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
	 * Read the bit for the block immediately after the extent of space if
	 * that block is within the block group.
	 */
	if (end < block_group->start + block_group->length) {
	if (end < btrfs_block_group_end(block_group)) {
		/* The next block may be in the next bitmap. */
		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
		if (end >= key.objectid + key.offset) {
@@ -938,7 +938,7 @@ static int add_free_space_extent(struct btrfs_trans_handle *trans,

right:
	/* Search for a neighbor on the right. */
	if (end == block_group->start + block_group->length)
	if (end == btrfs_block_group_end(block_group))
		goto insert;
	key.objectid = end;
	key.type = (u8)-1;
@@ -1104,7 +1104,7 @@ static int populate_free_space_tree(struct btrfs_trans_handle *trans,
	 * highest, block group).
	 */
	start = block_group->start;
	end = block_group->start + block_group->length;
	end = btrfs_block_group_end(block_group);
	while (ret == 0) {
		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);

@@ -1477,7 +1477,7 @@ int btrfs_remove_block_group_free_space(struct btrfs_trans_handle *trans,
	}

	start = block_group->start;
	end = block_group->start + block_group->length;
	end = btrfs_block_group_end(block_group);

	key.objectid = end - 1;
	key.type = (u8)-1;
@@ -1530,24 +1530,21 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
				   struct btrfs_path *path,
				   u32 expected_extent_count)
{
	struct btrfs_block_group *block_group;
	struct btrfs_fs_info *fs_info;
	struct btrfs_block_group *block_group = caching_ctl->block_group;
	struct btrfs_fs_info *fs_info = block_group->fs_info;
	struct btrfs_root *root;
	struct btrfs_key key;
	bool prev_bit_set = false;
	/* Initialize to silence GCC. */
	u64 extent_start = 0;
	u64 end, offset;
	const u64 end = btrfs_block_group_end(block_group);
	u64 offset;
	u64 total_found = 0;
	u32 extent_count = 0;
	int ret;

	block_group = caching_ctl->block_group;
	fs_info = block_group->fs_info;
	root = btrfs_free_space_root(block_group);

	end = block_group->start + block_group->length;

	while (1) {
		ret = btrfs_next_item(root, path);
		if (ret < 0)
@@ -1613,21 +1610,17 @@ static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
				   struct btrfs_path *path,
				   u32 expected_extent_count)
{
	struct btrfs_block_group *block_group;
	struct btrfs_fs_info *fs_info;
	struct btrfs_block_group *block_group = caching_ctl->block_group;
	struct btrfs_fs_info *fs_info = block_group->fs_info;
	struct btrfs_root *root;
	struct btrfs_key key;
	u64 end;
	const u64 end = btrfs_block_group_end(block_group);
	u64 total_found = 0;
	u32 extent_count = 0;
	int ret;

	block_group = caching_ctl->block_group;
	fs_info = block_group->fs_info;
	root = btrfs_free_space_root(block_group);

	end = block_group->start + block_group->length;

	while (1) {
		u64 space_added;

+5 −4
Original line number Diff line number Diff line
@@ -1688,9 +1688,9 @@ static int scrub_find_fill_first_stripe(struct btrfs_block_group *bg,
	scrub_stripe_reset_bitmaps(stripe);

	/* The range must be inside the bg. */
	ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length,
	ASSERT(logical_start >= bg->start && logical_end <= btrfs_block_group_end(bg),
	       "bg->start=%llu logical_start=%llu logical_end=%llu end=%llu",
	       bg->start, logical_start, logical_end, bg->start + bg->length);
	       bg->start, logical_start, logical_end, btrfs_block_group_end(bg));

	ret = find_first_extent_item(extent_root, extent_path, logical_start,
				     logical_len);
@@ -2319,7 +2319,7 @@ static int scrub_simple_mirror(struct scrub_ctx *sctx,
	int ret = 0;

	/* The range must be inside the bg */
	ASSERT(logical_start >= bg->start && logical_end <= bg->start + bg->length);
	ASSERT(logical_start >= bg->start && logical_end <= btrfs_block_group_end(bg));

	/* Go through each extent items inside the logical range */
	while (cur_logical < logical_end) {
@@ -2411,12 +2411,13 @@ static int scrub_simple_stripe(struct scrub_ctx *sctx,
	const u64 logical_increment = simple_stripe_full_stripe_len(map);
	const u64 orig_logical = simple_stripe_get_logical(map, bg, stripe_index);
	const u64 orig_physical = map->stripes[stripe_index].physical;
	const u64 end = btrfs_block_group_end(bg);
	const int mirror_num = simple_stripe_mirror_num(map, stripe_index);
	u64 cur_logical = orig_logical;
	u64 cur_physical = orig_physical;
	int ret = 0;

	while (cur_logical < bg->start + bg->length) {
	while (cur_logical < end) {
		/*
		 * Inside each stripe, RAID0 is just SINGLE, and RAID10 is
		 * just RAID1, so we can reuse scrub_simple_mirror() to scrub
Loading