@@ -336,25 +336,21 @@ static u64 calc_available_free_space(struct btrfs_fs_info *fs_info,
struct btrfs_space_info *space_info,
enum btrfs_reserve_flush_enum flush)
{
+ enum btrfs_raid_types index;
u64 profile;
u64 avail;
- int factor;
if (space_info->flags & BTRFS_BLOCK_GROUP_SYSTEM)
profile = btrfs_system_alloc_profile(fs_info);
else
profile = btrfs_metadata_alloc_profile(fs_info);
- avail = atomic64_read(&fs_info->free_chunk_space);
-
/*
- * If we have dup, raid1 or raid10 then only half of the free
- * space is actually usable. For raid56, the space info used
- * doesn't include the parity drive, so we don't have to
- * change the math
+ * Grab avail space from per-profile array which should be as accurate
+ * as chunk allocator.
*/
- factor = btrfs_bg_type_to_factor(profile);
- avail = div_u64(avail, factor);
+ index = btrfs_bg_flags_to_raid_index(profile);
+ avail = atomic64_read(&fs_info->fs_devices->per_profile_avail[index]);
/*
* If we aren't flushing all things, let us overcommit up to