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/stropts.h>
27 #include <sys/debug.h>
28 #include <sys/isa_defs.h>
29 #include <sys/int_limits.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <rpc/types.h>
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/varargs.h>
38 #include <sys/sunddi.h>
47 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
49 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
52 * nvpair.c - Provides kernel & userland interfaces for manipulating
67 * +--------------+ last i_nvp in list
68 * | nvpriv_t | +--------------------->
70 * +--+- nvp_list | | +------------+
71 * | | nvp_last -+--+ + nv_alloc_t |
72 * | | nvp_curr | |------------|
73 * | | nvp_nva -+----> | nva_ops |
74 * | | nvp_stat | | nva_arg |
75 * | +--------------+ +------------+
79 * +---------------------+ +-------------------+
80 * | i_nvp_t | +-->| i_nvp_t | +-->
81 * |---------------------| | |-------------------| |
82 * | nvi_next -+--+ | nvi_next -+--+
83 * | nvi_prev (NULL) | <----+ nvi_prev |
84 * | . . . . . . . . . . | | . . . . . . . . . |
85 * | nvp (nvpair_t) | | nvp (nvpair_t) |
86 * | - nvp_size | | - nvp_size |
87 * | - nvp_name_sz | | - nvp_name_sz |
88 * | - nvp_value_elem | | - nvp_value_elem |
89 * | - nvp_type | | - nvp_type |
90 * | - data ... | | - data ... |
91 * +---------------------+ +-------------------+
95 * +---------------------+ +---------------------+
96 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
97 * |---------------------| | | |---------------------|
98 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
99 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
100 * | . . . . . . . . . | | . . . . . . . . . |
101 * | nvp (nvpair_t) | | nvp (nvpair_t) |
102 * | - nvp_size | | - nvp_size |
103 * | - nvp_name_sz | | - nvp_name_sz |
104 * | - nvp_value_elem | | - nvp_value_elem |
105 * | - DATA_TYPE_NVLIST | | - nvp_type |
106 * | - data (embedded) | | - data ... |
107 * | nvlist name | +---------------------+
108 * | +--------------+ |
110 * | |--------------| |
111 * | | nvl_version | |
113 * | | nvl_priv --+---+---->
116 * | +--------------+ |
117 * +---------------------+
120 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
121 * allow value to be aligned on 8 byte boundary
123 * name_len is the length of the name string including the null terminator
126 #define NVP_SIZE_CALC(name_len, data_len) \
127 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
129 static int i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
);
130 static int nvlist_add_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
131 uint_t nelem
, const void *data
);
133 #define NV_STAT_EMBEDDED 0x1
134 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
135 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
137 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
138 #define NVPAIR2I_NVP(nvp) \
139 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
143 nv_alloc_init(nv_alloc_t
*nva
, const nv_alloc_ops_t
*nvo
, /* args */ ...)
151 va_start(valist
, nvo
);
152 if (nva
->nva_ops
->nv_ao_init
!= NULL
)
153 err
= nva
->nva_ops
->nv_ao_init(nva
, valist
);
160 nv_alloc_reset(nv_alloc_t
*nva
)
162 if (nva
->nva_ops
->nv_ao_reset
!= NULL
)
163 nva
->nva_ops
->nv_ao_reset(nva
);
167 nv_alloc_fini(nv_alloc_t
*nva
)
169 if (nva
->nva_ops
->nv_ao_fini
!= NULL
)
170 nva
->nva_ops
->nv_ao_fini(nva
);
174 nvlist_lookup_nv_alloc(nvlist_t
*nvl
)
179 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
182 return (priv
->nvp_nva
);
186 nv_mem_zalloc(nvpriv_t
*nvp
, size_t size
)
188 nv_alloc_t
*nva
= nvp
->nvp_nva
;
191 if ((buf
= nva
->nva_ops
->nv_ao_alloc(nva
, size
)) != NULL
)
198 nv_mem_free(nvpriv_t
*nvp
, void *buf
, size_t size
)
200 nv_alloc_t
*nva
= nvp
->nvp_nva
;
202 nva
->nva_ops
->nv_ao_free(nva
, buf
, size
);
206 nv_priv_init(nvpriv_t
*priv
, nv_alloc_t
*nva
, uint32_t stat
)
208 bzero(priv
, sizeof (nvpriv_t
));
211 priv
->nvp_stat
= stat
;
215 nv_priv_alloc(nv_alloc_t
*nva
)
220 * nv_mem_alloc() cannot called here because it needs the priv
223 if ((priv
= nva
->nva_ops
->nv_ao_alloc(nva
, sizeof (nvpriv_t
))) == NULL
)
226 nv_priv_init(priv
, nva
, 0);
232 * Embedded lists need their own nvpriv_t's. We create a new
233 * nvpriv_t using the parameters and allocator from the parent
237 nv_priv_alloc_embedded(nvpriv_t
*priv
)
241 if ((emb_priv
= nv_mem_zalloc(priv
, sizeof (nvpriv_t
))) == NULL
)
244 nv_priv_init(emb_priv
, priv
->nvp_nva
, NV_STAT_EMBEDDED
);
250 nvlist_init(nvlist_t
*nvl
, uint32_t nvflag
, nvpriv_t
*priv
)
252 nvl
->nvl_version
= NV_VERSION
;
253 nvl
->nvl_nvflag
= nvflag
& (NV_UNIQUE_NAME
|NV_UNIQUE_NAME_TYPE
);
254 nvl
->nvl_priv
= (uint64_t)(uintptr_t)priv
;
260 nvlist_nvflag(nvlist_t
*nvl
)
262 return (nvl
->nvl_nvflag
);
266 * nvlist_alloc - Allocate nvlist.
270 nvlist_alloc(nvlist_t
**nvlp
, uint_t nvflag
, int kmflag
)
272 #if defined(_KERNEL) && !defined(_BOOT)
273 return (nvlist_xalloc(nvlp
, nvflag
,
274 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
276 return (nvlist_xalloc(nvlp
, nvflag
, nv_alloc_nosleep
));
281 nvlist_xalloc(nvlist_t
**nvlp
, uint_t nvflag
, nv_alloc_t
*nva
)
285 if (nvlp
== NULL
|| nva
== NULL
)
288 if ((priv
= nv_priv_alloc(nva
)) == NULL
)
291 if ((*nvlp
= nv_mem_zalloc(priv
,
292 NV_ALIGN(sizeof (nvlist_t
)))) == NULL
) {
293 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
297 nvlist_init(*nvlp
, nvflag
, priv
);
303 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
306 nvp_buf_alloc(nvlist_t
*nvl
, size_t len
)
308 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
314 * Allocate the buffer
316 nvsize
= len
+ offsetof(i_nvp_t
, nvi_nvp
);
318 if ((buf
= nv_mem_zalloc(priv
, nvsize
)) == NULL
)
328 * nvp_buf_free - de-Allocate an i_nvp_t.
331 nvp_buf_free(nvlist_t
*nvl
, nvpair_t
*nvp
)
333 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
334 size_t nvsize
= nvp
->nvp_size
+ offsetof(i_nvp_t
, nvi_nvp
);
336 nv_mem_free(priv
, NVPAIR2I_NVP(nvp
), nvsize
);
340 * nvp_buf_link - link a new nv pair into the nvlist.
343 nvp_buf_link(nvlist_t
*nvl
, nvpair_t
*nvp
)
345 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
346 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
348 /* Put element at end of nvlist */
349 if (priv
->nvp_list
== NULL
) {
350 priv
->nvp_list
= priv
->nvp_last
= curr
;
352 curr
->nvi_prev
= priv
->nvp_last
;
353 priv
->nvp_last
->nvi_next
= curr
;
354 priv
->nvp_last
= curr
;
359 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
362 nvp_buf_unlink(nvlist_t
*nvl
, nvpair_t
*nvp
)
364 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
365 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
368 * protect nvlist_next_nvpair() against walking on freed memory.
370 if (priv
->nvp_curr
== curr
)
371 priv
->nvp_curr
= curr
->nvi_next
;
373 if (curr
== priv
->nvp_list
)
374 priv
->nvp_list
= curr
->nvi_next
;
376 curr
->nvi_prev
->nvi_next
= curr
->nvi_next
;
378 if (curr
== priv
->nvp_last
)
379 priv
->nvp_last
= curr
->nvi_prev
;
381 curr
->nvi_next
->nvi_prev
= curr
->nvi_prev
;
385 * take a nvpair type and number of elements and make sure the are valid
388 i_validate_type_nelem(data_type_t type
, uint_t nelem
)
391 case DATA_TYPE_BOOLEAN
:
395 case DATA_TYPE_BOOLEAN_VALUE
:
398 case DATA_TYPE_UINT8
:
399 case DATA_TYPE_INT16
:
400 case DATA_TYPE_UINT16
:
401 case DATA_TYPE_INT32
:
402 case DATA_TYPE_UINT32
:
403 case DATA_TYPE_INT64
:
404 case DATA_TYPE_UINT64
:
405 case DATA_TYPE_STRING
:
406 case DATA_TYPE_HRTIME
:
407 case DATA_TYPE_NVLIST
:
408 #if !defined(_KERNEL)
409 case DATA_TYPE_DOUBLE
:
414 case DATA_TYPE_BOOLEAN_ARRAY
:
415 case DATA_TYPE_BYTE_ARRAY
:
416 case DATA_TYPE_INT8_ARRAY
:
417 case DATA_TYPE_UINT8_ARRAY
:
418 case DATA_TYPE_INT16_ARRAY
:
419 case DATA_TYPE_UINT16_ARRAY
:
420 case DATA_TYPE_INT32_ARRAY
:
421 case DATA_TYPE_UINT32_ARRAY
:
422 case DATA_TYPE_INT64_ARRAY
:
423 case DATA_TYPE_UINT64_ARRAY
:
424 case DATA_TYPE_STRING_ARRAY
:
425 case DATA_TYPE_NVLIST_ARRAY
:
426 /* we allow arrays with 0 elements */
435 * Verify nvp_name_sz and check the name string length.
438 i_validate_nvpair_name(nvpair_t
*nvp
)
440 if ((nvp
->nvp_name_sz
<= 0) ||
441 (nvp
->nvp_size
< NVP_SIZE_CALC(nvp
->nvp_name_sz
, 0)))
444 /* verify the name string, make sure its terminated */
445 if (NVP_NAME(nvp
)[nvp
->nvp_name_sz
- 1] != '\0')
448 return (strlen(NVP_NAME(nvp
)) == nvp
->nvp_name_sz
- 1 ? 0 : EFAULT
);
452 i_validate_nvpair_value(data_type_t type
, uint_t nelem
, const void *data
)
455 case DATA_TYPE_BOOLEAN_VALUE
:
456 if (*(boolean_t
*)data
!= B_TRUE
&&
457 *(boolean_t
*)data
!= B_FALSE
)
460 case DATA_TYPE_BOOLEAN_ARRAY
: {
463 for (i
= 0; i
< nelem
; i
++)
464 if (((boolean_t
*)data
)[i
] != B_TRUE
&&
465 ((boolean_t
*)data
)[i
] != B_FALSE
)
477 * This function takes a pointer to what should be a nvpair and it's size
478 * and then verifies that all the nvpair fields make sense and can be
479 * trusted. This function is used when decoding packed nvpairs.
482 i_validate_nvpair(nvpair_t
*nvp
)
484 data_type_t type
= NVP_TYPE(nvp
);
487 /* verify nvp_name_sz, check the name string length */
488 if (i_validate_nvpair_name(nvp
) != 0)
491 if (i_validate_nvpair_value(type
, NVP_NELEM(nvp
), NVP_VALUE(nvp
)) != 0)
495 * verify nvp_type, nvp_value_elem, and also possibly
496 * verify string values and get the value size.
498 size2
= i_get_value_size(type
, NVP_VALUE(nvp
), NVP_NELEM(nvp
));
499 size1
= nvp
->nvp_size
- NVP_VALOFF(nvp
);
500 if (size2
< 0 || size1
!= NV_ALIGN(size2
))
507 nvlist_copy_pairs(nvlist_t
*snvl
, nvlist_t
*dnvl
)
512 if ((priv
= (nvpriv_t
*)(uintptr_t)snvl
->nvl_priv
) == NULL
)
515 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
516 nvpair_t
*nvp
= &curr
->nvi_nvp
;
519 if ((err
= nvlist_add_common(dnvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
520 NVP_NELEM(nvp
), NVP_VALUE(nvp
))) != 0)
528 * Frees all memory allocated for an nvpair (like embedded lists) with
529 * the exception of the nvpair buffer itself.
532 nvpair_free(nvpair_t
*nvp
)
534 switch (NVP_TYPE(nvp
)) {
535 case DATA_TYPE_NVLIST
:
536 nvlist_free(EMBEDDED_NVL(nvp
));
538 case DATA_TYPE_NVLIST_ARRAY
: {
539 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
542 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
544 nvlist_free(nvlp
[i
]);
553 * nvlist_free - free an unpacked nvlist
556 nvlist_free(nvlist_t
*nvl
)
562 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
566 * Unpacked nvlist are linked through i_nvp_t
568 curr
= priv
->nvp_list
;
569 while (curr
!= NULL
) {
570 nvpair_t
*nvp
= &curr
->nvi_nvp
;
571 curr
= curr
->nvi_next
;
574 nvp_buf_free(nvl
, nvp
);
577 if (!(priv
->nvp_stat
& NV_STAT_EMBEDDED
))
578 nv_mem_free(priv
, nvl
, NV_ALIGN(sizeof (nvlist_t
)));
582 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
586 nvlist_contains_nvp(nvlist_t
*nvl
, nvpair_t
*nvp
)
588 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
594 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
595 if (&curr
->nvi_nvp
== nvp
)
602 * Make a copy of nvlist
606 nvlist_dup(nvlist_t
*nvl
, nvlist_t
**nvlp
, int kmflag
)
608 #if defined(_KERNEL) && !defined(_BOOT)
609 return (nvlist_xdup(nvl
, nvlp
,
610 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
612 return (nvlist_xdup(nvl
, nvlp
, nv_alloc_nosleep
));
617 nvlist_xdup(nvlist_t
*nvl
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
622 if (nvl
== NULL
|| nvlp
== NULL
)
625 if ((err
= nvlist_xalloc(&ret
, nvl
->nvl_nvflag
, nva
)) != 0)
628 if ((err
= nvlist_copy_pairs(nvl
, ret
)) != 0)
637 * Remove all with matching name
640 nvlist_remove_all(nvlist_t
*nvl
, const char *name
)
646 if (nvl
== NULL
|| name
== NULL
||
647 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
650 curr
= priv
->nvp_list
;
651 while (curr
!= NULL
) {
652 nvpair_t
*nvp
= &curr
->nvi_nvp
;
654 curr
= curr
->nvi_next
;
655 if (strcmp(name
, NVP_NAME(nvp
)) != 0)
658 nvp_buf_unlink(nvl
, nvp
);
660 nvp_buf_free(nvl
, nvp
);
669 * Remove first one with matching name and type
672 nvlist_remove(nvlist_t
*nvl
, const char *name
, data_type_t type
)
677 if (nvl
== NULL
|| name
== NULL
||
678 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
681 curr
= priv
->nvp_list
;
682 while (curr
!= NULL
) {
683 nvpair_t
*nvp
= &curr
->nvi_nvp
;
685 if (strcmp(name
, NVP_NAME(nvp
)) == 0 && NVP_TYPE(nvp
) == type
) {
686 nvp_buf_unlink(nvl
, nvp
);
688 nvp_buf_free(nvl
, nvp
);
692 curr
= curr
->nvi_next
;
699 nvlist_remove_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
701 if (nvl
== NULL
|| nvp
== NULL
)
704 nvp_buf_unlink(nvl
, nvp
);
706 nvp_buf_free(nvl
, nvp
);
711 * This function calculates the size of an nvpair value.
713 * The data argument controls the behavior in case of the data types
714 * DATA_TYPE_STRING and
715 * DATA_TYPE_STRING_ARRAY
716 * Is data == NULL then the size of the string(s) is excluded.
719 i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
)
723 if (i_validate_type_nelem(type
, nelem
) != 0)
726 /* Calculate required size for holding value */
728 case DATA_TYPE_BOOLEAN
:
731 case DATA_TYPE_BOOLEAN_VALUE
:
732 value_sz
= sizeof (boolean_t
);
735 value_sz
= sizeof (uchar_t
);
738 value_sz
= sizeof (int8_t);
740 case DATA_TYPE_UINT8
:
741 value_sz
= sizeof (uint8_t);
743 case DATA_TYPE_INT16
:
744 value_sz
= sizeof (int16_t);
746 case DATA_TYPE_UINT16
:
747 value_sz
= sizeof (uint16_t);
749 case DATA_TYPE_INT32
:
750 value_sz
= sizeof (int32_t);
752 case DATA_TYPE_UINT32
:
753 value_sz
= sizeof (uint32_t);
755 case DATA_TYPE_INT64
:
756 value_sz
= sizeof (int64_t);
758 case DATA_TYPE_UINT64
:
759 value_sz
= sizeof (uint64_t);
761 #if !defined(_KERNEL)
762 case DATA_TYPE_DOUBLE
:
763 value_sz
= sizeof (double);
766 case DATA_TYPE_STRING
:
770 value_sz
= strlen(data
) + 1;
772 case DATA_TYPE_BOOLEAN_ARRAY
:
773 value_sz
= (uint64_t)nelem
* sizeof (boolean_t
);
775 case DATA_TYPE_BYTE_ARRAY
:
776 value_sz
= (uint64_t)nelem
* sizeof (uchar_t
);
778 case DATA_TYPE_INT8_ARRAY
:
779 value_sz
= (uint64_t)nelem
* sizeof (int8_t);
781 case DATA_TYPE_UINT8_ARRAY
:
782 value_sz
= (uint64_t)nelem
* sizeof (uint8_t);
784 case DATA_TYPE_INT16_ARRAY
:
785 value_sz
= (uint64_t)nelem
* sizeof (int16_t);
787 case DATA_TYPE_UINT16_ARRAY
:
788 value_sz
= (uint64_t)nelem
* sizeof (uint16_t);
790 case DATA_TYPE_INT32_ARRAY
:
791 value_sz
= (uint64_t)nelem
* sizeof (int32_t);
793 case DATA_TYPE_UINT32_ARRAY
:
794 value_sz
= (uint64_t)nelem
* sizeof (uint32_t);
796 case DATA_TYPE_INT64_ARRAY
:
797 value_sz
= (uint64_t)nelem
* sizeof (int64_t);
799 case DATA_TYPE_UINT64_ARRAY
:
800 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
802 case DATA_TYPE_STRING_ARRAY
:
803 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
806 char *const *strs
= data
;
809 /* no alignment requirement for strings */
810 for (i
= 0; i
< nelem
; i
++) {
813 value_sz
+= strlen(strs
[i
]) + 1;
817 case DATA_TYPE_HRTIME
:
818 value_sz
= sizeof (hrtime_t
);
820 case DATA_TYPE_NVLIST
:
821 value_sz
= NV_ALIGN(sizeof (nvlist_t
));
823 case DATA_TYPE_NVLIST_ARRAY
:
824 value_sz
= (uint64_t)nelem
* sizeof (uint64_t) +
825 (uint64_t)nelem
* NV_ALIGN(sizeof (nvlist_t
));
831 return (value_sz
> INT32_MAX
? -1 : (int)value_sz
);
835 nvlist_copy_embedded(nvlist_t
*nvl
, nvlist_t
*onvl
, nvlist_t
*emb_nvl
)
840 if ((priv
= nv_priv_alloc_embedded((nvpriv_t
*)(uintptr_t)
841 nvl
->nvl_priv
)) == NULL
)
844 nvlist_init(emb_nvl
, onvl
->nvl_nvflag
, priv
);
846 if ((err
= nvlist_copy_pairs(onvl
, emb_nvl
)) != 0) {
847 nvlist_free(emb_nvl
);
848 emb_nvl
->nvl_priv
= 0;
855 * nvlist_add_common - Add new <name,value> pair to nvlist
858 nvlist_add_common(nvlist_t
*nvl
, const char *name
,
859 data_type_t type
, uint_t nelem
, const void *data
)
864 int nvp_sz
, name_sz
, value_sz
;
867 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
870 if (nelem
!= 0 && data
== NULL
)
874 * Verify type and nelem and get the value size.
875 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
876 * is the size of the string(s) included.
878 if ((value_sz
= i_get_value_size(type
, data
, nelem
)) < 0)
881 if (i_validate_nvpair_value(type
, nelem
, data
) != 0)
885 * If we're adding an nvlist or nvlist array, ensure that we are not
886 * adding the input nvlist to itself, which would cause recursion,
887 * and ensure that no NULL nvlist pointers are present.
890 case DATA_TYPE_NVLIST
:
891 if (data
== nvl
|| data
== NULL
)
894 case DATA_TYPE_NVLIST_ARRAY
: {
895 nvlist_t
**onvlp
= (nvlist_t
**)data
;
896 for (i
= 0; i
< nelem
; i
++) {
897 if (onvlp
[i
] == nvl
|| onvlp
[i
] == NULL
)
906 /* calculate sizes of the nvpair elements and the nvpair itself */
907 name_sz
= strlen(name
) + 1;
909 nvp_sz
= NVP_SIZE_CALC(name_sz
, value_sz
);
911 if ((nvp
= nvp_buf_alloc(nvl
, nvp_sz
)) == NULL
)
914 ASSERT(nvp
->nvp_size
== nvp_sz
);
915 nvp
->nvp_name_sz
= name_sz
;
916 nvp
->nvp_value_elem
= nelem
;
917 nvp
->nvp_type
= type
;
918 bcopy(name
, NVP_NAME(nvp
), name_sz
);
921 case DATA_TYPE_BOOLEAN
:
923 case DATA_TYPE_STRING_ARRAY
: {
924 char *const *strs
= data
;
925 char *buf
= NVP_VALUE(nvp
);
926 char **cstrs
= (void *)buf
;
928 /* skip pre-allocated space for pointer array */
929 buf
+= nelem
* sizeof (uint64_t);
930 for (i
= 0; i
< nelem
; i
++) {
931 int slen
= strlen(strs
[i
]) + 1;
932 bcopy(strs
[i
], buf
, slen
);
938 case DATA_TYPE_NVLIST
: {
939 nvlist_t
*nnvl
= EMBEDDED_NVL(nvp
);
940 nvlist_t
*onvl
= (nvlist_t
*)data
;
942 if ((err
= nvlist_copy_embedded(nvl
, onvl
, nnvl
)) != 0) {
943 nvp_buf_free(nvl
, nvp
);
948 case DATA_TYPE_NVLIST_ARRAY
: {
949 nvlist_t
**onvlp
= (nvlist_t
**)data
;
950 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
951 nvlist_t
*embedded
= (nvlist_t
*)
952 ((uintptr_t)nvlp
+ nelem
* sizeof (uint64_t));
954 for (i
= 0; i
< nelem
; i
++) {
955 if ((err
= nvlist_copy_embedded(nvl
,
956 onvlp
[i
], embedded
)) != 0) {
958 * Free any successfully created lists
961 nvp_buf_free(nvl
, nvp
);
965 nvlp
[i
] = embedded
++;
970 bcopy(data
, NVP_VALUE(nvp
), value_sz
);
973 /* if unique name, remove before add */
974 if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME
)
975 (void) nvlist_remove_all(nvl
, name
);
976 else if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME_TYPE
)
977 (void) nvlist_remove(nvl
, name
, type
);
979 nvp_buf_link(nvl
, nvp
);
985 nvlist_add_boolean(nvlist_t
*nvl
, const char *name
)
987 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN
, 0, NULL
));
991 nvlist_add_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t val
)
993 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_VALUE
, 1, &val
));
997 nvlist_add_byte(nvlist_t
*nvl
, const char *name
, uchar_t val
)
999 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE
, 1, &val
));
1003 nvlist_add_int8(nvlist_t
*nvl
, const char *name
, int8_t val
)
1005 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8
, 1, &val
));
1009 nvlist_add_uint8(nvlist_t
*nvl
, const char *name
, uint8_t val
)
1011 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8
, 1, &val
));
1015 nvlist_add_int16(nvlist_t
*nvl
, const char *name
, int16_t val
)
1017 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16
, 1, &val
));
1021 nvlist_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
1023 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16
, 1, &val
));
1027 nvlist_add_int32(nvlist_t
*nvl
, const char *name
, int32_t val
)
1029 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32
, 1, &val
));
1033 nvlist_add_uint32(nvlist_t
*nvl
, const char *name
, uint32_t val
)
1035 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32
, 1, &val
));
1039 nvlist_add_int64(nvlist_t
*nvl
, const char *name
, int64_t val
)
1041 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64
, 1, &val
));
1045 nvlist_add_uint64(nvlist_t
*nvl
, const char *name
, uint64_t val
)
1047 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64
, 1, &val
));
1050 #if !defined(_KERNEL)
1052 nvlist_add_double(nvlist_t
*nvl
, const char *name
, double val
)
1054 return (nvlist_add_common(nvl
, name
, DATA_TYPE_DOUBLE
, 1, &val
));
1059 nvlist_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
1061 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING
, 1, (void *)val
));
1065 nvlist_add_boolean_array(nvlist_t
*nvl
, const char *name
,
1066 boolean_t
*a
, uint_t n
)
1068 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1072 nvlist_add_byte_array(nvlist_t
*nvl
, const char *name
, uchar_t
*a
, uint_t n
)
1074 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1078 nvlist_add_int8_array(nvlist_t
*nvl
, const char *name
, int8_t *a
, uint_t n
)
1080 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1084 nvlist_add_uint8_array(nvlist_t
*nvl
, const char *name
, uint8_t *a
, uint_t n
)
1086 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1090 nvlist_add_int16_array(nvlist_t
*nvl
, const char *name
, int16_t *a
, uint_t n
)
1092 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1096 nvlist_add_uint16_array(nvlist_t
*nvl
, const char *name
, uint16_t *a
, uint_t n
)
1098 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1102 nvlist_add_int32_array(nvlist_t
*nvl
, const char *name
, int32_t *a
, uint_t n
)
1104 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1108 nvlist_add_uint32_array(nvlist_t
*nvl
, const char *name
, uint32_t *a
, uint_t n
)
1110 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1114 nvlist_add_int64_array(nvlist_t
*nvl
, const char *name
, int64_t *a
, uint_t n
)
1116 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1120 nvlist_add_uint64_array(nvlist_t
*nvl
, const char *name
, uint64_t *a
, uint_t n
)
1122 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1126 nvlist_add_string_array(nvlist_t
*nvl
, const char *name
,
1127 char *const *a
, uint_t n
)
1129 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1133 nvlist_add_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t val
)
1135 return (nvlist_add_common(nvl
, name
, DATA_TYPE_HRTIME
, 1, &val
));
1139 nvlist_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
1141 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST
, 1, val
));
1145 nvlist_add_nvlist_array(nvlist_t
*nvl
, const char *name
, nvlist_t
**a
, uint_t n
)
1147 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1150 /* reading name-value pairs */
1152 nvlist_next_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
1158 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1161 curr
= NVPAIR2I_NVP(nvp
);
1164 * Ensure that nvp is a valid nvpair on this nvlist.
1165 * NB: nvp_curr is used only as a hint so that we don't always
1166 * have to walk the list to determine if nvp is still on the list.
1169 curr
= priv
->nvp_list
;
1170 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1171 curr
= curr
->nvi_next
;
1175 priv
->nvp_curr
= curr
;
1177 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1181 nvlist_prev_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
1187 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1190 curr
= NVPAIR2I_NVP(nvp
);
1193 curr
= priv
->nvp_last
;
1194 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1195 curr
= curr
->nvi_prev
;
1199 priv
->nvp_curr
= curr
;
1201 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1205 nvlist_empty(nvlist_t
*nvl
)
1210 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1213 return (priv
->nvp_list
== NULL
);
1217 nvpair_name(nvpair_t
*nvp
)
1219 return (NVP_NAME(nvp
));
1223 nvpair_type(nvpair_t
*nvp
)
1225 return (NVP_TYPE(nvp
));
1229 nvpair_type_is_array(nvpair_t
*nvp
)
1231 data_type_t type
= NVP_TYPE(nvp
);
1233 if ((type
== DATA_TYPE_BYTE_ARRAY
) ||
1234 (type
== DATA_TYPE_UINT8_ARRAY
) ||
1235 (type
== DATA_TYPE_INT16_ARRAY
) ||
1236 (type
== DATA_TYPE_UINT16_ARRAY
) ||
1237 (type
== DATA_TYPE_INT32_ARRAY
) ||
1238 (type
== DATA_TYPE_UINT32_ARRAY
) ||
1239 (type
== DATA_TYPE_INT64_ARRAY
) ||
1240 (type
== DATA_TYPE_UINT64_ARRAY
) ||
1241 (type
== DATA_TYPE_BOOLEAN_ARRAY
) ||
1242 (type
== DATA_TYPE_STRING_ARRAY
) ||
1243 (type
== DATA_TYPE_NVLIST_ARRAY
))
1250 nvpair_value_common(nvpair_t
*nvp
, data_type_t type
, uint_t
*nelem
, void *data
)
1252 if (nvp
== NULL
|| nvpair_type(nvp
) != type
)
1256 * For non-array types, we copy the data.
1257 * For array types (including string), we set a pointer.
1260 case DATA_TYPE_BOOLEAN
:
1265 case DATA_TYPE_BOOLEAN_VALUE
:
1266 case DATA_TYPE_BYTE
:
1267 case DATA_TYPE_INT8
:
1268 case DATA_TYPE_UINT8
:
1269 case DATA_TYPE_INT16
:
1270 case DATA_TYPE_UINT16
:
1271 case DATA_TYPE_INT32
:
1272 case DATA_TYPE_UINT32
:
1273 case DATA_TYPE_INT64
:
1274 case DATA_TYPE_UINT64
:
1275 case DATA_TYPE_HRTIME
:
1276 #if !defined(_KERNEL)
1277 case DATA_TYPE_DOUBLE
:
1281 bcopy(NVP_VALUE(nvp
), data
,
1282 (size_t)i_get_value_size(type
, NULL
, 1));
1287 case DATA_TYPE_NVLIST
:
1288 case DATA_TYPE_STRING
:
1291 *(void **)data
= (void *)NVP_VALUE(nvp
);
1296 case DATA_TYPE_BOOLEAN_ARRAY
:
1297 case DATA_TYPE_BYTE_ARRAY
:
1298 case DATA_TYPE_INT8_ARRAY
:
1299 case DATA_TYPE_UINT8_ARRAY
:
1300 case DATA_TYPE_INT16_ARRAY
:
1301 case DATA_TYPE_UINT16_ARRAY
:
1302 case DATA_TYPE_INT32_ARRAY
:
1303 case DATA_TYPE_UINT32_ARRAY
:
1304 case DATA_TYPE_INT64_ARRAY
:
1305 case DATA_TYPE_UINT64_ARRAY
:
1306 case DATA_TYPE_STRING_ARRAY
:
1307 case DATA_TYPE_NVLIST_ARRAY
:
1308 if (nelem
== NULL
|| data
== NULL
)
1310 if ((*nelem
= NVP_NELEM(nvp
)) != 0)
1311 *(void **)data
= (void *)NVP_VALUE(nvp
);
1313 *(void **)data
= NULL
;
1324 nvlist_lookup_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
1325 uint_t
*nelem
, void *data
)
1331 if (name
== NULL
|| nvl
== NULL
||
1332 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1335 if (!(nvl
->nvl_nvflag
& (NV_UNIQUE_NAME
| NV_UNIQUE_NAME_TYPE
)))
1338 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
1339 nvp
= &curr
->nvi_nvp
;
1341 if (strcmp(name
, NVP_NAME(nvp
)) == 0 && NVP_TYPE(nvp
) == type
)
1342 return (nvpair_value_common(nvp
, type
, nelem
, data
));
1349 nvlist_lookup_boolean(nvlist_t
*nvl
, const char *name
)
1351 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BOOLEAN
, NULL
, NULL
));
1355 nvlist_lookup_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t
*val
)
1357 return (nvlist_lookup_common(nvl
, name
,
1358 DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1362 nvlist_lookup_byte(nvlist_t
*nvl
, const char *name
, uchar_t
*val
)
1364 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE
, NULL
, val
));
1368 nvlist_lookup_int8(nvlist_t
*nvl
, const char *name
, int8_t *val
)
1370 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8
, NULL
, val
));
1374 nvlist_lookup_uint8(nvlist_t
*nvl
, const char *name
, uint8_t *val
)
1376 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8
, NULL
, val
));
1380 nvlist_lookup_int16(nvlist_t
*nvl
, const char *name
, int16_t *val
)
1382 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16
, NULL
, val
));
1386 nvlist_lookup_uint16(nvlist_t
*nvl
, const char *name
, uint16_t *val
)
1388 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16
, NULL
, val
));
1392 nvlist_lookup_int32(nvlist_t
*nvl
, const char *name
, int32_t *val
)
1394 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32
, NULL
, val
));
1398 nvlist_lookup_uint32(nvlist_t
*nvl
, const char *name
, uint32_t *val
)
1400 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32
, NULL
, val
));
1404 nvlist_lookup_int64(nvlist_t
*nvl
, const char *name
, int64_t *val
)
1406 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64
, NULL
, val
));
1410 nvlist_lookup_uint64(nvlist_t
*nvl
, const char *name
, uint64_t *val
)
1412 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64
, NULL
, val
));
1415 #if !defined(_KERNEL)
1417 nvlist_lookup_double(nvlist_t
*nvl
, const char *name
, double *val
)
1419 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_DOUBLE
, NULL
, val
));
1424 nvlist_lookup_string(nvlist_t
*nvl
, const char *name
, char **val
)
1426 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING
, NULL
, val
));
1430 nvlist_lookup_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
**val
)
1432 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST
, NULL
, val
));
1436 nvlist_lookup_boolean_array(nvlist_t
*nvl
, const char *name
,
1437 boolean_t
**a
, uint_t
*n
)
1439 return (nvlist_lookup_common(nvl
, name
,
1440 DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1444 nvlist_lookup_byte_array(nvlist_t
*nvl
, const char *name
,
1445 uchar_t
**a
, uint_t
*n
)
1447 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1451 nvlist_lookup_int8_array(nvlist_t
*nvl
, const char *name
, int8_t **a
, uint_t
*n
)
1453 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1457 nvlist_lookup_uint8_array(nvlist_t
*nvl
, const char *name
,
1458 uint8_t **a
, uint_t
*n
)
1460 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1464 nvlist_lookup_int16_array(nvlist_t
*nvl
, const char *name
,
1465 int16_t **a
, uint_t
*n
)
1467 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1471 nvlist_lookup_uint16_array(nvlist_t
*nvl
, const char *name
,
1472 uint16_t **a
, uint_t
*n
)
1474 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1478 nvlist_lookup_int32_array(nvlist_t
*nvl
, const char *name
,
1479 int32_t **a
, uint_t
*n
)
1481 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1485 nvlist_lookup_uint32_array(nvlist_t
*nvl
, const char *name
,
1486 uint32_t **a
, uint_t
*n
)
1488 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1492 nvlist_lookup_int64_array(nvlist_t
*nvl
, const char *name
,
1493 int64_t **a
, uint_t
*n
)
1495 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1499 nvlist_lookup_uint64_array(nvlist_t
*nvl
, const char *name
,
1500 uint64_t **a
, uint_t
*n
)
1502 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1506 nvlist_lookup_string_array(nvlist_t
*nvl
, const char *name
,
1507 char ***a
, uint_t
*n
)
1509 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1513 nvlist_lookup_nvlist_array(nvlist_t
*nvl
, const char *name
,
1514 nvlist_t
***a
, uint_t
*n
)
1516 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1520 nvlist_lookup_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t
*val
)
1522 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_HRTIME
, NULL
, val
));
1526 nvlist_lookup_pairs(nvlist_t
*nvl
, int flag
, ...)
1530 int noentok
= (flag
& NV_FLAG_NOENTOK
? 1 : 0);
1534 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
1539 switch (type
= va_arg(ap
, data_type_t
)) {
1540 case DATA_TYPE_BOOLEAN
:
1541 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, NULL
);
1544 case DATA_TYPE_BOOLEAN_VALUE
:
1545 case DATA_TYPE_BYTE
:
1546 case DATA_TYPE_INT8
:
1547 case DATA_TYPE_UINT8
:
1548 case DATA_TYPE_INT16
:
1549 case DATA_TYPE_UINT16
:
1550 case DATA_TYPE_INT32
:
1551 case DATA_TYPE_UINT32
:
1552 case DATA_TYPE_INT64
:
1553 case DATA_TYPE_UINT64
:
1554 case DATA_TYPE_HRTIME
:
1555 case DATA_TYPE_STRING
:
1556 case DATA_TYPE_NVLIST
:
1557 #if !defined(_KERNEL)
1558 case DATA_TYPE_DOUBLE
:
1560 val
= va_arg(ap
, void *);
1561 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, val
);
1564 case DATA_TYPE_BYTE_ARRAY
:
1565 case DATA_TYPE_BOOLEAN_ARRAY
:
1566 case DATA_TYPE_INT8_ARRAY
:
1567 case DATA_TYPE_UINT8_ARRAY
:
1568 case DATA_TYPE_INT16_ARRAY
:
1569 case DATA_TYPE_UINT16_ARRAY
:
1570 case DATA_TYPE_INT32_ARRAY
:
1571 case DATA_TYPE_UINT32_ARRAY
:
1572 case DATA_TYPE_INT64_ARRAY
:
1573 case DATA_TYPE_UINT64_ARRAY
:
1574 case DATA_TYPE_STRING_ARRAY
:
1575 case DATA_TYPE_NVLIST_ARRAY
:
1576 val
= va_arg(ap
, void *);
1577 nelem
= va_arg(ap
, uint_t
*);
1578 ret
= nvlist_lookup_common(nvl
, name
, type
, nelem
, val
);
1585 if (ret
== ENOENT
&& noentok
)
1594 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1595 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1596 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1597 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1598 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1599 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1600 * code also supports "a.d[3]e[1]" syntax).
1602 * If 'ip' is non-NULL and the last name component is an array, return the
1603 * value of the "...[index]" array index in *ip. For an array reference that
1604 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1605 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1606 * inside the 'name' string where the syntax error was detected.
1609 nvlist_lookup_nvpair_ei_sep(nvlist_t
*nvl
, const char *name
, const char sep
,
1610 nvpair_t
**ret
, int *ip
, char **ep
)
1621 *ip
= -1; /* not indexed */
1625 if ((nvl
== NULL
) || (name
== NULL
))
1628 /* step through components of name */
1629 for (np
= name
; np
&& *np
; np
= sepp
) {
1630 /* ensure unique names */
1631 if (!(nvl
->nvl_nvflag
& NV_UNIQUE_NAME
))
1634 /* skip white space */
1635 skip_whitespace(np
);
1639 /* set 'sepp' to end of current component 'np' */
1641 sepp
= strchr(np
, sep
);
1645 /* find start of next "[ index ]..." */
1646 idxp
= strchr(np
, '[');
1648 /* if sepp comes first, set idxp to NULL */
1649 if (sepp
&& idxp
&& (sepp
< idxp
))
1653 * At this point 'idxp' is set if there is an index
1654 * expected for the current component.
1657 /* set 'n' to length of current 'np' name component */
1660 /* keep sepp up to date for *ep use as we advance */
1661 skip_whitespace(idxp
);
1664 /* determine the index value */
1665 #if defined(_KERNEL) && !defined(_BOOT)
1666 if (ddi_strtol(idxp
, &idxep
, 0, &idx
))
1669 idx
= strtol(idxp
, &idxep
, 0);
1674 /* keep sepp up to date for *ep use as we advance */
1677 /* skip white space index value and check for ']' */
1678 skip_whitespace(sepp
);
1682 /* for embedded arrays, support C syntax: "a[1].b" */
1683 skip_whitespace(sepp
);
1684 if (sep
&& (*sepp
== sep
))
1692 /* trim trailing whitespace by reducing length of 'np' */
1695 for (n
--; (np
[n
] == ' ') || (np
[n
] == '\t'); n
--)
1699 /* skip whitespace, and set sepp to NULL if complete */
1701 skip_whitespace(sepp
);
1708 * o 'n' is the length of current 'np' component.
1709 * o 'idxp' is set if there was an index, and value 'idx'.
1710 * o 'sepp' is set to the beginning of the next component,
1711 * and set to NULL if we have no more components.
1713 * Search for nvpair with matching component name.
1715 for (nvp
= nvlist_next_nvpair(nvl
, NULL
); nvp
!= NULL
;
1716 nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
1718 /* continue if no match on name */
1719 if (strncmp(np
, nvpair_name(nvp
), n
) ||
1720 (strlen(nvpair_name(nvp
)) != n
))
1723 /* if indexed, verify type is array oriented */
1724 if (idxp
&& !nvpair_type_is_array(nvp
))
1728 * Full match found, return nvp and idx if this
1729 * was the last component.
1735 *ip
= (int)idx
; /* return index */
1736 return (0); /* found */
1740 * More components: current match must be
1741 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1742 * to support going deeper.
1744 if (nvpair_type(nvp
) == DATA_TYPE_NVLIST
) {
1745 nvl
= EMBEDDED_NVL(nvp
);
1747 } else if (nvpair_type(nvp
) == DATA_TYPE_NVLIST_ARRAY
) {
1748 (void) nvpair_value_nvlist_array(nvp
,
1749 &nva
, (uint_t
*)&n
);
1750 if ((n
< 0) || (idx
>= n
))
1756 /* type does not support more levels */
1760 goto fail
; /* 'name' not found */
1762 /* search for match of next component in embedded 'nvl' list */
1765 fail
: if (ep
&& sepp
)
1771 * Return pointer to nvpair with specified 'name'.
1774 nvlist_lookup_nvpair(nvlist_t
*nvl
, const char *name
, nvpair_t
**ret
)
1776 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, 0, ret
, NULL
, NULL
));
1780 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1781 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1784 int nvlist_lookup_nvpair_embedded_index(nvlist_t
*nvl
,
1785 const char *name
, nvpair_t
**ret
, int *ip
, char **ep
)
1787 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, '.', ret
, ip
, ep
));
1791 nvlist_exists(nvlist_t
*nvl
, const char *name
)
1797 if (name
== NULL
|| nvl
== NULL
||
1798 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1801 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
1802 nvp
= &curr
->nvi_nvp
;
1804 if (strcmp(name
, NVP_NAME(nvp
)) == 0)
1812 nvpair_value_boolean_value(nvpair_t
*nvp
, boolean_t
*val
)
1814 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1818 nvpair_value_byte(nvpair_t
*nvp
, uchar_t
*val
)
1820 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE
, NULL
, val
));
1824 nvpair_value_int8(nvpair_t
*nvp
, int8_t *val
)
1826 return (nvpair_value_common(nvp
, DATA_TYPE_INT8
, NULL
, val
));
1830 nvpair_value_uint8(nvpair_t
*nvp
, uint8_t *val
)
1832 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8
, NULL
, val
));
1836 nvpair_value_int16(nvpair_t
*nvp
, int16_t *val
)
1838 return (nvpair_value_common(nvp
, DATA_TYPE_INT16
, NULL
, val
));
1842 nvpair_value_uint16(nvpair_t
*nvp
, uint16_t *val
)
1844 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16
, NULL
, val
));
1848 nvpair_value_int32(nvpair_t
*nvp
, int32_t *val
)
1850 return (nvpair_value_common(nvp
, DATA_TYPE_INT32
, NULL
, val
));
1854 nvpair_value_uint32(nvpair_t
*nvp
, uint32_t *val
)
1856 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32
, NULL
, val
));
1860 nvpair_value_int64(nvpair_t
*nvp
, int64_t *val
)
1862 return (nvpair_value_common(nvp
, DATA_TYPE_INT64
, NULL
, val
));
1866 nvpair_value_uint64(nvpair_t
*nvp
, uint64_t *val
)
1868 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64
, NULL
, val
));
1871 #if !defined(_KERNEL)
1873 nvpair_value_double(nvpair_t
*nvp
, double *val
)
1875 return (nvpair_value_common(nvp
, DATA_TYPE_DOUBLE
, NULL
, val
));
1880 nvpair_value_string(nvpair_t
*nvp
, char **val
)
1882 return (nvpair_value_common(nvp
, DATA_TYPE_STRING
, NULL
, val
));
1886 nvpair_value_nvlist(nvpair_t
*nvp
, nvlist_t
**val
)
1888 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST
, NULL
, val
));
1892 nvpair_value_boolean_array(nvpair_t
*nvp
, boolean_t
**val
, uint_t
*nelem
)
1894 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_ARRAY
, nelem
, val
));
1898 nvpair_value_byte_array(nvpair_t
*nvp
, uchar_t
**val
, uint_t
*nelem
)
1900 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE_ARRAY
, nelem
, val
));
1904 nvpair_value_int8_array(nvpair_t
*nvp
, int8_t **val
, uint_t
*nelem
)
1906 return (nvpair_value_common(nvp
, DATA_TYPE_INT8_ARRAY
, nelem
, val
));
1910 nvpair_value_uint8_array(nvpair_t
*nvp
, uint8_t **val
, uint_t
*nelem
)
1912 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8_ARRAY
, nelem
, val
));
1916 nvpair_value_int16_array(nvpair_t
*nvp
, int16_t **val
, uint_t
*nelem
)
1918 return (nvpair_value_common(nvp
, DATA_TYPE_INT16_ARRAY
, nelem
, val
));
1922 nvpair_value_uint16_array(nvpair_t
*nvp
, uint16_t **val
, uint_t
*nelem
)
1924 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16_ARRAY
, nelem
, val
));
1928 nvpair_value_int32_array(nvpair_t
*nvp
, int32_t **val
, uint_t
*nelem
)
1930 return (nvpair_value_common(nvp
, DATA_TYPE_INT32_ARRAY
, nelem
, val
));
1934 nvpair_value_uint32_array(nvpair_t
*nvp
, uint32_t **val
, uint_t
*nelem
)
1936 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32_ARRAY
, nelem
, val
));
1940 nvpair_value_int64_array(nvpair_t
*nvp
, int64_t **val
, uint_t
*nelem
)
1942 return (nvpair_value_common(nvp
, DATA_TYPE_INT64_ARRAY
, nelem
, val
));
1946 nvpair_value_uint64_array(nvpair_t
*nvp
, uint64_t **val
, uint_t
*nelem
)
1948 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64_ARRAY
, nelem
, val
));
1952 nvpair_value_string_array(nvpair_t
*nvp
, char ***val
, uint_t
*nelem
)
1954 return (nvpair_value_common(nvp
, DATA_TYPE_STRING_ARRAY
, nelem
, val
));
1958 nvpair_value_nvlist_array(nvpair_t
*nvp
, nvlist_t
***val
, uint_t
*nelem
)
1960 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST_ARRAY
, nelem
, val
));
1964 nvpair_value_hrtime(nvpair_t
*nvp
, hrtime_t
*val
)
1966 return (nvpair_value_common(nvp
, DATA_TYPE_HRTIME
, NULL
, val
));
1970 * Add specified pair to the list.
1973 nvlist_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
1975 if (nvl
== NULL
|| nvp
== NULL
)
1978 return (nvlist_add_common(nvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
1979 NVP_NELEM(nvp
), NVP_VALUE(nvp
)));
1983 * Merge the supplied nvlists and put the result in dst.
1984 * The merged list will contain all names specified in both lists,
1985 * the values are taken from nvl in the case of duplicates.
1986 * Return 0 on success.
1990 nvlist_merge(nvlist_t
*dst
, nvlist_t
*nvl
, int flag
)
1992 if (nvl
== NULL
|| dst
== NULL
)
1996 return (nvlist_copy_pairs(nvl
, dst
));
2002 * Encoding related routines
2004 #define NVS_OP_ENCODE 0
2005 #define NVS_OP_DECODE 1
2006 #define NVS_OP_GETSIZE 2
2008 typedef struct nvs_ops nvs_ops_t
;
2012 const nvs_ops_t
*nvs_ops
;
2018 * nvs operations are:
2020 * encoding / decoding of a nvlist header (nvlist_t)
2021 * calculates the size used for header and end detection
2024 * responsible for the first part of encoding / decoding of an nvpair
2025 * calculates the decoded size of an nvpair
2028 * second part of encoding / decoding of an nvpair
2031 * calculates the encoding size of an nvpair
2034 * encodes the end detection mark (zeros).
2037 int (*nvs_nvlist
)(nvstream_t
*, nvlist_t
*, size_t *);
2038 int (*nvs_nvpair
)(nvstream_t
*, nvpair_t
*, size_t *);
2039 int (*nvs_nvp_op
)(nvstream_t
*, nvpair_t
*);
2040 int (*nvs_nvp_size
)(nvstream_t
*, nvpair_t
*, size_t *);
2041 int (*nvs_nvl_fini
)(nvstream_t
*);
2045 char nvh_encoding
; /* nvs encoding method */
2046 char nvh_endian
; /* nvs endian */
2047 char nvh_reserved1
; /* reserved for future use */
2048 char nvh_reserved2
; /* reserved for future use */
2052 nvs_encode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2054 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2058 * Walk nvpair in list and encode each nvpair
2060 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
2061 if (nvs
->nvs_ops
->nvs_nvpair(nvs
, &curr
->nvi_nvp
, NULL
) != 0)
2064 return (nvs
->nvs_ops
->nvs_nvl_fini(nvs
));
2068 nvs_decode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2075 * Get decoded size of next pair in stream, alloc
2076 * memory for nvpair_t, then decode the nvpair
2078 while ((err
= nvs
->nvs_ops
->nvs_nvpair(nvs
, NULL
, &nvsize
)) == 0) {
2079 if (nvsize
== 0) /* end of list */
2082 /* make sure len makes sense */
2083 if (nvsize
< NVP_SIZE_CALC(1, 0))
2086 if ((nvp
= nvp_buf_alloc(nvl
, nvsize
)) == NULL
)
2089 if ((err
= nvs
->nvs_ops
->nvs_nvp_op(nvs
, nvp
)) != 0) {
2090 nvp_buf_free(nvl
, nvp
);
2094 if (i_validate_nvpair(nvp
) != 0) {
2096 nvp_buf_free(nvl
, nvp
);
2100 nvp_buf_link(nvl
, nvp
);
2106 nvs_getsize_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2108 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2110 uint64_t nvsize
= *buflen
;
2114 * Get encoded size of nvpairs in nvlist
2116 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2117 if (nvs
->nvs_ops
->nvs_nvp_size(nvs
, &curr
->nvi_nvp
, &size
) != 0)
2120 if ((nvsize
+= size
) > INT32_MAX
)
2129 nvs_operation(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2133 if (nvl
->nvl_priv
== 0)
2137 * Perform the operation, starting with header, then each nvpair
2139 if ((err
= nvs
->nvs_ops
->nvs_nvlist(nvs
, nvl
, buflen
)) != 0)
2142 switch (nvs
->nvs_op
) {
2144 err
= nvs_encode_pairs(nvs
, nvl
);
2148 err
= nvs_decode_pairs(nvs
, nvl
);
2151 case NVS_OP_GETSIZE
:
2152 err
= nvs_getsize_pairs(nvs
, nvl
, buflen
);
2163 nvs_embedded(nvstream_t
*nvs
, nvlist_t
*embedded
)
2165 switch (nvs
->nvs_op
) {
2167 return (nvs_operation(nvs
, embedded
, NULL
));
2169 case NVS_OP_DECODE
: {
2173 if (embedded
->nvl_version
!= NV_VERSION
)
2176 if ((priv
= nv_priv_alloc_embedded(nvs
->nvs_priv
)) == NULL
)
2179 nvlist_init(embedded
, embedded
->nvl_nvflag
, priv
);
2181 if ((err
= nvs_operation(nvs
, embedded
, NULL
)) != 0)
2182 nvlist_free(embedded
);
2193 nvs_embedded_nvl_array(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2195 size_t nelem
= NVP_NELEM(nvp
);
2196 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
2199 switch (nvs
->nvs_op
) {
2201 for (i
= 0; i
< nelem
; i
++)
2202 if (nvs_embedded(nvs
, nvlp
[i
]) != 0)
2206 case NVS_OP_DECODE
: {
2207 size_t len
= nelem
* sizeof (uint64_t);
2208 nvlist_t
*embedded
= (nvlist_t
*)((uintptr_t)nvlp
+ len
);
2210 bzero(nvlp
, len
); /* don't trust packed data */
2211 for (i
= 0; i
< nelem
; i
++) {
2212 if (nvs_embedded(nvs
, embedded
) != 0) {
2217 nvlp
[i
] = embedded
++;
2221 case NVS_OP_GETSIZE
: {
2222 uint64_t nvsize
= 0;
2224 for (i
= 0; i
< nelem
; i
++) {
2227 if (nvs_operation(nvs
, nvlp
[i
], &nvp_sz
) != 0)
2230 if ((nvsize
+= nvp_sz
) > INT32_MAX
)
2244 static int nvs_native(nvstream_t
*, nvlist_t
*, char *, size_t *);
2245 static int nvs_xdr(nvstream_t
*, nvlist_t
*, char *, size_t *);
2248 * Common routine for nvlist operations:
2249 * encode, decode, getsize (encoded size).
2252 nvlist_common(nvlist_t
*nvl
, char *buf
, size_t *buflen
, int encoding
,
2258 #ifdef _LITTLE_ENDIAN
2259 int host_endian
= 1;
2261 int host_endian
= 0;
2262 #endif /* _LITTLE_ENDIAN */
2263 nvs_header_t
*nvh
= (void *)buf
;
2265 if (buflen
== NULL
|| nvl
== NULL
||
2266 (nvs
.nvs_priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2269 nvs
.nvs_op
= nvs_op
;
2272 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2273 * a buffer is allocated. The first 4 bytes in the buffer are
2274 * used for encoding method and host endian.
2278 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2281 nvh
->nvh_encoding
= encoding
;
2282 nvh
->nvh_endian
= nvl_endian
= host_endian
;
2283 nvh
->nvh_reserved1
= 0;
2284 nvh
->nvh_reserved2
= 0;
2288 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2291 /* get method of encoding from first byte */
2292 encoding
= nvh
->nvh_encoding
;
2293 nvl_endian
= nvh
->nvh_endian
;
2296 case NVS_OP_GETSIZE
:
2297 nvl_endian
= host_endian
;
2300 * add the size for encoding
2302 *buflen
= sizeof (nvs_header_t
);
2310 * Create an nvstream with proper encoding method
2313 case NV_ENCODE_NATIVE
:
2315 * check endianness, in case we are unpacking
2318 if (nvl_endian
!= host_endian
)
2320 err
= nvs_native(&nvs
, nvl
, buf
, buflen
);
2323 err
= nvs_xdr(&nvs
, nvl
, buf
, buflen
);
2334 nvlist_size(nvlist_t
*nvl
, size_t *size
, int encoding
)
2336 return (nvlist_common(nvl
, NULL
, size
, encoding
, NVS_OP_GETSIZE
));
2340 * Pack nvlist into contiguous memory
2344 nvlist_pack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2347 #if defined(_KERNEL) && !defined(_BOOT)
2348 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
,
2349 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
2351 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
, nv_alloc_nosleep
));
2356 nvlist_xpack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2364 if (nva
== NULL
|| nvl
== NULL
|| bufp
== NULL
|| buflen
== NULL
)
2368 return (nvlist_common(nvl
, *bufp
, buflen
, encoding
,
2372 * Here is a difficult situation:
2373 * 1. The nvlist has fixed allocator properties.
2374 * All other nvlist routines (like nvlist_add_*, ...) use
2376 * 2. When using nvlist_pack() the user can specify his own
2377 * allocator properties (e.g. by using KM_NOSLEEP).
2379 * We use the user specified properties (2). A clearer solution
2380 * will be to remove the kmflag from nvlist_pack(), but we will
2381 * not change the interface.
2383 nv_priv_init(&nvpriv
, nva
, 0);
2385 if (err
= nvlist_size(nvl
, &alloc_size
, encoding
))
2388 if ((buf
= nv_mem_zalloc(&nvpriv
, alloc_size
)) == NULL
)
2391 if ((err
= nvlist_common(nvl
, buf
, &alloc_size
, encoding
,
2392 NVS_OP_ENCODE
)) != 0) {
2393 nv_mem_free(&nvpriv
, buf
, alloc_size
);
2395 *buflen
= alloc_size
;
2403 * Unpack buf into an nvlist_t
2407 nvlist_unpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, int kmflag
)
2409 #if defined(_KERNEL) && !defined(_BOOT)
2410 return (nvlist_xunpack(buf
, buflen
, nvlp
,
2411 (kmflag
== KM_SLEEP
? nv_alloc_sleep
: nv_alloc_nosleep
)));
2413 return (nvlist_xunpack(buf
, buflen
, nvlp
, nv_alloc_nosleep
));
2418 nvlist_xunpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
2426 if ((err
= nvlist_xalloc(&nvl
, 0, nva
)) != 0)
2429 if ((err
= nvlist_common(nvl
, buf
, &buflen
, 0, NVS_OP_DECODE
)) != 0)
2438 * Native encoding functions
2442 * This structure is used when decoding a packed nvpair in
2443 * the native format. n_base points to a buffer containing the
2444 * packed nvpair. n_end is a pointer to the end of the buffer.
2445 * (n_end actually points to the first byte past the end of the
2446 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2447 * It points to the current data that we are decoding.
2448 * The amount of data left in the buffer is equal to n_end - n_curr.
2449 * n_flag is used to recognize a packed embedded list.
2458 nvs_native_create(nvstream_t
*nvs
, nvs_native_t
*native
, char *buf
,
2461 switch (nvs
->nvs_op
) {
2464 nvs
->nvs_private
= native
;
2465 native
->n_curr
= native
->n_base
= buf
;
2466 native
->n_end
= buf
+ buflen
;
2470 case NVS_OP_GETSIZE
:
2471 nvs
->nvs_private
= native
;
2472 native
->n_curr
= native
->n_base
= native
->n_end
= NULL
;
2482 nvs_native_destroy(nvstream_t
*nvs
)
2487 native_cp(nvstream_t
*nvs
, void *buf
, size_t size
)
2489 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2491 if (native
->n_curr
+ size
> native
->n_end
)
2495 * The bcopy() below eliminates alignment requirement
2496 * on the buffer (stream) and is preferred over direct access.
2498 switch (nvs
->nvs_op
) {
2500 bcopy(buf
, native
->n_curr
, size
);
2503 bcopy(native
->n_curr
, buf
, size
);
2509 native
->n_curr
+= size
;
2514 * operate on nvlist_t header
2517 nvs_native_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2519 nvs_native_t
*native
= nvs
->nvs_private
;
2521 switch (nvs
->nvs_op
) {
2525 return (0); /* packed embedded list */
2529 /* copy version and nvflag of the nvlist_t */
2530 if (native_cp(nvs
, &nvl
->nvl_version
, sizeof (int32_t)) != 0 ||
2531 native_cp(nvs
, &nvl
->nvl_nvflag
, sizeof (int32_t)) != 0)
2536 case NVS_OP_GETSIZE
:
2538 * if calculate for packed embedded list
2539 * 4 for end of the embedded list
2541 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2542 * and 4 for end of the entire list
2544 if (native
->n_flag
) {
2548 *size
+= 2 * sizeof (int32_t) + 4;
2559 nvs_native_nvl_fini(nvstream_t
*nvs
)
2561 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2562 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2564 * Add 4 zero bytes at end of nvlist. They are used
2565 * for end detection by the decode routine.
2567 if (native
->n_curr
+ sizeof (int) > native
->n_end
)
2570 bzero(native
->n_curr
, sizeof (int));
2571 native
->n_curr
+= sizeof (int);
2578 nvpair_native_embedded(nvstream_t
*nvs
, nvpair_t
*nvp
)
2580 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2581 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2582 nvlist_t
*packed
= (void *)
2583 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2585 * Null out the pointer that is meaningless in the packed
2586 * structure. The address may not be aligned, so we have
2589 bzero(&packed
->nvl_priv
, sizeof (packed
->nvl_priv
));
2592 return (nvs_embedded(nvs
, EMBEDDED_NVL(nvp
)));
2596 nvpair_native_embedded_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2598 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2599 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2600 char *value
= native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
);
2601 size_t len
= NVP_NELEM(nvp
) * sizeof (uint64_t);
2602 nvlist_t
*packed
= (nvlist_t
*)((uintptr_t)value
+ len
);
2605 * Null out pointers that are meaningless in the packed
2606 * structure. The addresses may not be aligned, so we have
2611 for (i
= 0; i
< NVP_NELEM(nvp
); i
++, packed
++)
2613 * Null out the pointer that is meaningless in the
2614 * packed structure. The address may not be aligned,
2615 * so we have to use bzero.
2617 bzero(&packed
->nvl_priv
, sizeof (packed
->nvl_priv
));
2620 return (nvs_embedded_nvl_array(nvs
, nvp
, NULL
));
2624 nvpair_native_string_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2626 switch (nvs
->nvs_op
) {
2627 case NVS_OP_ENCODE
: {
2628 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2629 uint64_t *strp
= (void *)
2630 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2632 * Null out pointers that are meaningless in the packed
2633 * structure. The addresses may not be aligned, so we have
2636 bzero(strp
, NVP_NELEM(nvp
) * sizeof (uint64_t));
2639 case NVS_OP_DECODE
: {
2640 char **strp
= (void *)NVP_VALUE(nvp
);
2641 char *buf
= ((char *)strp
+ NVP_NELEM(nvp
) * sizeof (uint64_t));
2644 for (i
= 0; i
< NVP_NELEM(nvp
); i
++) {
2646 buf
+= strlen(buf
) + 1;
2654 nvs_native_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2661 * We do the initial bcopy of the data before we look at
2662 * the nvpair type, because when we're decoding, we won't
2663 * have the correct values for the pair until we do the bcopy.
2665 switch (nvs
->nvs_op
) {
2668 if (native_cp(nvs
, nvp
, nvp
->nvp_size
) != 0)
2675 /* verify nvp_name_sz, check the name string length */
2676 if (i_validate_nvpair_name(nvp
) != 0)
2679 type
= NVP_TYPE(nvp
);
2682 * Verify type and nelem and get the value size.
2683 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2684 * is the size of the string(s) excluded.
2686 if ((value_sz
= i_get_value_size(type
, NULL
, NVP_NELEM(nvp
))) < 0)
2689 if (NVP_SIZE_CALC(nvp
->nvp_name_sz
, value_sz
) > nvp
->nvp_size
)
2693 case DATA_TYPE_NVLIST
:
2694 ret
= nvpair_native_embedded(nvs
, nvp
);
2696 case DATA_TYPE_NVLIST_ARRAY
:
2697 ret
= nvpair_native_embedded_array(nvs
, nvp
);
2699 case DATA_TYPE_STRING_ARRAY
:
2700 nvpair_native_string_array(nvs
, nvp
);
2710 nvs_native_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2712 uint64_t nvp_sz
= nvp
->nvp_size
;
2714 switch (NVP_TYPE(nvp
)) {
2715 case DATA_TYPE_NVLIST
: {
2718 if (nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
) != 0)
2724 case DATA_TYPE_NVLIST_ARRAY
: {
2727 if (nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
) != 0)
2737 if (nvp_sz
> INT32_MAX
)
2746 nvs_native_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2748 switch (nvs
->nvs_op
) {
2750 return (nvs_native_nvp_op(nvs
, nvp
));
2752 case NVS_OP_DECODE
: {
2753 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2756 /* try to read the size value from the stream */
2757 if (native
->n_curr
+ sizeof (int32_t) > native
->n_end
)
2759 bcopy(native
->n_curr
, &decode_len
, sizeof (int32_t));
2761 /* sanity check the size value */
2762 if (decode_len
< 0 ||
2763 decode_len
> native
->n_end
- native
->n_curr
)
2769 * If at the end of the stream then move the cursor
2770 * forward, otherwise nvpair_native_op() will read
2771 * the entire nvpair at the same cursor position.
2774 native
->n_curr
+= sizeof (int32_t);
2785 static const nvs_ops_t nvs_native_ops
= {
2789 nvs_native_nvp_size
,
2794 nvs_native(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
2796 nvs_native_t native
;
2799 nvs
->nvs_ops
= &nvs_native_ops
;
2801 if ((err
= nvs_native_create(nvs
, &native
, buf
+ sizeof (nvs_header_t
),
2802 *buflen
- sizeof (nvs_header_t
))) != 0)
2805 err
= nvs_operation(nvs
, nvl
, buflen
);
2807 nvs_native_destroy(nvs
);
2813 * XDR encoding functions
2815 * An xdr packed nvlist is encoded as:
2817 * - encoding methode and host endian (4 bytes)
2818 * - nvl_version (4 bytes)
2819 * - nvl_nvflag (4 bytes)
2821 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2822 * - encoded size of the nvpair (4 bytes)
2823 * - decoded size of the nvpair (4 bytes)
2824 * - name string, (4 + sizeof(NV_ALIGN4(string))
2825 * a string is coded as size (4 bytes) and data
2826 * - data type (4 bytes)
2827 * - number of elements in the nvpair (4 bytes)
2830 * - 2 zero's for end of the entire list (8 bytes)
2833 nvs_xdr_create(nvstream_t
*nvs
, XDR
*xdr
, char *buf
, size_t buflen
)
2835 /* xdr data must be 4 byte aligned */
2836 if ((ulong_t
)buf
% 4 != 0)
2839 switch (nvs
->nvs_op
) {
2841 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_ENCODE
);
2842 nvs
->nvs_private
= xdr
;
2845 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_DECODE
);
2846 nvs
->nvs_private
= xdr
;
2848 case NVS_OP_GETSIZE
:
2849 nvs
->nvs_private
= NULL
;
2857 nvs_xdr_destroy(nvstream_t
*nvs
)
2859 switch (nvs
->nvs_op
) {
2862 xdr_destroy((XDR
*)nvs
->nvs_private
);
2870 nvs_xdr_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2872 switch (nvs
->nvs_op
) {
2874 case NVS_OP_DECODE
: {
2875 XDR
*xdr
= nvs
->nvs_private
;
2877 if (!xdr_int(xdr
, &nvl
->nvl_version
) ||
2878 !xdr_u_int(xdr
, &nvl
->nvl_nvflag
))
2882 case NVS_OP_GETSIZE
: {
2884 * 2 * 4 for nvl_version + nvl_nvflag
2885 * and 8 for end of the entire list
2897 nvs_xdr_nvl_fini(nvstream_t
*nvs
)
2899 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2900 XDR
*xdr
= nvs
->nvs_private
;
2903 if (!xdr_int(xdr
, &zero
) || !xdr_int(xdr
, &zero
))
2911 * The format of xdr encoded nvpair is:
2912 * encode_size, decode_size, name string, data type, nelem, data
2915 nvs_xdr_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2919 char *buf_end
= (char *)nvp
+ nvp
->nvp_size
;
2921 uint_t nelem
, buflen
;
2923 XDR
*xdr
= nvs
->nvs_private
;
2925 ASSERT(xdr
!= NULL
&& nvp
!= NULL
);
2928 if ((buf
= NVP_NAME(nvp
)) >= buf_end
)
2930 buflen
= buf_end
- buf
;
2932 if (!xdr_string(xdr
, &buf
, buflen
- 1))
2934 nvp
->nvp_name_sz
= strlen(buf
) + 1;
2936 /* type and nelem */
2937 if (!xdr_int(xdr
, (int *)&nvp
->nvp_type
) ||
2938 !xdr_int(xdr
, &nvp
->nvp_value_elem
))
2941 type
= NVP_TYPE(nvp
);
2942 nelem
= nvp
->nvp_value_elem
;
2945 * Verify type and nelem and get the value size.
2946 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2947 * is the size of the string(s) excluded.
2949 if ((value_sz
= i_get_value_size(type
, NULL
, nelem
)) < 0)
2952 /* if there is no data to extract then return */
2957 if ((buf
= NVP_VALUE(nvp
)) >= buf_end
)
2959 buflen
= buf_end
- buf
;
2961 if (buflen
< value_sz
)
2965 case DATA_TYPE_NVLIST
:
2966 if (nvs_embedded(nvs
, (void *)buf
) == 0)
2970 case DATA_TYPE_NVLIST_ARRAY
:
2971 if (nvs_embedded_nvl_array(nvs
, nvp
, NULL
) == 0)
2975 case DATA_TYPE_BOOLEAN
:
2979 case DATA_TYPE_BYTE
:
2980 case DATA_TYPE_INT8
:
2981 case DATA_TYPE_UINT8
:
2982 ret
= xdr_char(xdr
, buf
);
2985 case DATA_TYPE_INT16
:
2986 ret
= xdr_short(xdr
, (void *)buf
);
2989 case DATA_TYPE_UINT16
:
2990 ret
= xdr_u_short(xdr
, (void *)buf
);
2993 case DATA_TYPE_BOOLEAN_VALUE
:
2994 case DATA_TYPE_INT32
:
2995 ret
= xdr_int(xdr
, (void *)buf
);
2998 case DATA_TYPE_UINT32
:
2999 ret
= xdr_u_int(xdr
, (void *)buf
);
3002 case DATA_TYPE_INT64
:
3003 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3006 case DATA_TYPE_UINT64
:
3007 ret
= xdr_u_longlong_t(xdr
, (void *)buf
);
3010 case DATA_TYPE_HRTIME
:
3012 * NOTE: must expose the definition of hrtime_t here
3014 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3016 #if !defined(_KERNEL)
3017 case DATA_TYPE_DOUBLE
:
3018 ret
= xdr_double(xdr
, (void *)buf
);
3021 case DATA_TYPE_STRING
:
3022 ret
= xdr_string(xdr
, &buf
, buflen
- 1);
3025 case DATA_TYPE_BYTE_ARRAY
:
3026 ret
= xdr_opaque(xdr
, buf
, nelem
);
3029 case DATA_TYPE_INT8_ARRAY
:
3030 case DATA_TYPE_UINT8_ARRAY
:
3031 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
, sizeof (int8_t),
3032 (xdrproc_t
)xdr_char
);
3035 case DATA_TYPE_INT16_ARRAY
:
3036 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int16_t),
3037 sizeof (int16_t), (xdrproc_t
)xdr_short
);
3040 case DATA_TYPE_UINT16_ARRAY
:
3041 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint16_t),
3042 sizeof (uint16_t), (xdrproc_t
)xdr_u_short
);
3045 case DATA_TYPE_BOOLEAN_ARRAY
:
3046 case DATA_TYPE_INT32_ARRAY
:
3047 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int32_t),
3048 sizeof (int32_t), (xdrproc_t
)xdr_int
);
3051 case DATA_TYPE_UINT32_ARRAY
:
3052 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint32_t),
3053 sizeof (uint32_t), (xdrproc_t
)xdr_u_int
);
3056 case DATA_TYPE_INT64_ARRAY
:
3057 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int64_t),
3058 sizeof (int64_t), (xdrproc_t
)xdr_longlong_t
);
3061 case DATA_TYPE_UINT64_ARRAY
:
3062 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint64_t),
3063 sizeof (uint64_t), (xdrproc_t
)xdr_u_longlong_t
);
3066 case DATA_TYPE_STRING_ARRAY
: {
3067 size_t len
= nelem
* sizeof (uint64_t);
3068 char **strp
= (void *)buf
;
3071 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3072 bzero(buf
, len
); /* don't trust packed data */
3074 for (i
= 0; i
< nelem
; i
++) {
3081 if (xdr_string(xdr
, &buf
, buflen
- 1) != TRUE
)
3084 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3086 len
= strlen(buf
) + 1;
3095 return (ret
== TRUE
? 0 : EFAULT
);
3099 nvs_xdr_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3101 data_type_t type
= NVP_TYPE(nvp
);
3103 * encode_size + decode_size + name string size + data type + nelem
3104 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3106 uint64_t nvp_sz
= 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp
))) + 4 + 4;
3109 case DATA_TYPE_BOOLEAN
:
3112 case DATA_TYPE_BOOLEAN_VALUE
:
3113 case DATA_TYPE_BYTE
:
3114 case DATA_TYPE_INT8
:
3115 case DATA_TYPE_UINT8
:
3116 case DATA_TYPE_INT16
:
3117 case DATA_TYPE_UINT16
:
3118 case DATA_TYPE_INT32
:
3119 case DATA_TYPE_UINT32
:
3120 nvp_sz
+= 4; /* 4 is the minimum xdr unit */
3123 case DATA_TYPE_INT64
:
3124 case DATA_TYPE_UINT64
:
3125 case DATA_TYPE_HRTIME
:
3126 #if !defined(_KERNEL)
3127 case DATA_TYPE_DOUBLE
:
3132 case DATA_TYPE_STRING
:
3133 nvp_sz
+= 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp
)));
3136 case DATA_TYPE_BYTE_ARRAY
:
3137 nvp_sz
+= NV_ALIGN4(NVP_NELEM(nvp
));
3140 case DATA_TYPE_BOOLEAN_ARRAY
:
3141 case DATA_TYPE_INT8_ARRAY
:
3142 case DATA_TYPE_UINT8_ARRAY
:
3143 case DATA_TYPE_INT16_ARRAY
:
3144 case DATA_TYPE_UINT16_ARRAY
:
3145 case DATA_TYPE_INT32_ARRAY
:
3146 case DATA_TYPE_UINT32_ARRAY
:
3147 nvp_sz
+= 4 + 4 * (uint64_t)NVP_NELEM(nvp
);
3150 case DATA_TYPE_INT64_ARRAY
:
3151 case DATA_TYPE_UINT64_ARRAY
:
3152 nvp_sz
+= 4 + 8 * (uint64_t)NVP_NELEM(nvp
);
3155 case DATA_TYPE_STRING_ARRAY
: {
3157 char **strs
= (void *)NVP_VALUE(nvp
);
3159 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
3160 nvp_sz
+= 4 + NV_ALIGN4(strlen(strs
[i
]));
3165 case DATA_TYPE_NVLIST
:
3166 case DATA_TYPE_NVLIST_ARRAY
: {
3168 int old_nvs_op
= nvs
->nvs_op
;
3171 nvs
->nvs_op
= NVS_OP_GETSIZE
;
3172 if (type
== DATA_TYPE_NVLIST
)
3173 err
= nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
);
3175 err
= nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
);
3176 nvs
->nvs_op
= old_nvs_op
;
3189 if (nvp_sz
> INT32_MAX
)
3199 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3200 * the largest nvpair that could be encoded in the buffer.
3202 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3203 * The size of a xdr packed nvpair without any data is 5 words.
3205 * Using the size of the data directly as an estimate would be ok
3206 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3207 * then the actual nvpair has space for an array of pointers to index
3208 * the strings. These pointers are not encoded into the packed xdr buffer.
3210 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3211 * of length 0, then each string is endcoded in xdr format as a single word.
3212 * Therefore when expanded to an nvpair there will be 2.25 word used for
3213 * each string. (a int64_t allocated for pointer usage, and a single char
3214 * for the null termination.)
3216 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3218 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3219 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3220 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3221 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3222 (NVS_XDR_DATA_LEN(x) * 2) + \
3223 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3226 nvs_xdr_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3228 XDR
*xdr
= nvs
->nvs_private
;
3229 int32_t encode_len
, decode_len
;
3231 switch (nvs
->nvs_op
) {
3232 case NVS_OP_ENCODE
: {
3235 if (nvs_xdr_nvp_size(nvs
, nvp
, &nvsize
) != 0)
3238 decode_len
= nvp
->nvp_size
;
3239 encode_len
= nvsize
;
3240 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3243 return (nvs_xdr_nvp_op(nvs
, nvp
));
3245 case NVS_OP_DECODE
: {
3246 struct xdr_bytesrec bytesrec
;
3248 /* get the encode and decode size */
3249 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3253 /* are we at the end of the stream? */
3257 /* sanity check the size parameter */
3258 if (!xdr_control(xdr
, XDR_GET_BYTES_AVAIL
, &bytesrec
))
3261 if (*size
> NVS_XDR_MAX_LEN(bytesrec
.xc_num_avail
))
3272 static const struct nvs_ops nvs_xdr_ops
= {
3281 nvs_xdr(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3286 nvs
->nvs_ops
= &nvs_xdr_ops
;
3288 if ((err
= nvs_xdr_create(nvs
, &xdr
, buf
+ sizeof (nvs_header_t
),
3289 *buflen
- sizeof (nvs_header_t
))) != 0)
3292 err
= nvs_operation(nvs
, nvl
, buflen
);
3294 nvs_xdr_destroy(nvs
);