17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53bb79becSeschrock * Common Development and Distribution License (the "License").
63bb79becSeschrock * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21d9638e54Smws
227c478bd9Sstevel@tonic-gate /*
23f6e214c7SGavin Maltby * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
277c478bd9Sstevel@tonic-gate #include <sys/debug.h>
287c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
297c478bd9Sstevel@tonic-gate #include <sys/int_limits.h>
307c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
317c478bd9Sstevel@tonic-gate #include <sys/nvpair_impl.h>
327c478bd9Sstevel@tonic-gate #include <rpc/types.h>
337c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
367c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
37602ca9eaScth #include <sys/ddi.h>
38602ca9eaScth #include <sys/sunddi.h>
39*c5d54b67SRichard Lowe #include <sys/sysmacros.h>
407c478bd9Sstevel@tonic-gate #else
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
42602ca9eaScth #include <stdlib.h>
43602ca9eaScth #include <string.h>
447c478bd9Sstevel@tonic-gate #include <strings.h>
45*c5d54b67SRichard Lowe #include <stddef.h>
467c478bd9Sstevel@tonic-gate #endif
477c478bd9Sstevel@tonic-gate
48602ca9eaScth #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * nvpair.c - Provides kernel & userland interfaces for manipulating
527c478bd9Sstevel@tonic-gate * name-value pairs.
537c478bd9Sstevel@tonic-gate *
547c478bd9Sstevel@tonic-gate * Overview Diagram
557c478bd9Sstevel@tonic-gate *
567c478bd9Sstevel@tonic-gate * +--------------+
577c478bd9Sstevel@tonic-gate * | nvlist_t |
587c478bd9Sstevel@tonic-gate * |--------------|
597c478bd9Sstevel@tonic-gate * | nvl_version |
607c478bd9Sstevel@tonic-gate * | nvl_nvflag |
617c478bd9Sstevel@tonic-gate * | nvl_priv -+-+
627c478bd9Sstevel@tonic-gate * | nvl_flag | |
637c478bd9Sstevel@tonic-gate * | nvl_pad | |
647c478bd9Sstevel@tonic-gate * +--------------+ |
657c478bd9Sstevel@tonic-gate * V
667c478bd9Sstevel@tonic-gate * +--------------+ last i_nvp in list
677c478bd9Sstevel@tonic-gate * | nvpriv_t | +--------------------->
687c478bd9Sstevel@tonic-gate * |--------------| |
697c478bd9Sstevel@tonic-gate * +--+- nvp_list | | +------------+
707c478bd9Sstevel@tonic-gate * | | nvp_last -+--+ + nv_alloc_t |
717c478bd9Sstevel@tonic-gate * | | nvp_curr | |------------|
727c478bd9Sstevel@tonic-gate * | | nvp_nva -+----> | nva_ops |
737c478bd9Sstevel@tonic-gate * | | nvp_stat | | nva_arg |
747c478bd9Sstevel@tonic-gate * | +--------------+ +------------+
757c478bd9Sstevel@tonic-gate * |
767c478bd9Sstevel@tonic-gate * +-------+
777c478bd9Sstevel@tonic-gate * V
787c478bd9Sstevel@tonic-gate * +---------------------+ +-------------------+
797c478bd9Sstevel@tonic-gate * | i_nvp_t | +-->| i_nvp_t | +-->
807c478bd9Sstevel@tonic-gate * |---------------------| | |-------------------| |
817c478bd9Sstevel@tonic-gate * | nvi_next -+--+ | nvi_next -+--+
827c478bd9Sstevel@tonic-gate * | nvi_prev (NULL) | <----+ nvi_prev |
837c478bd9Sstevel@tonic-gate * | . . . . . . . . . . | | . . . . . . . . . |
847c478bd9Sstevel@tonic-gate * | nvp (nvpair_t) | | nvp (nvpair_t) |
857c478bd9Sstevel@tonic-gate * | - nvp_size | | - nvp_size |
867c478bd9Sstevel@tonic-gate * | - nvp_name_sz | | - nvp_name_sz |
877c478bd9Sstevel@tonic-gate * | - nvp_value_elem | | - nvp_value_elem |
887c478bd9Sstevel@tonic-gate * | - nvp_type | | - nvp_type |
897c478bd9Sstevel@tonic-gate * | - data ... | | - data ... |
907c478bd9Sstevel@tonic-gate * +---------------------+ +-------------------+
917c478bd9Sstevel@tonic-gate *
927c478bd9Sstevel@tonic-gate *
937c478bd9Sstevel@tonic-gate *
947c478bd9Sstevel@tonic-gate * +---------------------+ +---------------------+
957c478bd9Sstevel@tonic-gate * | i_nvp_t | +--> +-->| i_nvp_t (last) |
967c478bd9Sstevel@tonic-gate * |---------------------| | | |---------------------|
977c478bd9Sstevel@tonic-gate * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
987c478bd9Sstevel@tonic-gate * <-+- nvi_prev |<-- ... <----+ nvi_prev |
997c478bd9Sstevel@tonic-gate * | . . . . . . . . . | | . . . . . . . . . |
1007c478bd9Sstevel@tonic-gate * | nvp (nvpair_t) | | nvp (nvpair_t) |
1017c478bd9Sstevel@tonic-gate * | - nvp_size | | - nvp_size |
1027c478bd9Sstevel@tonic-gate * | - nvp_name_sz | | - nvp_name_sz |
1037c478bd9Sstevel@tonic-gate * | - nvp_value_elem | | - nvp_value_elem |
1047c478bd9Sstevel@tonic-gate * | - DATA_TYPE_NVLIST | | - nvp_type |
1057c478bd9Sstevel@tonic-gate * | - data (embedded) | | - data ... |
1067c478bd9Sstevel@tonic-gate * | nvlist name | +---------------------+
1077c478bd9Sstevel@tonic-gate * | +--------------+ |
1087c478bd9Sstevel@tonic-gate * | | nvlist_t | |
1097c478bd9Sstevel@tonic-gate * | |--------------| |
1107c478bd9Sstevel@tonic-gate * | | nvl_version | |
1117c478bd9Sstevel@tonic-gate * | | nvl_nvflag | |
1127c478bd9Sstevel@tonic-gate * | | nvl_priv --+---+---->
1137c478bd9Sstevel@tonic-gate * | | nvl_flag | |
1147c478bd9Sstevel@tonic-gate * | | nvl_pad | |
1157c478bd9Sstevel@tonic-gate * | +--------------+ |
1167c478bd9Sstevel@tonic-gate * +---------------------+
1177c478bd9Sstevel@tonic-gate *
1187c478bd9Sstevel@tonic-gate *
1197c478bd9Sstevel@tonic-gate * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
1207c478bd9Sstevel@tonic-gate * allow value to be aligned on 8 byte boundary
1217c478bd9Sstevel@tonic-gate *
1227c478bd9Sstevel@tonic-gate * name_len is the length of the name string including the null terminator
1237c478bd9Sstevel@tonic-gate * so it must be >= 1
1247c478bd9Sstevel@tonic-gate */
1257c478bd9Sstevel@tonic-gate #define NVP_SIZE_CALC(name_len, data_len) \
1267c478bd9Sstevel@tonic-gate (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
1297c478bd9Sstevel@tonic-gate static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
1307c478bd9Sstevel@tonic-gate uint_t nelem, const void *data);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate #define NV_STAT_EMBEDDED 0x1
1337c478bd9Sstevel@tonic-gate #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
1347c478bd9Sstevel@tonic-gate #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
1377c478bd9Sstevel@tonic-gate #define NVPAIR2I_NVP(nvp) \
1387c478bd9Sstevel@tonic-gate ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate
1417c478bd9Sstevel@tonic-gate int
nv_alloc_init(nv_alloc_t * nva,const nv_alloc_ops_t * nvo,...)1427c478bd9Sstevel@tonic-gate nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate va_list valist;
1457c478bd9Sstevel@tonic-gate int err = 0;
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate nva->nva_ops = nvo;
1487c478bd9Sstevel@tonic-gate nva->nva_arg = NULL;
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate va_start(valist, nvo);
1517c478bd9Sstevel@tonic-gate if (nva->nva_ops->nv_ao_init != NULL)
1527c478bd9Sstevel@tonic-gate err = nva->nva_ops->nv_ao_init(nva, valist);
1537c478bd9Sstevel@tonic-gate va_end(valist);
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate return (err);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate void
nv_alloc_reset(nv_alloc_t * nva)1597c478bd9Sstevel@tonic-gate nv_alloc_reset(nv_alloc_t *nva)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate if (nva->nva_ops->nv_ao_reset != NULL)
1627c478bd9Sstevel@tonic-gate nva->nva_ops->nv_ao_reset(nva);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate void
nv_alloc_fini(nv_alloc_t * nva)1667c478bd9Sstevel@tonic-gate nv_alloc_fini(nv_alloc_t *nva)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate if (nva->nva_ops->nv_ao_fini != NULL)
1697c478bd9Sstevel@tonic-gate nva->nva_ops->nv_ao_fini(nva);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate nv_alloc_t *
nvlist_lookup_nv_alloc(nvlist_t * nvl)1737c478bd9Sstevel@tonic-gate nvlist_lookup_nv_alloc(nvlist_t *nvl)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate nvpriv_t *priv;
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate if (nvl == NULL ||
1787c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1797c478bd9Sstevel@tonic-gate return (NULL);
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate return (priv->nvp_nva);
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate static void *
nv_mem_zalloc(nvpriv_t * nvp,size_t size)1857c478bd9Sstevel@tonic-gate nv_mem_zalloc(nvpriv_t *nvp, size_t size)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate nv_alloc_t *nva = nvp->nvp_nva;
1887c478bd9Sstevel@tonic-gate void *buf;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
1917c478bd9Sstevel@tonic-gate bzero(buf, size);
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate return (buf);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate static void
nv_mem_free(nvpriv_t * nvp,void * buf,size_t size)1977c478bd9Sstevel@tonic-gate nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate nv_alloc_t *nva = nvp->nvp_nva;
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate nva->nva_ops->nv_ao_free(nva, buf, size);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate static void
nv_priv_init(nvpriv_t * priv,nv_alloc_t * nva,uint32_t stat)2057c478bd9Sstevel@tonic-gate nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
2067c478bd9Sstevel@tonic-gate {
207c5904d13Seschrock bzero(priv, sizeof (nvpriv_t));
2087c478bd9Sstevel@tonic-gate
2097c478bd9Sstevel@tonic-gate priv->nvp_nva = nva;
2107c478bd9Sstevel@tonic-gate priv->nvp_stat = stat;
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate
2137c478bd9Sstevel@tonic-gate static nvpriv_t *
nv_priv_alloc(nv_alloc_t * nva)2147c478bd9Sstevel@tonic-gate nv_priv_alloc(nv_alloc_t *nva)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate nvpriv_t *priv;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * nv_mem_alloc() cannot called here because it needs the priv
2207c478bd9Sstevel@tonic-gate * argument.
2217c478bd9Sstevel@tonic-gate */
2227c478bd9Sstevel@tonic-gate if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
2237c478bd9Sstevel@tonic-gate return (NULL);
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate nv_priv_init(priv, nva, 0);
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate return (priv);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate * Embedded lists need their own nvpriv_t's. We create a new
2327c478bd9Sstevel@tonic-gate * nvpriv_t using the parameters and allocator from the parent
2337c478bd9Sstevel@tonic-gate * list's nvpriv_t.
2347c478bd9Sstevel@tonic-gate */
2357c478bd9Sstevel@tonic-gate static nvpriv_t *
nv_priv_alloc_embedded(nvpriv_t * priv)2367c478bd9Sstevel@tonic-gate nv_priv_alloc_embedded(nvpriv_t *priv)
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate nvpriv_t *emb_priv;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
2417c478bd9Sstevel@tonic-gate return (NULL);
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
2447c478bd9Sstevel@tonic-gate
2457c478bd9Sstevel@tonic-gate return (emb_priv);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate static void
nvlist_init(nvlist_t * nvl,uint32_t nvflag,nvpriv_t * priv)2497c478bd9Sstevel@tonic-gate nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate nvl->nvl_version = NV_VERSION;
2527c478bd9Sstevel@tonic-gate nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
2537c478bd9Sstevel@tonic-gate nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
2547c478bd9Sstevel@tonic-gate nvl->nvl_flag = 0;
2557c478bd9Sstevel@tonic-gate nvl->nvl_pad = 0;
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate
258f6e214c7SGavin Maltby uint_t
nvlist_nvflag(nvlist_t * nvl)259f6e214c7SGavin Maltby nvlist_nvflag(nvlist_t *nvl)
260f6e214c7SGavin Maltby {
261f6e214c7SGavin Maltby return (nvl->nvl_nvflag);
262f6e214c7SGavin Maltby }
263f6e214c7SGavin Maltby
2647c478bd9Sstevel@tonic-gate /*
2657c478bd9Sstevel@tonic-gate * nvlist_alloc - Allocate nvlist.
2667c478bd9Sstevel@tonic-gate */
2677c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2687c478bd9Sstevel@tonic-gate int
nvlist_alloc(nvlist_t ** nvlp,uint_t nvflag,int kmflag)2697c478bd9Sstevel@tonic-gate nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
2707c478bd9Sstevel@tonic-gate {
2717c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
2727c478bd9Sstevel@tonic-gate return (nvlist_xalloc(nvlp, nvflag,
2737c478bd9Sstevel@tonic-gate (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2747c478bd9Sstevel@tonic-gate #else
2757c478bd9Sstevel@tonic-gate return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
2767c478bd9Sstevel@tonic-gate #endif
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate int
nvlist_xalloc(nvlist_t ** nvlp,uint_t nvflag,nv_alloc_t * nva)2807c478bd9Sstevel@tonic-gate nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate nvpriv_t *priv;
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate if (nvlp == NULL || nva == NULL)
2857c478bd9Sstevel@tonic-gate return (EINVAL);
2867c478bd9Sstevel@tonic-gate
2877c478bd9Sstevel@tonic-gate if ((priv = nv_priv_alloc(nva)) == NULL)
2887c478bd9Sstevel@tonic-gate return (ENOMEM);
2897c478bd9Sstevel@tonic-gate
2907c478bd9Sstevel@tonic-gate if ((*nvlp = nv_mem_zalloc(priv,
2917c478bd9Sstevel@tonic-gate NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
2927c478bd9Sstevel@tonic-gate nv_mem_free(priv, priv, sizeof (nvpriv_t));
2937c478bd9Sstevel@tonic-gate return (ENOMEM);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate nvlist_init(*nvlp, nvflag, priv);
2977c478bd9Sstevel@tonic-gate
2987c478bd9Sstevel@tonic-gate return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate /*
3027c478bd9Sstevel@tonic-gate * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
3037c478bd9Sstevel@tonic-gate */
3047c478bd9Sstevel@tonic-gate static nvpair_t *
nvp_buf_alloc(nvlist_t * nvl,size_t len)3057c478bd9Sstevel@tonic-gate nvp_buf_alloc(nvlist_t *nvl, size_t len)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3087c478bd9Sstevel@tonic-gate i_nvp_t *buf;
3097c478bd9Sstevel@tonic-gate nvpair_t *nvp;
3107c478bd9Sstevel@tonic-gate size_t nvsize;
3117c478bd9Sstevel@tonic-gate
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate * Allocate the buffer
3147c478bd9Sstevel@tonic-gate */
3157c478bd9Sstevel@tonic-gate nvsize = len + offsetof(i_nvp_t, nvi_nvp);
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
3187c478bd9Sstevel@tonic-gate return (NULL);
3197c478bd9Sstevel@tonic-gate
3207c478bd9Sstevel@tonic-gate nvp = &buf->nvi_nvp;
3217c478bd9Sstevel@tonic-gate nvp->nvp_size = len;
3227c478bd9Sstevel@tonic-gate
3237c478bd9Sstevel@tonic-gate return (nvp);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate * nvp_buf_free - de-Allocate an i_nvp_t.
3287c478bd9Sstevel@tonic-gate */
3297c478bd9Sstevel@tonic-gate static void
nvp_buf_free(nvlist_t * nvl,nvpair_t * nvp)3307c478bd9Sstevel@tonic-gate nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3337c478bd9Sstevel@tonic-gate size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
3367c478bd9Sstevel@tonic-gate }
3377c478bd9Sstevel@tonic-gate
3387c478bd9Sstevel@tonic-gate /*
3397c478bd9Sstevel@tonic-gate * nvp_buf_link - link a new nv pair into the nvlist.
3407c478bd9Sstevel@tonic-gate */
3417c478bd9Sstevel@tonic-gate static void
nvp_buf_link(nvlist_t * nvl,nvpair_t * nvp)3427c478bd9Sstevel@tonic-gate nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3457c478bd9Sstevel@tonic-gate i_nvp_t *curr = NVPAIR2I_NVP(nvp);
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate /* Put element at end of nvlist */
3487c478bd9Sstevel@tonic-gate if (priv->nvp_list == NULL) {
3497c478bd9Sstevel@tonic-gate priv->nvp_list = priv->nvp_last = curr;
3507c478bd9Sstevel@tonic-gate } else {
3517c478bd9Sstevel@tonic-gate curr->nvi_prev = priv->nvp_last;
3527c478bd9Sstevel@tonic-gate priv->nvp_last->nvi_next = curr;
3537c478bd9Sstevel@tonic-gate priv->nvp_last = curr;
3547c478bd9Sstevel@tonic-gate }
3557c478bd9Sstevel@tonic-gate }
3567c478bd9Sstevel@tonic-gate
3577c478bd9Sstevel@tonic-gate /*
3587c478bd9Sstevel@tonic-gate * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
3597c478bd9Sstevel@tonic-gate */
3607c478bd9Sstevel@tonic-gate static void
nvp_buf_unlink(nvlist_t * nvl,nvpair_t * nvp)3617c478bd9Sstevel@tonic-gate nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3647c478bd9Sstevel@tonic-gate i_nvp_t *curr = NVPAIR2I_NVP(nvp);
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate /*
3677c478bd9Sstevel@tonic-gate * protect nvlist_next_nvpair() against walking on freed memory.
3687c478bd9Sstevel@tonic-gate */
3697c478bd9Sstevel@tonic-gate if (priv->nvp_curr == curr)
3707c478bd9Sstevel@tonic-gate priv->nvp_curr = curr->nvi_next;
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate if (curr == priv->nvp_list)
3737c478bd9Sstevel@tonic-gate priv->nvp_list = curr->nvi_next;
3747c478bd9Sstevel@tonic-gate else
3757c478bd9Sstevel@tonic-gate curr->nvi_prev->nvi_next = curr->nvi_next;
3767c478bd9Sstevel@tonic-gate
3777c478bd9Sstevel@tonic-gate if (curr == priv->nvp_last)
3787c478bd9Sstevel@tonic-gate priv->nvp_last = curr->nvi_prev;
3797c478bd9Sstevel@tonic-gate else
3807c478bd9Sstevel@tonic-gate curr->nvi_next->nvi_prev = curr->nvi_prev;
3817c478bd9Sstevel@tonic-gate }
3827c478bd9Sstevel@tonic-gate
3837c478bd9Sstevel@tonic-gate /*
3847c478bd9Sstevel@tonic-gate * take a nvpair type and number of elements and make sure the are valid
3857c478bd9Sstevel@tonic-gate */
3867c478bd9Sstevel@tonic-gate static int
i_validate_type_nelem(data_type_t type,uint_t nelem)3877c478bd9Sstevel@tonic-gate i_validate_type_nelem(data_type_t type, uint_t nelem)
3887c478bd9Sstevel@tonic-gate {
3897c478bd9Sstevel@tonic-gate switch (type) {
3907c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
3917c478bd9Sstevel@tonic-gate if (nelem != 0)
3927c478bd9Sstevel@tonic-gate return (EINVAL);
3937c478bd9Sstevel@tonic-gate break;
3947c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
3957c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
3967c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
3977c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
3987c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
3997c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
4007c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
4017c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
4027c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
4037c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
4047c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
4057c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
4067c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
407825ba0f2Srobj #if !defined(_KERNEL)
408825ba0f2Srobj case DATA_TYPE_DOUBLE:
409825ba0f2Srobj #endif
4107c478bd9Sstevel@tonic-gate if (nelem != 1)
4117c478bd9Sstevel@tonic-gate return (EINVAL);
4127c478bd9Sstevel@tonic-gate break;
4137c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
4147c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
4157c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
4167c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
4177c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
4187c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
4197c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
4207c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
4217c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
4227c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
4237c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY:
4247c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
4257c478bd9Sstevel@tonic-gate /* we allow arrays with 0 elements */
4267c478bd9Sstevel@tonic-gate break;
4277c478bd9Sstevel@tonic-gate default:
4287c478bd9Sstevel@tonic-gate return (EINVAL);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate return (0);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate * Verify nvp_name_sz and check the name string length.
4357c478bd9Sstevel@tonic-gate */
4367c478bd9Sstevel@tonic-gate static int
i_validate_nvpair_name(nvpair_t * nvp)4377c478bd9Sstevel@tonic-gate i_validate_nvpair_name(nvpair_t *nvp)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate if ((nvp->nvp_name_sz <= 0) ||
4407c478bd9Sstevel@tonic-gate (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
4417c478bd9Sstevel@tonic-gate return (EFAULT);
4427c478bd9Sstevel@tonic-gate
4437c478bd9Sstevel@tonic-gate /* verify the name string, make sure its terminated */
4447c478bd9Sstevel@tonic-gate if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
4457c478bd9Sstevel@tonic-gate return (EFAULT);
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate static int
i_validate_nvpair_value(data_type_t type,uint_t nelem,const void * data)4517c478bd9Sstevel@tonic-gate i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate switch (type) {
4547c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
4557c478bd9Sstevel@tonic-gate if (*(boolean_t *)data != B_TRUE &&
4567c478bd9Sstevel@tonic-gate *(boolean_t *)data != B_FALSE)
4577c478bd9Sstevel@tonic-gate return (EINVAL);
4587c478bd9Sstevel@tonic-gate break;
4597c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY: {
4607c478bd9Sstevel@tonic-gate int i;
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++)
4637c478bd9Sstevel@tonic-gate if (((boolean_t *)data)[i] != B_TRUE &&
4647c478bd9Sstevel@tonic-gate ((boolean_t *)data)[i] != B_FALSE)
4657c478bd9Sstevel@tonic-gate return (EINVAL);
4667c478bd9Sstevel@tonic-gate break;
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate default:
4697c478bd9Sstevel@tonic-gate break;
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate return (0);
4737c478bd9Sstevel@tonic-gate }
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate * This function takes a pointer to what should be a nvpair and it's size
4777c478bd9Sstevel@tonic-gate * and then verifies that all the nvpair fields make sense and can be
4787c478bd9Sstevel@tonic-gate * trusted. This function is used when decoding packed nvpairs.
4797c478bd9Sstevel@tonic-gate */
4807c478bd9Sstevel@tonic-gate static int
i_validate_nvpair(nvpair_t * nvp)4817c478bd9Sstevel@tonic-gate i_validate_nvpair(nvpair_t *nvp)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate data_type_t type = NVP_TYPE(nvp);
4847c478bd9Sstevel@tonic-gate int size1, size2;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate /* verify nvp_name_sz, check the name string length */
4877c478bd9Sstevel@tonic-gate if (i_validate_nvpair_name(nvp) != 0)
4887c478bd9Sstevel@tonic-gate return (EFAULT);
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
4917c478bd9Sstevel@tonic-gate return (EFAULT);
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /*
4947c478bd9Sstevel@tonic-gate * verify nvp_type, nvp_value_elem, and also possibly
4957c478bd9Sstevel@tonic-gate * verify string values and get the value size.
4967c478bd9Sstevel@tonic-gate */
4977c478bd9Sstevel@tonic-gate size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
4987c478bd9Sstevel@tonic-gate size1 = nvp->nvp_size - NVP_VALOFF(nvp);
4997c478bd9Sstevel@tonic-gate if (size2 < 0 || size1 != NV_ALIGN(size2))
5007c478bd9Sstevel@tonic-gate return (EFAULT);
5017c478bd9Sstevel@tonic-gate
5027c478bd9Sstevel@tonic-gate return (0);
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate static int
nvlist_copy_pairs(nvlist_t * snvl,nvlist_t * dnvl)5067c478bd9Sstevel@tonic-gate nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate nvpriv_t *priv;
5097c478bd9Sstevel@tonic-gate i_nvp_t *curr;
5107c478bd9Sstevel@tonic-gate
5117c478bd9Sstevel@tonic-gate if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
5127c478bd9Sstevel@tonic-gate return (EINVAL);
5137c478bd9Sstevel@tonic-gate
5147c478bd9Sstevel@tonic-gate for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
5157c478bd9Sstevel@tonic-gate nvpair_t *nvp = &curr->nvi_nvp;
5167c478bd9Sstevel@tonic-gate int err;
5177c478bd9Sstevel@tonic-gate
5187c478bd9Sstevel@tonic-gate if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
5197c478bd9Sstevel@tonic-gate NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
5207c478bd9Sstevel@tonic-gate return (err);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate
5237c478bd9Sstevel@tonic-gate return (0);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate * Frees all memory allocated for an nvpair (like embedded lists) with
5287c478bd9Sstevel@tonic-gate * the exception of the nvpair buffer itself.
5297c478bd9Sstevel@tonic-gate */
5307c478bd9Sstevel@tonic-gate static void
nvpair_free(nvpair_t * nvp)5317c478bd9Sstevel@tonic-gate nvpair_free(nvpair_t *nvp)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate switch (NVP_TYPE(nvp)) {
5347c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
5357c478bd9Sstevel@tonic-gate nvlist_free(EMBEDDED_NVL(nvp));
5367c478bd9Sstevel@tonic-gate break;
5377c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: {
5387c478bd9Sstevel@tonic-gate nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
5397c478bd9Sstevel@tonic-gate int i;
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate for (i = 0; i < NVP_NELEM(nvp); i++)
5427c478bd9Sstevel@tonic-gate nvlist_free(nvlp[i]);
5437c478bd9Sstevel@tonic-gate break;
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate default:
5467c478bd9Sstevel@tonic-gate break;
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate * nvlist_free - free an unpacked nvlist
5527c478bd9Sstevel@tonic-gate */
5537c478bd9Sstevel@tonic-gate void
nvlist_free(nvlist_t * nvl)5547c478bd9Sstevel@tonic-gate nvlist_free(nvlist_t *nvl)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate nvpriv_t *priv;
5577c478bd9Sstevel@tonic-gate i_nvp_t *curr;
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate if (nvl == NULL ||
5607c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
5617c478bd9Sstevel@tonic-gate return;
5627c478bd9Sstevel@tonic-gate
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate * Unpacked nvlist are linked through i_nvp_t
5657c478bd9Sstevel@tonic-gate */
5667c478bd9Sstevel@tonic-gate curr = priv->nvp_list;
5677c478bd9Sstevel@tonic-gate while (curr != NULL) {
5687c478bd9Sstevel@tonic-gate nvpair_t *nvp = &curr->nvi_nvp;
5697c478bd9Sstevel@tonic-gate curr = curr->nvi_next;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate nvpair_free(nvp);
5727c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate
5757c478bd9Sstevel@tonic-gate if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
5767c478bd9Sstevel@tonic-gate nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
5777c478bd9Sstevel@tonic-gate else
5785ad82045Snd150628 nvl->nvl_priv = 0;
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate nv_mem_free(priv, priv, sizeof (nvpriv_t));
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate
5837c478bd9Sstevel@tonic-gate static int
nvlist_contains_nvp(nvlist_t * nvl,nvpair_t * nvp)5847c478bd9Sstevel@tonic-gate nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
5877c478bd9Sstevel@tonic-gate i_nvp_t *curr;
5887c478bd9Sstevel@tonic-gate
5897c478bd9Sstevel@tonic-gate if (nvp == NULL)
5907c478bd9Sstevel@tonic-gate return (0);
5917c478bd9Sstevel@tonic-gate
5927c478bd9Sstevel@tonic-gate for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
5937c478bd9Sstevel@tonic-gate if (&curr->nvi_nvp == nvp)
5947c478bd9Sstevel@tonic-gate return (1);
5957c478bd9Sstevel@tonic-gate
5967c478bd9Sstevel@tonic-gate return (0);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate
5997c478bd9Sstevel@tonic-gate /*
6007c478bd9Sstevel@tonic-gate * Make a copy of nvlist
6017c478bd9Sstevel@tonic-gate */
6027c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
6037c478bd9Sstevel@tonic-gate int
nvlist_dup(nvlist_t * nvl,nvlist_t ** nvlp,int kmflag)6047c478bd9Sstevel@tonic-gate nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
6077c478bd9Sstevel@tonic-gate return (nvlist_xdup(nvl, nvlp,
6087c478bd9Sstevel@tonic-gate (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
6097c478bd9Sstevel@tonic-gate #else
6107c478bd9Sstevel@tonic-gate return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
6117c478bd9Sstevel@tonic-gate #endif
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate
6147c478bd9Sstevel@tonic-gate int
nvlist_xdup(nvlist_t * nvl,nvlist_t ** nvlp,nv_alloc_t * nva)6157c478bd9Sstevel@tonic-gate nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
6167c478bd9Sstevel@tonic-gate {
6177c478bd9Sstevel@tonic-gate int err;
6183bb79becSeschrock nvlist_t *ret;
6197c478bd9Sstevel@tonic-gate
6207c478bd9Sstevel@tonic-gate if (nvl == NULL || nvlp == NULL)
6217c478bd9Sstevel@tonic-gate return (EINVAL);
6227c478bd9Sstevel@tonic-gate
6233bb79becSeschrock if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
6247c478bd9Sstevel@tonic-gate return (err);
6257c478bd9Sstevel@tonic-gate
6263bb79becSeschrock if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
6273bb79becSeschrock nvlist_free(ret);
6283bb79becSeschrock else
6293bb79becSeschrock *nvlp = ret;
6307c478bd9Sstevel@tonic-gate
6317c478bd9Sstevel@tonic-gate return (err);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate * Remove all with matching name
6367c478bd9Sstevel@tonic-gate */
6377c478bd9Sstevel@tonic-gate int
nvlist_remove_all(nvlist_t * nvl,const char * name)6387c478bd9Sstevel@tonic-gate nvlist_remove_all(nvlist_t *nvl, const char *name)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate nvpriv_t *priv;
6417c478bd9Sstevel@tonic-gate i_nvp_t *curr;
6427c478bd9Sstevel@tonic-gate int error = ENOENT;
6437c478bd9Sstevel@tonic-gate
6447c478bd9Sstevel@tonic-gate if (nvl == NULL || name == NULL ||
6457c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
6467c478bd9Sstevel@tonic-gate return (EINVAL);
6477c478bd9Sstevel@tonic-gate
6487c478bd9Sstevel@tonic-gate curr = priv->nvp_list;
6497c478bd9Sstevel@tonic-gate while (curr != NULL) {
6507c478bd9Sstevel@tonic-gate nvpair_t *nvp = &curr->nvi_nvp;
6517c478bd9Sstevel@tonic-gate
6527c478bd9Sstevel@tonic-gate curr = curr->nvi_next;
6537c478bd9Sstevel@tonic-gate if (strcmp(name, NVP_NAME(nvp)) != 0)
6547c478bd9Sstevel@tonic-gate continue;
6557c478bd9Sstevel@tonic-gate
6567c478bd9Sstevel@tonic-gate nvp_buf_unlink(nvl, nvp);
6577c478bd9Sstevel@tonic-gate nvpair_free(nvp);
6587c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
6597c478bd9Sstevel@tonic-gate
6607c478bd9Sstevel@tonic-gate error = 0;
6617c478bd9Sstevel@tonic-gate }
6627c478bd9Sstevel@tonic-gate
6637c478bd9Sstevel@tonic-gate return (error);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate * Remove first one with matching name and type
6687c478bd9Sstevel@tonic-gate */
6697c478bd9Sstevel@tonic-gate int
nvlist_remove(nvlist_t * nvl,const char * name,data_type_t type)6707c478bd9Sstevel@tonic-gate nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
6717c478bd9Sstevel@tonic-gate {
6727c478bd9Sstevel@tonic-gate nvpriv_t *priv;
6737c478bd9Sstevel@tonic-gate i_nvp_t *curr;
6747c478bd9Sstevel@tonic-gate
6757c478bd9Sstevel@tonic-gate if (nvl == NULL || name == NULL ||
6767c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
6777c478bd9Sstevel@tonic-gate return (EINVAL);
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate curr = priv->nvp_list;
6807c478bd9Sstevel@tonic-gate while (curr != NULL) {
6817c478bd9Sstevel@tonic-gate nvpair_t *nvp = &curr->nvi_nvp;
6827c478bd9Sstevel@tonic-gate
6837c478bd9Sstevel@tonic-gate if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
6847c478bd9Sstevel@tonic-gate nvp_buf_unlink(nvl, nvp);
6857c478bd9Sstevel@tonic-gate nvpair_free(nvp);
6867c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate return (0);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate curr = curr->nvi_next;
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate return (ENOENT);
6947c478bd9Sstevel@tonic-gate }
6957c478bd9Sstevel@tonic-gate
69692241e0bSTom Erickson int
nvlist_remove_nvpair(nvlist_t * nvl,nvpair_t * nvp)69792241e0bSTom Erickson nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
69892241e0bSTom Erickson {
69992241e0bSTom Erickson if (nvl == NULL || nvp == NULL)
70092241e0bSTom Erickson return (EINVAL);
70192241e0bSTom Erickson
70292241e0bSTom Erickson nvp_buf_unlink(nvl, nvp);
70392241e0bSTom Erickson nvpair_free(nvp);
70492241e0bSTom Erickson nvp_buf_free(nvl, nvp);
70592241e0bSTom Erickson return (0);
70692241e0bSTom Erickson }
70792241e0bSTom Erickson
7087c478bd9Sstevel@tonic-gate /*
7097c478bd9Sstevel@tonic-gate * This function calculates the size of an nvpair value.
7107c478bd9Sstevel@tonic-gate *
7117c478bd9Sstevel@tonic-gate * The data argument controls the behavior in case of the data types
7127c478bd9Sstevel@tonic-gate * DATA_TYPE_STRING and
7137c478bd9Sstevel@tonic-gate * DATA_TYPE_STRING_ARRAY
7147c478bd9Sstevel@tonic-gate * Is data == NULL then the size of the string(s) is excluded.
7157c478bd9Sstevel@tonic-gate */
7167c478bd9Sstevel@tonic-gate static int
i_get_value_size(data_type_t type,const void * data,uint_t nelem)7177c478bd9Sstevel@tonic-gate i_get_value_size(data_type_t type, const void *data, uint_t nelem)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate uint64_t value_sz;
7207c478bd9Sstevel@tonic-gate
7217c478bd9Sstevel@tonic-gate if (i_validate_type_nelem(type, nelem) != 0)
7227c478bd9Sstevel@tonic-gate return (-1);
7237c478bd9Sstevel@tonic-gate
7247c478bd9Sstevel@tonic-gate /* Calculate required size for holding value */
7257c478bd9Sstevel@tonic-gate switch (type) {
7267c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
7277c478bd9Sstevel@tonic-gate value_sz = 0;
7287c478bd9Sstevel@tonic-gate break;
7297c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
7307c478bd9Sstevel@tonic-gate value_sz = sizeof (boolean_t);
7317c478bd9Sstevel@tonic-gate break;
7327c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
7337c478bd9Sstevel@tonic-gate value_sz = sizeof (uchar_t);
7347c478bd9Sstevel@tonic-gate break;
7357c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
7367c478bd9Sstevel@tonic-gate value_sz = sizeof (int8_t);
7377c478bd9Sstevel@tonic-gate break;
7387c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
7397c478bd9Sstevel@tonic-gate value_sz = sizeof (uint8_t);
7407c478bd9Sstevel@tonic-gate break;
7417c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
7427c478bd9Sstevel@tonic-gate value_sz = sizeof (int16_t);
7437c478bd9Sstevel@tonic-gate break;
7447c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
7457c478bd9Sstevel@tonic-gate value_sz = sizeof (uint16_t);
7467c478bd9Sstevel@tonic-gate break;
7477c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
7487c478bd9Sstevel@tonic-gate value_sz = sizeof (int32_t);
7497c478bd9Sstevel@tonic-gate break;
7507c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
7517c478bd9Sstevel@tonic-gate value_sz = sizeof (uint32_t);
7527c478bd9Sstevel@tonic-gate break;
7537c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
7547c478bd9Sstevel@tonic-gate value_sz = sizeof (int64_t);
7557c478bd9Sstevel@tonic-gate break;
7567c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
7577c478bd9Sstevel@tonic-gate value_sz = sizeof (uint64_t);
7587c478bd9Sstevel@tonic-gate break;
759825ba0f2Srobj #if !defined(_KERNEL)
760825ba0f2Srobj case DATA_TYPE_DOUBLE:
761825ba0f2Srobj value_sz = sizeof (double);
762825ba0f2Srobj break;
763825ba0f2Srobj #endif
7647c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
7657c478bd9Sstevel@tonic-gate if (data == NULL)
7667c478bd9Sstevel@tonic-gate value_sz = 0;
7677c478bd9Sstevel@tonic-gate else
7687c478bd9Sstevel@tonic-gate value_sz = strlen(data) + 1;
7697c478bd9Sstevel@tonic-gate break;
7707c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
7717c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (boolean_t);
7727c478bd9Sstevel@tonic-gate break;
7737c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
7747c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uchar_t);
7757c478bd9Sstevel@tonic-gate break;
7767c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
7777c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (int8_t);
7787c478bd9Sstevel@tonic-gate break;
7797c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
7807c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint8_t);
7817c478bd9Sstevel@tonic-gate break;
7827c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
7837c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (int16_t);
7847c478bd9Sstevel@tonic-gate break;
7857c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
7867c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint16_t);
7877c478bd9Sstevel@tonic-gate break;
7887c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
7897c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (int32_t);
7907c478bd9Sstevel@tonic-gate break;
7917c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
7927c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint32_t);
7937c478bd9Sstevel@tonic-gate break;
7947c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
7957c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (int64_t);
7967c478bd9Sstevel@tonic-gate break;
7977c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
7987c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint64_t);
7997c478bd9Sstevel@tonic-gate break;
8007c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY:
8017c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint64_t);
8027c478bd9Sstevel@tonic-gate
8037c478bd9Sstevel@tonic-gate if (data != NULL) {
8047c478bd9Sstevel@tonic-gate char *const *strs = data;
8057c478bd9Sstevel@tonic-gate uint_t i;
8067c478bd9Sstevel@tonic-gate
8077c478bd9Sstevel@tonic-gate /* no alignment requirement for strings */
8087c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
8097c478bd9Sstevel@tonic-gate if (strs[i] == NULL)
8107c478bd9Sstevel@tonic-gate return (-1);
8117c478bd9Sstevel@tonic-gate value_sz += strlen(strs[i]) + 1;
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate break;
8157c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
8167c478bd9Sstevel@tonic-gate value_sz = sizeof (hrtime_t);
8177c478bd9Sstevel@tonic-gate break;
8187c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
8197c478bd9Sstevel@tonic-gate value_sz = NV_ALIGN(sizeof (nvlist_t));
8207c478bd9Sstevel@tonic-gate break;
8217c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
8227c478bd9Sstevel@tonic-gate value_sz = (uint64_t)nelem * sizeof (uint64_t) +
8237c478bd9Sstevel@tonic-gate (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
8247c478bd9Sstevel@tonic-gate break;
8257c478bd9Sstevel@tonic-gate default:
8267c478bd9Sstevel@tonic-gate return (-1);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate return (value_sz > INT32_MAX ? -1 : (int)value_sz);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate static int
nvlist_copy_embedded(nvlist_t * nvl,nvlist_t * onvl,nvlist_t * emb_nvl)8337c478bd9Sstevel@tonic-gate nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate nvpriv_t *priv;
8367c478bd9Sstevel@tonic-gate int err;
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
8397c478bd9Sstevel@tonic-gate nvl->nvl_priv)) == NULL)
8407c478bd9Sstevel@tonic-gate return (ENOMEM);
8417c478bd9Sstevel@tonic-gate
8427c478bd9Sstevel@tonic-gate nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
8457c478bd9Sstevel@tonic-gate nvlist_free(emb_nvl);
8467c478bd9Sstevel@tonic-gate emb_nvl->nvl_priv = 0;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate
8497c478bd9Sstevel@tonic-gate return (err);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate * nvlist_add_common - Add new <name,value> pair to nvlist
8547c478bd9Sstevel@tonic-gate */
8557c478bd9Sstevel@tonic-gate static int
nvlist_add_common(nvlist_t * nvl,const char * name,data_type_t type,uint_t nelem,const void * data)8567c478bd9Sstevel@tonic-gate nvlist_add_common(nvlist_t *nvl, const char *name,
8577c478bd9Sstevel@tonic-gate data_type_t type, uint_t nelem, const void *data)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate nvpair_t *nvp;
860d9638e54Smws uint_t i;
861d9638e54Smws
8627c478bd9Sstevel@tonic-gate int nvp_sz, name_sz, value_sz;
8637c478bd9Sstevel@tonic-gate int err = 0;
8647c478bd9Sstevel@tonic-gate
8657c478bd9Sstevel@tonic-gate if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
8667c478bd9Sstevel@tonic-gate return (EINVAL);
8677c478bd9Sstevel@tonic-gate
8687c478bd9Sstevel@tonic-gate if (nelem != 0 && data == NULL)
8697c478bd9Sstevel@tonic-gate return (EINVAL);
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate /*
8727c478bd9Sstevel@tonic-gate * Verify type and nelem and get the value size.
8737c478bd9Sstevel@tonic-gate * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
8747c478bd9Sstevel@tonic-gate * is the size of the string(s) included.
8757c478bd9Sstevel@tonic-gate */
8767c478bd9Sstevel@tonic-gate if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
8777c478bd9Sstevel@tonic-gate return (EINVAL);
8787c478bd9Sstevel@tonic-gate
8797c478bd9Sstevel@tonic-gate if (i_validate_nvpair_value(type, nelem, data) != 0)
8807c478bd9Sstevel@tonic-gate return (EINVAL);
8817c478bd9Sstevel@tonic-gate
882d9638e54Smws /*
883d9638e54Smws * If we're adding an nvlist or nvlist array, ensure that we are not
884d9638e54Smws * adding the input nvlist to itself, which would cause recursion,
885d9638e54Smws * and ensure that no NULL nvlist pointers are present.
886d9638e54Smws */
8877c478bd9Sstevel@tonic-gate switch (type) {
8887c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
889d9638e54Smws if (data == nvl || data == NULL)
8907c478bd9Sstevel@tonic-gate return (EINVAL);
8917c478bd9Sstevel@tonic-gate break;
8927c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: {
893d9638e54Smws nvlist_t **onvlp = (nvlist_t **)data;
894d9638e54Smws for (i = 0; i < nelem; i++) {
895d9638e54Smws if (onvlp[i] == nvl || onvlp[i] == NULL)
8967c478bd9Sstevel@tonic-gate return (EINVAL);
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate break;
899d9638e54Smws }
9005ad82045Snd150628 default:
9015ad82045Snd150628 break;
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate
9047c478bd9Sstevel@tonic-gate /* calculate sizes of the nvpair elements and the nvpair itself */
9057c478bd9Sstevel@tonic-gate name_sz = strlen(name) + 1;
9067c478bd9Sstevel@tonic-gate
9077c478bd9Sstevel@tonic-gate nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
9087c478bd9Sstevel@tonic-gate
9097c478bd9Sstevel@tonic-gate if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
9107c478bd9Sstevel@tonic-gate return (ENOMEM);
9117c478bd9Sstevel@tonic-gate
9127c478bd9Sstevel@tonic-gate ASSERT(nvp->nvp_size == nvp_sz);
9137c478bd9Sstevel@tonic-gate nvp->nvp_name_sz = name_sz;
9147c478bd9Sstevel@tonic-gate nvp->nvp_value_elem = nelem;
9157c478bd9Sstevel@tonic-gate nvp->nvp_type = type;
9167c478bd9Sstevel@tonic-gate bcopy(name, NVP_NAME(nvp), name_sz);
9177c478bd9Sstevel@tonic-gate
9187c478bd9Sstevel@tonic-gate switch (type) {
9197c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
9207c478bd9Sstevel@tonic-gate break;
9217c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY: {
9227c478bd9Sstevel@tonic-gate char *const *strs = data;
9237c478bd9Sstevel@tonic-gate char *buf = NVP_VALUE(nvp);
9247c478bd9Sstevel@tonic-gate char **cstrs = (void *)buf;
9257c478bd9Sstevel@tonic-gate
9267c478bd9Sstevel@tonic-gate /* skip pre-allocated space for pointer array */
9277c478bd9Sstevel@tonic-gate buf += nelem * sizeof (uint64_t);
9287c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
9297c478bd9Sstevel@tonic-gate int slen = strlen(strs[i]) + 1;
9307c478bd9Sstevel@tonic-gate bcopy(strs[i], buf, slen);
9317c478bd9Sstevel@tonic-gate cstrs[i] = buf;
9327c478bd9Sstevel@tonic-gate buf += slen;
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate break;
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST: {
9377c478bd9Sstevel@tonic-gate nvlist_t *nnvl = EMBEDDED_NVL(nvp);
9387c478bd9Sstevel@tonic-gate nvlist_t *onvl = (nvlist_t *)data;
9397c478bd9Sstevel@tonic-gate
9407c478bd9Sstevel@tonic-gate if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
9417c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
9427c478bd9Sstevel@tonic-gate return (err);
9437c478bd9Sstevel@tonic-gate }
9447c478bd9Sstevel@tonic-gate break;
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: {
9477c478bd9Sstevel@tonic-gate nvlist_t **onvlp = (nvlist_t **)data;
9487c478bd9Sstevel@tonic-gate nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
9497c478bd9Sstevel@tonic-gate nvlist_t *embedded = (nvlist_t *)
9507c478bd9Sstevel@tonic-gate ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
9517c478bd9Sstevel@tonic-gate
9527c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
9537c478bd9Sstevel@tonic-gate if ((err = nvlist_copy_embedded(nvl,
9547c478bd9Sstevel@tonic-gate onvlp[i], embedded)) != 0) {
9557c478bd9Sstevel@tonic-gate /*
9567c478bd9Sstevel@tonic-gate * Free any successfully created lists
9577c478bd9Sstevel@tonic-gate */
9587c478bd9Sstevel@tonic-gate nvpair_free(nvp);
9597c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
9607c478bd9Sstevel@tonic-gate return (err);
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate
9637c478bd9Sstevel@tonic-gate nvlp[i] = embedded++;
9647c478bd9Sstevel@tonic-gate }
9657c478bd9Sstevel@tonic-gate break;
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate default:
9687c478bd9Sstevel@tonic-gate bcopy(data, NVP_VALUE(nvp), value_sz);
9697c478bd9Sstevel@tonic-gate }
9707c478bd9Sstevel@tonic-gate
9717c478bd9Sstevel@tonic-gate /* if unique name, remove before add */
9727c478bd9Sstevel@tonic-gate if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
9737c478bd9Sstevel@tonic-gate (void) nvlist_remove_all(nvl, name);
9747c478bd9Sstevel@tonic-gate else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
9757c478bd9Sstevel@tonic-gate (void) nvlist_remove(nvl, name, type);
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate nvp_buf_link(nvl, nvp);
9787c478bd9Sstevel@tonic-gate
9797c478bd9Sstevel@tonic-gate return (0);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate
9827c478bd9Sstevel@tonic-gate int
nvlist_add_boolean(nvlist_t * nvl,const char * name)9837c478bd9Sstevel@tonic-gate nvlist_add_boolean(nvlist_t *nvl, const char *name)
9847c478bd9Sstevel@tonic-gate {
9857c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate int
nvlist_add_boolean_value(nvlist_t * nvl,const char * name,boolean_t val)9897c478bd9Sstevel@tonic-gate nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
9907c478bd9Sstevel@tonic-gate {
9917c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate
9947c478bd9Sstevel@tonic-gate int
nvlist_add_byte(nvlist_t * nvl,const char * name,uchar_t val)9957c478bd9Sstevel@tonic-gate nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
9967c478bd9Sstevel@tonic-gate {
9977c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
9987c478bd9Sstevel@tonic-gate }
9997c478bd9Sstevel@tonic-gate
10007c478bd9Sstevel@tonic-gate int
nvlist_add_int8(nvlist_t * nvl,const char * name,int8_t val)10017c478bd9Sstevel@tonic-gate nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
10027c478bd9Sstevel@tonic-gate {
10037c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate
10067c478bd9Sstevel@tonic-gate int
nvlist_add_uint8(nvlist_t * nvl,const char * name,uint8_t val)10077c478bd9Sstevel@tonic-gate nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate
10127c478bd9Sstevel@tonic-gate int
nvlist_add_int16(nvlist_t * nvl,const char * name,int16_t val)10137c478bd9Sstevel@tonic-gate nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate
10187c478bd9Sstevel@tonic-gate int
nvlist_add_uint16(nvlist_t * nvl,const char * name,uint16_t val)10197c478bd9Sstevel@tonic-gate nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
10207c478bd9Sstevel@tonic-gate {
10217c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
10227c478bd9Sstevel@tonic-gate }
10237c478bd9Sstevel@tonic-gate
10247c478bd9Sstevel@tonic-gate int
nvlist_add_int32(nvlist_t * nvl,const char * name,int32_t val)10257c478bd9Sstevel@tonic-gate nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
10267c478bd9Sstevel@tonic-gate {
10277c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
10287c478bd9Sstevel@tonic-gate }
10297c478bd9Sstevel@tonic-gate
10307c478bd9Sstevel@tonic-gate int
nvlist_add_uint32(nvlist_t * nvl,const char * name,uint32_t val)10317c478bd9Sstevel@tonic-gate nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate
10367c478bd9Sstevel@tonic-gate int
nvlist_add_int64(nvlist_t * nvl,const char * name,int64_t val)10377c478bd9Sstevel@tonic-gate nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate
10427c478bd9Sstevel@tonic-gate int
nvlist_add_uint64(nvlist_t * nvl,const char * name,uint64_t val)10437c478bd9Sstevel@tonic-gate nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate
1048825ba0f2Srobj #if !defined(_KERNEL)
1049825ba0f2Srobj int
nvlist_add_double(nvlist_t * nvl,const char * name,double val)1050825ba0f2Srobj nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1051825ba0f2Srobj {
1052825ba0f2Srobj return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1053825ba0f2Srobj }
1054825ba0f2Srobj #endif
1055825ba0f2Srobj
10567c478bd9Sstevel@tonic-gate int
nvlist_add_string(nvlist_t * nvl,const char * name,const char * val)10577c478bd9Sstevel@tonic-gate nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
10607c478bd9Sstevel@tonic-gate }
10617c478bd9Sstevel@tonic-gate
10627c478bd9Sstevel@tonic-gate int
nvlist_add_boolean_array(nvlist_t * nvl,const char * name,boolean_t * a,uint_t n)10637c478bd9Sstevel@tonic-gate nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
10647c478bd9Sstevel@tonic-gate boolean_t *a, uint_t n)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
10677c478bd9Sstevel@tonic-gate }
10687c478bd9Sstevel@tonic-gate
10697c478bd9Sstevel@tonic-gate int
nvlist_add_byte_array(nvlist_t * nvl,const char * name,uchar_t * a,uint_t n)10707c478bd9Sstevel@tonic-gate nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
10717c478bd9Sstevel@tonic-gate {
10727c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate
10757c478bd9Sstevel@tonic-gate int
nvlist_add_int8_array(nvlist_t * nvl,const char * name,int8_t * a,uint_t n)10767c478bd9Sstevel@tonic-gate nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
10777c478bd9Sstevel@tonic-gate {
10787c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
10797c478bd9Sstevel@tonic-gate }
10807c478bd9Sstevel@tonic-gate
10817c478bd9Sstevel@tonic-gate int
nvlist_add_uint8_array(nvlist_t * nvl,const char * name,uint8_t * a,uint_t n)10827c478bd9Sstevel@tonic-gate nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate
10877c478bd9Sstevel@tonic-gate int
nvlist_add_int16_array(nvlist_t * nvl,const char * name,int16_t * a,uint_t n)10887c478bd9Sstevel@tonic-gate nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate
10937c478bd9Sstevel@tonic-gate int
nvlist_add_uint16_array(nvlist_t * nvl,const char * name,uint16_t * a,uint_t n)10947c478bd9Sstevel@tonic-gate nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate
10997c478bd9Sstevel@tonic-gate int
nvlist_add_int32_array(nvlist_t * nvl,const char * name,int32_t * a,uint_t n)11007c478bd9Sstevel@tonic-gate nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
11037c478bd9Sstevel@tonic-gate }
11047c478bd9Sstevel@tonic-gate
11057c478bd9Sstevel@tonic-gate int
nvlist_add_uint32_array(nvlist_t * nvl,const char * name,uint32_t * a,uint_t n)11067c478bd9Sstevel@tonic-gate nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
11097c478bd9Sstevel@tonic-gate }
11107c478bd9Sstevel@tonic-gate
11117c478bd9Sstevel@tonic-gate int
nvlist_add_int64_array(nvlist_t * nvl,const char * name,int64_t * a,uint_t n)11127c478bd9Sstevel@tonic-gate nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate
11177c478bd9Sstevel@tonic-gate int
nvlist_add_uint64_array(nvlist_t * nvl,const char * name,uint64_t * a,uint_t n)11187c478bd9Sstevel@tonic-gate nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
11217c478bd9Sstevel@tonic-gate }
11227c478bd9Sstevel@tonic-gate
11237c478bd9Sstevel@tonic-gate int
nvlist_add_string_array(nvlist_t * nvl,const char * name,char * const * a,uint_t n)11247c478bd9Sstevel@tonic-gate nvlist_add_string_array(nvlist_t *nvl, const char *name,
11257c478bd9Sstevel@tonic-gate char *const *a, uint_t n)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate
11307c478bd9Sstevel@tonic-gate int
nvlist_add_hrtime(nvlist_t * nvl,const char * name,hrtime_t val)11317c478bd9Sstevel@tonic-gate nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
11327c478bd9Sstevel@tonic-gate {
11337c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate
11367c478bd9Sstevel@tonic-gate int
nvlist_add_nvlist(nvlist_t * nvl,const char * name,nvlist_t * val)11377c478bd9Sstevel@tonic-gate nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
11407c478bd9Sstevel@tonic-gate }
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate int
nvlist_add_nvlist_array(nvlist_t * nvl,const char * name,nvlist_t ** a,uint_t n)11437c478bd9Sstevel@tonic-gate nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate
11487c478bd9Sstevel@tonic-gate /* reading name-value pairs */
11497c478bd9Sstevel@tonic-gate nvpair_t *
nvlist_next_nvpair(nvlist_t * nvl,nvpair_t * nvp)11507c478bd9Sstevel@tonic-gate nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate nvpriv_t *priv;
11537c478bd9Sstevel@tonic-gate i_nvp_t *curr;
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate if (nvl == NULL ||
11567c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
11577c478bd9Sstevel@tonic-gate return (NULL);
11587c478bd9Sstevel@tonic-gate
11597c478bd9Sstevel@tonic-gate curr = NVPAIR2I_NVP(nvp);
11607c478bd9Sstevel@tonic-gate
11617c478bd9Sstevel@tonic-gate /*
11623cb34c60Sahrens * Ensure that nvp is a valid nvpair on this nvlist.
11633cb34c60Sahrens * NB: nvp_curr is used only as a hint so that we don't always
11643cb34c60Sahrens * have to walk the list to determine if nvp is still on the list.
11657c478bd9Sstevel@tonic-gate */
11667c478bd9Sstevel@tonic-gate if (nvp == NULL)
11677c478bd9Sstevel@tonic-gate curr = priv->nvp_list;
11683cb34c60Sahrens else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
11697c478bd9Sstevel@tonic-gate curr = curr->nvi_next;
11703cb34c60Sahrens else
11717c478bd9Sstevel@tonic-gate curr = NULL;
11727c478bd9Sstevel@tonic-gate
11737c478bd9Sstevel@tonic-gate priv->nvp_curr = curr;
11747c478bd9Sstevel@tonic-gate
11757c478bd9Sstevel@tonic-gate return (curr != NULL ? &curr->nvi_nvp : NULL);
11767c478bd9Sstevel@tonic-gate }
11777c478bd9Sstevel@tonic-gate
117892241e0bSTom Erickson nvpair_t *
nvlist_prev_nvpair(nvlist_t * nvl,nvpair_t * nvp)117992241e0bSTom Erickson nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
118092241e0bSTom Erickson {
118192241e0bSTom Erickson nvpriv_t *priv;
118292241e0bSTom Erickson i_nvp_t *curr;
118392241e0bSTom Erickson
118492241e0bSTom Erickson if (nvl == NULL ||
118592241e0bSTom Erickson (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
118692241e0bSTom Erickson return (NULL);
118792241e0bSTom Erickson
118892241e0bSTom Erickson curr = NVPAIR2I_NVP(nvp);
118992241e0bSTom Erickson
119092241e0bSTom Erickson if (nvp == NULL)
119192241e0bSTom Erickson curr = priv->nvp_last;
119292241e0bSTom Erickson else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
119392241e0bSTom Erickson curr = curr->nvi_prev;
119492241e0bSTom Erickson else
119592241e0bSTom Erickson curr = NULL;
119692241e0bSTom Erickson
119792241e0bSTom Erickson priv->nvp_curr = curr;
119892241e0bSTom Erickson
119992241e0bSTom Erickson return (curr != NULL ? &curr->nvi_nvp : NULL);
120092241e0bSTom Erickson }
120192241e0bSTom Erickson
120292241e0bSTom Erickson boolean_t
nvlist_empty(nvlist_t * nvl)120392241e0bSTom Erickson nvlist_empty(nvlist_t *nvl)
120492241e0bSTom Erickson {
120592241e0bSTom Erickson nvpriv_t *priv;
120692241e0bSTom Erickson
120792241e0bSTom Erickson if (nvl == NULL ||
120892241e0bSTom Erickson (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
120992241e0bSTom Erickson return (B_TRUE);
121092241e0bSTom Erickson
121192241e0bSTom Erickson return (priv->nvp_list == NULL);
121292241e0bSTom Erickson }
121392241e0bSTom Erickson
12147c478bd9Sstevel@tonic-gate char *
nvpair_name(nvpair_t * nvp)12157c478bd9Sstevel@tonic-gate nvpair_name(nvpair_t *nvp)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate return (NVP_NAME(nvp));
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate data_type_t
nvpair_type(nvpair_t * nvp)12217c478bd9Sstevel@tonic-gate nvpair_type(nvpair_t *nvp)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate return (NVP_TYPE(nvp));
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate
1226602ca9eaScth int
nvpair_type_is_array(nvpair_t * nvp)1227602ca9eaScth nvpair_type_is_array(nvpair_t *nvp)
1228602ca9eaScth {
1229602ca9eaScth data_type_t type = NVP_TYPE(nvp);
1230602ca9eaScth
1231602ca9eaScth if ((type == DATA_TYPE_BYTE_ARRAY) ||
1232602ca9eaScth (type == DATA_TYPE_UINT8_ARRAY) ||
1233602ca9eaScth (type == DATA_TYPE_INT16_ARRAY) ||
1234602ca9eaScth (type == DATA_TYPE_UINT16_ARRAY) ||
1235602ca9eaScth (type == DATA_TYPE_INT32_ARRAY) ||
1236602ca9eaScth (type == DATA_TYPE_UINT32_ARRAY) ||
1237602ca9eaScth (type == DATA_TYPE_INT64_ARRAY) ||
1238602ca9eaScth (type == DATA_TYPE_UINT64_ARRAY) ||
1239602ca9eaScth (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1240602ca9eaScth (type == DATA_TYPE_STRING_ARRAY) ||
1241602ca9eaScth (type == DATA_TYPE_NVLIST_ARRAY))
1242602ca9eaScth return (1);
1243602ca9eaScth return (0);
1244602ca9eaScth
1245602ca9eaScth }
1246602ca9eaScth
12477c478bd9Sstevel@tonic-gate static int
nvpair_value_common(nvpair_t * nvp,data_type_t type,uint_t * nelem,void * data)12487c478bd9Sstevel@tonic-gate nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
12497c478bd9Sstevel@tonic-gate {
12507c478bd9Sstevel@tonic-gate if (nvp == NULL || nvpair_type(nvp) != type)
12517c478bd9Sstevel@tonic-gate return (EINVAL);
12527c478bd9Sstevel@tonic-gate
12537c478bd9Sstevel@tonic-gate /*
12547c478bd9Sstevel@tonic-gate * For non-array types, we copy the data.
12557c478bd9Sstevel@tonic-gate * For array types (including string), we set a pointer.
12567c478bd9Sstevel@tonic-gate */
12577c478bd9Sstevel@tonic-gate switch (type) {
12587c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
12597c478bd9Sstevel@tonic-gate if (nelem != NULL)
12607c478bd9Sstevel@tonic-gate *nelem = 0;
12617c478bd9Sstevel@tonic-gate break;
12627c478bd9Sstevel@tonic-gate
12637c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
12647c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
12657c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
12667c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
12677c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
12687c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
12697c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
12707c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
12717c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
12727c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
12737c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
1274825ba0f2Srobj #if !defined(_KERNEL)
1275825ba0f2Srobj case DATA_TYPE_DOUBLE:
1276825ba0f2Srobj #endif
12777c478bd9Sstevel@tonic-gate if (data == NULL)
12787c478bd9Sstevel@tonic-gate return (EINVAL);
12797c478bd9Sstevel@tonic-gate bcopy(NVP_VALUE(nvp), data,
12807c478bd9Sstevel@tonic-gate (size_t)i_get_value_size(type, NULL, 1));
12817c478bd9Sstevel@tonic-gate if (nelem != NULL)
12827c478bd9Sstevel@tonic-gate *nelem = 1;
12837c478bd9Sstevel@tonic-gate break;
12847c478bd9Sstevel@tonic-gate
12857c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
12867c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
12877c478bd9Sstevel@tonic-gate if (data == NULL)
12887c478bd9Sstevel@tonic-gate return (EINVAL);
12897c478bd9Sstevel@tonic-gate *(void **)data = (void *)NVP_VALUE(nvp);
12907c478bd9Sstevel@tonic-gate if (nelem != NULL)
12917c478bd9Sstevel@tonic-gate *nelem = 1;
12927c478bd9Sstevel@tonic-gate break;
12937c478bd9Sstevel@tonic-gate
12947c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
12957c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
12967c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
12977c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
12987c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
12997c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
13007c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
13017c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
13027c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
13037c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
13047c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY:
13057c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
13067c478bd9Sstevel@tonic-gate if (nelem == NULL || data == NULL)
13077c478bd9Sstevel@tonic-gate return (EINVAL);
13087c478bd9Sstevel@tonic-gate if ((*nelem = NVP_NELEM(nvp)) != 0)
13097c478bd9Sstevel@tonic-gate *(void **)data = (void *)NVP_VALUE(nvp);
13107c478bd9Sstevel@tonic-gate else
13117c478bd9Sstevel@tonic-gate *(void **)data = NULL;
13127c478bd9Sstevel@tonic-gate break;
13137c478bd9Sstevel@tonic-gate
13147c478bd9Sstevel@tonic-gate default:
13157c478bd9Sstevel@tonic-gate return (ENOTSUP);
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate return (0);
13197c478bd9Sstevel@tonic-gate }
13207c478bd9Sstevel@tonic-gate
13217c478bd9Sstevel@tonic-gate static int
nvlist_lookup_common(nvlist_t * nvl,const char * name,data_type_t type,uint_t * nelem,void * data)13227c478bd9Sstevel@tonic-gate nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
13237c478bd9Sstevel@tonic-gate uint_t *nelem, void *data)
13247c478bd9Sstevel@tonic-gate {
13257c478bd9Sstevel@tonic-gate nvpriv_t *priv;
13267c478bd9Sstevel@tonic-gate nvpair_t *nvp;
13277c478bd9Sstevel@tonic-gate i_nvp_t *curr;
13287c478bd9Sstevel@tonic-gate
13297c478bd9Sstevel@tonic-gate if (name == NULL || nvl == NULL ||
13307c478bd9Sstevel@tonic-gate (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
13317c478bd9Sstevel@tonic-gate return (EINVAL);
13327c478bd9Sstevel@tonic-gate
13337c478bd9Sstevel@tonic-gate if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
13347c478bd9Sstevel@tonic-gate return (ENOTSUP);
13357c478bd9Sstevel@tonic-gate
13367c478bd9Sstevel@tonic-gate for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
13377c478bd9Sstevel@tonic-gate nvp = &curr->nvi_nvp;
13387c478bd9Sstevel@tonic-gate
13397c478bd9Sstevel@tonic-gate if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
13407c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, type, nelem, data));
13417c478bd9Sstevel@tonic-gate }
13427c478bd9Sstevel@tonic-gate
13437c478bd9Sstevel@tonic-gate return (ENOENT);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate
13467c478bd9Sstevel@tonic-gate int
nvlist_lookup_boolean(nvlist_t * nvl,const char * name)13477c478bd9Sstevel@tonic-gate nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate int
nvlist_lookup_boolean_value(nvlist_t * nvl,const char * name,boolean_t * val)13537c478bd9Sstevel@tonic-gate nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name,
13567c478bd9Sstevel@tonic-gate DATA_TYPE_BOOLEAN_VALUE, NULL, val));
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate
13597c478bd9Sstevel@tonic-gate int
nvlist_lookup_byte(nvlist_t * nvl,const char * name,uchar_t * val)13607c478bd9Sstevel@tonic-gate nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
13617c478bd9Sstevel@tonic-gate {
13627c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate
13657c478bd9Sstevel@tonic-gate int
nvlist_lookup_int8(nvlist_t * nvl,const char * name,int8_t * val)13667c478bd9Sstevel@tonic-gate nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
13697c478bd9Sstevel@tonic-gate }
13707c478bd9Sstevel@tonic-gate
13717c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint8(nvlist_t * nvl,const char * name,uint8_t * val)13727c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
13737c478bd9Sstevel@tonic-gate {
13747c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate
13777c478bd9Sstevel@tonic-gate int
nvlist_lookup_int16(nvlist_t * nvl,const char * name,int16_t * val)13787c478bd9Sstevel@tonic-gate nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate
13837c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint16(nvlist_t * nvl,const char * name,uint16_t * val)13847c478bd9Sstevel@tonic-gate nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
13857c478bd9Sstevel@tonic-gate {
13867c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate
13897c478bd9Sstevel@tonic-gate int
nvlist_lookup_int32(nvlist_t * nvl,const char * name,int32_t * val)13907c478bd9Sstevel@tonic-gate nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
13937c478bd9Sstevel@tonic-gate }
13947c478bd9Sstevel@tonic-gate
13957c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint32(nvlist_t * nvl,const char * name,uint32_t * val)13967c478bd9Sstevel@tonic-gate nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate
14017c478bd9Sstevel@tonic-gate int
nvlist_lookup_int64(nvlist_t * nvl,const char * name,int64_t * val)14027c478bd9Sstevel@tonic-gate nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate
14077c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint64(nvlist_t * nvl,const char * name,uint64_t * val)14087c478bd9Sstevel@tonic-gate nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
14097c478bd9Sstevel@tonic-gate {
14107c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
14117c478bd9Sstevel@tonic-gate }
14127c478bd9Sstevel@tonic-gate
1413825ba0f2Srobj #if !defined(_KERNEL)
1414825ba0f2Srobj int
nvlist_lookup_double(nvlist_t * nvl,const char * name,double * val)1415825ba0f2Srobj nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1416825ba0f2Srobj {
1417825ba0f2Srobj return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1418825ba0f2Srobj }
1419825ba0f2Srobj #endif
1420825ba0f2Srobj
14217c478bd9Sstevel@tonic-gate int
nvlist_lookup_string(nvlist_t * nvl,const char * name,char ** val)14227c478bd9Sstevel@tonic-gate nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
14237c478bd9Sstevel@tonic-gate {
14247c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate
14277c478bd9Sstevel@tonic-gate int
nvlist_lookup_nvlist(nvlist_t * nvl,const char * name,nvlist_t ** val)14287c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate
14337c478bd9Sstevel@tonic-gate int
nvlist_lookup_boolean_array(nvlist_t * nvl,const char * name,boolean_t ** a,uint_t * n)14347c478bd9Sstevel@tonic-gate nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
14357c478bd9Sstevel@tonic-gate boolean_t **a, uint_t *n)
14367c478bd9Sstevel@tonic-gate {
14377c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name,
14387c478bd9Sstevel@tonic-gate DATA_TYPE_BOOLEAN_ARRAY, n, a));
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate
14417c478bd9Sstevel@tonic-gate int
nvlist_lookup_byte_array(nvlist_t * nvl,const char * name,uchar_t ** a,uint_t * n)14427c478bd9Sstevel@tonic-gate nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
14437c478bd9Sstevel@tonic-gate uchar_t **a, uint_t *n)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
14467c478bd9Sstevel@tonic-gate }
14477c478bd9Sstevel@tonic-gate
14487c478bd9Sstevel@tonic-gate int
nvlist_lookup_int8_array(nvlist_t * nvl,const char * name,int8_t ** a,uint_t * n)14497c478bd9Sstevel@tonic-gate nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
14507c478bd9Sstevel@tonic-gate {
14517c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate
14547c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint8_array(nvlist_t * nvl,const char * name,uint8_t ** a,uint_t * n)14557c478bd9Sstevel@tonic-gate nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
14567c478bd9Sstevel@tonic-gate uint8_t **a, uint_t *n)
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate
14617c478bd9Sstevel@tonic-gate int
nvlist_lookup_int16_array(nvlist_t * nvl,const char * name,int16_t ** a,uint_t * n)14627c478bd9Sstevel@tonic-gate nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
14637c478bd9Sstevel@tonic-gate int16_t **a, uint_t *n)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate
14687c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint16_array(nvlist_t * nvl,const char * name,uint16_t ** a,uint_t * n)14697c478bd9Sstevel@tonic-gate nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
14707c478bd9Sstevel@tonic-gate uint16_t **a, uint_t *n)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate
14757c478bd9Sstevel@tonic-gate int
nvlist_lookup_int32_array(nvlist_t * nvl,const char * name,int32_t ** a,uint_t * n)14767c478bd9Sstevel@tonic-gate nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
14777c478bd9Sstevel@tonic-gate int32_t **a, uint_t *n)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
14807c478bd9Sstevel@tonic-gate }
14817c478bd9Sstevel@tonic-gate
14827c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint32_array(nvlist_t * nvl,const char * name,uint32_t ** a,uint_t * n)14837c478bd9Sstevel@tonic-gate nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
14847c478bd9Sstevel@tonic-gate uint32_t **a, uint_t *n)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
14877c478bd9Sstevel@tonic-gate }
14887c478bd9Sstevel@tonic-gate
14897c478bd9Sstevel@tonic-gate int
nvlist_lookup_int64_array(nvlist_t * nvl,const char * name,int64_t ** a,uint_t * n)14907c478bd9Sstevel@tonic-gate nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
14917c478bd9Sstevel@tonic-gate int64_t **a, uint_t *n)
14927c478bd9Sstevel@tonic-gate {
14937c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
14947c478bd9Sstevel@tonic-gate }
14957c478bd9Sstevel@tonic-gate
14967c478bd9Sstevel@tonic-gate int
nvlist_lookup_uint64_array(nvlist_t * nvl,const char * name,uint64_t ** a,uint_t * n)14977c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
14987c478bd9Sstevel@tonic-gate uint64_t **a, uint_t *n)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate
15037c478bd9Sstevel@tonic-gate int
nvlist_lookup_string_array(nvlist_t * nvl,const char * name,char *** a,uint_t * n)15047c478bd9Sstevel@tonic-gate nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
15057c478bd9Sstevel@tonic-gate char ***a, uint_t *n)
15067c478bd9Sstevel@tonic-gate {
15077c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate int
nvlist_lookup_nvlist_array(nvlist_t * nvl,const char * name,nvlist_t *** a,uint_t * n)15117c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
15127c478bd9Sstevel@tonic-gate nvlist_t ***a, uint_t *n)
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
15157c478bd9Sstevel@tonic-gate }
15167c478bd9Sstevel@tonic-gate
15177c478bd9Sstevel@tonic-gate int
nvlist_lookup_hrtime(nvlist_t * nvl,const char * name,hrtime_t * val)15187c478bd9Sstevel@tonic-gate nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
15197c478bd9Sstevel@tonic-gate {
15207c478bd9Sstevel@tonic-gate return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate
15237c478bd9Sstevel@tonic-gate int
nvlist_lookup_pairs(nvlist_t * nvl,int flag,...)15247c478bd9Sstevel@tonic-gate nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate va_list ap;
15277c478bd9Sstevel@tonic-gate char *name;
15287c478bd9Sstevel@tonic-gate int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
15297c478bd9Sstevel@tonic-gate int ret = 0;
15307c478bd9Sstevel@tonic-gate
15317c478bd9Sstevel@tonic-gate va_start(ap, flag);
15327c478bd9Sstevel@tonic-gate while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
15337c478bd9Sstevel@tonic-gate data_type_t type;
15347c478bd9Sstevel@tonic-gate void *val;
15357c478bd9Sstevel@tonic-gate uint_t *nelem;
15367c478bd9Sstevel@tonic-gate
15377c478bd9Sstevel@tonic-gate switch (type = va_arg(ap, data_type_t)) {
15387c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
15397c478bd9Sstevel@tonic-gate ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
15407c478bd9Sstevel@tonic-gate break;
15417c478bd9Sstevel@tonic-gate
15427c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
15437c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
15447c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
15457c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
15467c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
15477c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
15487c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
15497c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
15507c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
15517c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
15527c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
15537c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
15547c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
1555825ba0f2Srobj #if !defined(_KERNEL)
1556825ba0f2Srobj case DATA_TYPE_DOUBLE:
1557825ba0f2Srobj #endif
15587c478bd9Sstevel@tonic-gate val = va_arg(ap, void *);
15597c478bd9Sstevel@tonic-gate ret = nvlist_lookup_common(nvl, name, type, NULL, val);
15607c478bd9Sstevel@tonic-gate break;
15617c478bd9Sstevel@tonic-gate
15627c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
15637c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
15647c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
15657c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
15667c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
15677c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
15687c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
15697c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
15707c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
15717c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
15727c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY:
15737c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
15747c478bd9Sstevel@tonic-gate val = va_arg(ap, void *);
15757c478bd9Sstevel@tonic-gate nelem = va_arg(ap, uint_t *);
15767c478bd9Sstevel@tonic-gate ret = nvlist_lookup_common(nvl, name, type, nelem, val);
15777c478bd9Sstevel@tonic-gate break;
15787c478bd9Sstevel@tonic-gate
15797c478bd9Sstevel@tonic-gate default:
15807c478bd9Sstevel@tonic-gate ret = EINVAL;
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate
15837c478bd9Sstevel@tonic-gate if (ret == ENOENT && noentok)
15847c478bd9Sstevel@tonic-gate ret = 0;
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate va_end(ap);
15877c478bd9Sstevel@tonic-gate
15887c478bd9Sstevel@tonic-gate return (ret);
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate
1591602ca9eaScth /*
1592602ca9eaScth * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1593602ca9eaScth * returns zero and a pointer to the matching nvpair is returned in '*ret'
1594602ca9eaScth * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1595602ca9eaScth * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1596602ca9eaScth * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1597602ca9eaScth * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1598602ca9eaScth * code also supports "a.d[3]e[1]" syntax).
1599602ca9eaScth *
1600602ca9eaScth * If 'ip' is non-NULL and the last name component is an array, return the
1601602ca9eaScth * value of the "...[index]" array index in *ip. For an array reference that
1602602ca9eaScth * is not indexed, *ip will be returned as -1. If there is a syntax error in
1603602ca9eaScth * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1604602ca9eaScth * inside the 'name' string where the syntax error was detected.
1605602ca9eaScth */
1606602ca9eaScth static int
nvlist_lookup_nvpair_ei_sep(nvlist_t * nvl,const char * name,const char sep,nvpair_t ** ret,int * ip,char ** ep)1607602ca9eaScth nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1608602ca9eaScth nvpair_t **ret, int *ip, char **ep)
16091af98250Seschrock {
16101af98250Seschrock nvpair_t *nvp;
1611602ca9eaScth const char *np;
1612602ca9eaScth char *sepp;
1613602ca9eaScth char *idxp, *idxep;
1614602ca9eaScth nvlist_t **nva;
1615602ca9eaScth long idx;
1616602ca9eaScth int n;
16171af98250Seschrock
1618602ca9eaScth if (ip)
1619602ca9eaScth *ip = -1; /* not indexed */
1620602ca9eaScth if (ep)
1621602ca9eaScth *ep = NULL;
1622602ca9eaScth
1623602ca9eaScth if ((nvl == NULL) || (name == NULL))
16241af98250Seschrock return (EINVAL);
16251af98250Seschrock
1626602ca9eaScth /* step through components of name */
1627602ca9eaScth for (np = name; np && *np; np = sepp) {
1628602ca9eaScth /* ensure unique names */
16291af98250Seschrock if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
16301af98250Seschrock return (ENOTSUP);
16311af98250Seschrock
1632602ca9eaScth /* skip white space */
1633602ca9eaScth skip_whitespace(np);
1634602ca9eaScth if (*np == 0)
1635602ca9eaScth break;
16361af98250Seschrock
1637602ca9eaScth /* set 'sepp' to end of current component 'np' */
1638602ca9eaScth if (sep)
1639602ca9eaScth sepp = strchr(np, sep);
1640602ca9eaScth else
1641602ca9eaScth sepp = NULL;
1642602ca9eaScth
1643602ca9eaScth /* find start of next "[ index ]..." */
1644602ca9eaScth idxp = strchr(np, '[');
1645602ca9eaScth
1646602ca9eaScth /* if sepp comes first, set idxp to NULL */
1647602ca9eaScth if (sepp && idxp && (sepp < idxp))
1648602ca9eaScth idxp = NULL;
1649602ca9eaScth
1650602ca9eaScth /*
1651602ca9eaScth * At this point 'idxp' is set if there is an index
1652602ca9eaScth * expected for the current component.
1653602ca9eaScth */
1654602ca9eaScth if (idxp) {
1655602ca9eaScth /* set 'n' to length of current 'np' name component */
1656602ca9eaScth n = idxp++ - np;
1657602ca9eaScth
1658602ca9eaScth /* keep sepp up to date for *ep use as we advance */
1659602ca9eaScth skip_whitespace(idxp);
1660602ca9eaScth sepp = idxp;
1661602ca9eaScth
1662602ca9eaScth /* determine the index value */
1663602ca9eaScth #if defined(_KERNEL) && !defined(_BOOT)
1664602ca9eaScth if (ddi_strtol(idxp, &idxep, 0, &idx))
1665602ca9eaScth goto fail;
1666602ca9eaScth #else
1667602ca9eaScth idx = strtol(idxp, &idxep, 0);
1668602ca9eaScth #endif
1669602ca9eaScth if (idxep == idxp)
1670602ca9eaScth goto fail;
1671602ca9eaScth
1672602ca9eaScth /* keep sepp up to date for *ep use as we advance */
1673602ca9eaScth sepp = idxep;
1674602ca9eaScth
1675602ca9eaScth /* skip white space index value and check for ']' */
1676602ca9eaScth skip_whitespace(sepp);
1677602ca9eaScth if (*sepp++ != ']')
1678602ca9eaScth goto fail;
1679602ca9eaScth
1680602ca9eaScth /* for embedded arrays, support C syntax: "a[1].b" */
1681602ca9eaScth skip_whitespace(sepp);
1682602ca9eaScth if (sep && (*sepp == sep))
1683602ca9eaScth sepp++;
1684602ca9eaScth } else if (sepp) {
1685602ca9eaScth n = sepp++ - np;
1686602ca9eaScth } else {
1687602ca9eaScth n = strlen(np);
1688602ca9eaScth }
1689602ca9eaScth
1690602ca9eaScth /* trim trailing whitespace by reducing length of 'np' */
1691602ca9eaScth if (n == 0)
1692602ca9eaScth goto fail;
1693602ca9eaScth for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1694602ca9eaScth ;
1695602ca9eaScth n++;
1696602ca9eaScth
1697602ca9eaScth /* skip whitespace, and set sepp to NULL if complete */
1698602ca9eaScth if (sepp) {
1699602ca9eaScth skip_whitespace(sepp);
1700602ca9eaScth if (*sepp == 0)
1701602ca9eaScth sepp = NULL;
1702602ca9eaScth }
1703602ca9eaScth
1704602ca9eaScth /*
1705602ca9eaScth * At this point:
1706602ca9eaScth * o 'n' is the length of current 'np' component.
1707602ca9eaScth * o 'idxp' is set if there was an index, and value 'idx'.
1708602ca9eaScth * o 'sepp' is set to the beginning of the next component,
1709602ca9eaScth * and set to NULL if we have no more components.
1710602ca9eaScth *
1711602ca9eaScth * Search for nvpair with matching component name.
1712602ca9eaScth */
1713602ca9eaScth for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1714602ca9eaScth nvp = nvlist_next_nvpair(nvl, nvp)) {
1715602ca9eaScth
1716602ca9eaScth /* continue if no match on name */
1717602ca9eaScth if (strncmp(np, nvpair_name(nvp), n) ||
1718602ca9eaScth (strlen(nvpair_name(nvp)) != n))
1719602ca9eaScth continue;
1720602ca9eaScth
1721602ca9eaScth /* if indexed, verify type is array oriented */
1722602ca9eaScth if (idxp && !nvpair_type_is_array(nvp))
1723602ca9eaScth goto fail;
1724602ca9eaScth
1725602ca9eaScth /*
1726602ca9eaScth * Full match found, return nvp and idx if this
1727602ca9eaScth * was the last component.
1728602ca9eaScth */
1729602ca9eaScth if (sepp == NULL) {
1730602ca9eaScth if (ret)
17311af98250Seschrock *ret = nvp;
1732602ca9eaScth if (ip && idxp)
1733602ca9eaScth *ip = (int)idx; /* return index */
1734602ca9eaScth return (0); /* found */
17351af98250Seschrock }
17361af98250Seschrock
1737602ca9eaScth /*
1738602ca9eaScth * More components: current match must be
1739602ca9eaScth * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1740602ca9eaScth * to support going deeper.
1741602ca9eaScth */
1742602ca9eaScth if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1743602ca9eaScth nvl = EMBEDDED_NVL(nvp);
1744602ca9eaScth break;
1745602ca9eaScth } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1746602ca9eaScth (void) nvpair_value_nvlist_array(nvp,
1747602ca9eaScth &nva, (uint_t *)&n);
1748602ca9eaScth if ((n < 0) || (idx >= n))
1749602ca9eaScth goto fail;
1750602ca9eaScth nvl = nva[idx];
1751602ca9eaScth break;
1752602ca9eaScth }
1753602ca9eaScth
1754602ca9eaScth /* type does not support more levels */
1755602ca9eaScth goto fail;
1756602ca9eaScth }
1757602ca9eaScth if (nvp == NULL)
1758602ca9eaScth goto fail; /* 'name' not found */
1759602ca9eaScth
1760602ca9eaScth /* search for match of next component in embedded 'nvl' list */
1761602ca9eaScth }
1762602ca9eaScth
1763602ca9eaScth fail: if (ep && sepp)
1764602ca9eaScth *ep = sepp;
1765602ca9eaScth return (EINVAL);
1766602ca9eaScth }
1767602ca9eaScth
1768602ca9eaScth /*
1769602ca9eaScth * Return pointer to nvpair with specified 'name'.
1770602ca9eaScth */
1771602ca9eaScth int
nvlist_lookup_nvpair(nvlist_t * nvl,const char * name,nvpair_t ** ret)1772602ca9eaScth nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1773602ca9eaScth {
1774602ca9eaScth return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1775602ca9eaScth }
1776602ca9eaScth
1777602ca9eaScth /*
1778602ca9eaScth * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1779602ca9eaScth * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1780602ca9eaScth * description.
1781602ca9eaScth */
nvlist_lookup_nvpair_embedded_index(nvlist_t * nvl,const char * name,nvpair_t ** ret,int * ip,char ** ep)1782602ca9eaScth int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1783602ca9eaScth const char *name, nvpair_t **ret, int *ip, char **ep)
1784602ca9eaScth {
1785602ca9eaScth return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
17861af98250Seschrock }
17871af98250Seschrock
17881af98250Seschrock boolean_t
nvlist_exists(nvlist_t * nvl,const char * name)17891af98250Seschrock nvlist_exists(nvlist_t *nvl, const char *name)
17901af98250Seschrock {
17911af98250Seschrock nvpriv_t *priv;
17921af98250Seschrock nvpair_t *nvp;
17931af98250Seschrock i_nvp_t *curr;
17941af98250Seschrock
17951af98250Seschrock if (name == NULL || nvl == NULL ||
17961af98250Seschrock (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
17971af98250Seschrock return (B_FALSE);
17981af98250Seschrock
17991af98250Seschrock for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
18001af98250Seschrock nvp = &curr->nvi_nvp;
18011af98250Seschrock
18021af98250Seschrock if (strcmp(name, NVP_NAME(nvp)) == 0)
18031af98250Seschrock return (B_TRUE);
18041af98250Seschrock }
18051af98250Seschrock
18061af98250Seschrock return (B_FALSE);
18071af98250Seschrock }
18081af98250Seschrock
18091af98250Seschrock int
nvpair_value_boolean_value(nvpair_t * nvp,boolean_t * val)18107c478bd9Sstevel@tonic-gate nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
18137c478bd9Sstevel@tonic-gate }
18147c478bd9Sstevel@tonic-gate
18157c478bd9Sstevel@tonic-gate int
nvpair_value_byte(nvpair_t * nvp,uchar_t * val)18167c478bd9Sstevel@tonic-gate nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
18177c478bd9Sstevel@tonic-gate {
18187c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
18197c478bd9Sstevel@tonic-gate }
18207c478bd9Sstevel@tonic-gate
18217c478bd9Sstevel@tonic-gate int
nvpair_value_int8(nvpair_t * nvp,int8_t * val)18227c478bd9Sstevel@tonic-gate nvpair_value_int8(nvpair_t *nvp, int8_t *val)
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate
18277c478bd9Sstevel@tonic-gate int
nvpair_value_uint8(nvpair_t * nvp,uint8_t * val)18287c478bd9Sstevel@tonic-gate nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
18317c478bd9Sstevel@tonic-gate }
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate int
nvpair_value_int16(nvpair_t * nvp,int16_t * val)18347c478bd9Sstevel@tonic-gate nvpair_value_int16(nvpair_t *nvp, int16_t *val)
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate
18397c478bd9Sstevel@tonic-gate int
nvpair_value_uint16(nvpair_t * nvp,uint16_t * val)18407c478bd9Sstevel@tonic-gate nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate
18457c478bd9Sstevel@tonic-gate int
nvpair_value_int32(nvpair_t * nvp,int32_t * val)18467c478bd9Sstevel@tonic-gate nvpair_value_int32(nvpair_t *nvp, int32_t *val)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate
18517c478bd9Sstevel@tonic-gate int
nvpair_value_uint32(nvpair_t * nvp,uint32_t * val)18527c478bd9Sstevel@tonic-gate nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
18557c478bd9Sstevel@tonic-gate }
18567c478bd9Sstevel@tonic-gate
18577c478bd9Sstevel@tonic-gate int
nvpair_value_int64(nvpair_t * nvp,int64_t * val)18587c478bd9Sstevel@tonic-gate nvpair_value_int64(nvpair_t *nvp, int64_t *val)
18597c478bd9Sstevel@tonic-gate {
18607c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
18617c478bd9Sstevel@tonic-gate }
18627c478bd9Sstevel@tonic-gate
18637c478bd9Sstevel@tonic-gate int
nvpair_value_uint64(nvpair_t * nvp,uint64_t * val)18647c478bd9Sstevel@tonic-gate nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
18657c478bd9Sstevel@tonic-gate {
18667c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate
1869825ba0f2Srobj #if !defined(_KERNEL)
1870825ba0f2Srobj int
nvpair_value_double(nvpair_t * nvp,double * val)1871825ba0f2Srobj nvpair_value_double(nvpair_t *nvp, double *val)
1872825ba0f2Srobj {
1873825ba0f2Srobj return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1874825ba0f2Srobj }
1875825ba0f2Srobj #endif
1876825ba0f2Srobj
18777c478bd9Sstevel@tonic-gate int
nvpair_value_string(nvpair_t * nvp,char ** val)18787c478bd9Sstevel@tonic-gate nvpair_value_string(nvpair_t *nvp, char **val)
18797c478bd9Sstevel@tonic-gate {
18807c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate
18837c478bd9Sstevel@tonic-gate int
nvpair_value_nvlist(nvpair_t * nvp,nvlist_t ** val)18847c478bd9Sstevel@tonic-gate nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
18857c478bd9Sstevel@tonic-gate {
18867c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
18877c478bd9Sstevel@tonic-gate }
18887c478bd9Sstevel@tonic-gate
18897c478bd9Sstevel@tonic-gate int
nvpair_value_boolean_array(nvpair_t * nvp,boolean_t ** val,uint_t * nelem)18907c478bd9Sstevel@tonic-gate nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
18917c478bd9Sstevel@tonic-gate {
18927c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate
18957c478bd9Sstevel@tonic-gate int
nvpair_value_byte_array(nvpair_t * nvp,uchar_t ** val,uint_t * nelem)18967c478bd9Sstevel@tonic-gate nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
18977c478bd9Sstevel@tonic-gate {
18987c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
18997c478bd9Sstevel@tonic-gate }
19007c478bd9Sstevel@tonic-gate
19017c478bd9Sstevel@tonic-gate int
nvpair_value_int8_array(nvpair_t * nvp,int8_t ** val,uint_t * nelem)19027c478bd9Sstevel@tonic-gate nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
19037c478bd9Sstevel@tonic-gate {
19047c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate
19077c478bd9Sstevel@tonic-gate int
nvpair_value_uint8_array(nvpair_t * nvp,uint8_t ** val,uint_t * nelem)19087c478bd9Sstevel@tonic-gate nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
19117c478bd9Sstevel@tonic-gate }
19127c478bd9Sstevel@tonic-gate
19137c478bd9Sstevel@tonic-gate int
nvpair_value_int16_array(nvpair_t * nvp,int16_t ** val,uint_t * nelem)19147c478bd9Sstevel@tonic-gate nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
19157c478bd9Sstevel@tonic-gate {
19167c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
19177c478bd9Sstevel@tonic-gate }
19187c478bd9Sstevel@tonic-gate
19197c478bd9Sstevel@tonic-gate int
nvpair_value_uint16_array(nvpair_t * nvp,uint16_t ** val,uint_t * nelem)19207c478bd9Sstevel@tonic-gate nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
19217c478bd9Sstevel@tonic-gate {
19227c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
19237c478bd9Sstevel@tonic-gate }
19247c478bd9Sstevel@tonic-gate
19257c478bd9Sstevel@tonic-gate int
nvpair_value_int32_array(nvpair_t * nvp,int32_t ** val,uint_t * nelem)19267c478bd9Sstevel@tonic-gate nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate
19317c478bd9Sstevel@tonic-gate int
nvpair_value_uint32_array(nvpair_t * nvp,uint32_t ** val,uint_t * nelem)19327c478bd9Sstevel@tonic-gate nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
19337c478bd9Sstevel@tonic-gate {
19347c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
19357c478bd9Sstevel@tonic-gate }
19367c478bd9Sstevel@tonic-gate
19377c478bd9Sstevel@tonic-gate int
nvpair_value_int64_array(nvpair_t * nvp,int64_t ** val,uint_t * nelem)19387c478bd9Sstevel@tonic-gate nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
19397c478bd9Sstevel@tonic-gate {
19407c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate
19437c478bd9Sstevel@tonic-gate int
nvpair_value_uint64_array(nvpair_t * nvp,uint64_t ** val,uint_t * nelem)19447c478bd9Sstevel@tonic-gate nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
19477c478bd9Sstevel@tonic-gate }
19487c478bd9Sstevel@tonic-gate
19497c478bd9Sstevel@tonic-gate int
nvpair_value_string_array(nvpair_t * nvp,char *** val,uint_t * nelem)19507c478bd9Sstevel@tonic-gate nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
19517c478bd9Sstevel@tonic-gate {
19527c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
19537c478bd9Sstevel@tonic-gate }
19547c478bd9Sstevel@tonic-gate
19557c478bd9Sstevel@tonic-gate int
nvpair_value_nvlist_array(nvpair_t * nvp,nvlist_t *** val,uint_t * nelem)19567c478bd9Sstevel@tonic-gate nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
19577c478bd9Sstevel@tonic-gate {
19587c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
19597c478bd9Sstevel@tonic-gate }
19607c478bd9Sstevel@tonic-gate
19617c478bd9Sstevel@tonic-gate int
nvpair_value_hrtime(nvpair_t * nvp,hrtime_t * val)19627c478bd9Sstevel@tonic-gate nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
19637c478bd9Sstevel@tonic-gate {
19647c478bd9Sstevel@tonic-gate return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
19657c478bd9Sstevel@tonic-gate }
19667c478bd9Sstevel@tonic-gate
19677c478bd9Sstevel@tonic-gate /*
19687c478bd9Sstevel@tonic-gate * Add specified pair to the list.
19697c478bd9Sstevel@tonic-gate */
19707c478bd9Sstevel@tonic-gate int
nvlist_add_nvpair(nvlist_t * nvl,nvpair_t * nvp)19717c478bd9Sstevel@tonic-gate nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
19727c478bd9Sstevel@tonic-gate {
19737c478bd9Sstevel@tonic-gate if (nvl == NULL || nvp == NULL)
19747c478bd9Sstevel@tonic-gate return (EINVAL);
19757c478bd9Sstevel@tonic-gate
19767c478bd9Sstevel@tonic-gate return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
19777c478bd9Sstevel@tonic-gate NVP_NELEM(nvp), NVP_VALUE(nvp)));
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate
19807c478bd9Sstevel@tonic-gate /*
19817c478bd9Sstevel@tonic-gate * Merge the supplied nvlists and put the result in dst.
19827c478bd9Sstevel@tonic-gate * The merged list will contain all names specified in both lists,
19837c478bd9Sstevel@tonic-gate * the values are taken from nvl in the case of duplicates.
19847c478bd9Sstevel@tonic-gate * Return 0 on success.
19857c478bd9Sstevel@tonic-gate */
19867c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19877c478bd9Sstevel@tonic-gate int
nvlist_merge(nvlist_t * dst,nvlist_t * nvl,int flag)19887c478bd9Sstevel@tonic-gate nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
19897c478bd9Sstevel@tonic-gate {
19907c478bd9Sstevel@tonic-gate if (nvl == NULL || dst == NULL)
19917c478bd9Sstevel@tonic-gate return (EINVAL);
19927c478bd9Sstevel@tonic-gate
19937c478bd9Sstevel@tonic-gate if (dst != nvl)
19947c478bd9Sstevel@tonic-gate return (nvlist_copy_pairs(nvl, dst));
19957c478bd9Sstevel@tonic-gate
19967c478bd9Sstevel@tonic-gate return (0);
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate
19997c478bd9Sstevel@tonic-gate /*
20007c478bd9Sstevel@tonic-gate * Encoding related routines
20017c478bd9Sstevel@tonic-gate */
20027c478bd9Sstevel@tonic-gate #define NVS_OP_ENCODE 0
20037c478bd9Sstevel@tonic-gate #define NVS_OP_DECODE 1
20047c478bd9Sstevel@tonic-gate #define NVS_OP_GETSIZE 2
20057c478bd9Sstevel@tonic-gate
20067c478bd9Sstevel@tonic-gate typedef struct nvs_ops nvs_ops_t;
20077c478bd9Sstevel@tonic-gate
20087c478bd9Sstevel@tonic-gate typedef struct {
20097c478bd9Sstevel@tonic-gate int nvs_op;
20107c478bd9Sstevel@tonic-gate const nvs_ops_t *nvs_ops;
20117c478bd9Sstevel@tonic-gate void *nvs_private;
20127c478bd9Sstevel@tonic-gate nvpriv_t *nvs_priv;
20137c478bd9Sstevel@tonic-gate } nvstream_t;
20147c478bd9Sstevel@tonic-gate
20157c478bd9Sstevel@tonic-gate /*
20167c478bd9Sstevel@tonic-gate * nvs operations are:
20177c478bd9Sstevel@tonic-gate * - nvs_nvlist
20187c478bd9Sstevel@tonic-gate * encoding / decoding of a nvlist header (nvlist_t)
20197c478bd9Sstevel@tonic-gate * calculates the size used for header and end detection
20207c478bd9Sstevel@tonic-gate *
20217c478bd9Sstevel@tonic-gate * - nvs_nvpair
20227c478bd9Sstevel@tonic-gate * responsible for the first part of encoding / decoding of an nvpair
20237c478bd9Sstevel@tonic-gate * calculates the decoded size of an nvpair
20247c478bd9Sstevel@tonic-gate *
20257c478bd9Sstevel@tonic-gate * - nvs_nvp_op
20267c478bd9Sstevel@tonic-gate * second part of encoding / decoding of an nvpair
20277c478bd9Sstevel@tonic-gate *
20287c478bd9Sstevel@tonic-gate * - nvs_nvp_size
20297c478bd9Sstevel@tonic-gate * calculates the encoding size of an nvpair
20307c478bd9Sstevel@tonic-gate *
20317c478bd9Sstevel@tonic-gate * - nvs_nvl_fini
20327c478bd9Sstevel@tonic-gate * encodes the end detection mark (zeros).
20337c478bd9Sstevel@tonic-gate */
20347c478bd9Sstevel@tonic-gate struct nvs_ops {
20357c478bd9Sstevel@tonic-gate int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
20367c478bd9Sstevel@tonic-gate int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
20377c478bd9Sstevel@tonic-gate int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
20387c478bd9Sstevel@tonic-gate int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
20397c478bd9Sstevel@tonic-gate int (*nvs_nvl_fini)(nvstream_t *);
20407c478bd9Sstevel@tonic-gate };
20417c478bd9Sstevel@tonic-gate
20427c478bd9Sstevel@tonic-gate typedef struct {
20437c478bd9Sstevel@tonic-gate char nvh_encoding; /* nvs encoding method */
20447c478bd9Sstevel@tonic-gate char nvh_endian; /* nvs endian */
20457c478bd9Sstevel@tonic-gate char nvh_reserved1; /* reserved for future use */
20467c478bd9Sstevel@tonic-gate char nvh_reserved2; /* reserved for future use */
20477c478bd9Sstevel@tonic-gate } nvs_header_t;
20487c478bd9Sstevel@tonic-gate
20497c478bd9Sstevel@tonic-gate static int
nvs_encode_pairs(nvstream_t * nvs,nvlist_t * nvl)20507c478bd9Sstevel@tonic-gate nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
20517c478bd9Sstevel@tonic-gate {
20527c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
20537c478bd9Sstevel@tonic-gate i_nvp_t *curr;
20547c478bd9Sstevel@tonic-gate
20557c478bd9Sstevel@tonic-gate /*
20567c478bd9Sstevel@tonic-gate * Walk nvpair in list and encode each nvpair
20577c478bd9Sstevel@tonic-gate */
20587c478bd9Sstevel@tonic-gate for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
20597c478bd9Sstevel@tonic-gate if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
20607c478bd9Sstevel@tonic-gate return (EFAULT);
20617c478bd9Sstevel@tonic-gate
20627c478bd9Sstevel@tonic-gate return (nvs->nvs_ops->nvs_nvl_fini(nvs));
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate
20657c478bd9Sstevel@tonic-gate static int
nvs_decode_pairs(nvstream_t * nvs,nvlist_t * nvl)20667c478bd9Sstevel@tonic-gate nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
20677c478bd9Sstevel@tonic-gate {
20687c478bd9Sstevel@tonic-gate nvpair_t *nvp;
20697c478bd9Sstevel@tonic-gate size_t nvsize;
20707c478bd9Sstevel@tonic-gate int err;
20717c478bd9Sstevel@tonic-gate
20727c478bd9Sstevel@tonic-gate /*
20737c478bd9Sstevel@tonic-gate * Get decoded size of next pair in stream, alloc
20747c478bd9Sstevel@tonic-gate * memory for nvpair_t, then decode the nvpair
20757c478bd9Sstevel@tonic-gate */
20767c478bd9Sstevel@tonic-gate while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
20777c478bd9Sstevel@tonic-gate if (nvsize == 0) /* end of list */
20787c478bd9Sstevel@tonic-gate break;
20797c478bd9Sstevel@tonic-gate
20807c478bd9Sstevel@tonic-gate /* make sure len makes sense */
20817c478bd9Sstevel@tonic-gate if (nvsize < NVP_SIZE_CALC(1, 0))
20827c478bd9Sstevel@tonic-gate return (EFAULT);
20837c478bd9Sstevel@tonic-gate
20847c478bd9Sstevel@tonic-gate if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
20857c478bd9Sstevel@tonic-gate return (ENOMEM);
20867c478bd9Sstevel@tonic-gate
20877c478bd9Sstevel@tonic-gate if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
20887c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
20897c478bd9Sstevel@tonic-gate return (err);
20907c478bd9Sstevel@tonic-gate }
20917c478bd9Sstevel@tonic-gate
20927c478bd9Sstevel@tonic-gate if (i_validate_nvpair(nvp) != 0) {
20937c478bd9Sstevel@tonic-gate nvpair_free(nvp);
20947c478bd9Sstevel@tonic-gate nvp_buf_free(nvl, nvp);
20957c478bd9Sstevel@tonic-gate return (EFAULT);
20967c478bd9Sstevel@tonic-gate }
20977c478bd9Sstevel@tonic-gate
20987c478bd9Sstevel@tonic-gate nvp_buf_link(nvl, nvp);
20997c478bd9Sstevel@tonic-gate }
21007c478bd9Sstevel@tonic-gate return (err);
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate
21037c478bd9Sstevel@tonic-gate static int
nvs_getsize_pairs(nvstream_t * nvs,nvlist_t * nvl,size_t * buflen)21047c478bd9Sstevel@tonic-gate nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
21077c478bd9Sstevel@tonic-gate i_nvp_t *curr;
21087c478bd9Sstevel@tonic-gate uint64_t nvsize = *buflen;
21097c478bd9Sstevel@tonic-gate size_t size;
21107c478bd9Sstevel@tonic-gate
21117c478bd9Sstevel@tonic-gate /*
21127c478bd9Sstevel@tonic-gate * Get encoded size of nvpairs in nvlist
21137c478bd9Sstevel@tonic-gate */
21147c478bd9Sstevel@tonic-gate for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
21157c478bd9Sstevel@tonic-gate if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
21167c478bd9Sstevel@tonic-gate return (EINVAL);
21177c478bd9Sstevel@tonic-gate
21187c478bd9Sstevel@tonic-gate if ((nvsize += size) > INT32_MAX)
21197c478bd9Sstevel@tonic-gate return (EINVAL);
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate
21227c478bd9Sstevel@tonic-gate *buflen = nvsize;
21237c478bd9Sstevel@tonic-gate return (0);
21247c478bd9Sstevel@tonic-gate }
21257c478bd9Sstevel@tonic-gate
21267c478bd9Sstevel@tonic-gate static int
nvs_operation(nvstream_t * nvs,nvlist_t * nvl,size_t * buflen)21277c478bd9Sstevel@tonic-gate nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
21287c478bd9Sstevel@tonic-gate {
21297c478bd9Sstevel@tonic-gate int err;
21307c478bd9Sstevel@tonic-gate
21315ad82045Snd150628 if (nvl->nvl_priv == 0)
21327c478bd9Sstevel@tonic-gate return (EFAULT);
21337c478bd9Sstevel@tonic-gate
21347c478bd9Sstevel@tonic-gate /*
21357c478bd9Sstevel@tonic-gate * Perform the operation, starting with header, then each nvpair
21367c478bd9Sstevel@tonic-gate */
21377c478bd9Sstevel@tonic-gate if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
21387c478bd9Sstevel@tonic-gate return (err);
21397c478bd9Sstevel@tonic-gate
21407c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
21417c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
21427c478bd9Sstevel@tonic-gate err = nvs_encode_pairs(nvs, nvl);
21437c478bd9Sstevel@tonic-gate break;
21447c478bd9Sstevel@tonic-gate
21457c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
21467c478bd9Sstevel@tonic-gate err = nvs_decode_pairs(nvs, nvl);
21477c478bd9Sstevel@tonic-gate break;
21487c478bd9Sstevel@tonic-gate
21497c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE:
21507c478bd9Sstevel@tonic-gate err = nvs_getsize_pairs(nvs, nvl, buflen);
21517c478bd9Sstevel@tonic-gate break;
21527c478bd9Sstevel@tonic-gate
21537c478bd9Sstevel@tonic-gate default:
21547c478bd9Sstevel@tonic-gate err = EINVAL;
21557c478bd9Sstevel@tonic-gate }
21567c478bd9Sstevel@tonic-gate
21577c478bd9Sstevel@tonic-gate return (err);
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate
21607c478bd9Sstevel@tonic-gate static int
nvs_embedded(nvstream_t * nvs,nvlist_t * embedded)21617c478bd9Sstevel@tonic-gate nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
21627c478bd9Sstevel@tonic-gate {
21637c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
21647c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
21657c478bd9Sstevel@tonic-gate return (nvs_operation(nvs, embedded, NULL));
21667c478bd9Sstevel@tonic-gate
21677c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
21687c478bd9Sstevel@tonic-gate nvpriv_t *priv;
21697c478bd9Sstevel@tonic-gate int err;
21707c478bd9Sstevel@tonic-gate
21717c478bd9Sstevel@tonic-gate if (embedded->nvl_version != NV_VERSION)
21727c478bd9Sstevel@tonic-gate return (ENOTSUP);
21737c478bd9Sstevel@tonic-gate
21747c478bd9Sstevel@tonic-gate if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
21757c478bd9Sstevel@tonic-gate return (ENOMEM);
21767c478bd9Sstevel@tonic-gate
21777c478bd9Sstevel@tonic-gate nvlist_init(embedded, embedded->nvl_nvflag, priv);
21787c478bd9Sstevel@tonic-gate
21797c478bd9Sstevel@tonic-gate if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
21807c478bd9Sstevel@tonic-gate nvlist_free(embedded);
21817c478bd9Sstevel@tonic-gate return (err);
21827c478bd9Sstevel@tonic-gate }
21837c478bd9Sstevel@tonic-gate default:
21847c478bd9Sstevel@tonic-gate break;
21857c478bd9Sstevel@tonic-gate }
21867c478bd9Sstevel@tonic-gate
21877c478bd9Sstevel@tonic-gate return (EINVAL);
21887c478bd9Sstevel@tonic-gate }
21897c478bd9Sstevel@tonic-gate
21907c478bd9Sstevel@tonic-gate static int
nvs_embedded_nvl_array(nvstream_t * nvs,nvpair_t * nvp,size_t * size)21917c478bd9Sstevel@tonic-gate nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
21927c478bd9Sstevel@tonic-gate {
21937c478bd9Sstevel@tonic-gate size_t nelem = NVP_NELEM(nvp);
21947c478bd9Sstevel@tonic-gate nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
21957c478bd9Sstevel@tonic-gate int i;
21967c478bd9Sstevel@tonic-gate
21977c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
21987c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
21997c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++)
22007c478bd9Sstevel@tonic-gate if (nvs_embedded(nvs, nvlp[i]) != 0)
22017c478bd9Sstevel@tonic-gate return (EFAULT);
22027c478bd9Sstevel@tonic-gate break;
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
22057c478bd9Sstevel@tonic-gate size_t len = nelem * sizeof (uint64_t);
22067c478bd9Sstevel@tonic-gate nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
22077c478bd9Sstevel@tonic-gate
22087c478bd9Sstevel@tonic-gate bzero(nvlp, len); /* don't trust packed data */
22097c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
22107c478bd9Sstevel@tonic-gate if (nvs_embedded(nvs, embedded) != 0) {
22117c478bd9Sstevel@tonic-gate nvpair_free(nvp);
22127c478bd9Sstevel@tonic-gate return (EFAULT);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate nvlp[i] = embedded++;
22167c478bd9Sstevel@tonic-gate }
22177c478bd9Sstevel@tonic-gate break;
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE: {
22207c478bd9Sstevel@tonic-gate uint64_t nvsize = 0;
22217c478bd9Sstevel@tonic-gate
22227c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
22237c478bd9Sstevel@tonic-gate size_t nvp_sz = 0;
22247c478bd9Sstevel@tonic-gate
22257c478bd9Sstevel@tonic-gate if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
22267c478bd9Sstevel@tonic-gate return (EINVAL);
22277c478bd9Sstevel@tonic-gate
22287c478bd9Sstevel@tonic-gate if ((nvsize += nvp_sz) > INT32_MAX)
22297c478bd9Sstevel@tonic-gate return (EINVAL);
22307c478bd9Sstevel@tonic-gate }
22317c478bd9Sstevel@tonic-gate
22327c478bd9Sstevel@tonic-gate *size = nvsize;
22337c478bd9Sstevel@tonic-gate break;
22347c478bd9Sstevel@tonic-gate }
22357c478bd9Sstevel@tonic-gate default:
22367c478bd9Sstevel@tonic-gate return (EINVAL);
22377c478bd9Sstevel@tonic-gate }
22387c478bd9Sstevel@tonic-gate
22397c478bd9Sstevel@tonic-gate return (0);
22407c478bd9Sstevel@tonic-gate }
22417c478bd9Sstevel@tonic-gate
22427c478bd9Sstevel@tonic-gate static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
22437c478bd9Sstevel@tonic-gate static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
22447c478bd9Sstevel@tonic-gate
22457c478bd9Sstevel@tonic-gate /*
22467c478bd9Sstevel@tonic-gate * Common routine for nvlist operations:
22477c478bd9Sstevel@tonic-gate * encode, decode, getsize (encoded size).
22487c478bd9Sstevel@tonic-gate */
22497c478bd9Sstevel@tonic-gate static int
nvlist_common(nvlist_t * nvl,char * buf,size_t * buflen,int encoding,int nvs_op)22507c478bd9Sstevel@tonic-gate nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
22517c478bd9Sstevel@tonic-gate int nvs_op)
22527c478bd9Sstevel@tonic-gate {
22537c478bd9Sstevel@tonic-gate int err = 0;
22547c478bd9Sstevel@tonic-gate nvstream_t nvs;
22557c478bd9Sstevel@tonic-gate int nvl_endian;
22567c478bd9Sstevel@tonic-gate #ifdef _LITTLE_ENDIAN
22577c478bd9Sstevel@tonic-gate int host_endian = 1;
22587c478bd9Sstevel@tonic-gate #else
22597c478bd9Sstevel@tonic-gate int host_endian = 0;
22607c478bd9Sstevel@tonic-gate #endif /* _LITTLE_ENDIAN */
22617c478bd9Sstevel@tonic-gate nvs_header_t *nvh = (void *)buf;
22627c478bd9Sstevel@tonic-gate
22637c478bd9Sstevel@tonic-gate if (buflen == NULL || nvl == NULL ||
22647c478bd9Sstevel@tonic-gate (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
22657c478bd9Sstevel@tonic-gate return (EINVAL);
22667c478bd9Sstevel@tonic-gate
22677c478bd9Sstevel@tonic-gate nvs.nvs_op = nvs_op;
22687c478bd9Sstevel@tonic-gate
22697c478bd9Sstevel@tonic-gate /*
22707c478bd9Sstevel@tonic-gate * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
22717c478bd9Sstevel@tonic-gate * a buffer is allocated. The first 4 bytes in the buffer are
22727c478bd9Sstevel@tonic-gate * used for encoding method and host endian.
22737c478bd9Sstevel@tonic-gate */
22747c478bd9Sstevel@tonic-gate switch (nvs_op) {
22757c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
22767c478bd9Sstevel@tonic-gate if (buf == NULL || *buflen < sizeof (nvs_header_t))
22777c478bd9Sstevel@tonic-gate return (EINVAL);
22787c478bd9Sstevel@tonic-gate
22797c478bd9Sstevel@tonic-gate nvh->nvh_encoding = encoding;
22807c478bd9Sstevel@tonic-gate nvh->nvh_endian = nvl_endian = host_endian;
22817c478bd9Sstevel@tonic-gate nvh->nvh_reserved1 = 0;
22827c478bd9Sstevel@tonic-gate nvh->nvh_reserved2 = 0;
22837c478bd9Sstevel@tonic-gate break;
22847c478bd9Sstevel@tonic-gate
22857c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
22867c478bd9Sstevel@tonic-gate if (buf == NULL || *buflen < sizeof (nvs_header_t))
22877c478bd9Sstevel@tonic-gate return (EINVAL);
22887c478bd9Sstevel@tonic-gate
22897c478bd9Sstevel@tonic-gate /* get method of encoding from first byte */
22907c478bd9Sstevel@tonic-gate encoding = nvh->nvh_encoding;
22917c478bd9Sstevel@tonic-gate nvl_endian = nvh->nvh_endian;
22927c478bd9Sstevel@tonic-gate break;
22937c478bd9Sstevel@tonic-gate
22947c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE:
22957c478bd9Sstevel@tonic-gate nvl_endian = host_endian;
22967c478bd9Sstevel@tonic-gate
22977c478bd9Sstevel@tonic-gate /*
22987c478bd9Sstevel@tonic-gate * add the size for encoding
22997c478bd9Sstevel@tonic-gate */
23007c478bd9Sstevel@tonic-gate *buflen = sizeof (nvs_header_t);
23017c478bd9Sstevel@tonic-gate break;
23027c478bd9Sstevel@tonic-gate
23037c478bd9Sstevel@tonic-gate default:
23047c478bd9Sstevel@tonic-gate return (ENOTSUP);
23057c478bd9Sstevel@tonic-gate }
23067c478bd9Sstevel@tonic-gate
23077c478bd9Sstevel@tonic-gate /*
23087c478bd9Sstevel@tonic-gate * Create an nvstream with proper encoding method
23097c478bd9Sstevel@tonic-gate */
23107c478bd9Sstevel@tonic-gate switch (encoding) {
23117c478bd9Sstevel@tonic-gate case NV_ENCODE_NATIVE:
23127c478bd9Sstevel@tonic-gate /*
23137c478bd9Sstevel@tonic-gate * check endianness, in case we are unpacking
23147c478bd9Sstevel@tonic-gate * from a file
23157c478bd9Sstevel@tonic-gate */
23167c478bd9Sstevel@tonic-gate if (nvl_endian != host_endian)
23177c478bd9Sstevel@tonic-gate return (ENOTSUP);
23187c478bd9Sstevel@tonic-gate err = nvs_native(&nvs, nvl, buf, buflen);
23197c478bd9Sstevel@tonic-gate break;
23207c478bd9Sstevel@tonic-gate case NV_ENCODE_XDR:
23217c478bd9Sstevel@tonic-gate err = nvs_xdr(&nvs, nvl, buf, buflen);
23227c478bd9Sstevel@tonic-gate break;
23237c478bd9Sstevel@tonic-gate default:
23247c478bd9Sstevel@tonic-gate err = ENOTSUP;
23257c478bd9Sstevel@tonic-gate break;
23267c478bd9Sstevel@tonic-gate }
23277c478bd9Sstevel@tonic-gate
23287c478bd9Sstevel@tonic-gate return (err);
23297c478bd9Sstevel@tonic-gate }
23307c478bd9Sstevel@tonic-gate
23317c478bd9Sstevel@tonic-gate int
nvlist_size(nvlist_t * nvl,size_t * size,int encoding)23327c478bd9Sstevel@tonic-gate nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
23337c478bd9Sstevel@tonic-gate {
23347c478bd9Sstevel@tonic-gate return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
23357c478bd9Sstevel@tonic-gate }
23367c478bd9Sstevel@tonic-gate
23377c478bd9Sstevel@tonic-gate /*
23387c478bd9Sstevel@tonic-gate * Pack nvlist into contiguous memory
23397c478bd9Sstevel@tonic-gate */
23407c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
23417c478bd9Sstevel@tonic-gate int
nvlist_pack(nvlist_t * nvl,char ** bufp,size_t * buflen,int encoding,int kmflag)23427c478bd9Sstevel@tonic-gate nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
23437c478bd9Sstevel@tonic-gate int kmflag)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
23467c478bd9Sstevel@tonic-gate return (nvlist_xpack(nvl, bufp, buflen, encoding,
23477c478bd9Sstevel@tonic-gate (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
23487c478bd9Sstevel@tonic-gate #else
23497c478bd9Sstevel@tonic-gate return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
23507c478bd9Sstevel@tonic-gate #endif
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate
23537c478bd9Sstevel@tonic-gate int
nvlist_xpack(nvlist_t * nvl,char ** bufp,size_t * buflen,int encoding,nv_alloc_t * nva)23547c478bd9Sstevel@tonic-gate nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
23557c478bd9Sstevel@tonic-gate nv_alloc_t *nva)
23567c478bd9Sstevel@tonic-gate {
23577c478bd9Sstevel@tonic-gate nvpriv_t nvpriv;
23587c478bd9Sstevel@tonic-gate size_t alloc_size;
23597c478bd9Sstevel@tonic-gate char *buf;
23607c478bd9Sstevel@tonic-gate int err;
23617c478bd9Sstevel@tonic-gate
23627c478bd9Sstevel@tonic-gate if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
23637c478bd9Sstevel@tonic-gate return (EINVAL);
23647c478bd9Sstevel@tonic-gate
23657c478bd9Sstevel@tonic-gate if (*bufp != NULL)
23667c478bd9Sstevel@tonic-gate return (nvlist_common(nvl, *bufp, buflen, encoding,
23677c478bd9Sstevel@tonic-gate NVS_OP_ENCODE));
23687c478bd9Sstevel@tonic-gate
23697c478bd9Sstevel@tonic-gate /*
23707c478bd9Sstevel@tonic-gate * Here is a difficult situation:
23717c478bd9Sstevel@tonic-gate * 1. The nvlist has fixed allocator properties.
23727c478bd9Sstevel@tonic-gate * All other nvlist routines (like nvlist_add_*, ...) use
23737c478bd9Sstevel@tonic-gate * these properties.
23747c478bd9Sstevel@tonic-gate * 2. When using nvlist_pack() the user can specify his own
23757c478bd9Sstevel@tonic-gate * allocator properties (e.g. by using KM_NOSLEEP).
23767c478bd9Sstevel@tonic-gate *
23777c478bd9Sstevel@tonic-gate * We use the user specified properties (2). A clearer solution
23787c478bd9Sstevel@tonic-gate * will be to remove the kmflag from nvlist_pack(), but we will
23797c478bd9Sstevel@tonic-gate * not change the interface.
23807c478bd9Sstevel@tonic-gate */
23817c478bd9Sstevel@tonic-gate nv_priv_init(&nvpriv, nva, 0);
23827c478bd9Sstevel@tonic-gate
23837c478bd9Sstevel@tonic-gate if (err = nvlist_size(nvl, &alloc_size, encoding))
23847c478bd9Sstevel@tonic-gate return (err);
23857c478bd9Sstevel@tonic-gate
23867c478bd9Sstevel@tonic-gate if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
23877c478bd9Sstevel@tonic-gate return (ENOMEM);
23887c478bd9Sstevel@tonic-gate
23897c478bd9Sstevel@tonic-gate if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
23907c478bd9Sstevel@tonic-gate NVS_OP_ENCODE)) != 0) {
23917c478bd9Sstevel@tonic-gate nv_mem_free(&nvpriv, buf, alloc_size);
23927c478bd9Sstevel@tonic-gate } else {
23937c478bd9Sstevel@tonic-gate *buflen = alloc_size;
23947c478bd9Sstevel@tonic-gate *bufp = buf;
23957c478bd9Sstevel@tonic-gate }
23967c478bd9Sstevel@tonic-gate
23977c478bd9Sstevel@tonic-gate return (err);
23987c478bd9Sstevel@tonic-gate }
23997c478bd9Sstevel@tonic-gate
24007c478bd9Sstevel@tonic-gate /*
24017c478bd9Sstevel@tonic-gate * Unpack buf into an nvlist_t
24027c478bd9Sstevel@tonic-gate */
24037c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
24047c478bd9Sstevel@tonic-gate int
nvlist_unpack(char * buf,size_t buflen,nvlist_t ** nvlp,int kmflag)24057c478bd9Sstevel@tonic-gate nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
24067c478bd9Sstevel@tonic-gate {
24077c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
24087c478bd9Sstevel@tonic-gate return (nvlist_xunpack(buf, buflen, nvlp,
24097c478bd9Sstevel@tonic-gate (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
24107c478bd9Sstevel@tonic-gate #else
24117c478bd9Sstevel@tonic-gate return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
24127c478bd9Sstevel@tonic-gate #endif
24137c478bd9Sstevel@tonic-gate }
24147c478bd9Sstevel@tonic-gate
24157c478bd9Sstevel@tonic-gate int
nvlist_xunpack(char * buf,size_t buflen,nvlist_t ** nvlp,nv_alloc_t * nva)24167c478bd9Sstevel@tonic-gate nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
24177c478bd9Sstevel@tonic-gate {
24187c478bd9Sstevel@tonic-gate nvlist_t *nvl;
24197c478bd9Sstevel@tonic-gate int err;
24207c478bd9Sstevel@tonic-gate
24217c478bd9Sstevel@tonic-gate if (nvlp == NULL)
24227c478bd9Sstevel@tonic-gate return (EINVAL);
24237c478bd9Sstevel@tonic-gate
24247c478bd9Sstevel@tonic-gate if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
24257c478bd9Sstevel@tonic-gate return (err);
24267c478bd9Sstevel@tonic-gate
24277c478bd9Sstevel@tonic-gate if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
24287c478bd9Sstevel@tonic-gate nvlist_free(nvl);
24297c478bd9Sstevel@tonic-gate else
24307c478bd9Sstevel@tonic-gate *nvlp = nvl;
24317c478bd9Sstevel@tonic-gate
24327c478bd9Sstevel@tonic-gate return (err);
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate
24357c478bd9Sstevel@tonic-gate /*
24367c478bd9Sstevel@tonic-gate * Native encoding functions
24377c478bd9Sstevel@tonic-gate */
24387c478bd9Sstevel@tonic-gate typedef struct {
24397c478bd9Sstevel@tonic-gate /*
24407c478bd9Sstevel@tonic-gate * This structure is used when decoding a packed nvpair in
24417c478bd9Sstevel@tonic-gate * the native format. n_base points to a buffer containing the
24427c478bd9Sstevel@tonic-gate * packed nvpair. n_end is a pointer to the end of the buffer.
24437c478bd9Sstevel@tonic-gate * (n_end actually points to the first byte past the end of the
24447c478bd9Sstevel@tonic-gate * buffer.) n_curr is a pointer that lies between n_base and n_end.
24457c478bd9Sstevel@tonic-gate * It points to the current data that we are decoding.
24467c478bd9Sstevel@tonic-gate * The amount of data left in the buffer is equal to n_end - n_curr.
24477c478bd9Sstevel@tonic-gate * n_flag is used to recognize a packed embedded list.
24487c478bd9Sstevel@tonic-gate */
24497c478bd9Sstevel@tonic-gate caddr_t n_base;
24507c478bd9Sstevel@tonic-gate caddr_t n_end;
24517c478bd9Sstevel@tonic-gate caddr_t n_curr;
24527c478bd9Sstevel@tonic-gate uint_t n_flag;
24537c478bd9Sstevel@tonic-gate } nvs_native_t;
24547c478bd9Sstevel@tonic-gate
24557c478bd9Sstevel@tonic-gate static int
nvs_native_create(nvstream_t * nvs,nvs_native_t * native,char * buf,size_t buflen)24567c478bd9Sstevel@tonic-gate nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
24577c478bd9Sstevel@tonic-gate size_t buflen)
24587c478bd9Sstevel@tonic-gate {
24597c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
24607c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
24617c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
24627c478bd9Sstevel@tonic-gate nvs->nvs_private = native;
24637c478bd9Sstevel@tonic-gate native->n_curr = native->n_base = buf;
24647c478bd9Sstevel@tonic-gate native->n_end = buf + buflen;
24657c478bd9Sstevel@tonic-gate native->n_flag = 0;
24667c478bd9Sstevel@tonic-gate return (0);
24677c478bd9Sstevel@tonic-gate
24687c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE:
24697c478bd9Sstevel@tonic-gate nvs->nvs_private = native;
24707c478bd9Sstevel@tonic-gate native->n_curr = native->n_base = native->n_end = NULL;
24717c478bd9Sstevel@tonic-gate native->n_flag = 0;
24727c478bd9Sstevel@tonic-gate return (0);
24737c478bd9Sstevel@tonic-gate default:
24747c478bd9Sstevel@tonic-gate return (EINVAL);
24757c478bd9Sstevel@tonic-gate }
24767c478bd9Sstevel@tonic-gate }
24777c478bd9Sstevel@tonic-gate
24787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24797c478bd9Sstevel@tonic-gate static void
nvs_native_destroy(nvstream_t * nvs)24807c478bd9Sstevel@tonic-gate nvs_native_destroy(nvstream_t *nvs)
24817c478bd9Sstevel@tonic-gate {
24827c478bd9Sstevel@tonic-gate }
24837c478bd9Sstevel@tonic-gate
24847c478bd9Sstevel@tonic-gate static int
native_cp(nvstream_t * nvs,void * buf,size_t size)24857c478bd9Sstevel@tonic-gate native_cp(nvstream_t *nvs, void *buf, size_t size)
24867c478bd9Sstevel@tonic-gate {
24877c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
24887c478bd9Sstevel@tonic-gate
24897c478bd9Sstevel@tonic-gate if (native->n_curr + size > native->n_end)
24907c478bd9Sstevel@tonic-gate return (EFAULT);
24917c478bd9Sstevel@tonic-gate
24927c478bd9Sstevel@tonic-gate /*
24937c478bd9Sstevel@tonic-gate * The bcopy() below eliminates alignment requirement
24947c478bd9Sstevel@tonic-gate * on the buffer (stream) and is preferred over direct access.
24957c478bd9Sstevel@tonic-gate */
24967c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
24977c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
24987c478bd9Sstevel@tonic-gate bcopy(buf, native->n_curr, size);
24997c478bd9Sstevel@tonic-gate break;
25007c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
25017c478bd9Sstevel@tonic-gate bcopy(native->n_curr, buf, size);
25027c478bd9Sstevel@tonic-gate break;
25037c478bd9Sstevel@tonic-gate default:
25047c478bd9Sstevel@tonic-gate return (EINVAL);
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate
25077c478bd9Sstevel@tonic-gate native->n_curr += size;
25087c478bd9Sstevel@tonic-gate return (0);
25097c478bd9Sstevel@tonic-gate }
25107c478bd9Sstevel@tonic-gate
25117c478bd9Sstevel@tonic-gate /*
25127c478bd9Sstevel@tonic-gate * operate on nvlist_t header
25137c478bd9Sstevel@tonic-gate */
25147c478bd9Sstevel@tonic-gate static int
nvs_native_nvlist(nvstream_t * nvs,nvlist_t * nvl,size_t * size)25157c478bd9Sstevel@tonic-gate nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
25167c478bd9Sstevel@tonic-gate {
25177c478bd9Sstevel@tonic-gate nvs_native_t *native = nvs->nvs_private;
25187c478bd9Sstevel@tonic-gate
25197c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
25207c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
25217c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
25227c478bd9Sstevel@tonic-gate if (native->n_flag)
25237c478bd9Sstevel@tonic-gate return (0); /* packed embedded list */
25247c478bd9Sstevel@tonic-gate
25257c478bd9Sstevel@tonic-gate native->n_flag = 1;
25267c478bd9Sstevel@tonic-gate
25277c478bd9Sstevel@tonic-gate /* copy version and nvflag of the nvlist_t */
25287c478bd9Sstevel@tonic-gate if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
25297c478bd9Sstevel@tonic-gate native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
25307c478bd9Sstevel@tonic-gate return (EFAULT);
25317c478bd9Sstevel@tonic-gate
25327c478bd9Sstevel@tonic-gate return (0);
25337c478bd9Sstevel@tonic-gate
25347c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE:
25357c478bd9Sstevel@tonic-gate /*
25367c478bd9Sstevel@tonic-gate * if calculate for packed embedded list
25377c478bd9Sstevel@tonic-gate * 4 for end of the embedded list
25387c478bd9Sstevel@tonic-gate * else
25397c478bd9Sstevel@tonic-gate * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
25407c478bd9Sstevel@tonic-gate * and 4 for end of the entire list
25417c478bd9Sstevel@tonic-gate */
25427c478bd9Sstevel@tonic-gate if (native->n_flag) {
25437c478bd9Sstevel@tonic-gate *size += 4;
25447c478bd9Sstevel@tonic-gate } else {
25457c478bd9Sstevel@tonic-gate native->n_flag = 1;
25467c478bd9Sstevel@tonic-gate *size += 2 * sizeof (int32_t) + 4;
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate
25497c478bd9Sstevel@tonic-gate return (0);
25507c478bd9Sstevel@tonic-gate
25517c478bd9Sstevel@tonic-gate default:
25527c478bd9Sstevel@tonic-gate return (EINVAL);
25537c478bd9Sstevel@tonic-gate }
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate
25567c478bd9Sstevel@tonic-gate static int
nvs_native_nvl_fini(nvstream_t * nvs)25577c478bd9Sstevel@tonic-gate nvs_native_nvl_fini(nvstream_t *nvs)
25587c478bd9Sstevel@tonic-gate {
25597c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_ENCODE) {
25607c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
25617c478bd9Sstevel@tonic-gate /*
25627c478bd9Sstevel@tonic-gate * Add 4 zero bytes at end of nvlist. They are used
25637c478bd9Sstevel@tonic-gate * for end detection by the decode routine.
25647c478bd9Sstevel@tonic-gate */
25657c478bd9Sstevel@tonic-gate if (native->n_curr + sizeof (int) > native->n_end)
25667c478bd9Sstevel@tonic-gate return (EFAULT);
25677c478bd9Sstevel@tonic-gate
25687c478bd9Sstevel@tonic-gate bzero(native->n_curr, sizeof (int));
25697c478bd9Sstevel@tonic-gate native->n_curr += sizeof (int);
25707c478bd9Sstevel@tonic-gate }
25717c478bd9Sstevel@tonic-gate
25727c478bd9Sstevel@tonic-gate return (0);
25737c478bd9Sstevel@tonic-gate }
25747c478bd9Sstevel@tonic-gate
25757c478bd9Sstevel@tonic-gate static int
nvpair_native_embedded(nvstream_t * nvs,nvpair_t * nvp)25767c478bd9Sstevel@tonic-gate nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
25777c478bd9Sstevel@tonic-gate {
25787c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_ENCODE) {
25797c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
25807c478bd9Sstevel@tonic-gate nvlist_t *packed = (void *)
25817c478bd9Sstevel@tonic-gate (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
25827c478bd9Sstevel@tonic-gate /*
25837c478bd9Sstevel@tonic-gate * Null out the pointer that is meaningless in the packed
25847c478bd9Sstevel@tonic-gate * structure. The address may not be aligned, so we have
25857c478bd9Sstevel@tonic-gate * to use bzero.
25867c478bd9Sstevel@tonic-gate */
25877c478bd9Sstevel@tonic-gate bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
25887c478bd9Sstevel@tonic-gate }
25897c478bd9Sstevel@tonic-gate
25907c478bd9Sstevel@tonic-gate return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
25917c478bd9Sstevel@tonic-gate }
25927c478bd9Sstevel@tonic-gate
25937c478bd9Sstevel@tonic-gate static int
nvpair_native_embedded_array(nvstream_t * nvs,nvpair_t * nvp)25947c478bd9Sstevel@tonic-gate nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
25957c478bd9Sstevel@tonic-gate {
25967c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_ENCODE) {
25977c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
25987c478bd9Sstevel@tonic-gate char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
25997c478bd9Sstevel@tonic-gate size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
26007c478bd9Sstevel@tonic-gate nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
26017c478bd9Sstevel@tonic-gate int i;
26027c478bd9Sstevel@tonic-gate /*
26037c478bd9Sstevel@tonic-gate * Null out pointers that are meaningless in the packed
26047c478bd9Sstevel@tonic-gate * structure. The addresses may not be aligned, so we have
26057c478bd9Sstevel@tonic-gate * to use bzero.
26067c478bd9Sstevel@tonic-gate */
26077c478bd9Sstevel@tonic-gate bzero(value, len);
26087c478bd9Sstevel@tonic-gate
26097c478bd9Sstevel@tonic-gate for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
26107c478bd9Sstevel@tonic-gate /*
26117c478bd9Sstevel@tonic-gate * Null out the pointer that is meaningless in the
26127c478bd9Sstevel@tonic-gate * packed structure. The address may not be aligned,
26137c478bd9Sstevel@tonic-gate * so we have to use bzero.
26147c478bd9Sstevel@tonic-gate */
26157c478bd9Sstevel@tonic-gate bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
26167c478bd9Sstevel@tonic-gate }
26177c478bd9Sstevel@tonic-gate
26187c478bd9Sstevel@tonic-gate return (nvs_embedded_nvl_array(nvs, nvp, NULL));
26197c478bd9Sstevel@tonic-gate }
26207c478bd9Sstevel@tonic-gate
26217c478bd9Sstevel@tonic-gate static void
nvpair_native_string_array(nvstream_t * nvs,nvpair_t * nvp)26227c478bd9Sstevel@tonic-gate nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
26237c478bd9Sstevel@tonic-gate {
26247c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
26257c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE: {
26267c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
26277c478bd9Sstevel@tonic-gate uint64_t *strp = (void *)
26287c478bd9Sstevel@tonic-gate (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
26297c478bd9Sstevel@tonic-gate /*
26307c478bd9Sstevel@tonic-gate * Null out pointers that are meaningless in the packed
26317c478bd9Sstevel@tonic-gate * structure. The addresses may not be aligned, so we have
26327c478bd9Sstevel@tonic-gate * to use bzero.
26337c478bd9Sstevel@tonic-gate */
26347c478bd9Sstevel@tonic-gate bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
26357c478bd9Sstevel@tonic-gate break;
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
26387c478bd9Sstevel@tonic-gate char **strp = (void *)NVP_VALUE(nvp);
26397c478bd9Sstevel@tonic-gate char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
26407c478bd9Sstevel@tonic-gate int i;
26417c478bd9Sstevel@tonic-gate
26427c478bd9Sstevel@tonic-gate for (i = 0; i < NVP_NELEM(nvp); i++) {
26437c478bd9Sstevel@tonic-gate strp[i] = buf;
26447c478bd9Sstevel@tonic-gate buf += strlen(buf) + 1;
26457c478bd9Sstevel@tonic-gate }
26467c478bd9Sstevel@tonic-gate break;
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate }
26497c478bd9Sstevel@tonic-gate }
26507c478bd9Sstevel@tonic-gate
26517c478bd9Sstevel@tonic-gate static int
nvs_native_nvp_op(nvstream_t * nvs,nvpair_t * nvp)26527c478bd9Sstevel@tonic-gate nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
26537c478bd9Sstevel@tonic-gate {
26547c478bd9Sstevel@tonic-gate data_type_t type;
26557c478bd9Sstevel@tonic-gate int value_sz;
26567c478bd9Sstevel@tonic-gate int ret = 0;
26577c478bd9Sstevel@tonic-gate
26587c478bd9Sstevel@tonic-gate /*
26597c478bd9Sstevel@tonic-gate * We do the initial bcopy of the data before we look at
26607c478bd9Sstevel@tonic-gate * the nvpair type, because when we're decoding, we won't
26617c478bd9Sstevel@tonic-gate * have the correct values for the pair until we do the bcopy.
26627c478bd9Sstevel@tonic-gate */
26637c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
26647c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
26657c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
26667c478bd9Sstevel@tonic-gate if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
26677c478bd9Sstevel@tonic-gate return (EFAULT);
26687c478bd9Sstevel@tonic-gate break;
26697c478bd9Sstevel@tonic-gate default:
26707c478bd9Sstevel@tonic-gate return (EINVAL);
26717c478bd9Sstevel@tonic-gate }
26727c478bd9Sstevel@tonic-gate
26737c478bd9Sstevel@tonic-gate /* verify nvp_name_sz, check the name string length */
26747c478bd9Sstevel@tonic-gate if (i_validate_nvpair_name(nvp) != 0)
26757c478bd9Sstevel@tonic-gate return (EFAULT);
26767c478bd9Sstevel@tonic-gate
26777c478bd9Sstevel@tonic-gate type = NVP_TYPE(nvp);
26787c478bd9Sstevel@tonic-gate
26797c478bd9Sstevel@tonic-gate /*
26807c478bd9Sstevel@tonic-gate * Verify type and nelem and get the value size.
26817c478bd9Sstevel@tonic-gate * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
26827c478bd9Sstevel@tonic-gate * is the size of the string(s) excluded.
26837c478bd9Sstevel@tonic-gate */
26847c478bd9Sstevel@tonic-gate if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
26857c478bd9Sstevel@tonic-gate return (EFAULT);
26867c478bd9Sstevel@tonic-gate
26877c478bd9Sstevel@tonic-gate if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
26887c478bd9Sstevel@tonic-gate return (EFAULT);
26897c478bd9Sstevel@tonic-gate
26907c478bd9Sstevel@tonic-gate switch (type) {
26917c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
26927c478bd9Sstevel@tonic-gate ret = nvpair_native_embedded(nvs, nvp);
26937c478bd9Sstevel@tonic-gate break;
26947c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
26957c478bd9Sstevel@tonic-gate ret = nvpair_native_embedded_array(nvs, nvp);
26967c478bd9Sstevel@tonic-gate break;
26977c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY:
26987c478bd9Sstevel@tonic-gate nvpair_native_string_array(nvs, nvp);
26997c478bd9Sstevel@tonic-gate break;
27007c478bd9Sstevel@tonic-gate default:
27017c478bd9Sstevel@tonic-gate break;
27027c478bd9Sstevel@tonic-gate }
27037c478bd9Sstevel@tonic-gate
27047c478bd9Sstevel@tonic-gate return (ret);
27057c478bd9Sstevel@tonic-gate }
27067c478bd9Sstevel@tonic-gate
27077c478bd9Sstevel@tonic-gate static int
nvs_native_nvp_size(nvstream_t * nvs,nvpair_t * nvp,size_t * size)27087c478bd9Sstevel@tonic-gate nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
27097c478bd9Sstevel@tonic-gate {
27107c478bd9Sstevel@tonic-gate uint64_t nvp_sz = nvp->nvp_size;
27117c478bd9Sstevel@tonic-gate
27127c478bd9Sstevel@tonic-gate switch (NVP_TYPE(nvp)) {
27137c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST: {
27147c478bd9Sstevel@tonic-gate size_t nvsize = 0;
27157c478bd9Sstevel@tonic-gate
27167c478bd9Sstevel@tonic-gate if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
27177c478bd9Sstevel@tonic-gate return (EINVAL);
27187c478bd9Sstevel@tonic-gate
27197c478bd9Sstevel@tonic-gate nvp_sz += nvsize;
27207c478bd9Sstevel@tonic-gate break;
27217c478bd9Sstevel@tonic-gate }
27227c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: {
27237c478bd9Sstevel@tonic-gate size_t nvsize;
27247c478bd9Sstevel@tonic-gate
27257c478bd9Sstevel@tonic-gate if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
27267c478bd9Sstevel@tonic-gate return (EINVAL);
27277c478bd9Sstevel@tonic-gate
27287c478bd9Sstevel@tonic-gate nvp_sz += nvsize;
27297c478bd9Sstevel@tonic-gate break;
27307c478bd9Sstevel@tonic-gate }
27317c478bd9Sstevel@tonic-gate default:
27327c478bd9Sstevel@tonic-gate break;
27337c478bd9Sstevel@tonic-gate }
27347c478bd9Sstevel@tonic-gate
27357c478bd9Sstevel@tonic-gate if (nvp_sz > INT32_MAX)
27367c478bd9Sstevel@tonic-gate return (EINVAL);
27377c478bd9Sstevel@tonic-gate
27387c478bd9Sstevel@tonic-gate *size = nvp_sz;
27397c478bd9Sstevel@tonic-gate
27407c478bd9Sstevel@tonic-gate return (0);
27417c478bd9Sstevel@tonic-gate }
27427c478bd9Sstevel@tonic-gate
27437c478bd9Sstevel@tonic-gate static int
nvs_native_nvpair(nvstream_t * nvs,nvpair_t * nvp,size_t * size)27447c478bd9Sstevel@tonic-gate nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
27457c478bd9Sstevel@tonic-gate {
27467c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
27477c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
27487c478bd9Sstevel@tonic-gate return (nvs_native_nvp_op(nvs, nvp));
27497c478bd9Sstevel@tonic-gate
27507c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
27517c478bd9Sstevel@tonic-gate nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
27527c478bd9Sstevel@tonic-gate int32_t decode_len;
27537c478bd9Sstevel@tonic-gate
27547c478bd9Sstevel@tonic-gate /* try to read the size value from the stream */
27557c478bd9Sstevel@tonic-gate if (native->n_curr + sizeof (int32_t) > native->n_end)
27567c478bd9Sstevel@tonic-gate return (EFAULT);
27577c478bd9Sstevel@tonic-gate bcopy(native->n_curr, &decode_len, sizeof (int32_t));
27587c478bd9Sstevel@tonic-gate
27597c478bd9Sstevel@tonic-gate /* sanity check the size value */
27607c478bd9Sstevel@tonic-gate if (decode_len < 0 ||
27617c478bd9Sstevel@tonic-gate decode_len > native->n_end - native->n_curr)
27627c478bd9Sstevel@tonic-gate return (EFAULT);
27637c478bd9Sstevel@tonic-gate
27647c478bd9Sstevel@tonic-gate *size = decode_len;
27657c478bd9Sstevel@tonic-gate
27667c478bd9Sstevel@tonic-gate /*
27677c478bd9Sstevel@tonic-gate * If at the end of the stream then move the cursor
27687c478bd9Sstevel@tonic-gate * forward, otherwise nvpair_native_op() will read
27697c478bd9Sstevel@tonic-gate * the entire nvpair at the same cursor position.
27707c478bd9Sstevel@tonic-gate */
27717c478bd9Sstevel@tonic-gate if (*size == 0)
27727c478bd9Sstevel@tonic-gate native->n_curr += sizeof (int32_t);
27737c478bd9Sstevel@tonic-gate break;
27747c478bd9Sstevel@tonic-gate }
27757c478bd9Sstevel@tonic-gate
27767c478bd9Sstevel@tonic-gate default:
27777c478bd9Sstevel@tonic-gate return (EINVAL);
27787c478bd9Sstevel@tonic-gate }
27797c478bd9Sstevel@tonic-gate
27807c478bd9Sstevel@tonic-gate return (0);
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate
27837c478bd9Sstevel@tonic-gate static const nvs_ops_t nvs_native_ops = {
27847c478bd9Sstevel@tonic-gate nvs_native_nvlist,
27857c478bd9Sstevel@tonic-gate nvs_native_nvpair,
27867c478bd9Sstevel@tonic-gate nvs_native_nvp_op,
27877c478bd9Sstevel@tonic-gate nvs_native_nvp_size,
27887c478bd9Sstevel@tonic-gate nvs_native_nvl_fini
27897c478bd9Sstevel@tonic-gate };
27907c478bd9Sstevel@tonic-gate
27917c478bd9Sstevel@tonic-gate static int
nvs_native(nvstream_t * nvs,nvlist_t * nvl,char * buf,size_t * buflen)27927c478bd9Sstevel@tonic-gate nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
27937c478bd9Sstevel@tonic-gate {
27947c478bd9Sstevel@tonic-gate nvs_native_t native;
27957c478bd9Sstevel@tonic-gate int err;
27967c478bd9Sstevel@tonic-gate
27977c478bd9Sstevel@tonic-gate nvs->nvs_ops = &nvs_native_ops;
27987c478bd9Sstevel@tonic-gate
27997c478bd9Sstevel@tonic-gate if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
28007c478bd9Sstevel@tonic-gate *buflen - sizeof (nvs_header_t))) != 0)
28017c478bd9Sstevel@tonic-gate return (err);
28027c478bd9Sstevel@tonic-gate
28037c478bd9Sstevel@tonic-gate err = nvs_operation(nvs, nvl, buflen);
28047c478bd9Sstevel@tonic-gate
28057c478bd9Sstevel@tonic-gate nvs_native_destroy(nvs);
28067c478bd9Sstevel@tonic-gate
28077c478bd9Sstevel@tonic-gate return (err);
28087c478bd9Sstevel@tonic-gate }
28097c478bd9Sstevel@tonic-gate
28107c478bd9Sstevel@tonic-gate /*
28117c478bd9Sstevel@tonic-gate * XDR encoding functions
28127c478bd9Sstevel@tonic-gate *
28137c478bd9Sstevel@tonic-gate * An xdr packed nvlist is encoded as:
28147c478bd9Sstevel@tonic-gate *
28157c478bd9Sstevel@tonic-gate * - encoding methode and host endian (4 bytes)
28167c478bd9Sstevel@tonic-gate * - nvl_version (4 bytes)
28177c478bd9Sstevel@tonic-gate * - nvl_nvflag (4 bytes)
28187c478bd9Sstevel@tonic-gate *
28197c478bd9Sstevel@tonic-gate * - encoded nvpairs, the format of one xdr encoded nvpair is:
28207c478bd9Sstevel@tonic-gate * - encoded size of the nvpair (4 bytes)
28217c478bd9Sstevel@tonic-gate * - decoded size of the nvpair (4 bytes)
28227c478bd9Sstevel@tonic-gate * - name string, (4 + sizeof(NV_ALIGN4(string))
28237c478bd9Sstevel@tonic-gate * a string is coded as size (4 bytes) and data
28247c478bd9Sstevel@tonic-gate * - data type (4 bytes)
28257c478bd9Sstevel@tonic-gate * - number of elements in the nvpair (4 bytes)
28267c478bd9Sstevel@tonic-gate * - data
28277c478bd9Sstevel@tonic-gate *
28287c478bd9Sstevel@tonic-gate * - 2 zero's for end of the entire list (8 bytes)
28297c478bd9Sstevel@tonic-gate */
28307c478bd9Sstevel@tonic-gate static int
nvs_xdr_create(nvstream_t * nvs,XDR * xdr,char * buf,size_t buflen)28317c478bd9Sstevel@tonic-gate nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
28327c478bd9Sstevel@tonic-gate {
28337c478bd9Sstevel@tonic-gate /* xdr data must be 4 byte aligned */
28347c478bd9Sstevel@tonic-gate if ((ulong_t)buf % 4 != 0)
28357c478bd9Sstevel@tonic-gate return (EFAULT);
28367c478bd9Sstevel@tonic-gate
28377c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
28387c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
28397c478bd9Sstevel@tonic-gate xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
28407c478bd9Sstevel@tonic-gate nvs->nvs_private = xdr;
28417c478bd9Sstevel@tonic-gate return (0);
28427c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
28437c478bd9Sstevel@tonic-gate xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
28447c478bd9Sstevel@tonic-gate nvs->nvs_private = xdr;
28457c478bd9Sstevel@tonic-gate return (0);
28467c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE:
28477c478bd9Sstevel@tonic-gate nvs->nvs_private = NULL;
28487c478bd9Sstevel@tonic-gate return (0);
28497c478bd9Sstevel@tonic-gate default:
28507c478bd9Sstevel@tonic-gate return (EINVAL);
28517c478bd9Sstevel@tonic-gate }
28527c478bd9Sstevel@tonic-gate }
28537c478bd9Sstevel@tonic-gate
28547c478bd9Sstevel@tonic-gate static void
nvs_xdr_destroy(nvstream_t * nvs)28557c478bd9Sstevel@tonic-gate nvs_xdr_destroy(nvstream_t *nvs)
28567c478bd9Sstevel@tonic-gate {
28577c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
28587c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
28597c478bd9Sstevel@tonic-gate case NVS_OP_DECODE:
28607c478bd9Sstevel@tonic-gate xdr_destroy((XDR *)nvs->nvs_private);
28617c478bd9Sstevel@tonic-gate break;
28627c478bd9Sstevel@tonic-gate default:
28637c478bd9Sstevel@tonic-gate break;
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate
28677c478bd9Sstevel@tonic-gate static int
nvs_xdr_nvlist(nvstream_t * nvs,nvlist_t * nvl,size_t * size)28687c478bd9Sstevel@tonic-gate nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
28697c478bd9Sstevel@tonic-gate {
28707c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
28717c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE:
28727c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
28737c478bd9Sstevel@tonic-gate XDR *xdr = nvs->nvs_private;
28747c478bd9Sstevel@tonic-gate
28757c478bd9Sstevel@tonic-gate if (!xdr_int(xdr, &nvl->nvl_version) ||
28767c478bd9Sstevel@tonic-gate !xdr_u_int(xdr, &nvl->nvl_nvflag))
28777c478bd9Sstevel@tonic-gate return (EFAULT);
28787c478bd9Sstevel@tonic-gate break;
28797c478bd9Sstevel@tonic-gate }
28807c478bd9Sstevel@tonic-gate case NVS_OP_GETSIZE: {
28817c478bd9Sstevel@tonic-gate /*
28827c478bd9Sstevel@tonic-gate * 2 * 4 for nvl_version + nvl_nvflag
28837c478bd9Sstevel@tonic-gate * and 8 for end of the entire list
28847c478bd9Sstevel@tonic-gate */
28857c478bd9Sstevel@tonic-gate *size += 2 * 4 + 8;
28867c478bd9Sstevel@tonic-gate break;
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate default:
28897c478bd9Sstevel@tonic-gate return (EINVAL);
28907c478bd9Sstevel@tonic-gate }
28917c478bd9Sstevel@tonic-gate return (0);
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate
28947c478bd9Sstevel@tonic-gate static int
nvs_xdr_nvl_fini(nvstream_t * nvs)28957c478bd9Sstevel@tonic-gate nvs_xdr_nvl_fini(nvstream_t *nvs)
28967c478bd9Sstevel@tonic-gate {
28977c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_ENCODE) {
28987c478bd9Sstevel@tonic-gate XDR *xdr = nvs->nvs_private;
28997c478bd9Sstevel@tonic-gate int zero = 0;
29007c478bd9Sstevel@tonic-gate
29017c478bd9Sstevel@tonic-gate if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
29027c478bd9Sstevel@tonic-gate return (EFAULT);
29037c478bd9Sstevel@tonic-gate }
29047c478bd9Sstevel@tonic-gate
29057c478bd9Sstevel@tonic-gate return (0);
29067c478bd9Sstevel@tonic-gate }
29077c478bd9Sstevel@tonic-gate
29087c478bd9Sstevel@tonic-gate /*
29097c478bd9Sstevel@tonic-gate * The format of xdr encoded nvpair is:
29107c478bd9Sstevel@tonic-gate * encode_size, decode_size, name string, data type, nelem, data
29117c478bd9Sstevel@tonic-gate */
29127c478bd9Sstevel@tonic-gate static int
nvs_xdr_nvp_op(nvstream_t * nvs,nvpair_t * nvp)29137c478bd9Sstevel@tonic-gate nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
29147c478bd9Sstevel@tonic-gate {
29157c478bd9Sstevel@tonic-gate data_type_t type;
29167c478bd9Sstevel@tonic-gate char *buf;
29177c478bd9Sstevel@tonic-gate char *buf_end = (char *)nvp + nvp->nvp_size;
29187c478bd9Sstevel@tonic-gate int value_sz;
29197c478bd9Sstevel@tonic-gate uint_t nelem, buflen;
29207c478bd9Sstevel@tonic-gate bool_t ret = FALSE;
29217c478bd9Sstevel@tonic-gate XDR *xdr = nvs->nvs_private;
29227c478bd9Sstevel@tonic-gate
29237c478bd9Sstevel@tonic-gate ASSERT(xdr != NULL && nvp != NULL);
29247c478bd9Sstevel@tonic-gate
29257c478bd9Sstevel@tonic-gate /* name string */
29267c478bd9Sstevel@tonic-gate if ((buf = NVP_NAME(nvp)) >= buf_end)
29277c478bd9Sstevel@tonic-gate return (EFAULT);
29287c478bd9Sstevel@tonic-gate buflen = buf_end - buf;
29297c478bd9Sstevel@tonic-gate
29307c478bd9Sstevel@tonic-gate if (!xdr_string(xdr, &buf, buflen - 1))
29317c478bd9Sstevel@tonic-gate return (EFAULT);
29327c478bd9Sstevel@tonic-gate nvp->nvp_name_sz = strlen(buf) + 1;
29337c478bd9Sstevel@tonic-gate
29347c478bd9Sstevel@tonic-gate /* type and nelem */
29357c478bd9Sstevel@tonic-gate if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
29367c478bd9Sstevel@tonic-gate !xdr_int(xdr, &nvp->nvp_value_elem))
29377c478bd9Sstevel@tonic-gate return (EFAULT);
29387c478bd9Sstevel@tonic-gate
29397c478bd9Sstevel@tonic-gate type = NVP_TYPE(nvp);
29407c478bd9Sstevel@tonic-gate nelem = nvp->nvp_value_elem;
29417c478bd9Sstevel@tonic-gate
29427c478bd9Sstevel@tonic-gate /*
29437c478bd9Sstevel@tonic-gate * Verify type and nelem and get the value size.
29447c478bd9Sstevel@tonic-gate * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
29457c478bd9Sstevel@tonic-gate * is the size of the string(s) excluded.
29467c478bd9Sstevel@tonic-gate */
29477c478bd9Sstevel@tonic-gate if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
29487c478bd9Sstevel@tonic-gate return (EFAULT);
29497c478bd9Sstevel@tonic-gate
29507c478bd9Sstevel@tonic-gate /* if there is no data to extract then return */
29517c478bd9Sstevel@tonic-gate if (nelem == 0)
29527c478bd9Sstevel@tonic-gate return (0);
29537c478bd9Sstevel@tonic-gate
29547c478bd9Sstevel@tonic-gate /* value */
29557c478bd9Sstevel@tonic-gate if ((buf = NVP_VALUE(nvp)) >= buf_end)
29567c478bd9Sstevel@tonic-gate return (EFAULT);
29577c478bd9Sstevel@tonic-gate buflen = buf_end - buf;
29587c478bd9Sstevel@tonic-gate
29597c478bd9Sstevel@tonic-gate if (buflen < value_sz)
29607c478bd9Sstevel@tonic-gate return (EFAULT);
29617c478bd9Sstevel@tonic-gate
29627c478bd9Sstevel@tonic-gate switch (type) {
29637c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
29647c478bd9Sstevel@tonic-gate if (nvs_embedded(nvs, (void *)buf) == 0)
29657c478bd9Sstevel@tonic-gate return (0);
29667c478bd9Sstevel@tonic-gate break;
29677c478bd9Sstevel@tonic-gate
29687c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY:
29697c478bd9Sstevel@tonic-gate if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
29707c478bd9Sstevel@tonic-gate return (0);
29717c478bd9Sstevel@tonic-gate break;
29727c478bd9Sstevel@tonic-gate
29737c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
29747c478bd9Sstevel@tonic-gate ret = TRUE;
29757c478bd9Sstevel@tonic-gate break;
29767c478bd9Sstevel@tonic-gate
29777c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
29787c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
29797c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
29807c478bd9Sstevel@tonic-gate ret = xdr_char(xdr, buf);
29817c478bd9Sstevel@tonic-gate break;
29827c478bd9Sstevel@tonic-gate
29837c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
29847c478bd9Sstevel@tonic-gate ret = xdr_short(xdr, (void *)buf);
29857c478bd9Sstevel@tonic-gate break;
29867c478bd9Sstevel@tonic-gate
29877c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
29887c478bd9Sstevel@tonic-gate ret = xdr_u_short(xdr, (void *)buf);
29897c478bd9Sstevel@tonic-gate break;
29907c478bd9Sstevel@tonic-gate
29917c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
29927c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
29937c478bd9Sstevel@tonic-gate ret = xdr_int(xdr, (void *)buf);
29947c478bd9Sstevel@tonic-gate break;
29957c478bd9Sstevel@tonic-gate
29967c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
29977c478bd9Sstevel@tonic-gate ret = xdr_u_int(xdr, (void *)buf);
29987c478bd9Sstevel@tonic-gate break;
29997c478bd9Sstevel@tonic-gate
30007c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
30017c478bd9Sstevel@tonic-gate ret = xdr_longlong_t(xdr, (void *)buf);
30027c478bd9Sstevel@tonic-gate break;
30037c478bd9Sstevel@tonic-gate
30047c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
30057c478bd9Sstevel@tonic-gate ret = xdr_u_longlong_t(xdr, (void *)buf);
30067c478bd9Sstevel@tonic-gate break;
30077c478bd9Sstevel@tonic-gate
30087c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
30097c478bd9Sstevel@tonic-gate /*
30107c478bd9Sstevel@tonic-gate * NOTE: must expose the definition of hrtime_t here
30117c478bd9Sstevel@tonic-gate */
30127c478bd9Sstevel@tonic-gate ret = xdr_longlong_t(xdr, (void *)buf);
30137c478bd9Sstevel@tonic-gate break;
3014825ba0f2Srobj #if !defined(_KERNEL)
3015825ba0f2Srobj case DATA_TYPE_DOUBLE:
3016825ba0f2Srobj ret = xdr_double(xdr, (void *)buf);
3017825ba0f2Srobj break;
3018825ba0f2Srobj #endif
30197c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
30207c478bd9Sstevel@tonic-gate ret = xdr_string(xdr, &buf, buflen - 1);
30217c478bd9Sstevel@tonic-gate break;
30227c478bd9Sstevel@tonic-gate
30237c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
30247c478bd9Sstevel@tonic-gate ret = xdr_opaque(xdr, buf, nelem);
30257c478bd9Sstevel@tonic-gate break;
30267c478bd9Sstevel@tonic-gate
30277c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
30287c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
30297c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
30307c478bd9Sstevel@tonic-gate (xdrproc_t)xdr_char);
30317c478bd9Sstevel@tonic-gate break;
30327c478bd9Sstevel@tonic-gate
30337c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
30347c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
30357c478bd9Sstevel@tonic-gate sizeof (int16_t), (xdrproc_t)xdr_short);
30367c478bd9Sstevel@tonic-gate break;
30377c478bd9Sstevel@tonic-gate
30387c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
30397c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
30407c478bd9Sstevel@tonic-gate sizeof (uint16_t), (xdrproc_t)xdr_u_short);
30417c478bd9Sstevel@tonic-gate break;
30427c478bd9Sstevel@tonic-gate
30437c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
30447c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
30457c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
30467c478bd9Sstevel@tonic-gate sizeof (int32_t), (xdrproc_t)xdr_int);
30477c478bd9Sstevel@tonic-gate break;
30487c478bd9Sstevel@tonic-gate
30497c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
30507c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
30517c478bd9Sstevel@tonic-gate sizeof (uint32_t), (xdrproc_t)xdr_u_int);
30527c478bd9Sstevel@tonic-gate break;
30537c478bd9Sstevel@tonic-gate
30547c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
30557c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
30567c478bd9Sstevel@tonic-gate sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
30577c478bd9Sstevel@tonic-gate break;
30587c478bd9Sstevel@tonic-gate
30597c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
30607c478bd9Sstevel@tonic-gate ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
30617c478bd9Sstevel@tonic-gate sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
30627c478bd9Sstevel@tonic-gate break;
30637c478bd9Sstevel@tonic-gate
30647c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY: {
30657c478bd9Sstevel@tonic-gate size_t len = nelem * sizeof (uint64_t);
30667c478bd9Sstevel@tonic-gate char **strp = (void *)buf;
30677c478bd9Sstevel@tonic-gate int i;
30687c478bd9Sstevel@tonic-gate
30697c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_DECODE)
30707c478bd9Sstevel@tonic-gate bzero(buf, len); /* don't trust packed data */
30717c478bd9Sstevel@tonic-gate
30727c478bd9Sstevel@tonic-gate for (i = 0; i < nelem; i++) {
30737c478bd9Sstevel@tonic-gate if (buflen <= len)
30747c478bd9Sstevel@tonic-gate return (EFAULT);
30757c478bd9Sstevel@tonic-gate
30767c478bd9Sstevel@tonic-gate buf += len;
30777c478bd9Sstevel@tonic-gate buflen -= len;
30787c478bd9Sstevel@tonic-gate
30797c478bd9Sstevel@tonic-gate if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
30807c478bd9Sstevel@tonic-gate return (EFAULT);
30817c478bd9Sstevel@tonic-gate
30827c478bd9Sstevel@tonic-gate if (nvs->nvs_op == NVS_OP_DECODE)
30837c478bd9Sstevel@tonic-gate strp[i] = buf;
30847c478bd9Sstevel@tonic-gate len = strlen(buf) + 1;
30857c478bd9Sstevel@tonic-gate }
30867c478bd9Sstevel@tonic-gate ret = TRUE;
30877c478bd9Sstevel@tonic-gate break;
30887c478bd9Sstevel@tonic-gate }
30897c478bd9Sstevel@tonic-gate default:
30907c478bd9Sstevel@tonic-gate break;
30917c478bd9Sstevel@tonic-gate }
30927c478bd9Sstevel@tonic-gate
30937c478bd9Sstevel@tonic-gate return (ret == TRUE ? 0 : EFAULT);
30947c478bd9Sstevel@tonic-gate }
30957c478bd9Sstevel@tonic-gate
30967c478bd9Sstevel@tonic-gate static int
nvs_xdr_nvp_size(nvstream_t * nvs,nvpair_t * nvp,size_t * size)30977c478bd9Sstevel@tonic-gate nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
30987c478bd9Sstevel@tonic-gate {
30997c478bd9Sstevel@tonic-gate data_type_t type = NVP_TYPE(nvp);
31007c478bd9Sstevel@tonic-gate /*
31017c478bd9Sstevel@tonic-gate * encode_size + decode_size + name string size + data type + nelem
31027c478bd9Sstevel@tonic-gate * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
31037c478bd9Sstevel@tonic-gate */
31047c478bd9Sstevel@tonic-gate uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
31057c478bd9Sstevel@tonic-gate
31067c478bd9Sstevel@tonic-gate switch (type) {
31077c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN:
31087c478bd9Sstevel@tonic-gate break;
31097c478bd9Sstevel@tonic-gate
31107c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_VALUE:
31117c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE:
31127c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8:
31137c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8:
31147c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16:
31157c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16:
31167c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32:
31177c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32:
31187c478bd9Sstevel@tonic-gate nvp_sz += 4; /* 4 is the minimum xdr unit */
31197c478bd9Sstevel@tonic-gate break;
31207c478bd9Sstevel@tonic-gate
31217c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64:
31227c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64:
31237c478bd9Sstevel@tonic-gate case DATA_TYPE_HRTIME:
3124825ba0f2Srobj #if !defined(_KERNEL)
3125825ba0f2Srobj case DATA_TYPE_DOUBLE:
3126825ba0f2Srobj #endif
31277c478bd9Sstevel@tonic-gate nvp_sz += 8;
31287c478bd9Sstevel@tonic-gate break;
31297c478bd9Sstevel@tonic-gate
31307c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING:
31317c478bd9Sstevel@tonic-gate nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
31327c478bd9Sstevel@tonic-gate break;
31337c478bd9Sstevel@tonic-gate
31347c478bd9Sstevel@tonic-gate case DATA_TYPE_BYTE_ARRAY:
31357c478bd9Sstevel@tonic-gate nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
31367c478bd9Sstevel@tonic-gate break;
31377c478bd9Sstevel@tonic-gate
31387c478bd9Sstevel@tonic-gate case DATA_TYPE_BOOLEAN_ARRAY:
31397c478bd9Sstevel@tonic-gate case DATA_TYPE_INT8_ARRAY:
31407c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT8_ARRAY:
31417c478bd9Sstevel@tonic-gate case DATA_TYPE_INT16_ARRAY:
31427c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT16_ARRAY:
31437c478bd9Sstevel@tonic-gate case DATA_TYPE_INT32_ARRAY:
31447c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT32_ARRAY:
31457c478bd9Sstevel@tonic-gate nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
31467c478bd9Sstevel@tonic-gate break;
31477c478bd9Sstevel@tonic-gate
31487c478bd9Sstevel@tonic-gate case DATA_TYPE_INT64_ARRAY:
31497c478bd9Sstevel@tonic-gate case DATA_TYPE_UINT64_ARRAY:
31507c478bd9Sstevel@tonic-gate nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
31517c478bd9Sstevel@tonic-gate break;
31527c478bd9Sstevel@tonic-gate
31537c478bd9Sstevel@tonic-gate case DATA_TYPE_STRING_ARRAY: {
31547c478bd9Sstevel@tonic-gate int i;
31557c478bd9Sstevel@tonic-gate char **strs = (void *)NVP_VALUE(nvp);
31567c478bd9Sstevel@tonic-gate
31577c478bd9Sstevel@tonic-gate for (i = 0; i < NVP_NELEM(nvp); i++)
31587c478bd9Sstevel@tonic-gate nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
31597c478bd9Sstevel@tonic-gate
31607c478bd9Sstevel@tonic-gate break;
31617c478bd9Sstevel@tonic-gate }
31627c478bd9Sstevel@tonic-gate
31637c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST:
31647c478bd9Sstevel@tonic-gate case DATA_TYPE_NVLIST_ARRAY: {
31657c478bd9Sstevel@tonic-gate size_t nvsize = 0;
31667c478bd9Sstevel@tonic-gate int old_nvs_op = nvs->nvs_op;
31677c478bd9Sstevel@tonic-gate int err;
31687c478bd9Sstevel@tonic-gate
31697c478bd9Sstevel@tonic-gate nvs->nvs_op = NVS_OP_GETSIZE;
31707c478bd9Sstevel@tonic-gate if (type == DATA_TYPE_NVLIST)
31717c478bd9Sstevel@tonic-gate err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
31727c478bd9Sstevel@tonic-gate else
31737c478bd9Sstevel@tonic-gate err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
31747c478bd9Sstevel@tonic-gate nvs->nvs_op = old_nvs_op;
31757c478bd9Sstevel@tonic-gate
31767c478bd9Sstevel@tonic-gate if (err != 0)
31777c478bd9Sstevel@tonic-gate return (EINVAL);
31787c478bd9Sstevel@tonic-gate
31797c478bd9Sstevel@tonic-gate nvp_sz += nvsize;
31807c478bd9Sstevel@tonic-gate break;
31817c478bd9Sstevel@tonic-gate }
31827c478bd9Sstevel@tonic-gate
31837c478bd9Sstevel@tonic-gate default:
31847c478bd9Sstevel@tonic-gate return (EINVAL);
31857c478bd9Sstevel@tonic-gate }
31867c478bd9Sstevel@tonic-gate
31877c478bd9Sstevel@tonic-gate if (nvp_sz > INT32_MAX)
31887c478bd9Sstevel@tonic-gate return (EINVAL);
31897c478bd9Sstevel@tonic-gate
31907c478bd9Sstevel@tonic-gate *size = nvp_sz;
31917c478bd9Sstevel@tonic-gate
31927c478bd9Sstevel@tonic-gate return (0);
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate
31957c478bd9Sstevel@tonic-gate
31967c478bd9Sstevel@tonic-gate /*
31977c478bd9Sstevel@tonic-gate * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
31987c478bd9Sstevel@tonic-gate * the largest nvpair that could be encoded in the buffer.
31997c478bd9Sstevel@tonic-gate *
32007c478bd9Sstevel@tonic-gate * See comments above nvpair_xdr_op() for the format of xdr encoding.
32017c478bd9Sstevel@tonic-gate * The size of a xdr packed nvpair without any data is 5 words.
32027c478bd9Sstevel@tonic-gate *
32037c478bd9Sstevel@tonic-gate * Using the size of the data directly as an estimate would be ok
32047c478bd9Sstevel@tonic-gate * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
32057c478bd9Sstevel@tonic-gate * then the actual nvpair has space for an array of pointers to index
32067c478bd9Sstevel@tonic-gate * the strings. These pointers are not encoded into the packed xdr buffer.
32077c478bd9Sstevel@tonic-gate *
32087c478bd9Sstevel@tonic-gate * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
32097c478bd9Sstevel@tonic-gate * of length 0, then each string is endcoded in xdr format as a single word.
32107c478bd9Sstevel@tonic-gate * Therefore when expanded to an nvpair there will be 2.25 word used for
32117c478bd9Sstevel@tonic-gate * each string. (a int64_t allocated for pointer usage, and a single char
32127c478bd9Sstevel@tonic-gate * for the null termination.)
32137c478bd9Sstevel@tonic-gate *
32147c478bd9Sstevel@tonic-gate * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
32157c478bd9Sstevel@tonic-gate */
32167c478bd9Sstevel@tonic-gate #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
32177c478bd9Sstevel@tonic-gate #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
32187c478bd9Sstevel@tonic-gate 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
32197c478bd9Sstevel@tonic-gate #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
32207c478bd9Sstevel@tonic-gate (NVS_XDR_DATA_LEN(x) * 2) + \
32217c478bd9Sstevel@tonic-gate NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
32227c478bd9Sstevel@tonic-gate
32237c478bd9Sstevel@tonic-gate static int
nvs_xdr_nvpair(nvstream_t * nvs,nvpair_t * nvp,size_t * size)32247c478bd9Sstevel@tonic-gate nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
32257c478bd9Sstevel@tonic-gate {
32267c478bd9Sstevel@tonic-gate XDR *xdr = nvs->nvs_private;
32277c478bd9Sstevel@tonic-gate int32_t encode_len, decode_len;
32287c478bd9Sstevel@tonic-gate
32297c478bd9Sstevel@tonic-gate switch (nvs->nvs_op) {
32307c478bd9Sstevel@tonic-gate case NVS_OP_ENCODE: {
32317c478bd9Sstevel@tonic-gate size_t nvsize;
32327c478bd9Sstevel@tonic-gate
32337c478bd9Sstevel@tonic-gate if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
32347c478bd9Sstevel@tonic-gate return (EFAULT);
32357c478bd9Sstevel@tonic-gate
32367c478bd9Sstevel@tonic-gate decode_len = nvp->nvp_size;
32377c478bd9Sstevel@tonic-gate encode_len = nvsize;
32387c478bd9Sstevel@tonic-gate if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
32397c478bd9Sstevel@tonic-gate return (EFAULT);
32407c478bd9Sstevel@tonic-gate
32417c478bd9Sstevel@tonic-gate return (nvs_xdr_nvp_op(nvs, nvp));
32427c478bd9Sstevel@tonic-gate }
32437c478bd9Sstevel@tonic-gate case NVS_OP_DECODE: {
32447c478bd9Sstevel@tonic-gate struct xdr_bytesrec bytesrec;
32457c478bd9Sstevel@tonic-gate
32467c478bd9Sstevel@tonic-gate /* get the encode and decode size */
32477c478bd9Sstevel@tonic-gate if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
32487c478bd9Sstevel@tonic-gate return (EFAULT);
32497c478bd9Sstevel@tonic-gate *size = decode_len;
32507c478bd9Sstevel@tonic-gate
32517c478bd9Sstevel@tonic-gate /* are we at the end of the stream? */
32527c478bd9Sstevel@tonic-gate if (*size == 0)
32537c478bd9Sstevel@tonic-gate return (0);
32547c478bd9Sstevel@tonic-gate
32557c478bd9Sstevel@tonic-gate /* sanity check the size parameter */
32567c478bd9Sstevel@tonic-gate if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
32577c478bd9Sstevel@tonic-gate return (EFAULT);
32587c478bd9Sstevel@tonic-gate
32597c478bd9Sstevel@tonic-gate if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
32607c478bd9Sstevel@tonic-gate return (EFAULT);
32617c478bd9Sstevel@tonic-gate break;
32627c478bd9Sstevel@tonic-gate }
32637c478bd9Sstevel@tonic-gate
32647c478bd9Sstevel@tonic-gate default:
32657c478bd9Sstevel@tonic-gate return (EINVAL);
32667c478bd9Sstevel@tonic-gate }
32677c478bd9Sstevel@tonic-gate return (0);
32687c478bd9Sstevel@tonic-gate }
32697c478bd9Sstevel@tonic-gate
32707c478bd9Sstevel@tonic-gate static const struct nvs_ops nvs_xdr_ops = {
32717c478bd9Sstevel@tonic-gate nvs_xdr_nvlist,
32727c478bd9Sstevel@tonic-gate nvs_xdr_nvpair,
32737c478bd9Sstevel@tonic-gate nvs_xdr_nvp_op,
32747c478bd9Sstevel@tonic-gate nvs_xdr_nvp_size,
32757c478bd9Sstevel@tonic-gate nvs_xdr_nvl_fini
32767c478bd9Sstevel@tonic-gate };
32777c478bd9Sstevel@tonic-gate
32787c478bd9Sstevel@tonic-gate static int
nvs_xdr(nvstream_t * nvs,nvlist_t * nvl,char * buf,size_t * buflen)32797c478bd9Sstevel@tonic-gate nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
32807c478bd9Sstevel@tonic-gate {
32817c478bd9Sstevel@tonic-gate XDR xdr;
32827c478bd9Sstevel@tonic-gate int err;
32837c478bd9Sstevel@tonic-gate
32847c478bd9Sstevel@tonic-gate nvs->nvs_ops = &nvs_xdr_ops;
32857c478bd9Sstevel@tonic-gate
32867c478bd9Sstevel@tonic-gate if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
32877c478bd9Sstevel@tonic-gate *buflen - sizeof (nvs_header_t))) != 0)
32887c478bd9Sstevel@tonic-gate return (err);
32897c478bd9Sstevel@tonic-gate
32907c478bd9Sstevel@tonic-gate err = nvs_operation(nvs, nvl, buflen);
32917c478bd9Sstevel@tonic-gate
32927c478bd9Sstevel@tonic-gate nvs_xdr_destroy(nvs);
32937c478bd9Sstevel@tonic-gate
32947c478bd9Sstevel@tonic-gate return (err);
32957c478bd9Sstevel@tonic-gate }
3296