]> git.hungrycats.org Git - linux/commit
btrfs: allocator hints: keep the hint across device replace
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 4 Oct 2026 04:44:58 +0000 (00:44 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 12 Nov 2026 15:26:22 +0000 (10:26 -0500)
commit0c8ce232991b1c8a7b4e3d8feab52d2336ed0082
treea98f5b6e750872667b16c77f883a8a3c4128ccc9
parent0870d507ce29f3f70c419843d23c136f62996805
btrfs: allocator hints: keep the hint across device replace

Device replace drops the allocation hint, and changing a hint during a
replace can abort the transaction.

The replace target takes over the source's dev item when the replace
finishes, but not the in-memory device->type.  The on-disk item keeps the
hint only until the next btrfs_update_device() on the new device (any
chunk allocation on it), which writes type 0 over it.  Copy the type
together with the devid, under the same locks.

During a replace, the target is visible as devinfo/0 and has no dev item.
After the finish, the source's devid is BTRFS_DEV_REPLACE_DEVID until its
sysfs directory goes away.  A write to 'type' on either device makes
btrfs_update_device() miss the dev item (-ENOENT), and the store aborts
the transaction, forcing the filesystem read-only in the middle of the
replace.  Device remove has the same window between deleting the dev
item and removing the sysfs directory.

Return -EBUSY for the replace target and for a device with the replace
devid.  An -ENOENT that still gets past these checks (remove, or a
replace that finishes after them) ends the transaction without aborting
it and returns -ENODEV.

The source's hint can still be changed during a replace, and the target
inherits it at the finish, so a replace can move to a device of a
different type, e.g. SSD to HDD.  A write that races the finish can be
lost: it misses the dev item and returns -ENODEV, or the finish has
already copied the old type.  That is tolerable, and the next patch
closes the window.

Assisted-by: Claude:claude-opus-5-5
fs/btrfs/Kconfig
fs/btrfs/dev-replace.c
fs/btrfs/sysfs.c