]> 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:11 +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 71d7d4b12dc6e0253f0f59fb0a45917bbfc5c69d..5d0a95986be187333781eeba04f273f6d69095d7 100644 (file)
@@ -103,10 +103,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 8800918bf9f104cfc28139c7332292e21f80ed87..acce88f91d41255a345e3d6fb9c32f60771fad3a 100644 (file)
@@ -967,6 +967,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 ea8fcde5cde0e5370e35f123e2fe2d5a2d8e4d9e..80dc62c34003da9bb994f5ccf670a5a7d2d33c39 100644 (file)
@@ -2204,6 +2204,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);
@@ -2213,6 +2224,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);