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 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #include <sys/stropts.h>
30 #include <sys/debug.h>
31 #include <sys/isa_defs.h>
32 #include <sys/int_limits.h>
33 #include <sys/nvpair.h>
34 #include <sys/nvpair_impl.h>
35 #include <rpc/types.h>
38 #if defined(_KERNEL) && !defined(_BOOT)
39 #include <sys/varargs.h>
46 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
51 * nvpair.c - Provides kernel & userland interfaces for manipulating
66 * +--------------+ last i_nvp in list
67 * | nvpriv_t | +--------------------->
69 * +--+- nvp_list | | +------------+
70 * | | nvp_last -+--+ + nv_alloc_t |
71 * | | nvp_curr | |------------|
72 * | | nvp_nva -+----> | nva_ops |
73 * | | nvp_stat | | nva_arg |
74 * | +--------------+ +------------+
78 * +---------------------+ +-------------------+
79 * | i_nvp_t | +-->| i_nvp_t | +-->
80 * |---------------------| | |-------------------| |
81 * | nvi_next -+--+ | nvi_next -+--+
82 * | nvi_prev (NULL) | <----+ nvi_prev |
83 * | . . . . . . . . . . | | . . . . . . . . . |
84 * | nvp (nvpair_t) | | nvp (nvpair_t) |
85 * | - nvp_size | | - nvp_size |
86 * | - nvp_name_sz | | - nvp_name_sz |
87 * | - nvp_value_elem | | - nvp_value_elem |
88 * | - nvp_type | | - nvp_type |
89 * | - data ... | | - data ... |
90 * +---------------------+ +-------------------+
94 * +---------------------+ +---------------------+
95 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
96 * |---------------------| | | |---------------------|
97 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
98 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
99 * | . . . . . . . . . | | . . . . . . . . . |
100 * | nvp (nvpair_t) | | nvp (nvpair_t) |
101 * | - nvp_size | | - nvp_size |
102 * | - nvp_name_sz | | - nvp_name_sz |
103 * | - nvp_value_elem | | - nvp_value_elem |
104 * | - DATA_TYPE_NVLIST | | - nvp_type |
105 * | - data (embedded) | | - data ... |
106 * | nvlist name | +---------------------+
107 * | +--------------+ |
109 * | |--------------| |
110 * | | nvl_version | |
112 * | | nvl_priv --+---+---->
115 * | +--------------+ |
116 * +---------------------+
119 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
120 * allow value to be aligned on 8 byte boundary
122 * name_len is the length of the name string including the null terminator
125 #define NVP_SIZE_CALC(name_len, data_len) \
126 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
128 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
129 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
130 uint_t nelem, const void *data);
132 #define NV_STAT_EMBEDDED 0x1
133 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
134 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
136 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
137 #define NVPAIR2I_NVP(nvp) \
138 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
142 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
150 va_start(valist, nvo);
151 if (nva->nva_ops->nv_ao_init != NULL)
152 err = nva->nva_ops->nv_ao_init(nva, valist);
159 nv_alloc_reset(nv_alloc_t *nva)
161 if (nva->nva_ops->nv_ao_reset != NULL)
162 nva->nva_ops->nv_ao_reset(nva);
166 nv_alloc_fini(nv_alloc_t *nva)
168 if (nva->nva_ops->nv_ao_fini != NULL)
169 nva->nva_ops->nv_ao_fini(nva);
173 nvlist_lookup_nv_alloc(nvlist_t *nvl)
178 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
181 return (priv->nvp_nva);
185 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
187 nv_alloc_t *nva = nvp->nvp_nva;
190 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
197 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
199 nv_alloc_t *nva = nvp->nvp_nva;
201 nva->nva_ops->nv_ao_free(nva, buf, size);
205 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
207 bzero(priv, sizeof (priv));
210 priv->nvp_stat = stat;
214 nv_priv_alloc(nv_alloc_t *nva)
219 * nv_mem_alloc() cannot called here because it needs the priv
222 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
225 nv_priv_init(priv, nva, 0);
231 * Embedded lists need their own nvpriv_t's. We create a new
232 * nvpriv_t using the parameters and allocator from the parent
236 nv_priv_alloc_embedded(nvpriv_t *priv)
240 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
243 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
249 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
251 nvl->nvl_version = NV_VERSION;
252 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
253 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
259 * nvlist_alloc - Allocate nvlist.
263 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
265 #if defined(_KERNEL) && !defined(_BOOT)
266 return (nvlist_xalloc(nvlp, nvflag,
267 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
269 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
274 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
278 if (nvlp == NULL || nva == NULL)
281 if ((priv = nv_priv_alloc(nva)) == NULL)
284 if ((*nvlp = nv_mem_zalloc(priv,
285 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
286 nv_mem_free(priv, priv, sizeof (nvpriv_t));
290 nvlist_init(*nvlp, nvflag, priv);
296 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
299 nvp_buf_alloc(nvlist_t *nvl, size_t len)
301 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
307 * Allocate the buffer
309 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
311 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
321 * nvp_buf_free - de-Allocate an i_nvp_t.
324 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
326 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
327 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
329 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
333 * nvp_buf_link - link a new nv pair into the nvlist.
336 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
338 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
339 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
341 /* Put element at end of nvlist */
342 if (priv->nvp_list == NULL) {
343 priv->nvp_list = priv->nvp_last = curr;
345 curr->nvi_prev = priv->nvp_last;
346 priv->nvp_last->nvi_next = curr;
347 priv->nvp_last = curr;
352 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
355 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
357 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
358 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
361 * protect nvlist_next_nvpair() against walking on freed memory.
363 if (priv->nvp_curr == curr)
364 priv->nvp_curr = curr->nvi_next;
366 if (curr == priv->nvp_list)
367 priv->nvp_list = curr->nvi_next;
369 curr->nvi_prev->nvi_next = curr->nvi_next;
371 if (curr == priv->nvp_last)
372 priv->nvp_last = curr->nvi_prev;
374 curr->nvi_next->nvi_prev = curr->nvi_prev;
378 * take a nvpair type and number of elements and make sure the are valid
381 i_validate_type_nelem(data_type_t type, uint_t nelem)
384 case DATA_TYPE_BOOLEAN:
388 case DATA_TYPE_BOOLEAN_VALUE:
391 case DATA_TYPE_UINT8:
392 case DATA_TYPE_INT16:
393 case DATA_TYPE_UINT16:
394 case DATA_TYPE_INT32:
395 case DATA_TYPE_UINT32:
396 case DATA_TYPE_INT64:
397 case DATA_TYPE_UINT64:
398 case DATA_TYPE_STRING:
399 case DATA_TYPE_HRTIME:
400 case DATA_TYPE_NVLIST:
404 case DATA_TYPE_BOOLEAN_ARRAY:
405 case DATA_TYPE_BYTE_ARRAY:
406 case DATA_TYPE_INT8_ARRAY:
407 case DATA_TYPE_UINT8_ARRAY:
408 case DATA_TYPE_INT16_ARRAY:
409 case DATA_TYPE_UINT16_ARRAY:
410 case DATA_TYPE_INT32_ARRAY:
411 case DATA_TYPE_UINT32_ARRAY:
412 case DATA_TYPE_INT64_ARRAY:
413 case DATA_TYPE_UINT64_ARRAY:
414 case DATA_TYPE_STRING_ARRAY:
415 case DATA_TYPE_NVLIST_ARRAY:
416 /* we allow arrays with 0 elements */
425 * Verify nvp_name_sz and check the name string length.
428 i_validate_nvpair_name(nvpair_t *nvp)
430 if ((nvp->nvp_name_sz <= 0) ||
431 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
434 /* verify the name string, make sure its terminated */
435 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
438 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
442 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
445 case DATA_TYPE_BOOLEAN_VALUE:
446 if (*(boolean_t *)data != B_TRUE &&
447 *(boolean_t *)data != B_FALSE)
450 case DATA_TYPE_BOOLEAN_ARRAY: {
453 for (i = 0; i < nelem; i++)
454 if (((boolean_t *)data)[i] != B_TRUE &&
455 ((boolean_t *)data)[i] != B_FALSE)
467 * This function takes a pointer to what should be a nvpair and it's size
468 * and then verifies that all the nvpair fields make sense and can be
469 * trusted. This function is used when decoding packed nvpairs.
472 i_validate_nvpair(nvpair_t *nvp)
474 data_type_t type = NVP_TYPE(nvp);
477 /* verify nvp_name_sz, check the name string length */
478 if (i_validate_nvpair_name(nvp) != 0)
481 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
485 * verify nvp_type, nvp_value_elem, and also possibly
486 * verify string values and get the value size.
488 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
489 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
490 if (size2 < 0 || size1 != NV_ALIGN(size2))
497 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
502 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
505 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
506 nvpair_t *nvp = &curr->nvi_nvp;
509 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
510 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
518 * Frees all memory allocated for an nvpair (like embedded lists) with
519 * the exception of the nvpair buffer itself.
522 nvpair_free(nvpair_t *nvp)
524 switch (NVP_TYPE(nvp)) {
525 case DATA_TYPE_NVLIST:
526 nvlist_free(EMBEDDED_NVL(nvp));
528 case DATA_TYPE_NVLIST_ARRAY: {
529 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
532 for (i = 0; i < NVP_NELEM(nvp); i++)
534 nvlist_free(nvlp[i]);
543 * nvlist_free - free an unpacked nvlist
546 nvlist_free(nvlist_t *nvl)
552 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
556 * Unpacked nvlist are linked through i_nvp_t
558 curr = priv->nvp_list;
559 while (curr != NULL) {
560 nvpair_t *nvp = &curr->nvi_nvp;
561 curr = curr->nvi_next;
564 nvp_buf_free(nvl, nvp);
567 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
568 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
572 nv_mem_free(priv, priv, sizeof (nvpriv_t));
576 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
578 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
584 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
585 if (&curr->nvi_nvp == nvp)
592 * Make a copy of nvlist
596 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
598 #if defined(_KERNEL) && !defined(_BOOT)
599 return (nvlist_xdup(nvl, nvlp,
600 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
602 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
607 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
612 if (nvl == NULL || nvlp == NULL)
615 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
618 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
627 * Remove all with matching name
630 nvlist_remove_all(nvlist_t *nvl, const char *name)
636 if (nvl == NULL || name == NULL ||
637 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
640 curr = priv->nvp_list;
641 while (curr != NULL) {
642 nvpair_t *nvp = &curr->nvi_nvp;
644 curr = curr->nvi_next;
645 if (strcmp(name, NVP_NAME(nvp)) != 0)
648 nvp_buf_unlink(nvl, nvp);
650 nvp_buf_free(nvl, nvp);
659 * Remove first one with matching name and type
662 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
667 if (nvl == NULL || name == NULL ||
668 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
671 curr = priv->nvp_list;
672 while (curr != NULL) {
673 nvpair_t *nvp = &curr->nvi_nvp;
675 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
676 nvp_buf_unlink(nvl, nvp);
678 nvp_buf_free(nvl, nvp);
682 curr = curr->nvi_next;
689 * This function calculates the size of an nvpair value.
691 * The data argument controls the behavior in case of the data types
692 * DATA_TYPE_STRING and
693 * DATA_TYPE_STRING_ARRAY
694 * Is data == NULL then the size of the string(s) is excluded.
697 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
701 if (i_validate_type_nelem(type, nelem) != 0)
704 /* Calculate required size for holding value */
706 case DATA_TYPE_BOOLEAN:
709 case DATA_TYPE_BOOLEAN_VALUE:
710 value_sz = sizeof (boolean_t);
713 value_sz = sizeof (uchar_t);
716 value_sz = sizeof (int8_t);
718 case DATA_TYPE_UINT8:
719 value_sz = sizeof (uint8_t);
721 case DATA_TYPE_INT16:
722 value_sz = sizeof (int16_t);
724 case DATA_TYPE_UINT16:
725 value_sz = sizeof (uint16_t);
727 case DATA_TYPE_INT32:
728 value_sz = sizeof (int32_t);
730 case DATA_TYPE_UINT32:
731 value_sz = sizeof (uint32_t);
733 case DATA_TYPE_INT64:
734 value_sz = sizeof (int64_t);
736 case DATA_TYPE_UINT64:
737 value_sz = sizeof (uint64_t);
739 case DATA_TYPE_STRING:
743 value_sz = strlen(data) + 1;
745 case DATA_TYPE_BOOLEAN_ARRAY:
746 value_sz = (uint64_t)nelem * sizeof (boolean_t);
748 case DATA_TYPE_BYTE_ARRAY:
749 value_sz = (uint64_t)nelem * sizeof (uchar_t);
751 case DATA_TYPE_INT8_ARRAY:
752 value_sz = (uint64_t)nelem * sizeof (int8_t);
754 case DATA_TYPE_UINT8_ARRAY:
755 value_sz = (uint64_t)nelem * sizeof (uint8_t);
757 case DATA_TYPE_INT16_ARRAY:
758 value_sz = (uint64_t)nelem * sizeof (int16_t);
760 case DATA_TYPE_UINT16_ARRAY:
761 value_sz = (uint64_t)nelem * sizeof (uint16_t);
763 case DATA_TYPE_INT32_ARRAY:
764 value_sz = (uint64_t)nelem * sizeof (int32_t);
766 case DATA_TYPE_UINT32_ARRAY:
767 value_sz = (uint64_t)nelem * sizeof (uint32_t);
769 case DATA_TYPE_INT64_ARRAY:
770 value_sz = (uint64_t)nelem * sizeof (int64_t);
772 case DATA_TYPE_UINT64_ARRAY:
773 value_sz = (uint64_t)nelem * sizeof (uint64_t);
775 case DATA_TYPE_STRING_ARRAY:
776 value_sz = (uint64_t)nelem * sizeof (uint64_t);
779 char *const *strs = data;
782 /* no alignment requirement for strings */
783 for (i = 0; i < nelem; i++) {
786 value_sz += strlen(strs[i]) + 1;
790 case DATA_TYPE_HRTIME:
791 value_sz = sizeof (hrtime_t);
793 case DATA_TYPE_NVLIST:
794 value_sz = NV_ALIGN(sizeof (nvlist_t));
796 case DATA_TYPE_NVLIST_ARRAY:
797 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
798 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
804 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
808 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
813 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
814 nvl->nvl_priv)) == NULL)
817 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
819 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
820 nvlist_free(emb_nvl);
821 emb_nvl->nvl_priv = 0;
828 * nvlist_add_common - Add new <name,value> pair to nvlist
831 nvlist_add_common(nvlist_t *nvl, const char *name,
832 data_type_t type, uint_t nelem, const void *data)
837 int nvp_sz, name_sz, value_sz;
840 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
843 if (nelem != 0 && data == NULL)
847 * Verify type and nelem and get the value size.
848 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
849 * is the size of the string(s) included.
851 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
854 if (i_validate_nvpair_value(type, nelem, data) != 0)
858 * If we're adding an nvlist or nvlist array, ensure that we are not
859 * adding the input nvlist to itself, which would cause recursion,
860 * and ensure that no NULL nvlist pointers are present.
863 case DATA_TYPE_NVLIST:
864 if (data == nvl || data == NULL)
867 case DATA_TYPE_NVLIST_ARRAY: {
868 nvlist_t **onvlp = (nvlist_t **)data;
869 for (i = 0; i < nelem; i++) {
870 if (onvlp[i] == nvl || onvlp[i] == NULL)
879 /* calculate sizes of the nvpair elements and the nvpair itself */
880 name_sz = strlen(name) + 1;
882 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
884 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
887 ASSERT(nvp->nvp_size == nvp_sz);
888 nvp->nvp_name_sz = name_sz;
889 nvp->nvp_value_elem = nelem;
890 nvp->nvp_type = type;
891 bcopy(name, NVP_NAME(nvp), name_sz);
894 case DATA_TYPE_BOOLEAN:
896 case DATA_TYPE_STRING_ARRAY: {
897 char *const *strs = data;
898 char *buf = NVP_VALUE(nvp);
899 char **cstrs = (void *)buf;
901 /* skip pre-allocated space for pointer array */
902 buf += nelem * sizeof (uint64_t);
903 for (i = 0; i < nelem; i++) {
904 int slen = strlen(strs[i]) + 1;
905 bcopy(strs[i], buf, slen);
911 case DATA_TYPE_NVLIST: {
912 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
913 nvlist_t *onvl = (nvlist_t *)data;
915 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
916 nvp_buf_free(nvl, nvp);
921 case DATA_TYPE_NVLIST_ARRAY: {
922 nvlist_t **onvlp = (nvlist_t **)data;
923 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
924 nvlist_t *embedded = (nvlist_t *)
925 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
927 for (i = 0; i < nelem; i++) {
928 if ((err = nvlist_copy_embedded(nvl,
929 onvlp[i], embedded)) != 0) {
931 * Free any successfully created lists
934 nvp_buf_free(nvl, nvp);
938 nvlp[i] = embedded++;
943 bcopy(data, NVP_VALUE(nvp), value_sz);
946 /* if unique name, remove before add */
947 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
948 (void) nvlist_remove_all(nvl, name);
949 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
950 (void) nvlist_remove(nvl, name, type);
952 nvp_buf_link(nvl, nvp);
958 nvlist_add_boolean(nvlist_t *nvl, const char *name)
960 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
964 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
966 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
970 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
972 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
976 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
978 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
982 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
984 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
988 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
990 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
994 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
996 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1000 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1002 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1006 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1008 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1012 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1014 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1018 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1020 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1024 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1026 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1030 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1031 boolean_t *a, uint_t n)
1033 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1037 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1039 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1043 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1045 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1049 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1051 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1055 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1057 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1061 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1063 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1067 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1069 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1073 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1075 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1079 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1081 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1085 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1087 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1091 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1092 char *const *a, uint_t n)
1094 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1098 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1100 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1104 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1106 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1110 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1112 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1115 /* reading name-value pairs */
1117 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1123 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1126 curr = NVPAIR2I_NVP(nvp);
1129 * Ensure that nvp is a valid nvpair on this nvlist.
1130 * NB: nvp_curr is used only as a hint so that we don't always
1131 * have to walk the list to determine if nvp is still on the list.
1134 curr = priv->nvp_list;
1135 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1136 curr = curr->nvi_next;
1140 priv->nvp_curr = curr;
1142 return (curr != NULL ? &curr->nvi_nvp : NULL);
1146 nvpair_name(nvpair_t *nvp)
1148 return (NVP_NAME(nvp));
1152 nvpair_type(nvpair_t *nvp)
1154 return (NVP_TYPE(nvp));
1158 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1160 if (nvp == NULL || nvpair_type(nvp) != type)
1164 * For non-array types, we copy the data.
1165 * For array types (including string), we set a pointer.
1168 case DATA_TYPE_BOOLEAN:
1173 case DATA_TYPE_BOOLEAN_VALUE:
1174 case DATA_TYPE_BYTE:
1175 case DATA_TYPE_INT8:
1176 case DATA_TYPE_UINT8:
1177 case DATA_TYPE_INT16:
1178 case DATA_TYPE_UINT16:
1179 case DATA_TYPE_INT32:
1180 case DATA_TYPE_UINT32:
1181 case DATA_TYPE_INT64:
1182 case DATA_TYPE_UINT64:
1183 case DATA_TYPE_HRTIME:
1186 bcopy(NVP_VALUE(nvp), data,
1187 (size_t)i_get_value_size(type, NULL, 1));
1192 case DATA_TYPE_NVLIST:
1193 case DATA_TYPE_STRING:
1196 *(void **)data = (void *)NVP_VALUE(nvp);
1201 case DATA_TYPE_BOOLEAN_ARRAY:
1202 case DATA_TYPE_BYTE_ARRAY:
1203 case DATA_TYPE_INT8_ARRAY:
1204 case DATA_TYPE_UINT8_ARRAY:
1205 case DATA_TYPE_INT16_ARRAY:
1206 case DATA_TYPE_UINT16_ARRAY:
1207 case DATA_TYPE_INT32_ARRAY:
1208 case DATA_TYPE_UINT32_ARRAY:
1209 case DATA_TYPE_INT64_ARRAY:
1210 case DATA_TYPE_UINT64_ARRAY:
1211 case DATA_TYPE_STRING_ARRAY:
1212 case DATA_TYPE_NVLIST_ARRAY:
1213 if (nelem == NULL || data == NULL)
1215 if ((*nelem = NVP_NELEM(nvp)) != 0)
1216 *(void **)data = (void *)NVP_VALUE(nvp);
1218 *(void **)data = NULL;
1229 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1230 uint_t *nelem, void *data)
1236 if (name == NULL || nvl == NULL ||
1237 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1240 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1243 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1244 nvp = &curr->nvi_nvp;
1246 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1247 return (nvpair_value_common(nvp, type, nelem, data));
1254 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1256 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1260 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1262 return (nvlist_lookup_common(nvl, name,
1263 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1267 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1269 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1273 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1275 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1279 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1281 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1285 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1287 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1291 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1293 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1297 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1299 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1303 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1305 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1309 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1311 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1315 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1317 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1321 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1323 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1327 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1329 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1333 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1334 boolean_t **a, uint_t *n)
1336 return (nvlist_lookup_common(nvl, name,
1337 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1341 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1342 uchar_t **a, uint_t *n)
1344 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1348 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1350 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1354 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1355 uint8_t **a, uint_t *n)
1357 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1361 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1362 int16_t **a, uint_t *n)
1364 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1368 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1369 uint16_t **a, uint_t *n)
1371 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1375 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1376 int32_t **a, uint_t *n)
1378 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1382 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1383 uint32_t **a, uint_t *n)
1385 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1389 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1390 int64_t **a, uint_t *n)
1392 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1396 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1397 uint64_t **a, uint_t *n)
1399 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1403 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1404 char ***a, uint_t *n)
1406 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1410 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1411 nvlist_t ***a, uint_t *n)
1413 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1417 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1419 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1423 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1427 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1431 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1436 switch (type = va_arg(ap, data_type_t)) {
1437 case DATA_TYPE_BOOLEAN:
1438 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1441 case DATA_TYPE_BOOLEAN_VALUE:
1442 case DATA_TYPE_BYTE:
1443 case DATA_TYPE_INT8:
1444 case DATA_TYPE_UINT8:
1445 case DATA_TYPE_INT16:
1446 case DATA_TYPE_UINT16:
1447 case DATA_TYPE_INT32:
1448 case DATA_TYPE_UINT32:
1449 case DATA_TYPE_INT64:
1450 case DATA_TYPE_UINT64:
1451 case DATA_TYPE_HRTIME:
1452 case DATA_TYPE_STRING:
1453 case DATA_TYPE_NVLIST:
1454 val = va_arg(ap, void *);
1455 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1458 case DATA_TYPE_BYTE_ARRAY:
1459 case DATA_TYPE_BOOLEAN_ARRAY:
1460 case DATA_TYPE_INT8_ARRAY:
1461 case DATA_TYPE_UINT8_ARRAY:
1462 case DATA_TYPE_INT16_ARRAY:
1463 case DATA_TYPE_UINT16_ARRAY:
1464 case DATA_TYPE_INT32_ARRAY:
1465 case DATA_TYPE_UINT32_ARRAY:
1466 case DATA_TYPE_INT64_ARRAY:
1467 case DATA_TYPE_UINT64_ARRAY:
1468 case DATA_TYPE_STRING_ARRAY:
1469 case DATA_TYPE_NVLIST_ARRAY:
1470 val = va_arg(ap, void *);
1471 nelem = va_arg(ap, uint_t *);
1472 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1479 if (ret == ENOENT && noentok)
1488 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1494 if (name == NULL || nvl == NULL ||
1495 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1498 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1501 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1502 nvp = &curr->nvi_nvp;
1504 if (strcmp(name, NVP_NAME(nvp)) == 0) {
1514 nvlist_exists(nvlist_t *nvl, const char *name)
1520 if (name == NULL || nvl == NULL ||
1521 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1524 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1525 nvp = &curr->nvi_nvp;
1527 if (strcmp(name, NVP_NAME(nvp)) == 0)
1535 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1537 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1541 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1543 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1547 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1549 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1553 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1555 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1559 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1561 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1565 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1567 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1571 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1573 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1577 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1579 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1583 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1585 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1589 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1591 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1595 nvpair_value_string(nvpair_t *nvp, char **val)
1597 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1601 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1603 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1607 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1609 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1613 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1615 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1619 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1621 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1625 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1627 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1631 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1633 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1637 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1639 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1643 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1645 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1649 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1651 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1655 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1657 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1661 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1663 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1667 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1669 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1673 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1675 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1679 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1681 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1685 * Add specified pair to the list.
1688 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1690 if (nvl == NULL || nvp == NULL)
1693 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1694 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1698 * Merge the supplied nvlists and put the result in dst.
1699 * The merged list will contain all names specified in both lists,
1700 * the values are taken from nvl in the case of duplicates.
1701 * Return 0 on success.
1705 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1707 if (nvl == NULL || dst == NULL)
1711 return (nvlist_copy_pairs(nvl, dst));
1717 * Encoding related routines
1719 #define NVS_OP_ENCODE 0
1720 #define NVS_OP_DECODE 1
1721 #define NVS_OP_GETSIZE 2
1723 typedef struct nvs_ops nvs_ops_t;
1727 const nvs_ops_t *nvs_ops;
1733 * nvs operations are:
1735 * encoding / decoding of a nvlist header (nvlist_t)
1736 * calculates the size used for header and end detection
1739 * responsible for the first part of encoding / decoding of an nvpair
1740 * calculates the decoded size of an nvpair
1743 * second part of encoding / decoding of an nvpair
1746 * calculates the encoding size of an nvpair
1749 * encodes the end detection mark (zeros).
1752 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
1753 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
1754 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
1755 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
1756 int (*nvs_nvl_fini)(nvstream_t *);
1760 char nvh_encoding; /* nvs encoding method */
1761 char nvh_endian; /* nvs endian */
1762 char nvh_reserved1; /* reserved for future use */
1763 char nvh_reserved2; /* reserved for future use */
1767 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
1769 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
1773 * Walk nvpair in list and encode each nvpair
1775 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
1776 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
1779 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
1783 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
1790 * Get decoded size of next pair in stream, alloc
1791 * memory for nvpair_t, then decode the nvpair
1793 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
1794 if (nvsize == 0) /* end of list */
1797 /* make sure len makes sense */
1798 if (nvsize < NVP_SIZE_CALC(1, 0))
1801 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
1804 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
1805 nvp_buf_free(nvl, nvp);
1809 if (i_validate_nvpair(nvp) != 0) {
1811 nvp_buf_free(nvl, nvp);
1815 nvp_buf_link(nvl, nvp);
1821 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
1823 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
1825 uint64_t nvsize = *buflen;
1829 * Get encoded size of nvpairs in nvlist
1831 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1832 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
1835 if ((nvsize += size) > INT32_MAX)
1844 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
1848 if (nvl->nvl_priv == 0)
1852 * Perform the operation, starting with header, then each nvpair
1854 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
1857 switch (nvs->nvs_op) {
1859 err = nvs_encode_pairs(nvs, nvl);
1863 err = nvs_decode_pairs(nvs, nvl);
1866 case NVS_OP_GETSIZE:
1867 err = nvs_getsize_pairs(nvs, nvl, buflen);
1878 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
1880 switch (nvs->nvs_op) {
1882 return (nvs_operation(nvs, embedded, NULL));
1884 case NVS_OP_DECODE: {
1888 if (embedded->nvl_version != NV_VERSION)
1891 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
1894 nvlist_init(embedded, embedded->nvl_nvflag, priv);
1896 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
1897 nvlist_free(embedded);
1908 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
1910 size_t nelem = NVP_NELEM(nvp);
1911 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
1914 switch (nvs->nvs_op) {
1916 for (i = 0; i < nelem; i++)
1917 if (nvs_embedded(nvs, nvlp[i]) != 0)
1921 case NVS_OP_DECODE: {
1922 size_t len = nelem * sizeof (uint64_t);
1923 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
1925 bzero(nvlp, len); /* don't trust packed data */
1926 for (i = 0; i < nelem; i++) {
1927 if (nvs_embedded(nvs, embedded) != 0) {
1932 nvlp[i] = embedded++;
1936 case NVS_OP_GETSIZE: {
1937 uint64_t nvsize = 0;
1939 for (i = 0; i < nelem; i++) {
1942 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
1945 if ((nvsize += nvp_sz) > INT32_MAX)
1959 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
1960 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
1963 * Common routine for nvlist operations:
1964 * encode, decode, getsize (encoded size).
1967 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
1973 #ifdef _LITTLE_ENDIAN
1974 int host_endian = 1;
1976 int host_endian = 0;
1977 #endif /* _LITTLE_ENDIAN */
1978 nvs_header_t *nvh = (void *)buf;
1980 if (buflen == NULL || nvl == NULL ||
1981 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1984 nvs.nvs_op = nvs_op;
1987 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
1988 * a buffer is allocated. The first 4 bytes in the buffer are
1989 * used for encoding method and host endian.
1993 if (buf == NULL || *buflen < sizeof (nvs_header_t))
1996 nvh->nvh_encoding = encoding;
1997 nvh->nvh_endian = nvl_endian = host_endian;
1998 nvh->nvh_reserved1 = 0;
1999 nvh->nvh_reserved2 = 0;
2003 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2006 /* get method of encoding from first byte */
2007 encoding = nvh->nvh_encoding;
2008 nvl_endian = nvh->nvh_endian;
2011 case NVS_OP_GETSIZE:
2012 nvl_endian = host_endian;
2015 * add the size for encoding
2017 *buflen = sizeof (nvs_header_t);
2025 * Create an nvstream with proper encoding method
2028 case NV_ENCODE_NATIVE:
2030 * check endianness, in case we are unpacking
2033 if (nvl_endian != host_endian)
2035 err = nvs_native(&nvs, nvl, buf, buflen);
2038 err = nvs_xdr(&nvs, nvl, buf, buflen);
2049 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2051 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2055 * Pack nvlist into contiguous memory
2059 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2062 #if defined(_KERNEL) && !defined(_BOOT)
2063 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2064 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2066 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2071 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2079 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2083 return (nvlist_common(nvl, *bufp, buflen, encoding,
2087 * Here is a difficult situation:
2088 * 1. The nvlist has fixed allocator properties.
2089 * All other nvlist routines (like nvlist_add_*, ...) use
2091 * 2. When using nvlist_pack() the user can specify his own
2092 * allocator properties (e.g. by using KM_NOSLEEP).
2094 * We use the user specified properties (2). A clearer solution
2095 * will be to remove the kmflag from nvlist_pack(), but we will
2096 * not change the interface.
2098 nv_priv_init(&nvpriv, nva, 0);
2100 if (err = nvlist_size(nvl, &alloc_size, encoding))
2103 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2106 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2107 NVS_OP_ENCODE)) != 0) {
2108 nv_mem_free(&nvpriv, buf, alloc_size);
2110 *buflen = alloc_size;
2118 * Unpack buf into an nvlist_t
2122 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2124 #if defined(_KERNEL) && !defined(_BOOT)
2125 return (nvlist_xunpack(buf, buflen, nvlp,
2126 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2128 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2133 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2141 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2144 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2153 * Native encoding functions
2157 * This structure is used when decoding a packed nvpair in
2158 * the native format. n_base points to a buffer containing the
2159 * packed nvpair. n_end is a pointer to the end of the buffer.
2160 * (n_end actually points to the first byte past the end of the
2161 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2162 * It points to the current data that we are decoding.
2163 * The amount of data left in the buffer is equal to n_end - n_curr.
2164 * n_flag is used to recognize a packed embedded list.
2173 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2176 switch (nvs->nvs_op) {
2179 nvs->nvs_private = native;
2180 native->n_curr = native->n_base = buf;
2181 native->n_end = buf + buflen;
2185 case NVS_OP_GETSIZE:
2186 nvs->nvs_private = native;
2187 native->n_curr = native->n_base = native->n_end = NULL;
2197 nvs_native_destroy(nvstream_t *nvs)
2202 native_cp(nvstream_t *nvs, void *buf, size_t size)
2204 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2206 if (native->n_curr + size > native->n_end)
2210 * The bcopy() below eliminates alignment requirement
2211 * on the buffer (stream) and is preferred over direct access.
2213 switch (nvs->nvs_op) {
2215 bcopy(buf, native->n_curr, size);
2218 bcopy(native->n_curr, buf, size);
2224 native->n_curr += size;
2229 * operate on nvlist_t header
2232 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2234 nvs_native_t *native = nvs->nvs_private;
2236 switch (nvs->nvs_op) {
2240 return (0); /* packed embedded list */
2244 /* copy version and nvflag of the nvlist_t */
2245 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2246 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2251 case NVS_OP_GETSIZE:
2253 * if calculate for packed embedded list
2254 * 4 for end of the embedded list
2256 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2257 * and 4 for end of the entire list
2259 if (native->n_flag) {
2263 *size += 2 * sizeof (int32_t) + 4;
2274 nvs_native_nvl_fini(nvstream_t *nvs)
2276 if (nvs->nvs_op == NVS_OP_ENCODE) {
2277 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2279 * Add 4 zero bytes at end of nvlist. They are used
2280 * for end detection by the decode routine.
2282 if (native->n_curr + sizeof (int) > native->n_end)
2285 bzero(native->n_curr, sizeof (int));
2286 native->n_curr += sizeof (int);
2293 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2295 if (nvs->nvs_op == NVS_OP_ENCODE) {
2296 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2297 nvlist_t *packed = (void *)
2298 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2300 * Null out the pointer that is meaningless in the packed
2301 * structure. The address may not be aligned, so we have
2304 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2307 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2311 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2313 if (nvs->nvs_op == NVS_OP_ENCODE) {
2314 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2315 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2316 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2317 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2320 * Null out pointers that are meaningless in the packed
2321 * structure. The addresses may not be aligned, so we have
2326 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2328 * Null out the pointer that is meaningless in the
2329 * packed structure. The address may not be aligned,
2330 * so we have to use bzero.
2332 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2335 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2339 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2341 switch (nvs->nvs_op) {
2342 case NVS_OP_ENCODE: {
2343 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2344 uint64_t *strp = (void *)
2345 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2347 * Null out pointers that are meaningless in the packed
2348 * structure. The addresses may not be aligned, so we have
2351 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2354 case NVS_OP_DECODE: {
2355 char **strp = (void *)NVP_VALUE(nvp);
2356 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2359 for (i = 0; i < NVP_NELEM(nvp); i++) {
2361 buf += strlen(buf) + 1;
2369 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2376 * We do the initial bcopy of the data before we look at
2377 * the nvpair type, because when we're decoding, we won't
2378 * have the correct values for the pair until we do the bcopy.
2380 switch (nvs->nvs_op) {
2383 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2390 /* verify nvp_name_sz, check the name string length */
2391 if (i_validate_nvpair_name(nvp) != 0)
2394 type = NVP_TYPE(nvp);
2397 * Verify type and nelem and get the value size.
2398 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2399 * is the size of the string(s) excluded.
2401 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2404 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2408 case DATA_TYPE_NVLIST:
2409 ret = nvpair_native_embedded(nvs, nvp);
2411 case DATA_TYPE_NVLIST_ARRAY:
2412 ret = nvpair_native_embedded_array(nvs, nvp);
2414 case DATA_TYPE_STRING_ARRAY:
2415 nvpair_native_string_array(nvs, nvp);
2425 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2427 uint64_t nvp_sz = nvp->nvp_size;
2429 switch (NVP_TYPE(nvp)) {
2430 case DATA_TYPE_NVLIST: {
2433 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2439 case DATA_TYPE_NVLIST_ARRAY: {
2442 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2452 if (nvp_sz > INT32_MAX)
2461 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2463 switch (nvs->nvs_op) {
2465 return (nvs_native_nvp_op(nvs, nvp));
2467 case NVS_OP_DECODE: {
2468 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2471 /* try to read the size value from the stream */
2472 if (native->n_curr + sizeof (int32_t) > native->n_end)
2474 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2476 /* sanity check the size value */
2477 if (decode_len < 0 ||
2478 decode_len > native->n_end - native->n_curr)
2484 * If at the end of the stream then move the cursor
2485 * forward, otherwise nvpair_native_op() will read
2486 * the entire nvpair at the same cursor position.
2489 native->n_curr += sizeof (int32_t);
2500 static const nvs_ops_t nvs_native_ops = {
2504 nvs_native_nvp_size,
2509 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2511 nvs_native_t native;
2514 nvs->nvs_ops = &nvs_native_ops;
2516 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2517 *buflen - sizeof (nvs_header_t))) != 0)
2520 err = nvs_operation(nvs, nvl, buflen);
2522 nvs_native_destroy(nvs);
2528 * XDR encoding functions
2530 * An xdr packed nvlist is encoded as:
2532 * - encoding methode and host endian (4 bytes)
2533 * - nvl_version (4 bytes)
2534 * - nvl_nvflag (4 bytes)
2536 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2537 * - encoded size of the nvpair (4 bytes)
2538 * - decoded size of the nvpair (4 bytes)
2539 * - name string, (4 + sizeof(NV_ALIGN4(string))
2540 * a string is coded as size (4 bytes) and data
2541 * - data type (4 bytes)
2542 * - number of elements in the nvpair (4 bytes)
2545 * - 2 zero's for end of the entire list (8 bytes)
2548 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2550 /* xdr data must be 4 byte aligned */
2551 if ((ulong_t)buf % 4 != 0)
2554 switch (nvs->nvs_op) {
2556 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2557 nvs->nvs_private = xdr;
2560 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2561 nvs->nvs_private = xdr;
2563 case NVS_OP_GETSIZE:
2564 nvs->nvs_private = NULL;
2572 nvs_xdr_destroy(nvstream_t *nvs)
2574 switch (nvs->nvs_op) {
2577 xdr_destroy((XDR *)nvs->nvs_private);
2585 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2587 switch (nvs->nvs_op) {
2589 case NVS_OP_DECODE: {
2590 XDR *xdr = nvs->nvs_private;
2592 if (!xdr_int(xdr, &nvl->nvl_version) ||
2593 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2597 case NVS_OP_GETSIZE: {
2599 * 2 * 4 for nvl_version + nvl_nvflag
2600 * and 8 for end of the entire list
2612 nvs_xdr_nvl_fini(nvstream_t *nvs)
2614 if (nvs->nvs_op == NVS_OP_ENCODE) {
2615 XDR *xdr = nvs->nvs_private;
2618 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2626 * The format of xdr encoded nvpair is:
2627 * encode_size, decode_size, name string, data type, nelem, data
2630 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2634 char *buf_end = (char *)nvp + nvp->nvp_size;
2636 uint_t nelem, buflen;
2638 XDR *xdr = nvs->nvs_private;
2640 ASSERT(xdr != NULL && nvp != NULL);
2643 if ((buf = NVP_NAME(nvp)) >= buf_end)
2645 buflen = buf_end - buf;
2647 if (!xdr_string(xdr, &buf, buflen - 1))
2649 nvp->nvp_name_sz = strlen(buf) + 1;
2651 /* type and nelem */
2652 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2653 !xdr_int(xdr, &nvp->nvp_value_elem))
2656 type = NVP_TYPE(nvp);
2657 nelem = nvp->nvp_value_elem;
2660 * Verify type and nelem and get the value size.
2661 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2662 * is the size of the string(s) excluded.
2664 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2667 /* if there is no data to extract then return */
2672 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2674 buflen = buf_end - buf;
2676 if (buflen < value_sz)
2680 case DATA_TYPE_NVLIST:
2681 if (nvs_embedded(nvs, (void *)buf) == 0)
2685 case DATA_TYPE_NVLIST_ARRAY:
2686 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2690 case DATA_TYPE_BOOLEAN:
2694 case DATA_TYPE_BYTE:
2695 case DATA_TYPE_INT8:
2696 case DATA_TYPE_UINT8:
2697 ret = xdr_char(xdr, buf);
2700 case DATA_TYPE_INT16:
2701 ret = xdr_short(xdr, (void *)buf);
2704 case DATA_TYPE_UINT16:
2705 ret = xdr_u_short(xdr, (void *)buf);
2708 case DATA_TYPE_BOOLEAN_VALUE:
2709 case DATA_TYPE_INT32:
2710 ret = xdr_int(xdr, (void *)buf);
2713 case DATA_TYPE_UINT32:
2714 ret = xdr_u_int(xdr, (void *)buf);
2717 case DATA_TYPE_INT64:
2718 ret = xdr_longlong_t(xdr, (void *)buf);
2721 case DATA_TYPE_UINT64:
2722 ret = xdr_u_longlong_t(xdr, (void *)buf);
2725 case DATA_TYPE_HRTIME:
2727 * NOTE: must expose the definition of hrtime_t here
2729 ret = xdr_longlong_t(xdr, (void *)buf);
2732 case DATA_TYPE_STRING:
2733 ret = xdr_string(xdr, &buf, buflen - 1);
2736 case DATA_TYPE_BYTE_ARRAY:
2737 ret = xdr_opaque(xdr, buf, nelem);
2740 case DATA_TYPE_INT8_ARRAY:
2741 case DATA_TYPE_UINT8_ARRAY:
2742 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
2743 (xdrproc_t)xdr_char);
2746 case DATA_TYPE_INT16_ARRAY:
2747 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
2748 sizeof (int16_t), (xdrproc_t)xdr_short);
2751 case DATA_TYPE_UINT16_ARRAY:
2752 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
2753 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
2756 case DATA_TYPE_BOOLEAN_ARRAY:
2757 case DATA_TYPE_INT32_ARRAY:
2758 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
2759 sizeof (int32_t), (xdrproc_t)xdr_int);
2762 case DATA_TYPE_UINT32_ARRAY:
2763 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
2764 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
2767 case DATA_TYPE_INT64_ARRAY:
2768 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
2769 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
2772 case DATA_TYPE_UINT64_ARRAY:
2773 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
2774 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
2777 case DATA_TYPE_STRING_ARRAY: {
2778 size_t len = nelem * sizeof (uint64_t);
2779 char **strp = (void *)buf;
2782 if (nvs->nvs_op == NVS_OP_DECODE)
2783 bzero(buf, len); /* don't trust packed data */
2785 for (i = 0; i < nelem; i++) {
2792 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
2795 if (nvs->nvs_op == NVS_OP_DECODE)
2797 len = strlen(buf) + 1;
2806 return (ret == TRUE ? 0 : EFAULT);
2810 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2812 data_type_t type = NVP_TYPE(nvp);
2814 * encode_size + decode_size + name string size + data type + nelem
2815 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
2817 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
2820 case DATA_TYPE_BOOLEAN:
2823 case DATA_TYPE_BOOLEAN_VALUE:
2824 case DATA_TYPE_BYTE:
2825 case DATA_TYPE_INT8:
2826 case DATA_TYPE_UINT8:
2827 case DATA_TYPE_INT16:
2828 case DATA_TYPE_UINT16:
2829 case DATA_TYPE_INT32:
2830 case DATA_TYPE_UINT32:
2831 nvp_sz += 4; /* 4 is the minimum xdr unit */
2834 case DATA_TYPE_INT64:
2835 case DATA_TYPE_UINT64:
2836 case DATA_TYPE_HRTIME:
2840 case DATA_TYPE_STRING:
2841 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
2844 case DATA_TYPE_BYTE_ARRAY:
2845 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
2848 case DATA_TYPE_BOOLEAN_ARRAY:
2849 case DATA_TYPE_INT8_ARRAY:
2850 case DATA_TYPE_UINT8_ARRAY:
2851 case DATA_TYPE_INT16_ARRAY:
2852 case DATA_TYPE_UINT16_ARRAY:
2853 case DATA_TYPE_INT32_ARRAY:
2854 case DATA_TYPE_UINT32_ARRAY:
2855 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
2858 case DATA_TYPE_INT64_ARRAY:
2859 case DATA_TYPE_UINT64_ARRAY:
2860 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
2863 case DATA_TYPE_STRING_ARRAY: {
2865 char **strs = (void *)NVP_VALUE(nvp);
2867 for (i = 0; i < NVP_NELEM(nvp); i++)
2868 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
2873 case DATA_TYPE_NVLIST:
2874 case DATA_TYPE_NVLIST_ARRAY: {
2876 int old_nvs_op = nvs->nvs_op;
2879 nvs->nvs_op = NVS_OP_GETSIZE;
2880 if (type == DATA_TYPE_NVLIST)
2881 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
2883 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
2884 nvs->nvs_op = old_nvs_op;
2897 if (nvp_sz > INT32_MAX)
2907 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
2908 * the largest nvpair that could be encoded in the buffer.
2910 * See comments above nvpair_xdr_op() for the format of xdr encoding.
2911 * The size of a xdr packed nvpair without any data is 5 words.
2913 * Using the size of the data directly as an estimate would be ok
2914 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
2915 * then the actual nvpair has space for an array of pointers to index
2916 * the strings. These pointers are not encoded into the packed xdr buffer.
2918 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
2919 * of length 0, then each string is endcoded in xdr format as a single word.
2920 * Therefore when expanded to an nvpair there will be 2.25 word used for
2921 * each string. (a int64_t allocated for pointer usage, and a single char
2922 * for the null termination.)
2924 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
2926 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
2927 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
2928 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
2929 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
2930 (NVS_XDR_DATA_LEN(x) * 2) + \
2931 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
2934 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2936 XDR *xdr = nvs->nvs_private;
2937 int32_t encode_len, decode_len;
2939 switch (nvs->nvs_op) {
2940 case NVS_OP_ENCODE: {
2943 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
2946 decode_len = nvp->nvp_size;
2947 encode_len = nvsize;
2948 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
2951 return (nvs_xdr_nvp_op(nvs, nvp));
2953 case NVS_OP_DECODE: {
2954 struct xdr_bytesrec bytesrec;
2956 /* get the encode and decode size */
2957 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
2961 /* are we at the end of the stream? */
2965 /* sanity check the size parameter */
2966 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
2969 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
2980 static const struct nvs_ops nvs_xdr_ops = {
2989 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2994 nvs->nvs_ops = &nvs_xdr_ops;
2996 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
2997 *buflen - sizeof (nvs_header_t))) != 0)
3000 err = nvs_operation(nvs, nvl, buflen);
3002 nvs_xdr_destroy(nvs);