Skip to content

Let btrfs reclaim partly used data chunks so metadata cannot run out of room - #17399

Open
patcito wants to merge 1 commit into
omacom:quattrofrom
patcito:btrfs-data-reclaim
Open

patcito wants to merge 1 commit into
omacom:quattrofrom
patcito:btrfs-data-reclaim

Conversation

@patcito

@patcito patcito commented Nov 2, 2026

Copy link
Copy Markdown

Refs #15528 (implements its "Possible fixes" option 1)

Problem

On a btrfs root, data chunks can claim the whole device over time. Once nothing is left unallocated, metadata cannot grow, and writes fail with ENOSPC while df still shows plenty of free space. Apps stop starting and the desktop stalls.

Hit here on Omarchy with kernel 7.2.5 and a 475 GiB disk: df showed 28 GiB free, Device unallocated was 1 MiB and metadata was at 31.93 of 32.45 GiB. btrfs balance start -dusage=0 and -dusage=5 both relocated 0 of 446 chunks, because a balance needs an unallocated chunk to move data into. It took deleting a 24 GiB file before -dusage=10 could relocate 20 chunks and give metadata room again. #15528 and #6553 report the same state.

Omarchy configures no reclaim of partly used data block groups, and the kernel leaves it off by default.

Fix

  • etc/tmpfiles.d/omarchy-btrfs-reclaim.conf sets dynamic_reclaim and periodic_reclaim to 1 under /sys/fs/btrfs/*/allocation/data/ at every boot. The kernel then compacts partly used data block groups by itself, mostly when unallocated space runs low, so it does little work on a healthy disk.
  • A migration applies the drop-in at once, so a machine already near the limit does not wait for a reboot. It prompts for sudo only when a setting exists and is not already 1, so it does nothing on non-btrfs machines and for a second user.

This complements #15590, which makes the update guard detect the state, rather than replacing it.

Testing

  • systemd-tmpfiles --create on a test copy (systemd 262): writes every glob match, creates nothing in a directory without the file, and exits 0 when the glob matches nothing.
  • systemd-tmpfiles --dry-run --create on the drop-in against a real btrfs root resolves both globs to the root filesystem's settings.
  • The migration with HOME=$(mktemp -d) bash -euo pipefail exits 0 without prompting when both settings are already 1.
  • Running with both settings on after recovery: dynamic_reclaim = 1, periodic_reclaim = 1.

@greptile-apps

greptile-apps Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Enables Btrfs data reclaim through a boot-time tmpfiles drop-in.

  • Btrfs reclaims partly used data chunks automatically.

Reviews (1) · Last reviewed commit: "Let btrfs reclaim partly used data chunk..." · Reviewed by Greptile

@swaits

swaits commented Nov 25, 2026

Copy link
Copy Markdown

Another incident in the field, plus a confirmation that this PR's settings work on the current Omarchy kernel.

Machine: Framework 13 (AMD), Omarchy default layout (LUKS + btrfs root), 3.7 TiB NVMe at 84% full, linux-omarchy 7.2.15-4. The disk sees heavy small-file churn: many git worktrees with their own Rust target/ dirs, written and deleted constantly.

State when it hit:

Data      total 3669 GiB, used 3039 GiB   (~630 GiB free inside data chunks)
Metadata  total   27 GiB, used   26 GiB
Device unallocated: 1 MiB

What it looked like: this machine never reported ENOSPC. The whole desktop froze instead, browser included. Load average was ~120, but CPU, IO and memory pressure (PSI) were all near zero, so btop showed nothing hot. 110 threads were in D state, with writers blocked in handle_reserve_ticket, and one events_unbound kworker was pinned at ~95% CPU trying to reclaim metadata space. A user has no way to connect "everything is frozen and btop looks idle" to "the filesystem has no unallocated chunks".

Recovery: btrfs balance start -dusage=0 /, then -dusage=5, then -dusage=15. Unallocated went from 1 MiB to ~107 GiB, D-state threads dropped from 110 to 1, and the desktop recovered within minutes. Unlike the case in the PR description, -dusage=0 had empty chunks to remove here, which gave the later passes room to work.

This PR's settings: I then set dynamic_reclaim=1 and periodic_reclaim=1 on the data block groups through a tmpfiles w drop-in, the same mechanism as this PR (with the filesystem's UUID rather than the glob). Both read back 1 on 7.2.15-4.

I agree with keeping metadata out of it. I had also tried btrfsmaintenance's weekly balance, whose default config balances metadata (BTRFS_BALANCE_MUSAGE="5"). That conflicts with the advice cited in #15528, so I've cleared it on my machine. The kernel-side data-only reclaim in this PR looks like the right default. Hoping it gets a review. Refs #15528.

@chessing234

Copy link
Copy Markdown
Contributor

@dhh @omarchybot the data-chunk reclaim tmpfiles entry and migration from this PR are in #21027, unchanged in intent, next to the update-time check in #15590. Reclaim is what makes new metadata chunks possible. The update stop is what happens when unallocated space and metadata headroom are already under 1 GiB.

Please consider closing this as superseded only after #21027 is accepted. Leaving the close to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants