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.
26 #include <sys/stropts.h>
27 #include <sys/debug.h>
28 #include <sys/isa_defs.h>
29 #include <sys/int_limits.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <rpc/types.h>
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/varargs.h>
38 #include <sys/sunddi.h>
47 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
49 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
52 * nvpair.c - Provides kernel & userland interfaces for manipulating
67 * +--------------+ last i_nvp in list
68 * | nvpriv_t | +--------------------->
70 * +--+- nvp_list | | +------------+
71 * | | nvp_last -+--+ + nv_alloc_t |
72 * | | nvp_curr | |------------|
73 * | | nvp_nva -+----> | nva_ops |
74 * | | nvp_stat | | nva_arg |
75 * | +--------------+ +------------+
79 * +---------------------+ +-------------------+
80 * | i_nvp_t | +-->| i_nvp_t | +-->
81 * |---------------------| | |-------------------| |
82 * | nvi_next -+--+ | nvi_next -+--+
83 * | nvi_prev (NULL) | <----+ nvi_prev |
84 * | . . . . . . . . . . | | . . . . . . . . . |
85 * | nvp (nvpair_t) | | nvp (nvpair_t) |
86 * | - nvp_size | | - nvp_size |
87 * | - nvp_name_sz | | - nvp_name_sz |
88 * | - nvp_value_elem | | - nvp_value_elem |
89 * | - nvp_type | | - nvp_type |
90 * | - data ... | | - data ... |
91 * +---------------------+ +-------------------+
95 * +---------------------+ +---------------------+
96 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
97 * |---------------------| | | |---------------------|
98 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
99 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
100 * | . . . . . . . . . | | . . . . . . . . . |
101 * | nvp (nvpair_t) | | nvp (nvpair_t) |
102 * | - nvp_size | | - nvp_size |
103 * | - nvp_name_sz | | - nvp_name_sz |
104 * | - nvp_value_elem | | - nvp_value_elem |
105 * | - DATA_TYPE_NVLIST | | - nvp_type |
106 * | - data (embedded) | | - data ... |
107 * | nvlist name | +---------------------+
108 * | +--------------+ |
110 * | |--------------| |
111 * | | nvl_version | |
113 * | | nvl_priv --+---+---->
116 * | +--------------+ |
117 * +---------------------+
120 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
121 * allow value to be aligned on 8 byte boundary
123 * name_len is the length of the name string including the null terminator
126 #define NVP_SIZE_CALC(name_len, data_len) \
127 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
129 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
130 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
131 uint_t nelem, const void *data);
133 #define NV_STAT_EMBEDDED 0x1
134 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
135 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
137 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
138 #define NVPAIR2I_NVP(nvp) \
139 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
143 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
151 va_start(valist, nvo);
152 if (nva->nva_ops->nv_ao_init != NULL)
153 err = nva->nva_ops->nv_ao_init(nva, valist);
160 nv_alloc_reset(nv_alloc_t *nva)
162 if (nva->nva_ops->nv_ao_reset != NULL)
163 nva->nva_ops->nv_ao_reset(nva);
167 nv_alloc_fini(nv_alloc_t *nva)
169 if (nva->nva_ops->nv_ao_fini != NULL)
170 nva->nva_ops->nv_ao_fini(nva);
174 nvlist_lookup_nv_alloc(nvlist_t *nvl)
179 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
182 return (priv->nvp_nva);
186 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
188 nv_alloc_t *nva = nvp->nvp_nva;
191 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
198 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
200 nv_alloc_t *nva = nvp->nvp_nva;
202 nva->nva_ops->nv_ao_free(nva, buf, size);
206 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
208 bzero(priv, sizeof (nvpriv_t));
211 priv->nvp_stat = stat;
215 nv_priv_alloc(nv_alloc_t *nva)
220 * nv_mem_alloc() cannot called here because it needs the priv
223 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
226 nv_priv_init(priv, nva, 0);
232 * Embedded lists need their own nvpriv_t's. We create a new
233 * nvpriv_t using the parameters and allocator from the parent
237 nv_priv_alloc_embedded(nvpriv_t *priv)
241 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
244 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
250 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
252 nvl->nvl_version = NV_VERSION;
253 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
254 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
260 nvlist_nvflag(nvlist_t *nvl)
262 return (nvl->nvl_nvflag);
266 * nvlist_alloc - Allocate nvlist.
270 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
272 nv_alloc_t *nva = nv_alloc_nosleep;
274 #if defined(_KERNEL) && !defined(_BOOT)
277 nva = nv_alloc_sleep;
280 nva = nv_alloc_pushpage;
283 nva = nv_alloc_nosleep;
290 return (nvlist_xalloc(nvlp, nvflag, nva));
294 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
298 if (nvlp == NULL || nva == NULL)
301 if ((priv = nv_priv_alloc(nva)) == NULL)
304 if ((*nvlp = nv_mem_zalloc(priv,
305 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
306 nv_mem_free(priv, priv, sizeof (nvpriv_t));
310 nvlist_init(*nvlp, nvflag, priv);
316 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
319 nvp_buf_alloc(nvlist_t *nvl, size_t len)
321 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
327 * Allocate the buffer
329 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
331 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
341 * nvp_buf_free - de-Allocate an i_nvp_t.
344 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
346 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
347 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
349 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
353 * nvp_buf_link - link a new nv pair into the nvlist.
356 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
358 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
359 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
361 /* Put element at end of nvlist */
362 if (priv->nvp_list == NULL) {
363 priv->nvp_list = priv->nvp_last = curr;
365 curr->nvi_prev = priv->nvp_last;
366 priv->nvp_last->nvi_next = curr;
367 priv->nvp_last = curr;
372 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
375 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
377 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
378 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
381 * protect nvlist_next_nvpair() against walking on freed memory.
383 if (priv->nvp_curr == curr)
384 priv->nvp_curr = curr->nvi_next;
386 if (curr == priv->nvp_list)
387 priv->nvp_list = curr->nvi_next;
389 curr->nvi_prev->nvi_next = curr->nvi_next;
391 if (curr == priv->nvp_last)
392 priv->nvp_last = curr->nvi_prev;
394 curr->nvi_next->nvi_prev = curr->nvi_prev;
398 * take a nvpair type and number of elements and make sure the are valid
401 i_validate_type_nelem(data_type_t type, uint_t nelem)
404 case DATA_TYPE_BOOLEAN:
408 case DATA_TYPE_BOOLEAN_VALUE:
411 case DATA_TYPE_UINT8:
412 case DATA_TYPE_INT16:
413 case DATA_TYPE_UINT16:
414 case DATA_TYPE_INT32:
415 case DATA_TYPE_UINT32:
416 case DATA_TYPE_INT64:
417 case DATA_TYPE_UINT64:
418 case DATA_TYPE_STRING:
419 case DATA_TYPE_HRTIME:
420 case DATA_TYPE_NVLIST:
421 #if !defined(_KERNEL)
422 case DATA_TYPE_DOUBLE:
427 case DATA_TYPE_BOOLEAN_ARRAY:
428 case DATA_TYPE_BYTE_ARRAY:
429 case DATA_TYPE_INT8_ARRAY:
430 case DATA_TYPE_UINT8_ARRAY:
431 case DATA_TYPE_INT16_ARRAY:
432 case DATA_TYPE_UINT16_ARRAY:
433 case DATA_TYPE_INT32_ARRAY:
434 case DATA_TYPE_UINT32_ARRAY:
435 case DATA_TYPE_INT64_ARRAY:
436 case DATA_TYPE_UINT64_ARRAY:
437 case DATA_TYPE_STRING_ARRAY:
438 case DATA_TYPE_NVLIST_ARRAY:
439 /* we allow arrays with 0 elements */
448 * Verify nvp_name_sz and check the name string length.
451 i_validate_nvpair_name(nvpair_t *nvp)
453 if ((nvp->nvp_name_sz <= 0) ||
454 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
457 /* verify the name string, make sure its terminated */
458 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
461 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
465 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
468 case DATA_TYPE_BOOLEAN_VALUE:
469 if (*(boolean_t *)data != B_TRUE &&
470 *(boolean_t *)data != B_FALSE)
473 case DATA_TYPE_BOOLEAN_ARRAY: {
476 for (i = 0; i < nelem; i++)
477 if (((boolean_t *)data)[i] != B_TRUE &&
478 ((boolean_t *)data)[i] != B_FALSE)
490 * This function takes a pointer to what should be a nvpair and it's size
491 * and then verifies that all the nvpair fields make sense and can be
492 * trusted. This function is used when decoding packed nvpairs.
495 i_validate_nvpair(nvpair_t *nvp)
497 data_type_t type = NVP_TYPE(nvp);
500 /* verify nvp_name_sz, check the name string length */
501 if (i_validate_nvpair_name(nvp) != 0)
504 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
508 * verify nvp_type, nvp_value_elem, and also possibly
509 * verify string values and get the value size.
511 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
512 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
513 if (size2 < 0 || size1 != NV_ALIGN(size2))
520 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
525 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
528 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
529 nvpair_t *nvp = &curr->nvi_nvp;
532 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
533 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
541 * Frees all memory allocated for an nvpair (like embedded lists) with
542 * the exception of the nvpair buffer itself.
545 nvpair_free(nvpair_t *nvp)
547 switch (NVP_TYPE(nvp)) {
548 case DATA_TYPE_NVLIST:
549 nvlist_free(EMBEDDED_NVL(nvp));
551 case DATA_TYPE_NVLIST_ARRAY: {
552 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
555 for (i = 0; i < NVP_NELEM(nvp); i++)
557 nvlist_free(nvlp[i]);
566 * nvlist_free - free an unpacked nvlist
569 nvlist_free(nvlist_t *nvl)
575 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
579 * Unpacked nvlist are linked through i_nvp_t
581 curr = priv->nvp_list;
582 while (curr != NULL) {
583 nvpair_t *nvp = &curr->nvi_nvp;
584 curr = curr->nvi_next;
587 nvp_buf_free(nvl, nvp);
590 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
591 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
595 nv_mem_free(priv, priv, sizeof (nvpriv_t));
599 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
601 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
607 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
608 if (&curr->nvi_nvp == nvp)
615 * Make a copy of nvlist
619 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
621 #if defined(_KERNEL) && !defined(_BOOT)
622 return (nvlist_xdup(nvl, nvlp,
623 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
625 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
630 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
635 if (nvl == NULL || nvlp == NULL)
638 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
641 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
650 * Remove all with matching name
653 nvlist_remove_all(nvlist_t *nvl, const char *name)
659 if (nvl == NULL || name == NULL ||
660 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
663 curr = priv->nvp_list;
664 while (curr != NULL) {
665 nvpair_t *nvp = &curr->nvi_nvp;
667 curr = curr->nvi_next;
668 if (strcmp(name, NVP_NAME(nvp)) != 0)
671 nvp_buf_unlink(nvl, nvp);
673 nvp_buf_free(nvl, nvp);
682 * Remove first one with matching name and type
685 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
690 if (nvl == NULL || name == NULL ||
691 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
694 curr = priv->nvp_list;
695 while (curr != NULL) {
696 nvpair_t *nvp = &curr->nvi_nvp;
698 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
699 nvp_buf_unlink(nvl, nvp);
701 nvp_buf_free(nvl, nvp);
705 curr = curr->nvi_next;
712 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
714 if (nvl == NULL || nvp == NULL)
717 nvp_buf_unlink(nvl, nvp);
719 nvp_buf_free(nvl, nvp);
724 * This function calculates the size of an nvpair value.
726 * The data argument controls the behavior in case of the data types
727 * DATA_TYPE_STRING and
728 * DATA_TYPE_STRING_ARRAY
729 * Is data == NULL then the size of the string(s) is excluded.
732 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
736 if (i_validate_type_nelem(type, nelem) != 0)
739 /* Calculate required size for holding value */
741 case DATA_TYPE_BOOLEAN:
744 case DATA_TYPE_BOOLEAN_VALUE:
745 value_sz = sizeof (boolean_t);
748 value_sz = sizeof (uchar_t);
751 value_sz = sizeof (int8_t);
753 case DATA_TYPE_UINT8:
754 value_sz = sizeof (uint8_t);
756 case DATA_TYPE_INT16:
757 value_sz = sizeof (int16_t);
759 case DATA_TYPE_UINT16:
760 value_sz = sizeof (uint16_t);
762 case DATA_TYPE_INT32:
763 value_sz = sizeof (int32_t);
765 case DATA_TYPE_UINT32:
766 value_sz = sizeof (uint32_t);
768 case DATA_TYPE_INT64:
769 value_sz = sizeof (int64_t);
771 case DATA_TYPE_UINT64:
772 value_sz = sizeof (uint64_t);
774 #if !defined(_KERNEL)
775 case DATA_TYPE_DOUBLE:
776 value_sz = sizeof (double);
779 case DATA_TYPE_STRING:
783 value_sz = strlen(data) + 1;
785 case DATA_TYPE_BOOLEAN_ARRAY:
786 value_sz = (uint64_t)nelem * sizeof (boolean_t);
788 case DATA_TYPE_BYTE_ARRAY:
789 value_sz = (uint64_t)nelem * sizeof (uchar_t);
791 case DATA_TYPE_INT8_ARRAY:
792 value_sz = (uint64_t)nelem * sizeof (int8_t);
794 case DATA_TYPE_UINT8_ARRAY:
795 value_sz = (uint64_t)nelem * sizeof (uint8_t);
797 case DATA_TYPE_INT16_ARRAY:
798 value_sz = (uint64_t)nelem * sizeof (int16_t);
800 case DATA_TYPE_UINT16_ARRAY:
801 value_sz = (uint64_t)nelem * sizeof (uint16_t);
803 case DATA_TYPE_INT32_ARRAY:
804 value_sz = (uint64_t)nelem * sizeof (int32_t);
806 case DATA_TYPE_UINT32_ARRAY:
807 value_sz = (uint64_t)nelem * sizeof (uint32_t);
809 case DATA_TYPE_INT64_ARRAY:
810 value_sz = (uint64_t)nelem * sizeof (int64_t);
812 case DATA_TYPE_UINT64_ARRAY:
813 value_sz = (uint64_t)nelem * sizeof (uint64_t);
815 case DATA_TYPE_STRING_ARRAY:
816 value_sz = (uint64_t)nelem * sizeof (uint64_t);
819 char *const *strs = data;
822 /* no alignment requirement for strings */
823 for (i = 0; i < nelem; i++) {
826 value_sz += strlen(strs[i]) + 1;
830 case DATA_TYPE_HRTIME:
831 value_sz = sizeof (hrtime_t);
833 case DATA_TYPE_NVLIST:
834 value_sz = NV_ALIGN(sizeof (nvlist_t));
836 case DATA_TYPE_NVLIST_ARRAY:
837 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
838 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
844 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
848 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
853 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
854 nvl->nvl_priv)) == NULL)
857 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
859 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
860 nvlist_free(emb_nvl);
861 emb_nvl->nvl_priv = 0;
868 * nvlist_add_common - Add new <name,value> pair to nvlist
871 nvlist_add_common(nvlist_t *nvl, const char *name,
872 data_type_t type, uint_t nelem, const void *data)
877 int nvp_sz, name_sz, value_sz;
880 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
883 if (nelem != 0 && data == NULL)
887 * Verify type and nelem and get the value size.
888 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
889 * is the size of the string(s) included.
891 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
894 if (i_validate_nvpair_value(type, nelem, data) != 0)
898 * If we're adding an nvlist or nvlist array, ensure that we are not
899 * adding the input nvlist to itself, which would cause recursion,
900 * and ensure that no NULL nvlist pointers are present.
903 case DATA_TYPE_NVLIST:
904 if (data == nvl || data == NULL)
907 case DATA_TYPE_NVLIST_ARRAY: {
908 nvlist_t **onvlp = (nvlist_t **)data;
909 for (i = 0; i < nelem; i++) {
910 if (onvlp[i] == nvl || onvlp[i] == NULL)
919 /* calculate sizes of the nvpair elements and the nvpair itself */
920 name_sz = strlen(name) + 1;
922 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
924 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
927 ASSERT(nvp->nvp_size == nvp_sz);
928 nvp->nvp_name_sz = name_sz;
929 nvp->nvp_value_elem = nelem;
930 nvp->nvp_type = type;
931 bcopy(name, NVP_NAME(nvp), name_sz);
934 case DATA_TYPE_BOOLEAN:
936 case DATA_TYPE_STRING_ARRAY: {
937 char *const *strs = data;
938 char *buf = NVP_VALUE(nvp);
939 char **cstrs = (void *)buf;
941 /* skip pre-allocated space for pointer array */
942 buf += nelem * sizeof (uint64_t);
943 for (i = 0; i < nelem; i++) {
944 int slen = strlen(strs[i]) + 1;
945 bcopy(strs[i], buf, slen);
951 case DATA_TYPE_NVLIST: {
952 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
953 nvlist_t *onvl = (nvlist_t *)data;
955 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
956 nvp_buf_free(nvl, nvp);
961 case DATA_TYPE_NVLIST_ARRAY: {
962 nvlist_t **onvlp = (nvlist_t **)data;
963 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
964 nvlist_t *embedded = (nvlist_t *)
965 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
967 for (i = 0; i < nelem; i++) {
968 if ((err = nvlist_copy_embedded(nvl,
969 onvlp[i], embedded)) != 0) {
971 * Free any successfully created lists
974 nvp_buf_free(nvl, nvp);
978 nvlp[i] = embedded++;
983 bcopy(data, NVP_VALUE(nvp), value_sz);
986 /* if unique name, remove before add */
987 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
988 (void) nvlist_remove_all(nvl, name);
989 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
990 (void) nvlist_remove(nvl, name, type);
992 nvp_buf_link(nvl, nvp);
998 nvlist_add_boolean(nvlist_t *nvl, const char *name)
1000 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1004 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1006 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1010 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1012 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1016 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1018 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1022 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1024 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1028 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1030 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1034 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1036 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1040 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1042 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1046 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1048 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1052 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1054 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1058 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1060 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1063 #if !defined(_KERNEL)
1065 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1067 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1072 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1074 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1078 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1079 boolean_t *a, uint_t n)
1081 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1085 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1087 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1091 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1093 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1097 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1099 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1103 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1105 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1109 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1111 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1115 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1117 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1121 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1123 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1127 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1129 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1133 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1135 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1139 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1140 char *const *a, uint_t n)
1142 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1146 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1148 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1152 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1154 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1158 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1160 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1163 /* reading name-value pairs */
1165 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1171 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1174 curr = NVPAIR2I_NVP(nvp);
1177 * Ensure that nvp is a valid nvpair on this nvlist.
1178 * NB: nvp_curr is used only as a hint so that we don't always
1179 * have to walk the list to determine if nvp is still on the list.
1182 curr = priv->nvp_list;
1183 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1184 curr = curr->nvi_next;
1188 priv->nvp_curr = curr;
1190 return (curr != NULL ? &curr->nvi_nvp : NULL);
1194 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1200 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1203 curr = NVPAIR2I_NVP(nvp);
1206 curr = priv->nvp_last;
1207 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1208 curr = curr->nvi_prev;
1212 priv->nvp_curr = curr;
1214 return (curr != NULL ? &curr->nvi_nvp : NULL);
1218 nvlist_empty(nvlist_t *nvl)
1223 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1226 return (priv->nvp_list == NULL);
1230 nvpair_name(nvpair_t *nvp)
1232 return (NVP_NAME(nvp));
1236 nvpair_type(nvpair_t *nvp)
1238 return (NVP_TYPE(nvp));
1242 nvpair_type_is_array(nvpair_t *nvp)
1244 data_type_t type = NVP_TYPE(nvp);
1246 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1247 (type == DATA_TYPE_UINT8_ARRAY) ||
1248 (type == DATA_TYPE_INT16_ARRAY) ||
1249 (type == DATA_TYPE_UINT16_ARRAY) ||
1250 (type == DATA_TYPE_INT32_ARRAY) ||
1251 (type == DATA_TYPE_UINT32_ARRAY) ||
1252 (type == DATA_TYPE_INT64_ARRAY) ||
1253 (type == DATA_TYPE_UINT64_ARRAY) ||
1254 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1255 (type == DATA_TYPE_STRING_ARRAY) ||
1256 (type == DATA_TYPE_NVLIST_ARRAY))
1263 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1265 if (nvp == NULL || nvpair_type(nvp) != type)
1269 * For non-array types, we copy the data.
1270 * For array types (including string), we set a pointer.
1273 case DATA_TYPE_BOOLEAN:
1278 case DATA_TYPE_BOOLEAN_VALUE:
1279 case DATA_TYPE_BYTE:
1280 case DATA_TYPE_INT8:
1281 case DATA_TYPE_UINT8:
1282 case DATA_TYPE_INT16:
1283 case DATA_TYPE_UINT16:
1284 case DATA_TYPE_INT32:
1285 case DATA_TYPE_UINT32:
1286 case DATA_TYPE_INT64:
1287 case DATA_TYPE_UINT64:
1288 case DATA_TYPE_HRTIME:
1289 #if !defined(_KERNEL)
1290 case DATA_TYPE_DOUBLE:
1294 bcopy(NVP_VALUE(nvp), data,
1295 (size_t)i_get_value_size(type, NULL, 1));
1300 case DATA_TYPE_NVLIST:
1301 case DATA_TYPE_STRING:
1304 *(void **)data = (void *)NVP_VALUE(nvp);
1309 case DATA_TYPE_BOOLEAN_ARRAY:
1310 case DATA_TYPE_BYTE_ARRAY:
1311 case DATA_TYPE_INT8_ARRAY:
1312 case DATA_TYPE_UINT8_ARRAY:
1313 case DATA_TYPE_INT16_ARRAY:
1314 case DATA_TYPE_UINT16_ARRAY:
1315 case DATA_TYPE_INT32_ARRAY:
1316 case DATA_TYPE_UINT32_ARRAY:
1317 case DATA_TYPE_INT64_ARRAY:
1318 case DATA_TYPE_UINT64_ARRAY:
1319 case DATA_TYPE_STRING_ARRAY:
1320 case DATA_TYPE_NVLIST_ARRAY:
1321 if (nelem == NULL || data == NULL)
1323 if ((*nelem = NVP_NELEM(nvp)) != 0)
1324 *(void **)data = (void *)NVP_VALUE(nvp);
1326 *(void **)data = NULL;
1337 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1338 uint_t *nelem, void *data)
1344 if (name == NULL || nvl == NULL ||
1345 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1348 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1351 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1352 nvp = &curr->nvi_nvp;
1354 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1355 return (nvpair_value_common(nvp, type, nelem, data));
1362 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1364 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1368 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1370 return (nvlist_lookup_common(nvl, name,
1371 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1375 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1377 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1381 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1383 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1387 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1389 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1393 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1395 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1399 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1401 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1405 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1407 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1411 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1413 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1417 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1419 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1423 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1425 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1428 #if !defined(_KERNEL)
1430 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1432 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1437 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1439 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1443 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1445 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1449 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1450 boolean_t **a, uint_t *n)
1452 return (nvlist_lookup_common(nvl, name,
1453 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1457 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1458 uchar_t **a, uint_t *n)
1460 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1464 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1466 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1470 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1471 uint8_t **a, uint_t *n)
1473 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1477 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1478 int16_t **a, uint_t *n)
1480 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1484 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1485 uint16_t **a, uint_t *n)
1487 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1491 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1492 int32_t **a, uint_t *n)
1494 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1498 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1499 uint32_t **a, uint_t *n)
1501 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1505 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1506 int64_t **a, uint_t *n)
1508 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1512 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1513 uint64_t **a, uint_t *n)
1515 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1519 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1520 char ***a, uint_t *n)
1522 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1526 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1527 nvlist_t ***a, uint_t *n)
1529 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1533 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1535 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1539 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1543 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1547 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1552 switch (type = va_arg(ap, data_type_t)) {
1553 case DATA_TYPE_BOOLEAN:
1554 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1557 case DATA_TYPE_BOOLEAN_VALUE:
1558 case DATA_TYPE_BYTE:
1559 case DATA_TYPE_INT8:
1560 case DATA_TYPE_UINT8:
1561 case DATA_TYPE_INT16:
1562 case DATA_TYPE_UINT16:
1563 case DATA_TYPE_INT32:
1564 case DATA_TYPE_UINT32:
1565 case DATA_TYPE_INT64:
1566 case DATA_TYPE_UINT64:
1567 case DATA_TYPE_HRTIME:
1568 case DATA_TYPE_STRING:
1569 case DATA_TYPE_NVLIST:
1570 #if !defined(_KERNEL)
1571 case DATA_TYPE_DOUBLE:
1573 val = va_arg(ap, void *);
1574 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1577 case DATA_TYPE_BYTE_ARRAY:
1578 case DATA_TYPE_BOOLEAN_ARRAY:
1579 case DATA_TYPE_INT8_ARRAY:
1580 case DATA_TYPE_UINT8_ARRAY:
1581 case DATA_TYPE_INT16_ARRAY:
1582 case DATA_TYPE_UINT16_ARRAY:
1583 case DATA_TYPE_INT32_ARRAY:
1584 case DATA_TYPE_UINT32_ARRAY:
1585 case DATA_TYPE_INT64_ARRAY:
1586 case DATA_TYPE_UINT64_ARRAY:
1587 case DATA_TYPE_STRING_ARRAY:
1588 case DATA_TYPE_NVLIST_ARRAY:
1589 val = va_arg(ap, void *);
1590 nelem = va_arg(ap, uint_t *);
1591 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1598 if (ret == ENOENT && noentok)
1607 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1608 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1609 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1610 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1611 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1612 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1613 * code also supports "a.d[3]e[1]" syntax).
1615 * If 'ip' is non-NULL and the last name component is an array, return the
1616 * value of the "...[index]" array index in *ip. For an array reference that
1617 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1618 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1619 * inside the 'name' string where the syntax error was detected.
1622 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1623 nvpair_t **ret, int *ip, char **ep)
1634 *ip = -1; /* not indexed */
1638 if ((nvl == NULL) || (name == NULL))
1641 /* step through components of name */
1642 for (np = name; np && *np; np = sepp) {
1643 /* ensure unique names */
1644 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1647 /* skip white space */
1648 skip_whitespace(np);
1652 /* set 'sepp' to end of current component 'np' */
1654 sepp = strchr(np, sep);
1658 /* find start of next "[ index ]..." */
1659 idxp = strchr(np, '[');
1661 /* if sepp comes first, set idxp to NULL */
1662 if (sepp && idxp && (sepp < idxp))
1666 * At this point 'idxp' is set if there is an index
1667 * expected for the current component.
1670 /* set 'n' to length of current 'np' name component */
1673 /* keep sepp up to date for *ep use as we advance */
1674 skip_whitespace(idxp);
1677 /* determine the index value */
1678 #if defined(_KERNEL) && !defined(_BOOT)
1679 if (ddi_strtol(idxp, &idxep, 0, &idx))
1682 idx = strtol(idxp, &idxep, 0);
1687 /* keep sepp up to date for *ep use as we advance */
1690 /* skip white space index value and check for ']' */
1691 skip_whitespace(sepp);
1695 /* for embedded arrays, support C syntax: "a[1].b" */
1696 skip_whitespace(sepp);
1697 if (sep && (*sepp == sep))
1705 /* trim trailing whitespace by reducing length of 'np' */
1708 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1712 /* skip whitespace, and set sepp to NULL if complete */
1714 skip_whitespace(sepp);
1721 * o 'n' is the length of current 'np' component.
1722 * o 'idxp' is set if there was an index, and value 'idx'.
1723 * o 'sepp' is set to the beginning of the next component,
1724 * and set to NULL if we have no more components.
1726 * Search for nvpair with matching component name.
1728 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1729 nvp = nvlist_next_nvpair(nvl, nvp)) {
1731 /* continue if no match on name */
1732 if (strncmp(np, nvpair_name(nvp), n) ||
1733 (strlen(nvpair_name(nvp)) != n))
1736 /* if indexed, verify type is array oriented */
1737 if (idxp && !nvpair_type_is_array(nvp))
1741 * Full match found, return nvp and idx if this
1742 * was the last component.
1748 *ip = (int)idx; /* return index */
1749 return (0); /* found */
1753 * More components: current match must be
1754 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1755 * to support going deeper.
1757 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1758 nvl = EMBEDDED_NVL(nvp);
1760 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1761 (void) nvpair_value_nvlist_array(nvp,
1762 &nva, (uint_t *)&n);
1763 if ((n < 0) || (idx >= n))
1769 /* type does not support more levels */
1773 goto fail; /* 'name' not found */
1775 /* search for match of next component in embedded 'nvl' list */
1778 fail: if (ep && sepp)
1784 * Return pointer to nvpair with specified 'name'.
1787 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1789 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1793 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1794 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1797 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1798 const char *name, nvpair_t **ret, int *ip, char **ep)
1800 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1804 nvlist_exists(nvlist_t *nvl, const char *name)
1810 if (name == NULL || nvl == NULL ||
1811 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1814 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1815 nvp = &curr->nvi_nvp;
1817 if (strcmp(name, NVP_NAME(nvp)) == 0)
1825 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1827 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1831 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1833 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1837 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1839 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1843 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1845 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1849 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1851 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1855 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1857 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1861 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1863 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1867 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1869 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1873 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1875 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1879 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1881 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1884 #if !defined(_KERNEL)
1886 nvpair_value_double(nvpair_t *nvp, double *val)
1888 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1893 nvpair_value_string(nvpair_t *nvp, char **val)
1895 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1899 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1901 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1905 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1907 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1911 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1913 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1917 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1919 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1923 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1925 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1929 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1931 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1935 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1937 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1941 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1943 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1947 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1949 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1953 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1955 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1959 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1961 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1965 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1967 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1971 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1973 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1977 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1979 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1983 * Add specified pair to the list.
1986 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1988 if (nvl == NULL || nvp == NULL)
1991 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1992 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1996 * Merge the supplied nvlists and put the result in dst.
1997 * The merged list will contain all names specified in both lists,
1998 * the values are taken from nvl in the case of duplicates.
1999 * Return 0 on success.
2003 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2005 if (nvl == NULL || dst == NULL)
2009 return (nvlist_copy_pairs(nvl, dst));
2015 * Encoding related routines
2017 #define NVS_OP_ENCODE 0
2018 #define NVS_OP_DECODE 1
2019 #define NVS_OP_GETSIZE 2
2021 typedef struct nvs_ops nvs_ops_t;
2025 const nvs_ops_t *nvs_ops;
2031 * nvs operations are:
2033 * encoding / decoding of a nvlist header (nvlist_t)
2034 * calculates the size used for header and end detection
2037 * responsible for the first part of encoding / decoding of an nvpair
2038 * calculates the decoded size of an nvpair
2041 * second part of encoding / decoding of an nvpair
2044 * calculates the encoding size of an nvpair
2047 * encodes the end detection mark (zeros).
2050 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2051 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2052 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2053 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2054 int (*nvs_nvl_fini)(nvstream_t *);
2058 char nvh_encoding; /* nvs encoding method */
2059 char nvh_endian; /* nvs endian */
2060 char nvh_reserved1; /* reserved for future use */
2061 char nvh_reserved2; /* reserved for future use */
2065 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2067 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2071 * Walk nvpair in list and encode each nvpair
2073 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2074 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2077 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2081 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2088 * Get decoded size of next pair in stream, alloc
2089 * memory for nvpair_t, then decode the nvpair
2091 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2092 if (nvsize == 0) /* end of list */
2095 /* make sure len makes sense */
2096 if (nvsize < NVP_SIZE_CALC(1, 0))
2099 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2102 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2103 nvp_buf_free(nvl, nvp);
2107 if (i_validate_nvpair(nvp) != 0) {
2109 nvp_buf_free(nvl, nvp);
2113 nvp_buf_link(nvl, nvp);
2119 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2121 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2123 uint64_t nvsize = *buflen;
2127 * Get encoded size of nvpairs in nvlist
2129 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2130 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2133 if ((nvsize += size) > INT32_MAX)
2142 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2146 if (nvl->nvl_priv == 0)
2150 * Perform the operation, starting with header, then each nvpair
2152 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2155 switch (nvs->nvs_op) {
2157 err = nvs_encode_pairs(nvs, nvl);
2161 err = nvs_decode_pairs(nvs, nvl);
2164 case NVS_OP_GETSIZE:
2165 err = nvs_getsize_pairs(nvs, nvl, buflen);
2176 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2178 switch (nvs->nvs_op) {
2180 return (nvs_operation(nvs, embedded, NULL));
2182 case NVS_OP_DECODE: {
2186 if (embedded->nvl_version != NV_VERSION)
2189 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2192 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2194 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2195 nvlist_free(embedded);
2206 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2208 size_t nelem = NVP_NELEM(nvp);
2209 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2212 switch (nvs->nvs_op) {
2214 for (i = 0; i < nelem; i++)
2215 if (nvs_embedded(nvs, nvlp[i]) != 0)
2219 case NVS_OP_DECODE: {
2220 size_t len = nelem * sizeof (uint64_t);
2221 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2223 bzero(nvlp, len); /* don't trust packed data */
2224 for (i = 0; i < nelem; i++) {
2225 if (nvs_embedded(nvs, embedded) != 0) {
2230 nvlp[i] = embedded++;
2234 case NVS_OP_GETSIZE: {
2235 uint64_t nvsize = 0;
2237 for (i = 0; i < nelem; i++) {
2240 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2243 if ((nvsize += nvp_sz) > INT32_MAX)
2257 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2258 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2261 * Common routine for nvlist operations:
2262 * encode, decode, getsize (encoded size).
2265 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2271 #ifdef _LITTLE_ENDIAN
2272 int host_endian = 1;
2274 int host_endian = 0;
2275 #endif /* _LITTLE_ENDIAN */
2276 nvs_header_t *nvh = (void *)buf;
2278 if (buflen == NULL || nvl == NULL ||
2279 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2282 nvs.nvs_op = nvs_op;
2285 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2286 * a buffer is allocated. The first 4 bytes in the buffer are
2287 * used for encoding method and host endian.
2291 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2294 nvh->nvh_encoding = encoding;
2295 nvh->nvh_endian = nvl_endian = host_endian;
2296 nvh->nvh_reserved1 = 0;
2297 nvh->nvh_reserved2 = 0;
2301 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2304 /* get method of encoding from first byte */
2305 encoding = nvh->nvh_encoding;
2306 nvl_endian = nvh->nvh_endian;
2309 case NVS_OP_GETSIZE:
2310 nvl_endian = host_endian;
2313 * add the size for encoding
2315 *buflen = sizeof (nvs_header_t);
2323 * Create an nvstream with proper encoding method
2326 case NV_ENCODE_NATIVE:
2328 * check endianness, in case we are unpacking
2331 if (nvl_endian != host_endian)
2333 err = nvs_native(&nvs, nvl, buf, buflen);
2336 err = nvs_xdr(&nvs, nvl, buf, buflen);
2347 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2349 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2353 * Pack nvlist into contiguous memory
2357 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2360 #if defined(_KERNEL) && !defined(_BOOT)
2361 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2362 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2364 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2369 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2377 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2381 return (nvlist_common(nvl, *bufp, buflen, encoding,
2385 * Here is a difficult situation:
2386 * 1. The nvlist has fixed allocator properties.
2387 * All other nvlist routines (like nvlist_add_*, ...) use
2389 * 2. When using nvlist_pack() the user can specify his own
2390 * allocator properties (e.g. by using KM_NOSLEEP).
2392 * We use the user specified properties (2). A clearer solution
2393 * will be to remove the kmflag from nvlist_pack(), but we will
2394 * not change the interface.
2396 nv_priv_init(&nvpriv, nva, 0);
2398 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2401 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2404 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2405 NVS_OP_ENCODE)) != 0) {
2406 nv_mem_free(&nvpriv, buf, alloc_size);
2408 *buflen = alloc_size;
2416 * Unpack buf into an nvlist_t
2420 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2422 #if defined(_KERNEL) && !defined(_BOOT)
2423 return (nvlist_xunpack(buf, buflen, nvlp,
2424 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2426 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2431 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2439 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2442 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2451 * Native encoding functions
2455 * This structure is used when decoding a packed nvpair in
2456 * the native format. n_base points to a buffer containing the
2457 * packed nvpair. n_end is a pointer to the end of the buffer.
2458 * (n_end actually points to the first byte past the end of the
2459 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2460 * It points to the current data that we are decoding.
2461 * The amount of data left in the buffer is equal to n_end - n_curr.
2462 * n_flag is used to recognize a packed embedded list.
2471 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2474 switch (nvs->nvs_op) {
2477 nvs->nvs_private = native;
2478 native->n_curr = native->n_base = buf;
2479 native->n_end = buf + buflen;
2483 case NVS_OP_GETSIZE:
2484 nvs->nvs_private = native;
2485 native->n_curr = native->n_base = native->n_end = NULL;
2495 nvs_native_destroy(nvstream_t *nvs)
2500 native_cp(nvstream_t *nvs, void *buf, size_t size)
2502 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2504 if (native->n_curr + size > native->n_end)
2508 * The bcopy() below eliminates alignment requirement
2509 * on the buffer (stream) and is preferred over direct access.
2511 switch (nvs->nvs_op) {
2513 bcopy(buf, native->n_curr, size);
2516 bcopy(native->n_curr, buf, size);
2522 native->n_curr += size;
2527 * operate on nvlist_t header
2530 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2532 nvs_native_t *native = nvs->nvs_private;
2534 switch (nvs->nvs_op) {
2538 return (0); /* packed embedded list */
2542 /* copy version and nvflag of the nvlist_t */
2543 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2544 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2549 case NVS_OP_GETSIZE:
2551 * if calculate for packed embedded list
2552 * 4 for end of the embedded list
2554 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2555 * and 4 for end of the entire list
2557 if (native->n_flag) {
2561 *size += 2 * sizeof (int32_t) + 4;
2572 nvs_native_nvl_fini(nvstream_t *nvs)
2574 if (nvs->nvs_op == NVS_OP_ENCODE) {
2575 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2577 * Add 4 zero bytes at end of nvlist. They are used
2578 * for end detection by the decode routine.
2580 if (native->n_curr + sizeof (int) > native->n_end)
2583 bzero(native->n_curr, sizeof (int));
2584 native->n_curr += sizeof (int);
2591 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2593 if (nvs->nvs_op == NVS_OP_ENCODE) {
2594 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2595 nvlist_t *packed = (void *)
2596 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2598 * Null out the pointer that is meaningless in the packed
2599 * structure. The address may not be aligned, so we have
2602 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2605 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2609 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2611 if (nvs->nvs_op == NVS_OP_ENCODE) {
2612 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2613 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2614 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2615 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2618 * Null out pointers that are meaningless in the packed
2619 * structure. The addresses may not be aligned, so we have
2624 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2626 * Null out the pointer that is meaningless in the
2627 * packed structure. The address may not be aligned,
2628 * so we have to use bzero.
2630 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2633 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2637 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2639 switch (nvs->nvs_op) {
2640 case NVS_OP_ENCODE: {
2641 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2642 uint64_t *strp = (void *)
2643 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2645 * Null out pointers that are meaningless in the packed
2646 * structure. The addresses may not be aligned, so we have
2649 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2652 case NVS_OP_DECODE: {
2653 char **strp = (void *)NVP_VALUE(nvp);
2654 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2657 for (i = 0; i < NVP_NELEM(nvp); i++) {
2659 buf += strlen(buf) + 1;
2667 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2674 * We do the initial bcopy of the data before we look at
2675 * the nvpair type, because when we're decoding, we won't
2676 * have the correct values for the pair until we do the bcopy.
2678 switch (nvs->nvs_op) {
2681 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2688 /* verify nvp_name_sz, check the name string length */
2689 if (i_validate_nvpair_name(nvp) != 0)
2692 type = NVP_TYPE(nvp);
2695 * Verify type and nelem and get the value size.
2696 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2697 * is the size of the string(s) excluded.
2699 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2702 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2706 case DATA_TYPE_NVLIST:
2707 ret = nvpair_native_embedded(nvs, nvp);
2709 case DATA_TYPE_NVLIST_ARRAY:
2710 ret = nvpair_native_embedded_array(nvs, nvp);
2712 case DATA_TYPE_STRING_ARRAY:
2713 nvpair_native_string_array(nvs, nvp);
2723 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2725 uint64_t nvp_sz = nvp->nvp_size;
2727 switch (NVP_TYPE(nvp)) {
2728 case DATA_TYPE_NVLIST: {
2731 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2737 case DATA_TYPE_NVLIST_ARRAY: {
2740 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2750 if (nvp_sz > INT32_MAX)
2759 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2761 switch (nvs->nvs_op) {
2763 return (nvs_native_nvp_op(nvs, nvp));
2765 case NVS_OP_DECODE: {
2766 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2769 /* try to read the size value from the stream */
2770 if (native->n_curr + sizeof (int32_t) > native->n_end)
2772 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2774 /* sanity check the size value */
2775 if (decode_len < 0 ||
2776 decode_len > native->n_end - native->n_curr)
2782 * If at the end of the stream then move the cursor
2783 * forward, otherwise nvpair_native_op() will read
2784 * the entire nvpair at the same cursor position.
2787 native->n_curr += sizeof (int32_t);
2798 static const nvs_ops_t nvs_native_ops = {
2802 nvs_native_nvp_size,
2807 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2809 nvs_native_t native;
2812 nvs->nvs_ops = &nvs_native_ops;
2814 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2815 *buflen - sizeof (nvs_header_t))) != 0)
2818 err = nvs_operation(nvs, nvl, buflen);
2820 nvs_native_destroy(nvs);
2826 * XDR encoding functions
2828 * An xdr packed nvlist is encoded as:
2830 * - encoding methode and host endian (4 bytes)
2831 * - nvl_version (4 bytes)
2832 * - nvl_nvflag (4 bytes)
2834 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2835 * - encoded size of the nvpair (4 bytes)
2836 * - decoded size of the nvpair (4 bytes)
2837 * - name string, (4 + sizeof(NV_ALIGN4(string))
2838 * a string is coded as size (4 bytes) and data
2839 * - data type (4 bytes)
2840 * - number of elements in the nvpair (4 bytes)
2843 * - 2 zero's for end of the entire list (8 bytes)
2846 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2848 /* xdr data must be 4 byte aligned */
2849 if ((ulong_t)buf % 4 != 0)
2852 switch (nvs->nvs_op) {
2854 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2855 nvs->nvs_private = xdr;
2858 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2859 nvs->nvs_private = xdr;
2861 case NVS_OP_GETSIZE:
2862 nvs->nvs_private = NULL;
2870 nvs_xdr_destroy(nvstream_t *nvs)
2872 switch (nvs->nvs_op) {
2875 xdr_destroy((XDR *)nvs->nvs_private);
2883 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2885 switch (nvs->nvs_op) {
2887 case NVS_OP_DECODE: {
2888 XDR *xdr = nvs->nvs_private;
2890 if (!xdr_int(xdr, &nvl->nvl_version) ||
2891 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2895 case NVS_OP_GETSIZE: {
2897 * 2 * 4 for nvl_version + nvl_nvflag
2898 * and 8 for end of the entire list
2910 nvs_xdr_nvl_fini(nvstream_t *nvs)
2912 if (nvs->nvs_op == NVS_OP_ENCODE) {
2913 XDR *xdr = nvs->nvs_private;
2916 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2924 * The format of xdr encoded nvpair is:
2925 * encode_size, decode_size, name string, data type, nelem, data
2928 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2932 char *buf_end = (char *)nvp + nvp->nvp_size;
2934 uint_t nelem, buflen;
2936 XDR *xdr = nvs->nvs_private;
2938 ASSERT(xdr != NULL && nvp != NULL);
2941 if ((buf = NVP_NAME(nvp)) >= buf_end)
2943 buflen = buf_end - buf;
2945 if (!xdr_string(xdr, &buf, buflen - 1))
2947 nvp->nvp_name_sz = strlen(buf) + 1;
2949 /* type and nelem */
2950 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2951 !xdr_int(xdr, &nvp->nvp_value_elem))
2954 type = NVP_TYPE(nvp);
2955 nelem = nvp->nvp_value_elem;
2958 * Verify type and nelem and get the value size.
2959 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2960 * is the size of the string(s) excluded.
2962 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2965 /* if there is no data to extract then return */
2970 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2972 buflen = buf_end - buf;
2974 if (buflen < value_sz)
2978 case DATA_TYPE_NVLIST:
2979 if (nvs_embedded(nvs, (void *)buf) == 0)
2983 case DATA_TYPE_NVLIST_ARRAY:
2984 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2988 case DATA_TYPE_BOOLEAN:
2992 case DATA_TYPE_BYTE:
2993 case DATA_TYPE_INT8:
2994 case DATA_TYPE_UINT8:
2995 ret = xdr_char(xdr, buf);
2998 case DATA_TYPE_INT16:
2999 ret = xdr_short(xdr, (void *)buf);
3002 case DATA_TYPE_UINT16:
3003 ret = xdr_u_short(xdr, (void *)buf);
3006 case DATA_TYPE_BOOLEAN_VALUE:
3007 case DATA_TYPE_INT32:
3008 ret = xdr_int(xdr, (void *)buf);
3011 case DATA_TYPE_UINT32:
3012 ret = xdr_u_int(xdr, (void *)buf);
3015 case DATA_TYPE_INT64:
3016 ret = xdr_longlong_t(xdr, (void *)buf);
3019 case DATA_TYPE_UINT64:
3020 ret = xdr_u_longlong_t(xdr, (void *)buf);
3023 case DATA_TYPE_HRTIME:
3025 * NOTE: must expose the definition of hrtime_t here
3027 ret = xdr_longlong_t(xdr, (void *)buf);
3029 #if !defined(_KERNEL)
3030 case DATA_TYPE_DOUBLE:
3031 ret = xdr_double(xdr, (void *)buf);
3034 case DATA_TYPE_STRING:
3035 ret = xdr_string(xdr, &buf, buflen - 1);
3038 case DATA_TYPE_BYTE_ARRAY:
3039 ret = xdr_opaque(xdr, buf, nelem);
3042 case DATA_TYPE_INT8_ARRAY:
3043 case DATA_TYPE_UINT8_ARRAY:
3044 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3045 (xdrproc_t)xdr_char);
3048 case DATA_TYPE_INT16_ARRAY:
3049 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3050 sizeof (int16_t), (xdrproc_t)xdr_short);
3053 case DATA_TYPE_UINT16_ARRAY:
3054 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3055 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3058 case DATA_TYPE_BOOLEAN_ARRAY:
3059 case DATA_TYPE_INT32_ARRAY:
3060 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3061 sizeof (int32_t), (xdrproc_t)xdr_int);
3064 case DATA_TYPE_UINT32_ARRAY:
3065 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3066 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3069 case DATA_TYPE_INT64_ARRAY:
3070 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3071 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3074 case DATA_TYPE_UINT64_ARRAY:
3075 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3076 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3079 case DATA_TYPE_STRING_ARRAY: {
3080 size_t len = nelem * sizeof (uint64_t);
3081 char **strp = (void *)buf;
3084 if (nvs->nvs_op == NVS_OP_DECODE)
3085 bzero(buf, len); /* don't trust packed data */
3087 for (i = 0; i < nelem; i++) {
3094 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3097 if (nvs->nvs_op == NVS_OP_DECODE)
3099 len = strlen(buf) + 1;
3108 return (ret == TRUE ? 0 : EFAULT);
3112 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3114 data_type_t type = NVP_TYPE(nvp);
3116 * encode_size + decode_size + name string size + data type + nelem
3117 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3119 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3122 case DATA_TYPE_BOOLEAN:
3125 case DATA_TYPE_BOOLEAN_VALUE:
3126 case DATA_TYPE_BYTE:
3127 case DATA_TYPE_INT8:
3128 case DATA_TYPE_UINT8:
3129 case DATA_TYPE_INT16:
3130 case DATA_TYPE_UINT16:
3131 case DATA_TYPE_INT32:
3132 case DATA_TYPE_UINT32:
3133 nvp_sz += 4; /* 4 is the minimum xdr unit */
3136 case DATA_TYPE_INT64:
3137 case DATA_TYPE_UINT64:
3138 case DATA_TYPE_HRTIME:
3139 #if !defined(_KERNEL)
3140 case DATA_TYPE_DOUBLE:
3145 case DATA_TYPE_STRING:
3146 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3149 case DATA_TYPE_BYTE_ARRAY:
3150 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3153 case DATA_TYPE_BOOLEAN_ARRAY:
3154 case DATA_TYPE_INT8_ARRAY:
3155 case DATA_TYPE_UINT8_ARRAY:
3156 case DATA_TYPE_INT16_ARRAY:
3157 case DATA_TYPE_UINT16_ARRAY:
3158 case DATA_TYPE_INT32_ARRAY:
3159 case DATA_TYPE_UINT32_ARRAY:
3160 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3163 case DATA_TYPE_INT64_ARRAY:
3164 case DATA_TYPE_UINT64_ARRAY:
3165 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3168 case DATA_TYPE_STRING_ARRAY: {
3170 char **strs = (void *)NVP_VALUE(nvp);
3172 for (i = 0; i < NVP_NELEM(nvp); i++)
3173 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3178 case DATA_TYPE_NVLIST:
3179 case DATA_TYPE_NVLIST_ARRAY: {
3181 int old_nvs_op = nvs->nvs_op;
3184 nvs->nvs_op = NVS_OP_GETSIZE;
3185 if (type == DATA_TYPE_NVLIST)
3186 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3188 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3189 nvs->nvs_op = old_nvs_op;
3202 if (nvp_sz > INT32_MAX)
3212 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3213 * the largest nvpair that could be encoded in the buffer.
3215 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3216 * The size of a xdr packed nvpair without any data is 5 words.
3218 * Using the size of the data directly as an estimate would be ok
3219 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3220 * then the actual nvpair has space for an array of pointers to index
3221 * the strings. These pointers are not encoded into the packed xdr buffer.
3223 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3224 * of length 0, then each string is endcoded in xdr format as a single word.
3225 * Therefore when expanded to an nvpair there will be 2.25 word used for
3226 * each string. (a int64_t allocated for pointer usage, and a single char
3227 * for the null termination.)
3229 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3231 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3232 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3233 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3234 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3235 (NVS_XDR_DATA_LEN(x) * 2) + \
3236 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3239 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3241 XDR *xdr = nvs->nvs_private;
3242 int32_t encode_len, decode_len;
3244 switch (nvs->nvs_op) {
3245 case NVS_OP_ENCODE: {
3248 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3251 decode_len = nvp->nvp_size;
3252 encode_len = nvsize;
3253 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3256 return (nvs_xdr_nvp_op(nvs, nvp));
3258 case NVS_OP_DECODE: {
3259 struct xdr_bytesrec bytesrec;
3261 /* get the encode and decode size */
3262 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3266 /* are we at the end of the stream? */
3270 /* sanity check the size parameter */
3271 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3274 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3285 static const struct nvs_ops nvs_xdr_ops = {
3294 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3299 nvs->nvs_ops = &nvs_xdr_ops;
3301 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3302 *buflen - sizeof (nvs_header_t))) != 0)
3305 err = nvs_operation(nvs, nvl, buflen);
3307 nvs_xdr_destroy(nvs);
3312 #if defined(_KERNEL) && defined(HAVE_SPL)
3314 static int nvpair_init(void) { return 0; }
3315 static int nvpair_fini(void) { return 0; }
3317 spl_module_init(nvpair_init);
3318 spl_module_exit(nvpair_fini);
3320 MODULE_DESCRIPTION("Generic name/value pair implementation");
3321 MODULE_AUTHOR(ZFS_META_AUTHOR);
3322 MODULE_LICENSE(ZFS_META_LICENSE);
3324 EXPORT_SYMBOL(nv_alloc_init);
3325 EXPORT_SYMBOL(nv_alloc_reset);
3326 EXPORT_SYMBOL(nv_alloc_fini);
3328 /* list management */
3329 EXPORT_SYMBOL(nvlist_alloc);
3330 EXPORT_SYMBOL(nvlist_free);
3331 EXPORT_SYMBOL(nvlist_size);
3332 EXPORT_SYMBOL(nvlist_pack);
3333 EXPORT_SYMBOL(nvlist_unpack);
3334 EXPORT_SYMBOL(nvlist_dup);
3335 EXPORT_SYMBOL(nvlist_merge);
3337 EXPORT_SYMBOL(nvlist_xalloc);
3338 EXPORT_SYMBOL(nvlist_xpack);
3339 EXPORT_SYMBOL(nvlist_xunpack);
3340 EXPORT_SYMBOL(nvlist_xdup);
3341 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3343 EXPORT_SYMBOL(nvlist_add_nvpair);
3344 EXPORT_SYMBOL(nvlist_add_boolean);
3345 EXPORT_SYMBOL(nvlist_add_boolean_value);
3346 EXPORT_SYMBOL(nvlist_add_byte);
3347 EXPORT_SYMBOL(nvlist_add_int8);
3348 EXPORT_SYMBOL(nvlist_add_uint8);
3349 EXPORT_SYMBOL(nvlist_add_int16);
3350 EXPORT_SYMBOL(nvlist_add_uint16);
3351 EXPORT_SYMBOL(nvlist_add_int32);
3352 EXPORT_SYMBOL(nvlist_add_uint32);
3353 EXPORT_SYMBOL(nvlist_add_int64);
3354 EXPORT_SYMBOL(nvlist_add_uint64);
3355 EXPORT_SYMBOL(nvlist_add_string);
3356 EXPORT_SYMBOL(nvlist_add_nvlist);
3357 EXPORT_SYMBOL(nvlist_add_boolean_array);
3358 EXPORT_SYMBOL(nvlist_add_byte_array);
3359 EXPORT_SYMBOL(nvlist_add_int8_array);
3360 EXPORT_SYMBOL(nvlist_add_uint8_array);
3361 EXPORT_SYMBOL(nvlist_add_int16_array);
3362 EXPORT_SYMBOL(nvlist_add_uint16_array);
3363 EXPORT_SYMBOL(nvlist_add_int32_array);
3364 EXPORT_SYMBOL(nvlist_add_uint32_array);
3365 EXPORT_SYMBOL(nvlist_add_int64_array);
3366 EXPORT_SYMBOL(nvlist_add_uint64_array);
3367 EXPORT_SYMBOL(nvlist_add_string_array);
3368 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3369 EXPORT_SYMBOL(nvlist_next_nvpair);
3370 EXPORT_SYMBOL(nvlist_prev_nvpair);
3371 EXPORT_SYMBOL(nvlist_empty);
3372 EXPORT_SYMBOL(nvlist_add_hrtime);
3374 EXPORT_SYMBOL(nvlist_remove);
3375 EXPORT_SYMBOL(nvlist_remove_nvpair);
3376 EXPORT_SYMBOL(nvlist_remove_all);
3378 EXPORT_SYMBOL(nvlist_lookup_boolean);
3379 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3380 EXPORT_SYMBOL(nvlist_lookup_byte);
3381 EXPORT_SYMBOL(nvlist_lookup_int8);
3382 EXPORT_SYMBOL(nvlist_lookup_uint8);
3383 EXPORT_SYMBOL(nvlist_lookup_int16);
3384 EXPORT_SYMBOL(nvlist_lookup_uint16);
3385 EXPORT_SYMBOL(nvlist_lookup_int32);
3386 EXPORT_SYMBOL(nvlist_lookup_uint32);
3387 EXPORT_SYMBOL(nvlist_lookup_int64);
3388 EXPORT_SYMBOL(nvlist_lookup_uint64);
3389 EXPORT_SYMBOL(nvlist_lookup_string);
3390 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3391 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3392 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3393 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3394 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3395 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3396 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3397 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3398 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3399 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3400 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3401 EXPORT_SYMBOL(nvlist_lookup_string_array);
3402 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3403 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3404 EXPORT_SYMBOL(nvlist_lookup_pairs);
3406 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3407 EXPORT_SYMBOL(nvlist_exists);
3409 /* processing nvpair */
3410 EXPORT_SYMBOL(nvpair_name);
3411 EXPORT_SYMBOL(nvpair_type);
3412 EXPORT_SYMBOL(nvpair_value_boolean_value);
3413 EXPORT_SYMBOL(nvpair_value_byte);
3414 EXPORT_SYMBOL(nvpair_value_int8);
3415 EXPORT_SYMBOL(nvpair_value_uint8);
3416 EXPORT_SYMBOL(nvpair_value_int16);
3417 EXPORT_SYMBOL(nvpair_value_uint16);
3418 EXPORT_SYMBOL(nvpair_value_int32);
3419 EXPORT_SYMBOL(nvpair_value_uint32);
3420 EXPORT_SYMBOL(nvpair_value_int64);
3421 EXPORT_SYMBOL(nvpair_value_uint64);
3422 EXPORT_SYMBOL(nvpair_value_string);
3423 EXPORT_SYMBOL(nvpair_value_nvlist);
3424 EXPORT_SYMBOL(nvpair_value_boolean_array);
3425 EXPORT_SYMBOL(nvpair_value_byte_array);
3426 EXPORT_SYMBOL(nvpair_value_int8_array);
3427 EXPORT_SYMBOL(nvpair_value_uint8_array);
3428 EXPORT_SYMBOL(nvpair_value_int16_array);
3429 EXPORT_SYMBOL(nvpair_value_uint16_array);
3430 EXPORT_SYMBOL(nvpair_value_int32_array);
3431 EXPORT_SYMBOL(nvpair_value_uint32_array);
3432 EXPORT_SYMBOL(nvpair_value_int64_array);
3433 EXPORT_SYMBOL(nvpair_value_uint64_array);
3434 EXPORT_SYMBOL(nvpair_value_string_array);
3435 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3436 EXPORT_SYMBOL(nvpair_value_hrtime);