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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/stropts.h>
28 #include <sys/debug.h>
29 #include <sys/isa_defs.h>
30 #include <sys/int_limits.h>
31 #include <sys/nvpair.h>
32 #include <sys/nvpair_impl.h>
33 #include <rpc/types.h>
36 #if defined(_KERNEL) && !defined(_BOOT)
37 #include <sys/varargs.h>
39 #include <sys/sunddi.h>
48 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
50 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
53 * nvpair.c - Provides kernel & userland interfaces for manipulating
68 * +--------------+ last i_nvp in list
69 * | nvpriv_t | +--------------------->
71 * +--+- nvp_list | | +------------+
72 * | | nvp_last -+--+ + nv_alloc_t |
73 * | | nvp_curr | |------------|
74 * | | nvp_nva -+----> | nva_ops |
75 * | | nvp_stat | | nva_arg |
76 * | +--------------+ +------------+
80 * +---------------------+ +-------------------+
81 * | i_nvp_t | +-->| i_nvp_t | +-->
82 * |---------------------| | |-------------------| |
83 * | nvi_next -+--+ | nvi_next -+--+
84 * | nvi_prev (NULL) | <----+ nvi_prev |
85 * | . . . . . . . . . . | | . . . . . . . . . |
86 * | nvp (nvpair_t) | | nvp (nvpair_t) |
87 * | - nvp_size | | - nvp_size |
88 * | - nvp_name_sz | | - nvp_name_sz |
89 * | - nvp_value_elem | | - nvp_value_elem |
90 * | - nvp_type | | - nvp_type |
91 * | - data ... | | - data ... |
92 * +---------------------+ +-------------------+
96 * +---------------------+ +---------------------+
97 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
98 * |---------------------| | | |---------------------|
99 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
100 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
101 * | . . . . . . . . . | | . . . . . . . . . |
102 * | nvp (nvpair_t) | | nvp (nvpair_t) |
103 * | - nvp_size | | - nvp_size |
104 * | - nvp_name_sz | | - nvp_name_sz |
105 * | - nvp_value_elem | | - nvp_value_elem |
106 * | - DATA_TYPE_NVLIST | | - nvp_type |
107 * | - data (embedded) | | - data ... |
108 * | nvlist name | +---------------------+
109 * | +--------------+ |
111 * | |--------------| |
112 * | | nvl_version | |
114 * | | nvl_priv --+---+---->
117 * | +--------------+ |
118 * +---------------------+
121 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
122 * allow value to be aligned on 8 byte boundary
124 * name_len is the length of the name string including the null terminator
127 #define NVP_SIZE_CALC(name_len, data_len) \
128 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
130 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
131 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
132 uint_t nelem, const void *data);
134 #define NV_STAT_EMBEDDED 0x1
135 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
136 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
138 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
139 #define NVPAIR2I_NVP(nvp) \
140 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
144 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
152 va_start(valist, nvo);
153 if (nva->nva_ops->nv_ao_init != NULL)
154 err = nva->nva_ops->nv_ao_init(nva, valist);
161 nv_alloc_reset(nv_alloc_t *nva)
163 if (nva->nva_ops->nv_ao_reset != NULL)
164 nva->nva_ops->nv_ao_reset(nva);
168 nv_alloc_fini(nv_alloc_t *nva)
170 if (nva->nva_ops->nv_ao_fini != NULL)
171 nva->nva_ops->nv_ao_fini(nva);
175 nvlist_lookup_nv_alloc(nvlist_t *nvl)
180 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
183 return (priv->nvp_nva);
187 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
189 nv_alloc_t *nva = nvp->nvp_nva;
192 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
199 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
201 nv_alloc_t *nva = nvp->nvp_nva;
203 nva->nva_ops->nv_ao_free(nva, buf, size);
207 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
209 bzero(priv, sizeof (nvpriv_t));
212 priv->nvp_stat = stat;
216 nv_priv_alloc(nv_alloc_t *nva)
221 * nv_mem_alloc() cannot called here because it needs the priv
224 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
227 nv_priv_init(priv, nva, 0);
233 * Embedded lists need their own nvpriv_t's. We create a new
234 * nvpriv_t using the parameters and allocator from the parent
238 nv_priv_alloc_embedded(nvpriv_t *priv)
242 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
245 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
251 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
253 nvl->nvl_version = NV_VERSION;
254 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
255 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
261 * nvlist_alloc - Allocate nvlist.
265 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
267 #if defined(_KERNEL) && !defined(_BOOT)
268 return (nvlist_xalloc(nvlp, nvflag,
269 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
271 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
276 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
280 if (nvlp == NULL || nva == NULL)
283 if ((priv = nv_priv_alloc(nva)) == NULL)
286 if ((*nvlp = nv_mem_zalloc(priv,
287 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
288 nv_mem_free(priv, priv, sizeof (nvpriv_t));
292 nvlist_init(*nvlp, nvflag, priv);
298 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
301 nvp_buf_alloc(nvlist_t *nvl, size_t len)
303 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
309 * Allocate the buffer
311 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
313 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
323 * nvp_buf_free - de-Allocate an i_nvp_t.
326 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
328 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
329 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
331 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
335 * nvp_buf_link - link a new nv pair into the nvlist.
338 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
340 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
341 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
343 /* Put element at end of nvlist */
344 if (priv->nvp_list == NULL) {
345 priv->nvp_list = priv->nvp_last = curr;
347 curr->nvi_prev = priv->nvp_last;
348 priv->nvp_last->nvi_next = curr;
349 priv->nvp_last = curr;
354 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
357 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
359 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
360 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
363 * protect nvlist_next_nvpair() against walking on freed memory.
365 if (priv->nvp_curr == curr)
366 priv->nvp_curr = curr->nvi_next;
368 if (curr == priv->nvp_list)
369 priv->nvp_list = curr->nvi_next;
371 curr->nvi_prev->nvi_next = curr->nvi_next;
373 if (curr == priv->nvp_last)
374 priv->nvp_last = curr->nvi_prev;
376 curr->nvi_next->nvi_prev = curr->nvi_prev;
380 * take a nvpair type and number of elements and make sure the are valid
383 i_validate_type_nelem(data_type_t type, uint_t nelem)
386 case DATA_TYPE_BOOLEAN:
390 case DATA_TYPE_BOOLEAN_VALUE:
393 case DATA_TYPE_UINT8:
394 case DATA_TYPE_INT16:
395 case DATA_TYPE_UINT16:
396 case DATA_TYPE_INT32:
397 case DATA_TYPE_UINT32:
398 case DATA_TYPE_INT64:
399 case DATA_TYPE_UINT64:
400 case DATA_TYPE_STRING:
401 case DATA_TYPE_HRTIME:
402 case DATA_TYPE_NVLIST:
403 #if !defined(_KERNEL)
404 case DATA_TYPE_DOUBLE:
409 case DATA_TYPE_BOOLEAN_ARRAY:
410 case DATA_TYPE_BYTE_ARRAY:
411 case DATA_TYPE_INT8_ARRAY:
412 case DATA_TYPE_UINT8_ARRAY:
413 case DATA_TYPE_INT16_ARRAY:
414 case DATA_TYPE_UINT16_ARRAY:
415 case DATA_TYPE_INT32_ARRAY:
416 case DATA_TYPE_UINT32_ARRAY:
417 case DATA_TYPE_INT64_ARRAY:
418 case DATA_TYPE_UINT64_ARRAY:
419 case DATA_TYPE_STRING_ARRAY:
420 case DATA_TYPE_NVLIST_ARRAY:
421 /* we allow arrays with 0 elements */
430 * Verify nvp_name_sz and check the name string length.
433 i_validate_nvpair_name(nvpair_t *nvp)
435 if ((nvp->nvp_name_sz <= 0) ||
436 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
439 /* verify the name string, make sure its terminated */
440 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
443 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
447 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
450 case DATA_TYPE_BOOLEAN_VALUE:
451 if (*(boolean_t *)data != B_TRUE &&
452 *(boolean_t *)data != B_FALSE)
455 case DATA_TYPE_BOOLEAN_ARRAY: {
458 for (i = 0; i < nelem; i++)
459 if (((boolean_t *)data)[i] != B_TRUE &&
460 ((boolean_t *)data)[i] != B_FALSE)
472 * This function takes a pointer to what should be a nvpair and it's size
473 * and then verifies that all the nvpair fields make sense and can be
474 * trusted. This function is used when decoding packed nvpairs.
477 i_validate_nvpair(nvpair_t *nvp)
479 data_type_t type = NVP_TYPE(nvp);
482 /* verify nvp_name_sz, check the name string length */
483 if (i_validate_nvpair_name(nvp) != 0)
486 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
490 * verify nvp_type, nvp_value_elem, and also possibly
491 * verify string values and get the value size.
493 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
494 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
495 if (size2 < 0 || size1 != NV_ALIGN(size2))
502 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
507 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
510 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
511 nvpair_t *nvp = &curr->nvi_nvp;
514 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
515 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
523 * Frees all memory allocated for an nvpair (like embedded lists) with
524 * the exception of the nvpair buffer itself.
527 nvpair_free(nvpair_t *nvp)
529 switch (NVP_TYPE(nvp)) {
530 case DATA_TYPE_NVLIST:
531 nvlist_free(EMBEDDED_NVL(nvp));
533 case DATA_TYPE_NVLIST_ARRAY: {
534 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
537 for (i = 0; i < NVP_NELEM(nvp); i++)
539 nvlist_free(nvlp[i]);
548 * nvlist_free - free an unpacked nvlist
551 nvlist_free(nvlist_t *nvl)
557 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
561 * Unpacked nvlist are linked through i_nvp_t
563 curr = priv->nvp_list;
564 while (curr != NULL) {
565 nvpair_t *nvp = &curr->nvi_nvp;
566 curr = curr->nvi_next;
569 nvp_buf_free(nvl, nvp);
572 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
573 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
577 nv_mem_free(priv, priv, sizeof (nvpriv_t));
581 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
583 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
589 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
590 if (&curr->nvi_nvp == nvp)
597 * Make a copy of nvlist
601 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
603 #if defined(_KERNEL) && !defined(_BOOT)
604 return (nvlist_xdup(nvl, nvlp,
605 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
607 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
612 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
617 if (nvl == NULL || nvlp == NULL)
620 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
623 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
632 * Remove all with matching name
635 nvlist_remove_all(nvlist_t *nvl, const char *name)
641 if (nvl == NULL || name == NULL ||
642 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
645 curr = priv->nvp_list;
646 while (curr != NULL) {
647 nvpair_t *nvp = &curr->nvi_nvp;
649 curr = curr->nvi_next;
650 if (strcmp(name, NVP_NAME(nvp)) != 0)
653 nvp_buf_unlink(nvl, nvp);
655 nvp_buf_free(nvl, nvp);
664 * Remove first one with matching name and type
667 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
672 if (nvl == NULL || name == NULL ||
673 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
676 curr = priv->nvp_list;
677 while (curr != NULL) {
678 nvpair_t *nvp = &curr->nvi_nvp;
680 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
681 nvp_buf_unlink(nvl, nvp);
683 nvp_buf_free(nvl, nvp);
687 curr = curr->nvi_next;
694 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
696 if (nvl == NULL || nvp == NULL)
699 nvp_buf_unlink(nvl, nvp);
701 nvp_buf_free(nvl, nvp);
706 * This function calculates the size of an nvpair value.
708 * The data argument controls the behavior in case of the data types
709 * DATA_TYPE_STRING and
710 * DATA_TYPE_STRING_ARRAY
711 * Is data == NULL then the size of the string(s) is excluded.
714 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
718 if (i_validate_type_nelem(type, nelem) != 0)
721 /* Calculate required size for holding value */
723 case DATA_TYPE_BOOLEAN:
726 case DATA_TYPE_BOOLEAN_VALUE:
727 value_sz = sizeof (boolean_t);
730 value_sz = sizeof (uchar_t);
733 value_sz = sizeof (int8_t);
735 case DATA_TYPE_UINT8:
736 value_sz = sizeof (uint8_t);
738 case DATA_TYPE_INT16:
739 value_sz = sizeof (int16_t);
741 case DATA_TYPE_UINT16:
742 value_sz = sizeof (uint16_t);
744 case DATA_TYPE_INT32:
745 value_sz = sizeof (int32_t);
747 case DATA_TYPE_UINT32:
748 value_sz = sizeof (uint32_t);
750 case DATA_TYPE_INT64:
751 value_sz = sizeof (int64_t);
753 case DATA_TYPE_UINT64:
754 value_sz = sizeof (uint64_t);
756 #if !defined(_KERNEL)
757 case DATA_TYPE_DOUBLE:
758 value_sz = sizeof (double);
761 case DATA_TYPE_STRING:
765 value_sz = strlen(data) + 1;
767 case DATA_TYPE_BOOLEAN_ARRAY:
768 value_sz = (uint64_t)nelem * sizeof (boolean_t);
770 case DATA_TYPE_BYTE_ARRAY:
771 value_sz = (uint64_t)nelem * sizeof (uchar_t);
773 case DATA_TYPE_INT8_ARRAY:
774 value_sz = (uint64_t)nelem * sizeof (int8_t);
776 case DATA_TYPE_UINT8_ARRAY:
777 value_sz = (uint64_t)nelem * sizeof (uint8_t);
779 case DATA_TYPE_INT16_ARRAY:
780 value_sz = (uint64_t)nelem * sizeof (int16_t);
782 case DATA_TYPE_UINT16_ARRAY:
783 value_sz = (uint64_t)nelem * sizeof (uint16_t);
785 case DATA_TYPE_INT32_ARRAY:
786 value_sz = (uint64_t)nelem * sizeof (int32_t);
788 case DATA_TYPE_UINT32_ARRAY:
789 value_sz = (uint64_t)nelem * sizeof (uint32_t);
791 case DATA_TYPE_INT64_ARRAY:
792 value_sz = (uint64_t)nelem * sizeof (int64_t);
794 case DATA_TYPE_UINT64_ARRAY:
795 value_sz = (uint64_t)nelem * sizeof (uint64_t);
797 case DATA_TYPE_STRING_ARRAY:
798 value_sz = (uint64_t)nelem * sizeof (uint64_t);
801 char *const *strs = data;
804 /* no alignment requirement for strings */
805 for (i = 0; i < nelem; i++) {
808 value_sz += strlen(strs[i]) + 1;
812 case DATA_TYPE_HRTIME:
813 value_sz = sizeof (hrtime_t);
815 case DATA_TYPE_NVLIST:
816 value_sz = NV_ALIGN(sizeof (nvlist_t));
818 case DATA_TYPE_NVLIST_ARRAY:
819 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
820 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
826 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
830 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
835 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
836 nvl->nvl_priv)) == NULL)
839 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
841 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
842 nvlist_free(emb_nvl);
843 emb_nvl->nvl_priv = 0;
850 * nvlist_add_common - Add new <name,value> pair to nvlist
853 nvlist_add_common(nvlist_t *nvl, const char *name,
854 data_type_t type, uint_t nelem, const void *data)
859 int nvp_sz, name_sz, value_sz;
862 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
865 if (nelem != 0 && data == NULL)
869 * Verify type and nelem and get the value size.
870 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
871 * is the size of the string(s) included.
873 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
876 if (i_validate_nvpair_value(type, nelem, data) != 0)
880 * If we're adding an nvlist or nvlist array, ensure that we are not
881 * adding the input nvlist to itself, which would cause recursion,
882 * and ensure that no NULL nvlist pointers are present.
885 case DATA_TYPE_NVLIST:
886 if (data == nvl || data == NULL)
889 case DATA_TYPE_NVLIST_ARRAY: {
890 nvlist_t **onvlp = (nvlist_t **)data;
891 for (i = 0; i < nelem; i++) {
892 if (onvlp[i] == nvl || onvlp[i] == NULL)
901 /* calculate sizes of the nvpair elements and the nvpair itself */
902 name_sz = strlen(name) + 1;
904 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
906 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
909 ASSERT(nvp->nvp_size == nvp_sz);
910 nvp->nvp_name_sz = name_sz;
911 nvp->nvp_value_elem = nelem;
912 nvp->nvp_type = type;
913 bcopy(name, NVP_NAME(nvp), name_sz);
916 case DATA_TYPE_BOOLEAN:
918 case DATA_TYPE_STRING_ARRAY: {
919 char *const *strs = data;
920 char *buf = NVP_VALUE(nvp);
921 char **cstrs = (void *)buf;
923 /* skip pre-allocated space for pointer array */
924 buf += nelem * sizeof (uint64_t);
925 for (i = 0; i < nelem; i++) {
926 int slen = strlen(strs[i]) + 1;
927 bcopy(strs[i], buf, slen);
933 case DATA_TYPE_NVLIST: {
934 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
935 nvlist_t *onvl = (nvlist_t *)data;
937 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
938 nvp_buf_free(nvl, nvp);
943 case DATA_TYPE_NVLIST_ARRAY: {
944 nvlist_t **onvlp = (nvlist_t **)data;
945 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
946 nvlist_t *embedded = (nvlist_t *)
947 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
949 for (i = 0; i < nelem; i++) {
950 if ((err = nvlist_copy_embedded(nvl,
951 onvlp[i], embedded)) != 0) {
953 * Free any successfully created lists
956 nvp_buf_free(nvl, nvp);
960 nvlp[i] = embedded++;
965 bcopy(data, NVP_VALUE(nvp), value_sz);
968 /* if unique name, remove before add */
969 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
970 (void) nvlist_remove_all(nvl, name);
971 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
972 (void) nvlist_remove(nvl, name, type);
974 nvp_buf_link(nvl, nvp);
980 nvlist_add_boolean(nvlist_t *nvl, const char *name)
982 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
986 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
988 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
992 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
994 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
998 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1000 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1004 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1006 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1010 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1012 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1016 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1018 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1022 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1024 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1028 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1030 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1034 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1036 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1040 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1042 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1045 #if !defined(_KERNEL)
1047 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1049 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1054 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1056 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1060 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1061 boolean_t *a, uint_t n)
1063 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1067 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1069 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1073 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1075 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1079 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1081 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1085 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1087 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1091 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1093 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1097 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1099 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1103 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1105 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1109 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1111 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1115 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1117 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1121 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1122 char *const *a, uint_t n)
1124 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1128 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1130 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1134 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1136 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1140 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1142 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1145 /* reading name-value pairs */
1147 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1153 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1156 curr = NVPAIR2I_NVP(nvp);
1159 * Ensure that nvp is a valid nvpair on this nvlist.
1160 * NB: nvp_curr is used only as a hint so that we don't always
1161 * have to walk the list to determine if nvp is still on the list.
1164 curr = priv->nvp_list;
1165 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1166 curr = curr->nvi_next;
1170 priv->nvp_curr = curr;
1172 return (curr != NULL ? &curr->nvi_nvp : NULL);
1176 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1182 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1185 curr = NVPAIR2I_NVP(nvp);
1188 curr = priv->nvp_last;
1189 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1190 curr = curr->nvi_prev;
1194 priv->nvp_curr = curr;
1196 return (curr != NULL ? &curr->nvi_nvp : NULL);
1200 nvlist_empty(nvlist_t *nvl)
1205 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1208 return (priv->nvp_list == NULL);
1212 nvpair_name(nvpair_t *nvp)
1214 return (NVP_NAME(nvp));
1218 nvpair_type(nvpair_t *nvp)
1220 return (NVP_TYPE(nvp));
1224 nvpair_type_is_array(nvpair_t *nvp)
1226 data_type_t type = NVP_TYPE(nvp);
1228 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1229 (type == DATA_TYPE_UINT8_ARRAY) ||
1230 (type == DATA_TYPE_INT16_ARRAY) ||
1231 (type == DATA_TYPE_UINT16_ARRAY) ||
1232 (type == DATA_TYPE_INT32_ARRAY) ||
1233 (type == DATA_TYPE_UINT32_ARRAY) ||
1234 (type == DATA_TYPE_INT64_ARRAY) ||
1235 (type == DATA_TYPE_UINT64_ARRAY) ||
1236 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1237 (type == DATA_TYPE_STRING_ARRAY) ||
1238 (type == DATA_TYPE_NVLIST_ARRAY))
1245 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1247 if (nvp == NULL || nvpair_type(nvp) != type)
1251 * For non-array types, we copy the data.
1252 * For array types (including string), we set a pointer.
1255 case DATA_TYPE_BOOLEAN:
1260 case DATA_TYPE_BOOLEAN_VALUE:
1261 case DATA_TYPE_BYTE:
1262 case DATA_TYPE_INT8:
1263 case DATA_TYPE_UINT8:
1264 case DATA_TYPE_INT16:
1265 case DATA_TYPE_UINT16:
1266 case DATA_TYPE_INT32:
1267 case DATA_TYPE_UINT32:
1268 case DATA_TYPE_INT64:
1269 case DATA_TYPE_UINT64:
1270 case DATA_TYPE_HRTIME:
1271 #if !defined(_KERNEL)
1272 case DATA_TYPE_DOUBLE:
1276 bcopy(NVP_VALUE(nvp), data,
1277 (size_t)i_get_value_size(type, NULL, 1));
1282 case DATA_TYPE_NVLIST:
1283 case DATA_TYPE_STRING:
1286 *(void **)data = (void *)NVP_VALUE(nvp);
1291 case DATA_TYPE_BOOLEAN_ARRAY:
1292 case DATA_TYPE_BYTE_ARRAY:
1293 case DATA_TYPE_INT8_ARRAY:
1294 case DATA_TYPE_UINT8_ARRAY:
1295 case DATA_TYPE_INT16_ARRAY:
1296 case DATA_TYPE_UINT16_ARRAY:
1297 case DATA_TYPE_INT32_ARRAY:
1298 case DATA_TYPE_UINT32_ARRAY:
1299 case DATA_TYPE_INT64_ARRAY:
1300 case DATA_TYPE_UINT64_ARRAY:
1301 case DATA_TYPE_STRING_ARRAY:
1302 case DATA_TYPE_NVLIST_ARRAY:
1303 if (nelem == NULL || data == NULL)
1305 if ((*nelem = NVP_NELEM(nvp)) != 0)
1306 *(void **)data = (void *)NVP_VALUE(nvp);
1308 *(void **)data = NULL;
1319 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1320 uint_t *nelem, void *data)
1326 if (name == NULL || nvl == NULL ||
1327 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1330 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1333 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1334 nvp = &curr->nvi_nvp;
1336 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1337 return (nvpair_value_common(nvp, type, nelem, data));
1344 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1346 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1350 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1352 return (nvlist_lookup_common(nvl, name,
1353 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1357 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1359 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1363 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1365 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1369 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1371 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1375 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1377 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1381 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1383 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1387 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1389 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1393 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1395 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1399 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1401 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1405 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1407 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1410 #if !defined(_KERNEL)
1412 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1414 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1419 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1421 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1425 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1427 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1431 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1432 boolean_t **a, uint_t *n)
1434 return (nvlist_lookup_common(nvl, name,
1435 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1439 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1440 uchar_t **a, uint_t *n)
1442 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1446 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1448 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1452 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1453 uint8_t **a, uint_t *n)
1455 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1459 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1460 int16_t **a, uint_t *n)
1462 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1466 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1467 uint16_t **a, uint_t *n)
1469 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1473 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1474 int32_t **a, uint_t *n)
1476 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1480 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1481 uint32_t **a, uint_t *n)
1483 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1487 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1488 int64_t **a, uint_t *n)
1490 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1494 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1495 uint64_t **a, uint_t *n)
1497 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1501 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1502 char ***a, uint_t *n)
1504 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1508 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1509 nvlist_t ***a, uint_t *n)
1511 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1515 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1517 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1521 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1525 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1529 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1534 switch (type = va_arg(ap, data_type_t)) {
1535 case DATA_TYPE_BOOLEAN:
1536 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1539 case DATA_TYPE_BOOLEAN_VALUE:
1540 case DATA_TYPE_BYTE:
1541 case DATA_TYPE_INT8:
1542 case DATA_TYPE_UINT8:
1543 case DATA_TYPE_INT16:
1544 case DATA_TYPE_UINT16:
1545 case DATA_TYPE_INT32:
1546 case DATA_TYPE_UINT32:
1547 case DATA_TYPE_INT64:
1548 case DATA_TYPE_UINT64:
1549 case DATA_TYPE_HRTIME:
1550 case DATA_TYPE_STRING:
1551 case DATA_TYPE_NVLIST:
1552 #if !defined(_KERNEL)
1553 case DATA_TYPE_DOUBLE:
1555 val = va_arg(ap, void *);
1556 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1559 case DATA_TYPE_BYTE_ARRAY:
1560 case DATA_TYPE_BOOLEAN_ARRAY:
1561 case DATA_TYPE_INT8_ARRAY:
1562 case DATA_TYPE_UINT8_ARRAY:
1563 case DATA_TYPE_INT16_ARRAY:
1564 case DATA_TYPE_UINT16_ARRAY:
1565 case DATA_TYPE_INT32_ARRAY:
1566 case DATA_TYPE_UINT32_ARRAY:
1567 case DATA_TYPE_INT64_ARRAY:
1568 case DATA_TYPE_UINT64_ARRAY:
1569 case DATA_TYPE_STRING_ARRAY:
1570 case DATA_TYPE_NVLIST_ARRAY:
1571 val = va_arg(ap, void *);
1572 nelem = va_arg(ap, uint_t *);
1573 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1580 if (ret == ENOENT && noentok)
1589 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1590 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1591 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1592 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1593 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1594 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1595 * code also supports "a.d[3]e[1]" syntax).
1597 * If 'ip' is non-NULL and the last name component is an array, return the
1598 * value of the "...[index]" array index in *ip. For an array reference that
1599 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1600 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1601 * inside the 'name' string where the syntax error was detected.
1604 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1605 nvpair_t **ret, int *ip, char **ep)
1616 *ip = -1; /* not indexed */
1620 if ((nvl == NULL) || (name == NULL))
1623 /* step through components of name */
1624 for (np = name; np && *np; np = sepp) {
1625 /* ensure unique names */
1626 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1629 /* skip white space */
1630 skip_whitespace(np);
1634 /* set 'sepp' to end of current component 'np' */
1636 sepp = strchr(np, sep);
1640 /* find start of next "[ index ]..." */
1641 idxp = strchr(np, '[');
1643 /* if sepp comes first, set idxp to NULL */
1644 if (sepp && idxp && (sepp < idxp))
1648 * At this point 'idxp' is set if there is an index
1649 * expected for the current component.
1652 /* set 'n' to length of current 'np' name component */
1655 /* keep sepp up to date for *ep use as we advance */
1656 skip_whitespace(idxp);
1659 /* determine the index value */
1660 #if defined(_KERNEL) && !defined(_BOOT)
1661 if (ddi_strtol(idxp, &idxep, 0, &idx))
1664 idx = strtol(idxp, &idxep, 0);
1669 /* keep sepp up to date for *ep use as we advance */
1672 /* skip white space index value and check for ']' */
1673 skip_whitespace(sepp);
1677 /* for embedded arrays, support C syntax: "a[1].b" */
1678 skip_whitespace(sepp);
1679 if (sep && (*sepp == sep))
1687 /* trim trailing whitespace by reducing length of 'np' */
1690 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1694 /* skip whitespace, and set sepp to NULL if complete */
1696 skip_whitespace(sepp);
1703 * o 'n' is the length of current 'np' component.
1704 * o 'idxp' is set if there was an index, and value 'idx'.
1705 * o 'sepp' is set to the beginning of the next component,
1706 * and set to NULL if we have no more components.
1708 * Search for nvpair with matching component name.
1710 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1711 nvp = nvlist_next_nvpair(nvl, nvp)) {
1713 /* continue if no match on name */
1714 if (strncmp(np, nvpair_name(nvp), n) ||
1715 (strlen(nvpair_name(nvp)) != n))
1718 /* if indexed, verify type is array oriented */
1719 if (idxp && !nvpair_type_is_array(nvp))
1723 * Full match found, return nvp and idx if this
1724 * was the last component.
1730 *ip = (int)idx; /* return index */
1731 return (0); /* found */
1735 * More components: current match must be
1736 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1737 * to support going deeper.
1739 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1740 nvl = EMBEDDED_NVL(nvp);
1742 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1743 (void) nvpair_value_nvlist_array(nvp,
1744 &nva, (uint_t *)&n);
1745 if ((n < 0) || (idx >= n))
1751 /* type does not support more levels */
1755 goto fail; /* 'name' not found */
1757 /* search for match of next component in embedded 'nvl' list */
1760 fail: if (ep && sepp)
1766 * Return pointer to nvpair with specified 'name'.
1769 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1771 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1775 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1776 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1779 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1780 const char *name, nvpair_t **ret, int *ip, char **ep)
1782 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1786 nvlist_exists(nvlist_t *nvl, const char *name)
1792 if (name == NULL || nvl == NULL ||
1793 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1796 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1797 nvp = &curr->nvi_nvp;
1799 if (strcmp(name, NVP_NAME(nvp)) == 0)
1807 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1809 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1813 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1815 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1819 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1821 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1825 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1827 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1831 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1833 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1837 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1839 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1843 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1845 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1849 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1851 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1855 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1857 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1861 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1863 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1866 #if !defined(_KERNEL)
1868 nvpair_value_double(nvpair_t *nvp, double *val)
1870 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1875 nvpair_value_string(nvpair_t *nvp, char **val)
1877 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1881 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1883 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1887 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1889 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1893 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1895 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1899 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1901 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1905 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1907 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1911 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1913 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1917 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1919 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1923 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1925 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1929 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1931 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1935 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1937 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1941 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1943 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1947 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1949 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1953 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1955 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1959 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1961 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1965 * Add specified pair to the list.
1968 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1970 if (nvl == NULL || nvp == NULL)
1973 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1974 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1978 * Merge the supplied nvlists and put the result in dst.
1979 * The merged list will contain all names specified in both lists,
1980 * the values are taken from nvl in the case of duplicates.
1981 * Return 0 on success.
1985 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1987 if (nvl == NULL || dst == NULL)
1991 return (nvlist_copy_pairs(nvl, dst));
1997 * Encoding related routines
1999 #define NVS_OP_ENCODE 0
2000 #define NVS_OP_DECODE 1
2001 #define NVS_OP_GETSIZE 2
2003 typedef struct nvs_ops nvs_ops_t;
2007 const nvs_ops_t *nvs_ops;
2013 * nvs operations are:
2015 * encoding / decoding of a nvlist header (nvlist_t)
2016 * calculates the size used for header and end detection
2019 * responsible for the first part of encoding / decoding of an nvpair
2020 * calculates the decoded size of an nvpair
2023 * second part of encoding / decoding of an nvpair
2026 * calculates the encoding size of an nvpair
2029 * encodes the end detection mark (zeros).
2032 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2033 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2034 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2035 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2036 int (*nvs_nvl_fini)(nvstream_t *);
2040 char nvh_encoding; /* nvs encoding method */
2041 char nvh_endian; /* nvs endian */
2042 char nvh_reserved1; /* reserved for future use */
2043 char nvh_reserved2; /* reserved for future use */
2047 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2049 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2053 * Walk nvpair in list and encode each nvpair
2055 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2056 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2059 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2063 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2070 * Get decoded size of next pair in stream, alloc
2071 * memory for nvpair_t, then decode the nvpair
2073 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2074 if (nvsize == 0) /* end of list */
2077 /* make sure len makes sense */
2078 if (nvsize < NVP_SIZE_CALC(1, 0))
2081 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2084 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2085 nvp_buf_free(nvl, nvp);
2089 if (i_validate_nvpair(nvp) != 0) {
2091 nvp_buf_free(nvl, nvp);
2095 nvp_buf_link(nvl, nvp);
2101 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2103 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2105 uint64_t nvsize = *buflen;
2109 * Get encoded size of nvpairs in nvlist
2111 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2112 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2115 if ((nvsize += size) > INT32_MAX)
2124 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2128 if (nvl->nvl_priv == 0)
2132 * Perform the operation, starting with header, then each nvpair
2134 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2137 switch (nvs->nvs_op) {
2139 err = nvs_encode_pairs(nvs, nvl);
2143 err = nvs_decode_pairs(nvs, nvl);
2146 case NVS_OP_GETSIZE:
2147 err = nvs_getsize_pairs(nvs, nvl, buflen);
2158 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2160 switch (nvs->nvs_op) {
2162 return (nvs_operation(nvs, embedded, NULL));
2164 case NVS_OP_DECODE: {
2168 if (embedded->nvl_version != NV_VERSION)
2171 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2174 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2176 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2177 nvlist_free(embedded);
2188 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2190 size_t nelem = NVP_NELEM(nvp);
2191 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2194 switch (nvs->nvs_op) {
2196 for (i = 0; i < nelem; i++)
2197 if (nvs_embedded(nvs, nvlp[i]) != 0)
2201 case NVS_OP_DECODE: {
2202 size_t len = nelem * sizeof (uint64_t);
2203 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2205 bzero(nvlp, len); /* don't trust packed data */
2206 for (i = 0; i < nelem; i++) {
2207 if (nvs_embedded(nvs, embedded) != 0) {
2212 nvlp[i] = embedded++;
2216 case NVS_OP_GETSIZE: {
2217 uint64_t nvsize = 0;
2219 for (i = 0; i < nelem; i++) {
2222 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2225 if ((nvsize += nvp_sz) > INT32_MAX)
2239 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2240 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2243 * Common routine for nvlist operations:
2244 * encode, decode, getsize (encoded size).
2247 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2253 #ifdef _LITTLE_ENDIAN
2254 int host_endian = 1;
2256 int host_endian = 0;
2257 #endif /* _LITTLE_ENDIAN */
2258 nvs_header_t *nvh = (void *)buf;
2260 if (buflen == NULL || nvl == NULL ||
2261 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2264 nvs.nvs_op = nvs_op;
2267 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2268 * a buffer is allocated. The first 4 bytes in the buffer are
2269 * used for encoding method and host endian.
2273 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2276 nvh->nvh_encoding = encoding;
2277 nvh->nvh_endian = nvl_endian = host_endian;
2278 nvh->nvh_reserved1 = 0;
2279 nvh->nvh_reserved2 = 0;
2283 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2286 /* get method of encoding from first byte */
2287 encoding = nvh->nvh_encoding;
2288 nvl_endian = nvh->nvh_endian;
2291 case NVS_OP_GETSIZE:
2292 nvl_endian = host_endian;
2295 * add the size for encoding
2297 *buflen = sizeof (nvs_header_t);
2305 * Create an nvstream with proper encoding method
2308 case NV_ENCODE_NATIVE:
2310 * check endianness, in case we are unpacking
2313 if (nvl_endian != host_endian)
2315 err = nvs_native(&nvs, nvl, buf, buflen);
2318 err = nvs_xdr(&nvs, nvl, buf, buflen);
2329 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2331 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2335 * Pack nvlist into contiguous memory
2339 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2342 #if defined(_KERNEL) && !defined(_BOOT)
2343 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2344 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2346 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2351 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2359 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2363 return (nvlist_common(nvl, *bufp, buflen, encoding,
2367 * Here is a difficult situation:
2368 * 1. The nvlist has fixed allocator properties.
2369 * All other nvlist routines (like nvlist_add_*, ...) use
2371 * 2. When using nvlist_pack() the user can specify his own
2372 * allocator properties (e.g. by using KM_NOSLEEP).
2374 * We use the user specified properties (2). A clearer solution
2375 * will be to remove the kmflag from nvlist_pack(), but we will
2376 * not change the interface.
2378 nv_priv_init(&nvpriv, nva, 0);
2380 if (err = nvlist_size(nvl, &alloc_size, encoding))
2383 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2386 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2387 NVS_OP_ENCODE)) != 0) {
2388 nv_mem_free(&nvpriv, buf, alloc_size);
2390 *buflen = alloc_size;
2398 * Unpack buf into an nvlist_t
2402 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2404 #if defined(_KERNEL) && !defined(_BOOT)
2405 return (nvlist_xunpack(buf, buflen, nvlp,
2406 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2408 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2413 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2421 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2424 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2433 * Native encoding functions
2437 * This structure is used when decoding a packed nvpair in
2438 * the native format. n_base points to a buffer containing the
2439 * packed nvpair. n_end is a pointer to the end of the buffer.
2440 * (n_end actually points to the first byte past the end of the
2441 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2442 * It points to the current data that we are decoding.
2443 * The amount of data left in the buffer is equal to n_end - n_curr.
2444 * n_flag is used to recognize a packed embedded list.
2453 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2456 switch (nvs->nvs_op) {
2459 nvs->nvs_private = native;
2460 native->n_curr = native->n_base = buf;
2461 native->n_end = buf + buflen;
2465 case NVS_OP_GETSIZE:
2466 nvs->nvs_private = native;
2467 native->n_curr = native->n_base = native->n_end = NULL;
2477 nvs_native_destroy(nvstream_t *nvs)
2482 native_cp(nvstream_t *nvs, void *buf, size_t size)
2484 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2486 if (native->n_curr + size > native->n_end)
2490 * The bcopy() below eliminates alignment requirement
2491 * on the buffer (stream) and is preferred over direct access.
2493 switch (nvs->nvs_op) {
2495 bcopy(buf, native->n_curr, size);
2498 bcopy(native->n_curr, buf, size);
2504 native->n_curr += size;
2509 * operate on nvlist_t header
2512 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2514 nvs_native_t *native = nvs->nvs_private;
2516 switch (nvs->nvs_op) {
2520 return (0); /* packed embedded list */
2524 /* copy version and nvflag of the nvlist_t */
2525 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2526 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2531 case NVS_OP_GETSIZE:
2533 * if calculate for packed embedded list
2534 * 4 for end of the embedded list
2536 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2537 * and 4 for end of the entire list
2539 if (native->n_flag) {
2543 *size += 2 * sizeof (int32_t) + 4;
2554 nvs_native_nvl_fini(nvstream_t *nvs)
2556 if (nvs->nvs_op == NVS_OP_ENCODE) {
2557 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2559 * Add 4 zero bytes at end of nvlist. They are used
2560 * for end detection by the decode routine.
2562 if (native->n_curr + sizeof (int) > native->n_end)
2565 bzero(native->n_curr, sizeof (int));
2566 native->n_curr += sizeof (int);
2573 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2575 if (nvs->nvs_op == NVS_OP_ENCODE) {
2576 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2577 nvlist_t *packed = (void *)
2578 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2580 * Null out the pointer that is meaningless in the packed
2581 * structure. The address may not be aligned, so we have
2584 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2587 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2591 nvpair_native_embedded_array(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 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2596 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2597 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2600 * Null out pointers that are meaningless in the packed
2601 * structure. The addresses may not be aligned, so we have
2606 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2608 * Null out the pointer that is meaningless in the
2609 * packed structure. The address may not be aligned,
2610 * so we have to use bzero.
2612 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2615 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2619 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2621 switch (nvs->nvs_op) {
2622 case NVS_OP_ENCODE: {
2623 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2624 uint64_t *strp = (void *)
2625 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2627 * Null out pointers that are meaningless in the packed
2628 * structure. The addresses may not be aligned, so we have
2631 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2634 case NVS_OP_DECODE: {
2635 char **strp = (void *)NVP_VALUE(nvp);
2636 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2639 for (i = 0; i < NVP_NELEM(nvp); i++) {
2641 buf += strlen(buf) + 1;
2649 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2656 * We do the initial bcopy of the data before we look at
2657 * the nvpair type, because when we're decoding, we won't
2658 * have the correct values for the pair until we do the bcopy.
2660 switch (nvs->nvs_op) {
2663 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2670 /* verify nvp_name_sz, check the name string length */
2671 if (i_validate_nvpair_name(nvp) != 0)
2674 type = NVP_TYPE(nvp);
2677 * Verify type and nelem and get the value size.
2678 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2679 * is the size of the string(s) excluded.
2681 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2684 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2688 case DATA_TYPE_NVLIST:
2689 ret = nvpair_native_embedded(nvs, nvp);
2691 case DATA_TYPE_NVLIST_ARRAY:
2692 ret = nvpair_native_embedded_array(nvs, nvp);
2694 case DATA_TYPE_STRING_ARRAY:
2695 nvpair_native_string_array(nvs, nvp);
2705 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2707 uint64_t nvp_sz = nvp->nvp_size;
2709 switch (NVP_TYPE(nvp)) {
2710 case DATA_TYPE_NVLIST: {
2713 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2719 case DATA_TYPE_NVLIST_ARRAY: {
2722 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2732 if (nvp_sz > INT32_MAX)
2741 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2743 switch (nvs->nvs_op) {
2745 return (nvs_native_nvp_op(nvs, nvp));
2747 case NVS_OP_DECODE: {
2748 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2751 /* try to read the size value from the stream */
2752 if (native->n_curr + sizeof (int32_t) > native->n_end)
2754 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2756 /* sanity check the size value */
2757 if (decode_len < 0 ||
2758 decode_len > native->n_end - native->n_curr)
2764 * If at the end of the stream then move the cursor
2765 * forward, otherwise nvpair_native_op() will read
2766 * the entire nvpair at the same cursor position.
2769 native->n_curr += sizeof (int32_t);
2780 static const nvs_ops_t nvs_native_ops = {
2784 nvs_native_nvp_size,
2789 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2791 nvs_native_t native;
2794 nvs->nvs_ops = &nvs_native_ops;
2796 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2797 *buflen - sizeof (nvs_header_t))) != 0)
2800 err = nvs_operation(nvs, nvl, buflen);
2802 nvs_native_destroy(nvs);
2808 * XDR encoding functions
2810 * An xdr packed nvlist is encoded as:
2812 * - encoding methode and host endian (4 bytes)
2813 * - nvl_version (4 bytes)
2814 * - nvl_nvflag (4 bytes)
2816 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2817 * - encoded size of the nvpair (4 bytes)
2818 * - decoded size of the nvpair (4 bytes)
2819 * - name string, (4 + sizeof(NV_ALIGN4(string))
2820 * a string is coded as size (4 bytes) and data
2821 * - data type (4 bytes)
2822 * - number of elements in the nvpair (4 bytes)
2825 * - 2 zero's for end of the entire list (8 bytes)
2828 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2830 /* xdr data must be 4 byte aligned */
2831 if ((ulong_t)buf % 4 != 0)
2834 switch (nvs->nvs_op) {
2836 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2837 nvs->nvs_private = xdr;
2840 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2841 nvs->nvs_private = xdr;
2843 case NVS_OP_GETSIZE:
2844 nvs->nvs_private = NULL;
2852 nvs_xdr_destroy(nvstream_t *nvs)
2854 switch (nvs->nvs_op) {
2857 xdr_destroy((XDR *)nvs->nvs_private);
2865 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2867 switch (nvs->nvs_op) {
2869 case NVS_OP_DECODE: {
2870 XDR *xdr = nvs->nvs_private;
2872 if (!xdr_int(xdr, &nvl->nvl_version) ||
2873 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2877 case NVS_OP_GETSIZE: {
2879 * 2 * 4 for nvl_version + nvl_nvflag
2880 * and 8 for end of the entire list
2892 nvs_xdr_nvl_fini(nvstream_t *nvs)
2894 if (nvs->nvs_op == NVS_OP_ENCODE) {
2895 XDR *xdr = nvs->nvs_private;
2898 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2906 * The format of xdr encoded nvpair is:
2907 * encode_size, decode_size, name string, data type, nelem, data
2910 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2914 char *buf_end = (char *)nvp + nvp->nvp_size;
2916 uint_t nelem, buflen;
2918 XDR *xdr = nvs->nvs_private;
2920 ASSERT(xdr != NULL && nvp != NULL);
2923 if ((buf = NVP_NAME(nvp)) >= buf_end)
2925 buflen = buf_end - buf;
2927 if (!xdr_string(xdr, &buf, buflen - 1))
2929 nvp->nvp_name_sz = strlen(buf) + 1;
2931 /* type and nelem */
2932 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2933 !xdr_int(xdr, &nvp->nvp_value_elem))
2936 type = NVP_TYPE(nvp);
2937 nelem = nvp->nvp_value_elem;
2940 * Verify type and nelem and get the value size.
2941 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2942 * is the size of the string(s) excluded.
2944 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2947 /* if there is no data to extract then return */
2952 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2954 buflen = buf_end - buf;
2956 if (buflen < value_sz)
2960 case DATA_TYPE_NVLIST:
2961 if (nvs_embedded(nvs, (void *)buf) == 0)
2965 case DATA_TYPE_NVLIST_ARRAY:
2966 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2970 case DATA_TYPE_BOOLEAN:
2974 case DATA_TYPE_BYTE:
2975 case DATA_TYPE_INT8:
2976 case DATA_TYPE_UINT8:
2977 ret = xdr_char(xdr, buf);
2980 case DATA_TYPE_INT16:
2981 ret = xdr_short(xdr, (void *)buf);
2984 case DATA_TYPE_UINT16:
2985 ret = xdr_u_short(xdr, (void *)buf);
2988 case DATA_TYPE_BOOLEAN_VALUE:
2989 case DATA_TYPE_INT32:
2990 ret = xdr_int(xdr, (void *)buf);
2993 case DATA_TYPE_UINT32:
2994 ret = xdr_u_int(xdr, (void *)buf);
2997 case DATA_TYPE_INT64:
2998 ret = xdr_longlong_t(xdr, (void *)buf);
3001 case DATA_TYPE_UINT64:
3002 ret = xdr_u_longlong_t(xdr, (void *)buf);
3005 case DATA_TYPE_HRTIME:
3007 * NOTE: must expose the definition of hrtime_t here
3009 ret = xdr_longlong_t(xdr, (void *)buf);
3011 #if !defined(_KERNEL)
3012 case DATA_TYPE_DOUBLE:
3013 ret = xdr_double(xdr, (void *)buf);
3016 case DATA_TYPE_STRING:
3017 ret = xdr_string(xdr, &buf, buflen - 1);
3020 case DATA_TYPE_BYTE_ARRAY:
3021 ret = xdr_opaque(xdr, buf, nelem);
3024 case DATA_TYPE_INT8_ARRAY:
3025 case DATA_TYPE_UINT8_ARRAY:
3026 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3027 (xdrproc_t)xdr_char);
3030 case DATA_TYPE_INT16_ARRAY:
3031 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3032 sizeof (int16_t), (xdrproc_t)xdr_short);
3035 case DATA_TYPE_UINT16_ARRAY:
3036 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3037 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3040 case DATA_TYPE_BOOLEAN_ARRAY:
3041 case DATA_TYPE_INT32_ARRAY:
3042 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3043 sizeof (int32_t), (xdrproc_t)xdr_int);
3046 case DATA_TYPE_UINT32_ARRAY:
3047 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3048 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3051 case DATA_TYPE_INT64_ARRAY:
3052 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3053 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3056 case DATA_TYPE_UINT64_ARRAY:
3057 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3058 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3061 case DATA_TYPE_STRING_ARRAY: {
3062 size_t len = nelem * sizeof (uint64_t);
3063 char **strp = (void *)buf;
3066 if (nvs->nvs_op == NVS_OP_DECODE)
3067 bzero(buf, len); /* don't trust packed data */
3069 for (i = 0; i < nelem; i++) {
3076 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3079 if (nvs->nvs_op == NVS_OP_DECODE)
3081 len = strlen(buf) + 1;
3090 return (ret == TRUE ? 0 : EFAULT);
3094 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3096 data_type_t type = NVP_TYPE(nvp);
3098 * encode_size + decode_size + name string size + data type + nelem
3099 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3101 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3104 case DATA_TYPE_BOOLEAN:
3107 case DATA_TYPE_BOOLEAN_VALUE:
3108 case DATA_TYPE_BYTE:
3109 case DATA_TYPE_INT8:
3110 case DATA_TYPE_UINT8:
3111 case DATA_TYPE_INT16:
3112 case DATA_TYPE_UINT16:
3113 case DATA_TYPE_INT32:
3114 case DATA_TYPE_UINT32:
3115 nvp_sz += 4; /* 4 is the minimum xdr unit */
3118 case DATA_TYPE_INT64:
3119 case DATA_TYPE_UINT64:
3120 case DATA_TYPE_HRTIME:
3121 #if !defined(_KERNEL)
3122 case DATA_TYPE_DOUBLE:
3127 case DATA_TYPE_STRING:
3128 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3131 case DATA_TYPE_BYTE_ARRAY:
3132 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3135 case DATA_TYPE_BOOLEAN_ARRAY:
3136 case DATA_TYPE_INT8_ARRAY:
3137 case DATA_TYPE_UINT8_ARRAY:
3138 case DATA_TYPE_INT16_ARRAY:
3139 case DATA_TYPE_UINT16_ARRAY:
3140 case DATA_TYPE_INT32_ARRAY:
3141 case DATA_TYPE_UINT32_ARRAY:
3142 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3145 case DATA_TYPE_INT64_ARRAY:
3146 case DATA_TYPE_UINT64_ARRAY:
3147 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3150 case DATA_TYPE_STRING_ARRAY: {
3152 char **strs = (void *)NVP_VALUE(nvp);
3154 for (i = 0; i < NVP_NELEM(nvp); i++)
3155 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3160 case DATA_TYPE_NVLIST:
3161 case DATA_TYPE_NVLIST_ARRAY: {
3163 int old_nvs_op = nvs->nvs_op;
3166 nvs->nvs_op = NVS_OP_GETSIZE;
3167 if (type == DATA_TYPE_NVLIST)
3168 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3170 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3171 nvs->nvs_op = old_nvs_op;
3184 if (nvp_sz > INT32_MAX)
3194 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3195 * the largest nvpair that could be encoded in the buffer.
3197 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3198 * The size of a xdr packed nvpair without any data is 5 words.
3200 * Using the size of the data directly as an estimate would be ok
3201 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3202 * then the actual nvpair has space for an array of pointers to index
3203 * the strings. These pointers are not encoded into the packed xdr buffer.
3205 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3206 * of length 0, then each string is endcoded in xdr format as a single word.
3207 * Therefore when expanded to an nvpair there will be 2.25 word used for
3208 * each string. (a int64_t allocated for pointer usage, and a single char
3209 * for the null termination.)
3211 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3213 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3214 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3215 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3216 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3217 (NVS_XDR_DATA_LEN(x) * 2) + \
3218 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3221 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3223 XDR *xdr = nvs->nvs_private;
3224 int32_t encode_len, decode_len;
3226 switch (nvs->nvs_op) {
3227 case NVS_OP_ENCODE: {
3230 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3233 decode_len = nvp->nvp_size;
3234 encode_len = nvsize;
3235 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3238 return (nvs_xdr_nvp_op(nvs, nvp));
3240 case NVS_OP_DECODE: {
3241 struct xdr_bytesrec bytesrec;
3243 /* get the encode and decode size */
3244 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3248 /* are we at the end of the stream? */
3252 /* sanity check the size parameter */
3253 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3256 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3267 static const struct nvs_ops nvs_xdr_ops = {
3276 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3281 nvs->nvs_ops = &nvs_xdr_ops;
3283 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3284 *buflen - sizeof (nvs_header_t))) != 0)
3287 err = nvs_operation(nvs, nvl, buflen);
3289 nvs_xdr_destroy(nvs);