]> git.hungrycats.org Git - linux/commitdiff
btrfs: mount a single device filesystem with a device replace in progress
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Thu, 29 Oct 2026 16:17:21 +0000 (12:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:17:14 +0000 (21:17 -0500)
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
fs/btrfs/volumes.c

index 3d4ef6725de0e7e4e3894eae51d74d73ab3f82bf..456becaa77cf05bf244c242417a2caad6676b59f 100644 (file)
@@ -588,6 +588,27 @@ static int btrfs_free_stale_devices(dev_t devt, struct btrfs_device *skip_device
        return ret;
 }
 
+/*
+ * On a single device filesystem, the source and the target of a device
+ * replace share the fsid and both say num_devices == 1, but they are one
+ * filesystem, not clones.  The target has devid BTRFS_DEV_REPLACE_DEVID.
+ */
+static bool is_dev_replace_pair(struct btrfs_fs_devices *fs_devices,
+                               const struct btrfs_super_block *disk_super)
+{
+       struct btrfs_device *device;
+
+       if (btrfs_stack_device_id(&disk_super->dev_item) == BTRFS_DEV_REPLACE_DEVID)
+               return true;
+
+       list_for_each_entry(device, &fs_devices->devices, dev_list) {
+               if (device->devid == BTRFS_DEV_REPLACE_DEVID)
+                       return true;
+       }
+
+       return false;
+}
+
 static struct btrfs_fs_devices *find_fsid_by_device(
                                        struct btrfs_super_block *disk_super,
                                        dev_t devt, bool *same_fsid_diff_dev)
@@ -646,6 +667,8 @@ static struct btrfs_fs_devices *find_fsid_by_device(
                /* New device. */
                if (fsid_fs_devices == NULL) {
                        return NULL;
+               } else if (is_dev_replace_pair(fsid_fs_devices, disk_super)) {
+                       return fsid_fs_devices;
                } else {
                        /* sb::fsid is already used create a new temp_fsid. */
                        *same_fsid_diff_dev = true;
@@ -1462,8 +1485,15 @@ static bool btrfs_skip_registration(struct btrfs_super_block *disk_super,
                mutex_unlock(&fs_devices->device_list_mutex);
        }
 
+       /*
+        * The target of a device replace is not counted in num_devices, so
+        * the source and the target of a replace on a single device filesystem
+        * both say num_devices == 1.  The target must be registered, or the
+        * filesystem cannot be mounted while the replace is in progress.
+        */
        if (!mount_arg_dev && btrfs_super_num_devices(disk_super) == 1 &&
-           !(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING))
+           !(btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) &&
+           btrfs_stack_device_id(&disk_super->dev_item) != BTRFS_DEV_REPLACE_DEVID)
                return true;
 
        return false;