]> git.hungrycats.org Git - linux/commitdiff
btrfs: allocator hints: make the available space estimates honor the hints misc-next/topics/allocator-hints
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 18 Oct 2026 19:50:43 +0000 (15:50 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 12 Nov 2026 15:26:17 +0000 (10:26 -0500)
The chunk allocator keeps data chunks off METADATA_ONLY and NONE_ONLY
devices and metadata and system chunks off DATA_ONLY and NONE_ONLY
devices (gather_device_info()).  The available space estimates do not:
btrfs_update_per_profile_avail() simulates chunk allocation over every
writable device, and statfs adds up every device's unallocated space.
The space of a device a hint excludes is counted as available although
no chunk of that type can ever be made from it.

Metadata overcommit trusts that estimate (calc_available_free_space()),
so once the devices metadata may use are full, reservations are granted
against chunks that cannot be allocated, and the transactions that use
them abort.  Reproducer (raid56-wh-harness alloc-hints-space-test.sh):
raid1 data and metadata on three devices of 6, 3 and 6 GiB, devid 3 set
to NONE_ONLY (5) and emptied with a full balance, then data written
until ENOSPC (devid 2 fills while devid 1 still has 3 GiB) and small
files created until ENOSPC:

  BTRFS: error (device loop2 state A) in btrfs_finish_one_ordered:3410: errno=-28 No space left
  BTRFS error (device loop2 state EA): Transaction 29 aborted (-ENOSPC)
  BTRFS info (device loop2 state EA): forced readonly

and, after a remount, a second abort from __btrfs_update_delayed_inode()
while the files were read back.  The files written since the last
commit were lost.  statfs reported 3103 MiB available at the data ENOSPC.
The same filesystem without devid 3 (6 + 3 GiB, no hints) fills
metadata to a clean ENOSPC and reports 55 MiB.

Give the estimates the allocator's view of the hints:

- btrfs_dev_alloc_hint() decides whether a chunk type may use a device
  and returns its tier; btrfs_select_hint_tiers() keeps the tiers a
  chunk would use.  gather_device_info() now uses both instead of its
  open-coded copy, so the rules live in one place.
- The per-profile estimate is kept separately for data and for metadata
  and system chunks; alloc_virtual_chunk() skips the devices the hints
  exclude for the type and picks hint tiers the way the allocator does.
  btrfs_get_per_profile_avail() takes the block group type.  Mixed block
  groups ignore the hints, as in the allocator.
- statfs leaves out devices that data chunks may not use.  Only the
  available space changes: f_bavail (df Avail) leaves out the
  unallocated space of those devices, while f_blocks (df Size) and
  f_bfree (and so df Used) still count every device, including the ones
  the hints keep data off.  On a filesystem with a METADATA_ONLY device,
  for example, Size minus Used is larger than Avail by about that
  device's unallocated space.
- A type change recomputes the estimate.

PREFERRED_NONE devices still count: the allocator uses them as the last
tier.  Without CONFIG_BTRFS_ALLOCATOR_HINTS the data and metadata
estimates are identical and only one is computed.

The estimates have been hint-blind since "btrfs: add allocator_hint
mode", earlier in this series, taught the allocator the hints.

The fallback to fs_info->free_chunk_space (zoned filesystems, or a
failed estimate) and the reclaim thresholds that read it stay hint-blind.

With this change the reproducer fills metadata to a clean ENOSPC with no
abort and no lost files, and statfs reports the space data can really
use.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/space-info.c
fs/btrfs/super.c
fs/btrfs/sysfs.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h

index d5a84fa97462cc52d4c558c0d19cef303102ba55..4165ace1cedd3a1b9e3cb24d5565d3cc6dd10187 100644 (file)
@@ -461,7 +461,8 @@ static u64 calc_available_free_space(const struct btrfs_space_info *space_info,
        else
                profile = btrfs_metadata_alloc_profile(fs_info);
 
-       has_per_profile = btrfs_get_per_profile_avail(fs_info, profile, &avail);
+       has_per_profile = btrfs_get_per_profile_avail(fs_info, space_info->flags,
+                                                     profile, &avail);
        if (!has_per_profile) {
                avail = atomic64_read(&fs_info->free_chunk_space);
 
index b424cc836bd4a366b0c6264524b2d4d6000a3a41..74d2468914d31a28e0ace16839fef8a62167b182 100644 (file)
@@ -1644,6 +1644,10 @@ static inline int btrfs_calc_avail_data_space(struct btrfs_fs_info *fs_info,
                if (i >= nr_devices)
                        break;
 
+               /* Space the allocator hints keep data off is not free for data. */
+               if (!btrfs_device_allows_chunk_type(device, BTRFS_BLOCK_GROUP_DATA))
+                       continue;
+
                avail_space = device->total_bytes - device->bytes_used;
 
                /* align with stripe_len */
index 6e1d0636f9af90088732b9ed5404d3e58558d362..3732f58439c67d4a12995ed7d7d34f36867a4145 100644 (file)
@@ -2072,6 +2072,8 @@ static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
         */
        smp_mb__before_atomic();
        set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state);
+       /* The available space estimates depend on the hints. */
+       btrfs_update_per_profile_avail(fs_info);
        mutex_unlock(&fs_info->chunk_mutex);
 
        /* We don't want to do full transaction commit from inside sysfs */
index cb38ca3b61a65d956dd52a54af3c4cca8a0b1de9..19bbb4430d46a056421a4c78d8901b5186f142be 100644 (file)
@@ -200,7 +200,101 @@ static const int alloc_hint_map[BTRFS_DEV_ALLOCATION_MASK_COUNT] = {
        [BTRFS_DEV_ALLOCATION_PREFERRED_NONE] = 99,
        /* the other values are set to 0 */
 };
+
+/*
+ * Return false if the allocator hint of @device keeps chunks of @type off it,
+ * otherwise set @alloc_hint to the device's tier for @type (higher tiers are
+ * used first).  Mixed block groups cannot separate data and metadata
+ * placement and ignore the hints.
+ *
+ * Both the chunk allocator and the available space estimates use this:
+ * an estimate that counts a device the allocator never uses for that type
+ * promises space no chunk can be made from.
+ */
+static bool btrfs_dev_alloc_hint(const struct btrfs_device *device, u64 type,
+                                int *alloc_hint)
+{
+       int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
+
+       if ((type & BTRFS_BLOCK_GROUP_DATA) &&
+           (type & BTRFS_BLOCK_GROUP_METADATA)) {
+               *alloc_hint = 0;
+               return true;
+       }
+
+       if (type & BTRFS_BLOCK_GROUP_DATA) {
+               /* Data skips METADATA_ONLY and NONE_ONLY devices. */
+               if (hint == BTRFS_DEV_ALLOCATION_METADATA_ONLY ||
+                   hint == BTRFS_DEV_ALLOCATION_NONE_ONLY)
+                       return false;
+               /* Data-preferring devices first. */
+               *alloc_hint = -alloc_hint_map[hint];
+               return true;
+       }
+
+       /* Metadata and system skip DATA_ONLY and NONE_ONLY devices. */
+       if (hint == BTRFS_DEV_ALLOCATION_DATA_ONLY ||
+           hint == BTRFS_DEV_ALLOCATION_NONE_ONLY)
+               return false;
+       /* Metadata-preferring devices first, PREFERRED_NONE last. */
+       if (hint == BTRFS_DEV_ALLOCATION_PREFERRED_NONE)
+               *alloc_hint = -alloc_hint_map[hint];
+       else
+               *alloc_hint = alloc_hint_map[hint];
+       return true;
+}
+
+/*
+ * Return how many of the first @ndevs entries of @devices_info, sorted by
+ * btrfs_cmp_device_info(), a chunk of @type may use: the smallest set of
+ * whole hint tiers that has @devs_min devices, and for fixed-width mirrored
+ * profiles only the first @devs_max of those.
+ *
+ * Fixed-width mirrored profiles consume only the first devs_max candidates.
+ * Keep the highest-priority devices and only as many of the final hint
+ * group as needed; the caller then restores the max_avail ordering required
+ * by the stripe-size calculation.  Check the fixed-width property explicitly
+ * so a future variable-width RAID1 profile retains the whole-group behavior.
+ */
+static int btrfs_select_hint_tiers(const struct btrfs_device_info *devices_info,
+                                  int ndevs, u64 type, int devs_min, int devs_max)
+{
+       int n = 0;
+
+       while (n < ndevs) {
+               int hint = devices_info[n++].alloc_hint;
+
+               while (n < ndevs && devices_info[n].alloc_hint == hint)
+                       n++;
+               if (n >= devs_min)
+                       break;
+       }
+
+       if ((type & BTRFS_BLOCK_GROUP_RAID1_MASK) && devs_min == devs_max)
+               n = min(n, devs_max);
+
+       return n;
+}
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
+/*
+ * Return true if a chunk of @type may be placed on @device: the allocator
+ * hints keep data off METADATA_ONLY and NONE_ONLY devices and metadata and
+ * system chunks off DATA_ONLY and NONE_ONLY devices.  Space estimates that
+ * walk the devices themselves (statfs) must leave the others out.
+ */
+bool btrfs_device_allows_chunk_type(const struct btrfs_device *device, u64 type)
+{
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+       int alloc_hint;
+
+       if (btrfs_fs_incompat(device->fs_info, MIXED_GROUPS))
+               type |= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
+       return btrfs_dev_alloc_hint(device, type, &alloc_hint);
+#else
+       return true;
 #endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+}
 
 const char *btrfs_bg_type_to_raid_name(u64 flags)
 {
@@ -5646,7 +5740,7 @@ static int btrfs_cmp_device_info(const void *a, const void *b)
  */
 static int alloc_virtual_chunk(struct btrfs_fs_info *fs_info,
                               struct btrfs_device_info *devices_info,
-                              enum btrfs_raid_types type,
+                              enum btrfs_raid_types type, u64 chunk_type,
                               u64 *allocated)
 {
        const struct btrfs_raid_attr *raid_attr = &btrfs_raid_array[type];
@@ -5666,6 +5760,13 @@ static int alloc_virtual_chunk(struct btrfs_fs_info *fs_info,
                    test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
                        continue;
 
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+               /* Only the devices the chunk allocator would use for this type. */
+               if (!btrfs_dev_alloc_hint(device, chunk_type,
+                                         &devices_info[ndevs].alloc_hint))
+                       continue;
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
                if (device->total_bytes > device->bytes_used +
                                device->per_profile_allocated)
                        avail = device->total_bytes - device->bytes_used -
@@ -5700,6 +5801,16 @@ static int alloc_virtual_chunk(struct btrfs_fs_info *fs_info,
        }
        sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
             btrfs_cmp_device_info, NULL);
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+       /* The same hint tiers gather_device_info() would pick. */
+       ndevs = btrfs_select_hint_tiers(devices_info, ndevs,
+                                       raid_attr->bg_flag | chunk_type,
+                                       raid_attr->devs_min, raid_attr->devs_max);
+       for (int i = 0; i < ndevs; i++)
+               devices_info[i].alloc_hint = 0;
+       sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
+            btrfs_cmp_device_info, NULL);
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
        ndevs = rounddown(ndevs, raid_attr->devs_increment);
        if (ndevs < raid_attr->devs_min)
                return -ENOSPC;
@@ -5722,7 +5833,7 @@ static int alloc_virtual_chunk(struct btrfs_fs_info *fs_info,
 }
 
 static int calc_one_profile_avail(struct btrfs_fs_info *fs_info,
-                                 enum btrfs_raid_types type,
+                                 enum btrfs_raid_types type, u64 chunk_type,
                                  u64 *result_ret)
 {
        struct btrfs_device_info *devices_info = NULL;
@@ -5750,7 +5861,8 @@ static int calc_one_profile_avail(struct btrfs_fs_info *fs_info,
        list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list)
                device->per_profile_allocated = 0;
 
-       while (!alloc_virtual_chunk(fs_info, devices_info, type, &allocated))
+       while (!alloc_virtual_chunk(fs_info, devices_info, type, chunk_type,
+                                   &allocated))
                result += allocated;
 
 out:
@@ -5761,10 +5873,19 @@ out:
        return 0;
 }
 
-/* Update the per-profile available space array. */
+/*
+ * Update the per-profile available space arrays, one for data and one for
+ * metadata and system chunks: the allocator hints can make the devices a
+ * data chunk may use differ from those a metadata chunk may use.
+ */
 void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info)
 {
-       u64 results[BTRFS_NR_RAID_TYPES];
+       static const u64 chunk_types[BTRFS_NR_PER_PROFILE] = {
+               [BTRFS_PER_PROFILE_DATA] = BTRFS_BLOCK_GROUP_DATA,
+               [BTRFS_PER_PROFILE_METADATA] = BTRFS_BLOCK_GROUP_METADATA,
+       };
+       const bool mixed = btrfs_fs_incompat(fs_info, MIXED_GROUPS);
+       u64 results[BTRFS_NR_PER_PROFILE][BTRFS_NR_RAID_TYPES];
        int ret;
 
        /*
@@ -5774,21 +5895,33 @@ void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info)
        if (btrfs_is_zoned(fs_info))
                goto error;
 
-       for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
-               ret = calc_one_profile_avail(fs_info, i, &results[i]);
-               if (ret < 0)
-                       goto error;
+       for (int k = 0; k < BTRFS_NR_PER_PROFILE; k++) {
+               u64 chunk_type = chunk_types[k];
+
+               if (mixed)
+                       chunk_type = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
+               for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
+                       /* Without hints every type can use every device. */
+                       if (k > 0 && !IS_ENABLED(CONFIG_BTRFS_ALLOCATOR_HINTS)) {
+                               results[k][i] = results[0][i];
+                               continue;
+                       }
+                       ret = calc_one_profile_avail(fs_info, i, chunk_type,
+                                                    &results[k][i]);
+                       if (ret < 0)
+                               goto error;
+               }
        }
 
        spin_lock(&fs_info->fs_devices->per_profile_lock);
-       for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
-               fs_info->fs_devices->per_profile_avail[i] = results[i];
+       memcpy(fs_info->fs_devices->per_profile_avail, results, sizeof(results));
        spin_unlock(&fs_info->fs_devices->per_profile_lock);
        return;
 error:
        spin_lock(&fs_info->fs_devices->per_profile_lock);
-       for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
-               fs_info->fs_devices->per_profile_avail[i] = U64_MAX;
+       for (int k = 0; k < BTRFS_NR_PER_PROFILE; k++)
+               for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
+                       fs_info->fs_devices->per_profile_avail[k][i] = U64_MAX;
        spin_unlock(&fs_info->fs_devices->per_profile_lock);
 }
 
@@ -5991,56 +6124,9 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
                devices_info[ndevs].dev = device;
 
 #ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
-               if ((ctl->type & BTRFS_BLOCK_GROUP_DATA) &&
-                    (ctl->type & BTRFS_BLOCK_GROUP_METADATA)) {
-                       /*
-                        * Mixed block groups cannot separate data and metadata
-                        * placement. Ignore all hints and retain normal free-space
-                        * ordering.
-                        */
-                       devices_info[ndevs].alloc_hint = 0;
-               } else if (ctl->type & BTRFS_BLOCK_GROUP_DATA) {
-                       int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
-
-                       /*
-                        * skip BTRFS_DEV_METADATA_ONLY disks
-                        */
-                       if (BTRFS_DEV_ALLOCATION_METADATA_ONLY == hint)
-                               continue;
-                       /*
-                        * skip BTRFS_DEV_NONE_ONLY disks
-                        */
-                       if (BTRFS_DEV_ALLOCATION_NONE_ONLY == hint)
-                               continue;
-                       /*
-                        * if a data chunk must be allocated,
-                        * sort also by hint (data disk
-                        * higher priority)
-                        */
-                       devices_info[ndevs].alloc_hint = -alloc_hint_map[hint];
-               } else { /* BTRFS_BLOCK_GROUP_METADATA */
-                       int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
-
-                       /*
-                        * skip BTRFS_DEV_DATA_ONLY disks
-                        */
-                       if (BTRFS_DEV_ALLOCATION_DATA_ONLY == hint)
-                               continue;
-                       /*
-                        * skip BTRFS_DEV_NONE_ONLY disks
-                        */
-                       if (BTRFS_DEV_ALLOCATION_NONE_ONLY == hint)
-                               continue;
-                       /*
-                        * if a metadata chunk must be allocated,
-                        * sort also by hint (metadata hint
-                        * higher priority)
-                        */
-                       if (BTRFS_DEV_ALLOCATION_PREFERRED_NONE == hint)
-                               devices_info[ndevs].alloc_hint = -alloc_hint_map[hint];
-                       else
-                               devices_info[ndevs].alloc_hint = alloc_hint_map[hint];
-               }
+               if (!btrfs_dev_alloc_hint(device, ctl->type,
+                                         &devices_info[ndevs].alloc_hint))
+                       continue;
 #endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
 
                ++ndevs;
@@ -6066,28 +6152,8 @@ static int gather_device_info(struct btrfs_fs_devices *fs_devices,
         * select the minimum set of disks grouped by hint that
         * can host the chunk
         */
-       ndevs = 0;
-       while (ndevs < ctl->ndevs) {
-               int hint = devices_info[ndevs++].alloc_hint;
-               while (ndevs < ctl->ndevs &&
-                      devices_info[ndevs].alloc_hint == hint)
-                               ndevs++;
-               if (ndevs >= ctl->devs_min)
-                       break;
-       }
-
-       /*
-        * Fixed-width mirrored profiles consume only the first devs_max
-        * candidates. Keep the highest-priority devices and only as many of
-        * the final hint group as needed. The sort below then restores the
-        * max_avail ordering required by the stripe-size calculation.
-        *
-        * Check the fixed-width property explicitly so a future variable-width
-        * RAID1 profile retains the existing whole-group behavior.
-        */
-       if ((ctl->type & BTRFS_BLOCK_GROUP_RAID1_MASK) &&
-           ctl->devs_min == ctl->devs_max)
-               ndevs = min(ndevs, ctl->devs_max);
+       ndevs = btrfs_select_hint_tiers(devices_info, ctl->ndevs, ctl->type,
+                                       ctl->devs_min, ctl->devs_max);
 
        BUG_ON(ndevs > ctl->ndevs);
        ctl->ndevs = ndevs;
index 181b8a4ea14b3dde9ea611e2349498e335b18ff7..f8b15fc91f9ca4a5a9b43629f3f989f1936ebc33 100644 (file)
@@ -337,6 +337,13 @@ enum btrfs_read_policy {
        BTRFS_NR_READ_POLICY,
 };
 
+/* Index of btrfs_fs_devices::per_profile_avail. */
+enum {
+       BTRFS_PER_PROFILE_DATA,
+       BTRFS_PER_PROFILE_METADATA,     /* and system */
+       BTRFS_NR_PER_PROFILE
+};
+
 struct btrfs_fs_devices {
        u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
 
@@ -466,12 +473,14 @@ struct btrfs_fs_devices {
 #endif
 
        /*
-        * Each value indicates the available space for that profile.
+        * Each value indicates the available space for that profile, for data
+        * chunks and for metadata (and system) chunks, which the allocator
+        * hints can place on different devices.
         * U64_MAX means the estimation is unavailable.
         *
         * Protected by per_profile_lock;
         */
-       u64 per_profile_avail[BTRFS_NR_RAID_TYPES];
+       u64 per_profile_avail[BTRFS_NR_PER_PROFILE][BTRFS_NR_RAID_TYPES];
        spinlock_t per_profile_lock;
 };
 
@@ -926,18 +935,26 @@ const char *btrfs_bg_type_to_raid_name(u64 flags);
 int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
 bool btrfs_verify_dev_items(const struct btrfs_fs_info *fs_info);
 void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info);
+bool btrfs_device_allows_chunk_type(const struct btrfs_device *device, u64 type);
 
+/*
+ * @type is the block group type the space is for (DATA, METADATA or SYSTEM);
+ * @profile only selects the RAID profile.
+ */
 static inline bool btrfs_get_per_profile_avail(struct btrfs_fs_info *fs_info,
-                                              u64 profile, u64 *avail_ret)
+                                              u64 type, u64 profile,
+                                              u64 *avail_ret)
 {
        enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(profile);
        struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
+       int k = (type & BTRFS_BLOCK_GROUP_DATA) ? BTRFS_PER_PROFILE_DATA :
+                                                 BTRFS_PER_PROFILE_METADATA;
        bool uptodate = false;
 
        spin_lock(&fs_devices->per_profile_lock);
-       if (fs_devices->per_profile_avail[index] != U64_MAX) {
+       if (fs_devices->per_profile_avail[k][index] != U64_MAX) {
                uptodate = true;
-               *avail_ret = fs_devices->per_profile_avail[index];
+               *avail_ret = fs_devices->per_profile_avail[k][index];
        }
        spin_unlock(&fs_info->fs_devices->per_profile_lock);
        return uptodate;