From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 55644C3276E for ; Thu, 2 Jan 2020 07:48:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 2BF7E21655 for ; Thu, 2 Jan 2020 07:48:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727739AbgABHrV (ORCPT ); Thu, 2 Jan 2020 02:47:21 -0500 Received: from mx2.suse.de ([195.135.220.15]:48322 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726145AbgABHrV (ORCPT ); Thu, 2 Jan 2020 02:47:21 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 7550FAD20; Thu, 2 Jan 2020 07:47:19 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Cc: Marc Lehmann Subject: [PATCH 3/4] btrfs: space-info: Use per-profile available space in can_overcommit() Date: Thu, 2 Jan 2020 15:47:04 +0800 Message-Id: <20200102074705.136348-4-wqu@suse.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200102074705.136348-1-wqu@suse.com> References: <20200102074705.136348-1-wqu@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org For the following disk layout, can_overcommit() can cause false confidence in available space: devid 1 unallocated: 1T devid 2 unallocated: 10T metadata type: RAID1 As can_overcommit() simply uses unallocated space with factor to calculate the allocatable metadata chunk size. can_overcommit() believes we still have 5.5T for metadata chunks, while the truth is, we only have 1T available for metadata chunks. This can lead to ENOSPC at run_delalloc_range() and cause transaction abort. Since factor based calculation can't distinguish RAID1/RAID10 and DUP at all, we need proper chunk-allocator level awareness to do such estimation. Thankfully, we have per-profile available space already calculated, just use that facility to avoid such false confidence. Reported-by: Marc Lehmann Signed-off-by: Qu Wenruo --- fs/btrfs/space-info.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/space-info.c b/fs/btrfs/space-info.c index f09aa6ee9113..c26aba9e7124 100644 --- a/fs/btrfs/space-info.c +++ b/fs/btrfs/space-info.c @@ -164,10 +164,10 @@ static int can_overcommit(struct btrfs_fs_info *fs_info, enum btrfs_reserve_flush_enum flush, bool system_chunk) { + enum btrfs_raid_types index; u64 profile; u64 avail; u64 used; - int factor; /* Don't overcommit when in mixed mode. */ if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) @@ -179,16 +179,15 @@ static int can_overcommit(struct btrfs_fs_info *fs_info, profile = btrfs_metadata_alloc_profile(fs_info); used = btrfs_space_info_used(space_info, true); - 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); + spin_lock(&fs_info->fs_devices->per_profile_lock); + avail = fs_info->fs_devices->per_profile_avail[index]; + spin_unlock(&fs_info->fs_devices->per_profile_lock); /* * If we aren't flushing all things, let us overcommit up to -- 2.24.1