static int check_free_space_extents(struct btrfs_trans_handle *trans,
				    struct btrfs_fs_info *fs_info,
				    struct btrfs_block_group_cache *cache,
				    struct btrfs_path *path,
				    struct free_space_extent *extents,
				    unsigned int num_extents)
{
	struct btrfs_free_space_info *info;
	u32 flags;
	int ret;

	info = search_free_space_info(trans, fs_info, cache, path, 0);
	if (IS_ERR(info)) {
		test_msg("Could not find free space info\n");
		btrfs_release_path(path);
		return PTR_ERR(info);
	}
	flags = btrfs_free_space_flags(path->nodes[0], info);
	btrfs_release_path(path);

	ret = __check_free_space_extents(trans, fs_info, cache, path, extents,
					 num_extents);
	if (ret)
		return ret;

	/* Flip it to the other format and check that for good measure. */
	if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
		ret = convert_free_space_to_extents(trans, fs_info, cache, path);
		if (ret) {
			test_msg("Could not convert to extents\n");
			return ret;
		}
	} else {
		ret = convert_free_space_to_bitmaps(trans, fs_info, cache, path);
		if (ret) {
			test_msg("Could not convert to bitmaps\n");
			return ret;
		}
	}
	return __check_free_space_extents(trans, fs_info, cache, path, extents,
					  num_extents);
}
static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
        struct btrfs_fs_info *fs_info,
        struct btrfs_block_group_cache *block_group,
        struct btrfs_path *path,
        int new_extents)
{
    struct btrfs_free_space_info *info;
    u32 flags;
    u32 extent_count;
    int ret = 0;

    if (new_extents == 0)
        return 0;

    info = search_free_space_info(trans, fs_info, block_group, path, 1);
    if (IS_ERR(info)) {
        ret = PTR_ERR(info);
        goto out;
    }
    flags = btrfs_free_space_flags(path->nodes[0], info);
    extent_count = btrfs_free_space_extent_count(path->nodes[0], info);

    extent_count += new_extents;
    btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
    btrfs_mark_buffer_dirty(path->nodes[0]);
    btrfs_release_path(path);

    if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
            extent_count > block_group->bitmap_high_thresh) {
        ret = convert_free_space_to_bitmaps(trans, fs_info, block_group,
                                            path);
    } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
               extent_count < block_group->bitmap_low_thresh) {
        ret = convert_free_space_to_extents(trans, fs_info, block_group,
                                            path);
    }

out:
    return ret;
}