4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015, 2017 by Delphix. All rights reserved.
27 #include <sys/debug.h>
28 #include <sys/nvpair.h>
29 #include <sys/nvpair_impl.h>
30 #include <rpc/types.h>
33 #if defined(_KERNEL) && !defined(_BOOT)
34 #include <sys/varargs.h>
35 #include <sys/sunddi.h>
44 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
46 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
48 #if defined(__FreeBSD__) && !defined(_KERNEL)
50 * libnvpair is the lowest commen denominator for ZFS related libraries,
51 * defining aok here makes it usable by all ZFS related libraries
57 * nvpair.c - Provides kernel & userland interfaces for manipulating
72 * +--------------+ last i_nvp in list
73 * | nvpriv_t | +--------------------->
75 * +--+- nvp_list | | +------------+
76 * | | nvp_last -+--+ + nv_alloc_t |
77 * | | nvp_curr | |------------|
78 * | | nvp_nva -+----> | nva_ops |
79 * | | nvp_stat | | nva_arg |
80 * | +--------------+ +------------+
84 * +---------------------+ +-------------------+
85 * | i_nvp_t | +-->| i_nvp_t | +-->
86 * |---------------------| | |-------------------| |
87 * | nvi_next -+--+ | nvi_next -+--+
88 * | nvi_prev (NULL) | <----+ nvi_prev |
89 * | . . . . . . . . . . | | . . . . . . . . . |
90 * | nvp (nvpair_t) | | nvp (nvpair_t) |
91 * | - nvp_size | | - nvp_size |
92 * | - nvp_name_sz | | - nvp_name_sz |
93 * | - nvp_value_elem | | - nvp_value_elem |
94 * | - nvp_type | | - nvp_type |
95 * | - data ... | | - data ... |
96 * +---------------------+ +-------------------+
100 * +---------------------+ +---------------------+
101 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
102 * |---------------------| | | |---------------------|
103 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
104 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
105 * | . . . . . . . . . | | . . . . . . . . . |
106 * | nvp (nvpair_t) | | nvp (nvpair_t) |
107 * | - nvp_size | | - nvp_size |
108 * | - nvp_name_sz | | - nvp_name_sz |
109 * | - nvp_value_elem | | - nvp_value_elem |
110 * | - DATA_TYPE_NVLIST | | - nvp_type |
111 * | - data (embedded) | | - data ... |
112 * | nvlist name | +---------------------+
113 * | +--------------+ |
115 * | |--------------| |
116 * | | nvl_version | |
118 * | | nvl_priv --+---+---->
121 * | +--------------+ |
122 * +---------------------+
125 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
126 * allow value to be aligned on 8 byte boundary
128 * name_len is the length of the name string including the null terminator
131 #define NVP_SIZE_CALC(name_len, data_len) \
132 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
134 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
135 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
136 uint_t nelem, const void *data);
138 #define NV_STAT_EMBEDDED 0x1
139 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
140 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
142 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
143 #define NVPAIR2I_NVP(nvp) \
144 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
147 int nvpair_max_recursion = 20;
149 int nvpair_max_recursion = 100;
153 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
161 va_start(valist, nvo);
162 if (nva->nva_ops->nv_ao_init != NULL)
163 err = nva->nva_ops->nv_ao_init(nva, valist);
170 nv_alloc_reset(nv_alloc_t *nva)
172 if (nva->nva_ops->nv_ao_reset != NULL)
173 nva->nva_ops->nv_ao_reset(nva);
177 nv_alloc_fini(nv_alloc_t *nva)
179 if (nva->nva_ops->nv_ao_fini != NULL)
180 nva->nva_ops->nv_ao_fini(nva);
184 nvlist_lookup_nv_alloc(nvlist_t *nvl)
189 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
192 return (priv->nvp_nva);
196 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
198 nv_alloc_t *nva = nvp->nvp_nva;
201 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
208 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
210 nv_alloc_t *nva = nvp->nvp_nva;
212 nva->nva_ops->nv_ao_free(nva, buf, size);
216 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
218 bzero(priv, sizeof (nvpriv_t));
221 priv->nvp_stat = stat;
225 nv_priv_alloc(nv_alloc_t *nva)
230 * nv_mem_alloc() cannot called here because it needs the priv
233 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
236 nv_priv_init(priv, nva, 0);
242 * Embedded lists need their own nvpriv_t's. We create a new
243 * nvpriv_t using the parameters and allocator from the parent
247 nv_priv_alloc_embedded(nvpriv_t *priv)
251 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
254 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
260 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
262 nvl->nvl_version = NV_VERSION;
263 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
264 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
270 nvlist_nvflag(nvlist_t *nvl)
272 return (nvl->nvl_nvflag);
276 * nvlist_alloc - Allocate nvlist.
280 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
282 #if defined(_KERNEL) && !defined(_BOOT)
283 return (nvlist_xalloc(nvlp, nvflag,
284 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
286 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
291 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
295 if (nvlp == NULL || nva == NULL)
298 if ((priv = nv_priv_alloc(nva)) == NULL)
301 if ((*nvlp = nv_mem_zalloc(priv,
302 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
303 nv_mem_free(priv, priv, sizeof (nvpriv_t));
307 nvlist_init(*nvlp, nvflag, priv);
313 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
316 nvp_buf_alloc(nvlist_t *nvl, size_t len)
318 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
324 * Allocate the buffer
326 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
328 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
338 * nvp_buf_free - de-Allocate an i_nvp_t.
341 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
343 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
344 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
346 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
350 * nvp_buf_link - link a new nv pair into the nvlist.
353 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
355 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
356 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
358 /* Put element at end of nvlist */
359 if (priv->nvp_list == NULL) {
360 priv->nvp_list = priv->nvp_last = curr;
362 curr->nvi_prev = priv->nvp_last;
363 priv->nvp_last->nvi_next = curr;
364 priv->nvp_last = curr;
369 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
372 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
374 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
375 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
378 * protect nvlist_next_nvpair() against walking on freed memory.
380 if (priv->nvp_curr == curr)
381 priv->nvp_curr = curr->nvi_next;
383 if (curr == priv->nvp_list)
384 priv->nvp_list = curr->nvi_next;
386 curr->nvi_prev->nvi_next = curr->nvi_next;
388 if (curr == priv->nvp_last)
389 priv->nvp_last = curr->nvi_prev;
391 curr->nvi_next->nvi_prev = curr->nvi_prev;
395 * take a nvpair type and number of elements and make sure the are valid
398 i_validate_type_nelem(data_type_t type, uint_t nelem)
401 case DATA_TYPE_BOOLEAN:
405 case DATA_TYPE_BOOLEAN_VALUE:
408 case DATA_TYPE_UINT8:
409 case DATA_TYPE_INT16:
410 case DATA_TYPE_UINT16:
411 case DATA_TYPE_INT32:
412 case DATA_TYPE_UINT32:
413 case DATA_TYPE_INT64:
414 case DATA_TYPE_UINT64:
415 case DATA_TYPE_STRING:
416 case DATA_TYPE_HRTIME:
417 case DATA_TYPE_NVLIST:
418 #if !defined(_KERNEL)
419 case DATA_TYPE_DOUBLE:
424 case DATA_TYPE_BOOLEAN_ARRAY:
425 case DATA_TYPE_BYTE_ARRAY:
426 case DATA_TYPE_INT8_ARRAY:
427 case DATA_TYPE_UINT8_ARRAY:
428 case DATA_TYPE_INT16_ARRAY:
429 case DATA_TYPE_UINT16_ARRAY:
430 case DATA_TYPE_INT32_ARRAY:
431 case DATA_TYPE_UINT32_ARRAY:
432 case DATA_TYPE_INT64_ARRAY:
433 case DATA_TYPE_UINT64_ARRAY:
434 case DATA_TYPE_STRING_ARRAY:
435 case DATA_TYPE_NVLIST_ARRAY:
436 /* we allow arrays with 0 elements */
445 * Verify nvp_name_sz and check the name string length.
448 i_validate_nvpair_name(nvpair_t *nvp)
450 if ((nvp->nvp_name_sz <= 0) ||
451 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
454 /* verify the name string, make sure its terminated */
455 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
458 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
462 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
465 case DATA_TYPE_BOOLEAN_VALUE:
466 if (*(boolean_t *)data != B_TRUE &&
467 *(boolean_t *)data != B_FALSE)
470 case DATA_TYPE_BOOLEAN_ARRAY: {
473 for (i = 0; i < nelem; i++)
474 if (((boolean_t *)data)[i] != B_TRUE &&
475 ((boolean_t *)data)[i] != B_FALSE)
487 * This function takes a pointer to what should be a nvpair and it's size
488 * and then verifies that all the nvpair fields make sense and can be
489 * trusted. This function is used when decoding packed nvpairs.
492 i_validate_nvpair(nvpair_t *nvp)
494 data_type_t type = NVP_TYPE(nvp);
497 /* verify nvp_name_sz, check the name string length */
498 if (i_validate_nvpair_name(nvp) != 0)
501 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
505 * verify nvp_type, nvp_value_elem, and also possibly
506 * verify string values and get the value size.
508 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
509 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
510 if (size2 < 0 || size1 != NV_ALIGN(size2))
517 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
522 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
525 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
526 nvpair_t *nvp = &curr->nvi_nvp;
529 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
530 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
538 * Frees all memory allocated for an nvpair (like embedded lists) with
539 * the exception of the nvpair buffer itself.
542 nvpair_free(nvpair_t *nvp)
544 switch (NVP_TYPE(nvp)) {
545 case DATA_TYPE_NVLIST:
546 nvlist_free(EMBEDDED_NVL(nvp));
548 case DATA_TYPE_NVLIST_ARRAY: {
549 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
552 for (i = 0; i < NVP_NELEM(nvp); i++)
553 nvlist_free(nvlp[i]);
562 * nvlist_free - free an unpacked nvlist
565 nvlist_free(nvlist_t *nvl)
571 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
575 * Unpacked nvlist are linked through i_nvp_t
577 curr = priv->nvp_list;
578 while (curr != NULL) {
579 nvpair_t *nvp = &curr->nvi_nvp;
580 curr = curr->nvi_next;
583 nvp_buf_free(nvl, nvp);
586 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
587 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
591 nv_mem_free(priv, priv, sizeof (nvpriv_t));
595 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
597 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
603 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
604 if (&curr->nvi_nvp == nvp)
611 * Make a copy of nvlist
615 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
617 #if defined(_KERNEL) && !defined(_BOOT)
618 return (nvlist_xdup(nvl, nvlp,
619 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
621 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
626 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
631 if (nvl == NULL || nvlp == NULL)
634 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
637 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
646 * Remove all with matching name
649 nvlist_remove_all(nvlist_t *nvl, const char *name)
655 if (nvl == NULL || name == NULL ||
656 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
659 curr = priv->nvp_list;
660 while (curr != NULL) {
661 nvpair_t *nvp = &curr->nvi_nvp;
663 curr = curr->nvi_next;
664 if (strcmp(name, NVP_NAME(nvp)) != 0)
667 nvp_buf_unlink(nvl, nvp);
669 nvp_buf_free(nvl, nvp);
678 * Remove first one with matching name and type
681 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
686 if (nvl == NULL || name == NULL ||
687 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
690 curr = priv->nvp_list;
691 while (curr != NULL) {
692 nvpair_t *nvp = &curr->nvi_nvp;
694 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
695 nvp_buf_unlink(nvl, nvp);
697 nvp_buf_free(nvl, nvp);
701 curr = curr->nvi_next;
708 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
710 if (nvl == NULL || nvp == NULL)
713 nvp_buf_unlink(nvl, nvp);
715 nvp_buf_free(nvl, nvp);
720 * This function calculates the size of an nvpair value.
722 * The data argument controls the behavior in case of the data types
723 * DATA_TYPE_STRING and
724 * DATA_TYPE_STRING_ARRAY
725 * Is data == NULL then the size of the string(s) is excluded.
728 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
732 if (i_validate_type_nelem(type, nelem) != 0)
735 /* Calculate required size for holding value */
737 case DATA_TYPE_BOOLEAN:
740 case DATA_TYPE_BOOLEAN_VALUE:
741 value_sz = sizeof (boolean_t);
744 value_sz = sizeof (uchar_t);
747 value_sz = sizeof (int8_t);
749 case DATA_TYPE_UINT8:
750 value_sz = sizeof (uint8_t);
752 case DATA_TYPE_INT16:
753 value_sz = sizeof (int16_t);
755 case DATA_TYPE_UINT16:
756 value_sz = sizeof (uint16_t);
758 case DATA_TYPE_INT32:
759 value_sz = sizeof (int32_t);
761 case DATA_TYPE_UINT32:
762 value_sz = sizeof (uint32_t);
764 case DATA_TYPE_INT64:
765 value_sz = sizeof (int64_t);
767 case DATA_TYPE_UINT64:
768 value_sz = sizeof (uint64_t);
770 #if !defined(_KERNEL)
771 case DATA_TYPE_DOUBLE:
772 value_sz = sizeof (double);
775 case DATA_TYPE_STRING:
779 value_sz = strlen(data) + 1;
781 case DATA_TYPE_BOOLEAN_ARRAY:
782 value_sz = (uint64_t)nelem * sizeof (boolean_t);
784 case DATA_TYPE_BYTE_ARRAY:
785 value_sz = (uint64_t)nelem * sizeof (uchar_t);
787 case DATA_TYPE_INT8_ARRAY:
788 value_sz = (uint64_t)nelem * sizeof (int8_t);
790 case DATA_TYPE_UINT8_ARRAY:
791 value_sz = (uint64_t)nelem * sizeof (uint8_t);
793 case DATA_TYPE_INT16_ARRAY:
794 value_sz = (uint64_t)nelem * sizeof (int16_t);
796 case DATA_TYPE_UINT16_ARRAY:
797 value_sz = (uint64_t)nelem * sizeof (uint16_t);
799 case DATA_TYPE_INT32_ARRAY:
800 value_sz = (uint64_t)nelem * sizeof (int32_t);
802 case DATA_TYPE_UINT32_ARRAY:
803 value_sz = (uint64_t)nelem * sizeof (uint32_t);
805 case DATA_TYPE_INT64_ARRAY:
806 value_sz = (uint64_t)nelem * sizeof (int64_t);
808 case DATA_TYPE_UINT64_ARRAY:
809 value_sz = (uint64_t)nelem * sizeof (uint64_t);
811 case DATA_TYPE_STRING_ARRAY:
812 value_sz = (uint64_t)nelem * sizeof (uint64_t);
815 char *const *strs = data;
818 /* no alignment requirement for strings */
819 for (i = 0; i < nelem; i++) {
822 value_sz += strlen(strs[i]) + 1;
826 case DATA_TYPE_HRTIME:
827 value_sz = sizeof (hrtime_t);
829 case DATA_TYPE_NVLIST:
830 value_sz = NV_ALIGN(sizeof (nvlist_t));
832 case DATA_TYPE_NVLIST_ARRAY:
833 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
834 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
840 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
844 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
849 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
850 nvl->nvl_priv)) == NULL)
853 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
855 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
856 nvlist_free(emb_nvl);
857 emb_nvl->nvl_priv = 0;
864 * nvlist_add_common - Add new <name,value> pair to nvlist
867 nvlist_add_common(nvlist_t *nvl, const char *name,
868 data_type_t type, uint_t nelem, const void *data)
873 int nvp_sz, name_sz, value_sz;
876 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
879 if (nelem != 0 && data == NULL)
883 * Verify type and nelem and get the value size.
884 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
885 * is the size of the string(s) included.
887 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
890 if (i_validate_nvpair_value(type, nelem, data) != 0)
894 * If we're adding an nvlist or nvlist array, ensure that we are not
895 * adding the input nvlist to itself, which would cause recursion,
896 * and ensure that no NULL nvlist pointers are present.
899 case DATA_TYPE_NVLIST:
900 if (data == nvl || data == NULL)
903 case DATA_TYPE_NVLIST_ARRAY: {
904 nvlist_t **onvlp = (nvlist_t **)data;
905 for (i = 0; i < nelem; i++) {
906 if (onvlp[i] == nvl || onvlp[i] == NULL)
915 /* calculate sizes of the nvpair elements and the nvpair itself */
916 name_sz = strlen(name) + 1;
917 if (name_sz >= 1ULL << (sizeof (nvp->nvp_name_sz) * 8 - 1))
920 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
922 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
925 ASSERT(nvp->nvp_size == nvp_sz);
926 nvp->nvp_name_sz = name_sz;
927 nvp->nvp_value_elem = nelem;
928 nvp->nvp_type = type;
929 bcopy(name, NVP_NAME(nvp), name_sz);
932 case DATA_TYPE_BOOLEAN:
934 case DATA_TYPE_STRING_ARRAY: {
935 char *const *strs = data;
936 char *buf = NVP_VALUE(nvp);
937 char **cstrs = (void *)buf;
939 /* skip pre-allocated space for pointer array */
940 buf += nelem * sizeof (uint64_t);
941 for (i = 0; i < nelem; i++) {
942 int slen = strlen(strs[i]) + 1;
943 bcopy(strs[i], buf, slen);
949 case DATA_TYPE_NVLIST: {
950 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
951 nvlist_t *onvl = (nvlist_t *)data;
953 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
954 nvp_buf_free(nvl, nvp);
959 case DATA_TYPE_NVLIST_ARRAY: {
960 nvlist_t **onvlp = (nvlist_t **)data;
961 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
962 nvlist_t *embedded = (nvlist_t *)
963 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
965 for (i = 0; i < nelem; i++) {
966 if ((err = nvlist_copy_embedded(nvl,
967 onvlp[i], embedded)) != 0) {
969 * Free any successfully created lists
972 nvp_buf_free(nvl, nvp);
976 nvlp[i] = embedded++;
981 bcopy(data, NVP_VALUE(nvp), value_sz);
984 /* if unique name, remove before add */
985 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
986 (void) nvlist_remove_all(nvl, name);
987 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
988 (void) nvlist_remove(nvl, name, type);
990 nvp_buf_link(nvl, nvp);
996 nvlist_add_boolean(nvlist_t *nvl, const char *name)
998 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
1002 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
1004 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
1008 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1010 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1014 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1016 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1020 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1022 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1026 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1028 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1032 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1034 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1038 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1040 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1044 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1046 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1050 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1052 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1056 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1058 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1061 #if !defined(_KERNEL)
1063 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1065 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1070 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1072 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1076 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1077 boolean_t *a, uint_t n)
1079 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1083 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1085 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1089 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1091 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1095 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1097 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1101 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1103 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1107 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1109 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1113 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1115 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1119 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1121 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1125 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1127 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1131 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1133 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1137 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1138 char *const *a, uint_t n)
1140 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1144 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1146 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1150 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1152 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1156 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1158 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1161 /* reading name-value pairs */
1163 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1169 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1172 curr = NVPAIR2I_NVP(nvp);
1175 * Ensure that nvp is a valid nvpair on this nvlist.
1176 * NB: nvp_curr is used only as a hint so that we don't always
1177 * have to walk the list to determine if nvp is still on the list.
1180 curr = priv->nvp_list;
1181 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1182 curr = curr->nvi_next;
1186 priv->nvp_curr = curr;
1188 return (curr != NULL ? &curr->nvi_nvp : NULL);
1192 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1198 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1201 curr = NVPAIR2I_NVP(nvp);
1204 curr = priv->nvp_last;
1205 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1206 curr = curr->nvi_prev;
1210 priv->nvp_curr = curr;
1212 return (curr != NULL ? &curr->nvi_nvp : NULL);
1216 nvlist_empty(nvlist_t *nvl)
1221 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1224 return (priv->nvp_list == NULL);
1228 nvpair_name(nvpair_t *nvp)
1230 return (NVP_NAME(nvp));
1234 nvpair_type(nvpair_t *nvp)
1236 return (NVP_TYPE(nvp));
1240 nvpair_type_is_array(nvpair_t *nvp)
1242 data_type_t type = NVP_TYPE(nvp);
1244 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1245 (type == DATA_TYPE_INT8_ARRAY) ||
1246 (type == DATA_TYPE_UINT8_ARRAY) ||
1247 (type == DATA_TYPE_INT16_ARRAY) ||
1248 (type == DATA_TYPE_UINT16_ARRAY) ||
1249 (type == DATA_TYPE_INT32_ARRAY) ||
1250 (type == DATA_TYPE_UINT32_ARRAY) ||
1251 (type == DATA_TYPE_INT64_ARRAY) ||
1252 (type == DATA_TYPE_UINT64_ARRAY) ||
1253 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1254 (type == DATA_TYPE_STRING_ARRAY) ||
1255 (type == DATA_TYPE_NVLIST_ARRAY))
1262 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1264 if (nvp == NULL || nvpair_type(nvp) != type)
1268 * For non-array types, we copy the data.
1269 * For array types (including string), we set a pointer.
1272 case DATA_TYPE_BOOLEAN:
1277 case DATA_TYPE_BOOLEAN_VALUE:
1278 case DATA_TYPE_BYTE:
1279 case DATA_TYPE_INT8:
1280 case DATA_TYPE_UINT8:
1281 case DATA_TYPE_INT16:
1282 case DATA_TYPE_UINT16:
1283 case DATA_TYPE_INT32:
1284 case DATA_TYPE_UINT32:
1285 case DATA_TYPE_INT64:
1286 case DATA_TYPE_UINT64:
1287 case DATA_TYPE_HRTIME:
1288 #if !defined(_KERNEL)
1289 case DATA_TYPE_DOUBLE:
1293 bcopy(NVP_VALUE(nvp), data,
1294 (size_t)i_get_value_size(type, NULL, 1));
1299 case DATA_TYPE_NVLIST:
1300 case DATA_TYPE_STRING:
1303 *(void **)data = (void *)NVP_VALUE(nvp);
1308 case DATA_TYPE_BOOLEAN_ARRAY:
1309 case DATA_TYPE_BYTE_ARRAY:
1310 case DATA_TYPE_INT8_ARRAY:
1311 case DATA_TYPE_UINT8_ARRAY:
1312 case DATA_TYPE_INT16_ARRAY:
1313 case DATA_TYPE_UINT16_ARRAY:
1314 case DATA_TYPE_INT32_ARRAY:
1315 case DATA_TYPE_UINT32_ARRAY:
1316 case DATA_TYPE_INT64_ARRAY:
1317 case DATA_TYPE_UINT64_ARRAY:
1318 case DATA_TYPE_STRING_ARRAY:
1319 case DATA_TYPE_NVLIST_ARRAY:
1320 if (nelem == NULL || data == NULL)
1322 if ((*nelem = NVP_NELEM(nvp)) != 0)
1323 *(void **)data = (void *)NVP_VALUE(nvp);
1325 *(void **)data = NULL;
1336 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1337 uint_t *nelem, void *data)
1343 if (name == NULL || nvl == NULL ||
1344 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1347 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1350 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1351 nvp = &curr->nvi_nvp;
1353 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1354 return (nvpair_value_common(nvp, type, nelem, data));
1361 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1363 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1367 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1369 return (nvlist_lookup_common(nvl, name,
1370 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1374 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1376 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1380 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1382 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1386 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1388 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1392 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1394 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1398 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1400 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1404 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1406 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1410 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1412 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1416 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1418 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1422 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1424 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1427 #if !defined(_KERNEL)
1429 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1431 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1436 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1438 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1442 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1444 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1448 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1449 boolean_t **a, uint_t *n)
1451 return (nvlist_lookup_common(nvl, name,
1452 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1456 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1457 uchar_t **a, uint_t *n)
1459 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1463 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1465 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1469 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1470 uint8_t **a, uint_t *n)
1472 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1476 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1477 int16_t **a, uint_t *n)
1479 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1483 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1484 uint16_t **a, uint_t *n)
1486 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1490 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1491 int32_t **a, uint_t *n)
1493 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1497 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1498 uint32_t **a, uint_t *n)
1500 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1504 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1505 int64_t **a, uint_t *n)
1507 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1511 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1512 uint64_t **a, uint_t *n)
1514 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1518 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1519 char ***a, uint_t *n)
1521 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1525 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1526 nvlist_t ***a, uint_t *n)
1528 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1532 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1534 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1538 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1542 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1546 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1551 switch (type = va_arg(ap, data_type_t)) {
1552 case DATA_TYPE_BOOLEAN:
1553 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1556 case DATA_TYPE_BOOLEAN_VALUE:
1557 case DATA_TYPE_BYTE:
1558 case DATA_TYPE_INT8:
1559 case DATA_TYPE_UINT8:
1560 case DATA_TYPE_INT16:
1561 case DATA_TYPE_UINT16:
1562 case DATA_TYPE_INT32:
1563 case DATA_TYPE_UINT32:
1564 case DATA_TYPE_INT64:
1565 case DATA_TYPE_UINT64:
1566 case DATA_TYPE_HRTIME:
1567 case DATA_TYPE_STRING:
1568 case DATA_TYPE_NVLIST:
1569 #if !defined(_KERNEL)
1570 case DATA_TYPE_DOUBLE:
1572 val = va_arg(ap, void *);
1573 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1576 case DATA_TYPE_BYTE_ARRAY:
1577 case DATA_TYPE_BOOLEAN_ARRAY:
1578 case DATA_TYPE_INT8_ARRAY:
1579 case DATA_TYPE_UINT8_ARRAY:
1580 case DATA_TYPE_INT16_ARRAY:
1581 case DATA_TYPE_UINT16_ARRAY:
1582 case DATA_TYPE_INT32_ARRAY:
1583 case DATA_TYPE_UINT32_ARRAY:
1584 case DATA_TYPE_INT64_ARRAY:
1585 case DATA_TYPE_UINT64_ARRAY:
1586 case DATA_TYPE_STRING_ARRAY:
1587 case DATA_TYPE_NVLIST_ARRAY:
1588 val = va_arg(ap, void *);
1589 nelem = va_arg(ap, uint_t *);
1590 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1597 if (ret == ENOENT && noentok)
1606 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1607 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1608 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1609 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1610 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1611 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1612 * code also supports "a.d[3]e[1]" syntax).
1614 * If 'ip' is non-NULL and the last name component is an array, return the
1615 * value of the "...[index]" array index in *ip. For an array reference that
1616 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1617 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1618 * inside the 'name' string where the syntax error was detected.
1621 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1622 nvpair_t **ret, int *ip, char **ep)
1633 *ip = -1; /* not indexed */
1637 if ((nvl == NULL) || (name == NULL))
1642 /* step through components of name */
1643 for (np = name; np && *np; np = sepp) {
1644 /* ensure unique names */
1645 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1648 /* skip white space */
1649 skip_whitespace(np);
1653 /* set 'sepp' to end of current component 'np' */
1655 sepp = strchr(np, sep);
1659 /* find start of next "[ index ]..." */
1660 idxp = strchr(np, '[');
1662 /* if sepp comes first, set idxp to NULL */
1663 if (sepp && idxp && (sepp < idxp))
1667 * At this point 'idxp' is set if there is an index
1668 * expected for the current component.
1671 /* set 'n' to length of current 'np' name component */
1674 /* keep sepp up to date for *ep use as we advance */
1675 skip_whitespace(idxp);
1678 /* determine the index value */
1679 #if defined(_KERNEL) && !defined(_BOOT)
1680 if (ddi_strtol(idxp, &idxep, 0, &idx))
1683 idx = strtol(idxp, &idxep, 0);
1688 /* keep sepp up to date for *ep use as we advance */
1691 /* skip white space index value and check for ']' */
1692 skip_whitespace(sepp);
1696 /* for embedded arrays, support C syntax: "a[1].b" */
1697 skip_whitespace(sepp);
1698 if (sep && (*sepp == sep))
1706 /* trim trailing whitespace by reducing length of 'np' */
1709 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1713 /* skip whitespace, and set sepp to NULL if complete */
1715 skip_whitespace(sepp);
1722 * o 'n' is the length of current 'np' component.
1723 * o 'idxp' is set if there was an index, and value 'idx'.
1724 * o 'sepp' is set to the beginning of the next component,
1725 * and set to NULL if we have no more components.
1727 * Search for nvpair with matching component name.
1729 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1730 nvp = nvlist_next_nvpair(nvl, nvp)) {
1732 /* continue if no match on name */
1733 if (strncmp(np, nvpair_name(nvp), n) ||
1734 (strlen(nvpair_name(nvp)) != n))
1737 /* if indexed, verify type is array oriented */
1738 if (idxp && !nvpair_type_is_array(nvp))
1742 * Full match found, return nvp and idx if this
1743 * was the last component.
1749 *ip = (int)idx; /* return index */
1750 return (0); /* found */
1754 * More components: current match must be
1755 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1756 * to support going deeper.
1758 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1759 nvl = EMBEDDED_NVL(nvp);
1761 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1762 (void) nvpair_value_nvlist_array(nvp,
1763 &nva, (uint_t *)&n);
1764 if ((n < 0) || (idx >= n))
1770 /* type does not support more levels */
1774 goto fail; /* 'name' not found */
1776 /* search for match of next component in embedded 'nvl' list */
1779 fail: if (ep && sepp)
1785 * Return pointer to nvpair with specified 'name'.
1788 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1790 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1794 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1795 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1798 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1799 const char *name, nvpair_t **ret, int *ip, char **ep)
1801 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1805 nvlist_exists(nvlist_t *nvl, const char *name)
1811 if (name == NULL || nvl == NULL ||
1812 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1815 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1816 nvp = &curr->nvi_nvp;
1818 if (strcmp(name, NVP_NAME(nvp)) == 0)
1826 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1828 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1832 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1834 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1838 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1840 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1844 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1846 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1850 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1852 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1856 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1858 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1862 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1864 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1868 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1870 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1874 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1876 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1880 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1882 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1885 #if !defined(_KERNEL)
1887 nvpair_value_double(nvpair_t *nvp, double *val)
1889 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1894 nvpair_value_string(nvpair_t *nvp, char **val)
1896 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1900 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1902 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1906 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1908 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1912 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1914 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1918 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1920 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1924 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1926 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1930 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1932 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1936 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1938 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1942 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1944 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1948 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1950 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1954 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1956 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1960 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1962 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1966 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1968 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1972 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1974 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1978 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1980 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1984 * Add specified pair to the list.
1987 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1989 if (nvl == NULL || nvp == NULL)
1992 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1993 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1997 * Merge the supplied nvlists and put the result in dst.
1998 * The merged list will contain all names specified in both lists,
1999 * the values are taken from nvl in the case of duplicates.
2000 * Return 0 on success.
2004 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
2006 if (nvl == NULL || dst == NULL)
2010 return (nvlist_copy_pairs(nvl, dst));
2016 * Encoding related routines
2018 #define NVS_OP_ENCODE 0
2019 #define NVS_OP_DECODE 1
2020 #define NVS_OP_GETSIZE 2
2022 typedef struct nvs_ops nvs_ops_t;
2026 const nvs_ops_t *nvs_ops;
2033 * nvs operations are:
2035 * encoding / decoding of a nvlist header (nvlist_t)
2036 * calculates the size used for header and end detection
2039 * responsible for the first part of encoding / decoding of an nvpair
2040 * calculates the decoded size of an nvpair
2043 * second part of encoding / decoding of an nvpair
2046 * calculates the encoding size of an nvpair
2049 * encodes the end detection mark (zeros).
2052 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2053 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2054 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2055 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2056 int (*nvs_nvl_fini)(nvstream_t *);
2060 char nvh_encoding; /* nvs encoding method */
2061 char nvh_endian; /* nvs endian */
2062 char nvh_reserved1; /* reserved for future use */
2063 char nvh_reserved2; /* reserved for future use */
2067 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2069 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2073 * Walk nvpair in list and encode each nvpair
2075 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2076 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2079 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2083 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2090 * Get decoded size of next pair in stream, alloc
2091 * memory for nvpair_t, then decode the nvpair
2093 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2094 if (nvsize == 0) /* end of list */
2097 /* make sure len makes sense */
2098 if (nvsize < NVP_SIZE_CALC(1, 0))
2101 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2104 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2105 nvp_buf_free(nvl, nvp);
2109 if (i_validate_nvpair(nvp) != 0) {
2111 nvp_buf_free(nvl, nvp);
2115 nvp_buf_link(nvl, nvp);
2121 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2123 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2125 uint64_t nvsize = *buflen;
2129 * Get encoded size of nvpairs in nvlist
2131 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2132 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2135 if ((nvsize += size) > INT32_MAX)
2144 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2148 if (nvl->nvl_priv == 0)
2152 * Perform the operation, starting with header, then each nvpair
2154 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2157 switch (nvs->nvs_op) {
2159 err = nvs_encode_pairs(nvs, nvl);
2163 err = nvs_decode_pairs(nvs, nvl);
2166 case NVS_OP_GETSIZE:
2167 err = nvs_getsize_pairs(nvs, nvl, buflen);
2178 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2180 switch (nvs->nvs_op) {
2181 case NVS_OP_ENCODE: {
2184 if (nvs->nvs_recursion >= nvpair_max_recursion)
2186 nvs->nvs_recursion++;
2187 err = nvs_operation(nvs, embedded, NULL);
2188 nvs->nvs_recursion--;
2191 case NVS_OP_DECODE: {
2195 if (embedded->nvl_version != NV_VERSION)
2198 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2201 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2203 if (nvs->nvs_recursion >= nvpair_max_recursion) {
2204 nvlist_free(embedded);
2207 nvs->nvs_recursion++;
2208 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2209 nvlist_free(embedded);
2210 nvs->nvs_recursion--;
2221 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2223 size_t nelem = NVP_NELEM(nvp);
2224 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2227 switch (nvs->nvs_op) {
2229 for (i = 0; i < nelem; i++)
2230 if (nvs_embedded(nvs, nvlp[i]) != 0)
2234 case NVS_OP_DECODE: {
2235 size_t len = nelem * sizeof (uint64_t);
2236 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2238 bzero(nvlp, len); /* don't trust packed data */
2239 for (i = 0; i < nelem; i++) {
2240 if (nvs_embedded(nvs, embedded) != 0) {
2245 nvlp[i] = embedded++;
2249 case NVS_OP_GETSIZE: {
2250 uint64_t nvsize = 0;
2252 for (i = 0; i < nelem; i++) {
2255 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2258 if ((nvsize += nvp_sz) > INT32_MAX)
2272 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2273 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2276 * Common routine for nvlist operations:
2277 * encode, decode, getsize (encoded size).
2280 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2286 #if BYTE_ORDER == _LITTLE_ENDIAN
2287 int host_endian = 1;
2289 int host_endian = 0;
2290 #endif /* _LITTLE_ENDIAN */
2291 nvs_header_t *nvh = (void *)buf;
2293 if (buflen == NULL || nvl == NULL ||
2294 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2297 nvs.nvs_op = nvs_op;
2298 nvs.nvs_recursion = 0;
2301 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2302 * a buffer is allocated. The first 4 bytes in the buffer are
2303 * used for encoding method and host endian.
2307 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2310 nvh->nvh_encoding = encoding;
2311 nvh->nvh_endian = nvl_endian = host_endian;
2312 nvh->nvh_reserved1 = 0;
2313 nvh->nvh_reserved2 = 0;
2317 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2320 /* get method of encoding from first byte */
2321 encoding = nvh->nvh_encoding;
2322 nvl_endian = nvh->nvh_endian;
2325 case NVS_OP_GETSIZE:
2326 nvl_endian = host_endian;
2329 * add the size for encoding
2331 *buflen = sizeof (nvs_header_t);
2339 * Create an nvstream with proper encoding method
2342 case NV_ENCODE_NATIVE:
2344 * check endianness, in case we are unpacking
2347 if (nvl_endian != host_endian)
2349 err = nvs_native(&nvs, nvl, buf, buflen);
2352 err = nvs_xdr(&nvs, nvl, buf, buflen);
2363 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2365 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2369 * Pack nvlist into contiguous memory
2373 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2376 #if defined(_KERNEL) && !defined(_BOOT)
2377 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2378 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2380 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2385 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2393 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2397 return (nvlist_common(nvl, *bufp, buflen, encoding,
2401 * Here is a difficult situation:
2402 * 1. The nvlist has fixed allocator properties.
2403 * All other nvlist routines (like nvlist_add_*, ...) use
2405 * 2. When using nvlist_pack() the user can specify their own
2406 * allocator properties (e.g. by using KM_NOSLEEP).
2408 * We use the user specified properties (2). A clearer solution
2409 * will be to remove the kmflag from nvlist_pack(), but we will
2410 * not change the interface.
2412 nv_priv_init(&nvpriv, nva, 0);
2414 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2417 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2420 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2421 NVS_OP_ENCODE)) != 0) {
2422 nv_mem_free(&nvpriv, buf, alloc_size);
2424 *buflen = alloc_size;
2432 * Unpack buf into an nvlist_t
2436 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2438 #if defined(_KERNEL) && !defined(_BOOT)
2439 return (nvlist_xunpack(buf, buflen, nvlp,
2440 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2442 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2447 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2455 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2458 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2467 * Native encoding functions
2471 * This structure is used when decoding a packed nvpair in
2472 * the native format. n_base points to a buffer containing the
2473 * packed nvpair. n_end is a pointer to the end of the buffer.
2474 * (n_end actually points to the first byte past the end of the
2475 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2476 * It points to the current data that we are decoding.
2477 * The amount of data left in the buffer is equal to n_end - n_curr.
2478 * n_flag is used to recognize a packed embedded list.
2487 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2490 switch (nvs->nvs_op) {
2493 nvs->nvs_private = native;
2494 native->n_curr = native->n_base = buf;
2495 native->n_end = buf + buflen;
2499 case NVS_OP_GETSIZE:
2500 nvs->nvs_private = native;
2501 native->n_curr = native->n_base = native->n_end = NULL;
2511 nvs_native_destroy(nvstream_t *nvs)
2516 native_cp(nvstream_t *nvs, void *buf, size_t size)
2518 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2520 if (native->n_curr + size > native->n_end)
2524 * The bcopy() below eliminates alignment requirement
2525 * on the buffer (stream) and is preferred over direct access.
2527 switch (nvs->nvs_op) {
2529 bcopy(buf, native->n_curr, size);
2532 bcopy(native->n_curr, buf, size);
2538 native->n_curr += size;
2543 * operate on nvlist_t header
2546 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2548 nvs_native_t *native = nvs->nvs_private;
2550 switch (nvs->nvs_op) {
2554 return (0); /* packed embedded list */
2558 /* copy version and nvflag of the nvlist_t */
2559 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2560 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2565 case NVS_OP_GETSIZE:
2567 * if calculate for packed embedded list
2568 * 4 for end of the embedded list
2570 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2571 * and 4 for end of the entire list
2573 if (native->n_flag) {
2577 *size += 2 * sizeof (int32_t) + 4;
2588 nvs_native_nvl_fini(nvstream_t *nvs)
2590 if (nvs->nvs_op == NVS_OP_ENCODE) {
2591 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2593 * Add 4 zero bytes at end of nvlist. They are used
2594 * for end detection by the decode routine.
2596 if (native->n_curr + sizeof (int) > native->n_end)
2599 bzero(native->n_curr, sizeof (int));
2600 native->n_curr += sizeof (int);
2607 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2609 if (nvs->nvs_op == NVS_OP_ENCODE) {
2610 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2611 char *packed = (void *)
2612 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2614 * Null out the pointer that is meaningless in the packed
2615 * structure. The address may not be aligned, so we have
2618 bzero(packed + offsetof(nvlist_t, nvl_priv),
2619 sizeof(((nvlist_t *)NULL)->nvl_priv));
2622 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2626 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2628 if (nvs->nvs_op == NVS_OP_ENCODE) {
2629 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2630 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2631 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2634 * Null out pointers that are meaningless in the packed
2635 * structure. The addresses may not be aligned, so we have
2641 for (i = 0; i < NVP_NELEM(nvp); i++) {
2643 * Null out the pointer that is meaningless in the
2644 * packed structure. The address may not be aligned,
2645 * so we have to use bzero.
2647 bzero(value + offsetof(nvlist_t, nvl_priv),
2648 sizeof(((nvlist_t *)NULL)->nvl_priv));
2649 value += sizeof(nvlist_t);
2653 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2657 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2659 switch (nvs->nvs_op) {
2660 case NVS_OP_ENCODE: {
2661 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2662 uint64_t *strp = (void *)
2663 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2665 * Null out pointers that are meaningless in the packed
2666 * structure. The addresses may not be aligned, so we have
2669 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2672 case NVS_OP_DECODE: {
2673 char **strp = (void *)NVP_VALUE(nvp);
2674 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2677 for (i = 0; i < NVP_NELEM(nvp); i++) {
2679 buf += strlen(buf) + 1;
2687 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2694 * We do the initial bcopy of the data before we look at
2695 * the nvpair type, because when we're decoding, we won't
2696 * have the correct values for the pair until we do the bcopy.
2698 switch (nvs->nvs_op) {
2701 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2708 /* verify nvp_name_sz, check the name string length */
2709 if (i_validate_nvpair_name(nvp) != 0)
2712 type = NVP_TYPE(nvp);
2715 * Verify type and nelem and get the value size.
2716 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2717 * is the size of the string(s) excluded.
2719 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2722 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2726 case DATA_TYPE_NVLIST:
2727 ret = nvpair_native_embedded(nvs, nvp);
2729 case DATA_TYPE_NVLIST_ARRAY:
2730 ret = nvpair_native_embedded_array(nvs, nvp);
2732 case DATA_TYPE_STRING_ARRAY:
2733 nvpair_native_string_array(nvs, nvp);
2743 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2745 uint64_t nvp_sz = nvp->nvp_size;
2747 switch (NVP_TYPE(nvp)) {
2748 case DATA_TYPE_NVLIST: {
2751 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2757 case DATA_TYPE_NVLIST_ARRAY: {
2760 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2770 if (nvp_sz > INT32_MAX)
2779 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2781 switch (nvs->nvs_op) {
2783 return (nvs_native_nvp_op(nvs, nvp));
2785 case NVS_OP_DECODE: {
2786 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2789 /* try to read the size value from the stream */
2790 if (native->n_curr + sizeof (int32_t) > native->n_end)
2792 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2794 /* sanity check the size value */
2795 if (decode_len < 0 ||
2796 decode_len > native->n_end - native->n_curr)
2802 * If at the end of the stream then move the cursor
2803 * forward, otherwise nvpair_native_op() will read
2804 * the entire nvpair at the same cursor position.
2807 native->n_curr += sizeof (int32_t);
2818 static const nvs_ops_t nvs_native_ops = {
2822 nvs_native_nvp_size,
2827 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2829 nvs_native_t native;
2832 nvs->nvs_ops = &nvs_native_ops;
2834 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2835 *buflen - sizeof (nvs_header_t))) != 0)
2838 err = nvs_operation(nvs, nvl, buflen);
2840 nvs_native_destroy(nvs);
2846 * XDR encoding functions
2848 * An xdr packed nvlist is encoded as:
2850 * - encoding methode and host endian (4 bytes)
2851 * - nvl_version (4 bytes)
2852 * - nvl_nvflag (4 bytes)
2854 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2855 * - encoded size of the nvpair (4 bytes)
2856 * - decoded size of the nvpair (4 bytes)
2857 * - name string, (4 + sizeof(NV_ALIGN4(string))
2858 * a string is coded as size (4 bytes) and data
2859 * - data type (4 bytes)
2860 * - number of elements in the nvpair (4 bytes)
2863 * - 2 zero's for end of the entire list (8 bytes)
2866 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2868 /* xdr data must be 4 byte aligned */
2869 if ((ulong_t)buf % 4 != 0)
2872 switch (nvs->nvs_op) {
2874 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2875 nvs->nvs_private = xdr;
2878 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2879 nvs->nvs_private = xdr;
2881 case NVS_OP_GETSIZE:
2882 nvs->nvs_private = NULL;
2890 nvs_xdr_destroy(nvstream_t *nvs)
2892 switch (nvs->nvs_op) {
2895 xdr_destroy((XDR *)nvs->nvs_private);
2903 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2905 switch (nvs->nvs_op) {
2907 case NVS_OP_DECODE: {
2908 XDR *xdr = nvs->nvs_private;
2910 if (!xdr_int(xdr, &nvl->nvl_version) ||
2911 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2915 case NVS_OP_GETSIZE: {
2917 * 2 * 4 for nvl_version + nvl_nvflag
2918 * and 8 for end of the entire list
2930 nvs_xdr_nvl_fini(nvstream_t *nvs)
2932 if (nvs->nvs_op == NVS_OP_ENCODE) {
2933 XDR *xdr = nvs->nvs_private;
2936 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2944 * The format of xdr encoded nvpair is:
2945 * encode_size, decode_size, name string, data type, nelem, data
2948 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2952 char *buf_end = (char *)nvp + nvp->nvp_size;
2954 uint_t nelem, buflen;
2956 XDR *xdr = nvs->nvs_private;
2958 ASSERT(xdr != NULL && nvp != NULL);
2961 if ((buf = NVP_NAME(nvp)) >= buf_end)
2963 buflen = buf_end - buf;
2965 if (!xdr_string(xdr, &buf, buflen - 1))
2967 nvp->nvp_name_sz = strlen(buf) + 1;
2969 /* type and nelem */
2970 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2971 !xdr_int(xdr, &nvp->nvp_value_elem))
2974 type = NVP_TYPE(nvp);
2975 nelem = nvp->nvp_value_elem;
2978 * Verify type and nelem and get the value size.
2979 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2980 * is the size of the string(s) excluded.
2982 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2985 /* if there is no data to extract then return */
2990 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2992 buflen = buf_end - buf;
2994 if (buflen < value_sz)
2998 case DATA_TYPE_NVLIST:
2999 if (nvs_embedded(nvs, (void *)buf) == 0)
3003 case DATA_TYPE_NVLIST_ARRAY:
3004 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
3008 case DATA_TYPE_BOOLEAN:
3012 case DATA_TYPE_BYTE:
3013 case DATA_TYPE_INT8:
3014 case DATA_TYPE_UINT8:
3015 ret = xdr_char(xdr, buf);
3018 case DATA_TYPE_INT16:
3019 ret = xdr_short(xdr, (void *)buf);
3022 case DATA_TYPE_UINT16:
3023 ret = xdr_u_short(xdr, (void *)buf);
3026 case DATA_TYPE_BOOLEAN_VALUE:
3027 case DATA_TYPE_INT32:
3028 ret = xdr_int(xdr, (void *)buf);
3031 case DATA_TYPE_UINT32:
3032 ret = xdr_u_int(xdr, (void *)buf);
3035 case DATA_TYPE_INT64:
3036 ret = xdr_longlong_t(xdr, (void *)buf);
3039 case DATA_TYPE_UINT64:
3040 ret = xdr_u_longlong_t(xdr, (void *)buf);
3043 case DATA_TYPE_HRTIME:
3045 * NOTE: must expose the definition of hrtime_t here
3047 ret = xdr_longlong_t(xdr, (void *)buf);
3049 #if !defined(_KERNEL)
3050 case DATA_TYPE_DOUBLE:
3051 ret = xdr_double(xdr, (void *)buf);
3054 case DATA_TYPE_STRING:
3055 ret = xdr_string(xdr, &buf, buflen - 1);
3058 case DATA_TYPE_BYTE_ARRAY:
3059 ret = xdr_opaque(xdr, buf, nelem);
3062 case DATA_TYPE_INT8_ARRAY:
3063 case DATA_TYPE_UINT8_ARRAY:
3064 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3065 (xdrproc_t)xdr_char);
3068 case DATA_TYPE_INT16_ARRAY:
3069 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3070 sizeof (int16_t), (xdrproc_t)xdr_short);
3073 case DATA_TYPE_UINT16_ARRAY:
3074 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3075 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3078 case DATA_TYPE_BOOLEAN_ARRAY:
3079 case DATA_TYPE_INT32_ARRAY:
3080 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3081 sizeof (int32_t), (xdrproc_t)xdr_int);
3084 case DATA_TYPE_UINT32_ARRAY:
3085 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3086 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3089 case DATA_TYPE_INT64_ARRAY:
3090 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3091 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3094 case DATA_TYPE_UINT64_ARRAY:
3095 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3096 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3099 case DATA_TYPE_STRING_ARRAY: {
3100 size_t len = nelem * sizeof (uint64_t);
3101 char **strp = (void *)buf;
3104 if (nvs->nvs_op == NVS_OP_DECODE)
3105 bzero(buf, len); /* don't trust packed data */
3107 for (i = 0; i < nelem; i++) {
3114 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3117 if (nvs->nvs_op == NVS_OP_DECODE)
3119 len = strlen(buf) + 1;
3128 return (ret == TRUE ? 0 : EFAULT);
3132 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3134 data_type_t type = NVP_TYPE(nvp);
3136 * encode_size + decode_size + name string size + data type + nelem
3137 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3139 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3142 case DATA_TYPE_BOOLEAN:
3145 case DATA_TYPE_BOOLEAN_VALUE:
3146 case DATA_TYPE_BYTE:
3147 case DATA_TYPE_INT8:
3148 case DATA_TYPE_UINT8:
3149 case DATA_TYPE_INT16:
3150 case DATA_TYPE_UINT16:
3151 case DATA_TYPE_INT32:
3152 case DATA_TYPE_UINT32:
3153 nvp_sz += 4; /* 4 is the minimum xdr unit */
3156 case DATA_TYPE_INT64:
3157 case DATA_TYPE_UINT64:
3158 case DATA_TYPE_HRTIME:
3159 #if !defined(_KERNEL)
3160 case DATA_TYPE_DOUBLE:
3165 case DATA_TYPE_STRING:
3166 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3169 case DATA_TYPE_BYTE_ARRAY:
3170 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3173 case DATA_TYPE_BOOLEAN_ARRAY:
3174 case DATA_TYPE_INT8_ARRAY:
3175 case DATA_TYPE_UINT8_ARRAY:
3176 case DATA_TYPE_INT16_ARRAY:
3177 case DATA_TYPE_UINT16_ARRAY:
3178 case DATA_TYPE_INT32_ARRAY:
3179 case DATA_TYPE_UINT32_ARRAY:
3180 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3183 case DATA_TYPE_INT64_ARRAY:
3184 case DATA_TYPE_UINT64_ARRAY:
3185 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3188 case DATA_TYPE_STRING_ARRAY: {
3190 char **strs = (void *)NVP_VALUE(nvp);
3192 for (i = 0; i < NVP_NELEM(nvp); i++)
3193 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3198 case DATA_TYPE_NVLIST:
3199 case DATA_TYPE_NVLIST_ARRAY: {
3201 int old_nvs_op = nvs->nvs_op;
3204 nvs->nvs_op = NVS_OP_GETSIZE;
3205 if (type == DATA_TYPE_NVLIST)
3206 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3208 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3209 nvs->nvs_op = old_nvs_op;
3222 if (nvp_sz > INT32_MAX)
3232 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3233 * the largest nvpair that could be encoded in the buffer.
3235 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3236 * The size of a xdr packed nvpair without any data is 5 words.
3238 * Using the size of the data directly as an estimate would be ok
3239 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3240 * then the actual nvpair has space for an array of pointers to index
3241 * the strings. These pointers are not encoded into the packed xdr buffer.
3243 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3244 * of length 0, then each string is endcoded in xdr format as a single word.
3245 * Therefore when expanded to an nvpair there will be 2.25 word used for
3246 * each string. (a int64_t allocated for pointer usage, and a single char
3247 * for the null termination.)
3249 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3251 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3252 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3253 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3254 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3255 (NVS_XDR_DATA_LEN(x) * 2) + \
3256 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3259 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3261 XDR *xdr = nvs->nvs_private;
3262 int32_t encode_len, decode_len;
3264 switch (nvs->nvs_op) {
3265 case NVS_OP_ENCODE: {
3268 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3271 decode_len = nvp->nvp_size;
3272 encode_len = nvsize;
3273 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3276 return (nvs_xdr_nvp_op(nvs, nvp));
3278 case NVS_OP_DECODE: {
3279 struct xdr_bytesrec bytesrec;
3281 /* get the encode and decode size */
3282 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3286 /* are we at the end of the stream? */
3290 /* sanity check the size parameter */
3291 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3294 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3305 static const struct nvs_ops nvs_xdr_ops = {
3314 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3319 nvs->nvs_ops = &nvs_xdr_ops;
3321 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3322 *buflen - sizeof (nvs_header_t))) != 0)
3325 err = nvs_operation(nvs, nvl, buflen);
3327 nvs_xdr_destroy(nvs);