btrfs-progs: balance: Sync the fs before balancing metadata chunks
PATCH 1/1 btrfs-progs: balance: Sync the fs before balancing metadata chunks
[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.
--- cmds-balance.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
cmds-balance.c +18 -0
@@ -24,6 +24,7 @@#include <sys/stat.h> #include <fcntl.h> #include <errno.h> +#include <btrfsutil.h> #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 ) {/*
Signature · 1 line
2.18.0
Quoted 6 lines · Qu Wenruo
The most straightforward is to sync the fs before balancing metadata chunks.
We could enhance the kernel
bytes_may_usecalculation, but I doubt about the complexity.
So I take the easy fix to reduce the false ENOSPC reports.
Agreed.
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 468
Quoted 9 lines
cmds-balance.c +9
-0+/* +* 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);
As the fd is already open, we should use the _fd version,
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 472
Quoted 4 lines
cmds-balance.c +4
-0+if (ret) { +error ("failed to sync the fs before balance: %m" ); + ret = -errno; + goto out;
and possibly only warn if there's an error returned as the sync failure is not a critical condition.
Quoted 22 lines · David Sterba
Quoted 6 lines · Qu Wenruo
The most straightforward is to sync the fs before balancing metadata chunks.
We could enhance the kernel
bytes_may_usecalculation, but I doubt about the complexity.
So I take the easy fix to reduce the false ENOSPC reports.Agreed.
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 468
Quoted 9 lines
cmds-balance.c +9
-0+/* +* 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);As the fd is already open, we should use the
_fdversion,
Quoted 7 lines
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 472
Quoted 4 lines
cmds-balance.c +4
-0+if (ret) { +error ("failed to sync the fs before balance: %m" ); + ret = -errno; + goto out;and possibly only warn if there's an error returned as the sync failure is not a critical condition.
AFAIK if we can't even sync the fs, the balance is definitely going to fail, as the most common failure mode for syncfs is RO fs, caused by aborted transaction.
Thus I still think we should error out. Or is there some other non-critical failure mode I missed?
Thanks,
Qu
Quoted 33 lines · Qu Wenruo
Quoted 22 lines · David Sterba
Quoted 6 lines · Qu Wenruo
The most straightforward is to sync the fs before balancing metadata chunks.
We could enhance the kernel
bytes_may_usecalculation, but I doubt about the complexity.
So I take the easy fix to reduce the false ENOSPC reports.Agreed.
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 468
Quoted 9 lines
cmds-balance.c +9
-0+/* +* 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);As the fd is already open, we should use the
_fdversion,Quoted 7 lines
cmds-balance.c static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, · line 472
Quoted 4 lines
cmds-balance.c +4
-0+if (ret) { +error ("failed to sync the fs before balance: %m" ); + ret = -errno; + goto out;and possibly only warn if there's an error returned as the sync failure is not a critical condition.
Quoted 6 lines
AFAIK if we can't even sync the fs, the balance is definitely going to fail, as the most common failure mode for syncfs is RO fs, caused by aborted transaction.
Thus I still think we should error out. Or is there some other non-critical failure mode I missed?
The read-only filesystem will be checked when balance starts, that's where it gets reported.