]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/libnv/nvpair.c
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[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         nv_free(str);
1197         return (nvp);
1198 }
1199
1200 nvpair_t *
1201 nvpair_create_null(const char *name)
1202 {
1203
1204         return (nvpair_allocv(name, NV_TYPE_NULL, 0, 0, 0));
1205 }
1206
1207 nvpair_t *
1208 nvpair_create_bool(const char *name, bool value)
1209 {
1210
1211         return (nvpair_allocv(name, NV_TYPE_BOOL, value ? 1 : 0,
1212             sizeof(uint8_t), 0));
1213 }
1214
1215 nvpair_t *
1216 nvpair_create_number(const char *name, uint64_t value)
1217 {
1218
1219         return (nvpair_allocv(name, NV_TYPE_NUMBER, value, sizeof(value), 0));
1220 }
1221
1222 nvpair_t *
1223 nvpair_create_string(const char *name, const char *value)
1224 {
1225         nvpair_t *nvp;
1226         size_t size;
1227         char *data;
1228
1229         if (value == NULL) {
1230                 ERRNO_SET(EINVAL);
1231                 return (NULL);
1232         }
1233
1234         data = nv_strdup(value);
1235         if (data == NULL)
1236                 return (NULL);
1237         size = strlen(value) + 1;
1238
1239         nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)data,
1240             size, 0);
1241         if (nvp == NULL)
1242                 nv_free(data);
1243
1244         return (nvp);
1245 }
1246
1247 nvpair_t *
1248 nvpair_create_nvlist(const char *name, const nvlist_t *value)
1249 {
1250         nvlist_t *nvl;
1251         nvpair_t *nvp;
1252
1253         if (value == NULL) {
1254                 ERRNO_SET(EINVAL);
1255                 return (NULL);
1256         }
1257
1258         nvl = nvlist_clone(value);
1259         if (nvl == NULL)
1260                 return (NULL);
1261
1262         nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)nvl, 0,
1263             0);
1264         if (nvp == NULL)
1265                 nvlist_destroy(nvl);
1266         else
1267                 nvlist_set_parent(nvl, nvp);
1268
1269         return (nvp);
1270 }
1271
1272 #ifndef _KERNEL
1273 nvpair_t *
1274 nvpair_create_descriptor(const char *name, int value)
1275 {
1276         nvpair_t *nvp;
1277
1278         value = fcntl(value, F_DUPFD_CLOEXEC, 0);
1279         if (value < 0)
1280                 return (NULL);
1281
1282         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1283             sizeof(int64_t), 0);
1284         if (nvp == NULL) {
1285                 ERRNO_SAVE();
1286                 close(value);
1287                 ERRNO_RESTORE();
1288         }
1289
1290         return (nvp);
1291 }
1292 #endif
1293
1294 nvpair_t *
1295 nvpair_create_binary(const char *name, const void *value, size_t size)
1296 {
1297         nvpair_t *nvp;
1298         void *data;
1299
1300         if (value == NULL || size == 0) {
1301                 ERRNO_SET(EINVAL);
1302                 return (NULL);
1303         }
1304
1305         data = nv_malloc(size);
1306         if (data == NULL)
1307                 return (NULL);
1308         memcpy(data, value, size);
1309
1310         nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)data,
1311             size, 0);
1312         if (nvp == NULL)
1313                 nv_free(data);
1314
1315         return (nvp);
1316 }
1317
1318 nvpair_t *
1319 nvpair_create_bool_array(const char *name, const bool *value, size_t nitems)
1320 {
1321         nvpair_t *nvp;
1322         size_t size;
1323         void *data;
1324
1325         if (value == NULL || nitems == 0) {
1326                 ERRNO_SET(EINVAL);
1327                 return (NULL);
1328         }
1329
1330         size = sizeof(value[0]) * nitems;
1331         data = nv_malloc(size);
1332         if (data == NULL)
1333                 return (NULL);
1334
1335         memcpy(data, value, size);
1336         nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY, (uint64_t)(uintptr_t)data,
1337             size, nitems);
1338         if (nvp == NULL) {
1339                 ERRNO_SAVE();
1340                 nv_free(data);
1341                 ERRNO_RESTORE();
1342         }
1343
1344         return (nvp);
1345 }
1346
1347 nvpair_t *
1348 nvpair_create_number_array(const char *name, const uint64_t *value,
1349     size_t nitems)
1350 {
1351         nvpair_t *nvp;
1352         size_t size;
1353         void *data;
1354
1355         if (value == NULL || nitems == 0) {
1356                 ERRNO_SET(EINVAL);
1357                 return (NULL);
1358         }
1359
1360         size = sizeof(value[0]) * nitems;
1361         data = nv_malloc(size);
1362         if (data == NULL)
1363                 return (NULL);
1364
1365         memcpy(data, value, size);
1366         nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1367             (uint64_t)(uintptr_t)data, size, nitems);
1368         if (nvp == NULL) {
1369                 ERRNO_SAVE();
1370                 nv_free(data);
1371                 ERRNO_RESTORE();
1372         }
1373
1374         return (nvp);
1375 }
1376
1377 nvpair_t *
1378 nvpair_create_string_array(const char *name, const char * const *value,
1379     size_t nitems)
1380 {
1381         nvpair_t *nvp;
1382         unsigned int ii;
1383         size_t datasize, size;
1384         char **data;
1385
1386         if (value == NULL || nitems == 0) {
1387                 ERRNO_SET(EINVAL);
1388                 return (NULL);
1389         }
1390
1391         nvp = NULL;
1392         datasize = 0;
1393         data = nv_malloc(sizeof(value[0]) * nitems);
1394         if (data == NULL)
1395                 return (NULL);
1396
1397         for (ii = 0; ii < nitems; ii++) {
1398                 if (value[ii] == NULL) {
1399                         ERRNO_SET(EINVAL);
1400                         goto fail;
1401                 }
1402
1403                 size = strlen(value[ii]) + 1;
1404                 datasize += size;
1405                 data[ii] = nv_strdup(value[ii]);
1406                 if (data[ii] == NULL)
1407                         goto fail;
1408         }
1409         nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1410             (uint64_t)(uintptr_t)data, datasize, nitems);
1411
1412 fail:
1413         if (nvp == NULL) {
1414                 ERRNO_SAVE();
1415                 for (; ii > 0; ii--)
1416                         nv_free(data[ii - 1]);
1417                 nv_free(data);
1418                 ERRNO_RESTORE();
1419         }
1420
1421         return (nvp);
1422 }
1423
1424 nvpair_t *
1425 nvpair_create_nvlist_array(const char *name, const nvlist_t * const *value,
1426     size_t nitems)
1427 {
1428         unsigned int ii;
1429         nvlist_t **nvls;
1430         nvpair_t *parent;
1431         int flags;
1432
1433         nvls = NULL;
1434
1435         if (value == NULL || nitems == 0) {
1436                 ERRNO_SET(EINVAL);
1437                 return (NULL);
1438         }
1439
1440         nvls = nv_malloc(sizeof(value[0]) * nitems);
1441         if (nvls == NULL)
1442                 return (NULL);
1443
1444         for (ii = 0; ii < nitems; ii++) {
1445                 if (value[ii] == NULL) {
1446                         ERRNO_SET(EINVAL);
1447                         goto fail;
1448                 }
1449
1450                 nvls[ii] = nvlist_clone(value[ii]);
1451                 if (nvls[ii] == NULL)
1452                         goto fail;
1453
1454                 if (ii > 0) {
1455                         nvpair_t *nvp;
1456
1457                         nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1458                             (uint64_t)(uintptr_t)nvls[ii], 0, 0);
1459                         if (nvp == NULL) {
1460                                 ERRNO_SAVE();
1461                                 nvlist_destroy(nvls[ii]);
1462                                 ERRNO_RESTORE();
1463                                 goto fail;
1464                         }
1465                         nvlist_set_array_next(nvls[ii - 1], nvp);
1466                 }
1467         }
1468         flags = nvlist_flags(nvls[nitems - 1]) | NV_FLAG_IN_ARRAY;
1469         nvlist_set_flags(nvls[nitems - 1], flags);
1470
1471         parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1472             (uint64_t)(uintptr_t)nvls, 0, nitems);
1473         if (parent == NULL)
1474                 goto fail;
1475
1476         for (ii = 0; ii < nitems; ii++)
1477                 nvlist_set_parent(nvls[ii], parent);
1478
1479         return (parent);
1480
1481 fail:
1482         ERRNO_SAVE();
1483         for (; ii > 0; ii--)
1484                 nvlist_destroy(nvls[ii - 1]);
1485         nv_free(nvls);
1486         ERRNO_RESTORE();
1487
1488         return (NULL);
1489 }
1490
1491 #ifndef _KERNEL
1492 nvpair_t *
1493 nvpair_create_descriptor_array(const char *name, const int *value,
1494     size_t nitems)
1495 {
1496         unsigned int ii;
1497         nvpair_t *nvp;
1498         int *fds;
1499
1500         if (value == NULL) {
1501                 ERRNO_SET(EINVAL);
1502                 return (NULL);
1503         }
1504
1505         nvp = NULL;
1506
1507         fds = nv_malloc(sizeof(value[0]) * nitems);
1508         if (fds == NULL)
1509                 return (NULL);
1510         for (ii = 0; ii < nitems; ii++) {
1511                 if (value[ii] == -1) {
1512                         fds[ii] = -1;
1513                 } else {
1514                         fds[ii] = fcntl(value[ii], F_DUPFD_CLOEXEC, 0);
1515                         if (fds[ii] == -1)
1516                                 goto fail;
1517                 }
1518         }
1519
1520         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1521             (uint64_t)(uintptr_t)fds, sizeof(int64_t) * nitems, nitems);
1522
1523 fail:
1524         if (nvp == NULL) {
1525                 ERRNO_SAVE();
1526                 for (; ii > 0; ii--) {
1527                         if (fds[ii - 1] != -1)
1528                                 close(fds[ii - 1]);
1529                 }
1530                 nv_free(fds);
1531                 ERRNO_RESTORE();
1532         }
1533
1534         return (nvp);
1535 }
1536 #endif
1537
1538 nvpair_t *
1539 nvpair_move_string(const char *name, char *value)
1540 {
1541         nvpair_t *nvp;
1542
1543         if (value == NULL) {
1544                 ERRNO_SET(EINVAL);
1545                 return (NULL);
1546         }
1547
1548         nvp = nvpair_allocv(name, NV_TYPE_STRING, (uint64_t)(uintptr_t)value,
1549             strlen(value) + 1, 0);
1550         if (nvp == NULL) {
1551                 ERRNO_SAVE();
1552                 nv_free(value);
1553                 ERRNO_RESTORE();
1554         }
1555
1556         return (nvp);
1557 }
1558
1559 nvpair_t *
1560 nvpair_move_nvlist(const char *name, nvlist_t *value)
1561 {
1562         nvpair_t *nvp;
1563
1564         if (value == NULL || nvlist_get_nvpair_parent(value) != NULL) {
1565                 ERRNO_SET(EINVAL);
1566                 return (NULL);
1567         }
1568
1569         if (nvlist_error(value) != 0) {
1570                 ERRNO_SET(nvlist_error(value));
1571                 nvlist_destroy(value);
1572                 return (NULL);
1573         }
1574
1575         nvp = nvpair_allocv(name, NV_TYPE_NVLIST, (uint64_t)(uintptr_t)value,
1576             0, 0);
1577         if (nvp == NULL)
1578                 nvlist_destroy(value);
1579         else
1580                 nvlist_set_parent(value, nvp);
1581
1582         return (nvp);
1583 }
1584
1585 #ifndef _KERNEL
1586 nvpair_t *
1587 nvpair_move_descriptor(const char *name, int value)
1588 {
1589         nvpair_t *nvp;
1590
1591         if (value < 0 || !fd_is_valid(value)) {
1592                 ERRNO_SET(EBADF);
1593                 return (NULL);
1594         }
1595
1596         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR, (uint64_t)value,
1597             sizeof(int64_t), 0);
1598         if (nvp == NULL) {
1599                 ERRNO_SAVE();
1600                 close(value);
1601                 ERRNO_RESTORE();
1602         }
1603
1604         return (nvp);
1605 }
1606 #endif
1607
1608 nvpair_t *
1609 nvpair_move_binary(const char *name, void *value, size_t size)
1610 {
1611         nvpair_t *nvp;
1612
1613         if (value == NULL || size == 0) {
1614                 ERRNO_SET(EINVAL);
1615                 return (NULL);
1616         }
1617
1618         nvp = nvpair_allocv(name, NV_TYPE_BINARY, (uint64_t)(uintptr_t)value,
1619             size, 0);
1620         if (nvp == NULL) {
1621                 ERRNO_SAVE();
1622                 nv_free(value);
1623                 ERRNO_RESTORE();
1624         }
1625
1626         return (nvp);
1627 }
1628
1629 nvpair_t *
1630 nvpair_move_bool_array(const char *name, bool *value, size_t nitems)
1631 {
1632         nvpair_t *nvp;
1633
1634         if (value == NULL || nitems == 0) {
1635                 ERRNO_SET(EINVAL);
1636                 return (NULL);
1637         }
1638
1639         nvp = nvpair_allocv(name, NV_TYPE_BOOL_ARRAY,
1640             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1641         if (nvp == NULL) {
1642                 ERRNO_SAVE();
1643                 nv_free(value);
1644                 ERRNO_RESTORE();
1645         }
1646
1647         return (nvp);
1648 }
1649
1650 nvpair_t *
1651 nvpair_move_string_array(const char *name, char **value, size_t nitems)
1652 {
1653         nvpair_t *nvp;
1654         size_t i, size;
1655
1656         if (value == NULL || nitems == 0) {
1657                 ERRNO_SET(EINVAL);
1658                 return (NULL);
1659         }
1660
1661         size = 0;
1662         for (i = 0; i < nitems; i++) {
1663                 if (value[i] == NULL) {
1664                         ERRNO_SET(EINVAL);
1665                         return (NULL);
1666                 }
1667
1668                 size += strlen(value[i]) + 1;
1669         }
1670
1671         nvp = nvpair_allocv(name, NV_TYPE_STRING_ARRAY,
1672             (uint64_t)(uintptr_t)value, size, nitems);
1673         if (nvp == NULL) {
1674                 ERRNO_SAVE();
1675                 for (i = 0; i < nitems; i++)
1676                         nv_free(value[i]);
1677                 nv_free(value);
1678                 ERRNO_RESTORE();
1679         }
1680
1681         return (nvp);
1682 }
1683
1684 nvpair_t *
1685 nvpair_move_number_array(const char *name, uint64_t *value, size_t nitems)
1686 {
1687         nvpair_t *nvp;
1688
1689         if (value == NULL || nitems == 0) {
1690                 ERRNO_SET(EINVAL);
1691                 return (NULL);
1692         }
1693
1694         nvp = nvpair_allocv(name, NV_TYPE_NUMBER_ARRAY,
1695             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1696         if (nvp == NULL) {
1697                 ERRNO_SAVE();
1698                 nv_free(value);
1699                 ERRNO_RESTORE();
1700         }
1701
1702         return (nvp);
1703 }
1704
1705 nvpair_t *
1706 nvpair_move_nvlist_array(const char *name, nvlist_t **value, size_t nitems)
1707 {
1708         nvpair_t *parent;
1709         unsigned int ii;
1710         int flags;
1711
1712         if (value == NULL || nitems == 0) {
1713                 ERRNO_SET(EINVAL);
1714                 return (NULL);
1715         }
1716
1717         for (ii = 0; ii < nitems; ii++) {
1718                 if (value == NULL || nvlist_error(value[ii]) != 0 ||
1719                     nvlist_get_pararr(value[ii], NULL) != NULL) {
1720                         ERRNO_SET(EINVAL);
1721                         goto fail;
1722                 }
1723                 if (ii > 0) {
1724                         nvpair_t *nvp;
1725
1726                         nvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1727                             (uint64_t)(uintptr_t)value[ii], 0, 0);
1728                         if (nvp == NULL)
1729                                 goto fail;
1730                         nvlist_set_array_next(value[ii - 1], nvp);
1731                 }
1732         }
1733         flags = nvlist_flags(value[nitems - 1]) | NV_FLAG_IN_ARRAY;
1734         nvlist_set_flags(value[nitems - 1], flags);
1735
1736         parent = nvpair_allocv(name, NV_TYPE_NVLIST_ARRAY,
1737             (uint64_t)(uintptr_t)value, 0, nitems);
1738         if (parent == NULL)
1739                 goto fail;
1740
1741         for (ii = 0; ii < nitems; ii++)
1742                 nvlist_set_parent(value[ii], parent);
1743
1744         return (parent);
1745 fail:
1746         ERRNO_SAVE();
1747         for (ii = 0; ii < nitems; ii++) {
1748                 if (value[ii] != NULL &&
1749                     nvlist_get_pararr(value[ii], NULL) != NULL) {
1750                         nvlist_destroy(value[ii]);
1751                 }
1752         }
1753         nv_free(value);
1754         ERRNO_RESTORE();
1755
1756         return (NULL);
1757 }
1758
1759 #ifndef _KERNEL
1760 nvpair_t *
1761 nvpair_move_descriptor_array(const char *name, int *value, size_t nitems)
1762 {
1763         nvpair_t *nvp;
1764         size_t i;
1765
1766         if (value == NULL || nitems == 0) {
1767                 ERRNO_SET(EINVAL);
1768                 return (NULL);
1769         }
1770
1771         for (i = 0; i < nitems; i++) {
1772                 if (value[i] != -1 && !fd_is_valid(value[i])) {
1773                         ERRNO_SET(EBADF);
1774                         goto fail;
1775                 }
1776         }
1777
1778         nvp = nvpair_allocv(name, NV_TYPE_DESCRIPTOR_ARRAY,
1779             (uint64_t)(uintptr_t)value, sizeof(value[0]) * nitems, nitems);
1780         if (nvp == NULL)
1781                 goto fail;
1782
1783         return (nvp);
1784 fail:
1785         ERRNO_SAVE();
1786         for (i = 0; i < nitems; i++) {
1787                 if (fd_is_valid(value[i]))
1788                         close(value[i]);
1789         }
1790         nv_free(value);
1791         ERRNO_RESTORE();
1792
1793         return (NULL);
1794 }
1795 #endif
1796
1797 bool
1798 nvpair_get_bool(const nvpair_t *nvp)
1799 {
1800
1801         NVPAIR_ASSERT(nvp);
1802
1803         return (nvp->nvp_data == 1);
1804 }
1805
1806 uint64_t
1807 nvpair_get_number(const nvpair_t *nvp)
1808 {
1809
1810         NVPAIR_ASSERT(nvp);
1811
1812         return (nvp->nvp_data);
1813 }
1814
1815 const char *
1816 nvpair_get_string(const nvpair_t *nvp)
1817 {
1818
1819         NVPAIR_ASSERT(nvp);
1820         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING);
1821
1822         return ((const char *)(intptr_t)nvp->nvp_data);
1823 }
1824
1825 const nvlist_t *
1826 nvpair_get_nvlist(const nvpair_t *nvp)
1827 {
1828
1829         NVPAIR_ASSERT(nvp);
1830         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST);
1831
1832         return ((const nvlist_t *)(intptr_t)nvp->nvp_data);
1833 }
1834
1835 #ifndef _KERNEL
1836 int
1837 nvpair_get_descriptor(const nvpair_t *nvp)
1838 {
1839
1840         NVPAIR_ASSERT(nvp);
1841         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR);
1842
1843         return ((int)nvp->nvp_data);
1844 }
1845 #endif
1846
1847 const void *
1848 nvpair_get_binary(const nvpair_t *nvp, size_t *sizep)
1849 {
1850
1851         NVPAIR_ASSERT(nvp);
1852         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BINARY);
1853
1854         if (sizep != NULL)
1855                 *sizep = nvp->nvp_datasize;
1856
1857         return ((const void *)(intptr_t)nvp->nvp_data);
1858 }
1859
1860 const bool *
1861 nvpair_get_bool_array(const nvpair_t *nvp, size_t *nitems)
1862 {
1863
1864         NVPAIR_ASSERT(nvp);
1865         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1866
1867         if (nitems != NULL)
1868                 *nitems = nvp->nvp_nitems;
1869
1870         return ((const bool *)(intptr_t)nvp->nvp_data);
1871 }
1872
1873 const uint64_t *
1874 nvpair_get_number_array(const nvpair_t *nvp, size_t *nitems)
1875 {
1876
1877         NVPAIR_ASSERT(nvp);
1878         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1879
1880         if (nitems != NULL)
1881                 *nitems = nvp->nvp_nitems;
1882
1883         return ((const uint64_t *)(intptr_t)nvp->nvp_data);
1884 }
1885
1886 const char * const *
1887 nvpair_get_string_array(const nvpair_t *nvp, size_t *nitems)
1888 {
1889
1890         NVPAIR_ASSERT(nvp);
1891         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1892
1893         if (nitems != NULL)
1894                 *nitems = nvp->nvp_nitems;
1895
1896         return ((const char * const *)(intptr_t)nvp->nvp_data);
1897 }
1898
1899 const nvlist_t * const *
1900 nvpair_get_nvlist_array(const nvpair_t *nvp, size_t *nitems)
1901 {
1902
1903         NVPAIR_ASSERT(nvp);
1904         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1905
1906         if (nitems != NULL)
1907                 *nitems = nvp->nvp_nitems;
1908
1909         return ((const nvlist_t * const *)((intptr_t)nvp->nvp_data));
1910 }
1911
1912 #ifndef _KERNEL
1913 const int *
1914 nvpair_get_descriptor_array(const nvpair_t *nvp, size_t *nitems)
1915 {
1916
1917         NVPAIR_ASSERT(nvp);
1918         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
1919
1920         if (nitems != NULL)
1921                 *nitems = nvp->nvp_nitems;
1922
1923         return ((const int *)(intptr_t)nvp->nvp_data);
1924 }
1925 #endif
1926
1927 int
1928 nvpair_append_bool_array(nvpair_t *nvp, const bool value)
1929 {
1930
1931         NVPAIR_ASSERT(nvp);
1932         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_BOOL_ARRAY);
1933         return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
1934 }
1935
1936 int
1937 nvpair_append_number_array(nvpair_t *nvp, const uint64_t value)
1938 {
1939
1940         NVPAIR_ASSERT(nvp);
1941         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NUMBER_ARRAY);
1942         return (nvpair_append(nvp, &value, sizeof(value), sizeof(value)));
1943 }
1944
1945 int
1946 nvpair_append_string_array(nvpair_t *nvp, const char *value)
1947 {
1948         char *str;
1949
1950         NVPAIR_ASSERT(nvp);
1951         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_STRING_ARRAY);
1952         if (value == NULL) {
1953                 ERRNO_SET(EINVAL);
1954                 return (-1);
1955         }
1956         str = nv_strdup(value);
1957         if (str == NULL) {
1958                 return (-1);
1959         }
1960         if (nvpair_append(nvp, &str, sizeof(str), strlen(str) + 1) == -1) {
1961                 nv_free(str);
1962                 return (-1);
1963         }
1964         return (0);
1965 }
1966
1967 int
1968 nvpair_append_nvlist_array(nvpair_t *nvp, const nvlist_t *value)
1969 {
1970         nvpair_t *tmpnvp;
1971         nvlist_t *nvl, *prev;
1972         int flags;
1973
1974         NVPAIR_ASSERT(nvp);
1975         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_NVLIST_ARRAY);
1976         if (value == NULL || nvlist_error(value) != 0 ||
1977             nvlist_get_pararr(value, NULL) != NULL) {
1978                 ERRNO_SET(EINVAL);
1979                 return (-1);
1980         }
1981         nvl = nvlist_clone(value);
1982         if (nvl == NULL) {
1983                 return (-1);
1984         }
1985         flags = nvlist_flags(nvl) | NV_FLAG_IN_ARRAY;
1986         nvlist_set_flags(nvl, flags);
1987
1988         tmpnvp = NULL;
1989         prev = NULL;
1990         if (nvp->nvp_nitems > 0) {
1991                 nvlist_t **nvls = (void *)(uintptr_t)nvp->nvp_data;
1992
1993                 prev = nvls[nvp->nvp_nitems - 1];
1994                 PJDLOG_ASSERT(prev != NULL);
1995
1996                 tmpnvp = nvpair_allocv(" ", NV_TYPE_NVLIST,
1997                     (uint64_t)(uintptr_t)nvl, 0, 0);
1998                 if (tmpnvp == NULL) {
1999                         goto fail;
2000                 }
2001         }
2002         if (nvpair_append(nvp, &nvl, sizeof(nvl), 0) == -1) {
2003                 goto fail;
2004         }
2005         if (tmpnvp) {
2006                 NVPAIR_ASSERT(tmpnvp);
2007                 nvlist_set_array_next(prev, tmpnvp);
2008         }
2009         nvlist_set_parent(nvl, nvp);
2010         return (0);
2011 fail:
2012         if (tmpnvp) {
2013                 nvpair_free(tmpnvp);
2014         }
2015         nvlist_destroy(nvl);
2016         return (-1);
2017 }
2018
2019 #ifndef _KERNEL
2020 int
2021 nvpair_append_descriptor_array(nvpair_t *nvp, const int value)
2022 {
2023         int fd;
2024
2025         NVPAIR_ASSERT(nvp);
2026         PJDLOG_ASSERT(nvp->nvp_type == NV_TYPE_DESCRIPTOR_ARRAY);
2027         fd = fcntl(value, F_DUPFD_CLOEXEC, 0);
2028         if (fd == -1) {
2029                 return (-1);
2030         }
2031         if (nvpair_append(nvp, &fd, sizeof(fd), sizeof(fd)) == -1) {
2032                 close(fd);
2033                 return (-1);
2034         }
2035         return (0);
2036 }
2037 #endif
2038
2039 void
2040 nvpair_free(nvpair_t *nvp)
2041 {
2042         size_t i;
2043
2044         NVPAIR_ASSERT(nvp);
2045         PJDLOG_ASSERT(nvp->nvp_list == NULL);
2046
2047         nvp->nvp_magic = 0;
2048         switch (nvp->nvp_type) {
2049 #ifndef _KERNEL
2050         case NV_TYPE_DESCRIPTOR:
2051                 close((int)nvp->nvp_data);
2052                 break;
2053         case NV_TYPE_DESCRIPTOR_ARRAY:
2054                 for (i = 0; i < nvp->nvp_nitems; i++)
2055                         close(((int *)(intptr_t)nvp->nvp_data)[i]);
2056                 nv_free((int *)(intptr_t)nvp->nvp_data);
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