xref: /illumos-gate/usr/src/common/nvpair/nvpair.c (revision c3d26abc9ee97b4f60233556aadeb57e0bd30bb9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #include <sys/stropts.h>
27 #include <sys/debug.h>
28 #include <sys/isa_defs.h>
29 #include <sys/int_limits.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <rpc/types.h>
33 #include <rpc/xdr.h>
34 
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/varargs.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #else
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44 #endif
45 
46 #ifndef	offsetof
47 #define	offsetof(s, m)		((size_t)(&(((s *)0)->m)))
48 #endif
49 #define	skip_whitespace(p)	while ((*(p) == ' ') || (*(p) == '\t')) p++
50 
51 /*
52  * nvpair.c - Provides kernel & userland interfaces for manipulating
53  *	name-value pairs.
54  *
55  * Overview Diagram
56  *
57  *  +--------------+
58  *  |  nvlist_t    |
59  *  |--------------|
60  *  | nvl_version  |
61  *  | nvl_nvflag   |
62  *  | nvl_priv    -+-+
63  *  | nvl_flag     | |
64  *  | nvl_pad      | |
65  *  +--------------+ |
66  *                   V
67  *      +--------------+      last i_nvp in list
68  *      | nvpriv_t     |  +--------------------->
69  *      |--------------|  |
70  *   +--+- nvp_list    |  |   +------------+
71  *   |  |  nvp_last   -+--+   + nv_alloc_t |
72  *   |  |  nvp_curr    |      |------------|
73  *   |  |  nvp_nva    -+----> | nva_ops    |
74  *   |  |  nvp_stat    |      | nva_arg    |
75  *   |  +--------------+      +------------+
76  *   |
77  *   +-------+
78  *           V
79  *   +---------------------+      +-------------------+
80  *   |  i_nvp_t            |  +-->|  i_nvp_t          |  +-->
81  *   |---------------------|  |   |-------------------|  |
82  *   | nvi_next           -+--+   | nvi_next         -+--+
83  *   | nvi_prev (NULL)     | <----+ nvi_prev          |
84  *   | . . . . . . . . . . |      | . . . . . . . . . |
85  *   | nvp (nvpair_t)      |      | nvp (nvpair_t)    |
86  *   |  - nvp_size         |      |  - nvp_size       |
87  *   |  - nvp_name_sz      |      |  - nvp_name_sz    |
88  *   |  - nvp_value_elem   |      |  - nvp_value_elem |
89  *   |  - nvp_type         |      |  - nvp_type       |
90  *   |  - data ...         |      |  - data ...       |
91  *   +---------------------+      +-------------------+
92  *
93  *
94  *
95  *   +---------------------+              +---------------------+
96  *   |  i_nvp_t            |  +-->    +-->|  i_nvp_t (last)     |
97  *   |---------------------|  |       |   |---------------------|
98  *   |  nvi_next          -+--+ ... --+   | nvi_next (NULL)     |
99  * <-+- nvi_prev           |<-- ...  <----+ nvi_prev            |
100  *   | . . . . . . . . .   |              | . . . . . . . . .   |
101  *   | nvp (nvpair_t)      |              | nvp (nvpair_t)      |
102  *   |  - nvp_size         |              |  - nvp_size         |
103  *   |  - nvp_name_sz      |              |  - nvp_name_sz      |
104  *   |  - nvp_value_elem   |              |  - nvp_value_elem   |
105  *   |  - DATA_TYPE_NVLIST |              |  - nvp_type         |
106  *   |  - data (embedded)  |              |  - data ...         |
107  *   |    nvlist name      |              +---------------------+
108  *   |  +--------------+   |
109  *   |  |  nvlist_t    |   |
110  *   |  |--------------|   |
111  *   |  | nvl_version  |   |
112  *   |  | nvl_nvflag   |   |
113  *   |  | nvl_priv   --+---+---->
114  *   |  | nvl_flag     |   |
115  *   |  | nvl_pad      |   |
116  *   |  +--------------+   |
117  *   +---------------------+
118  *
119  *
120  * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
121  * allow value to be aligned on 8 byte boundary
122  *
123  * name_len is the length of the name string including the null terminator
124  * so it must be >= 1
125  */
126 #define	NVP_SIZE_CALC(name_len, data_len) \
127 	(NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
128 
129 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
130 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
131     uint_t nelem, const void *data);
132 
133 #define	NV_STAT_EMBEDDED	0x1
134 #define	EMBEDDED_NVL(nvp)	((nvlist_t *)(void *)NVP_VALUE(nvp))
135 #define	EMBEDDED_NVL_ARRAY(nvp)	((nvlist_t **)(void *)NVP_VALUE(nvp))
136 
137 #define	NVP_VALOFF(nvp)	(NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
138 #define	NVPAIR2I_NVP(nvp) \
139 	((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
140 
141 
142 int
143 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
144 {
145 	va_list valist;
146 	int err = 0;
147 
148 	nva->nva_ops = nvo;
149 	nva->nva_arg = NULL;
150 
151 	va_start(valist, nvo);
152 	if (nva->nva_ops->nv_ao_init != NULL)
153 		err = nva->nva_ops->nv_ao_init(nva, valist);
154 	va_end(valist);
155 
156 	return (err);
157 }
158 
159 void
160 nv_alloc_reset(nv_alloc_t *nva)
161 {
162 	if (nva->nva_ops->nv_ao_reset != NULL)
163 		nva->nva_ops->nv_ao_reset(nva);
164 }
165 
166 void
167 nv_alloc_fini(nv_alloc_t *nva)
168 {
169 	if (nva->nva_ops->nv_ao_fini != NULL)
170 		nva->nva_ops->nv_ao_fini(nva);
171 }
172 
173 nv_alloc_t *
174 nvlist_lookup_nv_alloc(nvlist_t *nvl)
175 {
176 	nvpriv_t *priv;
177 
178 	if (nvl == NULL ||
179 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
180 		return (NULL);
181 
182 	return (priv->nvp_nva);
183 }
184 
185 static void *
186 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
187 {
188 	nv_alloc_t *nva = nvp->nvp_nva;
189 	void *buf;
190 
191 	if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
192 		bzero(buf, size);
193 
194 	return (buf);
195 }
196 
197 static void
198 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
199 {
200 	nv_alloc_t *nva = nvp->nvp_nva;
201 
202 	nva->nva_ops->nv_ao_free(nva, buf, size);
203 }
204 
205 static void
206 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
207 {
208 	bzero(priv, sizeof (nvpriv_t));
209 
210 	priv->nvp_nva = nva;
211 	priv->nvp_stat = stat;
212 }
213 
214 static nvpriv_t *
215 nv_priv_alloc(nv_alloc_t *nva)
216 {
217 	nvpriv_t *priv;
218 
219 	/*
220 	 * nv_mem_alloc() cannot called here because it needs the priv
221 	 * argument.
222 	 */
223 	if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
224 		return (NULL);
225 
226 	nv_priv_init(priv, nva, 0);
227 
228 	return (priv);
229 }
230 
231 /*
232  * Embedded lists need their own nvpriv_t's.  We create a new
233  * nvpriv_t using the parameters and allocator from the parent
234  * list's nvpriv_t.
235  */
236 static nvpriv_t *
237 nv_priv_alloc_embedded(nvpriv_t *priv)
238 {
239 	nvpriv_t *emb_priv;
240 
241 	if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
242 		return (NULL);
243 
244 	nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
245 
246 	return (emb_priv);
247 }
248 
249 static void
250 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
251 {
252 	nvl->nvl_version = NV_VERSION;
253 	nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
254 	nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
255 	nvl->nvl_flag = 0;
256 	nvl->nvl_pad = 0;
257 }
258 
259 uint_t
260 nvlist_nvflag(nvlist_t *nvl)
261 {
262 	return (nvl->nvl_nvflag);
263 }
264 
265 /*
266  * nvlist_alloc - Allocate nvlist.
267  */
268 /*ARGSUSED1*/
269 int
270 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
271 {
272 #if defined(_KERNEL) && !defined(_BOOT)
273 	return (nvlist_xalloc(nvlp, nvflag,
274 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
275 #else
276 	return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
277 #endif
278 }
279 
280 int
281 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
282 {
283 	nvpriv_t *priv;
284 
285 	if (nvlp == NULL || nva == NULL)
286 		return (EINVAL);
287 
288 	if ((priv = nv_priv_alloc(nva)) == NULL)
289 		return (ENOMEM);
290 
291 	if ((*nvlp = nv_mem_zalloc(priv,
292 	    NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
293 		nv_mem_free(priv, priv, sizeof (nvpriv_t));
294 		return (ENOMEM);
295 	}
296 
297 	nvlist_init(*nvlp, nvflag, priv);
298 
299 	return (0);
300 }
301 
302 /*
303  * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
304  */
305 static nvpair_t *
306 nvp_buf_alloc(nvlist_t *nvl, size_t len)
307 {
308 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
309 	i_nvp_t *buf;
310 	nvpair_t *nvp;
311 	size_t nvsize;
312 
313 	/*
314 	 * Allocate the buffer
315 	 */
316 	nvsize = len + offsetof(i_nvp_t, nvi_nvp);
317 
318 	if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
319 		return (NULL);
320 
321 	nvp = &buf->nvi_nvp;
322 	nvp->nvp_size = len;
323 
324 	return (nvp);
325 }
326 
327 /*
328  * nvp_buf_free - de-Allocate an i_nvp_t.
329  */
330 static void
331 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
332 {
333 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
334 	size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
335 
336 	nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
337 }
338 
339 /*
340  * nvp_buf_link - link a new nv pair into the nvlist.
341  */
342 static void
343 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
344 {
345 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
346 	i_nvp_t *curr = NVPAIR2I_NVP(nvp);
347 
348 	/* Put element at end of nvlist */
349 	if (priv->nvp_list == NULL) {
350 		priv->nvp_list = priv->nvp_last = curr;
351 	} else {
352 		curr->nvi_prev = priv->nvp_last;
353 		priv->nvp_last->nvi_next = curr;
354 		priv->nvp_last = curr;
355 	}
356 }
357 
358 /*
359  * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
360  */
361 static void
362 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
363 {
364 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
365 	i_nvp_t *curr = NVPAIR2I_NVP(nvp);
366 
367 	/*
368 	 * protect nvlist_next_nvpair() against walking on freed memory.
369 	 */
370 	if (priv->nvp_curr == curr)
371 		priv->nvp_curr = curr->nvi_next;
372 
373 	if (curr == priv->nvp_list)
374 		priv->nvp_list = curr->nvi_next;
375 	else
376 		curr->nvi_prev->nvi_next = curr->nvi_next;
377 
378 	if (curr == priv->nvp_last)
379 		priv->nvp_last = curr->nvi_prev;
380 	else
381 		curr->nvi_next->nvi_prev = curr->nvi_prev;
382 }
383 
384 /*
385  * take a nvpair type and number of elements and make sure the are valid
386  */
387 static int
388 i_validate_type_nelem(data_type_t type, uint_t nelem)
389 {
390 	switch (type) {
391 	case DATA_TYPE_BOOLEAN:
392 		if (nelem != 0)
393 			return (EINVAL);
394 		break;
395 	case DATA_TYPE_BOOLEAN_VALUE:
396 	case DATA_TYPE_BYTE:
397 	case DATA_TYPE_INT8:
398 	case DATA_TYPE_UINT8:
399 	case DATA_TYPE_INT16:
400 	case DATA_TYPE_UINT16:
401 	case DATA_TYPE_INT32:
402 	case DATA_TYPE_UINT32:
403 	case DATA_TYPE_INT64:
404 	case DATA_TYPE_UINT64:
405 	case DATA_TYPE_STRING:
406 	case DATA_TYPE_HRTIME:
407 	case DATA_TYPE_NVLIST:
408 #if !defined(_KERNEL)
409 	case DATA_TYPE_DOUBLE:
410 #endif
411 		if (nelem != 1)
412 			return (EINVAL);
413 		break;
414 	case DATA_TYPE_BOOLEAN_ARRAY:
415 	case DATA_TYPE_BYTE_ARRAY:
416 	case DATA_TYPE_INT8_ARRAY:
417 	case DATA_TYPE_UINT8_ARRAY:
418 	case DATA_TYPE_INT16_ARRAY:
419 	case DATA_TYPE_UINT16_ARRAY:
420 	case DATA_TYPE_INT32_ARRAY:
421 	case DATA_TYPE_UINT32_ARRAY:
422 	case DATA_TYPE_INT64_ARRAY:
423 	case DATA_TYPE_UINT64_ARRAY:
424 	case DATA_TYPE_STRING_ARRAY:
425 	case DATA_TYPE_NVLIST_ARRAY:
426 		/* we allow arrays with 0 elements */
427 		break;
428 	default:
429 		return (EINVAL);
430 	}
431 	return (0);
432 }
433 
434 /*
435  * Verify nvp_name_sz and check the name string length.
436  */
437 static int
438 i_validate_nvpair_name(nvpair_t *nvp)
439 {
440 	if ((nvp->nvp_name_sz <= 0) ||
441 	    (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
442 		return (EFAULT);
443 
444 	/* verify the name string, make sure its terminated */
445 	if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
446 		return (EFAULT);
447 
448 	return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
449 }
450 
451 static int
452 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
453 {
454 	switch (type) {
455 	case DATA_TYPE_BOOLEAN_VALUE:
456 		if (*(boolean_t *)data != B_TRUE &&
457 		    *(boolean_t *)data != B_FALSE)
458 			return (EINVAL);
459 		break;
460 	case DATA_TYPE_BOOLEAN_ARRAY: {
461 		int i;
462 
463 		for (i = 0; i < nelem; i++)
464 			if (((boolean_t *)data)[i] != B_TRUE &&
465 			    ((boolean_t *)data)[i] != B_FALSE)
466 				return (EINVAL);
467 		break;
468 	}
469 	default:
470 		break;
471 	}
472 
473 	return (0);
474 }
475 
476 /*
477  * This function takes a pointer to what should be a nvpair and it's size
478  * and then verifies that all the nvpair fields make sense and can be
479  * trusted.  This function is used when decoding packed nvpairs.
480  */
481 static int
482 i_validate_nvpair(nvpair_t *nvp)
483 {
484 	data_type_t type = NVP_TYPE(nvp);
485 	int size1, size2;
486 
487 	/* verify nvp_name_sz, check the name string length */
488 	if (i_validate_nvpair_name(nvp) != 0)
489 		return (EFAULT);
490 
491 	if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
492 		return (EFAULT);
493 
494 	/*
495 	 * verify nvp_type, nvp_value_elem, and also possibly
496 	 * verify string values and get the value size.
497 	 */
498 	size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
499 	size1 = nvp->nvp_size - NVP_VALOFF(nvp);
500 	if (size2 < 0 || size1 != NV_ALIGN(size2))
501 		return (EFAULT);
502 
503 	return (0);
504 }
505 
506 static int
507 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
508 {
509 	nvpriv_t *priv;
510 	i_nvp_t *curr;
511 
512 	if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
513 		return (EINVAL);
514 
515 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
516 		nvpair_t *nvp = &curr->nvi_nvp;
517 		int err;
518 
519 		if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
520 		    NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
521 			return (err);
522 	}
523 
524 	return (0);
525 }
526 
527 /*
528  * Frees all memory allocated for an nvpair (like embedded lists) with
529  * the exception of the nvpair buffer itself.
530  */
531 static void
532 nvpair_free(nvpair_t *nvp)
533 {
534 	switch (NVP_TYPE(nvp)) {
535 	case DATA_TYPE_NVLIST:
536 		nvlist_free(EMBEDDED_NVL(nvp));
537 		break;
538 	case DATA_TYPE_NVLIST_ARRAY: {
539 		nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
540 		int i;
541 
542 		for (i = 0; i < NVP_NELEM(nvp); i++)
543 			if (nvlp[i] != NULL)
544 				nvlist_free(nvlp[i]);
545 		break;
546 	}
547 	default:
548 		break;
549 	}
550 }
551 
552 /*
553  * nvlist_free - free an unpacked nvlist
554  */
555 void
556 nvlist_free(nvlist_t *nvl)
557 {
558 	nvpriv_t *priv;
559 	i_nvp_t *curr;
560 
561 	if (nvl == NULL ||
562 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
563 		return;
564 
565 	/*
566 	 * Unpacked nvlist are linked through i_nvp_t
567 	 */
568 	curr = priv->nvp_list;
569 	while (curr != NULL) {
570 		nvpair_t *nvp = &curr->nvi_nvp;
571 		curr = curr->nvi_next;
572 
573 		nvpair_free(nvp);
574 		nvp_buf_free(nvl, nvp);
575 	}
576 
577 	if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
578 		nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
579 	else
580 		nvl->nvl_priv = 0;
581 
582 	nv_mem_free(priv, priv, sizeof (nvpriv_t));
583 }
584 
585 static int
586 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
587 {
588 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
589 	i_nvp_t *curr;
590 
591 	if (nvp == NULL)
592 		return (0);
593 
594 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
595 		if (&curr->nvi_nvp == nvp)
596 			return (1);
597 
598 	return (0);
599 }
600 
601 /*
602  * Make a copy of nvlist
603  */
604 /*ARGSUSED1*/
605 int
606 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
607 {
608 #if defined(_KERNEL) && !defined(_BOOT)
609 	return (nvlist_xdup(nvl, nvlp,
610 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
611 #else
612 	return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
613 #endif
614 }
615 
616 int
617 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
618 {
619 	int err;
620 	nvlist_t *ret;
621 
622 	if (nvl == NULL || nvlp == NULL)
623 		return (EINVAL);
624 
625 	if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
626 		return (err);
627 
628 	if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
629 		nvlist_free(ret);
630 	else
631 		*nvlp = ret;
632 
633 	return (err);
634 }
635 
636 /*
637  * Remove all with matching name
638  */
639 int
640 nvlist_remove_all(nvlist_t *nvl, const char *name)
641 {
642 	nvpriv_t *priv;
643 	i_nvp_t *curr;
644 	int error = ENOENT;
645 
646 	if (nvl == NULL || name == NULL ||
647 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
648 		return (EINVAL);
649 
650 	curr = priv->nvp_list;
651 	while (curr != NULL) {
652 		nvpair_t *nvp = &curr->nvi_nvp;
653 
654 		curr = curr->nvi_next;
655 		if (strcmp(name, NVP_NAME(nvp)) != 0)
656 			continue;
657 
658 		nvp_buf_unlink(nvl, nvp);
659 		nvpair_free(nvp);
660 		nvp_buf_free(nvl, nvp);
661 
662 		error = 0;
663 	}
664 
665 	return (error);
666 }
667 
668 /*
669  * Remove first one with matching name and type
670  */
671 int
672 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
673 {
674 	nvpriv_t *priv;
675 	i_nvp_t *curr;
676 
677 	if (nvl == NULL || name == NULL ||
678 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
679 		return (EINVAL);
680 
681 	curr = priv->nvp_list;
682 	while (curr != NULL) {
683 		nvpair_t *nvp = &curr->nvi_nvp;
684 
685 		if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
686 			nvp_buf_unlink(nvl, nvp);
687 			nvpair_free(nvp);
688 			nvp_buf_free(nvl, nvp);
689 
690 			return (0);
691 		}
692 		curr = curr->nvi_next;
693 	}
694 
695 	return (ENOENT);
696 }
697 
698 int
699 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
700 {
701 	if (nvl == NULL || nvp == NULL)
702 		return (EINVAL);
703 
704 	nvp_buf_unlink(nvl, nvp);
705 	nvpair_free(nvp);
706 	nvp_buf_free(nvl, nvp);
707 	return (0);
708 }
709 
710 /*
711  * This function calculates the size of an nvpair value.
712  *
713  * The data argument controls the behavior in case of the data types
714  * 	DATA_TYPE_STRING    	and
715  *	DATA_TYPE_STRING_ARRAY
716  * Is data == NULL then the size of the string(s) is excluded.
717  */
718 static int
719 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
720 {
721 	uint64_t value_sz;
722 
723 	if (i_validate_type_nelem(type, nelem) != 0)
724 		return (-1);
725 
726 	/* Calculate required size for holding value */
727 	switch (type) {
728 	case DATA_TYPE_BOOLEAN:
729 		value_sz = 0;
730 		break;
731 	case DATA_TYPE_BOOLEAN_VALUE:
732 		value_sz = sizeof (boolean_t);
733 		break;
734 	case DATA_TYPE_BYTE:
735 		value_sz = sizeof (uchar_t);
736 		break;
737 	case DATA_TYPE_INT8:
738 		value_sz = sizeof (int8_t);
739 		break;
740 	case DATA_TYPE_UINT8:
741 		value_sz = sizeof (uint8_t);
742 		break;
743 	case DATA_TYPE_INT16:
744 		value_sz = sizeof (int16_t);
745 		break;
746 	case DATA_TYPE_UINT16:
747 		value_sz = sizeof (uint16_t);
748 		break;
749 	case DATA_TYPE_INT32:
750 		value_sz = sizeof (int32_t);
751 		break;
752 	case DATA_TYPE_UINT32:
753 		value_sz = sizeof (uint32_t);
754 		break;
755 	case DATA_TYPE_INT64:
756 		value_sz = sizeof (int64_t);
757 		break;
758 	case DATA_TYPE_UINT64:
759 		value_sz = sizeof (uint64_t);
760 		break;
761 #if !defined(_KERNEL)
762 	case DATA_TYPE_DOUBLE:
763 		value_sz = sizeof (double);
764 		break;
765 #endif
766 	case DATA_TYPE_STRING:
767 		if (data == NULL)
768 			value_sz = 0;
769 		else
770 			value_sz = strlen(data) + 1;
771 		break;
772 	case DATA_TYPE_BOOLEAN_ARRAY:
773 		value_sz = (uint64_t)nelem * sizeof (boolean_t);
774 		break;
775 	case DATA_TYPE_BYTE_ARRAY:
776 		value_sz = (uint64_t)nelem * sizeof (uchar_t);
777 		break;
778 	case DATA_TYPE_INT8_ARRAY:
779 		value_sz = (uint64_t)nelem * sizeof (int8_t);
780 		break;
781 	case DATA_TYPE_UINT8_ARRAY:
782 		value_sz = (uint64_t)nelem * sizeof (uint8_t);
783 		break;
784 	case DATA_TYPE_INT16_ARRAY:
785 		value_sz = (uint64_t)nelem * sizeof (int16_t);
786 		break;
787 	case DATA_TYPE_UINT16_ARRAY:
788 		value_sz = (uint64_t)nelem * sizeof (uint16_t);
789 		break;
790 	case DATA_TYPE_INT32_ARRAY:
791 		value_sz = (uint64_t)nelem * sizeof (int32_t);
792 		break;
793 	case DATA_TYPE_UINT32_ARRAY:
794 		value_sz = (uint64_t)nelem * sizeof (uint32_t);
795 		break;
796 	case DATA_TYPE_INT64_ARRAY:
797 		value_sz = (uint64_t)nelem * sizeof (int64_t);
798 		break;
799 	case DATA_TYPE_UINT64_ARRAY:
800 		value_sz = (uint64_t)nelem * sizeof (uint64_t);
801 		break;
802 	case DATA_TYPE_STRING_ARRAY:
803 		value_sz = (uint64_t)nelem * sizeof (uint64_t);
804 
805 		if (data != NULL) {
806 			char *const *strs = data;
807 			uint_t i;
808 
809 			/* no alignment requirement for strings */
810 			for (i = 0; i < nelem; i++) {
811 				if (strs[i] == NULL)
812 					return (-1);
813 				value_sz += strlen(strs[i]) + 1;
814 			}
815 		}
816 		break;
817 	case DATA_TYPE_HRTIME:
818 		value_sz = sizeof (hrtime_t);
819 		break;
820 	case DATA_TYPE_NVLIST:
821 		value_sz = NV_ALIGN(sizeof (nvlist_t));
822 		break;
823 	case DATA_TYPE_NVLIST_ARRAY:
824 		value_sz = (uint64_t)nelem * sizeof (uint64_t) +
825 		    (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
826 		break;
827 	default:
828 		return (-1);
829 	}
830 
831 	return (value_sz > INT32_MAX ? -1 : (int)value_sz);
832 }
833 
834 static int
835 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
836 {
837 	nvpriv_t *priv;
838 	int err;
839 
840 	if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
841 	    nvl->nvl_priv)) == NULL)
842 		return (ENOMEM);
843 
844 	nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
845 
846 	if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
847 		nvlist_free(emb_nvl);
848 		emb_nvl->nvl_priv = 0;
849 	}
850 
851 	return (err);
852 }
853 
854 /*
855  * nvlist_add_common - Add new <name,value> pair to nvlist
856  */
857 static int
858 nvlist_add_common(nvlist_t *nvl, const char *name,
859     data_type_t type, uint_t nelem, const void *data)
860 {
861 	nvpair_t *nvp;
862 	uint_t i;
863 
864 	int nvp_sz, name_sz, value_sz;
865 	int err = 0;
866 
867 	if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
868 		return (EINVAL);
869 
870 	if (nelem != 0 && data == NULL)
871 		return (EINVAL);
872 
873 	/*
874 	 * Verify type and nelem and get the value size.
875 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
876 	 * is the size of the string(s) included.
877 	 */
878 	if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
879 		return (EINVAL);
880 
881 	if (i_validate_nvpair_value(type, nelem, data) != 0)
882 		return (EINVAL);
883 
884 	/*
885 	 * If we're adding an nvlist or nvlist array, ensure that we are not
886 	 * adding the input nvlist to itself, which would cause recursion,
887 	 * and ensure that no NULL nvlist pointers are present.
888 	 */
889 	switch (type) {
890 	case DATA_TYPE_NVLIST:
891 		if (data == nvl || data == NULL)
892 			return (EINVAL);
893 		break;
894 	case DATA_TYPE_NVLIST_ARRAY: {
895 		nvlist_t **onvlp = (nvlist_t **)data;
896 		for (i = 0; i < nelem; i++) {
897 			if (onvlp[i] == nvl || onvlp[i] == NULL)
898 				return (EINVAL);
899 		}
900 		break;
901 	}
902 	default:
903 		break;
904 	}
905 
906 	/* calculate sizes of the nvpair elements and the nvpair itself */
907 	name_sz = strlen(name) + 1;
908 
909 	nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
910 
911 	if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
912 		return (ENOMEM);
913 
914 	ASSERT(nvp->nvp_size == nvp_sz);
915 	nvp->nvp_name_sz = name_sz;
916 	nvp->nvp_value_elem = nelem;
917 	nvp->nvp_type = type;
918 	bcopy(name, NVP_NAME(nvp), name_sz);
919 
920 	switch (type) {
921 	case DATA_TYPE_BOOLEAN:
922 		break;
923 	case DATA_TYPE_STRING_ARRAY: {
924 		char *const *strs = data;
925 		char *buf = NVP_VALUE(nvp);
926 		char **cstrs = (void *)buf;
927 
928 		/* skip pre-allocated space for pointer array */
929 		buf += nelem * sizeof (uint64_t);
930 		for (i = 0; i < nelem; i++) {
931 			int slen = strlen(strs[i]) + 1;
932 			bcopy(strs[i], buf, slen);
933 			cstrs[i] = buf;
934 			buf += slen;
935 		}
936 		break;
937 	}
938 	case DATA_TYPE_NVLIST: {
939 		nvlist_t *nnvl = EMBEDDED_NVL(nvp);
940 		nvlist_t *onvl = (nvlist_t *)data;
941 
942 		if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
943 			nvp_buf_free(nvl, nvp);
944 			return (err);
945 		}
946 		break;
947 	}
948 	case DATA_TYPE_NVLIST_ARRAY: {
949 		nvlist_t **onvlp = (nvlist_t **)data;
950 		nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
951 		nvlist_t *embedded = (nvlist_t *)
952 		    ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
953 
954 		for (i = 0; i < nelem; i++) {
955 			if ((err = nvlist_copy_embedded(nvl,
956 			    onvlp[i], embedded)) != 0) {
957 				/*
958 				 * Free any successfully created lists
959 				 */
960 				nvpair_free(nvp);
961 				nvp_buf_free(nvl, nvp);
962 				return (err);
963 			}
964 
965 			nvlp[i] = embedded++;
966 		}
967 		break;
968 	}
969 	default:
970 		bcopy(data, NVP_VALUE(nvp), value_sz);
971 	}
972 
973 	/* if unique name, remove before add */
974 	if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
975 		(void) nvlist_remove_all(nvl, name);
976 	else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
977 		(void) nvlist_remove(nvl, name, type);
978 
979 	nvp_buf_link(nvl, nvp);
980 
981 	return (0);
982 }
983 
984 int
985 nvlist_add_boolean(nvlist_t *nvl, const char *name)
986 {
987 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
988 }
989 
990 int
991 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
992 {
993 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
994 }
995 
996 int
997 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
998 {
999 	return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1000 }
1001 
1002 int
1003 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1004 {
1005 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1006 }
1007 
1008 int
1009 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1010 {
1011 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1012 }
1013 
1014 int
1015 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1016 {
1017 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1018 }
1019 
1020 int
1021 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1022 {
1023 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1024 }
1025 
1026 int
1027 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1028 {
1029 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1030 }
1031 
1032 int
1033 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1034 {
1035 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1036 }
1037 
1038 int
1039 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1040 {
1041 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1042 }
1043 
1044 int
1045 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1046 {
1047 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1048 }
1049 
1050 #if !defined(_KERNEL)
1051 int
1052 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1053 {
1054 	return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1055 }
1056 #endif
1057 
1058 int
1059 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1060 {
1061 	return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1062 }
1063 
1064 int
1065 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1066     boolean_t *a, uint_t n)
1067 {
1068 	return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1069 }
1070 
1071 int
1072 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1073 {
1074 	return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1075 }
1076 
1077 int
1078 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1079 {
1080 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1081 }
1082 
1083 int
1084 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1085 {
1086 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1087 }
1088 
1089 int
1090 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1091 {
1092 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1093 }
1094 
1095 int
1096 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1097 {
1098 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1099 }
1100 
1101 int
1102 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1103 {
1104 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1105 }
1106 
1107 int
1108 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1109 {
1110 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1111 }
1112 
1113 int
1114 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1115 {
1116 	return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1117 }
1118 
1119 int
1120 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1121 {
1122 	return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1123 }
1124 
1125 int
1126 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1127     char *const *a, uint_t n)
1128 {
1129 	return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1130 }
1131 
1132 int
1133 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1134 {
1135 	return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1136 }
1137 
1138 int
1139 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1140 {
1141 	return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1142 }
1143 
1144 int
1145 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1146 {
1147 	return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1148 }
1149 
1150 /* reading name-value pairs */
1151 nvpair_t *
1152 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1153 {
1154 	nvpriv_t *priv;
1155 	i_nvp_t *curr;
1156 
1157 	if (nvl == NULL ||
1158 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1159 		return (NULL);
1160 
1161 	curr = NVPAIR2I_NVP(nvp);
1162 
1163 	/*
1164 	 * Ensure that nvp is a valid nvpair on this nvlist.
1165 	 * NB: nvp_curr is used only as a hint so that we don't always
1166 	 * have to walk the list to determine if nvp is still on the list.
1167 	 */
1168 	if (nvp == NULL)
1169 		curr = priv->nvp_list;
1170 	else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1171 		curr = curr->nvi_next;
1172 	else
1173 		curr = NULL;
1174 
1175 	priv->nvp_curr = curr;
1176 
1177 	return (curr != NULL ? &curr->nvi_nvp : NULL);
1178 }
1179 
1180 nvpair_t *
1181 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1182 {
1183 	nvpriv_t *priv;
1184 	i_nvp_t *curr;
1185 
1186 	if (nvl == NULL ||
1187 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1188 		return (NULL);
1189 
1190 	curr = NVPAIR2I_NVP(nvp);
1191 
1192 	if (nvp == NULL)
1193 		curr = priv->nvp_last;
1194 	else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1195 		curr = curr->nvi_prev;
1196 	else
1197 		curr = NULL;
1198 
1199 	priv->nvp_curr = curr;
1200 
1201 	return (curr != NULL ? &curr->nvi_nvp : NULL);
1202 }
1203 
1204 boolean_t
1205 nvlist_empty(nvlist_t *nvl)
1206 {
1207 	nvpriv_t *priv;
1208 
1209 	if (nvl == NULL ||
1210 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1211 		return (B_TRUE);
1212 
1213 	return (priv->nvp_list == NULL);
1214 }
1215 
1216 char *
1217 nvpair_name(nvpair_t *nvp)
1218 {
1219 	return (NVP_NAME(nvp));
1220 }
1221 
1222 data_type_t
1223 nvpair_type(nvpair_t *nvp)
1224 {
1225 	return (NVP_TYPE(nvp));
1226 }
1227 
1228 int
1229 nvpair_type_is_array(nvpair_t *nvp)
1230 {
1231 	data_type_t type = NVP_TYPE(nvp);
1232 
1233 	if ((type == DATA_TYPE_BYTE_ARRAY) ||
1234 	    (type == DATA_TYPE_INT8_ARRAY) ||
1235 	    (type == DATA_TYPE_UINT8_ARRAY) ||
1236 	    (type == DATA_TYPE_INT16_ARRAY) ||
1237 	    (type == DATA_TYPE_UINT16_ARRAY) ||
1238 	    (type == DATA_TYPE_INT32_ARRAY) ||
1239 	    (type == DATA_TYPE_UINT32_ARRAY) ||
1240 	    (type == DATA_TYPE_INT64_ARRAY) ||
1241 	    (type == DATA_TYPE_UINT64_ARRAY) ||
1242 	    (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1243 	    (type == DATA_TYPE_STRING_ARRAY) ||
1244 	    (type == DATA_TYPE_NVLIST_ARRAY))
1245 		return (1);
1246 	return (0);
1247 
1248 }
1249 
1250 static int
1251 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1252 {
1253 	if (nvp == NULL || nvpair_type(nvp) != type)
1254 		return (EINVAL);
1255 
1256 	/*
1257 	 * For non-array types, we copy the data.
1258 	 * For array types (including string), we set a pointer.
1259 	 */
1260 	switch (type) {
1261 	case DATA_TYPE_BOOLEAN:
1262 		if (nelem != NULL)
1263 			*nelem = 0;
1264 		break;
1265 
1266 	case DATA_TYPE_BOOLEAN_VALUE:
1267 	case DATA_TYPE_BYTE:
1268 	case DATA_TYPE_INT8:
1269 	case DATA_TYPE_UINT8:
1270 	case DATA_TYPE_INT16:
1271 	case DATA_TYPE_UINT16:
1272 	case DATA_TYPE_INT32:
1273 	case DATA_TYPE_UINT32:
1274 	case DATA_TYPE_INT64:
1275 	case DATA_TYPE_UINT64:
1276 	case DATA_TYPE_HRTIME:
1277 #if !defined(_KERNEL)
1278 	case DATA_TYPE_DOUBLE:
1279 #endif
1280 		if (data == NULL)
1281 			return (EINVAL);
1282 		bcopy(NVP_VALUE(nvp), data,
1283 		    (size_t)i_get_value_size(type, NULL, 1));
1284 		if (nelem != NULL)
1285 			*nelem = 1;
1286 		break;
1287 
1288 	case DATA_TYPE_NVLIST:
1289 	case DATA_TYPE_STRING:
1290 		if (data == NULL)
1291 			return (EINVAL);
1292 		*(void **)data = (void *)NVP_VALUE(nvp);
1293 		if (nelem != NULL)
1294 			*nelem = 1;
1295 		break;
1296 
1297 	case DATA_TYPE_BOOLEAN_ARRAY:
1298 	case DATA_TYPE_BYTE_ARRAY:
1299 	case DATA_TYPE_INT8_ARRAY:
1300 	case DATA_TYPE_UINT8_ARRAY:
1301 	case DATA_TYPE_INT16_ARRAY:
1302 	case DATA_TYPE_UINT16_ARRAY:
1303 	case DATA_TYPE_INT32_ARRAY:
1304 	case DATA_TYPE_UINT32_ARRAY:
1305 	case DATA_TYPE_INT64_ARRAY:
1306 	case DATA_TYPE_UINT64_ARRAY:
1307 	case DATA_TYPE_STRING_ARRAY:
1308 	case DATA_TYPE_NVLIST_ARRAY:
1309 		if (nelem == NULL || data == NULL)
1310 			return (EINVAL);
1311 		if ((*nelem = NVP_NELEM(nvp)) != 0)
1312 			*(void **)data = (void *)NVP_VALUE(nvp);
1313 		else
1314 			*(void **)data = NULL;
1315 		break;
1316 
1317 	default:
1318 		return (ENOTSUP);
1319 	}
1320 
1321 	return (0);
1322 }
1323 
1324 static int
1325 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1326     uint_t *nelem, void *data)
1327 {
1328 	nvpriv_t *priv;
1329 	nvpair_t *nvp;
1330 	i_nvp_t *curr;
1331 
1332 	if (name == NULL || nvl == NULL ||
1333 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1334 		return (EINVAL);
1335 
1336 	if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1337 		return (ENOTSUP);
1338 
1339 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1340 		nvp = &curr->nvi_nvp;
1341 
1342 		if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1343 			return (nvpair_value_common(nvp, type, nelem, data));
1344 	}
1345 
1346 	return (ENOENT);
1347 }
1348 
1349 int
1350 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1351 {
1352 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1353 }
1354 
1355 int
1356 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1357 {
1358 	return (nvlist_lookup_common(nvl, name,
1359 	    DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1360 }
1361 
1362 int
1363 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1364 {
1365 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1366 }
1367 
1368 int
1369 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1370 {
1371 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1372 }
1373 
1374 int
1375 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1376 {
1377 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1378 }
1379 
1380 int
1381 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1382 {
1383 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1384 }
1385 
1386 int
1387 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1388 {
1389 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1390 }
1391 
1392 int
1393 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1394 {
1395 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1396 }
1397 
1398 int
1399 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1400 {
1401 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1402 }
1403 
1404 int
1405 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1406 {
1407 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1408 }
1409 
1410 int
1411 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1412 {
1413 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1414 }
1415 
1416 #if !defined(_KERNEL)
1417 int
1418 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1419 {
1420 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1421 }
1422 #endif
1423 
1424 int
1425 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1426 {
1427 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1428 }
1429 
1430 int
1431 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1432 {
1433 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1434 }
1435 
1436 int
1437 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1438     boolean_t **a, uint_t *n)
1439 {
1440 	return (nvlist_lookup_common(nvl, name,
1441 	    DATA_TYPE_BOOLEAN_ARRAY, n, a));
1442 }
1443 
1444 int
1445 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1446     uchar_t **a, uint_t *n)
1447 {
1448 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1449 }
1450 
1451 int
1452 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1453 {
1454 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1455 }
1456 
1457 int
1458 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1459     uint8_t **a, uint_t *n)
1460 {
1461 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1462 }
1463 
1464 int
1465 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1466     int16_t **a, uint_t *n)
1467 {
1468 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1469 }
1470 
1471 int
1472 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1473     uint16_t **a, uint_t *n)
1474 {
1475 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1476 }
1477 
1478 int
1479 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1480     int32_t **a, uint_t *n)
1481 {
1482 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1483 }
1484 
1485 int
1486 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1487     uint32_t **a, uint_t *n)
1488 {
1489 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1490 }
1491 
1492 int
1493 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1494     int64_t **a, uint_t *n)
1495 {
1496 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1497 }
1498 
1499 int
1500 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1501     uint64_t **a, uint_t *n)
1502 {
1503 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1504 }
1505 
1506 int
1507 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1508     char ***a, uint_t *n)
1509 {
1510 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1511 }
1512 
1513 int
1514 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1515     nvlist_t ***a, uint_t *n)
1516 {
1517 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1518 }
1519 
1520 int
1521 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1522 {
1523 	return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1524 }
1525 
1526 int
1527 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1528 {
1529 	va_list ap;
1530 	char *name;
1531 	int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1532 	int ret = 0;
1533 
1534 	va_start(ap, flag);
1535 	while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1536 		data_type_t type;
1537 		void *val;
1538 		uint_t *nelem;
1539 
1540 		switch (type = va_arg(ap, data_type_t)) {
1541 		case DATA_TYPE_BOOLEAN:
1542 			ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1543 			break;
1544 
1545 		case DATA_TYPE_BOOLEAN_VALUE:
1546 		case DATA_TYPE_BYTE:
1547 		case DATA_TYPE_INT8:
1548 		case DATA_TYPE_UINT8:
1549 		case DATA_TYPE_INT16:
1550 		case DATA_TYPE_UINT16:
1551 		case DATA_TYPE_INT32:
1552 		case DATA_TYPE_UINT32:
1553 		case DATA_TYPE_INT64:
1554 		case DATA_TYPE_UINT64:
1555 		case DATA_TYPE_HRTIME:
1556 		case DATA_TYPE_STRING:
1557 		case DATA_TYPE_NVLIST:
1558 #if !defined(_KERNEL)
1559 		case DATA_TYPE_DOUBLE:
1560 #endif
1561 			val = va_arg(ap, void *);
1562 			ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1563 			break;
1564 
1565 		case DATA_TYPE_BYTE_ARRAY:
1566 		case DATA_TYPE_BOOLEAN_ARRAY:
1567 		case DATA_TYPE_INT8_ARRAY:
1568 		case DATA_TYPE_UINT8_ARRAY:
1569 		case DATA_TYPE_INT16_ARRAY:
1570 		case DATA_TYPE_UINT16_ARRAY:
1571 		case DATA_TYPE_INT32_ARRAY:
1572 		case DATA_TYPE_UINT32_ARRAY:
1573 		case DATA_TYPE_INT64_ARRAY:
1574 		case DATA_TYPE_UINT64_ARRAY:
1575 		case DATA_TYPE_STRING_ARRAY:
1576 		case DATA_TYPE_NVLIST_ARRAY:
1577 			val = va_arg(ap, void *);
1578 			nelem = va_arg(ap, uint_t *);
1579 			ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1580 			break;
1581 
1582 		default:
1583 			ret = EINVAL;
1584 		}
1585 
1586 		if (ret == ENOENT && noentok)
1587 			ret = 0;
1588 	}
1589 	va_end(ap);
1590 
1591 	return (ret);
1592 }
1593 
1594 /*
1595  * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1596  * returns zero and a pointer to the matching nvpair is returned in '*ret'
1597  * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1598  * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1599  * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1600  * "a.d[3].e[1]".  This matches the C syntax for array embed (for convience,
1601  * code also supports "a.d[3]e[1]" syntax).
1602  *
1603  * If 'ip' is non-NULL and the last name component is an array, return the
1604  * value of the "...[index]" array index in *ip. For an array reference that
1605  * is not indexed, *ip will be returned as -1. If there is a syntax error in
1606  * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1607  * inside the 'name' string where the syntax error was detected.
1608  */
1609 static int
1610 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1611     nvpair_t **ret, int *ip, char **ep)
1612 {
1613 	nvpair_t	*nvp;
1614 	const char	*np;
1615 	char		*sepp;
1616 	char		*idxp, *idxep;
1617 	nvlist_t	**nva;
1618 	long		idx;
1619 	int		n;
1620 
1621 	if (ip)
1622 		*ip = -1;			/* not indexed */
1623 	if (ep)
1624 		*ep = NULL;
1625 
1626 	if ((nvl == NULL) || (name == NULL))
1627 		return (EINVAL);
1628 
1629 	sepp = NULL;
1630 	idx = 0;
1631 	/* step through components of name */
1632 	for (np = name; np && *np; np = sepp) {
1633 		/* ensure unique names */
1634 		if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1635 			return (ENOTSUP);
1636 
1637 		/* skip white space */
1638 		skip_whitespace(np);
1639 		if (*np == 0)
1640 			break;
1641 
1642 		/* set 'sepp' to end of current component 'np' */
1643 		if (sep)
1644 			sepp = strchr(np, sep);
1645 		else
1646 			sepp = NULL;
1647 
1648 		/* find start of next "[ index ]..." */
1649 		idxp = strchr(np, '[');
1650 
1651 		/* if sepp comes first, set idxp to NULL */
1652 		if (sepp && idxp && (sepp < idxp))
1653 			idxp = NULL;
1654 
1655 		/*
1656 		 * At this point 'idxp' is set if there is an index
1657 		 * expected for the current component.
1658 		 */
1659 		if (idxp) {
1660 			/* set 'n' to length of current 'np' name component */
1661 			n = idxp++ - np;
1662 
1663 			/* keep sepp up to date for *ep use as we advance */
1664 			skip_whitespace(idxp);
1665 			sepp = idxp;
1666 
1667 			/* determine the index value */
1668 #if defined(_KERNEL) && !defined(_BOOT)
1669 			if (ddi_strtol(idxp, &idxep, 0, &idx))
1670 				goto fail;
1671 #else
1672 			idx = strtol(idxp, &idxep, 0);
1673 #endif
1674 			if (idxep == idxp)
1675 				goto fail;
1676 
1677 			/* keep sepp up to date for *ep use as we advance */
1678 			sepp = idxep;
1679 
1680 			/* skip white space index value and check for ']' */
1681 			skip_whitespace(sepp);
1682 			if (*sepp++ != ']')
1683 				goto fail;
1684 
1685 			/* for embedded arrays, support C syntax: "a[1].b" */
1686 			skip_whitespace(sepp);
1687 			if (sep && (*sepp == sep))
1688 				sepp++;
1689 		} else if (sepp) {
1690 			n = sepp++ - np;
1691 		} else {
1692 			n = strlen(np);
1693 		}
1694 
1695 		/* trim trailing whitespace by reducing length of 'np' */
1696 		if (n == 0)
1697 			goto fail;
1698 		for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1699 			;
1700 		n++;
1701 
1702 		/* skip whitespace, and set sepp to NULL if complete */
1703 		if (sepp) {
1704 			skip_whitespace(sepp);
1705 			if (*sepp == 0)
1706 				sepp = NULL;
1707 		}
1708 
1709 		/*
1710 		 * At this point:
1711 		 * o  'n' is the length of current 'np' component.
1712 		 * o  'idxp' is set if there was an index, and value 'idx'.
1713 		 * o  'sepp' is set to the beginning of the next component,
1714 		 *    and set to NULL if we have no more components.
1715 		 *
1716 		 * Search for nvpair with matching component name.
1717 		 */
1718 		for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1719 		    nvp = nvlist_next_nvpair(nvl, nvp)) {
1720 
1721 			/* continue if no match on name */
1722 			if (strncmp(np, nvpair_name(nvp), n) ||
1723 			    (strlen(nvpair_name(nvp)) != n))
1724 				continue;
1725 
1726 			/* if indexed, verify type is array oriented */
1727 			if (idxp && !nvpair_type_is_array(nvp))
1728 				goto fail;
1729 
1730 			/*
1731 			 * Full match found, return nvp and idx if this
1732 			 * was the last component.
1733 			 */
1734 			if (sepp == NULL) {
1735 				if (ret)
1736 					*ret = nvp;
1737 				if (ip && idxp)
1738 					*ip = (int)idx;	/* return index */
1739 				return (0);		/* found */
1740 			}
1741 
1742 			/*
1743 			 * More components: current match must be
1744 			 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1745 			 * to support going deeper.
1746 			 */
1747 			if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1748 				nvl = EMBEDDED_NVL(nvp);
1749 				break;
1750 			} else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1751 				(void) nvpair_value_nvlist_array(nvp,
1752 				    &nva, (uint_t *)&n);
1753 				if ((n < 0) || (idx >= n))
1754 					goto fail;
1755 				nvl = nva[idx];
1756 				break;
1757 			}
1758 
1759 			/* type does not support more levels */
1760 			goto fail;
1761 		}
1762 		if (nvp == NULL)
1763 			goto fail;		/* 'name' not found */
1764 
1765 		/* search for match of next component in embedded 'nvl' list */
1766 	}
1767 
1768 fail:	if (ep && sepp)
1769 		*ep = sepp;
1770 	return (EINVAL);
1771 }
1772 
1773 /*
1774  * Return pointer to nvpair with specified 'name'.
1775  */
1776 int
1777 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1778 {
1779 	return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1780 }
1781 
1782 /*
1783  * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1784  * and return array index).  See nvlist_lookup_nvpair_ei_sep for more detailed
1785  * description.
1786  */
1787 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1788     const char *name, nvpair_t **ret, int *ip, char **ep)
1789 {
1790 	return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1791 }
1792 
1793 boolean_t
1794 nvlist_exists(nvlist_t *nvl, const char *name)
1795 {
1796 	nvpriv_t *priv;
1797 	nvpair_t *nvp;
1798 	i_nvp_t *curr;
1799 
1800 	if (name == NULL || nvl == NULL ||
1801 	    (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1802 		return (B_FALSE);
1803 
1804 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1805 		nvp = &curr->nvi_nvp;
1806 
1807 		if (strcmp(name, NVP_NAME(nvp)) == 0)
1808 			return (B_TRUE);
1809 	}
1810 
1811 	return (B_FALSE);
1812 }
1813 
1814 int
1815 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1816 {
1817 	return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1818 }
1819 
1820 int
1821 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1822 {
1823 	return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1824 }
1825 
1826 int
1827 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1828 {
1829 	return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1830 }
1831 
1832 int
1833 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1834 {
1835 	return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1836 }
1837 
1838 int
1839 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1840 {
1841 	return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1842 }
1843 
1844 int
1845 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1846 {
1847 	return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1848 }
1849 
1850 int
1851 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1852 {
1853 	return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1854 }
1855 
1856 int
1857 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1858 {
1859 	return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1860 }
1861 
1862 int
1863 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1864 {
1865 	return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1866 }
1867 
1868 int
1869 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1870 {
1871 	return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1872 }
1873 
1874 #if !defined(_KERNEL)
1875 int
1876 nvpair_value_double(nvpair_t *nvp, double *val)
1877 {
1878 	return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1879 }
1880 #endif
1881 
1882 int
1883 nvpair_value_string(nvpair_t *nvp, char **val)
1884 {
1885 	return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1886 }
1887 
1888 int
1889 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1890 {
1891 	return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1892 }
1893 
1894 int
1895 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1896 {
1897 	return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1898 }
1899 
1900 int
1901 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1902 {
1903 	return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1904 }
1905 
1906 int
1907 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1908 {
1909 	return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1910 }
1911 
1912 int
1913 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1914 {
1915 	return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1916 }
1917 
1918 int
1919 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1920 {
1921 	return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1922 }
1923 
1924 int
1925 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1926 {
1927 	return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1928 }
1929 
1930 int
1931 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1932 {
1933 	return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1934 }
1935 
1936 int
1937 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1938 {
1939 	return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1940 }
1941 
1942 int
1943 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1944 {
1945 	return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1946 }
1947 
1948 int
1949 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1950 {
1951 	return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1952 }
1953 
1954 int
1955 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1956 {
1957 	return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1958 }
1959 
1960 int
1961 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1962 {
1963 	return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1964 }
1965 
1966 int
1967 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1968 {
1969 	return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1970 }
1971 
1972 /*
1973  * Add specified pair to the list.
1974  */
1975 int
1976 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1977 {
1978 	if (nvl == NULL || nvp == NULL)
1979 		return (EINVAL);
1980 
1981 	return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1982 	    NVP_NELEM(nvp), NVP_VALUE(nvp)));
1983 }
1984 
1985 /*
1986  * Merge the supplied nvlists and put the result in dst.
1987  * The merged list will contain all names specified in both lists,
1988  * the values are taken from nvl in the case of duplicates.
1989  * Return 0 on success.
1990  */
1991 /*ARGSUSED*/
1992 int
1993 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1994 {
1995 	if (nvl == NULL || dst == NULL)
1996 		return (EINVAL);
1997 
1998 	if (dst != nvl)
1999 		return (nvlist_copy_pairs(nvl, dst));
2000 
2001 	return (0);
2002 }
2003 
2004 /*
2005  * Encoding related routines
2006  */
2007 #define	NVS_OP_ENCODE	0
2008 #define	NVS_OP_DECODE	1
2009 #define	NVS_OP_GETSIZE	2
2010 
2011 typedef struct nvs_ops nvs_ops_t;
2012 
2013 typedef struct {
2014 	int		nvs_op;
2015 	const nvs_ops_t	*nvs_ops;
2016 	void		*nvs_private;
2017 	nvpriv_t	*nvs_priv;
2018 } nvstream_t;
2019 
2020 /*
2021  * nvs operations are:
2022  *   - nvs_nvlist
2023  *     encoding / decoding of a nvlist header (nvlist_t)
2024  *     calculates the size used for header and end detection
2025  *
2026  *   - nvs_nvpair
2027  *     responsible for the first part of encoding / decoding of an nvpair
2028  *     calculates the decoded size of an nvpair
2029  *
2030  *   - nvs_nvp_op
2031  *     second part of encoding / decoding of an nvpair
2032  *
2033  *   - nvs_nvp_size
2034  *     calculates the encoding size of an nvpair
2035  *
2036  *   - nvs_nvl_fini
2037  *     encodes the end detection mark (zeros).
2038  */
2039 struct nvs_ops {
2040 	int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2041 	int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2042 	int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2043 	int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2044 	int (*nvs_nvl_fini)(nvstream_t *);
2045 };
2046 
2047 typedef struct {
2048 	char	nvh_encoding;	/* nvs encoding method */
2049 	char	nvh_endian;	/* nvs endian */
2050 	char	nvh_reserved1;	/* reserved for future use */
2051 	char	nvh_reserved2;	/* reserved for future use */
2052 } nvs_header_t;
2053 
2054 static int
2055 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2056 {
2057 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2058 	i_nvp_t *curr;
2059 
2060 	/*
2061 	 * Walk nvpair in list and encode each nvpair
2062 	 */
2063 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2064 		if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2065 			return (EFAULT);
2066 
2067 	return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2068 }
2069 
2070 static int
2071 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2072 {
2073 	nvpair_t *nvp;
2074 	size_t nvsize;
2075 	int err;
2076 
2077 	/*
2078 	 * Get decoded size of next pair in stream, alloc
2079 	 * memory for nvpair_t, then decode the nvpair
2080 	 */
2081 	while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2082 		if (nvsize == 0) /* end of list */
2083 			break;
2084 
2085 		/* make sure len makes sense */
2086 		if (nvsize < NVP_SIZE_CALC(1, 0))
2087 			return (EFAULT);
2088 
2089 		if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2090 			return (ENOMEM);
2091 
2092 		if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2093 			nvp_buf_free(nvl, nvp);
2094 			return (err);
2095 		}
2096 
2097 		if (i_validate_nvpair(nvp) != 0) {
2098 			nvpair_free(nvp);
2099 			nvp_buf_free(nvl, nvp);
2100 			return (EFAULT);
2101 		}
2102 
2103 		nvp_buf_link(nvl, nvp);
2104 	}
2105 	return (err);
2106 }
2107 
2108 static int
2109 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2110 {
2111 	nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2112 	i_nvp_t *curr;
2113 	uint64_t nvsize = *buflen;
2114 	size_t size;
2115 
2116 	/*
2117 	 * Get encoded size of nvpairs in nvlist
2118 	 */
2119 	for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2120 		if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2121 			return (EINVAL);
2122 
2123 		if ((nvsize += size) > INT32_MAX)
2124 			return (EINVAL);
2125 	}
2126 
2127 	*buflen = nvsize;
2128 	return (0);
2129 }
2130 
2131 static int
2132 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2133 {
2134 	int err;
2135 
2136 	if (nvl->nvl_priv == 0)
2137 		return (EFAULT);
2138 
2139 	/*
2140 	 * Perform the operation, starting with header, then each nvpair
2141 	 */
2142 	if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2143 		return (err);
2144 
2145 	switch (nvs->nvs_op) {
2146 	case NVS_OP_ENCODE:
2147 		err = nvs_encode_pairs(nvs, nvl);
2148 		break;
2149 
2150 	case NVS_OP_DECODE:
2151 		err = nvs_decode_pairs(nvs, nvl);
2152 		break;
2153 
2154 	case NVS_OP_GETSIZE:
2155 		err = nvs_getsize_pairs(nvs, nvl, buflen);
2156 		break;
2157 
2158 	default:
2159 		err = EINVAL;
2160 	}
2161 
2162 	return (err);
2163 }
2164 
2165 static int
2166 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2167 {
2168 	switch (nvs->nvs_op) {
2169 	case NVS_OP_ENCODE:
2170 		return (nvs_operation(nvs, embedded, NULL));
2171 
2172 	case NVS_OP_DECODE: {
2173 		nvpriv_t *priv;
2174 		int err;
2175 
2176 		if (embedded->nvl_version != NV_VERSION)
2177 			return (ENOTSUP);
2178 
2179 		if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2180 			return (ENOMEM);
2181 
2182 		nvlist_init(embedded, embedded->nvl_nvflag, priv);
2183 
2184 		if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2185 			nvlist_free(embedded);
2186 		return (err);
2187 	}
2188 	default:
2189 		break;
2190 	}
2191 
2192 	return (EINVAL);
2193 }
2194 
2195 static int
2196 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2197 {
2198 	size_t nelem = NVP_NELEM(nvp);
2199 	nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2200 	int i;
2201 
2202 	switch (nvs->nvs_op) {
2203 	case NVS_OP_ENCODE:
2204 		for (i = 0; i < nelem; i++)
2205 			if (nvs_embedded(nvs, nvlp[i]) != 0)
2206 				return (EFAULT);
2207 		break;
2208 
2209 	case NVS_OP_DECODE: {
2210 		size_t len = nelem * sizeof (uint64_t);
2211 		nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2212 
2213 		bzero(nvlp, len);	/* don't trust packed data */
2214 		for (i = 0; i < nelem; i++) {
2215 			if (nvs_embedded(nvs, embedded) != 0) {
2216 				nvpair_free(nvp);
2217 				return (EFAULT);
2218 			}
2219 
2220 			nvlp[i] = embedded++;
2221 		}
2222 		break;
2223 	}
2224 	case NVS_OP_GETSIZE: {
2225 		uint64_t nvsize = 0;
2226 
2227 		for (i = 0; i < nelem; i++) {
2228 			size_t nvp_sz = 0;
2229 
2230 			if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2231 				return (EINVAL);
2232 
2233 			if ((nvsize += nvp_sz) > INT32_MAX)
2234 				return (EINVAL);
2235 		}
2236 
2237 		*size = nvsize;
2238 		break;
2239 	}
2240 	default:
2241 		return (EINVAL);
2242 	}
2243 
2244 	return (0);
2245 }
2246 
2247 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2248 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2249 
2250 /*
2251  * Common routine for nvlist operations:
2252  * encode, decode, getsize (encoded size).
2253  */
2254 static int
2255 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2256     int nvs_op)
2257 {
2258 	int err = 0;
2259 	nvstream_t nvs;
2260 	int nvl_endian;
2261 #ifdef	_LITTLE_ENDIAN
2262 	int host_endian = 1;
2263 #else
2264 	int host_endian = 0;
2265 #endif	/* _LITTLE_ENDIAN */
2266 	nvs_header_t *nvh = (void *)buf;
2267 
2268 	if (buflen == NULL || nvl == NULL ||
2269 	    (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2270 		return (EINVAL);
2271 
2272 	nvs.nvs_op = nvs_op;
2273 
2274 	/*
2275 	 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2276 	 * a buffer is allocated.  The first 4 bytes in the buffer are
2277 	 * used for encoding method and host endian.
2278 	 */
2279 	switch (nvs_op) {
2280 	case NVS_OP_ENCODE:
2281 		if (buf == NULL || *buflen < sizeof (nvs_header_t))
2282 			return (EINVAL);
2283 
2284 		nvh->nvh_encoding = encoding;
2285 		nvh->nvh_endian = nvl_endian = host_endian;
2286 		nvh->nvh_reserved1 = 0;
2287 		nvh->nvh_reserved2 = 0;
2288 		break;
2289 
2290 	case NVS_OP_DECODE:
2291 		if (buf == NULL || *buflen < sizeof (nvs_header_t))
2292 			return (EINVAL);
2293 
2294 		/* get method of encoding from first byte */
2295 		encoding = nvh->nvh_encoding;
2296 		nvl_endian = nvh->nvh_endian;
2297 		break;
2298 
2299 	case NVS_OP_GETSIZE:
2300 		nvl_endian = host_endian;
2301 
2302 		/*
2303 		 * add the size for encoding
2304 		 */
2305 		*buflen = sizeof (nvs_header_t);
2306 		break;
2307 
2308 	default:
2309 		return (ENOTSUP);
2310 	}
2311 
2312 	/*
2313 	 * Create an nvstream with proper encoding method
2314 	 */
2315 	switch (encoding) {
2316 	case NV_ENCODE_NATIVE:
2317 		/*
2318 		 * check endianness, in case we are unpacking
2319 		 * from a file
2320 		 */
2321 		if (nvl_endian != host_endian)
2322 			return (ENOTSUP);
2323 		err = nvs_native(&nvs, nvl, buf, buflen);
2324 		break;
2325 	case NV_ENCODE_XDR:
2326 		err = nvs_xdr(&nvs, nvl, buf, buflen);
2327 		break;
2328 	default:
2329 		err = ENOTSUP;
2330 		break;
2331 	}
2332 
2333 	return (err);
2334 }
2335 
2336 int
2337 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2338 {
2339 	return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2340 }
2341 
2342 /*
2343  * Pack nvlist into contiguous memory
2344  */
2345 /*ARGSUSED1*/
2346 int
2347 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2348     int kmflag)
2349 {
2350 #if defined(_KERNEL) && !defined(_BOOT)
2351 	return (nvlist_xpack(nvl, bufp, buflen, encoding,
2352 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2353 #else
2354 	return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2355 #endif
2356 }
2357 
2358 int
2359 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2360     nv_alloc_t *nva)
2361 {
2362 	nvpriv_t nvpriv;
2363 	size_t alloc_size;
2364 	char *buf;
2365 	int err;
2366 
2367 	if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2368 		return (EINVAL);
2369 
2370 	if (*bufp != NULL)
2371 		return (nvlist_common(nvl, *bufp, buflen, encoding,
2372 		    NVS_OP_ENCODE));
2373 
2374 	/*
2375 	 * Here is a difficult situation:
2376 	 * 1. The nvlist has fixed allocator properties.
2377 	 *    All other nvlist routines (like nvlist_add_*, ...) use
2378 	 *    these properties.
2379 	 * 2. When using nvlist_pack() the user can specify his own
2380 	 *    allocator properties (e.g. by using KM_NOSLEEP).
2381 	 *
2382 	 * We use the user specified properties (2). A clearer solution
2383 	 * will be to remove the kmflag from nvlist_pack(), but we will
2384 	 * not change the interface.
2385 	 */
2386 	nv_priv_init(&nvpriv, nva, 0);
2387 
2388 	if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2389 		return (err);
2390 
2391 	if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2392 		return (ENOMEM);
2393 
2394 	if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2395 	    NVS_OP_ENCODE)) != 0) {
2396 		nv_mem_free(&nvpriv, buf, alloc_size);
2397 	} else {
2398 		*buflen = alloc_size;
2399 		*bufp = buf;
2400 	}
2401 
2402 	return (err);
2403 }
2404 
2405 /*
2406  * Unpack buf into an nvlist_t
2407  */
2408 /*ARGSUSED1*/
2409 int
2410 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2411 {
2412 #if defined(_KERNEL) && !defined(_BOOT)
2413 	return (nvlist_xunpack(buf, buflen, nvlp,
2414 	    (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2415 #else
2416 	return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2417 #endif
2418 }
2419 
2420 int
2421 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2422 {
2423 	nvlist_t *nvl;
2424 	int err;
2425 
2426 	if (nvlp == NULL)
2427 		return (EINVAL);
2428 
2429 	if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2430 		return (err);
2431 
2432 	if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2433 		nvlist_free(nvl);
2434 	else
2435 		*nvlp = nvl;
2436 
2437 	return (err);
2438 }
2439 
2440 /*
2441  * Native encoding functions
2442  */
2443 typedef struct {
2444 	/*
2445 	 * This structure is used when decoding a packed nvpair in
2446 	 * the native format.  n_base points to a buffer containing the
2447 	 * packed nvpair.  n_end is a pointer to the end of the buffer.
2448 	 * (n_end actually points to the first byte past the end of the
2449 	 * buffer.)  n_curr is a pointer that lies between n_base and n_end.
2450 	 * It points to the current data that we are decoding.
2451 	 * The amount of data left in the buffer is equal to n_end - n_curr.
2452 	 * n_flag is used to recognize a packed embedded list.
2453 	 */
2454 	caddr_t n_base;
2455 	caddr_t n_end;
2456 	caddr_t n_curr;
2457 	uint_t  n_flag;
2458 } nvs_native_t;
2459 
2460 static int
2461 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2462     size_t buflen)
2463 {
2464 	switch (nvs->nvs_op) {
2465 	case NVS_OP_ENCODE:
2466 	case NVS_OP_DECODE:
2467 		nvs->nvs_private = native;
2468 		native->n_curr = native->n_base = buf;
2469 		native->n_end = buf + buflen;
2470 		native->n_flag = 0;
2471 		return (0);
2472 
2473 	case NVS_OP_GETSIZE:
2474 		nvs->nvs_private = native;
2475 		native->n_curr = native->n_base = native->n_end = NULL;
2476 		native->n_flag = 0;
2477 		return (0);
2478 	default:
2479 		return (EINVAL);
2480 	}
2481 }
2482 
2483 /*ARGSUSED*/
2484 static void
2485 nvs_native_destroy(nvstream_t *nvs)
2486 {
2487 }
2488 
2489 static int
2490 native_cp(nvstream_t *nvs, void *buf, size_t size)
2491 {
2492 	nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2493 
2494 	if (native->n_curr + size > native->n_end)
2495 		return (EFAULT);
2496 
2497 	/*
2498 	 * The bcopy() below eliminates alignment requirement
2499 	 * on the buffer (stream) and is preferred over direct access.
2500 	 */
2501 	switch (nvs->nvs_op) {
2502 	case NVS_OP_ENCODE:
2503 		bcopy(buf, native->n_curr, size);
2504 		break;
2505 	case NVS_OP_DECODE:
2506 		bcopy(native->n_curr, buf, size);
2507 		break;
2508 	default:
2509 		return (EINVAL);
2510 	}
2511 
2512 	native->n_curr += size;
2513 	return (0);
2514 }
2515 
2516 /*
2517  * operate on nvlist_t header
2518  */
2519 static int
2520 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2521 {
2522 	nvs_native_t *native = nvs->nvs_private;
2523 
2524 	switch (nvs->nvs_op) {
2525 	case NVS_OP_ENCODE:
2526 	case NVS_OP_DECODE:
2527 		if (native->n_flag)
2528 			return (0);	/* packed embedded list */
2529 
2530 		native->n_flag = 1;
2531 
2532 		/* copy version and nvflag of the nvlist_t */
2533 		if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2534 		    native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2535 			return (EFAULT);
2536 
2537 		return (0);
2538 
2539 	case NVS_OP_GETSIZE:
2540 		/*
2541 		 * if calculate for packed embedded list
2542 		 * 	4 for end of the embedded list
2543 		 * else
2544 		 * 	2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2545 		 * 	and 4 for end of the entire list
2546 		 */
2547 		if (native->n_flag) {
2548 			*size += 4;
2549 		} else {
2550 			native->n_flag = 1;
2551 			*size += 2 * sizeof (int32_t) + 4;
2552 		}
2553 
2554 		return (0);
2555 
2556 	default:
2557 		return (EINVAL);
2558 	}
2559 }
2560 
2561 static int
2562 nvs_native_nvl_fini(nvstream_t *nvs)
2563 {
2564 	if (nvs->nvs_op == NVS_OP_ENCODE) {
2565 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2566 		/*
2567 		 * Add 4 zero bytes at end of nvlist. They are used
2568 		 * for end detection by the decode routine.
2569 		 */
2570 		if (native->n_curr + sizeof (int) > native->n_end)
2571 			return (EFAULT);
2572 
2573 		bzero(native->n_curr, sizeof (int));
2574 		native->n_curr += sizeof (int);
2575 	}
2576 
2577 	return (0);
2578 }
2579 
2580 static int
2581 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2582 {
2583 	if (nvs->nvs_op == NVS_OP_ENCODE) {
2584 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2585 		nvlist_t *packed = (void *)
2586 		    (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2587 		/*
2588 		 * Null out the pointer that is meaningless in the packed
2589 		 * structure. The address may not be aligned, so we have
2590 		 * to use bzero.
2591 		 */
2592 		bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2593 	}
2594 
2595 	return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2596 }
2597 
2598 static int
2599 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2600 {
2601 	if (nvs->nvs_op == NVS_OP_ENCODE) {
2602 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2603 		char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2604 		size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2605 		nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2606 		int i;
2607 		/*
2608 		 * Null out pointers that are meaningless in the packed
2609 		 * structure. The addresses may not be aligned, so we have
2610 		 * to use bzero.
2611 		 */
2612 		bzero(value, len);
2613 
2614 		for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2615 			/*
2616 			 * Null out the pointer that is meaningless in the
2617 			 * packed structure. The address may not be aligned,
2618 			 * so we have to use bzero.
2619 			 */
2620 			bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2621 	}
2622 
2623 	return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2624 }
2625 
2626 static void
2627 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2628 {
2629 	switch (nvs->nvs_op) {
2630 	case NVS_OP_ENCODE: {
2631 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2632 		uint64_t *strp = (void *)
2633 		    (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2634 		/*
2635 		 * Null out pointers that are meaningless in the packed
2636 		 * structure. The addresses may not be aligned, so we have
2637 		 * to use bzero.
2638 		 */
2639 		bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2640 		break;
2641 	}
2642 	case NVS_OP_DECODE: {
2643 		char **strp = (void *)NVP_VALUE(nvp);
2644 		char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2645 		int i;
2646 
2647 		for (i = 0; i < NVP_NELEM(nvp); i++) {
2648 			strp[i] = buf;
2649 			buf += strlen(buf) + 1;
2650 		}
2651 		break;
2652 	}
2653 	}
2654 }
2655 
2656 static int
2657 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2658 {
2659 	data_type_t type;
2660 	int value_sz;
2661 	int ret = 0;
2662 
2663 	/*
2664 	 * We do the initial bcopy of the data before we look at
2665 	 * the nvpair type, because when we're decoding, we won't
2666 	 * have the correct values for the pair until we do the bcopy.
2667 	 */
2668 	switch (nvs->nvs_op) {
2669 	case NVS_OP_ENCODE:
2670 	case NVS_OP_DECODE:
2671 		if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2672 			return (EFAULT);
2673 		break;
2674 	default:
2675 		return (EINVAL);
2676 	}
2677 
2678 	/* verify nvp_name_sz, check the name string length */
2679 	if (i_validate_nvpair_name(nvp) != 0)
2680 		return (EFAULT);
2681 
2682 	type = NVP_TYPE(nvp);
2683 
2684 	/*
2685 	 * Verify type and nelem and get the value size.
2686 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2687 	 * is the size of the string(s) excluded.
2688 	 */
2689 	if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2690 		return (EFAULT);
2691 
2692 	if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2693 		return (EFAULT);
2694 
2695 	switch (type) {
2696 	case DATA_TYPE_NVLIST:
2697 		ret = nvpair_native_embedded(nvs, nvp);
2698 		break;
2699 	case DATA_TYPE_NVLIST_ARRAY:
2700 		ret = nvpair_native_embedded_array(nvs, nvp);
2701 		break;
2702 	case DATA_TYPE_STRING_ARRAY:
2703 		nvpair_native_string_array(nvs, nvp);
2704 		break;
2705 	default:
2706 		break;
2707 	}
2708 
2709 	return (ret);
2710 }
2711 
2712 static int
2713 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2714 {
2715 	uint64_t nvp_sz = nvp->nvp_size;
2716 
2717 	switch (NVP_TYPE(nvp)) {
2718 	case DATA_TYPE_NVLIST: {
2719 		size_t nvsize = 0;
2720 
2721 		if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2722 			return (EINVAL);
2723 
2724 		nvp_sz += nvsize;
2725 		break;
2726 	}
2727 	case DATA_TYPE_NVLIST_ARRAY: {
2728 		size_t nvsize;
2729 
2730 		if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2731 			return (EINVAL);
2732 
2733 		nvp_sz += nvsize;
2734 		break;
2735 	}
2736 	default:
2737 		break;
2738 	}
2739 
2740 	if (nvp_sz > INT32_MAX)
2741 		return (EINVAL);
2742 
2743 	*size = nvp_sz;
2744 
2745 	return (0);
2746 }
2747 
2748 static int
2749 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2750 {
2751 	switch (nvs->nvs_op) {
2752 	case NVS_OP_ENCODE:
2753 		return (nvs_native_nvp_op(nvs, nvp));
2754 
2755 	case NVS_OP_DECODE: {
2756 		nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2757 		int32_t decode_len;
2758 
2759 		/* try to read the size value from the stream */
2760 		if (native->n_curr + sizeof (int32_t) > native->n_end)
2761 			return (EFAULT);
2762 		bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2763 
2764 		/* sanity check the size value */
2765 		if (decode_len < 0 ||
2766 		    decode_len > native->n_end - native->n_curr)
2767 			return (EFAULT);
2768 
2769 		*size = decode_len;
2770 
2771 		/*
2772 		 * If at the end of the stream then move the cursor
2773 		 * forward, otherwise nvpair_native_op() will read
2774 		 * the entire nvpair at the same cursor position.
2775 		 */
2776 		if (*size == 0)
2777 			native->n_curr += sizeof (int32_t);
2778 		break;
2779 	}
2780 
2781 	default:
2782 		return (EINVAL);
2783 	}
2784 
2785 	return (0);
2786 }
2787 
2788 static const nvs_ops_t nvs_native_ops = {
2789 	nvs_native_nvlist,
2790 	nvs_native_nvpair,
2791 	nvs_native_nvp_op,
2792 	nvs_native_nvp_size,
2793 	nvs_native_nvl_fini
2794 };
2795 
2796 static int
2797 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2798 {
2799 	nvs_native_t native;
2800 	int err;
2801 
2802 	nvs->nvs_ops = &nvs_native_ops;
2803 
2804 	if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2805 	    *buflen - sizeof (nvs_header_t))) != 0)
2806 		return (err);
2807 
2808 	err = nvs_operation(nvs, nvl, buflen);
2809 
2810 	nvs_native_destroy(nvs);
2811 
2812 	return (err);
2813 }
2814 
2815 /*
2816  * XDR encoding functions
2817  *
2818  * An xdr packed nvlist is encoded as:
2819  *
2820  *  - encoding methode and host endian (4 bytes)
2821  *  - nvl_version (4 bytes)
2822  *  - nvl_nvflag (4 bytes)
2823  *
2824  *  - encoded nvpairs, the format of one xdr encoded nvpair is:
2825  *	- encoded size of the nvpair (4 bytes)
2826  *	- decoded size of the nvpair (4 bytes)
2827  *	- name string, (4 + sizeof(NV_ALIGN4(string))
2828  *	  a string is coded as size (4 bytes) and data
2829  *	- data type (4 bytes)
2830  *	- number of elements in the nvpair (4 bytes)
2831  *	- data
2832  *
2833  *  - 2 zero's for end of the entire list (8 bytes)
2834  */
2835 static int
2836 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2837 {
2838 	/* xdr data must be 4 byte aligned */
2839 	if ((ulong_t)buf % 4 != 0)
2840 		return (EFAULT);
2841 
2842 	switch (nvs->nvs_op) {
2843 	case NVS_OP_ENCODE:
2844 		xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2845 		nvs->nvs_private = xdr;
2846 		return (0);
2847 	case NVS_OP_DECODE:
2848 		xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2849 		nvs->nvs_private = xdr;
2850 		return (0);
2851 	case NVS_OP_GETSIZE:
2852 		nvs->nvs_private = NULL;
2853 		return (0);
2854 	default:
2855 		return (EINVAL);
2856 	}
2857 }
2858 
2859 static void
2860 nvs_xdr_destroy(nvstream_t *nvs)
2861 {
2862 	switch (nvs->nvs_op) {
2863 	case NVS_OP_ENCODE:
2864 	case NVS_OP_DECODE:
2865 		xdr_destroy((XDR *)nvs->nvs_private);
2866 		break;
2867 	default:
2868 		break;
2869 	}
2870 }
2871 
2872 static int
2873 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2874 {
2875 	switch (nvs->nvs_op) {
2876 	case NVS_OP_ENCODE:
2877 	case NVS_OP_DECODE: {
2878 		XDR 	*xdr = nvs->nvs_private;
2879 
2880 		if (!xdr_int(xdr, &nvl->nvl_version) ||
2881 		    !xdr_u_int(xdr, &nvl->nvl_nvflag))
2882 			return (EFAULT);
2883 		break;
2884 	}
2885 	case NVS_OP_GETSIZE: {
2886 		/*
2887 		 * 2 * 4 for nvl_version + nvl_nvflag
2888 		 * and 8 for end of the entire list
2889 		 */
2890 		*size += 2 * 4 + 8;
2891 		break;
2892 	}
2893 	default:
2894 		return (EINVAL);
2895 	}
2896 	return (0);
2897 }
2898 
2899 static int
2900 nvs_xdr_nvl_fini(nvstream_t *nvs)
2901 {
2902 	if (nvs->nvs_op == NVS_OP_ENCODE) {
2903 		XDR *xdr = nvs->nvs_private;
2904 		int zero = 0;
2905 
2906 		if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2907 			return (EFAULT);
2908 	}
2909 
2910 	return (0);
2911 }
2912 
2913 /*
2914  * The format of xdr encoded nvpair is:
2915  * encode_size, decode_size, name string, data type, nelem, data
2916  */
2917 static int
2918 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2919 {
2920 	data_type_t type;
2921 	char	*buf;
2922 	char	*buf_end = (char *)nvp + nvp->nvp_size;
2923 	int	value_sz;
2924 	uint_t	nelem, buflen;
2925 	bool_t	ret = FALSE;
2926 	XDR	*xdr = nvs->nvs_private;
2927 
2928 	ASSERT(xdr != NULL && nvp != NULL);
2929 
2930 	/* name string */
2931 	if ((buf = NVP_NAME(nvp)) >= buf_end)
2932 		return (EFAULT);
2933 	buflen = buf_end - buf;
2934 
2935 	if (!xdr_string(xdr, &buf, buflen - 1))
2936 		return (EFAULT);
2937 	nvp->nvp_name_sz = strlen(buf) + 1;
2938 
2939 	/* type and nelem */
2940 	if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2941 	    !xdr_int(xdr, &nvp->nvp_value_elem))
2942 		return (EFAULT);
2943 
2944 	type = NVP_TYPE(nvp);
2945 	nelem = nvp->nvp_value_elem;
2946 
2947 	/*
2948 	 * Verify type and nelem and get the value size.
2949 	 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2950 	 * is the size of the string(s) excluded.
2951 	 */
2952 	if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2953 		return (EFAULT);
2954 
2955 	/* if there is no data to extract then return */
2956 	if (nelem == 0)
2957 		return (0);
2958 
2959 	/* value */
2960 	if ((buf = NVP_VALUE(nvp)) >= buf_end)
2961 		return (EFAULT);
2962 	buflen = buf_end - buf;
2963 
2964 	if (buflen < value_sz)
2965 		return (EFAULT);
2966 
2967 	switch (type) {
2968 	case DATA_TYPE_NVLIST:
2969 		if (nvs_embedded(nvs, (void *)buf) == 0)
2970 			return (0);
2971 		break;
2972 
2973 	case DATA_TYPE_NVLIST_ARRAY:
2974 		if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2975 			return (0);
2976 		break;
2977 
2978 	case DATA_TYPE_BOOLEAN:
2979 		ret = TRUE;
2980 		break;
2981 
2982 	case DATA_TYPE_BYTE:
2983 	case DATA_TYPE_INT8:
2984 	case DATA_TYPE_UINT8:
2985 		ret = xdr_char(xdr, buf);
2986 		break;
2987 
2988 	case DATA_TYPE_INT16:
2989 		ret = xdr_short(xdr, (void *)buf);
2990 		break;
2991 
2992 	case DATA_TYPE_UINT16:
2993 		ret = xdr_u_short(xdr, (void *)buf);
2994 		break;
2995 
2996 	case DATA_TYPE_BOOLEAN_VALUE:
2997 	case DATA_TYPE_INT32:
2998 		ret = xdr_int(xdr, (void *)buf);
2999 		break;
3000 
3001 	case DATA_TYPE_UINT32:
3002 		ret = xdr_u_int(xdr, (void *)buf);
3003 		break;
3004 
3005 	case DATA_TYPE_INT64:
3006 		ret = xdr_longlong_t(xdr, (void *)buf);
3007 		break;
3008 
3009 	case DATA_TYPE_UINT64:
3010 		ret = xdr_u_longlong_t(xdr, (void *)buf);
3011 		break;
3012 
3013 	case DATA_TYPE_HRTIME:
3014 		/*
3015 		 * NOTE: must expose the definition of hrtime_t here
3016 		 */
3017 		ret = xdr_longlong_t(xdr, (void *)buf);
3018 		break;
3019 #if !defined(_KERNEL)
3020 	case DATA_TYPE_DOUBLE:
3021 		ret = xdr_double(xdr, (void *)buf);
3022 		break;
3023 #endif
3024 	case DATA_TYPE_STRING:
3025 		ret = xdr_string(xdr, &buf, buflen - 1);
3026 		break;
3027 
3028 	case DATA_TYPE_BYTE_ARRAY:
3029 		ret = xdr_opaque(xdr, buf, nelem);
3030 		break;
3031 
3032 	case DATA_TYPE_INT8_ARRAY:
3033 	case DATA_TYPE_UINT8_ARRAY:
3034 		ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3035 		    (xdrproc_t)xdr_char);
3036 		break;
3037 
3038 	case DATA_TYPE_INT16_ARRAY:
3039 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3040 		    sizeof (int16_t), (xdrproc_t)xdr_short);
3041 		break;
3042 
3043 	case DATA_TYPE_UINT16_ARRAY:
3044 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3045 		    sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3046 		break;
3047 
3048 	case DATA_TYPE_BOOLEAN_ARRAY:
3049 	case DATA_TYPE_INT32_ARRAY:
3050 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3051 		    sizeof (int32_t), (xdrproc_t)xdr_int);
3052 		break;
3053 
3054 	case DATA_TYPE_UINT32_ARRAY:
3055 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3056 		    sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3057 		break;
3058 
3059 	case DATA_TYPE_INT64_ARRAY:
3060 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3061 		    sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3062 		break;
3063 
3064 	case DATA_TYPE_UINT64_ARRAY:
3065 		ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3066 		    sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3067 		break;
3068 
3069 	case DATA_TYPE_STRING_ARRAY: {
3070 		size_t len = nelem * sizeof (uint64_t);
3071 		char **strp = (void *)buf;
3072 		int i;
3073 
3074 		if (nvs->nvs_op == NVS_OP_DECODE)
3075 			bzero(buf, len);	/* don't trust packed data */
3076 
3077 		for (i = 0; i < nelem; i++) {
3078 			if (buflen <= len)
3079 				return (EFAULT);
3080 
3081 			buf += len;
3082 			buflen -= len;
3083 
3084 			if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3085 				return (EFAULT);
3086 
3087 			if (nvs->nvs_op == NVS_OP_DECODE)
3088 				strp[i] = buf;
3089 			len = strlen(buf) + 1;
3090 		}
3091 		ret = TRUE;
3092 		break;
3093 	}
3094 	default:
3095 		break;
3096 	}
3097 
3098 	return (ret == TRUE ? 0 : EFAULT);
3099 }
3100 
3101 static int
3102 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3103 {
3104 	data_type_t type = NVP_TYPE(nvp);
3105 	/*
3106 	 * encode_size + decode_size + name string size + data type + nelem
3107 	 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3108 	 */
3109 	uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3110 
3111 	switch (type) {
3112 	case DATA_TYPE_BOOLEAN:
3113 		break;
3114 
3115 	case DATA_TYPE_BOOLEAN_VALUE:
3116 	case DATA_TYPE_BYTE:
3117 	case DATA_TYPE_INT8:
3118 	case DATA_TYPE_UINT8:
3119 	case DATA_TYPE_INT16:
3120 	case DATA_TYPE_UINT16:
3121 	case DATA_TYPE_INT32:
3122 	case DATA_TYPE_UINT32:
3123 		nvp_sz += 4;	/* 4 is the minimum xdr unit */
3124 		break;
3125 
3126 	case DATA_TYPE_INT64:
3127 	case DATA_TYPE_UINT64:
3128 	case DATA_TYPE_HRTIME:
3129 #if !defined(_KERNEL)
3130 	case DATA_TYPE_DOUBLE:
3131 #endif
3132 		nvp_sz += 8;
3133 		break;
3134 
3135 	case DATA_TYPE_STRING:
3136 		nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3137 		break;
3138 
3139 	case DATA_TYPE_BYTE_ARRAY:
3140 		nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3141 		break;
3142 
3143 	case DATA_TYPE_BOOLEAN_ARRAY:
3144 	case DATA_TYPE_INT8_ARRAY:
3145 	case DATA_TYPE_UINT8_ARRAY:
3146 	case DATA_TYPE_INT16_ARRAY:
3147 	case DATA_TYPE_UINT16_ARRAY:
3148 	case DATA_TYPE_INT32_ARRAY:
3149 	case DATA_TYPE_UINT32_ARRAY:
3150 		nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3151 		break;
3152 
3153 	case DATA_TYPE_INT64_ARRAY:
3154 	case DATA_TYPE_UINT64_ARRAY:
3155 		nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3156 		break;
3157 
3158 	case DATA_TYPE_STRING_ARRAY: {
3159 		int i;
3160 		char **strs = (void *)NVP_VALUE(nvp);
3161 
3162 		for (i = 0; i < NVP_NELEM(nvp); i++)
3163 			nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3164 
3165 		break;
3166 	}
3167 
3168 	case DATA_TYPE_NVLIST:
3169 	case DATA_TYPE_NVLIST_ARRAY: {
3170 		size_t nvsize = 0;
3171 		int old_nvs_op = nvs->nvs_op;
3172 		int err;
3173 
3174 		nvs->nvs_op = NVS_OP_GETSIZE;
3175 		if (type == DATA_TYPE_NVLIST)
3176 			err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3177 		else
3178 			err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3179 		nvs->nvs_op = old_nvs_op;
3180 
3181 		if (err != 0)
3182 			return (EINVAL);
3183 
3184 		nvp_sz += nvsize;
3185 		break;
3186 	}
3187 
3188 	default:
3189 		return (EINVAL);
3190 	}
3191 
3192 	if (nvp_sz > INT32_MAX)
3193 		return (EINVAL);
3194 
3195 	*size = nvp_sz;
3196 
3197 	return (0);
3198 }
3199 
3200 
3201 /*
3202  * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3203  * the largest nvpair that could be encoded in the buffer.
3204  *
3205  * See comments above nvpair_xdr_op() for the format of xdr encoding.
3206  * The size of a xdr packed nvpair without any data is 5 words.
3207  *
3208  * Using the size of the data directly as an estimate would be ok
3209  * in all cases except one.  If the data type is of DATA_TYPE_STRING_ARRAY
3210  * then the actual nvpair has space for an array of pointers to index
3211  * the strings.  These pointers are not encoded into the packed xdr buffer.
3212  *
3213  * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3214  * of length 0, then each string is endcoded in xdr format as a single word.
3215  * Therefore when expanded to an nvpair there will be 2.25 word used for
3216  * each string.  (a int64_t allocated for pointer usage, and a single char
3217  * for the null termination.)
3218  *
3219  * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3220  */
3221 #define	NVS_XDR_HDR_LEN		((size_t)(5 * 4))
3222 #define	NVS_XDR_DATA_LEN(y)	(((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3223 					0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3224 #define	NVS_XDR_MAX_LEN(x)	(NVP_SIZE_CALC(1, 0) + \
3225 					(NVS_XDR_DATA_LEN(x) * 2) + \
3226 					NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3227 
3228 static int
3229 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3230 {
3231 	XDR 	*xdr = nvs->nvs_private;
3232 	int32_t	encode_len, decode_len;
3233 
3234 	switch (nvs->nvs_op) {
3235 	case NVS_OP_ENCODE: {
3236 		size_t nvsize;
3237 
3238 		if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3239 			return (EFAULT);
3240 
3241 		decode_len = nvp->nvp_size;
3242 		encode_len = nvsize;
3243 		if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3244 			return (EFAULT);
3245 
3246 		return (nvs_xdr_nvp_op(nvs, nvp));
3247 	}
3248 	case NVS_OP_DECODE: {
3249 		struct xdr_bytesrec bytesrec;
3250 
3251 		/* get the encode and decode size */
3252 		if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3253 			return (EFAULT);
3254 		*size = decode_len;
3255 
3256 		/* are we at the end of the stream? */
3257 		if (*size == 0)
3258 			return (0);
3259 
3260 		/* sanity check the size parameter */
3261 		if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3262 			return (EFAULT);
3263 
3264 		if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3265 			return (EFAULT);
3266 		break;
3267 	}
3268 
3269 	default:
3270 		return (EINVAL);
3271 	}
3272 	return (0);
3273 }
3274 
3275 static const struct nvs_ops nvs_xdr_ops = {
3276 	nvs_xdr_nvlist,
3277 	nvs_xdr_nvpair,
3278 	nvs_xdr_nvp_op,
3279 	nvs_xdr_nvp_size,
3280 	nvs_xdr_nvl_fini
3281 };
3282 
3283 static int
3284 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3285 {
3286 	XDR xdr;
3287 	int err;
3288 
3289 	nvs->nvs_ops = &nvs_xdr_ops;
3290 
3291 	if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3292 	    *buflen - sizeof (nvs_header_t))) != 0)
3293 		return (err);
3294 
3295 	err = nvs_operation(nvs, nvl, buflen);
3296 
3297 	nvs_xdr_destroy(nvs);
3298 
3299 	return (err);
3300 }
3301