4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <sys/debug.h>
27 #include <sys/nvpair.h>
28 #include <sys/nvpair_impl.h>
29 #include <rpc/types.h>
32 #if defined(_KERNEL) && !defined(_BOOT)
33 #include <sys/varargs.h>
34 #include <sys/sunddi.h>
43 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
45 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
48 * nvpair.c - Provides kernel & userland interfaces for manipulating
63 * +--------------+ last i_nvp in list
64 * | nvpriv_t | +--------------------->
66 * +--+- nvp_list | | +------------+
67 * | | nvp_last -+--+ + nv_alloc_t |
68 * | | nvp_curr | |------------|
69 * | | nvp_nva -+----> | nva_ops |
70 * | | nvp_stat | | nva_arg |
71 * | +--------------+ +------------+
75 * +---------------------+ +-------------------+
76 * | i_nvp_t | +-->| i_nvp_t | +-->
77 * |---------------------| | |-------------------| |
78 * | nvi_next -+--+ | nvi_next -+--+
79 * | nvi_prev (NULL) | <----+ nvi_prev |
80 * | . . . . . . . . . . | | . . . . . . . . . |
81 * | nvp (nvpair_t) | | nvp (nvpair_t) |
82 * | - nvp_size | | - nvp_size |
83 * | - nvp_name_sz | | - nvp_name_sz |
84 * | - nvp_value_elem | | - nvp_value_elem |
85 * | - nvp_type | | - nvp_type |
86 * | - data ... | | - data ... |
87 * +---------------------+ +-------------------+
91 * +---------------------+ +---------------------+
92 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
93 * |---------------------| | | |---------------------|
94 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
95 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
96 * | . . . . . . . . . | | . . . . . . . . . |
97 * | nvp (nvpair_t) | | nvp (nvpair_t) |
98 * | - nvp_size | | - nvp_size |
99 * | - nvp_name_sz | | - nvp_name_sz |
100 * | - nvp_value_elem | | - nvp_value_elem |
101 * | - DATA_TYPE_NVLIST | | - nvp_type |
102 * | - data (embedded) | | - data ... |
103 * | nvlist name | +---------------------+
104 * | +--------------+ |
106 * | |--------------| |
107 * | | nvl_version | |
109 * | | nvl_priv --+---+---->
112 * | +--------------+ |
113 * +---------------------+
116 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
117 * allow value to be aligned on 8 byte boundary
119 * name_len is the length of the name string including the null terminator
122 #define NVP_SIZE_CALC(name_len, data_len) \
123 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
125 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
126 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
127 uint_t nelem, const void *data);
129 #define NV_STAT_EMBEDDED 0x1
130 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
131 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
133 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
134 #define NVPAIR2I_NVP(nvp) \
135 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
139 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
147 va_start(valist, nvo);
148 if (nva->nva_ops->nv_ao_init != NULL)
149 err = nva->nva_ops->nv_ao_init(nva, valist);
156 nv_alloc_reset(nv_alloc_t *nva)
158 if (nva->nva_ops->nv_ao_reset != NULL)
159 nva->nva_ops->nv_ao_reset(nva);
163 nv_alloc_fini(nv_alloc_t *nva)
165 if (nva->nva_ops->nv_ao_fini != NULL)
166 nva->nva_ops->nv_ao_fini(nva);
170 nvlist_lookup_nv_alloc(nvlist_t *nvl)
175 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
178 return (priv->nvp_nva);
182 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
184 nv_alloc_t *nva = nvp->nvp_nva;
187 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
194 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
196 nv_alloc_t *nva = nvp->nvp_nva;
198 nva->nva_ops->nv_ao_free(nva, buf, size);
202 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
204 bzero(priv, sizeof (nvpriv_t));
207 priv->nvp_stat = stat;
211 nv_priv_alloc(nv_alloc_t *nva)
216 * nv_mem_alloc() cannot called here because it needs the priv
219 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
222 nv_priv_init(priv, nva, 0);
228 * Embedded lists need their own nvpriv_t's. We create a new
229 * nvpriv_t using the parameters and allocator from the parent
233 nv_priv_alloc_embedded(nvpriv_t *priv)
237 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
240 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
246 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
248 nvl->nvl_version = NV_VERSION;
249 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
250 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
256 nvlist_nvflag(nvlist_t *nvl)
258 return (nvl->nvl_nvflag);
262 * nvlist_alloc - Allocate nvlist.
266 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
268 #if defined(_KERNEL) && !defined(_BOOT)
269 return (nvlist_xalloc(nvlp, nvflag,
270 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
272 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
277 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
281 if (nvlp == NULL || nva == NULL)
284 if ((priv = nv_priv_alloc(nva)) == NULL)
287 if ((*nvlp = nv_mem_zalloc(priv,
288 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
289 nv_mem_free(priv, priv, sizeof (nvpriv_t));
293 nvlist_init(*nvlp, nvflag, priv);
299 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
302 nvp_buf_alloc(nvlist_t *nvl, size_t len)
304 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
310 * Allocate the buffer
312 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
314 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
324 * nvp_buf_free - de-Allocate an i_nvp_t.
327 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
329 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
330 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
332 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
336 * nvp_buf_link - link a new nv pair into the nvlist.
339 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
341 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
342 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
344 /* Put element at end of nvlist */
345 if (priv->nvp_list == NULL) {
346 priv->nvp_list = priv->nvp_last = curr;
348 curr->nvi_prev = priv->nvp_last;
349 priv->nvp_last->nvi_next = curr;
350 priv->nvp_last = curr;
355 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
358 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
360 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
361 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
364 * protect nvlist_next_nvpair() against walking on freed memory.
366 if (priv->nvp_curr == curr)
367 priv->nvp_curr = curr->nvi_next;
369 if (curr == priv->nvp_list)
370 priv->nvp_list = curr->nvi_next;
372 curr->nvi_prev->nvi_next = curr->nvi_next;
374 if (curr == priv->nvp_last)
375 priv->nvp_last = curr->nvi_prev;
377 curr->nvi_next->nvi_prev = curr->nvi_prev;
381 * take a nvpair type and number of elements and make sure the are valid
384 i_validate_type_nelem(data_type_t type, uint_t nelem)
387 case DATA_TYPE_BOOLEAN:
391 case DATA_TYPE_BOOLEAN_VALUE:
394 case DATA_TYPE_UINT8:
395 case DATA_TYPE_INT16:
396 case DATA_TYPE_UINT16:
397 case DATA_TYPE_INT32:
398 case DATA_TYPE_UINT32:
399 case DATA_TYPE_INT64:
400 case DATA_TYPE_UINT64:
401 case DATA_TYPE_STRING:
402 case DATA_TYPE_HRTIME:
403 case DATA_TYPE_NVLIST:
404 #if !defined(_KERNEL)
405 case DATA_TYPE_DOUBLE:
410 case DATA_TYPE_BOOLEAN_ARRAY:
411 case DATA_TYPE_BYTE_ARRAY:
412 case DATA_TYPE_INT8_ARRAY:
413 case DATA_TYPE_UINT8_ARRAY:
414 case DATA_TYPE_INT16_ARRAY:
415 case DATA_TYPE_UINT16_ARRAY:
416 case DATA_TYPE_INT32_ARRAY:
417 case DATA_TYPE_UINT32_ARRAY:
418 case DATA_TYPE_INT64_ARRAY:
419 case DATA_TYPE_UINT64_ARRAY:
420 case DATA_TYPE_STRING_ARRAY:
421 case DATA_TYPE_NVLIST_ARRAY:
422 /* we allow arrays with 0 elements */
431 * Verify nvp_name_sz and check the name string length.
434 i_validate_nvpair_name(nvpair_t *nvp)
436 if ((nvp->nvp_name_sz <= 0) ||
437 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
440 /* verify the name string, make sure its terminated */
441 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
444 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
448 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
451 case DATA_TYPE_BOOLEAN_VALUE:
452 if (*(boolean_t *)data != B_TRUE &&
453 *(boolean_t *)data != B_FALSE)
456 case DATA_TYPE_BOOLEAN_ARRAY: {
459 for (i = 0; i < nelem; i++)
460 if (((boolean_t *)data)[i] != B_TRUE &&
461 ((boolean_t *)data)[i] != B_FALSE)
473 * This function takes a pointer to what should be a nvpair and it's size
474 * and then verifies that all the nvpair fields make sense and can be
475 * trusted. This function is used when decoding packed nvpairs.
478 i_validate_nvpair(nvpair_t *nvp)
480 data_type_t type = NVP_TYPE(nvp);
483 /* verify nvp_name_sz, check the name string length */
484 if (i_validate_nvpair_name(nvp) != 0)
487 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
491 * verify nvp_type, nvp_value_elem, and also possibly
492 * verify string values and get the value size.
494 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
495 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
496 if (size2 < 0 || size1 != NV_ALIGN(size2))
503 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
508 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
511 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
512 nvpair_t *nvp = &curr->nvi_nvp;
515 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
516 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
524 * Frees all memory allocated for an nvpair (like embedded lists) with
525 * the exception of the nvpair buffer itself.
528 nvpair_free(nvpair_t *nvp)
530 switch (NVP_TYPE(nvp)) {
531 case DATA_TYPE_NVLIST:
532 nvlist_free(EMBEDDED_NVL(nvp));
534 case DATA_TYPE_NVLIST_ARRAY: {
535 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
538 for (i = 0; i < NVP_NELEM(nvp); i++)
540 nvlist_free(nvlp[i]);
549 * nvlist_free - free an unpacked nvlist
552 nvlist_free(nvlist_t *nvl)
558 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
562 * Unpacked nvlist are linked through i_nvp_t
564 curr = priv->nvp_list;
565 while (curr != NULL) {
566 nvpair_t *nvp = &curr->nvi_nvp;
567 curr = curr->nvi_next;
570 nvp_buf_free(nvl, nvp);
573 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
574 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
578 nv_mem_free(priv, priv, sizeof (nvpriv_t));
582 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
584 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
590 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
591 if (&curr->nvi_nvp == nvp)
598 * Make a copy of nvlist
602 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
604 #if defined(_KERNEL) && !defined(_BOOT)
605 return (nvlist_xdup(nvl, nvlp,
606 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
608 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
613 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
618 if (nvl == NULL || nvlp == NULL)
621 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
624 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
633 * Remove all with matching name
636 nvlist_remove_all(nvlist_t *nvl, const char *name)
642 if (nvl == NULL || name == NULL ||
643 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
646 curr = priv->nvp_list;
647 while (curr != NULL) {
648 nvpair_t *nvp = &curr->nvi_nvp;
650 curr = curr->nvi_next;
651 if (strcmp(name, NVP_NAME(nvp)) != 0)
654 nvp_buf_unlink(nvl, nvp);
656 nvp_buf_free(nvl, nvp);
665 * Remove first one with matching name and type
668 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
673 if (nvl == NULL || name == NULL ||
674 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
677 curr = priv->nvp_list;
678 while (curr != NULL) {
679 nvpair_t *nvp = &curr->nvi_nvp;
681 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
682 nvp_buf_unlink(nvl, nvp);
684 nvp_buf_free(nvl, nvp);
688 curr = curr->nvi_next;
695 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
697 if (nvl == NULL || nvp == NULL)
700 nvp_buf_unlink(nvl, nvp);
702 nvp_buf_free(nvl, nvp);
707 * This function calculates the size of an nvpair value.
709 * The data argument controls the behavior in case of the data types
710 * DATA_TYPE_STRING and
711 * DATA_TYPE_STRING_ARRAY
712 * Is data == NULL then the size of the string(s) is excluded.
715 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
719 if (i_validate_type_nelem(type, nelem) != 0)
722 /* Calculate required size for holding value */
724 case DATA_TYPE_BOOLEAN:
727 case DATA_TYPE_BOOLEAN_VALUE:
728 value_sz = sizeof (boolean_t);
731 value_sz = sizeof (uchar_t);
734 value_sz = sizeof (int8_t);
736 case DATA_TYPE_UINT8:
737 value_sz = sizeof (uint8_t);
739 case DATA_TYPE_INT16:
740 value_sz = sizeof (int16_t);
742 case DATA_TYPE_UINT16:
743 value_sz = sizeof (uint16_t);
745 case DATA_TYPE_INT32:
746 value_sz = sizeof (int32_t);
748 case DATA_TYPE_UINT32:
749 value_sz = sizeof (uint32_t);
751 case DATA_TYPE_INT64:
752 value_sz = sizeof (int64_t);
754 case DATA_TYPE_UINT64:
755 value_sz = sizeof (uint64_t);
757 #if !defined(_KERNEL)
758 case DATA_TYPE_DOUBLE:
759 value_sz = sizeof (double);
762 case DATA_TYPE_STRING:
766 value_sz = strlen(data) + 1;
768 case DATA_TYPE_BOOLEAN_ARRAY:
769 value_sz = (uint64_t)nelem * sizeof (boolean_t);
771 case DATA_TYPE_BYTE_ARRAY:
772 value_sz = (uint64_t)nelem * sizeof (uchar_t);
774 case DATA_TYPE_INT8_ARRAY:
775 value_sz = (uint64_t)nelem * sizeof (int8_t);
777 case DATA_TYPE_UINT8_ARRAY:
778 value_sz = (uint64_t)nelem * sizeof (uint8_t);
780 case DATA_TYPE_INT16_ARRAY:
781 value_sz = (uint64_t)nelem * sizeof (int16_t);
783 case DATA_TYPE_UINT16_ARRAY:
784 value_sz = (uint64_t)nelem * sizeof (uint16_t);
786 case DATA_TYPE_INT32_ARRAY:
787 value_sz = (uint64_t)nelem * sizeof (int32_t);
789 case DATA_TYPE_UINT32_ARRAY:
790 value_sz = (uint64_t)nelem * sizeof (uint32_t);
792 case DATA_TYPE_INT64_ARRAY:
793 value_sz = (uint64_t)nelem * sizeof (int64_t);
795 case DATA_TYPE_UINT64_ARRAY:
796 value_sz = (uint64_t)nelem * sizeof (uint64_t);
798 case DATA_TYPE_STRING_ARRAY:
799 value_sz = (uint64_t)nelem * sizeof (uint64_t);
802 char *const *strs = data;
805 /* no alignment requirement for strings */
806 for (i = 0; i < nelem; i++) {
809 value_sz += strlen(strs[i]) + 1;
813 case DATA_TYPE_HRTIME:
814 value_sz = sizeof (hrtime_t);
816 case DATA_TYPE_NVLIST:
817 value_sz = NV_ALIGN(sizeof (nvlist_t));
819 case DATA_TYPE_NVLIST_ARRAY:
820 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
821 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
827 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
831 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
836 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
837 nvl->nvl_priv)) == NULL)
840 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
842 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
843 nvlist_free(emb_nvl);
844 emb_nvl->nvl_priv = 0;
851 * nvlist_add_common - Add new <name,value> pair to nvlist
854 nvlist_add_common(nvlist_t *nvl, const char *name,
855 data_type_t type, uint_t nelem, const void *data)
860 int nvp_sz, name_sz, value_sz;
863 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
866 if (nelem != 0 && data == NULL)
870 * Verify type and nelem and get the value size.
871 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
872 * is the size of the string(s) included.
874 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
877 if (i_validate_nvpair_value(type, nelem, data) != 0)
881 * If we're adding an nvlist or nvlist array, ensure that we are not
882 * adding the input nvlist to itself, which would cause recursion,
883 * and ensure that no NULL nvlist pointers are present.
886 case DATA_TYPE_NVLIST:
887 if (data == nvl || data == NULL)
890 case DATA_TYPE_NVLIST_ARRAY: {
891 nvlist_t **onvlp = (nvlist_t **)data;
892 for (i = 0; i < nelem; i++) {
893 if (onvlp[i] == nvl || onvlp[i] == NULL)
902 /* calculate sizes of the nvpair elements and the nvpair itself */
903 name_sz = strlen(name) + 1;
905 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
907 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
910 ASSERT(nvp->nvp_size == nvp_sz);
911 nvp->nvp_name_sz = name_sz;
912 nvp->nvp_value_elem = nelem;
913 nvp->nvp_type = type;
914 bcopy(name, NVP_NAME(nvp), name_sz);
917 case DATA_TYPE_BOOLEAN:
919 case DATA_TYPE_STRING_ARRAY: {
920 char *const *strs = data;
921 char *buf = NVP_VALUE(nvp);
922 char **cstrs = (void *)buf;
924 /* skip pre-allocated space for pointer array */
925 buf += nelem * sizeof (uint64_t);
926 for (i = 0; i < nelem; i++) {
927 int slen = strlen(strs[i]) + 1;
928 bcopy(strs[i], buf, slen);
934 case DATA_TYPE_NVLIST: {
935 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
936 nvlist_t *onvl = (nvlist_t *)data;
938 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
939 nvp_buf_free(nvl, nvp);
944 case DATA_TYPE_NVLIST_ARRAY: {
945 nvlist_t **onvlp = (nvlist_t **)data;
946 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
947 nvlist_t *embedded = (nvlist_t *)
948 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
950 for (i = 0; i < nelem; i++) {
951 if ((err = nvlist_copy_embedded(nvl,
952 onvlp[i], embedded)) != 0) {
954 * Free any successfully created lists
957 nvp_buf_free(nvl, nvp);
961 nvlp[i] = embedded++;
966 bcopy(data, NVP_VALUE(nvp), value_sz);
969 /* if unique name, remove before add */
970 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
971 (void) nvlist_remove_all(nvl, name);
972 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
973 (void) nvlist_remove(nvl, name, type);
975 nvp_buf_link(nvl, nvp);
981 nvlist_add_boolean(nvlist_t *nvl, const char *name)
983 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
987 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
989 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
993 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
995 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
999 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1001 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1005 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1007 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1011 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1013 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1017 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1019 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1023 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1025 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1029 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1031 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1035 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1037 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1041 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1043 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1046 #if !defined(_KERNEL)
1048 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1050 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1055 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1057 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1061 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1062 boolean_t *a, uint_t n)
1064 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1068 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1070 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1074 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1076 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1080 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1082 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1086 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1088 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1092 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1094 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1098 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1100 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1104 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1106 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1110 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1112 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1116 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1118 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1122 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1123 char *const *a, uint_t n)
1125 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1129 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1131 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1135 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1137 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1141 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1143 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1146 /* reading name-value pairs */
1148 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1154 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1157 curr = NVPAIR2I_NVP(nvp);
1160 * Ensure that nvp is a valid nvpair on this nvlist.
1161 * NB: nvp_curr is used only as a hint so that we don't always
1162 * have to walk the list to determine if nvp is still on the list.
1165 curr = priv->nvp_list;
1166 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1167 curr = curr->nvi_next;
1171 priv->nvp_curr = curr;
1173 return (curr != NULL ? &curr->nvi_nvp : NULL);
1177 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1183 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1186 curr = NVPAIR2I_NVP(nvp);
1189 curr = priv->nvp_last;
1190 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1191 curr = curr->nvi_prev;
1195 priv->nvp_curr = curr;
1197 return (curr != NULL ? &curr->nvi_nvp : NULL);
1201 nvlist_empty(nvlist_t *nvl)
1206 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1209 return (priv->nvp_list == NULL);
1213 nvpair_name(nvpair_t *nvp)
1215 return (NVP_NAME(nvp));
1219 nvpair_type(nvpair_t *nvp)
1221 return (NVP_TYPE(nvp));
1225 nvpair_type_is_array(nvpair_t *nvp)
1227 data_type_t type = NVP_TYPE(nvp);
1229 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1230 (type == DATA_TYPE_UINT8_ARRAY) ||
1231 (type == DATA_TYPE_INT16_ARRAY) ||
1232 (type == DATA_TYPE_UINT16_ARRAY) ||
1233 (type == DATA_TYPE_INT32_ARRAY) ||
1234 (type == DATA_TYPE_UINT32_ARRAY) ||
1235 (type == DATA_TYPE_INT64_ARRAY) ||
1236 (type == DATA_TYPE_UINT64_ARRAY) ||
1237 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1238 (type == DATA_TYPE_STRING_ARRAY) ||
1239 (type == DATA_TYPE_NVLIST_ARRAY))
1246 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1248 if (nvp == NULL || nvpair_type(nvp) != type)
1252 * For non-array types, we copy the data.
1253 * For array types (including string), we set a pointer.
1256 case DATA_TYPE_BOOLEAN:
1261 case DATA_TYPE_BOOLEAN_VALUE:
1262 case DATA_TYPE_BYTE:
1263 case DATA_TYPE_INT8:
1264 case DATA_TYPE_UINT8:
1265 case DATA_TYPE_INT16:
1266 case DATA_TYPE_UINT16:
1267 case DATA_TYPE_INT32:
1268 case DATA_TYPE_UINT32:
1269 case DATA_TYPE_INT64:
1270 case DATA_TYPE_UINT64:
1271 case DATA_TYPE_HRTIME:
1272 #if !defined(_KERNEL)
1273 case DATA_TYPE_DOUBLE:
1277 bcopy(NVP_VALUE(nvp), data,
1278 (size_t)i_get_value_size(type, NULL, 1));
1283 case DATA_TYPE_NVLIST:
1284 case DATA_TYPE_STRING:
1287 *(void **)data = (void *)NVP_VALUE(nvp);
1292 case DATA_TYPE_BOOLEAN_ARRAY:
1293 case DATA_TYPE_BYTE_ARRAY:
1294 case DATA_TYPE_INT8_ARRAY:
1295 case DATA_TYPE_UINT8_ARRAY:
1296 case DATA_TYPE_INT16_ARRAY:
1297 case DATA_TYPE_UINT16_ARRAY:
1298 case DATA_TYPE_INT32_ARRAY:
1299 case DATA_TYPE_UINT32_ARRAY:
1300 case DATA_TYPE_INT64_ARRAY:
1301 case DATA_TYPE_UINT64_ARRAY:
1302 case DATA_TYPE_STRING_ARRAY:
1303 case DATA_TYPE_NVLIST_ARRAY:
1304 if (nelem == NULL || data == NULL)
1306 if ((*nelem = NVP_NELEM(nvp)) != 0)
1307 *(void **)data = (void *)NVP_VALUE(nvp);
1309 *(void **)data = NULL;
1320 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1321 uint_t *nelem, void *data)
1327 if (name == NULL || nvl == NULL ||
1328 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1331 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1334 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1335 nvp = &curr->nvi_nvp;
1337 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1338 return (nvpair_value_common(nvp, type, nelem, data));
1345 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1347 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1351 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1353 return (nvlist_lookup_common(nvl, name,
1354 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1358 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1360 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1364 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1366 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1370 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1372 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1376 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1378 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1382 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1384 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1388 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1390 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1394 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1396 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1400 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1402 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1406 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1408 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1411 #if !defined(_KERNEL)
1413 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1415 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1420 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1422 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1426 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1428 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1432 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1433 boolean_t **a, uint_t *n)
1435 return (nvlist_lookup_common(nvl, name,
1436 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1440 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1441 uchar_t **a, uint_t *n)
1443 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1447 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1449 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1453 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1454 uint8_t **a, uint_t *n)
1456 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1460 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1461 int16_t **a, uint_t *n)
1463 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1467 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1468 uint16_t **a, uint_t *n)
1470 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1474 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1475 int32_t **a, uint_t *n)
1477 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1481 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1482 uint32_t **a, uint_t *n)
1484 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1488 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1489 int64_t **a, uint_t *n)
1491 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1495 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1496 uint64_t **a, uint_t *n)
1498 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1502 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1503 char ***a, uint_t *n)
1505 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1509 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1510 nvlist_t ***a, uint_t *n)
1512 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1516 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1518 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1522 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1526 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1530 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1535 switch (type = va_arg(ap, data_type_t)) {
1536 case DATA_TYPE_BOOLEAN:
1537 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1540 case DATA_TYPE_BOOLEAN_VALUE:
1541 case DATA_TYPE_BYTE:
1542 case DATA_TYPE_INT8:
1543 case DATA_TYPE_UINT8:
1544 case DATA_TYPE_INT16:
1545 case DATA_TYPE_UINT16:
1546 case DATA_TYPE_INT32:
1547 case DATA_TYPE_UINT32:
1548 case DATA_TYPE_INT64:
1549 case DATA_TYPE_UINT64:
1550 case DATA_TYPE_HRTIME:
1551 case DATA_TYPE_STRING:
1552 case DATA_TYPE_NVLIST:
1553 #if !defined(_KERNEL)
1554 case DATA_TYPE_DOUBLE:
1556 val = va_arg(ap, void *);
1557 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1560 case DATA_TYPE_BYTE_ARRAY:
1561 case DATA_TYPE_BOOLEAN_ARRAY:
1562 case DATA_TYPE_INT8_ARRAY:
1563 case DATA_TYPE_UINT8_ARRAY:
1564 case DATA_TYPE_INT16_ARRAY:
1565 case DATA_TYPE_UINT16_ARRAY:
1566 case DATA_TYPE_INT32_ARRAY:
1567 case DATA_TYPE_UINT32_ARRAY:
1568 case DATA_TYPE_INT64_ARRAY:
1569 case DATA_TYPE_UINT64_ARRAY:
1570 case DATA_TYPE_STRING_ARRAY:
1571 case DATA_TYPE_NVLIST_ARRAY:
1572 val = va_arg(ap, void *);
1573 nelem = va_arg(ap, uint_t *);
1574 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1581 if (ret == ENOENT && noentok)
1590 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1591 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1592 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1593 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1594 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1595 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1596 * code also supports "a.d[3]e[1]" syntax).
1598 * If 'ip' is non-NULL and the last name component is an array, return the
1599 * value of the "...[index]" array index in *ip. For an array reference that
1600 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1601 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1602 * inside the 'name' string where the syntax error was detected.
1605 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1606 nvpair_t **ret, int *ip, char **ep)
1617 *ip = -1; /* not indexed */
1621 if ((nvl == NULL) || (name == NULL))
1624 /* step through components of name */
1625 for (np = name; np && *np; np = sepp) {
1626 /* ensure unique names */
1627 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1630 /* skip white space */
1631 skip_whitespace(np);
1635 /* set 'sepp' to end of current component 'np' */
1637 sepp = strchr(np, sep);
1641 /* find start of next "[ index ]..." */
1642 idxp = strchr(np, '[');
1644 /* if sepp comes first, set idxp to NULL */
1645 if (sepp && idxp && (sepp < idxp))
1649 * At this point 'idxp' is set if there is an index
1650 * expected for the current component.
1653 /* set 'n' to length of current 'np' name component */
1656 /* keep sepp up to date for *ep use as we advance */
1657 skip_whitespace(idxp);
1660 /* determine the index value */
1661 #if defined(_KERNEL) && !defined(_BOOT)
1662 if (ddi_strtol(idxp, &idxep, 0, &idx))
1665 idx = strtol(idxp, &idxep, 0);
1670 /* keep sepp up to date for *ep use as we advance */
1673 /* skip white space index value and check for ']' */
1674 skip_whitespace(sepp);
1678 /* for embedded arrays, support C syntax: "a[1].b" */
1679 skip_whitespace(sepp);
1680 if (sep && (*sepp == sep))
1688 /* trim trailing whitespace by reducing length of 'np' */
1691 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1695 /* skip whitespace, and set sepp to NULL if complete */
1697 skip_whitespace(sepp);
1704 * o 'n' is the length of current 'np' component.
1705 * o 'idxp' is set if there was an index, and value 'idx'.
1706 * o 'sepp' is set to the beginning of the next component,
1707 * and set to NULL if we have no more components.
1709 * Search for nvpair with matching component name.
1711 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1712 nvp = nvlist_next_nvpair(nvl, nvp)) {
1714 /* continue if no match on name */
1715 if (strncmp(np, nvpair_name(nvp), n) ||
1716 (strlen(nvpair_name(nvp)) != n))
1719 /* if indexed, verify type is array oriented */
1720 if (idxp && !nvpair_type_is_array(nvp))
1724 * Full match found, return nvp and idx if this
1725 * was the last component.
1731 *ip = (int)idx; /* return index */
1732 return (0); /* found */
1736 * More components: current match must be
1737 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1738 * to support going deeper.
1740 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1741 nvl = EMBEDDED_NVL(nvp);
1743 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1744 (void) nvpair_value_nvlist_array(nvp,
1745 &nva, (uint_t *)&n);
1746 if ((n < 0) || (idx >= n))
1752 /* type does not support more levels */
1756 goto fail; /* 'name' not found */
1758 /* search for match of next component in embedded 'nvl' list */
1761 fail: if (ep && sepp)
1767 * Return pointer to nvpair with specified 'name'.
1770 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1772 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1776 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1777 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1780 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1781 const char *name, nvpair_t **ret, int *ip, char **ep)
1783 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1787 nvlist_exists(nvlist_t *nvl, const char *name)
1793 if (name == NULL || nvl == NULL ||
1794 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1797 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1798 nvp = &curr->nvi_nvp;
1800 if (strcmp(name, NVP_NAME(nvp)) == 0)
1808 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1810 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1814 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1816 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1820 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1822 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1826 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1828 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1832 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1834 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1838 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1840 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1844 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1846 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1850 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1852 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1856 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1858 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1862 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1864 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1867 #if !defined(_KERNEL)
1869 nvpair_value_double(nvpair_t *nvp, double *val)
1871 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1876 nvpair_value_string(nvpair_t *nvp, char **val)
1878 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1882 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1884 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1888 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1890 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1894 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1896 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1900 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1902 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1906 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1908 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1912 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1914 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1918 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1920 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1924 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1926 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1930 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1932 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1936 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1938 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1942 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1944 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1948 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1950 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1954 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1956 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1960 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1962 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1966 * Add specified pair to the list.
1969 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1971 if (nvl == NULL || nvp == NULL)
1974 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1975 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1979 * Merge the supplied nvlists and put the result in dst.
1980 * The merged list will contain all names specified in both lists,
1981 * the values are taken from nvl in the case of duplicates.
1982 * Return 0 on success.
1986 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1988 if (nvl == NULL || dst == NULL)
1992 return (nvlist_copy_pairs(nvl, dst));
1998 * Encoding related routines
2000 #define NVS_OP_ENCODE 0
2001 #define NVS_OP_DECODE 1
2002 #define NVS_OP_GETSIZE 2
2004 typedef struct nvs_ops nvs_ops_t;
2008 const nvs_ops_t *nvs_ops;
2014 * nvs operations are:
2016 * encoding / decoding of a nvlist header (nvlist_t)
2017 * calculates the size used for header and end detection
2020 * responsible for the first part of encoding / decoding of an nvpair
2021 * calculates the decoded size of an nvpair
2024 * second part of encoding / decoding of an nvpair
2027 * calculates the encoding size of an nvpair
2030 * encodes the end detection mark (zeros).
2033 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2034 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2035 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2036 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2037 int (*nvs_nvl_fini)(nvstream_t *);
2041 char nvh_encoding; /* nvs encoding method */
2042 char nvh_endian; /* nvs endian */
2043 char nvh_reserved1; /* reserved for future use */
2044 char nvh_reserved2; /* reserved for future use */
2048 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2050 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2054 * Walk nvpair in list and encode each nvpair
2056 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2057 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2060 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2064 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2071 * Get decoded size of next pair in stream, alloc
2072 * memory for nvpair_t, then decode the nvpair
2074 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2075 if (nvsize == 0) /* end of list */
2078 /* make sure len makes sense */
2079 if (nvsize < NVP_SIZE_CALC(1, 0))
2082 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2085 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2086 nvp_buf_free(nvl, nvp);
2090 if (i_validate_nvpair(nvp) != 0) {
2092 nvp_buf_free(nvl, nvp);
2096 nvp_buf_link(nvl, nvp);
2102 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2104 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2106 uint64_t nvsize = *buflen;
2110 * Get encoded size of nvpairs in nvlist
2112 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2113 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2116 if ((nvsize += size) > INT32_MAX)
2125 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2129 if (nvl->nvl_priv == 0)
2133 * Perform the operation, starting with header, then each nvpair
2135 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2138 switch (nvs->nvs_op) {
2140 err = nvs_encode_pairs(nvs, nvl);
2144 err = nvs_decode_pairs(nvs, nvl);
2147 case NVS_OP_GETSIZE:
2148 err = nvs_getsize_pairs(nvs, nvl, buflen);
2159 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2161 switch (nvs->nvs_op) {
2163 return (nvs_operation(nvs, embedded, NULL));
2165 case NVS_OP_DECODE: {
2169 if (embedded->nvl_version != NV_VERSION)
2172 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2175 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2177 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2178 nvlist_free(embedded);
2189 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2191 size_t nelem = NVP_NELEM(nvp);
2192 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2195 switch (nvs->nvs_op) {
2197 for (i = 0; i < nelem; i++)
2198 if (nvs_embedded(nvs, nvlp[i]) != 0)
2202 case NVS_OP_DECODE: {
2203 size_t len = nelem * sizeof (uint64_t);
2204 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2206 bzero(nvlp, len); /* don't trust packed data */
2207 for (i = 0; i < nelem; i++) {
2208 if (nvs_embedded(nvs, embedded) != 0) {
2213 nvlp[i] = embedded++;
2217 case NVS_OP_GETSIZE: {
2218 uint64_t nvsize = 0;
2220 for (i = 0; i < nelem; i++) {
2223 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2226 if ((nvsize += nvp_sz) > INT32_MAX)
2240 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2241 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2244 * Common routine for nvlist operations:
2245 * encode, decode, getsize (encoded size).
2248 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2254 #if BYTE_ORDER == _LITTLE_ENDIAN
2255 int host_endian = 1;
2257 int host_endian = 0;
2258 #endif /* _LITTLE_ENDIAN */
2259 nvs_header_t *nvh = (void *)buf;
2261 if (buflen == NULL || nvl == NULL ||
2262 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2265 nvs.nvs_op = nvs_op;
2268 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2269 * a buffer is allocated. The first 4 bytes in the buffer are
2270 * used for encoding method and host endian.
2274 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2277 nvh->nvh_encoding = encoding;
2278 nvh->nvh_endian = nvl_endian = host_endian;
2279 nvh->nvh_reserved1 = 0;
2280 nvh->nvh_reserved2 = 0;
2284 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2287 /* get method of encoding from first byte */
2288 encoding = nvh->nvh_encoding;
2289 nvl_endian = nvh->nvh_endian;
2292 case NVS_OP_GETSIZE:
2293 nvl_endian = host_endian;
2296 * add the size for encoding
2298 *buflen = sizeof (nvs_header_t);
2306 * Create an nvstream with proper encoding method
2309 case NV_ENCODE_NATIVE:
2311 * check endianness, in case we are unpacking
2314 if (nvl_endian != host_endian)
2316 err = nvs_native(&nvs, nvl, buf, buflen);
2319 err = nvs_xdr(&nvs, nvl, buf, buflen);
2330 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2332 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2336 * Pack nvlist into contiguous memory
2340 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2343 #if defined(_KERNEL) && !defined(_BOOT)
2344 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2345 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2347 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2352 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2360 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2364 return (nvlist_common(nvl, *bufp, buflen, encoding,
2368 * Here is a difficult situation:
2369 * 1. The nvlist has fixed allocator properties.
2370 * All other nvlist routines (like nvlist_add_*, ...) use
2372 * 2. When using nvlist_pack() the user can specify his own
2373 * allocator properties (e.g. by using KM_NOSLEEP).
2375 * We use the user specified properties (2). A clearer solution
2376 * will be to remove the kmflag from nvlist_pack(), but we will
2377 * not change the interface.
2379 nv_priv_init(&nvpriv, nva, 0);
2381 if (err = nvlist_size(nvl, &alloc_size, encoding))
2384 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2387 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2388 NVS_OP_ENCODE)) != 0) {
2389 nv_mem_free(&nvpriv, buf, alloc_size);
2391 *buflen = alloc_size;
2399 * Unpack buf into an nvlist_t
2403 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2405 #if defined(_KERNEL) && !defined(_BOOT)
2406 return (nvlist_xunpack(buf, buflen, nvlp,
2407 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2409 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2414 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2422 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2425 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2434 * Native encoding functions
2438 * This structure is used when decoding a packed nvpair in
2439 * the native format. n_base points to a buffer containing the
2440 * packed nvpair. n_end is a pointer to the end of the buffer.
2441 * (n_end actually points to the first byte past the end of the
2442 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2443 * It points to the current data that we are decoding.
2444 * The amount of data left in the buffer is equal to n_end - n_curr.
2445 * n_flag is used to recognize a packed embedded list.
2454 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2457 switch (nvs->nvs_op) {
2460 nvs->nvs_private = native;
2461 native->n_curr = native->n_base = buf;
2462 native->n_end = buf + buflen;
2466 case NVS_OP_GETSIZE:
2467 nvs->nvs_private = native;
2468 native->n_curr = native->n_base = native->n_end = NULL;
2478 nvs_native_destroy(nvstream_t *nvs)
2483 native_cp(nvstream_t *nvs, void *buf, size_t size)
2485 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2487 if (native->n_curr + size > native->n_end)
2491 * The bcopy() below eliminates alignment requirement
2492 * on the buffer (stream) and is preferred over direct access.
2494 switch (nvs->nvs_op) {
2496 bcopy(buf, native->n_curr, size);
2499 bcopy(native->n_curr, buf, size);
2505 native->n_curr += size;
2510 * operate on nvlist_t header
2513 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2515 nvs_native_t *native = nvs->nvs_private;
2517 switch (nvs->nvs_op) {
2521 return (0); /* packed embedded list */
2525 /* copy version and nvflag of the nvlist_t */
2526 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2527 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2532 case NVS_OP_GETSIZE:
2534 * if calculate for packed embedded list
2535 * 4 for end of the embedded list
2537 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2538 * and 4 for end of the entire list
2540 if (native->n_flag) {
2544 *size += 2 * sizeof (int32_t) + 4;
2555 nvs_native_nvl_fini(nvstream_t *nvs)
2557 if (nvs->nvs_op == NVS_OP_ENCODE) {
2558 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2560 * Add 4 zero bytes at end of nvlist. They are used
2561 * for end detection by the decode routine.
2563 if (native->n_curr + sizeof (int) > native->n_end)
2566 bzero(native->n_curr, sizeof (int));
2567 native->n_curr += sizeof (int);
2574 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2576 if (nvs->nvs_op == NVS_OP_ENCODE) {
2577 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2578 char *packed = (void *)
2579 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2581 * Null out the pointer that is meaningless in the packed
2582 * structure. The address may not be aligned, so we have
2585 bzero(packed + offsetof(nvlist_t, nvl_priv),
2586 sizeof(((nvlist_t *)NULL)->nvl_priv));
2589 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2593 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2595 if (nvs->nvs_op == NVS_OP_ENCODE) {
2596 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2597 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2598 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2601 * Null out pointers that are meaningless in the packed
2602 * structure. The addresses may not be aligned, so we have
2608 for (i = 0; i < NVP_NELEM(nvp); i++) {
2610 * Null out the pointer that is meaningless in the
2611 * packed structure. The address may not be aligned,
2612 * so we have to use bzero.
2614 bzero(value + offsetof(nvlist_t, nvl_priv),
2615 sizeof(((nvlist_t *)NULL)->nvl_priv));
2616 value += sizeof(nvlist_t);
2620 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2624 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2626 switch (nvs->nvs_op) {
2627 case NVS_OP_ENCODE: {
2628 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2629 uint64_t *strp = (void *)
2630 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2632 * Null out pointers that are meaningless in the packed
2633 * structure. The addresses may not be aligned, so we have
2636 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2639 case NVS_OP_DECODE: {
2640 char **strp = (void *)NVP_VALUE(nvp);
2641 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2644 for (i = 0; i < NVP_NELEM(nvp); i++) {
2646 buf += strlen(buf) + 1;
2654 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2661 * We do the initial bcopy of the data before we look at
2662 * the nvpair type, because when we're decoding, we won't
2663 * have the correct values for the pair until we do the bcopy.
2665 switch (nvs->nvs_op) {
2668 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2675 /* verify nvp_name_sz, check the name string length */
2676 if (i_validate_nvpair_name(nvp) != 0)
2679 type = NVP_TYPE(nvp);
2682 * Verify type and nelem and get the value size.
2683 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2684 * is the size of the string(s) excluded.
2686 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2689 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2693 case DATA_TYPE_NVLIST:
2694 ret = nvpair_native_embedded(nvs, nvp);
2696 case DATA_TYPE_NVLIST_ARRAY:
2697 ret = nvpair_native_embedded_array(nvs, nvp);
2699 case DATA_TYPE_STRING_ARRAY:
2700 nvpair_native_string_array(nvs, nvp);
2710 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2712 uint64_t nvp_sz = nvp->nvp_size;
2714 switch (NVP_TYPE(nvp)) {
2715 case DATA_TYPE_NVLIST: {
2718 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2724 case DATA_TYPE_NVLIST_ARRAY: {
2727 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2737 if (nvp_sz > INT32_MAX)
2746 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2748 switch (nvs->nvs_op) {
2750 return (nvs_native_nvp_op(nvs, nvp));
2752 case NVS_OP_DECODE: {
2753 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2756 /* try to read the size value from the stream */
2757 if (native->n_curr + sizeof (int32_t) > native->n_end)
2759 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2761 /* sanity check the size value */
2762 if (decode_len < 0 ||
2763 decode_len > native->n_end - native->n_curr)
2769 * If at the end of the stream then move the cursor
2770 * forward, otherwise nvpair_native_op() will read
2771 * the entire nvpair at the same cursor position.
2774 native->n_curr += sizeof (int32_t);
2785 static const nvs_ops_t nvs_native_ops = {
2789 nvs_native_nvp_size,
2794 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2796 nvs_native_t native;
2799 nvs->nvs_ops = &nvs_native_ops;
2801 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2802 *buflen - sizeof (nvs_header_t))) != 0)
2805 err = nvs_operation(nvs, nvl, buflen);
2807 nvs_native_destroy(nvs);
2813 * XDR encoding functions
2815 * An xdr packed nvlist is encoded as:
2817 * - encoding methode and host endian (4 bytes)
2818 * - nvl_version (4 bytes)
2819 * - nvl_nvflag (4 bytes)
2821 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2822 * - encoded size of the nvpair (4 bytes)
2823 * - decoded size of the nvpair (4 bytes)
2824 * - name string, (4 + sizeof(NV_ALIGN4(string))
2825 * a string is coded as size (4 bytes) and data
2826 * - data type (4 bytes)
2827 * - number of elements in the nvpair (4 bytes)
2830 * - 2 zero's for end of the entire list (8 bytes)
2833 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2835 /* xdr data must be 4 byte aligned */
2836 if ((ulong_t)buf % 4 != 0)
2839 switch (nvs->nvs_op) {
2841 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2842 nvs->nvs_private = xdr;
2845 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2846 nvs->nvs_private = xdr;
2848 case NVS_OP_GETSIZE:
2849 nvs->nvs_private = NULL;
2857 nvs_xdr_destroy(nvstream_t *nvs)
2859 switch (nvs->nvs_op) {
2862 xdr_destroy((XDR *)nvs->nvs_private);
2870 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2872 switch (nvs->nvs_op) {
2874 case NVS_OP_DECODE: {
2875 XDR *xdr = nvs->nvs_private;
2877 if (!xdr_int(xdr, &nvl->nvl_version) ||
2878 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2882 case NVS_OP_GETSIZE: {
2884 * 2 * 4 for nvl_version + nvl_nvflag
2885 * and 8 for end of the entire list
2897 nvs_xdr_nvl_fini(nvstream_t *nvs)
2899 if (nvs->nvs_op == NVS_OP_ENCODE) {
2900 XDR *xdr = nvs->nvs_private;
2903 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2911 * The format of xdr encoded nvpair is:
2912 * encode_size, decode_size, name string, data type, nelem, data
2915 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2919 char *buf_end = (char *)nvp + nvp->nvp_size;
2921 uint_t nelem, buflen;
2923 XDR *xdr = nvs->nvs_private;
2925 ASSERT(xdr != NULL && nvp != NULL);
2928 if ((buf = NVP_NAME(nvp)) >= buf_end)
2930 buflen = buf_end - buf;
2932 if (!xdr_string(xdr, &buf, buflen - 1))
2934 nvp->nvp_name_sz = strlen(buf) + 1;
2936 /* type and nelem */
2937 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2938 !xdr_int(xdr, &nvp->nvp_value_elem))
2941 type = NVP_TYPE(nvp);
2942 nelem = nvp->nvp_value_elem;
2945 * Verify type and nelem and get the value size.
2946 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2947 * is the size of the string(s) excluded.
2949 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2952 /* if there is no data to extract then return */
2957 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2959 buflen = buf_end - buf;
2961 if (buflen < value_sz)
2965 case DATA_TYPE_NVLIST:
2966 if (nvs_embedded(nvs, (void *)buf) == 0)
2970 case DATA_TYPE_NVLIST_ARRAY:
2971 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2975 case DATA_TYPE_BOOLEAN:
2979 case DATA_TYPE_BYTE:
2980 case DATA_TYPE_INT8:
2981 case DATA_TYPE_UINT8:
2982 ret = xdr_char(xdr, buf);
2985 case DATA_TYPE_INT16:
2986 ret = xdr_short(xdr, (void *)buf);
2989 case DATA_TYPE_UINT16:
2990 ret = xdr_u_short(xdr, (void *)buf);
2993 case DATA_TYPE_BOOLEAN_VALUE:
2994 case DATA_TYPE_INT32:
2995 ret = xdr_int(xdr, (void *)buf);
2998 case DATA_TYPE_UINT32:
2999 ret = xdr_u_int(xdr, (void *)buf);
3002 case DATA_TYPE_INT64:
3003 ret = xdr_longlong_t(xdr, (void *)buf);
3006 case DATA_TYPE_UINT64:
3007 ret = xdr_u_longlong_t(xdr, (void *)buf);
3010 case DATA_TYPE_HRTIME:
3012 * NOTE: must expose the definition of hrtime_t here
3014 ret = xdr_longlong_t(xdr, (void *)buf);
3016 #if !defined(_KERNEL)
3017 case DATA_TYPE_DOUBLE:
3018 ret = xdr_double(xdr, (void *)buf);
3021 case DATA_TYPE_STRING:
3022 ret = xdr_string(xdr, &buf, buflen - 1);
3025 case DATA_TYPE_BYTE_ARRAY:
3026 ret = xdr_opaque(xdr, buf, nelem);
3029 case DATA_TYPE_INT8_ARRAY:
3030 case DATA_TYPE_UINT8_ARRAY:
3031 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3032 (xdrproc_t)xdr_char);
3035 case DATA_TYPE_INT16_ARRAY:
3036 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3037 sizeof (int16_t), (xdrproc_t)xdr_short);
3040 case DATA_TYPE_UINT16_ARRAY:
3041 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3042 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3045 case DATA_TYPE_BOOLEAN_ARRAY:
3046 case DATA_TYPE_INT32_ARRAY:
3047 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3048 sizeof (int32_t), (xdrproc_t)xdr_int);
3051 case DATA_TYPE_UINT32_ARRAY:
3052 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3053 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3056 case DATA_TYPE_INT64_ARRAY:
3057 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3058 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3061 case DATA_TYPE_UINT64_ARRAY:
3062 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3063 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3066 case DATA_TYPE_STRING_ARRAY: {
3067 size_t len = nelem * sizeof (uint64_t);
3068 char **strp = (void *)buf;
3071 if (nvs->nvs_op == NVS_OP_DECODE)
3072 bzero(buf, len); /* don't trust packed data */
3074 for (i = 0; i < nelem; i++) {
3081 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3084 if (nvs->nvs_op == NVS_OP_DECODE)
3086 len = strlen(buf) + 1;
3095 return (ret == TRUE ? 0 : EFAULT);
3099 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3101 data_type_t type = NVP_TYPE(nvp);
3103 * encode_size + decode_size + name string size + data type + nelem
3104 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3106 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3109 case DATA_TYPE_BOOLEAN:
3112 case DATA_TYPE_BOOLEAN_VALUE:
3113 case DATA_TYPE_BYTE:
3114 case DATA_TYPE_INT8:
3115 case DATA_TYPE_UINT8:
3116 case DATA_TYPE_INT16:
3117 case DATA_TYPE_UINT16:
3118 case DATA_TYPE_INT32:
3119 case DATA_TYPE_UINT32:
3120 nvp_sz += 4; /* 4 is the minimum xdr unit */
3123 case DATA_TYPE_INT64:
3124 case DATA_TYPE_UINT64:
3125 case DATA_TYPE_HRTIME:
3126 #if !defined(_KERNEL)
3127 case DATA_TYPE_DOUBLE:
3132 case DATA_TYPE_STRING:
3133 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3136 case DATA_TYPE_BYTE_ARRAY:
3137 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3140 case DATA_TYPE_BOOLEAN_ARRAY:
3141 case DATA_TYPE_INT8_ARRAY:
3142 case DATA_TYPE_UINT8_ARRAY:
3143 case DATA_TYPE_INT16_ARRAY:
3144 case DATA_TYPE_UINT16_ARRAY:
3145 case DATA_TYPE_INT32_ARRAY:
3146 case DATA_TYPE_UINT32_ARRAY:
3147 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3150 case DATA_TYPE_INT64_ARRAY:
3151 case DATA_TYPE_UINT64_ARRAY:
3152 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3155 case DATA_TYPE_STRING_ARRAY: {
3157 char **strs = (void *)NVP_VALUE(nvp);
3159 for (i = 0; i < NVP_NELEM(nvp); i++)
3160 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3165 case DATA_TYPE_NVLIST:
3166 case DATA_TYPE_NVLIST_ARRAY: {
3168 int old_nvs_op = nvs->nvs_op;
3171 nvs->nvs_op = NVS_OP_GETSIZE;
3172 if (type == DATA_TYPE_NVLIST)
3173 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3175 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3176 nvs->nvs_op = old_nvs_op;
3189 if (nvp_sz > INT32_MAX)
3199 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3200 * the largest nvpair that could be encoded in the buffer.
3202 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3203 * The size of a xdr packed nvpair without any data is 5 words.
3205 * Using the size of the data directly as an estimate would be ok
3206 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3207 * then the actual nvpair has space for an array of pointers to index
3208 * the strings. These pointers are not encoded into the packed xdr buffer.
3210 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3211 * of length 0, then each string is endcoded in xdr format as a single word.
3212 * Therefore when expanded to an nvpair there will be 2.25 word used for
3213 * each string. (a int64_t allocated for pointer usage, and a single char
3214 * for the null termination.)
3216 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3218 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3219 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3220 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3221 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3222 (NVS_XDR_DATA_LEN(x) * 2) + \
3223 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3226 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3228 XDR *xdr = nvs->nvs_private;
3229 int32_t encode_len, decode_len;
3231 switch (nvs->nvs_op) {
3232 case NVS_OP_ENCODE: {
3235 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3238 decode_len = nvp->nvp_size;
3239 encode_len = nvsize;
3240 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3243 return (nvs_xdr_nvp_op(nvs, nvp));
3245 case NVS_OP_DECODE: {
3246 struct xdr_bytesrec bytesrec;
3248 /* get the encode and decode size */
3249 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3253 /* are we at the end of the stream? */
3257 /* sanity check the size parameter */
3258 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3261 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3272 static const struct nvs_ops nvs_xdr_ops = {
3281 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3286 nvs->nvs_ops = &nvs_xdr_ops;
3288 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3289 *buflen - sizeof (nvs_header_t))) != 0)
3292 err = nvs_operation(nvs, nvl, buflen);
3294 nvs_xdr_destroy(nvs);