btrfs: mount a single device filesystem with a device replace in progress
The target of a device replace has devid BTRFS_DEV_REPLACE_DEVID (0)
until the replace finishes, and it is not counted in num_devices. On a
single device filesystem, the superblocks of both the source and the
target say num_devices == 1, and since v6.7 two changes for single
device filesystems keep such a pair from being assembled:
- btrfs_skip_registration() does not register a scanned device whose
superblock says num_devices == 1, so a device scan skips the target,
and only the device given to mount is registered.
- find_fsid_by_device() gives a device whose superblock says
num_devices == 1 a temporary fsid when another device with the same
fsid is already registered, as it takes it for a clone. So even when
the target is registered, the source given to mount is put in a
separate btrfs_fs_devices with a temp-fsid.
When a replace was in progress, e.g. suspended by an unmount, the mount
then fails:
BTRFS warning (device ...): cannot mount because device replace operation is ongoing and
BTRFS warning (device ...): tgtdev (devid 0) is missing, need to run 'btrfs dev scan'?
BTRFS error (device ...): failed to init dev_replace: -EIO
and running 'btrfs device scan' as suggested does not help.
Register a scanned device whose superblock has the replace target devid,
and do not give a temp-fsid to a device of a replace source and target
pair, i.e. when either the new device or a device already registered
with that fsid is the replace target. The filesystem can then be
mounted from the source device after the target has been scanned, as one
with more devices can, and the replace resumes. The source superblock
gives no hint of the replace, so it is still not registered by a scan,
and mounting from the target device still fails.
A stale target superblock is now registered too. The mount then fails
with "replace without active item, run 'device scan --forget' on the
target device", as it already does on a filesystem with more devices.
That needs a target superblock that was not overwritten when its
replace ended, e.g. after a crash at that point.
Fixes: bc27d6f0aa0e4 ("btrfs: scan but don't register device on single device filesystem") Fixes: a5b8a5f9f8355 ("btrfs: support cloned-device mount capability") Assisted-by: Claude:claude-opus-5-5