[BTRFS_DEV_ALLOCATION_PREFERRED_NONE] = 99,
/* the other values are set to 0 */
};
+
+/*
+ * Return false if the allocator hint of @device keeps chunks of @type off it,
+ * otherwise set @alloc_hint to the device's tier for @type (higher tiers are
+ * used first). Mixed block groups cannot separate data and metadata
+ * placement and ignore the hints.
+ *
+ * Both the chunk allocator and the available space estimates use this:
+ * an estimate that counts a device the allocator never uses for that type
+ * promises space no chunk can be made from.
+ */
+static bool btrfs_dev_alloc_hint(const struct btrfs_device *device, u64 type,
+ int *alloc_hint)
+{
+ int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
+
+ if ((type & BTRFS_BLOCK_GROUP_DATA) &&
+ (type & BTRFS_BLOCK_GROUP_METADATA)) {
+ *alloc_hint = 0;
+ return true;
+ }
+
+ if (type & BTRFS_BLOCK_GROUP_DATA) {
+ /* Data skips METADATA_ONLY and NONE_ONLY devices. */
+ if (hint == BTRFS_DEV_ALLOCATION_METADATA_ONLY ||
+ hint == BTRFS_DEV_ALLOCATION_NONE_ONLY)
+ return false;
+ /* Data-preferring devices first. */
+ *alloc_hint = -alloc_hint_map[hint];
+ return true;
+ }
+
+ /* Metadata and system skip DATA_ONLY and NONE_ONLY devices. */
+ if (hint == BTRFS_DEV_ALLOCATION_DATA_ONLY ||
+ hint == BTRFS_DEV_ALLOCATION_NONE_ONLY)
+ return false;
+ /* Metadata-preferring devices first, PREFERRED_NONE last. */
+ if (hint == BTRFS_DEV_ALLOCATION_PREFERRED_NONE)
+ *alloc_hint = -alloc_hint_map[hint];
+ else
+ *alloc_hint = alloc_hint_map[hint];
+ return true;
+}
+
+/*
+ * Return how many of the first @ndevs entries of @devices_info, sorted by
+ * btrfs_cmp_device_info(), a chunk of @type may use: the smallest set of
+ * whole hint tiers that has @devs_min devices, and for fixed-width mirrored
+ * profiles only the first @devs_max of those.
+ *
+ * Fixed-width mirrored profiles consume only the first devs_max candidates.
+ * Keep the highest-priority devices and only as many of the final hint
+ * group as needed; the caller then restores the max_avail ordering required
+ * by the stripe-size calculation. Check the fixed-width property explicitly
+ * so a future variable-width RAID1 profile retains the whole-group behavior.
+ */
+static int btrfs_select_hint_tiers(const struct btrfs_device_info *devices_info,
+ int ndevs, u64 type, int devs_min, int devs_max)
+{
+ int n = 0;
+
+ while (n < ndevs) {
+ int hint = devices_info[n++].alloc_hint;
+
+ while (n < ndevs && devices_info[n].alloc_hint == hint)
+ n++;
+ if (n >= devs_min)
+ break;
+ }
+
+ if ((type & BTRFS_BLOCK_GROUP_RAID1_MASK) && devs_min == devs_max)
+ n = min(n, devs_max);
+
+ return n;
+}
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
+/*
+ * Return true if a chunk of @type may be placed on @device: the allocator
+ * hints keep data off METADATA_ONLY and NONE_ONLY devices and metadata and
+ * system chunks off DATA_ONLY and NONE_ONLY devices. Space estimates that
+ * walk the devices themselves (statfs) must leave the others out.
+ */
+bool btrfs_device_allows_chunk_type(const struct btrfs_device *device, u64 type)
+{
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+ int alloc_hint;
+
+ if (btrfs_fs_incompat(device->fs_info, MIXED_GROUPS))
+ type |= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
+ return btrfs_dev_alloc_hint(device, type, &alloc_hint);
+#else
+ return true;
#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+}
const char *btrfs_bg_type_to_raid_name(u64 flags)
{
*/
static int alloc_virtual_chunk(struct btrfs_fs_info *fs_info,
struct btrfs_device_info *devices_info,
- enum btrfs_raid_types type,
+ enum btrfs_raid_types type, u64 chunk_type,
u64 *allocated)
{
const struct btrfs_raid_attr *raid_attr = &btrfs_raid_array[type];
test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state))
continue;
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+ /* Only the devices the chunk allocator would use for this type. */
+ if (!btrfs_dev_alloc_hint(device, chunk_type,
+ &devices_info[ndevs].alloc_hint))
+ continue;
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
+
if (device->total_bytes > device->bytes_used +
device->per_profile_allocated)
avail = device->total_bytes - device->bytes_used -
}
sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
btrfs_cmp_device_info, NULL);
+#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
+ /* The same hint tiers gather_device_info() would pick. */
+ ndevs = btrfs_select_hint_tiers(devices_info, ndevs,
+ raid_attr->bg_flag | chunk_type,
+ raid_attr->devs_min, raid_attr->devs_max);
+ for (int i = 0; i < ndevs; i++)
+ devices_info[i].alloc_hint = 0;
+ sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
+ btrfs_cmp_device_info, NULL);
+#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
ndevs = rounddown(ndevs, raid_attr->devs_increment);
if (ndevs < raid_attr->devs_min)
return -ENOSPC;
}
static int calc_one_profile_avail(struct btrfs_fs_info *fs_info,
- enum btrfs_raid_types type,
+ enum btrfs_raid_types type, u64 chunk_type,
u64 *result_ret)
{
struct btrfs_device_info *devices_info = NULL;
list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list)
device->per_profile_allocated = 0;
- while (!alloc_virtual_chunk(fs_info, devices_info, type, &allocated))
+ while (!alloc_virtual_chunk(fs_info, devices_info, type, chunk_type,
+ &allocated))
result += allocated;
out:
return 0;
}
-/* Update the per-profile available space array. */
+/*
+ * Update the per-profile available space arrays, one for data and one for
+ * metadata and system chunks: the allocator hints can make the devices a
+ * data chunk may use differ from those a metadata chunk may use.
+ */
void btrfs_update_per_profile_avail(struct btrfs_fs_info *fs_info)
{
- u64 results[BTRFS_NR_RAID_TYPES];
+ static const u64 chunk_types[BTRFS_NR_PER_PROFILE] = {
+ [BTRFS_PER_PROFILE_DATA] = BTRFS_BLOCK_GROUP_DATA,
+ [BTRFS_PER_PROFILE_METADATA] = BTRFS_BLOCK_GROUP_METADATA,
+ };
+ const bool mixed = btrfs_fs_incompat(fs_info, MIXED_GROUPS);
+ u64 results[BTRFS_NR_PER_PROFILE][BTRFS_NR_RAID_TYPES];
int ret;
/*
if (btrfs_is_zoned(fs_info))
goto error;
- for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
- ret = calc_one_profile_avail(fs_info, i, &results[i]);
- if (ret < 0)
- goto error;
+ for (int k = 0; k < BTRFS_NR_PER_PROFILE; k++) {
+ u64 chunk_type = chunk_types[k];
+
+ if (mixed)
+ chunk_type = BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA;
+ for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
+ /* Without hints every type can use every device. */
+ if (k > 0 && !IS_ENABLED(CONFIG_BTRFS_ALLOCATOR_HINTS)) {
+ results[k][i] = results[0][i];
+ continue;
+ }
+ ret = calc_one_profile_avail(fs_info, i, chunk_type,
+ &results[k][i]);
+ if (ret < 0)
+ goto error;
+ }
}
spin_lock(&fs_info->fs_devices->per_profile_lock);
- for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
- fs_info->fs_devices->per_profile_avail[i] = results[i];
+ memcpy(fs_info->fs_devices->per_profile_avail, results, sizeof(results));
spin_unlock(&fs_info->fs_devices->per_profile_lock);
return;
error:
spin_lock(&fs_info->fs_devices->per_profile_lock);
- for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
- fs_info->fs_devices->per_profile_avail[i] = U64_MAX;
+ for (int k = 0; k < BTRFS_NR_PER_PROFILE; k++)
+ for (int i = 0; i < BTRFS_NR_RAID_TYPES; i++)
+ fs_info->fs_devices->per_profile_avail[k][i] = U64_MAX;
spin_unlock(&fs_info->fs_devices->per_profile_lock);
}
devices_info[ndevs].dev = device;
#ifdef CONFIG_BTRFS_ALLOCATOR_HINTS
- if ((ctl->type & BTRFS_BLOCK_GROUP_DATA) &&
- (ctl->type & BTRFS_BLOCK_GROUP_METADATA)) {
- /*
- * Mixed block groups cannot separate data and metadata
- * placement. Ignore all hints and retain normal free-space
- * ordering.
- */
- devices_info[ndevs].alloc_hint = 0;
- } else if (ctl->type & BTRFS_BLOCK_GROUP_DATA) {
- int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
-
- /*
- * skip BTRFS_DEV_METADATA_ONLY disks
- */
- if (BTRFS_DEV_ALLOCATION_METADATA_ONLY == hint)
- continue;
- /*
- * skip BTRFS_DEV_NONE_ONLY disks
- */
- if (BTRFS_DEV_ALLOCATION_NONE_ONLY == hint)
- continue;
- /*
- * if a data chunk must be allocated,
- * sort also by hint (data disk
- * higher priority)
- */
- devices_info[ndevs].alloc_hint = -alloc_hint_map[hint];
- } else { /* BTRFS_BLOCK_GROUP_METADATA */
- int hint = device->type & BTRFS_DEV_ALLOCATION_MASK;
-
- /*
- * skip BTRFS_DEV_DATA_ONLY disks
- */
- if (BTRFS_DEV_ALLOCATION_DATA_ONLY == hint)
- continue;
- /*
- * skip BTRFS_DEV_NONE_ONLY disks
- */
- if (BTRFS_DEV_ALLOCATION_NONE_ONLY == hint)
- continue;
- /*
- * if a metadata chunk must be allocated,
- * sort also by hint (metadata hint
- * higher priority)
- */
- if (BTRFS_DEV_ALLOCATION_PREFERRED_NONE == hint)
- devices_info[ndevs].alloc_hint = -alloc_hint_map[hint];
- else
- devices_info[ndevs].alloc_hint = alloc_hint_map[hint];
- }
+ if (!btrfs_dev_alloc_hint(device, ctl->type,
+ &devices_info[ndevs].alloc_hint))
+ continue;
#endif /* CONFIG_BTRFS_ALLOCATOR_HINTS */
++ndevs;
* select the minimum set of disks grouped by hint that
* can host the chunk
*/
- ndevs = 0;
- while (ndevs < ctl->ndevs) {
- int hint = devices_info[ndevs++].alloc_hint;
- while (ndevs < ctl->ndevs &&
- devices_info[ndevs].alloc_hint == hint)
- ndevs++;
- if (ndevs >= ctl->devs_min)
- break;
- }
-
- /*
- * Fixed-width mirrored profiles consume only the first devs_max
- * candidates. Keep the highest-priority devices and only as many of
- * the final hint group as needed. The sort below then restores the
- * max_avail ordering required by the stripe-size calculation.
- *
- * Check the fixed-width property explicitly so a future variable-width
- * RAID1 profile retains the existing whole-group behavior.
- */
- if ((ctl->type & BTRFS_BLOCK_GROUP_RAID1_MASK) &&
- ctl->devs_min == ctl->devs_max)
- ndevs = min(ndevs, ctl->devs_max);
+ ndevs = btrfs_select_hint_tiers(devices_info, ctl->ndevs, ctl->type,
+ ctl->devs_min, ctl->devs_max);
BUG_ON(ndevs > ctl->ndevs);
ctl->ndevs = ndevs;