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 http://www.opensolaris.org/os/licensing.
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]
23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015, 2017 by Delphix. All rights reserved.
27 #include <sys/debug.h>
28 #include <sys/isa_defs.h>
29 #include <sys/nvpair.h>
30 #include <sys/nvpair_impl.h>
31 #include <sys/types.h>
32 #include <sys/strings.h>
36 #include <sys/sunddi.h>
37 #include <sys/sysmacros.h>
44 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
47 * nvpair.c - Provides kernel & userland interfaces for manipulating
62 * +--------------+ last i_nvp in list
63 * | nvpriv_t | +--------------------->
65 * +--+- nvp_list | | +------------+
66 * | | nvp_last -+--+ + nv_alloc_t |
67 * | | nvp_curr | |------------|
68 * | | nvp_nva -+----> | nva_ops |
69 * | | nvp_stat | | nva_arg |
70 * | +--------------+ +------------+
74 * +---------------------+ +-------------------+
75 * | i_nvp_t | +-->| i_nvp_t | +-->
76 * |---------------------| | |-------------------| |
77 * | nvi_next -+--+ | nvi_next -+--+
78 * | nvi_prev (NULL) | <----+ nvi_prev |
79 * | . . . . . . . . . . | | . . . . . . . . . |
80 * | nvp (nvpair_t) | | nvp (nvpair_t) |
81 * | - nvp_size | | - nvp_size |
82 * | - nvp_name_sz | | - nvp_name_sz |
83 * | - nvp_value_elem | | - nvp_value_elem |
84 * | - nvp_type | | - nvp_type |
85 * | - data ... | | - data ... |
86 * +---------------------+ +-------------------+
90 * +---------------------+ +---------------------+
91 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
92 * |---------------------| | | |---------------------|
93 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
94 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
95 * | . . . . . . . . . | | . . . . . . . . . |
96 * | nvp (nvpair_t) | | nvp (nvpair_t) |
97 * | - nvp_size | | - nvp_size |
98 * | - nvp_name_sz | | - nvp_name_sz |
99 * | - nvp_value_elem | | - nvp_value_elem |
100 * | - DATA_TYPE_NVLIST | | - nvp_type |
101 * | - data (embedded) | | - data ... |
102 * | nvlist name | +---------------------+
103 * | +--------------+ |
105 * | |--------------| |
106 * | | nvl_version | |
108 * | | nvl_priv --+---+---->
111 * | +--------------+ |
112 * +---------------------+
115 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
116 * allow value to be aligned on 8 byte boundary
118 * name_len is the length of the name string including the null terminator
121 #define NVP_SIZE_CALC(name_len, data_len) \
122 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
124 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
125 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
126 uint_t nelem, const void *data);
128 #define NV_STAT_EMBEDDED 0x1
129 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
130 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
132 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
133 #define NVPAIR2I_NVP(nvp) \
134 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
137 int nvpair_max_recursion = 20;
139 int nvpair_max_recursion = 100;
142 uint64_t nvlist_hashtable_init_size = (1 << 4);
145 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
153 va_start(valist, nvo);
154 if (nva->nva_ops->nv_ao_init != NULL)
155 err = nva->nva_ops->nv_ao_init(nva, valist);
162 nv_alloc_reset(nv_alloc_t *nva)
164 if (nva->nva_ops->nv_ao_reset != NULL)
165 nva->nva_ops->nv_ao_reset(nva);
169 nv_alloc_fini(nv_alloc_t *nva)
171 if (nva->nva_ops->nv_ao_fini != NULL)
172 nva->nva_ops->nv_ao_fini(nva);
176 nvlist_lookup_nv_alloc(nvlist_t *nvl)
181 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
184 return (priv->nvp_nva);
188 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
190 nv_alloc_t *nva = nvp->nvp_nva;
193 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
200 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
202 nv_alloc_t *nva = nvp->nvp_nva;
204 nva->nva_ops->nv_ao_free(nva, buf, size);
208 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
210 bzero(priv, sizeof (nvpriv_t));
213 priv->nvp_stat = stat;
217 nv_priv_alloc(nv_alloc_t *nva)
222 * nv_mem_alloc() cannot called here because it needs the priv
225 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
228 nv_priv_init(priv, nva, 0);
234 * Embedded lists need their own nvpriv_t's. We create a new
235 * nvpriv_t using the parameters and allocator from the parent
239 nv_priv_alloc_embedded(nvpriv_t *priv)
243 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
246 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
252 nvt_tab_alloc(nvpriv_t *priv, uint64_t buckets)
254 ASSERT3P(priv->nvp_hashtable, ==, NULL);
255 ASSERT0(priv->nvp_nbuckets);
256 ASSERT0(priv->nvp_nentries);
258 i_nvp_t **tab = nv_mem_zalloc(priv, buckets * sizeof (i_nvp_t *));
262 priv->nvp_hashtable = tab;
263 priv->nvp_nbuckets = buckets;
268 nvt_tab_free(nvpriv_t *priv)
270 i_nvp_t **tab = priv->nvp_hashtable;
272 ASSERT0(priv->nvp_nbuckets);
273 ASSERT0(priv->nvp_nentries);
277 nv_mem_free(priv, tab, priv->nvp_nbuckets * sizeof (i_nvp_t *));
279 priv->nvp_hashtable = NULL;
280 priv->nvp_nbuckets = 0;
281 priv->nvp_nentries = 0;
285 nvt_hash(const char *p)
287 uint32_t g, hval = 0;
290 hval = (hval << 4) + *p++;
291 if ((g = (hval & 0xf0000000)) != 0)
299 nvt_nvpair_match(nvpair_t *nvp1, nvpair_t *nvp2, uint32_t nvflag)
301 boolean_t match = B_FALSE;
302 if (nvflag & NV_UNIQUE_NAME_TYPE) {
303 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0 &&
304 NVP_TYPE(nvp1) == NVP_TYPE(nvp2))
307 ASSERT(nvflag == 0 || nvflag & NV_UNIQUE_NAME);
308 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0)
315 nvt_lookup_name_type(nvlist_t *nvl, const char *name, data_type_t type)
317 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
318 ASSERT(priv != NULL);
320 i_nvp_t **tab = priv->nvp_hashtable;
323 ASSERT3P(priv->nvp_list, ==, NULL);
324 ASSERT0(priv->nvp_nbuckets);
325 ASSERT0(priv->nvp_nentries);
328 ASSERT(priv->nvp_nbuckets != 0);
331 uint64_t hash = nvt_hash(name);
332 uint64_t index = hash & (priv->nvp_nbuckets - 1);
334 ASSERT3U(index, <, priv->nvp_nbuckets);
335 i_nvp_t *entry = tab[index];
337 for (i_nvp_t *e = entry; e != NULL; e = e->nvi_hashtable_next) {
338 if (strcmp(NVP_NAME(&e->nvi_nvp), name) == 0 &&
339 (type == DATA_TYPE_DONTCARE ||
340 NVP_TYPE(&e->nvi_nvp) == type))
341 return (&e->nvi_nvp);
347 nvt_lookup_name(nvlist_t *nvl, const char *name)
349 return (nvt_lookup_name_type(nvl, name, DATA_TYPE_DONTCARE));
353 nvt_resize(nvpriv_t *priv, uint32_t new_size)
355 i_nvp_t **tab = priv->nvp_hashtable;
358 * Migrate all the entries from the current table
359 * to a newly-allocated table with the new size by
360 * re-adjusting the pointers of their entries.
362 uint32_t size = priv->nvp_nbuckets;
363 uint32_t new_mask = new_size - 1;
364 ASSERT(ISP2(new_size));
366 i_nvp_t **new_tab = nv_mem_zalloc(priv, new_size * sizeof (i_nvp_t *));
370 uint32_t nentries = 0;
371 for (uint32_t i = 0; i < size; i++) {
372 i_nvp_t *next, *e = tab[i];
375 next = e->nvi_hashtable_next;
377 uint32_t hash = nvt_hash(NVP_NAME(&e->nvi_nvp));
378 uint32_t index = hash & new_mask;
380 e->nvi_hashtable_next = new_tab[index];
388 ASSERT3U(nentries, ==, priv->nvp_nentries);
392 priv->nvp_hashtable = new_tab;
393 priv->nvp_nbuckets = new_size;
394 priv->nvp_nentries = nentries;
400 nvt_needs_togrow(nvpriv_t *priv)
403 * Grow only when we have more elements than buckets
404 * and the # of buckets doesn't overflow.
406 return (priv->nvp_nentries > priv->nvp_nbuckets &&
407 (UINT32_MAX >> 1) >= priv->nvp_nbuckets);
411 * Allocate a new table that's twice the size of the old one,
412 * and migrate all the entries from the old one to the new
413 * one by re-adjusting their pointers.
416 nvt_grow(nvpriv_t *priv)
418 uint32_t current_size = priv->nvp_nbuckets;
419 /* ensure we won't overflow */
420 ASSERT3U(UINT32_MAX >> 1, >=, current_size);
421 return (nvt_resize(priv, current_size << 1));
425 nvt_needs_toshrink(nvpriv_t *priv)
428 * Shrink only when the # of elements is less than or
429 * equal to 1/4 the # of buckets. Never shrink less than
430 * nvlist_hashtable_init_size.
432 ASSERT3U(priv->nvp_nbuckets, >=, nvlist_hashtable_init_size);
433 if (priv->nvp_nbuckets == nvlist_hashtable_init_size)
435 return (priv->nvp_nentries <= (priv->nvp_nbuckets >> 2));
439 * Allocate a new table that's half the size of the old one,
440 * and migrate all the entries from the old one to the new
441 * one by re-adjusting their pointers.
444 nvt_shrink(nvpriv_t *priv)
446 uint32_t current_size = priv->nvp_nbuckets;
447 /* ensure we won't overflow */
448 ASSERT3U(current_size, >=, nvlist_hashtable_init_size);
449 return (nvt_resize(priv, current_size >> 1));
453 nvt_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
455 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
457 if (nvt_needs_toshrink(priv)) {
458 int err = nvt_shrink(priv);
462 i_nvp_t **tab = priv->nvp_hashtable;
464 char *name = NVP_NAME(nvp);
465 uint64_t hash = nvt_hash(name);
466 uint64_t index = hash & (priv->nvp_nbuckets - 1);
468 ASSERT3U(index, <, priv->nvp_nbuckets);
469 i_nvp_t *bucket = tab[index];
471 for (i_nvp_t *prev = NULL, *e = bucket;
472 e != NULL; prev = e, e = e->nvi_hashtable_next) {
473 if (nvt_nvpair_match(&e->nvi_nvp, nvp, nvl->nvl_flag)) {
475 prev->nvi_hashtable_next =
476 e->nvi_hashtable_next;
478 ASSERT3P(e, ==, bucket);
479 tab[index] = e->nvi_hashtable_next;
481 e->nvi_hashtable_next = NULL;
482 priv->nvp_nentries--;
491 nvt_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
493 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
495 /* initialize nvpair table now if it doesn't exist. */
496 if (priv->nvp_hashtable == NULL) {
497 int err = nvt_tab_alloc(priv, nvlist_hashtable_init_size);
503 * if we don't allow duplicate entries, make sure to
504 * unlink any existing entries from the table.
506 if (nvl->nvl_nvflag != 0) {
507 int err = nvt_remove_nvpair(nvl, nvp);
512 if (nvt_needs_togrow(priv)) {
513 int err = nvt_grow(priv);
517 i_nvp_t **tab = priv->nvp_hashtable;
519 char *name = NVP_NAME(nvp);
520 uint64_t hash = nvt_hash(name);
521 uint64_t index = hash & (priv->nvp_nbuckets - 1);
523 ASSERT3U(index, <, priv->nvp_nbuckets);
524 i_nvp_t *bucket = tab[index];
526 /* insert link at the beginning of the bucket */
527 i_nvp_t *new_entry = NVPAIR2I_NVP(nvp);
528 ASSERT3P(new_entry->nvi_hashtable_next, ==, NULL);
529 new_entry->nvi_hashtable_next = bucket;
530 tab[index] = new_entry;
532 priv->nvp_nentries++;
537 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
539 nvl->nvl_version = NV_VERSION;
540 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
541 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
547 nvlist_nvflag(nvlist_t *nvl)
549 return (nvl->nvl_nvflag);
553 nvlist_nv_alloc(int kmflag)
558 return (nv_alloc_sleep);
560 return (nv_alloc_pushpage);
562 return (nv_alloc_nosleep);
565 return (nv_alloc_nosleep);
570 * nvlist_alloc - Allocate nvlist.
573 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
575 return (nvlist_xalloc(nvlp, nvflag, nvlist_nv_alloc(kmflag)));
579 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
583 if (nvlp == NULL || nva == NULL)
586 if ((priv = nv_priv_alloc(nva)) == NULL)
589 if ((*nvlp = nv_mem_zalloc(priv,
590 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
591 nv_mem_free(priv, priv, sizeof (nvpriv_t));
595 nvlist_init(*nvlp, nvflag, priv);
601 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
604 nvp_buf_alloc(nvlist_t *nvl, size_t len)
606 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
612 * Allocate the buffer
614 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
616 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
626 * nvp_buf_free - de-Allocate an i_nvp_t.
629 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
631 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
632 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
634 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
638 * nvp_buf_link - link a new nv pair into the nvlist.
641 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
643 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
644 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
646 /* Put element at end of nvlist */
647 if (priv->nvp_list == NULL) {
648 priv->nvp_list = priv->nvp_last = curr;
650 curr->nvi_prev = priv->nvp_last;
651 priv->nvp_last->nvi_next = curr;
652 priv->nvp_last = curr;
657 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
660 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
662 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
663 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
666 * protect nvlist_next_nvpair() against walking on freed memory.
668 if (priv->nvp_curr == curr)
669 priv->nvp_curr = curr->nvi_next;
671 if (curr == priv->nvp_list)
672 priv->nvp_list = curr->nvi_next;
674 curr->nvi_prev->nvi_next = curr->nvi_next;
676 if (curr == priv->nvp_last)
677 priv->nvp_last = curr->nvi_prev;
679 curr->nvi_next->nvi_prev = curr->nvi_prev;
683 * take a nvpair type and number of elements and make sure the are valid
686 i_validate_type_nelem(data_type_t type, uint_t nelem)
689 case DATA_TYPE_BOOLEAN:
693 case DATA_TYPE_BOOLEAN_VALUE:
696 case DATA_TYPE_UINT8:
697 case DATA_TYPE_INT16:
698 case DATA_TYPE_UINT16:
699 case DATA_TYPE_INT32:
700 case DATA_TYPE_UINT32:
701 case DATA_TYPE_INT64:
702 case DATA_TYPE_UINT64:
703 case DATA_TYPE_STRING:
704 case DATA_TYPE_HRTIME:
705 case DATA_TYPE_NVLIST:
706 #if !defined(_KERNEL)
707 case DATA_TYPE_DOUBLE:
712 case DATA_TYPE_BOOLEAN_ARRAY:
713 case DATA_TYPE_BYTE_ARRAY:
714 case DATA_TYPE_INT8_ARRAY:
715 case DATA_TYPE_UINT8_ARRAY:
716 case DATA_TYPE_INT16_ARRAY:
717 case DATA_TYPE_UINT16_ARRAY:
718 case DATA_TYPE_INT32_ARRAY:
719 case DATA_TYPE_UINT32_ARRAY:
720 case DATA_TYPE_INT64_ARRAY:
721 case DATA_TYPE_UINT64_ARRAY:
722 case DATA_TYPE_STRING_ARRAY:
723 case DATA_TYPE_NVLIST_ARRAY:
724 /* we allow arrays with 0 elements */
733 * Verify nvp_name_sz and check the name string length.
736 i_validate_nvpair_name(nvpair_t *nvp)
738 if ((nvp->nvp_name_sz <= 0) ||
739 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
742 /* verify the name string, make sure its terminated */
743 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
746 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
750 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
753 case DATA_TYPE_BOOLEAN_VALUE:
754 if (*(boolean_t *)data != B_TRUE &&
755 *(boolean_t *)data != B_FALSE)
758 case DATA_TYPE_BOOLEAN_ARRAY: {
761 for (i = 0; i < nelem; i++)
762 if (((boolean_t *)data)[i] != B_TRUE &&
763 ((boolean_t *)data)[i] != B_FALSE)
775 * This function takes a pointer to what should be a nvpair and it's size
776 * and then verifies that all the nvpair fields make sense and can be
777 * trusted. This function is used when decoding packed nvpairs.
780 i_validate_nvpair(nvpair_t *nvp)
782 data_type_t type = NVP_TYPE(nvp);
785 /* verify nvp_name_sz, check the name string length */
786 if (i_validate_nvpair_name(nvp) != 0)
789 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
793 * verify nvp_type, nvp_value_elem, and also possibly
794 * verify string values and get the value size.
796 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
797 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
798 if (size2 < 0 || size1 != NV_ALIGN(size2))
805 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
810 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
813 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
814 nvpair_t *nvp = &curr->nvi_nvp;
817 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
818 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
826 * Frees all memory allocated for an nvpair (like embedded lists) with
827 * the exception of the nvpair buffer itself.
830 nvpair_free(nvpair_t *nvp)
832 switch (NVP_TYPE(nvp)) {
833 case DATA_TYPE_NVLIST:
834 nvlist_free(EMBEDDED_NVL(nvp));
836 case DATA_TYPE_NVLIST_ARRAY: {
837 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
840 for (i = 0; i < NVP_NELEM(nvp); i++)
842 nvlist_free(nvlp[i]);
851 * nvlist_free - free an unpacked nvlist
854 nvlist_free(nvlist_t *nvl)
860 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
864 * Unpacked nvlist are linked through i_nvp_t
866 curr = priv->nvp_list;
867 while (curr != NULL) {
868 nvpair_t *nvp = &curr->nvi_nvp;
869 curr = curr->nvi_next;
872 nvp_buf_free(nvl, nvp);
875 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
876 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
881 nv_mem_free(priv, priv, sizeof (nvpriv_t));
885 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
887 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
893 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
894 if (&curr->nvi_nvp == nvp)
901 * Make a copy of nvlist
904 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
906 return (nvlist_xdup(nvl, nvlp, nvlist_nv_alloc(kmflag)));
910 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
915 if (nvl == NULL || nvlp == NULL)
918 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
921 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
930 * Remove all with matching name
933 nvlist_remove_all(nvlist_t *nvl, const char *name)
937 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
941 while ((nvp = nvt_lookup_name(nvl, name)) != NULL) {
942 VERIFY0(nvlist_remove_nvpair(nvl, nvp));
950 * Remove first one with matching name and type
953 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
955 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
958 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
962 return (nvlist_remove_nvpair(nvl, nvp));
966 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
968 if (nvl == NULL || nvp == NULL)
971 int err = nvt_remove_nvpair(nvl, nvp);
975 nvp_buf_unlink(nvl, nvp);
977 nvp_buf_free(nvl, nvp);
982 * This function calculates the size of an nvpair value.
984 * The data argument controls the behavior in case of the data types
985 * DATA_TYPE_STRING and
986 * DATA_TYPE_STRING_ARRAY
987 * Is data == NULL then the size of the string(s) is excluded.
990 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
994 if (i_validate_type_nelem(type, nelem) != 0)
997 /* Calculate required size for holding value */
999 case DATA_TYPE_BOOLEAN:
1002 case DATA_TYPE_BOOLEAN_VALUE:
1003 value_sz = sizeof (boolean_t);
1005 case DATA_TYPE_BYTE:
1006 value_sz = sizeof (uchar_t);
1008 case DATA_TYPE_INT8:
1009 value_sz = sizeof (int8_t);
1011 case DATA_TYPE_UINT8:
1012 value_sz = sizeof (uint8_t);
1014 case DATA_TYPE_INT16:
1015 value_sz = sizeof (int16_t);
1017 case DATA_TYPE_UINT16:
1018 value_sz = sizeof (uint16_t);
1020 case DATA_TYPE_INT32:
1021 value_sz = sizeof (int32_t);
1023 case DATA_TYPE_UINT32:
1024 value_sz = sizeof (uint32_t);
1026 case DATA_TYPE_INT64:
1027 value_sz = sizeof (int64_t);
1029 case DATA_TYPE_UINT64:
1030 value_sz = sizeof (uint64_t);
1032 #if !defined(_KERNEL)
1033 case DATA_TYPE_DOUBLE:
1034 value_sz = sizeof (double);
1037 case DATA_TYPE_STRING:
1041 value_sz = strlen(data) + 1;
1043 case DATA_TYPE_BOOLEAN_ARRAY:
1044 value_sz = (uint64_t)nelem * sizeof (boolean_t);
1046 case DATA_TYPE_BYTE_ARRAY:
1047 value_sz = (uint64_t)nelem * sizeof (uchar_t);
1049 case DATA_TYPE_INT8_ARRAY:
1050 value_sz = (uint64_t)nelem * sizeof (int8_t);
1052 case DATA_TYPE_UINT8_ARRAY:
1053 value_sz = (uint64_t)nelem * sizeof (uint8_t);
1055 case DATA_TYPE_INT16_ARRAY:
1056 value_sz = (uint64_t)nelem * sizeof (int16_t);
1058 case DATA_TYPE_UINT16_ARRAY:
1059 value_sz = (uint64_t)nelem * sizeof (uint16_t);
1061 case DATA_TYPE_INT32_ARRAY:
1062 value_sz = (uint64_t)nelem * sizeof (int32_t);
1064 case DATA_TYPE_UINT32_ARRAY:
1065 value_sz = (uint64_t)nelem * sizeof (uint32_t);
1067 case DATA_TYPE_INT64_ARRAY:
1068 value_sz = (uint64_t)nelem * sizeof (int64_t);
1070 case DATA_TYPE_UINT64_ARRAY:
1071 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1073 case DATA_TYPE_STRING_ARRAY:
1074 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1077 char *const *strs = data;
1080 /* no alignment requirement for strings */
1081 for (i = 0; i < nelem; i++) {
1082 if (strs[i] == NULL)
1084 value_sz += strlen(strs[i]) + 1;
1088 case DATA_TYPE_HRTIME:
1089 value_sz = sizeof (hrtime_t);
1091 case DATA_TYPE_NVLIST:
1092 value_sz = NV_ALIGN(sizeof (nvlist_t));
1094 case DATA_TYPE_NVLIST_ARRAY:
1095 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
1096 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
1102 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
1106 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
1111 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
1112 nvl->nvl_priv)) == NULL)
1115 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
1117 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
1118 nvlist_free(emb_nvl);
1119 emb_nvl->nvl_priv = 0;
1126 * nvlist_add_common - Add new <name,value> pair to nvlist
1129 nvlist_add_common(nvlist_t *nvl, const char *name,
1130 data_type_t type, uint_t nelem, const void *data)
1135 int nvp_sz, name_sz, value_sz;
1138 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1141 if (nelem != 0 && data == NULL)
1145 * Verify type and nelem and get the value size.
1146 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1147 * is the size of the string(s) included.
1149 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
1152 if (i_validate_nvpair_value(type, nelem, data) != 0)
1156 * If we're adding an nvlist or nvlist array, ensure that we are not
1157 * adding the input nvlist to itself, which would cause recursion,
1158 * and ensure that no NULL nvlist pointers are present.
1161 case DATA_TYPE_NVLIST:
1162 if (data == nvl || data == NULL)
1165 case DATA_TYPE_NVLIST_ARRAY: {
1166 nvlist_t **onvlp = (nvlist_t **)data;
1167 for (i = 0; i < nelem; i++) {
1168 if (onvlp[i] == nvl || onvlp[i] == NULL)
1177 /* calculate sizes of the nvpair elements and the nvpair itself */
1178 name_sz = strlen(name) + 1;
1179 if (name_sz >= 1ULL << (sizeof (nvp->nvp_name_sz) * NBBY - 1))
1182 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
1184 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
1187 ASSERT(nvp->nvp_size == nvp_sz);
1188 nvp->nvp_name_sz = name_sz;
1189 nvp->nvp_value_elem = nelem;
1190 nvp->nvp_type = type;
1191 bcopy(name, NVP_NAME(nvp), name_sz);
1194 case DATA_TYPE_BOOLEAN:
1196 case DATA_TYPE_STRING_ARRAY: {
1197 char *const *strs = data;
1198 char *buf = NVP_VALUE(nvp);
1199 char **cstrs = (void *)buf;
1201 /* skip pre-allocated space for pointer array */
1202 buf += nelem * sizeof (uint64_t);
1203 for (i = 0; i < nelem; i++) {
1204 int slen = strlen(strs[i]) + 1;
1205 bcopy(strs[i], buf, slen);
1211 case DATA_TYPE_NVLIST: {
1212 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
1213 nvlist_t *onvl = (nvlist_t *)data;
1215 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
1216 nvp_buf_free(nvl, nvp);
1221 case DATA_TYPE_NVLIST_ARRAY: {
1222 nvlist_t **onvlp = (nvlist_t **)data;
1223 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1224 nvlist_t *embedded = (nvlist_t *)
1225 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
1227 for (i = 0; i < nelem; i++) {
1228 if ((err = nvlist_copy_embedded(nvl,
1229 onvlp[i], embedded)) != 0) {
1231 * Free any successfully created lists
1234 nvp_buf_free(nvl, nvp);
1238 nvlp[i] = embedded++;
1243 bcopy(data, NVP_VALUE(nvp), value_sz);
1246 /* if unique name, remove before add */
1247 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
1248 (void) nvlist_remove_all(nvl, name);
1249 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
1250 (void) nvlist_remove(nvl, name, type);
1252 err = nvt_add_nvpair(nvl, nvp);
1255 nvp_buf_free(nvl, nvp);
1258 nvp_buf_link(nvl, nvp);
1264 nvlist_add_boolean(nvlist_t *nvl, const char *name)
1266 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1270 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1272 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1276 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1278 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1282 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1284 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1288 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1290 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1294 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1296 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1300 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1302 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1306 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1308 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1312 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1314 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1318 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1320 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1324 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1326 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1329 #if !defined(_KERNEL)
1331 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1333 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1338 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1340 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1344 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1345 boolean_t *a, uint_t n)
1347 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1351 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1353 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1357 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1359 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1363 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1365 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1369 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1371 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1375 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1377 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1381 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1383 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1387 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1389 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1393 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1395 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1399 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1401 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1405 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1406 char *const *a, uint_t n)
1408 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1412 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1414 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1418 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1420 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1424 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1426 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1429 /* reading name-value pairs */
1431 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1437 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1440 curr = NVPAIR2I_NVP(nvp);
1443 * Ensure that nvp is a valid nvpair on this nvlist.
1444 * NB: nvp_curr is used only as a hint so that we don't always
1445 * have to walk the list to determine if nvp is still on the list.
1448 curr = priv->nvp_list;
1449 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1450 curr = curr->nvi_next;
1454 priv->nvp_curr = curr;
1456 return (curr != NULL ? &curr->nvi_nvp : NULL);
1460 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1466 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1469 curr = NVPAIR2I_NVP(nvp);
1472 curr = priv->nvp_last;
1473 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1474 curr = curr->nvi_prev;
1478 priv->nvp_curr = curr;
1480 return (curr != NULL ? &curr->nvi_nvp : NULL);
1484 nvlist_empty(nvlist_t *nvl)
1489 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1492 return (priv->nvp_list == NULL);
1496 nvpair_name(nvpair_t *nvp)
1498 return (NVP_NAME(nvp));
1502 nvpair_type(nvpair_t *nvp)
1504 return (NVP_TYPE(nvp));
1508 nvpair_type_is_array(nvpair_t *nvp)
1510 data_type_t type = NVP_TYPE(nvp);
1512 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1513 (type == DATA_TYPE_INT8_ARRAY) ||
1514 (type == DATA_TYPE_UINT8_ARRAY) ||
1515 (type == DATA_TYPE_INT16_ARRAY) ||
1516 (type == DATA_TYPE_UINT16_ARRAY) ||
1517 (type == DATA_TYPE_INT32_ARRAY) ||
1518 (type == DATA_TYPE_UINT32_ARRAY) ||
1519 (type == DATA_TYPE_INT64_ARRAY) ||
1520 (type == DATA_TYPE_UINT64_ARRAY) ||
1521 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1522 (type == DATA_TYPE_STRING_ARRAY) ||
1523 (type == DATA_TYPE_NVLIST_ARRAY))
1530 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1534 if (nvp == NULL || nvpair_type(nvp) != type)
1538 * For non-array types, we copy the data.
1539 * For array types (including string), we set a pointer.
1542 case DATA_TYPE_BOOLEAN:
1547 case DATA_TYPE_BOOLEAN_VALUE:
1548 case DATA_TYPE_BYTE:
1549 case DATA_TYPE_INT8:
1550 case DATA_TYPE_UINT8:
1551 case DATA_TYPE_INT16:
1552 case DATA_TYPE_UINT16:
1553 case DATA_TYPE_INT32:
1554 case DATA_TYPE_UINT32:
1555 case DATA_TYPE_INT64:
1556 case DATA_TYPE_UINT64:
1557 case DATA_TYPE_HRTIME:
1558 #if !defined(_KERNEL)
1559 case DATA_TYPE_DOUBLE:
1563 if ((value_sz = i_get_value_size(type, NULL, 1)) < 0)
1565 bcopy(NVP_VALUE(nvp), data, (size_t)value_sz);
1570 case DATA_TYPE_NVLIST:
1571 case DATA_TYPE_STRING:
1574 *(void **)data = (void *)NVP_VALUE(nvp);
1579 case DATA_TYPE_BOOLEAN_ARRAY:
1580 case DATA_TYPE_BYTE_ARRAY:
1581 case DATA_TYPE_INT8_ARRAY:
1582 case DATA_TYPE_UINT8_ARRAY:
1583 case DATA_TYPE_INT16_ARRAY:
1584 case DATA_TYPE_UINT16_ARRAY:
1585 case DATA_TYPE_INT32_ARRAY:
1586 case DATA_TYPE_UINT32_ARRAY:
1587 case DATA_TYPE_INT64_ARRAY:
1588 case DATA_TYPE_UINT64_ARRAY:
1589 case DATA_TYPE_STRING_ARRAY:
1590 case DATA_TYPE_NVLIST_ARRAY:
1591 if (nelem == NULL || data == NULL)
1593 if ((*nelem = NVP_NELEM(nvp)) != 0)
1594 *(void **)data = (void *)NVP_VALUE(nvp);
1596 *(void **)data = NULL;
1607 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1608 uint_t *nelem, void *data)
1610 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1613 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1616 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
1620 return (nvpair_value_common(nvp, type, nelem, data));
1624 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1626 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1630 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1632 return (nvlist_lookup_common(nvl, name,
1633 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1637 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1639 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1643 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1645 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1649 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1651 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1655 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1657 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1661 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1663 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1667 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1669 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1673 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1675 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1679 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1681 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1685 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1687 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1690 #if !defined(_KERNEL)
1692 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1694 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1699 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1701 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1705 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1707 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1711 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1712 boolean_t **a, uint_t *n)
1714 return (nvlist_lookup_common(nvl, name,
1715 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1719 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1720 uchar_t **a, uint_t *n)
1722 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1726 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1728 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1732 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1733 uint8_t **a, uint_t *n)
1735 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1739 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1740 int16_t **a, uint_t *n)
1742 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1746 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1747 uint16_t **a, uint_t *n)
1749 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1753 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1754 int32_t **a, uint_t *n)
1756 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1760 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1761 uint32_t **a, uint_t *n)
1763 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1767 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1768 int64_t **a, uint_t *n)
1770 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1774 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1775 uint64_t **a, uint_t *n)
1777 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1781 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1782 char ***a, uint_t *n)
1784 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1788 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1789 nvlist_t ***a, uint_t *n)
1791 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1795 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1797 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1801 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1805 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1809 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1814 switch (type = va_arg(ap, data_type_t)) {
1815 case DATA_TYPE_BOOLEAN:
1816 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1819 case DATA_TYPE_BOOLEAN_VALUE:
1820 case DATA_TYPE_BYTE:
1821 case DATA_TYPE_INT8:
1822 case DATA_TYPE_UINT8:
1823 case DATA_TYPE_INT16:
1824 case DATA_TYPE_UINT16:
1825 case DATA_TYPE_INT32:
1826 case DATA_TYPE_UINT32:
1827 case DATA_TYPE_INT64:
1828 case DATA_TYPE_UINT64:
1829 case DATA_TYPE_HRTIME:
1830 case DATA_TYPE_STRING:
1831 case DATA_TYPE_NVLIST:
1832 #if !defined(_KERNEL)
1833 case DATA_TYPE_DOUBLE:
1835 val = va_arg(ap, void *);
1836 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1839 case DATA_TYPE_BYTE_ARRAY:
1840 case DATA_TYPE_BOOLEAN_ARRAY:
1841 case DATA_TYPE_INT8_ARRAY:
1842 case DATA_TYPE_UINT8_ARRAY:
1843 case DATA_TYPE_INT16_ARRAY:
1844 case DATA_TYPE_UINT16_ARRAY:
1845 case DATA_TYPE_INT32_ARRAY:
1846 case DATA_TYPE_UINT32_ARRAY:
1847 case DATA_TYPE_INT64_ARRAY:
1848 case DATA_TYPE_UINT64_ARRAY:
1849 case DATA_TYPE_STRING_ARRAY:
1850 case DATA_TYPE_NVLIST_ARRAY:
1851 val = va_arg(ap, void *);
1852 nelem = va_arg(ap, uint_t *);
1853 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1860 if (ret == ENOENT && noentok)
1869 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1870 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1871 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1872 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1873 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1874 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1875 * code also supports "a.d[3]e[1]" syntax).
1877 * If 'ip' is non-NULL and the last name component is an array, return the
1878 * value of the "...[index]" array index in *ip. For an array reference that
1879 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1880 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1881 * inside the 'name' string where the syntax error was detected.
1884 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1885 nvpair_t **ret, int *ip, char **ep)
1896 *ip = -1; /* not indexed */
1900 if ((nvl == NULL) || (name == NULL))
1905 /* step through components of name */
1906 for (np = name; np && *np; np = sepp) {
1907 /* ensure unique names */
1908 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1911 /* skip white space */
1912 skip_whitespace(np);
1916 /* set 'sepp' to end of current component 'np' */
1918 sepp = strchr(np, sep);
1922 /* find start of next "[ index ]..." */
1923 idxp = strchr(np, '[');
1925 /* if sepp comes first, set idxp to NULL */
1926 if (sepp && idxp && (sepp < idxp))
1930 * At this point 'idxp' is set if there is an index
1931 * expected for the current component.
1934 /* set 'n' to length of current 'np' name component */
1937 /* keep sepp up to date for *ep use as we advance */
1938 skip_whitespace(idxp);
1941 /* determine the index value */
1942 #if defined(_KERNEL)
1943 if (ddi_strtol(idxp, &idxep, 0, &idx))
1946 idx = strtol(idxp, &idxep, 0);
1951 /* keep sepp up to date for *ep use as we advance */
1954 /* skip white space index value and check for ']' */
1955 skip_whitespace(sepp);
1959 /* for embedded arrays, support C syntax: "a[1].b" */
1960 skip_whitespace(sepp);
1961 if (sep && (*sepp == sep))
1969 /* trim trailing whitespace by reducing length of 'np' */
1972 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1976 /* skip whitespace, and set sepp to NULL if complete */
1978 skip_whitespace(sepp);
1985 * o 'n' is the length of current 'np' component.
1986 * o 'idxp' is set if there was an index, and value 'idx'.
1987 * o 'sepp' is set to the beginning of the next component,
1988 * and set to NULL if we have no more components.
1990 * Search for nvpair with matching component name.
1992 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1993 nvp = nvlist_next_nvpair(nvl, nvp)) {
1995 /* continue if no match on name */
1996 if (strncmp(np, nvpair_name(nvp), n) ||
1997 (strlen(nvpair_name(nvp)) != n))
2000 /* if indexed, verify type is array oriented */
2001 if (idxp && !nvpair_type_is_array(nvp))
2005 * Full match found, return nvp and idx if this
2006 * was the last component.
2012 *ip = (int)idx; /* return index */
2013 return (0); /* found */
2017 * More components: current match must be
2018 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2019 * to support going deeper.
2021 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
2022 nvl = EMBEDDED_NVL(nvp);
2024 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
2025 (void) nvpair_value_nvlist_array(nvp,
2026 &nva, (uint_t *)&n);
2027 if ((n < 0) || (idx >= n))
2033 /* type does not support more levels */
2037 goto fail; /* 'name' not found */
2039 /* search for match of next component in embedded 'nvl' list */
2042 fail: if (ep && sepp)
2048 * Return pointer to nvpair with specified 'name'.
2051 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
2053 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
2057 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2058 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2061 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
2062 const char *name, nvpair_t **ret, int *ip, char **ep)
2064 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
2068 nvlist_exists(nvlist_t *nvl, const char *name)
2074 if (name == NULL || nvl == NULL ||
2075 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2078 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2079 nvp = &curr->nvi_nvp;
2081 if (strcmp(name, NVP_NAME(nvp)) == 0)
2089 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
2091 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
2095 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
2097 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
2101 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
2103 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
2107 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
2109 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
2113 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
2115 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
2119 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
2121 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
2125 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
2127 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
2131 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
2133 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
2137 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
2139 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
2143 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
2145 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
2148 #if !defined(_KERNEL)
2150 nvpair_value_double(nvpair_t *nvp, double *val)
2152 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
2157 nvpair_value_string(nvpair_t *nvp, char **val)
2159 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
2163 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
2165 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
2169 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
2171 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
2175 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
2177 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
2181 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
2183 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
2187 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
2189 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
2193 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
2195 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
2199 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
2201 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
2205 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
2207 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
2211 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
2213 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
2217 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
2219 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
2223 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
2225 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
2229 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
2231 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
2235 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
2237 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
2241 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
2243 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
2247 * Add specified pair to the list.
2250 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2252 if (nvl == NULL || nvp == NULL)
2255 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
2256 NVP_NELEM(nvp), NVP_VALUE(nvp)));
2260 * Merge the supplied nvlists and put the result in dst.
2261 * The merged list will contain all names specified in both lists,
2262 * the values are taken from nvl in the case of duplicates.
2263 * Return 0 on success.
2267 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2269 if (nvl == NULL || dst == NULL)
2273 return (nvlist_copy_pairs(nvl, dst));
2279 * Encoding related routines
2281 #define NVS_OP_ENCODE 0
2282 #define NVS_OP_DECODE 1
2283 #define NVS_OP_GETSIZE 2
2285 typedef struct nvs_ops nvs_ops_t;
2289 const nvs_ops_t *nvs_ops;
2296 * nvs operations are:
2298 * encoding / decoding of an nvlist header (nvlist_t)
2299 * calculates the size used for header and end detection
2302 * responsible for the first part of encoding / decoding of an nvpair
2303 * calculates the decoded size of an nvpair
2306 * second part of encoding / decoding of an nvpair
2309 * calculates the encoding size of an nvpair
2312 * encodes the end detection mark (zeros).
2315 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2316 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2317 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2318 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2319 int (*nvs_nvl_fini)(nvstream_t *);
2323 char nvh_encoding; /* nvs encoding method */
2324 char nvh_endian; /* nvs endian */
2325 char nvh_reserved1; /* reserved for future use */
2326 char nvh_reserved2; /* reserved for future use */
2330 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2332 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2336 * Walk nvpair in list and encode each nvpair
2338 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2339 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2342 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2346 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2353 * Get decoded size of next pair in stream, alloc
2354 * memory for nvpair_t, then decode the nvpair
2356 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2357 if (nvsize == 0) /* end of list */
2360 /* make sure len makes sense */
2361 if (nvsize < NVP_SIZE_CALC(1, 0))
2364 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2367 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2368 nvp_buf_free(nvl, nvp);
2372 if (i_validate_nvpair(nvp) != 0) {
2374 nvp_buf_free(nvl, nvp);
2378 err = nvt_add_nvpair(nvl, nvp);
2381 nvp_buf_free(nvl, nvp);
2384 nvp_buf_link(nvl, nvp);
2390 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2392 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2394 uint64_t nvsize = *buflen;
2398 * Get encoded size of nvpairs in nvlist
2400 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2401 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2404 if ((nvsize += size) > INT32_MAX)
2413 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2417 if (nvl->nvl_priv == 0)
2421 * Perform the operation, starting with header, then each nvpair
2423 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2426 switch (nvs->nvs_op) {
2428 err = nvs_encode_pairs(nvs, nvl);
2432 err = nvs_decode_pairs(nvs, nvl);
2435 case NVS_OP_GETSIZE:
2436 err = nvs_getsize_pairs(nvs, nvl, buflen);
2447 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2449 switch (nvs->nvs_op) {
2450 case NVS_OP_ENCODE: {
2453 if (nvs->nvs_recursion >= nvpair_max_recursion)
2455 nvs->nvs_recursion++;
2456 err = nvs_operation(nvs, embedded, NULL);
2457 nvs->nvs_recursion--;
2460 case NVS_OP_DECODE: {
2464 if (embedded->nvl_version != NV_VERSION)
2467 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2470 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2472 if (nvs->nvs_recursion >= nvpair_max_recursion) {
2473 nvlist_free(embedded);
2476 nvs->nvs_recursion++;
2477 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2478 nvlist_free(embedded);
2479 nvs->nvs_recursion--;
2490 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2492 size_t nelem = NVP_NELEM(nvp);
2493 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2496 switch (nvs->nvs_op) {
2498 for (i = 0; i < nelem; i++)
2499 if (nvs_embedded(nvs, nvlp[i]) != 0)
2503 case NVS_OP_DECODE: {
2504 size_t len = nelem * sizeof (uint64_t);
2505 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2507 bzero(nvlp, len); /* don't trust packed data */
2508 for (i = 0; i < nelem; i++) {
2509 if (nvs_embedded(nvs, embedded) != 0) {
2514 nvlp[i] = embedded++;
2518 case NVS_OP_GETSIZE: {
2519 uint64_t nvsize = 0;
2521 for (i = 0; i < nelem; i++) {
2524 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2527 if ((nvsize += nvp_sz) > INT32_MAX)
2541 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2542 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2545 * Common routine for nvlist operations:
2546 * encode, decode, getsize (encoded size).
2549 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2555 #ifdef _LITTLE_ENDIAN
2556 int host_endian = 1;
2558 int host_endian = 0;
2559 #endif /* _LITTLE_ENDIAN */
2560 nvs_header_t *nvh = (void *)buf;
2562 if (buflen == NULL || nvl == NULL ||
2563 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2566 nvs.nvs_op = nvs_op;
2567 nvs.nvs_recursion = 0;
2570 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2571 * a buffer is allocated. The first 4 bytes in the buffer are
2572 * used for encoding method and host endian.
2576 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2579 nvh->nvh_encoding = encoding;
2580 nvh->nvh_endian = nvl_endian = host_endian;
2581 nvh->nvh_reserved1 = 0;
2582 nvh->nvh_reserved2 = 0;
2586 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2589 /* get method of encoding from first byte */
2590 encoding = nvh->nvh_encoding;
2591 nvl_endian = nvh->nvh_endian;
2594 case NVS_OP_GETSIZE:
2595 nvl_endian = host_endian;
2598 * add the size for encoding
2600 *buflen = sizeof (nvs_header_t);
2608 * Create an nvstream with proper encoding method
2611 case NV_ENCODE_NATIVE:
2613 * check endianness, in case we are unpacking
2616 if (nvl_endian != host_endian)
2618 err = nvs_native(&nvs, nvl, buf, buflen);
2621 err = nvs_xdr(&nvs, nvl, buf, buflen);
2632 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2634 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2638 * Pack nvlist into contiguous memory
2641 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2644 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2645 nvlist_nv_alloc(kmflag)));
2649 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2657 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2661 return (nvlist_common(nvl, *bufp, buflen, encoding,
2665 * Here is a difficult situation:
2666 * 1. The nvlist has fixed allocator properties.
2667 * All other nvlist routines (like nvlist_add_*, ...) use
2669 * 2. When using nvlist_pack() the user can specify their own
2670 * allocator properties (e.g. by using KM_NOSLEEP).
2672 * We use the user specified properties (2). A clearer solution
2673 * will be to remove the kmflag from nvlist_pack(), but we will
2674 * not change the interface.
2676 nv_priv_init(&nvpriv, nva, 0);
2678 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2681 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2684 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2685 NVS_OP_ENCODE)) != 0) {
2686 nv_mem_free(&nvpriv, buf, alloc_size);
2688 *buflen = alloc_size;
2696 * Unpack buf into an nvlist_t
2699 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2701 return (nvlist_xunpack(buf, buflen, nvlp, nvlist_nv_alloc(kmflag)));
2705 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2713 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2716 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2725 * Native encoding functions
2729 * This structure is used when decoding a packed nvpair in
2730 * the native format. n_base points to a buffer containing the
2731 * packed nvpair. n_end is a pointer to the end of the buffer.
2732 * (n_end actually points to the first byte past the end of the
2733 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2734 * It points to the current data that we are decoding.
2735 * The amount of data left in the buffer is equal to n_end - n_curr.
2736 * n_flag is used to recognize a packed embedded list.
2745 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2748 switch (nvs->nvs_op) {
2751 nvs->nvs_private = native;
2752 native->n_curr = native->n_base = buf;
2753 native->n_end = buf + buflen;
2757 case NVS_OP_GETSIZE:
2758 nvs->nvs_private = native;
2759 native->n_curr = native->n_base = native->n_end = NULL;
2769 nvs_native_destroy(nvstream_t *nvs)
2774 native_cp(nvstream_t *nvs, void *buf, size_t size)
2776 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2778 if (native->n_curr + size > native->n_end)
2782 * The bcopy() below eliminates alignment requirement
2783 * on the buffer (stream) and is preferred over direct access.
2785 switch (nvs->nvs_op) {
2787 bcopy(buf, native->n_curr, size);
2790 bcopy(native->n_curr, buf, size);
2796 native->n_curr += size;
2801 * operate on nvlist_t header
2804 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2806 nvs_native_t *native = nvs->nvs_private;
2808 switch (nvs->nvs_op) {
2812 return (0); /* packed embedded list */
2816 /* copy version and nvflag of the nvlist_t */
2817 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2818 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2823 case NVS_OP_GETSIZE:
2825 * if calculate for packed embedded list
2826 * 4 for end of the embedded list
2828 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2829 * and 4 for end of the entire list
2831 if (native->n_flag) {
2835 *size += 2 * sizeof (int32_t) + 4;
2846 nvs_native_nvl_fini(nvstream_t *nvs)
2848 if (nvs->nvs_op == NVS_OP_ENCODE) {
2849 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2851 * Add 4 zero bytes at end of nvlist. They are used
2852 * for end detection by the decode routine.
2854 if (native->n_curr + sizeof (int) > native->n_end)
2857 bzero(native->n_curr, sizeof (int));
2858 native->n_curr += sizeof (int);
2865 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2867 if (nvs->nvs_op == NVS_OP_ENCODE) {
2868 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2869 nvlist_t *packed = (void *)
2870 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2872 * Null out the pointer that is meaningless in the packed
2873 * structure. The address may not be aligned, so we have
2876 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2880 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2884 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2886 if (nvs->nvs_op == NVS_OP_ENCODE) {
2887 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2888 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2889 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2890 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2893 * Null out pointers that are meaningless in the packed
2894 * structure. The addresses may not be aligned, so we have
2899 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2901 * Null out the pointer that is meaningless in the
2902 * packed structure. The address may not be aligned,
2903 * so we have to use bzero.
2905 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2909 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2913 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2915 switch (nvs->nvs_op) {
2916 case NVS_OP_ENCODE: {
2917 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2918 uint64_t *strp = (void *)
2919 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2921 * Null out pointers that are meaningless in the packed
2922 * structure. The addresses may not be aligned, so we have
2925 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2928 case NVS_OP_DECODE: {
2929 char **strp = (void *)NVP_VALUE(nvp);
2930 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2933 for (i = 0; i < NVP_NELEM(nvp); i++) {
2935 buf += strlen(buf) + 1;
2943 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2950 * We do the initial bcopy of the data before we look at
2951 * the nvpair type, because when we're decoding, we won't
2952 * have the correct values for the pair until we do the bcopy.
2954 switch (nvs->nvs_op) {
2957 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2964 /* verify nvp_name_sz, check the name string length */
2965 if (i_validate_nvpair_name(nvp) != 0)
2968 type = NVP_TYPE(nvp);
2971 * Verify type and nelem and get the value size.
2972 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2973 * is the size of the string(s) excluded.
2975 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2978 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2982 case DATA_TYPE_NVLIST:
2983 ret = nvpair_native_embedded(nvs, nvp);
2985 case DATA_TYPE_NVLIST_ARRAY:
2986 ret = nvpair_native_embedded_array(nvs, nvp);
2988 case DATA_TYPE_STRING_ARRAY:
2989 nvpair_native_string_array(nvs, nvp);
2999 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3001 uint64_t nvp_sz = nvp->nvp_size;
3003 switch (NVP_TYPE(nvp)) {
3004 case DATA_TYPE_NVLIST: {
3007 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
3013 case DATA_TYPE_NVLIST_ARRAY: {
3016 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
3026 if (nvp_sz > INT32_MAX)
3035 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3037 switch (nvs->nvs_op) {
3039 return (nvs_native_nvp_op(nvs, nvp));
3041 case NVS_OP_DECODE: {
3042 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
3045 /* try to read the size value from the stream */
3046 if (native->n_curr + sizeof (int32_t) > native->n_end)
3048 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
3050 /* sanity check the size value */
3051 if (decode_len < 0 ||
3052 decode_len > native->n_end - native->n_curr)
3058 * If at the end of the stream then move the cursor
3059 * forward, otherwise nvpair_native_op() will read
3060 * the entire nvpair at the same cursor position.
3063 native->n_curr += sizeof (int32_t);
3074 static const nvs_ops_t nvs_native_ops = {
3075 .nvs_nvlist = nvs_native_nvlist,
3076 .nvs_nvpair = nvs_native_nvpair,
3077 .nvs_nvp_op = nvs_native_nvp_op,
3078 .nvs_nvp_size = nvs_native_nvp_size,
3079 .nvs_nvl_fini = nvs_native_nvl_fini
3083 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3085 nvs_native_t native;
3088 nvs->nvs_ops = &nvs_native_ops;
3090 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
3091 *buflen - sizeof (nvs_header_t))) != 0)
3094 err = nvs_operation(nvs, nvl, buflen);
3096 nvs_native_destroy(nvs);
3102 * XDR encoding functions
3104 * An xdr packed nvlist is encoded as:
3106 * - encoding methode and host endian (4 bytes)
3107 * - nvl_version (4 bytes)
3108 * - nvl_nvflag (4 bytes)
3110 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3111 * - encoded size of the nvpair (4 bytes)
3112 * - decoded size of the nvpair (4 bytes)
3113 * - name string, (4 + sizeof(NV_ALIGN4(string))
3114 * a string is coded as size (4 bytes) and data
3115 * - data type (4 bytes)
3116 * - number of elements in the nvpair (4 bytes)
3119 * - 2 zero's for end of the entire list (8 bytes)
3122 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
3124 /* xdr data must be 4 byte aligned */
3125 if ((ulong_t)buf % 4 != 0)
3128 switch (nvs->nvs_op) {
3130 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
3131 nvs->nvs_private = xdr;
3134 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
3135 nvs->nvs_private = xdr;
3137 case NVS_OP_GETSIZE:
3138 nvs->nvs_private = NULL;
3146 nvs_xdr_destroy(nvstream_t *nvs)
3148 switch (nvs->nvs_op) {
3151 xdr_destroy((XDR *)nvs->nvs_private);
3159 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
3161 switch (nvs->nvs_op) {
3163 case NVS_OP_DECODE: {
3164 XDR *xdr = nvs->nvs_private;
3166 if (!xdr_int(xdr, &nvl->nvl_version) ||
3167 !xdr_u_int(xdr, &nvl->nvl_nvflag))
3171 case NVS_OP_GETSIZE: {
3173 * 2 * 4 for nvl_version + nvl_nvflag
3174 * and 8 for end of the entire list
3186 nvs_xdr_nvl_fini(nvstream_t *nvs)
3188 if (nvs->nvs_op == NVS_OP_ENCODE) {
3189 XDR *xdr = nvs->nvs_private;
3192 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
3200 * The format of xdr encoded nvpair is:
3201 * encode_size, decode_size, name string, data type, nelem, data
3204 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
3208 char *buf_end = (char *)nvp + nvp->nvp_size;
3210 uint_t nelem, buflen;
3212 XDR *xdr = nvs->nvs_private;
3214 ASSERT(xdr != NULL && nvp != NULL);
3217 if ((buf = NVP_NAME(nvp)) >= buf_end)
3219 buflen = buf_end - buf;
3221 if (!xdr_string(xdr, &buf, buflen - 1))
3223 nvp->nvp_name_sz = strlen(buf) + 1;
3225 /* type and nelem */
3226 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
3227 !xdr_int(xdr, &nvp->nvp_value_elem))
3230 type = NVP_TYPE(nvp);
3231 nelem = nvp->nvp_value_elem;
3234 * Verify type and nelem and get the value size.
3235 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3236 * is the size of the string(s) excluded.
3238 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
3241 /* if there is no data to extract then return */
3246 if ((buf = NVP_VALUE(nvp)) >= buf_end)
3248 buflen = buf_end - buf;
3250 if (buflen < value_sz)
3254 case DATA_TYPE_NVLIST:
3255 if (nvs_embedded(nvs, (void *)buf) == 0)
3259 case DATA_TYPE_NVLIST_ARRAY:
3260 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
3264 case DATA_TYPE_BOOLEAN:
3268 case DATA_TYPE_BYTE:
3269 case DATA_TYPE_INT8:
3270 case DATA_TYPE_UINT8:
3271 ret = xdr_char(xdr, buf);
3274 case DATA_TYPE_INT16:
3275 ret = xdr_short(xdr, (void *)buf);
3278 case DATA_TYPE_UINT16:
3279 ret = xdr_u_short(xdr, (void *)buf);
3282 case DATA_TYPE_BOOLEAN_VALUE:
3283 case DATA_TYPE_INT32:
3284 ret = xdr_int(xdr, (void *)buf);
3287 case DATA_TYPE_UINT32:
3288 ret = xdr_u_int(xdr, (void *)buf);
3291 case DATA_TYPE_INT64:
3292 ret = xdr_longlong_t(xdr, (void *)buf);
3295 case DATA_TYPE_UINT64:
3296 ret = xdr_u_longlong_t(xdr, (void *)buf);
3299 case DATA_TYPE_HRTIME:
3301 * NOTE: must expose the definition of hrtime_t here
3303 ret = xdr_longlong_t(xdr, (void *)buf);
3305 #if !defined(_KERNEL)
3306 case DATA_TYPE_DOUBLE:
3307 ret = xdr_double(xdr, (void *)buf);
3310 case DATA_TYPE_STRING:
3311 ret = xdr_string(xdr, &buf, buflen - 1);
3314 case DATA_TYPE_BYTE_ARRAY:
3315 ret = xdr_opaque(xdr, buf, nelem);
3318 case DATA_TYPE_INT8_ARRAY:
3319 case DATA_TYPE_UINT8_ARRAY:
3320 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3321 (xdrproc_t)xdr_char);
3324 case DATA_TYPE_INT16_ARRAY:
3325 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3326 sizeof (int16_t), (xdrproc_t)xdr_short);
3329 case DATA_TYPE_UINT16_ARRAY:
3330 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3331 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3334 case DATA_TYPE_BOOLEAN_ARRAY:
3335 case DATA_TYPE_INT32_ARRAY:
3336 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3337 sizeof (int32_t), (xdrproc_t)xdr_int);
3340 case DATA_TYPE_UINT32_ARRAY:
3341 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3342 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3345 case DATA_TYPE_INT64_ARRAY:
3346 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3347 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3350 case DATA_TYPE_UINT64_ARRAY:
3351 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3352 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3355 case DATA_TYPE_STRING_ARRAY: {
3356 size_t len = nelem * sizeof (uint64_t);
3357 char **strp = (void *)buf;
3360 if (nvs->nvs_op == NVS_OP_DECODE)
3361 bzero(buf, len); /* don't trust packed data */
3363 for (i = 0; i < nelem; i++) {
3370 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3373 if (nvs->nvs_op == NVS_OP_DECODE)
3375 len = strlen(buf) + 1;
3384 return (ret == TRUE ? 0 : EFAULT);
3388 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3390 data_type_t type = NVP_TYPE(nvp);
3392 * encode_size + decode_size + name string size + data type + nelem
3393 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3395 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3398 case DATA_TYPE_BOOLEAN:
3401 case DATA_TYPE_BOOLEAN_VALUE:
3402 case DATA_TYPE_BYTE:
3403 case DATA_TYPE_INT8:
3404 case DATA_TYPE_UINT8:
3405 case DATA_TYPE_INT16:
3406 case DATA_TYPE_UINT16:
3407 case DATA_TYPE_INT32:
3408 case DATA_TYPE_UINT32:
3409 nvp_sz += 4; /* 4 is the minimum xdr unit */
3412 case DATA_TYPE_INT64:
3413 case DATA_TYPE_UINT64:
3414 case DATA_TYPE_HRTIME:
3415 #if !defined(_KERNEL)
3416 case DATA_TYPE_DOUBLE:
3421 case DATA_TYPE_STRING:
3422 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3425 case DATA_TYPE_BYTE_ARRAY:
3426 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3429 case DATA_TYPE_BOOLEAN_ARRAY:
3430 case DATA_TYPE_INT8_ARRAY:
3431 case DATA_TYPE_UINT8_ARRAY:
3432 case DATA_TYPE_INT16_ARRAY:
3433 case DATA_TYPE_UINT16_ARRAY:
3434 case DATA_TYPE_INT32_ARRAY:
3435 case DATA_TYPE_UINT32_ARRAY:
3436 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3439 case DATA_TYPE_INT64_ARRAY:
3440 case DATA_TYPE_UINT64_ARRAY:
3441 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3444 case DATA_TYPE_STRING_ARRAY: {
3446 char **strs = (void *)NVP_VALUE(nvp);
3448 for (i = 0; i < NVP_NELEM(nvp); i++)
3449 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3454 case DATA_TYPE_NVLIST:
3455 case DATA_TYPE_NVLIST_ARRAY: {
3457 int old_nvs_op = nvs->nvs_op;
3460 nvs->nvs_op = NVS_OP_GETSIZE;
3461 if (type == DATA_TYPE_NVLIST)
3462 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3464 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3465 nvs->nvs_op = old_nvs_op;
3478 if (nvp_sz > INT32_MAX)
3488 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3489 * the largest nvpair that could be encoded in the buffer.
3491 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3492 * The size of a xdr packed nvpair without any data is 5 words.
3494 * Using the size of the data directly as an estimate would be ok
3495 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3496 * then the actual nvpair has space for an array of pointers to index
3497 * the strings. These pointers are not encoded into the packed xdr buffer.
3499 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3500 * of length 0, then each string is endcoded in xdr format as a single word.
3501 * Therefore when expanded to an nvpair there will be 2.25 word used for
3502 * each string. (a int64_t allocated for pointer usage, and a single char
3503 * for the null termination.)
3505 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3507 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3508 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3509 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3510 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3511 (NVS_XDR_DATA_LEN(x) * 2) + \
3512 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3515 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3517 XDR *xdr = nvs->nvs_private;
3518 int32_t encode_len, decode_len;
3520 switch (nvs->nvs_op) {
3521 case NVS_OP_ENCODE: {
3524 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3527 decode_len = nvp->nvp_size;
3528 encode_len = nvsize;
3529 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3532 return (nvs_xdr_nvp_op(nvs, nvp));
3534 case NVS_OP_DECODE: {
3535 struct xdr_bytesrec bytesrec;
3537 /* get the encode and decode size */
3538 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3542 /* are we at the end of the stream? */
3546 /* sanity check the size parameter */
3547 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3550 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3561 static const struct nvs_ops nvs_xdr_ops = {
3562 .nvs_nvlist = nvs_xdr_nvlist,
3563 .nvs_nvpair = nvs_xdr_nvpair,
3564 .nvs_nvp_op = nvs_xdr_nvp_op,
3565 .nvs_nvp_size = nvs_xdr_nvp_size,
3566 .nvs_nvl_fini = nvs_xdr_nvl_fini
3570 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3575 nvs->nvs_ops = &nvs_xdr_ops;
3577 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3578 *buflen - sizeof (nvs_header_t))) != 0)
3581 err = nvs_operation(nvs, nvl, buflen);
3583 nvs_xdr_destroy(nvs);
3588 #if defined(_KERNEL)
3600 module_init(nvpair_init);
3601 module_exit(nvpair_fini);
3603 MODULE_DESCRIPTION("Generic name/value pair implementation");
3604 MODULE_AUTHOR(ZFS_META_AUTHOR);
3605 MODULE_LICENSE(ZFS_META_LICENSE);
3606 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
3608 EXPORT_SYMBOL(nv_alloc_init);
3609 EXPORT_SYMBOL(nv_alloc_reset);
3610 EXPORT_SYMBOL(nv_alloc_fini);
3612 /* list management */
3613 EXPORT_SYMBOL(nvlist_alloc);
3614 EXPORT_SYMBOL(nvlist_free);
3615 EXPORT_SYMBOL(nvlist_size);
3616 EXPORT_SYMBOL(nvlist_pack);
3617 EXPORT_SYMBOL(nvlist_unpack);
3618 EXPORT_SYMBOL(nvlist_dup);
3619 EXPORT_SYMBOL(nvlist_merge);
3621 EXPORT_SYMBOL(nvlist_xalloc);
3622 EXPORT_SYMBOL(nvlist_xpack);
3623 EXPORT_SYMBOL(nvlist_xunpack);
3624 EXPORT_SYMBOL(nvlist_xdup);
3625 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3627 EXPORT_SYMBOL(nvlist_add_nvpair);
3628 EXPORT_SYMBOL(nvlist_add_boolean);
3629 EXPORT_SYMBOL(nvlist_add_boolean_value);
3630 EXPORT_SYMBOL(nvlist_add_byte);
3631 EXPORT_SYMBOL(nvlist_add_int8);
3632 EXPORT_SYMBOL(nvlist_add_uint8);
3633 EXPORT_SYMBOL(nvlist_add_int16);
3634 EXPORT_SYMBOL(nvlist_add_uint16);
3635 EXPORT_SYMBOL(nvlist_add_int32);
3636 EXPORT_SYMBOL(nvlist_add_uint32);
3637 EXPORT_SYMBOL(nvlist_add_int64);
3638 EXPORT_SYMBOL(nvlist_add_uint64);
3639 EXPORT_SYMBOL(nvlist_add_string);
3640 EXPORT_SYMBOL(nvlist_add_nvlist);
3641 EXPORT_SYMBOL(nvlist_add_boolean_array);
3642 EXPORT_SYMBOL(nvlist_add_byte_array);
3643 EXPORT_SYMBOL(nvlist_add_int8_array);
3644 EXPORT_SYMBOL(nvlist_add_uint8_array);
3645 EXPORT_SYMBOL(nvlist_add_int16_array);
3646 EXPORT_SYMBOL(nvlist_add_uint16_array);
3647 EXPORT_SYMBOL(nvlist_add_int32_array);
3648 EXPORT_SYMBOL(nvlist_add_uint32_array);
3649 EXPORT_SYMBOL(nvlist_add_int64_array);
3650 EXPORT_SYMBOL(nvlist_add_uint64_array);
3651 EXPORT_SYMBOL(nvlist_add_string_array);
3652 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3653 EXPORT_SYMBOL(nvlist_next_nvpair);
3654 EXPORT_SYMBOL(nvlist_prev_nvpair);
3655 EXPORT_SYMBOL(nvlist_empty);
3656 EXPORT_SYMBOL(nvlist_add_hrtime);
3658 EXPORT_SYMBOL(nvlist_remove);
3659 EXPORT_SYMBOL(nvlist_remove_nvpair);
3660 EXPORT_SYMBOL(nvlist_remove_all);
3662 EXPORT_SYMBOL(nvlist_lookup_boolean);
3663 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3664 EXPORT_SYMBOL(nvlist_lookup_byte);
3665 EXPORT_SYMBOL(nvlist_lookup_int8);
3666 EXPORT_SYMBOL(nvlist_lookup_uint8);
3667 EXPORT_SYMBOL(nvlist_lookup_int16);
3668 EXPORT_SYMBOL(nvlist_lookup_uint16);
3669 EXPORT_SYMBOL(nvlist_lookup_int32);
3670 EXPORT_SYMBOL(nvlist_lookup_uint32);
3671 EXPORT_SYMBOL(nvlist_lookup_int64);
3672 EXPORT_SYMBOL(nvlist_lookup_uint64);
3673 EXPORT_SYMBOL(nvlist_lookup_string);
3674 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3675 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3676 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3677 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3678 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3679 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3680 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3681 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3682 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3683 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3684 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3685 EXPORT_SYMBOL(nvlist_lookup_string_array);
3686 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3687 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3688 EXPORT_SYMBOL(nvlist_lookup_pairs);
3690 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3691 EXPORT_SYMBOL(nvlist_exists);
3693 /* processing nvpair */
3694 EXPORT_SYMBOL(nvpair_name);
3695 EXPORT_SYMBOL(nvpair_type);
3696 EXPORT_SYMBOL(nvpair_value_boolean_value);
3697 EXPORT_SYMBOL(nvpair_value_byte);
3698 EXPORT_SYMBOL(nvpair_value_int8);
3699 EXPORT_SYMBOL(nvpair_value_uint8);
3700 EXPORT_SYMBOL(nvpair_value_int16);
3701 EXPORT_SYMBOL(nvpair_value_uint16);
3702 EXPORT_SYMBOL(nvpair_value_int32);
3703 EXPORT_SYMBOL(nvpair_value_uint32);
3704 EXPORT_SYMBOL(nvpair_value_int64);
3705 EXPORT_SYMBOL(nvpair_value_uint64);
3706 EXPORT_SYMBOL(nvpair_value_string);
3707 EXPORT_SYMBOL(nvpair_value_nvlist);
3708 EXPORT_SYMBOL(nvpair_value_boolean_array);
3709 EXPORT_SYMBOL(nvpair_value_byte_array);
3710 EXPORT_SYMBOL(nvpair_value_int8_array);
3711 EXPORT_SYMBOL(nvpair_value_uint8_array);
3712 EXPORT_SYMBOL(nvpair_value_int16_array);
3713 EXPORT_SYMBOL(nvpair_value_uint16_array);
3714 EXPORT_SYMBOL(nvpair_value_int32_array);
3715 EXPORT_SYMBOL(nvpair_value_uint32_array);
3716 EXPORT_SYMBOL(nvpair_value_int64_array);
3717 EXPORT_SYMBOL(nvpair_value_uint64_array);
3718 EXPORT_SYMBOL(nvpair_value_string_array);
3719 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3720 EXPORT_SYMBOL(nvpair_value_hrtime);