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 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/debug.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <rpc/types.h>
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/varargs.h>
43 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
48 * nvpair.c - Provides kernel & userland interfaces for manipulating
63 * +--------------+ last i_nvp in list
64 * | nvpriv_t | +--------------------->
66 * +--+- nvp_list | | +------------+
67 * | | nvp_last -+--+ + nv_alloc_t |
68 * | | nvp_curr | |------------|
69 * | | nvp_nva -+----> | nva_ops |
70 * | | nvp_stat | | nva_arg |
71 * | +--------------+ +------------+
75 * +---------------------+ +-------------------+
76 * | i_nvp_t | +-->| i_nvp_t | +-->
77 * |---------------------| | |-------------------| |
78 * | nvi_next -+--+ | nvi_next -+--+
79 * | nvi_prev (NULL) | <----+ nvi_prev |
80 * | . . . . . . . . . . | | . . . . . . . . . |
81 * | nvp (nvpair_t) | | nvp (nvpair_t) |
82 * | - nvp_size | | - nvp_size |
83 * | - nvp_name_sz | | - nvp_name_sz |
84 * | - nvp_value_elem | | - nvp_value_elem |
85 * | - nvp_type | | - nvp_type |
86 * | - data ... | | - data ... |
87 * +---------------------+ +-------------------+
91 * +---------------------+ +---------------------+
92 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
93 * |---------------------| | | |---------------------|
94 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
95 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
96 * | . . . . . . . . . | | . . . . . . . . . |
97 * | nvp (nvpair_t) | | nvp (nvpair_t) |
98 * | - nvp_size | | - nvp_size |
99 * | - nvp_name_sz | | - nvp_name_sz |
100 * | - nvp_value_elem | | - nvp_value_elem |
101 * | - DATA_TYPE_NVLIST | | - nvp_type |
102 * | - data (embedded) | | - data ... |
103 * | nvlist name | +---------------------+
104 * | +--------------+ |
106 * | |--------------| |
107 * | | nvl_version | |
109 * | | nvl_priv --+---+---->
112 * | +--------------+ |
113 * +---------------------+
116 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
117 * allow value to be aligned on 8 byte boundary
119 * name_len is the length of the name string including the null terminator
122 #define NVP_SIZE_CALC(name_len, data_len) \
123 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
125 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
126 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
127 uint_t nelem, const void *data);
129 #define NV_STAT_EMBEDDED 0x1
130 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
131 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
133 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
134 #define NVPAIR2I_NVP(nvp) \
135 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
139 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
147 va_start(valist, nvo);
148 if (nva->nva_ops->nv_ao_init != NULL)
149 err = nva->nva_ops->nv_ao_init(nva, valist);
156 nv_alloc_reset(nv_alloc_t *nva)
158 if (nva->nva_ops->nv_ao_reset != NULL)
159 nva->nva_ops->nv_ao_reset(nva);
163 nv_alloc_fini(nv_alloc_t *nva)
165 if (nva->nva_ops->nv_ao_fini != NULL)
166 nva->nva_ops->nv_ao_fini(nva);
170 nvlist_lookup_nv_alloc(nvlist_t *nvl)
175 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
178 return (priv->nvp_nva);
182 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
184 nv_alloc_t *nva = nvp->nvp_nva;
187 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
194 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
196 nv_alloc_t *nva = nvp->nvp_nva;
198 nva->nva_ops->nv_ao_free(nva, buf, size);
202 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
204 bzero(priv, sizeof (priv));
207 priv->nvp_stat = stat;
211 nv_priv_alloc(nv_alloc_t *nva)
216 * nv_mem_alloc() cannot called here because it needs the priv
219 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
222 nv_priv_init(priv, nva, 0);
228 * Embedded lists need their own nvpriv_t's. We create a new
229 * nvpriv_t using the parameters and allocator from the parent
233 nv_priv_alloc_embedded(nvpriv_t *priv)
237 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
240 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
246 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
248 nvl->nvl_version = NV_VERSION;
249 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
250 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
256 * nvlist_alloc - Allocate nvlist.
260 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
262 #if defined(_KERNEL) && !defined(_BOOT)
263 return (nvlist_xalloc(nvlp, nvflag,
264 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
266 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
271 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
275 if (nvlp == NULL || nva == NULL)
278 if ((priv = nv_priv_alloc(nva)) == NULL)
281 if ((*nvlp = nv_mem_zalloc(priv,
282 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
283 nv_mem_free(priv, priv, sizeof (nvpriv_t));
287 nvlist_init(*nvlp, nvflag, priv);
293 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
296 nvp_buf_alloc(nvlist_t *nvl, size_t len)
298 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
304 * Allocate the buffer
306 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
308 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
318 * nvp_buf_free - de-Allocate an i_nvp_t.
321 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
323 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
324 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
326 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
330 * nvp_buf_link - link a new nv pair into the nvlist.
333 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
335 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
336 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
338 /* Put element at end of nvlist */
339 if (priv->nvp_list == NULL) {
340 priv->nvp_list = priv->nvp_last = curr;
342 curr->nvi_prev = priv->nvp_last;
343 priv->nvp_last->nvi_next = curr;
344 priv->nvp_last = curr;
349 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
352 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
354 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
355 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
358 * protect nvlist_next_nvpair() against walking on freed memory.
360 if (priv->nvp_curr == curr)
361 priv->nvp_curr = curr->nvi_next;
363 if (curr == priv->nvp_list)
364 priv->nvp_list = curr->nvi_next;
366 curr->nvi_prev->nvi_next = curr->nvi_next;
368 if (curr == priv->nvp_last)
369 priv->nvp_last = curr->nvi_prev;
371 curr->nvi_next->nvi_prev = curr->nvi_prev;
375 * take a nvpair type and number of elements and make sure the are valid
378 i_validate_type_nelem(data_type_t type, uint_t nelem)
381 case DATA_TYPE_BOOLEAN:
385 case DATA_TYPE_BOOLEAN_VALUE:
388 case DATA_TYPE_UINT8:
389 case DATA_TYPE_INT16:
390 case DATA_TYPE_UINT16:
391 case DATA_TYPE_INT32:
392 case DATA_TYPE_UINT32:
393 case DATA_TYPE_INT64:
394 case DATA_TYPE_UINT64:
395 case DATA_TYPE_STRING:
396 case DATA_TYPE_HRTIME:
397 case DATA_TYPE_NVLIST:
401 case DATA_TYPE_BOOLEAN_ARRAY:
402 case DATA_TYPE_BYTE_ARRAY:
403 case DATA_TYPE_INT8_ARRAY:
404 case DATA_TYPE_UINT8_ARRAY:
405 case DATA_TYPE_INT16_ARRAY:
406 case DATA_TYPE_UINT16_ARRAY:
407 case DATA_TYPE_INT32_ARRAY:
408 case DATA_TYPE_UINT32_ARRAY:
409 case DATA_TYPE_INT64_ARRAY:
410 case DATA_TYPE_UINT64_ARRAY:
411 case DATA_TYPE_STRING_ARRAY:
412 case DATA_TYPE_NVLIST_ARRAY:
413 /* we allow arrays with 0 elements */
422 * Verify nvp_name_sz and check the name string length.
425 i_validate_nvpair_name(nvpair_t *nvp)
427 if ((nvp->nvp_name_sz <= 0) ||
428 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
431 /* verify the name string, make sure its terminated */
432 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
435 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
439 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
442 case DATA_TYPE_BOOLEAN_VALUE:
443 if (*(boolean_t *)data != B_TRUE &&
444 *(boolean_t *)data != B_FALSE)
447 case DATA_TYPE_BOOLEAN_ARRAY: {
450 for (i = 0; i < nelem; i++)
451 if (((boolean_t *)data)[i] != B_TRUE &&
452 ((boolean_t *)data)[i] != B_FALSE)
464 * This function takes a pointer to what should be a nvpair and it's size
465 * and then verifies that all the nvpair fields make sense and can be
466 * trusted. This function is used when decoding packed nvpairs.
469 i_validate_nvpair(nvpair_t *nvp)
471 data_type_t type = NVP_TYPE(nvp);
474 /* verify nvp_name_sz, check the name string length */
475 if (i_validate_nvpair_name(nvp) != 0)
478 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
482 * verify nvp_type, nvp_value_elem, and also possibly
483 * verify string values and get the value size.
485 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
486 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
487 if (size2 < 0 || size1 != NV_ALIGN(size2))
494 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
499 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
502 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
503 nvpair_t *nvp = &curr->nvi_nvp;
506 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
507 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
515 * Frees all memory allocated for an nvpair (like embedded lists) with
516 * the exception of the nvpair buffer itself.
519 nvpair_free(nvpair_t *nvp)
521 switch (NVP_TYPE(nvp)) {
522 case DATA_TYPE_NVLIST:
523 nvlist_free(EMBEDDED_NVL(nvp));
525 case DATA_TYPE_NVLIST_ARRAY: {
526 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
529 for (i = 0; i < NVP_NELEM(nvp); i++)
531 nvlist_free(nvlp[i]);
540 * nvlist_free - free an unpacked nvlist
543 nvlist_free(nvlist_t *nvl)
549 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
553 * Unpacked nvlist are linked through i_nvp_t
555 curr = priv->nvp_list;
556 while (curr != NULL) {
557 nvpair_t *nvp = &curr->nvi_nvp;
558 curr = curr->nvi_next;
561 nvp_buf_free(nvl, nvp);
564 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
565 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
569 nv_mem_free(priv, priv, sizeof (nvpriv_t));
573 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
575 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
581 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
582 if (&curr->nvi_nvp == nvp)
589 * Make a copy of nvlist
593 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
595 #if defined(_KERNEL) && !defined(_BOOT)
596 return (nvlist_xdup(nvl, nvlp,
597 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
599 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
604 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
609 if (nvl == NULL || nvlp == NULL)
612 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
615 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
624 * Remove all with matching name
627 nvlist_remove_all(nvlist_t *nvl, const char *name)
633 if (nvl == NULL || name == NULL ||
634 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
637 curr = priv->nvp_list;
638 while (curr != NULL) {
639 nvpair_t *nvp = &curr->nvi_nvp;
641 curr = curr->nvi_next;
642 if (strcmp(name, NVP_NAME(nvp)) != 0)
645 nvp_buf_unlink(nvl, nvp);
647 nvp_buf_free(nvl, nvp);
656 * Remove first one with matching name and type
659 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
664 if (nvl == NULL || name == NULL ||
665 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
668 curr = priv->nvp_list;
669 while (curr != NULL) {
670 nvpair_t *nvp = &curr->nvi_nvp;
672 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
673 nvp_buf_unlink(nvl, nvp);
675 nvp_buf_free(nvl, nvp);
679 curr = curr->nvi_next;
686 * This function calculates the size of an nvpair value.
688 * The data argument controls the behavior in case of the data types
689 * DATA_TYPE_STRING and
690 * DATA_TYPE_STRING_ARRAY
691 * Is data == NULL then the size of the string(s) is excluded.
694 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
698 if (i_validate_type_nelem(type, nelem) != 0)
701 /* Calculate required size for holding value */
703 case DATA_TYPE_BOOLEAN:
706 case DATA_TYPE_BOOLEAN_VALUE:
707 value_sz = sizeof (boolean_t);
710 value_sz = sizeof (uchar_t);
713 value_sz = sizeof (int8_t);
715 case DATA_TYPE_UINT8:
716 value_sz = sizeof (uint8_t);
718 case DATA_TYPE_INT16:
719 value_sz = sizeof (int16_t);
721 case DATA_TYPE_UINT16:
722 value_sz = sizeof (uint16_t);
724 case DATA_TYPE_INT32:
725 value_sz = sizeof (int32_t);
727 case DATA_TYPE_UINT32:
728 value_sz = sizeof (uint32_t);
730 case DATA_TYPE_INT64:
731 value_sz = sizeof (int64_t);
733 case DATA_TYPE_UINT64:
734 value_sz = sizeof (uint64_t);
736 case DATA_TYPE_STRING:
740 value_sz = strlen(data) + 1;
742 case DATA_TYPE_BOOLEAN_ARRAY:
743 value_sz = (uint64_t)nelem * sizeof (boolean_t);
745 case DATA_TYPE_BYTE_ARRAY:
746 value_sz = (uint64_t)nelem * sizeof (uchar_t);
748 case DATA_TYPE_INT8_ARRAY:
749 value_sz = (uint64_t)nelem * sizeof (int8_t);
751 case DATA_TYPE_UINT8_ARRAY:
752 value_sz = (uint64_t)nelem * sizeof (uint8_t);
754 case DATA_TYPE_INT16_ARRAY:
755 value_sz = (uint64_t)nelem * sizeof (int16_t);
757 case DATA_TYPE_UINT16_ARRAY:
758 value_sz = (uint64_t)nelem * sizeof (uint16_t);
760 case DATA_TYPE_INT32_ARRAY:
761 value_sz = (uint64_t)nelem * sizeof (int32_t);
763 case DATA_TYPE_UINT32_ARRAY:
764 value_sz = (uint64_t)nelem * sizeof (uint32_t);
766 case DATA_TYPE_INT64_ARRAY:
767 value_sz = (uint64_t)nelem * sizeof (int64_t);
769 case DATA_TYPE_UINT64_ARRAY:
770 value_sz = (uint64_t)nelem * sizeof (uint64_t);
772 case DATA_TYPE_STRING_ARRAY:
773 value_sz = (uint64_t)nelem * sizeof (uint64_t);
776 char *const *strs = data;
779 /* no alignment requirement for strings */
780 for (i = 0; i < nelem; i++) {
783 value_sz += strlen(strs[i]) + 1;
787 case DATA_TYPE_HRTIME:
788 value_sz = sizeof (hrtime_t);
790 case DATA_TYPE_NVLIST:
791 value_sz = NV_ALIGN(sizeof (nvlist_t));
793 case DATA_TYPE_NVLIST_ARRAY:
794 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
795 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
801 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
805 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
810 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
811 nvl->nvl_priv)) == NULL)
814 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
816 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
817 nvlist_free(emb_nvl);
818 emb_nvl->nvl_priv = 0;
825 * nvlist_add_common - Add new <name,value> pair to nvlist
828 nvlist_add_common(nvlist_t *nvl, const char *name,
829 data_type_t type, uint_t nelem, const void *data)
834 int nvp_sz, name_sz, value_sz;
837 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
840 if (nelem != 0 && data == NULL)
844 * Verify type and nelem and get the value size.
845 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
846 * is the size of the string(s) included.
848 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
851 if (i_validate_nvpair_value(type, nelem, data) != 0)
855 * If we're adding an nvlist or nvlist array, ensure that we are not
856 * adding the input nvlist to itself, which would cause recursion,
857 * and ensure that no NULL nvlist pointers are present.
860 case DATA_TYPE_NVLIST:
861 if (data == nvl || data == NULL)
864 case DATA_TYPE_NVLIST_ARRAY: {
865 nvlist_t **onvlp = (nvlist_t **)data;
866 for (i = 0; i < nelem; i++) {
867 if (onvlp[i] == nvl || onvlp[i] == NULL)
876 /* calculate sizes of the nvpair elements and the nvpair itself */
877 name_sz = strlen(name) + 1;
879 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
881 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
884 ASSERT(nvp->nvp_size == nvp_sz);
885 nvp->nvp_name_sz = name_sz;
886 nvp->nvp_value_elem = nelem;
887 nvp->nvp_type = type;
888 bcopy(name, NVP_NAME(nvp), name_sz);
891 case DATA_TYPE_BOOLEAN:
893 case DATA_TYPE_STRING_ARRAY: {
894 char *const *strs = data;
895 char *buf = NVP_VALUE(nvp);
896 char **cstrs = (void *)buf;
898 /* skip pre-allocated space for pointer array */
899 buf += nelem * sizeof (uint64_t);
900 for (i = 0; i < nelem; i++) {
901 int slen = strlen(strs[i]) + 1;
902 bcopy(strs[i], buf, slen);
908 case DATA_TYPE_NVLIST: {
909 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
910 nvlist_t *onvl = (nvlist_t *)data;
912 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
913 nvp_buf_free(nvl, nvp);
918 case DATA_TYPE_NVLIST_ARRAY: {
919 nvlist_t **onvlp = (nvlist_t **)data;
920 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
921 nvlist_t *embedded = (nvlist_t *)
922 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
924 for (i = 0; i < nelem; i++) {
925 if ((err = nvlist_copy_embedded(nvl,
926 onvlp[i], embedded)) != 0) {
928 * Free any successfully created lists
931 nvp_buf_free(nvl, nvp);
935 nvlp[i] = embedded++;
940 bcopy(data, NVP_VALUE(nvp), value_sz);
943 /* if unique name, remove before add */
944 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
945 (void) nvlist_remove_all(nvl, name);
946 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
947 (void) nvlist_remove(nvl, name, type);
949 nvp_buf_link(nvl, nvp);
955 nvlist_add_boolean(nvlist_t *nvl, const char *name)
957 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
961 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
963 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
967 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
969 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
973 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
975 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
979 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
981 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
985 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
987 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
991 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
993 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
997 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
999 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1003 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1005 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1009 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1011 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1015 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1017 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1021 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1023 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1027 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1028 boolean_t *a, uint_t n)
1030 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1034 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1036 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1040 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1042 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1046 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1048 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1052 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1054 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1058 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1060 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1064 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1066 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1070 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1072 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1076 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1078 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1082 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1084 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1088 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1089 char *const *a, uint_t n)
1091 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1095 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1097 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1101 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1103 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1107 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1109 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1112 /* reading name-value pairs */
1114 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1120 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1123 curr = NVPAIR2I_NVP(nvp);
1126 * Ensure that nvp is an valid pointer.
1129 curr = priv->nvp_list;
1130 else if (priv->nvp_curr == curr)
1131 curr = curr->nvi_next;
1132 else if (nvlist_contains_nvp(nvl, nvp) == 0)
1135 priv->nvp_curr = curr;
1137 return (curr != NULL ? &curr->nvi_nvp : NULL);
1141 nvpair_name(nvpair_t *nvp)
1143 return (NVP_NAME(nvp));
1147 nvpair_type(nvpair_t *nvp)
1149 return (NVP_TYPE(nvp));
1153 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1155 if (nvp == NULL || nvpair_type(nvp) != type)
1159 * For non-array types, we copy the data.
1160 * For array types (including string), we set a pointer.
1163 case DATA_TYPE_BOOLEAN:
1168 case DATA_TYPE_BOOLEAN_VALUE:
1169 case DATA_TYPE_BYTE:
1170 case DATA_TYPE_INT8:
1171 case DATA_TYPE_UINT8:
1172 case DATA_TYPE_INT16:
1173 case DATA_TYPE_UINT16:
1174 case DATA_TYPE_INT32:
1175 case DATA_TYPE_UINT32:
1176 case DATA_TYPE_INT64:
1177 case DATA_TYPE_UINT64:
1178 case DATA_TYPE_HRTIME:
1181 bcopy(NVP_VALUE(nvp), data,
1182 (size_t)i_get_value_size(type, NULL, 1));
1187 case DATA_TYPE_NVLIST:
1188 case DATA_TYPE_STRING:
1191 *(void **)data = (void *)NVP_VALUE(nvp);
1196 case DATA_TYPE_BOOLEAN_ARRAY:
1197 case DATA_TYPE_BYTE_ARRAY:
1198 case DATA_TYPE_INT8_ARRAY:
1199 case DATA_TYPE_UINT8_ARRAY:
1200 case DATA_TYPE_INT16_ARRAY:
1201 case DATA_TYPE_UINT16_ARRAY:
1202 case DATA_TYPE_INT32_ARRAY:
1203 case DATA_TYPE_UINT32_ARRAY:
1204 case DATA_TYPE_INT64_ARRAY:
1205 case DATA_TYPE_UINT64_ARRAY:
1206 case DATA_TYPE_STRING_ARRAY:
1207 case DATA_TYPE_NVLIST_ARRAY:
1208 if (nelem == NULL || data == NULL)
1210 if ((*nelem = NVP_NELEM(nvp)) != 0)
1211 *(void **)data = (void *)NVP_VALUE(nvp);
1213 *(void **)data = NULL;
1224 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1225 uint_t *nelem, void *data)
1231 if (name == NULL || nvl == NULL ||
1232 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1235 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1238 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1239 nvp = &curr->nvi_nvp;
1241 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1242 return (nvpair_value_common(nvp, type, nelem, data));
1249 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1251 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1255 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1257 return (nvlist_lookup_common(nvl, name,
1258 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1262 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1264 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1268 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1270 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1274 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1276 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1280 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1282 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1286 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1288 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1292 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1294 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1298 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1300 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1304 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1306 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1310 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1312 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1316 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1318 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1322 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1324 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1328 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1329 boolean_t **a, uint_t *n)
1331 return (nvlist_lookup_common(nvl, name,
1332 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1336 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1337 uchar_t **a, uint_t *n)
1339 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1343 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1345 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1349 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1350 uint8_t **a, uint_t *n)
1352 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1356 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1357 int16_t **a, uint_t *n)
1359 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1363 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1364 uint16_t **a, uint_t *n)
1366 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1370 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1371 int32_t **a, uint_t *n)
1373 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1377 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1378 uint32_t **a, uint_t *n)
1380 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1384 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1385 int64_t **a, uint_t *n)
1387 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1391 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1392 uint64_t **a, uint_t *n)
1394 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1398 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1399 char ***a, uint_t *n)
1401 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1405 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1406 nvlist_t ***a, uint_t *n)
1408 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1412 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1414 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1418 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1422 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1426 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1431 switch (type = va_arg(ap, data_type_t)) {
1432 case DATA_TYPE_BOOLEAN:
1433 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1436 case DATA_TYPE_BOOLEAN_VALUE:
1437 case DATA_TYPE_BYTE:
1438 case DATA_TYPE_INT8:
1439 case DATA_TYPE_UINT8:
1440 case DATA_TYPE_INT16:
1441 case DATA_TYPE_UINT16:
1442 case DATA_TYPE_INT32:
1443 case DATA_TYPE_UINT32:
1444 case DATA_TYPE_INT64:
1445 case DATA_TYPE_UINT64:
1446 case DATA_TYPE_HRTIME:
1447 case DATA_TYPE_STRING:
1448 case DATA_TYPE_NVLIST:
1449 val = va_arg(ap, void *);
1450 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1453 case DATA_TYPE_BYTE_ARRAY:
1454 case DATA_TYPE_BOOLEAN_ARRAY:
1455 case DATA_TYPE_INT8_ARRAY:
1456 case DATA_TYPE_UINT8_ARRAY:
1457 case DATA_TYPE_INT16_ARRAY:
1458 case DATA_TYPE_UINT16_ARRAY:
1459 case DATA_TYPE_INT32_ARRAY:
1460 case DATA_TYPE_UINT32_ARRAY:
1461 case DATA_TYPE_INT64_ARRAY:
1462 case DATA_TYPE_UINT64_ARRAY:
1463 case DATA_TYPE_STRING_ARRAY:
1464 case DATA_TYPE_NVLIST_ARRAY:
1465 val = va_arg(ap, void *);
1466 nelem = va_arg(ap, uint_t *);
1467 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1474 if (ret == ENOENT && noentok)
1483 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1485 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1489 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1491 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1495 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1497 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1501 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1503 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1507 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1509 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1513 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1515 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1519 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1521 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1525 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1527 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1531 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1533 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1537 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1539 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1543 nvpair_value_string(nvpair_t *nvp, char **val)
1545 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1549 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1551 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1555 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1557 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1561 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1563 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1567 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1569 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1573 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1575 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1579 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1581 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1585 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1587 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1591 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1593 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1597 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1599 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1603 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1605 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1609 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1611 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1615 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1617 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1621 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1623 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1627 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1629 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1633 * Add specified pair to the list.
1636 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1638 if (nvl == NULL || nvp == NULL)
1641 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1642 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1646 * Merge the supplied nvlists and put the result in dst.
1647 * The merged list will contain all names specified in both lists,
1648 * the values are taken from nvl in the case of duplicates.
1649 * Return 0 on success.
1653 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1655 if (nvl == NULL || dst == NULL)
1659 return (nvlist_copy_pairs(nvl, dst));
1665 * Encoding related routines
1667 #define NVS_OP_ENCODE 0
1668 #define NVS_OP_DECODE 1
1669 #define NVS_OP_GETSIZE 2
1671 typedef struct nvs_ops nvs_ops_t;
1675 const nvs_ops_t *nvs_ops;
1681 * nvs operations are:
1683 * encoding / decoding of a nvlist header (nvlist_t)
1684 * calculates the size used for header and end detection
1687 * responsible for the first part of encoding / decoding of an nvpair
1688 * calculates the decoded size of an nvpair
1691 * second part of encoding / decoding of an nvpair
1694 * calculates the encoding size of an nvpair
1697 * encodes the end detection mark (zeros).
1700 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
1701 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
1702 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
1703 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
1704 int (*nvs_nvl_fini)(nvstream_t *);
1708 char nvh_encoding; /* nvs encoding method */
1709 char nvh_endian; /* nvs endian */
1710 char nvh_reserved1; /* reserved for future use */
1711 char nvh_reserved2; /* reserved for future use */
1715 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
1717 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
1721 * Walk nvpair in list and encode each nvpair
1723 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
1724 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
1727 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
1731 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
1738 * Get decoded size of next pair in stream, alloc
1739 * memory for nvpair_t, then decode the nvpair
1741 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
1742 if (nvsize == 0) /* end of list */
1745 /* make sure len makes sense */
1746 if (nvsize < NVP_SIZE_CALC(1, 0))
1749 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
1752 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
1753 nvp_buf_free(nvl, nvp);
1757 if (i_validate_nvpair(nvp) != 0) {
1759 nvp_buf_free(nvl, nvp);
1763 nvp_buf_link(nvl, nvp);
1769 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
1771 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
1773 uint64_t nvsize = *buflen;
1777 * Get encoded size of nvpairs in nvlist
1779 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1780 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
1783 if ((nvsize += size) > INT32_MAX)
1792 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
1796 if (nvl->nvl_priv == 0)
1800 * Perform the operation, starting with header, then each nvpair
1802 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
1805 switch (nvs->nvs_op) {
1807 err = nvs_encode_pairs(nvs, nvl);
1811 err = nvs_decode_pairs(nvs, nvl);
1814 case NVS_OP_GETSIZE:
1815 err = nvs_getsize_pairs(nvs, nvl, buflen);
1826 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
1828 switch (nvs->nvs_op) {
1830 return (nvs_operation(nvs, embedded, NULL));
1832 case NVS_OP_DECODE: {
1836 if (embedded->nvl_version != NV_VERSION)
1839 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
1842 nvlist_init(embedded, embedded->nvl_nvflag, priv);
1844 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
1845 nvlist_free(embedded);
1856 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
1858 size_t nelem = NVP_NELEM(nvp);
1859 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1862 switch (nvs->nvs_op) {
1864 for (i = 0; i < nelem; i++)
1865 if (nvs_embedded(nvs, nvlp[i]) != 0)
1869 case NVS_OP_DECODE: {
1870 size_t len = nelem * sizeof (uint64_t);
1871 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
1873 bzero(nvlp, len); /* don't trust packed data */
1874 for (i = 0; i < nelem; i++) {
1875 if (nvs_embedded(nvs, embedded) != 0) {
1880 nvlp[i] = embedded++;
1884 case NVS_OP_GETSIZE: {
1885 uint64_t nvsize = 0;
1887 for (i = 0; i < nelem; i++) {
1890 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
1893 if ((nvsize += nvp_sz) > INT32_MAX)
1907 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
1908 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
1911 * Common routine for nvlist operations:
1912 * encode, decode, getsize (encoded size).
1915 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
1921 #if BYTE_ORDER == _LITTLE_ENDIAN
1922 int host_endian = 1;
1924 int host_endian = 0;
1925 #endif /* _LITTLE_ENDIAN */
1926 nvs_header_t *nvh = (void *)buf;
1928 if (buflen == NULL || nvl == NULL ||
1929 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1932 nvs.nvs_op = nvs_op;
1935 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
1936 * a buffer is allocated. The first 4 bytes in the buffer are
1937 * used for encoding method and host endian.
1941 if (buf == NULL || *buflen < sizeof (nvs_header_t))
1944 nvh->nvh_encoding = encoding;
1945 nvh->nvh_endian = nvl_endian = host_endian;
1946 nvh->nvh_reserved1 = 0;
1947 nvh->nvh_reserved2 = 0;
1951 if (buf == NULL || *buflen < sizeof (nvs_header_t))
1954 /* get method of encoding from first byte */
1955 encoding = nvh->nvh_encoding;
1956 nvl_endian = nvh->nvh_endian;
1959 case NVS_OP_GETSIZE:
1960 nvl_endian = host_endian;
1963 * add the size for encoding
1965 *buflen = sizeof (nvs_header_t);
1973 * Create an nvstream with proper encoding method
1976 case NV_ENCODE_NATIVE:
1978 * check endianness, in case we are unpacking
1981 if (nvl_endian != host_endian)
1983 err = nvs_native(&nvs, nvl, buf, buflen);
1986 err = nvs_xdr(&nvs, nvl, buf, buflen);
1997 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
1999 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2003 * Pack nvlist into contiguous memory
2007 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2010 #if defined(_KERNEL) && !defined(_BOOT)
2011 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2012 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2014 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2019 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2027 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2031 return (nvlist_common(nvl, *bufp, buflen, encoding,
2035 * Here is a difficult situation:
2036 * 1. The nvlist has fixed allocator properties.
2037 * All other nvlist routines (like nvlist_add_*, ...) use
2039 * 2. When using nvlist_pack() the user can specify his own
2040 * allocator properties (e.g. by using KM_NOSLEEP).
2042 * We use the user specified properties (2). A clearer solution
2043 * will be to remove the kmflag from nvlist_pack(), but we will
2044 * not change the interface.
2046 nv_priv_init(&nvpriv, nva, 0);
2048 if (err = nvlist_size(nvl, &alloc_size, encoding))
2051 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2054 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2055 NVS_OP_ENCODE)) != 0) {
2056 nv_mem_free(&nvpriv, buf, alloc_size);
2058 *buflen = alloc_size;
2066 * Unpack buf into an nvlist_t
2070 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2072 #if defined(_KERNEL) && !defined(_BOOT)
2073 return (nvlist_xunpack(buf, buflen, nvlp,
2074 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2076 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2081 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2089 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2092 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2101 * Native encoding functions
2105 * This structure is used when decoding a packed nvpair in
2106 * the native format. n_base points to a buffer containing the
2107 * packed nvpair. n_end is a pointer to the end of the buffer.
2108 * (n_end actually points to the first byte past the end of the
2109 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2110 * It points to the current data that we are decoding.
2111 * The amount of data left in the buffer is equal to n_end - n_curr.
2112 * n_flag is used to recognize a packed embedded list.
2121 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2124 switch (nvs->nvs_op) {
2127 nvs->nvs_private = native;
2128 native->n_curr = native->n_base = buf;
2129 native->n_end = buf + buflen;
2133 case NVS_OP_GETSIZE:
2134 nvs->nvs_private = native;
2135 native->n_curr = native->n_base = native->n_end = NULL;
2145 nvs_native_destroy(nvstream_t *nvs)
2150 native_cp(nvstream_t *nvs, void *buf, size_t size)
2152 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2154 if (native->n_curr + size > native->n_end)
2158 * The bcopy() below eliminates alignment requirement
2159 * on the buffer (stream) and is preferred over direct access.
2161 switch (nvs->nvs_op) {
2163 bcopy(buf, native->n_curr, size);
2166 bcopy(native->n_curr, buf, size);
2172 native->n_curr += size;
2177 * operate on nvlist_t header
2180 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2182 nvs_native_t *native = nvs->nvs_private;
2184 switch (nvs->nvs_op) {
2188 return (0); /* packed embedded list */
2192 /* copy version and nvflag of the nvlist_t */
2193 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2194 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2199 case NVS_OP_GETSIZE:
2201 * if calculate for packed embedded list
2202 * 4 for end of the embedded list
2204 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2205 * and 4 for end of the entire list
2207 if (native->n_flag) {
2211 *size += 2 * sizeof (int32_t) + 4;
2222 nvs_native_nvl_fini(nvstream_t *nvs)
2224 if (nvs->nvs_op == NVS_OP_ENCODE) {
2225 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2227 * Add 4 zero bytes at end of nvlist. They are used
2228 * for end detection by the decode routine.
2230 if (native->n_curr + sizeof (int) > native->n_end)
2233 bzero(native->n_curr, sizeof (int));
2234 native->n_curr += sizeof (int);
2241 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2243 if (nvs->nvs_op == NVS_OP_ENCODE) {
2244 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2245 nvlist_t *packed = (void *)
2246 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2248 * Null out the pointer that is meaningless in the packed
2249 * structure. The address may not be aligned, so we have
2252 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2255 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2259 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2261 if (nvs->nvs_op == NVS_OP_ENCODE) {
2262 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2263 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2264 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2265 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2268 * Null out pointers that are meaningless in the packed
2269 * structure. The addresses may not be aligned, so we have
2274 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2276 * Null out the pointer that is meaningless in the
2277 * packed structure. The address may not be aligned,
2278 * so we have to use bzero.
2280 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2283 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2287 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2289 switch (nvs->nvs_op) {
2290 case NVS_OP_ENCODE: {
2291 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2292 uint64_t *strp = (void *)
2293 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2295 * Null out pointers that are meaningless in the packed
2296 * structure. The addresses may not be aligned, so we have
2299 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2302 case NVS_OP_DECODE: {
2303 char **strp = (void *)NVP_VALUE(nvp);
2304 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2307 for (i = 0; i < NVP_NELEM(nvp); i++) {
2309 buf += strlen(buf) + 1;
2317 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2324 * We do the initial bcopy of the data before we look at
2325 * the nvpair type, because when we're decoding, we won't
2326 * have the correct values for the pair until we do the bcopy.
2328 switch (nvs->nvs_op) {
2331 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2338 /* verify nvp_name_sz, check the name string length */
2339 if (i_validate_nvpair_name(nvp) != 0)
2342 type = NVP_TYPE(nvp);
2345 * Verify type and nelem and get the value size.
2346 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2347 * is the size of the string(s) excluded.
2349 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2352 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2356 case DATA_TYPE_NVLIST:
2357 ret = nvpair_native_embedded(nvs, nvp);
2359 case DATA_TYPE_NVLIST_ARRAY:
2360 ret = nvpair_native_embedded_array(nvs, nvp);
2362 case DATA_TYPE_STRING_ARRAY:
2363 nvpair_native_string_array(nvs, nvp);
2373 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2375 uint64_t nvp_sz = nvp->nvp_size;
2377 switch (NVP_TYPE(nvp)) {
2378 case DATA_TYPE_NVLIST: {
2381 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2387 case DATA_TYPE_NVLIST_ARRAY: {
2390 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2400 if (nvp_sz > INT32_MAX)
2409 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2411 switch (nvs->nvs_op) {
2413 return (nvs_native_nvp_op(nvs, nvp));
2415 case NVS_OP_DECODE: {
2416 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2419 /* try to read the size value from the stream */
2420 if (native->n_curr + sizeof (int32_t) > native->n_end)
2422 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2424 /* sanity check the size value */
2425 if (decode_len < 0 ||
2426 decode_len > native->n_end - native->n_curr)
2432 * If at the end of the stream then move the cursor
2433 * forward, otherwise nvpair_native_op() will read
2434 * the entire nvpair at the same cursor position.
2437 native->n_curr += sizeof (int32_t);
2448 static const nvs_ops_t nvs_native_ops = {
2452 nvs_native_nvp_size,
2457 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2459 nvs_native_t native;
2462 nvs->nvs_ops = &nvs_native_ops;
2464 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2465 *buflen - sizeof (nvs_header_t))) != 0)
2468 err = nvs_operation(nvs, nvl, buflen);
2470 nvs_native_destroy(nvs);
2476 * XDR encoding functions
2478 * An xdr packed nvlist is encoded as:
2480 * - encoding methode and host endian (4 bytes)
2481 * - nvl_version (4 bytes)
2482 * - nvl_nvflag (4 bytes)
2484 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2485 * - encoded size of the nvpair (4 bytes)
2486 * - decoded size of the nvpair (4 bytes)
2487 * - name string, (4 + sizeof(NV_ALIGN4(string))
2488 * a string is coded as size (4 bytes) and data
2489 * - data type (4 bytes)
2490 * - number of elements in the nvpair (4 bytes)
2493 * - 2 zero's for end of the entire list (8 bytes)
2496 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2498 /* xdr data must be 4 byte aligned */
2499 if ((ulong_t)buf % 4 != 0)
2502 switch (nvs->nvs_op) {
2504 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2505 nvs->nvs_private = xdr;
2508 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2509 nvs->nvs_private = xdr;
2511 case NVS_OP_GETSIZE:
2512 nvs->nvs_private = NULL;
2520 nvs_xdr_destroy(nvstream_t *nvs)
2522 switch (nvs->nvs_op) {
2525 xdr_destroy((XDR *)nvs->nvs_private);
2533 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2535 switch (nvs->nvs_op) {
2537 case NVS_OP_DECODE: {
2538 XDR *xdr = nvs->nvs_private;
2540 if (!xdr_int(xdr, &nvl->nvl_version) ||
2541 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2545 case NVS_OP_GETSIZE: {
2547 * 2 * 4 for nvl_version + nvl_nvflag
2548 * and 8 for end of the entire list
2560 nvs_xdr_nvl_fini(nvstream_t *nvs)
2562 if (nvs->nvs_op == NVS_OP_ENCODE) {
2563 XDR *xdr = nvs->nvs_private;
2566 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2574 * The format of xdr encoded nvpair is:
2575 * encode_size, decode_size, name string, data type, nelem, data
2578 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2582 char *buf_end = (char *)nvp + nvp->nvp_size;
2584 uint_t nelem, buflen;
2586 XDR *xdr = nvs->nvs_private;
2588 ASSERT(xdr != NULL && nvp != NULL);
2591 if ((buf = NVP_NAME(nvp)) >= buf_end)
2593 buflen = buf_end - buf;
2595 if (!xdr_string(xdr, &buf, buflen - 1))
2597 nvp->nvp_name_sz = strlen(buf) + 1;
2599 /* type and nelem */
2600 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2601 !xdr_int(xdr, &nvp->nvp_value_elem))
2604 type = NVP_TYPE(nvp);
2605 nelem = nvp->nvp_value_elem;
2608 * Verify type and nelem and get the value size.
2609 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2610 * is the size of the string(s) excluded.
2612 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2615 /* if there is no data to extract then return */
2620 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2622 buflen = buf_end - buf;
2624 if (buflen < value_sz)
2628 case DATA_TYPE_NVLIST:
2629 if (nvs_embedded(nvs, (void *)buf) == 0)
2633 case DATA_TYPE_NVLIST_ARRAY:
2634 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2638 case DATA_TYPE_BOOLEAN:
2642 case DATA_TYPE_BYTE:
2643 case DATA_TYPE_INT8:
2644 case DATA_TYPE_UINT8:
2645 ret = xdr_char(xdr, buf);
2648 case DATA_TYPE_INT16:
2649 ret = xdr_short(xdr, (void *)buf);
2652 case DATA_TYPE_UINT16:
2653 ret = xdr_u_short(xdr, (void *)buf);
2656 case DATA_TYPE_BOOLEAN_VALUE:
2657 case DATA_TYPE_INT32:
2658 ret = xdr_int(xdr, (void *)buf);
2661 case DATA_TYPE_UINT32:
2662 ret = xdr_u_int(xdr, (void *)buf);
2665 case DATA_TYPE_INT64:
2666 ret = xdr_longlong_t(xdr, (void *)buf);
2669 case DATA_TYPE_UINT64:
2670 ret = xdr_u_longlong_t(xdr, (void *)buf);
2673 case DATA_TYPE_HRTIME:
2675 * NOTE: must expose the definition of hrtime_t here
2677 ret = xdr_longlong_t(xdr, (void *)buf);
2680 case DATA_TYPE_STRING:
2681 ret = xdr_string(xdr, &buf, buflen - 1);
2684 case DATA_TYPE_BYTE_ARRAY:
2685 ret = xdr_opaque(xdr, buf, nelem);
2688 case DATA_TYPE_INT8_ARRAY:
2689 case DATA_TYPE_UINT8_ARRAY:
2690 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
2691 (xdrproc_t)xdr_char);
2694 case DATA_TYPE_INT16_ARRAY:
2695 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
2696 sizeof (int16_t), (xdrproc_t)xdr_short);
2699 case DATA_TYPE_UINT16_ARRAY:
2700 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
2701 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
2704 case DATA_TYPE_BOOLEAN_ARRAY:
2705 case DATA_TYPE_INT32_ARRAY:
2706 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
2707 sizeof (int32_t), (xdrproc_t)xdr_int);
2710 case DATA_TYPE_UINT32_ARRAY:
2711 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
2712 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
2715 case DATA_TYPE_INT64_ARRAY:
2716 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
2717 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
2720 case DATA_TYPE_UINT64_ARRAY:
2721 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
2722 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
2725 case DATA_TYPE_STRING_ARRAY: {
2726 size_t len = nelem * sizeof (uint64_t);
2727 char **strp = (void *)buf;
2730 if (nvs->nvs_op == NVS_OP_DECODE)
2731 bzero(buf, len); /* don't trust packed data */
2733 for (i = 0; i < nelem; i++) {
2740 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
2743 if (nvs->nvs_op == NVS_OP_DECODE)
2745 len = strlen(buf) + 1;
2754 return (ret == TRUE ? 0 : EFAULT);
2758 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2760 data_type_t type = NVP_TYPE(nvp);
2762 * encode_size + decode_size + name string size + data type + nelem
2763 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
2765 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
2768 case DATA_TYPE_BOOLEAN:
2771 case DATA_TYPE_BOOLEAN_VALUE:
2772 case DATA_TYPE_BYTE:
2773 case DATA_TYPE_INT8:
2774 case DATA_TYPE_UINT8:
2775 case DATA_TYPE_INT16:
2776 case DATA_TYPE_UINT16:
2777 case DATA_TYPE_INT32:
2778 case DATA_TYPE_UINT32:
2779 nvp_sz += 4; /* 4 is the minimum xdr unit */
2782 case DATA_TYPE_INT64:
2783 case DATA_TYPE_UINT64:
2784 case DATA_TYPE_HRTIME:
2788 case DATA_TYPE_STRING:
2789 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
2792 case DATA_TYPE_BYTE_ARRAY:
2793 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
2796 case DATA_TYPE_BOOLEAN_ARRAY:
2797 case DATA_TYPE_INT8_ARRAY:
2798 case DATA_TYPE_UINT8_ARRAY:
2799 case DATA_TYPE_INT16_ARRAY:
2800 case DATA_TYPE_UINT16_ARRAY:
2801 case DATA_TYPE_INT32_ARRAY:
2802 case DATA_TYPE_UINT32_ARRAY:
2803 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
2806 case DATA_TYPE_INT64_ARRAY:
2807 case DATA_TYPE_UINT64_ARRAY:
2808 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
2811 case DATA_TYPE_STRING_ARRAY: {
2813 char **strs = (void *)NVP_VALUE(nvp);
2815 for (i = 0; i < NVP_NELEM(nvp); i++)
2816 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
2821 case DATA_TYPE_NVLIST:
2822 case DATA_TYPE_NVLIST_ARRAY: {
2824 int old_nvs_op = nvs->nvs_op;
2827 nvs->nvs_op = NVS_OP_GETSIZE;
2828 if (type == DATA_TYPE_NVLIST)
2829 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
2831 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
2832 nvs->nvs_op = old_nvs_op;
2845 if (nvp_sz > INT32_MAX)
2855 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
2856 * the largest nvpair that could be encoded in the buffer.
2858 * See comments above nvpair_xdr_op() for the format of xdr encoding.
2859 * The size of a xdr packed nvpair without any data is 5 words.
2861 * Using the size of the data directly as an estimate would be ok
2862 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
2863 * then the actual nvpair has space for an array of pointers to index
2864 * the strings. These pointers are not encoded into the packed xdr buffer.
2866 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
2867 * of length 0, then each string is endcoded in xdr format as a single word.
2868 * Therefore when expanded to an nvpair there will be 2.25 word used for
2869 * each string. (a int64_t allocated for pointer usage, and a single char
2870 * for the null termination.)
2872 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
2874 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
2875 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
2876 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
2877 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
2878 (NVS_XDR_DATA_LEN(x) * 2) + \
2879 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
2882 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2884 XDR *xdr = nvs->nvs_private;
2885 int32_t encode_len, decode_len;
2887 switch (nvs->nvs_op) {
2888 case NVS_OP_ENCODE: {
2891 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
2894 decode_len = nvp->nvp_size;
2895 encode_len = nvsize;
2896 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
2899 return (nvs_xdr_nvp_op(nvs, nvp));
2901 case NVS_OP_DECODE: {
2902 struct xdr_bytesrec bytesrec;
2904 /* get the encode and decode size */
2905 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
2909 /* are we at the end of the stream? */
2913 /* sanity check the size parameter */
2914 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
2917 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
2928 static const struct nvs_ops nvs_xdr_ops = {
2937 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2942 nvs->nvs_ops = &nvs_xdr_ops;
2944 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
2945 *buflen - sizeof (nvs_header_t))) != 0)
2948 err = nvs_operation(nvs, nvl, buflen);
2950 nvs_xdr_destroy(nvs);