Path: blob/main/sys/contrib/openzfs/module/nvpair/fnvpair.c
48383 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) 2012, 2018 by Delphix. All rights reserved.24*/2526#include <sys/nvpair.h>27#include <sys/kmem.h>28#include <sys/debug.h>29#include <sys/param.h>30#ifndef _KERNEL31#include <stdlib.h>32#endif3334/*35* "Force" nvlist wrapper.36*37* These functions wrap the nvlist_* functions with assertions that assume38* the operation is successful. This allows the caller's code to be much39* more readable, especially for the fnvlist_lookup_* and fnvpair_value_*40* functions, which can return the requested value (rather than filling in41* a pointer).42*43* These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate44* with KM_SLEEP.45*46* More wrappers should be added as needed -- for example47* nvlist_lookup_*_array and nvpair_value_*_array.48*/4950nvlist_t *51fnvlist_alloc(void)52{53nvlist_t *nvl;54VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP));55return (nvl);56}5758void59fnvlist_free(nvlist_t *nvl)60{61nvlist_free(nvl);62}6364size_t65fnvlist_size(nvlist_t *nvl)66{67size_t size;68VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE));69return (size);70}7172/*73* Returns allocated buffer of size *sizep. Caller must free the buffer with74* fnvlist_pack_free().75*/76char *77fnvlist_pack(nvlist_t *nvl, size_t *sizep)78{79char *packed = 0;80VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE,81KM_SLEEP), ==, 0);82return (packed);83}8485void86fnvlist_pack_free(char *pack, size_t size)87{88#ifdef _KERNEL89kmem_free(pack, size);90#else91(void) size;92free(pack);93#endif94}9596nvlist_t *97fnvlist_unpack(char *buf, size_t buflen)98{99nvlist_t *rv;100VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));101return (rv);102}103104nvlist_t *105fnvlist_dup(const nvlist_t *nvl)106{107nvlist_t *rv;108VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP));109return (rv);110}111112void113fnvlist_merge(nvlist_t *dst, nvlist_t *src)114{115VERIFY0(nvlist_merge(dst, src, KM_SLEEP));116}117118size_t119fnvlist_num_pairs(nvlist_t *nvl)120{121size_t count = 0;122nvpair_t *pair;123124for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL;125pair = nvlist_next_nvpair(nvl, pair))126count++;127return (count);128}129130void131fnvlist_add_boolean(nvlist_t *nvl, const char *name)132{133VERIFY0(nvlist_add_boolean(nvl, name));134}135136void137fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)138{139VERIFY0(nvlist_add_boolean_value(nvl, name, val));140}141142void143fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)144{145VERIFY0(nvlist_add_byte(nvl, name, val));146}147148void149fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)150{151VERIFY0(nvlist_add_int8(nvl, name, val));152}153154void155fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)156{157VERIFY0(nvlist_add_uint8(nvl, name, val));158}159160void161fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)162{163VERIFY0(nvlist_add_int16(nvl, name, val));164}165166void167fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)168{169VERIFY0(nvlist_add_uint16(nvl, name, val));170}171172void173fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)174{175VERIFY0(nvlist_add_int32(nvl, name, val));176}177178void179fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)180{181VERIFY0(nvlist_add_uint32(nvl, name, val));182}183184void185fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)186{187VERIFY0(nvlist_add_int64(nvl, name, val));188}189190void191fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)192{193VERIFY0(nvlist_add_uint64(nvl, name, val));194}195196void197fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val)198{199VERIFY0(nvlist_add_string(nvl, name, val));200}201202void203fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)204{205VERIFY0(nvlist_add_nvlist(nvl, name, val));206}207208void209fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair)210{211VERIFY0(nvlist_add_nvpair(nvl, pair));212}213214void215fnvlist_add_boolean_array(nvlist_t *nvl, const char *name,216const boolean_t *val, uint_t n)217{218VERIFY0(nvlist_add_boolean_array(nvl, name, val, n));219}220221void222fnvlist_add_byte_array(nvlist_t *nvl, const char *name, const uchar_t *val,223uint_t n)224{225VERIFY0(nvlist_add_byte_array(nvl, name, val, n));226}227228void229fnvlist_add_int8_array(nvlist_t *nvl, const char *name, const int8_t *val,230uint_t n)231{232VERIFY0(nvlist_add_int8_array(nvl, name, val, n));233}234235void236fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, const uint8_t *val,237uint_t n)238{239VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));240}241242void243fnvlist_add_int16_array(nvlist_t *nvl, const char *name, const int16_t *val,244uint_t n)245{246VERIFY0(nvlist_add_int16_array(nvl, name, val, n));247}248249void250fnvlist_add_uint16_array(nvlist_t *nvl, const char *name,251const uint16_t *val, uint_t n)252{253VERIFY0(nvlist_add_uint16_array(nvl, name, val, n));254}255256void257fnvlist_add_int32_array(nvlist_t *nvl, const char *name, const int32_t *val,258uint_t n)259{260VERIFY0(nvlist_add_int32_array(nvl, name, val, n));261}262263void264fnvlist_add_uint32_array(nvlist_t *nvl, const char *name,265const uint32_t *val, uint_t n)266{267VERIFY0(nvlist_add_uint32_array(nvl, name, val, n));268}269270void271fnvlist_add_int64_array(nvlist_t *nvl, const char *name, const int64_t *val,272uint_t n)273{274VERIFY0(nvlist_add_int64_array(nvl, name, val, n));275}276277void278fnvlist_add_uint64_array(nvlist_t *nvl, const char *name,279const uint64_t *val, uint_t n)280{281VERIFY0(nvlist_add_uint64_array(nvl, name, val, n));282}283284void285fnvlist_add_string_array(nvlist_t *nvl, const char *name,286const char * const *val, uint_t n)287{288VERIFY0(nvlist_add_string_array(nvl, name, val, n));289}290291void292fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name,293const nvlist_t * const *val, uint_t n)294{295VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n));296}297298void299fnvlist_remove(nvlist_t *nvl, const char *name)300{301VERIFY0(nvlist_remove_all(nvl, name));302}303304void305fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)306{307VERIFY0(nvlist_remove_nvpair(nvl, pair));308}309310nvpair_t *311fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)312{313nvpair_t *rv;314VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));315return (rv);316}317318/* returns B_TRUE if the entry exists */319boolean_t320fnvlist_lookup_boolean(const nvlist_t *nvl, const char *name)321{322return (nvlist_lookup_boolean(nvl, name) == 0);323}324325boolean_t326fnvlist_lookup_boolean_value(const nvlist_t *nvl, const char *name)327{328boolean_t rv;329VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));330return (rv);331}332333uchar_t334fnvlist_lookup_byte(const nvlist_t *nvl, const char *name)335{336uchar_t rv;337VERIFY0(nvlist_lookup_byte(nvl, name, &rv));338return (rv);339}340341int8_t342fnvlist_lookup_int8(const nvlist_t *nvl, const char *name)343{344int8_t rv;345VERIFY0(nvlist_lookup_int8(nvl, name, &rv));346return (rv);347}348349int16_t350fnvlist_lookup_int16(const nvlist_t *nvl, const char *name)351{352int16_t rv;353VERIFY0(nvlist_lookup_int16(nvl, name, &rv));354return (rv);355}356357int32_t358fnvlist_lookup_int32(const nvlist_t *nvl, const char *name)359{360int32_t rv;361VERIFY0(nvlist_lookup_int32(nvl, name, &rv));362return (rv);363}364365int64_t366fnvlist_lookup_int64(const nvlist_t *nvl, const char *name)367{368int64_t rv;369VERIFY0(nvlist_lookup_int64(nvl, name, &rv));370return (rv);371}372373uint8_t374fnvlist_lookup_uint8(const nvlist_t *nvl, const char *name)375{376uint8_t rv;377VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));378return (rv);379}380381uint16_t382fnvlist_lookup_uint16(const nvlist_t *nvl, const char *name)383{384uint16_t rv;385VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));386return (rv);387}388389uint32_t390fnvlist_lookup_uint32(const nvlist_t *nvl, const char *name)391{392uint32_t rv;393VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));394return (rv);395}396397uint64_t398fnvlist_lookup_uint64(const nvlist_t *nvl, const char *name)399{400uint64_t rv;401VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));402return (rv);403}404405const char *406fnvlist_lookup_string(const nvlist_t *nvl, const char *name)407{408const char *rv;409VERIFY0(nvlist_lookup_string(nvl, name, &rv));410return (rv);411}412413nvlist_t *414fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)415{416nvlist_t *rv;417VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));418return (rv);419}420boolean_t *421fnvlist_lookup_boolean_array(nvlist_t *nvl, const char *name, uint_t *n)422{423boolean_t *rv;424VERIFY0(nvlist_lookup_boolean_array(nvl, name, &rv, n));425return (rv);426}427428uchar_t *429fnvlist_lookup_byte_array(nvlist_t *nvl, const char *name, uint_t *n)430{431uchar_t *rv;432VERIFY0(nvlist_lookup_byte_array(nvl, name, &rv, n));433return (rv);434}435436int8_t *437fnvlist_lookup_int8_array(nvlist_t *nvl, const char *name, uint_t *n)438{439int8_t *rv;440VERIFY0(nvlist_lookup_int8_array(nvl, name, &rv, n));441return (rv);442}443444uint8_t *445fnvlist_lookup_uint8_array(nvlist_t *nvl, const char *name, uint_t *n)446{447uint8_t *rv;448VERIFY0(nvlist_lookup_uint8_array(nvl, name, &rv, n));449return (rv);450}451452int16_t *453fnvlist_lookup_int16_array(nvlist_t *nvl, const char *name, uint_t *n)454{455int16_t *rv;456VERIFY0(nvlist_lookup_int16_array(nvl, name, &rv, n));457return (rv);458}459460uint16_t *461fnvlist_lookup_uint16_array(nvlist_t *nvl, const char *name, uint_t *n)462{463uint16_t *rv;464VERIFY0(nvlist_lookup_uint16_array(nvl, name, &rv, n));465return (rv);466}467468int32_t *469fnvlist_lookup_int32_array(nvlist_t *nvl, const char *name, uint_t *n)470{471int32_t *rv;472VERIFY0(nvlist_lookup_int32_array(nvl, name, &rv, n));473return (rv);474}475476uint32_t *477fnvlist_lookup_uint32_array(nvlist_t *nvl, const char *name, uint_t *n)478{479uint32_t *rv;480VERIFY0(nvlist_lookup_uint32_array(nvl, name, &rv, n));481return (rv);482}483484int64_t *485fnvlist_lookup_int64_array(nvlist_t *nvl, const char *name, uint_t *n)486{487int64_t *rv;488VERIFY0(nvlist_lookup_int64_array(nvl, name, &rv, n));489return (rv);490}491492uint64_t *493fnvlist_lookup_uint64_array(nvlist_t *nvl, const char *name, uint_t *n)494{495uint64_t *rv;496VERIFY0(nvlist_lookup_uint64_array(nvl, name, &rv, n));497return (rv);498}499500boolean_t501fnvpair_value_boolean_value(const nvpair_t *nvp)502{503boolean_t rv;504VERIFY0(nvpair_value_boolean_value(nvp, &rv));505return (rv);506}507508uchar_t509fnvpair_value_byte(const nvpair_t *nvp)510{511uchar_t rv;512VERIFY0(nvpair_value_byte(nvp, &rv));513return (rv);514}515516int8_t517fnvpair_value_int8(const nvpair_t *nvp)518{519int8_t rv;520VERIFY0(nvpair_value_int8(nvp, &rv));521return (rv);522}523524int16_t525fnvpair_value_int16(const nvpair_t *nvp)526{527int16_t rv;528VERIFY0(nvpair_value_int16(nvp, &rv));529return (rv);530}531532int32_t533fnvpair_value_int32(const nvpair_t *nvp)534{535int32_t rv;536VERIFY0(nvpair_value_int32(nvp, &rv));537return (rv);538}539540int64_t541fnvpair_value_int64(const nvpair_t *nvp)542{543int64_t rv;544VERIFY0(nvpair_value_int64(nvp, &rv));545return (rv);546}547548uint8_t549fnvpair_value_uint8(const nvpair_t *nvp)550{551uint8_t rv;552VERIFY0(nvpair_value_uint8(nvp, &rv));553return (rv);554}555556uint16_t557fnvpair_value_uint16(const nvpair_t *nvp)558{559uint16_t rv;560VERIFY0(nvpair_value_uint16(nvp, &rv));561return (rv);562}563564uint32_t565fnvpair_value_uint32(const nvpair_t *nvp)566{567uint32_t rv;568VERIFY0(nvpair_value_uint32(nvp, &rv));569return (rv);570}571572uint64_t573fnvpair_value_uint64(const nvpair_t *nvp)574{575uint64_t rv;576VERIFY0(nvpair_value_uint64(nvp, &rv));577return (rv);578}579580const char *581fnvpair_value_string(const nvpair_t *nvp)582{583const char *rv;584VERIFY0(nvpair_value_string(nvp, &rv));585return (rv);586}587588nvlist_t *589fnvpair_value_nvlist(nvpair_t *nvp)590{591nvlist_t *rv;592VERIFY0(nvpair_value_nvlist(nvp, &rv));593return (rv);594}595596#if defined(_KERNEL)597598EXPORT_SYMBOL(fnvlist_alloc);599EXPORT_SYMBOL(fnvlist_free);600EXPORT_SYMBOL(fnvlist_size);601EXPORT_SYMBOL(fnvlist_pack);602EXPORT_SYMBOL(fnvlist_pack_free);603EXPORT_SYMBOL(fnvlist_unpack);604EXPORT_SYMBOL(fnvlist_dup);605EXPORT_SYMBOL(fnvlist_merge);606607EXPORT_SYMBOL(fnvlist_add_nvpair);608EXPORT_SYMBOL(fnvlist_add_boolean);609EXPORT_SYMBOL(fnvlist_add_boolean_value);610EXPORT_SYMBOL(fnvlist_add_byte);611EXPORT_SYMBOL(fnvlist_add_int8);612EXPORT_SYMBOL(fnvlist_add_uint8);613EXPORT_SYMBOL(fnvlist_add_int16);614EXPORT_SYMBOL(fnvlist_add_uint16);615EXPORT_SYMBOL(fnvlist_add_int32);616EXPORT_SYMBOL(fnvlist_add_uint32);617EXPORT_SYMBOL(fnvlist_add_int64);618EXPORT_SYMBOL(fnvlist_add_uint64);619EXPORT_SYMBOL(fnvlist_add_string);620EXPORT_SYMBOL(fnvlist_add_nvlist);621EXPORT_SYMBOL(fnvlist_add_boolean_array);622EXPORT_SYMBOL(fnvlist_add_byte_array);623EXPORT_SYMBOL(fnvlist_add_int8_array);624EXPORT_SYMBOL(fnvlist_add_uint8_array);625EXPORT_SYMBOL(fnvlist_add_int16_array);626EXPORT_SYMBOL(fnvlist_add_uint16_array);627EXPORT_SYMBOL(fnvlist_add_int32_array);628EXPORT_SYMBOL(fnvlist_add_uint32_array);629EXPORT_SYMBOL(fnvlist_add_int64_array);630EXPORT_SYMBOL(fnvlist_add_uint64_array);631EXPORT_SYMBOL(fnvlist_add_string_array);632EXPORT_SYMBOL(fnvlist_add_nvlist_array);633634EXPORT_SYMBOL(fnvlist_remove);635EXPORT_SYMBOL(fnvlist_remove_nvpair);636637EXPORT_SYMBOL(fnvlist_lookup_nvpair);638EXPORT_SYMBOL(fnvlist_lookup_boolean);639EXPORT_SYMBOL(fnvlist_lookup_boolean_value);640EXPORT_SYMBOL(fnvlist_lookup_byte);641EXPORT_SYMBOL(fnvlist_lookup_int8);642EXPORT_SYMBOL(fnvlist_lookup_uint8);643EXPORT_SYMBOL(fnvlist_lookup_int16);644EXPORT_SYMBOL(fnvlist_lookup_uint16);645EXPORT_SYMBOL(fnvlist_lookup_int32);646EXPORT_SYMBOL(fnvlist_lookup_uint32);647EXPORT_SYMBOL(fnvlist_lookup_int64);648EXPORT_SYMBOL(fnvlist_lookup_uint64);649EXPORT_SYMBOL(fnvlist_lookup_string);650EXPORT_SYMBOL(fnvlist_lookup_nvlist);651652EXPORT_SYMBOL(fnvpair_value_boolean_value);653EXPORT_SYMBOL(fnvpair_value_byte);654EXPORT_SYMBOL(fnvpair_value_int8);655EXPORT_SYMBOL(fnvpair_value_uint8);656EXPORT_SYMBOL(fnvpair_value_int16);657EXPORT_SYMBOL(fnvpair_value_uint16);658EXPORT_SYMBOL(fnvpair_value_int32);659EXPORT_SYMBOL(fnvpair_value_uint32);660EXPORT_SYMBOL(fnvpair_value_int64);661EXPORT_SYMBOL(fnvpair_value_uint64);662EXPORT_SYMBOL(fnvpair_value_string);663EXPORT_SYMBOL(fnvpair_value_nvlist);664EXPORT_SYMBOL(fnvlist_num_pairs);665666#endif667668669