Path: blob/main/sys/contrib/openzfs/lib/libspl/tunables.c
105585 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2025, Rob Norris <[email protected]>24*/2526#include <stddef.h>27#include <string.h>28#include <stdlib.h>29#include <stdio.h>30#include <errno.h>31#include <limits.h>32#include <inttypes.h>33#include <sys/tunables.h>3435/*36* Userspace tunables.37*38* Tunables are external pointers to global variables that are wired up to the39* host environment in some way that allows the operator to directly change40* their values "under the hood".41*42* In userspace, the "host environment" is the program using libzpool.so. So43* that it can manipulate tunables if it wants, we provide an API to access44* them.45*46* Tunables are declared through the ZFS_MODULE_PARAM* macros, which associate47* a global variable with some metadata we can use to describe and access the48* tunable. This is done by creating a uniquely-named zfs_tunable_t.49*50* At runtime, we need a way to discover these zfs_tunable_t items. Since they51* are declared globally, all over the codebase, there's no central place to52* record or list them. So, we take advantage of the compiler's "linker set"53* feature.54*55* In the ZFS_MODULE_PARAM macro, after we create the zfs_tunable_t, we also56* create a zfs_tunable_t* pointing to it. That pointer is forced into the57* "zfs_tunables" ELF section in compiled object. At link time, the linker will58* collect all these pointers into one single big "zfs_tunable" section, and59* will generate two new symbols in the final object: __start_zfs_tunable and60* __stop_zfs_tunable. These point to the first and last item in that section,61* which allows us to access the pointers in that section like an array, and62* through those pointers access the tunable metadata, and from there the63* actual C variable that the tunable describes.64*/6566extern const zfs_tunable_t *__start_zfs_tunables;67extern const zfs_tunable_t *__stop_zfs_tunables;6869/*70* Because there are no tunables in libspl itself, the above symbols will not71* be generated, which will stop libspl being linked at all. To work around72* that, we force a symbol into that section, and then when iterating, skip73* any NULL pointers.74*/75static void *__zfs_tunable__placeholder76__attribute__((__section__("zfs_tunables")))77__attribute__((__used__)) = NULL;7879/*80* Find the name tunable by walking through the linker set and comparing names,81* as described above. This is not particularly efficient but it's a fairly82* rare task, so it shouldn't be a big deal.83*/84const zfs_tunable_t *85zfs_tunable_lookup(const char *name)86{87for (const zfs_tunable_t **ztp = &__start_zfs_tunables;88ztp != &__stop_zfs_tunables; ztp++) {89const zfs_tunable_t *zt = *ztp;90if (zt == NULL)91continue;92if (strcmp(name, zt->zt_name) == 0)93return (zt);94}9596return (NULL);97}9899/*100* Like zfs_tunable_lookup, but call the provided callback for each tunable.101*/102void103zfs_tunable_iter(zfs_tunable_iter_t cb, void *arg)104{105for (const zfs_tunable_t **ztp = &__start_zfs_tunables;106ztp != &__stop_zfs_tunables; ztp++) {107const zfs_tunable_t *zt = *ztp;108if (zt == NULL)109continue;110if (cb(zt, arg))111return;112}113}114115/*116* Parse a string into an int or uint. It's easier to have a pair of "generic"117* functions that clamp to a given min and max rather than have multiple118* functions for each width of type.119*/120static int121zfs_tunable_parse_int(const char *val, intmax_t *np,122intmax_t min, intmax_t max)123{124intmax_t n;125char *end;126int err;127128errno = 0;129n = strtoimax(val, &end, 0);130if ((err = errno) != 0)131return (err);132if (*end != '\0')133return (EINVAL);134if (n < min || n > max)135return (ERANGE);136*np = n;137return (0);138}139140static int141zfs_tunable_parse_uint(const char *val, uintmax_t *np,142uintmax_t min, uintmax_t max)143{144uintmax_t n;145char *end;146int err;147148errno = 0;149n = strtoumax(val, &end, 0);150if ((err = errno) != 0)151return (err);152if (*end != '\0')153return (EINVAL);154if (strchr(val, '-'))155return (ERANGE);156if (n < min || n > max)157return (ERANGE);158*np = n;159return (0);160}161162/*163* Set helpers for each tunable type. Parses the string, and if produces a164* valid value for the tunable, sets it. No effort is made to make sure the165* tunable is of the right type; that's done in zfs_tunable_set() below.166*/167static int168zfs_tunable_set_int(const zfs_tunable_t *zt, const char *val)169{170intmax_t n;171int err = zfs_tunable_parse_int(val, &n, INT_MIN, INT_MAX);172if (err != 0)173return (err);174*(int *)zt->zt_varp = n;175return (0);176}177178static int179zfs_tunable_set_uint(const zfs_tunable_t *zt, const char *val)180{181uintmax_t n;182int err = zfs_tunable_parse_uint(val, &n, 0, UINT_MAX);183if (err != 0)184return (err);185*(unsigned int *)zt->zt_varp = n;186return (0);187}188189static int190zfs_tunable_set_ulong(const zfs_tunable_t *zt, const char *val)191{192uintmax_t n;193int err = zfs_tunable_parse_uint(val, &n, 0, ULONG_MAX);194if (err != 0)195return (err);196*(unsigned long *)zt->zt_varp = n;197return (0);198}199200static int201zfs_tunable_set_u64(const zfs_tunable_t *zt, const char *val)202{203uintmax_t n;204int err = zfs_tunable_parse_uint(val, &n, 0, UINT64_MAX);205if (err != 0)206return (err);207*(uint64_t *)zt->zt_varp = n;208return (0);209}210211static int212zfs_tunable_set_string(const zfs_tunable_t *zt, const char *val)213{214(void) zt, (void) val;215/*216* We can't currently handle strings. String tunables are pointers217* into read-only memory, so we can update the pointer, but not the218* contents. That would mean taking an allocation, but we don't have219* an obvious place to free it.220*221* For now, it's no big deal as there's only a couple of string222* tunables anyway.223*/224return (ENOTSUP);225}226227/*228* Get helpers for each tunable type. Converts the value to a string if229* necessary and writes it into the provided buffer. The type is assumed to230* be correct; zfs_tunable_get() below will call the correct function for the231* type.232*/233static int234zfs_tunable_get_int(const zfs_tunable_t *zt, char *val, size_t valsz)235{236snprintf(val, valsz, "%d", *(int *)zt->zt_varp);237return (0);238}239240static int241zfs_tunable_get_uint(const zfs_tunable_t *zt, char *val, size_t valsz)242{243snprintf(val, valsz, "%u", *(unsigned int *)zt->zt_varp);244return (0);245}246247static int248zfs_tunable_get_ulong(const zfs_tunable_t *zt, char *val, size_t valsz)249{250snprintf(val, valsz, "%lu", *(unsigned long *)zt->zt_varp);251return (0);252}253254static int255zfs_tunable_get_u64(const zfs_tunable_t *zt, char *val, size_t valsz)256{257snprintf(val, valsz, "%"PRIu64, *(uint64_t *)zt->zt_varp);258return (0);259}260261static int262zfs_tunable_get_string(const zfs_tunable_t *zt, char *val, size_t valsz)263{264strlcpy(val, *(char **)zt->zt_varp, valsz);265return (0);266}267268/* The public set function. Delegates to the type-specific version. */269int270zfs_tunable_set(const zfs_tunable_t *zt, const char *val)271{272int err;273switch (zt->zt_type) {274case ZFS_TUNABLE_TYPE_INT:275err = zfs_tunable_set_int(zt, val);276break;277case ZFS_TUNABLE_TYPE_UINT:278err = zfs_tunable_set_uint(zt, val);279break;280case ZFS_TUNABLE_TYPE_ULONG:281err = zfs_tunable_set_ulong(zt, val);282break;283case ZFS_TUNABLE_TYPE_U64:284err = zfs_tunable_set_u64(zt, val);285break;286case ZFS_TUNABLE_TYPE_STRING:287err = zfs_tunable_set_string(zt, val);288break;289default:290err = EOPNOTSUPP;291break;292}293return (err);294}295296/* The public get function. Delegates to the type-specific version. */297int298zfs_tunable_get(const zfs_tunable_t *zt, char *val, size_t valsz)299{300int err;301switch (zt->zt_type) {302case ZFS_TUNABLE_TYPE_INT:303err = zfs_tunable_get_int(zt, val, valsz);304break;305case ZFS_TUNABLE_TYPE_UINT:306err = zfs_tunable_get_uint(zt, val, valsz);307break;308case ZFS_TUNABLE_TYPE_ULONG:309err = zfs_tunable_get_ulong(zt, val, valsz);310break;311case ZFS_TUNABLE_TYPE_U64:312err = zfs_tunable_get_u64(zt, val, valsz);313break;314case ZFS_TUNABLE_TYPE_STRING:315err = zfs_tunable_get_string(zt, val, valsz);316break;317default:318err = EOPNOTSUPP;319break;320}321return (err);322}323324325