Re: [PATCH] btrfs: fix return value when race occur between balance and cancel/pause

From: David Sterba
Date: Thu Aug 10 2023 - 08:11:31 EST


On Wed, Aug 09, 2023 at 11:48:10PM -0400, xiaoshoukui wrote:
> Issue a pause or cancel IOCTL request after judging that there is no
> pause or cancel request on the path of __btrfs_balance to return 0,
> which will mislead the user that the pause or cancel requests are
> successful.In fact, the balance request has not been paused or canceled.
>
> On that race condition, a non-zero errno should be returned to the user.
>
> Signed-off-by: xiaoshoukui <xiaoshoukui@xxxxxxxxxxxxx>
> ---
> fs/btrfs/fs.h | 6 ++++++
> fs/btrfs/volumes.c | 14 +++++++++-----
> 2 files changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
> index 203d2a267828..c27def881922 100644
> --- a/fs/btrfs/fs.h
> +++ b/fs/btrfs/fs.h
> @@ -93,6 +93,12 @@ enum {
> */
> BTRFS_FS_BALANCE_RUNNING,
>
> + /* Indicate that balance has been paused. */
> + BTRFS_FS_BALANCE_PAUSED,
> +
> + /* Indicate that balance has been canceled. */
> + BTRFS_FS_BALANCE_CANCELED,

I don't like that the status is tracked in several bits like that, in
addition to the already complicated locking and state transitions of
restarted balance. I think this is a hint that some things can be
simplified or combined together, though it could be difficult