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.
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/strings.h>
48 #include <sys/sunddi.h>
49 #include <sys/sysmacros.h>
56 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
59 * nvpair.c - Provides kernel & userland interfaces for manipulating
74 * +--------------+ last i_nvp in list
75 * | nvpriv_t | +--------------------->
77 * +--+- nvp_list | | +------------+
78 * | | nvp_last -+--+ + nv_alloc_t |
79 * | | nvp_curr | |------------|
80 * | | nvp_nva -+----> | nva_ops |
81 * | | nvp_stat | | nva_arg |
82 * | +--------------+ +------------+
86 * +---------------------+ +-------------------+
87 * | i_nvp_t | +-->| i_nvp_t | +-->
88 * |---------------------| | |-------------------| |
89 * | nvi_next -+--+ | nvi_next -+--+
90 * | nvi_prev (NULL) | <----+ nvi_prev |
91 * | . . . . . . . . . . | | . . . . . . . . . |
92 * | nvp (nvpair_t) | | nvp (nvpair_t) |
93 * | - nvp_size | | - nvp_size |
94 * | - nvp_name_sz | | - nvp_name_sz |
95 * | - nvp_value_elem | | - nvp_value_elem |
96 * | - nvp_type | | - nvp_type |
97 * | - data ... | | - data ... |
98 * +---------------------+ +-------------------+
102 * +---------------------+ +---------------------+
103 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
104 * |---------------------| | | |---------------------|
105 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
106 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
107 * | . . . . . . . . . | | . . . . . . . . . |
108 * | nvp (nvpair_t) | | nvp (nvpair_t) |
109 * | - nvp_size | | - nvp_size |
110 * | - nvp_name_sz | | - nvp_name_sz |
111 * | - nvp_value_elem | | - nvp_value_elem |
112 * | - DATA_TYPE_NVLIST | | - nvp_type |
113 * | - data (embedded) | | - data ... |
114 * | nvlist name | +---------------------+
115 * | +--------------+ |
117 * | |--------------| |
118 * | | nvl_version | |
120 * | | nvl_priv --+---+---->
123 * | +--------------+ |
124 * +---------------------+
127 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
128 * allow value to be aligned on 8 byte boundary
130 * name_len is the length of the name string including the null terminator
133 #define NVP_SIZE_CALC(name_len, data_len) \
134 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
136 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
137 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
138 uint_t nelem, const void *data);
140 #define NV_STAT_EMBEDDED 0x1
141 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
142 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
144 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
145 #define NVPAIR2I_NVP(nvp) \
146 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
149 int nvpair_max_recursion = 20;
151 int nvpair_max_recursion = 100;
154 uint64_t nvlist_hashtable_init_size = (1 << 4);
157 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
165 va_start(valist, nvo);
166 if (nva->nva_ops->nv_ao_init != NULL)
167 err = nva->nva_ops->nv_ao_init(nva, valist);
174 nv_alloc_reset(nv_alloc_t *nva)
176 if (nva->nva_ops->nv_ao_reset != NULL)
177 nva->nva_ops->nv_ao_reset(nva);
181 nv_alloc_fini(nv_alloc_t *nva)
183 if (nva->nva_ops->nv_ao_fini != NULL)
184 nva->nva_ops->nv_ao_fini(nva);
188 nvlist_lookup_nv_alloc(nvlist_t *nvl)
193 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
196 return (priv->nvp_nva);
200 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
202 nv_alloc_t *nva = nvp->nvp_nva;
205 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
212 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
214 nv_alloc_t *nva = nvp->nvp_nva;
216 nva->nva_ops->nv_ao_free(nva, buf, size);
220 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
222 bzero(priv, sizeof (nvpriv_t));
225 priv->nvp_stat = stat;
229 nv_priv_alloc(nv_alloc_t *nva)
234 * nv_mem_alloc() cannot called here because it needs the priv
237 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
240 nv_priv_init(priv, nva, 0);
246 * Embedded lists need their own nvpriv_t's. We create a new
247 * nvpriv_t using the parameters and allocator from the parent
251 nv_priv_alloc_embedded(nvpriv_t *priv)
255 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
258 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
264 nvt_tab_alloc(nvpriv_t *priv, uint64_t buckets)
266 ASSERT3P(priv->nvp_hashtable, ==, NULL);
267 ASSERT0(priv->nvp_nbuckets);
268 ASSERT0(priv->nvp_nentries);
270 i_nvp_t **tab = nv_mem_zalloc(priv, buckets * sizeof (i_nvp_t *));
274 priv->nvp_hashtable = tab;
275 priv->nvp_nbuckets = buckets;
280 nvt_tab_free(nvpriv_t *priv)
282 i_nvp_t **tab = priv->nvp_hashtable;
284 ASSERT0(priv->nvp_nbuckets);
285 ASSERT0(priv->nvp_nentries);
289 nv_mem_free(priv, tab, priv->nvp_nbuckets * sizeof (i_nvp_t *));
291 priv->nvp_hashtable = NULL;
292 priv->nvp_nbuckets = 0;
293 priv->nvp_nentries = 0;
297 nvt_hash(const char *p)
299 uint32_t g, hval = 0;
302 hval = (hval << 4) + *p++;
303 if ((g = (hval & 0xf0000000)) != 0)
311 nvt_nvpair_match(nvpair_t *nvp1, nvpair_t *nvp2, uint32_t nvflag)
313 boolean_t match = B_FALSE;
314 if (nvflag & NV_UNIQUE_NAME_TYPE) {
315 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0 &&
316 NVP_TYPE(nvp1) == NVP_TYPE(nvp2))
319 ASSERT(nvflag == 0 || nvflag & NV_UNIQUE_NAME);
320 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0)
327 nvt_lookup_name_type(nvlist_t *nvl, const char *name, data_type_t type)
329 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
330 ASSERT(priv != NULL);
332 i_nvp_t **tab = priv->nvp_hashtable;
335 ASSERT3P(priv->nvp_list, ==, NULL);
336 ASSERT0(priv->nvp_nbuckets);
337 ASSERT0(priv->nvp_nentries);
340 ASSERT(priv->nvp_nbuckets != 0);
343 uint64_t hash = nvt_hash(name);
344 uint64_t index = hash & (priv->nvp_nbuckets - 1);
346 ASSERT3U(index, <, priv->nvp_nbuckets);
347 i_nvp_t *entry = tab[index];
349 for (i_nvp_t *e = entry; e != NULL; e = e->nvi_hashtable_next) {
350 if (strcmp(NVP_NAME(&e->nvi_nvp), name) == 0 &&
351 (type == DATA_TYPE_DONTCARE ||
352 NVP_TYPE(&e->nvi_nvp) == type))
353 return (&e->nvi_nvp);
359 nvt_lookup_name(nvlist_t *nvl, const char *name)
361 return (nvt_lookup_name_type(nvl, name, DATA_TYPE_DONTCARE));
365 nvt_resize(nvpriv_t *priv, uint32_t new_size)
367 i_nvp_t **tab = priv->nvp_hashtable;
370 * Migrate all the entries from the current table
371 * to a newly-allocated table with the new size by
372 * re-adjusting the pointers of their entries.
374 uint32_t size = priv->nvp_nbuckets;
375 uint32_t new_mask = new_size - 1;
376 ASSERT(ISP2(new_size));
378 i_nvp_t **new_tab = nv_mem_zalloc(priv, new_size * sizeof (i_nvp_t *));
382 uint32_t nentries = 0;
383 for (uint32_t i = 0; i < size; i++) {
384 i_nvp_t *next, *e = tab[i];
387 next = e->nvi_hashtable_next;
389 uint32_t hash = nvt_hash(NVP_NAME(&e->nvi_nvp));
390 uint32_t index = hash & new_mask;
392 e->nvi_hashtable_next = new_tab[index];
400 ASSERT3U(nentries, ==, priv->nvp_nentries);
404 priv->nvp_hashtable = new_tab;
405 priv->nvp_nbuckets = new_size;
406 priv->nvp_nentries = nentries;
412 nvt_needs_togrow(nvpriv_t *priv)
415 * Grow only when we have more elements than buckets
416 * and the # of buckets doesn't overflow.
418 return (priv->nvp_nentries > priv->nvp_nbuckets &&
419 (UINT32_MAX >> 1) >= priv->nvp_nbuckets);
423 * Allocate a new table that's twice the size of the old one,
424 * and migrate all the entries from the old one to the new
425 * one by re-adjusting their pointers.
428 nvt_grow(nvpriv_t *priv)
430 uint32_t current_size = priv->nvp_nbuckets;
431 /* ensure we won't overflow */
432 ASSERT3U(UINT32_MAX >> 1, >=, current_size);
433 return (nvt_resize(priv, current_size << 1));
437 nvt_needs_toshrink(nvpriv_t *priv)
440 * Shrink only when the # of elements is less than or
441 * equal to 1/4 the # of buckets. Never shrink less than
442 * nvlist_hashtable_init_size.
444 ASSERT3U(priv->nvp_nbuckets, >=, nvlist_hashtable_init_size);
445 if (priv->nvp_nbuckets == nvlist_hashtable_init_size)
447 return (priv->nvp_nentries <= (priv->nvp_nbuckets >> 2));
451 * Allocate a new table that's half the size of the old one,
452 * and migrate all the entries from the old one to the new
453 * one by re-adjusting their pointers.
456 nvt_shrink(nvpriv_t *priv)
458 uint32_t current_size = priv->nvp_nbuckets;
459 /* ensure we won't overflow */
460 ASSERT3U(current_size, >=, nvlist_hashtable_init_size);
461 return (nvt_resize(priv, current_size >> 1));
465 nvt_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
467 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
469 if (nvt_needs_toshrink(priv)) {
470 int err = nvt_shrink(priv);
474 i_nvp_t **tab = priv->nvp_hashtable;
476 char *name = NVP_NAME(nvp);
477 uint64_t hash = nvt_hash(name);
478 uint64_t index = hash & (priv->nvp_nbuckets - 1);
480 ASSERT3U(index, <, priv->nvp_nbuckets);
481 i_nvp_t *bucket = tab[index];
483 for (i_nvp_t *prev = NULL, *e = bucket;
484 e != NULL; prev = e, e = e->nvi_hashtable_next) {
485 if (nvt_nvpair_match(&e->nvi_nvp, nvp, nvl->nvl_nvflag)) {
487 prev->nvi_hashtable_next =
488 e->nvi_hashtable_next;
490 ASSERT3P(e, ==, bucket);
491 tab[index] = e->nvi_hashtable_next;
493 e->nvi_hashtable_next = NULL;
494 priv->nvp_nentries--;
503 nvt_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
505 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
507 /* initialize nvpair table now if it doesn't exist. */
508 if (priv->nvp_hashtable == NULL) {
509 int err = nvt_tab_alloc(priv, nvlist_hashtable_init_size);
515 * if we don't allow duplicate entries, make sure to
516 * unlink any existing entries from the table.
518 if (nvl->nvl_nvflag != 0) {
519 int err = nvt_remove_nvpair(nvl, nvp);
524 if (nvt_needs_togrow(priv)) {
525 int err = nvt_grow(priv);
529 i_nvp_t **tab = priv->nvp_hashtable;
531 char *name = NVP_NAME(nvp);
532 uint64_t hash = nvt_hash(name);
533 uint64_t index = hash & (priv->nvp_nbuckets - 1);
535 ASSERT3U(index, <, priv->nvp_nbuckets);
536 i_nvp_t *bucket = tab[index];
538 /* insert link at the beginning of the bucket */
539 i_nvp_t *new_entry = NVPAIR2I_NVP(nvp);
540 ASSERT3P(new_entry->nvi_hashtable_next, ==, NULL);
541 new_entry->nvi_hashtable_next = bucket;
542 tab[index] = new_entry;
544 priv->nvp_nentries++;
549 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
551 nvl->nvl_version = NV_VERSION;
552 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
553 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
559 nvlist_nvflag(nvlist_t *nvl)
561 return (nvl->nvl_nvflag);
565 nvlist_nv_alloc(int kmflag)
570 return (nv_alloc_sleep);
572 return (nv_alloc_nosleep);
574 return (nv_alloc_pushpage);
577 return (nv_alloc_nosleep);
582 * nvlist_alloc - Allocate nvlist.
585 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
587 return (nvlist_xalloc(nvlp, nvflag, nvlist_nv_alloc(kmflag)));
591 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
595 if (nvlp == NULL || nva == NULL)
598 if ((priv = nv_priv_alloc(nva)) == NULL)
601 if ((*nvlp = nv_mem_zalloc(priv,
602 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
603 nv_mem_free(priv, priv, sizeof (nvpriv_t));
607 nvlist_init(*nvlp, nvflag, priv);
613 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
616 nvp_buf_alloc(nvlist_t *nvl, size_t len)
618 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
624 * Allocate the buffer
626 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
628 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
638 * nvp_buf_free - de-Allocate an i_nvp_t.
641 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
643 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
644 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
646 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
650 * nvp_buf_link - link a new nv pair into the nvlist.
653 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
655 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
656 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
658 /* Put element at end of nvlist */
659 if (priv->nvp_list == NULL) {
660 priv->nvp_list = priv->nvp_last = curr;
662 curr->nvi_prev = priv->nvp_last;
663 priv->nvp_last->nvi_next = curr;
664 priv->nvp_last = curr;
669 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
672 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
674 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
675 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
678 * protect nvlist_next_nvpair() against walking on freed memory.
680 if (priv->nvp_curr == curr)
681 priv->nvp_curr = curr->nvi_next;
683 if (curr == priv->nvp_list)
684 priv->nvp_list = curr->nvi_next;
686 curr->nvi_prev->nvi_next = curr->nvi_next;
688 if (curr == priv->nvp_last)
689 priv->nvp_last = curr->nvi_prev;
691 curr->nvi_next->nvi_prev = curr->nvi_prev;
695 * take a nvpair type and number of elements and make sure the are valid
698 i_validate_type_nelem(data_type_t type, uint_t nelem)
701 case DATA_TYPE_BOOLEAN:
705 case DATA_TYPE_BOOLEAN_VALUE:
708 case DATA_TYPE_UINT8:
709 case DATA_TYPE_INT16:
710 case DATA_TYPE_UINT16:
711 case DATA_TYPE_INT32:
712 case DATA_TYPE_UINT32:
713 case DATA_TYPE_INT64:
714 case DATA_TYPE_UINT64:
715 case DATA_TYPE_STRING:
716 case DATA_TYPE_HRTIME:
717 case DATA_TYPE_NVLIST:
718 #if !defined(_KERNEL)
719 case DATA_TYPE_DOUBLE:
724 case DATA_TYPE_BOOLEAN_ARRAY:
725 case DATA_TYPE_BYTE_ARRAY:
726 case DATA_TYPE_INT8_ARRAY:
727 case DATA_TYPE_UINT8_ARRAY:
728 case DATA_TYPE_INT16_ARRAY:
729 case DATA_TYPE_UINT16_ARRAY:
730 case DATA_TYPE_INT32_ARRAY:
731 case DATA_TYPE_UINT32_ARRAY:
732 case DATA_TYPE_INT64_ARRAY:
733 case DATA_TYPE_UINT64_ARRAY:
734 case DATA_TYPE_STRING_ARRAY:
735 case DATA_TYPE_NVLIST_ARRAY:
736 /* we allow arrays with 0 elements */
745 * Verify nvp_name_sz and check the name string length.
748 i_validate_nvpair_name(nvpair_t *nvp)
750 if ((nvp->nvp_name_sz <= 0) ||
751 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
754 /* verify the name string, make sure its terminated */
755 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
758 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
762 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
765 case DATA_TYPE_BOOLEAN_VALUE:
766 if (*(boolean_t *)data != B_TRUE &&
767 *(boolean_t *)data != B_FALSE)
770 case DATA_TYPE_BOOLEAN_ARRAY: {
773 for (i = 0; i < nelem; i++)
774 if (((boolean_t *)data)[i] != B_TRUE &&
775 ((boolean_t *)data)[i] != B_FALSE)
787 * This function takes a pointer to what should be a nvpair and it's size
788 * and then verifies that all the nvpair fields make sense and can be
789 * trusted. This function is used when decoding packed nvpairs.
792 i_validate_nvpair(nvpair_t *nvp)
794 data_type_t type = NVP_TYPE(nvp);
797 /* verify nvp_name_sz, check the name string length */
798 if (i_validate_nvpair_name(nvp) != 0)
801 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
805 * verify nvp_type, nvp_value_elem, and also possibly
806 * verify string values and get the value size.
808 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
809 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
810 if (size2 < 0 || size1 != NV_ALIGN(size2))
817 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
822 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
825 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
826 nvpair_t *nvp = &curr->nvi_nvp;
829 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
830 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
838 * Frees all memory allocated for an nvpair (like embedded lists) with
839 * the exception of the nvpair buffer itself.
842 nvpair_free(nvpair_t *nvp)
844 switch (NVP_TYPE(nvp)) {
845 case DATA_TYPE_NVLIST:
846 nvlist_free(EMBEDDED_NVL(nvp));
848 case DATA_TYPE_NVLIST_ARRAY: {
849 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
852 for (i = 0; i < NVP_NELEM(nvp); i++)
854 nvlist_free(nvlp[i]);
863 * nvlist_free - free an unpacked nvlist
866 nvlist_free(nvlist_t *nvl)
872 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
876 * Unpacked nvlist are linked through i_nvp_t
878 curr = priv->nvp_list;
879 while (curr != NULL) {
880 nvpair_t *nvp = &curr->nvi_nvp;
881 curr = curr->nvi_next;
884 nvp_buf_free(nvl, nvp);
887 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
888 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
893 nv_mem_free(priv, priv, sizeof (nvpriv_t));
897 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
899 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
905 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
906 if (&curr->nvi_nvp == nvp)
913 * Make a copy of nvlist
916 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
918 return (nvlist_xdup(nvl, nvlp, nvlist_nv_alloc(kmflag)));
922 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
927 if (nvl == NULL || nvlp == NULL)
930 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
933 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
942 * Remove all with matching name
945 nvlist_remove_all(nvlist_t *nvl, const char *name)
949 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
953 while ((nvp = nvt_lookup_name(nvl, name)) != NULL) {
954 VERIFY0(nvlist_remove_nvpair(nvl, nvp));
962 * Remove first one with matching name and type
965 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
967 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
970 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
974 return (nvlist_remove_nvpair(nvl, nvp));
978 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
980 if (nvl == NULL || nvp == NULL)
983 int err = nvt_remove_nvpair(nvl, nvp);
987 nvp_buf_unlink(nvl, nvp);
989 nvp_buf_free(nvl, nvp);
994 * This function calculates the size of an nvpair value.
996 * The data argument controls the behavior in case of the data types
997 * DATA_TYPE_STRING and
998 * DATA_TYPE_STRING_ARRAY
999 * Is data == NULL then the size of the string(s) is excluded.
1002 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
1006 if (i_validate_type_nelem(type, nelem) != 0)
1009 /* Calculate required size for holding value */
1011 case DATA_TYPE_BOOLEAN:
1014 case DATA_TYPE_BOOLEAN_VALUE:
1015 value_sz = sizeof (boolean_t);
1017 case DATA_TYPE_BYTE:
1018 value_sz = sizeof (uchar_t);
1020 case DATA_TYPE_INT8:
1021 value_sz = sizeof (int8_t);
1023 case DATA_TYPE_UINT8:
1024 value_sz = sizeof (uint8_t);
1026 case DATA_TYPE_INT16:
1027 value_sz = sizeof (int16_t);
1029 case DATA_TYPE_UINT16:
1030 value_sz = sizeof (uint16_t);
1032 case DATA_TYPE_INT32:
1033 value_sz = sizeof (int32_t);
1035 case DATA_TYPE_UINT32:
1036 value_sz = sizeof (uint32_t);
1038 case DATA_TYPE_INT64:
1039 value_sz = sizeof (int64_t);
1041 case DATA_TYPE_UINT64:
1042 value_sz = sizeof (uint64_t);
1044 #if !defined(_KERNEL)
1045 case DATA_TYPE_DOUBLE:
1046 value_sz = sizeof (double);
1049 case DATA_TYPE_STRING:
1053 value_sz = strlen(data) + 1;
1055 case DATA_TYPE_BOOLEAN_ARRAY:
1056 value_sz = (uint64_t)nelem * sizeof (boolean_t);
1058 case DATA_TYPE_BYTE_ARRAY:
1059 value_sz = (uint64_t)nelem * sizeof (uchar_t);
1061 case DATA_TYPE_INT8_ARRAY:
1062 value_sz = (uint64_t)nelem * sizeof (int8_t);
1064 case DATA_TYPE_UINT8_ARRAY:
1065 value_sz = (uint64_t)nelem * sizeof (uint8_t);
1067 case DATA_TYPE_INT16_ARRAY:
1068 value_sz = (uint64_t)nelem * sizeof (int16_t);
1070 case DATA_TYPE_UINT16_ARRAY:
1071 value_sz = (uint64_t)nelem * sizeof (uint16_t);
1073 case DATA_TYPE_INT32_ARRAY:
1074 value_sz = (uint64_t)nelem * sizeof (int32_t);
1076 case DATA_TYPE_UINT32_ARRAY:
1077 value_sz = (uint64_t)nelem * sizeof (uint32_t);
1079 case DATA_TYPE_INT64_ARRAY:
1080 value_sz = (uint64_t)nelem * sizeof (int64_t);
1082 case DATA_TYPE_UINT64_ARRAY:
1083 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1085 case DATA_TYPE_STRING_ARRAY:
1086 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1089 char *const *strs = data;
1092 /* no alignment requirement for strings */
1093 for (i = 0; i < nelem; i++) {
1094 if (strs[i] == NULL)
1096 value_sz += strlen(strs[i]) + 1;
1100 case DATA_TYPE_HRTIME:
1101 value_sz = sizeof (hrtime_t);
1103 case DATA_TYPE_NVLIST:
1104 value_sz = NV_ALIGN(sizeof (nvlist_t));
1106 case DATA_TYPE_NVLIST_ARRAY:
1107 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
1108 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
1114 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
1118 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
1123 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
1124 nvl->nvl_priv)) == NULL)
1127 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
1129 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
1130 nvlist_free(emb_nvl);
1131 emb_nvl->nvl_priv = 0;
1138 * nvlist_add_common - Add new <name,value> pair to nvlist
1141 nvlist_add_common(nvlist_t *nvl, const char *name,
1142 data_type_t type, uint_t nelem, const void *data)
1147 int nvp_sz, name_sz, value_sz;
1150 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1153 if (nelem != 0 && data == NULL)
1157 * Verify type and nelem and get the value size.
1158 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1159 * is the size of the string(s) included.
1161 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
1164 if (i_validate_nvpair_value(type, nelem, data) != 0)
1168 * If we're adding an nvlist or nvlist array, ensure that we are not
1169 * adding the input nvlist to itself, which would cause recursion,
1170 * and ensure that no NULL nvlist pointers are present.
1173 case DATA_TYPE_NVLIST:
1174 if (data == nvl || data == NULL)
1177 case DATA_TYPE_NVLIST_ARRAY: {
1178 nvlist_t **onvlp = (nvlist_t **)data;
1179 for (i = 0; i < nelem; i++) {
1180 if (onvlp[i] == nvl || onvlp[i] == NULL)
1189 /* calculate sizes of the nvpair elements and the nvpair itself */
1190 name_sz = strlen(name) + 1;
1191 if (name_sz >= 1ULL << (sizeof (nvp->nvp_name_sz) * NBBY - 1))
1194 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
1196 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
1199 ASSERT(nvp->nvp_size == nvp_sz);
1200 nvp->nvp_name_sz = name_sz;
1201 nvp->nvp_value_elem = nelem;
1202 nvp->nvp_type = type;
1203 bcopy(name, NVP_NAME(nvp), name_sz);
1206 case DATA_TYPE_BOOLEAN:
1208 case DATA_TYPE_STRING_ARRAY: {
1209 char *const *strs = data;
1210 char *buf = NVP_VALUE(nvp);
1211 char **cstrs = (void *)buf;
1213 /* skip pre-allocated space for pointer array */
1214 buf += nelem * sizeof (uint64_t);
1215 for (i = 0; i < nelem; i++) {
1216 int slen = strlen(strs[i]) + 1;
1217 bcopy(strs[i], buf, slen);
1223 case DATA_TYPE_NVLIST: {
1224 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
1225 nvlist_t *onvl = (nvlist_t *)data;
1227 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
1228 nvp_buf_free(nvl, nvp);
1233 case DATA_TYPE_NVLIST_ARRAY: {
1234 nvlist_t **onvlp = (nvlist_t **)data;
1235 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1236 nvlist_t *embedded = (nvlist_t *)
1237 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
1239 for (i = 0; i < nelem; i++) {
1240 if ((err = nvlist_copy_embedded(nvl,
1241 onvlp[i], embedded)) != 0) {
1243 * Free any successfully created lists
1246 nvp_buf_free(nvl, nvp);
1250 nvlp[i] = embedded++;
1255 bcopy(data, NVP_VALUE(nvp), value_sz);
1258 /* if unique name, remove before add */
1259 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
1260 (void) nvlist_remove_all(nvl, name);
1261 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
1262 (void) nvlist_remove(nvl, name, type);
1264 err = nvt_add_nvpair(nvl, nvp);
1267 nvp_buf_free(nvl, nvp);
1270 nvp_buf_link(nvl, nvp);
1276 nvlist_add_boolean(nvlist_t *nvl, const char *name)
1278 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1282 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1284 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1288 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1290 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1294 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1296 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1300 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1302 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1306 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1308 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1312 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1314 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1318 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1320 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1324 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1326 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1330 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1332 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1336 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1338 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1341 #if !defined(_KERNEL)
1343 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1345 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1350 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1352 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1356 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1357 boolean_t *a, uint_t n)
1359 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1363 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1365 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1369 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1371 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1375 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1377 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1381 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1383 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1387 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1389 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1393 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1395 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1399 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1401 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1405 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1407 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1411 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1413 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1417 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1418 char *const *a, uint_t n)
1420 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1424 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1426 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1430 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1432 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1436 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1438 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1441 /* reading name-value pairs */
1443 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1449 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1452 curr = NVPAIR2I_NVP(nvp);
1455 * Ensure that nvp is a valid nvpair on this nvlist.
1456 * NB: nvp_curr is used only as a hint so that we don't always
1457 * have to walk the list to determine if nvp is still on the list.
1460 curr = priv->nvp_list;
1461 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1462 curr = curr->nvi_next;
1466 priv->nvp_curr = curr;
1468 return (curr != NULL ? &curr->nvi_nvp : NULL);
1472 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1478 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1481 curr = NVPAIR2I_NVP(nvp);
1484 curr = priv->nvp_last;
1485 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1486 curr = curr->nvi_prev;
1490 priv->nvp_curr = curr;
1492 return (curr != NULL ? &curr->nvi_nvp : NULL);
1496 nvlist_empty(nvlist_t *nvl)
1501 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1504 return (priv->nvp_list == NULL);
1508 nvpair_name(nvpair_t *nvp)
1510 return (NVP_NAME(nvp));
1514 nvpair_type(nvpair_t *nvp)
1516 return (NVP_TYPE(nvp));
1520 nvpair_type_is_array(nvpair_t *nvp)
1522 data_type_t type = NVP_TYPE(nvp);
1524 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1525 (type == DATA_TYPE_INT8_ARRAY) ||
1526 (type == DATA_TYPE_UINT8_ARRAY) ||
1527 (type == DATA_TYPE_INT16_ARRAY) ||
1528 (type == DATA_TYPE_UINT16_ARRAY) ||
1529 (type == DATA_TYPE_INT32_ARRAY) ||
1530 (type == DATA_TYPE_UINT32_ARRAY) ||
1531 (type == DATA_TYPE_INT64_ARRAY) ||
1532 (type == DATA_TYPE_UINT64_ARRAY) ||
1533 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1534 (type == DATA_TYPE_STRING_ARRAY) ||
1535 (type == DATA_TYPE_NVLIST_ARRAY))
1542 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1546 if (nvp == NULL || nvpair_type(nvp) != type)
1550 * For non-array types, we copy the data.
1551 * For array types (including string), we set a pointer.
1554 case DATA_TYPE_BOOLEAN:
1559 case DATA_TYPE_BOOLEAN_VALUE:
1560 case DATA_TYPE_BYTE:
1561 case DATA_TYPE_INT8:
1562 case DATA_TYPE_UINT8:
1563 case DATA_TYPE_INT16:
1564 case DATA_TYPE_UINT16:
1565 case DATA_TYPE_INT32:
1566 case DATA_TYPE_UINT32:
1567 case DATA_TYPE_INT64:
1568 case DATA_TYPE_UINT64:
1569 case DATA_TYPE_HRTIME:
1570 #if !defined(_KERNEL)
1571 case DATA_TYPE_DOUBLE:
1575 if ((value_sz = i_get_value_size(type, NULL, 1)) < 0)
1577 bcopy(NVP_VALUE(nvp), data, (size_t)value_sz);
1582 case DATA_TYPE_NVLIST:
1583 case DATA_TYPE_STRING:
1586 *(void **)data = (void *)NVP_VALUE(nvp);
1591 case DATA_TYPE_BOOLEAN_ARRAY:
1592 case DATA_TYPE_BYTE_ARRAY:
1593 case DATA_TYPE_INT8_ARRAY:
1594 case DATA_TYPE_UINT8_ARRAY:
1595 case DATA_TYPE_INT16_ARRAY:
1596 case DATA_TYPE_UINT16_ARRAY:
1597 case DATA_TYPE_INT32_ARRAY:
1598 case DATA_TYPE_UINT32_ARRAY:
1599 case DATA_TYPE_INT64_ARRAY:
1600 case DATA_TYPE_UINT64_ARRAY:
1601 case DATA_TYPE_STRING_ARRAY:
1602 case DATA_TYPE_NVLIST_ARRAY:
1603 if (nelem == NULL || data == NULL)
1605 if ((*nelem = NVP_NELEM(nvp)) != 0)
1606 *(void **)data = (void *)NVP_VALUE(nvp);
1608 *(void **)data = NULL;
1619 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1620 uint_t *nelem, void *data)
1622 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1625 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1628 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
1632 return (nvpair_value_common(nvp, type, nelem, data));
1636 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1638 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1642 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1644 return (nvlist_lookup_common(nvl, name,
1645 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1649 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1651 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1655 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1657 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1661 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1663 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1667 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1669 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1673 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1675 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1679 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1681 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1685 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1687 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1691 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1693 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1697 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1699 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1702 #if !defined(_KERNEL)
1704 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1706 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1711 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1713 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1717 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1719 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1723 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1724 boolean_t **a, uint_t *n)
1726 return (nvlist_lookup_common(nvl, name,
1727 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1731 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1732 uchar_t **a, uint_t *n)
1734 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1738 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1740 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1744 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1745 uint8_t **a, uint_t *n)
1747 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1751 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1752 int16_t **a, uint_t *n)
1754 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1758 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1759 uint16_t **a, uint_t *n)
1761 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1765 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1766 int32_t **a, uint_t *n)
1768 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1772 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1773 uint32_t **a, uint_t *n)
1775 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1779 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1780 int64_t **a, uint_t *n)
1782 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1786 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1787 uint64_t **a, uint_t *n)
1789 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1793 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1794 char ***a, uint_t *n)
1796 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1800 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1801 nvlist_t ***a, uint_t *n)
1803 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1807 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1809 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1813 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1817 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1821 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1826 switch (type = va_arg(ap, data_type_t)) {
1827 case DATA_TYPE_BOOLEAN:
1828 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1831 case DATA_TYPE_BOOLEAN_VALUE:
1832 case DATA_TYPE_BYTE:
1833 case DATA_TYPE_INT8:
1834 case DATA_TYPE_UINT8:
1835 case DATA_TYPE_INT16:
1836 case DATA_TYPE_UINT16:
1837 case DATA_TYPE_INT32:
1838 case DATA_TYPE_UINT32:
1839 case DATA_TYPE_INT64:
1840 case DATA_TYPE_UINT64:
1841 case DATA_TYPE_HRTIME:
1842 case DATA_TYPE_STRING:
1843 case DATA_TYPE_NVLIST:
1844 #if !defined(_KERNEL)
1845 case DATA_TYPE_DOUBLE:
1847 val = va_arg(ap, void *);
1848 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1851 case DATA_TYPE_BYTE_ARRAY:
1852 case DATA_TYPE_BOOLEAN_ARRAY:
1853 case DATA_TYPE_INT8_ARRAY:
1854 case DATA_TYPE_UINT8_ARRAY:
1855 case DATA_TYPE_INT16_ARRAY:
1856 case DATA_TYPE_UINT16_ARRAY:
1857 case DATA_TYPE_INT32_ARRAY:
1858 case DATA_TYPE_UINT32_ARRAY:
1859 case DATA_TYPE_INT64_ARRAY:
1860 case DATA_TYPE_UINT64_ARRAY:
1861 case DATA_TYPE_STRING_ARRAY:
1862 case DATA_TYPE_NVLIST_ARRAY:
1863 val = va_arg(ap, void *);
1864 nelem = va_arg(ap, uint_t *);
1865 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1872 if (ret == ENOENT && noentok)
1881 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1882 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1883 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1884 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1885 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1886 * "a.d[3].e[1]". This matches the C syntax for array embed (for convenience,
1887 * code also supports "a.d[3]e[1]" syntax).
1889 * If 'ip' is non-NULL and the last name component is an array, return the
1890 * value of the "...[index]" array index in *ip. For an array reference that
1891 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1892 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1893 * inside the 'name' string where the syntax error was detected.
1896 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1897 nvpair_t **ret, int *ip, char **ep)
1908 *ip = -1; /* not indexed */
1912 if ((nvl == NULL) || (name == NULL))
1917 /* step through components of name */
1918 for (np = name; np && *np; np = sepp) {
1919 /* ensure unique names */
1920 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1923 /* skip white space */
1924 skip_whitespace(np);
1928 /* set 'sepp' to end of current component 'np' */
1930 sepp = strchr(np, sep);
1934 /* find start of next "[ index ]..." */
1935 idxp = strchr(np, '[');
1937 /* if sepp comes first, set idxp to NULL */
1938 if (sepp && idxp && (sepp < idxp))
1942 * At this point 'idxp' is set if there is an index
1943 * expected for the current component.
1946 /* set 'n' to length of current 'np' name component */
1949 /* keep sepp up to date for *ep use as we advance */
1950 skip_whitespace(idxp);
1953 /* determine the index value */
1954 #if defined(_KERNEL)
1955 if (ddi_strtol(idxp, &idxep, 0, &idx))
1958 idx = strtol(idxp, &idxep, 0);
1963 /* keep sepp up to date for *ep use as we advance */
1966 /* skip white space index value and check for ']' */
1967 skip_whitespace(sepp);
1971 /* for embedded arrays, support C syntax: "a[1].b" */
1972 skip_whitespace(sepp);
1973 if (sep && (*sepp == sep))
1981 /* trim trailing whitespace by reducing length of 'np' */
1984 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1988 /* skip whitespace, and set sepp to NULL if complete */
1990 skip_whitespace(sepp);
1997 * o 'n' is the length of current 'np' component.
1998 * o 'idxp' is set if there was an index, and value 'idx'.
1999 * o 'sepp' is set to the beginning of the next component,
2000 * and set to NULL if we have no more components.
2002 * Search for nvpair with matching component name.
2004 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
2005 nvp = nvlist_next_nvpair(nvl, nvp)) {
2007 /* continue if no match on name */
2008 if (strncmp(np, nvpair_name(nvp), n) ||
2009 (strlen(nvpair_name(nvp)) != n))
2012 /* if indexed, verify type is array oriented */
2013 if (idxp && !nvpair_type_is_array(nvp))
2017 * Full match found, return nvp and idx if this
2018 * was the last component.
2024 *ip = (int)idx; /* return index */
2025 return (0); /* found */
2029 * More components: current match must be
2030 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2031 * to support going deeper.
2033 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
2034 nvl = EMBEDDED_NVL(nvp);
2036 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
2037 (void) nvpair_value_nvlist_array(nvp,
2038 &nva, (uint_t *)&n);
2039 if ((n < 0) || (idx >= n))
2045 /* type does not support more levels */
2049 goto fail; /* 'name' not found */
2051 /* search for match of next component in embedded 'nvl' list */
2054 fail: if (ep && sepp)
2060 * Return pointer to nvpair with specified 'name'.
2063 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
2065 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
2069 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2070 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2073 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
2074 const char *name, nvpair_t **ret, int *ip, char **ep)
2076 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
2080 nvlist_exists(nvlist_t *nvl, const char *name)
2086 if (name == NULL || nvl == NULL ||
2087 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2090 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2091 nvp = &curr->nvi_nvp;
2093 if (strcmp(name, NVP_NAME(nvp)) == 0)
2101 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
2103 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
2107 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
2109 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
2113 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
2115 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
2119 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
2121 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
2125 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
2127 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
2131 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
2133 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
2137 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
2139 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
2143 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
2145 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
2149 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
2151 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
2155 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
2157 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
2160 #if !defined(_KERNEL)
2162 nvpair_value_double(nvpair_t *nvp, double *val)
2164 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
2169 nvpair_value_string(nvpair_t *nvp, char **val)
2171 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
2175 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
2177 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
2181 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
2183 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
2187 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
2189 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
2193 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
2195 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
2199 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
2201 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
2205 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
2207 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
2211 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
2213 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
2217 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
2219 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
2223 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
2225 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
2229 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
2231 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
2235 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
2237 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
2241 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
2243 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
2247 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
2249 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
2253 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
2255 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
2259 * Add specified pair to the list.
2262 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2264 if (nvl == NULL || nvp == NULL)
2267 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
2268 NVP_NELEM(nvp), NVP_VALUE(nvp)));
2272 * Merge the supplied nvlists and put the result in dst.
2273 * The merged list will contain all names specified in both lists,
2274 * the values are taken from nvl in the case of duplicates.
2275 * Return 0 on success.
2279 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2281 if (nvl == NULL || dst == NULL)
2285 return (nvlist_copy_pairs(nvl, dst));
2291 * Encoding related routines
2293 #define NVS_OP_ENCODE 0
2294 #define NVS_OP_DECODE 1
2295 #define NVS_OP_GETSIZE 2
2297 typedef struct nvs_ops nvs_ops_t;
2301 const nvs_ops_t *nvs_ops;
2308 * nvs operations are:
2310 * encoding / decoding of an nvlist header (nvlist_t)
2311 * calculates the size used for header and end detection
2314 * responsible for the first part of encoding / decoding of an nvpair
2315 * calculates the decoded size of an nvpair
2318 * second part of encoding / decoding of an nvpair
2321 * calculates the encoding size of an nvpair
2324 * encodes the end detection mark (zeros).
2327 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2328 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2329 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2330 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2331 int (*nvs_nvl_fini)(nvstream_t *);
2335 char nvh_encoding; /* nvs encoding method */
2336 char nvh_endian; /* nvs endian */
2337 char nvh_reserved1; /* reserved for future use */
2338 char nvh_reserved2; /* reserved for future use */
2342 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2344 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2348 * Walk nvpair in list and encode each nvpair
2350 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2351 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2354 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2358 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2365 * Get decoded size of next pair in stream, alloc
2366 * memory for nvpair_t, then decode the nvpair
2368 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2369 if (nvsize == 0) /* end of list */
2372 /* make sure len makes sense */
2373 if (nvsize < NVP_SIZE_CALC(1, 0))
2376 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2379 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2380 nvp_buf_free(nvl, nvp);
2384 if (i_validate_nvpair(nvp) != 0) {
2386 nvp_buf_free(nvl, nvp);
2390 err = nvt_add_nvpair(nvl, nvp);
2393 nvp_buf_free(nvl, nvp);
2396 nvp_buf_link(nvl, nvp);
2402 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2404 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2406 uint64_t nvsize = *buflen;
2410 * Get encoded size of nvpairs in nvlist
2412 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2413 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2416 if ((nvsize += size) > INT32_MAX)
2425 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2429 if (nvl->nvl_priv == 0)
2433 * Perform the operation, starting with header, then each nvpair
2435 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2438 switch (nvs->nvs_op) {
2440 err = nvs_encode_pairs(nvs, nvl);
2444 err = nvs_decode_pairs(nvs, nvl);
2447 case NVS_OP_GETSIZE:
2448 err = nvs_getsize_pairs(nvs, nvl, buflen);
2459 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2461 switch (nvs->nvs_op) {
2462 case NVS_OP_ENCODE: {
2465 if (nvs->nvs_recursion >= nvpair_max_recursion)
2467 nvs->nvs_recursion++;
2468 err = nvs_operation(nvs, embedded, NULL);
2469 nvs->nvs_recursion--;
2472 case NVS_OP_DECODE: {
2476 if (embedded->nvl_version != NV_VERSION)
2479 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2482 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2484 if (nvs->nvs_recursion >= nvpair_max_recursion) {
2485 nvlist_free(embedded);
2488 nvs->nvs_recursion++;
2489 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2490 nvlist_free(embedded);
2491 nvs->nvs_recursion--;
2502 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2504 size_t nelem = NVP_NELEM(nvp);
2505 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2508 switch (nvs->nvs_op) {
2510 for (i = 0; i < nelem; i++)
2511 if (nvs_embedded(nvs, nvlp[i]) != 0)
2515 case NVS_OP_DECODE: {
2516 size_t len = nelem * sizeof (uint64_t);
2517 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2519 bzero(nvlp, len); /* don't trust packed data */
2520 for (i = 0; i < nelem; i++) {
2521 if (nvs_embedded(nvs, embedded) != 0) {
2526 nvlp[i] = embedded++;
2530 case NVS_OP_GETSIZE: {
2531 uint64_t nvsize = 0;
2533 for (i = 0; i < nelem; i++) {
2536 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2539 if ((nvsize += nvp_sz) > INT32_MAX)
2553 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2554 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2557 * Common routine for nvlist operations:
2558 * encode, decode, getsize (encoded size).
2561 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2567 #if defined(_ZFS_LITTLE_ENDIAN)
2568 int host_endian = 1;
2569 #elif defined(_ZFS_BIG_ENDIAN)
2570 int host_endian = 0;
2572 #error "No endian defined!"
2573 #endif /* _ZFS_LITTLE_ENDIAN */
2576 if (buflen == NULL || nvl == NULL ||
2577 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2580 nvs.nvs_op = nvs_op;
2581 nvs.nvs_recursion = 0;
2584 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2585 * a buffer is allocated. The first 4 bytes in the buffer are
2586 * used for encoding method and host endian.
2590 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2594 nvh->nvh_encoding = encoding;
2595 nvh->nvh_endian = nvl_endian = host_endian;
2596 nvh->nvh_reserved1 = 0;
2597 nvh->nvh_reserved2 = 0;
2601 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2604 /* get method of encoding from first byte */
2606 encoding = nvh->nvh_encoding;
2607 nvl_endian = nvh->nvh_endian;
2610 case NVS_OP_GETSIZE:
2611 nvl_endian = host_endian;
2614 * add the size for encoding
2616 *buflen = sizeof (nvs_header_t);
2624 * Create an nvstream with proper encoding method
2627 case NV_ENCODE_NATIVE:
2629 * check endianness, in case we are unpacking
2632 if (nvl_endian != host_endian)
2634 err = nvs_native(&nvs, nvl, buf, buflen);
2637 err = nvs_xdr(&nvs, nvl, buf, buflen);
2648 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2650 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2654 * Pack nvlist into contiguous memory
2657 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2660 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2661 nvlist_nv_alloc(kmflag)));
2665 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2673 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2677 return (nvlist_common(nvl, *bufp, buflen, encoding,
2681 * Here is a difficult situation:
2682 * 1. The nvlist has fixed allocator properties.
2683 * All other nvlist routines (like nvlist_add_*, ...) use
2685 * 2. When using nvlist_pack() the user can specify their own
2686 * allocator properties (e.g. by using KM_NOSLEEP).
2688 * We use the user specified properties (2). A clearer solution
2689 * will be to remove the kmflag from nvlist_pack(), but we will
2690 * not change the interface.
2692 nv_priv_init(&nvpriv, nva, 0);
2694 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2697 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2700 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2701 NVS_OP_ENCODE)) != 0) {
2702 nv_mem_free(&nvpriv, buf, alloc_size);
2704 *buflen = alloc_size;
2712 * Unpack buf into an nvlist_t
2715 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2717 return (nvlist_xunpack(buf, buflen, nvlp, nvlist_nv_alloc(kmflag)));
2721 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2729 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2732 if ((err = nvlist_common(nvl, buf, &buflen, NV_ENCODE_NATIVE,
2733 NVS_OP_DECODE)) != 0)
2742 * Native encoding functions
2746 * This structure is used when decoding a packed nvpair in
2747 * the native format. n_base points to a buffer containing the
2748 * packed nvpair. n_end is a pointer to the end of the buffer.
2749 * (n_end actually points to the first byte past the end of the
2750 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2751 * It points to the current data that we are decoding.
2752 * The amount of data left in the buffer is equal to n_end - n_curr.
2753 * n_flag is used to recognize a packed embedded list.
2762 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2765 switch (nvs->nvs_op) {
2768 nvs->nvs_private = native;
2769 native->n_curr = native->n_base = buf;
2770 native->n_end = buf + buflen;
2774 case NVS_OP_GETSIZE:
2775 nvs->nvs_private = native;
2776 native->n_curr = native->n_base = native->n_end = NULL;
2786 nvs_native_destroy(nvstream_t *nvs)
2791 native_cp(nvstream_t *nvs, void *buf, size_t size)
2793 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2795 if (native->n_curr + size > native->n_end)
2799 * The bcopy() below eliminates alignment requirement
2800 * on the buffer (stream) and is preferred over direct access.
2802 switch (nvs->nvs_op) {
2804 bcopy(buf, native->n_curr, size);
2807 bcopy(native->n_curr, buf, size);
2813 native->n_curr += size;
2818 * operate on nvlist_t header
2821 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2823 nvs_native_t *native = nvs->nvs_private;
2825 switch (nvs->nvs_op) {
2829 return (0); /* packed embedded list */
2833 /* copy version and nvflag of the nvlist_t */
2834 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2835 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2840 case NVS_OP_GETSIZE:
2842 * if calculate for packed embedded list
2843 * 4 for end of the embedded list
2845 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2846 * and 4 for end of the entire list
2848 if (native->n_flag) {
2852 *size += 2 * sizeof (int32_t) + 4;
2863 nvs_native_nvl_fini(nvstream_t *nvs)
2865 if (nvs->nvs_op == NVS_OP_ENCODE) {
2866 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2868 * Add 4 zero bytes at end of nvlist. They are used
2869 * for end detection by the decode routine.
2871 if (native->n_curr + sizeof (int) > native->n_end)
2874 bzero(native->n_curr, sizeof (int));
2875 native->n_curr += sizeof (int);
2882 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2884 if (nvs->nvs_op == NVS_OP_ENCODE) {
2885 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2886 nvlist_t *packed = (void *)
2887 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2889 * Null out the pointer that is meaningless in the packed
2890 * structure. The address may not be aligned, so we have
2893 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2897 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2901 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2903 if (nvs->nvs_op == NVS_OP_ENCODE) {
2904 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2905 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2906 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2907 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2910 * Null out pointers that are meaningless in the packed
2911 * structure. The addresses may not be aligned, so we have
2916 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2918 * Null out the pointer that is meaningless in the
2919 * packed structure. The address may not be aligned,
2920 * so we have to use bzero.
2922 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2926 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2930 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2932 switch (nvs->nvs_op) {
2933 case NVS_OP_ENCODE: {
2934 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2935 uint64_t *strp = (void *)
2936 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2938 * Null out pointers that are meaningless in the packed
2939 * structure. The addresses may not be aligned, so we have
2942 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2945 case NVS_OP_DECODE: {
2946 char **strp = (void *)NVP_VALUE(nvp);
2947 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2950 for (i = 0; i < NVP_NELEM(nvp); i++) {
2952 buf += strlen(buf) + 1;
2960 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2967 * We do the initial bcopy of the data before we look at
2968 * the nvpair type, because when we're decoding, we won't
2969 * have the correct values for the pair until we do the bcopy.
2971 switch (nvs->nvs_op) {
2974 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2981 /* verify nvp_name_sz, check the name string length */
2982 if (i_validate_nvpair_name(nvp) != 0)
2985 type = NVP_TYPE(nvp);
2988 * Verify type and nelem and get the value size.
2989 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2990 * is the size of the string(s) excluded.
2992 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2995 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2999 case DATA_TYPE_NVLIST:
3000 ret = nvpair_native_embedded(nvs, nvp);
3002 case DATA_TYPE_NVLIST_ARRAY:
3003 ret = nvpair_native_embedded_array(nvs, nvp);
3005 case DATA_TYPE_STRING_ARRAY:
3006 nvpair_native_string_array(nvs, nvp);
3016 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3018 uint64_t nvp_sz = nvp->nvp_size;
3020 switch (NVP_TYPE(nvp)) {
3021 case DATA_TYPE_NVLIST: {
3024 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
3030 case DATA_TYPE_NVLIST_ARRAY: {
3033 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
3043 if (nvp_sz > INT32_MAX)
3052 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3054 switch (nvs->nvs_op) {
3056 return (nvs_native_nvp_op(nvs, nvp));
3058 case NVS_OP_DECODE: {
3059 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
3062 /* try to read the size value from the stream */
3063 if (native->n_curr + sizeof (int32_t) > native->n_end)
3065 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
3067 /* sanity check the size value */
3068 if (decode_len < 0 ||
3069 decode_len > native->n_end - native->n_curr)
3075 * If at the end of the stream then move the cursor
3076 * forward, otherwise nvpair_native_op() will read
3077 * the entire nvpair at the same cursor position.
3080 native->n_curr += sizeof (int32_t);
3091 static const nvs_ops_t nvs_native_ops = {
3092 .nvs_nvlist = nvs_native_nvlist,
3093 .nvs_nvpair = nvs_native_nvpair,
3094 .nvs_nvp_op = nvs_native_nvp_op,
3095 .nvs_nvp_size = nvs_native_nvp_size,
3096 .nvs_nvl_fini = nvs_native_nvl_fini
3100 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3102 nvs_native_t native;
3105 nvs->nvs_ops = &nvs_native_ops;
3107 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
3108 *buflen - sizeof (nvs_header_t))) != 0)
3111 err = nvs_operation(nvs, nvl, buflen);
3113 nvs_native_destroy(nvs);
3119 * XDR encoding functions
3121 * An xdr packed nvlist is encoded as:
3123 * - encoding method and host endian (4 bytes)
3124 * - nvl_version (4 bytes)
3125 * - nvl_nvflag (4 bytes)
3127 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3128 * - encoded size of the nvpair (4 bytes)
3129 * - decoded size of the nvpair (4 bytes)
3130 * - name string, (4 + sizeof(NV_ALIGN4(string))
3131 * a string is coded as size (4 bytes) and data
3132 * - data type (4 bytes)
3133 * - number of elements in the nvpair (4 bytes)
3136 * - 2 zero's for end of the entire list (8 bytes)
3139 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
3141 /* xdr data must be 4 byte aligned */
3142 if ((ulong_t)buf % 4 != 0)
3145 switch (nvs->nvs_op) {
3147 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
3148 nvs->nvs_private = xdr;
3151 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
3152 nvs->nvs_private = xdr;
3154 case NVS_OP_GETSIZE:
3155 nvs->nvs_private = NULL;
3163 nvs_xdr_destroy(nvstream_t *nvs)
3165 switch (nvs->nvs_op) {
3168 xdr_destroy((XDR *)nvs->nvs_private);
3176 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
3178 switch (nvs->nvs_op) {
3180 case NVS_OP_DECODE: {
3181 XDR *xdr = nvs->nvs_private;
3183 if (!xdr_int(xdr, &nvl->nvl_version) ||
3184 !xdr_u_int(xdr, &nvl->nvl_nvflag))
3188 case NVS_OP_GETSIZE: {
3190 * 2 * 4 for nvl_version + nvl_nvflag
3191 * and 8 for end of the entire list
3203 nvs_xdr_nvl_fini(nvstream_t *nvs)
3205 if (nvs->nvs_op == NVS_OP_ENCODE) {
3206 XDR *xdr = nvs->nvs_private;
3209 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
3217 * The format of xdr encoded nvpair is:
3218 * encode_size, decode_size, name string, data type, nelem, data
3221 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
3225 char *buf_end = (char *)nvp + nvp->nvp_size;
3227 uint_t nelem, buflen;
3229 XDR *xdr = nvs->nvs_private;
3231 ASSERT(xdr != NULL && nvp != NULL);
3234 if ((buf = NVP_NAME(nvp)) >= buf_end)
3236 buflen = buf_end - buf;
3238 if (!xdr_string(xdr, &buf, buflen - 1))
3240 nvp->nvp_name_sz = strlen(buf) + 1;
3242 /* type and nelem */
3243 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
3244 !xdr_int(xdr, &nvp->nvp_value_elem))
3247 type = NVP_TYPE(nvp);
3248 nelem = nvp->nvp_value_elem;
3251 * Verify type and nelem and get the value size.
3252 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3253 * is the size of the string(s) excluded.
3255 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
3258 /* if there is no data to extract then return */
3263 if ((buf = NVP_VALUE(nvp)) >= buf_end)
3265 buflen = buf_end - buf;
3267 if (buflen < value_sz)
3271 case DATA_TYPE_NVLIST:
3272 if (nvs_embedded(nvs, (void *)buf) == 0)
3276 case DATA_TYPE_NVLIST_ARRAY:
3277 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
3281 case DATA_TYPE_BOOLEAN:
3285 case DATA_TYPE_BYTE:
3286 case DATA_TYPE_INT8:
3287 case DATA_TYPE_UINT8:
3288 ret = xdr_char(xdr, buf);
3291 case DATA_TYPE_INT16:
3292 ret = xdr_short(xdr, (void *)buf);
3295 case DATA_TYPE_UINT16:
3296 ret = xdr_u_short(xdr, (void *)buf);
3299 case DATA_TYPE_BOOLEAN_VALUE:
3300 case DATA_TYPE_INT32:
3301 ret = xdr_int(xdr, (void *)buf);
3304 case DATA_TYPE_UINT32:
3305 ret = xdr_u_int(xdr, (void *)buf);
3308 case DATA_TYPE_INT64:
3309 ret = xdr_longlong_t(xdr, (void *)buf);
3312 case DATA_TYPE_UINT64:
3313 ret = xdr_u_longlong_t(xdr, (void *)buf);
3316 case DATA_TYPE_HRTIME:
3318 * NOTE: must expose the definition of hrtime_t here
3320 ret = xdr_longlong_t(xdr, (void *)buf);
3322 #if !defined(_KERNEL)
3323 case DATA_TYPE_DOUBLE:
3324 ret = xdr_double(xdr, (void *)buf);
3327 case DATA_TYPE_STRING:
3328 ret = xdr_string(xdr, &buf, buflen - 1);
3331 case DATA_TYPE_BYTE_ARRAY:
3332 ret = xdr_opaque(xdr, buf, nelem);
3335 case DATA_TYPE_INT8_ARRAY:
3336 case DATA_TYPE_UINT8_ARRAY:
3337 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3338 (xdrproc_t)xdr_char);
3341 case DATA_TYPE_INT16_ARRAY:
3342 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3343 sizeof (int16_t), (xdrproc_t)xdr_short);
3346 case DATA_TYPE_UINT16_ARRAY:
3347 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3348 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3351 case DATA_TYPE_BOOLEAN_ARRAY:
3352 case DATA_TYPE_INT32_ARRAY:
3353 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3354 sizeof (int32_t), (xdrproc_t)xdr_int);
3357 case DATA_TYPE_UINT32_ARRAY:
3358 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3359 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3362 case DATA_TYPE_INT64_ARRAY:
3363 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3364 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3367 case DATA_TYPE_UINT64_ARRAY:
3368 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3369 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3372 case DATA_TYPE_STRING_ARRAY: {
3373 size_t len = nelem * sizeof (uint64_t);
3374 char **strp = (void *)buf;
3377 if (nvs->nvs_op == NVS_OP_DECODE)
3378 bzero(buf, len); /* don't trust packed data */
3380 for (i = 0; i < nelem; i++) {
3387 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3390 if (nvs->nvs_op == NVS_OP_DECODE)
3392 len = strlen(buf) + 1;
3401 return (ret == TRUE ? 0 : EFAULT);
3405 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3407 data_type_t type = NVP_TYPE(nvp);
3409 * encode_size + decode_size + name string size + data type + nelem
3410 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3412 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3415 case DATA_TYPE_BOOLEAN:
3418 case DATA_TYPE_BOOLEAN_VALUE:
3419 case DATA_TYPE_BYTE:
3420 case DATA_TYPE_INT8:
3421 case DATA_TYPE_UINT8:
3422 case DATA_TYPE_INT16:
3423 case DATA_TYPE_UINT16:
3424 case DATA_TYPE_INT32:
3425 case DATA_TYPE_UINT32:
3426 nvp_sz += 4; /* 4 is the minimum xdr unit */
3429 case DATA_TYPE_INT64:
3430 case DATA_TYPE_UINT64:
3431 case DATA_TYPE_HRTIME:
3432 #if !defined(_KERNEL)
3433 case DATA_TYPE_DOUBLE:
3438 case DATA_TYPE_STRING:
3439 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3442 case DATA_TYPE_BYTE_ARRAY:
3443 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3446 case DATA_TYPE_BOOLEAN_ARRAY:
3447 case DATA_TYPE_INT8_ARRAY:
3448 case DATA_TYPE_UINT8_ARRAY:
3449 case DATA_TYPE_INT16_ARRAY:
3450 case DATA_TYPE_UINT16_ARRAY:
3451 case DATA_TYPE_INT32_ARRAY:
3452 case DATA_TYPE_UINT32_ARRAY:
3453 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3456 case DATA_TYPE_INT64_ARRAY:
3457 case DATA_TYPE_UINT64_ARRAY:
3458 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3461 case DATA_TYPE_STRING_ARRAY: {
3463 char **strs = (void *)NVP_VALUE(nvp);
3465 for (i = 0; i < NVP_NELEM(nvp); i++)
3466 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3471 case DATA_TYPE_NVLIST:
3472 case DATA_TYPE_NVLIST_ARRAY: {
3474 int old_nvs_op = nvs->nvs_op;
3477 nvs->nvs_op = NVS_OP_GETSIZE;
3478 if (type == DATA_TYPE_NVLIST)
3479 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3481 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3482 nvs->nvs_op = old_nvs_op;
3495 if (nvp_sz > INT32_MAX)
3505 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3506 * the largest nvpair that could be encoded in the buffer.
3508 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3509 * The size of a xdr packed nvpair without any data is 5 words.
3511 * Using the size of the data directly as an estimate would be ok
3512 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3513 * then the actual nvpair has space for an array of pointers to index
3514 * the strings. These pointers are not encoded into the packed xdr buffer.
3516 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3517 * of length 0, then each string is encoded in xdr format as a single word.
3518 * Therefore when expanded to an nvpair there will be 2.25 word used for
3519 * each string. (a int64_t allocated for pointer usage, and a single char
3520 * for the null termination.)
3522 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3524 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3525 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3526 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3527 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3528 (NVS_XDR_DATA_LEN(x) * 2) + \
3529 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3532 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3534 XDR *xdr = nvs->nvs_private;
3535 int32_t encode_len, decode_len;
3537 switch (nvs->nvs_op) {
3538 case NVS_OP_ENCODE: {
3541 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3544 decode_len = nvp->nvp_size;
3545 encode_len = nvsize;
3546 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3549 return (nvs_xdr_nvp_op(nvs, nvp));
3551 case NVS_OP_DECODE: {
3552 struct xdr_bytesrec bytesrec;
3554 /* get the encode and decode size */
3555 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3559 /* are we at the end of the stream? */
3563 /* sanity check the size parameter */
3564 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3567 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3578 static const struct nvs_ops nvs_xdr_ops = {
3579 .nvs_nvlist = nvs_xdr_nvlist,
3580 .nvs_nvpair = nvs_xdr_nvpair,
3581 .nvs_nvp_op = nvs_xdr_nvp_op,
3582 .nvs_nvp_size = nvs_xdr_nvp_size,
3583 .nvs_nvl_fini = nvs_xdr_nvl_fini
3587 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3592 nvs->nvs_ops = &nvs_xdr_ops;
3594 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3595 *buflen - sizeof (nvs_header_t))) != 0)
3598 err = nvs_operation(nvs, nvl, buflen);
3600 nvs_xdr_destroy(nvs);
3605 #if defined(_KERNEL)
3617 module_init(nvpair_init);
3618 module_exit(nvpair_fini);
3621 ZFS_MODULE_DESCRIPTION("Generic name/value pair implementation");
3622 ZFS_MODULE_AUTHOR(ZFS_META_AUTHOR);
3623 ZFS_MODULE_LICENSE(ZFS_META_LICENSE);
3624 ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
3626 EXPORT_SYMBOL(nv_alloc_init);
3627 EXPORT_SYMBOL(nv_alloc_reset);
3628 EXPORT_SYMBOL(nv_alloc_fini);
3630 /* list management */
3631 EXPORT_SYMBOL(nvlist_alloc);
3632 EXPORT_SYMBOL(nvlist_free);
3633 EXPORT_SYMBOL(nvlist_size);
3634 EXPORT_SYMBOL(nvlist_pack);
3635 EXPORT_SYMBOL(nvlist_unpack);
3636 EXPORT_SYMBOL(nvlist_dup);
3637 EXPORT_SYMBOL(nvlist_merge);
3639 EXPORT_SYMBOL(nvlist_xalloc);
3640 EXPORT_SYMBOL(nvlist_xpack);
3641 EXPORT_SYMBOL(nvlist_xunpack);
3642 EXPORT_SYMBOL(nvlist_xdup);
3643 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3645 EXPORT_SYMBOL(nvlist_add_nvpair);
3646 EXPORT_SYMBOL(nvlist_add_boolean);
3647 EXPORT_SYMBOL(nvlist_add_boolean_value);
3648 EXPORT_SYMBOL(nvlist_add_byte);
3649 EXPORT_SYMBOL(nvlist_add_int8);
3650 EXPORT_SYMBOL(nvlist_add_uint8);
3651 EXPORT_SYMBOL(nvlist_add_int16);
3652 EXPORT_SYMBOL(nvlist_add_uint16);
3653 EXPORT_SYMBOL(nvlist_add_int32);
3654 EXPORT_SYMBOL(nvlist_add_uint32);
3655 EXPORT_SYMBOL(nvlist_add_int64);
3656 EXPORT_SYMBOL(nvlist_add_uint64);
3657 EXPORT_SYMBOL(nvlist_add_string);
3658 EXPORT_SYMBOL(nvlist_add_nvlist);
3659 EXPORT_SYMBOL(nvlist_add_boolean_array);
3660 EXPORT_SYMBOL(nvlist_add_byte_array);
3661 EXPORT_SYMBOL(nvlist_add_int8_array);
3662 EXPORT_SYMBOL(nvlist_add_uint8_array);
3663 EXPORT_SYMBOL(nvlist_add_int16_array);
3664 EXPORT_SYMBOL(nvlist_add_uint16_array);
3665 EXPORT_SYMBOL(nvlist_add_int32_array);
3666 EXPORT_SYMBOL(nvlist_add_uint32_array);
3667 EXPORT_SYMBOL(nvlist_add_int64_array);
3668 EXPORT_SYMBOL(nvlist_add_uint64_array);
3669 EXPORT_SYMBOL(nvlist_add_string_array);
3670 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3671 EXPORT_SYMBOL(nvlist_next_nvpair);
3672 EXPORT_SYMBOL(nvlist_prev_nvpair);
3673 EXPORT_SYMBOL(nvlist_empty);
3674 EXPORT_SYMBOL(nvlist_add_hrtime);
3676 EXPORT_SYMBOL(nvlist_remove);
3677 EXPORT_SYMBOL(nvlist_remove_nvpair);
3678 EXPORT_SYMBOL(nvlist_remove_all);
3680 EXPORT_SYMBOL(nvlist_lookup_boolean);
3681 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3682 EXPORT_SYMBOL(nvlist_lookup_byte);
3683 EXPORT_SYMBOL(nvlist_lookup_int8);
3684 EXPORT_SYMBOL(nvlist_lookup_uint8);
3685 EXPORT_SYMBOL(nvlist_lookup_int16);
3686 EXPORT_SYMBOL(nvlist_lookup_uint16);
3687 EXPORT_SYMBOL(nvlist_lookup_int32);
3688 EXPORT_SYMBOL(nvlist_lookup_uint32);
3689 EXPORT_SYMBOL(nvlist_lookup_int64);
3690 EXPORT_SYMBOL(nvlist_lookup_uint64);
3691 EXPORT_SYMBOL(nvlist_lookup_string);
3692 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3693 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3694 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3695 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3696 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3697 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3698 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3699 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3700 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3701 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3702 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3703 EXPORT_SYMBOL(nvlist_lookup_string_array);
3704 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3705 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3706 EXPORT_SYMBOL(nvlist_lookup_pairs);
3708 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3709 EXPORT_SYMBOL(nvlist_exists);
3711 /* processing nvpair */
3712 EXPORT_SYMBOL(nvpair_name);
3713 EXPORT_SYMBOL(nvpair_type);
3714 EXPORT_SYMBOL(nvpair_value_boolean_value);
3715 EXPORT_SYMBOL(nvpair_value_byte);
3716 EXPORT_SYMBOL(nvpair_value_int8);
3717 EXPORT_SYMBOL(nvpair_value_uint8);
3718 EXPORT_SYMBOL(nvpair_value_int16);
3719 EXPORT_SYMBOL(nvpair_value_uint16);
3720 EXPORT_SYMBOL(nvpair_value_int32);
3721 EXPORT_SYMBOL(nvpair_value_uint32);
3722 EXPORT_SYMBOL(nvpair_value_int64);
3723 EXPORT_SYMBOL(nvpair_value_uint64);
3724 EXPORT_SYMBOL(nvpair_value_string);
3725 EXPORT_SYMBOL(nvpair_value_nvlist);
3726 EXPORT_SYMBOL(nvpair_value_boolean_array);
3727 EXPORT_SYMBOL(nvpair_value_byte_array);
3728 EXPORT_SYMBOL(nvpair_value_int8_array);
3729 EXPORT_SYMBOL(nvpair_value_uint8_array);
3730 EXPORT_SYMBOL(nvpair_value_int16_array);
3731 EXPORT_SYMBOL(nvpair_value_uint16_array);
3732 EXPORT_SYMBOL(nvpair_value_int32_array);
3733 EXPORT_SYMBOL(nvpair_value_uint32_array);
3734 EXPORT_SYMBOL(nvpair_value_int64_array);
3735 EXPORT_SYMBOL(nvpair_value_uint64_array);
3736 EXPORT_SYMBOL(nvpair_value_string_array);
3737 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3738 EXPORT_SYMBOL(nvpair_value_hrtime);