]> git.hungrycats.org Git - linux/commitdiff
btrfs: change the DEV_ITEM 'type' field via sysfs
authorKai Krakow <kai@kaishome.de>
Fri, 10 Jul 2026 22:25:21 +0000 (00:25 +0200)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Mon, 2 Nov 2026 02:06:49 +0000 (21:06 -0500)
Known limitations:

- Device replace does not copy the allocation hint to the target
  device. Users must reapply it after replacement.
- The sysfs update is not serialized with chunk allocation through
  chunk_mutex, so an allocation concurrent with an update may observe
  either value.

A future revision should replace the type field with dedicated
transactional structures. Transactions should not be triggered directly
from sysfs writes, see:

https://lore.kernel.org/linux-btrfs/20251213200920.1808679-1-kai@kaishome.de/

v2:
- Adds a check to prevent modification while the file system is still
  mounting.

v3:
- Accept only valid allocation hint types

v4:
- Document the device replace and chunk allocation synchronization
  limitations of the current type-based interface.

Link: https://github.com/kakra/linux/pull/36#issuecomment-3406301805
Reported-by: Eli Venter <eli@genedx.com>
Co-authored-by: Goffredo Baroncelli <kreijack@inwind.it>
Signed-off-by: Kai Krakow <kai@kaishome.de>
fs/btrfs/sysfs.c
fs/btrfs/volumes.c
fs/btrfs/volumes.h

index 83b2464cd4ecefb65c49b1ca5208d1984220a63e..36db4c227f897e8d447cdf9c7243ff68b46a05e7 100644 (file)
@@ -2140,6 +2140,22 @@ static ssize_t btrfs_devinfo_error_stats_show(struct kobject *kobj,
 BTRFS_ATTR(devid, error_stats, btrfs_devinfo_error_stats_show);
 
 #ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+static bool btrfs_dev_allocation_hint_valid(u64 type)
+{
+       if (type & ~((1ULL << BTRFS_DEV_ALLOCATION_MASK_BIT_COUNT) - 1))
+               return false;
+
+       switch (type) {
+       case BTRFS_DEV_ALLOCATION_PREFERRED_DATA:
+       case BTRFS_DEV_ALLOCATION_PREFERRED_METADATA:
+       case BTRFS_DEV_ALLOCATION_METADATA_ONLY:
+       case BTRFS_DEV_ALLOCATION_DATA_ONLY:
+               return true;
+       default:
+               return false;
+       }
+}
+
 static ssize_t btrfs_devinfo_type_show(struct kobject *kobj,
                                       struct kobj_attribute *a, char *buf)
 {
@@ -2148,7 +2164,69 @@ static ssize_t btrfs_devinfo_type_show(struct kobject *kobj,
 
        return scnprintf(buf, PAGE_SIZE, "0x%016llx\n", device->type);
 }
-BTRFS_ATTR(devid, type, btrfs_devinfo_type_show);
+
+static ssize_t btrfs_devinfo_type_store(struct kobject *kobj,
+                                       struct kobj_attribute *a,
+                                       const char *buf, size_t len)
+{
+       struct btrfs_fs_info *fs_info;
+       struct btrfs_root *root;
+       struct btrfs_device *device;
+       int ret;
+       struct btrfs_trans_handle *trans;
+
+       u64 type, prev_type;
+
+       device = container_of(kobj, struct btrfs_device, devid_kobj);
+       fs_info = device->fs_info;
+       if (!fs_info)
+               return -EPERM;
+
+       /*
+        * Changing the type field requires starting a transaction which will cause a NULL dereference in
+        * __reserve_bytes if the file system is not fully open. Thus, return EBUSY if the file system is not fully
+        * initialized.
+        */
+       if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
+               return -EBUSY;
+
+       root = fs_info->chunk_root;
+       if (sb_rdonly(fs_info->sb))
+               return -EROFS;
+
+       ret = kstrtou64(buf, 0, &type);
+       if (ret < 0)
+               return -EINVAL;
+
+       /* for now, only allow defined allocation hint values */
+       if (!btrfs_dev_allocation_hint_valid(type))
+               return -EINVAL;
+
+       trans = btrfs_start_transaction(root, 1);
+       if (IS_ERR(trans))
+               return PTR_ERR(trans);
+
+       prev_type = device->type;
+       device->type = type;
+
+       ret = btrfs_update_device(trans, device);
+
+       if (ret < 0) {
+               btrfs_abort_transaction(trans, ret);
+               btrfs_end_transaction(trans);
+               goto abort;
+       }
+
+       ret = btrfs_commit_transaction(trans);
+       if (ret < 0)
+               goto abort;
+
+       return len;
+abort:
+       device->type = prev_type;
+       return ret;
+}
+BTRFS_ATTR_RW(devid, type, btrfs_devinfo_type_show, btrfs_devinfo_type_store);
 #endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
 
 /*
index ca6fc702d94bc9c886af91fef2f97223e8ba5a89..80a82e03aba3eeb4dac63a5f2e7e71fe3a0cf95e 100644 (file)
@@ -3056,8 +3056,8 @@ error:
        return ret;
 }
 
-static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
-                                       struct btrfs_device *device)
+noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
+                                struct btrfs_device *device)
 {
        int ret;
        BTRFS_PATH_AUTO_FREE(path);
index 4678f8870ed92faae782552b863586aa4082ffba..f63251a0dea8138b6b03bcbb8f05695467fca1ec 100644 (file)
@@ -924,6 +924,8 @@ static inline bool btrfs_get_per_profile_avail(struct btrfs_fs_info *fs_info,
 }
 
 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
+int btrfs_update_device(struct btrfs_trans_handle *trans,
+                       struct btrfs_device *device);
 
 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
 const u8 *btrfs_sb_fsid_ptr(const struct btrfs_super_block *sb);