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/debug.h>
27 #include <sys/nvpair.h>
28 #include <sys/nvpair_impl.h>
29 #include <rpc/types.h>
32 #if defined(_KERNEL) && !defined(_BOOT)
33 #include <sys/varargs.h>
34 #include <sys/sunddi.h>
43 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
45 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
47 #if defined(__FreeBSD__) && !defined(_KERNEL)
49 * libnvpair is the lowest commen denominator for ZFS related libraries,
50 * defining aok here makes it usable by all ZFS related libraries
56 * nvpair.c - Provides kernel & userland interfaces for manipulating
71 * +--------------+ last i_nvp in list
72 * | nvpriv_t | +--------------------->
74 * +--+- nvp_list | | +------------+
75 * | | nvp_last -+--+ + nv_alloc_t |
76 * | | nvp_curr | |------------|
77 * | | nvp_nva -+----> | nva_ops |
78 * | | nvp_stat | | nva_arg |
79 * | +--------------+ +------------+
83 * +---------------------+ +-------------------+
84 * | i_nvp_t | +-->| i_nvp_t | +-->
85 * |---------------------| | |-------------------| |
86 * | nvi_next -+--+ | nvi_next -+--+
87 * | nvi_prev (NULL) | <----+ nvi_prev |
88 * | . . . . . . . . . . | | . . . . . . . . . |
89 * | nvp (nvpair_t) | | nvp (nvpair_t) |
90 * | - nvp_size | | - nvp_size |
91 * | - nvp_name_sz | | - nvp_name_sz |
92 * | - nvp_value_elem | | - nvp_value_elem |
93 * | - nvp_type | | - nvp_type |
94 * | - data ... | | - data ... |
95 * +---------------------+ +-------------------+
99 * +---------------------+ +---------------------+
100 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
101 * |---------------------| | | |---------------------|
102 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
103 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
104 * | . . . . . . . . . | | . . . . . . . . . |
105 * | nvp (nvpair_t) | | nvp (nvpair_t) |
106 * | - nvp_size | | - nvp_size |
107 * | - nvp_name_sz | | - nvp_name_sz |
108 * | - nvp_value_elem | | - nvp_value_elem |
109 * | - DATA_TYPE_NVLIST | | - nvp_type |
110 * | - data (embedded) | | - data ... |
111 * | nvlist name | +---------------------+
112 * | +--------------+ |
114 * | |--------------| |
115 * | | nvl_version | |
117 * | | nvl_priv --+---+---->
120 * | +--------------+ |
121 * +---------------------+
124 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
125 * allow value to be aligned on 8 byte boundary
127 * name_len is the length of the name string including the null terminator
130 #define NVP_SIZE_CALC(name_len, data_len) \
131 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
133 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
134 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
135 uint_t nelem, const void *data);
137 #define NV_STAT_EMBEDDED 0x1
138 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
139 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
141 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
142 #define NVPAIR2I_NVP(nvp) \
143 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
147 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
155 va_start(valist, nvo);
156 if (nva->nva_ops->nv_ao_init != NULL)
157 err = nva->nva_ops->nv_ao_init(nva, valist);
164 nv_alloc_reset(nv_alloc_t *nva)
166 if (nva->nva_ops->nv_ao_reset != NULL)
167 nva->nva_ops->nv_ao_reset(nva);
171 nv_alloc_fini(nv_alloc_t *nva)
173 if (nva->nva_ops->nv_ao_fini != NULL)
174 nva->nva_ops->nv_ao_fini(nva);
178 nvlist_lookup_nv_alloc(nvlist_t *nvl)
183 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
186 return (priv->nvp_nva);
190 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
192 nv_alloc_t *nva = nvp->nvp_nva;
195 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
202 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
204 nv_alloc_t *nva = nvp->nvp_nva;
206 nva->nva_ops->nv_ao_free(nva, buf, size);
210 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
212 bzero(priv, sizeof (nvpriv_t));
215 priv->nvp_stat = stat;
219 nv_priv_alloc(nv_alloc_t *nva)
224 * nv_mem_alloc() cannot called here because it needs the priv
227 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
230 nv_priv_init(priv, nva, 0);
236 * Embedded lists need their own nvpriv_t's. We create a new
237 * nvpriv_t using the parameters and allocator from the parent
241 nv_priv_alloc_embedded(nvpriv_t *priv)
245 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
248 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
254 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
256 nvl->nvl_version = NV_VERSION;
257 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
258 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
264 nvlist_nvflag(nvlist_t *nvl)
266 return (nvl->nvl_nvflag);
270 * nvlist_alloc - Allocate nvlist.
274 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
276 #if defined(_KERNEL) && !defined(_BOOT)
277 return (nvlist_xalloc(nvlp, nvflag,
278 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
280 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
285 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
289 if (nvlp == NULL || nva == NULL)
292 if ((priv = nv_priv_alloc(nva)) == NULL)
295 if ((*nvlp = nv_mem_zalloc(priv,
296 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
297 nv_mem_free(priv, priv, sizeof (nvpriv_t));
301 nvlist_init(*nvlp, nvflag, priv);
307 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
310 nvp_buf_alloc(nvlist_t *nvl, size_t len)
312 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
318 * Allocate the buffer
320 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
322 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
332 * nvp_buf_free - de-Allocate an i_nvp_t.
335 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
337 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
338 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
340 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
344 * nvp_buf_link - link a new nv pair into the nvlist.
347 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
349 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
350 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
352 /* Put element at end of nvlist */
353 if (priv->nvp_list == NULL) {
354 priv->nvp_list = priv->nvp_last = curr;
356 curr->nvi_prev = priv->nvp_last;
357 priv->nvp_last->nvi_next = curr;
358 priv->nvp_last = curr;
363 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
366 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
368 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
369 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
372 * protect nvlist_next_nvpair() against walking on freed memory.
374 if (priv->nvp_curr == curr)
375 priv->nvp_curr = curr->nvi_next;
377 if (curr == priv->nvp_list)
378 priv->nvp_list = curr->nvi_next;
380 curr->nvi_prev->nvi_next = curr->nvi_next;
382 if (curr == priv->nvp_last)
383 priv->nvp_last = curr->nvi_prev;
385 curr->nvi_next->nvi_prev = curr->nvi_prev;
389 * take a nvpair type and number of elements and make sure the are valid
392 i_validate_type_nelem(data_type_t type, uint_t nelem)
395 case DATA_TYPE_BOOLEAN:
399 case DATA_TYPE_BOOLEAN_VALUE:
402 case DATA_TYPE_UINT8:
403 case DATA_TYPE_INT16:
404 case DATA_TYPE_UINT16:
405 case DATA_TYPE_INT32:
406 case DATA_TYPE_UINT32:
407 case DATA_TYPE_INT64:
408 case DATA_TYPE_UINT64:
409 case DATA_TYPE_STRING:
410 case DATA_TYPE_HRTIME:
411 case DATA_TYPE_NVLIST:
412 #if !defined(_KERNEL)
413 case DATA_TYPE_DOUBLE:
418 case DATA_TYPE_BOOLEAN_ARRAY:
419 case DATA_TYPE_BYTE_ARRAY:
420 case DATA_TYPE_INT8_ARRAY:
421 case DATA_TYPE_UINT8_ARRAY:
422 case DATA_TYPE_INT16_ARRAY:
423 case DATA_TYPE_UINT16_ARRAY:
424 case DATA_TYPE_INT32_ARRAY:
425 case DATA_TYPE_UINT32_ARRAY:
426 case DATA_TYPE_INT64_ARRAY:
427 case DATA_TYPE_UINT64_ARRAY:
428 case DATA_TYPE_STRING_ARRAY:
429 case DATA_TYPE_NVLIST_ARRAY:
430 /* we allow arrays with 0 elements */
439 * Verify nvp_name_sz and check the name string length.
442 i_validate_nvpair_name(nvpair_t *nvp)
444 if ((nvp->nvp_name_sz <= 0) ||
445 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
448 /* verify the name string, make sure its terminated */
449 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
452 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
456 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
459 case DATA_TYPE_BOOLEAN_VALUE:
460 if (*(boolean_t *)data != B_TRUE &&
461 *(boolean_t *)data != B_FALSE)
464 case DATA_TYPE_BOOLEAN_ARRAY: {
467 for (i = 0; i < nelem; i++)
468 if (((boolean_t *)data)[i] != B_TRUE &&
469 ((boolean_t *)data)[i] != B_FALSE)
481 * This function takes a pointer to what should be a nvpair and it's size
482 * and then verifies that all the nvpair fields make sense and can be
483 * trusted. This function is used when decoding packed nvpairs.
486 i_validate_nvpair(nvpair_t *nvp)
488 data_type_t type = NVP_TYPE(nvp);
491 /* verify nvp_name_sz, check the name string length */
492 if (i_validate_nvpair_name(nvp) != 0)
495 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
499 * verify nvp_type, nvp_value_elem, and also possibly
500 * verify string values and get the value size.
502 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
503 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
504 if (size2 < 0 || size1 != NV_ALIGN(size2))
511 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
516 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
519 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
520 nvpair_t *nvp = &curr->nvi_nvp;
523 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
524 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
532 * Frees all memory allocated for an nvpair (like embedded lists) with
533 * the exception of the nvpair buffer itself.
536 nvpair_free(nvpair_t *nvp)
538 switch (NVP_TYPE(nvp)) {
539 case DATA_TYPE_NVLIST:
540 nvlist_free(EMBEDDED_NVL(nvp));
542 case DATA_TYPE_NVLIST_ARRAY: {
543 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
546 for (i = 0; i < NVP_NELEM(nvp); i++)
547 nvlist_free(nvlp[i]);
556 * nvlist_free - free an unpacked nvlist
559 nvlist_free(nvlist_t *nvl)
565 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
569 * Unpacked nvlist are linked through i_nvp_t
571 curr = priv->nvp_list;
572 while (curr != NULL) {
573 nvpair_t *nvp = &curr->nvi_nvp;
574 curr = curr->nvi_next;
577 nvp_buf_free(nvl, nvp);
580 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
581 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
585 nv_mem_free(priv, priv, sizeof (nvpriv_t));
589 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
591 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
597 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
598 if (&curr->nvi_nvp == nvp)
605 * Make a copy of nvlist
609 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
611 #if defined(_KERNEL) && !defined(_BOOT)
612 return (nvlist_xdup(nvl, nvlp,
613 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
615 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
620 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
625 if (nvl == NULL || nvlp == NULL)
628 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
631 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
640 * Remove all with matching name
643 nvlist_remove_all(nvlist_t *nvl, const char *name)
649 if (nvl == NULL || name == NULL ||
650 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
653 curr = priv->nvp_list;
654 while (curr != NULL) {
655 nvpair_t *nvp = &curr->nvi_nvp;
657 curr = curr->nvi_next;
658 if (strcmp(name, NVP_NAME(nvp)) != 0)
661 nvp_buf_unlink(nvl, nvp);
663 nvp_buf_free(nvl, nvp);
672 * Remove first one with matching name and type
675 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
680 if (nvl == NULL || name == NULL ||
681 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
684 curr = priv->nvp_list;
685 while (curr != NULL) {
686 nvpair_t *nvp = &curr->nvi_nvp;
688 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
689 nvp_buf_unlink(nvl, nvp);
691 nvp_buf_free(nvl, nvp);
695 curr = curr->nvi_next;
702 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
704 if (nvl == NULL || nvp == NULL)
707 nvp_buf_unlink(nvl, nvp);
709 nvp_buf_free(nvl, nvp);
714 * This function calculates the size of an nvpair value.
716 * The data argument controls the behavior in case of the data types
717 * DATA_TYPE_STRING and
718 * DATA_TYPE_STRING_ARRAY
719 * Is data == NULL then the size of the string(s) is excluded.
722 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
726 if (i_validate_type_nelem(type, nelem) != 0)
729 /* Calculate required size for holding value */
731 case DATA_TYPE_BOOLEAN:
734 case DATA_TYPE_BOOLEAN_VALUE:
735 value_sz = sizeof (boolean_t);
738 value_sz = sizeof (uchar_t);
741 value_sz = sizeof (int8_t);
743 case DATA_TYPE_UINT8:
744 value_sz = sizeof (uint8_t);
746 case DATA_TYPE_INT16:
747 value_sz = sizeof (int16_t);
749 case DATA_TYPE_UINT16:
750 value_sz = sizeof (uint16_t);
752 case DATA_TYPE_INT32:
753 value_sz = sizeof (int32_t);
755 case DATA_TYPE_UINT32:
756 value_sz = sizeof (uint32_t);
758 case DATA_TYPE_INT64:
759 value_sz = sizeof (int64_t);
761 case DATA_TYPE_UINT64:
762 value_sz = sizeof (uint64_t);
764 #if !defined(_KERNEL)
765 case DATA_TYPE_DOUBLE:
766 value_sz = sizeof (double);
769 case DATA_TYPE_STRING:
773 value_sz = strlen(data) + 1;
775 case DATA_TYPE_BOOLEAN_ARRAY:
776 value_sz = (uint64_t)nelem * sizeof (boolean_t);
778 case DATA_TYPE_BYTE_ARRAY:
779 value_sz = (uint64_t)nelem * sizeof (uchar_t);
781 case DATA_TYPE_INT8_ARRAY:
782 value_sz = (uint64_t)nelem * sizeof (int8_t);
784 case DATA_TYPE_UINT8_ARRAY:
785 value_sz = (uint64_t)nelem * sizeof (uint8_t);
787 case DATA_TYPE_INT16_ARRAY:
788 value_sz = (uint64_t)nelem * sizeof (int16_t);
790 case DATA_TYPE_UINT16_ARRAY:
791 value_sz = (uint64_t)nelem * sizeof (uint16_t);
793 case DATA_TYPE_INT32_ARRAY:
794 value_sz = (uint64_t)nelem * sizeof (int32_t);
796 case DATA_TYPE_UINT32_ARRAY:
797 value_sz = (uint64_t)nelem * sizeof (uint32_t);
799 case DATA_TYPE_INT64_ARRAY:
800 value_sz = (uint64_t)nelem * sizeof (int64_t);
802 case DATA_TYPE_UINT64_ARRAY:
803 value_sz = (uint64_t)nelem * sizeof (uint64_t);
805 case DATA_TYPE_STRING_ARRAY:
806 value_sz = (uint64_t)nelem * sizeof (uint64_t);
809 char *const *strs = data;
812 /* no alignment requirement for strings */
813 for (i = 0; i < nelem; i++) {
816 value_sz += strlen(strs[i]) + 1;
820 case DATA_TYPE_HRTIME:
821 value_sz = sizeof (hrtime_t);
823 case DATA_TYPE_NVLIST:
824 value_sz = NV_ALIGN(sizeof (nvlist_t));
826 case DATA_TYPE_NVLIST_ARRAY:
827 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
828 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
834 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
838 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
843 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
844 nvl->nvl_priv)) == NULL)
847 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
849 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
850 nvlist_free(emb_nvl);
851 emb_nvl->nvl_priv = 0;
858 * nvlist_add_common - Add new <name,value> pair to nvlist
861 nvlist_add_common(nvlist_t *nvl, const char *name,
862 data_type_t type, uint_t nelem, const void *data)
867 int nvp_sz, name_sz, value_sz;
870 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
873 if (nelem != 0 && data == NULL)
877 * Verify type and nelem and get the value size.
878 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
879 * is the size of the string(s) included.
881 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
884 if (i_validate_nvpair_value(type, nelem, data) != 0)
888 * If we're adding an nvlist or nvlist array, ensure that we are not
889 * adding the input nvlist to itself, which would cause recursion,
890 * and ensure that no NULL nvlist pointers are present.
893 case DATA_TYPE_NVLIST:
894 if (data == nvl || data == NULL)
897 case DATA_TYPE_NVLIST_ARRAY: {
898 nvlist_t **onvlp = (nvlist_t **)data;
899 for (i = 0; i < nelem; i++) {
900 if (onvlp[i] == nvl || onvlp[i] == NULL)
909 /* calculate sizes of the nvpair elements and the nvpair itself */
910 name_sz = strlen(name) + 1;
912 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
914 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
917 ASSERT(nvp->nvp_size == nvp_sz);
918 nvp->nvp_name_sz = name_sz;
919 nvp->nvp_value_elem = nelem;
920 nvp->nvp_type = type;
921 bcopy(name, NVP_NAME(nvp), name_sz);
924 case DATA_TYPE_BOOLEAN:
926 case DATA_TYPE_STRING_ARRAY: {
927 char *const *strs = data;
928 char *buf = NVP_VALUE(nvp);
929 char **cstrs = (void *)buf;
931 /* skip pre-allocated space for pointer array */
932 buf += nelem * sizeof (uint64_t);
933 for (i = 0; i < nelem; i++) {
934 int slen = strlen(strs[i]) + 1;
935 bcopy(strs[i], buf, slen);
941 case DATA_TYPE_NVLIST: {
942 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
943 nvlist_t *onvl = (nvlist_t *)data;
945 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
946 nvp_buf_free(nvl, nvp);
951 case DATA_TYPE_NVLIST_ARRAY: {
952 nvlist_t **onvlp = (nvlist_t **)data;
953 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
954 nvlist_t *embedded = (nvlist_t *)
955 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
957 for (i = 0; i < nelem; i++) {
958 if ((err = nvlist_copy_embedded(nvl,
959 onvlp[i], embedded)) != 0) {
961 * Free any successfully created lists
964 nvp_buf_free(nvl, nvp);
968 nvlp[i] = embedded++;
973 bcopy(data, NVP_VALUE(nvp), value_sz);
976 /* if unique name, remove before add */
977 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
978 (void) nvlist_remove_all(nvl, name);
979 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
980 (void) nvlist_remove(nvl, name, type);
982 nvp_buf_link(nvl, nvp);
988 nvlist_add_boolean(nvlist_t *nvl, const char *name)
990 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
994 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
996 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1000 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1002 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1006 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1008 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1012 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1014 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1018 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1020 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1024 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1026 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1030 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1032 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1036 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1038 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1042 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1044 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1048 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1050 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1053 #if !defined(_KERNEL)
1055 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1057 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1062 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1064 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1068 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1069 boolean_t *a, uint_t n)
1071 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1075 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1077 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1081 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1083 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1087 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1089 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1093 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1095 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1099 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1101 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1105 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1107 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1111 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1113 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1117 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1119 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1123 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1125 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1129 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1130 char *const *a, uint_t n)
1132 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1136 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1138 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1142 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1144 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1148 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1150 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1153 /* reading name-value pairs */
1155 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1161 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1164 curr = NVPAIR2I_NVP(nvp);
1167 * Ensure that nvp is a valid nvpair on this nvlist.
1168 * NB: nvp_curr is used only as a hint so that we don't always
1169 * have to walk the list to determine if nvp is still on the list.
1172 curr = priv->nvp_list;
1173 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1174 curr = curr->nvi_next;
1178 priv->nvp_curr = curr;
1180 return (curr != NULL ? &curr->nvi_nvp : NULL);
1184 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1190 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1193 curr = NVPAIR2I_NVP(nvp);
1196 curr = priv->nvp_last;
1197 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1198 curr = curr->nvi_prev;
1202 priv->nvp_curr = curr;
1204 return (curr != NULL ? &curr->nvi_nvp : NULL);
1208 nvlist_empty(nvlist_t *nvl)
1213 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1216 return (priv->nvp_list == NULL);
1220 nvpair_name(nvpair_t *nvp)
1222 return (NVP_NAME(nvp));
1226 nvpair_type(nvpair_t *nvp)
1228 return (NVP_TYPE(nvp));
1232 nvpair_type_is_array(nvpair_t *nvp)
1234 data_type_t type = NVP_TYPE(nvp);
1236 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1237 (type == DATA_TYPE_INT8_ARRAY) ||
1238 (type == DATA_TYPE_UINT8_ARRAY) ||
1239 (type == DATA_TYPE_INT16_ARRAY) ||
1240 (type == DATA_TYPE_UINT16_ARRAY) ||
1241 (type == DATA_TYPE_INT32_ARRAY) ||
1242 (type == DATA_TYPE_UINT32_ARRAY) ||
1243 (type == DATA_TYPE_INT64_ARRAY) ||
1244 (type == DATA_TYPE_UINT64_ARRAY) ||
1245 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1246 (type == DATA_TYPE_STRING_ARRAY) ||
1247 (type == DATA_TYPE_NVLIST_ARRAY))
1254 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1256 if (nvp == NULL || nvpair_type(nvp) != type)
1260 * For non-array types, we copy the data.
1261 * For array types (including string), we set a pointer.
1264 case DATA_TYPE_BOOLEAN:
1269 case DATA_TYPE_BOOLEAN_VALUE:
1270 case DATA_TYPE_BYTE:
1271 case DATA_TYPE_INT8:
1272 case DATA_TYPE_UINT8:
1273 case DATA_TYPE_INT16:
1274 case DATA_TYPE_UINT16:
1275 case DATA_TYPE_INT32:
1276 case DATA_TYPE_UINT32:
1277 case DATA_TYPE_INT64:
1278 case DATA_TYPE_UINT64:
1279 case DATA_TYPE_HRTIME:
1280 #if !defined(_KERNEL)
1281 case DATA_TYPE_DOUBLE:
1285 bcopy(NVP_VALUE(nvp), data,
1286 (size_t)i_get_value_size(type, NULL, 1));
1291 case DATA_TYPE_NVLIST:
1292 case DATA_TYPE_STRING:
1295 *(void **)data = (void *)NVP_VALUE(nvp);
1300 case DATA_TYPE_BOOLEAN_ARRAY:
1301 case DATA_TYPE_BYTE_ARRAY:
1302 case DATA_TYPE_INT8_ARRAY:
1303 case DATA_TYPE_UINT8_ARRAY:
1304 case DATA_TYPE_INT16_ARRAY:
1305 case DATA_TYPE_UINT16_ARRAY:
1306 case DATA_TYPE_INT32_ARRAY:
1307 case DATA_TYPE_UINT32_ARRAY:
1308 case DATA_TYPE_INT64_ARRAY:
1309 case DATA_TYPE_UINT64_ARRAY:
1310 case DATA_TYPE_STRING_ARRAY:
1311 case DATA_TYPE_NVLIST_ARRAY:
1312 if (nelem == NULL || data == NULL)
1314 if ((*nelem = NVP_NELEM(nvp)) != 0)
1315 *(void **)data = (void *)NVP_VALUE(nvp);
1317 *(void **)data = NULL;
1328 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1329 uint_t *nelem, void *data)
1335 if (name == NULL || nvl == NULL ||
1336 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1339 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1342 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1343 nvp = &curr->nvi_nvp;
1345 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1346 return (nvpair_value_common(nvp, type, nelem, data));
1353 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1355 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1359 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1361 return (nvlist_lookup_common(nvl, name,
1362 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1366 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1368 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1372 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1374 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1378 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1380 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1384 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1386 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1390 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1392 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1396 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1398 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1402 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1404 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1408 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1410 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1414 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1416 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1419 #if !defined(_KERNEL)
1421 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1423 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1428 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1430 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1434 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1436 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1440 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1441 boolean_t **a, uint_t *n)
1443 return (nvlist_lookup_common(nvl, name,
1444 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1448 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1449 uchar_t **a, uint_t *n)
1451 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1455 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1457 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1461 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1462 uint8_t **a, uint_t *n)
1464 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1468 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1469 int16_t **a, uint_t *n)
1471 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1475 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1476 uint16_t **a, uint_t *n)
1478 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1482 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1483 int32_t **a, uint_t *n)
1485 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1489 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1490 uint32_t **a, uint_t *n)
1492 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1496 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1497 int64_t **a, uint_t *n)
1499 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1503 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1504 uint64_t **a, uint_t *n)
1506 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1510 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1511 char ***a, uint_t *n)
1513 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1517 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1518 nvlist_t ***a, uint_t *n)
1520 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1524 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1526 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1530 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1534 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1538 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1543 switch (type = va_arg(ap, data_type_t)) {
1544 case DATA_TYPE_BOOLEAN:
1545 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1548 case DATA_TYPE_BOOLEAN_VALUE:
1549 case DATA_TYPE_BYTE:
1550 case DATA_TYPE_INT8:
1551 case DATA_TYPE_UINT8:
1552 case DATA_TYPE_INT16:
1553 case DATA_TYPE_UINT16:
1554 case DATA_TYPE_INT32:
1555 case DATA_TYPE_UINT32:
1556 case DATA_TYPE_INT64:
1557 case DATA_TYPE_UINT64:
1558 case DATA_TYPE_HRTIME:
1559 case DATA_TYPE_STRING:
1560 case DATA_TYPE_NVLIST:
1561 #if !defined(_KERNEL)
1562 case DATA_TYPE_DOUBLE:
1564 val = va_arg(ap, void *);
1565 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1568 case DATA_TYPE_BYTE_ARRAY:
1569 case DATA_TYPE_BOOLEAN_ARRAY:
1570 case DATA_TYPE_INT8_ARRAY:
1571 case DATA_TYPE_UINT8_ARRAY:
1572 case DATA_TYPE_INT16_ARRAY:
1573 case DATA_TYPE_UINT16_ARRAY:
1574 case DATA_TYPE_INT32_ARRAY:
1575 case DATA_TYPE_UINT32_ARRAY:
1576 case DATA_TYPE_INT64_ARRAY:
1577 case DATA_TYPE_UINT64_ARRAY:
1578 case DATA_TYPE_STRING_ARRAY:
1579 case DATA_TYPE_NVLIST_ARRAY:
1580 val = va_arg(ap, void *);
1581 nelem = va_arg(ap, uint_t *);
1582 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1589 if (ret == ENOENT && noentok)
1598 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1599 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1600 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1601 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1602 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1603 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1604 * code also supports "a.d[3]e[1]" syntax).
1606 * If 'ip' is non-NULL and the last name component is an array, return the
1607 * value of the "...[index]" array index in *ip. For an array reference that
1608 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1609 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1610 * inside the 'name' string where the syntax error was detected.
1613 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1614 nvpair_t **ret, int *ip, char **ep)
1625 *ip = -1; /* not indexed */
1629 if ((nvl == NULL) || (name == NULL))
1632 /* step through components of name */
1633 for (np = name; np && *np; np = sepp) {
1634 /* ensure unique names */
1635 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1638 /* skip white space */
1639 skip_whitespace(np);
1643 /* set 'sepp' to end of current component 'np' */
1645 sepp = strchr(np, sep);
1649 /* find start of next "[ index ]..." */
1650 idxp = strchr(np, '[');
1652 /* if sepp comes first, set idxp to NULL */
1653 if (sepp && idxp && (sepp < idxp))
1657 * At this point 'idxp' is set if there is an index
1658 * expected for the current component.
1661 /* set 'n' to length of current 'np' name component */
1664 /* keep sepp up to date for *ep use as we advance */
1665 skip_whitespace(idxp);
1668 /* determine the index value */
1669 #if defined(_KERNEL) && !defined(_BOOT)
1670 if (ddi_strtol(idxp, &idxep, 0, &idx))
1673 idx = strtol(idxp, &idxep, 0);
1678 /* keep sepp up to date for *ep use as we advance */
1681 /* skip white space index value and check for ']' */
1682 skip_whitespace(sepp);
1686 /* for embedded arrays, support C syntax: "a[1].b" */
1687 skip_whitespace(sepp);
1688 if (sep && (*sepp == sep))
1696 /* trim trailing whitespace by reducing length of 'np' */
1699 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1703 /* skip whitespace, and set sepp to NULL if complete */
1705 skip_whitespace(sepp);
1712 * o 'n' is the length of current 'np' component.
1713 * o 'idxp' is set if there was an index, and value 'idx'.
1714 * o 'sepp' is set to the beginning of the next component,
1715 * and set to NULL if we have no more components.
1717 * Search for nvpair with matching component name.
1719 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1720 nvp = nvlist_next_nvpair(nvl, nvp)) {
1722 /* continue if no match on name */
1723 if (strncmp(np, nvpair_name(nvp), n) ||
1724 (strlen(nvpair_name(nvp)) != n))
1727 /* if indexed, verify type is array oriented */
1728 if (idxp && !nvpair_type_is_array(nvp))
1732 * Full match found, return nvp and idx if this
1733 * was the last component.
1739 *ip = (int)idx; /* return index */
1740 return (0); /* found */
1744 * More components: current match must be
1745 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1746 * to support going deeper.
1748 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1749 nvl = EMBEDDED_NVL(nvp);
1751 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1752 (void) nvpair_value_nvlist_array(nvp,
1753 &nva, (uint_t *)&n);
1754 if ((n < 0) || (idx >= n))
1760 /* type does not support more levels */
1764 goto fail; /* 'name' not found */
1766 /* search for match of next component in embedded 'nvl' list */
1769 fail: if (ep && sepp)
1775 * Return pointer to nvpair with specified 'name'.
1778 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1780 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1784 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1785 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1788 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1789 const char *name, nvpair_t **ret, int *ip, char **ep)
1791 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1795 nvlist_exists(nvlist_t *nvl, const char *name)
1801 if (name == NULL || nvl == NULL ||
1802 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1805 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1806 nvp = &curr->nvi_nvp;
1808 if (strcmp(name, NVP_NAME(nvp)) == 0)
1816 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1818 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1822 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1824 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1828 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1830 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1834 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1836 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1840 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1842 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1846 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1848 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1852 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1854 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1858 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1860 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1864 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1866 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1870 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1872 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1875 #if !defined(_KERNEL)
1877 nvpair_value_double(nvpair_t *nvp, double *val)
1879 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1884 nvpair_value_string(nvpair_t *nvp, char **val)
1886 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1890 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1892 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1896 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1898 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1902 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1904 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1908 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1910 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1914 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1916 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1920 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1922 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1926 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1928 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1932 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1934 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1938 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1940 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1944 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1946 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1950 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1952 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1956 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1958 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1962 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1964 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1968 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1970 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1974 * Add specified pair to the list.
1977 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1979 if (nvl == NULL || nvp == NULL)
1982 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1983 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1987 * Merge the supplied nvlists and put the result in dst.
1988 * The merged list will contain all names specified in both lists,
1989 * the values are taken from nvl in the case of duplicates.
1990 * Return 0 on success.
1994 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1996 if (nvl == NULL || dst == NULL)
2000 return (nvlist_copy_pairs(nvl, dst));
2006 * Encoding related routines
2008 #define NVS_OP_ENCODE 0
2009 #define NVS_OP_DECODE 1
2010 #define NVS_OP_GETSIZE 2
2012 typedef struct nvs_ops nvs_ops_t;
2016 const nvs_ops_t *nvs_ops;
2022 * nvs operations are:
2024 * encoding / decoding of a nvlist header (nvlist_t)
2025 * calculates the size used for header and end detection
2028 * responsible for the first part of encoding / decoding of an nvpair
2029 * calculates the decoded size of an nvpair
2032 * second part of encoding / decoding of an nvpair
2035 * calculates the encoding size of an nvpair
2038 * encodes the end detection mark (zeros).
2041 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2042 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2043 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2044 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2045 int (*nvs_nvl_fini)(nvstream_t *);
2049 char nvh_encoding; /* nvs encoding method */
2050 char nvh_endian; /* nvs endian */
2051 char nvh_reserved1; /* reserved for future use */
2052 char nvh_reserved2; /* reserved for future use */
2056 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2058 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2062 * Walk nvpair in list and encode each nvpair
2064 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2065 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2068 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2072 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2079 * Get decoded size of next pair in stream, alloc
2080 * memory for nvpair_t, then decode the nvpair
2082 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2083 if (nvsize == 0) /* end of list */
2086 /* make sure len makes sense */
2087 if (nvsize < NVP_SIZE_CALC(1, 0))
2090 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2093 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2094 nvp_buf_free(nvl, nvp);
2098 if (i_validate_nvpair(nvp) != 0) {
2100 nvp_buf_free(nvl, nvp);
2104 nvp_buf_link(nvl, nvp);
2110 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2112 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2114 uint64_t nvsize = *buflen;
2118 * Get encoded size of nvpairs in nvlist
2120 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2121 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2124 if ((nvsize += size) > INT32_MAX)
2133 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2137 if (nvl->nvl_priv == 0)
2141 * Perform the operation, starting with header, then each nvpair
2143 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2146 switch (nvs->nvs_op) {
2148 err = nvs_encode_pairs(nvs, nvl);
2152 err = nvs_decode_pairs(nvs, nvl);
2155 case NVS_OP_GETSIZE:
2156 err = nvs_getsize_pairs(nvs, nvl, buflen);
2167 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2169 switch (nvs->nvs_op) {
2171 return (nvs_operation(nvs, embedded, NULL));
2173 case NVS_OP_DECODE: {
2177 if (embedded->nvl_version != NV_VERSION)
2180 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2183 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2185 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2186 nvlist_free(embedded);
2197 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2199 size_t nelem = NVP_NELEM(nvp);
2200 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2203 switch (nvs->nvs_op) {
2205 for (i = 0; i < nelem; i++)
2206 if (nvs_embedded(nvs, nvlp[i]) != 0)
2210 case NVS_OP_DECODE: {
2211 size_t len = nelem * sizeof (uint64_t);
2212 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2214 bzero(nvlp, len); /* don't trust packed data */
2215 for (i = 0; i < nelem; i++) {
2216 if (nvs_embedded(nvs, embedded) != 0) {
2221 nvlp[i] = embedded++;
2225 case NVS_OP_GETSIZE: {
2226 uint64_t nvsize = 0;
2228 for (i = 0; i < nelem; i++) {
2231 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2234 if ((nvsize += nvp_sz) > INT32_MAX)
2248 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2249 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2252 * Common routine for nvlist operations:
2253 * encode, decode, getsize (encoded size).
2256 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2262 #if BYTE_ORDER == _LITTLE_ENDIAN
2263 int host_endian = 1;
2265 int host_endian = 0;
2266 #endif /* _LITTLE_ENDIAN */
2267 nvs_header_t *nvh = (void *)buf;
2269 if (buflen == NULL || nvl == NULL ||
2270 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2273 nvs.nvs_op = nvs_op;
2276 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2277 * a buffer is allocated. The first 4 bytes in the buffer are
2278 * used for encoding method and host endian.
2282 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2285 nvh->nvh_encoding = encoding;
2286 nvh->nvh_endian = nvl_endian = host_endian;
2287 nvh->nvh_reserved1 = 0;
2288 nvh->nvh_reserved2 = 0;
2292 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2295 /* get method of encoding from first byte */
2296 encoding = nvh->nvh_encoding;
2297 nvl_endian = nvh->nvh_endian;
2300 case NVS_OP_GETSIZE:
2301 nvl_endian = host_endian;
2304 * add the size for encoding
2306 *buflen = sizeof (nvs_header_t);
2314 * Create an nvstream with proper encoding method
2317 case NV_ENCODE_NATIVE:
2319 * check endianness, in case we are unpacking
2322 if (nvl_endian != host_endian)
2324 err = nvs_native(&nvs, nvl, buf, buflen);
2327 err = nvs_xdr(&nvs, nvl, buf, buflen);
2338 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2340 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2344 * Pack nvlist into contiguous memory
2348 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2351 #if defined(_KERNEL) && !defined(_BOOT)
2352 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2353 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2355 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2360 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2368 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2372 return (nvlist_common(nvl, *bufp, buflen, encoding,
2376 * Here is a difficult situation:
2377 * 1. The nvlist has fixed allocator properties.
2378 * All other nvlist routines (like nvlist_add_*, ...) use
2380 * 2. When using nvlist_pack() the user can specify his own
2381 * allocator properties (e.g. by using KM_NOSLEEP).
2383 * We use the user specified properties (2). A clearer solution
2384 * will be to remove the kmflag from nvlist_pack(), but we will
2385 * not change the interface.
2387 nv_priv_init(&nvpriv, nva, 0);
2389 if (err = nvlist_size(nvl, &alloc_size, encoding))
2392 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2395 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2396 NVS_OP_ENCODE)) != 0) {
2397 nv_mem_free(&nvpriv, buf, alloc_size);
2399 *buflen = alloc_size;
2407 * Unpack buf into an nvlist_t
2411 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2413 #if defined(_KERNEL) && !defined(_BOOT)
2414 return (nvlist_xunpack(buf, buflen, nvlp,
2415 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2417 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2422 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2430 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2433 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2442 * Native encoding functions
2446 * This structure is used when decoding a packed nvpair in
2447 * the native format. n_base points to a buffer containing the
2448 * packed nvpair. n_end is a pointer to the end of the buffer.
2449 * (n_end actually points to the first byte past the end of the
2450 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2451 * It points to the current data that we are decoding.
2452 * The amount of data left in the buffer is equal to n_end - n_curr.
2453 * n_flag is used to recognize a packed embedded list.
2462 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2465 switch (nvs->nvs_op) {
2468 nvs->nvs_private = native;
2469 native->n_curr = native->n_base = buf;
2470 native->n_end = buf + buflen;
2474 case NVS_OP_GETSIZE:
2475 nvs->nvs_private = native;
2476 native->n_curr = native->n_base = native->n_end = NULL;
2486 nvs_native_destroy(nvstream_t *nvs)
2491 native_cp(nvstream_t *nvs, void *buf, size_t size)
2493 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2495 if (native->n_curr + size > native->n_end)
2499 * The bcopy() below eliminates alignment requirement
2500 * on the buffer (stream) and is preferred over direct access.
2502 switch (nvs->nvs_op) {
2504 bcopy(buf, native->n_curr, size);
2507 bcopy(native->n_curr, buf, size);
2513 native->n_curr += size;
2518 * operate on nvlist_t header
2521 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2523 nvs_native_t *native = nvs->nvs_private;
2525 switch (nvs->nvs_op) {
2529 return (0); /* packed embedded list */
2533 /* copy version and nvflag of the nvlist_t */
2534 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2535 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2540 case NVS_OP_GETSIZE:
2542 * if calculate for packed embedded list
2543 * 4 for end of the embedded list
2545 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2546 * and 4 for end of the entire list
2548 if (native->n_flag) {
2552 *size += 2 * sizeof (int32_t) + 4;
2563 nvs_native_nvl_fini(nvstream_t *nvs)
2565 if (nvs->nvs_op == NVS_OP_ENCODE) {
2566 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2568 * Add 4 zero bytes at end of nvlist. They are used
2569 * for end detection by the decode routine.
2571 if (native->n_curr + sizeof (int) > native->n_end)
2574 bzero(native->n_curr, sizeof (int));
2575 native->n_curr += sizeof (int);
2582 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2584 if (nvs->nvs_op == NVS_OP_ENCODE) {
2585 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2586 char *packed = (void *)
2587 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2589 * Null out the pointer that is meaningless in the packed
2590 * structure. The address may not be aligned, so we have
2593 bzero(packed + offsetof(nvlist_t, nvl_priv),
2594 sizeof(((nvlist_t *)NULL)->nvl_priv));
2597 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2601 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2603 if (nvs->nvs_op == NVS_OP_ENCODE) {
2604 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2605 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2606 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2609 * Null out pointers that are meaningless in the packed
2610 * structure. The addresses may not be aligned, so we have
2616 for (i = 0; i < NVP_NELEM(nvp); i++) {
2618 * Null out the pointer that is meaningless in the
2619 * packed structure. The address may not be aligned,
2620 * so we have to use bzero.
2622 bzero(value + offsetof(nvlist_t, nvl_priv),
2623 sizeof(((nvlist_t *)NULL)->nvl_priv));
2624 value += sizeof(nvlist_t);
2628 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2632 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2634 switch (nvs->nvs_op) {
2635 case NVS_OP_ENCODE: {
2636 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2637 uint64_t *strp = (void *)
2638 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2640 * Null out pointers that are meaningless in the packed
2641 * structure. The addresses may not be aligned, so we have
2644 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2647 case NVS_OP_DECODE: {
2648 char **strp = (void *)NVP_VALUE(nvp);
2649 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2652 for (i = 0; i < NVP_NELEM(nvp); i++) {
2654 buf += strlen(buf) + 1;
2662 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2669 * We do the initial bcopy of the data before we look at
2670 * the nvpair type, because when we're decoding, we won't
2671 * have the correct values for the pair until we do the bcopy.
2673 switch (nvs->nvs_op) {
2676 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2683 /* verify nvp_name_sz, check the name string length */
2684 if (i_validate_nvpair_name(nvp) != 0)
2687 type = NVP_TYPE(nvp);
2690 * Verify type and nelem and get the value size.
2691 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2692 * is the size of the string(s) excluded.
2694 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2697 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2701 case DATA_TYPE_NVLIST:
2702 ret = nvpair_native_embedded(nvs, nvp);
2704 case DATA_TYPE_NVLIST_ARRAY:
2705 ret = nvpair_native_embedded_array(nvs, nvp);
2707 case DATA_TYPE_STRING_ARRAY:
2708 nvpair_native_string_array(nvs, nvp);
2718 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2720 uint64_t nvp_sz = nvp->nvp_size;
2722 switch (NVP_TYPE(nvp)) {
2723 case DATA_TYPE_NVLIST: {
2726 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2732 case DATA_TYPE_NVLIST_ARRAY: {
2735 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2745 if (nvp_sz > INT32_MAX)
2754 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2756 switch (nvs->nvs_op) {
2758 return (nvs_native_nvp_op(nvs, nvp));
2760 case NVS_OP_DECODE: {
2761 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2764 /* try to read the size value from the stream */
2765 if (native->n_curr + sizeof (int32_t) > native->n_end)
2767 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2769 /* sanity check the size value */
2770 if (decode_len < 0 ||
2771 decode_len > native->n_end - native->n_curr)
2777 * If at the end of the stream then move the cursor
2778 * forward, otherwise nvpair_native_op() will read
2779 * the entire nvpair at the same cursor position.
2782 native->n_curr += sizeof (int32_t);
2793 static const nvs_ops_t nvs_native_ops = {
2797 nvs_native_nvp_size,
2802 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2804 nvs_native_t native;
2807 nvs->nvs_ops = &nvs_native_ops;
2809 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2810 *buflen - sizeof (nvs_header_t))) != 0)
2813 err = nvs_operation(nvs, nvl, buflen);
2815 nvs_native_destroy(nvs);
2821 * XDR encoding functions
2823 * An xdr packed nvlist is encoded as:
2825 * - encoding methode and host endian (4 bytes)
2826 * - nvl_version (4 bytes)
2827 * - nvl_nvflag (4 bytes)
2829 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2830 * - encoded size of the nvpair (4 bytes)
2831 * - decoded size of the nvpair (4 bytes)
2832 * - name string, (4 + sizeof(NV_ALIGN4(string))
2833 * a string is coded as size (4 bytes) and data
2834 * - data type (4 bytes)
2835 * - number of elements in the nvpair (4 bytes)
2838 * - 2 zero's for end of the entire list (8 bytes)
2841 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2843 /* xdr data must be 4 byte aligned */
2844 if ((ulong_t)buf % 4 != 0)
2847 switch (nvs->nvs_op) {
2849 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2850 nvs->nvs_private = xdr;
2853 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2854 nvs->nvs_private = xdr;
2856 case NVS_OP_GETSIZE:
2857 nvs->nvs_private = NULL;
2865 nvs_xdr_destroy(nvstream_t *nvs)
2867 switch (nvs->nvs_op) {
2870 xdr_destroy((XDR *)nvs->nvs_private);
2878 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2880 switch (nvs->nvs_op) {
2882 case NVS_OP_DECODE: {
2883 XDR *xdr = nvs->nvs_private;
2885 if (!xdr_int(xdr, &nvl->nvl_version) ||
2886 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2890 case NVS_OP_GETSIZE: {
2892 * 2 * 4 for nvl_version + nvl_nvflag
2893 * and 8 for end of the entire list
2905 nvs_xdr_nvl_fini(nvstream_t *nvs)
2907 if (nvs->nvs_op == NVS_OP_ENCODE) {
2908 XDR *xdr = nvs->nvs_private;
2911 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2919 * The format of xdr encoded nvpair is:
2920 * encode_size, decode_size, name string, data type, nelem, data
2923 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2927 char *buf_end = (char *)nvp + nvp->nvp_size;
2929 uint_t nelem, buflen;
2931 XDR *xdr = nvs->nvs_private;
2933 ASSERT(xdr != NULL && nvp != NULL);
2936 if ((buf = NVP_NAME(nvp)) >= buf_end)
2938 buflen = buf_end - buf;
2940 if (!xdr_string(xdr, &buf, buflen - 1))
2942 nvp->nvp_name_sz = strlen(buf) + 1;
2944 /* type and nelem */
2945 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2946 !xdr_int(xdr, &nvp->nvp_value_elem))
2949 type = NVP_TYPE(nvp);
2950 nelem = nvp->nvp_value_elem;
2953 * Verify type and nelem and get the value size.
2954 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2955 * is the size of the string(s) excluded.
2957 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2960 /* if there is no data to extract then return */
2965 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2967 buflen = buf_end - buf;
2969 if (buflen < value_sz)
2973 case DATA_TYPE_NVLIST:
2974 if (nvs_embedded(nvs, (void *)buf) == 0)
2978 case DATA_TYPE_NVLIST_ARRAY:
2979 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2983 case DATA_TYPE_BOOLEAN:
2987 case DATA_TYPE_BYTE:
2988 case DATA_TYPE_INT8:
2989 case DATA_TYPE_UINT8:
2990 ret = xdr_char(xdr, buf);
2993 case DATA_TYPE_INT16:
2994 ret = xdr_short(xdr, (void *)buf);
2997 case DATA_TYPE_UINT16:
2998 ret = xdr_u_short(xdr, (void *)buf);
3001 case DATA_TYPE_BOOLEAN_VALUE:
3002 case DATA_TYPE_INT32:
3003 ret = xdr_int(xdr, (void *)buf);
3006 case DATA_TYPE_UINT32:
3007 ret = xdr_u_int(xdr, (void *)buf);
3010 case DATA_TYPE_INT64:
3011 ret = xdr_longlong_t(xdr, (void *)buf);
3014 case DATA_TYPE_UINT64:
3015 ret = xdr_u_longlong_t(xdr, (void *)buf);
3018 case DATA_TYPE_HRTIME:
3020 * NOTE: must expose the definition of hrtime_t here
3022 ret = xdr_longlong_t(xdr, (void *)buf);
3024 #if !defined(_KERNEL)
3025 case DATA_TYPE_DOUBLE:
3026 ret = xdr_double(xdr, (void *)buf);
3029 case DATA_TYPE_STRING:
3030 ret = xdr_string(xdr, &buf, buflen - 1);
3033 case DATA_TYPE_BYTE_ARRAY:
3034 ret = xdr_opaque(xdr, buf, nelem);
3037 case DATA_TYPE_INT8_ARRAY:
3038 case DATA_TYPE_UINT8_ARRAY:
3039 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3040 (xdrproc_t)xdr_char);
3043 case DATA_TYPE_INT16_ARRAY:
3044 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3045 sizeof (int16_t), (xdrproc_t)xdr_short);
3048 case DATA_TYPE_UINT16_ARRAY:
3049 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3050 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3053 case DATA_TYPE_BOOLEAN_ARRAY:
3054 case DATA_TYPE_INT32_ARRAY:
3055 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3056 sizeof (int32_t), (xdrproc_t)xdr_int);
3059 case DATA_TYPE_UINT32_ARRAY:
3060 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3061 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3064 case DATA_TYPE_INT64_ARRAY:
3065 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3066 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3069 case DATA_TYPE_UINT64_ARRAY:
3070 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3071 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3074 case DATA_TYPE_STRING_ARRAY: {
3075 size_t len = nelem * sizeof (uint64_t);
3076 char **strp = (void *)buf;
3079 if (nvs->nvs_op == NVS_OP_DECODE)
3080 bzero(buf, len); /* don't trust packed data */
3082 for (i = 0; i < nelem; i++) {
3089 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3092 if (nvs->nvs_op == NVS_OP_DECODE)
3094 len = strlen(buf) + 1;
3103 return (ret == TRUE ? 0 : EFAULT);
3107 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3109 data_type_t type = NVP_TYPE(nvp);
3111 * encode_size + decode_size + name string size + data type + nelem
3112 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3114 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3117 case DATA_TYPE_BOOLEAN:
3120 case DATA_TYPE_BOOLEAN_VALUE:
3121 case DATA_TYPE_BYTE:
3122 case DATA_TYPE_INT8:
3123 case DATA_TYPE_UINT8:
3124 case DATA_TYPE_INT16:
3125 case DATA_TYPE_UINT16:
3126 case DATA_TYPE_INT32:
3127 case DATA_TYPE_UINT32:
3128 nvp_sz += 4; /* 4 is the minimum xdr unit */
3131 case DATA_TYPE_INT64:
3132 case DATA_TYPE_UINT64:
3133 case DATA_TYPE_HRTIME:
3134 #if !defined(_KERNEL)
3135 case DATA_TYPE_DOUBLE:
3140 case DATA_TYPE_STRING:
3141 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3144 case DATA_TYPE_BYTE_ARRAY:
3145 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3148 case DATA_TYPE_BOOLEAN_ARRAY:
3149 case DATA_TYPE_INT8_ARRAY:
3150 case DATA_TYPE_UINT8_ARRAY:
3151 case DATA_TYPE_INT16_ARRAY:
3152 case DATA_TYPE_UINT16_ARRAY:
3153 case DATA_TYPE_INT32_ARRAY:
3154 case DATA_TYPE_UINT32_ARRAY:
3155 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3158 case DATA_TYPE_INT64_ARRAY:
3159 case DATA_TYPE_UINT64_ARRAY:
3160 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3163 case DATA_TYPE_STRING_ARRAY: {
3165 char **strs = (void *)NVP_VALUE(nvp);
3167 for (i = 0; i < NVP_NELEM(nvp); i++)
3168 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3173 case DATA_TYPE_NVLIST:
3174 case DATA_TYPE_NVLIST_ARRAY: {
3176 int old_nvs_op = nvs->nvs_op;
3179 nvs->nvs_op = NVS_OP_GETSIZE;
3180 if (type == DATA_TYPE_NVLIST)
3181 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3183 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3184 nvs->nvs_op = old_nvs_op;
3197 if (nvp_sz > INT32_MAX)
3207 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3208 * the largest nvpair that could be encoded in the buffer.
3210 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3211 * The size of a xdr packed nvpair without any data is 5 words.
3213 * Using the size of the data directly as an estimate would be ok
3214 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3215 * then the actual nvpair has space for an array of pointers to index
3216 * the strings. These pointers are not encoded into the packed xdr buffer.
3218 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3219 * of length 0, then each string is endcoded in xdr format as a single word.
3220 * Therefore when expanded to an nvpair there will be 2.25 word used for
3221 * each string. (a int64_t allocated for pointer usage, and a single char
3222 * for the null termination.)
3224 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3226 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3227 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3228 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3229 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3230 (NVS_XDR_DATA_LEN(x) * 2) + \
3231 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3234 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3236 XDR *xdr = nvs->nvs_private;
3237 int32_t encode_len, decode_len;
3239 switch (nvs->nvs_op) {
3240 case NVS_OP_ENCODE: {
3243 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3246 decode_len = nvp->nvp_size;
3247 encode_len = nvsize;
3248 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3251 return (nvs_xdr_nvp_op(nvs, nvp));
3253 case NVS_OP_DECODE: {
3254 struct xdr_bytesrec bytesrec;
3256 /* get the encode and decode size */
3257 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3261 /* are we at the end of the stream? */
3265 /* sanity check the size parameter */
3266 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3269 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3280 static const struct nvs_ops nvs_xdr_ops = {
3289 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3294 nvs->nvs_ops = &nvs_xdr_ops;
3296 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3297 *buflen - sizeof (nvs_header_t))) != 0)
3300 err = nvs_operation(nvs, nvl, buflen);
3302 nvs_xdr_destroy(nvs);