Always validate checksums for Direct I/O reads
[zfs.git] / module / nvpair / nvpair.c
blob887f7d32df4a4eb508cbc11a95552d905c6f0932
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015, 2017 by Delphix. All rights reserved.
25 * Copyright 2018 RackTop Systems.
29 * Links to Illumos.org for more information on Interface Libraries:
30 * [1] https://illumos.org/man/3lib/libnvpair
31 * [2] https://illumos.org/man/3nvpair/nvlist_alloc
32 * [3] https://illumos.org/man/9f/nvlist_alloc
33 * [4] https://illumos.org/man/9f/nvlist_next_nvpair
34 * [5] https://illumos.org/man/9f/nvpair_value_byte
37 #include <sys/debug.h>
38 #include <sys/isa_defs.h>
39 #include <sys/nvpair.h>
40 #include <sys/nvpair_impl.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/string.h>
44 #include <rpc/types.h>
45 #include <rpc/xdr.h>
46 #include <sys/mod.h>
48 #if defined(_KERNEL)
49 #include <sys/sunddi.h>
50 #include <sys/sysmacros.h>
51 #else
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include <stddef.h>
55 #endif
57 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) (p)++
60 * nvpair.c - Provides kernel & userland interfaces for manipulating
61 * name-value pairs.
63 * Overview Diagram
65 * +--------------+
66 * | nvlist_t |
67 * |--------------|
68 * | nvl_version |
69 * | nvl_nvflag |
70 * | nvl_priv -+-+
71 * | nvl_flag | |
72 * | nvl_pad | |
73 * +--------------+ |
74 * V
75 * +--------------+ last i_nvp in list
76 * | nvpriv_t | +--------------------->
77 * |--------------| |
78 * +--+- nvp_list | | +------------+
79 * | | nvp_last -+--+ + nv_alloc_t |
80 * | | nvp_curr | |------------|
81 * | | nvp_nva -+----> | nva_ops |
82 * | | nvp_stat | | nva_arg |
83 * | +--------------+ +------------+
84 * |
85 * +-------+
86 * V
87 * +---------------------+ +-------------------+
88 * | i_nvp_t | +-->| i_nvp_t | +-->
89 * |---------------------| | |-------------------| |
90 * | nvi_next -+--+ | nvi_next -+--+
91 * | nvi_prev (NULL) | <----+ nvi_prev |
92 * | . . . . . . . . . . | | . . . . . . . . . |
93 * | nvp (nvpair_t) | | nvp (nvpair_t) |
94 * | - nvp_size | | - nvp_size |
95 * | - nvp_name_sz | | - nvp_name_sz |
96 * | - nvp_value_elem | | - nvp_value_elem |
97 * | - nvp_type | | - nvp_type |
98 * | - data ... | | - data ... |
99 * +---------------------+ +-------------------+
103 * +---------------------+ +---------------------+
104 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
105 * |---------------------| | | |---------------------|
106 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
107 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
108 * | . . . . . . . . . | | . . . . . . . . . |
109 * | nvp (nvpair_t) | | nvp (nvpair_t) |
110 * | - nvp_size | | - nvp_size |
111 * | - nvp_name_sz | | - nvp_name_sz |
112 * | - nvp_value_elem | | - nvp_value_elem |
113 * | - DATA_TYPE_NVLIST | | - nvp_type |
114 * | - data (embedded) | | - data ... |
115 * | nvlist name | +---------------------+
116 * | +--------------+ |
117 * | | nvlist_t | |
118 * | |--------------| |
119 * | | nvl_version | |
120 * | | nvl_nvflag | |
121 * | | nvl_priv --+---+---->
122 * | | nvl_flag | |
123 * | | nvl_pad | |
124 * | +--------------+ |
125 * +---------------------+
128 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
129 * allow value to be aligned on 8 byte boundary
131 * name_len is the length of the name string including the null terminator
132 * so it must be >= 1
134 #define NVP_SIZE_CALC(name_len, data_len) \
135 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
137 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
138 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
139 uint_t nelem, const void *data);
141 #define NV_STAT_EMBEDDED 0x1
142 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
143 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
145 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
146 #define NVPAIR2I_NVP(nvp) \
147 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
149 #ifdef _KERNEL
150 static const int nvpair_max_recursion = 20;
151 #else
152 static const int nvpair_max_recursion = 100;
153 #endif
155 static const uint64_t nvlist_hashtable_init_size = (1 << 4);
158 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
160 va_list valist;
161 int err = 0;
163 nva->nva_ops = nvo;
164 nva->nva_arg = NULL;
166 va_start(valist, nvo);
167 if (nva->nva_ops->nv_ao_init != NULL)
168 err = nva->nva_ops->nv_ao_init(nva, valist);
169 va_end(valist);
171 return (err);
174 void
175 nv_alloc_reset(nv_alloc_t *nva)
177 if (nva->nva_ops->nv_ao_reset != NULL)
178 nva->nva_ops->nv_ao_reset(nva);
181 void
182 nv_alloc_fini(nv_alloc_t *nva)
184 if (nva->nva_ops->nv_ao_fini != NULL)
185 nva->nva_ops->nv_ao_fini(nva);
188 nv_alloc_t *
189 nvlist_lookup_nv_alloc(nvlist_t *nvl)
191 nvpriv_t *priv;
193 if (nvl == NULL ||
194 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
195 return (NULL);
197 return (priv->nvp_nva);
200 static void *
201 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
203 nv_alloc_t *nva = nvp->nvp_nva;
204 void *buf;
206 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
207 memset(buf, 0, size);
209 return (buf);
212 static void
213 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
215 nv_alloc_t *nva = nvp->nvp_nva;
217 nva->nva_ops->nv_ao_free(nva, buf, size);
220 static void
221 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
223 memset(priv, 0, sizeof (nvpriv_t));
225 priv->nvp_nva = nva;
226 priv->nvp_stat = stat;
229 static nvpriv_t *
230 nv_priv_alloc(nv_alloc_t *nva)
232 nvpriv_t *priv;
235 * nv_mem_alloc() cannot called here because it needs the priv
236 * argument.
238 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
239 return (NULL);
241 nv_priv_init(priv, nva, 0);
243 return (priv);
247 * Embedded lists need their own nvpriv_t's. We create a new
248 * nvpriv_t using the parameters and allocator from the parent
249 * list's nvpriv_t.
251 static nvpriv_t *
252 nv_priv_alloc_embedded(nvpriv_t *priv)
254 nvpriv_t *emb_priv;
256 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
257 return (NULL);
259 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
261 return (emb_priv);
264 static int
265 nvt_tab_alloc(nvpriv_t *priv, uint64_t buckets)
267 ASSERT3P(priv->nvp_hashtable, ==, NULL);
268 ASSERT0(priv->nvp_nbuckets);
269 ASSERT0(priv->nvp_nentries);
271 i_nvp_t **tab = nv_mem_zalloc(priv, buckets * sizeof (i_nvp_t *));
272 if (tab == NULL)
273 return (ENOMEM);
275 priv->nvp_hashtable = tab;
276 priv->nvp_nbuckets = buckets;
277 return (0);
280 static void
281 nvt_tab_free(nvpriv_t *priv)
283 i_nvp_t **tab = priv->nvp_hashtable;
284 if (tab == NULL) {
285 ASSERT0(priv->nvp_nbuckets);
286 ASSERT0(priv->nvp_nentries);
287 return;
290 nv_mem_free(priv, tab, priv->nvp_nbuckets * sizeof (i_nvp_t *));
292 priv->nvp_hashtable = NULL;
293 priv->nvp_nbuckets = 0;
294 priv->nvp_nentries = 0;
297 static uint32_t
298 nvt_hash(const char *p)
300 uint32_t g, hval = 0;
302 while (*p) {
303 hval = (hval << 4) + *p++;
304 if ((g = (hval & 0xf0000000)) != 0)
305 hval ^= g >> 24;
306 hval &= ~g;
308 return (hval);
311 static boolean_t
312 nvt_nvpair_match(const nvpair_t *nvp1, const nvpair_t *nvp2, uint32_t nvflag)
314 boolean_t match = B_FALSE;
315 if (nvflag & NV_UNIQUE_NAME_TYPE) {
316 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0 &&
317 NVP_TYPE(nvp1) == NVP_TYPE(nvp2))
318 match = B_TRUE;
319 } else {
320 ASSERT(nvflag == 0 || nvflag & NV_UNIQUE_NAME);
321 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0)
322 match = B_TRUE;
324 return (match);
327 static nvpair_t *
328 nvt_lookup_name_type(const nvlist_t *nvl, const char *name, data_type_t type)
330 const nvpriv_t *priv = (const nvpriv_t *)(uintptr_t)nvl->nvl_priv;
331 ASSERT(priv != NULL);
333 i_nvp_t **tab = priv->nvp_hashtable;
335 if (tab == NULL) {
336 ASSERT3P(priv->nvp_list, ==, NULL);
337 ASSERT0(priv->nvp_nbuckets);
338 ASSERT0(priv->nvp_nentries);
339 return (NULL);
340 } else {
341 ASSERT(priv->nvp_nbuckets != 0);
344 uint64_t hash = nvt_hash(name);
345 uint64_t index = hash & (priv->nvp_nbuckets - 1);
347 ASSERT3U(index, <, priv->nvp_nbuckets);
348 i_nvp_t *entry = tab[index];
350 for (i_nvp_t *e = entry; e != NULL; e = e->nvi_hashtable_next) {
351 if (strcmp(NVP_NAME(&e->nvi_nvp), name) == 0 &&
352 (type == DATA_TYPE_DONTCARE ||
353 NVP_TYPE(&e->nvi_nvp) == type))
354 return (&e->nvi_nvp);
356 return (NULL);
359 static nvpair_t *
360 nvt_lookup_name(const nvlist_t *nvl, const char *name)
362 return (nvt_lookup_name_type(nvl, name, DATA_TYPE_DONTCARE));
365 static int
366 nvt_resize(nvpriv_t *priv, uint32_t new_size)
368 i_nvp_t **tab = priv->nvp_hashtable;
371 * Migrate all the entries from the current table
372 * to a newly-allocated table with the new size by
373 * re-adjusting the pointers of their entries.
375 uint32_t size = priv->nvp_nbuckets;
376 uint32_t new_mask = new_size - 1;
377 ASSERT(ISP2(new_size));
379 i_nvp_t **new_tab = nv_mem_zalloc(priv, new_size * sizeof (i_nvp_t *));
380 if (new_tab == NULL)
381 return (ENOMEM);
383 uint32_t nentries = 0;
384 for (uint32_t i = 0; i < size; i++) {
385 i_nvp_t *next, *e = tab[i];
387 while (e != NULL) {
388 next = e->nvi_hashtable_next;
390 uint32_t hash = nvt_hash(NVP_NAME(&e->nvi_nvp));
391 uint32_t index = hash & new_mask;
393 e->nvi_hashtable_next = new_tab[index];
394 new_tab[index] = e;
395 nentries++;
397 e = next;
399 tab[i] = NULL;
401 ASSERT3U(nentries, ==, priv->nvp_nentries);
403 nvt_tab_free(priv);
405 priv->nvp_hashtable = new_tab;
406 priv->nvp_nbuckets = new_size;
407 priv->nvp_nentries = nentries;
409 return (0);
412 static boolean_t
413 nvt_needs_togrow(nvpriv_t *priv)
416 * Grow only when we have more elements than buckets
417 * and the # of buckets doesn't overflow.
419 return (priv->nvp_nentries > priv->nvp_nbuckets &&
420 (UINT32_MAX >> 1) >= priv->nvp_nbuckets);
424 * Allocate a new table that's twice the size of the old one,
425 * and migrate all the entries from the old one to the new
426 * one by re-adjusting their pointers.
428 static int
429 nvt_grow(nvpriv_t *priv)
431 uint32_t current_size = priv->nvp_nbuckets;
432 /* ensure we won't overflow */
433 ASSERT3U(UINT32_MAX >> 1, >=, current_size);
434 return (nvt_resize(priv, current_size << 1));
437 static boolean_t
438 nvt_needs_toshrink(nvpriv_t *priv)
441 * Shrink only when the # of elements is less than or
442 * equal to 1/4 the # of buckets. Never shrink less than
443 * nvlist_hashtable_init_size.
445 ASSERT3U(priv->nvp_nbuckets, >=, nvlist_hashtable_init_size);
446 if (priv->nvp_nbuckets == nvlist_hashtable_init_size)
447 return (B_FALSE);
448 return (priv->nvp_nentries <= (priv->nvp_nbuckets >> 2));
452 * Allocate a new table that's half the size of the old one,
453 * and migrate all the entries from the old one to the new
454 * one by re-adjusting their pointers.
456 static int
457 nvt_shrink(nvpriv_t *priv)
459 uint32_t current_size = priv->nvp_nbuckets;
460 /* ensure we won't overflow */
461 ASSERT3U(current_size, >=, nvlist_hashtable_init_size);
462 return (nvt_resize(priv, current_size >> 1));
465 static int
466 nvt_remove_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
468 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
470 if (nvt_needs_toshrink(priv)) {
471 int err = nvt_shrink(priv);
472 if (err != 0)
473 return (err);
475 i_nvp_t **tab = priv->nvp_hashtable;
477 const char *name = NVP_NAME(nvp);
478 uint64_t hash = nvt_hash(name);
479 uint64_t index = hash & (priv->nvp_nbuckets - 1);
481 ASSERT3U(index, <, priv->nvp_nbuckets);
482 i_nvp_t *bucket = tab[index];
484 for (i_nvp_t *prev = NULL, *e = bucket;
485 e != NULL; prev = e, e = e->nvi_hashtable_next) {
486 if (nvt_nvpair_match(&e->nvi_nvp, nvp, nvl->nvl_nvflag)) {
487 if (prev != NULL) {
488 prev->nvi_hashtable_next =
489 e->nvi_hashtable_next;
490 } else {
491 ASSERT3P(e, ==, bucket);
492 tab[index] = e->nvi_hashtable_next;
494 e->nvi_hashtable_next = NULL;
495 priv->nvp_nentries--;
496 break;
500 return (0);
503 static int
504 nvt_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
506 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
508 /* initialize nvpair table now if it doesn't exist. */
509 if (priv->nvp_hashtable == NULL) {
510 int err = nvt_tab_alloc(priv, nvlist_hashtable_init_size);
511 if (err != 0)
512 return (err);
516 * if we don't allow duplicate entries, make sure to
517 * unlink any existing entries from the table.
519 if (nvl->nvl_nvflag != 0) {
520 int err = nvt_remove_nvpair(nvl, nvp);
521 if (err != 0)
522 return (err);
525 if (nvt_needs_togrow(priv)) {
526 int err = nvt_grow(priv);
527 if (err != 0)
528 return (err);
530 i_nvp_t **tab = priv->nvp_hashtable;
532 const char *name = NVP_NAME(nvp);
533 uint64_t hash = nvt_hash(name);
534 uint64_t index = hash & (priv->nvp_nbuckets - 1);
536 ASSERT3U(index, <, priv->nvp_nbuckets);
537 // cppcheck-suppress nullPointerRedundantCheck
538 i_nvp_t *bucket = tab[index];
540 /* insert link at the beginning of the bucket */
541 i_nvp_t *new_entry = NVPAIR2I_NVP(nvp);
542 ASSERT3P(new_entry->nvi_hashtable_next, ==, NULL);
543 new_entry->nvi_hashtable_next = bucket;
544 // cppcheck-suppress nullPointerRedundantCheck
545 tab[index] = new_entry;
547 priv->nvp_nentries++;
548 return (0);
551 static void
552 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
554 nvl->nvl_version = NV_VERSION;
555 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
556 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
557 nvl->nvl_flag = 0;
558 nvl->nvl_pad = 0;
561 uint_t
562 nvlist_nvflag(nvlist_t *nvl)
564 return (nvl->nvl_nvflag);
567 static nv_alloc_t *
568 nvlist_nv_alloc(int kmflag)
570 #if defined(_KERNEL)
571 switch (kmflag) {
572 case KM_SLEEP:
573 return (nv_alloc_sleep);
574 case KM_NOSLEEP:
575 return (nv_alloc_nosleep);
576 default:
577 return (nv_alloc_pushpage);
579 #else
580 (void) kmflag;
581 return (nv_alloc_nosleep);
582 #endif /* _KERNEL */
586 * nvlist_alloc - Allocate nvlist.
589 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
591 return (nvlist_xalloc(nvlp, nvflag, nvlist_nv_alloc(kmflag)));
595 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
597 nvpriv_t *priv;
599 if (nvlp == NULL || nva == NULL)
600 return (EINVAL);
602 if ((priv = nv_priv_alloc(nva)) == NULL)
603 return (ENOMEM);
605 if ((*nvlp = nv_mem_zalloc(priv,
606 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
607 nv_mem_free(priv, priv, sizeof (nvpriv_t));
608 return (ENOMEM);
611 nvlist_init(*nvlp, nvflag, priv);
613 return (0);
617 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
619 static nvpair_t *
620 nvp_buf_alloc(nvlist_t *nvl, size_t len)
622 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
623 i_nvp_t *buf;
624 nvpair_t *nvp;
625 size_t nvsize;
628 * Allocate the buffer
630 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
632 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
633 return (NULL);
635 nvp = &buf->nvi_nvp;
636 nvp->nvp_size = len;
638 return (nvp);
642 * nvp_buf_free - de-Allocate an i_nvp_t.
644 static void
645 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
647 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
648 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
650 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
654 * nvp_buf_link - link a new nv pair into the nvlist.
656 static void
657 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
659 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
660 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
662 /* Put element at end of nvlist */
663 if (priv->nvp_list == NULL) {
664 priv->nvp_list = priv->nvp_last = curr;
665 } else {
666 curr->nvi_prev = priv->nvp_last;
667 priv->nvp_last->nvi_next = curr;
668 priv->nvp_last = curr;
673 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
675 static void
676 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
678 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
679 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
682 * protect nvlist_next_nvpair() against walking on freed memory.
684 if (priv->nvp_curr == curr)
685 priv->nvp_curr = curr->nvi_next;
687 if (curr == priv->nvp_list)
688 priv->nvp_list = curr->nvi_next;
689 else
690 curr->nvi_prev->nvi_next = curr->nvi_next;
692 if (curr == priv->nvp_last)
693 priv->nvp_last = curr->nvi_prev;
694 else
695 curr->nvi_next->nvi_prev = curr->nvi_prev;
699 * take a nvpair type and number of elements and make sure the are valid
701 static int
702 i_validate_type_nelem(data_type_t type, uint_t nelem)
704 switch (type) {
705 case DATA_TYPE_BOOLEAN:
706 if (nelem != 0)
707 return (EINVAL);
708 break;
709 case DATA_TYPE_BOOLEAN_VALUE:
710 case DATA_TYPE_BYTE:
711 case DATA_TYPE_INT8:
712 case DATA_TYPE_UINT8:
713 case DATA_TYPE_INT16:
714 case DATA_TYPE_UINT16:
715 case DATA_TYPE_INT32:
716 case DATA_TYPE_UINT32:
717 case DATA_TYPE_INT64:
718 case DATA_TYPE_UINT64:
719 case DATA_TYPE_STRING:
720 case DATA_TYPE_HRTIME:
721 case DATA_TYPE_NVLIST:
722 #if !defined(_KERNEL)
723 case DATA_TYPE_DOUBLE:
724 #endif
725 if (nelem != 1)
726 return (EINVAL);
727 break;
728 case DATA_TYPE_BOOLEAN_ARRAY:
729 case DATA_TYPE_BYTE_ARRAY:
730 case DATA_TYPE_INT8_ARRAY:
731 case DATA_TYPE_UINT8_ARRAY:
732 case DATA_TYPE_INT16_ARRAY:
733 case DATA_TYPE_UINT16_ARRAY:
734 case DATA_TYPE_INT32_ARRAY:
735 case DATA_TYPE_UINT32_ARRAY:
736 case DATA_TYPE_INT64_ARRAY:
737 case DATA_TYPE_UINT64_ARRAY:
738 case DATA_TYPE_STRING_ARRAY:
739 case DATA_TYPE_NVLIST_ARRAY:
740 /* we allow arrays with 0 elements */
741 break;
742 default:
743 return (EINVAL);
745 return (0);
749 * Verify nvp_name_sz and check the name string length.
751 static int
752 i_validate_nvpair_name(nvpair_t *nvp)
754 if ((nvp->nvp_name_sz <= 0) ||
755 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
756 return (EFAULT);
758 /* verify the name string, make sure its terminated */
759 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
760 return (EFAULT);
762 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
765 static int
766 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
768 switch (type) {
769 case DATA_TYPE_BOOLEAN_VALUE:
770 if (*(boolean_t *)data != B_TRUE &&
771 *(boolean_t *)data != B_FALSE)
772 return (EINVAL);
773 break;
774 case DATA_TYPE_BOOLEAN_ARRAY: {
775 int i;
777 for (i = 0; i < nelem; i++)
778 if (((boolean_t *)data)[i] != B_TRUE &&
779 ((boolean_t *)data)[i] != B_FALSE)
780 return (EINVAL);
781 break;
783 default:
784 break;
787 return (0);
791 * This function takes a pointer to what should be a nvpair and it's size
792 * and then verifies that all the nvpair fields make sense and can be
793 * trusted. This function is used when decoding packed nvpairs.
795 static int
796 i_validate_nvpair(nvpair_t *nvp)
798 data_type_t type = NVP_TYPE(nvp);
799 int size1, size2;
801 /* verify nvp_name_sz, check the name string length */
802 if (i_validate_nvpair_name(nvp) != 0)
803 return (EFAULT);
805 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
806 return (EFAULT);
809 * verify nvp_type, nvp_value_elem, and also possibly
810 * verify string values and get the value size.
812 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
813 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
814 if (size2 < 0 || size1 != NV_ALIGN(size2))
815 return (EFAULT);
817 return (0);
820 static int
821 nvlist_copy_pairs(const nvlist_t *snvl, nvlist_t *dnvl)
823 const nvpriv_t *priv;
824 const i_nvp_t *curr;
826 if ((priv = (const nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
827 return (EINVAL);
829 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
830 const nvpair_t *nvp = &curr->nvi_nvp;
831 int err;
833 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
834 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
835 return (err);
838 return (0);
842 * Frees all memory allocated for an nvpair (like embedded lists) with
843 * the exception of the nvpair buffer itself.
845 static void
846 nvpair_free(nvpair_t *nvp)
848 switch (NVP_TYPE(nvp)) {
849 case DATA_TYPE_NVLIST:
850 nvlist_free(EMBEDDED_NVL(nvp));
851 break;
852 case DATA_TYPE_NVLIST_ARRAY: {
853 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
854 int i;
856 for (i = 0; i < NVP_NELEM(nvp); i++)
857 if (nvlp[i] != NULL)
858 nvlist_free(nvlp[i]);
859 break;
861 default:
862 break;
867 * nvlist_free - free an unpacked nvlist
869 void
870 nvlist_free(nvlist_t *nvl)
872 nvpriv_t *priv;
873 i_nvp_t *curr;
875 if (nvl == NULL ||
876 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
877 return;
880 * Unpacked nvlist are linked through i_nvp_t
882 curr = priv->nvp_list;
883 while (curr != NULL) {
884 nvpair_t *nvp = &curr->nvi_nvp;
885 curr = curr->nvi_next;
887 nvpair_free(nvp);
888 nvp_buf_free(nvl, nvp);
891 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
892 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
893 else
894 nvl->nvl_priv = 0;
896 nvt_tab_free(priv);
897 nv_mem_free(priv, priv, sizeof (nvpriv_t));
900 static int
901 nvlist_contains_nvp(const nvlist_t *nvl, const nvpair_t *nvp)
903 const nvpriv_t *priv = (const nvpriv_t *)(uintptr_t)nvl->nvl_priv;
904 const i_nvp_t *curr;
906 if (nvp == NULL)
907 return (0);
909 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
910 if (&curr->nvi_nvp == nvp)
911 return (1);
913 return (0);
917 * Make a copy of nvlist
920 nvlist_dup(const nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
922 return (nvlist_xdup(nvl, nvlp, nvlist_nv_alloc(kmflag)));
926 nvlist_xdup(const nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
928 int err;
929 nvlist_t *ret;
931 if (nvl == NULL || nvlp == NULL)
932 return (EINVAL);
934 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
935 return (err);
937 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
938 nvlist_free(ret);
939 else
940 *nvlp = ret;
942 return (err);
946 * Remove all with matching name
949 nvlist_remove_all(nvlist_t *nvl, const char *name)
951 int error = ENOENT;
953 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
954 return (EINVAL);
956 nvpair_t *nvp;
957 while ((nvp = nvt_lookup_name(nvl, name)) != NULL) {
958 VERIFY0(nvlist_remove_nvpair(nvl, nvp));
959 error = 0;
962 return (error);
966 * Remove first one with matching name and type
969 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
971 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
972 return (EINVAL);
974 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
975 if (nvp == NULL)
976 return (ENOENT);
978 return (nvlist_remove_nvpair(nvl, nvp));
982 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
984 if (nvl == NULL || nvp == NULL)
985 return (EINVAL);
987 int err = nvt_remove_nvpair(nvl, nvp);
988 if (err != 0)
989 return (err);
991 nvp_buf_unlink(nvl, nvp);
992 nvpair_free(nvp);
993 nvp_buf_free(nvl, nvp);
994 return (0);
998 * This function calculates the size of an nvpair value.
1000 * The data argument controls the behavior in case of the data types
1001 * DATA_TYPE_STRING and
1002 * DATA_TYPE_STRING_ARRAY
1003 * Is data == NULL then the size of the string(s) is excluded.
1005 static int
1006 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
1008 uint64_t value_sz;
1010 if (i_validate_type_nelem(type, nelem) != 0)
1011 return (-1);
1013 /* Calculate required size for holding value */
1014 switch (type) {
1015 case DATA_TYPE_BOOLEAN:
1016 value_sz = 0;
1017 break;
1018 case DATA_TYPE_BOOLEAN_VALUE:
1019 value_sz = sizeof (boolean_t);
1020 break;
1021 case DATA_TYPE_BYTE:
1022 value_sz = sizeof (uchar_t);
1023 break;
1024 case DATA_TYPE_INT8:
1025 value_sz = sizeof (int8_t);
1026 break;
1027 case DATA_TYPE_UINT8:
1028 value_sz = sizeof (uint8_t);
1029 break;
1030 case DATA_TYPE_INT16:
1031 value_sz = sizeof (int16_t);
1032 break;
1033 case DATA_TYPE_UINT16:
1034 value_sz = sizeof (uint16_t);
1035 break;
1036 case DATA_TYPE_INT32:
1037 value_sz = sizeof (int32_t);
1038 break;
1039 case DATA_TYPE_UINT32:
1040 value_sz = sizeof (uint32_t);
1041 break;
1042 case DATA_TYPE_INT64:
1043 value_sz = sizeof (int64_t);
1044 break;
1045 case DATA_TYPE_UINT64:
1046 value_sz = sizeof (uint64_t);
1047 break;
1048 #if !defined(_KERNEL)
1049 case DATA_TYPE_DOUBLE:
1050 value_sz = sizeof (double);
1051 break;
1052 #endif
1053 case DATA_TYPE_STRING:
1054 if (data == NULL)
1055 value_sz = 0;
1056 else
1057 value_sz = strlen(data) + 1;
1058 break;
1059 case DATA_TYPE_BOOLEAN_ARRAY:
1060 value_sz = (uint64_t)nelem * sizeof (boolean_t);
1061 break;
1062 case DATA_TYPE_BYTE_ARRAY:
1063 value_sz = (uint64_t)nelem * sizeof (uchar_t);
1064 break;
1065 case DATA_TYPE_INT8_ARRAY:
1066 value_sz = (uint64_t)nelem * sizeof (int8_t);
1067 break;
1068 case DATA_TYPE_UINT8_ARRAY:
1069 value_sz = (uint64_t)nelem * sizeof (uint8_t);
1070 break;
1071 case DATA_TYPE_INT16_ARRAY:
1072 value_sz = (uint64_t)nelem * sizeof (int16_t);
1073 break;
1074 case DATA_TYPE_UINT16_ARRAY:
1075 value_sz = (uint64_t)nelem * sizeof (uint16_t);
1076 break;
1077 case DATA_TYPE_INT32_ARRAY:
1078 value_sz = (uint64_t)nelem * sizeof (int32_t);
1079 break;
1080 case DATA_TYPE_UINT32_ARRAY:
1081 value_sz = (uint64_t)nelem * sizeof (uint32_t);
1082 break;
1083 case DATA_TYPE_INT64_ARRAY:
1084 value_sz = (uint64_t)nelem * sizeof (int64_t);
1085 break;
1086 case DATA_TYPE_UINT64_ARRAY:
1087 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1088 break;
1089 case DATA_TYPE_STRING_ARRAY:
1090 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1092 if (data != NULL) {
1093 char *const *strs = data;
1094 uint_t i;
1096 /* no alignment requirement for strings */
1097 for (i = 0; i < nelem; i++) {
1098 if (strs[i] == NULL)
1099 return (-1);
1100 value_sz += strlen(strs[i]) + 1;
1103 break;
1104 case DATA_TYPE_HRTIME:
1105 value_sz = sizeof (hrtime_t);
1106 break;
1107 case DATA_TYPE_NVLIST:
1108 value_sz = NV_ALIGN(sizeof (nvlist_t));
1109 break;
1110 case DATA_TYPE_NVLIST_ARRAY:
1111 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
1112 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
1113 break;
1114 default:
1115 return (-1);
1118 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
1121 static int
1122 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
1124 nvpriv_t *priv;
1125 int err;
1127 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
1128 nvl->nvl_priv)) == NULL)
1129 return (ENOMEM);
1131 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
1133 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
1134 nvlist_free(emb_nvl);
1135 emb_nvl->nvl_priv = 0;
1138 return (err);
1142 * nvlist_add_common - Add new <name,value> pair to nvlist
1144 static int
1145 nvlist_add_common(nvlist_t *nvl, const char *name,
1146 data_type_t type, uint_t nelem, const void *data)
1148 nvpair_t *nvp;
1149 uint_t i;
1151 int nvp_sz, name_sz, value_sz;
1152 int err = 0;
1154 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1155 return (EINVAL);
1157 if (nelem != 0 && data == NULL)
1158 return (EINVAL);
1161 * Verify type and nelem and get the value size.
1162 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1163 * is the size of the string(s) included.
1165 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
1166 return (EINVAL);
1168 if (i_validate_nvpair_value(type, nelem, data) != 0)
1169 return (EINVAL);
1172 * If we're adding an nvlist or nvlist array, ensure that we are not
1173 * adding the input nvlist to itself, which would cause recursion,
1174 * and ensure that no NULL nvlist pointers are present.
1176 switch (type) {
1177 case DATA_TYPE_NVLIST:
1178 if (data == nvl || data == NULL)
1179 return (EINVAL);
1180 break;
1181 case DATA_TYPE_NVLIST_ARRAY: {
1182 nvlist_t **onvlp = (nvlist_t **)data;
1183 for (i = 0; i < nelem; i++) {
1184 if (onvlp[i] == nvl || onvlp[i] == NULL)
1185 return (EINVAL);
1187 break;
1189 default:
1190 break;
1193 /* calculate sizes of the nvpair elements and the nvpair itself */
1194 name_sz = strlen(name) + 1;
1195 if (name_sz >= 1ULL << (sizeof (nvp->nvp_name_sz) * NBBY - 1))
1196 return (EINVAL);
1198 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
1200 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
1201 return (ENOMEM);
1203 ASSERT(nvp->nvp_size == nvp_sz);
1204 nvp->nvp_name_sz = name_sz;
1205 nvp->nvp_value_elem = nelem;
1206 nvp->nvp_type = type;
1207 memcpy(NVP_NAME(nvp), name, name_sz);
1209 switch (type) {
1210 case DATA_TYPE_BOOLEAN:
1211 break;
1212 case DATA_TYPE_STRING_ARRAY: {
1213 char *const *strs = data;
1214 char *buf = NVP_VALUE(nvp);
1215 char **cstrs = (void *)buf;
1217 /* skip pre-allocated space for pointer array */
1218 buf += nelem * sizeof (uint64_t);
1219 for (i = 0; i < nelem; i++) {
1220 int slen = strlen(strs[i]) + 1;
1221 memcpy(buf, strs[i], slen);
1222 cstrs[i] = buf;
1223 buf += slen;
1225 break;
1227 case DATA_TYPE_NVLIST: {
1228 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
1229 nvlist_t *onvl = (nvlist_t *)data;
1231 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
1232 nvp_buf_free(nvl, nvp);
1233 return (err);
1235 break;
1237 case DATA_TYPE_NVLIST_ARRAY: {
1238 nvlist_t **onvlp = (nvlist_t **)data;
1239 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1240 nvlist_t *embedded = (nvlist_t *)
1241 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
1243 for (i = 0; i < nelem; i++) {
1244 if ((err = nvlist_copy_embedded(nvl,
1245 onvlp[i], embedded)) != 0) {
1247 * Free any successfully created lists
1249 nvpair_free(nvp);
1250 nvp_buf_free(nvl, nvp);
1251 return (err);
1254 nvlp[i] = embedded++;
1256 break;
1258 default:
1259 memcpy(NVP_VALUE(nvp), data, value_sz);
1262 /* if unique name, remove before add */
1263 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
1264 (void) nvlist_remove_all(nvl, name);
1265 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
1266 (void) nvlist_remove(nvl, name, type);
1268 err = nvt_add_nvpair(nvl, nvp);
1269 if (err != 0) {
1270 nvpair_free(nvp);
1271 nvp_buf_free(nvl, nvp);
1272 return (err);
1274 nvp_buf_link(nvl, nvp);
1276 return (0);
1280 nvlist_add_boolean(nvlist_t *nvl, const char *name)
1282 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1286 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1288 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1292 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1294 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1298 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1300 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1304 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1306 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1310 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1312 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1316 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1318 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1322 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1324 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1328 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1330 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1334 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1336 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1340 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1342 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1345 #if !defined(_KERNEL)
1347 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1349 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1351 #endif
1354 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1356 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1360 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1361 const boolean_t *a, uint_t n)
1363 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1367 nvlist_add_byte_array(nvlist_t *nvl, const char *name, const uchar_t *a,
1368 uint_t n)
1370 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1374 nvlist_add_int8_array(nvlist_t *nvl, const char *name, const int8_t *a,
1375 uint_t n)
1377 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1381 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, const uint8_t *a,
1382 uint_t n)
1384 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1388 nvlist_add_int16_array(nvlist_t *nvl, const char *name, const int16_t *a,
1389 uint_t n)
1391 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1395 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, const uint16_t *a,
1396 uint_t n)
1398 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1402 nvlist_add_int32_array(nvlist_t *nvl, const char *name, const int32_t *a,
1403 uint_t n)
1405 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1409 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, const uint32_t *a,
1410 uint_t n)
1412 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1416 nvlist_add_int64_array(nvlist_t *nvl, const char *name, const int64_t *a,
1417 uint_t n)
1419 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1423 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, const uint64_t *a,
1424 uint_t n)
1426 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1430 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1431 const char *const *a, uint_t n)
1433 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1437 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1439 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1443 nvlist_add_nvlist(nvlist_t *nvl, const char *name, const nvlist_t *val)
1445 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1449 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name,
1450 const nvlist_t * const *a, uint_t n)
1452 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1455 /* reading name-value pairs */
1456 nvpair_t *
1457 nvlist_next_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1459 nvpriv_t *priv;
1460 i_nvp_t *curr;
1462 if (nvl == NULL ||
1463 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1464 return (NULL);
1466 curr = NVPAIR2I_NVP(nvp);
1469 * Ensure that nvp is a valid nvpair on this nvlist.
1470 * NB: nvp_curr is used only as a hint so that we don't always
1471 * have to walk the list to determine if nvp is still on the list.
1473 if (nvp == NULL)
1474 curr = priv->nvp_list;
1475 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1476 curr = curr->nvi_next;
1477 else
1478 curr = NULL;
1480 priv->nvp_curr = curr;
1482 return (curr != NULL ? &curr->nvi_nvp : NULL);
1485 nvpair_t *
1486 nvlist_prev_nvpair(nvlist_t *nvl, const nvpair_t *nvp)
1488 nvpriv_t *priv;
1489 i_nvp_t *curr;
1491 if (nvl == NULL ||
1492 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1493 return (NULL);
1495 curr = NVPAIR2I_NVP(nvp);
1497 if (nvp == NULL)
1498 curr = priv->nvp_last;
1499 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1500 curr = curr->nvi_prev;
1501 else
1502 curr = NULL;
1504 priv->nvp_curr = curr;
1506 return (curr != NULL ? &curr->nvi_nvp : NULL);
1509 boolean_t
1510 nvlist_empty(const nvlist_t *nvl)
1512 const nvpriv_t *priv;
1514 if (nvl == NULL ||
1515 (priv = (const nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1516 return (B_TRUE);
1518 return (priv->nvp_list == NULL);
1521 const char *
1522 nvpair_name(const nvpair_t *nvp)
1524 return (NVP_NAME(nvp));
1527 data_type_t
1528 nvpair_type(const nvpair_t *nvp)
1530 return (NVP_TYPE(nvp));
1534 nvpair_type_is_array(const nvpair_t *nvp)
1536 data_type_t type = NVP_TYPE(nvp);
1538 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1539 (type == DATA_TYPE_INT8_ARRAY) ||
1540 (type == DATA_TYPE_UINT8_ARRAY) ||
1541 (type == DATA_TYPE_INT16_ARRAY) ||
1542 (type == DATA_TYPE_UINT16_ARRAY) ||
1543 (type == DATA_TYPE_INT32_ARRAY) ||
1544 (type == DATA_TYPE_UINT32_ARRAY) ||
1545 (type == DATA_TYPE_INT64_ARRAY) ||
1546 (type == DATA_TYPE_UINT64_ARRAY) ||
1547 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1548 (type == DATA_TYPE_STRING_ARRAY) ||
1549 (type == DATA_TYPE_NVLIST_ARRAY))
1550 return (1);
1551 return (0);
1555 static int
1556 nvpair_value_common(const nvpair_t *nvp, data_type_t type, uint_t *nelem,
1557 void *data)
1559 int value_sz;
1561 if (nvp == NULL || nvpair_type(nvp) != type)
1562 return (EINVAL);
1565 * For non-array types, we copy the data.
1566 * For array types (including string), we set a pointer.
1568 switch (type) {
1569 case DATA_TYPE_BOOLEAN:
1570 if (nelem != NULL)
1571 *nelem = 0;
1572 break;
1574 case DATA_TYPE_BOOLEAN_VALUE:
1575 case DATA_TYPE_BYTE:
1576 case DATA_TYPE_INT8:
1577 case DATA_TYPE_UINT8:
1578 case DATA_TYPE_INT16:
1579 case DATA_TYPE_UINT16:
1580 case DATA_TYPE_INT32:
1581 case DATA_TYPE_UINT32:
1582 case DATA_TYPE_INT64:
1583 case DATA_TYPE_UINT64:
1584 case DATA_TYPE_HRTIME:
1585 #if !defined(_KERNEL)
1586 case DATA_TYPE_DOUBLE:
1587 #endif
1588 if (data == NULL)
1589 return (EINVAL);
1590 if ((value_sz = i_get_value_size(type, NULL, 1)) < 0)
1591 return (EINVAL);
1592 memcpy(data, NVP_VALUE(nvp), (size_t)value_sz);
1593 if (nelem != NULL)
1594 *nelem = 1;
1595 break;
1597 case DATA_TYPE_NVLIST:
1598 case DATA_TYPE_STRING:
1599 if (data == NULL)
1600 return (EINVAL);
1602 * This discards the const from nvp, so all callers for these
1603 * types must not accept const nvpairs.
1605 *(void **)data = (void *)NVP_VALUE(nvp);
1606 if (nelem != NULL)
1607 *nelem = 1;
1608 break;
1610 case DATA_TYPE_BOOLEAN_ARRAY:
1611 case DATA_TYPE_BYTE_ARRAY:
1612 case DATA_TYPE_INT8_ARRAY:
1613 case DATA_TYPE_UINT8_ARRAY:
1614 case DATA_TYPE_INT16_ARRAY:
1615 case DATA_TYPE_UINT16_ARRAY:
1616 case DATA_TYPE_INT32_ARRAY:
1617 case DATA_TYPE_UINT32_ARRAY:
1618 case DATA_TYPE_INT64_ARRAY:
1619 case DATA_TYPE_UINT64_ARRAY:
1620 case DATA_TYPE_STRING_ARRAY:
1621 case DATA_TYPE_NVLIST_ARRAY:
1622 if (nelem == NULL || data == NULL)
1623 return (EINVAL);
1625 * This discards the const from nvp, so all callers for these
1626 * types must not accept const nvpairs.
1628 if ((*nelem = NVP_NELEM(nvp)) != 0)
1629 *(void **)data = (void *)NVP_VALUE(nvp);
1630 else
1631 *(void **)data = NULL;
1632 break;
1634 default:
1635 return (ENOTSUP);
1638 return (0);
1641 static int
1642 nvlist_lookup_common(const nvlist_t *nvl, const char *name, data_type_t type,
1643 uint_t *nelem, void *data)
1645 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1646 return (EINVAL);
1648 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1649 return (ENOTSUP);
1651 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
1652 if (nvp == NULL)
1653 return (ENOENT);
1655 return (nvpair_value_common(nvp, type, nelem, data));
1659 nvlist_lookup_boolean(const nvlist_t *nvl, const char *name)
1661 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1665 nvlist_lookup_boolean_value(const nvlist_t *nvl, const char *name,
1666 boolean_t *val)
1668 return (nvlist_lookup_common(nvl, name,
1669 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1673 nvlist_lookup_byte(const nvlist_t *nvl, const char *name, uchar_t *val)
1675 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1679 nvlist_lookup_int8(const nvlist_t *nvl, const char *name, int8_t *val)
1681 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1685 nvlist_lookup_uint8(const nvlist_t *nvl, const char *name, uint8_t *val)
1687 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1691 nvlist_lookup_int16(const nvlist_t *nvl, const char *name, int16_t *val)
1693 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1697 nvlist_lookup_uint16(const nvlist_t *nvl, const char *name, uint16_t *val)
1699 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1703 nvlist_lookup_int32(const nvlist_t *nvl, const char *name, int32_t *val)
1705 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1709 nvlist_lookup_uint32(const nvlist_t *nvl, const char *name, uint32_t *val)
1711 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1715 nvlist_lookup_int64(const nvlist_t *nvl, const char *name, int64_t *val)
1717 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1721 nvlist_lookup_uint64(const nvlist_t *nvl, const char *name, uint64_t *val)
1723 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1726 #if !defined(_KERNEL)
1728 nvlist_lookup_double(const nvlist_t *nvl, const char *name, double *val)
1730 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1732 #endif
1735 nvlist_lookup_string(const nvlist_t *nvl, const char *name, const char **val)
1737 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1741 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1743 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1747 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1748 boolean_t **a, uint_t *n)
1750 return (nvlist_lookup_common(nvl, name,
1751 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1755 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1756 uchar_t **a, uint_t *n)
1758 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1762 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1764 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1768 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1769 uint8_t **a, uint_t *n)
1771 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1775 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1776 int16_t **a, uint_t *n)
1778 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1782 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1783 uint16_t **a, uint_t *n)
1785 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1789 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1790 int32_t **a, uint_t *n)
1792 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1796 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1797 uint32_t **a, uint_t *n)
1799 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1803 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1804 int64_t **a, uint_t *n)
1806 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1810 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1811 uint64_t **a, uint_t *n)
1813 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1817 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1818 char ***a, uint_t *n)
1820 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1824 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1825 nvlist_t ***a, uint_t *n)
1827 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1831 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1833 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1837 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1839 va_list ap;
1840 char *name;
1841 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1842 int ret = 0;
1844 va_start(ap, flag);
1845 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1846 data_type_t type;
1847 void *val;
1848 uint_t *nelem;
1850 switch (type = va_arg(ap, data_type_t)) {
1851 case DATA_TYPE_BOOLEAN:
1852 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1853 break;
1855 case DATA_TYPE_BOOLEAN_VALUE:
1856 case DATA_TYPE_BYTE:
1857 case DATA_TYPE_INT8:
1858 case DATA_TYPE_UINT8:
1859 case DATA_TYPE_INT16:
1860 case DATA_TYPE_UINT16:
1861 case DATA_TYPE_INT32:
1862 case DATA_TYPE_UINT32:
1863 case DATA_TYPE_INT64:
1864 case DATA_TYPE_UINT64:
1865 case DATA_TYPE_HRTIME:
1866 case DATA_TYPE_STRING:
1867 case DATA_TYPE_NVLIST:
1868 #if !defined(_KERNEL)
1869 case DATA_TYPE_DOUBLE:
1870 #endif
1871 val = va_arg(ap, void *);
1872 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1873 break;
1875 case DATA_TYPE_BYTE_ARRAY:
1876 case DATA_TYPE_BOOLEAN_ARRAY:
1877 case DATA_TYPE_INT8_ARRAY:
1878 case DATA_TYPE_UINT8_ARRAY:
1879 case DATA_TYPE_INT16_ARRAY:
1880 case DATA_TYPE_UINT16_ARRAY:
1881 case DATA_TYPE_INT32_ARRAY:
1882 case DATA_TYPE_UINT32_ARRAY:
1883 case DATA_TYPE_INT64_ARRAY:
1884 case DATA_TYPE_UINT64_ARRAY:
1885 case DATA_TYPE_STRING_ARRAY:
1886 case DATA_TYPE_NVLIST_ARRAY:
1887 val = va_arg(ap, void *);
1888 nelem = va_arg(ap, uint_t *);
1889 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1890 break;
1892 default:
1893 ret = EINVAL;
1896 if (ret == ENOENT && noentok)
1897 ret = 0;
1899 va_end(ap);
1901 return (ret);
1905 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1906 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1907 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1908 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1909 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1910 * "a.d[3].e[1]". This matches the C syntax for array embed (for convenience,
1911 * code also supports "a.d[3]e[1]" syntax).
1913 * If 'ip' is non-NULL and the last name component is an array, return the
1914 * value of the "...[index]" array index in *ip. For an array reference that
1915 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1916 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1917 * inside the 'name' string where the syntax error was detected.
1919 static int
1920 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1921 nvpair_t **ret, int *ip, const char **ep)
1923 nvpair_t *nvp;
1924 const char *np;
1925 char *sepp = NULL;
1926 char *idxp, *idxep;
1927 nvlist_t **nva;
1928 long idx = 0;
1929 int n;
1931 if (ip)
1932 *ip = -1; /* not indexed */
1933 if (ep)
1934 *ep = NULL;
1936 if ((nvl == NULL) || (name == NULL))
1937 return (EINVAL);
1939 sepp = NULL;
1940 idx = 0;
1941 /* step through components of name */
1942 for (np = name; np && *np; np = sepp) {
1943 /* ensure unique names */
1944 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1945 return (ENOTSUP);
1947 /* skip white space */
1948 skip_whitespace(np);
1949 if (*np == 0)
1950 break;
1952 /* set 'sepp' to end of current component 'np' */
1953 if (sep)
1954 sepp = strchr(np, sep);
1955 else
1956 sepp = NULL;
1958 /* find start of next "[ index ]..." */
1959 idxp = strchr(np, '[');
1961 /* if sepp comes first, set idxp to NULL */
1962 if (sepp && idxp && (sepp < idxp))
1963 idxp = NULL;
1966 * At this point 'idxp' is set if there is an index
1967 * expected for the current component.
1969 if (idxp) {
1970 /* set 'n' to length of current 'np' name component */
1971 n = idxp++ - np;
1973 /* keep sepp up to date for *ep use as we advance */
1974 skip_whitespace(idxp);
1975 sepp = idxp;
1977 /* determine the index value */
1978 #if defined(_KERNEL)
1979 if (ddi_strtol(idxp, &idxep, 0, &idx))
1980 goto fail;
1981 #else
1982 idx = strtol(idxp, &idxep, 0);
1983 #endif
1984 if (idxep == idxp)
1985 goto fail;
1987 /* keep sepp up to date for *ep use as we advance */
1988 sepp = idxep;
1990 /* skip white space index value and check for ']' */
1991 skip_whitespace(sepp);
1992 if (*sepp++ != ']')
1993 goto fail;
1995 /* for embedded arrays, support C syntax: "a[1].b" */
1996 skip_whitespace(sepp);
1997 if (sep && (*sepp == sep))
1998 sepp++;
1999 } else if (sepp) {
2000 n = sepp++ - np;
2001 } else {
2002 n = strlen(np);
2005 /* trim trailing whitespace by reducing length of 'np' */
2006 if (n == 0)
2007 goto fail;
2008 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
2010 n++;
2012 /* skip whitespace, and set sepp to NULL if complete */
2013 if (sepp) {
2014 skip_whitespace(sepp);
2015 if (*sepp == 0)
2016 sepp = NULL;
2020 * At this point:
2021 * o 'n' is the length of current 'np' component.
2022 * o 'idxp' is set if there was an index, and value 'idx'.
2023 * o 'sepp' is set to the beginning of the next component,
2024 * and set to NULL if we have no more components.
2026 * Search for nvpair with matching component name.
2028 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2029 nvp = nvlist_next_nvpair(nvl, nvp)) {
2031 /* continue if no match on name */
2032 if (strncmp(np, nvpair_name(nvp), n) ||
2033 (strlen(nvpair_name(nvp)) != n))
2034 continue;
2036 /* if indexed, verify type is array oriented */
2037 if (idxp && !nvpair_type_is_array(nvp))
2038 goto fail;
2041 * Full match found, return nvp and idx if this
2042 * was the last component.
2044 if (sepp == NULL) {
2045 if (ret)
2046 *ret = nvp;
2047 if (ip && idxp)
2048 *ip = (int)idx; /* return index */
2049 return (0); /* found */
2053 * More components: current match must be
2054 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2055 * to support going deeper.
2057 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
2058 nvl = EMBEDDED_NVL(nvp);
2059 break;
2060 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
2061 if (nvpair_value_nvlist_array(nvp,
2062 &nva, (uint_t *)&n) != 0)
2063 goto fail;
2064 if (nva == NULL)
2065 goto fail;
2066 if ((n < 0) || (idx >= n))
2067 goto fail;
2068 nvl = nva[idx];
2069 break;
2072 /* type does not support more levels */
2073 goto fail;
2075 if (nvp == NULL)
2076 goto fail; /* 'name' not found */
2078 /* search for match of next component in embedded 'nvl' list */
2081 fail: if (ep && sepp)
2082 *ep = sepp;
2083 return (EINVAL);
2087 * Return pointer to nvpair with specified 'name'.
2090 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
2092 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
2096 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2097 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2098 * description.
2100 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
2101 const char *name, nvpair_t **ret, int *ip, const char **ep)
2103 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
2106 boolean_t
2107 nvlist_exists(const nvlist_t *nvl, const char *name)
2109 nvpriv_t *priv;
2110 nvpair_t *nvp;
2111 i_nvp_t *curr;
2113 if (name == NULL || nvl == NULL ||
2114 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2115 return (B_FALSE);
2117 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2118 nvp = &curr->nvi_nvp;
2120 if (strcmp(name, NVP_NAME(nvp)) == 0)
2121 return (B_TRUE);
2124 return (B_FALSE);
2128 nvpair_value_boolean_value(const nvpair_t *nvp, boolean_t *val)
2130 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
2134 nvpair_value_byte(const nvpair_t *nvp, uchar_t *val)
2136 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
2140 nvpair_value_int8(const nvpair_t *nvp, int8_t *val)
2142 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
2146 nvpair_value_uint8(const nvpair_t *nvp, uint8_t *val)
2148 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
2152 nvpair_value_int16(const nvpair_t *nvp, int16_t *val)
2154 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
2158 nvpair_value_uint16(const nvpair_t *nvp, uint16_t *val)
2160 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
2164 nvpair_value_int32(const nvpair_t *nvp, int32_t *val)
2166 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
2170 nvpair_value_uint32(const nvpair_t *nvp, uint32_t *val)
2172 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
2176 nvpair_value_int64(const nvpair_t *nvp, int64_t *val)
2178 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
2182 nvpair_value_uint64(const nvpair_t *nvp, uint64_t *val)
2184 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
2187 #if !defined(_KERNEL)
2189 nvpair_value_double(const nvpair_t *nvp, double *val)
2191 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
2193 #endif
2196 nvpair_value_string(const nvpair_t *nvp, const char **val)
2198 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
2202 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
2204 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
2208 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
2210 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
2214 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
2216 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
2220 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
2222 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
2226 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
2228 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
2232 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
2234 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
2238 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
2240 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
2244 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
2246 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
2250 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
2252 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
2256 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
2258 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
2262 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
2264 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
2268 nvpair_value_string_array(nvpair_t *nvp, const char ***val, uint_t *nelem)
2270 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
2274 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
2276 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
2280 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
2282 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
2286 * Add specified pair to the list.
2289 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2291 if (nvl == NULL || nvp == NULL)
2292 return (EINVAL);
2294 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
2295 NVP_NELEM(nvp), NVP_VALUE(nvp)));
2299 * Merge the supplied nvlists and put the result in dst.
2300 * The merged list will contain all names specified in both lists,
2301 * the values are taken from nvl in the case of duplicates.
2302 * Return 0 on success.
2305 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2307 (void) flag;
2309 if (nvl == NULL || dst == NULL)
2310 return (EINVAL);
2312 if (dst != nvl)
2313 return (nvlist_copy_pairs(nvl, dst));
2315 return (0);
2319 * Encoding related routines
2321 #define NVS_OP_ENCODE 0
2322 #define NVS_OP_DECODE 1
2323 #define NVS_OP_GETSIZE 2
2325 typedef struct nvs_ops nvs_ops_t;
2327 typedef struct {
2328 int nvs_op;
2329 const nvs_ops_t *nvs_ops;
2330 void *nvs_private;
2331 nvpriv_t *nvs_priv;
2332 int nvs_recursion;
2333 } nvstream_t;
2336 * nvs operations are:
2337 * - nvs_nvlist
2338 * encoding / decoding of an nvlist header (nvlist_t)
2339 * calculates the size used for header and end detection
2341 * - nvs_nvpair
2342 * responsible for the first part of encoding / decoding of an nvpair
2343 * calculates the decoded size of an nvpair
2345 * - nvs_nvp_op
2346 * second part of encoding / decoding of an nvpair
2348 * - nvs_nvp_size
2349 * calculates the encoding size of an nvpair
2351 * - nvs_nvl_fini
2352 * encodes the end detection mark (zeros).
2354 struct nvs_ops {
2355 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2356 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2357 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2358 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2359 int (*nvs_nvl_fini)(nvstream_t *);
2362 typedef struct {
2363 char nvh_encoding; /* nvs encoding method */
2364 char nvh_endian; /* nvs endian */
2365 char nvh_reserved1; /* reserved for future use */
2366 char nvh_reserved2; /* reserved for future use */
2367 } nvs_header_t;
2369 static int
2370 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2372 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2373 i_nvp_t *curr;
2376 * Walk nvpair in list and encode each nvpair
2378 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2379 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2380 return (EFAULT);
2382 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2385 static int
2386 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2388 nvpair_t *nvp;
2389 size_t nvsize;
2390 int err;
2393 * Get decoded size of next pair in stream, alloc
2394 * memory for nvpair_t, then decode the nvpair
2396 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2397 if (nvsize == 0) /* end of list */
2398 break;
2400 /* make sure len makes sense */
2401 if (nvsize < NVP_SIZE_CALC(1, 0))
2402 return (EFAULT);
2404 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2405 return (ENOMEM);
2407 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2408 nvp_buf_free(nvl, nvp);
2409 return (err);
2412 if (i_validate_nvpair(nvp) != 0) {
2413 nvpair_free(nvp);
2414 nvp_buf_free(nvl, nvp);
2415 return (EFAULT);
2418 err = nvt_add_nvpair(nvl, nvp);
2419 if (err != 0) {
2420 nvpair_free(nvp);
2421 nvp_buf_free(nvl, nvp);
2422 return (err);
2424 nvp_buf_link(nvl, nvp);
2426 return (err);
2429 static int
2430 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2432 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2433 i_nvp_t *curr;
2434 uint64_t nvsize = *buflen;
2435 size_t size;
2438 * Get encoded size of nvpairs in nvlist
2440 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2441 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2442 return (EINVAL);
2444 if ((nvsize += size) > INT32_MAX)
2445 return (EINVAL);
2448 *buflen = nvsize;
2449 return (0);
2452 static int
2453 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2455 int err;
2457 if (nvl->nvl_priv == 0)
2458 return (EFAULT);
2461 * Perform the operation, starting with header, then each nvpair
2463 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2464 return (err);
2466 switch (nvs->nvs_op) {
2467 case NVS_OP_ENCODE:
2468 err = nvs_encode_pairs(nvs, nvl);
2469 break;
2471 case NVS_OP_DECODE:
2472 err = nvs_decode_pairs(nvs, nvl);
2473 break;
2475 case NVS_OP_GETSIZE:
2476 err = nvs_getsize_pairs(nvs, nvl, buflen);
2477 break;
2479 default:
2480 err = EINVAL;
2483 return (err);
2486 static int
2487 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2489 switch (nvs->nvs_op) {
2490 case NVS_OP_ENCODE: {
2491 int err;
2493 if (nvs->nvs_recursion >= nvpair_max_recursion)
2494 return (EINVAL);
2495 nvs->nvs_recursion++;
2496 err = nvs_operation(nvs, embedded, NULL);
2497 nvs->nvs_recursion--;
2498 return (err);
2500 case NVS_OP_DECODE: {
2501 nvpriv_t *priv;
2502 int err;
2504 if (embedded->nvl_version != NV_VERSION)
2505 return (ENOTSUP);
2507 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2508 return (ENOMEM);
2510 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2512 if (nvs->nvs_recursion >= nvpair_max_recursion) {
2513 nvlist_free(embedded);
2514 return (EINVAL);
2516 nvs->nvs_recursion++;
2517 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2518 nvlist_free(embedded);
2519 nvs->nvs_recursion--;
2520 return (err);
2522 default:
2523 break;
2526 return (EINVAL);
2529 static int
2530 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2532 size_t nelem = NVP_NELEM(nvp);
2533 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2534 int i;
2536 switch (nvs->nvs_op) {
2537 case NVS_OP_ENCODE:
2538 for (i = 0; i < nelem; i++)
2539 if (nvs_embedded(nvs, nvlp[i]) != 0)
2540 return (EFAULT);
2541 break;
2543 case NVS_OP_DECODE: {
2544 size_t len = nelem * sizeof (uint64_t);
2545 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2547 memset(nvlp, 0, len); /* don't trust packed data */
2548 for (i = 0; i < nelem; i++) {
2549 if (nvs_embedded(nvs, embedded) != 0) {
2550 nvpair_free(nvp);
2551 return (EFAULT);
2554 nvlp[i] = embedded++;
2556 break;
2558 case NVS_OP_GETSIZE: {
2559 uint64_t nvsize = 0;
2561 for (i = 0; i < nelem; i++) {
2562 size_t nvp_sz = 0;
2564 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2565 return (EINVAL);
2567 if ((nvsize += nvp_sz) > INT32_MAX)
2568 return (EINVAL);
2571 *size = nvsize;
2572 break;
2574 default:
2575 return (EINVAL);
2578 return (0);
2581 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2582 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2585 * Common routine for nvlist operations:
2586 * encode, decode, getsize (encoded size).
2588 static int
2589 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2590 int nvs_op)
2592 int err = 0;
2593 nvstream_t nvs;
2594 int nvl_endian;
2595 #if defined(_ZFS_LITTLE_ENDIAN)
2596 int host_endian = 1;
2597 #elif defined(_ZFS_BIG_ENDIAN)
2598 int host_endian = 0;
2599 #else
2600 #error "No endian defined!"
2601 #endif /* _ZFS_LITTLE_ENDIAN */
2602 nvs_header_t *nvh;
2604 if (buflen == NULL || nvl == NULL ||
2605 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2606 return (EINVAL);
2608 nvs.nvs_op = nvs_op;
2609 nvs.nvs_recursion = 0;
2612 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2613 * a buffer is allocated. The first 4 bytes in the buffer are
2614 * used for encoding method and host endian.
2616 switch (nvs_op) {
2617 case NVS_OP_ENCODE:
2618 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2619 return (EINVAL);
2621 nvh = (void *)buf;
2622 nvh->nvh_encoding = encoding;
2623 nvh->nvh_endian = nvl_endian = host_endian;
2624 nvh->nvh_reserved1 = 0;
2625 nvh->nvh_reserved2 = 0;
2626 break;
2628 case NVS_OP_DECODE:
2629 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2630 return (EINVAL);
2632 /* get method of encoding from first byte */
2633 nvh = (void *)buf;
2634 encoding = nvh->nvh_encoding;
2635 nvl_endian = nvh->nvh_endian;
2636 break;
2638 case NVS_OP_GETSIZE:
2639 nvl_endian = host_endian;
2642 * add the size for encoding
2644 *buflen = sizeof (nvs_header_t);
2645 break;
2647 default:
2648 return (ENOTSUP);
2652 * Create an nvstream with proper encoding method
2654 switch (encoding) {
2655 case NV_ENCODE_NATIVE:
2657 * check endianness, in case we are unpacking
2658 * from a file
2660 if (nvl_endian != host_endian)
2661 return (ENOTSUP);
2662 err = nvs_native(&nvs, nvl, buf, buflen);
2663 break;
2664 case NV_ENCODE_XDR:
2665 err = nvs_xdr(&nvs, nvl, buf, buflen);
2666 break;
2667 default:
2668 err = ENOTSUP;
2669 break;
2672 return (err);
2676 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2678 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2682 * Pack nvlist into contiguous memory
2685 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2686 int kmflag)
2688 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2689 nvlist_nv_alloc(kmflag)));
2693 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2694 nv_alloc_t *nva)
2696 nvpriv_t nvpriv;
2697 size_t alloc_size;
2698 char *buf;
2699 int err;
2701 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2702 return (EINVAL);
2704 if (*bufp != NULL)
2705 return (nvlist_common(nvl, *bufp, buflen, encoding,
2706 NVS_OP_ENCODE));
2709 * Here is a difficult situation:
2710 * 1. The nvlist has fixed allocator properties.
2711 * All other nvlist routines (like nvlist_add_*, ...) use
2712 * these properties.
2713 * 2. When using nvlist_pack() the user can specify their own
2714 * allocator properties (e.g. by using KM_NOSLEEP).
2716 * We use the user specified properties (2). A clearer solution
2717 * will be to remove the kmflag from nvlist_pack(), but we will
2718 * not change the interface.
2720 nv_priv_init(&nvpriv, nva, 0);
2722 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2723 return (err);
2725 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2726 return (ENOMEM);
2728 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2729 NVS_OP_ENCODE)) != 0) {
2730 nv_mem_free(&nvpriv, buf, alloc_size);
2731 } else {
2732 *buflen = alloc_size;
2733 *bufp = buf;
2736 return (err);
2740 * Unpack buf into an nvlist_t
2743 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2745 return (nvlist_xunpack(buf, buflen, nvlp, nvlist_nv_alloc(kmflag)));
2749 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2751 nvlist_t *nvl;
2752 int err;
2754 if (nvlp == NULL)
2755 return (EINVAL);
2757 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2758 return (err);
2760 if ((err = nvlist_common(nvl, buf, &buflen, NV_ENCODE_NATIVE,
2761 NVS_OP_DECODE)) != 0)
2762 nvlist_free(nvl);
2763 else
2764 *nvlp = nvl;
2766 return (err);
2770 * Native encoding functions
2772 typedef struct {
2774 * This structure is used when decoding a packed nvpair in
2775 * the native format. n_base points to a buffer containing the
2776 * packed nvpair. n_end is a pointer to the end of the buffer.
2777 * (n_end actually points to the first byte past the end of the
2778 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2779 * It points to the current data that we are decoding.
2780 * The amount of data left in the buffer is equal to n_end - n_curr.
2781 * n_flag is used to recognize a packed embedded list.
2783 caddr_t n_base;
2784 caddr_t n_end;
2785 caddr_t n_curr;
2786 uint_t n_flag;
2787 } nvs_native_t;
2789 static int
2790 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2791 size_t buflen)
2793 switch (nvs->nvs_op) {
2794 case NVS_OP_ENCODE:
2795 case NVS_OP_DECODE:
2796 nvs->nvs_private = native;
2797 native->n_curr = native->n_base = buf;
2798 native->n_end = buf + buflen;
2799 native->n_flag = 0;
2800 return (0);
2802 case NVS_OP_GETSIZE:
2803 nvs->nvs_private = native;
2804 native->n_curr = native->n_base = native->n_end = NULL;
2805 native->n_flag = 0;
2806 return (0);
2807 default:
2808 return (EINVAL);
2812 static void
2813 nvs_native_destroy(nvstream_t *nvs)
2815 nvs->nvs_private = NULL;
2818 static int
2819 native_cp(nvstream_t *nvs, void *buf, size_t size)
2821 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2823 if (native->n_curr + size > native->n_end)
2824 return (EFAULT);
2827 * The memcpy() below eliminates alignment requirement
2828 * on the buffer (stream) and is preferred over direct access.
2830 switch (nvs->nvs_op) {
2831 case NVS_OP_ENCODE:
2832 memcpy(native->n_curr, buf, size);
2833 break;
2834 case NVS_OP_DECODE:
2835 memcpy(buf, native->n_curr, size);
2836 break;
2837 default:
2838 return (EINVAL);
2841 native->n_curr += size;
2842 return (0);
2846 * operate on nvlist_t header
2848 static int
2849 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2851 nvs_native_t *native = nvs->nvs_private;
2853 switch (nvs->nvs_op) {
2854 case NVS_OP_ENCODE:
2855 case NVS_OP_DECODE:
2856 if (native->n_flag)
2857 return (0); /* packed embedded list */
2859 native->n_flag = 1;
2861 /* copy version and nvflag of the nvlist_t */
2862 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2863 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2864 return (EFAULT);
2866 return (0);
2868 case NVS_OP_GETSIZE:
2870 * if calculate for packed embedded list
2871 * 4 for end of the embedded list
2872 * else
2873 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2874 * and 4 for end of the entire list
2876 if (native->n_flag) {
2877 *size += 4;
2878 } else {
2879 native->n_flag = 1;
2880 *size += 2 * sizeof (int32_t) + 4;
2883 return (0);
2885 default:
2886 return (EINVAL);
2890 static int
2891 nvs_native_nvl_fini(nvstream_t *nvs)
2893 if (nvs->nvs_op == NVS_OP_ENCODE) {
2894 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2896 * Add 4 zero bytes at end of nvlist. They are used
2897 * for end detection by the decode routine.
2899 if (native->n_curr + sizeof (int) > native->n_end)
2900 return (EFAULT);
2902 memset(native->n_curr, 0, sizeof (int));
2903 native->n_curr += sizeof (int);
2906 return (0);
2909 static int
2910 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2912 if (nvs->nvs_op == NVS_OP_ENCODE) {
2913 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2914 nvlist_t *packed = (void *)
2915 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2917 * Null out the pointer that is meaningless in the packed
2918 * structure. The address may not be aligned, so we have
2919 * to use memset.
2921 memset((char *)packed + offsetof(nvlist_t, nvl_priv),
2922 0, sizeof (uint64_t));
2925 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2928 static int
2929 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2931 if (nvs->nvs_op == NVS_OP_ENCODE) {
2932 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2933 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2934 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2935 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2936 int i;
2938 * Null out pointers that are meaningless in the packed
2939 * structure. The addresses may not be aligned, so we have
2940 * to use memset.
2942 memset(value, 0, len);
2944 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2946 * Null out the pointer that is meaningless in the
2947 * packed structure. The address may not be aligned,
2948 * so we have to use memset.
2950 memset((char *)packed + offsetof(nvlist_t, nvl_priv),
2951 0, sizeof (uint64_t));
2954 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2957 static void
2958 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2960 switch (nvs->nvs_op) {
2961 case NVS_OP_ENCODE: {
2962 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2963 uint64_t *strp = (void *)
2964 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2966 * Null out pointers that are meaningless in the packed
2967 * structure. The addresses may not be aligned, so we have
2968 * to use memset.
2970 memset(strp, 0, NVP_NELEM(nvp) * sizeof (uint64_t));
2971 break;
2973 case NVS_OP_DECODE: {
2974 char **strp = (void *)NVP_VALUE(nvp);
2975 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2976 int i;
2978 for (i = 0; i < NVP_NELEM(nvp); i++) {
2979 strp[i] = buf;
2980 buf += strlen(buf) + 1;
2982 break;
2987 static int
2988 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2990 data_type_t type;
2991 int value_sz;
2992 int ret = 0;
2995 * We do the initial memcpy of the data before we look at
2996 * the nvpair type, because when we're decoding, we won't
2997 * have the correct values for the pair until we do the memcpy.
2999 switch (nvs->nvs_op) {
3000 case NVS_OP_ENCODE:
3001 case NVS_OP_DECODE:
3002 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
3003 return (EFAULT);
3004 break;
3005 default:
3006 return (EINVAL);
3009 /* verify nvp_name_sz, check the name string length */
3010 if (i_validate_nvpair_name(nvp) != 0)
3011 return (EFAULT);
3013 type = NVP_TYPE(nvp);
3016 * Verify type and nelem and get the value size.
3017 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3018 * is the size of the string(s) excluded.
3020 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
3021 return (EFAULT);
3023 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
3024 return (EFAULT);
3026 switch (type) {
3027 case DATA_TYPE_NVLIST:
3028 ret = nvpair_native_embedded(nvs, nvp);
3029 break;
3030 case DATA_TYPE_NVLIST_ARRAY:
3031 ret = nvpair_native_embedded_array(nvs, nvp);
3032 break;
3033 case DATA_TYPE_STRING_ARRAY:
3034 nvpair_native_string_array(nvs, nvp);
3035 break;
3036 default:
3037 break;
3040 return (ret);
3043 static int
3044 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3046 uint64_t nvp_sz = nvp->nvp_size;
3048 switch (NVP_TYPE(nvp)) {
3049 case DATA_TYPE_NVLIST: {
3050 size_t nvsize = 0;
3052 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
3053 return (EINVAL);
3055 nvp_sz += nvsize;
3056 break;
3058 case DATA_TYPE_NVLIST_ARRAY: {
3059 size_t nvsize;
3061 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
3062 return (EINVAL);
3064 nvp_sz += nvsize;
3065 break;
3067 default:
3068 break;
3071 if (nvp_sz > INT32_MAX)
3072 return (EINVAL);
3074 *size = nvp_sz;
3076 return (0);
3079 static int
3080 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3082 switch (nvs->nvs_op) {
3083 case NVS_OP_ENCODE:
3084 return (nvs_native_nvp_op(nvs, nvp));
3086 case NVS_OP_DECODE: {
3087 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
3088 int32_t decode_len;
3090 /* try to read the size value from the stream */
3091 if (native->n_curr + sizeof (int32_t) > native->n_end)
3092 return (EFAULT);
3093 memcpy(&decode_len, native->n_curr, sizeof (int32_t));
3095 /* sanity check the size value */
3096 if (decode_len < 0 ||
3097 decode_len > native->n_end - native->n_curr)
3098 return (EFAULT);
3100 *size = decode_len;
3103 * If at the end of the stream then move the cursor
3104 * forward, otherwise nvpair_native_op() will read
3105 * the entire nvpair at the same cursor position.
3107 if (*size == 0)
3108 native->n_curr += sizeof (int32_t);
3109 break;
3112 default:
3113 return (EINVAL);
3116 return (0);
3119 static const nvs_ops_t nvs_native_ops = {
3120 .nvs_nvlist = nvs_native_nvlist,
3121 .nvs_nvpair = nvs_native_nvpair,
3122 .nvs_nvp_op = nvs_native_nvp_op,
3123 .nvs_nvp_size = nvs_native_nvp_size,
3124 .nvs_nvl_fini = nvs_native_nvl_fini
3127 static int
3128 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3130 nvs_native_t native;
3131 int err;
3133 nvs->nvs_ops = &nvs_native_ops;
3135 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
3136 *buflen - sizeof (nvs_header_t))) != 0)
3137 return (err);
3139 err = nvs_operation(nvs, nvl, buflen);
3141 nvs_native_destroy(nvs);
3143 return (err);
3147 * XDR encoding functions
3149 * An xdr packed nvlist is encoded as:
3151 * - encoding method and host endian (4 bytes)
3152 * - nvl_version (4 bytes)
3153 * - nvl_nvflag (4 bytes)
3155 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3156 * - encoded size of the nvpair (4 bytes)
3157 * - decoded size of the nvpair (4 bytes)
3158 * - name string, (4 + sizeof(NV_ALIGN4(string))
3159 * a string is coded as size (4 bytes) and data
3160 * - data type (4 bytes)
3161 * - number of elements in the nvpair (4 bytes)
3162 * - data
3164 * - 2 zero's for end of the entire list (8 bytes)
3166 static int
3167 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
3169 /* xdr data must be 4 byte aligned */
3170 if ((ulong_t)buf % 4 != 0)
3171 return (EFAULT);
3173 switch (nvs->nvs_op) {
3174 case NVS_OP_ENCODE:
3175 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
3176 nvs->nvs_private = xdr;
3177 return (0);
3178 case NVS_OP_DECODE:
3179 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
3180 nvs->nvs_private = xdr;
3181 return (0);
3182 case NVS_OP_GETSIZE:
3183 nvs->nvs_private = NULL;
3184 return (0);
3185 default:
3186 return (EINVAL);
3190 static void
3191 nvs_xdr_destroy(nvstream_t *nvs)
3193 switch (nvs->nvs_op) {
3194 case NVS_OP_ENCODE:
3195 case NVS_OP_DECODE:
3196 nvs->nvs_private = NULL;
3197 break;
3198 default:
3199 break;
3203 static int
3204 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
3206 switch (nvs->nvs_op) {
3207 case NVS_OP_ENCODE:
3208 case NVS_OP_DECODE: {
3209 XDR *xdr = nvs->nvs_private;
3211 if (!xdr_int(xdr, &nvl->nvl_version) ||
3212 !xdr_u_int(xdr, &nvl->nvl_nvflag))
3213 return (EFAULT);
3214 break;
3216 case NVS_OP_GETSIZE: {
3218 * 2 * 4 for nvl_version + nvl_nvflag
3219 * and 8 for end of the entire list
3221 *size += 2 * 4 + 8;
3222 break;
3224 default:
3225 return (EINVAL);
3227 return (0);
3230 static int
3231 nvs_xdr_nvl_fini(nvstream_t *nvs)
3233 if (nvs->nvs_op == NVS_OP_ENCODE) {
3234 XDR *xdr = nvs->nvs_private;
3235 int zero = 0;
3237 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
3238 return (EFAULT);
3241 return (0);
3245 * xdrproc_t-compatible callbacks for xdr_array()
3248 #if defined(_KERNEL) && defined(__linux__) /* Linux kernel */
3250 #define NVS_BUILD_XDRPROC_T(type) \
3251 static bool_t \
3252 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr) \
3254 return (xdr_##type(xdrs, ptr)); \
3257 #elif !defined(_KERNEL) && defined(XDR_CONTROL) /* tirpc */
3259 #define NVS_BUILD_XDRPROC_T(type) \
3260 static bool_t \
3261 nvs_xdr_nvp_##type(XDR *xdrs, ...) \
3263 va_list args; \
3264 void *ptr; \
3266 va_start(args, xdrs); \
3267 ptr = va_arg(args, void *); \
3268 va_end(args); \
3270 return (xdr_##type(xdrs, ptr)); \
3273 #else /* FreeBSD, sunrpc */
3275 #define NVS_BUILD_XDRPROC_T(type) \
3276 static bool_t \
3277 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr, ...) \
3279 return (xdr_##type(xdrs, ptr)); \
3282 #endif
3284 /* BEGIN CSTYLED */
3285 NVS_BUILD_XDRPROC_T(char);
3286 NVS_BUILD_XDRPROC_T(short);
3287 NVS_BUILD_XDRPROC_T(u_short);
3288 NVS_BUILD_XDRPROC_T(int);
3289 NVS_BUILD_XDRPROC_T(u_int);
3290 NVS_BUILD_XDRPROC_T(longlong_t);
3291 NVS_BUILD_XDRPROC_T(u_longlong_t);
3292 /* END CSTYLED */
3295 * The format of xdr encoded nvpair is:
3296 * encode_size, decode_size, name string, data type, nelem, data
3298 static int
3299 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
3301 ASSERT(nvs != NULL && nvp != NULL);
3303 data_type_t type;
3304 char *buf;
3305 char *buf_end = (char *)nvp + nvp->nvp_size;
3306 int value_sz;
3307 uint_t nelem, buflen;
3308 bool_t ret = FALSE;
3309 XDR *xdr = nvs->nvs_private;
3311 ASSERT(xdr != NULL);
3313 /* name string */
3314 if ((buf = NVP_NAME(nvp)) >= buf_end)
3315 return (EFAULT);
3316 buflen = buf_end - buf;
3318 if (!xdr_string(xdr, &buf, buflen - 1))
3319 return (EFAULT);
3320 nvp->nvp_name_sz = strlen(buf) + 1;
3322 /* type and nelem */
3323 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
3324 !xdr_int(xdr, &nvp->nvp_value_elem))
3325 return (EFAULT);
3327 type = NVP_TYPE(nvp);
3328 nelem = nvp->nvp_value_elem;
3331 * Verify type and nelem and get the value size.
3332 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3333 * is the size of the string(s) excluded.
3335 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
3336 return (EFAULT);
3338 /* if there is no data to extract then return */
3339 if (nelem == 0)
3340 return (0);
3342 /* value */
3343 if ((buf = NVP_VALUE(nvp)) >= buf_end)
3344 return (EFAULT);
3345 buflen = buf_end - buf;
3347 if (buflen < value_sz)
3348 return (EFAULT);
3350 switch (type) {
3351 case DATA_TYPE_NVLIST:
3352 if (nvs_embedded(nvs, (void *)buf) == 0)
3353 return (0);
3354 break;
3356 case DATA_TYPE_NVLIST_ARRAY:
3357 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
3358 return (0);
3359 break;
3361 case DATA_TYPE_BOOLEAN:
3362 ret = TRUE;
3363 break;
3365 case DATA_TYPE_BYTE:
3366 case DATA_TYPE_INT8:
3367 case DATA_TYPE_UINT8:
3368 ret = xdr_char(xdr, buf);
3369 break;
3371 case DATA_TYPE_INT16:
3372 ret = xdr_short(xdr, (void *)buf);
3373 break;
3375 case DATA_TYPE_UINT16:
3376 ret = xdr_u_short(xdr, (void *)buf);
3377 break;
3379 case DATA_TYPE_BOOLEAN_VALUE:
3380 case DATA_TYPE_INT32:
3381 ret = xdr_int(xdr, (void *)buf);
3382 break;
3384 case DATA_TYPE_UINT32:
3385 ret = xdr_u_int(xdr, (void *)buf);
3386 break;
3388 case DATA_TYPE_INT64:
3389 ret = xdr_longlong_t(xdr, (void *)buf);
3390 break;
3392 case DATA_TYPE_UINT64:
3393 ret = xdr_u_longlong_t(xdr, (void *)buf);
3394 break;
3396 case DATA_TYPE_HRTIME:
3398 * NOTE: must expose the definition of hrtime_t here
3400 ret = xdr_longlong_t(xdr, (void *)buf);
3401 break;
3402 #if !defined(_KERNEL)
3403 case DATA_TYPE_DOUBLE:
3404 ret = xdr_double(xdr, (void *)buf);
3405 break;
3406 #endif
3407 case DATA_TYPE_STRING:
3408 ret = xdr_string(xdr, &buf, buflen - 1);
3409 break;
3411 case DATA_TYPE_BYTE_ARRAY:
3412 ret = xdr_opaque(xdr, buf, nelem);
3413 break;
3415 case DATA_TYPE_INT8_ARRAY:
3416 case DATA_TYPE_UINT8_ARRAY:
3417 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3418 nvs_xdr_nvp_char);
3419 break;
3421 case DATA_TYPE_INT16_ARRAY:
3422 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3423 sizeof (int16_t), nvs_xdr_nvp_short);
3424 break;
3426 case DATA_TYPE_UINT16_ARRAY:
3427 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3428 sizeof (uint16_t), nvs_xdr_nvp_u_short);
3429 break;
3431 case DATA_TYPE_BOOLEAN_ARRAY:
3432 case DATA_TYPE_INT32_ARRAY:
3433 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3434 sizeof (int32_t), nvs_xdr_nvp_int);
3435 break;
3437 case DATA_TYPE_UINT32_ARRAY:
3438 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3439 sizeof (uint32_t), nvs_xdr_nvp_u_int);
3440 break;
3442 case DATA_TYPE_INT64_ARRAY:
3443 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3444 sizeof (int64_t), nvs_xdr_nvp_longlong_t);
3445 break;
3447 case DATA_TYPE_UINT64_ARRAY:
3448 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3449 sizeof (uint64_t), nvs_xdr_nvp_u_longlong_t);
3450 break;
3452 case DATA_TYPE_STRING_ARRAY: {
3453 size_t len = nelem * sizeof (uint64_t);
3454 char **strp = (void *)buf;
3455 int i;
3457 if (nvs->nvs_op == NVS_OP_DECODE)
3458 memset(buf, 0, len); /* don't trust packed data */
3460 for (i = 0; i < nelem; i++) {
3461 if (buflen <= len)
3462 return (EFAULT);
3464 buf += len;
3465 buflen -= len;
3467 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3468 return (EFAULT);
3470 if (nvs->nvs_op == NVS_OP_DECODE)
3471 strp[i] = buf;
3472 len = strlen(buf) + 1;
3474 ret = TRUE;
3475 break;
3477 default:
3478 break;
3481 return (ret == TRUE ? 0 : EFAULT);
3484 static int
3485 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3487 data_type_t type = NVP_TYPE(nvp);
3489 * encode_size + decode_size + name string size + data type + nelem
3490 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3492 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3494 switch (type) {
3495 case DATA_TYPE_BOOLEAN:
3496 break;
3498 case DATA_TYPE_BOOLEAN_VALUE:
3499 case DATA_TYPE_BYTE:
3500 case DATA_TYPE_INT8:
3501 case DATA_TYPE_UINT8:
3502 case DATA_TYPE_INT16:
3503 case DATA_TYPE_UINT16:
3504 case DATA_TYPE_INT32:
3505 case DATA_TYPE_UINT32:
3506 nvp_sz += 4; /* 4 is the minimum xdr unit */
3507 break;
3509 case DATA_TYPE_INT64:
3510 case DATA_TYPE_UINT64:
3511 case DATA_TYPE_HRTIME:
3512 #if !defined(_KERNEL)
3513 case DATA_TYPE_DOUBLE:
3514 #endif
3515 nvp_sz += 8;
3516 break;
3518 case DATA_TYPE_STRING:
3519 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3520 break;
3522 case DATA_TYPE_BYTE_ARRAY:
3523 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3524 break;
3526 case DATA_TYPE_BOOLEAN_ARRAY:
3527 case DATA_TYPE_INT8_ARRAY:
3528 case DATA_TYPE_UINT8_ARRAY:
3529 case DATA_TYPE_INT16_ARRAY:
3530 case DATA_TYPE_UINT16_ARRAY:
3531 case DATA_TYPE_INT32_ARRAY:
3532 case DATA_TYPE_UINT32_ARRAY:
3533 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3534 break;
3536 case DATA_TYPE_INT64_ARRAY:
3537 case DATA_TYPE_UINT64_ARRAY:
3538 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3539 break;
3541 case DATA_TYPE_STRING_ARRAY: {
3542 int i;
3543 char **strs = (void *)NVP_VALUE(nvp);
3545 for (i = 0; i < NVP_NELEM(nvp); i++)
3546 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3548 break;
3551 case DATA_TYPE_NVLIST:
3552 case DATA_TYPE_NVLIST_ARRAY: {
3553 size_t nvsize = 0;
3554 int old_nvs_op = nvs->nvs_op;
3555 int err;
3557 nvs->nvs_op = NVS_OP_GETSIZE;
3558 if (type == DATA_TYPE_NVLIST)
3559 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3560 else
3561 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3562 nvs->nvs_op = old_nvs_op;
3564 if (err != 0)
3565 return (EINVAL);
3567 nvp_sz += nvsize;
3568 break;
3571 default:
3572 return (EINVAL);
3575 if (nvp_sz > INT32_MAX)
3576 return (EINVAL);
3578 *size = nvp_sz;
3580 return (0);
3585 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3586 * the largest nvpair that could be encoded in the buffer.
3588 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3589 * The size of a xdr packed nvpair without any data is 5 words.
3591 * Using the size of the data directly as an estimate would be ok
3592 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3593 * then the actual nvpair has space for an array of pointers to index
3594 * the strings. These pointers are not encoded into the packed xdr buffer.
3596 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3597 * of length 0, then each string is encoded in xdr format as a single word.
3598 * Therefore when expanded to an nvpair there will be 2.25 word used for
3599 * each string. (a int64_t allocated for pointer usage, and a single char
3600 * for the null termination.)
3602 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3604 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3605 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3606 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3607 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3608 (NVS_XDR_DATA_LEN(x) * 2) + \
3609 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3611 static int
3612 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3614 XDR *xdr = nvs->nvs_private;
3615 int32_t encode_len, decode_len;
3617 switch (nvs->nvs_op) {
3618 case NVS_OP_ENCODE: {
3619 size_t nvsize;
3621 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3622 return (EFAULT);
3624 decode_len = nvp->nvp_size;
3625 encode_len = nvsize;
3626 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3627 return (EFAULT);
3629 return (nvs_xdr_nvp_op(nvs, nvp));
3631 case NVS_OP_DECODE: {
3632 struct xdr_bytesrec bytesrec;
3634 /* get the encode and decode size */
3635 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3636 return (EFAULT);
3637 *size = decode_len;
3639 /* are we at the end of the stream? */
3640 if (*size == 0)
3641 return (0);
3643 /* sanity check the size parameter */
3644 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3645 return (EFAULT);
3647 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3648 return (EFAULT);
3649 break;
3652 default:
3653 return (EINVAL);
3655 return (0);
3658 static const struct nvs_ops nvs_xdr_ops = {
3659 .nvs_nvlist = nvs_xdr_nvlist,
3660 .nvs_nvpair = nvs_xdr_nvpair,
3661 .nvs_nvp_op = nvs_xdr_nvp_op,
3662 .nvs_nvp_size = nvs_xdr_nvp_size,
3663 .nvs_nvl_fini = nvs_xdr_nvl_fini
3666 static int
3667 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3669 XDR xdr;
3670 int err;
3672 nvs->nvs_ops = &nvs_xdr_ops;
3674 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3675 *buflen - sizeof (nvs_header_t))) != 0)
3676 return (err);
3678 err = nvs_operation(nvs, nvl, buflen);
3680 nvs_xdr_destroy(nvs);
3682 return (err);
3685 EXPORT_SYMBOL(nv_alloc_init);
3686 EXPORT_SYMBOL(nv_alloc_reset);
3687 EXPORT_SYMBOL(nv_alloc_fini);
3689 /* list management */
3690 EXPORT_SYMBOL(nvlist_alloc);
3691 EXPORT_SYMBOL(nvlist_free);
3692 EXPORT_SYMBOL(nvlist_size);
3693 EXPORT_SYMBOL(nvlist_pack);
3694 EXPORT_SYMBOL(nvlist_unpack);
3695 EXPORT_SYMBOL(nvlist_dup);
3696 EXPORT_SYMBOL(nvlist_merge);
3698 EXPORT_SYMBOL(nvlist_xalloc);
3699 EXPORT_SYMBOL(nvlist_xpack);
3700 EXPORT_SYMBOL(nvlist_xunpack);
3701 EXPORT_SYMBOL(nvlist_xdup);
3702 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3704 EXPORT_SYMBOL(nvlist_add_nvpair);
3705 EXPORT_SYMBOL(nvlist_add_boolean);
3706 EXPORT_SYMBOL(nvlist_add_boolean_value);
3707 EXPORT_SYMBOL(nvlist_add_byte);
3708 EXPORT_SYMBOL(nvlist_add_int8);
3709 EXPORT_SYMBOL(nvlist_add_uint8);
3710 EXPORT_SYMBOL(nvlist_add_int16);
3711 EXPORT_SYMBOL(nvlist_add_uint16);
3712 EXPORT_SYMBOL(nvlist_add_int32);
3713 EXPORT_SYMBOL(nvlist_add_uint32);
3714 EXPORT_SYMBOL(nvlist_add_int64);
3715 EXPORT_SYMBOL(nvlist_add_uint64);
3716 EXPORT_SYMBOL(nvlist_add_string);
3717 EXPORT_SYMBOL(nvlist_add_nvlist);
3718 EXPORT_SYMBOL(nvlist_add_boolean_array);
3719 EXPORT_SYMBOL(nvlist_add_byte_array);
3720 EXPORT_SYMBOL(nvlist_add_int8_array);
3721 EXPORT_SYMBOL(nvlist_add_uint8_array);
3722 EXPORT_SYMBOL(nvlist_add_int16_array);
3723 EXPORT_SYMBOL(nvlist_add_uint16_array);
3724 EXPORT_SYMBOL(nvlist_add_int32_array);
3725 EXPORT_SYMBOL(nvlist_add_uint32_array);
3726 EXPORT_SYMBOL(nvlist_add_int64_array);
3727 EXPORT_SYMBOL(nvlist_add_uint64_array);
3728 EXPORT_SYMBOL(nvlist_add_string_array);
3729 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3730 EXPORT_SYMBOL(nvlist_next_nvpair);
3731 EXPORT_SYMBOL(nvlist_prev_nvpair);
3732 EXPORT_SYMBOL(nvlist_empty);
3733 EXPORT_SYMBOL(nvlist_add_hrtime);
3735 EXPORT_SYMBOL(nvlist_remove);
3736 EXPORT_SYMBOL(nvlist_remove_nvpair);
3737 EXPORT_SYMBOL(nvlist_remove_all);
3739 EXPORT_SYMBOL(nvlist_lookup_boolean);
3740 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3741 EXPORT_SYMBOL(nvlist_lookup_byte);
3742 EXPORT_SYMBOL(nvlist_lookup_int8);
3743 EXPORT_SYMBOL(nvlist_lookup_uint8);
3744 EXPORT_SYMBOL(nvlist_lookup_int16);
3745 EXPORT_SYMBOL(nvlist_lookup_uint16);
3746 EXPORT_SYMBOL(nvlist_lookup_int32);
3747 EXPORT_SYMBOL(nvlist_lookup_uint32);
3748 EXPORT_SYMBOL(nvlist_lookup_int64);
3749 EXPORT_SYMBOL(nvlist_lookup_uint64);
3750 EXPORT_SYMBOL(nvlist_lookup_string);
3751 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3752 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3753 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3754 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3755 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3756 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3757 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3758 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3759 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3760 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3761 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3762 EXPORT_SYMBOL(nvlist_lookup_string_array);
3763 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3764 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3765 EXPORT_SYMBOL(nvlist_lookup_pairs);
3767 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3768 EXPORT_SYMBOL(nvlist_exists);
3770 /* processing nvpair */
3771 EXPORT_SYMBOL(nvpair_name);
3772 EXPORT_SYMBOL(nvpair_type);
3773 EXPORT_SYMBOL(nvpair_value_boolean_value);
3774 EXPORT_SYMBOL(nvpair_value_byte);
3775 EXPORT_SYMBOL(nvpair_value_int8);
3776 EXPORT_SYMBOL(nvpair_value_uint8);
3777 EXPORT_SYMBOL(nvpair_value_int16);
3778 EXPORT_SYMBOL(nvpair_value_uint16);
3779 EXPORT_SYMBOL(nvpair_value_int32);
3780 EXPORT_SYMBOL(nvpair_value_uint32);
3781 EXPORT_SYMBOL(nvpair_value_int64);
3782 EXPORT_SYMBOL(nvpair_value_uint64);
3783 EXPORT_SYMBOL(nvpair_value_string);
3784 EXPORT_SYMBOL(nvpair_value_nvlist);
3785 EXPORT_SYMBOL(nvpair_value_boolean_array);
3786 EXPORT_SYMBOL(nvpair_value_byte_array);
3787 EXPORT_SYMBOL(nvpair_value_int8_array);
3788 EXPORT_SYMBOL(nvpair_value_uint8_array);
3789 EXPORT_SYMBOL(nvpair_value_int16_array);
3790 EXPORT_SYMBOL(nvpair_value_uint16_array);
3791 EXPORT_SYMBOL(nvpair_value_int32_array);
3792 EXPORT_SYMBOL(nvpair_value_uint32_array);
3793 EXPORT_SYMBOL(nvpair_value_int64_array);
3794 EXPORT_SYMBOL(nvpair_value_uint64_array);
3795 EXPORT_SYMBOL(nvpair_value_string_array);
3796 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3797 EXPORT_SYMBOL(nvpair_value_hrtime);