freebsd/kstat: replace existing kstat if name is reused

Normally, when trying to add a sysctl name that already exists, the
kernel rejects it with a warning. This changes the code to search for a
sysctl with the wanted name in same root. If it exists, it is destroyed,
allowing the new one to go in.

Arguably, a collision like this shouldn't ever happen, but during
import multiple vdev_t (and so vdev_queue_t, and so vdev_queue stats)
can exist at the same time for the same guid. There's no real way to
tell which is which without substantial refactoring in the import and
vdev init codepaths, whch is probably worthwhile but not for today.

Sponsored-by: Klara, Inc.
Sponsored-by: Syneto
Signed-off-by: Rob Norris <rob.norris@klarasystems.com>
This commit is contained in:
Rob Norris 2024-05-16 15:23:40 +10:00 committed by Rob Norris
parent 2c251e814c
commit b827625471
1 changed files with 19 additions and 0 deletions

View File

@ -420,7 +420,26 @@ kstat_install_named(kstat_t *ksp)
if (ksent->data_type != 0) { if (ksent->data_type != 0) {
typelast = ksent->data_type; typelast = ksent->data_type;
namelast = ksent->name; namelast = ksent->name;
/*
* If a sysctl with this name already exists on this on
* this root, first remove it by deleting it from its
* old context, and then destroying it.
*/
struct sysctl_oid *oid = NULL;
SYSCTL_FOREACH(oid,
SYSCTL_CHILDREN(ksp->ks_sysctl_root)) {
if (strcmp(oid->oid_name, namelast) == 0) {
kstat_t *oldksp =
(kstat_t *)oid->oid_arg1;
sysctl_ctx_entry_del(
&oldksp->ks_sysctl_ctx, oid);
sysctl_remove_oid(oid, 1, 0);
break;
} }
}
}
switch (typelast) { switch (typelast) {
case KSTAT_DATA_CHAR: case KSTAT_DATA_CHAR:
/* Not Implemented */ /* Not Implemented */