]> git.hungrycats.org Git - linux/commitdiff
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)
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

index 21b4694ce987807fb55c938a31b900edc75710fd..fb1c2b9312ec5f274535279513f7d445887937a0 100644 (file)
@@ -101,10 +101,10 @@ config BTRFS_ALLOCATOR_HINTS
          They cannot separate the two chunk classes and are outside the
          tiering use case targeted by this feature.
 
-         Device replace does not copy the allocation hint to the target
-         device. Reapply the hint after replacing a device. Hint updates are
-         also not serialized with chunk allocation, so a concurrent new chunk
-         may use either the old or the new setting.
+         Device replace copies the allocation hint to the target device;
+         the target's hint cannot be changed until the replace finishes.
+         Hint updates are not serialized with chunk allocation, so a
+         concurrent new chunk may use either the old or the new setting.
 
          You are advised to watch your free space closely with btrfs tools
          instead of relying on df only.
index 0284be0e4e826d601053e8be9839a533e2cd09cb..7576b5d21d1d0b93adac12c589ae1685ca53481f 100644 (file)
@@ -986,6 +986,12 @@ error:
                          btrfs_dev_name(tgt_device));
        clear_bit(BTRFS_DEV_STATE_REPLACE_TGT, &tgt_device->dev_state);
        tgt_device->devid = src_device->devid;
+       /*
+        * The target inherits the source's dev item, including the type
+        * field (allocation hints).  Copy it, or the next btrfs_update_device()
+        * on the target writes type 0 over it.
+        */
+       tgt_device->type = src_device->type;
        src_device->devid = BTRFS_DEV_REPLACE_DEVID;
        memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
        memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
index 1c56220b86d93b3a9c9e2f5232f5db39cc02a4c9..52e6afe36650c3641c6dc33d42e0b6ffc002f0f6 100644 (file)
@@ -2164,6 +2164,17 @@ static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
        if (!btrfs_dev_allocation_hint_valid(type))
                return -EINVAL;
 
+       /*
+        * A replace target has no dev item of its own until the replace
+        * finishes and it takes over the source's dev item and type.  After
+        * the finish, the source has the replace devid and no dev item.  The
+        * source's type may change during the replace: the target inherits
+        * it at the finish.
+        */
+       if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state) ||
+           device->devid == BTRFS_DEV_REPLACE_DEVID)
+               return -EBUSY;
+
        trans = btrfs_start_transaction(root, 1);
        if (IS_ERR(trans))
                return PTR_ERR(trans);
@@ -2173,6 +2184,17 @@ static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
 
        ret = btrfs_update_device(trans, device);
 
+       /*
+        * No dev item: the device is being removed, or lost its devid to a
+        * replace that finished after the checks above.  Nothing was changed,
+        * so there is nothing to abort.
+        */
+       if (ret == -ENOENT) {
+               btrfs_end_transaction(trans);
+               ret = -ENODEV;
+               goto abort;
+       }
+
        if (ret < 0) {
                btrfs_abort_transaction(trans, ret);
                btrfs_end_transaction(trans);