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.
28 #include <sys/debug.h>
29 #include <sys/isa_defs.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/strings.h>
39 #include <sys/sunddi.h>
40 #include <sys/sysmacros.h>
47 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
50 * nvpair.c - Provides kernel & userland interfaces for manipulating
65 * +--------------+ last i_nvp in list
66 * | nvpriv_t | +--------------------->
68 * +--+- nvp_list | | +------------+
69 * | | nvp_last -+--+ + nv_alloc_t |
70 * | | nvp_curr | |------------|
71 * | | nvp_nva -+----> | nva_ops |
72 * | | nvp_stat | | nva_arg |
73 * | +--------------+ +------------+
77 * +---------------------+ +-------------------+
78 * | i_nvp_t | +-->| i_nvp_t | +-->
79 * |---------------------| | |-------------------| |
80 * | nvi_next -+--+ | nvi_next -+--+
81 * | nvi_prev (NULL) | <----+ nvi_prev |
82 * | . . . . . . . . . . | | . . . . . . . . . |
83 * | nvp (nvpair_t) | | nvp (nvpair_t) |
84 * | - nvp_size | | - nvp_size |
85 * | - nvp_name_sz | | - nvp_name_sz |
86 * | - nvp_value_elem | | - nvp_value_elem |
87 * | - nvp_type | | - nvp_type |
88 * | - data ... | | - data ... |
89 * +---------------------+ +-------------------+
93 * +---------------------+ +---------------------+
94 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
95 * |---------------------| | | |---------------------|
96 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
97 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
98 * | . . . . . . . . . | | . . . . . . . . . |
99 * | nvp (nvpair_t) | | nvp (nvpair_t) |
100 * | - nvp_size | | - nvp_size |
101 * | - nvp_name_sz | | - nvp_name_sz |
102 * | - nvp_value_elem | | - nvp_value_elem |
103 * | - DATA_TYPE_NVLIST | | - nvp_type |
104 * | - data (embedded) | | - data ... |
105 * | nvlist name | +---------------------+
106 * | +--------------+ |
108 * | |--------------| |
109 * | | nvl_version | |
111 * | | nvl_priv --+---+---->
114 * | +--------------+ |
115 * +---------------------+
118 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
119 * allow value to be aligned on 8 byte boundary
121 * name_len is the length of the name string including the null terminator
124 #define NVP_SIZE_CALC(name_len, data_len) \
125 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
127 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
128 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
129 uint_t nelem, const void *data);
131 #define NV_STAT_EMBEDDED 0x1
132 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
133 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
135 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
136 #define NVPAIR2I_NVP(nvp) \
137 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
140 int nvpair_max_recursion = 20;
142 int nvpair_max_recursion = 100;
145 uint64_t nvlist_hashtable_init_size = (1 << 4);
148 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
156 va_start(valist, nvo);
157 if (nva->nva_ops->nv_ao_init != NULL)
158 err = nva->nva_ops->nv_ao_init(nva, valist);
165 nv_alloc_reset(nv_alloc_t *nva)
167 if (nva->nva_ops->nv_ao_reset != NULL)
168 nva->nva_ops->nv_ao_reset(nva);
172 nv_alloc_fini(nv_alloc_t *nva)
174 if (nva->nva_ops->nv_ao_fini != NULL)
175 nva->nva_ops->nv_ao_fini(nva);
179 nvlist_lookup_nv_alloc(nvlist_t *nvl)
184 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
187 return (priv->nvp_nva);
191 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
193 nv_alloc_t *nva = nvp->nvp_nva;
196 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
203 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
205 nv_alloc_t *nva = nvp->nvp_nva;
207 nva->nva_ops->nv_ao_free(nva, buf, size);
211 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
213 bzero(priv, sizeof (nvpriv_t));
216 priv->nvp_stat = stat;
220 nv_priv_alloc(nv_alloc_t *nva)
225 * nv_mem_alloc() cannot called here because it needs the priv
228 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
231 nv_priv_init(priv, nva, 0);
237 * Embedded lists need their own nvpriv_t's. We create a new
238 * nvpriv_t using the parameters and allocator from the parent
242 nv_priv_alloc_embedded(nvpriv_t *priv)
246 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
249 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
255 nvt_tab_alloc(nvpriv_t *priv, uint64_t buckets)
257 ASSERT3P(priv->nvp_hashtable, ==, NULL);
258 ASSERT0(priv->nvp_nbuckets);
259 ASSERT0(priv->nvp_nentries);
261 i_nvp_t **tab = nv_mem_zalloc(priv, buckets * sizeof (i_nvp_t *));
265 priv->nvp_hashtable = tab;
266 priv->nvp_nbuckets = buckets;
271 nvt_tab_free(nvpriv_t *priv)
273 i_nvp_t **tab = priv->nvp_hashtable;
275 ASSERT0(priv->nvp_nbuckets);
276 ASSERT0(priv->nvp_nentries);
280 nv_mem_free(priv, tab, priv->nvp_nbuckets * sizeof (i_nvp_t *));
282 priv->nvp_hashtable = NULL;
283 priv->nvp_nbuckets = 0;
284 priv->nvp_nentries = 0;
288 nvt_hash(const char *p)
290 uint32_t g, hval = 0;
293 hval = (hval << 4) + *p++;
294 if ((g = (hval & 0xf0000000)) != 0)
302 nvt_nvpair_match(nvpair_t *nvp1, nvpair_t *nvp2, uint32_t nvflag)
304 boolean_t match = B_FALSE;
305 if (nvflag & NV_UNIQUE_NAME_TYPE) {
306 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0 &&
307 NVP_TYPE(nvp1) == NVP_TYPE(nvp2))
310 ASSERT(nvflag == 0 || nvflag & NV_UNIQUE_NAME);
311 if (strcmp(NVP_NAME(nvp1), NVP_NAME(nvp2)) == 0)
318 nvt_lookup_name_type(nvlist_t *nvl, const char *name, data_type_t type)
320 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
321 ASSERT(priv != NULL);
323 i_nvp_t **tab = priv->nvp_hashtable;
326 ASSERT3P(priv->nvp_list, ==, NULL);
327 ASSERT0(priv->nvp_nbuckets);
328 ASSERT0(priv->nvp_nentries);
331 ASSERT(priv->nvp_nbuckets != 0);
334 uint64_t hash = nvt_hash(name);
335 uint64_t index = hash & (priv->nvp_nbuckets - 1);
337 ASSERT3U(index, <, priv->nvp_nbuckets);
338 i_nvp_t *entry = tab[index];
340 for (i_nvp_t *e = entry; e != NULL; e = e->nvi_hashtable_next) {
341 if (strcmp(NVP_NAME(&e->nvi_nvp), name) == 0 &&
342 (type == DATA_TYPE_DONTCARE ||
343 NVP_TYPE(&e->nvi_nvp) == type))
344 return (&e->nvi_nvp);
350 nvt_lookup_name(nvlist_t *nvl, const char *name)
352 return (nvt_lookup_name_type(nvl, name, DATA_TYPE_DONTCARE));
356 nvt_resize(nvpriv_t *priv, uint32_t new_size)
358 i_nvp_t **tab = priv->nvp_hashtable;
361 * Migrate all the entries from the current table
362 * to a newly-allocated table with the new size by
363 * re-adjusting the pointers of their entries.
365 uint32_t size = priv->nvp_nbuckets;
366 uint32_t new_mask = new_size - 1;
367 ASSERT(ISP2(new_size));
369 i_nvp_t **new_tab = nv_mem_zalloc(priv, new_size * sizeof (i_nvp_t *));
373 uint32_t nentries = 0;
374 for (uint32_t i = 0; i < size; i++) {
375 i_nvp_t *next, *e = tab[i];
378 next = e->nvi_hashtable_next;
380 uint32_t hash = nvt_hash(NVP_NAME(&e->nvi_nvp));
381 uint32_t index = hash & new_mask;
383 e->nvi_hashtable_next = new_tab[index];
391 ASSERT3U(nentries, ==, priv->nvp_nentries);
395 priv->nvp_hashtable = new_tab;
396 priv->nvp_nbuckets = new_size;
397 priv->nvp_nentries = nentries;
403 nvt_needs_togrow(nvpriv_t *priv)
406 * Grow only when we have more elements than buckets
407 * and the # of buckets doesn't overflow.
409 return (priv->nvp_nentries > priv->nvp_nbuckets &&
410 (UINT32_MAX >> 1) >= priv->nvp_nbuckets);
414 * Allocate a new table that's twice the size of the old one,
415 * and migrate all the entries from the old one to the new
416 * one by re-adjusting their pointers.
419 nvt_grow(nvpriv_t *priv)
421 uint32_t current_size = priv->nvp_nbuckets;
422 /* ensure we won't overflow */
423 ASSERT3U(UINT32_MAX >> 1, >=, current_size);
424 return (nvt_resize(priv, current_size << 1));
428 nvt_needs_toshrink(nvpriv_t *priv)
431 * Shrink only when the # of elements is less than or
432 * equal to 1/4 the # of buckets. Never shrink less than
433 * nvlist_hashtable_init_size.
435 ASSERT3U(priv->nvp_nbuckets, >=, nvlist_hashtable_init_size);
436 if (priv->nvp_nbuckets == nvlist_hashtable_init_size)
438 return (priv->nvp_nentries <= (priv->nvp_nbuckets >> 2));
442 * Allocate a new table that's half the size of the old one,
443 * and migrate all the entries from the old one to the new
444 * one by re-adjusting their pointers.
447 nvt_shrink(nvpriv_t *priv)
449 uint32_t current_size = priv->nvp_nbuckets;
450 /* ensure we won't overflow */
451 ASSERT3U(current_size, >=, nvlist_hashtable_init_size);
452 return (nvt_resize(priv, current_size >> 1));
456 nvt_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
458 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
460 if (nvt_needs_toshrink(priv)) {
461 int err = nvt_shrink(priv);
465 i_nvp_t **tab = priv->nvp_hashtable;
467 char *name = NVP_NAME(nvp);
468 uint64_t hash = nvt_hash(name);
469 uint64_t index = hash & (priv->nvp_nbuckets - 1);
471 ASSERT3U(index, <, priv->nvp_nbuckets);
472 i_nvp_t *bucket = tab[index];
474 for (i_nvp_t *prev = NULL, *e = bucket;
475 e != NULL; prev = e, e = e->nvi_hashtable_next) {
476 if (nvt_nvpair_match(&e->nvi_nvp, nvp, nvl->nvl_nvflag)) {
478 prev->nvi_hashtable_next =
479 e->nvi_hashtable_next;
481 ASSERT3P(e, ==, bucket);
482 tab[index] = e->nvi_hashtable_next;
484 e->nvi_hashtable_next = NULL;
485 priv->nvp_nentries--;
494 nvt_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
496 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
498 /* initialize nvpair table now if it doesn't exist. */
499 if (priv->nvp_hashtable == NULL) {
500 int err = nvt_tab_alloc(priv, nvlist_hashtable_init_size);
506 * if we don't allow duplicate entries, make sure to
507 * unlink any existing entries from the table.
509 if (nvl->nvl_nvflag != 0) {
510 int err = nvt_remove_nvpair(nvl, nvp);
515 if (nvt_needs_togrow(priv)) {
516 int err = nvt_grow(priv);
520 i_nvp_t **tab = priv->nvp_hashtable;
522 char *name = NVP_NAME(nvp);
523 uint64_t hash = nvt_hash(name);
524 uint64_t index = hash & (priv->nvp_nbuckets - 1);
526 ASSERT3U(index, <, priv->nvp_nbuckets);
527 i_nvp_t *bucket = tab[index];
529 /* insert link at the beginning of the bucket */
530 i_nvp_t *new_entry = NVPAIR2I_NVP(nvp);
531 ASSERT3P(new_entry->nvi_hashtable_next, ==, NULL);
532 new_entry->nvi_hashtable_next = bucket;
533 tab[index] = new_entry;
535 priv->nvp_nentries++;
540 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
542 nvl->nvl_version = NV_VERSION;
543 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
544 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
550 nvlist_nvflag(nvlist_t *nvl)
552 return (nvl->nvl_nvflag);
556 nvlist_nv_alloc(int kmflag)
561 return (nv_alloc_sleep);
563 return (nv_alloc_nosleep);
565 return (nv_alloc_pushpage);
568 return (nv_alloc_nosleep);
573 * nvlist_alloc - Allocate nvlist.
576 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
578 return (nvlist_xalloc(nvlp, nvflag, nvlist_nv_alloc(kmflag)));
582 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
586 if (nvlp == NULL || nva == NULL)
589 if ((priv = nv_priv_alloc(nva)) == NULL)
592 if ((*nvlp = nv_mem_zalloc(priv,
593 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
594 nv_mem_free(priv, priv, sizeof (nvpriv_t));
598 nvlist_init(*nvlp, nvflag, priv);
604 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
607 nvp_buf_alloc(nvlist_t *nvl, size_t len)
609 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
615 * Allocate the buffer
617 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
619 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
629 * nvp_buf_free - de-Allocate an i_nvp_t.
632 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
634 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
635 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
637 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
641 * nvp_buf_link - link a new nv pair into the nvlist.
644 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
646 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
647 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
649 /* Put element at end of nvlist */
650 if (priv->nvp_list == NULL) {
651 priv->nvp_list = priv->nvp_last = curr;
653 curr->nvi_prev = priv->nvp_last;
654 priv->nvp_last->nvi_next = curr;
655 priv->nvp_last = curr;
660 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
663 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
665 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
666 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
669 * protect nvlist_next_nvpair() against walking on freed memory.
671 if (priv->nvp_curr == curr)
672 priv->nvp_curr = curr->nvi_next;
674 if (curr == priv->nvp_list)
675 priv->nvp_list = curr->nvi_next;
677 curr->nvi_prev->nvi_next = curr->nvi_next;
679 if (curr == priv->nvp_last)
680 priv->nvp_last = curr->nvi_prev;
682 curr->nvi_next->nvi_prev = curr->nvi_prev;
686 * take a nvpair type and number of elements and make sure the are valid
689 i_validate_type_nelem(data_type_t type, uint_t nelem)
692 case DATA_TYPE_BOOLEAN:
696 case DATA_TYPE_BOOLEAN_VALUE:
699 case DATA_TYPE_UINT8:
700 case DATA_TYPE_INT16:
701 case DATA_TYPE_UINT16:
702 case DATA_TYPE_INT32:
703 case DATA_TYPE_UINT32:
704 case DATA_TYPE_INT64:
705 case DATA_TYPE_UINT64:
706 case DATA_TYPE_STRING:
707 case DATA_TYPE_HRTIME:
708 case DATA_TYPE_NVLIST:
709 #if !defined(_KERNEL)
710 case DATA_TYPE_DOUBLE:
715 case DATA_TYPE_BOOLEAN_ARRAY:
716 case DATA_TYPE_BYTE_ARRAY:
717 case DATA_TYPE_INT8_ARRAY:
718 case DATA_TYPE_UINT8_ARRAY:
719 case DATA_TYPE_INT16_ARRAY:
720 case DATA_TYPE_UINT16_ARRAY:
721 case DATA_TYPE_INT32_ARRAY:
722 case DATA_TYPE_UINT32_ARRAY:
723 case DATA_TYPE_INT64_ARRAY:
724 case DATA_TYPE_UINT64_ARRAY:
725 case DATA_TYPE_STRING_ARRAY:
726 case DATA_TYPE_NVLIST_ARRAY:
727 /* we allow arrays with 0 elements */
736 * Verify nvp_name_sz and check the name string length.
739 i_validate_nvpair_name(nvpair_t *nvp)
741 if ((nvp->nvp_name_sz <= 0) ||
742 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
745 /* verify the name string, make sure its terminated */
746 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
749 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
753 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
756 case DATA_TYPE_BOOLEAN_VALUE:
757 if (*(boolean_t *)data != B_TRUE &&
758 *(boolean_t *)data != B_FALSE)
761 case DATA_TYPE_BOOLEAN_ARRAY: {
764 for (i = 0; i < nelem; i++)
765 if (((boolean_t *)data)[i] != B_TRUE &&
766 ((boolean_t *)data)[i] != B_FALSE)
778 * This function takes a pointer to what should be a nvpair and it's size
779 * and then verifies that all the nvpair fields make sense and can be
780 * trusted. This function is used when decoding packed nvpairs.
783 i_validate_nvpair(nvpair_t *nvp)
785 data_type_t type = NVP_TYPE(nvp);
788 /* verify nvp_name_sz, check the name string length */
789 if (i_validate_nvpair_name(nvp) != 0)
792 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
796 * verify nvp_type, nvp_value_elem, and also possibly
797 * verify string values and get the value size.
799 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
800 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
801 if (size2 < 0 || size1 != NV_ALIGN(size2))
808 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
813 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
816 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
817 nvpair_t *nvp = &curr->nvi_nvp;
820 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
821 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
829 * Frees all memory allocated for an nvpair (like embedded lists) with
830 * the exception of the nvpair buffer itself.
833 nvpair_free(nvpair_t *nvp)
835 switch (NVP_TYPE(nvp)) {
836 case DATA_TYPE_NVLIST:
837 nvlist_free(EMBEDDED_NVL(nvp));
839 case DATA_TYPE_NVLIST_ARRAY: {
840 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
843 for (i = 0; i < NVP_NELEM(nvp); i++)
845 nvlist_free(nvlp[i]);
854 * nvlist_free - free an unpacked nvlist
857 nvlist_free(nvlist_t *nvl)
863 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
867 * Unpacked nvlist are linked through i_nvp_t
869 curr = priv->nvp_list;
870 while (curr != NULL) {
871 nvpair_t *nvp = &curr->nvi_nvp;
872 curr = curr->nvi_next;
875 nvp_buf_free(nvl, nvp);
878 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
879 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
884 nv_mem_free(priv, priv, sizeof (nvpriv_t));
888 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
890 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
896 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
897 if (&curr->nvi_nvp == nvp)
904 * Make a copy of nvlist
907 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
909 return (nvlist_xdup(nvl, nvlp, nvlist_nv_alloc(kmflag)));
913 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
918 if (nvl == NULL || nvlp == NULL)
921 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
924 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
933 * Remove all with matching name
936 nvlist_remove_all(nvlist_t *nvl, const char *name)
940 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
944 while ((nvp = nvt_lookup_name(nvl, name)) != NULL) {
945 VERIFY0(nvlist_remove_nvpair(nvl, nvp));
953 * Remove first one with matching name and type
956 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
958 if (nvl == NULL || name == NULL || nvl->nvl_priv == 0)
961 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
965 return (nvlist_remove_nvpair(nvl, nvp));
969 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
971 if (nvl == NULL || nvp == NULL)
974 int err = nvt_remove_nvpair(nvl, nvp);
978 nvp_buf_unlink(nvl, nvp);
980 nvp_buf_free(nvl, nvp);
985 * This function calculates the size of an nvpair value.
987 * The data argument controls the behavior in case of the data types
988 * DATA_TYPE_STRING and
989 * DATA_TYPE_STRING_ARRAY
990 * Is data == NULL then the size of the string(s) is excluded.
993 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
997 if (i_validate_type_nelem(type, nelem) != 0)
1000 /* Calculate required size for holding value */
1002 case DATA_TYPE_BOOLEAN:
1005 case DATA_TYPE_BOOLEAN_VALUE:
1006 value_sz = sizeof (boolean_t);
1008 case DATA_TYPE_BYTE:
1009 value_sz = sizeof (uchar_t);
1011 case DATA_TYPE_INT8:
1012 value_sz = sizeof (int8_t);
1014 case DATA_TYPE_UINT8:
1015 value_sz = sizeof (uint8_t);
1017 case DATA_TYPE_INT16:
1018 value_sz = sizeof (int16_t);
1020 case DATA_TYPE_UINT16:
1021 value_sz = sizeof (uint16_t);
1023 case DATA_TYPE_INT32:
1024 value_sz = sizeof (int32_t);
1026 case DATA_TYPE_UINT32:
1027 value_sz = sizeof (uint32_t);
1029 case DATA_TYPE_INT64:
1030 value_sz = sizeof (int64_t);
1032 case DATA_TYPE_UINT64:
1033 value_sz = sizeof (uint64_t);
1035 #if !defined(_KERNEL)
1036 case DATA_TYPE_DOUBLE:
1037 value_sz = sizeof (double);
1040 case DATA_TYPE_STRING:
1044 value_sz = strlen(data) + 1;
1046 case DATA_TYPE_BOOLEAN_ARRAY:
1047 value_sz = (uint64_t)nelem * sizeof (boolean_t);
1049 case DATA_TYPE_BYTE_ARRAY:
1050 value_sz = (uint64_t)nelem * sizeof (uchar_t);
1052 case DATA_TYPE_INT8_ARRAY:
1053 value_sz = (uint64_t)nelem * sizeof (int8_t);
1055 case DATA_TYPE_UINT8_ARRAY:
1056 value_sz = (uint64_t)nelem * sizeof (uint8_t);
1058 case DATA_TYPE_INT16_ARRAY:
1059 value_sz = (uint64_t)nelem * sizeof (int16_t);
1061 case DATA_TYPE_UINT16_ARRAY:
1062 value_sz = (uint64_t)nelem * sizeof (uint16_t);
1064 case DATA_TYPE_INT32_ARRAY:
1065 value_sz = (uint64_t)nelem * sizeof (int32_t);
1067 case DATA_TYPE_UINT32_ARRAY:
1068 value_sz = (uint64_t)nelem * sizeof (uint32_t);
1070 case DATA_TYPE_INT64_ARRAY:
1071 value_sz = (uint64_t)nelem * sizeof (int64_t);
1073 case DATA_TYPE_UINT64_ARRAY:
1074 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1076 case DATA_TYPE_STRING_ARRAY:
1077 value_sz = (uint64_t)nelem * sizeof (uint64_t);
1080 char *const *strs = data;
1083 /* no alignment requirement for strings */
1084 for (i = 0; i < nelem; i++) {
1085 if (strs[i] == NULL)
1087 value_sz += strlen(strs[i]) + 1;
1091 case DATA_TYPE_HRTIME:
1092 value_sz = sizeof (hrtime_t);
1094 case DATA_TYPE_NVLIST:
1095 value_sz = NV_ALIGN(sizeof (nvlist_t));
1097 case DATA_TYPE_NVLIST_ARRAY:
1098 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
1099 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
1105 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
1109 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
1114 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
1115 nvl->nvl_priv)) == NULL)
1118 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
1120 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
1121 nvlist_free(emb_nvl);
1122 emb_nvl->nvl_priv = 0;
1129 * nvlist_add_common - Add new <name,value> pair to nvlist
1132 nvlist_add_common(nvlist_t *nvl, const char *name,
1133 data_type_t type, uint_t nelem, const void *data)
1138 int nvp_sz, name_sz, value_sz;
1141 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1144 if (nelem != 0 && data == NULL)
1148 * Verify type and nelem and get the value size.
1149 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1150 * is the size of the string(s) included.
1152 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
1155 if (i_validate_nvpair_value(type, nelem, data) != 0)
1159 * If we're adding an nvlist or nvlist array, ensure that we are not
1160 * adding the input nvlist to itself, which would cause recursion,
1161 * and ensure that no NULL nvlist pointers are present.
1164 case DATA_TYPE_NVLIST:
1165 if (data == nvl || data == NULL)
1168 case DATA_TYPE_NVLIST_ARRAY: {
1169 nvlist_t **onvlp = (nvlist_t **)data;
1170 for (i = 0; i < nelem; i++) {
1171 if (onvlp[i] == nvl || onvlp[i] == NULL)
1180 /* calculate sizes of the nvpair elements and the nvpair itself */
1181 name_sz = strlen(name) + 1;
1182 if (name_sz >= 1ULL << (sizeof (nvp->nvp_name_sz) * NBBY - 1))
1185 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
1187 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
1190 ASSERT(nvp->nvp_size == nvp_sz);
1191 nvp->nvp_name_sz = name_sz;
1192 nvp->nvp_value_elem = nelem;
1193 nvp->nvp_type = type;
1194 bcopy(name, NVP_NAME(nvp), name_sz);
1197 case DATA_TYPE_BOOLEAN:
1199 case DATA_TYPE_STRING_ARRAY: {
1200 char *const *strs = data;
1201 char *buf = NVP_VALUE(nvp);
1202 char **cstrs = (void *)buf;
1204 /* skip pre-allocated space for pointer array */
1205 buf += nelem * sizeof (uint64_t);
1206 for (i = 0; i < nelem; i++) {
1207 int slen = strlen(strs[i]) + 1;
1208 bcopy(strs[i], buf, slen);
1214 case DATA_TYPE_NVLIST: {
1215 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
1216 nvlist_t *onvl = (nvlist_t *)data;
1218 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
1219 nvp_buf_free(nvl, nvp);
1224 case DATA_TYPE_NVLIST_ARRAY: {
1225 nvlist_t **onvlp = (nvlist_t **)data;
1226 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1227 nvlist_t *embedded = (nvlist_t *)
1228 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
1230 for (i = 0; i < nelem; i++) {
1231 if ((err = nvlist_copy_embedded(nvl,
1232 onvlp[i], embedded)) != 0) {
1234 * Free any successfully created lists
1237 nvp_buf_free(nvl, nvp);
1241 nvlp[i] = embedded++;
1246 bcopy(data, NVP_VALUE(nvp), value_sz);
1249 /* if unique name, remove before add */
1250 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
1251 (void) nvlist_remove_all(nvl, name);
1252 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
1253 (void) nvlist_remove(nvl, name, type);
1255 err = nvt_add_nvpair(nvl, nvp);
1258 nvp_buf_free(nvl, nvp);
1261 nvp_buf_link(nvl, nvp);
1267 nvlist_add_boolean(nvlist_t *nvl, const char *name)
1269 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1273 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1275 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1279 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1281 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1285 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1287 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1291 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1293 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1297 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1299 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1303 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1305 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1309 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1311 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1315 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1317 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1321 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1323 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1327 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1329 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1332 #if !defined(_KERNEL)
1334 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1336 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1341 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1343 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1347 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1348 boolean_t *a, uint_t n)
1350 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1354 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1356 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1360 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1362 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1366 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1368 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1372 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1374 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1378 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1380 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1384 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1386 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1390 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1392 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1396 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1398 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1402 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1404 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1408 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1409 char *const *a, uint_t n)
1411 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1415 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1417 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1421 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1423 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1427 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1429 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1432 /* reading name-value pairs */
1434 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1440 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1443 curr = NVPAIR2I_NVP(nvp);
1446 * Ensure that nvp is a valid nvpair on this nvlist.
1447 * NB: nvp_curr is used only as a hint so that we don't always
1448 * have to walk the list to determine if nvp is still on the list.
1451 curr = priv->nvp_list;
1452 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1453 curr = curr->nvi_next;
1457 priv->nvp_curr = curr;
1459 return (curr != NULL ? &curr->nvi_nvp : NULL);
1463 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1469 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1472 curr = NVPAIR2I_NVP(nvp);
1475 curr = priv->nvp_last;
1476 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1477 curr = curr->nvi_prev;
1481 priv->nvp_curr = curr;
1483 return (curr != NULL ? &curr->nvi_nvp : NULL);
1487 nvlist_empty(nvlist_t *nvl)
1492 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1495 return (priv->nvp_list == NULL);
1499 nvpair_name(nvpair_t *nvp)
1501 return (NVP_NAME(nvp));
1505 nvpair_type(nvpair_t *nvp)
1507 return (NVP_TYPE(nvp));
1511 nvpair_type_is_array(nvpair_t *nvp)
1513 data_type_t type = NVP_TYPE(nvp);
1515 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1516 (type == DATA_TYPE_INT8_ARRAY) ||
1517 (type == DATA_TYPE_UINT8_ARRAY) ||
1518 (type == DATA_TYPE_INT16_ARRAY) ||
1519 (type == DATA_TYPE_UINT16_ARRAY) ||
1520 (type == DATA_TYPE_INT32_ARRAY) ||
1521 (type == DATA_TYPE_UINT32_ARRAY) ||
1522 (type == DATA_TYPE_INT64_ARRAY) ||
1523 (type == DATA_TYPE_UINT64_ARRAY) ||
1524 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1525 (type == DATA_TYPE_STRING_ARRAY) ||
1526 (type == DATA_TYPE_NVLIST_ARRAY))
1533 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1537 if (nvp == NULL || nvpair_type(nvp) != type)
1541 * For non-array types, we copy the data.
1542 * For array types (including string), we set a pointer.
1545 case DATA_TYPE_BOOLEAN:
1550 case DATA_TYPE_BOOLEAN_VALUE:
1551 case DATA_TYPE_BYTE:
1552 case DATA_TYPE_INT8:
1553 case DATA_TYPE_UINT8:
1554 case DATA_TYPE_INT16:
1555 case DATA_TYPE_UINT16:
1556 case DATA_TYPE_INT32:
1557 case DATA_TYPE_UINT32:
1558 case DATA_TYPE_INT64:
1559 case DATA_TYPE_UINT64:
1560 case DATA_TYPE_HRTIME:
1561 #if !defined(_KERNEL)
1562 case DATA_TYPE_DOUBLE:
1566 if ((value_sz = i_get_value_size(type, NULL, 1)) < 0)
1568 bcopy(NVP_VALUE(nvp), data, (size_t)value_sz);
1573 case DATA_TYPE_NVLIST:
1574 case DATA_TYPE_STRING:
1577 *(void **)data = (void *)NVP_VALUE(nvp);
1582 case DATA_TYPE_BOOLEAN_ARRAY:
1583 case DATA_TYPE_BYTE_ARRAY:
1584 case DATA_TYPE_INT8_ARRAY:
1585 case DATA_TYPE_UINT8_ARRAY:
1586 case DATA_TYPE_INT16_ARRAY:
1587 case DATA_TYPE_UINT16_ARRAY:
1588 case DATA_TYPE_INT32_ARRAY:
1589 case DATA_TYPE_UINT32_ARRAY:
1590 case DATA_TYPE_INT64_ARRAY:
1591 case DATA_TYPE_UINT64_ARRAY:
1592 case DATA_TYPE_STRING_ARRAY:
1593 case DATA_TYPE_NVLIST_ARRAY:
1594 if (nelem == NULL || data == NULL)
1596 if ((*nelem = NVP_NELEM(nvp)) != 0)
1597 *(void **)data = (void *)NVP_VALUE(nvp);
1599 *(void **)data = NULL;
1610 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1611 uint_t *nelem, void *data)
1613 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
1616 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1619 nvpair_t *nvp = nvt_lookup_name_type(nvl, name, type);
1623 return (nvpair_value_common(nvp, type, nelem, data));
1627 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1629 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1633 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1635 return (nvlist_lookup_common(nvl, name,
1636 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1640 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1642 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1646 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1648 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1652 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1654 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1658 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1660 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1664 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1666 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1670 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1672 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1676 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1678 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1682 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1684 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1688 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1690 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1693 #if !defined(_KERNEL)
1695 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1697 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1702 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1704 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1708 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1710 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1714 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1715 boolean_t **a, uint_t *n)
1717 return (nvlist_lookup_common(nvl, name,
1718 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1722 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1723 uchar_t **a, uint_t *n)
1725 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1729 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1731 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1735 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1736 uint8_t **a, uint_t *n)
1738 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1742 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1743 int16_t **a, uint_t *n)
1745 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1749 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1750 uint16_t **a, uint_t *n)
1752 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1756 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1757 int32_t **a, uint_t *n)
1759 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1763 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1764 uint32_t **a, uint_t *n)
1766 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1770 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1771 int64_t **a, uint_t *n)
1773 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1777 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1778 uint64_t **a, uint_t *n)
1780 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1784 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1785 char ***a, uint_t *n)
1787 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1791 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1792 nvlist_t ***a, uint_t *n)
1794 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1798 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1800 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1804 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1808 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1812 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1817 switch (type = va_arg(ap, data_type_t)) {
1818 case DATA_TYPE_BOOLEAN:
1819 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1822 case DATA_TYPE_BOOLEAN_VALUE:
1823 case DATA_TYPE_BYTE:
1824 case DATA_TYPE_INT8:
1825 case DATA_TYPE_UINT8:
1826 case DATA_TYPE_INT16:
1827 case DATA_TYPE_UINT16:
1828 case DATA_TYPE_INT32:
1829 case DATA_TYPE_UINT32:
1830 case DATA_TYPE_INT64:
1831 case DATA_TYPE_UINT64:
1832 case DATA_TYPE_HRTIME:
1833 case DATA_TYPE_STRING:
1834 case DATA_TYPE_NVLIST:
1835 #if !defined(_KERNEL)
1836 case DATA_TYPE_DOUBLE:
1838 val = va_arg(ap, void *);
1839 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1842 case DATA_TYPE_BYTE_ARRAY:
1843 case DATA_TYPE_BOOLEAN_ARRAY:
1844 case DATA_TYPE_INT8_ARRAY:
1845 case DATA_TYPE_UINT8_ARRAY:
1846 case DATA_TYPE_INT16_ARRAY:
1847 case DATA_TYPE_UINT16_ARRAY:
1848 case DATA_TYPE_INT32_ARRAY:
1849 case DATA_TYPE_UINT32_ARRAY:
1850 case DATA_TYPE_INT64_ARRAY:
1851 case DATA_TYPE_UINT64_ARRAY:
1852 case DATA_TYPE_STRING_ARRAY:
1853 case DATA_TYPE_NVLIST_ARRAY:
1854 val = va_arg(ap, void *);
1855 nelem = va_arg(ap, uint_t *);
1856 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1863 if (ret == ENOENT && noentok)
1872 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1873 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1874 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1875 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1876 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1877 * "a.d[3].e[1]". This matches the C syntax for array embed (for convenience,
1878 * code also supports "a.d[3]e[1]" syntax).
1880 * If 'ip' is non-NULL and the last name component is an array, return the
1881 * value of the "...[index]" array index in *ip. For an array reference that
1882 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1883 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1884 * inside the 'name' string where the syntax error was detected.
1887 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1888 nvpair_t **ret, int *ip, char **ep)
1899 *ip = -1; /* not indexed */
1903 if ((nvl == NULL) || (name == NULL))
1908 /* step through components of name */
1909 for (np = name; np && *np; np = sepp) {
1910 /* ensure unique names */
1911 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1914 /* skip white space */
1915 skip_whitespace(np);
1919 /* set 'sepp' to end of current component 'np' */
1921 sepp = strchr(np, sep);
1925 /* find start of next "[ index ]..." */
1926 idxp = strchr(np, '[');
1928 /* if sepp comes first, set idxp to NULL */
1929 if (sepp && idxp && (sepp < idxp))
1933 * At this point 'idxp' is set if there is an index
1934 * expected for the current component.
1937 /* set 'n' to length of current 'np' name component */
1940 /* keep sepp up to date for *ep use as we advance */
1941 skip_whitespace(idxp);
1944 /* determine the index value */
1945 #if defined(_KERNEL)
1946 if (ddi_strtol(idxp, &idxep, 0, &idx))
1949 idx = strtol(idxp, &idxep, 0);
1954 /* keep sepp up to date for *ep use as we advance */
1957 /* skip white space index value and check for ']' */
1958 skip_whitespace(sepp);
1962 /* for embedded arrays, support C syntax: "a[1].b" */
1963 skip_whitespace(sepp);
1964 if (sep && (*sepp == sep))
1972 /* trim trailing whitespace by reducing length of 'np' */
1975 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1979 /* skip whitespace, and set sepp to NULL if complete */
1981 skip_whitespace(sepp);
1988 * o 'n' is the length of current 'np' component.
1989 * o 'idxp' is set if there was an index, and value 'idx'.
1990 * o 'sepp' is set to the beginning of the next component,
1991 * and set to NULL if we have no more components.
1993 * Search for nvpair with matching component name.
1995 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1996 nvp = nvlist_next_nvpair(nvl, nvp)) {
1998 /* continue if no match on name */
1999 if (strncmp(np, nvpair_name(nvp), n) ||
2000 (strlen(nvpair_name(nvp)) != n))
2003 /* if indexed, verify type is array oriented */
2004 if (idxp && !nvpair_type_is_array(nvp))
2008 * Full match found, return nvp and idx if this
2009 * was the last component.
2015 *ip = (int)idx; /* return index */
2016 return (0); /* found */
2020 * More components: current match must be
2021 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2022 * to support going deeper.
2024 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
2025 nvl = EMBEDDED_NVL(nvp);
2027 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
2028 (void) nvpair_value_nvlist_array(nvp,
2029 &nva, (uint_t *)&n);
2030 if ((n < 0) || (idx >= n))
2036 /* type does not support more levels */
2040 goto fail; /* 'name' not found */
2042 /* search for match of next component in embedded 'nvl' list */
2045 fail: if (ep && sepp)
2051 * Return pointer to nvpair with specified 'name'.
2054 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
2056 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
2060 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2061 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2064 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
2065 const char *name, nvpair_t **ret, int *ip, char **ep)
2067 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
2071 nvlist_exists(nvlist_t *nvl, const char *name)
2077 if (name == NULL || nvl == NULL ||
2078 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2081 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2082 nvp = &curr->nvi_nvp;
2084 if (strcmp(name, NVP_NAME(nvp)) == 0)
2092 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
2094 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
2098 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
2100 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
2104 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
2106 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
2110 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
2112 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
2116 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
2118 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
2122 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
2124 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
2128 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
2130 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
2134 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
2136 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
2140 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
2142 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
2146 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
2148 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
2151 #if !defined(_KERNEL)
2153 nvpair_value_double(nvpair_t *nvp, double *val)
2155 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
2160 nvpair_value_string(nvpair_t *nvp, char **val)
2162 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
2166 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
2168 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
2172 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
2174 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
2178 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
2180 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
2184 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
2186 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
2190 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
2192 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
2196 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
2198 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
2202 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
2204 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
2208 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
2210 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
2214 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
2216 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
2220 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
2222 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
2226 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
2228 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
2232 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
2234 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
2238 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
2240 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
2244 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
2246 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
2250 * Add specified pair to the list.
2253 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
2255 if (nvl == NULL || nvp == NULL)
2258 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
2259 NVP_NELEM(nvp), NVP_VALUE(nvp)));
2263 * Merge the supplied nvlists and put the result in dst.
2264 * The merged list will contain all names specified in both lists,
2265 * the values are taken from nvl in the case of duplicates.
2266 * Return 0 on success.
2270 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2272 if (nvl == NULL || dst == NULL)
2276 return (nvlist_copy_pairs(nvl, dst));
2282 * Encoding related routines
2284 #define NVS_OP_ENCODE 0
2285 #define NVS_OP_DECODE 1
2286 #define NVS_OP_GETSIZE 2
2288 typedef struct nvs_ops nvs_ops_t;
2292 const nvs_ops_t *nvs_ops;
2299 * nvs operations are:
2301 * encoding / decoding of an nvlist header (nvlist_t)
2302 * calculates the size used for header and end detection
2305 * responsible for the first part of encoding / decoding of an nvpair
2306 * calculates the decoded size of an nvpair
2309 * second part of encoding / decoding of an nvpair
2312 * calculates the encoding size of an nvpair
2315 * encodes the end detection mark (zeros).
2318 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2319 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2320 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2321 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2322 int (*nvs_nvl_fini)(nvstream_t *);
2326 char nvh_encoding; /* nvs encoding method */
2327 char nvh_endian; /* nvs endian */
2328 char nvh_reserved1; /* reserved for future use */
2329 char nvh_reserved2; /* reserved for future use */
2333 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2335 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2339 * Walk nvpair in list and encode each nvpair
2341 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2342 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2345 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2349 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2356 * Get decoded size of next pair in stream, alloc
2357 * memory for nvpair_t, then decode the nvpair
2359 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2360 if (nvsize == 0) /* end of list */
2363 /* make sure len makes sense */
2364 if (nvsize < NVP_SIZE_CALC(1, 0))
2367 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2370 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2371 nvp_buf_free(nvl, nvp);
2375 if (i_validate_nvpair(nvp) != 0) {
2377 nvp_buf_free(nvl, nvp);
2381 err = nvt_add_nvpair(nvl, nvp);
2384 nvp_buf_free(nvl, nvp);
2387 nvp_buf_link(nvl, nvp);
2393 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2395 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2397 uint64_t nvsize = *buflen;
2401 * Get encoded size of nvpairs in nvlist
2403 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2404 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2407 if ((nvsize += size) > INT32_MAX)
2416 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2420 if (nvl->nvl_priv == 0)
2424 * Perform the operation, starting with header, then each nvpair
2426 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2429 switch (nvs->nvs_op) {
2431 err = nvs_encode_pairs(nvs, nvl);
2435 err = nvs_decode_pairs(nvs, nvl);
2438 case NVS_OP_GETSIZE:
2439 err = nvs_getsize_pairs(nvs, nvl, buflen);
2450 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2452 switch (nvs->nvs_op) {
2453 case NVS_OP_ENCODE: {
2456 if (nvs->nvs_recursion >= nvpair_max_recursion)
2458 nvs->nvs_recursion++;
2459 err = nvs_operation(nvs, embedded, NULL);
2460 nvs->nvs_recursion--;
2463 case NVS_OP_DECODE: {
2467 if (embedded->nvl_version != NV_VERSION)
2470 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2473 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2475 if (nvs->nvs_recursion >= nvpair_max_recursion) {
2476 nvlist_free(embedded);
2479 nvs->nvs_recursion++;
2480 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2481 nvlist_free(embedded);
2482 nvs->nvs_recursion--;
2493 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2495 size_t nelem = NVP_NELEM(nvp);
2496 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2499 switch (nvs->nvs_op) {
2501 for (i = 0; i < nelem; i++)
2502 if (nvs_embedded(nvs, nvlp[i]) != 0)
2506 case NVS_OP_DECODE: {
2507 size_t len = nelem * sizeof (uint64_t);
2508 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2510 bzero(nvlp, len); /* don't trust packed data */
2511 for (i = 0; i < nelem; i++) {
2512 if (nvs_embedded(nvs, embedded) != 0) {
2517 nvlp[i] = embedded++;
2521 case NVS_OP_GETSIZE: {
2522 uint64_t nvsize = 0;
2524 for (i = 0; i < nelem; i++) {
2527 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2530 if ((nvsize += nvp_sz) > INT32_MAX)
2544 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2545 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2548 * Common routine for nvlist operations:
2549 * encode, decode, getsize (encoded size).
2552 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2558 #if defined(_ZFS_LITTLE_ENDIAN)
2559 int host_endian = 1;
2560 #elif defined(_ZFS_BIG_ENDIAN)
2561 int host_endian = 0;
2563 #error "No endian defined!"
2564 #endif /* _ZFS_LITTLE_ENDIAN */
2567 if (buflen == NULL || nvl == NULL ||
2568 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2571 nvs.nvs_op = nvs_op;
2572 nvs.nvs_recursion = 0;
2575 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2576 * a buffer is allocated. The first 4 bytes in the buffer are
2577 * used for encoding method and host endian.
2581 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2585 nvh->nvh_encoding = encoding;
2586 nvh->nvh_endian = nvl_endian = host_endian;
2587 nvh->nvh_reserved1 = 0;
2588 nvh->nvh_reserved2 = 0;
2592 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2595 /* get method of encoding from first byte */
2597 encoding = nvh->nvh_encoding;
2598 nvl_endian = nvh->nvh_endian;
2601 case NVS_OP_GETSIZE:
2602 nvl_endian = host_endian;
2605 * add the size for encoding
2607 *buflen = sizeof (nvs_header_t);
2615 * Create an nvstream with proper encoding method
2618 case NV_ENCODE_NATIVE:
2620 * check endianness, in case we are unpacking
2623 if (nvl_endian != host_endian)
2625 err = nvs_native(&nvs, nvl, buf, buflen);
2628 err = nvs_xdr(&nvs, nvl, buf, buflen);
2639 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2641 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2645 * Pack nvlist into contiguous memory
2648 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2651 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2652 nvlist_nv_alloc(kmflag)));
2656 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2664 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2668 return (nvlist_common(nvl, *bufp, buflen, encoding,
2672 * Here is a difficult situation:
2673 * 1. The nvlist has fixed allocator properties.
2674 * All other nvlist routines (like nvlist_add_*, ...) use
2676 * 2. When using nvlist_pack() the user can specify their own
2677 * allocator properties (e.g. by using KM_NOSLEEP).
2679 * We use the user specified properties (2). A clearer solution
2680 * will be to remove the kmflag from nvlist_pack(), but we will
2681 * not change the interface.
2683 nv_priv_init(&nvpriv, nva, 0);
2685 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2688 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2691 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2692 NVS_OP_ENCODE)) != 0) {
2693 nv_mem_free(&nvpriv, buf, alloc_size);
2695 *buflen = alloc_size;
2703 * Unpack buf into an nvlist_t
2706 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2708 return (nvlist_xunpack(buf, buflen, nvlp, nvlist_nv_alloc(kmflag)));
2712 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2720 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2723 if ((err = nvlist_common(nvl, buf, &buflen, NV_ENCODE_NATIVE,
2724 NVS_OP_DECODE)) != 0)
2733 * Native encoding functions
2737 * This structure is used when decoding a packed nvpair in
2738 * the native format. n_base points to a buffer containing the
2739 * packed nvpair. n_end is a pointer to the end of the buffer.
2740 * (n_end actually points to the first byte past the end of the
2741 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2742 * It points to the current data that we are decoding.
2743 * The amount of data left in the buffer is equal to n_end - n_curr.
2744 * n_flag is used to recognize a packed embedded list.
2753 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2756 switch (nvs->nvs_op) {
2759 nvs->nvs_private = native;
2760 native->n_curr = native->n_base = buf;
2761 native->n_end = buf + buflen;
2765 case NVS_OP_GETSIZE:
2766 nvs->nvs_private = native;
2767 native->n_curr = native->n_base = native->n_end = NULL;
2777 nvs_native_destroy(nvstream_t *nvs)
2782 native_cp(nvstream_t *nvs, void *buf, size_t size)
2784 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2786 if (native->n_curr + size > native->n_end)
2790 * The bcopy() below eliminates alignment requirement
2791 * on the buffer (stream) and is preferred over direct access.
2793 switch (nvs->nvs_op) {
2795 bcopy(buf, native->n_curr, size);
2798 bcopy(native->n_curr, buf, size);
2804 native->n_curr += size;
2809 * operate on nvlist_t header
2812 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2814 nvs_native_t *native = nvs->nvs_private;
2816 switch (nvs->nvs_op) {
2820 return (0); /* packed embedded list */
2824 /* copy version and nvflag of the nvlist_t */
2825 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2826 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2831 case NVS_OP_GETSIZE:
2833 * if calculate for packed embedded list
2834 * 4 for end of the embedded list
2836 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2837 * and 4 for end of the entire list
2839 if (native->n_flag) {
2843 *size += 2 * sizeof (int32_t) + 4;
2854 nvs_native_nvl_fini(nvstream_t *nvs)
2856 if (nvs->nvs_op == NVS_OP_ENCODE) {
2857 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2859 * Add 4 zero bytes at end of nvlist. They are used
2860 * for end detection by the decode routine.
2862 if (native->n_curr + sizeof (int) > native->n_end)
2865 bzero(native->n_curr, sizeof (int));
2866 native->n_curr += sizeof (int);
2873 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2875 if (nvs->nvs_op == NVS_OP_ENCODE) {
2876 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2877 nvlist_t *packed = (void *)
2878 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2880 * Null out the pointer that is meaningless in the packed
2881 * structure. The address may not be aligned, so we have
2884 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2888 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2892 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2894 if (nvs->nvs_op == NVS_OP_ENCODE) {
2895 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2896 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2897 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2898 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2901 * Null out pointers that are meaningless in the packed
2902 * structure. The addresses may not be aligned, so we have
2907 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2909 * Null out the pointer that is meaningless in the
2910 * packed structure. The address may not be aligned,
2911 * so we have to use bzero.
2913 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2917 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2921 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2923 switch (nvs->nvs_op) {
2924 case NVS_OP_ENCODE: {
2925 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2926 uint64_t *strp = (void *)
2927 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2929 * Null out pointers that are meaningless in the packed
2930 * structure. The addresses may not be aligned, so we have
2933 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2936 case NVS_OP_DECODE: {
2937 char **strp = (void *)NVP_VALUE(nvp);
2938 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2941 for (i = 0; i < NVP_NELEM(nvp); i++) {
2943 buf += strlen(buf) + 1;
2951 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2958 * We do the initial bcopy of the data before we look at
2959 * the nvpair type, because when we're decoding, we won't
2960 * have the correct values for the pair until we do the bcopy.
2962 switch (nvs->nvs_op) {
2965 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2972 /* verify nvp_name_sz, check the name string length */
2973 if (i_validate_nvpair_name(nvp) != 0)
2976 type = NVP_TYPE(nvp);
2979 * Verify type and nelem and get the value size.
2980 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2981 * is the size of the string(s) excluded.
2983 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2986 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2990 case DATA_TYPE_NVLIST:
2991 ret = nvpair_native_embedded(nvs, nvp);
2993 case DATA_TYPE_NVLIST_ARRAY:
2994 ret = nvpair_native_embedded_array(nvs, nvp);
2996 case DATA_TYPE_STRING_ARRAY:
2997 nvpair_native_string_array(nvs, nvp);
3007 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3009 uint64_t nvp_sz = nvp->nvp_size;
3011 switch (NVP_TYPE(nvp)) {
3012 case DATA_TYPE_NVLIST: {
3015 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
3021 case DATA_TYPE_NVLIST_ARRAY: {
3024 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
3034 if (nvp_sz > INT32_MAX)
3043 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3045 switch (nvs->nvs_op) {
3047 return (nvs_native_nvp_op(nvs, nvp));
3049 case NVS_OP_DECODE: {
3050 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
3053 /* try to read the size value from the stream */
3054 if (native->n_curr + sizeof (int32_t) > native->n_end)
3056 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
3058 /* sanity check the size value */
3059 if (decode_len < 0 ||
3060 decode_len > native->n_end - native->n_curr)
3066 * If at the end of the stream then move the cursor
3067 * forward, otherwise nvpair_native_op() will read
3068 * the entire nvpair at the same cursor position.
3071 native->n_curr += sizeof (int32_t);
3082 static const nvs_ops_t nvs_native_ops = {
3083 .nvs_nvlist = nvs_native_nvlist,
3084 .nvs_nvpair = nvs_native_nvpair,
3085 .nvs_nvp_op = nvs_native_nvp_op,
3086 .nvs_nvp_size = nvs_native_nvp_size,
3087 .nvs_nvl_fini = nvs_native_nvl_fini
3091 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3093 nvs_native_t native;
3096 nvs->nvs_ops = &nvs_native_ops;
3098 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
3099 *buflen - sizeof (nvs_header_t))) != 0)
3102 err = nvs_operation(nvs, nvl, buflen);
3104 nvs_native_destroy(nvs);
3110 * XDR encoding functions
3112 * An xdr packed nvlist is encoded as:
3114 * - encoding method and host endian (4 bytes)
3115 * - nvl_version (4 bytes)
3116 * - nvl_nvflag (4 bytes)
3118 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3119 * - encoded size of the nvpair (4 bytes)
3120 * - decoded size of the nvpair (4 bytes)
3121 * - name string, (4 + sizeof(NV_ALIGN4(string))
3122 * a string is coded as size (4 bytes) and data
3123 * - data type (4 bytes)
3124 * - number of elements in the nvpair (4 bytes)
3127 * - 2 zero's for end of the entire list (8 bytes)
3130 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
3132 /* xdr data must be 4 byte aligned */
3133 if ((ulong_t)buf % 4 != 0)
3136 switch (nvs->nvs_op) {
3138 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
3139 nvs->nvs_private = xdr;
3142 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
3143 nvs->nvs_private = xdr;
3145 case NVS_OP_GETSIZE:
3146 nvs->nvs_private = NULL;
3154 nvs_xdr_destroy(nvstream_t *nvs)
3156 switch (nvs->nvs_op) {
3159 xdr_destroy((XDR *)nvs->nvs_private);
3167 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
3169 switch (nvs->nvs_op) {
3171 case NVS_OP_DECODE: {
3172 XDR *xdr = nvs->nvs_private;
3174 if (!xdr_int(xdr, &nvl->nvl_version) ||
3175 !xdr_u_int(xdr, &nvl->nvl_nvflag))
3179 case NVS_OP_GETSIZE: {
3181 * 2 * 4 for nvl_version + nvl_nvflag
3182 * and 8 for end of the entire list
3194 nvs_xdr_nvl_fini(nvstream_t *nvs)
3196 if (nvs->nvs_op == NVS_OP_ENCODE) {
3197 XDR *xdr = nvs->nvs_private;
3200 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
3208 * The format of xdr encoded nvpair is:
3209 * encode_size, decode_size, name string, data type, nelem, data
3212 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
3216 char *buf_end = (char *)nvp + nvp->nvp_size;
3218 uint_t nelem, buflen;
3220 XDR *xdr = nvs->nvs_private;
3222 ASSERT(xdr != NULL && nvp != NULL);
3225 if ((buf = NVP_NAME(nvp)) >= buf_end)
3227 buflen = buf_end - buf;
3229 if (!xdr_string(xdr, &buf, buflen - 1))
3231 nvp->nvp_name_sz = strlen(buf) + 1;
3233 /* type and nelem */
3234 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
3235 !xdr_int(xdr, &nvp->nvp_value_elem))
3238 type = NVP_TYPE(nvp);
3239 nelem = nvp->nvp_value_elem;
3242 * Verify type and nelem and get the value size.
3243 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3244 * is the size of the string(s) excluded.
3246 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
3249 /* if there is no data to extract then return */
3254 if ((buf = NVP_VALUE(nvp)) >= buf_end)
3256 buflen = buf_end - buf;
3258 if (buflen < value_sz)
3262 case DATA_TYPE_NVLIST:
3263 if (nvs_embedded(nvs, (void *)buf) == 0)
3267 case DATA_TYPE_NVLIST_ARRAY:
3268 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
3272 case DATA_TYPE_BOOLEAN:
3276 case DATA_TYPE_BYTE:
3277 case DATA_TYPE_INT8:
3278 case DATA_TYPE_UINT8:
3279 ret = xdr_char(xdr, buf);
3282 case DATA_TYPE_INT16:
3283 ret = xdr_short(xdr, (void *)buf);
3286 case DATA_TYPE_UINT16:
3287 ret = xdr_u_short(xdr, (void *)buf);
3290 case DATA_TYPE_BOOLEAN_VALUE:
3291 case DATA_TYPE_INT32:
3292 ret = xdr_int(xdr, (void *)buf);
3295 case DATA_TYPE_UINT32:
3296 ret = xdr_u_int(xdr, (void *)buf);
3299 case DATA_TYPE_INT64:
3300 ret = xdr_longlong_t(xdr, (void *)buf);
3303 case DATA_TYPE_UINT64:
3304 ret = xdr_u_longlong_t(xdr, (void *)buf);
3307 case DATA_TYPE_HRTIME:
3309 * NOTE: must expose the definition of hrtime_t here
3311 ret = xdr_longlong_t(xdr, (void *)buf);
3313 #if !defined(_KERNEL)
3314 case DATA_TYPE_DOUBLE:
3315 ret = xdr_double(xdr, (void *)buf);
3318 case DATA_TYPE_STRING:
3319 ret = xdr_string(xdr, &buf, buflen - 1);
3322 case DATA_TYPE_BYTE_ARRAY:
3323 ret = xdr_opaque(xdr, buf, nelem);
3326 case DATA_TYPE_INT8_ARRAY:
3327 case DATA_TYPE_UINT8_ARRAY:
3328 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3329 (xdrproc_t)xdr_char);
3332 case DATA_TYPE_INT16_ARRAY:
3333 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3334 sizeof (int16_t), (xdrproc_t)xdr_short);
3337 case DATA_TYPE_UINT16_ARRAY:
3338 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3339 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3342 case DATA_TYPE_BOOLEAN_ARRAY:
3343 case DATA_TYPE_INT32_ARRAY:
3344 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3345 sizeof (int32_t), (xdrproc_t)xdr_int);
3348 case DATA_TYPE_UINT32_ARRAY:
3349 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3350 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3353 case DATA_TYPE_INT64_ARRAY:
3354 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3355 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3358 case DATA_TYPE_UINT64_ARRAY:
3359 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3360 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3363 case DATA_TYPE_STRING_ARRAY: {
3364 size_t len = nelem * sizeof (uint64_t);
3365 char **strp = (void *)buf;
3368 if (nvs->nvs_op == NVS_OP_DECODE)
3369 bzero(buf, len); /* don't trust packed data */
3371 for (i = 0; i < nelem; i++) {
3378 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3381 if (nvs->nvs_op == NVS_OP_DECODE)
3383 len = strlen(buf) + 1;
3392 return (ret == TRUE ? 0 : EFAULT);
3396 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3398 data_type_t type = NVP_TYPE(nvp);
3400 * encode_size + decode_size + name string size + data type + nelem
3401 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3403 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3406 case DATA_TYPE_BOOLEAN:
3409 case DATA_TYPE_BOOLEAN_VALUE:
3410 case DATA_TYPE_BYTE:
3411 case DATA_TYPE_INT8:
3412 case DATA_TYPE_UINT8:
3413 case DATA_TYPE_INT16:
3414 case DATA_TYPE_UINT16:
3415 case DATA_TYPE_INT32:
3416 case DATA_TYPE_UINT32:
3417 nvp_sz += 4; /* 4 is the minimum xdr unit */
3420 case DATA_TYPE_INT64:
3421 case DATA_TYPE_UINT64:
3422 case DATA_TYPE_HRTIME:
3423 #if !defined(_KERNEL)
3424 case DATA_TYPE_DOUBLE:
3429 case DATA_TYPE_STRING:
3430 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3433 case DATA_TYPE_BYTE_ARRAY:
3434 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3437 case DATA_TYPE_BOOLEAN_ARRAY:
3438 case DATA_TYPE_INT8_ARRAY:
3439 case DATA_TYPE_UINT8_ARRAY:
3440 case DATA_TYPE_INT16_ARRAY:
3441 case DATA_TYPE_UINT16_ARRAY:
3442 case DATA_TYPE_INT32_ARRAY:
3443 case DATA_TYPE_UINT32_ARRAY:
3444 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3447 case DATA_TYPE_INT64_ARRAY:
3448 case DATA_TYPE_UINT64_ARRAY:
3449 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3452 case DATA_TYPE_STRING_ARRAY: {
3454 char **strs = (void *)NVP_VALUE(nvp);
3456 for (i = 0; i < NVP_NELEM(nvp); i++)
3457 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3462 case DATA_TYPE_NVLIST:
3463 case DATA_TYPE_NVLIST_ARRAY: {
3465 int old_nvs_op = nvs->nvs_op;
3468 nvs->nvs_op = NVS_OP_GETSIZE;
3469 if (type == DATA_TYPE_NVLIST)
3470 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3472 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3473 nvs->nvs_op = old_nvs_op;
3486 if (nvp_sz > INT32_MAX)
3496 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3497 * the largest nvpair that could be encoded in the buffer.
3499 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3500 * The size of a xdr packed nvpair without any data is 5 words.
3502 * Using the size of the data directly as an estimate would be ok
3503 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3504 * then the actual nvpair has space for an array of pointers to index
3505 * the strings. These pointers are not encoded into the packed xdr buffer.
3507 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3508 * of length 0, then each string is encoded in xdr format as a single word.
3509 * Therefore when expanded to an nvpair there will be 2.25 word used for
3510 * each string. (a int64_t allocated for pointer usage, and a single char
3511 * for the null termination.)
3513 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3515 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3516 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3517 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3518 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3519 (NVS_XDR_DATA_LEN(x) * 2) + \
3520 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3523 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3525 XDR *xdr = nvs->nvs_private;
3526 int32_t encode_len, decode_len;
3528 switch (nvs->nvs_op) {
3529 case NVS_OP_ENCODE: {
3532 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3535 decode_len = nvp->nvp_size;
3536 encode_len = nvsize;
3537 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3540 return (nvs_xdr_nvp_op(nvs, nvp));
3542 case NVS_OP_DECODE: {
3543 struct xdr_bytesrec bytesrec;
3545 /* get the encode and decode size */
3546 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3550 /* are we at the end of the stream? */
3554 /* sanity check the size parameter */
3555 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3558 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3569 static const struct nvs_ops nvs_xdr_ops = {
3570 .nvs_nvlist = nvs_xdr_nvlist,
3571 .nvs_nvpair = nvs_xdr_nvpair,
3572 .nvs_nvp_op = nvs_xdr_nvp_op,
3573 .nvs_nvp_size = nvs_xdr_nvp_size,
3574 .nvs_nvl_fini = nvs_xdr_nvl_fini
3578 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3583 nvs->nvs_ops = &nvs_xdr_ops;
3585 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3586 *buflen - sizeof (nvs_header_t))) != 0)
3589 err = nvs_operation(nvs, nvl, buflen);
3591 nvs_xdr_destroy(nvs);
3596 #if defined(_KERNEL)
3608 module_init(nvpair_init);
3609 module_exit(nvpair_fini);
3612 ZFS_MODULE_DESCRIPTION("Generic name/value pair implementation");
3613 ZFS_MODULE_AUTHOR(ZFS_META_AUTHOR);
3614 ZFS_MODULE_LICENSE(ZFS_META_LICENSE);
3615 ZFS_MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
3617 EXPORT_SYMBOL(nv_alloc_init);
3618 EXPORT_SYMBOL(nv_alloc_reset);
3619 EXPORT_SYMBOL(nv_alloc_fini);
3621 /* list management */
3622 EXPORT_SYMBOL(nvlist_alloc);
3623 EXPORT_SYMBOL(nvlist_free);
3624 EXPORT_SYMBOL(nvlist_size);
3625 EXPORT_SYMBOL(nvlist_pack);
3626 EXPORT_SYMBOL(nvlist_unpack);
3627 EXPORT_SYMBOL(nvlist_dup);
3628 EXPORT_SYMBOL(nvlist_merge);
3630 EXPORT_SYMBOL(nvlist_xalloc);
3631 EXPORT_SYMBOL(nvlist_xpack);
3632 EXPORT_SYMBOL(nvlist_xunpack);
3633 EXPORT_SYMBOL(nvlist_xdup);
3634 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3636 EXPORT_SYMBOL(nvlist_add_nvpair);
3637 EXPORT_SYMBOL(nvlist_add_boolean);
3638 EXPORT_SYMBOL(nvlist_add_boolean_value);
3639 EXPORT_SYMBOL(nvlist_add_byte);
3640 EXPORT_SYMBOL(nvlist_add_int8);
3641 EXPORT_SYMBOL(nvlist_add_uint8);
3642 EXPORT_SYMBOL(nvlist_add_int16);
3643 EXPORT_SYMBOL(nvlist_add_uint16);
3644 EXPORT_SYMBOL(nvlist_add_int32);
3645 EXPORT_SYMBOL(nvlist_add_uint32);
3646 EXPORT_SYMBOL(nvlist_add_int64);
3647 EXPORT_SYMBOL(nvlist_add_uint64);
3648 EXPORT_SYMBOL(nvlist_add_string);
3649 EXPORT_SYMBOL(nvlist_add_nvlist);
3650 EXPORT_SYMBOL(nvlist_add_boolean_array);
3651 EXPORT_SYMBOL(nvlist_add_byte_array);
3652 EXPORT_SYMBOL(nvlist_add_int8_array);
3653 EXPORT_SYMBOL(nvlist_add_uint8_array);
3654 EXPORT_SYMBOL(nvlist_add_int16_array);
3655 EXPORT_SYMBOL(nvlist_add_uint16_array);
3656 EXPORT_SYMBOL(nvlist_add_int32_array);
3657 EXPORT_SYMBOL(nvlist_add_uint32_array);
3658 EXPORT_SYMBOL(nvlist_add_int64_array);
3659 EXPORT_SYMBOL(nvlist_add_uint64_array);
3660 EXPORT_SYMBOL(nvlist_add_string_array);
3661 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3662 EXPORT_SYMBOL(nvlist_next_nvpair);
3663 EXPORT_SYMBOL(nvlist_prev_nvpair);
3664 EXPORT_SYMBOL(nvlist_empty);
3665 EXPORT_SYMBOL(nvlist_add_hrtime);
3667 EXPORT_SYMBOL(nvlist_remove);
3668 EXPORT_SYMBOL(nvlist_remove_nvpair);
3669 EXPORT_SYMBOL(nvlist_remove_all);
3671 EXPORT_SYMBOL(nvlist_lookup_boolean);
3672 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3673 EXPORT_SYMBOL(nvlist_lookup_byte);
3674 EXPORT_SYMBOL(nvlist_lookup_int8);
3675 EXPORT_SYMBOL(nvlist_lookup_uint8);
3676 EXPORT_SYMBOL(nvlist_lookup_int16);
3677 EXPORT_SYMBOL(nvlist_lookup_uint16);
3678 EXPORT_SYMBOL(nvlist_lookup_int32);
3679 EXPORT_SYMBOL(nvlist_lookup_uint32);
3680 EXPORT_SYMBOL(nvlist_lookup_int64);
3681 EXPORT_SYMBOL(nvlist_lookup_uint64);
3682 EXPORT_SYMBOL(nvlist_lookup_string);
3683 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3684 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3685 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3686 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3687 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3688 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3689 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3690 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3691 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3692 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3693 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3694 EXPORT_SYMBOL(nvlist_lookup_string_array);
3695 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3696 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3697 EXPORT_SYMBOL(nvlist_lookup_pairs);
3699 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3700 EXPORT_SYMBOL(nvlist_exists);
3702 /* processing nvpair */
3703 EXPORT_SYMBOL(nvpair_name);
3704 EXPORT_SYMBOL(nvpair_type);
3705 EXPORT_SYMBOL(nvpair_value_boolean_value);
3706 EXPORT_SYMBOL(nvpair_value_byte);
3707 EXPORT_SYMBOL(nvpair_value_int8);
3708 EXPORT_SYMBOL(nvpair_value_uint8);
3709 EXPORT_SYMBOL(nvpair_value_int16);
3710 EXPORT_SYMBOL(nvpair_value_uint16);
3711 EXPORT_SYMBOL(nvpair_value_int32);
3712 EXPORT_SYMBOL(nvpair_value_uint32);
3713 EXPORT_SYMBOL(nvpair_value_int64);
3714 EXPORT_SYMBOL(nvpair_value_uint64);
3715 EXPORT_SYMBOL(nvpair_value_string);
3716 EXPORT_SYMBOL(nvpair_value_nvlist);
3717 EXPORT_SYMBOL(nvpair_value_boolean_array);
3718 EXPORT_SYMBOL(nvpair_value_byte_array);
3719 EXPORT_SYMBOL(nvpair_value_int8_array);
3720 EXPORT_SYMBOL(nvpair_value_uint8_array);
3721 EXPORT_SYMBOL(nvpair_value_int16_array);
3722 EXPORT_SYMBOL(nvpair_value_uint16_array);
3723 EXPORT_SYMBOL(nvpair_value_int32_array);
3724 EXPORT_SYMBOL(nvpair_value_uint32_array);
3725 EXPORT_SYMBOL(nvpair_value_int64_array);
3726 EXPORT_SYMBOL(nvpair_value_uint64_array);
3727 EXPORT_SYMBOL(nvpair_value_string_array);
3728 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3729 EXPORT_SYMBOL(nvpair_value_hrtime);