They cannot separate the two chunk classes and are outside the
tiering use case targeted by this feature.
+ A hint change applies to new chunks at once and is written to the
+ device item at the next transaction commit (sync to force it).
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.
* on the target writes type 0 over it.
*/
tgt_device->type = src_device->type;
+ /* A hint change not yet committed goes to the target's dev item. */
+ if (test_and_clear_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &src_device->dev_state))
+ set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &tgt_device->dev_state);
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));
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;
+ u64 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;
+ /* The dev item is written by the next transaction commit. */
+ if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
+ return -EBUSY;
+
ret = kstrtou64(buf, 0, &type);
if (ret < 0)
return -EINVAL;
if (!btrfs_dev_allocation_hint_valid(type))
return -EINVAL;
+ /* Seed devices are read-only and not on fs_devices->devices. */
+ if (device->fs_devices != fs_info->fs_devices)
+ return -EROFS;
+
/*
- * 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.
+ * No transaction here. Device remove drains this attribute while it
+ * holds a transaction handle and device_list_mutex, so a commit (which
+ * waits for that handle) or a join (which can wait for
+ * device_list_mutex when it creates pending block groups) from inside
+ * the store can deadlock against it. Set the hint and mark the dev
+ * item dirty; btrfs_update_dirty_device_types() writes it at the next
+ * commit, as the label and feature stores do with the super block.
+ *
+ * chunk_mutex serializes with chunk allocation, which reads the hint,
+ * and with the end of a device replace, which moves the hint and the
+ * dirty bit to the target. A replace target has no dev item of its
+ * own until then.
*/
+ mutex_lock(&fs_info->chunk_mutex);
if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state) ||
- device->devid == BTRFS_DEV_REPLACE_DEVID)
+ device->devid == BTRFS_DEV_REPLACE_DEVID) {
+ mutex_unlock(&fs_info->chunk_mutex);
return -EBUSY;
-
- 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);
-
+ }
+ WRITE_ONCE(device->type, type);
/*
- * 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.
+ * The commit reads the type after it sees the bit, without
+ * chunk_mutex: order the store before the bit.
*/
- if (ret == -ENOENT) {
- btrfs_end_transaction(trans);
- ret = -ENODEV;
- goto abort;
- }
+ smp_mb__before_atomic();
+ set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state);
+ mutex_unlock(&fs_info->chunk_mutex);
- if (ret < 0) {
- btrfs_abort_transaction(trans, ret);
- btrfs_end_transaction(trans);
- goto abort;
- }
-
- ret = btrfs_commit_transaction(trans);
- if (ret < 0)
- goto abort;
+ /* We don't want to do full transaction commit from inside sysfs */
+ set_bit(BTRFS_FS_NEED_TRANS_COMMIT, &fs_info->flags);
+ wake_up_process(fs_info->transaction_kthread);
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 */
goto lockdep_trans_commit_start_release;
}
+ /* Allocation hints changed through sysfs, see btrfs_devinfo_type_store() */
+ btrfs_update_dirty_device_types(trans);
+
btrfs_create_pending_block_groups(trans);
if (!test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &cur_trans->flags)) {
dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
btrfs_set_device_id(leaf, dev_item, device->devid);
- btrfs_set_device_type(leaf, dev_item, device->type);
+ btrfs_set_device_type(leaf, dev_item, READ_ONCE(device->type));
btrfs_set_device_io_align(leaf, dev_item, device->io_align);
btrfs_set_device_io_width(leaf, dev_item, device->io_width);
btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
return ret;
}
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+/*
+ * Write the dev items whose allocation hint was changed through sysfs (see
+ * btrfs_devinfo_type_store()). Called at the start of a transaction commit.
+ * A hint that cannot be written stays dirty and is retried at the next
+ * commit: it is not worth aborting the transaction for.
+ */
+void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans)
+{
+ struct btrfs_fs_info *fs_info = trans->fs_info;
+ struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
+ struct btrfs_device *device;
+ unsigned int dirty = 0;
+ int failed = 0;
+ int ret;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(device, &fs_devices->devices, dev_list)
+ if (test_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state))
+ dirty++;
+ rcu_read_unlock();
+ if (!dirty)
+ return;
+
+ /*
+ * Reserve the chunk tree space before device_list_mutex: the
+ * reservation takes chunk_mutex and may allocate a system chunk.
+ */
+ while (dirty--)
+ btrfs_reserve_chunk_metadata(trans, false);
+
+ mutex_lock(&fs_devices->device_list_mutex);
+ list_for_each_entry(device, &fs_devices->devices, dev_list) {
+ if (!test_and_clear_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state))
+ continue;
+ ret = btrfs_update_device(trans, device);
+ /* Being removed: its dev item is already gone, and the hint with it. */
+ if (ret == -ENOENT)
+ continue;
+ if (ret) {
+ set_bit(BTRFS_DEV_STATE_TYPE_DIRTY, &device->dev_state);
+ failed = ret;
+ }
+ }
+ mutex_unlock(&fs_devices->device_list_mutex);
+
+ btrfs_trans_release_chunk_metadata(trans);
+
+ if (failed)
+ btrfs_warn_rl(fs_info,
+ "failed to write the allocation hint to a dev item: %d, retrying at the next commit",
+ failed);
+}
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index)
{
btrfs_dev_stat_inc(dev, index);
#define BTRFS_DEV_STATE_REPLACE_TGT (3)
#define BTRFS_DEV_STATE_FLUSH_SENT (4)
#define BTRFS_DEV_STATE_NO_READA (5)
+/* dev item needs writing at the next commit (allocation hint changed) */
+#define BTRFS_DEV_STATE_TYPE_DIRTY (6)
/* Special value encoding failure to write primary super block. */
#define BTRFS_SUPER_PRIMARY_WRITE_ERROR (INT_MAX / 2)
int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans);
+#else
+static inline void btrfs_update_dirty_device_types(struct btrfs_trans_handle *trans) { }
+#endif
void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
void btrfs_destroy_dev_replace_tgtdev(struct btrfs_device *tgtdev);