From patchwork Tue Jan 29 06:57:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 10785545 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0B772922 for ; Tue, 29 Jan 2019 06:57:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1C462B120 for ; Tue, 29 Jan 2019 06:57:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E5D4B2B122; Tue, 29 Jan 2019 06:57:45 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 84CCA2B120 for ; Tue, 29 Jan 2019 06:57:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725747AbfA2G5o (ORCPT ); Tue, 29 Jan 2019 01:57:44 -0500 Received: from mx2.suse.de ([195.135.220.15]:38444 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725267AbfA2G5o (ORCPT ); Tue, 29 Jan 2019 01:57:44 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 0625AAB48 for ; Tue, 29 Jan 2019 06:57:43 +0000 (UTC) From: Qu Wenruo To: linux-btrfs@vger.kernel.org Subject: [PATCH] btrfs-progs: balance: Sync the fs before balancing metadata chunks Date: Tue, 29 Jan 2019 14:57:39 +0800 Message-Id: <20190129065739.31707-1-wqu@suse.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP [BUG] Btrfs will report false ENOSPC balancing metadata chunk. The following script can easily reproduce it: #!/bin/bash dev=/dev/test/test mnt=/mnt/btrfs umount $dev &> /dev/null umount $mnt &> /dev/null mkfs.btrfs -f $dev mount $dev $mnt btrfs subv create $mnt/subv for ((i = 0; i < 1024; i++)) do xfs_io -f -c "pwrite 0 4k" $mnt/subv/file_$i > /dev/null done btrfs balance start -m $mnt [CAUSE] It's metadata space_info::bytes_may_use causing the problem. For above case, we need to reserve enough metadata space for all the created small files. [FIX] The most straightforward is to sync the fs before balancing metadata chunks. We could enhance the kernel bytes_may_use calculation, but I doubt about the complexity. So I take the easy fix to reduce the false ENOSPC reports. Signed-off-by: Qu Wenruo --- cmds-balance.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmds-balance.c b/cmds-balance.c index 15dc385e..a617a1d2 100644 --- a/cmds-balance.c +++ b/cmds-balance.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "kerncompat.h" #include "ctree.h" @@ -32,6 +33,7 @@ #include "commands.h" #include "utils.h" +#include "utils.h" #include "help.h" static const char * const balance_cmd_group_usage[] = { @@ -455,6 +457,22 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, printf("\nStarting balance without any filters.\n"); } + /* + * There may be many over-reserved space for metadata block groups, + * especially for inlined file extents. + * + * Do a sync here will free those over-reserved space and hugely + * reduce the possibility of some false ENOSPC + */ + if (args->flags & BTRFS_BALANCE_METADATA) { + ret = btrfs_util_sync(path); + if (ret) { + error("failed to sync the fs before balance: %m"); + ret = -errno; + goto out; + } + } + ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args); if (ret < 0) { /*