Path: blob/main/sys/contrib/openzfs/lib/libspl/tunables.c
48378 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;126errno = 0;127n = strtoimax(val, &end, 0);128if (errno != 0)129return (errno);130if (*end != '\0')131return (EINVAL);132if (n < min || n > max)133return (ERANGE);134*np = n;135return (0);136}137138static int139zfs_tunable_parse_uint(const char *val, uintmax_t *np,140uintmax_t min, uintmax_t max)141{142uintmax_t n;143char *end;144errno = 0;145n = strtoumax(val, &end, 0);146if (errno != 0)147return (errno);148if (*end != '\0')149return (EINVAL);150if (strchr(val, '-'))151return (ERANGE);152if (n < min || n > max)153return (ERANGE);154*np = n;155return (0);156}157158/*159* Set helpers for each tunable type. Parses the string, and if produces a160* valid value for the tunable, sets it. No effort is made to make sure the161* tunable is of the right type; that's done in zfs_tunable_set() below.162*/163static int164zfs_tunable_set_int(const zfs_tunable_t *zt, const char *val)165{166intmax_t n;167int err = zfs_tunable_parse_int(val, &n, INT_MIN, INT_MAX);168if (err != 0)169return (err);170*(int *)zt->zt_varp = n;171return (0);172}173174static int175zfs_tunable_set_uint(const zfs_tunable_t *zt, const char *val)176{177uintmax_t n;178int err = zfs_tunable_parse_uint(val, &n, 0, UINT_MAX);179if (err != 0)180return (err);181*(unsigned int *)zt->zt_varp = n;182return (0);183}184185static int186zfs_tunable_set_ulong(const zfs_tunable_t *zt, const char *val)187{188uintmax_t n;189int err = zfs_tunable_parse_uint(val, &n, 0, ULONG_MAX);190if (err != 0)191return (err);192*(unsigned long *)zt->zt_varp = n;193return (0);194}195196static int197zfs_tunable_set_u64(const zfs_tunable_t *zt, const char *val)198{199uintmax_t n;200int err = zfs_tunable_parse_uint(val, &n, 0, UINT64_MAX);201if (err != 0)202return (err);203*(uint64_t *)zt->zt_varp = n;204return (0);205}206207static int208zfs_tunable_set_string(const zfs_tunable_t *zt, const char *val)209{210(void) zt, (void) val;211/*212* We can't currently handle strings. String tunables are pointers213* into read-only memory, so we can update the pointer, but not the214* contents. That would mean taking an allocation, but we don't have215* an obvious place to free it.216*217* For now, it's no big deal as there's only a couple of string218* tunables anyway.219*/220return (ENOTSUP);221}222223/*224* Get helpers for each tunable type. Converts the value to a string if225* necessary and writes it into the provided buffer. The type is assumed to226* be correct; zfs_tunable_get() below will call the correct function for the227* type.228*/229static int230zfs_tunable_get_int(const zfs_tunable_t *zt, char *val, size_t valsz)231{232snprintf(val, valsz, "%d", *(int *)zt->zt_varp);233return (0);234}235236static int237zfs_tunable_get_uint(const zfs_tunable_t *zt, char *val, size_t valsz)238{239snprintf(val, valsz, "%u", *(unsigned int *)zt->zt_varp);240return (0);241}242243static int244zfs_tunable_get_ulong(const zfs_tunable_t *zt, char *val, size_t valsz)245{246snprintf(val, valsz, "%lu", *(unsigned long *)zt->zt_varp);247return (0);248}249250static int251zfs_tunable_get_u64(const zfs_tunable_t *zt, char *val, size_t valsz)252{253snprintf(val, valsz, "%"PRIu64, *(uint64_t *)zt->zt_varp);254return (0);255}256257static int258zfs_tunable_get_string(const zfs_tunable_t *zt, char *val, size_t valsz)259{260strlcpy(val, *(char **)zt->zt_varp, valsz);261return (0);262}263264/* The public set function. Delegates to the type-specific version. */265int266zfs_tunable_set(const zfs_tunable_t *zt, const char *val)267{268int err;269switch (zt->zt_type) {270case ZFS_TUNABLE_TYPE_INT:271err = zfs_tunable_set_int(zt, val);272break;273case ZFS_TUNABLE_TYPE_UINT:274err = zfs_tunable_set_uint(zt, val);275break;276case ZFS_TUNABLE_TYPE_ULONG:277err = zfs_tunable_set_ulong(zt, val);278break;279case ZFS_TUNABLE_TYPE_U64:280err = zfs_tunable_set_u64(zt, val);281break;282case ZFS_TUNABLE_TYPE_STRING:283err = zfs_tunable_set_string(zt, val);284break;285default:286err = EOPNOTSUPP;287break;288}289return (err);290}291292/* The public get function. Delegates to the type-specific version. */293int294zfs_tunable_get(const zfs_tunable_t *zt, char *val, size_t valsz)295{296int err;297switch (zt->zt_type) {298case ZFS_TUNABLE_TYPE_INT:299err = zfs_tunable_get_int(zt, val, valsz);300break;301case ZFS_TUNABLE_TYPE_UINT:302err = zfs_tunable_get_uint(zt, val, valsz);303break;304case ZFS_TUNABLE_TYPE_ULONG:305err = zfs_tunable_get_ulong(zt, val, valsz);306break;307case ZFS_TUNABLE_TYPE_U64:308err = zfs_tunable_get_u64(zt, val, valsz);309break;310case ZFS_TUNABLE_TYPE_STRING:311err = zfs_tunable_get_string(zt, val, valsz);312break;313default:314err = EOPNOTSUPP;315break;316}317return (err);318}319320321