]> git.hungrycats.org Git - linux/commitdiff
btrfs: zoned: only change active zone counter on successful (de)activation
authorJohannes Thumshirn <johannes.thumshirn@wdc.com>
Wed, 9 Sep 2026 06:47:25 +0000 (08:47 +0200)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:17 +0000 (21:36 +0100)
btrfs_zone_activate() unconditionally decrements the reserved_active_zones
counter regardless if btrfs_dev_set_active_zone() fails to set the zone
active, i.e. because it raced with another call that already set the bit
in the bitmask, or not.

This can lead to a double decrement of the counter in case the bit has
already been set.

Only decrement the counter *iff* btrfs_dev_set_active_zone() successfully
marked the zone in the bitmap.

Mirror this behaviour when clearing the bit again.

Fixes: a7e1ac7bdc5a ("btrfs: zoned: reserve zones for an active metadata/system block group")
Reviewed-by: Daniel Vacek <neelx@suse.com>
Signed-off-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/zoned.c

index 1cda88332284e83aad7704633c449101d4770731..43973d9809b3e33c13be93b5548e9e3f67270a19 100644 (file)
@@ -1117,11 +1117,13 @@ u64 btrfs_find_allocatable_zones(struct btrfs_device *device, u64 hole_start,
        return pos;
 }
 
-static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
+static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos, bool *new)
 {
        struct btrfs_zoned_device_info *zone_info = device->zone_info;
        unsigned int zno = (pos >> zone_info->zone_size_shift);
 
+       *new = false;
+
        /* We can use any number of zones */
        if (zone_info->max_active_zones == 0)
                return true;
@@ -1133,23 +1135,29 @@ static bool btrfs_dev_set_active_zone(struct btrfs_device *device, u64 pos)
                if (test_and_set_bit(zno, zone_info->active_zones)) {
                        /* Someone already set the bit */
                        atomic_inc(&zone_info->active_zones_left);
+               } else {
+                       *new = true;
                }
        }
 
        return true;
 }
 
-static void btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
+static bool btrfs_dev_clear_active_zone(struct btrfs_device *device, u64 pos)
 {
        struct btrfs_zoned_device_info *zone_info = device->zone_info;
        unsigned int zno = (pos >> zone_info->zone_size_shift);
 
        /* We can use any number of zones */
        if (zone_info->max_active_zones == 0)
-               return;
+               return false;
 
-       if (test_and_clear_bit(zno, zone_info->active_zones))
+       if (test_and_clear_bit(zno, zone_info->active_zones)) {
                atomic_inc(&zone_info->active_zones_left);
+               return true;
+       }
+
+       return false;
 }
 
 int btrfs_reset_device_zone(struct btrfs_device *device, u64 physical,
@@ -2402,6 +2410,7 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)
        u64 physical;
        const bool is_data = (block_group->flags & BTRFS_BLOCK_GROUP_DATA);
        bool ret;
+       bool new;
        int i;
 
        if (!btrfs_is_zoned(block_group->fs_info))
@@ -2455,12 +2464,12 @@ bool btrfs_zone_activate(struct btrfs_block_group *block_group)
                        goto out_unlock;
                }
 
-               if (!btrfs_dev_set_active_zone(device, physical)) {
+               if (!btrfs_dev_set_active_zone(device, physical, &new)) {
                        /* Cannot activate the zone */
                        ret = false;
                        goto out_unlock;
                }
-               if (!is_data)
+               if (!is_data && new)
                        zinfo->reserved_active_zones--;
        }
 
@@ -2507,6 +2516,7 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
        struct btrfs_device *device = stripe->dev;
        const u64 physical = stripe->physical;
        struct btrfs_zoned_device_info *zinfo = device->zone_info;
+       bool cleared;
        int ret;
 
        if (!device->bdev)
@@ -2528,9 +2538,9 @@ static int call_zone_finish(struct btrfs_block_group *block_group,
                        return ret;
        }
 
-       if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA))
+       cleared = btrfs_dev_clear_active_zone(device, physical);
+       if (!(block_group->flags & BTRFS_BLOCK_GROUP_DATA) && cleared)
                zinfo->reserved_active_zones++;
-       btrfs_dev_clear_active_zone(device, physical);
 
        return 0;
 }