xref: /freebsd/sys/contrib/libnv/nvlist.c (revision c1d255d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2013 The FreeBSD Foundation
5  * Copyright (c) 2013-2015 Mariusz Zaborski <oshogbo@FreeBSD.org>
6  * All rights reserved.
7  *
8  * This software was developed by Pawel Jakub Dawidek under sponsorship from
9  * the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/endian.h>
38 #include <sys/queue.h>
39 
40 #ifdef _KERNEL
41 
42 #include <sys/errno.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 
48 #include <machine/stdarg.h>
49 
50 #else
51 #include <sys/socket.h>
52 
53 #include <errno.h>
54 #include <stdarg.h>
55 #include <stdbool.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 
62 #include "msgio.h"
63 #endif
64 
65 #ifdef HAVE_PJDLOG
66 #include <pjdlog.h>
67 #endif
68 
69 #include <sys/nv.h>
70 
71 #include "nv_impl.h"
72 #include "nvlist_impl.h"
73 #include "nvpair_impl.h"
74 
75 #ifndef	HAVE_PJDLOG
76 #ifdef _KERNEL
77 #define	PJDLOG_ASSERT(...)		MPASS(__VA_ARGS__)
78 #define	PJDLOG_RASSERT(expr, ...)	KASSERT(expr, (__VA_ARGS__))
79 #define	PJDLOG_ABORT(...)		panic(__VA_ARGS__)
80 #else
81 #include <assert.h>
82 #define	PJDLOG_ASSERT(...)		assert(__VA_ARGS__)
83 #define	PJDLOG_RASSERT(expr, ...)	assert(expr)
84 #define	PJDLOG_ABORT(...)		do {				\
85 	fprintf(stderr, "%s:%u: ", __FILE__, __LINE__);			\
86 	fprintf(stderr, __VA_ARGS__);					\
87 	fprintf(stderr, "\n");						\
88 	abort();							\
89 } while (0)
90 #endif
91 #endif
92 
93 #define	NV_FLAG_PRIVATE_MASK	(NV_FLAG_BIG_ENDIAN | NV_FLAG_IN_ARRAY)
94 #define	NV_FLAG_PUBLIC_MASK	(NV_FLAG_IGNORE_CASE | NV_FLAG_NO_UNIQUE)
95 #define	NV_FLAG_ALL_MASK	(NV_FLAG_PRIVATE_MASK | NV_FLAG_PUBLIC_MASK)
96 
97 #define	NVLIST_MAGIC	0x6e766c	/* "nvl" */
98 struct nvlist {
99 	int		 nvl_magic;
100 	int		 nvl_error;
101 	int		 nvl_flags;
102 	size_t		 nvl_datasize;
103 	nvpair_t	*nvl_parent;
104 	nvpair_t	*nvl_array_next;
105 	struct nvl_head	 nvl_head;
106 };
107 
108 #define	NVLIST_ASSERT(nvl)	do {					\
109 	PJDLOG_ASSERT((nvl) != NULL);					\
110 	PJDLOG_ASSERT((nvl)->nvl_magic == NVLIST_MAGIC);		\
111 } while (0)
112 
113 #ifdef _KERNEL
114 MALLOC_DEFINE(M_NVLIST, "nvlist", "kernel nvlist");
115 #endif
116 
117 #define	NVPAIR_ASSERT(nvp)	nvpair_assert(nvp)
118 
119 #define	NVLIST_HEADER_MAGIC	0x6c
120 #define	NVLIST_HEADER_VERSION	0x00
121 struct nvlist_header {
122 	uint8_t		nvlh_magic;
123 	uint8_t		nvlh_version;
124 	uint8_t		nvlh_flags;
125 	uint64_t	nvlh_descriptors;
126 	uint64_t	nvlh_size;
127 } __packed;
128 
129 nvlist_t *
130 nvlist_create(int flags)
131 {
132 	nvlist_t *nvl;
133 
134 	PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
135 
136 	nvl = nv_malloc(sizeof(*nvl));
137 	if (nvl == NULL)
138 		return (NULL);
139 	nvl->nvl_error = 0;
140 	nvl->nvl_flags = flags;
141 	nvl->nvl_parent = NULL;
142 	nvl->nvl_array_next = NULL;
143 	nvl->nvl_datasize = sizeof(struct nvlist_header);
144 	TAILQ_INIT(&nvl->nvl_head);
145 	nvl->nvl_magic = NVLIST_MAGIC;
146 
147 	return (nvl);
148 }
149 
150 void
151 nvlist_destroy(nvlist_t *nvl)
152 {
153 	nvpair_t *nvp;
154 
155 	if (nvl == NULL)
156 		return;
157 
158 	ERRNO_SAVE();
159 
160 	NVLIST_ASSERT(nvl);
161 
162 	while ((nvp = nvlist_first_nvpair(nvl)) != NULL) {
163 		nvlist_remove_nvpair(nvl, nvp);
164 		nvpair_free(nvp);
165 	}
166 	if (nvl->nvl_array_next != NULL)
167 		nvpair_free_structure(nvl->nvl_array_next);
168 	nvl->nvl_array_next = NULL;
169 	nvl->nvl_parent = NULL;
170 	nvl->nvl_magic = 0;
171 	nv_free(nvl);
172 
173 	ERRNO_RESTORE();
174 }
175 
176 void
177 nvlist_set_error(nvlist_t *nvl, int error)
178 {
179 
180 	PJDLOG_ASSERT(error != 0);
181 
182 	/*
183 	 * Check for error != 0 so that we don't do the wrong thing if somebody
184 	 * tries to abuse this API when asserts are disabled.
185 	 */
186 	if (nvl != NULL && error != 0 && nvl->nvl_error == 0)
187 		nvl->nvl_error = error;
188 }
189 
190 int
191 nvlist_error(const nvlist_t *nvl)
192 {
193 
194 	if (nvl == NULL)
195 		return (ENOMEM);
196 
197 	NVLIST_ASSERT(nvl);
198 
199 	return (nvl->nvl_error);
200 }
201 
202 nvpair_t *
203 nvlist_get_nvpair_parent(const nvlist_t *nvl)
204 {
205 
206 	NVLIST_ASSERT(nvl);
207 
208 	return (nvl->nvl_parent);
209 }
210 
211 const nvlist_t *
212 nvlist_get_parent(const nvlist_t *nvl, void **cookiep)
213 {
214 	nvpair_t *nvp;
215 
216 	NVLIST_ASSERT(nvl);
217 
218 	nvp = nvl->nvl_parent;
219 	if (cookiep != NULL)
220 		*cookiep = nvp;
221 	if (nvp == NULL)
222 		return (NULL);
223 
224 	return (nvpair_nvlist(nvp));
225 }
226 
227 void
228 nvlist_set_parent(nvlist_t *nvl, nvpair_t *parent)
229 {
230 
231 	NVLIST_ASSERT(nvl);
232 
233 	nvl->nvl_parent = parent;
234 }
235 
236 void
237 nvlist_set_array_next(nvlist_t *nvl, nvpair_t *ele)
238 {
239 
240 	NVLIST_ASSERT(nvl);
241 
242 	if (ele != NULL) {
243 		nvl->nvl_flags |= NV_FLAG_IN_ARRAY;
244 	} else {
245 		nvl->nvl_flags &= ~NV_FLAG_IN_ARRAY;
246 		nv_free(nvl->nvl_array_next);
247 	}
248 
249 	nvl->nvl_array_next = ele;
250 }
251 
252 static void
253 nvlist_update_size(nvlist_t *nvl, nvpair_t *new, ssize_t mul)
254 {
255 	ssize_t size;
256 	size_t nitems;
257 	const nvlist_t *nvlistnew;
258 	const nvlist_t * const *nvlarray;
259 	nvlist_t *parent;
260 	unsigned int ii;
261 
262 	NVLIST_ASSERT(nvl);
263 	NVPAIR_ASSERT(new);
264 	PJDLOG_ASSERT(mul == 1 || mul == -1);
265 
266 	size = nvpair_header_size();
267 	size += strlen(nvpair_name(new)) + 1;
268 
269 	if (nvpair_type(new) == NV_TYPE_NVLIST) {
270 		nvlistnew = nvpair_get_nvlist(new);
271 		size += nvlistnew->nvl_datasize;
272 		size += nvpair_header_size() + 1;
273 	} else if (nvpair_type(new) == NV_TYPE_NVLIST_ARRAY) {
274 		nvlarray = nvpair_get_nvlist_array(new, &nitems);
275 		PJDLOG_ASSERT(nitems > 0);
276 
277 		size += (nvpair_header_size() + 1) * nitems;
278 		for (ii = 0; ii < nitems; ii++) {
279 			PJDLOG_ASSERT(nvlarray[ii]->nvl_error == 0);
280 			size += nvlarray[ii]->nvl_datasize;
281 		}
282 	} else {
283 		size += nvpair_size(new);
284 	}
285 
286 	size *= mul;
287 
288 	nvl->nvl_datasize += size;
289 
290 	parent = nvl;
291 	while ((parent = __DECONST(nvlist_t *,
292 	    nvlist_get_parent(parent, NULL))) != NULL) {
293 		parent->nvl_datasize += size;
294 	}
295 }
296 
297 nvpair_t *
298 nvlist_get_array_next_nvpair(nvlist_t *nvl)
299 {
300 
301 	NVLIST_ASSERT(nvl);
302 
303 	return (nvl->nvl_array_next);
304 }
305 
306 bool
307 nvlist_in_array(const nvlist_t *nvl)
308 {
309 
310 	NVLIST_ASSERT(nvl);
311 
312 	return ((nvl->nvl_flags & NV_FLAG_IN_ARRAY) != 0);
313 }
314 
315 const nvlist_t *
316 nvlist_get_array_next(const nvlist_t *nvl)
317 {
318 	nvpair_t *nvp;
319 
320 	NVLIST_ASSERT(nvl);
321 
322 	nvp = nvl->nvl_array_next;
323 	if (nvp == NULL)
324 		return (NULL);
325 
326 	return (nvpair_get_nvlist(nvp));
327 }
328 
329 const nvlist_t *
330 nvlist_get_pararr(const nvlist_t *nvl, void **cookiep)
331 {
332 	const nvlist_t *ret;
333 
334 	ret = nvlist_get_array_next(nvl);
335 	if (ret != NULL) {
336 		if (cookiep != NULL)
337 			*cookiep = NULL;
338 		return (ret);
339 	}
340 
341 	return (nvlist_get_parent(nvl, cookiep));
342 }
343 
344 bool
345 nvlist_empty(const nvlist_t *nvl)
346 {
347 
348 	NVLIST_ASSERT(nvl);
349 	PJDLOG_ASSERT(nvl->nvl_error == 0);
350 
351 	return (nvlist_first_nvpair(nvl) == NULL);
352 }
353 
354 int
355 nvlist_flags(const nvlist_t *nvl)
356 {
357 
358 	NVLIST_ASSERT(nvl);
359 	PJDLOG_ASSERT(nvl->nvl_error == 0);
360 
361 	return (nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
362 }
363 
364 void
365 nvlist_set_flags(nvlist_t *nvl, int flags)
366 {
367 
368 	NVLIST_ASSERT(nvl);
369 	PJDLOG_ASSERT(nvl->nvl_error == 0);
370 
371 	nvl->nvl_flags = flags;
372 }
373 
374 void
375 nvlist_report_missing(int type, const char *name)
376 {
377 
378 	PJDLOG_ABORT("Element '%s' of type %s doesn't exist.",
379 	    name, nvpair_type_string(type));
380 }
381 
382 static nvpair_t *
383 nvlist_find(const nvlist_t *nvl, int type, const char *name)
384 {
385 	nvpair_t *nvp;
386 
387 	NVLIST_ASSERT(nvl);
388 	PJDLOG_ASSERT(nvl->nvl_error == 0);
389 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
390 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
391 
392 	for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
393 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
394 		if (type != NV_TYPE_NONE && nvpair_type(nvp) != type)
395 			continue;
396 		if ((nvl->nvl_flags & NV_FLAG_IGNORE_CASE) != 0) {
397 			if (strcasecmp(nvpair_name(nvp), name) != 0)
398 				continue;
399 		} else {
400 			if (strcmp(nvpair_name(nvp), name) != 0)
401 				continue;
402 		}
403 		break;
404 	}
405 
406 	if (nvp == NULL)
407 		ERRNO_SET(ENOENT);
408 
409 	return (nvp);
410 }
411 
412 bool
413 nvlist_exists_type(const nvlist_t *nvl, const char *name, int type)
414 {
415 
416 	NVLIST_ASSERT(nvl);
417 	PJDLOG_ASSERT(nvl->nvl_error == 0);
418 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
419 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
420 
421 	return (nvlist_find(nvl, type, name) != NULL);
422 }
423 
424 void
425 nvlist_free_type(nvlist_t *nvl, const char *name, int type)
426 {
427 	nvpair_t *nvp;
428 
429 	NVLIST_ASSERT(nvl);
430 	PJDLOG_ASSERT(nvl->nvl_error == 0);
431 	PJDLOG_ASSERT(type == NV_TYPE_NONE ||
432 	    (type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST));
433 
434 	nvp = nvlist_find(nvl, type, name);
435 	if (nvp != NULL)
436 		nvlist_free_nvpair(nvl, nvp);
437 	else
438 		nvlist_report_missing(type, name);
439 }
440 
441 nvlist_t *
442 nvlist_clone(const nvlist_t *nvl)
443 {
444 	nvlist_t *newnvl;
445 	nvpair_t *nvp, *newnvp;
446 
447 	NVLIST_ASSERT(nvl);
448 
449 	if (nvl->nvl_error != 0) {
450 		ERRNO_SET(nvl->nvl_error);
451 		return (NULL);
452 	}
453 
454 	newnvl = nvlist_create(nvl->nvl_flags & NV_FLAG_PUBLIC_MASK);
455 	for (nvp = nvlist_first_nvpair(nvl); nvp != NULL;
456 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
457 		newnvp = nvpair_clone(nvp);
458 		if (newnvp == NULL)
459 			break;
460 		(void)nvlist_move_nvpair(newnvl, newnvp);
461 	}
462 	if (nvp != NULL) {
463 		nvlist_destroy(newnvl);
464 		return (NULL);
465 	}
466 	return (newnvl);
467 }
468 
469 #ifndef _KERNEL
470 static bool
471 nvlist_dump_error_check(const nvlist_t *nvl, int fd, int level)
472 {
473 
474 	if (nvlist_error(nvl) != 0) {
475 		dprintf(fd, "%*serror: %d\n", level * 4, "",
476 		    nvlist_error(nvl));
477 		return (true);
478 	}
479 
480 	return (false);
481 }
482 
483 /*
484  * Dump content of nvlist.
485  */
486 void
487 nvlist_dump(const nvlist_t *nvl, int fd)
488 {
489 	const nvlist_t *tmpnvl;
490 	nvpair_t *nvp, *tmpnvp;
491 	void *cookie;
492 	int level;
493 
494 	level = 0;
495 	if (nvlist_dump_error_check(nvl, fd, level))
496 		return;
497 
498 	nvp = nvlist_first_nvpair(nvl);
499 	while (nvp != NULL) {
500 		dprintf(fd, "%*s%s (%s):", level * 4, "", nvpair_name(nvp),
501 		    nvpair_type_string(nvpair_type(nvp)));
502 		switch (nvpair_type(nvp)) {
503 		case NV_TYPE_NULL:
504 			dprintf(fd, " null\n");
505 			break;
506 		case NV_TYPE_BOOL:
507 			dprintf(fd, " %s\n", nvpair_get_bool(nvp) ?
508 			    "TRUE" : "FALSE");
509 			break;
510 		case NV_TYPE_NUMBER:
511 			dprintf(fd, " %ju (%jd) (0x%jx)\n",
512 			    (uintmax_t)nvpair_get_number(nvp),
513 			    (intmax_t)nvpair_get_number(nvp),
514 			    (uintmax_t)nvpair_get_number(nvp));
515 			break;
516 		case NV_TYPE_STRING:
517 			dprintf(fd, " [%s]\n", nvpair_get_string(nvp));
518 			break;
519 		case NV_TYPE_NVLIST:
520 			dprintf(fd, "\n");
521 			tmpnvl = nvpair_get_nvlist(nvp);
522 			if (nvlist_dump_error_check(tmpnvl, fd, level + 1))
523 				break;
524 			tmpnvp = nvlist_first_nvpair(tmpnvl);
525 			if (tmpnvp != NULL) {
526 				nvl = tmpnvl;
527 				nvp = tmpnvp;
528 				level++;
529 				continue;
530 			}
531 			break;
532 		case NV_TYPE_DESCRIPTOR:
533 			dprintf(fd, " %d\n", nvpair_get_descriptor(nvp));
534 			break;
535 		case NV_TYPE_BINARY:
536 		    {
537 			const unsigned char *binary;
538 			unsigned int ii;
539 			size_t size;
540 
541 			binary = nvpair_get_binary(nvp, &size);
542 			dprintf(fd, " %zu ", size);
543 			for (ii = 0; ii < size; ii++)
544 				dprintf(fd, "%02hhx", binary[ii]);
545 			dprintf(fd, "\n");
546 			break;
547 		    }
548 		case NV_TYPE_BOOL_ARRAY:
549 		    {
550 			const bool *value;
551 			unsigned int ii;
552 			size_t nitems;
553 
554 			value = nvpair_get_bool_array(nvp, &nitems);
555 			dprintf(fd, " [ ");
556 			for (ii = 0; ii < nitems; ii++) {
557 				dprintf(fd, "%s", value[ii] ? "TRUE" : "FALSE");
558 				if (ii != nitems - 1)
559 					dprintf(fd, ", ");
560 			}
561 			dprintf(fd, " ]\n");
562 			break;
563 		    }
564 		case NV_TYPE_STRING_ARRAY:
565 		    {
566 			const char * const *value;
567 			unsigned int ii;
568 			size_t nitems;
569 
570 			value = nvpair_get_string_array(nvp, &nitems);
571 			dprintf(fd, " [ ");
572 			for (ii = 0; ii < nitems; ii++) {
573 				if (value[ii] == NULL)
574 					dprintf(fd, "NULL");
575 				else
576 					dprintf(fd, "\"%s\"", value[ii]);
577 				if (ii != nitems - 1)
578 					dprintf(fd, ", ");
579 			}
580 			dprintf(fd, " ]\n");
581 			break;
582 		    }
583 		case NV_TYPE_NUMBER_ARRAY:
584 		    {
585 			const uint64_t *value;
586 			unsigned int ii;
587 			size_t nitems;
588 
589 			value = nvpair_get_number_array(nvp, &nitems);
590 			dprintf(fd, " [ ");
591 			for (ii = 0; ii < nitems; ii++) {
592 				dprintf(fd, "%ju (%jd) (0x%jx)",
593 				    value[ii], value[ii], value[ii]);
594 				if (ii != nitems - 1)
595 					dprintf(fd, ", ");
596 			}
597 			dprintf(fd, " ]\n");
598 			break;
599 		    }
600 		case NV_TYPE_DESCRIPTOR_ARRAY:
601 		    {
602 			const int *value;
603 			unsigned int ii;
604 			size_t nitems;
605 
606 			value = nvpair_get_descriptor_array(nvp, &nitems);
607 			dprintf(fd, " [ ");
608 			for (ii = 0; ii < nitems; ii++) {
609 				dprintf(fd, "%d", value[ii]);
610 				if (ii != nitems - 1)
611 					dprintf(fd, ", ");
612 			}
613 			dprintf(fd, " ]\n");
614 			break;
615 		    }
616 		case NV_TYPE_NVLIST_ARRAY:
617 		    {
618 			const nvlist_t * const *value;
619 			unsigned int ii;
620 			size_t nitems;
621 
622 			value = nvpair_get_nvlist_array(nvp, &nitems);
623 			dprintf(fd, " %zu\n", nitems);
624 			tmpnvl = NULL;
625 			tmpnvp = NULL;
626 			for (ii = 0; ii < nitems; ii++) {
627 				if (nvlist_dump_error_check(value[ii], fd,
628 				    level + 1)) {
629 					break;
630 				}
631 
632 				if (tmpnvl == NULL) {
633 					tmpnvp = nvlist_first_nvpair(value[ii]);
634 					if (tmpnvp != NULL) {
635 						tmpnvl = value[ii];
636 					} else {
637 						dprintf(fd, "%*s,\n",
638 						    (level + 1) * 4, "");
639 					}
640 				}
641 			}
642 			if (tmpnvp != NULL) {
643 				nvl = tmpnvl;
644 				nvp = tmpnvp;
645 				level++;
646 				continue;
647 			}
648 			break;
649 		    }
650 		default:
651 			PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
652 		}
653 
654 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
655 			do {
656 				cookie = NULL;
657 				if (nvlist_in_array(nvl))
658 					dprintf(fd, "%*s,\n", level * 4, "");
659 				nvl = nvlist_get_pararr(nvl, &cookie);
660 				if (nvl == NULL)
661 					return;
662 				if (nvlist_in_array(nvl) && cookie == NULL) {
663 					nvp = nvlist_first_nvpair(nvl);
664 				} else {
665 					nvp = cookie;
666 					level--;
667 				}
668 			} while (nvp == NULL);
669 			if (nvlist_in_array(nvl) && cookie == NULL)
670 				break;
671 		}
672 	}
673 }
674 
675 void
676 nvlist_fdump(const nvlist_t *nvl, FILE *fp)
677 {
678 
679 	fflush(fp);
680 	nvlist_dump(nvl, fileno(fp));
681 }
682 #endif
683 
684 /*
685  * The function obtains size of the nvlist after nvlist_pack().
686  */
687 size_t
688 nvlist_size(const nvlist_t *nvl)
689 {
690 
691 	return (nvl->nvl_datasize);
692 }
693 
694 #ifndef _KERNEL
695 static int *
696 nvlist_xdescriptors(const nvlist_t *nvl, int *descs)
697 {
698 	void *cookie;
699 	nvpair_t *nvp;
700 	int type;
701 
702 	NVLIST_ASSERT(nvl);
703 	PJDLOG_ASSERT(nvl->nvl_error == 0);
704 
705 	cookie = NULL;
706 	do {
707 		while (nvlist_next(nvl, &type, &cookie) != NULL) {
708 			nvp = cookie;
709 			switch (type) {
710 			case NV_TYPE_DESCRIPTOR:
711 				*descs = nvpair_get_descriptor(nvp);
712 				descs++;
713 				break;
714 			case NV_TYPE_DESCRIPTOR_ARRAY:
715 			    {
716 				const int *value;
717 				size_t nitems;
718 				unsigned int ii;
719 
720 				value = nvpair_get_descriptor_array(nvp,
721 				    &nitems);
722 				for (ii = 0; ii < nitems; ii++) {
723 					*descs = value[ii];
724 					descs++;
725 				}
726 				break;
727 			    }
728 			case NV_TYPE_NVLIST:
729 				nvl = nvpair_get_nvlist(nvp);
730 				cookie = NULL;
731 				break;
732 			case NV_TYPE_NVLIST_ARRAY:
733 			    {
734 				const nvlist_t * const *value;
735 				size_t nitems;
736 
737 				value = nvpair_get_nvlist_array(nvp, &nitems);
738 				PJDLOG_ASSERT(value != NULL);
739 				PJDLOG_ASSERT(nitems > 0);
740 
741 				nvl = value[0];
742 				cookie = NULL;
743 				break;
744 			    }
745 			}
746 		}
747 	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
748 
749 	return (descs);
750 }
751 #endif
752 
753 #ifndef _KERNEL
754 int *
755 nvlist_descriptors(const nvlist_t *nvl, size_t *nitemsp)
756 {
757 	size_t nitems;
758 	int *fds;
759 
760 	nitems = nvlist_ndescriptors(nvl);
761 	fds = nv_malloc(sizeof(fds[0]) * (nitems + 1));
762 	if (fds == NULL)
763 		return (NULL);
764 	if (nitems > 0)
765 		nvlist_xdescriptors(nvl, fds);
766 	fds[nitems] = -1;
767 	if (nitemsp != NULL)
768 		*nitemsp = nitems;
769 	return (fds);
770 }
771 #endif
772 
773 size_t
774 nvlist_ndescriptors(const nvlist_t *nvl)
775 {
776 #ifndef _KERNEL
777 	void *cookie;
778 	nvpair_t *nvp;
779 	size_t ndescs;
780 	int type;
781 
782 	NVLIST_ASSERT(nvl);
783 	PJDLOG_ASSERT(nvl->nvl_error == 0);
784 
785 	ndescs = 0;
786 	cookie = NULL;
787 	do {
788 		while (nvlist_next(nvl, &type, &cookie) != NULL) {
789 			nvp = cookie;
790 			switch (type) {
791 			case NV_TYPE_DESCRIPTOR:
792 				ndescs++;
793 				break;
794 			case NV_TYPE_NVLIST:
795 				nvl = nvpair_get_nvlist(nvp);
796 				cookie = NULL;
797 				break;
798 			case NV_TYPE_NVLIST_ARRAY:
799 			    {
800 				const nvlist_t * const *value;
801 				size_t nitems;
802 
803 				value = nvpair_get_nvlist_array(nvp, &nitems);
804 				PJDLOG_ASSERT(value != NULL);
805 				PJDLOG_ASSERT(nitems > 0);
806 
807 				nvl = value[0];
808 				cookie = NULL;
809 				break;
810 			    }
811 			case NV_TYPE_DESCRIPTOR_ARRAY:
812 			    {
813 				size_t nitems;
814 
815 				(void)nvpair_get_descriptor_array(nvp,
816 				    &nitems);
817 				ndescs += nitems;
818 				break;
819 			    }
820 			}
821 		}
822 	} while ((nvl = nvlist_get_pararr(nvl, &cookie)) != NULL);
823 
824 	return (ndescs);
825 #else
826 	return (0);
827 #endif
828 }
829 
830 static unsigned char *
831 nvlist_pack_header(const nvlist_t *nvl, unsigned char *ptr, size_t *leftp)
832 {
833 	struct nvlist_header nvlhdr;
834 
835 	NVLIST_ASSERT(nvl);
836 
837 	nvlhdr.nvlh_magic = NVLIST_HEADER_MAGIC;
838 	nvlhdr.nvlh_version = NVLIST_HEADER_VERSION;
839 	nvlhdr.nvlh_flags = nvl->nvl_flags;
840 #if BYTE_ORDER == BIG_ENDIAN
841 	nvlhdr.nvlh_flags |= NV_FLAG_BIG_ENDIAN;
842 #endif
843 	nvlhdr.nvlh_descriptors = nvlist_ndescriptors(nvl);
844 	nvlhdr.nvlh_size = *leftp - sizeof(nvlhdr);
845 	PJDLOG_ASSERT(*leftp >= sizeof(nvlhdr));
846 	memcpy(ptr, &nvlhdr, sizeof(nvlhdr));
847 	ptr += sizeof(nvlhdr);
848 	*leftp -= sizeof(nvlhdr);
849 
850 	return (ptr);
851 }
852 
853 static void *
854 nvlist_xpack(const nvlist_t *nvl, int64_t *fdidxp, size_t *sizep)
855 {
856 	unsigned char *buf, *ptr;
857 	size_t left, size;
858 	const nvlist_t *tmpnvl;
859 	nvpair_t *nvp, *tmpnvp;
860 	void *cookie;
861 
862 	NVLIST_ASSERT(nvl);
863 
864 	if (nvl->nvl_error != 0) {
865 		ERRNO_SET(nvl->nvl_error);
866 		return (NULL);
867 	}
868 
869 	size = nvlist_size(nvl);
870 	buf = nv_malloc(size);
871 	if (buf == NULL)
872 		return (NULL);
873 
874 	ptr = buf;
875 	left = size;
876 
877 	ptr = nvlist_pack_header(nvl, ptr, &left);
878 
879 	nvp = nvlist_first_nvpair(nvl);
880 	while (nvp != NULL) {
881 		NVPAIR_ASSERT(nvp);
882 
883 		nvpair_init_datasize(nvp);
884 		ptr = nvpair_pack_header(nvp, ptr, &left);
885 		if (ptr == NULL)
886 			goto fail;
887 		switch (nvpair_type(nvp)) {
888 		case NV_TYPE_NULL:
889 			ptr = nvpair_pack_null(nvp, ptr, &left);
890 			break;
891 		case NV_TYPE_BOOL:
892 			ptr = nvpair_pack_bool(nvp, ptr, &left);
893 			break;
894 		case NV_TYPE_NUMBER:
895 			ptr = nvpair_pack_number(nvp, ptr, &left);
896 			break;
897 		case NV_TYPE_STRING:
898 			ptr = nvpair_pack_string(nvp, ptr, &left);
899 			break;
900 		case NV_TYPE_NVLIST:
901 			tmpnvl = nvpair_get_nvlist(nvp);
902 			ptr = nvlist_pack_header(tmpnvl, ptr, &left);
903 			if (ptr == NULL)
904 				goto fail;
905 			tmpnvp = nvlist_first_nvpair(tmpnvl);
906 			if (tmpnvp != NULL) {
907 				nvl = tmpnvl;
908 				nvp = tmpnvp;
909 				continue;
910 			}
911 			ptr = nvpair_pack_nvlist_up(ptr, &left);
912 			break;
913 #ifndef _KERNEL
914 		case NV_TYPE_DESCRIPTOR:
915 			ptr = nvpair_pack_descriptor(nvp, ptr, fdidxp, &left);
916 			break;
917 		case NV_TYPE_DESCRIPTOR_ARRAY:
918 			ptr = nvpair_pack_descriptor_array(nvp, ptr, fdidxp,
919 			    &left);
920 			break;
921 #endif
922 		case NV_TYPE_BINARY:
923 			ptr = nvpair_pack_binary(nvp, ptr, &left);
924 			break;
925 		case NV_TYPE_BOOL_ARRAY:
926 			ptr = nvpair_pack_bool_array(nvp, ptr, &left);
927 			break;
928 		case NV_TYPE_NUMBER_ARRAY:
929 			ptr = nvpair_pack_number_array(nvp, ptr, &left);
930 			break;
931 		case NV_TYPE_STRING_ARRAY:
932 			ptr = nvpair_pack_string_array(nvp, ptr, &left);
933 			break;
934 		case NV_TYPE_NVLIST_ARRAY:
935 		    {
936 			const nvlist_t * const * value;
937 			size_t nitems;
938 			unsigned int ii;
939 
940 			tmpnvl = NULL;
941 			value = nvpair_get_nvlist_array(nvp, &nitems);
942 			for (ii = 0; ii < nitems; ii++) {
943 				ptr = nvlist_pack_header(value[ii], ptr, &left);
944 				if (ptr == NULL)
945 					goto out;
946 				tmpnvp = nvlist_first_nvpair(value[ii]);
947 				if (tmpnvp != NULL) {
948 					tmpnvl = value[ii];
949 					break;
950 				}
951 				ptr = nvpair_pack_nvlist_array_next(ptr, &left);
952 				if (ptr == NULL)
953 					goto out;
954 			}
955 			if (tmpnvl != NULL) {
956 				nvl = tmpnvl;
957 				nvp = tmpnvp;
958 				continue;
959 			}
960 			break;
961 		    }
962 		default:
963 			PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
964 		}
965 		if (ptr == NULL)
966 			goto fail;
967 		while ((nvp = nvlist_next_nvpair(nvl, nvp)) == NULL) {
968 			do {
969 				cookie = NULL;
970 				if (nvlist_in_array(nvl)) {
971 					ptr = nvpair_pack_nvlist_array_next(ptr,
972 					    &left);
973 					if (ptr == NULL)
974 						goto fail;
975 				}
976 				nvl = nvlist_get_pararr(nvl, &cookie);
977 				if (nvl == NULL)
978 					goto out;
979 				if (nvlist_in_array(nvl) && cookie == NULL) {
980 					nvp = nvlist_first_nvpair(nvl);
981 					ptr = nvlist_pack_header(nvl, ptr,
982 					    &left);
983 					if (ptr == NULL)
984 						goto fail;
985 				} else if (nvpair_type((nvpair_t *)cookie) !=
986 				    NV_TYPE_NVLIST_ARRAY) {
987 					ptr = nvpair_pack_nvlist_up(ptr, &left);
988 					if (ptr == NULL)
989 						goto fail;
990 					nvp = cookie;
991 				} else {
992 					nvp = cookie;
993 				}
994 			} while (nvp == NULL);
995 			if (nvlist_in_array(nvl) && cookie == NULL)
996 				break;
997 		}
998 	}
999 
1000 out:
1001 	if (sizep != NULL)
1002 		*sizep = size;
1003 	return (buf);
1004 fail:
1005 	nv_free(buf);
1006 	return (NULL);
1007 }
1008 
1009 void *
1010 nvlist_pack(const nvlist_t *nvl, size_t *sizep)
1011 {
1012 
1013 	NVLIST_ASSERT(nvl);
1014 
1015 	if (nvl->nvl_error != 0) {
1016 		ERRNO_SET(nvl->nvl_error);
1017 		return (NULL);
1018 	}
1019 
1020 	if (nvlist_ndescriptors(nvl) > 0) {
1021 		ERRNO_SET(EOPNOTSUPP);
1022 		return (NULL);
1023 	}
1024 
1025 	return (nvlist_xpack(nvl, NULL, sizep));
1026 }
1027 
1028 static bool
1029 nvlist_check_header(struct nvlist_header *nvlhdrp)
1030 {
1031 
1032 	if (nvlhdrp->nvlh_magic != NVLIST_HEADER_MAGIC) {
1033 		ERRNO_SET(EINVAL);
1034 		return (false);
1035 	}
1036 	if ((nvlhdrp->nvlh_flags & ~NV_FLAG_ALL_MASK) != 0) {
1037 		ERRNO_SET(EINVAL);
1038 		return (false);
1039 	}
1040 #if BYTE_ORDER == BIG_ENDIAN
1041 	if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) == 0) {
1042 		nvlhdrp->nvlh_size = le64toh(nvlhdrp->nvlh_size);
1043 		nvlhdrp->nvlh_descriptors = le64toh(nvlhdrp->nvlh_descriptors);
1044 	}
1045 #else
1046 	if ((nvlhdrp->nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0) {
1047 		nvlhdrp->nvlh_size = be64toh(nvlhdrp->nvlh_size);
1048 		nvlhdrp->nvlh_descriptors = be64toh(nvlhdrp->nvlh_descriptors);
1049 	}
1050 #endif
1051 	return (true);
1052 }
1053 
1054 const unsigned char *
1055 nvlist_unpack_header(nvlist_t *nvl, const unsigned char *ptr, size_t nfds,
1056     bool *isbep, size_t *leftp)
1057 {
1058 	struct nvlist_header nvlhdr;
1059 	int inarrayf;
1060 
1061 	if (*leftp < sizeof(nvlhdr))
1062 		goto fail;
1063 
1064 	memcpy(&nvlhdr, ptr, sizeof(nvlhdr));
1065 
1066 	if (!nvlist_check_header(&nvlhdr))
1067 		goto fail;
1068 
1069 	if (nvlhdr.nvlh_size != *leftp - sizeof(nvlhdr))
1070 		goto fail;
1071 
1072 	/*
1073 	 * nvlh_descriptors might be smaller than nfds in embedded nvlists.
1074 	 */
1075 	if (nvlhdr.nvlh_descriptors > nfds)
1076 		goto fail;
1077 
1078 	if ((nvlhdr.nvlh_flags & ~NV_FLAG_ALL_MASK) != 0)
1079 		goto fail;
1080 
1081 	inarrayf = (nvl->nvl_flags & NV_FLAG_IN_ARRAY);
1082 	nvl->nvl_flags = (nvlhdr.nvlh_flags & NV_FLAG_PUBLIC_MASK) | inarrayf;
1083 
1084 	ptr += sizeof(nvlhdr);
1085 	if (isbep != NULL)
1086 		*isbep = (((int)nvlhdr.nvlh_flags & NV_FLAG_BIG_ENDIAN) != 0);
1087 	*leftp -= sizeof(nvlhdr);
1088 
1089 	return (ptr);
1090 fail:
1091 	ERRNO_SET(EINVAL);
1092 	return (NULL);
1093 }
1094 
1095 static nvlist_t *
1096 nvlist_xunpack(const void *buf, size_t size, const int *fds, size_t nfds,
1097     int flags)
1098 {
1099 	const unsigned char *ptr;
1100 	nvlist_t *nvl, *retnvl, *tmpnvl, *array;
1101 	nvpair_t *nvp;
1102 	size_t left;
1103 	bool isbe;
1104 
1105 	PJDLOG_ASSERT((flags & ~(NV_FLAG_PUBLIC_MASK)) == 0);
1106 
1107 	left = size;
1108 	ptr = buf;
1109 
1110 	tmpnvl = array = NULL;
1111 	nvl = retnvl = nvlist_create(0);
1112 	if (nvl == NULL)
1113 		goto fail;
1114 
1115 	ptr = nvlist_unpack_header(nvl, ptr, nfds, &isbe, &left);
1116 	if (ptr == NULL)
1117 		goto fail;
1118 	if (nvl->nvl_flags != flags) {
1119 		ERRNO_SET(EILSEQ);
1120 		goto fail;
1121 	}
1122 
1123 	while (left > 0) {
1124 		ptr = nvpair_unpack(isbe, ptr, &left, &nvp);
1125 		if (ptr == NULL)
1126 			goto fail;
1127 		switch (nvpair_type(nvp)) {
1128 		case NV_TYPE_NULL:
1129 			ptr = nvpair_unpack_null(isbe, nvp, ptr, &left);
1130 			break;
1131 		case NV_TYPE_BOOL:
1132 			ptr = nvpair_unpack_bool(isbe, nvp, ptr, &left);
1133 			break;
1134 		case NV_TYPE_NUMBER:
1135 			ptr = nvpair_unpack_number(isbe, nvp, ptr, &left);
1136 			break;
1137 		case NV_TYPE_STRING:
1138 			ptr = nvpair_unpack_string(isbe, nvp, ptr, &left);
1139 			break;
1140 		case NV_TYPE_NVLIST:
1141 			ptr = nvpair_unpack_nvlist(isbe, nvp, ptr, &left, nfds,
1142 			    &tmpnvl);
1143 			if (tmpnvl == NULL || ptr == NULL)
1144 				goto fail;
1145 			nvlist_set_parent(tmpnvl, nvp);
1146 			break;
1147 #ifndef _KERNEL
1148 		case NV_TYPE_DESCRIPTOR:
1149 			ptr = nvpair_unpack_descriptor(isbe, nvp, ptr, &left,
1150 			    fds, nfds);
1151 			break;
1152 		case NV_TYPE_DESCRIPTOR_ARRAY:
1153 			ptr = nvpair_unpack_descriptor_array(isbe, nvp, ptr,
1154 			    &left, fds, nfds);
1155 			break;
1156 #endif
1157 		case NV_TYPE_BINARY:
1158 			ptr = nvpair_unpack_binary(isbe, nvp, ptr, &left);
1159 			break;
1160 		case NV_TYPE_NVLIST_UP:
1161 			if (nvl->nvl_parent == NULL)
1162 				goto fail;
1163 			nvl = nvpair_nvlist(nvl->nvl_parent);
1164 			nvpair_free_structure(nvp);
1165 			continue;
1166 		case NV_TYPE_NVLIST_ARRAY_NEXT:
1167 			if (nvl->nvl_array_next == NULL) {
1168 				if (nvl->nvl_parent == NULL)
1169 					goto fail;
1170 				nvl = nvpair_nvlist(nvl->nvl_parent);
1171 			} else {
1172 				nvl = __DECONST(nvlist_t *,
1173 				    nvlist_get_array_next(nvl));
1174 				ptr = nvlist_unpack_header(nvl, ptr, nfds,
1175 				    &isbe, &left);
1176 				if (ptr == NULL)
1177 					goto fail;
1178 			}
1179 			nvpair_free_structure(nvp);
1180 			continue;
1181 		case NV_TYPE_BOOL_ARRAY:
1182 			ptr = nvpair_unpack_bool_array(isbe, nvp, ptr, &left);
1183 			break;
1184 		case NV_TYPE_NUMBER_ARRAY:
1185 			ptr = nvpair_unpack_number_array(isbe, nvp, ptr, &left);
1186 			break;
1187 		case NV_TYPE_STRING_ARRAY:
1188 			ptr = nvpair_unpack_string_array(isbe, nvp, ptr, &left);
1189 			break;
1190 		case NV_TYPE_NVLIST_ARRAY:
1191 			ptr = nvpair_unpack_nvlist_array(isbe, nvp, ptr, &left,
1192 			    &array);
1193 			if (ptr == NULL)
1194 				goto fail;
1195 			PJDLOG_ASSERT(array != NULL);
1196 			tmpnvl = array;
1197 			do {
1198 				nvlist_set_parent(array, nvp);
1199 				array = __DECONST(nvlist_t *,
1200 				    nvlist_get_array_next(array));
1201 			} while (array != NULL);
1202 			ptr = nvlist_unpack_header(tmpnvl, ptr, nfds, &isbe,
1203 			    &left);
1204 			break;
1205 		default:
1206 			PJDLOG_ABORT("Invalid type (%d).", nvpair_type(nvp));
1207 		}
1208 		if (ptr == NULL)
1209 			goto fail;
1210 		if (!nvlist_move_nvpair(nvl, nvp))
1211 			goto fail;
1212 		if (tmpnvl != NULL) {
1213 			nvl = tmpnvl;
1214 			tmpnvl = NULL;
1215 		}
1216 	}
1217 
1218 	return (retnvl);
1219 fail:
1220 	nvlist_destroy(retnvl);
1221 	return (NULL);
1222 }
1223 
1224 nvlist_t *
1225 nvlist_unpack(const void *buf, size_t size, int flags)
1226 {
1227 
1228 	return (nvlist_xunpack(buf, size, NULL, 0, flags));
1229 }
1230 
1231 #ifndef _KERNEL
1232 int
1233 nvlist_send(int sock, const nvlist_t *nvl)
1234 {
1235 	size_t datasize, nfds;
1236 	int *fds;
1237 	void *data;
1238 	int64_t fdidx;
1239 	int ret;
1240 
1241 	if (nvlist_error(nvl) != 0) {
1242 		ERRNO_SET(nvlist_error(nvl));
1243 		return (-1);
1244 	}
1245 
1246 	fds = nvlist_descriptors(nvl, &nfds);
1247 	if (fds == NULL)
1248 		return (-1);
1249 
1250 	ret = -1;
1251 	fdidx = 0;
1252 
1253 	data = nvlist_xpack(nvl, &fdidx, &datasize);
1254 	if (data == NULL)
1255 		goto out;
1256 
1257 	if (buf_send(sock, data, datasize) == -1)
1258 		goto out;
1259 
1260 	if (nfds > 0) {
1261 		if (fd_send(sock, fds, nfds) == -1)
1262 			goto out;
1263 	}
1264 
1265 	ret = 0;
1266 out:
1267 	ERRNO_SAVE();
1268 	nv_free(fds);
1269 	nv_free(data);
1270 	ERRNO_RESTORE();
1271 	return (ret);
1272 }
1273 
1274 nvlist_t *
1275 nvlist_recv(int sock, int flags)
1276 {
1277 	struct nvlist_header nvlhdr;
1278 	nvlist_t *nvl, *ret;
1279 	unsigned char *buf;
1280 	size_t nfds, size, i;
1281 	int *fds;
1282 
1283 	if (buf_recv(sock, &nvlhdr, sizeof(nvlhdr)) == -1)
1284 		return (NULL);
1285 
1286 	if (!nvlist_check_header(&nvlhdr))
1287 		return (NULL);
1288 
1289 	nfds = (size_t)nvlhdr.nvlh_descriptors;
1290 	size = sizeof(nvlhdr) + (size_t)nvlhdr.nvlh_size;
1291 
1292 	buf = nv_malloc(size);
1293 	if (buf == NULL)
1294 		return (NULL);
1295 
1296 	memcpy(buf, &nvlhdr, sizeof(nvlhdr));
1297 
1298 	ret = NULL;
1299 	fds = NULL;
1300 
1301 	if (buf_recv(sock, buf + sizeof(nvlhdr), size - sizeof(nvlhdr)) == -1)
1302 		goto out;
1303 
1304 	if (nfds > 0) {
1305 		fds = nv_malloc(nfds * sizeof(fds[0]));
1306 		if (fds == NULL)
1307 			goto out;
1308 		if (fd_recv(sock, fds, nfds) == -1)
1309 			goto out;
1310 	}
1311 
1312 	nvl = nvlist_xunpack(buf, size, fds, nfds, flags);
1313 	if (nvl == NULL) {
1314 		ERRNO_SAVE();
1315 		for (i = 0; i < nfds; i++)
1316 			close(fds[i]);
1317 		ERRNO_RESTORE();
1318 		goto out;
1319 	}
1320 
1321 	ret = nvl;
1322 out:
1323 	ERRNO_SAVE();
1324 	nv_free(buf);
1325 	nv_free(fds);
1326 	ERRNO_RESTORE();
1327 
1328 	return (ret);
1329 }
1330 
1331 nvlist_t *
1332 nvlist_xfer(int sock, nvlist_t *nvl, int flags)
1333 {
1334 
1335 	if (nvlist_send(sock, nvl) < 0) {
1336 		nvlist_destroy(nvl);
1337 		return (NULL);
1338 	}
1339 	nvlist_destroy(nvl);
1340 	return (nvlist_recv(sock, flags));
1341 }
1342 #endif
1343 
1344 nvpair_t *
1345 nvlist_first_nvpair(const nvlist_t *nvl)
1346 {
1347 
1348 	NVLIST_ASSERT(nvl);
1349 
1350 	return (TAILQ_FIRST(&nvl->nvl_head));
1351 }
1352 
1353 nvpair_t *
1354 nvlist_next_nvpair(const nvlist_t *nvl __unused, const nvpair_t *nvp)
1355 {
1356 	nvpair_t *retnvp;
1357 
1358 	NVLIST_ASSERT(nvl);
1359 	NVPAIR_ASSERT(nvp);
1360 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1361 
1362 	retnvp = nvpair_next(nvp);
1363 	PJDLOG_ASSERT(retnvp == NULL || nvpair_nvlist(retnvp) == nvl);
1364 
1365 	return (retnvp);
1366 
1367 }
1368 
1369 nvpair_t *
1370 nvlist_prev_nvpair(const nvlist_t *nvl __unused, const nvpair_t *nvp)
1371 {
1372 	nvpair_t *retnvp;
1373 
1374 	NVLIST_ASSERT(nvl);
1375 	NVPAIR_ASSERT(nvp);
1376 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
1377 
1378 	retnvp = nvpair_prev(nvp);
1379 	PJDLOG_ASSERT(nvpair_nvlist(retnvp) == nvl);
1380 
1381 	return (retnvp);
1382 }
1383 
1384 const char *
1385 nvlist_next(const nvlist_t *nvl, int *typep, void **cookiep)
1386 {
1387 	nvpair_t *nvp;
1388 
1389 	NVLIST_ASSERT(nvl);
1390 
1391 	if (cookiep == NULL || *cookiep == NULL)
1392 		nvp = nvlist_first_nvpair(nvl);
1393 	else
1394 		nvp = nvlist_next_nvpair(nvl, *cookiep);
1395 	if (nvp == NULL)
1396 		return (NULL);
1397 	if (typep != NULL)
1398 		*typep = nvpair_type(nvp);
1399 	if (cookiep != NULL)
1400 		*cookiep = nvp;
1401 	return (nvpair_name(nvp));
1402 }
1403 
1404 bool
1405 nvlist_exists(const nvlist_t *nvl, const char *name)
1406 {
1407 
1408 	return (nvlist_find(nvl, NV_TYPE_NONE, name) != NULL);
1409 }
1410 
1411 #define	NVLIST_EXISTS(type, TYPE)					\
1412 bool									\
1413 nvlist_exists_##type(const nvlist_t *nvl, const char *name)		\
1414 {									\
1415 									\
1416 	return (nvlist_find(nvl, NV_TYPE_##TYPE, name) != NULL);	\
1417 }
1418 
1419 NVLIST_EXISTS(null, NULL)
1420 NVLIST_EXISTS(bool, BOOL)
1421 NVLIST_EXISTS(number, NUMBER)
1422 NVLIST_EXISTS(string, STRING)
1423 NVLIST_EXISTS(nvlist, NVLIST)
1424 NVLIST_EXISTS(binary, BINARY)
1425 NVLIST_EXISTS(bool_array, BOOL_ARRAY)
1426 NVLIST_EXISTS(number_array, NUMBER_ARRAY)
1427 NVLIST_EXISTS(string_array, STRING_ARRAY)
1428 NVLIST_EXISTS(nvlist_array, NVLIST_ARRAY)
1429 #ifndef _KERNEL
1430 NVLIST_EXISTS(descriptor, DESCRIPTOR)
1431 NVLIST_EXISTS(descriptor_array, DESCRIPTOR_ARRAY)
1432 #endif
1433 
1434 #undef	NVLIST_EXISTS
1435 
1436 void
1437 nvlist_add_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1438 {
1439 	nvpair_t *newnvp;
1440 
1441 	NVPAIR_ASSERT(nvp);
1442 
1443 	if (nvlist_error(nvl) != 0) {
1444 		ERRNO_SET(nvlist_error(nvl));
1445 		return;
1446 	}
1447 	if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1448 		if (nvlist_exists(nvl, nvpair_name(nvp))) {
1449 			nvl->nvl_error = EEXIST;
1450 			ERRNO_SET(nvlist_error(nvl));
1451 			return;
1452 		}
1453 	}
1454 
1455 	newnvp = nvpair_clone(nvp);
1456 	if (newnvp == NULL) {
1457 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1458 		ERRNO_SET(nvlist_error(nvl));
1459 		return;
1460 	}
1461 
1462 	nvpair_insert(&nvl->nvl_head, newnvp, nvl);
1463 	nvlist_update_size(nvl, newnvp, 1);
1464 }
1465 
1466 void
1467 nvlist_add_stringf(nvlist_t *nvl, const char *name, const char *valuefmt, ...)
1468 {
1469 	va_list valueap;
1470 
1471 	va_start(valueap, valuefmt);
1472 	nvlist_add_stringv(nvl, name, valuefmt, valueap);
1473 	va_end(valueap);
1474 }
1475 
1476 void
1477 nvlist_add_stringv(nvlist_t *nvl, const char *name, const char *valuefmt,
1478     va_list valueap)
1479 {
1480 	nvpair_t *nvp;
1481 
1482 	if (nvlist_error(nvl) != 0) {
1483 		ERRNO_SET(nvlist_error(nvl));
1484 		return;
1485 	}
1486 
1487 	nvp = nvpair_create_stringv(name, valuefmt, valueap);
1488 	if (nvp == NULL) {
1489 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1490 		ERRNO_SET(nvl->nvl_error);
1491 	} else {
1492 		(void)nvlist_move_nvpair(nvl, nvp);
1493 	}
1494 }
1495 
1496 void
1497 nvlist_add_null(nvlist_t *nvl, const char *name)
1498 {
1499 	nvpair_t *nvp;
1500 
1501 	if (nvlist_error(nvl) != 0) {
1502 		ERRNO_SET(nvlist_error(nvl));
1503 		return;
1504 	}
1505 
1506 	nvp = nvpair_create_null(name);
1507 	if (nvp == NULL) {
1508 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1509 		ERRNO_SET(nvl->nvl_error);
1510 	} else {
1511 		(void)nvlist_move_nvpair(nvl, nvp);
1512 	}
1513 }
1514 
1515 void
1516 nvlist_add_binary(nvlist_t *nvl, const char *name, const void *value,
1517     size_t size)
1518 {
1519 	nvpair_t *nvp;
1520 
1521 	if (nvlist_error(nvl) != 0) {
1522 		ERRNO_SET(nvlist_error(nvl));
1523 		return;
1524 	}
1525 
1526 	nvp = nvpair_create_binary(name, value, size);
1527 	if (nvp == NULL) {
1528 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1529 		ERRNO_SET(nvl->nvl_error);
1530 	} else {
1531 		(void)nvlist_move_nvpair(nvl, nvp);
1532 	}
1533 }
1534 
1535 
1536 #define	NVLIST_ADD(vtype, type)						\
1537 void									\
1538 nvlist_add_##type(nvlist_t *nvl, const char *name, vtype value)		\
1539 {									\
1540 	nvpair_t *nvp;							\
1541 									\
1542 	if (nvlist_error(nvl) != 0) {					\
1543 		ERRNO_SET(nvlist_error(nvl));				\
1544 		return;							\
1545 	}								\
1546 									\
1547 	nvp = nvpair_create_##type(name, value);			\
1548 	if (nvp == NULL) {						\
1549 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1550 		ERRNO_SET(nvl->nvl_error);				\
1551 	} else {							\
1552 		(void)nvlist_move_nvpair(nvl, nvp);			\
1553 	}								\
1554 }
1555 
1556 NVLIST_ADD(bool, bool)
1557 NVLIST_ADD(uint64_t, number)
1558 NVLIST_ADD(const char *, string)
1559 NVLIST_ADD(const nvlist_t *, nvlist)
1560 #ifndef _KERNEL
1561 NVLIST_ADD(int, descriptor);
1562 #endif
1563 
1564 #undef	NVLIST_ADD
1565 
1566 #define	NVLIST_ADD_ARRAY(vtype, type)					\
1567 void									\
1568 nvlist_add_##type##_array(nvlist_t *nvl, const char *name, vtype value,	\
1569     size_t nitems)							\
1570 {									\
1571 	nvpair_t *nvp;							\
1572 									\
1573 	if (nvlist_error(nvl) != 0) {					\
1574 		ERRNO_SET(nvlist_error(nvl));				\
1575 		return;							\
1576 	}								\
1577 									\
1578 	nvp = nvpair_create_##type##_array(name, value, nitems);	\
1579 	if (nvp == NULL) {						\
1580 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1581 		ERRNO_SET(nvl->nvl_error);				\
1582 	} else {							\
1583 		(void)nvlist_move_nvpair(nvl, nvp);			\
1584 	}								\
1585 }
1586 
1587 NVLIST_ADD_ARRAY(const bool *, bool)
1588 NVLIST_ADD_ARRAY(const uint64_t *, number)
1589 NVLIST_ADD_ARRAY(const char * const *, string)
1590 NVLIST_ADD_ARRAY(const nvlist_t * const *, nvlist)
1591 #ifndef _KERNEL
1592 NVLIST_ADD_ARRAY(const int *, descriptor)
1593 #endif
1594 
1595 #undef	NVLIST_ADD_ARRAY
1596 
1597 #define	NVLIST_APPEND_ARRAY(vtype, type, TYPE)				\
1598 void									\
1599 nvlist_append_##type##_array(nvlist_t *nvl, const char *name, vtype value)\
1600 {									\
1601 	nvpair_t *nvp;							\
1602 									\
1603 	if (nvlist_error(nvl) != 0) {					\
1604 		ERRNO_SET(nvlist_error(nvl));				\
1605 		return;							\
1606 	}								\
1607 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1608 	if (nvp == NULL) {						\
1609 		nvlist_add_##type##_array(nvl, name, &value, 1);	\
1610 		return;							\
1611 	}								\
1612 	nvlist_update_size(nvl, nvp, -1);				\
1613 	if (nvpair_append_##type##_array(nvp, value) == -1) {		\
1614 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);		\
1615 		ERRNO_SET(nvl->nvl_error);				\
1616 	}								\
1617 	nvlist_update_size(nvl, nvp, 1);				\
1618 }
1619 
1620 NVLIST_APPEND_ARRAY(const bool, bool, BOOL)
1621 NVLIST_APPEND_ARRAY(const uint64_t, number, NUMBER)
1622 NVLIST_APPEND_ARRAY(const char *, string, STRING)
1623 NVLIST_APPEND_ARRAY(const nvlist_t *, nvlist, NVLIST)
1624 #ifndef _KERNEL
1625 NVLIST_APPEND_ARRAY(const int, descriptor, DESCRIPTOR)
1626 #endif
1627 
1628 #undef	NVLIST_APPEND_ARRAY
1629 
1630 bool
1631 nvlist_move_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1632 {
1633 
1634 	NVPAIR_ASSERT(nvp);
1635 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == NULL);
1636 
1637 	if (nvlist_error(nvl) != 0) {
1638 		nvpair_free(nvp);
1639 		ERRNO_SET(nvlist_error(nvl));
1640 		return (false);
1641 	}
1642 	if ((nvl->nvl_flags & NV_FLAG_NO_UNIQUE) == 0) {
1643 		if (nvlist_exists(nvl, nvpair_name(nvp))) {
1644 			nvpair_free(nvp);
1645 			nvl->nvl_error = EEXIST;
1646 			ERRNO_SET(nvl->nvl_error);
1647 			return (false);
1648 		}
1649 	}
1650 
1651 	nvpair_insert(&nvl->nvl_head, nvp, nvl);
1652 	nvlist_update_size(nvl, nvp, 1);
1653 	return (true);
1654 }
1655 
1656 void
1657 nvlist_move_string(nvlist_t *nvl, const char *name, char *value)
1658 {
1659 	nvpair_t *nvp;
1660 
1661 	if (nvlist_error(nvl) != 0) {
1662 		nv_free(value);
1663 		ERRNO_SET(nvlist_error(nvl));
1664 		return;
1665 	}
1666 
1667 	nvp = nvpair_move_string(name, value);
1668 	if (nvp == NULL) {
1669 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1670 		ERRNO_SET(nvl->nvl_error);
1671 	} else {
1672 		(void)nvlist_move_nvpair(nvl, nvp);
1673 	}
1674 }
1675 
1676 void
1677 nvlist_move_nvlist(nvlist_t *nvl, const char *name, nvlist_t *value)
1678 {
1679 	nvpair_t *nvp;
1680 
1681 	if (nvlist_error(nvl) != 0) {
1682 		if (value != NULL && nvlist_get_nvpair_parent(value) != NULL)
1683 			nvlist_destroy(value);
1684 		ERRNO_SET(nvlist_error(nvl));
1685 		return;
1686 	}
1687 
1688 	nvp = nvpair_move_nvlist(name, value);
1689 	if (nvp == NULL) {
1690 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1691 		ERRNO_SET(nvl->nvl_error);
1692 	} else {
1693 		(void)nvlist_move_nvpair(nvl, nvp);
1694 	}
1695 }
1696 
1697 #ifndef _KERNEL
1698 void
1699 nvlist_move_descriptor(nvlist_t *nvl, const char *name, int value)
1700 {
1701 	nvpair_t *nvp;
1702 
1703 	if (nvlist_error(nvl) != 0) {
1704 		close(value);
1705 		ERRNO_SET(nvlist_error(nvl));
1706 		return;
1707 	}
1708 
1709 	nvp = nvpair_move_descriptor(name, value);
1710 	if (nvp == NULL) {
1711 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1712 		ERRNO_SET(nvl->nvl_error);
1713 	} else {
1714 		(void)nvlist_move_nvpair(nvl, nvp);
1715 	}
1716 }
1717 #endif
1718 
1719 void
1720 nvlist_move_binary(nvlist_t *nvl, const char *name, void *value, size_t size)
1721 {
1722 	nvpair_t *nvp;
1723 
1724 	if (nvlist_error(nvl) != 0) {
1725 		nv_free(value);
1726 		ERRNO_SET(nvlist_error(nvl));
1727 		return;
1728 	}
1729 
1730 	nvp = nvpair_move_binary(name, value, size);
1731 	if (nvp == NULL) {
1732 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1733 		ERRNO_SET(nvl->nvl_error);
1734 	} else {
1735 		(void)nvlist_move_nvpair(nvl, nvp);
1736 	}
1737 }
1738 
1739 void
1740 nvlist_move_bool_array(nvlist_t *nvl, const char *name, bool *value,
1741     size_t nitems)
1742 {
1743 	nvpair_t *nvp;
1744 
1745 	if (nvlist_error(nvl) != 0) {
1746 		nv_free(value);
1747 		ERRNO_SET(nvlist_error(nvl));
1748 		return;
1749 	}
1750 
1751 	nvp = nvpair_move_bool_array(name, value, nitems);
1752 	if (nvp == NULL) {
1753 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1754 		ERRNO_SET(nvl->nvl_error);
1755 	} else {
1756 		(void)nvlist_move_nvpair(nvl, nvp);
1757 	}
1758 }
1759 
1760 void
1761 nvlist_move_string_array(nvlist_t *nvl, const char *name, char **value,
1762     size_t nitems)
1763 {
1764 	nvpair_t *nvp;
1765 	size_t i;
1766 
1767 	if (nvlist_error(nvl) != 0) {
1768 		if (value != NULL) {
1769 			for (i = 0; i < nitems; i++)
1770 				nv_free(value[i]);
1771 			nv_free(value);
1772 		}
1773 		ERRNO_SET(nvlist_error(nvl));
1774 		return;
1775 	}
1776 
1777 	nvp = nvpair_move_string_array(name, value, nitems);
1778 	if (nvp == NULL) {
1779 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1780 		ERRNO_SET(nvl->nvl_error);
1781 	} else {
1782 		(void)nvlist_move_nvpair(nvl, nvp);
1783 	}
1784 }
1785 
1786 void
1787 nvlist_move_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **value,
1788     size_t nitems)
1789 {
1790 	nvpair_t *nvp;
1791 	size_t i;
1792 
1793 	if (nvlist_error(nvl) != 0) {
1794 		if (value != NULL) {
1795 			for (i = 0; i < nitems; i++) {
1796 				if (nvlist_get_pararr(value[i], NULL) == NULL)
1797 					nvlist_destroy(value[i]);
1798 			}
1799 		}
1800 		nv_free(value);
1801 		ERRNO_SET(nvlist_error(nvl));
1802 		return;
1803 	}
1804 
1805 	nvp = nvpair_move_nvlist_array(name, value, nitems);
1806 	if (nvp == NULL) {
1807 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1808 		ERRNO_SET(nvl->nvl_error);
1809 	} else {
1810 		(void)nvlist_move_nvpair(nvl, nvp);
1811 	}
1812 }
1813 
1814 void
1815 nvlist_move_number_array(nvlist_t *nvl, const char *name, uint64_t *value,
1816     size_t nitems)
1817 {
1818 	nvpair_t *nvp;
1819 
1820 	if (nvlist_error(nvl) != 0) {
1821 		nv_free(value);
1822 		ERRNO_SET(nvlist_error(nvl));
1823 		return;
1824 	}
1825 
1826 	nvp = nvpair_move_number_array(name, value, nitems);
1827 	if (nvp == NULL) {
1828 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1829 		ERRNO_SET(nvl->nvl_error);
1830 	} else {
1831 		(void)nvlist_move_nvpair(nvl, nvp);
1832 	}
1833 }
1834 
1835 #ifndef _KERNEL
1836 void
1837 nvlist_move_descriptor_array(nvlist_t *nvl, const char *name, int *value,
1838     size_t nitems)
1839 {
1840 	nvpair_t *nvp;
1841 	size_t i;
1842 
1843 	if (nvlist_error(nvl) != 0) {
1844 		if (value != 0) {
1845 			for (i = 0; i < nitems; i++)
1846 				close(value[i]);
1847 			nv_free(value);
1848 		}
1849 
1850 		ERRNO_SET(nvlist_error(nvl));
1851 		return;
1852 	}
1853 
1854 	nvp = nvpair_move_descriptor_array(name, value, nitems);
1855 	if (nvp == NULL) {
1856 		nvl->nvl_error = ERRNO_OR_DEFAULT(ENOMEM);
1857 		ERRNO_SET(nvl->nvl_error);
1858 	} else {
1859 		(void)nvlist_move_nvpair(nvl, nvp);
1860 	}
1861 }
1862 #endif
1863 
1864 const nvpair_t *
1865 nvlist_get_nvpair(const nvlist_t *nvl, const char *name)
1866 {
1867 
1868 	return (nvlist_find(nvl, NV_TYPE_NONE, name));
1869 }
1870 
1871 #define	NVLIST_GET(ftype, type, TYPE)					\
1872 ftype									\
1873 nvlist_get_##type(const nvlist_t *nvl, const char *name)		\
1874 {									\
1875 	const nvpair_t *nvp;						\
1876 									\
1877 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name);			\
1878 	if (nvp == NULL)						\
1879 		nvlist_report_missing(NV_TYPE_##TYPE, name);		\
1880 	return (nvpair_get_##type(nvp));				\
1881 }
1882 
1883 NVLIST_GET(bool, bool, BOOL)
1884 NVLIST_GET(uint64_t, number, NUMBER)
1885 NVLIST_GET(const char *, string, STRING)
1886 NVLIST_GET(const nvlist_t *, nvlist, NVLIST)
1887 #ifndef _KERNEL
1888 NVLIST_GET(int, descriptor, DESCRIPTOR)
1889 #endif
1890 
1891 #undef	NVLIST_GET
1892 
1893 const void *
1894 nvlist_get_binary(const nvlist_t *nvl, const char *name, size_t *sizep)
1895 {
1896 	nvpair_t *nvp;
1897 
1898 	nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1899 	if (nvp == NULL)
1900 		nvlist_report_missing(NV_TYPE_BINARY, name);
1901 
1902 	return (nvpair_get_binary(nvp, sizep));
1903 }
1904 
1905 #define	NVLIST_GET_ARRAY(ftype, type, TYPE)				\
1906 ftype									\
1907 nvlist_get_##type##_array(const nvlist_t *nvl, const char *name,	\
1908     size_t *nitems)							\
1909 {									\
1910 	const nvpair_t *nvp;						\
1911 									\
1912 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1913 	if (nvp == NULL)						\
1914 		nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name);	\
1915 	return (nvpair_get_##type##_array(nvp, nitems));		\
1916 }
1917 
1918 NVLIST_GET_ARRAY(const bool *, bool, BOOL)
1919 NVLIST_GET_ARRAY(const uint64_t *, number, NUMBER)
1920 NVLIST_GET_ARRAY(const char * const *, string, STRING)
1921 NVLIST_GET_ARRAY(const nvlist_t * const *, nvlist, NVLIST)
1922 #ifndef _KERNEL
1923 NVLIST_GET_ARRAY(const int *, descriptor, DESCRIPTOR)
1924 #endif
1925 
1926 #undef	NVLIST_GET_ARRAY
1927 
1928 #define	NVLIST_TAKE(ftype, type, TYPE)					\
1929 ftype									\
1930 nvlist_take_##type(nvlist_t *nvl, const char *name)			\
1931 {									\
1932 	nvpair_t *nvp;							\
1933 	ftype value;							\
1934 									\
1935 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE, name);			\
1936 	if (nvp == NULL)						\
1937 		nvlist_report_missing(NV_TYPE_##TYPE, name);		\
1938 	value = (ftype)(intptr_t)nvpair_get_##type(nvp);		\
1939 	nvlist_remove_nvpair(nvl, nvp);					\
1940 	nvpair_free_structure(nvp);					\
1941 	return (value);							\
1942 }
1943 
1944 NVLIST_TAKE(bool, bool, BOOL)
1945 NVLIST_TAKE(uint64_t, number, NUMBER)
1946 NVLIST_TAKE(char *, string, STRING)
1947 NVLIST_TAKE(nvlist_t *, nvlist, NVLIST)
1948 #ifndef _KERNEL
1949 NVLIST_TAKE(int, descriptor, DESCRIPTOR)
1950 #endif
1951 
1952 #undef	NVLIST_TAKE
1953 
1954 void *
1955 nvlist_take_binary(nvlist_t *nvl, const char *name, size_t *sizep)
1956 {
1957 	nvpair_t *nvp;
1958 	void *value;
1959 
1960 	nvp = nvlist_find(nvl, NV_TYPE_BINARY, name);
1961 	if (nvp == NULL)
1962 		nvlist_report_missing(NV_TYPE_BINARY, name);
1963 
1964 	value = (void *)(intptr_t)nvpair_get_binary(nvp, sizep);
1965 	nvlist_remove_nvpair(nvl, nvp);
1966 	nvpair_free_structure(nvp);
1967 	return (value);
1968 }
1969 
1970 #define	NVLIST_TAKE_ARRAY(ftype, type, TYPE)				\
1971 ftype									\
1972 nvlist_take_##type##_array(nvlist_t *nvl, const char *name,		\
1973     size_t *nitems)							\
1974 {									\
1975 	nvpair_t *nvp;							\
1976 	ftype value;							\
1977 									\
1978 	nvp = nvlist_find(nvl, NV_TYPE_##TYPE##_ARRAY, name);		\
1979 	if (nvp == NULL)						\
1980 		nvlist_report_missing(NV_TYPE_##TYPE##_ARRAY, name);	\
1981 	value = (ftype)(intptr_t)nvpair_get_##type##_array(nvp, nitems);\
1982 	nvlist_remove_nvpair(nvl, nvp);					\
1983 	nvpair_free_structure(nvp);					\
1984 	return (value);							\
1985 }
1986 
1987 NVLIST_TAKE_ARRAY(bool *, bool, BOOL)
1988 NVLIST_TAKE_ARRAY(uint64_t *, number, NUMBER)
1989 NVLIST_TAKE_ARRAY(char **, string, STRING)
1990 NVLIST_TAKE_ARRAY(nvlist_t **, nvlist, NVLIST)
1991 #ifndef _KERNEL
1992 NVLIST_TAKE_ARRAY(int *, descriptor, DESCRIPTOR)
1993 #endif
1994 
1995 void
1996 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1997 {
1998 
1999 	NVLIST_ASSERT(nvl);
2000 	NVPAIR_ASSERT(nvp);
2001 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2002 
2003 	nvpair_remove(&nvl->nvl_head, nvp, nvl);
2004 	nvlist_update_size(nvl, nvp, -1);
2005 }
2006 
2007 void
2008 nvlist_free(nvlist_t *nvl, const char *name)
2009 {
2010 
2011 	nvlist_free_type(nvl, name, NV_TYPE_NONE);
2012 }
2013 
2014 #define	NVLIST_FREE(type, TYPE)						\
2015 void									\
2016 nvlist_free_##type(nvlist_t *nvl, const char *name)			\
2017 {									\
2018 									\
2019 	nvlist_free_type(nvl, name, NV_TYPE_##TYPE);			\
2020 }
2021 
2022 NVLIST_FREE(null, NULL)
2023 NVLIST_FREE(bool, BOOL)
2024 NVLIST_FREE(number, NUMBER)
2025 NVLIST_FREE(string, STRING)
2026 NVLIST_FREE(nvlist, NVLIST)
2027 NVLIST_FREE(binary, BINARY)
2028 NVLIST_FREE(bool_array, BOOL_ARRAY)
2029 NVLIST_FREE(number_array, NUMBER_ARRAY)
2030 NVLIST_FREE(string_array, STRING_ARRAY)
2031 NVLIST_FREE(nvlist_array, NVLIST_ARRAY)
2032 #ifndef _KERNEL
2033 NVLIST_FREE(descriptor, DESCRIPTOR)
2034 NVLIST_FREE(descriptor_array, DESCRIPTOR_ARRAY)
2035 #endif
2036 
2037 #undef	NVLIST_FREE
2038 
2039 void
2040 nvlist_free_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2041 {
2042 
2043 	NVLIST_ASSERT(nvl);
2044 	NVPAIR_ASSERT(nvp);
2045 	PJDLOG_ASSERT(nvpair_nvlist(nvp) == nvl);
2046 
2047 	nvlist_remove_nvpair(nvl, nvp);
2048 	nvpair_free(nvp);
2049 }
2050 
2051