]> git.hungrycats.org Git - linux/commit
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)
commit6a353324411183a02f61928cb9168e7cb04d66f1
tree1c0f7178e65388ef9f35fc6896c778383461ce19
parentb08ec73e721bfc56c3c64e2bc22c26054ec8af3a
btrfs: allocator hints: make the available space estimates honor the hints

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