]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/libnv/nvpair.c
Update clang, llvm, lld, lldb, compiler-rt and libc++ version number to
[FreeBSD/FreeBSD.git] / sys / contrib / libnv / nvpair.c
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/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/systm.h>
46
47 #include <machine/stdarg.h>
48
49 #else
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdarg.h>
53 #include <stdbool.h>
54 #include <stdint.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 #include "common_impl.h"
60 #endif
61
62 #ifdef HAVE_PJDLOG
63 #include <pjdlog.h>
64 #endif
65
66 #include <sys/nv.h>
67
68 #include "nv_impl.h"
69 #include "nvlist_impl.h"
70 #include "nvpair_impl.h"
71
72 #ifndef HAVE_PJDLOG
73 #ifdef _KERNEL
74 #define PJDLOG_ASSERT(...)              MPASS(__VA_ARGS__)
75 #define PJDLOG_RASSERT(expr, ...)       KASSERT(expr, (__VA_ARGS__))
76 #define PJDLOG_ABORT(...)               panic(__VA_ARGS__)
77 #else
78 #include <assert.h>
79 #define PJDLOG_ASSERT(...)              assert(__VA_ARGS__)
80 #define PJDLOG_RASSERT(expr, ...)       assert(expr)
81 #define PJDLOG_ABORT(...)               abort()
82 #endif
83 #endif
84
85 #define NVPAIR_MAGIC    0x6e7670        /* "nvp" */
86 struct nvpair {
87         int              nvp_magic;
88         char            *nvp_name;
89         int              nvp_type;
90         uint64_t         nvp_data;
91         size_t           nvp_datasize;
92         size_t           nvp_nitems;    /* Used only for array types. */
93         nvlist_t        *nvp_list;
94         TAILQ_ENTRY(nvpair) nvp_next;
95 };
96
97 #define NVPAIR_ASSERT(nvp)      do {                                    \
98         PJDLOG_ASSERT((nvp) != NULL);                                   \
99         PJDLOG_ASSERT((nvp)->nvp_magic == NVPAIR_MAGIC);                \
100 } while (0)
101
102 struct nvpair_header {
103         uint8_t         nvph_type;
104         uint16_t        nvph_namesize;
105         uint64_t        nvph_datasize;
106         uint64_t        nvph_nitems;
107 } __packed;
108
109
110 void
111 nvpair_assert(const nvpair_t *nvp)
112 {
113
114         NVPAIR_ASSERT(nvp);
115 }
116
117 static nvpair_t *
118 nvpair_allocv(const char *name, int type, uint64_t data, size_t datasize,
119     size_t nitems)
120 {
121         nvpair_t *nvp;
122         size_t namelen;
123
124         PJDLOG_ASSERT(type >= NV_TYPE_FIRST && type <= NV_TYPE_LAST);
125
126         namelen = strlen(name);
127         if (namelen >= NV_NAME_MAX) {
128                 ERRNO_SET(ENAMETOOLONG);
129                 return (NULL);
130         }
131
132         nvp = nv_calloc(1, sizeof(*nvp) + namelen + 1);
133         if (nvp != NULL) {
134                 nvp->nvp_name = (char *)(nvp + 1);
135                 memcpy(nvp->nvp_name, name, namelen);
136                 nvp->nvp_name[namelen] = '\0';
137                 nvp->nvp_type = type;
138                 nvp->nvp_data = data;
139                 nvp->nvp_datasize = datasize;
140                 nvp->nvp_nitems = nitems;
141                 nvp->nvp_magic = NVPAIR_MAGIC;
142         }
143
144         return (nvp);
145 }
146
147 static int
148 nvpair_append(nvpair_t *nvp, const void *value, size_t valsize, size_t datasize)
149 {
150         void *olddata, *data, *valp;
151         size_t oldlen;
152
153         oldlen = nvp->nvp_nitems * valsize;
154         olddata = (void *)(uintptr_t)nvp->nvp_data;
155         data = nv_realloc(olddata, oldlen + valsize);
156         if (data == NULL) {
157                 ERRNO_SET(ENOMEM);
158                 return (-1);
159         }
160         valp = (unsigned char *)data + oldlen;
161         memcpy(valp, value, valsize);
162
163         nvp->nvp_data = (uint64_t)(uintptr_t)data;
164         nvp->nvp_datasize += datasize;
165         nvp->nvp_nitems++;
166         return (0);
167 }
168
169 nvlist_t *
170 nvpair_nvlist(const nvpair_t *nvp)
171 {
172
173         NVPAIR_ASSERT(nvp);
174
175         return (nvp->nvp_list);
176 }
177
178 nvpair_t *
179 nvpair_next(const nvpair_t *nvp)
180 {
181
182         NVPAIR_ASSERT(nvp);
183         PJDLOG_ASSERT(nvp->nvp_list != NULL);
184
185         return (TAILQ_NEXT(nvp, nvp_next));
186 }
187
188 nvpair_t *
189 nvpair_prev(const nvpair_t *nvp)
190 {
191
192         NVPAIR_ASSERT(nvp);
193         PJDLOG_ASSERT(nvp->nvp_list != NULL);
194
195         return (TAILQ_PREV(nvp, nvl_head, nvp_next));
196 }
197
198 void
199 nvpair_insert(struct nvl_head *head, nvpair_t *nvp, nvlist_t *nvl)
200 {
201
202         NVPAIR_ASSERT(nvp);
203         PJDLOG_ASSERT(nvp->nvp_list == NULL);
204         PJDLOG_ASSERT((nvlist_flags(nvl) & NV_FLAG_NO_UNIQUE) != 0 ||
205             !nvlist_exists(nvl, nvpair_name(nvp)));
206
207         TAILQ_INSERT_TAIL(head, nvp, nvp_next);
208         nvp->nvp_list = nvl;
209 }
210
211 static void
212 nvpair_remove_nvlist(nvpair_t *nvp)
213 {
214         nvlist_t *nvl;
215
216         /* XXX: DECONST is bad, mkay? */
217         nvl = __DECONST(nvlist_t *, nvpair_get_nvlist(nvp));
218         PJDLOG_ASSERT(nvl != NULL);
219         nvlist_set_parent(nvl, NULL);
220 }
221
222 static void
223 nvpair_remove_nvlist_array(nvpair_t *nvp)
224 {
225         nvlist_t **nvlarray;
226         size_t count, i;
227
228         /* XXX: DECONST is bad, mkay? */
229         nvlarray = __DECONST(nvlist_t **,
230             nvpair_get_nvlist_array(nvp, &count));
231         for (i = 0; i < count; i++) {
232                 nvlist_set_array_next(nvlarray[i], NULL);
233                 nvlist_set_parent(nvlarray[i], NULL);
234         }
235 }
236
237 void
238 nvpair_remove(struct nvl_head *head, nvpair_t *nvp, const nvlist_t *nvl)
239 {
240
241         NVPAIR_ASSERT(nvp);
242         PJDLOG_ASSERT(nvp->nvp_list == nvl);
243
244         if (nvpair_type(nvp) == NV_TYPE_NVLIST)
245                 nvpair_remove_nvlist(nvp);
246         else if (nvpair_type(nvp) == NV_TYPE_NVLIST_ARRAY)
247                 nvpair_remove_nvlist_array(nvp);
248
249         TAILQ_REMOVE(head, nvp, nvp_next);
250         nvp->nvp_list = NULL;
251 }
252
253 nvpair_t *
254 nvpair_clone(const nvpair_t *nvp)
255 {
256         nvpair_t *newnvp;
257         const char *name;
258         const void *data;
259         size_t datasize;
260
261         NVPAIR_ASSERT(nvp);
262
263         name = nvpair_name(nvp);
264
265         switch (nvpair_type(nvp)) {
266         case NV_TYPE_NULL:
267                 newnvp = nvpair_create_null(name);
268                 break;
269         case NV_TYPE_BOOL:
270                 newnvp = nvpair_create_bool(name, nvpair_get_bool(nvp));
271                 break;
272         case NV_TYPE_NUMBER:
273                 newnvp = nvpair_create_number(name, nvpair_get_number(nvp));
274                 break;
275         case NV_TYPE_STRING:
276                 newnvp = nvpair_create_string(name, nvpair_get_string(nvp));
277                 break;
278         case NV_TYPE_NVLIST:
279                 newnvp = nvpair_create_nvlist(name, nvpair_get_nvlist(nvp));
280                 break;
281         case NV_TYPE_BINARY:
282                 data = nvpair_get_binary(nvp, &datasize);
283                 newnvp = nvpair_create_binary(name, data, datasize);
284                 break;
285         case NV_TYPE_BOOL_ARRAY:
286                 data = nvpair_get_bool_array(nvp, &datasize);
287                 newnvp = nvpair_create_bool_array(name, data, datasize);
288                 break;
289         case NV_TYPE_NUMBER_ARRAY:
290                 data = nvpair_get_number_array(nvp, &datasize);
291                 newnvp = nvpair_create_number_array(name, data, datasize);
292                 break;
293         case NV_TYPE_STRING_ARRAY:
294                 data = nvpair_get_string_array(nvp, &datasize);
295                 newnvp = nvpair_create_string_array(name, data, datasize);
296                 break;
297         case NV_TYPE_NVLIST_ARRAY:
298                 data = nvpair_get_nvlist_array(nvp, &datasize);
299                 newnvp = nvpair_create_nvlist_array(name, data, datasize);
300                 break;
301 #ifndef _KERNEL
302         case NV_TYPE_DESCRIPTOR:
303                 newnvp = nvpair_create_descriptor(name,
304                     nvpair_get_descriptor(nvp));
305                 break;
306         case NV_TYPE_DESCRIPTOR_ARRAY:
307                 data = nvpair_get_descriptor_array(nvp, &datasize);
308                 newnvp = nvpair_create_descriptor_array(name, data, datasize);
309                 break;
310 #endif
311         default:
312                 PJDLOG_ABORT("Unknown type: %d.", nvpair_type(nvp));
313         }
314
315         return (newnvp);
316 }
317
318 size_t
319 nvpair_header_size(void)
320 {
321
322         return (sizeof(struct nvpair_header));
323 }
324
325 size_t
326 nvpair_size(const nvpair_t *nvp)
327 {
328
329         NVPAIR_ASSERT(nvp);
330
331         return (nvp->nvp_datasize);
332 }
333
334 unsigned char *
335 nvpair_pack_header(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
336 {
337         struct nvpair_header nvphdr;
338         size_t namesize;
339
340         NVPAIR_ASSERT(nvp);
341
342         nvphdr.nvph_type = nvp->nvp_type;
343         namesize = strlen(nvp->nvp_name) + 1;
344         PJDLOG_ASSERT(namesize > 0 && namesize <= UINT16_MAX);
345         nvphdr.nvph_namesize = namesize;
346         nvphdr.nvph_datasize = nvp->nvp_datasize;
347         nvphdr.nvph_nitems = nvp->nvp_nitems;
348         PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
349         memcpy(ptr, &nvphdr, sizeof(nvphdr));
350         ptr += sizeof(nvphdr);
351         *leftp -= sizeof(nvphdr);
352
353         PJDLOG_ASSERT(*leftp >= namesize);
354         memcpy(ptr, nvp->nvp_name, namesize);
355         ptr += namesize;
356         *leftp -= namesize;
357
358         return (ptr);
359 }
360
361 unsigned char *
362 nvpair_pack_null(const nvpair_t *nvp, unsigned char *ptr,
363     size_t *leftp __unused)
364 {
365
366         NVPAIR_ASSERT(nvp);
367         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
368
369         return (ptr);
370 }
371
372 unsigned char *
373 nvpair_pack_bool(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
374 {
375         uint8_t value;
376
377         NVPAIR_ASSERT(nvp);
378         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
379
380         value = (uint8_t)nvp->nvp_data;
381
382         PJDLOG_ASSERT(*leftp >= sizeof(value));
383         memcpy(ptr, &value, sizeof(value));
384         ptr += sizeof(value);
385         *leftp -= sizeof(value);
386
387         return (ptr);
388 }
389
390 unsigned char *
391 nvpair_pack_number(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
392 {
393         uint64_t value;
394
395         NVPAIR_ASSERT(nvp);
396         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
397
398         value = (uint64_t)nvp->nvp_data;
399
400         PJDLOG_ASSERT(*leftp >= sizeof(value));
401         memcpy(ptr, &value, sizeof(value));
402         ptr += sizeof(value);
403         *leftp -= sizeof(value);
404
405         return (ptr);
406 }
407
408 unsigned char *
409 nvpair_pack_string(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
410 {
411
412         NVPAIR_ASSERT(nvp);
413         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
414
415         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
416         memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
417         ptr += nvp->nvp_datasize;
418         *leftp -= nvp->nvp_datasize;
419
420         return (ptr);
421 }
422
423 unsigned char *
424 nvpair_pack_nvlist_up(unsigned char *ptr, size_t *leftp)
425 {
426         struct nvpair_header nvphdr;
427         size_t namesize;
428         const char *name = "";
429
430         namesize = 1;
431         nvphdr.nvph_type = NV_TYPE_NVLIST_UP;
432         nvphdr.nvph_namesize = namesize;
433         nvphdr.nvph_datasize = 0;
434         nvphdr.nvph_nitems = 0;
435         PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
436         memcpy(ptr, &nvphdr, sizeof(nvphdr));
437         ptr += sizeof(nvphdr);
438         *leftp -= sizeof(nvphdr);
439
440         PJDLOG_ASSERT(*leftp >= namesize);
441         memcpy(ptr, name, namesize);
442         ptr += namesize;
443         *leftp -= namesize;
444
445         return (ptr);
446 }
447
448 unsigned char *
449 nvpair_pack_nvlist_array_next(unsigned char *ptr, size_t *leftp)
450 {
451         struct nvpair_header nvphdr;
452         size_t namesize;
453         const char *name = "";
454
455         namesize = 1;
456         nvphdr.nvph_type = NV_TYPE_NVLIST_ARRAY_NEXT;
457         nvphdr.nvph_namesize = namesize;
458         nvphdr.nvph_datasize = 0;
459         nvphdr.nvph_nitems = 0;
460         PJDLOG_ASSERT(*leftp >= sizeof(nvphdr));
461         memcpy(ptr, &nvphdr, sizeof(nvphdr));
462         ptr += sizeof(nvphdr);
463         *leftp -= sizeof(nvphdr);
464
465         PJDLOG_ASSERT(*leftp >= namesize);
466         memcpy(ptr, name, namesize);
467         ptr += namesize;
468         *leftp -= namesize;
469
470         return (ptr);
471 }
472
473 #ifndef _KERNEL
474 unsigned char *
475 nvpair_pack_descriptor(const nvpair_t *nvp, unsigned char *ptr, int64_t *fdidxp,
476     size_t *leftp)
477 {
478         int64_t value;
479
480         NVPAIR_ASSERT(nvp);
481         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
482
483         value = (int64_t)nvp->nvp_data;
484         if (value != -1) {
485                 /*
486                  * If there is a real descriptor here, we change its number
487                  * to position in the array of descriptors send via control
488                  * message.
489                  */
490                 PJDLOG_ASSERT(fdidxp != NULL);
491
492                 value = *fdidxp;
493                 (*fdidxp)++;
494         }
495
496         PJDLOG_ASSERT(*leftp >= sizeof(value));
497         memcpy(ptr, &value, sizeof(value));
498         ptr += sizeof(value);
499         *leftp -= sizeof(value);
500
501         return (ptr);
502 }
503 #endif
504
505 unsigned char *
506 nvpair_pack_binary(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
507 {
508
509         NVPAIR_ASSERT(nvp);
510         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
511
512         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
513         memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
514         ptr += nvp->nvp_datasize;
515         *leftp -= nvp->nvp_datasize;
516
517         return (ptr);
518 }
519
520 unsigned char *
521 nvpair_pack_bool_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
522 {
523
524         NVPAIR_ASSERT(nvp);
525         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
526         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
527
528         memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
529         ptr += nvp->nvp_datasize;
530         *leftp -= nvp->nvp_datasize;
531
532         return (ptr);
533 }
534
535 unsigned char *
536 nvpair_pack_number_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
537 {
538
539         NVPAIR_ASSERT(nvp);
540         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
541         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
542
543         memcpy(ptr, (const void *)(intptr_t)nvp->nvp_data, nvp->nvp_datasize);
544         ptr += nvp->nvp_datasize;
545         *leftp -= nvp->nvp_datasize;
546
547         return (ptr);
548 }
549
550 unsigned char *
551 nvpair_pack_string_array(const nvpair_t *nvp, unsigned char *ptr, size_t *leftp)
552 {
553         unsigned int ii;
554         size_t size, len;
555         const char * const *array;
556
557         NVPAIR_ASSERT(nvp);
558         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
559         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
560
561         size = 0;
562         array = nvpair_get_string_array(nvp, NULL);
563         PJDLOG_ASSERT(array != NULL);
564
565         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
566                 len = strlen(array[ii]) + 1;
567                 PJDLOG_ASSERT(*leftp >= len);
568
569                 memcpy(ptr, (const void *)array[ii], len);
570                 size += len;
571                 ptr += len;
572                 *leftp -= len;
573         }
574
575         PJDLOG_ASSERT(size == nvp->nvp_datasize);
576
577         return (ptr);
578 }
579
580 #ifndef _KERNEL
581 unsigned char *
582 nvpair_pack_descriptor_array(const nvpair_t *nvp, unsigned char *ptr,
583     int64_t *fdidxp, size_t *leftp)
584 {
585         int64_t value;
586         const int *array;
587         unsigned int ii;
588
589         NVPAIR_ASSERT(nvp);
590         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
591         PJDLOG_ASSERT(*leftp >= nvp->nvp_datasize);
592
593         array = nvpair_get_descriptor_array(nvp, NULL);
594         PJDLOG_ASSERT(array != NULL);
595
596         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
597                 PJDLOG_ASSERT(*leftp >= sizeof(value));
598
599                 value = array[ii];
600                 if (value != -1) {
601                         /*
602                          * If there is a real descriptor here, we change its
603                          * number to position in the array of descriptors send
604                          * via control message.
605                          */
606                         PJDLOG_ASSERT(fdidxp != NULL);
607
608                         value = *fdidxp;
609                         (*fdidxp)++;
610                 }
611                 memcpy(ptr, &value, sizeof(value));
612                 ptr += sizeof(value);
613                 *leftp -= sizeof(value);
614         }
615
616         return (ptr);
617 }
618 #endif
619
620 void
621 nvpair_init_datasize(nvpair_t *nvp)
622 {
623
624         NVPAIR_ASSERT(nvp);
625
626         if (nvp->nvp_type == NV_TYPE_NVLIST) {
627                 if (nvp->nvp_data == 0) {
628                         nvp->nvp_datasize = 0;
629                 } else {
630                         nvp->nvp_datasize =
631                             nvlist_size((const nvlist_t *)(intptr_t)nvp->nvp_data);
632                 }
633         }
634 }
635
636 const unsigned char *
637 nvpair_unpack_header(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
638     size_t *leftp)
639 {
640         struct nvpair_header nvphdr;
641
642         if (*leftp < sizeof(nvphdr))
643                 goto fail;
644
645         memcpy(&nvphdr, ptr, sizeof(nvphdr));
646         ptr += sizeof(nvphdr);
647         *leftp -= sizeof(nvphdr);
648
649 #if NV_TYPE_FIRST > 0
650         if (nvphdr.nvph_type < NV_TYPE_FIRST)
651                 goto fail;
652 #endif
653         if (nvphdr.nvph_type > NV_TYPE_LAST &&
654             nvphdr.nvph_type != NV_TYPE_NVLIST_UP &&
655             nvphdr.nvph_type != NV_TYPE_NVLIST_ARRAY_NEXT) {
656                 goto fail;
657         }
658
659 #if BYTE_ORDER == BIG_ENDIAN
660         if (!isbe) {
661                 nvphdr.nvph_namesize = le16toh(nvphdr.nvph_namesize);
662                 nvphdr.nvph_datasize = le64toh(nvphdr.nvph_datasize);
663         }
664 #else
665         if (isbe) {
666                 nvphdr.nvph_namesize = be16toh(nvphdr.nvph_namesize);
667                 nvphdr.nvph_datasize = be64toh(nvphdr.nvph_datasize);
668         }
669 #endif
670
671         if (nvphdr.nvph_namesize > NV_NAME_MAX)
672                 goto fail;
673         if (*leftp < nvphdr.nvph_namesize)
674                 goto fail;
675         if (nvphdr.nvph_namesize < 1)
676                 goto fail;
677         if (strnlen((const char *)ptr, nvphdr.nvph_namesize) !=
678             (size_t)(nvphdr.nvph_namesize - 1)) {
679                 goto fail;
680         }
681
682         memcpy(nvp->nvp_name, ptr, nvphdr.nvph_namesize);
683         ptr += nvphdr.nvph_namesize;
684         *leftp -= nvphdr.nvph_namesize;
685
686         if (*leftp < nvphdr.nvph_datasize)
687                 goto fail;
688
689         nvp->nvp_type = nvphdr.nvph_type;
690         nvp->nvp_data = 0;
691         nvp->nvp_datasize = nvphdr.nvph_datasize;
692         nvp->nvp_nitems = nvphdr.nvph_nitems;
693
694         return (ptr);
695 fail:
696         ERRNO_SET(EINVAL);
697         return (NULL);
698 }
699
700 const unsigned char *
701 nvpair_unpack_null(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
702     size_t *leftp __unused)
703 {
704
705         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NULL);
706
707         if (nvp->nvp_datasize != 0) {
708                 ERRNO_SET(EINVAL);
709                 return (NULL);
710         }
711
712         return (ptr);
713 }
714
715 const unsigned char *
716 nvpair_unpack_bool(bool isbe __unused, nvpair_t *nvp, const unsigned char *ptr,
717     size_t *leftp)
718 {
719         uint8_t value;
720
721         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL);
722
723         if (nvp->nvp_datasize != sizeof(value)) {
724                 ERRNO_SET(EINVAL);
725                 return (NULL);
726         }
727         if (*leftp < sizeof(value)) {
728                 ERRNO_SET(EINVAL);
729                 return (NULL);
730         }
731
732         memcpy(&value, ptr, sizeof(value));
733         ptr += sizeof(value);
734         *leftp -= sizeof(value);
735
736         if (value != 0 && value != 1) {
737                 ERRNO_SET(EINVAL);
738                 return (NULL);
739         }
740
741         nvp->nvp_data = (uint64_t)value;
742
743         return (ptr);
744 }
745
746 const unsigned char *
747 nvpair_unpack_number(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
748      size_t *leftp)
749 {
750
751         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER);
752
753         if (nvp->nvp_datasize != sizeof(uint64_t)) {
754                 ERRNO_SET(EINVAL);
755                 return (NULL);
756         }
757         if (*leftp < sizeof(uint64_t)) {
758                 ERRNO_SET(EINVAL);
759                 return (NULL);
760         }
761
762         if (isbe)
763                 nvp->nvp_data = be64dec(ptr);
764         else
765                 nvp->nvp_data = le64dec(ptr);
766
767         ptr += sizeof(uint64_t);
768         *leftp -= sizeof(uint64_t);
769
770         return (ptr);
771 }
772
773 const unsigned char *
774 nvpair_unpack_string(bool isbe __unused, nvpair_t *nvp,
775     const unsigned char *ptr, size_t *leftp)
776 {
777
778         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
779
780         if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
781                 ERRNO_SET(EINVAL);
782                 return (NULL);
783         }
784
785         if (strnlen((const char *)ptr, nvp->nvp_datasize) !=
786             nvp->nvp_datasize - 1) {
787                 ERRNO_SET(EINVAL);
788                 return (NULL);
789         }
790
791         nvp->nvp_data = (uint64_t)(uintptr_t)nv_strdup((const char *)ptr);
792         if (nvp->nvp_data == 0)
793                 return (NULL);
794
795         ptr += nvp->nvp_datasize;
796         *leftp -= nvp->nvp_datasize;
797
798         return (ptr);
799 }
800
801 const unsigned char *
802 nvpair_unpack_nvlist(bool isbe __unused, nvpair_t *nvp,
803     const unsigned char *ptr, size_t *leftp, size_t nfds, nvlist_t **child)
804 {
805         nvlist_t *value;
806
807         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
808
809         if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
810                 ERRNO_SET(EINVAL);
811                 return (NULL);
812         }
813
814         value = nvlist_create(0);
815         if (value == NULL)
816                 return (NULL);
817
818         ptr = nvlist_unpack_header(value, ptr, nfds, NULL, leftp);
819         if (ptr == NULL)
820                 return (NULL);
821
822         nvp->nvp_data = (uint64_t)(uintptr_t)value;
823         *child = value;
824
825         return (ptr);
826 }
827
828 #ifndef _KERNEL
829 const unsigned char *
830 nvpair_unpack_descriptor(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
831     size_t *leftp, const int *fds, size_t nfds)
832 {
833         int64_t idx;
834
835         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
836
837         if (nvp->nvp_datasize != sizeof(idx)) {
838                 ERRNO_SET(EINVAL);
839                 return (NULL);
840         }
841         if (*leftp < sizeof(idx)) {
842                 ERRNO_SET(EINVAL);
843                 return (NULL);
844         }
845
846         if (isbe)
847                 idx = be64dec(ptr);
848         else
849                 idx = le64dec(ptr);
850
851         if (idx < 0) {
852                 ERRNO_SET(EINVAL);
853                 return (NULL);
854         }
855
856         if ((size_t)idx >= nfds) {
857                 ERRNO_SET(EINVAL);
858                 return (NULL);
859         }
860
861         nvp->nvp_data = (uint64_t)fds[idx];
862
863         ptr += sizeof(idx);
864         *leftp -= sizeof(idx);
865
866         return (ptr);
867 }
868 #endif
869
870 const unsigned char *
871 nvpair_unpack_binary(bool isbe __unused, nvpair_t *nvp,
872     const unsigned char *ptr, size_t *leftp)
873 {
874         void *value;
875
876         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
877
878         if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0) {
879                 ERRNO_SET(EINVAL);
880                 return (NULL);
881         }
882
883         value = nv_malloc(nvp->nvp_datasize);
884         if (value == NULL)
885                 return (NULL);
886
887         memcpy(value, ptr, nvp->nvp_datasize);
888         ptr += nvp->nvp_datasize;
889         *leftp -= nvp->nvp_datasize;
890
891         nvp->nvp_data = (uint64_t)(uintptr_t)value;
892
893         return (ptr);
894 }
895
896 const unsigned char *
897 nvpair_unpack_bool_array(bool isbe __unused, nvpair_t *nvp,
898     const unsigned char *ptr, size_t *leftp)
899 {
900         uint8_t *value;
901         size_t size;
902         unsigned int i;
903
904         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
905
906         size = sizeof(*value) * nvp->nvp_nitems;
907         if (nvp->nvp_datasize != size || *leftp < size ||
908             nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
909                 ERRNO_SET(EINVAL);
910                 return (NULL);
911         }
912
913         value = nv_malloc(size);
914         if (value == NULL)
915                 return (NULL);
916
917         for (i = 0; i < nvp->nvp_nitems; i++) {
918                 value[i] = *(const uint8_t *)ptr;
919
920                 ptr += sizeof(*value);
921                 *leftp -= sizeof(*value);
922         }
923
924         nvp->nvp_data = (uint64_t)(uintptr_t)value;
925
926         return (ptr);
927 }
928
929 const unsigned char *
930 nvpair_unpack_number_array(bool isbe, nvpair_t *nvp, const unsigned char *ptr,
931      size_t *leftp)
932 {
933         uint64_t *value;
934         size_t size;
935         unsigned int i;
936
937         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
938
939         size = sizeof(*value) * nvp->nvp_nitems;
940         if (nvp->nvp_datasize != size || *leftp < size ||
941             nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
942                 ERRNO_SET(EINVAL);
943                 return (NULL);
944         }
945
946         value = nv_malloc(size);
947         if (value == NULL)
948                 return (NULL);
949
950         for (i = 0; i < nvp->nvp_nitems; i++) {
951                 if (isbe)
952                         value[i] = be64dec(ptr);
953                 else
954                         value[i] = le64dec(ptr);
955
956                 ptr += sizeof(*value);
957                 *leftp -= sizeof(*value);
958         }
959
960         nvp->nvp_data = (uint64_t)(uintptr_t)value;
961
962         return (ptr);
963 }
964
965 const unsigned char *
966 nvpair_unpack_string_array(bool isbe __unused, nvpair_t *nvp,
967     const unsigned char *ptr, size_t *leftp)
968 {
969         ssize_t size;
970         size_t len;
971         const char *tmp;
972         char **value;
973         unsigned int ii, j;
974
975         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
976
977         if (*leftp < nvp->nvp_datasize || nvp->nvp_datasize == 0 ||
978             nvp->nvp_nitems == 0) {
979                 ERRNO_SET(EINVAL);
980                 return (NULL);
981         }
982
983         size = nvp->nvp_datasize;
984         tmp = (const char *)ptr;
985         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
986                 len = strnlen(tmp, size - 1) + 1;
987                 size -= len;
988                 if (size < 0) {
989                         ERRNO_SET(EINVAL);
990                         return (NULL);
991                 }
992                 tmp += len;
993         }
994         if (size != 0) {
995                 ERRNO_SET(EINVAL);
996                 return (NULL);
997         }
998
999         value = nv_malloc(sizeof(*value) * nvp->nvp_nitems);
1000         if (value == NULL)
1001                 return (NULL);
1002
1003         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1004                 value[ii] = nv_strdup((const char *)ptr);
1005                 if (value[ii] == NULL)
1006                         goto out;
1007                 len = strlen(value[ii]) + 1;
1008                 ptr += len;
1009                 *leftp -= len;
1010         }
1011         nvp->nvp_data = (uint64_t)(uintptr_t)value;
1012
1013         return (ptr);
1014 out:
1015         for (j = 0; j < ii; j++)
1016                 nv_free(value[j]);
1017         nv_free(value);
1018         return (NULL);
1019 }
1020
1021 #ifndef _KERNEL
1022 const unsigned char *
1023 nvpair_unpack_descriptor_array(bool isbe, nvpair_t *nvp,
1024     const unsigned char *ptr, size_t *leftp, const int *fds, size_t nfds)
1025 {
1026         int64_t idx;
1027         size_t size;
1028         unsigned int ii;
1029         int *array;
1030
1031         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1032
1033         size = sizeof(idx) * nvp->nvp_nitems;
1034         if (nvp->nvp_datasize != size || *leftp < size ||
1035             nvp->nvp_nitems == 0 || size < nvp->nvp_nitems) {
1036                 ERRNO_SET(EINVAL);
1037                 return (NULL);
1038         }
1039
1040         array = (int *)nv_malloc(size);
1041         if (array == NULL)
1042                 return (NULL);
1043
1044         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1045                 if (isbe)
1046                         idx = be64dec(ptr);
1047                 else
1048                         idx = le64dec(ptr);
1049
1050                 if (idx < 0) {
1051                         ERRNO_SET(EINVAL);
1052                         nv_free(array);
1053                         return (NULL);
1054                 }
1055
1056                 if ((size_t)idx >= nfds) {
1057                         ERRNO_SET(EINVAL);
1058                         nv_free(array);
1059                         return (NULL);
1060                 }
1061
1062                 array[ii] = (uint64_t)fds[idx];
1063
1064                 ptr += sizeof(idx);
1065                 *leftp -= sizeof(idx);
1066         }
1067
1068         nvp->nvp_data = (uint64_t)(uintptr_t)array;
1069
1070         return (ptr);
1071 }
1072 #endif
1073
1074 const unsigned char *
1075 nvpair_unpack_nvlist_array(bool isbe __unused, nvpair_t *nvp,
1076     const unsigned char *ptr, size_t *leftp, nvlist_t **firstel)
1077 {
1078         nvlist_t **value;
1079         nvpair_t *tmpnvp;
1080         unsigned int ii, j;
1081         size_t sizeup;
1082
1083         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1084
1085         sizeup = sizeof(struct nvpair_header) * nvp->nvp_nitems;
1086         if (nvp->nvp_nitems == 0 || sizeup < nvp->nvp_nitems ||
1087             sizeup > *leftp) {
1088                 ERRNO_SET(EINVAL);
1089                 return (NULL);
1090         }
1091
1092         value = nv_malloc(nvp->nvp_nitems * sizeof(*value));
1093         if (value == NULL)
1094                 return (NULL);
1095
1096         for (ii = 0; ii < nvp->nvp_nitems; ii++) {
1097                 value[ii] = nvlist_create(0);
1098                 if (value[ii] == NULL)
1099                         goto fail;
1100                 if (ii > 0) {
1101                         tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1102                             (uint64_t)(uintptr_t)value[ii], 0, 0);
1103                         if (tmpnvp == NULL)
1104                                 goto fail;
1105                         nvlist_set_array_next(value[ii - 1], tmpnvp);
1106                 }
1107         }
1108         nvlist_set_flags(value[nvp->nvp_nitems - 1], NV_FLAG_IN_ARRAY);
1109
1110         nvp->nvp_data = (uint64_t)(uintptr_t)value;
1111         *firstel = value[0];
1112
1113         return (ptr);
1114 fail:
1115         ERRNO_SAVE();
1116         for (j = 0; j <= ii; j++)
1117                 nvlist_destroy(value[j]);
1118         nv_free(value);
1119         ERRNO_RESTORE();
1120
1121         return (NULL);
1122 }
1123
1124 const unsigned char *
1125 nvpair_unpack(bool isbe, const unsigned char *ptr, size_t *leftp,
1126     nvpair_t **nvpp)
1127 {
1128         nvpair_t *nvp, *tmp;
1129
1130         nvp = nv_calloc(1, sizeof(*nvp) + NV_NAME_MAX);
1131         if (nvp == NULL)
1132                 return (NULL);
1133         nvp->nvp_name = (char *)(nvp + 1);
1134
1135         ptr = nvpair_unpack_header(isbe, nvp, ptr, leftp);
1136         if (ptr == NULL)
1137                 goto fail;
1138         tmp = nv_realloc(nvp, sizeof(*nvp) + strlen(nvp->nvp_name) + 1);
1139         if (tmp == NULL)
1140                 goto fail;
1141         nvp = tmp;
1142
1143         /* Update nvp_name after realloc(). */
1144         nvp->nvp_name = (char *)(nvp + 1);
1145         nvp->nvp_data = 0x00;
1146         nvp->nvp_magic = NVPAIR_MAGIC;
1147         *nvpp = nvp;
1148         return (ptr);
1149 fail:
1150         nv_free(nvp);
1151         return (NULL);
1152 }
1153
1154 int
1155 nvpair_type(const nvpair_t *nvp)
1156 {
1157
1158         NVPAIR_ASSERT(nvp);
1159
1160         return (nvp->nvp_type);
1161 }
1162
1163 const char *
1164 nvpair_name(const nvpair_t *nvp)
1165 {
1166
1167         NVPAIR_ASSERT(nvp);
1168
1169         return (nvp->nvp_name);
1170 }
1171
1172 nvpair_t *
1173 nvpair_create_stringf(const char *name, const char *valuefmt, ...)
1174 {
1175         va_list valueap;
1176         nvpair_t *nvp;
1177
1178         va_start(valueap, valuefmt);
1179         nvp = nvpair_create_stringv(name, valuefmt, valueap);
1180         va_end(valueap);
1181
1182         return (nvp);
1183 }
1184
1185 nvpair_t *
1186 nvpair_create_stringv(const char *name, const char *valuefmt, va_list valueap)
1187 {
1188         nvpair_t *nvp;
1189         char *str;
1190         int len;
1191
1192         len = nv_vasprintf(&str, valuefmt, valueap);
1193         if (len < 0)
1194                 return (NULL);
1195         nvp = nvpair_create_string(name, str);
1196         if (nvp == NULL)
1197                 nv_free(str);
1198         return (nvp);
1199 }
1200
1201 nvpair_t *
1202 nvpair_create_null(const char *name)
1203 {
1204
1205         return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0, 0));
1206 }
1207
1208 nvpair_t *
1209 nvpair_create_bool(const char *name, bool value)
1210 {
1211
1212         return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
1213             sizeof(uint8_t), 0));
1214 }
1215
1216 nvpair_t *
1217 nvpair_create_number(const char *name, uint64_t value)
1218 {
1219
1220         return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value), 0));
1221 }
1222
1223 nvpair_t *
1224 nvpair_create_string(const char *name, const char *value)
1225 {
1226         nvpair_t *nvp;
1227         size_t size;
1228         char *data;
1229
1230         if (value == NULL) {
1231                 ERRNO_SET(EINVAL);
1232                 return (NULL);
1233         }
1234
1235         data = nv_strdup(value);
1236         if (data == NULL)
1237                 return (NULL);
1238         size = strlen(value) + 1;
1239
1240         nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
1241             size, 0);
1242         if (nvp == NULL)
1243                 nv_free(data);
1244
1245         return (nvp);
1246 }
1247
1248 nvpair_t *
1249 nvpair_create_nvlist(const char *name, const nvlist_t *value)
1250 {
1251         nvlist_t *nvl;
1252         nvpair_t *nvp;
1253
1254         if (value == NULL) {
1255                 ERRNO_SET(EINVAL);
1256                 return (NULL);
1257         }
1258
1259         nvl = nvlist_clone(value);
1260         if (nvl == NULL)
1261                 return (NULL);
1262
1263         nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0,
1264             0);
1265         if (nvp == NULL)
1266                 nvlist_destroy(nvl);
1267         else
1268                 nvlist_set_parent(nvl, nvp);
1269
1270         return (nvp);
1271 }
1272
1273 #ifndef _KERNEL
1274 nvpair_t *
1275 nvpair_create_descriptor(const char *name, int value)
1276 {
1277         nvpair_t *nvp;
1278
1279         value = fcntl(value, F_DUPFD_CLOEXEC, 0);
1280         if (value < 0)
1281                 return (NULL);
1282
1283         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1284             sizeof(int64_t), 0);
1285         if (nvp == NULL) {
1286                 ERRNO_SAVE();
1287                 close(value);
1288                 ERRNO_RESTORE();
1289         }
1290
1291         return (nvp);
1292 }
1293 #endif
1294
1295 nvpair_t *
1296 nvpair_create_binary(const char *name, const void *value, size_t size)
1297 {
1298         nvpair_t *nvp;
1299         void *data;
1300
1301         if (value == NULL || size == 0) {
1302                 ERRNO_SET(EINVAL);
1303                 return (NULL);
1304         }
1305
1306         data = nv_malloc(size);
1307         if (data == NULL)
1308                 return (NULL);
1309         memcpy(data, value, size);
1310
1311         nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
1312             size, 0);
1313         if (nvp == NULL)
1314                 nv_free(data);
1315
1316         return (nvp);
1317 }
1318
1319 nvpair_t *
1320 nvpair_create_bool_array(const char *name, const bool *value, size_t nitems)
1321 {
1322         nvpair_t *nvp;
1323         size_t size;
1324         void *data;
1325
1326         if (value == NULL || nitems == 0) {
1327                 ERRNO_SET(EINVAL);
1328                 return (NULL);
1329         }
1330
1331         size = sizeof(value[0]) * nitems;
1332         data = nv_malloc(size);
1333         if (data == NULL)
1334                 return (NULL);
1335
1336         memcpy(data, value, size);
1337         nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data,
1338             size, nitems);
1339         if (nvp == NULL) {
1340                 ERRNO_SAVE();
1341                 nv_free(data);
1342                 ERRNO_RESTORE();
1343         }
1344
1345         return (nvp);
1346 }
1347
1348 nvpair_t *
1349 nvpair_create_number_array(const char *name, const uint64_t *value,
1350     size_t nitems)
1351 {
1352         nvpair_t *nvp;
1353         size_t size;
1354         void *data;
1355
1356         if (value == NULL || nitems == 0) {
1357                 ERRNO_SET(EINVAL);
1358                 return (NULL);
1359         }
1360
1361         size = sizeof(value[0]) * nitems;
1362         data = nv_malloc(size);
1363         if (data == NULL)
1364                 return (NULL);
1365
1366         memcpy(data, value, size);
1367         nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1368             (uint64_t)(uintptr_t)data, size, nitems);
1369         if (nvp == NULL) {
1370                 ERRNO_SAVE();
1371                 nv_free(data);
1372                 ERRNO_RESTORE();
1373         }
1374
1375         return (nvp);
1376 }
1377
1378 nvpair_t *
1379 nvpair_create_string_array(const char *name, const char * const *value,
1380     size_t nitems)
1381 {
1382         nvpair_t *nvp;
1383         unsigned int ii;
1384         size_t datasize, size;
1385         char **data;
1386
1387         if (value == NULL || nitems == 0) {
1388                 ERRNO_SET(EINVAL);
1389                 return (NULL);
1390         }
1391
1392         nvp = NULL;
1393         datasize = 0;
1394         data = nv_malloc(sizeof(value[0]) * nitems);
1395         if (data == NULL)
1396                 return (NULL);
1397
1398         for (ii = 0; ii < nitems; ii++) {
1399                 if (value[ii] == NULL) {
1400                         ERRNO_SET(EINVAL);
1401                         goto fail;
1402                 }
1403
1404                 size = strlen(value[ii]) + 1;
1405                 datasize += size;
1406                 data[ii] = nv_strdup(value[ii]);
1407                 if (data[ii] == NULL)
1408                         goto fail;
1409         }
1410         nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1411             (uint64_t)(uintptr_t)data, datasize, nitems);
1412
1413 fail:
1414         if (nvp == NULL) {
1415                 ERRNO_SAVE();
1416                 for (; ii > 0; ii--)
1417                         nv_free(data[ii - 1]);
1418                 nv_free(data);
1419                 ERRNO_RESTORE();
1420         }
1421
1422         return (nvp);
1423 }
1424
1425 nvpair_t *
1426 nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value,
1427     size_t nitems)
1428 {
1429         unsigned int ii;
1430         nvlist_t **nvls;
1431         nvpair_t *parent;
1432         int flags;
1433
1434         nvls = NULL;
1435
1436         if (value == NULL || nitems == 0) {
1437                 ERRNO_SET(EINVAL);
1438                 return (NULL);
1439         }
1440
1441         nvls = nv_malloc(sizeof(value[0]) * nitems);
1442         if (nvls == NULL)
1443                 return (NULL);
1444
1445         for (ii = 0; ii < nitems; ii++) {
1446                 if (value[ii] == NULL) {
1447                         ERRNO_SET(EINVAL);
1448                         goto fail;
1449                 }
1450
1451                 nvls[ii] = nvlist_clone(value[ii]);
1452                 if (nvls[ii] == NULL)
1453                         goto fail;
1454
1455                 if (ii > 0) {
1456                         nvpair_t *nvp;
1457
1458                         nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1459                             (uint64_t)(uintptr_t)nvls[ii], 0, 0);
1460                         if (nvp == NULL) {
1461                                 ERRNO_SAVE();
1462                                 nvlist_destroy(nvls[ii]);
1463                                 ERRNO_RESTORE();
1464                                 goto fail;
1465                         }
1466                         nvlist_set_array_next(nvls[ii - 1], nvp);
1467                 }
1468         }
1469         flags = nvlist_flags(nvls[nitems - 1]) | NV_FLAG_IN_ARRAY;
1470         nvlist_set_flags(nvls[nitems - 1], flags);
1471
1472         parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1473             (uint64_t)(uintptr_t)nvls, 0, nitems);
1474         if (parent == NULL)
1475                 goto fail;
1476
1477         for (ii = 0; ii < nitems; ii++)
1478                 nvlist_set_parent(nvls[ii], parent);
1479
1480         return (parent);
1481
1482 fail:
1483         ERRNO_SAVE();
1484         for (; ii > 0; ii--)
1485                 nvlist_destroy(nvls[ii - 1]);
1486         nv_free(nvls);
1487         ERRNO_RESTORE();
1488
1489         return (NULL);
1490 }
1491
1492 #ifndef _KERNEL
1493 nvpair_t *
1494 nvpair_create_descriptor_array(const char *name, const int *value,
1495     size_t nitems)
1496 {
1497         unsigned int ii;
1498         nvpair_t *nvp;
1499         int *fds;
1500
1501         if (value == NULL) {
1502                 ERRNO_SET(EINVAL);
1503                 return (NULL);
1504         }
1505
1506         nvp = NULL;
1507
1508         fds = nv_malloc(sizeof(value[0]) * nitems);
1509         if (fds == NULL)
1510                 return (NULL);
1511         for (ii = 0; ii < nitems; ii++) {
1512                 if (value[ii] == -1) {
1513                         fds[ii] = -1;
1514                 } else {
1515                         fds[ii] = fcntl(value[ii], F_DUPFD_CLOEXEC, 0);
1516                         if (fds[ii] == -1)
1517                                 goto fail;
1518                 }
1519         }
1520
1521         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1522             (uint64_t)(uintptr_t)fds, sizeof(int64_t) * nitems, nitems);
1523
1524 fail:
1525         if (nvp == NULL) {
1526                 ERRNO_SAVE();
1527                 for (; ii > 0; ii--) {
1528                         if (fds[ii - 1] != -1)
1529                                 close(fds[ii - 1]);
1530                 }
1531                 nv_free(fds);
1532                 ERRNO_RESTORE();
1533         }
1534
1535         return (nvp);
1536 }
1537 #endif
1538
1539 nvpair_t *
1540 nvpair_move_string(const char *name, char *value)
1541 {
1542         nvpair_t *nvp;
1543
1544         if (value == NULL) {
1545                 ERRNO_SET(EINVAL);
1546                 return (NULL);
1547         }
1548
1549         nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
1550             strlen(value) + 1, 0);
1551         if (nvp == NULL) {
1552                 ERRNO_SAVE();
1553                 nv_free(value);
1554                 ERRNO_RESTORE();
1555         }
1556
1557         return (nvp);
1558 }
1559
1560 nvpair_t *
1561 nvpair_move_nvlist(const char *name, nvlist_t *value)
1562 {
1563         nvpair_t *nvp;
1564
1565         if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) {
1566                 ERRNO_SET(EINVAL);
1567                 return (NULL);
1568         }
1569
1570         if (nvlist_error(value) != 0) {
1571                 ERRNO_SET(nvlist_error(value));
1572                 nvlist_destroy(value);
1573                 return (NULL);
1574         }
1575
1576         nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
1577             0, 0);
1578         if (nvp == NULL)
1579                 nvlist_destroy(value);
1580         else
1581                 nvlist_set_parent(value, nvp);
1582
1583         return (nvp);
1584 }
1585
1586 #ifndef _KERNEL
1587 nvpair_t *
1588 nvpair_move_descriptor(const char *name, int value)
1589 {
1590         nvpair_t *nvp;
1591
1592         if (value < 0 || !fd_is_valid(value)) {
1593                 ERRNO_SET(EBADF);
1594                 return (NULL);
1595         }
1596
1597         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1598             sizeof(int64_t), 0);
1599         if (nvp == NULL) {
1600                 ERRNO_SAVE();
1601                 close(value);
1602                 ERRNO_RESTORE();
1603         }
1604
1605         return (nvp);
1606 }
1607 #endif
1608
1609 nvpair_t *
1610 nvpair_move_binary(const char *name, void *value, size_t size)
1611 {
1612         nvpair_t *nvp;
1613
1614         if (value == NULL || size == 0) {
1615                 ERRNO_SET(EINVAL);
1616                 return (NULL);
1617         }
1618
1619         nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
1620             size, 0);
1621         if (nvp == NULL) {
1622                 ERRNO_SAVE();
1623                 nv_free(value);
1624                 ERRNO_RESTORE();
1625         }
1626
1627         return (nvp);
1628 }
1629
1630 nvpair_t *
1631 nvpair_move_bool_array(const char *name, bool *value, size_t nitems)
1632 {
1633         nvpair_t *nvp;
1634
1635         if (value == NULL || nitems == 0) {
1636                 ERRNO_SET(EINVAL);
1637                 return (NULL);
1638         }
1639
1640         nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY,
1641             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1642         if (nvp == NULL) {
1643                 ERRNO_SAVE();
1644                 nv_free(value);
1645                 ERRNO_RESTORE();
1646         }
1647
1648         return (nvp);
1649 }
1650
1651 nvpair_t *
1652 nvpair_move_string_array(const char *name, char **value, size_t nitems)
1653 {
1654         nvpair_t *nvp;
1655         size_t i, size;
1656
1657         if (value == NULL || nitems == 0) {
1658                 ERRNO_SET(EINVAL);
1659                 return (NULL);
1660         }
1661
1662         size = 0;
1663         for (i = 0; i < nitems; i++) {
1664                 if (value[i] == NULL) {
1665                         ERRNO_SET(EINVAL);
1666                         return (NULL);
1667                 }
1668
1669                 size += strlen(value[i]) + 1;
1670         }
1671
1672         nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1673             (uint64_t)(uintptr_t)value, size, nitems);
1674         if (nvp == NULL) {
1675                 ERRNO_SAVE();
1676                 for (i = 0; i < nitems; i++)
1677                         nv_free(value[i]);
1678                 nv_free(value);
1679                 ERRNO_RESTORE();
1680         }
1681
1682         return (nvp);
1683 }
1684
1685 nvpair_t *
1686 nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems)
1687 {
1688         nvpair_t *nvp;
1689
1690         if (value == NULL || nitems == 0) {
1691                 ERRNO_SET(EINVAL);
1692                 return (NULL);
1693         }
1694
1695         nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1696             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1697         if (nvp == NULL) {
1698                 ERRNO_SAVE();
1699                 nv_free(value);
1700                 ERRNO_RESTORE();
1701         }
1702
1703         return (nvp);
1704 }
1705
1706 nvpair_t *
1707 nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems)
1708 {
1709         nvpair_t *parent;
1710         unsigned int ii;
1711         int flags;
1712
1713         if (value == NULL || nitems == 0) {
1714                 ERRNO_SET(EINVAL);
1715                 return (NULL);
1716         }
1717
1718         for (ii = 0; ii < nitems; ii++) {
1719                 if (value == NULL || nvlist_error(value[ii]) != 0 ||
1720                     nvlist_get_pararr(value[ii], NULL) != NULL) {
1721                         ERRNO_SET(EINVAL);
1722                         goto fail;
1723                 }
1724                 if (ii > 0) {
1725                         nvpair_t *nvp;
1726
1727                         nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1728                             (uint64_t)(uintptr_t)value[ii], 0, 0);
1729                         if (nvp == NULL)
1730                                 goto fail;
1731                         nvlist_set_array_next(value[ii - 1], nvp);
1732                 }
1733         }
1734         flags = nvlist_flags(value[nitems - 1]) | NV_FLAG_IN_ARRAY;
1735         nvlist_set_flags(value[nitems - 1], flags);
1736
1737         parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1738             (uint64_t)(uintptr_t)value, 0, nitems);
1739         if (parent == NULL)
1740                 goto fail;
1741
1742         for (ii = 0; ii < nitems; ii++)
1743                 nvlist_set_parent(value[ii], parent);
1744
1745         return (parent);
1746 fail:
1747         ERRNO_SAVE();
1748         for (ii = 0; ii < nitems; ii++) {
1749                 if (value[ii] != NULL &&
1750                     nvlist_get_pararr(value[ii], NULL) != NULL) {
1751                         nvlist_destroy(value[ii]);
1752                 }
1753         }
1754         nv_free(value);
1755         ERRNO_RESTORE();
1756
1757         return (NULL);
1758 }
1759
1760 #ifndef _KERNEL
1761 nvpair_t *
1762 nvpair_move_descriptor_array(const char *name, int *value, size_t nitems)
1763 {
1764         nvpair_t *nvp;
1765         size_t i;
1766
1767         if (value == NULL || nitems == 0) {
1768                 ERRNO_SET(EINVAL);
1769                 return (NULL);
1770         }
1771
1772         for (i = 0; i < nitems; i++) {
1773                 if (value[i] != -1 && !fd_is_valid(value[i])) {
1774                         ERRNO_SET(EBADF);
1775                         goto fail;
1776                 }
1777         }
1778
1779         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1780             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1781         if (nvp == NULL)
1782                 goto fail;
1783
1784         return (nvp);
1785 fail:
1786         ERRNO_SAVE();
1787         for (i = 0; i < nitems; i++) {
1788                 if (fd_is_valid(value[i]))
1789                         close(value[i]);
1790         }
1791         nv_free(value);
1792         ERRNO_RESTORE();
1793
1794         return (NULL);
1795 }
1796 #endif
1797
1798 bool
1799 nvpair_get_bool(const nvpair_t *nvp)
1800 {
1801
1802         NVPAIR_ASSERT(nvp);
1803
1804         return (nvp->nvp_data == 1);
1805 }
1806
1807 uint64_t
1808 nvpair_get_number(const nvpair_t *nvp)
1809 {
1810
1811         NVPAIR_ASSERT(nvp);
1812
1813         return (nvp->nvp_data);
1814 }
1815
1816 const char *
1817 nvpair_get_string(const nvpair_t *nvp)
1818 {
1819
1820         NVPAIR_ASSERT(nvp);
1821         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
1822
1823         return ((const char *)(intptr_t)nvp->nvp_data);
1824 }
1825
1826 const nvlist_t *
1827 nvpair_get_nvlist(const nvpair_t *nvp)
1828 {
1829
1830         NVPAIR_ASSERT(nvp);
1831         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
1832
1833         return ((const nvlist_t *)(intptr_t)nvp->nvp_data);
1834 }
1835
1836 #ifndef _KERNEL
1837 int
1838 nvpair_get_descriptor(const nvpair_t *nvp)
1839 {
1840
1841         NVPAIR_ASSERT(nvp);
1842         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
1843
1844         return ((int)nvp->nvp_data);
1845 }
1846 #endif
1847
1848 const void *
1849 nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
1850 {
1851
1852         NVPAIR_ASSERT(nvp);
1853         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
1854
1855         if (sizep != NULL)
1856                 *sizep = nvp->nvp_datasize;
1857
1858         return ((const void *)(intptr_t)nvp->nvp_data);
1859 }
1860
1861 const bool *
1862 nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitems)
1863 {
1864
1865         NVPAIR_ASSERT(nvp);
1866         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1867
1868         if (nitems != NULL)
1869                 *nitems = nvp->nvp_nitems;
1870
1871         return ((const bool *)(intptr_t)nvp->nvp_data);
1872 }
1873
1874 const uint64_t *
1875 nvpair_get_number_array(const nvpair_t *nvp, size_t *nitems)
1876 {
1877
1878         NVPAIR_ASSERT(nvp);
1879         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1880
1881         if (nitems != NULL)
1882                 *nitems = nvp->nvp_nitems;
1883
1884         return ((const uint64_t *)(intptr_t)nvp->nvp_data);
1885 }
1886
1887 const char * const *
1888 nvpair_get_string_array(const nvpair_t *nvp, size_t *nitems)
1889 {
1890
1891         NVPAIR_ASSERT(nvp);
1892         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1893
1894         if (nitems != NULL)
1895                 *nitems = nvp->nvp_nitems;
1896
1897         return ((const char * const *)(intptr_t)nvp->nvp_data);
1898 }
1899
1900 const nvlist_t * const *
1901 nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitems)
1902 {
1903
1904         NVPAIR_ASSERT(nvp);
1905         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1906
1907         if (nitems != NULL)
1908                 *nitems = nvp->nvp_nitems;
1909
1910         return ((const nvlist_t * const *)((intptr_t)nvp->nvp_data));
1911 }
1912
1913 #ifndef _KERNEL
1914 const int *
1915 nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitems)
1916 {
1917
1918         NVPAIR_ASSERT(nvp);
1919         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1920
1921         if (nitems != NULL)
1922                 *nitems = nvp->nvp_nitems;
1923
1924         return ((const int *)(intptr_t)nvp->nvp_data);
1925 }
1926 #endif
1927
1928 int
1929 nvpair_append_bool_array(nvpair_t *nvp, const bool value)
1930 {
1931
1932         NVPAIR_ASSERT(nvp);
1933         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1934         return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
1935 }
1936
1937 int
1938 nvpair_append_number_array(nvpair_t *nvp, const uint64_t value)
1939 {
1940
1941         NVPAIR_ASSERT(nvp);
1942         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1943         return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
1944 }
1945
1946 int
1947 nvpair_append_string_array(nvpair_t *nvp, const char *value)
1948 {
1949         char *str;
1950
1951         NVPAIR_ASSERT(nvp);
1952         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1953         if (value == NULL) {
1954                 ERRNO_SET(EINVAL);
1955                 return (-1);
1956         }
1957         str = nv_strdup(value);
1958         if (str == NULL) {
1959                 return (-1);
1960         }
1961         if (nvpair_append(nvp, &str, sizeof(str), strlen(str) + 1) == -1) {
1962                 nv_free(str);
1963                 return (-1);
1964         }
1965         return (0);
1966 }
1967
1968 int
1969 nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value)
1970 {
1971         nvpair_t *tmpnvp;
1972         nvlist_t *nvl, *prev;
1973         int flags;
1974
1975         NVPAIR_ASSERT(nvp);
1976         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1977         if (value == NULL || nvlist_error(value) != 0 ||
1978             nvlist_get_pararr(value, NULL) != NULL) {
1979                 ERRNO_SET(EINVAL);
1980                 return (-1);
1981         }
1982         nvl = nvlist_clone(value);
1983         if (nvl == NULL) {
1984                 return (-1);
1985         }
1986         flags = nvlist_flags(nvl) | NV_FLAG_IN_ARRAY;
1987         nvlist_set_flags(nvl, flags);
1988
1989         tmpnvp = NULL;
1990         prev = NULL;
1991         if (nvp->nvp_nitems > 0) {
1992                 nvlist_t **nvls = (void *)(uintptr_t)nvp->nvp_data;
1993
1994                 prev = nvls[nvp->nvp_nitems - 1];
1995                 PJDLOG_ASSERT(prev != NULL);
1996
1997                 tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1998                     (uint64_t)(uintptr_t)nvl, 0, 0);
1999                 if (tmpnvp == NULL) {
2000                         goto fail;
2001                 }
2002         }
2003         if (nvpair_append(nvp, &nvl, sizeof(nvl), 0) == -1) {
2004                 goto fail;
2005         }
2006         if (tmpnvp) {
2007                 NVPAIR_ASSERT(tmpnvp);
2008                 nvlist_set_array_next(prev, tmpnvp);
2009         }
2010         nvlist_set_parent(nvl, nvp);
2011         return (0);
2012 fail:
2013         if (tmpnvp) {
2014                 nvpair_free(tmpnvp);
2015         }
2016         nvlist_destroy(nvl);
2017         return (-1);
2018 }
2019
2020 #ifndef _KERNEL
2021 int
2022 nvpair_append_descriptor_array(nvpair_t *nvp, const int value)
2023 {
2024         int fd;
2025
2026         NVPAIR_ASSERT(nvp);
2027         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
2028         fd = fcntl(value, F_DUPFD_CLOEXEC, 0);
2029         if (fd == -1) {
2030                 return (-1);
2031         }
2032         if (nvpair_append(nvp, &fd, sizeof(fd), sizeof(fd)) == -1) {
2033                 close(fd);
2034                 return (-1);
2035         }
2036         return (0);
2037 }
2038 #endif
2039
2040 void
2041 nvpair_free(nvpair_t *nvp)
2042 {
2043         size_t i;
2044
2045         NVPAIR_ASSERT(nvp);
2046         PJDLOG_ASSERT(nvp->nvp_list == NULL);
2047
2048         nvp->nvp_magic = 0;
2049         switch (nvp->nvp_type) {
2050 #ifndef _KERNEL
2051         case NV_TYPE_DESCRIPTOR:
2052                 close((int)nvp->nvp_data);
2053                 break;
2054         case NV_TYPE_DESCRIPTOR_ARRAY:
2055                 for (i = 0; i < nvp->nvp_nitems; i++)
2056                         close(((int *)(intptr_t)nvp->nvp_data)[i]);
2057                 break;
2058 #endif
2059         case NV_TYPE_NVLIST:
2060                 nvlist_destroy((nvlist_t *)(intptr_t)nvp->nvp_data);
2061                 break;
2062         case NV_TYPE_STRING:
2063                 nv_free((char *)(intptr_t)nvp->nvp_data);
2064                 break;
2065         case NV_TYPE_BINARY:
2066                 nv_free((void *)(intptr_t)nvp->nvp_data);
2067                 break;
2068         case NV_TYPE_NVLIST_ARRAY:
2069                 for (i = 0; i < nvp->nvp_nitems; i++) {
2070                         nvlist_destroy(
2071                             ((nvlist_t **)(intptr_t)nvp->nvp_data)[i]);
2072                 }
2073                 nv_free(((nvlist_t **)(intptr_t)nvp->nvp_data));
2074                 break;
2075         case NV_TYPE_NUMBER_ARRAY:
2076                 nv_free((uint64_t *)(intptr_t)nvp->nvp_data);
2077                 break;
2078         case NV_TYPE_BOOL_ARRAY:
2079                 nv_free((bool *)(intptr_t)nvp->nvp_data);
2080                 break;
2081         case NV_TYPE_STRING_ARRAY:
2082                 for (i = 0; i < nvp->nvp_nitems; i++)
2083                         nv_free(((char **)(intptr_t)nvp->nvp_data)[i]);
2084                 nv_free((char **)(intptr_t)nvp->nvp_data);
2085                 break;
2086         }
2087         nv_free(nvp);
2088 }
2089
2090 void
2091 nvpair_free_structure(nvpair_t *nvp)
2092 {
2093
2094         NVPAIR_ASSERT(nvp);
2095         PJDLOG_ASSERT(nvp->nvp_list == NULL);
2096
2097         nvp->nvp_magic = 0;
2098         nv_free(nvp);
2099 }
2100
2101 const char *
2102 nvpair_type_string(int type)
2103 {
2104
2105         switch (type) {
2106         case NV_TYPE_NULL:
2107                 return ("NULL");
2108         case NV_TYPE_BOOL:
2109                 return ("BOOL");
2110         case NV_TYPE_NUMBER:
2111                 return ("NUMBER");
2112         case NV_TYPE_STRING:
2113                 return ("STRING");
2114         case NV_TYPE_NVLIST:
2115                 return ("NVLIST");
2116         case NV_TYPE_DESCRIPTOR:
2117                 return ("DESCRIPTOR");
2118         case NV_TYPE_BINARY:
2119                 return ("BINARY");
2120         case NV_TYPE_BOOL_ARRAY:
2121                 return ("BOOL ARRAY");
2122         case NV_TYPE_NUMBER_ARRAY:
2123                 return ("NUMBER ARRAY");
2124         case NV_TYPE_STRING_ARRAY:
2125                 return ("STRING ARRAY");
2126         case NV_TYPE_NVLIST_ARRAY:
2127                 return ("NVLIST ARRAY");
2128         case NV_TYPE_DESCRIPTOR_ARRAY:
2129                 return ("DESCRIPTOR ARRAY");
2130         default:
2131                 return ("<UNKNOWN>");
2132         }
2133 }
2134