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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/stropts.h>
30 #include <sys/debug.h>
31 #include <sys/isa_defs.h>
32 #include <sys/int_limits.h>
33 #include <sys/nvpair.h>
34 #include <sys/nvpair_impl.h>
35 #include <rpc/types.h>
38 #if defined(_KERNEL) && !defined(_BOOT)
39 #include <sys/varargs.h>
41 #include <sys/sunddi.h>
50 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
52 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
55 * nvpair.c - Provides kernel & userland interfaces for manipulating
70 * +--------------+ last i_nvp in list
71 * | nvpriv_t | +--------------------->
73 * +--+- nvp_list | | +------------+
74 * | | nvp_last -+--+ + nv_alloc_t |
75 * | | nvp_curr | |------------|
76 * | | nvp_nva -+----> | nva_ops |
77 * | | nvp_stat | | nva_arg |
78 * | +--------------+ +------------+
82 * +---------------------+ +-------------------+
83 * | i_nvp_t | +-->| i_nvp_t | +-->
84 * |---------------------| | |-------------------| |
85 * | nvi_next -+--+ | nvi_next -+--+
86 * | nvi_prev (NULL) | <----+ nvi_prev |
87 * | . . . . . . . . . . | | . . . . . . . . . |
88 * | nvp (nvpair_t) | | nvp (nvpair_t) |
89 * | - nvp_size | | - nvp_size |
90 * | - nvp_name_sz | | - nvp_name_sz |
91 * | - nvp_value_elem | | - nvp_value_elem |
92 * | - nvp_type | | - nvp_type |
93 * | - data ... | | - data ... |
94 * +---------------------+ +-------------------+
98 * +---------------------+ +---------------------+
99 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
100 * |---------------------| | | |---------------------|
101 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
102 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
103 * | . . . . . . . . . | | . . . . . . . . . |
104 * | nvp (nvpair_t) | | nvp (nvpair_t) |
105 * | - nvp_size | | - nvp_size |
106 * | - nvp_name_sz | | - nvp_name_sz |
107 * | - nvp_value_elem | | - nvp_value_elem |
108 * | - DATA_TYPE_NVLIST | | - nvp_type |
109 * | - data (embedded) | | - data ... |
110 * | nvlist name | +---------------------+
111 * | +--------------+ |
113 * | |--------------| |
114 * | | nvl_version | |
116 * | | nvl_priv --+---+---->
119 * | +--------------+ |
120 * +---------------------+
123 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
124 * allow value to be aligned on 8 byte boundary
126 * name_len is the length of the name string including the null terminator
129 #define NVP_SIZE_CALC(name_len, data_len) \
130 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
132 static int i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
);
133 static int nvlist_add_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
134 uint_t nelem
, const void *data
);
136 #define NV_STAT_EMBEDDED 0x1
137 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
138 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
140 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
141 #define NVPAIR2I_NVP(nvp) \
142 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
146 nv_alloc_init(nv_alloc_t
*nva
, const nv_alloc_ops_t
*nvo
, /* args */ ...)
154 va_start(valist
, nvo
);
155 if (nva
->nva_ops
->nv_ao_init
!= NULL
)
156 err
= nva
->nva_ops
->nv_ao_init(nva
, valist
);
163 nv_alloc_reset(nv_alloc_t
*nva
)
165 if (nva
->nva_ops
->nv_ao_reset
!= NULL
)
166 nva
->nva_ops
->nv_ao_reset(nva
);
170 nv_alloc_fini(nv_alloc_t
*nva
)
172 if (nva
->nva_ops
->nv_ao_fini
!= NULL
)
173 nva
->nva_ops
->nv_ao_fini(nva
);
177 nvlist_lookup_nv_alloc(nvlist_t
*nvl
)
182 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
185 return (priv
->nvp_nva
);
189 nv_mem_zalloc(nvpriv_t
*nvp
, size_t size
)
191 nv_alloc_t
*nva
= nvp
->nvp_nva
;
194 if ((buf
= nva
->nva_ops
->nv_ao_alloc(nva
, size
)) != NULL
)
201 nv_mem_free(nvpriv_t
*nvp
, void *buf
, size_t size
)
203 nv_alloc_t
*nva
= nvp
->nvp_nva
;
205 nva
->nva_ops
->nv_ao_free(nva
, buf
, size
);
209 nv_priv_init(nvpriv_t
*priv
, nv_alloc_t
*nva
, uint32_t stat
)
211 bzero(priv
, sizeof (nvpriv_t
));
214 priv
->nvp_stat
= stat
;
218 nv_priv_alloc(nv_alloc_t
*nva
)
223 * nv_mem_alloc() cannot called here because it needs the priv
226 if ((priv
= nva
->nva_ops
->nv_ao_alloc(nva
, sizeof (nvpriv_t
))) == NULL
)
229 nv_priv_init(priv
, nva
, 0);
235 * Embedded lists need their own nvpriv_t's. We create a new
236 * nvpriv_t using the parameters and allocator from the parent
240 nv_priv_alloc_embedded(nvpriv_t
*priv
)
244 if ((emb_priv
= nv_mem_zalloc(priv
, sizeof (nvpriv_t
))) == NULL
)
247 nv_priv_init(emb_priv
, priv
->nvp_nva
, NV_STAT_EMBEDDED
);
253 nvlist_init(nvlist_t
*nvl
, uint32_t nvflag
, nvpriv_t
*priv
)
255 nvl
->nvl_version
= NV_VERSION
;
256 nvl
->nvl_nvflag
= nvflag
& (NV_UNIQUE_NAME
|NV_UNIQUE_NAME_TYPE
);
257 nvl
->nvl_priv
= (uint64_t)(uintptr_t)priv
;
263 * nvlist_alloc - Allocate nvlist.
267 nvlist_alloc(nvlist_t
**nvlp
, uint_t nvflag
, int kmflag
)
269 #if defined(_KERNEL) && !defined(_BOOT)
270 return (nvlist_xalloc(nvlp
, nvflag
,
271 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
273 return (nvlist_xalloc(nvlp
, nvflag
, nv_alloc_nosleep
));
278 nvlist_xalloc(nvlist_t
**nvlp
, uint_t nvflag
, nv_alloc_t
*nva
)
282 if (nvlp
== NULL
|| nva
== NULL
)
285 if ((priv
= nv_priv_alloc(nva
)) == NULL
)
288 if ((*nvlp
= nv_mem_zalloc(priv
,
289 NV_ALIGN(sizeof (nvlist_t
)))) == NULL
) {
290 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
294 nvlist_init(*nvlp
, nvflag
, priv
);
300 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
303 nvp_buf_alloc(nvlist_t
*nvl
, size_t len
)
305 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
311 * Allocate the buffer
313 nvsize
= len
+ offsetof(i_nvp_t
, nvi_nvp
);
315 if ((buf
= nv_mem_zalloc(priv
, nvsize
)) == NULL
)
325 * nvp_buf_free - de-Allocate an i_nvp_t.
328 nvp_buf_free(nvlist_t
*nvl
, nvpair_t
*nvp
)
330 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
331 size_t nvsize
= nvp
->nvp_size
+ offsetof(i_nvp_t
, nvi_nvp
);
333 nv_mem_free(priv
, NVPAIR2I_NVP(nvp
), nvsize
);
337 * nvp_buf_link - link a new nv pair into the nvlist.
340 nvp_buf_link(nvlist_t
*nvl
, nvpair_t
*nvp
)
342 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
343 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
345 /* Put element at end of nvlist */
346 if (priv
->nvp_list
== NULL
) {
347 priv
->nvp_list
= priv
->nvp_last
= curr
;
349 curr
->nvi_prev
= priv
->nvp_last
;
350 priv
->nvp_last
->nvi_next
= curr
;
351 priv
->nvp_last
= curr
;
356 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
359 nvp_buf_unlink(nvlist_t
*nvl
, nvpair_t
*nvp
)
361 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
362 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
365 * protect nvlist_next_nvpair() against walking on freed memory.
367 if (priv
->nvp_curr
== curr
)
368 priv
->nvp_curr
= curr
->nvi_next
;
370 if (curr
== priv
->nvp_list
)
371 priv
->nvp_list
= curr
->nvi_next
;
373 curr
->nvi_prev
->nvi_next
= curr
->nvi_next
;
375 if (curr
== priv
->nvp_last
)
376 priv
->nvp_last
= curr
->nvi_prev
;
378 curr
->nvi_next
->nvi_prev
= curr
->nvi_prev
;
382 * take a nvpair type and number of elements and make sure the are valid
385 i_validate_type_nelem(data_type_t type
, uint_t nelem
)
388 case DATA_TYPE_BOOLEAN
:
392 case DATA_TYPE_BOOLEAN_VALUE
:
395 case DATA_TYPE_UINT8
:
396 case DATA_TYPE_INT16
:
397 case DATA_TYPE_UINT16
:
398 case DATA_TYPE_INT32
:
399 case DATA_TYPE_UINT32
:
400 case DATA_TYPE_INT64
:
401 case DATA_TYPE_UINT64
:
402 case DATA_TYPE_STRING
:
403 case DATA_TYPE_HRTIME
:
404 case DATA_TYPE_NVLIST
:
405 #if !defined(_KERNEL)
406 case DATA_TYPE_DOUBLE
:
411 case DATA_TYPE_BOOLEAN_ARRAY
:
412 case DATA_TYPE_BYTE_ARRAY
:
413 case DATA_TYPE_INT8_ARRAY
:
414 case DATA_TYPE_UINT8_ARRAY
:
415 case DATA_TYPE_INT16_ARRAY
:
416 case DATA_TYPE_UINT16_ARRAY
:
417 case DATA_TYPE_INT32_ARRAY
:
418 case DATA_TYPE_UINT32_ARRAY
:
419 case DATA_TYPE_INT64_ARRAY
:
420 case DATA_TYPE_UINT64_ARRAY
:
421 case DATA_TYPE_STRING_ARRAY
:
422 case DATA_TYPE_NVLIST_ARRAY
:
423 /* we allow arrays with 0 elements */
432 * Verify nvp_name_sz and check the name string length.
435 i_validate_nvpair_name(nvpair_t
*nvp
)
437 if ((nvp
->nvp_name_sz
<= 0) ||
438 (nvp
->nvp_size
< NVP_SIZE_CALC(nvp
->nvp_name_sz
, 0)))
441 /* verify the name string, make sure its terminated */
442 if (NVP_NAME(nvp
)[nvp
->nvp_name_sz
- 1] != '\0')
445 return (strlen(NVP_NAME(nvp
)) == nvp
->nvp_name_sz
- 1 ? 0 : EFAULT
);
449 i_validate_nvpair_value(data_type_t type
, uint_t nelem
, const void *data
)
452 case DATA_TYPE_BOOLEAN_VALUE
:
453 if (*(boolean_t
*)data
!= B_TRUE
&&
454 *(boolean_t
*)data
!= B_FALSE
)
457 case DATA_TYPE_BOOLEAN_ARRAY
: {
460 for (i
= 0; i
< nelem
; i
++)
461 if (((boolean_t
*)data
)[i
] != B_TRUE
&&
462 ((boolean_t
*)data
)[i
] != B_FALSE
)
474 * This function takes a pointer to what should be a nvpair and it's size
475 * and then verifies that all the nvpair fields make sense and can be
476 * trusted. This function is used when decoding packed nvpairs.
479 i_validate_nvpair(nvpair_t
*nvp
)
481 data_type_t type
= NVP_TYPE(nvp
);
484 /* verify nvp_name_sz, check the name string length */
485 if (i_validate_nvpair_name(nvp
) != 0)
488 if (i_validate_nvpair_value(type
, NVP_NELEM(nvp
), NVP_VALUE(nvp
)) != 0)
492 * verify nvp_type, nvp_value_elem, and also possibly
493 * verify string values and get the value size.
495 size2
= i_get_value_size(type
, NVP_VALUE(nvp
), NVP_NELEM(nvp
));
496 size1
= nvp
->nvp_size
- NVP_VALOFF(nvp
);
497 if (size2
< 0 || size1
!= NV_ALIGN(size2
))
504 nvlist_copy_pairs(nvlist_t
*snvl
, nvlist_t
*dnvl
)
509 if ((priv
= (nvpriv_t
*)(uintptr_t)snvl
->nvl_priv
) == NULL
)
512 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
513 nvpair_t
*nvp
= &curr
->nvi_nvp
;
516 if ((err
= nvlist_add_common(dnvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
517 NVP_NELEM(nvp
), NVP_VALUE(nvp
))) != 0)
525 * Frees all memory allocated for an nvpair (like embedded lists) with
526 * the exception of the nvpair buffer itself.
529 nvpair_free(nvpair_t
*nvp
)
531 switch (NVP_TYPE(nvp
)) {
532 case DATA_TYPE_NVLIST
:
533 nvlist_free(EMBEDDED_NVL(nvp
));
535 case DATA_TYPE_NVLIST_ARRAY
: {
536 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
539 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
541 nvlist_free(nvlp
[i
]);
550 * nvlist_free - free an unpacked nvlist
553 nvlist_free(nvlist_t
*nvl
)
559 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
563 * Unpacked nvlist are linked through i_nvp_t
565 curr
= priv
->nvp_list
;
566 while (curr
!= NULL
) {
567 nvpair_t
*nvp
= &curr
->nvi_nvp
;
568 curr
= curr
->nvi_next
;
571 nvp_buf_free(nvl
, nvp
);
574 if (!(priv
->nvp_stat
& NV_STAT_EMBEDDED
))
575 nv_mem_free(priv
, nvl
, NV_ALIGN(sizeof (nvlist_t
)));
579 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
583 nvlist_contains_nvp(nvlist_t
*nvl
, nvpair_t
*nvp
)
585 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
591 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
592 if (&curr
->nvi_nvp
== nvp
)
599 * Make a copy of nvlist
603 nvlist_dup(nvlist_t
*nvl
, nvlist_t
**nvlp
, int kmflag
)
605 #if defined(_KERNEL) && !defined(_BOOT)
606 return (nvlist_xdup(nvl
, nvlp
,
607 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
609 return (nvlist_xdup(nvl
, nvlp
, nv_alloc_nosleep
));
614 nvlist_xdup(nvlist_t
*nvl
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
619 if (nvl
== NULL
|| nvlp
== NULL
)
622 if ((err
= nvlist_xalloc(&ret
, nvl
->nvl_nvflag
, nva
)) != 0)
625 if ((err
= nvlist_copy_pairs(nvl
, ret
)) != 0)
634 * Remove all with matching name
637 nvlist_remove_all(nvlist_t
*nvl
, const char *name
)
643 if (nvl
== NULL
|| name
== NULL
||
644 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
647 curr
= priv
->nvp_list
;
648 while (curr
!= NULL
) {
649 nvpair_t
*nvp
= &curr
->nvi_nvp
;
651 curr
= curr
->nvi_next
;
652 if (strcmp(name
, NVP_NAME(nvp
)) != 0)
655 nvp_buf_unlink(nvl
, nvp
);
657 nvp_buf_free(nvl
, nvp
);
666 * Remove first one with matching name and type
669 nvlist_remove(nvlist_t
*nvl
, const char *name
, data_type_t type
)
674 if (nvl
== NULL
|| name
== NULL
||
675 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
678 curr
= priv
->nvp_list
;
679 while (curr
!= NULL
) {
680 nvpair_t
*nvp
= &curr
->nvi_nvp
;
682 if (strcmp(name
, NVP_NAME(nvp
)) == 0 && NVP_TYPE(nvp
) == type
) {
683 nvp_buf_unlink(nvl
, nvp
);
685 nvp_buf_free(nvl
, nvp
);
689 curr
= curr
->nvi_next
;
696 * This function calculates the size of an nvpair value.
698 * The data argument controls the behavior in case of the data types
699 * DATA_TYPE_STRING and
700 * DATA_TYPE_STRING_ARRAY
701 * Is data == NULL then the size of the string(s) is excluded.
704 i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
)
708 if (i_validate_type_nelem(type
, nelem
) != 0)
711 /* Calculate required size for holding value */
713 case DATA_TYPE_BOOLEAN
:
716 case DATA_TYPE_BOOLEAN_VALUE
:
717 value_sz
= sizeof (boolean_t
);
720 value_sz
= sizeof (uchar_t
);
723 value_sz
= sizeof (int8_t);
725 case DATA_TYPE_UINT8
:
726 value_sz
= sizeof (uint8_t);
728 case DATA_TYPE_INT16
:
729 value_sz
= sizeof (int16_t);
731 case DATA_TYPE_UINT16
:
732 value_sz
= sizeof (uint16_t);
734 case DATA_TYPE_INT32
:
735 value_sz
= sizeof (int32_t);
737 case DATA_TYPE_UINT32
:
738 value_sz
= sizeof (uint32_t);
740 case DATA_TYPE_INT64
:
741 value_sz
= sizeof (int64_t);
743 case DATA_TYPE_UINT64
:
744 value_sz
= sizeof (uint64_t);
746 #if !defined(_KERNEL)
747 case DATA_TYPE_DOUBLE
:
748 value_sz
= sizeof (double);
751 case DATA_TYPE_STRING
:
755 value_sz
= strlen(data
) + 1;
757 case DATA_TYPE_BOOLEAN_ARRAY
:
758 value_sz
= (uint64_t)nelem
* sizeof (boolean_t
);
760 case DATA_TYPE_BYTE_ARRAY
:
761 value_sz
= (uint64_t)nelem
* sizeof (uchar_t
);
763 case DATA_TYPE_INT8_ARRAY
:
764 value_sz
= (uint64_t)nelem
* sizeof (int8_t);
766 case DATA_TYPE_UINT8_ARRAY
:
767 value_sz
= (uint64_t)nelem
* sizeof (uint8_t);
769 case DATA_TYPE_INT16_ARRAY
:
770 value_sz
= (uint64_t)nelem
* sizeof (int16_t);
772 case DATA_TYPE_UINT16_ARRAY
:
773 value_sz
= (uint64_t)nelem
* sizeof (uint16_t);
775 case DATA_TYPE_INT32_ARRAY
:
776 value_sz
= (uint64_t)nelem
* sizeof (int32_t);
778 case DATA_TYPE_UINT32_ARRAY
:
779 value_sz
= (uint64_t)nelem
* sizeof (uint32_t);
781 case DATA_TYPE_INT64_ARRAY
:
782 value_sz
= (uint64_t)nelem
* sizeof (int64_t);
784 case DATA_TYPE_UINT64_ARRAY
:
785 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
787 case DATA_TYPE_STRING_ARRAY
:
788 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
791 char *const *strs
= data
;
794 /* no alignment requirement for strings */
795 for (i
= 0; i
< nelem
; i
++) {
798 value_sz
+= strlen(strs
[i
]) + 1;
802 case DATA_TYPE_HRTIME
:
803 value_sz
= sizeof (hrtime_t
);
805 case DATA_TYPE_NVLIST
:
806 value_sz
= NV_ALIGN(sizeof (nvlist_t
));
808 case DATA_TYPE_NVLIST_ARRAY
:
809 value_sz
= (uint64_t)nelem
* sizeof (uint64_t) +
810 (uint64_t)nelem
* NV_ALIGN(sizeof (nvlist_t
));
816 return (value_sz
> INT32_MAX
? -1 : (int)value_sz
);
820 nvlist_copy_embedded(nvlist_t
*nvl
, nvlist_t
*onvl
, nvlist_t
*emb_nvl
)
825 if ((priv
= nv_priv_alloc_embedded((nvpriv_t
*)(uintptr_t)
826 nvl
->nvl_priv
)) == NULL
)
829 nvlist_init(emb_nvl
, onvl
->nvl_nvflag
, priv
);
831 if ((err
= nvlist_copy_pairs(onvl
, emb_nvl
)) != 0) {
832 nvlist_free(emb_nvl
);
833 emb_nvl
->nvl_priv
= 0;
840 * nvlist_add_common - Add new <name,value> pair to nvlist
843 nvlist_add_common(nvlist_t
*nvl
, const char *name
,
844 data_type_t type
, uint_t nelem
, const void *data
)
849 int nvp_sz
, name_sz
, value_sz
;
852 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
855 if (nelem
!= 0 && data
== NULL
)
859 * Verify type and nelem and get the value size.
860 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
861 * is the size of the string(s) included.
863 if ((value_sz
= i_get_value_size(type
, data
, nelem
)) < 0)
866 if (i_validate_nvpair_value(type
, nelem
, data
) != 0)
870 * If we're adding an nvlist or nvlist array, ensure that we are not
871 * adding the input nvlist to itself, which would cause recursion,
872 * and ensure that no NULL nvlist pointers are present.
875 case DATA_TYPE_NVLIST
:
876 if (data
== nvl
|| data
== NULL
)
879 case DATA_TYPE_NVLIST_ARRAY
: {
880 nvlist_t
**onvlp
= (nvlist_t
**)data
;
881 for (i
= 0; i
< nelem
; i
++) {
882 if (onvlp
[i
] == nvl
|| onvlp
[i
] == NULL
)
891 /* calculate sizes of the nvpair elements and the nvpair itself */
892 name_sz
= strlen(name
) + 1;
894 nvp_sz
= NVP_SIZE_CALC(name_sz
, value_sz
);
896 if ((nvp
= nvp_buf_alloc(nvl
, nvp_sz
)) == NULL
)
899 ASSERT(nvp
->nvp_size
== nvp_sz
);
900 nvp
->nvp_name_sz
= name_sz
;
901 nvp
->nvp_value_elem
= nelem
;
902 nvp
->nvp_type
= type
;
903 bcopy(name
, NVP_NAME(nvp
), name_sz
);
906 case DATA_TYPE_BOOLEAN
:
908 case DATA_TYPE_STRING_ARRAY
: {
909 char *const *strs
= data
;
910 char *buf
= NVP_VALUE(nvp
);
911 char **cstrs
= (void *)buf
;
913 /* skip pre-allocated space for pointer array */
914 buf
+= nelem
* sizeof (uint64_t);
915 for (i
= 0; i
< nelem
; i
++) {
916 int slen
= strlen(strs
[i
]) + 1;
917 bcopy(strs
[i
], buf
, slen
);
923 case DATA_TYPE_NVLIST
: {
924 nvlist_t
*nnvl
= EMBEDDED_NVL(nvp
);
925 nvlist_t
*onvl
= (nvlist_t
*)data
;
927 if ((err
= nvlist_copy_embedded(nvl
, onvl
, nnvl
)) != 0) {
928 nvp_buf_free(nvl
, nvp
);
933 case DATA_TYPE_NVLIST_ARRAY
: {
934 nvlist_t
**onvlp
= (nvlist_t
**)data
;
935 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
936 nvlist_t
*embedded
= (nvlist_t
*)
937 ((uintptr_t)nvlp
+ nelem
* sizeof (uint64_t));
939 for (i
= 0; i
< nelem
; i
++) {
940 if ((err
= nvlist_copy_embedded(nvl
,
941 onvlp
[i
], embedded
)) != 0) {
943 * Free any successfully created lists
946 nvp_buf_free(nvl
, nvp
);
950 nvlp
[i
] = embedded
++;
955 bcopy(data
, NVP_VALUE(nvp
), value_sz
);
958 /* if unique name, remove before add */
959 if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME
)
960 (void) nvlist_remove_all(nvl
, name
);
961 else if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME_TYPE
)
962 (void) nvlist_remove(nvl
, name
, type
);
964 nvp_buf_link(nvl
, nvp
);
970 nvlist_add_boolean(nvlist_t
*nvl
, const char *name
)
972 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN
, 0, NULL
));
976 nvlist_add_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t val
)
978 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_VALUE
, 1, &val
));
982 nvlist_add_byte(nvlist_t
*nvl
, const char *name
, uchar_t val
)
984 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE
, 1, &val
));
988 nvlist_add_int8(nvlist_t
*nvl
, const char *name
, int8_t val
)
990 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8
, 1, &val
));
994 nvlist_add_uint8(nvlist_t
*nvl
, const char *name
, uint8_t val
)
996 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8
, 1, &val
));
1000 nvlist_add_int16(nvlist_t
*nvl
, const char *name
, int16_t val
)
1002 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16
, 1, &val
));
1006 nvlist_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
1008 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16
, 1, &val
));
1012 nvlist_add_int32(nvlist_t
*nvl
, const char *name
, int32_t val
)
1014 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32
, 1, &val
));
1018 nvlist_add_uint32(nvlist_t
*nvl
, const char *name
, uint32_t val
)
1020 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32
, 1, &val
));
1024 nvlist_add_int64(nvlist_t
*nvl
, const char *name
, int64_t val
)
1026 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64
, 1, &val
));
1030 nvlist_add_uint64(nvlist_t
*nvl
, const char *name
, uint64_t val
)
1032 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64
, 1, &val
));
1035 #if !defined(_KERNEL)
1037 nvlist_add_double(nvlist_t
*nvl
, const char *name
, double val
)
1039 return (nvlist_add_common(nvl
, name
, DATA_TYPE_DOUBLE
, 1, &val
));
1044 nvlist_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
1046 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING
, 1, (void *)val
));
1050 nvlist_add_boolean_array(nvlist_t
*nvl
, const char *name
,
1051 boolean_t
*a
, uint_t n
)
1053 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1057 nvlist_add_byte_array(nvlist_t
*nvl
, const char *name
, uchar_t
*a
, uint_t n
)
1059 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1063 nvlist_add_int8_array(nvlist_t
*nvl
, const char *name
, int8_t *a
, uint_t n
)
1065 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1069 nvlist_add_uint8_array(nvlist_t
*nvl
, const char *name
, uint8_t *a
, uint_t n
)
1071 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1075 nvlist_add_int16_array(nvlist_t
*nvl
, const char *name
, int16_t *a
, uint_t n
)
1077 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1081 nvlist_add_uint16_array(nvlist_t
*nvl
, const char *name
, uint16_t *a
, uint_t n
)
1083 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1087 nvlist_add_int32_array(nvlist_t
*nvl
, const char *name
, int32_t *a
, uint_t n
)
1089 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1093 nvlist_add_uint32_array(nvlist_t
*nvl
, const char *name
, uint32_t *a
, uint_t n
)
1095 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1099 nvlist_add_int64_array(nvlist_t
*nvl
, const char *name
, int64_t *a
, uint_t n
)
1101 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1105 nvlist_add_uint64_array(nvlist_t
*nvl
, const char *name
, uint64_t *a
, uint_t n
)
1107 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1111 nvlist_add_string_array(nvlist_t
*nvl
, const char *name
,
1112 char *const *a
, uint_t n
)
1114 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1118 nvlist_add_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t val
)
1120 return (nvlist_add_common(nvl
, name
, DATA_TYPE_HRTIME
, 1, &val
));
1124 nvlist_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
1126 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST
, 1, val
));
1130 nvlist_add_nvlist_array(nvlist_t
*nvl
, const char *name
, nvlist_t
**a
, uint_t n
)
1132 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1135 /* reading name-value pairs */
1137 nvlist_next_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
1143 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1146 curr
= NVPAIR2I_NVP(nvp
);
1149 * Ensure that nvp is a valid nvpair on this nvlist.
1150 * NB: nvp_curr is used only as a hint so that we don't always
1151 * have to walk the list to determine if nvp is still on the list.
1154 curr
= priv
->nvp_list
;
1155 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1156 curr
= curr
->nvi_next
;
1160 priv
->nvp_curr
= curr
;
1162 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1166 nvpair_name(nvpair_t
*nvp
)
1168 return (NVP_NAME(nvp
));
1172 nvpair_type(nvpair_t
*nvp
)
1174 return (NVP_TYPE(nvp
));
1178 nvpair_type_is_array(nvpair_t
*nvp
)
1180 data_type_t type
= NVP_TYPE(nvp
);
1182 if ((type
== DATA_TYPE_BYTE_ARRAY
) ||
1183 (type
== DATA_TYPE_UINT8_ARRAY
) ||
1184 (type
== DATA_TYPE_INT16_ARRAY
) ||
1185 (type
== DATA_TYPE_UINT16_ARRAY
) ||
1186 (type
== DATA_TYPE_INT32_ARRAY
) ||
1187 (type
== DATA_TYPE_UINT32_ARRAY
) ||
1188 (type
== DATA_TYPE_INT64_ARRAY
) ||
1189 (type
== DATA_TYPE_UINT64_ARRAY
) ||
1190 (type
== DATA_TYPE_BOOLEAN_ARRAY
) ||
1191 (type
== DATA_TYPE_STRING_ARRAY
) ||
1192 (type
== DATA_TYPE_NVLIST_ARRAY
))
1199 nvpair_value_common(nvpair_t
*nvp
, data_type_t type
, uint_t
*nelem
, void *data
)
1201 if (nvp
== NULL
|| nvpair_type(nvp
) != type
)
1205 * For non-array types, we copy the data.
1206 * For array types (including string), we set a pointer.
1209 case DATA_TYPE_BOOLEAN
:
1214 case DATA_TYPE_BOOLEAN_VALUE
:
1215 case DATA_TYPE_BYTE
:
1216 case DATA_TYPE_INT8
:
1217 case DATA_TYPE_UINT8
:
1218 case DATA_TYPE_INT16
:
1219 case DATA_TYPE_UINT16
:
1220 case DATA_TYPE_INT32
:
1221 case DATA_TYPE_UINT32
:
1222 case DATA_TYPE_INT64
:
1223 case DATA_TYPE_UINT64
:
1224 case DATA_TYPE_HRTIME
:
1225 #if !defined(_KERNEL)
1226 case DATA_TYPE_DOUBLE
:
1230 bcopy(NVP_VALUE(nvp
), data
,
1231 (size_t)i_get_value_size(type
, NULL
, 1));
1236 case DATA_TYPE_NVLIST
:
1237 case DATA_TYPE_STRING
:
1240 *(void **)data
= (void *)NVP_VALUE(nvp
);
1245 case DATA_TYPE_BOOLEAN_ARRAY
:
1246 case DATA_TYPE_BYTE_ARRAY
:
1247 case DATA_TYPE_INT8_ARRAY
:
1248 case DATA_TYPE_UINT8_ARRAY
:
1249 case DATA_TYPE_INT16_ARRAY
:
1250 case DATA_TYPE_UINT16_ARRAY
:
1251 case DATA_TYPE_INT32_ARRAY
:
1252 case DATA_TYPE_UINT32_ARRAY
:
1253 case DATA_TYPE_INT64_ARRAY
:
1254 case DATA_TYPE_UINT64_ARRAY
:
1255 case DATA_TYPE_STRING_ARRAY
:
1256 case DATA_TYPE_NVLIST_ARRAY
:
1257 if (nelem
== NULL
|| data
== NULL
)
1259 if ((*nelem
= NVP_NELEM(nvp
)) != 0)
1260 *(void **)data
= (void *)NVP_VALUE(nvp
);
1262 *(void **)data
= NULL
;
1273 nvlist_lookup_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
1274 uint_t
*nelem
, void *data
)
1280 if (name
== NULL
|| nvl
== NULL
||
1281 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1284 if (!(nvl
->nvl_nvflag
& (NV_UNIQUE_NAME
| NV_UNIQUE_NAME_TYPE
)))
1287 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
1288 nvp
= &curr
->nvi_nvp
;
1290 if (strcmp(name
, NVP_NAME(nvp
)) == 0 && NVP_TYPE(nvp
) == type
)
1291 return (nvpair_value_common(nvp
, type
, nelem
, data
));
1298 nvlist_lookup_boolean(nvlist_t
*nvl
, const char *name
)
1300 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BOOLEAN
, NULL
, NULL
));
1304 nvlist_lookup_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t
*val
)
1306 return (nvlist_lookup_common(nvl
, name
,
1307 DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1311 nvlist_lookup_byte(nvlist_t
*nvl
, const char *name
, uchar_t
*val
)
1313 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE
, NULL
, val
));
1317 nvlist_lookup_int8(nvlist_t
*nvl
, const char *name
, int8_t *val
)
1319 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8
, NULL
, val
));
1323 nvlist_lookup_uint8(nvlist_t
*nvl
, const char *name
, uint8_t *val
)
1325 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8
, NULL
, val
));
1329 nvlist_lookup_int16(nvlist_t
*nvl
, const char *name
, int16_t *val
)
1331 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16
, NULL
, val
));
1335 nvlist_lookup_uint16(nvlist_t
*nvl
, const char *name
, uint16_t *val
)
1337 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16
, NULL
, val
));
1341 nvlist_lookup_int32(nvlist_t
*nvl
, const char *name
, int32_t *val
)
1343 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32
, NULL
, val
));
1347 nvlist_lookup_uint32(nvlist_t
*nvl
, const char *name
, uint32_t *val
)
1349 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32
, NULL
, val
));
1353 nvlist_lookup_int64(nvlist_t
*nvl
, const char *name
, int64_t *val
)
1355 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64
, NULL
, val
));
1359 nvlist_lookup_uint64(nvlist_t
*nvl
, const char *name
, uint64_t *val
)
1361 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64
, NULL
, val
));
1364 #if !defined(_KERNEL)
1366 nvlist_lookup_double(nvlist_t
*nvl
, const char *name
, double *val
)
1368 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_DOUBLE
, NULL
, val
));
1373 nvlist_lookup_string(nvlist_t
*nvl
, const char *name
, char **val
)
1375 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING
, NULL
, val
));
1379 nvlist_lookup_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
**val
)
1381 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST
, NULL
, val
));
1385 nvlist_lookup_boolean_array(nvlist_t
*nvl
, const char *name
,
1386 boolean_t
**a
, uint_t
*n
)
1388 return (nvlist_lookup_common(nvl
, name
,
1389 DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1393 nvlist_lookup_byte_array(nvlist_t
*nvl
, const char *name
,
1394 uchar_t
**a
, uint_t
*n
)
1396 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1400 nvlist_lookup_int8_array(nvlist_t
*nvl
, const char *name
, int8_t **a
, uint_t
*n
)
1402 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1406 nvlist_lookup_uint8_array(nvlist_t
*nvl
, const char *name
,
1407 uint8_t **a
, uint_t
*n
)
1409 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1413 nvlist_lookup_int16_array(nvlist_t
*nvl
, const char *name
,
1414 int16_t **a
, uint_t
*n
)
1416 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1420 nvlist_lookup_uint16_array(nvlist_t
*nvl
, const char *name
,
1421 uint16_t **a
, uint_t
*n
)
1423 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1427 nvlist_lookup_int32_array(nvlist_t
*nvl
, const char *name
,
1428 int32_t **a
, uint_t
*n
)
1430 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1434 nvlist_lookup_uint32_array(nvlist_t
*nvl
, const char *name
,
1435 uint32_t **a
, uint_t
*n
)
1437 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1441 nvlist_lookup_int64_array(nvlist_t
*nvl
, const char *name
,
1442 int64_t **a
, uint_t
*n
)
1444 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1448 nvlist_lookup_uint64_array(nvlist_t
*nvl
, const char *name
,
1449 uint64_t **a
, uint_t
*n
)
1451 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1455 nvlist_lookup_string_array(nvlist_t
*nvl
, const char *name
,
1456 char ***a
, uint_t
*n
)
1458 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1462 nvlist_lookup_nvlist_array(nvlist_t
*nvl
, const char *name
,
1463 nvlist_t
***a
, uint_t
*n
)
1465 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1469 nvlist_lookup_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t
*val
)
1471 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_HRTIME
, NULL
, val
));
1475 nvlist_lookup_pairs(nvlist_t
*nvl
, int flag
, ...)
1479 int noentok
= (flag
& NV_FLAG_NOENTOK
? 1 : 0);
1483 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
1488 switch (type
= va_arg(ap
, data_type_t
)) {
1489 case DATA_TYPE_BOOLEAN
:
1490 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, NULL
);
1493 case DATA_TYPE_BOOLEAN_VALUE
:
1494 case DATA_TYPE_BYTE
:
1495 case DATA_TYPE_INT8
:
1496 case DATA_TYPE_UINT8
:
1497 case DATA_TYPE_INT16
:
1498 case DATA_TYPE_UINT16
:
1499 case DATA_TYPE_INT32
:
1500 case DATA_TYPE_UINT32
:
1501 case DATA_TYPE_INT64
:
1502 case DATA_TYPE_UINT64
:
1503 case DATA_TYPE_HRTIME
:
1504 case DATA_TYPE_STRING
:
1505 case DATA_TYPE_NVLIST
:
1506 #if !defined(_KERNEL)
1507 case DATA_TYPE_DOUBLE
:
1509 val
= va_arg(ap
, void *);
1510 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, val
);
1513 case DATA_TYPE_BYTE_ARRAY
:
1514 case DATA_TYPE_BOOLEAN_ARRAY
:
1515 case DATA_TYPE_INT8_ARRAY
:
1516 case DATA_TYPE_UINT8_ARRAY
:
1517 case DATA_TYPE_INT16_ARRAY
:
1518 case DATA_TYPE_UINT16_ARRAY
:
1519 case DATA_TYPE_INT32_ARRAY
:
1520 case DATA_TYPE_UINT32_ARRAY
:
1521 case DATA_TYPE_INT64_ARRAY
:
1522 case DATA_TYPE_UINT64_ARRAY
:
1523 case DATA_TYPE_STRING_ARRAY
:
1524 case DATA_TYPE_NVLIST_ARRAY
:
1525 val
= va_arg(ap
, void *);
1526 nelem
= va_arg(ap
, uint_t
*);
1527 ret
= nvlist_lookup_common(nvl
, name
, type
, nelem
, val
);
1534 if (ret
== ENOENT
&& noentok
)
1543 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1544 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1545 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1546 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1547 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1548 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1549 * code also supports "a.d[3]e[1]" syntax).
1551 * If 'ip' is non-NULL and the last name component is an array, return the
1552 * value of the "...[index]" array index in *ip. For an array reference that
1553 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1554 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1555 * inside the 'name' string where the syntax error was detected.
1558 nvlist_lookup_nvpair_ei_sep(nvlist_t
*nvl
, const char *name
, const char sep
,
1559 nvpair_t
**ret
, int *ip
, char **ep
)
1570 *ip
= -1; /* not indexed */
1574 if ((nvl
== NULL
) || (name
== NULL
))
1577 /* step through components of name */
1578 for (np
= name
; np
&& *np
; np
= sepp
) {
1579 /* ensure unique names */
1580 if (!(nvl
->nvl_nvflag
& NV_UNIQUE_NAME
))
1583 /* skip white space */
1584 skip_whitespace(np
);
1588 /* set 'sepp' to end of current component 'np' */
1590 sepp
= strchr(np
, sep
);
1594 /* find start of next "[ index ]..." */
1595 idxp
= strchr(np
, '[');
1597 /* if sepp comes first, set idxp to NULL */
1598 if (sepp
&& idxp
&& (sepp
< idxp
))
1602 * At this point 'idxp' is set if there is an index
1603 * expected for the current component.
1606 /* set 'n' to length of current 'np' name component */
1609 /* keep sepp up to date for *ep use as we advance */
1610 skip_whitespace(idxp
);
1613 /* determine the index value */
1614 #if defined(_KERNEL) && !defined(_BOOT)
1615 if (ddi_strtol(idxp
, &idxep
, 0, &idx
))
1618 idx
= strtol(idxp
, &idxep
, 0);
1623 /* keep sepp up to date for *ep use as we advance */
1626 /* skip white space index value and check for ']' */
1627 skip_whitespace(sepp
);
1631 /* for embedded arrays, support C syntax: "a[1].b" */
1632 skip_whitespace(sepp
);
1633 if (sep
&& (*sepp
== sep
))
1641 /* trim trailing whitespace by reducing length of 'np' */
1644 for (n
--; (np
[n
] == ' ') || (np
[n
] == '\t'); n
--)
1648 /* skip whitespace, and set sepp to NULL if complete */
1650 skip_whitespace(sepp
);
1657 * o 'n' is the length of current 'np' component.
1658 * o 'idxp' is set if there was an index, and value 'idx'.
1659 * o 'sepp' is set to the beginning of the next component,
1660 * and set to NULL if we have no more components.
1662 * Search for nvpair with matching component name.
1664 for (nvp
= nvlist_next_nvpair(nvl
, NULL
); nvp
!= NULL
;
1665 nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
1667 /* continue if no match on name */
1668 if (strncmp(np
, nvpair_name(nvp
), n
) ||
1669 (strlen(nvpair_name(nvp
)) != n
))
1672 /* if indexed, verify type is array oriented */
1673 if (idxp
&& !nvpair_type_is_array(nvp
))
1677 * Full match found, return nvp and idx if this
1678 * was the last component.
1684 *ip
= (int)idx
; /* return index */
1685 return (0); /* found */
1689 * More components: current match must be
1690 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1691 * to support going deeper.
1693 if (nvpair_type(nvp
) == DATA_TYPE_NVLIST
) {
1694 nvl
= EMBEDDED_NVL(nvp
);
1696 } else if (nvpair_type(nvp
) == DATA_TYPE_NVLIST_ARRAY
) {
1697 (void) nvpair_value_nvlist_array(nvp
,
1698 &nva
, (uint_t
*)&n
);
1699 if ((n
< 0) || (idx
>= n
))
1705 /* type does not support more levels */
1709 goto fail
; /* 'name' not found */
1711 /* search for match of next component in embedded 'nvl' list */
1714 fail
: if (ep
&& sepp
)
1720 * Return pointer to nvpair with specified 'name'.
1723 nvlist_lookup_nvpair(nvlist_t
*nvl
, const char *name
, nvpair_t
**ret
)
1725 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, 0, ret
, NULL
, NULL
));
1729 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1730 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1733 int nvlist_lookup_nvpair_embedded_index(nvlist_t
*nvl
,
1734 const char *name
, nvpair_t
**ret
, int *ip
, char **ep
)
1736 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, '.', ret
, ip
, ep
));
1740 nvlist_exists(nvlist_t
*nvl
, const char *name
)
1746 if (name
== NULL
|| nvl
== NULL
||
1747 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1750 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
1751 nvp
= &curr
->nvi_nvp
;
1753 if (strcmp(name
, NVP_NAME(nvp
)) == 0)
1761 nvpair_value_boolean_value(nvpair_t
*nvp
, boolean_t
*val
)
1763 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1767 nvpair_value_byte(nvpair_t
*nvp
, uchar_t
*val
)
1769 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE
, NULL
, val
));
1773 nvpair_value_int8(nvpair_t
*nvp
, int8_t *val
)
1775 return (nvpair_value_common(nvp
, DATA_TYPE_INT8
, NULL
, val
));
1779 nvpair_value_uint8(nvpair_t
*nvp
, uint8_t *val
)
1781 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8
, NULL
, val
));
1785 nvpair_value_int16(nvpair_t
*nvp
, int16_t *val
)
1787 return (nvpair_value_common(nvp
, DATA_TYPE_INT16
, NULL
, val
));
1791 nvpair_value_uint16(nvpair_t
*nvp
, uint16_t *val
)
1793 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16
, NULL
, val
));
1797 nvpair_value_int32(nvpair_t
*nvp
, int32_t *val
)
1799 return (nvpair_value_common(nvp
, DATA_TYPE_INT32
, NULL
, val
));
1803 nvpair_value_uint32(nvpair_t
*nvp
, uint32_t *val
)
1805 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32
, NULL
, val
));
1809 nvpair_value_int64(nvpair_t
*nvp
, int64_t *val
)
1811 return (nvpair_value_common(nvp
, DATA_TYPE_INT64
, NULL
, val
));
1815 nvpair_value_uint64(nvpair_t
*nvp
, uint64_t *val
)
1817 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64
, NULL
, val
));
1820 #if !defined(_KERNEL)
1822 nvpair_value_double(nvpair_t
*nvp
, double *val
)
1824 return (nvpair_value_common(nvp
, DATA_TYPE_DOUBLE
, NULL
, val
));
1829 nvpair_value_string(nvpair_t
*nvp
, char **val
)
1831 return (nvpair_value_common(nvp
, DATA_TYPE_STRING
, NULL
, val
));
1835 nvpair_value_nvlist(nvpair_t
*nvp
, nvlist_t
**val
)
1837 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST
, NULL
, val
));
1841 nvpair_value_boolean_array(nvpair_t
*nvp
, boolean_t
**val
, uint_t
*nelem
)
1843 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_ARRAY
, nelem
, val
));
1847 nvpair_value_byte_array(nvpair_t
*nvp
, uchar_t
**val
, uint_t
*nelem
)
1849 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE_ARRAY
, nelem
, val
));
1853 nvpair_value_int8_array(nvpair_t
*nvp
, int8_t **val
, uint_t
*nelem
)
1855 return (nvpair_value_common(nvp
, DATA_TYPE_INT8_ARRAY
, nelem
, val
));
1859 nvpair_value_uint8_array(nvpair_t
*nvp
, uint8_t **val
, uint_t
*nelem
)
1861 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8_ARRAY
, nelem
, val
));
1865 nvpair_value_int16_array(nvpair_t
*nvp
, int16_t **val
, uint_t
*nelem
)
1867 return (nvpair_value_common(nvp
, DATA_TYPE_INT16_ARRAY
, nelem
, val
));
1871 nvpair_value_uint16_array(nvpair_t
*nvp
, uint16_t **val
, uint_t
*nelem
)
1873 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16_ARRAY
, nelem
, val
));
1877 nvpair_value_int32_array(nvpair_t
*nvp
, int32_t **val
, uint_t
*nelem
)
1879 return (nvpair_value_common(nvp
, DATA_TYPE_INT32_ARRAY
, nelem
, val
));
1883 nvpair_value_uint32_array(nvpair_t
*nvp
, uint32_t **val
, uint_t
*nelem
)
1885 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32_ARRAY
, nelem
, val
));
1889 nvpair_value_int64_array(nvpair_t
*nvp
, int64_t **val
, uint_t
*nelem
)
1891 return (nvpair_value_common(nvp
, DATA_TYPE_INT64_ARRAY
, nelem
, val
));
1895 nvpair_value_uint64_array(nvpair_t
*nvp
, uint64_t **val
, uint_t
*nelem
)
1897 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64_ARRAY
, nelem
, val
));
1901 nvpair_value_string_array(nvpair_t
*nvp
, char ***val
, uint_t
*nelem
)
1903 return (nvpair_value_common(nvp
, DATA_TYPE_STRING_ARRAY
, nelem
, val
));
1907 nvpair_value_nvlist_array(nvpair_t
*nvp
, nvlist_t
***val
, uint_t
*nelem
)
1909 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST_ARRAY
, nelem
, val
));
1913 nvpair_value_hrtime(nvpair_t
*nvp
, hrtime_t
*val
)
1915 return (nvpair_value_common(nvp
, DATA_TYPE_HRTIME
, NULL
, val
));
1919 * Add specified pair to the list.
1922 nvlist_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
1924 if (nvl
== NULL
|| nvp
== NULL
)
1927 return (nvlist_add_common(nvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
1928 NVP_NELEM(nvp
), NVP_VALUE(nvp
)));
1932 * Merge the supplied nvlists and put the result in dst.
1933 * The merged list will contain all names specified in both lists,
1934 * the values are taken from nvl in the case of duplicates.
1935 * Return 0 on success.
1939 nvlist_merge(nvlist_t
*dst
, nvlist_t
*nvl
, int flag
)
1941 if (nvl
== NULL
|| dst
== NULL
)
1945 return (nvlist_copy_pairs(nvl
, dst
));
1951 * Encoding related routines
1953 #define NVS_OP_ENCODE 0
1954 #define NVS_OP_DECODE 1
1955 #define NVS_OP_GETSIZE 2
1957 typedef struct nvs_ops nvs_ops_t
;
1961 const nvs_ops_t
*nvs_ops
;
1967 * nvs operations are:
1969 * encoding / decoding of a nvlist header (nvlist_t)
1970 * calculates the size used for header and end detection
1973 * responsible for the first part of encoding / decoding of an nvpair
1974 * calculates the decoded size of an nvpair
1977 * second part of encoding / decoding of an nvpair
1980 * calculates the encoding size of an nvpair
1983 * encodes the end detection mark (zeros).
1986 int (*nvs_nvlist
)(nvstream_t
*, nvlist_t
*, size_t *);
1987 int (*nvs_nvpair
)(nvstream_t
*, nvpair_t
*, size_t *);
1988 int (*nvs_nvp_op
)(nvstream_t
*, nvpair_t
*);
1989 int (*nvs_nvp_size
)(nvstream_t
*, nvpair_t
*, size_t *);
1990 int (*nvs_nvl_fini
)(nvstream_t
*);
1994 char nvh_encoding
; /* nvs encoding method */
1995 char nvh_endian
; /* nvs endian */
1996 char nvh_reserved1
; /* reserved for future use */
1997 char nvh_reserved2
; /* reserved for future use */
2001 nvs_encode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2003 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2007 * Walk nvpair in list and encode each nvpair
2009 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
2010 if (nvs
->nvs_ops
->nvs_nvpair(nvs
, &curr
->nvi_nvp
, NULL
) != 0)
2013 return (nvs
->nvs_ops
->nvs_nvl_fini(nvs
));
2017 nvs_decode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2024 * Get decoded size of next pair in stream, alloc
2025 * memory for nvpair_t, then decode the nvpair
2027 while ((err
= nvs
->nvs_ops
->nvs_nvpair(nvs
, NULL
, &nvsize
)) == 0) {
2028 if (nvsize
== 0) /* end of list */
2031 /* make sure len makes sense */
2032 if (nvsize
< NVP_SIZE_CALC(1, 0))
2035 if ((nvp
= nvp_buf_alloc(nvl
, nvsize
)) == NULL
)
2038 if ((err
= nvs
->nvs_ops
->nvs_nvp_op(nvs
, nvp
)) != 0) {
2039 nvp_buf_free(nvl
, nvp
);
2043 if (i_validate_nvpair(nvp
) != 0) {
2045 nvp_buf_free(nvl
, nvp
);
2049 nvp_buf_link(nvl
, nvp
);
2055 nvs_getsize_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2057 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2059 uint64_t nvsize
= *buflen
;
2063 * Get encoded size of nvpairs in nvlist
2065 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2066 if (nvs
->nvs_ops
->nvs_nvp_size(nvs
, &curr
->nvi_nvp
, &size
) != 0)
2069 if ((nvsize
+= size
) > INT32_MAX
)
2078 nvs_operation(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2082 if (nvl
->nvl_priv
== 0)
2086 * Perform the operation, starting with header, then each nvpair
2088 if ((err
= nvs
->nvs_ops
->nvs_nvlist(nvs
, nvl
, buflen
)) != 0)
2091 switch (nvs
->nvs_op
) {
2093 err
= nvs_encode_pairs(nvs
, nvl
);
2097 err
= nvs_decode_pairs(nvs
, nvl
);
2100 case NVS_OP_GETSIZE
:
2101 err
= nvs_getsize_pairs(nvs
, nvl
, buflen
);
2112 nvs_embedded(nvstream_t
*nvs
, nvlist_t
*embedded
)
2114 switch (nvs
->nvs_op
) {
2116 return (nvs_operation(nvs
, embedded
, NULL
));
2118 case NVS_OP_DECODE
: {
2122 if (embedded
->nvl_version
!= NV_VERSION
)
2125 if ((priv
= nv_priv_alloc_embedded(nvs
->nvs_priv
)) == NULL
)
2128 nvlist_init(embedded
, embedded
->nvl_nvflag
, priv
);
2130 if ((err
= nvs_operation(nvs
, embedded
, NULL
)) != 0)
2131 nvlist_free(embedded
);
2142 nvs_embedded_nvl_array(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2144 size_t nelem
= NVP_NELEM(nvp
);
2145 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
2148 switch (nvs
->nvs_op
) {
2150 for (i
= 0; i
< nelem
; i
++)
2151 if (nvs_embedded(nvs
, nvlp
[i
]) != 0)
2155 case NVS_OP_DECODE
: {
2156 size_t len
= nelem
* sizeof (uint64_t);
2157 nvlist_t
*embedded
= (nvlist_t
*)((uintptr_t)nvlp
+ len
);
2159 bzero(nvlp
, len
); /* don't trust packed data */
2160 for (i
= 0; i
< nelem
; i
++) {
2161 if (nvs_embedded(nvs
, embedded
) != 0) {
2166 nvlp
[i
] = embedded
++;
2170 case NVS_OP_GETSIZE
: {
2171 uint64_t nvsize
= 0;
2173 for (i
= 0; i
< nelem
; i
++) {
2176 if (nvs_operation(nvs
, nvlp
[i
], &nvp_sz
) != 0)
2179 if ((nvsize
+= nvp_sz
) > INT32_MAX
)
2193 static int nvs_native(nvstream_t
*, nvlist_t
*, char *, size_t *);
2194 static int nvs_xdr(nvstream_t
*, nvlist_t
*, char *, size_t *);
2197 * Common routine for nvlist operations:
2198 * encode, decode, getsize (encoded size).
2201 nvlist_common(nvlist_t
*nvl
, char *buf
, size_t *buflen
, int encoding
,
2207 #ifdef _LITTLE_ENDIAN
2208 int host_endian
= 1;
2210 int host_endian
= 0;
2211 #endif /* _LITTLE_ENDIAN */
2212 nvs_header_t
*nvh
= (void *)buf
;
2214 if (buflen
== NULL
|| nvl
== NULL
||
2215 (nvs
.nvs_priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2218 nvs
.nvs_op
= nvs_op
;
2221 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2222 * a buffer is allocated. The first 4 bytes in the buffer are
2223 * used for encoding method and host endian.
2227 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2230 nvh
->nvh_encoding
= encoding
;
2231 nvh
->nvh_endian
= nvl_endian
= host_endian
;
2232 nvh
->nvh_reserved1
= 0;
2233 nvh
->nvh_reserved2
= 0;
2237 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2240 /* get method of encoding from first byte */
2241 encoding
= nvh
->nvh_encoding
;
2242 nvl_endian
= nvh
->nvh_endian
;
2245 case NVS_OP_GETSIZE
:
2246 nvl_endian
= host_endian
;
2249 * add the size for encoding
2251 *buflen
= sizeof (nvs_header_t
);
2259 * Create an nvstream with proper encoding method
2262 case NV_ENCODE_NATIVE
:
2264 * check endianness, in case we are unpacking
2267 if (nvl_endian
!= host_endian
)
2269 err
= nvs_native(&nvs
, nvl
, buf
, buflen
);
2272 err
= nvs_xdr(&nvs
, nvl
, buf
, buflen
);
2283 nvlist_size(nvlist_t
*nvl
, size_t *size
, int encoding
)
2285 return (nvlist_common(nvl
, NULL
, size
, encoding
, NVS_OP_GETSIZE
));
2289 * Pack nvlist into contiguous memory
2293 nvlist_pack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2296 #if defined(_KERNEL) && !defined(_BOOT)
2297 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
,
2298 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
2300 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
, nv_alloc_nosleep
));
2305 nvlist_xpack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2313 if (nva
== NULL
|| nvl
== NULL
|| bufp
== NULL
|| buflen
== NULL
)
2317 return (nvlist_common(nvl
, *bufp
, buflen
, encoding
,
2321 * Here is a difficult situation:
2322 * 1. The nvlist has fixed allocator properties.
2323 * All other nvlist routines (like nvlist_add_*, ...) use
2325 * 2. When using nvlist_pack() the user can specify his own
2326 * allocator properties (e.g. by using KM_NOSLEEP).
2328 * We use the user specified properties (2). A clearer solution
2329 * will be to remove the kmflag from nvlist_pack(), but we will
2330 * not change the interface.
2332 nv_priv_init(&nvpriv
, nva
, 0);
2334 if (err
= nvlist_size(nvl
, &alloc_size
, encoding
))
2337 if ((buf
= nv_mem_zalloc(&nvpriv
, alloc_size
)) == NULL
)
2340 if ((err
= nvlist_common(nvl
, buf
, &alloc_size
, encoding
,
2341 NVS_OP_ENCODE
)) != 0) {
2342 nv_mem_free(&nvpriv
, buf
, alloc_size
);
2344 *buflen
= alloc_size
;
2352 * Unpack buf into an nvlist_t
2356 nvlist_unpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, int kmflag
)
2358 #if defined(_KERNEL) && !defined(_BOOT)
2359 return (nvlist_xunpack(buf
, buflen
, nvlp
,
2360 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
2362 return (nvlist_xunpack(buf
, buflen
, nvlp
, nv_alloc_nosleep
));
2367 nvlist_xunpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
2375 if ((err
= nvlist_xalloc(&nvl
, 0, nva
)) != 0)
2378 if ((err
= nvlist_common(nvl
, buf
, &buflen
, 0, NVS_OP_DECODE
)) != 0)
2387 * Native encoding functions
2391 * This structure is used when decoding a packed nvpair in
2392 * the native format. n_base points to a buffer containing the
2393 * packed nvpair. n_end is a pointer to the end of the buffer.
2394 * (n_end actually points to the first byte past the end of the
2395 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2396 * It points to the current data that we are decoding.
2397 * The amount of data left in the buffer is equal to n_end - n_curr.
2398 * n_flag is used to recognize a packed embedded list.
2407 nvs_native_create(nvstream_t
*nvs
, nvs_native_t
*native
, char *buf
,
2410 switch (nvs
->nvs_op
) {
2413 nvs
->nvs_private
= native
;
2414 native
->n_curr
= native
->n_base
= buf
;
2415 native
->n_end
= buf
+ buflen
;
2419 case NVS_OP_GETSIZE
:
2420 nvs
->nvs_private
= native
;
2421 native
->n_curr
= native
->n_base
= native
->n_end
= NULL
;
2431 nvs_native_destroy(nvstream_t
*nvs
)
2436 native_cp(nvstream_t
*nvs
, void *buf
, size_t size
)
2438 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2440 if (native
->n_curr
+ size
> native
->n_end
)
2444 * The bcopy() below eliminates alignment requirement
2445 * on the buffer (stream) and is preferred over direct access.
2447 switch (nvs
->nvs_op
) {
2449 bcopy(buf
, native
->n_curr
, size
);
2452 bcopy(native
->n_curr
, buf
, size
);
2458 native
->n_curr
+= size
;
2463 * operate on nvlist_t header
2466 nvs_native_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2468 nvs_native_t
*native
= nvs
->nvs_private
;
2470 switch (nvs
->nvs_op
) {
2474 return (0); /* packed embedded list */
2478 /* copy version and nvflag of the nvlist_t */
2479 if (native_cp(nvs
, &nvl
->nvl_version
, sizeof (int32_t)) != 0 ||
2480 native_cp(nvs
, &nvl
->nvl_nvflag
, sizeof (int32_t)) != 0)
2485 case NVS_OP_GETSIZE
:
2487 * if calculate for packed embedded list
2488 * 4 for end of the embedded list
2490 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2491 * and 4 for end of the entire list
2493 if (native
->n_flag
) {
2497 *size
+= 2 * sizeof (int32_t) + 4;
2508 nvs_native_nvl_fini(nvstream_t
*nvs
)
2510 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2511 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2513 * Add 4 zero bytes at end of nvlist. They are used
2514 * for end detection by the decode routine.
2516 if (native
->n_curr
+ sizeof (int) > native
->n_end
)
2519 bzero(native
->n_curr
, sizeof (int));
2520 native
->n_curr
+= sizeof (int);
2527 nvpair_native_embedded(nvstream_t
*nvs
, nvpair_t
*nvp
)
2529 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2530 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2531 nvlist_t
*packed
= (void *)
2532 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2534 * Null out the pointer that is meaningless in the packed
2535 * structure. The address may not be aligned, so we have
2538 bzero(&packed
->nvl_priv
, sizeof (packed
->nvl_priv
));
2541 return (nvs_embedded(nvs
, EMBEDDED_NVL(nvp
)));
2545 nvpair_native_embedded_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2547 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2548 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2549 char *value
= native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
);
2550 size_t len
= NVP_NELEM(nvp
) * sizeof (uint64_t);
2551 nvlist_t
*packed
= (nvlist_t
*)((uintptr_t)value
+ len
);
2554 * Null out pointers that are meaningless in the packed
2555 * structure. The addresses may not be aligned, so we have
2560 for (i
= 0; i
< NVP_NELEM(nvp
); i
++, packed
++)
2562 * Null out the pointer that is meaningless in the
2563 * packed structure. The address may not be aligned,
2564 * so we have to use bzero.
2566 bzero(&packed
->nvl_priv
, sizeof (packed
->nvl_priv
));
2569 return (nvs_embedded_nvl_array(nvs
, nvp
, NULL
));
2573 nvpair_native_string_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2575 switch (nvs
->nvs_op
) {
2576 case NVS_OP_ENCODE
: {
2577 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2578 uint64_t *strp
= (void *)
2579 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2581 * Null out pointers that are meaningless in the packed
2582 * structure. The addresses may not be aligned, so we have
2585 bzero(strp
, NVP_NELEM(nvp
) * sizeof (uint64_t));
2588 case NVS_OP_DECODE
: {
2589 char **strp
= (void *)NVP_VALUE(nvp
);
2590 char *buf
= ((char *)strp
+ NVP_NELEM(nvp
) * sizeof (uint64_t));
2593 for (i
= 0; i
< NVP_NELEM(nvp
); i
++) {
2595 buf
+= strlen(buf
) + 1;
2603 nvs_native_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2610 * We do the initial bcopy of the data before we look at
2611 * the nvpair type, because when we're decoding, we won't
2612 * have the correct values for the pair until we do the bcopy.
2614 switch (nvs
->nvs_op
) {
2617 if (native_cp(nvs
, nvp
, nvp
->nvp_size
) != 0)
2624 /* verify nvp_name_sz, check the name string length */
2625 if (i_validate_nvpair_name(nvp
) != 0)
2628 type
= NVP_TYPE(nvp
);
2631 * Verify type and nelem and get the value size.
2632 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2633 * is the size of the string(s) excluded.
2635 if ((value_sz
= i_get_value_size(type
, NULL
, NVP_NELEM(nvp
))) < 0)
2638 if (NVP_SIZE_CALC(nvp
->nvp_name_sz
, value_sz
) > nvp
->nvp_size
)
2642 case DATA_TYPE_NVLIST
:
2643 ret
= nvpair_native_embedded(nvs
, nvp
);
2645 case DATA_TYPE_NVLIST_ARRAY
:
2646 ret
= nvpair_native_embedded_array(nvs
, nvp
);
2648 case DATA_TYPE_STRING_ARRAY
:
2649 nvpair_native_string_array(nvs
, nvp
);
2659 nvs_native_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2661 uint64_t nvp_sz
= nvp
->nvp_size
;
2663 switch (NVP_TYPE(nvp
)) {
2664 case DATA_TYPE_NVLIST
: {
2667 if (nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
) != 0)
2673 case DATA_TYPE_NVLIST_ARRAY
: {
2676 if (nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
) != 0)
2686 if (nvp_sz
> INT32_MAX
)
2695 nvs_native_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2697 switch (nvs
->nvs_op
) {
2699 return (nvs_native_nvp_op(nvs
, nvp
));
2701 case NVS_OP_DECODE
: {
2702 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2705 /* try to read the size value from the stream */
2706 if (native
->n_curr
+ sizeof (int32_t) > native
->n_end
)
2708 bcopy(native
->n_curr
, &decode_len
, sizeof (int32_t));
2710 /* sanity check the size value */
2711 if (decode_len
< 0 ||
2712 decode_len
> native
->n_end
- native
->n_curr
)
2718 * If at the end of the stream then move the cursor
2719 * forward, otherwise nvpair_native_op() will read
2720 * the entire nvpair at the same cursor position.
2723 native
->n_curr
+= sizeof (int32_t);
2734 static const nvs_ops_t nvs_native_ops
= {
2738 nvs_native_nvp_size
,
2743 nvs_native(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
2745 nvs_native_t native
;
2748 nvs
->nvs_ops
= &nvs_native_ops
;
2750 if ((err
= nvs_native_create(nvs
, &native
, buf
+ sizeof (nvs_header_t
),
2751 *buflen
- sizeof (nvs_header_t
))) != 0)
2754 err
= nvs_operation(nvs
, nvl
, buflen
);
2756 nvs_native_destroy(nvs
);
2762 * XDR encoding functions
2764 * An xdr packed nvlist is encoded as:
2766 * - encoding methode and host endian (4 bytes)
2767 * - nvl_version (4 bytes)
2768 * - nvl_nvflag (4 bytes)
2770 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2771 * - encoded size of the nvpair (4 bytes)
2772 * - decoded size of the nvpair (4 bytes)
2773 * - name string, (4 + sizeof(NV_ALIGN4(string))
2774 * a string is coded as size (4 bytes) and data
2775 * - data type (4 bytes)
2776 * - number of elements in the nvpair (4 bytes)
2779 * - 2 zero's for end of the entire list (8 bytes)
2782 nvs_xdr_create(nvstream_t
*nvs
, XDR
*xdr
, char *buf
, size_t buflen
)
2784 /* xdr data must be 4 byte aligned */
2785 if ((ulong_t
)buf
% 4 != 0)
2788 switch (nvs
->nvs_op
) {
2790 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_ENCODE
);
2791 nvs
->nvs_private
= xdr
;
2794 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_DECODE
);
2795 nvs
->nvs_private
= xdr
;
2797 case NVS_OP_GETSIZE
:
2798 nvs
->nvs_private
= NULL
;
2806 nvs_xdr_destroy(nvstream_t
*nvs
)
2808 switch (nvs
->nvs_op
) {
2811 xdr_destroy((XDR
*)nvs
->nvs_private
);
2819 nvs_xdr_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2821 switch (nvs
->nvs_op
) {
2823 case NVS_OP_DECODE
: {
2824 XDR
*xdr
= nvs
->nvs_private
;
2826 if (!xdr_int(xdr
, &nvl
->nvl_version
) ||
2827 !xdr_u_int(xdr
, &nvl
->nvl_nvflag
))
2831 case NVS_OP_GETSIZE
: {
2833 * 2 * 4 for nvl_version + nvl_nvflag
2834 * and 8 for end of the entire list
2846 nvs_xdr_nvl_fini(nvstream_t
*nvs
)
2848 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2849 XDR
*xdr
= nvs
->nvs_private
;
2852 if (!xdr_int(xdr
, &zero
) || !xdr_int(xdr
, &zero
))
2860 * The format of xdr encoded nvpair is:
2861 * encode_size, decode_size, name string, data type, nelem, data
2864 nvs_xdr_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2868 char *buf_end
= (char *)nvp
+ nvp
->nvp_size
;
2870 uint_t nelem
, buflen
;
2872 XDR
*xdr
= nvs
->nvs_private
;
2874 ASSERT(xdr
!= NULL
&& nvp
!= NULL
);
2877 if ((buf
= NVP_NAME(nvp
)) >= buf_end
)
2879 buflen
= buf_end
- buf
;
2881 if (!xdr_string(xdr
, &buf
, buflen
- 1))
2883 nvp
->nvp_name_sz
= strlen(buf
) + 1;
2885 /* type and nelem */
2886 if (!xdr_int(xdr
, (int *)&nvp
->nvp_type
) ||
2887 !xdr_int(xdr
, &nvp
->nvp_value_elem
))
2890 type
= NVP_TYPE(nvp
);
2891 nelem
= nvp
->nvp_value_elem
;
2894 * Verify type and nelem and get the value size.
2895 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2896 * is the size of the string(s) excluded.
2898 if ((value_sz
= i_get_value_size(type
, NULL
, nelem
)) < 0)
2901 /* if there is no data to extract then return */
2906 if ((buf
= NVP_VALUE(nvp
)) >= buf_end
)
2908 buflen
= buf_end
- buf
;
2910 if (buflen
< value_sz
)
2914 case DATA_TYPE_NVLIST
:
2915 if (nvs_embedded(nvs
, (void *)buf
) == 0)
2919 case DATA_TYPE_NVLIST_ARRAY
:
2920 if (nvs_embedded_nvl_array(nvs
, nvp
, NULL
) == 0)
2924 case DATA_TYPE_BOOLEAN
:
2928 case DATA_TYPE_BYTE
:
2929 case DATA_TYPE_INT8
:
2930 case DATA_TYPE_UINT8
:
2931 ret
= xdr_char(xdr
, buf
);
2934 case DATA_TYPE_INT16
:
2935 ret
= xdr_short(xdr
, (void *)buf
);
2938 case DATA_TYPE_UINT16
:
2939 ret
= xdr_u_short(xdr
, (void *)buf
);
2942 case DATA_TYPE_BOOLEAN_VALUE
:
2943 case DATA_TYPE_INT32
:
2944 ret
= xdr_int(xdr
, (void *)buf
);
2947 case DATA_TYPE_UINT32
:
2948 ret
= xdr_u_int(xdr
, (void *)buf
);
2951 case DATA_TYPE_INT64
:
2952 ret
= xdr_longlong_t(xdr
, (void *)buf
);
2955 case DATA_TYPE_UINT64
:
2956 ret
= xdr_u_longlong_t(xdr
, (void *)buf
);
2959 case DATA_TYPE_HRTIME
:
2961 * NOTE: must expose the definition of hrtime_t here
2963 ret
= xdr_longlong_t(xdr
, (void *)buf
);
2965 #if !defined(_KERNEL)
2966 case DATA_TYPE_DOUBLE
:
2967 ret
= xdr_double(xdr
, (void *)buf
);
2970 case DATA_TYPE_STRING
:
2971 ret
= xdr_string(xdr
, &buf
, buflen
- 1);
2974 case DATA_TYPE_BYTE_ARRAY
:
2975 ret
= xdr_opaque(xdr
, buf
, nelem
);
2978 case DATA_TYPE_INT8_ARRAY
:
2979 case DATA_TYPE_UINT8_ARRAY
:
2980 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
, sizeof (int8_t),
2981 (xdrproc_t
)xdr_char
);
2984 case DATA_TYPE_INT16_ARRAY
:
2985 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int16_t),
2986 sizeof (int16_t), (xdrproc_t
)xdr_short
);
2989 case DATA_TYPE_UINT16_ARRAY
:
2990 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint16_t),
2991 sizeof (uint16_t), (xdrproc_t
)xdr_u_short
);
2994 case DATA_TYPE_BOOLEAN_ARRAY
:
2995 case DATA_TYPE_INT32_ARRAY
:
2996 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int32_t),
2997 sizeof (int32_t), (xdrproc_t
)xdr_int
);
3000 case DATA_TYPE_UINT32_ARRAY
:
3001 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint32_t),
3002 sizeof (uint32_t), (xdrproc_t
)xdr_u_int
);
3005 case DATA_TYPE_INT64_ARRAY
:
3006 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int64_t),
3007 sizeof (int64_t), (xdrproc_t
)xdr_longlong_t
);
3010 case DATA_TYPE_UINT64_ARRAY
:
3011 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint64_t),
3012 sizeof (uint64_t), (xdrproc_t
)xdr_u_longlong_t
);
3015 case DATA_TYPE_STRING_ARRAY
: {
3016 size_t len
= nelem
* sizeof (uint64_t);
3017 char **strp
= (void *)buf
;
3020 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3021 bzero(buf
, len
); /* don't trust packed data */
3023 for (i
= 0; i
< nelem
; i
++) {
3030 if (xdr_string(xdr
, &buf
, buflen
- 1) != TRUE
)
3033 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3035 len
= strlen(buf
) + 1;
3044 return (ret
== TRUE
? 0 : EFAULT
);
3048 nvs_xdr_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3050 data_type_t type
= NVP_TYPE(nvp
);
3052 * encode_size + decode_size + name string size + data type + nelem
3053 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3055 uint64_t nvp_sz
= 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp
))) + 4 + 4;
3058 case DATA_TYPE_BOOLEAN
:
3061 case DATA_TYPE_BOOLEAN_VALUE
:
3062 case DATA_TYPE_BYTE
:
3063 case DATA_TYPE_INT8
:
3064 case DATA_TYPE_UINT8
:
3065 case DATA_TYPE_INT16
:
3066 case DATA_TYPE_UINT16
:
3067 case DATA_TYPE_INT32
:
3068 case DATA_TYPE_UINT32
:
3069 nvp_sz
+= 4; /* 4 is the minimum xdr unit */
3072 case DATA_TYPE_INT64
:
3073 case DATA_TYPE_UINT64
:
3074 case DATA_TYPE_HRTIME
:
3075 #if !defined(_KERNEL)
3076 case DATA_TYPE_DOUBLE
:
3081 case DATA_TYPE_STRING
:
3082 nvp_sz
+= 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp
)));
3085 case DATA_TYPE_BYTE_ARRAY
:
3086 nvp_sz
+= NV_ALIGN4(NVP_NELEM(nvp
));
3089 case DATA_TYPE_BOOLEAN_ARRAY
:
3090 case DATA_TYPE_INT8_ARRAY
:
3091 case DATA_TYPE_UINT8_ARRAY
:
3092 case DATA_TYPE_INT16_ARRAY
:
3093 case DATA_TYPE_UINT16_ARRAY
:
3094 case DATA_TYPE_INT32_ARRAY
:
3095 case DATA_TYPE_UINT32_ARRAY
:
3096 nvp_sz
+= 4 + 4 * (uint64_t)NVP_NELEM(nvp
);
3099 case DATA_TYPE_INT64_ARRAY
:
3100 case DATA_TYPE_UINT64_ARRAY
:
3101 nvp_sz
+= 4 + 8 * (uint64_t)NVP_NELEM(nvp
);
3104 case DATA_TYPE_STRING_ARRAY
: {
3106 char **strs
= (void *)NVP_VALUE(nvp
);
3108 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
3109 nvp_sz
+= 4 + NV_ALIGN4(strlen(strs
[i
]));
3114 case DATA_TYPE_NVLIST
:
3115 case DATA_TYPE_NVLIST_ARRAY
: {
3117 int old_nvs_op
= nvs
->nvs_op
;
3120 nvs
->nvs_op
= NVS_OP_GETSIZE
;
3121 if (type
== DATA_TYPE_NVLIST
)
3122 err
= nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
);
3124 err
= nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
);
3125 nvs
->nvs_op
= old_nvs_op
;
3138 if (nvp_sz
> INT32_MAX
)
3148 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3149 * the largest nvpair that could be encoded in the buffer.
3151 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3152 * The size of a xdr packed nvpair without any data is 5 words.
3154 * Using the size of the data directly as an estimate would be ok
3155 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3156 * then the actual nvpair has space for an array of pointers to index
3157 * the strings. These pointers are not encoded into the packed xdr buffer.
3159 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3160 * of length 0, then each string is endcoded in xdr format as a single word.
3161 * Therefore when expanded to an nvpair there will be 2.25 word used for
3162 * each string. (a int64_t allocated for pointer usage, and a single char
3163 * for the null termination.)
3165 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3167 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3168 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3169 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3170 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3171 (NVS_XDR_DATA_LEN(x) * 2) + \
3172 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3175 nvs_xdr_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3177 XDR
*xdr
= nvs
->nvs_private
;
3178 int32_t encode_len
, decode_len
;
3180 switch (nvs
->nvs_op
) {
3181 case NVS_OP_ENCODE
: {
3184 if (nvs_xdr_nvp_size(nvs
, nvp
, &nvsize
) != 0)
3187 decode_len
= nvp
->nvp_size
;
3188 encode_len
= nvsize
;
3189 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3192 return (nvs_xdr_nvp_op(nvs
, nvp
));
3194 case NVS_OP_DECODE
: {
3195 struct xdr_bytesrec bytesrec
;
3197 /* get the encode and decode size */
3198 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3202 /* are we at the end of the stream? */
3206 /* sanity check the size parameter */
3207 if (!xdr_control(xdr
, XDR_GET_BYTES_AVAIL
, &bytesrec
))
3210 if (*size
> NVS_XDR_MAX_LEN(bytesrec
.xc_num_avail
))
3221 static const struct nvs_ops nvs_xdr_ops
= {
3230 nvs_xdr(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3235 nvs
->nvs_ops
= &nvs_xdr_ops
;
3237 if ((err
= nvs_xdr_create(nvs
, &xdr
, buf
+ sizeof (nvs_header_t
),
3238 *buflen
- sizeof (nvs_header_t
))) != 0)
3241 err
= nvs_operation(nvs
, nvl
, buflen
);
3243 nvs_xdr_destroy(nvs
);