xref: /titanic_51/usr/src/common/nvpair/nvpair.c (revision 5ad820458efd0fdb914baff9c1447c22b819fa23)
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 /*
233bb79becSeschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
307c478bd9Sstevel@tonic-gate #include <sys/debug.h>
317c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
327c478bd9Sstevel@tonic-gate #include <sys/int_limits.h>
337c478bd9Sstevel@tonic-gate #include <sys/nvpair.h>
347c478bd9Sstevel@tonic-gate #include <sys/nvpair_impl.h>
357c478bd9Sstevel@tonic-gate #include <rpc/types.h>
367c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
397c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
407c478bd9Sstevel@tonic-gate #else
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
427c478bd9Sstevel@tonic-gate #include <strings.h>
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #ifndef	offsetof
467c478bd9Sstevel@tonic-gate #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
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
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
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
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 *
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 *
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
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
2057c478bd9Sstevel@tonic-gate nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	bzero(priv, sizeof (priv));
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 *
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 *
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
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 
2587c478bd9Sstevel@tonic-gate /*
2597c478bd9Sstevel@tonic-gate  * nvlist_alloc - Allocate nvlist.
2607c478bd9Sstevel@tonic-gate  */
2617c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
2627c478bd9Sstevel@tonic-gate int
2637c478bd9Sstevel@tonic-gate nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
2667c478bd9Sstevel@tonic-gate 	return (nvlist_xalloc(nvlp, nvflag,
2677c478bd9Sstevel@tonic-gate 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2687c478bd9Sstevel@tonic-gate #else
2697c478bd9Sstevel@tonic-gate 	return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
2707c478bd9Sstevel@tonic-gate #endif
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate int
2747c478bd9Sstevel@tonic-gate nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (nvlp == NULL || nva == NULL)
2797c478bd9Sstevel@tonic-gate 		return (EINVAL);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if ((priv = nv_priv_alloc(nva)) == NULL)
2827c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if ((*nvlp = nv_mem_zalloc(priv,
2857c478bd9Sstevel@tonic-gate 	    NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
2867c478bd9Sstevel@tonic-gate 		nv_mem_free(priv, priv, sizeof (nvpriv_t));
2877c478bd9Sstevel@tonic-gate 		return (ENOMEM);
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	nvlist_init(*nvlp, nvflag, priv);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (0);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
2967c478bd9Sstevel@tonic-gate  * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
2977c478bd9Sstevel@tonic-gate  */
2987c478bd9Sstevel@tonic-gate static nvpair_t *
2997c478bd9Sstevel@tonic-gate nvp_buf_alloc(nvlist_t *nvl, size_t len)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3027c478bd9Sstevel@tonic-gate 	i_nvp_t *buf;
3037c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
3047c478bd9Sstevel@tonic-gate 	size_t nvsize;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * Allocate the buffer
3087c478bd9Sstevel@tonic-gate 	 */
3097c478bd9Sstevel@tonic-gate 	nvsize = len + offsetof(i_nvp_t, nvi_nvp);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
3127c478bd9Sstevel@tonic-gate 		return (NULL);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	nvp = &buf->nvi_nvp;
3157c478bd9Sstevel@tonic-gate 	nvp->nvp_size = len;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	return (nvp);
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * nvp_buf_free - de-Allocate an i_nvp_t.
3227c478bd9Sstevel@tonic-gate  */
3237c478bd9Sstevel@tonic-gate static void
3247c478bd9Sstevel@tonic-gate nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3277c478bd9Sstevel@tonic-gate 	size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * nvp_buf_link - link a new nv pair into the nvlist.
3347c478bd9Sstevel@tonic-gate  */
3357c478bd9Sstevel@tonic-gate static void
3367c478bd9Sstevel@tonic-gate nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3397c478bd9Sstevel@tonic-gate 	i_nvp_t *curr = NVPAIR2I_NVP(nvp);
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	/* Put element at end of nvlist */
3427c478bd9Sstevel@tonic-gate 	if (priv->nvp_list == NULL) {
3437c478bd9Sstevel@tonic-gate 		priv->nvp_list = priv->nvp_last = curr;
3447c478bd9Sstevel@tonic-gate 	} else {
3457c478bd9Sstevel@tonic-gate 		curr->nvi_prev = priv->nvp_last;
3467c478bd9Sstevel@tonic-gate 		priv->nvp_last->nvi_next = curr;
3477c478bd9Sstevel@tonic-gate 		priv->nvp_last = curr;
3487c478bd9Sstevel@tonic-gate 	}
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate static void
3557c478bd9Sstevel@tonic-gate nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
3587c478bd9Sstevel@tonic-gate 	i_nvp_t *curr = NVPAIR2I_NVP(nvp);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	/*
3617c478bd9Sstevel@tonic-gate 	 * protect nvlist_next_nvpair() against walking on freed memory.
3627c478bd9Sstevel@tonic-gate 	 */
3637c478bd9Sstevel@tonic-gate 	if (priv->nvp_curr == curr)
3647c478bd9Sstevel@tonic-gate 		priv->nvp_curr = curr->nvi_next;
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	if (curr == priv->nvp_list)
3677c478bd9Sstevel@tonic-gate 		priv->nvp_list = curr->nvi_next;
3687c478bd9Sstevel@tonic-gate 	else
3697c478bd9Sstevel@tonic-gate 		curr->nvi_prev->nvi_next = curr->nvi_next;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	if (curr == priv->nvp_last)
3727c478bd9Sstevel@tonic-gate 		priv->nvp_last = curr->nvi_prev;
3737c478bd9Sstevel@tonic-gate 	else
3747c478bd9Sstevel@tonic-gate 		curr->nvi_next->nvi_prev = curr->nvi_prev;
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * take a nvpair type and number of elements and make sure the are valid
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate static int
3817c478bd9Sstevel@tonic-gate i_validate_type_nelem(data_type_t type, uint_t nelem)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	switch (type) {
3847c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
3857c478bd9Sstevel@tonic-gate 		if (nelem != 0)
3867c478bd9Sstevel@tonic-gate 			return (EINVAL);
3877c478bd9Sstevel@tonic-gate 		break;
3887c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
3897c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
3907c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8:
3917c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8:
3927c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
3937c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
3947c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
3957c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
3967c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
3977c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
3987c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
3997c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
4007c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
4017c478bd9Sstevel@tonic-gate 		if (nelem != 1)
4027c478bd9Sstevel@tonic-gate 			return (EINVAL);
4037c478bd9Sstevel@tonic-gate 		break;
4047c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY:
4057c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
4067c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8_ARRAY:
4077c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8_ARRAY:
4087c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16_ARRAY:
4097c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16_ARRAY:
4107c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32_ARRAY:
4117c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32_ARRAY:
4127c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64_ARRAY:
4137c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64_ARRAY:
4147c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY:
4157c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY:
4167c478bd9Sstevel@tonic-gate 		/* we allow arrays with 0 elements */
4177c478bd9Sstevel@tonic-gate 		break;
4187c478bd9Sstevel@tonic-gate 	default:
4197c478bd9Sstevel@tonic-gate 		return (EINVAL);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	return (0);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate  * Verify nvp_name_sz and check the name string length.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate static int
4287c478bd9Sstevel@tonic-gate i_validate_nvpair_name(nvpair_t *nvp)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	if ((nvp->nvp_name_sz <= 0) ||
4317c478bd9Sstevel@tonic-gate 	    (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
4327c478bd9Sstevel@tonic-gate 		return (EFAULT);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/* verify the name string, make sure its terminated */
4357c478bd9Sstevel@tonic-gate 	if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
4367c478bd9Sstevel@tonic-gate 		return (EFAULT);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
4397c478bd9Sstevel@tonic-gate }
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate static int
4427c478bd9Sstevel@tonic-gate i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	switch (type) {
4457c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
4467c478bd9Sstevel@tonic-gate 		if (*(boolean_t *)data != B_TRUE &&
4477c478bd9Sstevel@tonic-gate 		    *(boolean_t *)data != B_FALSE)
4487c478bd9Sstevel@tonic-gate 			return (EINVAL);
4497c478bd9Sstevel@tonic-gate 		break;
4507c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY: {
4517c478bd9Sstevel@tonic-gate 		int i;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++)
4547c478bd9Sstevel@tonic-gate 			if (((boolean_t *)data)[i] != B_TRUE &&
4557c478bd9Sstevel@tonic-gate 			    ((boolean_t *)data)[i] != B_FALSE)
4567c478bd9Sstevel@tonic-gate 				return (EINVAL);
4577c478bd9Sstevel@tonic-gate 		break;
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 	default:
4607c478bd9Sstevel@tonic-gate 		break;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	return (0);
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
4677c478bd9Sstevel@tonic-gate  * This function takes a pointer to what should be a nvpair and it's size
4687c478bd9Sstevel@tonic-gate  * and then verifies that all the nvpair fields make sense and can be
4697c478bd9Sstevel@tonic-gate  * trusted.  This function is used when decoding packed nvpairs.
4707c478bd9Sstevel@tonic-gate  */
4717c478bd9Sstevel@tonic-gate static int
4727c478bd9Sstevel@tonic-gate i_validate_nvpair(nvpair_t *nvp)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	data_type_t type = NVP_TYPE(nvp);
4757c478bd9Sstevel@tonic-gate 	int size1, size2;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	/* verify nvp_name_sz, check the name string length */
4787c478bd9Sstevel@tonic-gate 	if (i_validate_nvpair_name(nvp) != 0)
4797c478bd9Sstevel@tonic-gate 		return (EFAULT);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
4827c478bd9Sstevel@tonic-gate 		return (EFAULT);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * verify nvp_type, nvp_value_elem, and also possibly
4867c478bd9Sstevel@tonic-gate 	 * verify string values and get the value size.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
4897c478bd9Sstevel@tonic-gate 	size1 = nvp->nvp_size - NVP_VALOFF(nvp);
4907c478bd9Sstevel@tonic-gate 	if (size2 < 0 || size1 != NV_ALIGN(size2))
4917c478bd9Sstevel@tonic-gate 		return (EFAULT);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	return (0);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate static int
4977c478bd9Sstevel@tonic-gate nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
4987c478bd9Sstevel@tonic-gate {
4997c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
5007c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
5037c478bd9Sstevel@tonic-gate 		return (EINVAL);
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
5067c478bd9Sstevel@tonic-gate 		nvpair_t *nvp = &curr->nvi_nvp;
5077c478bd9Sstevel@tonic-gate 		int err;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 		if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
5107c478bd9Sstevel@tonic-gate 		    NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
5117c478bd9Sstevel@tonic-gate 			return (err);
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	return (0);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate  * Frees all memory allocated for an nvpair (like embedded lists) with
5197c478bd9Sstevel@tonic-gate  * the exception of the nvpair buffer itself.
5207c478bd9Sstevel@tonic-gate  */
5217c478bd9Sstevel@tonic-gate static void
5227c478bd9Sstevel@tonic-gate nvpair_free(nvpair_t *nvp)
5237c478bd9Sstevel@tonic-gate {
5247c478bd9Sstevel@tonic-gate 	switch (NVP_TYPE(nvp)) {
5257c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
5267c478bd9Sstevel@tonic-gate 		nvlist_free(EMBEDDED_NVL(nvp));
5277c478bd9Sstevel@tonic-gate 		break;
5287c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY: {
5297c478bd9Sstevel@tonic-gate 		nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
5307c478bd9Sstevel@tonic-gate 		int i;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		for (i = 0; i < NVP_NELEM(nvp); i++)
5337c478bd9Sstevel@tonic-gate 			if (nvlp[i] != NULL)
5347c478bd9Sstevel@tonic-gate 				nvlist_free(nvlp[i]);
5357c478bd9Sstevel@tonic-gate 		break;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 	default:
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate /*
5437c478bd9Sstevel@tonic-gate  * nvlist_free - free an unpacked nvlist
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate void
5467c478bd9Sstevel@tonic-gate nvlist_free(nvlist_t *nvl)
5477c478bd9Sstevel@tonic-gate {
5487c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
5497c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if (nvl == NULL ||
5527c478bd9Sstevel@tonic-gate 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
5537c478bd9Sstevel@tonic-gate 		return;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	/*
5567c478bd9Sstevel@tonic-gate 	 * Unpacked nvlist are linked through i_nvp_t
5577c478bd9Sstevel@tonic-gate 	 */
5587c478bd9Sstevel@tonic-gate 	curr = priv->nvp_list;
5597c478bd9Sstevel@tonic-gate 	while (curr != NULL) {
5607c478bd9Sstevel@tonic-gate 		nvpair_t *nvp = &curr->nvi_nvp;
5617c478bd9Sstevel@tonic-gate 		curr = curr->nvi_next;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		nvpair_free(nvp);
5647c478bd9Sstevel@tonic-gate 		nvp_buf_free(nvl, nvp);
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
5687c478bd9Sstevel@tonic-gate 		nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
5697c478bd9Sstevel@tonic-gate 	else
570*5ad82045Snd150628 		nvl->nvl_priv = 0;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	nv_mem_free(priv, priv, sizeof (nvpriv_t));
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate static int
5767c478bd9Sstevel@tonic-gate nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
5797c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	if (nvp == NULL)
5827c478bd9Sstevel@tonic-gate 		return (0);
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
5857c478bd9Sstevel@tonic-gate 		if (&curr->nvi_nvp == nvp)
5867c478bd9Sstevel@tonic-gate 			return (1);
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	return (0);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate /*
5927c478bd9Sstevel@tonic-gate  * Make a copy of nvlist
5937c478bd9Sstevel@tonic-gate  */
5947c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
5957c478bd9Sstevel@tonic-gate int
5967c478bd9Sstevel@tonic-gate nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
5997c478bd9Sstevel@tonic-gate 	return (nvlist_xdup(nvl, nvlp,
6007c478bd9Sstevel@tonic-gate 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
6017c478bd9Sstevel@tonic-gate #else
6027c478bd9Sstevel@tonic-gate 	return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
6037c478bd9Sstevel@tonic-gate #endif
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate int
6077c478bd9Sstevel@tonic-gate nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
6087c478bd9Sstevel@tonic-gate {
6097c478bd9Sstevel@tonic-gate 	int err;
6103bb79becSeschrock 	nvlist_t *ret;
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	if (nvl == NULL || nvlp == NULL)
6137c478bd9Sstevel@tonic-gate 		return (EINVAL);
6147c478bd9Sstevel@tonic-gate 
6153bb79becSeschrock 	if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
6167c478bd9Sstevel@tonic-gate 		return (err);
6177c478bd9Sstevel@tonic-gate 
6183bb79becSeschrock 	if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
6193bb79becSeschrock 		nvlist_free(ret);
6203bb79becSeschrock 	else
6213bb79becSeschrock 		*nvlp = ret;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (err);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /*
6277c478bd9Sstevel@tonic-gate  * Remove all with matching name
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate int
6307c478bd9Sstevel@tonic-gate nvlist_remove_all(nvlist_t *nvl, const char *name)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
6337c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
6347c478bd9Sstevel@tonic-gate 	int error = ENOENT;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if (nvl == NULL || name == NULL ||
6377c478bd9Sstevel@tonic-gate 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
6387c478bd9Sstevel@tonic-gate 		return (EINVAL);
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	curr = priv->nvp_list;
6417c478bd9Sstevel@tonic-gate 	while (curr != NULL) {
6427c478bd9Sstevel@tonic-gate 		nvpair_t *nvp = &curr->nvi_nvp;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		curr = curr->nvi_next;
6457c478bd9Sstevel@tonic-gate 		if (strcmp(name, NVP_NAME(nvp)) != 0)
6467c478bd9Sstevel@tonic-gate 			continue;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		nvp_buf_unlink(nvl, nvp);
6497c478bd9Sstevel@tonic-gate 		nvpair_free(nvp);
6507c478bd9Sstevel@tonic-gate 		nvp_buf_free(nvl, nvp);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		error = 0;
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	return (error);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /*
6597c478bd9Sstevel@tonic-gate  * Remove first one with matching name and type
6607c478bd9Sstevel@tonic-gate  */
6617c478bd9Sstevel@tonic-gate int
6627c478bd9Sstevel@tonic-gate nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
6637c478bd9Sstevel@tonic-gate {
6647c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
6657c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (nvl == NULL || name == NULL ||
6687c478bd9Sstevel@tonic-gate 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
6697c478bd9Sstevel@tonic-gate 		return (EINVAL);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	curr = priv->nvp_list;
6727c478bd9Sstevel@tonic-gate 	while (curr != NULL) {
6737c478bd9Sstevel@tonic-gate 		nvpair_t *nvp = &curr->nvi_nvp;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
6767c478bd9Sstevel@tonic-gate 			nvp_buf_unlink(nvl, nvp);
6777c478bd9Sstevel@tonic-gate 			nvpair_free(nvp);
6787c478bd9Sstevel@tonic-gate 			nvp_buf_free(nvl, nvp);
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 			return (0);
6817c478bd9Sstevel@tonic-gate 		}
6827c478bd9Sstevel@tonic-gate 		curr = curr->nvi_next;
6837c478bd9Sstevel@tonic-gate 	}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return (ENOENT);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate /*
6897c478bd9Sstevel@tonic-gate  * This function calculates the size of an nvpair value.
6907c478bd9Sstevel@tonic-gate  *
6917c478bd9Sstevel@tonic-gate  * The data argument controls the behavior in case of the data types
6927c478bd9Sstevel@tonic-gate  * 	DATA_TYPE_STRING    	and
6937c478bd9Sstevel@tonic-gate  *	DATA_TYPE_STRING_ARRAY
6947c478bd9Sstevel@tonic-gate  * Is data == NULL then the size of the string(s) is excluded.
6957c478bd9Sstevel@tonic-gate  */
6967c478bd9Sstevel@tonic-gate static int
6977c478bd9Sstevel@tonic-gate i_get_value_size(data_type_t type, const void *data, uint_t nelem)
6987c478bd9Sstevel@tonic-gate {
6997c478bd9Sstevel@tonic-gate 	uint64_t value_sz;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	if (i_validate_type_nelem(type, nelem) != 0)
7027c478bd9Sstevel@tonic-gate 		return (-1);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* Calculate required size for holding value */
7057c478bd9Sstevel@tonic-gate 	switch (type) {
7067c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
7077c478bd9Sstevel@tonic-gate 		value_sz = 0;
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
7107c478bd9Sstevel@tonic-gate 		value_sz = sizeof (boolean_t);
7117c478bd9Sstevel@tonic-gate 		break;
7127c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
7137c478bd9Sstevel@tonic-gate 		value_sz = sizeof (uchar_t);
7147c478bd9Sstevel@tonic-gate 		break;
7157c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8:
7167c478bd9Sstevel@tonic-gate 		value_sz = sizeof (int8_t);
7177c478bd9Sstevel@tonic-gate 		break;
7187c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8:
7197c478bd9Sstevel@tonic-gate 		value_sz = sizeof (uint8_t);
7207c478bd9Sstevel@tonic-gate 		break;
7217c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
7227c478bd9Sstevel@tonic-gate 		value_sz = sizeof (int16_t);
7237c478bd9Sstevel@tonic-gate 		break;
7247c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
7257c478bd9Sstevel@tonic-gate 		value_sz = sizeof (uint16_t);
7267c478bd9Sstevel@tonic-gate 		break;
7277c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
7287c478bd9Sstevel@tonic-gate 		value_sz = sizeof (int32_t);
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
7317c478bd9Sstevel@tonic-gate 		value_sz = sizeof (uint32_t);
7327c478bd9Sstevel@tonic-gate 		break;
7337c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
7347c478bd9Sstevel@tonic-gate 		value_sz = sizeof (int64_t);
7357c478bd9Sstevel@tonic-gate 		break;
7367c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
7377c478bd9Sstevel@tonic-gate 		value_sz = sizeof (uint64_t);
7387c478bd9Sstevel@tonic-gate 		break;
7397c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
7407c478bd9Sstevel@tonic-gate 		if (data == NULL)
7417c478bd9Sstevel@tonic-gate 			value_sz = 0;
7427c478bd9Sstevel@tonic-gate 		else
7437c478bd9Sstevel@tonic-gate 			value_sz = strlen(data) + 1;
7447c478bd9Sstevel@tonic-gate 		break;
7457c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY:
7467c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (boolean_t);
7477c478bd9Sstevel@tonic-gate 		break;
7487c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
7497c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uchar_t);
7507c478bd9Sstevel@tonic-gate 		break;
7517c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8_ARRAY:
7527c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (int8_t);
7537c478bd9Sstevel@tonic-gate 		break;
7547c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8_ARRAY:
7557c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint8_t);
7567c478bd9Sstevel@tonic-gate 		break;
7577c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16_ARRAY:
7587c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (int16_t);
7597c478bd9Sstevel@tonic-gate 		break;
7607c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16_ARRAY:
7617c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint16_t);
7627c478bd9Sstevel@tonic-gate 		break;
7637c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32_ARRAY:
7647c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (int32_t);
7657c478bd9Sstevel@tonic-gate 		break;
7667c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32_ARRAY:
7677c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint32_t);
7687c478bd9Sstevel@tonic-gate 		break;
7697c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64_ARRAY:
7707c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (int64_t);
7717c478bd9Sstevel@tonic-gate 		break;
7727c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64_ARRAY:
7737c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint64_t);
7747c478bd9Sstevel@tonic-gate 		break;
7757c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY:
7767c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint64_t);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		if (data != NULL) {
7797c478bd9Sstevel@tonic-gate 			char *const *strs = data;
7807c478bd9Sstevel@tonic-gate 			uint_t i;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 			/* no alignment requirement for strings */
7837c478bd9Sstevel@tonic-gate 			for (i = 0; i < nelem; i++) {
7847c478bd9Sstevel@tonic-gate 				if (strs[i] == NULL)
7857c478bd9Sstevel@tonic-gate 					return (-1);
7867c478bd9Sstevel@tonic-gate 				value_sz += strlen(strs[i]) + 1;
7877c478bd9Sstevel@tonic-gate 			}
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 		break;
7907c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
7917c478bd9Sstevel@tonic-gate 		value_sz = sizeof (hrtime_t);
7927c478bd9Sstevel@tonic-gate 		break;
7937c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
7947c478bd9Sstevel@tonic-gate 		value_sz = NV_ALIGN(sizeof (nvlist_t));
7957c478bd9Sstevel@tonic-gate 		break;
7967c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY:
7977c478bd9Sstevel@tonic-gate 		value_sz = (uint64_t)nelem * sizeof (uint64_t) +
7987c478bd9Sstevel@tonic-gate 		    (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
7997c478bd9Sstevel@tonic-gate 		break;
8007c478bd9Sstevel@tonic-gate 	default:
8017c478bd9Sstevel@tonic-gate 		return (-1);
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	return (value_sz > INT32_MAX ? -1 : (int)value_sz);
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate static int
8087c478bd9Sstevel@tonic-gate nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
8097c478bd9Sstevel@tonic-gate {
8107c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
8117c478bd9Sstevel@tonic-gate 	int err;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
8147c478bd9Sstevel@tonic-gate 	    nvl->nvl_priv)) == NULL)
8157c478bd9Sstevel@tonic-gate 		return (ENOMEM);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
8207c478bd9Sstevel@tonic-gate 		nvlist_free(emb_nvl);
8217c478bd9Sstevel@tonic-gate 		emb_nvl->nvl_priv = 0;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	return (err);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * nvlist_add_common - Add new <name,value> pair to nvlist
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate static int
8317c478bd9Sstevel@tonic-gate nvlist_add_common(nvlist_t *nvl, const char *name,
8327c478bd9Sstevel@tonic-gate     data_type_t type, uint_t nelem, const void *data)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
835d9638e54Smws 	uint_t i;
836d9638e54Smws 
8377c478bd9Sstevel@tonic-gate 	int nvp_sz, name_sz, value_sz;
8387c478bd9Sstevel@tonic-gate 	int err = 0;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
8417c478bd9Sstevel@tonic-gate 		return (EINVAL);
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (nelem != 0 && data == NULL)
8447c478bd9Sstevel@tonic-gate 		return (EINVAL);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * Verify type and nelem and get the value size.
8487c478bd9Sstevel@tonic-gate 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
8497c478bd9Sstevel@tonic-gate 	 * is the size of the string(s) included.
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
8527c478bd9Sstevel@tonic-gate 		return (EINVAL);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (i_validate_nvpair_value(type, nelem, data) != 0)
8557c478bd9Sstevel@tonic-gate 		return (EINVAL);
8567c478bd9Sstevel@tonic-gate 
857d9638e54Smws 	/*
858d9638e54Smws 	 * If we're adding an nvlist or nvlist array, ensure that we are not
859d9638e54Smws 	 * adding the input nvlist to itself, which would cause recursion,
860d9638e54Smws 	 * and ensure that no NULL nvlist pointers are present.
861d9638e54Smws 	 */
8627c478bd9Sstevel@tonic-gate 	switch (type) {
8637c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
864d9638e54Smws 		if (data == nvl || data == NULL)
8657c478bd9Sstevel@tonic-gate 			return (EINVAL);
8667c478bd9Sstevel@tonic-gate 		break;
8677c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY: {
868d9638e54Smws 		nvlist_t **onvlp = (nvlist_t **)data;
869d9638e54Smws 		for (i = 0; i < nelem; i++) {
870d9638e54Smws 			if (onvlp[i] == nvl || onvlp[i] == NULL)
8717c478bd9Sstevel@tonic-gate 				return (EINVAL);
8727c478bd9Sstevel@tonic-gate 		}
8737c478bd9Sstevel@tonic-gate 		break;
874d9638e54Smws 	}
875*5ad82045Snd150628 	default:
876*5ad82045Snd150628 		break;
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/* calculate sizes of the nvpair elements and the nvpair itself */
8807c478bd9Sstevel@tonic-gate 	name_sz = strlen(name) + 1;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
8857c478bd9Sstevel@tonic-gate 		return (ENOMEM);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	ASSERT(nvp->nvp_size == nvp_sz);
8887c478bd9Sstevel@tonic-gate 	nvp->nvp_name_sz = name_sz;
8897c478bd9Sstevel@tonic-gate 	nvp->nvp_value_elem = nelem;
8907c478bd9Sstevel@tonic-gate 	nvp->nvp_type = type;
8917c478bd9Sstevel@tonic-gate 	bcopy(name, NVP_NAME(nvp), name_sz);
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	switch (type) {
8947c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
8957c478bd9Sstevel@tonic-gate 		break;
8967c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY: {
8977c478bd9Sstevel@tonic-gate 		char *const *strs = data;
8987c478bd9Sstevel@tonic-gate 		char *buf = NVP_VALUE(nvp);
8997c478bd9Sstevel@tonic-gate 		char **cstrs = (void *)buf;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		/* skip pre-allocated space for pointer array */
9027c478bd9Sstevel@tonic-gate 		buf += nelem * sizeof (uint64_t);
9037c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
9047c478bd9Sstevel@tonic-gate 			int slen = strlen(strs[i]) + 1;
9057c478bd9Sstevel@tonic-gate 			bcopy(strs[i], buf, slen);
9067c478bd9Sstevel@tonic-gate 			cstrs[i] = buf;
9077c478bd9Sstevel@tonic-gate 			buf += slen;
9087c478bd9Sstevel@tonic-gate 		}
9097c478bd9Sstevel@tonic-gate 		break;
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST: {
9127c478bd9Sstevel@tonic-gate 		nvlist_t *nnvl = EMBEDDED_NVL(nvp);
9137c478bd9Sstevel@tonic-gate 		nvlist_t *onvl = (nvlist_t *)data;
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
9167c478bd9Sstevel@tonic-gate 			nvp_buf_free(nvl, nvp);
9177c478bd9Sstevel@tonic-gate 			return (err);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 		break;
9207c478bd9Sstevel@tonic-gate 	}
9217c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY: {
9227c478bd9Sstevel@tonic-gate 		nvlist_t **onvlp = (nvlist_t **)data;
9237c478bd9Sstevel@tonic-gate 		nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
9247c478bd9Sstevel@tonic-gate 		nvlist_t *embedded = (nvlist_t *)
9257c478bd9Sstevel@tonic-gate 		    ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
9287c478bd9Sstevel@tonic-gate 			if ((err = nvlist_copy_embedded(nvl,
9297c478bd9Sstevel@tonic-gate 			    onvlp[i], embedded)) != 0) {
9307c478bd9Sstevel@tonic-gate 				/*
9317c478bd9Sstevel@tonic-gate 				 * Free any successfully created lists
9327c478bd9Sstevel@tonic-gate 				 */
9337c478bd9Sstevel@tonic-gate 				nvpair_free(nvp);
9347c478bd9Sstevel@tonic-gate 				nvp_buf_free(nvl, nvp);
9357c478bd9Sstevel@tonic-gate 				return (err);
9367c478bd9Sstevel@tonic-gate 			}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 			nvlp[i] = embedded++;
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		break;
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	default:
9437c478bd9Sstevel@tonic-gate 		bcopy(data, NVP_VALUE(nvp), value_sz);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/* if unique name, remove before add */
9477c478bd9Sstevel@tonic-gate 	if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
9487c478bd9Sstevel@tonic-gate 		(void) nvlist_remove_all(nvl, name);
9497c478bd9Sstevel@tonic-gate 	else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
9507c478bd9Sstevel@tonic-gate 		(void) nvlist_remove(nvl, name, type);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	nvp_buf_link(nvl, nvp);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	return (0);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate int
9587c478bd9Sstevel@tonic-gate nvlist_add_boolean(nvlist_t *nvl, const char *name)
9597c478bd9Sstevel@tonic-gate {
9607c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate int
9647c478bd9Sstevel@tonic-gate nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate int
9707c478bd9Sstevel@tonic-gate nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate int
9767c478bd9Sstevel@tonic-gate nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate int
9827c478bd9Sstevel@tonic-gate nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
9857c478bd9Sstevel@tonic-gate }
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate int
9887c478bd9Sstevel@tonic-gate nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
9897c478bd9Sstevel@tonic-gate {
9907c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate int
9947c478bd9Sstevel@tonic-gate nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
9957c478bd9Sstevel@tonic-gate {
9967c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
9977c478bd9Sstevel@tonic-gate }
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate int
10007c478bd9Sstevel@tonic-gate nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate int
10067c478bd9Sstevel@tonic-gate nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
10077c478bd9Sstevel@tonic-gate {
10087c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate int
10127c478bd9Sstevel@tonic-gate nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate int
10187c478bd9Sstevel@tonic-gate nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate int
10247c478bd9Sstevel@tonic-gate nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate int
10307c478bd9Sstevel@tonic-gate nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
10317c478bd9Sstevel@tonic-gate     boolean_t *a, uint_t n)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate int
10377c478bd9Sstevel@tonic-gate nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
10387c478bd9Sstevel@tonic-gate {
10397c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate int
10437c478bd9Sstevel@tonic-gate nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
10467c478bd9Sstevel@tonic-gate }
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate int
10497c478bd9Sstevel@tonic-gate nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate int
10557c478bd9Sstevel@tonic-gate nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
10567c478bd9Sstevel@tonic-gate {
10577c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
10587c478bd9Sstevel@tonic-gate }
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate int
10617c478bd9Sstevel@tonic-gate nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
10627c478bd9Sstevel@tonic-gate {
10637c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate int
10677c478bd9Sstevel@tonic-gate nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
10707c478bd9Sstevel@tonic-gate }
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate int
10737c478bd9Sstevel@tonic-gate nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
10767c478bd9Sstevel@tonic-gate }
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate int
10797c478bd9Sstevel@tonic-gate nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate int
10857c478bd9Sstevel@tonic-gate nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate int
10917c478bd9Sstevel@tonic-gate nvlist_add_string_array(nvlist_t *nvl, const char *name,
10927c478bd9Sstevel@tonic-gate     char *const *a, uint_t n)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
10957c478bd9Sstevel@tonic-gate }
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate int
10987c478bd9Sstevel@tonic-gate nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate int
11047c478bd9Sstevel@tonic-gate nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate int
11107c478bd9Sstevel@tonic-gate nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /* reading name-value pairs */
11167c478bd9Sstevel@tonic-gate nvpair_t *
11177c478bd9Sstevel@tonic-gate nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
11207c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (nvl == NULL ||
11237c478bd9Sstevel@tonic-gate 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
11247c478bd9Sstevel@tonic-gate 		return (NULL);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	curr = NVPAIR2I_NVP(nvp);
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	/*
11297c478bd9Sstevel@tonic-gate 	 * Ensure that nvp is an valid pointer.
11307c478bd9Sstevel@tonic-gate 	 */
11317c478bd9Sstevel@tonic-gate 	if (nvp == NULL)
11327c478bd9Sstevel@tonic-gate 		curr = priv->nvp_list;
11337c478bd9Sstevel@tonic-gate 	else if (priv->nvp_curr == curr)
11347c478bd9Sstevel@tonic-gate 		curr = curr->nvi_next;
11357c478bd9Sstevel@tonic-gate 	else if (nvlist_contains_nvp(nvl, nvp) == 0)
11367c478bd9Sstevel@tonic-gate 		curr = NULL;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	priv->nvp_curr = curr;
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 	return (curr != NULL ? &curr->nvi_nvp : NULL);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate char *
11447c478bd9Sstevel@tonic-gate nvpair_name(nvpair_t *nvp)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	return (NVP_NAME(nvp));
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate data_type_t
11507c478bd9Sstevel@tonic-gate nvpair_type(nvpair_t *nvp)
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 	return (NVP_TYPE(nvp));
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate static int
11567c478bd9Sstevel@tonic-gate nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
11577c478bd9Sstevel@tonic-gate {
11587c478bd9Sstevel@tonic-gate 	if (nvp == NULL || nvpair_type(nvp) != type)
11597c478bd9Sstevel@tonic-gate 		return (EINVAL);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	/*
11627c478bd9Sstevel@tonic-gate 	 * For non-array types, we copy the data.
11637c478bd9Sstevel@tonic-gate 	 * For array types (including string), we set a pointer.
11647c478bd9Sstevel@tonic-gate 	 */
11657c478bd9Sstevel@tonic-gate 	switch (type) {
11667c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
11677c478bd9Sstevel@tonic-gate 		if (nelem != NULL)
11687c478bd9Sstevel@tonic-gate 			*nelem = 0;
11697c478bd9Sstevel@tonic-gate 		break;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
11727c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
11737c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8:
11747c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8:
11757c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
11767c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
11777c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
11787c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
11797c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
11807c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
11817c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
11827c478bd9Sstevel@tonic-gate 		if (data == NULL)
11837c478bd9Sstevel@tonic-gate 			return (EINVAL);
11847c478bd9Sstevel@tonic-gate 		bcopy(NVP_VALUE(nvp), data,
11857c478bd9Sstevel@tonic-gate 		    (size_t)i_get_value_size(type, NULL, 1));
11867c478bd9Sstevel@tonic-gate 		if (nelem != NULL)
11877c478bd9Sstevel@tonic-gate 			*nelem = 1;
11887c478bd9Sstevel@tonic-gate 		break;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
11917c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
11927c478bd9Sstevel@tonic-gate 		if (data == NULL)
11937c478bd9Sstevel@tonic-gate 			return (EINVAL);
11947c478bd9Sstevel@tonic-gate 		*(void **)data = (void *)NVP_VALUE(nvp);
11957c478bd9Sstevel@tonic-gate 		if (nelem != NULL)
11967c478bd9Sstevel@tonic-gate 			*nelem = 1;
11977c478bd9Sstevel@tonic-gate 		break;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY:
12007c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
12017c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8_ARRAY:
12027c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8_ARRAY:
12037c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16_ARRAY:
12047c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16_ARRAY:
12057c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32_ARRAY:
12067c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32_ARRAY:
12077c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64_ARRAY:
12087c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64_ARRAY:
12097c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY:
12107c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY:
12117c478bd9Sstevel@tonic-gate 		if (nelem == NULL || data == NULL)
12127c478bd9Sstevel@tonic-gate 			return (EINVAL);
12137c478bd9Sstevel@tonic-gate 		if ((*nelem = NVP_NELEM(nvp)) != 0)
12147c478bd9Sstevel@tonic-gate 			*(void **)data = (void *)NVP_VALUE(nvp);
12157c478bd9Sstevel@tonic-gate 		else
12167c478bd9Sstevel@tonic-gate 			*(void **)data = NULL;
12177c478bd9Sstevel@tonic-gate 		break;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	default:
12207c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
12217c478bd9Sstevel@tonic-gate 	}
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	return (0);
12247c478bd9Sstevel@tonic-gate }
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate static int
12277c478bd9Sstevel@tonic-gate nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
12287c478bd9Sstevel@tonic-gate     uint_t *nelem, void *data)
12297c478bd9Sstevel@tonic-gate {
12307c478bd9Sstevel@tonic-gate 	nvpriv_t *priv;
12317c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
12327c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	if (name == NULL || nvl == NULL ||
12357c478bd9Sstevel@tonic-gate 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
12367c478bd9Sstevel@tonic-gate 		return (EINVAL);
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
12397c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
12427c478bd9Sstevel@tonic-gate 		nvp = &curr->nvi_nvp;
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 		if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
12457c478bd9Sstevel@tonic-gate 			return (nvpair_value_common(nvp, type, nelem, data));
12467c478bd9Sstevel@tonic-gate 	}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	return (ENOENT);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate int
12527c478bd9Sstevel@tonic-gate nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
12537c478bd9Sstevel@tonic-gate {
12547c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate int
12587c478bd9Sstevel@tonic-gate nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
12597c478bd9Sstevel@tonic-gate {
12607c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name,
12617c478bd9Sstevel@tonic-gate 	    DATA_TYPE_BOOLEAN_VALUE, NULL, val));
12627c478bd9Sstevel@tonic-gate }
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate int
12657c478bd9Sstevel@tonic-gate nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
12687c478bd9Sstevel@tonic-gate }
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate int
12717c478bd9Sstevel@tonic-gate nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
12747c478bd9Sstevel@tonic-gate }
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate int
12777c478bd9Sstevel@tonic-gate nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
12787c478bd9Sstevel@tonic-gate {
12797c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate int
12837c478bd9Sstevel@tonic-gate nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate int
12897c478bd9Sstevel@tonic-gate nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate int
12957c478bd9Sstevel@tonic-gate nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
12987c478bd9Sstevel@tonic-gate }
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate int
13017c478bd9Sstevel@tonic-gate nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
13027c478bd9Sstevel@tonic-gate {
13037c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate int
13077c478bd9Sstevel@tonic-gate nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate int
13137c478bd9Sstevel@tonic-gate nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
13147c478bd9Sstevel@tonic-gate {
13157c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate int
13197c478bd9Sstevel@tonic-gate nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate int
13257c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate int
13317c478bd9Sstevel@tonic-gate nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
13327c478bd9Sstevel@tonic-gate     boolean_t **a, uint_t *n)
13337c478bd9Sstevel@tonic-gate {
13347c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name,
13357c478bd9Sstevel@tonic-gate 	    DATA_TYPE_BOOLEAN_ARRAY, n, a));
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate int
13397c478bd9Sstevel@tonic-gate nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
13407c478bd9Sstevel@tonic-gate     uchar_t **a, uint_t *n)
13417c478bd9Sstevel@tonic-gate {
13427c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate int
13467c478bd9Sstevel@tonic-gate nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
13477c478bd9Sstevel@tonic-gate {
13487c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
13497c478bd9Sstevel@tonic-gate }
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate int
13527c478bd9Sstevel@tonic-gate nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
13537c478bd9Sstevel@tonic-gate     uint8_t **a, uint_t *n)
13547c478bd9Sstevel@tonic-gate {
13557c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate int
13597c478bd9Sstevel@tonic-gate nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
13607c478bd9Sstevel@tonic-gate     int16_t **a, uint_t *n)
13617c478bd9Sstevel@tonic-gate {
13627c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate int
13667c478bd9Sstevel@tonic-gate nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
13677c478bd9Sstevel@tonic-gate     uint16_t **a, uint_t *n)
13687c478bd9Sstevel@tonic-gate {
13697c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
13707c478bd9Sstevel@tonic-gate }
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate int
13737c478bd9Sstevel@tonic-gate nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
13747c478bd9Sstevel@tonic-gate     int32_t **a, uint_t *n)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
13777c478bd9Sstevel@tonic-gate }
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate int
13807c478bd9Sstevel@tonic-gate nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
13817c478bd9Sstevel@tonic-gate     uint32_t **a, uint_t *n)
13827c478bd9Sstevel@tonic-gate {
13837c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
13847c478bd9Sstevel@tonic-gate }
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate int
13877c478bd9Sstevel@tonic-gate nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
13887c478bd9Sstevel@tonic-gate     int64_t **a, uint_t *n)
13897c478bd9Sstevel@tonic-gate {
13907c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate int
13947c478bd9Sstevel@tonic-gate nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
13957c478bd9Sstevel@tonic-gate     uint64_t **a, uint_t *n)
13967c478bd9Sstevel@tonic-gate {
13977c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate int
14017c478bd9Sstevel@tonic-gate nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
14027c478bd9Sstevel@tonic-gate     char ***a, uint_t *n)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
14057c478bd9Sstevel@tonic-gate }
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate int
14087c478bd9Sstevel@tonic-gate nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
14097c478bd9Sstevel@tonic-gate     nvlist_t ***a, uint_t *n)
14107c478bd9Sstevel@tonic-gate {
14117c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate int
14157c478bd9Sstevel@tonic-gate nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
14167c478bd9Sstevel@tonic-gate {
14177c478bd9Sstevel@tonic-gate 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate int
14217c478bd9Sstevel@tonic-gate nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
14227c478bd9Sstevel@tonic-gate {
14237c478bd9Sstevel@tonic-gate 	va_list ap;
14247c478bd9Sstevel@tonic-gate 	char *name;
14257c478bd9Sstevel@tonic-gate 	int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
14267c478bd9Sstevel@tonic-gate 	int ret = 0;
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	va_start(ap, flag);
14297c478bd9Sstevel@tonic-gate 	while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
14307c478bd9Sstevel@tonic-gate 		data_type_t type;
14317c478bd9Sstevel@tonic-gate 		void *val;
14327c478bd9Sstevel@tonic-gate 		uint_t *nelem;
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 		switch (type = va_arg(ap, data_type_t)) {
14357c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN:
14367c478bd9Sstevel@tonic-gate 			ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
14377c478bd9Sstevel@tonic-gate 			break;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_VALUE:
14407c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE:
14417c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8:
14427c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8:
14437c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16:
14447c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16:
14457c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32:
14467c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32:
14477c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64:
14487c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64:
14497c478bd9Sstevel@tonic-gate 		case DATA_TYPE_HRTIME:
14507c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING:
14517c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST:
14527c478bd9Sstevel@tonic-gate 			val = va_arg(ap, void *);
14537c478bd9Sstevel@tonic-gate 			ret = nvlist_lookup_common(nvl, name, type, NULL, val);
14547c478bd9Sstevel@tonic-gate 			break;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BYTE_ARRAY:
14577c478bd9Sstevel@tonic-gate 		case DATA_TYPE_BOOLEAN_ARRAY:
14587c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT8_ARRAY:
14597c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT8_ARRAY:
14607c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT16_ARRAY:
14617c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT16_ARRAY:
14627c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT32_ARRAY:
14637c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT32_ARRAY:
14647c478bd9Sstevel@tonic-gate 		case DATA_TYPE_INT64_ARRAY:
14657c478bd9Sstevel@tonic-gate 		case DATA_TYPE_UINT64_ARRAY:
14667c478bd9Sstevel@tonic-gate 		case DATA_TYPE_STRING_ARRAY:
14677c478bd9Sstevel@tonic-gate 		case DATA_TYPE_NVLIST_ARRAY:
14687c478bd9Sstevel@tonic-gate 			val = va_arg(ap, void *);
14697c478bd9Sstevel@tonic-gate 			nelem = va_arg(ap, uint_t *);
14707c478bd9Sstevel@tonic-gate 			ret = nvlist_lookup_common(nvl, name, type, nelem, val);
14717c478bd9Sstevel@tonic-gate 			break;
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 		default:
14747c478bd9Sstevel@tonic-gate 			ret = EINVAL;
14757c478bd9Sstevel@tonic-gate 		}
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		if (ret == ENOENT && noentok)
14787c478bd9Sstevel@tonic-gate 			ret = 0;
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 	va_end(ap);
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	return (ret);
14837c478bd9Sstevel@tonic-gate }
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate int
14867c478bd9Sstevel@tonic-gate nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate int
14927c478bd9Sstevel@tonic-gate nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
14937c478bd9Sstevel@tonic-gate {
14947c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate int
14987c478bd9Sstevel@tonic-gate nvpair_value_int8(nvpair_t *nvp, int8_t *val)
14997c478bd9Sstevel@tonic-gate {
15007c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate int
15047c478bd9Sstevel@tonic-gate nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate int
15107c478bd9Sstevel@tonic-gate nvpair_value_int16(nvpair_t *nvp, int16_t *val)
15117c478bd9Sstevel@tonic-gate {
15127c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate int
15167c478bd9Sstevel@tonic-gate nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
15177c478bd9Sstevel@tonic-gate {
15187c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
15197c478bd9Sstevel@tonic-gate }
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate int
15227c478bd9Sstevel@tonic-gate nvpair_value_int32(nvpair_t *nvp, int32_t *val)
15237c478bd9Sstevel@tonic-gate {
15247c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
15257c478bd9Sstevel@tonic-gate }
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate int
15287c478bd9Sstevel@tonic-gate nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
15297c478bd9Sstevel@tonic-gate {
15307c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
15317c478bd9Sstevel@tonic-gate }
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate int
15347c478bd9Sstevel@tonic-gate nvpair_value_int64(nvpair_t *nvp, int64_t *val)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate int
15407c478bd9Sstevel@tonic-gate nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
15437c478bd9Sstevel@tonic-gate }
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate int
15467c478bd9Sstevel@tonic-gate nvpair_value_string(nvpair_t *nvp, char **val)
15477c478bd9Sstevel@tonic-gate {
15487c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate int
15527c478bd9Sstevel@tonic-gate nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
15537c478bd9Sstevel@tonic-gate {
15547c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
15557c478bd9Sstevel@tonic-gate }
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate int
15587c478bd9Sstevel@tonic-gate nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
15597c478bd9Sstevel@tonic-gate {
15607c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate int
15647c478bd9Sstevel@tonic-gate nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
15657c478bd9Sstevel@tonic-gate {
15667c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
15677c478bd9Sstevel@tonic-gate }
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate int
15707c478bd9Sstevel@tonic-gate nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
15717c478bd9Sstevel@tonic-gate {
15727c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate int
15767c478bd9Sstevel@tonic-gate nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate int
15827c478bd9Sstevel@tonic-gate nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
15837c478bd9Sstevel@tonic-gate {
15847c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
15857c478bd9Sstevel@tonic-gate }
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate int
15887c478bd9Sstevel@tonic-gate nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
15897c478bd9Sstevel@tonic-gate {
15907c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
15917c478bd9Sstevel@tonic-gate }
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate int
15947c478bd9Sstevel@tonic-gate nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
15957c478bd9Sstevel@tonic-gate {
15967c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate int
16007c478bd9Sstevel@tonic-gate nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
16037c478bd9Sstevel@tonic-gate }
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate int
16067c478bd9Sstevel@tonic-gate nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
16077c478bd9Sstevel@tonic-gate {
16087c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate int
16127c478bd9Sstevel@tonic-gate nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate int
16187c478bd9Sstevel@tonic-gate nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate int
16247c478bd9Sstevel@tonic-gate nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
16277c478bd9Sstevel@tonic-gate }
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate int
16307c478bd9Sstevel@tonic-gate nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
16317c478bd9Sstevel@tonic-gate {
16327c478bd9Sstevel@tonic-gate 	return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
16337c478bd9Sstevel@tonic-gate }
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate /*
16367c478bd9Sstevel@tonic-gate  * Add specified pair to the list.
16377c478bd9Sstevel@tonic-gate  */
16387c478bd9Sstevel@tonic-gate int
16397c478bd9Sstevel@tonic-gate nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	if (nvl == NULL || nvp == NULL)
16427c478bd9Sstevel@tonic-gate 		return (EINVAL);
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
16457c478bd9Sstevel@tonic-gate 	    NVP_NELEM(nvp), NVP_VALUE(nvp)));
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate /*
16497c478bd9Sstevel@tonic-gate  * Merge the supplied nvlists and put the result in dst.
16507c478bd9Sstevel@tonic-gate  * The merged list will contain all names specified in both lists,
16517c478bd9Sstevel@tonic-gate  * the values are taken from nvl in the case of duplicates.
16527c478bd9Sstevel@tonic-gate  * Return 0 on success.
16537c478bd9Sstevel@tonic-gate  */
16547c478bd9Sstevel@tonic-gate /*ARGSUSED*/
16557c478bd9Sstevel@tonic-gate int
16567c478bd9Sstevel@tonic-gate nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
16577c478bd9Sstevel@tonic-gate {
16587c478bd9Sstevel@tonic-gate 	if (nvl == NULL || dst == NULL)
16597c478bd9Sstevel@tonic-gate 		return (EINVAL);
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate 	if (dst != nvl)
16627c478bd9Sstevel@tonic-gate 		return (nvlist_copy_pairs(nvl, dst));
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	return (0);
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate /*
16687c478bd9Sstevel@tonic-gate  * Encoding related routines
16697c478bd9Sstevel@tonic-gate  */
16707c478bd9Sstevel@tonic-gate #define	NVS_OP_ENCODE	0
16717c478bd9Sstevel@tonic-gate #define	NVS_OP_DECODE	1
16727c478bd9Sstevel@tonic-gate #define	NVS_OP_GETSIZE	2
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate typedef struct nvs_ops nvs_ops_t;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate typedef struct {
16777c478bd9Sstevel@tonic-gate 	int		nvs_op;
16787c478bd9Sstevel@tonic-gate 	const nvs_ops_t	*nvs_ops;
16797c478bd9Sstevel@tonic-gate 	void		*nvs_private;
16807c478bd9Sstevel@tonic-gate 	nvpriv_t	*nvs_priv;
16817c478bd9Sstevel@tonic-gate } nvstream_t;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate /*
16847c478bd9Sstevel@tonic-gate  * nvs operations are:
16857c478bd9Sstevel@tonic-gate  *   - nvs_nvlist
16867c478bd9Sstevel@tonic-gate  *     encoding / decoding of a nvlist header (nvlist_t)
16877c478bd9Sstevel@tonic-gate  *     calculates the size used for header and end detection
16887c478bd9Sstevel@tonic-gate  *
16897c478bd9Sstevel@tonic-gate  *   - nvs_nvpair
16907c478bd9Sstevel@tonic-gate  *     responsible for the first part of encoding / decoding of an nvpair
16917c478bd9Sstevel@tonic-gate  *     calculates the decoded size of an nvpair
16927c478bd9Sstevel@tonic-gate  *
16937c478bd9Sstevel@tonic-gate  *   - nvs_nvp_op
16947c478bd9Sstevel@tonic-gate  *     second part of encoding / decoding of an nvpair
16957c478bd9Sstevel@tonic-gate  *
16967c478bd9Sstevel@tonic-gate  *   - nvs_nvp_size
16977c478bd9Sstevel@tonic-gate  *     calculates the encoding size of an nvpair
16987c478bd9Sstevel@tonic-gate  *
16997c478bd9Sstevel@tonic-gate  *   - nvs_nvl_fini
17007c478bd9Sstevel@tonic-gate  *     encodes the end detection mark (zeros).
17017c478bd9Sstevel@tonic-gate  */
17027c478bd9Sstevel@tonic-gate struct nvs_ops {
17037c478bd9Sstevel@tonic-gate 	int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
17047c478bd9Sstevel@tonic-gate 	int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
17057c478bd9Sstevel@tonic-gate 	int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
17067c478bd9Sstevel@tonic-gate 	int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
17077c478bd9Sstevel@tonic-gate 	int (*nvs_nvl_fini)(nvstream_t *);
17087c478bd9Sstevel@tonic-gate };
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate typedef struct {
17117c478bd9Sstevel@tonic-gate 	char	nvh_encoding;	/* nvs encoding method */
17127c478bd9Sstevel@tonic-gate 	char	nvh_endian;	/* nvs endian */
17137c478bd9Sstevel@tonic-gate 	char	nvh_reserved1;	/* reserved for future use */
17147c478bd9Sstevel@tonic-gate 	char	nvh_reserved2;	/* reserved for future use */
17157c478bd9Sstevel@tonic-gate } nvs_header_t;
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate static int
17187c478bd9Sstevel@tonic-gate nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
17197c478bd9Sstevel@tonic-gate {
17207c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
17217c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * Walk nvpair in list and encode each nvpair
17257c478bd9Sstevel@tonic-gate 	 */
17267c478bd9Sstevel@tonic-gate 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
17277c478bd9Sstevel@tonic-gate 		if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
17287c478bd9Sstevel@tonic-gate 			return (EFAULT);
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	return (nvs->nvs_ops->nvs_nvl_fini(nvs));
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate static int
17347c478bd9Sstevel@tonic-gate nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
17357c478bd9Sstevel@tonic-gate {
17367c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
17377c478bd9Sstevel@tonic-gate 	size_t nvsize;
17387c478bd9Sstevel@tonic-gate 	int err;
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate 	/*
17417c478bd9Sstevel@tonic-gate 	 * Get decoded size of next pair in stream, alloc
17427c478bd9Sstevel@tonic-gate 	 * memory for nvpair_t, then decode the nvpair
17437c478bd9Sstevel@tonic-gate 	 */
17447c478bd9Sstevel@tonic-gate 	while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
17457c478bd9Sstevel@tonic-gate 		if (nvsize == 0) /* end of list */
17467c478bd9Sstevel@tonic-gate 			break;
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate 		/* make sure len makes sense */
17497c478bd9Sstevel@tonic-gate 		if (nvsize < NVP_SIZE_CALC(1, 0))
17507c478bd9Sstevel@tonic-gate 			return (EFAULT);
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 		if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
17537c478bd9Sstevel@tonic-gate 			return (ENOMEM);
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 		if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
17567c478bd9Sstevel@tonic-gate 			nvp_buf_free(nvl, nvp);
17577c478bd9Sstevel@tonic-gate 			return (err);
17587c478bd9Sstevel@tonic-gate 		}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 		if (i_validate_nvpair(nvp) != 0) {
17617c478bd9Sstevel@tonic-gate 			nvpair_free(nvp);
17627c478bd9Sstevel@tonic-gate 			nvp_buf_free(nvl, nvp);
17637c478bd9Sstevel@tonic-gate 			return (EFAULT);
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 
17667c478bd9Sstevel@tonic-gate 		nvp_buf_link(nvl, nvp);
17677c478bd9Sstevel@tonic-gate 	}
17687c478bd9Sstevel@tonic-gate 	return (err);
17697c478bd9Sstevel@tonic-gate }
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate static int
17727c478bd9Sstevel@tonic-gate nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
17737c478bd9Sstevel@tonic-gate {
17747c478bd9Sstevel@tonic-gate 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
17757c478bd9Sstevel@tonic-gate 	i_nvp_t *curr;
17767c478bd9Sstevel@tonic-gate 	uint64_t nvsize = *buflen;
17777c478bd9Sstevel@tonic-gate 	size_t size;
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	/*
17807c478bd9Sstevel@tonic-gate 	 * Get encoded size of nvpairs in nvlist
17817c478bd9Sstevel@tonic-gate 	 */
17827c478bd9Sstevel@tonic-gate 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
17837c478bd9Sstevel@tonic-gate 		if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
17847c478bd9Sstevel@tonic-gate 			return (EINVAL);
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 		if ((nvsize += size) > INT32_MAX)
17877c478bd9Sstevel@tonic-gate 			return (EINVAL);
17887c478bd9Sstevel@tonic-gate 	}
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	*buflen = nvsize;
17917c478bd9Sstevel@tonic-gate 	return (0);
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate static int
17957c478bd9Sstevel@tonic-gate nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	int err;
17987c478bd9Sstevel@tonic-gate 
1799*5ad82045Snd150628 	if (nvl->nvl_priv == 0)
18007c478bd9Sstevel@tonic-gate 		return (EFAULT);
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	/*
18037c478bd9Sstevel@tonic-gate 	 * Perform the operation, starting with header, then each nvpair
18047c478bd9Sstevel@tonic-gate 	 */
18057c478bd9Sstevel@tonic-gate 	if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
18067c478bd9Sstevel@tonic-gate 		return (err);
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
18097c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
18107c478bd9Sstevel@tonic-gate 		err = nvs_encode_pairs(nvs, nvl);
18117c478bd9Sstevel@tonic-gate 		break;
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
18147c478bd9Sstevel@tonic-gate 		err = nvs_decode_pairs(nvs, nvl);
18157c478bd9Sstevel@tonic-gate 		break;
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE:
18187c478bd9Sstevel@tonic-gate 		err = nvs_getsize_pairs(nvs, nvl, buflen);
18197c478bd9Sstevel@tonic-gate 		break;
18207c478bd9Sstevel@tonic-gate 
18217c478bd9Sstevel@tonic-gate 	default:
18227c478bd9Sstevel@tonic-gate 		err = EINVAL;
18237c478bd9Sstevel@tonic-gate 	}
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	return (err);
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate static int
18297c478bd9Sstevel@tonic-gate nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
18307c478bd9Sstevel@tonic-gate {
18317c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
18327c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
18337c478bd9Sstevel@tonic-gate 		return (nvs_operation(nvs, embedded, NULL));
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
18367c478bd9Sstevel@tonic-gate 		nvpriv_t *priv;
18377c478bd9Sstevel@tonic-gate 		int err;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 		if (embedded->nvl_version != NV_VERSION)
18407c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 		if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
18437c478bd9Sstevel@tonic-gate 			return (ENOMEM);
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 		nvlist_init(embedded, embedded->nvl_nvflag, priv);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
18487c478bd9Sstevel@tonic-gate 			nvlist_free(embedded);
18497c478bd9Sstevel@tonic-gate 		return (err);
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 	default:
18527c478bd9Sstevel@tonic-gate 		break;
18537c478bd9Sstevel@tonic-gate 	}
18547c478bd9Sstevel@tonic-gate 
18557c478bd9Sstevel@tonic-gate 	return (EINVAL);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate static int
18597c478bd9Sstevel@tonic-gate nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	size_t nelem = NVP_NELEM(nvp);
18627c478bd9Sstevel@tonic-gate 	nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
18637c478bd9Sstevel@tonic-gate 	int i;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
18667c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
18677c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++)
18687c478bd9Sstevel@tonic-gate 			if (nvs_embedded(nvs, nvlp[i]) != 0)
18697c478bd9Sstevel@tonic-gate 				return (EFAULT);
18707c478bd9Sstevel@tonic-gate 		break;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
18737c478bd9Sstevel@tonic-gate 		size_t len = nelem * sizeof (uint64_t);
18747c478bd9Sstevel@tonic-gate 		nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 		bzero(nvlp, len);	/* don't trust packed data */
18777c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
18787c478bd9Sstevel@tonic-gate 			if (nvs_embedded(nvs, embedded) != 0) {
18797c478bd9Sstevel@tonic-gate 				nvpair_free(nvp);
18807c478bd9Sstevel@tonic-gate 				return (EFAULT);
18817c478bd9Sstevel@tonic-gate 			}
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 			nvlp[i] = embedded++;
18847c478bd9Sstevel@tonic-gate 		}
18857c478bd9Sstevel@tonic-gate 		break;
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE: {
18887c478bd9Sstevel@tonic-gate 		uint64_t nvsize = 0;
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
18917c478bd9Sstevel@tonic-gate 			size_t nvp_sz = 0;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 			if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
18947c478bd9Sstevel@tonic-gate 				return (EINVAL);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 			if ((nvsize += nvp_sz) > INT32_MAX)
18977c478bd9Sstevel@tonic-gate 				return (EINVAL);
18987c478bd9Sstevel@tonic-gate 		}
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 		*size = nvsize;
19017c478bd9Sstevel@tonic-gate 		break;
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 	default:
19047c478bd9Sstevel@tonic-gate 		return (EINVAL);
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	return (0);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
19117c478bd9Sstevel@tonic-gate static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate /*
19147c478bd9Sstevel@tonic-gate  * Common routine for nvlist operations:
19157c478bd9Sstevel@tonic-gate  * encode, decode, getsize (encoded size).
19167c478bd9Sstevel@tonic-gate  */
19177c478bd9Sstevel@tonic-gate static int
19187c478bd9Sstevel@tonic-gate nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
19197c478bd9Sstevel@tonic-gate     int nvs_op)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate 	int err = 0;
19227c478bd9Sstevel@tonic-gate 	nvstream_t nvs;
19237c478bd9Sstevel@tonic-gate 	int nvl_endian;
19247c478bd9Sstevel@tonic-gate #ifdef	_LITTLE_ENDIAN
19257c478bd9Sstevel@tonic-gate 	int host_endian = 1;
19267c478bd9Sstevel@tonic-gate #else
19277c478bd9Sstevel@tonic-gate 	int host_endian = 0;
19287c478bd9Sstevel@tonic-gate #endif	/* _LITTLE_ENDIAN */
19297c478bd9Sstevel@tonic-gate 	nvs_header_t *nvh = (void *)buf;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	if (buflen == NULL || nvl == NULL ||
19327c478bd9Sstevel@tonic-gate 	    (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
19337c478bd9Sstevel@tonic-gate 		return (EINVAL);
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	nvs.nvs_op = nvs_op;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
19397c478bd9Sstevel@tonic-gate 	 * a buffer is allocated.  The first 4 bytes in the buffer are
19407c478bd9Sstevel@tonic-gate 	 * used for encoding method and host endian.
19417c478bd9Sstevel@tonic-gate 	 */
19427c478bd9Sstevel@tonic-gate 	switch (nvs_op) {
19437c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
19447c478bd9Sstevel@tonic-gate 		if (buf == NULL || *buflen < sizeof (nvs_header_t))
19457c478bd9Sstevel@tonic-gate 			return (EINVAL);
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		nvh->nvh_encoding = encoding;
19487c478bd9Sstevel@tonic-gate 		nvh->nvh_endian = nvl_endian = host_endian;
19497c478bd9Sstevel@tonic-gate 		nvh->nvh_reserved1 = 0;
19507c478bd9Sstevel@tonic-gate 		nvh->nvh_reserved2 = 0;
19517c478bd9Sstevel@tonic-gate 		break;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
19547c478bd9Sstevel@tonic-gate 		if (buf == NULL || *buflen < sizeof (nvs_header_t))
19557c478bd9Sstevel@tonic-gate 			return (EINVAL);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 		/* get method of encoding from first byte */
19587c478bd9Sstevel@tonic-gate 		encoding = nvh->nvh_encoding;
19597c478bd9Sstevel@tonic-gate 		nvl_endian = nvh->nvh_endian;
19607c478bd9Sstevel@tonic-gate 		break;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE:
19637c478bd9Sstevel@tonic-gate 		nvl_endian = host_endian;
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 		/*
19667c478bd9Sstevel@tonic-gate 		 * add the size for encoding
19677c478bd9Sstevel@tonic-gate 		 */
19687c478bd9Sstevel@tonic-gate 		*buflen = sizeof (nvs_header_t);
19697c478bd9Sstevel@tonic-gate 		break;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	default:
19727c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	/*
19767c478bd9Sstevel@tonic-gate 	 * Create an nvstream with proper encoding method
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	switch (encoding) {
19797c478bd9Sstevel@tonic-gate 	case NV_ENCODE_NATIVE:
19807c478bd9Sstevel@tonic-gate 		/*
19817c478bd9Sstevel@tonic-gate 		 * check endianness, in case we are unpacking
19827c478bd9Sstevel@tonic-gate 		 * from a file
19837c478bd9Sstevel@tonic-gate 		 */
19847c478bd9Sstevel@tonic-gate 		if (nvl_endian != host_endian)
19857c478bd9Sstevel@tonic-gate 			return (ENOTSUP);
19867c478bd9Sstevel@tonic-gate 		err = nvs_native(&nvs, nvl, buf, buflen);
19877c478bd9Sstevel@tonic-gate 		break;
19887c478bd9Sstevel@tonic-gate 	case NV_ENCODE_XDR:
19897c478bd9Sstevel@tonic-gate 		err = nvs_xdr(&nvs, nvl, buf, buflen);
19907c478bd9Sstevel@tonic-gate 		break;
19917c478bd9Sstevel@tonic-gate 	default:
19927c478bd9Sstevel@tonic-gate 		err = ENOTSUP;
19937c478bd9Sstevel@tonic-gate 		break;
19947c478bd9Sstevel@tonic-gate 	}
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	return (err);
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate int
20007c478bd9Sstevel@tonic-gate nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate 	return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
20037c478bd9Sstevel@tonic-gate }
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate /*
20067c478bd9Sstevel@tonic-gate  * Pack nvlist into contiguous memory
20077c478bd9Sstevel@tonic-gate  */
20087c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
20097c478bd9Sstevel@tonic-gate int
20107c478bd9Sstevel@tonic-gate nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
20117c478bd9Sstevel@tonic-gate     int kmflag)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
20147c478bd9Sstevel@tonic-gate 	return (nvlist_xpack(nvl, bufp, buflen, encoding,
20157c478bd9Sstevel@tonic-gate 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
20167c478bd9Sstevel@tonic-gate #else
20177c478bd9Sstevel@tonic-gate 	return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
20187c478bd9Sstevel@tonic-gate #endif
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate int
20227c478bd9Sstevel@tonic-gate nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
20237c478bd9Sstevel@tonic-gate     nv_alloc_t *nva)
20247c478bd9Sstevel@tonic-gate {
20257c478bd9Sstevel@tonic-gate 	nvpriv_t nvpriv;
20267c478bd9Sstevel@tonic-gate 	size_t alloc_size;
20277c478bd9Sstevel@tonic-gate 	char *buf;
20287c478bd9Sstevel@tonic-gate 	int err;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
20317c478bd9Sstevel@tonic-gate 		return (EINVAL);
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	if (*bufp != NULL)
20347c478bd9Sstevel@tonic-gate 		return (nvlist_common(nvl, *bufp, buflen, encoding,
20357c478bd9Sstevel@tonic-gate 		    NVS_OP_ENCODE));
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate 	/*
20387c478bd9Sstevel@tonic-gate 	 * Here is a difficult situation:
20397c478bd9Sstevel@tonic-gate 	 * 1. The nvlist has fixed allocator properties.
20407c478bd9Sstevel@tonic-gate 	 *    All other nvlist routines (like nvlist_add_*, ...) use
20417c478bd9Sstevel@tonic-gate 	 *    these properties.
20427c478bd9Sstevel@tonic-gate 	 * 2. When using nvlist_pack() the user can specify his own
20437c478bd9Sstevel@tonic-gate 	 *    allocator properties (e.g. by using KM_NOSLEEP).
20447c478bd9Sstevel@tonic-gate 	 *
20457c478bd9Sstevel@tonic-gate 	 * We use the user specified properties (2). A clearer solution
20467c478bd9Sstevel@tonic-gate 	 * will be to remove the kmflag from nvlist_pack(), but we will
20477c478bd9Sstevel@tonic-gate 	 * not change the interface.
20487c478bd9Sstevel@tonic-gate 	 */
20497c478bd9Sstevel@tonic-gate 	nv_priv_init(&nvpriv, nva, 0);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	if (err = nvlist_size(nvl, &alloc_size, encoding))
20527c478bd9Sstevel@tonic-gate 		return (err);
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
20557c478bd9Sstevel@tonic-gate 		return (ENOMEM);
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate 	if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
20587c478bd9Sstevel@tonic-gate 	    NVS_OP_ENCODE)) != 0) {
20597c478bd9Sstevel@tonic-gate 		nv_mem_free(&nvpriv, buf, alloc_size);
20607c478bd9Sstevel@tonic-gate 	} else {
20617c478bd9Sstevel@tonic-gate 		*buflen = alloc_size;
20627c478bd9Sstevel@tonic-gate 		*bufp = buf;
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	return (err);
20667c478bd9Sstevel@tonic-gate }
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate /*
20697c478bd9Sstevel@tonic-gate  * Unpack buf into an nvlist_t
20707c478bd9Sstevel@tonic-gate  */
20717c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
20727c478bd9Sstevel@tonic-gate int
20737c478bd9Sstevel@tonic-gate nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
20747c478bd9Sstevel@tonic-gate {
20757c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_BOOT)
20767c478bd9Sstevel@tonic-gate 	return (nvlist_xunpack(buf, buflen, nvlp,
20777c478bd9Sstevel@tonic-gate 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
20787c478bd9Sstevel@tonic-gate #else
20797c478bd9Sstevel@tonic-gate 	return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
20807c478bd9Sstevel@tonic-gate #endif
20817c478bd9Sstevel@tonic-gate }
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate int
20847c478bd9Sstevel@tonic-gate nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
20877c478bd9Sstevel@tonic-gate 	int err;
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	if (nvlp == NULL)
20907c478bd9Sstevel@tonic-gate 		return (EINVAL);
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
20937c478bd9Sstevel@tonic-gate 		return (err);
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate 	if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
20967c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
20977c478bd9Sstevel@tonic-gate 	else
20987c478bd9Sstevel@tonic-gate 		*nvlp = nvl;
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	return (err);
21017c478bd9Sstevel@tonic-gate }
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate /*
21047c478bd9Sstevel@tonic-gate  * Native encoding functions
21057c478bd9Sstevel@tonic-gate  */
21067c478bd9Sstevel@tonic-gate typedef struct {
21077c478bd9Sstevel@tonic-gate 	/*
21087c478bd9Sstevel@tonic-gate 	 * This structure is used when decoding a packed nvpair in
21097c478bd9Sstevel@tonic-gate 	 * the native format.  n_base points to a buffer containing the
21107c478bd9Sstevel@tonic-gate 	 * packed nvpair.  n_end is a pointer to the end of the buffer.
21117c478bd9Sstevel@tonic-gate 	 * (n_end actually points to the first byte past the end of the
21127c478bd9Sstevel@tonic-gate 	 * buffer.)  n_curr is a pointer that lies between n_base and n_end.
21137c478bd9Sstevel@tonic-gate 	 * It points to the current data that we are decoding.
21147c478bd9Sstevel@tonic-gate 	 * The amount of data left in the buffer is equal to n_end - n_curr.
21157c478bd9Sstevel@tonic-gate 	 * n_flag is used to recognize a packed embedded list.
21167c478bd9Sstevel@tonic-gate 	 */
21177c478bd9Sstevel@tonic-gate 	caddr_t n_base;
21187c478bd9Sstevel@tonic-gate 	caddr_t n_end;
21197c478bd9Sstevel@tonic-gate 	caddr_t n_curr;
21207c478bd9Sstevel@tonic-gate 	uint_t  n_flag;
21217c478bd9Sstevel@tonic-gate } nvs_native_t;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate static int
21247c478bd9Sstevel@tonic-gate nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
21257c478bd9Sstevel@tonic-gate     size_t buflen)
21267c478bd9Sstevel@tonic-gate {
21277c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
21287c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
21297c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
21307c478bd9Sstevel@tonic-gate 		nvs->nvs_private = native;
21317c478bd9Sstevel@tonic-gate 		native->n_curr = native->n_base = buf;
21327c478bd9Sstevel@tonic-gate 		native->n_end = buf + buflen;
21337c478bd9Sstevel@tonic-gate 		native->n_flag = 0;
21347c478bd9Sstevel@tonic-gate 		return (0);
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE:
21377c478bd9Sstevel@tonic-gate 		nvs->nvs_private = native;
21387c478bd9Sstevel@tonic-gate 		native->n_curr = native->n_base = native->n_end = NULL;
21397c478bd9Sstevel@tonic-gate 		native->n_flag = 0;
21407c478bd9Sstevel@tonic-gate 		return (0);
21417c478bd9Sstevel@tonic-gate 	default:
21427c478bd9Sstevel@tonic-gate 		return (EINVAL);
21437c478bd9Sstevel@tonic-gate 	}
21447c478bd9Sstevel@tonic-gate }
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21477c478bd9Sstevel@tonic-gate static void
21487c478bd9Sstevel@tonic-gate nvs_native_destroy(nvstream_t *nvs)
21497c478bd9Sstevel@tonic-gate {
21507c478bd9Sstevel@tonic-gate }
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate static int
21537c478bd9Sstevel@tonic-gate native_cp(nvstream_t *nvs, void *buf, size_t size)
21547c478bd9Sstevel@tonic-gate {
21557c478bd9Sstevel@tonic-gate 	nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
21567c478bd9Sstevel@tonic-gate 
21577c478bd9Sstevel@tonic-gate 	if (native->n_curr + size > native->n_end)
21587c478bd9Sstevel@tonic-gate 		return (EFAULT);
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	/*
21617c478bd9Sstevel@tonic-gate 	 * The bcopy() below eliminates alignment requirement
21627c478bd9Sstevel@tonic-gate 	 * on the buffer (stream) and is preferred over direct access.
21637c478bd9Sstevel@tonic-gate 	 */
21647c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
21657c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
21667c478bd9Sstevel@tonic-gate 		bcopy(buf, native->n_curr, size);
21677c478bd9Sstevel@tonic-gate 		break;
21687c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
21697c478bd9Sstevel@tonic-gate 		bcopy(native->n_curr, buf, size);
21707c478bd9Sstevel@tonic-gate 		break;
21717c478bd9Sstevel@tonic-gate 	default:
21727c478bd9Sstevel@tonic-gate 		return (EINVAL);
21737c478bd9Sstevel@tonic-gate 	}
21747c478bd9Sstevel@tonic-gate 
21757c478bd9Sstevel@tonic-gate 	native->n_curr += size;
21767c478bd9Sstevel@tonic-gate 	return (0);
21777c478bd9Sstevel@tonic-gate }
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate /*
21807c478bd9Sstevel@tonic-gate  * operate on nvlist_t header
21817c478bd9Sstevel@tonic-gate  */
21827c478bd9Sstevel@tonic-gate static int
21837c478bd9Sstevel@tonic-gate nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	nvs_native_t *native = nvs->nvs_private;
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
21887c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
21897c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
21907c478bd9Sstevel@tonic-gate 		if (native->n_flag)
21917c478bd9Sstevel@tonic-gate 			return (0);	/* packed embedded list */
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 		native->n_flag = 1;
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 		/* copy version and nvflag of the nvlist_t */
21967c478bd9Sstevel@tonic-gate 		if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
21977c478bd9Sstevel@tonic-gate 		    native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
21987c478bd9Sstevel@tonic-gate 			return (EFAULT);
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 		return (0);
22017c478bd9Sstevel@tonic-gate 
22027c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE:
22037c478bd9Sstevel@tonic-gate 		/*
22047c478bd9Sstevel@tonic-gate 		 * if calculate for packed embedded list
22057c478bd9Sstevel@tonic-gate 		 * 	4 for end of the embedded list
22067c478bd9Sstevel@tonic-gate 		 * else
22077c478bd9Sstevel@tonic-gate 		 * 	2 * sizeof (int32_t) for nvl_version and nvl_nvflag
22087c478bd9Sstevel@tonic-gate 		 * 	and 4 for end of the entire list
22097c478bd9Sstevel@tonic-gate 		 */
22107c478bd9Sstevel@tonic-gate 		if (native->n_flag) {
22117c478bd9Sstevel@tonic-gate 			*size += 4;
22127c478bd9Sstevel@tonic-gate 		} else {
22137c478bd9Sstevel@tonic-gate 			native->n_flag = 1;
22147c478bd9Sstevel@tonic-gate 			*size += 2 * sizeof (int32_t) + 4;
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 		return (0);
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	default:
22207c478bd9Sstevel@tonic-gate 		return (EINVAL);
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate }
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate static int
22257c478bd9Sstevel@tonic-gate nvs_native_nvl_fini(nvstream_t *nvs)
22267c478bd9Sstevel@tonic-gate {
22277c478bd9Sstevel@tonic-gate 	if (nvs->nvs_op == NVS_OP_ENCODE) {
22287c478bd9Sstevel@tonic-gate 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
22297c478bd9Sstevel@tonic-gate 		/*
22307c478bd9Sstevel@tonic-gate 		 * Add 4 zero bytes at end of nvlist. They are used
22317c478bd9Sstevel@tonic-gate 		 * for end detection by the decode routine.
22327c478bd9Sstevel@tonic-gate 		 */
22337c478bd9Sstevel@tonic-gate 		if (native->n_curr + sizeof (int) > native->n_end)
22347c478bd9Sstevel@tonic-gate 			return (EFAULT);
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 		bzero(native->n_curr, sizeof (int));
22377c478bd9Sstevel@tonic-gate 		native->n_curr += sizeof (int);
22387c478bd9Sstevel@tonic-gate 	}
22397c478bd9Sstevel@tonic-gate 
22407c478bd9Sstevel@tonic-gate 	return (0);
22417c478bd9Sstevel@tonic-gate }
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate static int
22447c478bd9Sstevel@tonic-gate nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
22457c478bd9Sstevel@tonic-gate {
22467c478bd9Sstevel@tonic-gate 	if (nvs->nvs_op == NVS_OP_ENCODE) {
22477c478bd9Sstevel@tonic-gate 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
22487c478bd9Sstevel@tonic-gate 		nvlist_t *packed = (void *)
22497c478bd9Sstevel@tonic-gate 		    (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
22507c478bd9Sstevel@tonic-gate 		/*
22517c478bd9Sstevel@tonic-gate 		 * Null out the pointer that is meaningless in the packed
22527c478bd9Sstevel@tonic-gate 		 * structure. The address may not be aligned, so we have
22537c478bd9Sstevel@tonic-gate 		 * to use bzero.
22547c478bd9Sstevel@tonic-gate 		 */
22557c478bd9Sstevel@tonic-gate 		bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
22567c478bd9Sstevel@tonic-gate 	}
22577c478bd9Sstevel@tonic-gate 
22587c478bd9Sstevel@tonic-gate 	return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
22597c478bd9Sstevel@tonic-gate }
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate static int
22627c478bd9Sstevel@tonic-gate nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	if (nvs->nvs_op == NVS_OP_ENCODE) {
22657c478bd9Sstevel@tonic-gate 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
22667c478bd9Sstevel@tonic-gate 		char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
22677c478bd9Sstevel@tonic-gate 		size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
22687c478bd9Sstevel@tonic-gate 		nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
22697c478bd9Sstevel@tonic-gate 		int i;
22707c478bd9Sstevel@tonic-gate 		/*
22717c478bd9Sstevel@tonic-gate 		 * Null out pointers that are meaningless in the packed
22727c478bd9Sstevel@tonic-gate 		 * structure. The addresses may not be aligned, so we have
22737c478bd9Sstevel@tonic-gate 		 * to use bzero.
22747c478bd9Sstevel@tonic-gate 		 */
22757c478bd9Sstevel@tonic-gate 		bzero(value, len);
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate 		for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
22787c478bd9Sstevel@tonic-gate 			/*
22797c478bd9Sstevel@tonic-gate 			 * Null out the pointer that is meaningless in the
22807c478bd9Sstevel@tonic-gate 			 * packed structure. The address may not be aligned,
22817c478bd9Sstevel@tonic-gate 			 * so we have to use bzero.
22827c478bd9Sstevel@tonic-gate 			 */
22837c478bd9Sstevel@tonic-gate 			bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
22847c478bd9Sstevel@tonic-gate 	}
22857c478bd9Sstevel@tonic-gate 
22867c478bd9Sstevel@tonic-gate 	return (nvs_embedded_nvl_array(nvs, nvp, NULL));
22877c478bd9Sstevel@tonic-gate }
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate static void
22907c478bd9Sstevel@tonic-gate nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
22937c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE: {
22947c478bd9Sstevel@tonic-gate 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
22957c478bd9Sstevel@tonic-gate 		uint64_t *strp = (void *)
22967c478bd9Sstevel@tonic-gate 		    (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
22977c478bd9Sstevel@tonic-gate 		/*
22987c478bd9Sstevel@tonic-gate 		 * Null out pointers that are meaningless in the packed
22997c478bd9Sstevel@tonic-gate 		 * structure. The addresses may not be aligned, so we have
23007c478bd9Sstevel@tonic-gate 		 * to use bzero.
23017c478bd9Sstevel@tonic-gate 		 */
23027c478bd9Sstevel@tonic-gate 		bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
23037c478bd9Sstevel@tonic-gate 		break;
23047c478bd9Sstevel@tonic-gate 	}
23057c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
23067c478bd9Sstevel@tonic-gate 		char **strp = (void *)NVP_VALUE(nvp);
23077c478bd9Sstevel@tonic-gate 		char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
23087c478bd9Sstevel@tonic-gate 		int i;
23097c478bd9Sstevel@tonic-gate 
23107c478bd9Sstevel@tonic-gate 		for (i = 0; i < NVP_NELEM(nvp); i++) {
23117c478bd9Sstevel@tonic-gate 			strp[i] = buf;
23127c478bd9Sstevel@tonic-gate 			buf += strlen(buf) + 1;
23137c478bd9Sstevel@tonic-gate 		}
23147c478bd9Sstevel@tonic-gate 		break;
23157c478bd9Sstevel@tonic-gate 	}
23167c478bd9Sstevel@tonic-gate 	}
23177c478bd9Sstevel@tonic-gate }
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate static int
23207c478bd9Sstevel@tonic-gate nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
23217c478bd9Sstevel@tonic-gate {
23227c478bd9Sstevel@tonic-gate 	data_type_t type;
23237c478bd9Sstevel@tonic-gate 	int value_sz;
23247c478bd9Sstevel@tonic-gate 	int ret = 0;
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate 	/*
23277c478bd9Sstevel@tonic-gate 	 * We do the initial bcopy of the data before we look at
23287c478bd9Sstevel@tonic-gate 	 * the nvpair type, because when we're decoding, we won't
23297c478bd9Sstevel@tonic-gate 	 * have the correct values for the pair until we do the bcopy.
23307c478bd9Sstevel@tonic-gate 	 */
23317c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
23327c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
23337c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
23347c478bd9Sstevel@tonic-gate 		if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
23357c478bd9Sstevel@tonic-gate 			return (EFAULT);
23367c478bd9Sstevel@tonic-gate 		break;
23377c478bd9Sstevel@tonic-gate 	default:
23387c478bd9Sstevel@tonic-gate 		return (EINVAL);
23397c478bd9Sstevel@tonic-gate 	}
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 	/* verify nvp_name_sz, check the name string length */
23427c478bd9Sstevel@tonic-gate 	if (i_validate_nvpair_name(nvp) != 0)
23437c478bd9Sstevel@tonic-gate 		return (EFAULT);
23447c478bd9Sstevel@tonic-gate 
23457c478bd9Sstevel@tonic-gate 	type = NVP_TYPE(nvp);
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 	/*
23487c478bd9Sstevel@tonic-gate 	 * Verify type and nelem and get the value size.
23497c478bd9Sstevel@tonic-gate 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
23507c478bd9Sstevel@tonic-gate 	 * is the size of the string(s) excluded.
23517c478bd9Sstevel@tonic-gate 	 */
23527c478bd9Sstevel@tonic-gate 	if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
23537c478bd9Sstevel@tonic-gate 		return (EFAULT);
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
23567c478bd9Sstevel@tonic-gate 		return (EFAULT);
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	switch (type) {
23597c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
23607c478bd9Sstevel@tonic-gate 		ret = nvpair_native_embedded(nvs, nvp);
23617c478bd9Sstevel@tonic-gate 		break;
23627c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY:
23637c478bd9Sstevel@tonic-gate 		ret = nvpair_native_embedded_array(nvs, nvp);
23647c478bd9Sstevel@tonic-gate 		break;
23657c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY:
23667c478bd9Sstevel@tonic-gate 		nvpair_native_string_array(nvs, nvp);
23677c478bd9Sstevel@tonic-gate 		break;
23687c478bd9Sstevel@tonic-gate 	default:
23697c478bd9Sstevel@tonic-gate 		break;
23707c478bd9Sstevel@tonic-gate 	}
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 	return (ret);
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate static int
23767c478bd9Sstevel@tonic-gate nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate 	uint64_t nvp_sz = nvp->nvp_size;
23797c478bd9Sstevel@tonic-gate 
23807c478bd9Sstevel@tonic-gate 	switch (NVP_TYPE(nvp)) {
23817c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST: {
23827c478bd9Sstevel@tonic-gate 		size_t nvsize = 0;
23837c478bd9Sstevel@tonic-gate 
23847c478bd9Sstevel@tonic-gate 		if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
23857c478bd9Sstevel@tonic-gate 			return (EINVAL);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 		nvp_sz += nvsize;
23887c478bd9Sstevel@tonic-gate 		break;
23897c478bd9Sstevel@tonic-gate 	}
23907c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY: {
23917c478bd9Sstevel@tonic-gate 		size_t nvsize;
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 		if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
23947c478bd9Sstevel@tonic-gate 			return (EINVAL);
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate 		nvp_sz += nvsize;
23977c478bd9Sstevel@tonic-gate 		break;
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 	default:
24007c478bd9Sstevel@tonic-gate 		break;
24017c478bd9Sstevel@tonic-gate 	}
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	if (nvp_sz > INT32_MAX)
24047c478bd9Sstevel@tonic-gate 		return (EINVAL);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 	*size = nvp_sz;
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	return (0);
24097c478bd9Sstevel@tonic-gate }
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate static int
24127c478bd9Sstevel@tonic-gate nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
24137c478bd9Sstevel@tonic-gate {
24147c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
24157c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
24167c478bd9Sstevel@tonic-gate 		return (nvs_native_nvp_op(nvs, nvp));
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
24197c478bd9Sstevel@tonic-gate 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
24207c478bd9Sstevel@tonic-gate 		int32_t decode_len;
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 		/* try to read the size value from the stream */
24237c478bd9Sstevel@tonic-gate 		if (native->n_curr + sizeof (int32_t) > native->n_end)
24247c478bd9Sstevel@tonic-gate 			return (EFAULT);
24257c478bd9Sstevel@tonic-gate 		bcopy(native->n_curr, &decode_len, sizeof (int32_t));
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 		/* sanity check the size value */
24287c478bd9Sstevel@tonic-gate 		if (decode_len < 0 ||
24297c478bd9Sstevel@tonic-gate 		    decode_len > native->n_end - native->n_curr)
24307c478bd9Sstevel@tonic-gate 			return (EFAULT);
24317c478bd9Sstevel@tonic-gate 
24327c478bd9Sstevel@tonic-gate 		*size = decode_len;
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate 		/*
24357c478bd9Sstevel@tonic-gate 		 * If at the end of the stream then move the cursor
24367c478bd9Sstevel@tonic-gate 		 * forward, otherwise nvpair_native_op() will read
24377c478bd9Sstevel@tonic-gate 		 * the entire nvpair at the same cursor position.
24387c478bd9Sstevel@tonic-gate 		 */
24397c478bd9Sstevel@tonic-gate 		if (*size == 0)
24407c478bd9Sstevel@tonic-gate 			native->n_curr += sizeof (int32_t);
24417c478bd9Sstevel@tonic-gate 		break;
24427c478bd9Sstevel@tonic-gate 	}
24437c478bd9Sstevel@tonic-gate 
24447c478bd9Sstevel@tonic-gate 	default:
24457c478bd9Sstevel@tonic-gate 		return (EINVAL);
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 
24487c478bd9Sstevel@tonic-gate 	return (0);
24497c478bd9Sstevel@tonic-gate }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate static const nvs_ops_t nvs_native_ops = {
24527c478bd9Sstevel@tonic-gate 	nvs_native_nvlist,
24537c478bd9Sstevel@tonic-gate 	nvs_native_nvpair,
24547c478bd9Sstevel@tonic-gate 	nvs_native_nvp_op,
24557c478bd9Sstevel@tonic-gate 	nvs_native_nvp_size,
24567c478bd9Sstevel@tonic-gate 	nvs_native_nvl_fini
24577c478bd9Sstevel@tonic-gate };
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate static int
24607c478bd9Sstevel@tonic-gate nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
24617c478bd9Sstevel@tonic-gate {
24627c478bd9Sstevel@tonic-gate 	nvs_native_t native;
24637c478bd9Sstevel@tonic-gate 	int err;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	nvs->nvs_ops = &nvs_native_ops;
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
24687c478bd9Sstevel@tonic-gate 	    *buflen - sizeof (nvs_header_t))) != 0)
24697c478bd9Sstevel@tonic-gate 		return (err);
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	err = nvs_operation(nvs, nvl, buflen);
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	nvs_native_destroy(nvs);
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	return (err);
24767c478bd9Sstevel@tonic-gate }
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate /*
24797c478bd9Sstevel@tonic-gate  * XDR encoding functions
24807c478bd9Sstevel@tonic-gate  *
24817c478bd9Sstevel@tonic-gate  * An xdr packed nvlist is encoded as:
24827c478bd9Sstevel@tonic-gate  *
24837c478bd9Sstevel@tonic-gate  *  - encoding methode and host endian (4 bytes)
24847c478bd9Sstevel@tonic-gate  *  - nvl_version (4 bytes)
24857c478bd9Sstevel@tonic-gate  *  - nvl_nvflag (4 bytes)
24867c478bd9Sstevel@tonic-gate  *
24877c478bd9Sstevel@tonic-gate  *  - encoded nvpairs, the format of one xdr encoded nvpair is:
24887c478bd9Sstevel@tonic-gate  *	- encoded size of the nvpair (4 bytes)
24897c478bd9Sstevel@tonic-gate  *	- decoded size of the nvpair (4 bytes)
24907c478bd9Sstevel@tonic-gate  *	- name string, (4 + sizeof(NV_ALIGN4(string))
24917c478bd9Sstevel@tonic-gate  *	  a string is coded as size (4 bytes) and data
24927c478bd9Sstevel@tonic-gate  *	- data type (4 bytes)
24937c478bd9Sstevel@tonic-gate  *	- number of elements in the nvpair (4 bytes)
24947c478bd9Sstevel@tonic-gate  *	- data
24957c478bd9Sstevel@tonic-gate  *
24967c478bd9Sstevel@tonic-gate  *  - 2 zero's for end of the entire list (8 bytes)
24977c478bd9Sstevel@tonic-gate  */
24987c478bd9Sstevel@tonic-gate static int
24997c478bd9Sstevel@tonic-gate nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
25007c478bd9Sstevel@tonic-gate {
25017c478bd9Sstevel@tonic-gate 	/* xdr data must be 4 byte aligned */
25027c478bd9Sstevel@tonic-gate 	if ((ulong_t)buf % 4 != 0)
25037c478bd9Sstevel@tonic-gate 		return (EFAULT);
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
25067c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
25077c478bd9Sstevel@tonic-gate 		xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
25087c478bd9Sstevel@tonic-gate 		nvs->nvs_private = xdr;
25097c478bd9Sstevel@tonic-gate 		return (0);
25107c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
25117c478bd9Sstevel@tonic-gate 		xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
25127c478bd9Sstevel@tonic-gate 		nvs->nvs_private = xdr;
25137c478bd9Sstevel@tonic-gate 		return (0);
25147c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE:
25157c478bd9Sstevel@tonic-gate 		nvs->nvs_private = NULL;
25167c478bd9Sstevel@tonic-gate 		return (0);
25177c478bd9Sstevel@tonic-gate 	default:
25187c478bd9Sstevel@tonic-gate 		return (EINVAL);
25197c478bd9Sstevel@tonic-gate 	}
25207c478bd9Sstevel@tonic-gate }
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate static void
25237c478bd9Sstevel@tonic-gate nvs_xdr_destroy(nvstream_t *nvs)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
25267c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
25277c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE:
25287c478bd9Sstevel@tonic-gate 		xdr_destroy((XDR *)nvs->nvs_private);
25297c478bd9Sstevel@tonic-gate 		break;
25307c478bd9Sstevel@tonic-gate 	default:
25317c478bd9Sstevel@tonic-gate 		break;
25327c478bd9Sstevel@tonic-gate 	}
25337c478bd9Sstevel@tonic-gate }
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate static int
25367c478bd9Sstevel@tonic-gate nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
25377c478bd9Sstevel@tonic-gate {
25387c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
25397c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE:
25407c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
25417c478bd9Sstevel@tonic-gate 		XDR 	*xdr = nvs->nvs_private;
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 		if (!xdr_int(xdr, &nvl->nvl_version) ||
25447c478bd9Sstevel@tonic-gate 		    !xdr_u_int(xdr, &nvl->nvl_nvflag))
25457c478bd9Sstevel@tonic-gate 			return (EFAULT);
25467c478bd9Sstevel@tonic-gate 		break;
25477c478bd9Sstevel@tonic-gate 	}
25487c478bd9Sstevel@tonic-gate 	case NVS_OP_GETSIZE: {
25497c478bd9Sstevel@tonic-gate 		/*
25507c478bd9Sstevel@tonic-gate 		 * 2 * 4 for nvl_version + nvl_nvflag
25517c478bd9Sstevel@tonic-gate 		 * and 8 for end of the entire list
25527c478bd9Sstevel@tonic-gate 		 */
25537c478bd9Sstevel@tonic-gate 		*size += 2 * 4 + 8;
25547c478bd9Sstevel@tonic-gate 		break;
25557c478bd9Sstevel@tonic-gate 	}
25567c478bd9Sstevel@tonic-gate 	default:
25577c478bd9Sstevel@tonic-gate 		return (EINVAL);
25587c478bd9Sstevel@tonic-gate 	}
25597c478bd9Sstevel@tonic-gate 	return (0);
25607c478bd9Sstevel@tonic-gate }
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate static int
25637c478bd9Sstevel@tonic-gate nvs_xdr_nvl_fini(nvstream_t *nvs)
25647c478bd9Sstevel@tonic-gate {
25657c478bd9Sstevel@tonic-gate 	if (nvs->nvs_op == NVS_OP_ENCODE) {
25667c478bd9Sstevel@tonic-gate 		XDR *xdr = nvs->nvs_private;
25677c478bd9Sstevel@tonic-gate 		int zero = 0;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 		if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
25707c478bd9Sstevel@tonic-gate 			return (EFAULT);
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	return (0);
25747c478bd9Sstevel@tonic-gate }
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate /*
25777c478bd9Sstevel@tonic-gate  * The format of xdr encoded nvpair is:
25787c478bd9Sstevel@tonic-gate  * encode_size, decode_size, name string, data type, nelem, data
25797c478bd9Sstevel@tonic-gate  */
25807c478bd9Sstevel@tonic-gate static int
25817c478bd9Sstevel@tonic-gate nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
25827c478bd9Sstevel@tonic-gate {
25837c478bd9Sstevel@tonic-gate 	data_type_t type;
25847c478bd9Sstevel@tonic-gate 	char	*buf;
25857c478bd9Sstevel@tonic-gate 	char	*buf_end = (char *)nvp + nvp->nvp_size;
25867c478bd9Sstevel@tonic-gate 	int	value_sz;
25877c478bd9Sstevel@tonic-gate 	uint_t	nelem, buflen;
25887c478bd9Sstevel@tonic-gate 	bool_t	ret = FALSE;
25897c478bd9Sstevel@tonic-gate 	XDR	*xdr = nvs->nvs_private;
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 	ASSERT(xdr != NULL && nvp != NULL);
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	/* name string */
25947c478bd9Sstevel@tonic-gate 	if ((buf = NVP_NAME(nvp)) >= buf_end)
25957c478bd9Sstevel@tonic-gate 		return (EFAULT);
25967c478bd9Sstevel@tonic-gate 	buflen = buf_end - buf;
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	if (!xdr_string(xdr, &buf, buflen - 1))
25997c478bd9Sstevel@tonic-gate 		return (EFAULT);
26007c478bd9Sstevel@tonic-gate 	nvp->nvp_name_sz = strlen(buf) + 1;
26017c478bd9Sstevel@tonic-gate 
26027c478bd9Sstevel@tonic-gate 	/* type and nelem */
26037c478bd9Sstevel@tonic-gate 	if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
26047c478bd9Sstevel@tonic-gate 	    !xdr_int(xdr, &nvp->nvp_value_elem))
26057c478bd9Sstevel@tonic-gate 		return (EFAULT);
26067c478bd9Sstevel@tonic-gate 
26077c478bd9Sstevel@tonic-gate 	type = NVP_TYPE(nvp);
26087c478bd9Sstevel@tonic-gate 	nelem = nvp->nvp_value_elem;
26097c478bd9Sstevel@tonic-gate 
26107c478bd9Sstevel@tonic-gate 	/*
26117c478bd9Sstevel@tonic-gate 	 * Verify type and nelem and get the value size.
26127c478bd9Sstevel@tonic-gate 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
26137c478bd9Sstevel@tonic-gate 	 * is the size of the string(s) excluded.
26147c478bd9Sstevel@tonic-gate 	 */
26157c478bd9Sstevel@tonic-gate 	if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
26167c478bd9Sstevel@tonic-gate 		return (EFAULT);
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	/* if there is no data to extract then return */
26197c478bd9Sstevel@tonic-gate 	if (nelem == 0)
26207c478bd9Sstevel@tonic-gate 		return (0);
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	/* value */
26237c478bd9Sstevel@tonic-gate 	if ((buf = NVP_VALUE(nvp)) >= buf_end)
26247c478bd9Sstevel@tonic-gate 		return (EFAULT);
26257c478bd9Sstevel@tonic-gate 	buflen = buf_end - buf;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	if (buflen < value_sz)
26287c478bd9Sstevel@tonic-gate 		return (EFAULT);
26297c478bd9Sstevel@tonic-gate 
26307c478bd9Sstevel@tonic-gate 	switch (type) {
26317c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
26327c478bd9Sstevel@tonic-gate 		if (nvs_embedded(nvs, (void *)buf) == 0)
26337c478bd9Sstevel@tonic-gate 			return (0);
26347c478bd9Sstevel@tonic-gate 		break;
26357c478bd9Sstevel@tonic-gate 
26367c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY:
26377c478bd9Sstevel@tonic-gate 		if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
26387c478bd9Sstevel@tonic-gate 			return (0);
26397c478bd9Sstevel@tonic-gate 		break;
26407c478bd9Sstevel@tonic-gate 
26417c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
26427c478bd9Sstevel@tonic-gate 		ret = TRUE;
26437c478bd9Sstevel@tonic-gate 		break;
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
26467c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8:
26477c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8:
26487c478bd9Sstevel@tonic-gate 		ret = xdr_char(xdr, buf);
26497c478bd9Sstevel@tonic-gate 		break;
26507c478bd9Sstevel@tonic-gate 
26517c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
26527c478bd9Sstevel@tonic-gate 		ret = xdr_short(xdr, (void *)buf);
26537c478bd9Sstevel@tonic-gate 		break;
26547c478bd9Sstevel@tonic-gate 
26557c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
26567c478bd9Sstevel@tonic-gate 		ret = xdr_u_short(xdr, (void *)buf);
26577c478bd9Sstevel@tonic-gate 		break;
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
26607c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
26617c478bd9Sstevel@tonic-gate 		ret = xdr_int(xdr, (void *)buf);
26627c478bd9Sstevel@tonic-gate 		break;
26637c478bd9Sstevel@tonic-gate 
26647c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
26657c478bd9Sstevel@tonic-gate 		ret = xdr_u_int(xdr, (void *)buf);
26667c478bd9Sstevel@tonic-gate 		break;
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
26697c478bd9Sstevel@tonic-gate 		ret = xdr_longlong_t(xdr, (void *)buf);
26707c478bd9Sstevel@tonic-gate 		break;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
26737c478bd9Sstevel@tonic-gate 		ret = xdr_u_longlong_t(xdr, (void *)buf);
26747c478bd9Sstevel@tonic-gate 		break;
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
26777c478bd9Sstevel@tonic-gate 		/*
26787c478bd9Sstevel@tonic-gate 		 * NOTE: must expose the definition of hrtime_t here
26797c478bd9Sstevel@tonic-gate 		 */
26807c478bd9Sstevel@tonic-gate 		ret = xdr_longlong_t(xdr, (void *)buf);
26817c478bd9Sstevel@tonic-gate 		break;
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
26847c478bd9Sstevel@tonic-gate 		ret = xdr_string(xdr, &buf, buflen - 1);
26857c478bd9Sstevel@tonic-gate 		break;
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
26887c478bd9Sstevel@tonic-gate 		ret = xdr_opaque(xdr, buf, nelem);
26897c478bd9Sstevel@tonic-gate 		break;
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8_ARRAY:
26927c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8_ARRAY:
26937c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
26947c478bd9Sstevel@tonic-gate 		    (xdrproc_t)xdr_char);
26957c478bd9Sstevel@tonic-gate 		break;
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16_ARRAY:
26987c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
26997c478bd9Sstevel@tonic-gate 		    sizeof (int16_t), (xdrproc_t)xdr_short);
27007c478bd9Sstevel@tonic-gate 		break;
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16_ARRAY:
27037c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
27047c478bd9Sstevel@tonic-gate 		    sizeof (uint16_t), (xdrproc_t)xdr_u_short);
27057c478bd9Sstevel@tonic-gate 		break;
27067c478bd9Sstevel@tonic-gate 
27077c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY:
27087c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32_ARRAY:
27097c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
27107c478bd9Sstevel@tonic-gate 		    sizeof (int32_t), (xdrproc_t)xdr_int);
27117c478bd9Sstevel@tonic-gate 		break;
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32_ARRAY:
27147c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
27157c478bd9Sstevel@tonic-gate 		    sizeof (uint32_t), (xdrproc_t)xdr_u_int);
27167c478bd9Sstevel@tonic-gate 		break;
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64_ARRAY:
27197c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
27207c478bd9Sstevel@tonic-gate 		    sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
27217c478bd9Sstevel@tonic-gate 		break;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64_ARRAY:
27247c478bd9Sstevel@tonic-gate 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
27257c478bd9Sstevel@tonic-gate 		    sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
27267c478bd9Sstevel@tonic-gate 		break;
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY: {
27297c478bd9Sstevel@tonic-gate 		size_t len = nelem * sizeof (uint64_t);
27307c478bd9Sstevel@tonic-gate 		char **strp = (void *)buf;
27317c478bd9Sstevel@tonic-gate 		int i;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 		if (nvs->nvs_op == NVS_OP_DECODE)
27347c478bd9Sstevel@tonic-gate 			bzero(buf, len);	/* don't trust packed data */
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 		for (i = 0; i < nelem; i++) {
27377c478bd9Sstevel@tonic-gate 			if (buflen <= len)
27387c478bd9Sstevel@tonic-gate 				return (EFAULT);
27397c478bd9Sstevel@tonic-gate 
27407c478bd9Sstevel@tonic-gate 			buf += len;
27417c478bd9Sstevel@tonic-gate 			buflen -= len;
27427c478bd9Sstevel@tonic-gate 
27437c478bd9Sstevel@tonic-gate 			if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
27447c478bd9Sstevel@tonic-gate 				return (EFAULT);
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate 			if (nvs->nvs_op == NVS_OP_DECODE)
27477c478bd9Sstevel@tonic-gate 				strp[i] = buf;
27487c478bd9Sstevel@tonic-gate 			len = strlen(buf) + 1;
27497c478bd9Sstevel@tonic-gate 		}
27507c478bd9Sstevel@tonic-gate 		ret = TRUE;
27517c478bd9Sstevel@tonic-gate 		break;
27527c478bd9Sstevel@tonic-gate 	}
27537c478bd9Sstevel@tonic-gate 	default:
27547c478bd9Sstevel@tonic-gate 		break;
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	return (ret == TRUE ? 0 : EFAULT);
27587c478bd9Sstevel@tonic-gate }
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate static int
27617c478bd9Sstevel@tonic-gate nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
27627c478bd9Sstevel@tonic-gate {
27637c478bd9Sstevel@tonic-gate 	data_type_t type = NVP_TYPE(nvp);
27647c478bd9Sstevel@tonic-gate 	/*
27657c478bd9Sstevel@tonic-gate 	 * encode_size + decode_size + name string size + data type + nelem
27667c478bd9Sstevel@tonic-gate 	 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
27677c478bd9Sstevel@tonic-gate 	 */
27687c478bd9Sstevel@tonic-gate 	uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 	switch (type) {
27717c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN:
27727c478bd9Sstevel@tonic-gate 		break;
27737c478bd9Sstevel@tonic-gate 
27747c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_VALUE:
27757c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE:
27767c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8:
27777c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8:
27787c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16:
27797c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16:
27807c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32:
27817c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32:
27827c478bd9Sstevel@tonic-gate 		nvp_sz += 4;	/* 4 is the minimum xdr unit */
27837c478bd9Sstevel@tonic-gate 		break;
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64:
27867c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64:
27877c478bd9Sstevel@tonic-gate 	case DATA_TYPE_HRTIME:
27887c478bd9Sstevel@tonic-gate 		nvp_sz += 8;
27897c478bd9Sstevel@tonic-gate 		break;
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING:
27927c478bd9Sstevel@tonic-gate 		nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
27937c478bd9Sstevel@tonic-gate 		break;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BYTE_ARRAY:
27967c478bd9Sstevel@tonic-gate 		nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
27977c478bd9Sstevel@tonic-gate 		break;
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	case DATA_TYPE_BOOLEAN_ARRAY:
28007c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT8_ARRAY:
28017c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT8_ARRAY:
28027c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT16_ARRAY:
28037c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT16_ARRAY:
28047c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT32_ARRAY:
28057c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT32_ARRAY:
28067c478bd9Sstevel@tonic-gate 		nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
28077c478bd9Sstevel@tonic-gate 		break;
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 	case DATA_TYPE_INT64_ARRAY:
28107c478bd9Sstevel@tonic-gate 	case DATA_TYPE_UINT64_ARRAY:
28117c478bd9Sstevel@tonic-gate 		nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
28127c478bd9Sstevel@tonic-gate 		break;
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	case DATA_TYPE_STRING_ARRAY: {
28157c478bd9Sstevel@tonic-gate 		int i;
28167c478bd9Sstevel@tonic-gate 		char **strs = (void *)NVP_VALUE(nvp);
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 		for (i = 0; i < NVP_NELEM(nvp); i++)
28197c478bd9Sstevel@tonic-gate 			nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 		break;
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST:
28257c478bd9Sstevel@tonic-gate 	case DATA_TYPE_NVLIST_ARRAY: {
28267c478bd9Sstevel@tonic-gate 		size_t nvsize = 0;
28277c478bd9Sstevel@tonic-gate 		int old_nvs_op = nvs->nvs_op;
28287c478bd9Sstevel@tonic-gate 		int err;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 		nvs->nvs_op = NVS_OP_GETSIZE;
28317c478bd9Sstevel@tonic-gate 		if (type == DATA_TYPE_NVLIST)
28327c478bd9Sstevel@tonic-gate 			err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
28337c478bd9Sstevel@tonic-gate 		else
28347c478bd9Sstevel@tonic-gate 			err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
28357c478bd9Sstevel@tonic-gate 		nvs->nvs_op = old_nvs_op;
28367c478bd9Sstevel@tonic-gate 
28377c478bd9Sstevel@tonic-gate 		if (err != 0)
28387c478bd9Sstevel@tonic-gate 			return (EINVAL);
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		nvp_sz += nvsize;
28417c478bd9Sstevel@tonic-gate 		break;
28427c478bd9Sstevel@tonic-gate 	}
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	default:
28457c478bd9Sstevel@tonic-gate 		return (EINVAL);
28467c478bd9Sstevel@tonic-gate 	}
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 	if (nvp_sz > INT32_MAX)
28497c478bd9Sstevel@tonic-gate 		return (EINVAL);
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	*size = nvp_sz;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 	return (0);
28547c478bd9Sstevel@tonic-gate }
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate /*
28587c478bd9Sstevel@tonic-gate  * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
28597c478bd9Sstevel@tonic-gate  * the largest nvpair that could be encoded in the buffer.
28607c478bd9Sstevel@tonic-gate  *
28617c478bd9Sstevel@tonic-gate  * See comments above nvpair_xdr_op() for the format of xdr encoding.
28627c478bd9Sstevel@tonic-gate  * The size of a xdr packed nvpair without any data is 5 words.
28637c478bd9Sstevel@tonic-gate  *
28647c478bd9Sstevel@tonic-gate  * Using the size of the data directly as an estimate would be ok
28657c478bd9Sstevel@tonic-gate  * in all cases except one.  If the data type is of DATA_TYPE_STRING_ARRAY
28667c478bd9Sstevel@tonic-gate  * then the actual nvpair has space for an array of pointers to index
28677c478bd9Sstevel@tonic-gate  * the strings.  These pointers are not encoded into the packed xdr buffer.
28687c478bd9Sstevel@tonic-gate  *
28697c478bd9Sstevel@tonic-gate  * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
28707c478bd9Sstevel@tonic-gate  * of length 0, then each string is endcoded in xdr format as a single word.
28717c478bd9Sstevel@tonic-gate  * Therefore when expanded to an nvpair there will be 2.25 word used for
28727c478bd9Sstevel@tonic-gate  * each string.  (a int64_t allocated for pointer usage, and a single char
28737c478bd9Sstevel@tonic-gate  * for the null termination.)
28747c478bd9Sstevel@tonic-gate  *
28757c478bd9Sstevel@tonic-gate  * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
28767c478bd9Sstevel@tonic-gate  */
28777c478bd9Sstevel@tonic-gate #define	NVS_XDR_HDR_LEN		((size_t)(5 * 4))
28787c478bd9Sstevel@tonic-gate #define	NVS_XDR_DATA_LEN(y)	(((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
28797c478bd9Sstevel@tonic-gate 					0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
28807c478bd9Sstevel@tonic-gate #define	NVS_XDR_MAX_LEN(x)	(NVP_SIZE_CALC(1, 0) + \
28817c478bd9Sstevel@tonic-gate 					(NVS_XDR_DATA_LEN(x) * 2) + \
28827c478bd9Sstevel@tonic-gate 					NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate static int
28857c478bd9Sstevel@tonic-gate nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	XDR 	*xdr = nvs->nvs_private;
28887c478bd9Sstevel@tonic-gate 	int32_t	encode_len, decode_len;
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	switch (nvs->nvs_op) {
28917c478bd9Sstevel@tonic-gate 	case NVS_OP_ENCODE: {
28927c478bd9Sstevel@tonic-gate 		size_t nvsize;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 		if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
28957c478bd9Sstevel@tonic-gate 			return (EFAULT);
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 		decode_len = nvp->nvp_size;
28987c478bd9Sstevel@tonic-gate 		encode_len = nvsize;
28997c478bd9Sstevel@tonic-gate 		if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
29007c478bd9Sstevel@tonic-gate 			return (EFAULT);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 		return (nvs_xdr_nvp_op(nvs, nvp));
29037c478bd9Sstevel@tonic-gate 	}
29047c478bd9Sstevel@tonic-gate 	case NVS_OP_DECODE: {
29057c478bd9Sstevel@tonic-gate 		struct xdr_bytesrec bytesrec;
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 		/* get the encode and decode size */
29087c478bd9Sstevel@tonic-gate 		if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
29097c478bd9Sstevel@tonic-gate 			return (EFAULT);
29107c478bd9Sstevel@tonic-gate 		*size = decode_len;
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 		/* are we at the end of the stream? */
29137c478bd9Sstevel@tonic-gate 		if (*size == 0)
29147c478bd9Sstevel@tonic-gate 			return (0);
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 		/* sanity check the size parameter */
29177c478bd9Sstevel@tonic-gate 		if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
29187c478bd9Sstevel@tonic-gate 			return (EFAULT);
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 		if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
29217c478bd9Sstevel@tonic-gate 			return (EFAULT);
29227c478bd9Sstevel@tonic-gate 		break;
29237c478bd9Sstevel@tonic-gate 	}
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	default:
29267c478bd9Sstevel@tonic-gate 		return (EINVAL);
29277c478bd9Sstevel@tonic-gate 	}
29287c478bd9Sstevel@tonic-gate 	return (0);
29297c478bd9Sstevel@tonic-gate }
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate static const struct nvs_ops nvs_xdr_ops = {
29327c478bd9Sstevel@tonic-gate 	nvs_xdr_nvlist,
29337c478bd9Sstevel@tonic-gate 	nvs_xdr_nvpair,
29347c478bd9Sstevel@tonic-gate 	nvs_xdr_nvp_op,
29357c478bd9Sstevel@tonic-gate 	nvs_xdr_nvp_size,
29367c478bd9Sstevel@tonic-gate 	nvs_xdr_nvl_fini
29377c478bd9Sstevel@tonic-gate };
29387c478bd9Sstevel@tonic-gate 
29397c478bd9Sstevel@tonic-gate static int
29407c478bd9Sstevel@tonic-gate nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
29417c478bd9Sstevel@tonic-gate {
29427c478bd9Sstevel@tonic-gate 	XDR xdr;
29437c478bd9Sstevel@tonic-gate 	int err;
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 	nvs->nvs_ops = &nvs_xdr_ops;
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 	if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
29487c478bd9Sstevel@tonic-gate 	    *buflen - sizeof (nvs_header_t))) != 0)
29497c478bd9Sstevel@tonic-gate 		return (err);
29507c478bd9Sstevel@tonic-gate 
29517c478bd9Sstevel@tonic-gate 	err = nvs_operation(nvs, nvl, buflen);
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate 	nvs_xdr_destroy(nvs);
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	return (err);
29567c478bd9Sstevel@tonic-gate }
2957