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_INT8_ARRAY) ||
1231 (type == DATA_TYPE_UINT8_ARRAY) ||
1232 (type == DATA_TYPE_INT16_ARRAY) ||
1233 (type == DATA_TYPE_UINT16_ARRAY) ||
1234 (type == DATA_TYPE_INT32_ARRAY) ||
1235 (type == DATA_TYPE_UINT32_ARRAY) ||
1236 (type == DATA_TYPE_INT64_ARRAY) ||
1237 (type == DATA_TYPE_UINT64_ARRAY) ||
1238 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1239 (type == DATA_TYPE_STRING_ARRAY) ||
1240 (type == DATA_TYPE_NVLIST_ARRAY))
1247 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1249 if (nvp == NULL || nvpair_type(nvp) != type)
1253 * For non-array types, we copy the data.
1254 * For array types (including string), we set a pointer.
1257 case DATA_TYPE_BOOLEAN:
1262 case DATA_TYPE_BOOLEAN_VALUE:
1263 case DATA_TYPE_BYTE:
1264 case DATA_TYPE_INT8:
1265 case DATA_TYPE_UINT8:
1266 case DATA_TYPE_INT16:
1267 case DATA_TYPE_UINT16:
1268 case DATA_TYPE_INT32:
1269 case DATA_TYPE_UINT32:
1270 case DATA_TYPE_INT64:
1271 case DATA_TYPE_UINT64:
1272 case DATA_TYPE_HRTIME:
1273 #if !defined(_KERNEL)
1274 case DATA_TYPE_DOUBLE:
1278 bcopy(NVP_VALUE(nvp), data,
1279 (size_t)i_get_value_size(type, NULL, 1));
1284 case DATA_TYPE_NVLIST:
1285 case DATA_TYPE_STRING:
1288 *(void **)data = (void *)NVP_VALUE(nvp);
1293 case DATA_TYPE_BOOLEAN_ARRAY:
1294 case DATA_TYPE_BYTE_ARRAY:
1295 case DATA_TYPE_INT8_ARRAY:
1296 case DATA_TYPE_UINT8_ARRAY:
1297 case DATA_TYPE_INT16_ARRAY:
1298 case DATA_TYPE_UINT16_ARRAY:
1299 case DATA_TYPE_INT32_ARRAY:
1300 case DATA_TYPE_UINT32_ARRAY:
1301 case DATA_TYPE_INT64_ARRAY:
1302 case DATA_TYPE_UINT64_ARRAY:
1303 case DATA_TYPE_STRING_ARRAY:
1304 case DATA_TYPE_NVLIST_ARRAY:
1305 if (nelem == NULL || data == NULL)
1307 if ((*nelem = NVP_NELEM(nvp)) != 0)
1308 *(void **)data = (void *)NVP_VALUE(nvp);
1310 *(void **)data = NULL;
1321 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1322 uint_t *nelem, void *data)
1328 if (name == NULL || nvl == NULL ||
1329 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1332 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1335 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1336 nvp = &curr->nvi_nvp;
1338 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1339 return (nvpair_value_common(nvp, type, nelem, data));
1346 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1348 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1352 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1354 return (nvlist_lookup_common(nvl, name,
1355 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1359 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1361 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1365 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1367 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1371 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1373 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1377 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1379 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1383 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1385 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1389 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1391 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1395 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1397 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1401 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1403 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1407 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1409 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1412 #if !defined(_KERNEL)
1414 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1416 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1421 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1423 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1427 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1429 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1433 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1434 boolean_t **a, uint_t *n)
1436 return (nvlist_lookup_common(nvl, name,
1437 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1441 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1442 uchar_t **a, uint_t *n)
1444 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1448 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1450 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1454 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1455 uint8_t **a, uint_t *n)
1457 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1461 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1462 int16_t **a, uint_t *n)
1464 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1468 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1469 uint16_t **a, uint_t *n)
1471 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1475 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1476 int32_t **a, uint_t *n)
1478 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1482 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1483 uint32_t **a, uint_t *n)
1485 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1489 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1490 int64_t **a, uint_t *n)
1492 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1496 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1497 uint64_t **a, uint_t *n)
1499 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1503 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1504 char ***a, uint_t *n)
1506 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1510 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1511 nvlist_t ***a, uint_t *n)
1513 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1517 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1519 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1523 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1527 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1531 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1536 switch (type = va_arg(ap, data_type_t)) {
1537 case DATA_TYPE_BOOLEAN:
1538 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1541 case DATA_TYPE_BOOLEAN_VALUE:
1542 case DATA_TYPE_BYTE:
1543 case DATA_TYPE_INT8:
1544 case DATA_TYPE_UINT8:
1545 case DATA_TYPE_INT16:
1546 case DATA_TYPE_UINT16:
1547 case DATA_TYPE_INT32:
1548 case DATA_TYPE_UINT32:
1549 case DATA_TYPE_INT64:
1550 case DATA_TYPE_UINT64:
1551 case DATA_TYPE_HRTIME:
1552 case DATA_TYPE_STRING:
1553 case DATA_TYPE_NVLIST:
1554 #if !defined(_KERNEL)
1555 case DATA_TYPE_DOUBLE:
1557 val = va_arg(ap, void *);
1558 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1561 case DATA_TYPE_BYTE_ARRAY:
1562 case DATA_TYPE_BOOLEAN_ARRAY:
1563 case DATA_TYPE_INT8_ARRAY:
1564 case DATA_TYPE_UINT8_ARRAY:
1565 case DATA_TYPE_INT16_ARRAY:
1566 case DATA_TYPE_UINT16_ARRAY:
1567 case DATA_TYPE_INT32_ARRAY:
1568 case DATA_TYPE_UINT32_ARRAY:
1569 case DATA_TYPE_INT64_ARRAY:
1570 case DATA_TYPE_UINT64_ARRAY:
1571 case DATA_TYPE_STRING_ARRAY:
1572 case DATA_TYPE_NVLIST_ARRAY:
1573 val = va_arg(ap, void *);
1574 nelem = va_arg(ap, uint_t *);
1575 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1582 if (ret == ENOENT && noentok)
1591 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1592 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1593 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1594 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1595 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1596 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1597 * code also supports "a.d[3]e[1]" syntax).
1599 * If 'ip' is non-NULL and the last name component is an array, return the
1600 * value of the "...[index]" array index in *ip. For an array reference that
1601 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1602 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1603 * inside the 'name' string where the syntax error was detected.
1606 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1607 nvpair_t **ret, int *ip, char **ep)
1618 *ip = -1; /* not indexed */
1622 if ((nvl == NULL) || (name == NULL))
1625 /* step through components of name */
1626 for (np = name; np && *np; np = sepp) {
1627 /* ensure unique names */
1628 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1631 /* skip white space */
1632 skip_whitespace(np);
1636 /* set 'sepp' to end of current component 'np' */
1638 sepp = strchr(np, sep);
1642 /* find start of next "[ index ]..." */
1643 idxp = strchr(np, '[');
1645 /* if sepp comes first, set idxp to NULL */
1646 if (sepp && idxp && (sepp < idxp))
1650 * At this point 'idxp' is set if there is an index
1651 * expected for the current component.
1654 /* set 'n' to length of current 'np' name component */
1657 /* keep sepp up to date for *ep use as we advance */
1658 skip_whitespace(idxp);
1661 /* determine the index value */
1662 #if defined(_KERNEL) && !defined(_BOOT)
1663 if (ddi_strtol(idxp, &idxep, 0, &idx))
1666 idx = strtol(idxp, &idxep, 0);
1671 /* keep sepp up to date for *ep use as we advance */
1674 /* skip white space index value and check for ']' */
1675 skip_whitespace(sepp);
1679 /* for embedded arrays, support C syntax: "a[1].b" */
1680 skip_whitespace(sepp);
1681 if (sep && (*sepp == sep))
1689 /* trim trailing whitespace by reducing length of 'np' */
1692 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1696 /* skip whitespace, and set sepp to NULL if complete */
1698 skip_whitespace(sepp);
1705 * o 'n' is the length of current 'np' component.
1706 * o 'idxp' is set if there was an index, and value 'idx'.
1707 * o 'sepp' is set to the beginning of the next component,
1708 * and set to NULL if we have no more components.
1710 * Search for nvpair with matching component name.
1712 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1713 nvp = nvlist_next_nvpair(nvl, nvp)) {
1715 /* continue if no match on name */
1716 if (strncmp(np, nvpair_name(nvp), n) ||
1717 (strlen(nvpair_name(nvp)) != n))
1720 /* if indexed, verify type is array oriented */
1721 if (idxp && !nvpair_type_is_array(nvp))
1725 * Full match found, return nvp and idx if this
1726 * was the last component.
1732 *ip = (int)idx; /* return index */
1733 return (0); /* found */
1737 * More components: current match must be
1738 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1739 * to support going deeper.
1741 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1742 nvl = EMBEDDED_NVL(nvp);
1744 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1745 (void) nvpair_value_nvlist_array(nvp,
1746 &nva, (uint_t *)&n);
1747 if ((n < 0) || (idx >= n))
1753 /* type does not support more levels */
1757 goto fail; /* 'name' not found */
1759 /* search for match of next component in embedded 'nvl' list */
1762 fail: if (ep && sepp)
1768 * Return pointer to nvpair with specified 'name'.
1771 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1773 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1777 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1778 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1781 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1782 const char *name, nvpair_t **ret, int *ip, char **ep)
1784 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1788 nvlist_exists(nvlist_t *nvl, const char *name)
1794 if (name == NULL || nvl == NULL ||
1795 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1798 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1799 nvp = &curr->nvi_nvp;
1801 if (strcmp(name, NVP_NAME(nvp)) == 0)
1809 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1811 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1815 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1817 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1821 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1823 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1827 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1829 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1833 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1835 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1839 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1841 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1845 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1847 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1851 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1853 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1857 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1859 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1863 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1865 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1868 #if !defined(_KERNEL)
1870 nvpair_value_double(nvpair_t *nvp, double *val)
1872 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1877 nvpair_value_string(nvpair_t *nvp, char **val)
1879 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1883 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1885 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1889 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1891 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1895 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1897 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1901 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1903 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1907 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1909 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1913 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1915 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1919 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1921 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1925 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1927 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1931 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1933 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1937 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1939 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1943 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1945 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1949 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1951 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1955 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1957 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1961 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1963 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1967 * Add specified pair to the list.
1970 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1972 if (nvl == NULL || nvp == NULL)
1975 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1976 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1980 * Merge the supplied nvlists and put the result in dst.
1981 * The merged list will contain all names specified in both lists,
1982 * the values are taken from nvl in the case of duplicates.
1983 * Return 0 on success.
1987 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1989 if (nvl == NULL || dst == NULL)
1993 return (nvlist_copy_pairs(nvl, dst));
1999 * Encoding related routines
2001 #define NVS_OP_ENCODE 0
2002 #define NVS_OP_DECODE 1
2003 #define NVS_OP_GETSIZE 2
2005 typedef struct nvs_ops nvs_ops_t;
2009 const nvs_ops_t *nvs_ops;
2015 * nvs operations are:
2017 * encoding / decoding of a nvlist header (nvlist_t)
2018 * calculates the size used for header and end detection
2021 * responsible for the first part of encoding / decoding of an nvpair
2022 * calculates the decoded size of an nvpair
2025 * second part of encoding / decoding of an nvpair
2028 * calculates the encoding size of an nvpair
2031 * encodes the end detection mark (zeros).
2034 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2035 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2036 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2037 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2038 int (*nvs_nvl_fini)(nvstream_t *);
2042 char nvh_encoding; /* nvs encoding method */
2043 char nvh_endian; /* nvs endian */
2044 char nvh_reserved1; /* reserved for future use */
2045 char nvh_reserved2; /* reserved for future use */
2049 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2051 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2055 * Walk nvpair in list and encode each nvpair
2057 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2058 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2061 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2065 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2072 * Get decoded size of next pair in stream, alloc
2073 * memory for nvpair_t, then decode the nvpair
2075 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2076 if (nvsize == 0) /* end of list */
2079 /* make sure len makes sense */
2080 if (nvsize < NVP_SIZE_CALC(1, 0))
2083 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2086 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2087 nvp_buf_free(nvl, nvp);
2091 if (i_validate_nvpair(nvp) != 0) {
2093 nvp_buf_free(nvl, nvp);
2097 nvp_buf_link(nvl, nvp);
2103 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2105 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2107 uint64_t nvsize = *buflen;
2111 * Get encoded size of nvpairs in nvlist
2113 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2114 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2117 if ((nvsize += size) > INT32_MAX)
2126 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2130 if (nvl->nvl_priv == 0)
2134 * Perform the operation, starting with header, then each nvpair
2136 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2139 switch (nvs->nvs_op) {
2141 err = nvs_encode_pairs(nvs, nvl);
2145 err = nvs_decode_pairs(nvs, nvl);
2148 case NVS_OP_GETSIZE:
2149 err = nvs_getsize_pairs(nvs, nvl, buflen);
2160 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2162 switch (nvs->nvs_op) {
2164 return (nvs_operation(nvs, embedded, NULL));
2166 case NVS_OP_DECODE: {
2170 if (embedded->nvl_version != NV_VERSION)
2173 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2176 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2178 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2179 nvlist_free(embedded);
2190 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2192 size_t nelem = NVP_NELEM(nvp);
2193 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2196 switch (nvs->nvs_op) {
2198 for (i = 0; i < nelem; i++)
2199 if (nvs_embedded(nvs, nvlp[i]) != 0)
2203 case NVS_OP_DECODE: {
2204 size_t len = nelem * sizeof (uint64_t);
2205 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2207 bzero(nvlp, len); /* don't trust packed data */
2208 for (i = 0; i < nelem; i++) {
2209 if (nvs_embedded(nvs, embedded) != 0) {
2214 nvlp[i] = embedded++;
2218 case NVS_OP_GETSIZE: {
2219 uint64_t nvsize = 0;
2221 for (i = 0; i < nelem; i++) {
2224 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2227 if ((nvsize += nvp_sz) > INT32_MAX)
2241 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2242 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2245 * Common routine for nvlist operations:
2246 * encode, decode, getsize (encoded size).
2249 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2255 #if BYTE_ORDER == _LITTLE_ENDIAN
2256 int host_endian = 1;
2258 int host_endian = 0;
2259 #endif /* _LITTLE_ENDIAN */
2260 nvs_header_t *nvh = (void *)buf;
2262 if (buflen == NULL || nvl == NULL ||
2263 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2266 nvs.nvs_op = nvs_op;
2269 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2270 * a buffer is allocated. The first 4 bytes in the buffer are
2271 * used for encoding method and host endian.
2275 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2278 nvh->nvh_encoding = encoding;
2279 nvh->nvh_endian = nvl_endian = host_endian;
2280 nvh->nvh_reserved1 = 0;
2281 nvh->nvh_reserved2 = 0;
2285 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2288 /* get method of encoding from first byte */
2289 encoding = nvh->nvh_encoding;
2290 nvl_endian = nvh->nvh_endian;
2293 case NVS_OP_GETSIZE:
2294 nvl_endian = host_endian;
2297 * add the size for encoding
2299 *buflen = sizeof (nvs_header_t);
2307 * Create an nvstream with proper encoding method
2310 case NV_ENCODE_NATIVE:
2312 * check endianness, in case we are unpacking
2315 if (nvl_endian != host_endian)
2317 err = nvs_native(&nvs, nvl, buf, buflen);
2320 err = nvs_xdr(&nvs, nvl, buf, buflen);
2331 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2333 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2337 * Pack nvlist into contiguous memory
2341 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2344 #if defined(_KERNEL) && !defined(_BOOT)
2345 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2346 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2348 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2353 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2361 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2365 return (nvlist_common(nvl, *bufp, buflen, encoding,
2369 * Here is a difficult situation:
2370 * 1. The nvlist has fixed allocator properties.
2371 * All other nvlist routines (like nvlist_add_*, ...) use
2373 * 2. When using nvlist_pack() the user can specify his own
2374 * allocator properties (e.g. by using KM_NOSLEEP).
2376 * We use the user specified properties (2). A clearer solution
2377 * will be to remove the kmflag from nvlist_pack(), but we will
2378 * not change the interface.
2380 nv_priv_init(&nvpriv, nva, 0);
2382 if (err = nvlist_size(nvl, &alloc_size, encoding))
2385 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2388 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2389 NVS_OP_ENCODE)) != 0) {
2390 nv_mem_free(&nvpriv, buf, alloc_size);
2392 *buflen = alloc_size;
2400 * Unpack buf into an nvlist_t
2404 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2406 #if defined(_KERNEL) && !defined(_BOOT)
2407 return (nvlist_xunpack(buf, buflen, nvlp,
2408 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2410 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2415 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2423 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2426 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2435 * Native encoding functions
2439 * This structure is used when decoding a packed nvpair in
2440 * the native format. n_base points to a buffer containing the
2441 * packed nvpair. n_end is a pointer to the end of the buffer.
2442 * (n_end actually points to the first byte past the end of the
2443 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2444 * It points to the current data that we are decoding.
2445 * The amount of data left in the buffer is equal to n_end - n_curr.
2446 * n_flag is used to recognize a packed embedded list.
2455 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2458 switch (nvs->nvs_op) {
2461 nvs->nvs_private = native;
2462 native->n_curr = native->n_base = buf;
2463 native->n_end = buf + buflen;
2467 case NVS_OP_GETSIZE:
2468 nvs->nvs_private = native;
2469 native->n_curr = native->n_base = native->n_end = NULL;
2479 nvs_native_destroy(nvstream_t *nvs)
2484 native_cp(nvstream_t *nvs, void *buf, size_t size)
2486 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2488 if (native->n_curr + size > native->n_end)
2492 * The bcopy() below eliminates alignment requirement
2493 * on the buffer (stream) and is preferred over direct access.
2495 switch (nvs->nvs_op) {
2497 bcopy(buf, native->n_curr, size);
2500 bcopy(native->n_curr, buf, size);
2506 native->n_curr += size;
2511 * operate on nvlist_t header
2514 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2516 nvs_native_t *native = nvs->nvs_private;
2518 switch (nvs->nvs_op) {
2522 return (0); /* packed embedded list */
2526 /* copy version and nvflag of the nvlist_t */
2527 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2528 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2533 case NVS_OP_GETSIZE:
2535 * if calculate for packed embedded list
2536 * 4 for end of the embedded list
2538 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2539 * and 4 for end of the entire list
2541 if (native->n_flag) {
2545 *size += 2 * sizeof (int32_t) + 4;
2556 nvs_native_nvl_fini(nvstream_t *nvs)
2558 if (nvs->nvs_op == NVS_OP_ENCODE) {
2559 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2561 * Add 4 zero bytes at end of nvlist. They are used
2562 * for end detection by the decode routine.
2564 if (native->n_curr + sizeof (int) > native->n_end)
2567 bzero(native->n_curr, sizeof (int));
2568 native->n_curr += sizeof (int);
2575 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2577 if (nvs->nvs_op == NVS_OP_ENCODE) {
2578 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2579 char *packed = (void *)
2580 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2582 * Null out the pointer that is meaningless in the packed
2583 * structure. The address may not be aligned, so we have
2586 bzero(packed + offsetof(nvlist_t, nvl_priv),
2587 sizeof(((nvlist_t *)NULL)->nvl_priv));
2590 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2594 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2596 if (nvs->nvs_op == NVS_OP_ENCODE) {
2597 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2598 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2599 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2602 * Null out pointers that are meaningless in the packed
2603 * structure. The addresses may not be aligned, so we have
2609 for (i = 0; i < NVP_NELEM(nvp); i++) {
2611 * Null out the pointer that is meaningless in the
2612 * packed structure. The address may not be aligned,
2613 * so we have to use bzero.
2615 bzero(value + offsetof(nvlist_t, nvl_priv),
2616 sizeof(((nvlist_t *)NULL)->nvl_priv));
2617 value += sizeof(nvlist_t);
2621 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2625 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2627 switch (nvs->nvs_op) {
2628 case NVS_OP_ENCODE: {
2629 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2630 uint64_t *strp = (void *)
2631 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2633 * Null out pointers that are meaningless in the packed
2634 * structure. The addresses may not be aligned, so we have
2637 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2640 case NVS_OP_DECODE: {
2641 char **strp = (void *)NVP_VALUE(nvp);
2642 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2645 for (i = 0; i < NVP_NELEM(nvp); i++) {
2647 buf += strlen(buf) + 1;
2655 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2662 * We do the initial bcopy of the data before we look at
2663 * the nvpair type, because when we're decoding, we won't
2664 * have the correct values for the pair until we do the bcopy.
2666 switch (nvs->nvs_op) {
2669 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2676 /* verify nvp_name_sz, check the name string length */
2677 if (i_validate_nvpair_name(nvp) != 0)
2680 type = NVP_TYPE(nvp);
2683 * Verify type and nelem and get the value size.
2684 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2685 * is the size of the string(s) excluded.
2687 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2690 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2694 case DATA_TYPE_NVLIST:
2695 ret = nvpair_native_embedded(nvs, nvp);
2697 case DATA_TYPE_NVLIST_ARRAY:
2698 ret = nvpair_native_embedded_array(nvs, nvp);
2700 case DATA_TYPE_STRING_ARRAY:
2701 nvpair_native_string_array(nvs, nvp);
2711 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2713 uint64_t nvp_sz = nvp->nvp_size;
2715 switch (NVP_TYPE(nvp)) {
2716 case DATA_TYPE_NVLIST: {
2719 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2725 case DATA_TYPE_NVLIST_ARRAY: {
2728 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2738 if (nvp_sz > INT32_MAX)
2747 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2749 switch (nvs->nvs_op) {
2751 return (nvs_native_nvp_op(nvs, nvp));
2753 case NVS_OP_DECODE: {
2754 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2757 /* try to read the size value from the stream */
2758 if (native->n_curr + sizeof (int32_t) > native->n_end)
2760 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2762 /* sanity check the size value */
2763 if (decode_len < 0 ||
2764 decode_len > native->n_end - native->n_curr)
2770 * If at the end of the stream then move the cursor
2771 * forward, otherwise nvpair_native_op() will read
2772 * the entire nvpair at the same cursor position.
2775 native->n_curr += sizeof (int32_t);
2786 static const nvs_ops_t nvs_native_ops = {
2790 nvs_native_nvp_size,
2795 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2797 nvs_native_t native;
2800 nvs->nvs_ops = &nvs_native_ops;
2802 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2803 *buflen - sizeof (nvs_header_t))) != 0)
2806 err = nvs_operation(nvs, nvl, buflen);
2808 nvs_native_destroy(nvs);
2814 * XDR encoding functions
2816 * An xdr packed nvlist is encoded as:
2818 * - encoding methode and host endian (4 bytes)
2819 * - nvl_version (4 bytes)
2820 * - nvl_nvflag (4 bytes)
2822 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2823 * - encoded size of the nvpair (4 bytes)
2824 * - decoded size of the nvpair (4 bytes)
2825 * - name string, (4 + sizeof(NV_ALIGN4(string))
2826 * a string is coded as size (4 bytes) and data
2827 * - data type (4 bytes)
2828 * - number of elements in the nvpair (4 bytes)
2831 * - 2 zero's for end of the entire list (8 bytes)
2834 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2836 /* xdr data must be 4 byte aligned */
2837 if ((ulong_t)buf % 4 != 0)
2840 switch (nvs->nvs_op) {
2842 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2843 nvs->nvs_private = xdr;
2846 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2847 nvs->nvs_private = xdr;
2849 case NVS_OP_GETSIZE:
2850 nvs->nvs_private = NULL;
2858 nvs_xdr_destroy(nvstream_t *nvs)
2860 switch (nvs->nvs_op) {
2863 xdr_destroy((XDR *)nvs->nvs_private);
2871 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2873 switch (nvs->nvs_op) {
2875 case NVS_OP_DECODE: {
2876 XDR *xdr = nvs->nvs_private;
2878 if (!xdr_int(xdr, &nvl->nvl_version) ||
2879 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2883 case NVS_OP_GETSIZE: {
2885 * 2 * 4 for nvl_version + nvl_nvflag
2886 * and 8 for end of the entire list
2898 nvs_xdr_nvl_fini(nvstream_t *nvs)
2900 if (nvs->nvs_op == NVS_OP_ENCODE) {
2901 XDR *xdr = nvs->nvs_private;
2904 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2912 * The format of xdr encoded nvpair is:
2913 * encode_size, decode_size, name string, data type, nelem, data
2916 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2920 char *buf_end = (char *)nvp + nvp->nvp_size;
2922 uint_t nelem, buflen;
2924 XDR *xdr = nvs->nvs_private;
2926 ASSERT(xdr != NULL && nvp != NULL);
2929 if ((buf = NVP_NAME(nvp)) >= buf_end)
2931 buflen = buf_end - buf;
2933 if (!xdr_string(xdr, &buf, buflen - 1))
2935 nvp->nvp_name_sz = strlen(buf) + 1;
2937 /* type and nelem */
2938 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2939 !xdr_int(xdr, &nvp->nvp_value_elem))
2942 type = NVP_TYPE(nvp);
2943 nelem = nvp->nvp_value_elem;
2946 * Verify type and nelem and get the value size.
2947 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2948 * is the size of the string(s) excluded.
2950 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2953 /* if there is no data to extract then return */
2958 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2960 buflen = buf_end - buf;
2962 if (buflen < value_sz)
2966 case DATA_TYPE_NVLIST:
2967 if (nvs_embedded(nvs, (void *)buf) == 0)
2971 case DATA_TYPE_NVLIST_ARRAY:
2972 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2976 case DATA_TYPE_BOOLEAN:
2980 case DATA_TYPE_BYTE:
2981 case DATA_TYPE_INT8:
2982 case DATA_TYPE_UINT8:
2983 ret = xdr_char(xdr, buf);
2986 case DATA_TYPE_INT16:
2987 ret = xdr_short(xdr, (void *)buf);
2990 case DATA_TYPE_UINT16:
2991 ret = xdr_u_short(xdr, (void *)buf);
2994 case DATA_TYPE_BOOLEAN_VALUE:
2995 case DATA_TYPE_INT32:
2996 ret = xdr_int(xdr, (void *)buf);
2999 case DATA_TYPE_UINT32:
3000 ret = xdr_u_int(xdr, (void *)buf);
3003 case DATA_TYPE_INT64:
3004 ret = xdr_longlong_t(xdr, (void *)buf);
3007 case DATA_TYPE_UINT64:
3008 ret = xdr_u_longlong_t(xdr, (void *)buf);
3011 case DATA_TYPE_HRTIME:
3013 * NOTE: must expose the definition of hrtime_t here
3015 ret = xdr_longlong_t(xdr, (void *)buf);
3017 #if !defined(_KERNEL)
3018 case DATA_TYPE_DOUBLE:
3019 ret = xdr_double(xdr, (void *)buf);
3022 case DATA_TYPE_STRING:
3023 ret = xdr_string(xdr, &buf, buflen - 1);
3026 case DATA_TYPE_BYTE_ARRAY:
3027 ret = xdr_opaque(xdr, buf, nelem);
3030 case DATA_TYPE_INT8_ARRAY:
3031 case DATA_TYPE_UINT8_ARRAY:
3032 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3033 (xdrproc_t)xdr_char);
3036 case DATA_TYPE_INT16_ARRAY:
3037 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3038 sizeof (int16_t), (xdrproc_t)xdr_short);
3041 case DATA_TYPE_UINT16_ARRAY:
3042 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3043 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3046 case DATA_TYPE_BOOLEAN_ARRAY:
3047 case DATA_TYPE_INT32_ARRAY:
3048 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3049 sizeof (int32_t), (xdrproc_t)xdr_int);
3052 case DATA_TYPE_UINT32_ARRAY:
3053 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3054 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3057 case DATA_TYPE_INT64_ARRAY:
3058 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3059 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3062 case DATA_TYPE_UINT64_ARRAY:
3063 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3064 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3067 case DATA_TYPE_STRING_ARRAY: {
3068 size_t len = nelem * sizeof (uint64_t);
3069 char **strp = (void *)buf;
3072 if (nvs->nvs_op == NVS_OP_DECODE)
3073 bzero(buf, len); /* don't trust packed data */
3075 for (i = 0; i < nelem; i++) {
3082 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3085 if (nvs->nvs_op == NVS_OP_DECODE)
3087 len = strlen(buf) + 1;
3096 return (ret == TRUE ? 0 : EFAULT);
3100 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3102 data_type_t type = NVP_TYPE(nvp);
3104 * encode_size + decode_size + name string size + data type + nelem
3105 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3107 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3110 case DATA_TYPE_BOOLEAN:
3113 case DATA_TYPE_BOOLEAN_VALUE:
3114 case DATA_TYPE_BYTE:
3115 case DATA_TYPE_INT8:
3116 case DATA_TYPE_UINT8:
3117 case DATA_TYPE_INT16:
3118 case DATA_TYPE_UINT16:
3119 case DATA_TYPE_INT32:
3120 case DATA_TYPE_UINT32:
3121 nvp_sz += 4; /* 4 is the minimum xdr unit */
3124 case DATA_TYPE_INT64:
3125 case DATA_TYPE_UINT64:
3126 case DATA_TYPE_HRTIME:
3127 #if !defined(_KERNEL)
3128 case DATA_TYPE_DOUBLE:
3133 case DATA_TYPE_STRING:
3134 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3137 case DATA_TYPE_BYTE_ARRAY:
3138 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3141 case DATA_TYPE_BOOLEAN_ARRAY:
3142 case DATA_TYPE_INT8_ARRAY:
3143 case DATA_TYPE_UINT8_ARRAY:
3144 case DATA_TYPE_INT16_ARRAY:
3145 case DATA_TYPE_UINT16_ARRAY:
3146 case DATA_TYPE_INT32_ARRAY:
3147 case DATA_TYPE_UINT32_ARRAY:
3148 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3151 case DATA_TYPE_INT64_ARRAY:
3152 case DATA_TYPE_UINT64_ARRAY:
3153 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3156 case DATA_TYPE_STRING_ARRAY: {
3158 char **strs = (void *)NVP_VALUE(nvp);
3160 for (i = 0; i < NVP_NELEM(nvp); i++)
3161 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3166 case DATA_TYPE_NVLIST:
3167 case DATA_TYPE_NVLIST_ARRAY: {
3169 int old_nvs_op = nvs->nvs_op;
3172 nvs->nvs_op = NVS_OP_GETSIZE;
3173 if (type == DATA_TYPE_NVLIST)
3174 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3176 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3177 nvs->nvs_op = old_nvs_op;
3190 if (nvp_sz > INT32_MAX)
3200 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3201 * the largest nvpair that could be encoded in the buffer.
3203 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3204 * The size of a xdr packed nvpair without any data is 5 words.
3206 * Using the size of the data directly as an estimate would be ok
3207 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3208 * then the actual nvpair has space for an array of pointers to index
3209 * the strings. These pointers are not encoded into the packed xdr buffer.
3211 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3212 * of length 0, then each string is endcoded in xdr format as a single word.
3213 * Therefore when expanded to an nvpair there will be 2.25 word used for
3214 * each string. (a int64_t allocated for pointer usage, and a single char
3215 * for the null termination.)
3217 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3219 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3220 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3221 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3222 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3223 (NVS_XDR_DATA_LEN(x) * 2) + \
3224 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3227 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3229 XDR *xdr = nvs->nvs_private;
3230 int32_t encode_len, decode_len;
3232 switch (nvs->nvs_op) {
3233 case NVS_OP_ENCODE: {
3236 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3239 decode_len = nvp->nvp_size;
3240 encode_len = nvsize;
3241 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3244 return (nvs_xdr_nvp_op(nvs, nvp));
3246 case NVS_OP_DECODE: {
3247 struct xdr_bytesrec bytesrec;
3249 /* get the encode and decode size */
3250 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3254 /* are we at the end of the stream? */
3258 /* sanity check the size parameter */
3259 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3262 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3273 static const struct nvs_ops nvs_xdr_ops = {
3282 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3287 nvs->nvs_ops = &nvs_xdr_ops;
3289 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3290 *buflen - sizeof (nvs_header_t))) != 0)
3293 err = nvs_operation(nvs, nvl, buflen);
3295 nvs_xdr_destroy(nvs);