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 https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2015, 2017 by Delphix. All rights reserved.
25 * Copyright 2018 RackTop Systems.
29 * Links to Illumos.org for more information on Interface Libraries:
30 * [1] https://illumos.org/man/3lib/libnvpair
31 * [2] https://illumos.org/man/3nvpair/nvlist_alloc
32 * [3] https://illumos.org/man/9f/nvlist_alloc
33 * [4] https://illumos.org/man/9f/nvlist_next_nvpair
34 * [5] https://illumos.org/man/9f/nvpair_value_byte
37 #include <sys/debug.h>
38 #include <sys/isa_defs.h>
39 #include <sys/nvpair.h>
40 #include <sys/nvpair_impl.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/string.h>
48 #include <sys/sunddi.h>
49 #include <sys/sysmacros.h>
56 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) (p)++
59 * nvpair.c - Provides kernel & userland interfaces for manipulating
74 * +--------------+ last i_nvp in list
75 * | nvpriv_t | +--------------------->
77 * +--+- nvp_list | | +------------+
78 * | | nvp_last -+--+ + nv_alloc_t |
79 * | | nvp_curr | |------------|
80 * | | nvp_nva -+----> | nva_ops |
81 * | | nvp_stat | | nva_arg |
82 * | +--------------+ +------------+
86 * +---------------------+ +-------------------+
87 * | i_nvp_t | +-->| i_nvp_t | +-->
88 * |---------------------| | |-------------------| |
89 * | nvi_next -+--+ | nvi_next -+--+
90 * | nvi_prev (NULL) | <----+ nvi_prev |
91 * | . . . . . . . . . . | | . . . . . . . . . |
92 * | nvp (nvpair_t) | | nvp (nvpair_t) |
93 * | - nvp_size | | - nvp_size |
94 * | - nvp_name_sz | | - nvp_name_sz |
95 * | - nvp_value_elem | | - nvp_value_elem |
96 * | - nvp_type | | - nvp_type |
97 * | - data ... | | - data ... |
98 * +---------------------+ +-------------------+
102 * +---------------------+ +---------------------+
103 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
104 * |---------------------| | | |---------------------|
105 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
106 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
107 * | . . . . . . . . . | | . . . . . . . . . |
108 * | nvp (nvpair_t) | | nvp (nvpair_t) |
109 * | - nvp_size | | - nvp_size |
110 * | - nvp_name_sz | | - nvp_name_sz |
111 * | - nvp_value_elem | | - nvp_value_elem |
112 * | - DATA_TYPE_NVLIST | | - nvp_type |
113 * | - data (embedded) | | - data ... |
114 * | nvlist name | +---------------------+
115 * | +--------------+ |
117 * | |--------------| |
118 * | | nvl_version | |
120 * | | nvl_priv --+---+---->
123 * | +--------------+ |
124 * +---------------------+
127 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
128 * allow value to be aligned on 8 byte boundary
130 * name_len is the length of the name string including the null terminator
133 #define NVP_SIZE_CALC(name_len, data_len) \
134 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
136 static int i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
);
137 static int nvlist_add_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
138 uint_t nelem
, const void *data
);
140 #define NV_STAT_EMBEDDED 0x1
141 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
142 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
144 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
145 #define NVPAIR2I_NVP(nvp) \
146 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
149 static const int nvpair_max_recursion
= 20;
151 static const int nvpair_max_recursion
= 100;
154 static const uint64_t nvlist_hashtable_init_size
= (1 << 4);
157 nv_alloc_init(nv_alloc_t
*nva
, const nv_alloc_ops_t
*nvo
, /* args */ ...)
165 va_start(valist
, nvo
);
166 if (nva
->nva_ops
->nv_ao_init
!= NULL
)
167 err
= nva
->nva_ops
->nv_ao_init(nva
, valist
);
174 nv_alloc_reset(nv_alloc_t
*nva
)
176 if (nva
->nva_ops
->nv_ao_reset
!= NULL
)
177 nva
->nva_ops
->nv_ao_reset(nva
);
181 nv_alloc_fini(nv_alloc_t
*nva
)
183 if (nva
->nva_ops
->nv_ao_fini
!= NULL
)
184 nva
->nva_ops
->nv_ao_fini(nva
);
188 nvlist_lookup_nv_alloc(nvlist_t
*nvl
)
193 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
196 return (priv
->nvp_nva
);
200 nv_mem_zalloc(nvpriv_t
*nvp
, size_t size
)
202 nv_alloc_t
*nva
= nvp
->nvp_nva
;
205 if ((buf
= nva
->nva_ops
->nv_ao_alloc(nva
, size
)) != NULL
)
206 memset(buf
, 0, size
);
212 nv_mem_free(nvpriv_t
*nvp
, void *buf
, size_t size
)
214 nv_alloc_t
*nva
= nvp
->nvp_nva
;
216 nva
->nva_ops
->nv_ao_free(nva
, buf
, size
);
220 nv_priv_init(nvpriv_t
*priv
, nv_alloc_t
*nva
, uint32_t stat
)
222 memset(priv
, 0, sizeof (nvpriv_t
));
225 priv
->nvp_stat
= stat
;
229 nv_priv_alloc(nv_alloc_t
*nva
)
234 * nv_mem_alloc() cannot called here because it needs the priv
237 if ((priv
= nva
->nva_ops
->nv_ao_alloc(nva
, sizeof (nvpriv_t
))) == NULL
)
240 nv_priv_init(priv
, nva
, 0);
246 * Embedded lists need their own nvpriv_t's. We create a new
247 * nvpriv_t using the parameters and allocator from the parent
251 nv_priv_alloc_embedded(nvpriv_t
*priv
)
255 if ((emb_priv
= nv_mem_zalloc(priv
, sizeof (nvpriv_t
))) == NULL
)
258 nv_priv_init(emb_priv
, priv
->nvp_nva
, NV_STAT_EMBEDDED
);
264 nvt_tab_alloc(nvpriv_t
*priv
, uint64_t buckets
)
266 ASSERT3P(priv
->nvp_hashtable
, ==, NULL
);
267 ASSERT0(priv
->nvp_nbuckets
);
268 ASSERT0(priv
->nvp_nentries
);
270 i_nvp_t
**tab
= nv_mem_zalloc(priv
, buckets
* sizeof (i_nvp_t
*));
274 priv
->nvp_hashtable
= tab
;
275 priv
->nvp_nbuckets
= buckets
;
280 nvt_tab_free(nvpriv_t
*priv
)
282 i_nvp_t
**tab
= priv
->nvp_hashtable
;
284 ASSERT0(priv
->nvp_nbuckets
);
285 ASSERT0(priv
->nvp_nentries
);
289 nv_mem_free(priv
, tab
, priv
->nvp_nbuckets
* sizeof (i_nvp_t
*));
291 priv
->nvp_hashtable
= NULL
;
292 priv
->nvp_nbuckets
= 0;
293 priv
->nvp_nentries
= 0;
297 nvt_hash(const char *p
)
299 uint32_t g
, hval
= 0;
302 hval
= (hval
<< 4) + *p
++;
303 if ((g
= (hval
& 0xf0000000)) != 0)
311 nvt_nvpair_match(const nvpair_t
*nvp1
, const nvpair_t
*nvp2
, uint32_t nvflag
)
313 boolean_t match
= B_FALSE
;
314 if (nvflag
& NV_UNIQUE_NAME_TYPE
) {
315 if (strcmp(NVP_NAME(nvp1
), NVP_NAME(nvp2
)) == 0 &&
316 NVP_TYPE(nvp1
) == NVP_TYPE(nvp2
))
319 ASSERT(nvflag
== 0 || nvflag
& NV_UNIQUE_NAME
);
320 if (strcmp(NVP_NAME(nvp1
), NVP_NAME(nvp2
)) == 0)
327 nvt_lookup_name_type(const nvlist_t
*nvl
, const char *name
, data_type_t type
)
329 const nvpriv_t
*priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
330 ASSERT(priv
!= NULL
);
332 i_nvp_t
**tab
= priv
->nvp_hashtable
;
335 ASSERT3P(priv
->nvp_list
, ==, NULL
);
336 ASSERT0(priv
->nvp_nbuckets
);
337 ASSERT0(priv
->nvp_nentries
);
340 ASSERT(priv
->nvp_nbuckets
!= 0);
343 uint64_t hash
= nvt_hash(name
);
344 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
346 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
347 i_nvp_t
*entry
= tab
[index
];
349 for (i_nvp_t
*e
= entry
; e
!= NULL
; e
= e
->nvi_hashtable_next
) {
350 if (strcmp(NVP_NAME(&e
->nvi_nvp
), name
) == 0 &&
351 (type
== DATA_TYPE_DONTCARE
||
352 NVP_TYPE(&e
->nvi_nvp
) == type
))
353 return (&e
->nvi_nvp
);
359 nvt_lookup_name(const nvlist_t
*nvl
, const char *name
)
361 return (nvt_lookup_name_type(nvl
, name
, DATA_TYPE_DONTCARE
));
365 nvt_resize(nvpriv_t
*priv
, uint32_t new_size
)
367 i_nvp_t
**tab
= priv
->nvp_hashtable
;
370 * Migrate all the entries from the current table
371 * to a newly-allocated table with the new size by
372 * re-adjusting the pointers of their entries.
374 uint32_t size
= priv
->nvp_nbuckets
;
375 uint32_t new_mask
= new_size
- 1;
376 ASSERT(ISP2(new_size
));
378 i_nvp_t
**new_tab
= nv_mem_zalloc(priv
, new_size
* sizeof (i_nvp_t
*));
382 uint32_t nentries
= 0;
383 for (uint32_t i
= 0; i
< size
; i
++) {
384 i_nvp_t
*next
, *e
= tab
[i
];
387 next
= e
->nvi_hashtable_next
;
389 uint32_t hash
= nvt_hash(NVP_NAME(&e
->nvi_nvp
));
390 uint32_t index
= hash
& new_mask
;
392 e
->nvi_hashtable_next
= new_tab
[index
];
400 ASSERT3U(nentries
, ==, priv
->nvp_nentries
);
404 priv
->nvp_hashtable
= new_tab
;
405 priv
->nvp_nbuckets
= new_size
;
406 priv
->nvp_nentries
= nentries
;
412 nvt_needs_togrow(nvpriv_t
*priv
)
415 * Grow only when we have more elements than buckets
416 * and the # of buckets doesn't overflow.
418 return (priv
->nvp_nentries
> priv
->nvp_nbuckets
&&
419 (UINT32_MAX
>> 1) >= priv
->nvp_nbuckets
);
423 * Allocate a new table that's twice the size of the old one,
424 * and migrate all the entries from the old one to the new
425 * one by re-adjusting their pointers.
428 nvt_grow(nvpriv_t
*priv
)
430 uint32_t current_size
= priv
->nvp_nbuckets
;
431 /* ensure we won't overflow */
432 ASSERT3U(UINT32_MAX
>> 1, >=, current_size
);
433 return (nvt_resize(priv
, current_size
<< 1));
437 nvt_needs_toshrink(nvpriv_t
*priv
)
440 * Shrink only when the # of elements is less than or
441 * equal to 1/4 the # of buckets. Never shrink less than
442 * nvlist_hashtable_init_size.
444 ASSERT3U(priv
->nvp_nbuckets
, >=, nvlist_hashtable_init_size
);
445 if (priv
->nvp_nbuckets
== nvlist_hashtable_init_size
)
447 return (priv
->nvp_nentries
<= (priv
->nvp_nbuckets
>> 2));
451 * Allocate a new table that's half the size of the old one,
452 * and migrate all the entries from the old one to the new
453 * one by re-adjusting their pointers.
456 nvt_shrink(nvpriv_t
*priv
)
458 uint32_t current_size
= priv
->nvp_nbuckets
;
459 /* ensure we won't overflow */
460 ASSERT3U(current_size
, >=, nvlist_hashtable_init_size
);
461 return (nvt_resize(priv
, current_size
>> 1));
465 nvt_remove_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
467 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
469 if (nvt_needs_toshrink(priv
)) {
470 int err
= nvt_shrink(priv
);
474 i_nvp_t
**tab
= priv
->nvp_hashtable
;
476 const char *name
= NVP_NAME(nvp
);
477 uint64_t hash
= nvt_hash(name
);
478 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
480 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
481 i_nvp_t
*bucket
= tab
[index
];
483 for (i_nvp_t
*prev
= NULL
, *e
= bucket
;
484 e
!= NULL
; prev
= e
, e
= e
->nvi_hashtable_next
) {
485 if (nvt_nvpair_match(&e
->nvi_nvp
, nvp
, nvl
->nvl_nvflag
)) {
487 prev
->nvi_hashtable_next
=
488 e
->nvi_hashtable_next
;
490 ASSERT3P(e
, ==, bucket
);
491 tab
[index
] = e
->nvi_hashtable_next
;
493 e
->nvi_hashtable_next
= NULL
;
494 priv
->nvp_nentries
--;
503 nvt_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
505 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
507 /* initialize nvpair table now if it doesn't exist. */
508 if (priv
->nvp_hashtable
== NULL
) {
509 int err
= nvt_tab_alloc(priv
, nvlist_hashtable_init_size
);
515 * if we don't allow duplicate entries, make sure to
516 * unlink any existing entries from the table.
518 if (nvl
->nvl_nvflag
!= 0) {
519 int err
= nvt_remove_nvpair(nvl
, nvp
);
524 if (nvt_needs_togrow(priv
)) {
525 int err
= nvt_grow(priv
);
529 i_nvp_t
**tab
= priv
->nvp_hashtable
;
531 const char *name
= NVP_NAME(nvp
);
532 uint64_t hash
= nvt_hash(name
);
533 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
535 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
536 // cppcheck-suppress nullPointerRedundantCheck
537 i_nvp_t
*bucket
= tab
[index
];
539 /* insert link at the beginning of the bucket */
540 i_nvp_t
*new_entry
= NVPAIR2I_NVP(nvp
);
541 ASSERT3P(new_entry
->nvi_hashtable_next
, ==, NULL
);
542 new_entry
->nvi_hashtable_next
= bucket
;
543 // cppcheck-suppress nullPointerRedundantCheck
544 tab
[index
] = new_entry
;
546 priv
->nvp_nentries
++;
551 nvlist_init(nvlist_t
*nvl
, uint32_t nvflag
, nvpriv_t
*priv
)
553 nvl
->nvl_version
= NV_VERSION
;
554 nvl
->nvl_nvflag
= nvflag
& (NV_UNIQUE_NAME
|NV_UNIQUE_NAME_TYPE
);
555 nvl
->nvl_priv
= (uint64_t)(uintptr_t)priv
;
561 nvlist_nvflag(nvlist_t
*nvl
)
563 return (nvl
->nvl_nvflag
);
567 nvlist_nv_alloc(int kmflag
)
572 return (nv_alloc_sleep
);
574 return (nv_alloc_nosleep
);
576 return (nv_alloc_pushpage
);
580 return (nv_alloc_nosleep
);
585 * nvlist_alloc - Allocate nvlist.
588 nvlist_alloc(nvlist_t
**nvlp
, uint_t nvflag
, int kmflag
)
590 return (nvlist_xalloc(nvlp
, nvflag
, nvlist_nv_alloc(kmflag
)));
594 nvlist_xalloc(nvlist_t
**nvlp
, uint_t nvflag
, nv_alloc_t
*nva
)
598 if (nvlp
== NULL
|| nva
== NULL
)
601 if ((priv
= nv_priv_alloc(nva
)) == NULL
)
604 if ((*nvlp
= nv_mem_zalloc(priv
,
605 NV_ALIGN(sizeof (nvlist_t
)))) == NULL
) {
606 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
610 nvlist_init(*nvlp
, nvflag
, priv
);
616 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
619 nvp_buf_alloc(nvlist_t
*nvl
, size_t len
)
621 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
627 * Allocate the buffer
629 nvsize
= len
+ offsetof(i_nvp_t
, nvi_nvp
);
631 if ((buf
= nv_mem_zalloc(priv
, nvsize
)) == NULL
)
641 * nvp_buf_free - de-Allocate an i_nvp_t.
644 nvp_buf_free(nvlist_t
*nvl
, nvpair_t
*nvp
)
646 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
647 size_t nvsize
= nvp
->nvp_size
+ offsetof(i_nvp_t
, nvi_nvp
);
649 nv_mem_free(priv
, NVPAIR2I_NVP(nvp
), nvsize
);
653 * nvp_buf_link - link a new nv pair into the nvlist.
656 nvp_buf_link(nvlist_t
*nvl
, nvpair_t
*nvp
)
658 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
659 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
661 /* Put element at end of nvlist */
662 if (priv
->nvp_list
== NULL
) {
663 priv
->nvp_list
= priv
->nvp_last
= curr
;
665 curr
->nvi_prev
= priv
->nvp_last
;
666 priv
->nvp_last
->nvi_next
= curr
;
667 priv
->nvp_last
= curr
;
672 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
675 nvp_buf_unlink(nvlist_t
*nvl
, nvpair_t
*nvp
)
677 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
678 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
681 * protect nvlist_next_nvpair() against walking on freed memory.
683 if (priv
->nvp_curr
== curr
)
684 priv
->nvp_curr
= curr
->nvi_next
;
686 if (curr
== priv
->nvp_list
)
687 priv
->nvp_list
= curr
->nvi_next
;
689 curr
->nvi_prev
->nvi_next
= curr
->nvi_next
;
691 if (curr
== priv
->nvp_last
)
692 priv
->nvp_last
= curr
->nvi_prev
;
694 curr
->nvi_next
->nvi_prev
= curr
->nvi_prev
;
698 * take a nvpair type and number of elements and make sure the are valid
701 i_validate_type_nelem(data_type_t type
, uint_t nelem
)
704 case DATA_TYPE_BOOLEAN
:
708 case DATA_TYPE_BOOLEAN_VALUE
:
711 case DATA_TYPE_UINT8
:
712 case DATA_TYPE_INT16
:
713 case DATA_TYPE_UINT16
:
714 case DATA_TYPE_INT32
:
715 case DATA_TYPE_UINT32
:
716 case DATA_TYPE_INT64
:
717 case DATA_TYPE_UINT64
:
718 case DATA_TYPE_STRING
:
719 case DATA_TYPE_HRTIME
:
720 case DATA_TYPE_NVLIST
:
721 #if !defined(_KERNEL)
722 case DATA_TYPE_DOUBLE
:
727 case DATA_TYPE_BOOLEAN_ARRAY
:
728 case DATA_TYPE_BYTE_ARRAY
:
729 case DATA_TYPE_INT8_ARRAY
:
730 case DATA_TYPE_UINT8_ARRAY
:
731 case DATA_TYPE_INT16_ARRAY
:
732 case DATA_TYPE_UINT16_ARRAY
:
733 case DATA_TYPE_INT32_ARRAY
:
734 case DATA_TYPE_UINT32_ARRAY
:
735 case DATA_TYPE_INT64_ARRAY
:
736 case DATA_TYPE_UINT64_ARRAY
:
737 case DATA_TYPE_STRING_ARRAY
:
738 case DATA_TYPE_NVLIST_ARRAY
:
739 /* we allow arrays with 0 elements */
748 * Verify nvp_name_sz and check the name string length.
751 i_validate_nvpair_name(nvpair_t
*nvp
)
753 if ((nvp
->nvp_name_sz
<= 0) ||
754 (nvp
->nvp_size
< NVP_SIZE_CALC(nvp
->nvp_name_sz
, 0)))
757 /* verify the name string, make sure its terminated */
758 if (NVP_NAME(nvp
)[nvp
->nvp_name_sz
- 1] != '\0')
761 return (strlen(NVP_NAME(nvp
)) == nvp
->nvp_name_sz
- 1 ? 0 : EFAULT
);
765 i_validate_nvpair_value(data_type_t type
, uint_t nelem
, const void *data
)
768 case DATA_TYPE_BOOLEAN_VALUE
:
769 if (*(boolean_t
*)data
!= B_TRUE
&&
770 *(boolean_t
*)data
!= B_FALSE
)
773 case DATA_TYPE_BOOLEAN_ARRAY
: {
776 for (i
= 0; i
< nelem
; i
++)
777 if (((boolean_t
*)data
)[i
] != B_TRUE
&&
778 ((boolean_t
*)data
)[i
] != B_FALSE
)
790 * This function takes a pointer to what should be a nvpair and it's size
791 * and then verifies that all the nvpair fields make sense and can be
792 * trusted. This function is used when decoding packed nvpairs.
795 i_validate_nvpair(nvpair_t
*nvp
)
797 data_type_t type
= NVP_TYPE(nvp
);
800 /* verify nvp_name_sz, check the name string length */
801 if (i_validate_nvpair_name(nvp
) != 0)
804 if (i_validate_nvpair_value(type
, NVP_NELEM(nvp
), NVP_VALUE(nvp
)) != 0)
808 * verify nvp_type, nvp_value_elem, and also possibly
809 * verify string values and get the value size.
811 size2
= i_get_value_size(type
, NVP_VALUE(nvp
), NVP_NELEM(nvp
));
812 size1
= nvp
->nvp_size
- NVP_VALOFF(nvp
);
813 if (size2
< 0 || size1
!= NV_ALIGN(size2
))
820 nvlist_copy_pairs(const nvlist_t
*snvl
, nvlist_t
*dnvl
)
822 const nvpriv_t
*priv
;
825 if ((priv
= (const nvpriv_t
*)(uintptr_t)snvl
->nvl_priv
) == NULL
)
828 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
829 const nvpair_t
*nvp
= &curr
->nvi_nvp
;
832 if ((err
= nvlist_add_common(dnvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
833 NVP_NELEM(nvp
), NVP_VALUE(nvp
))) != 0)
841 * Frees all memory allocated for an nvpair (like embedded lists) with
842 * the exception of the nvpair buffer itself.
845 nvpair_free(nvpair_t
*nvp
)
847 switch (NVP_TYPE(nvp
)) {
848 case DATA_TYPE_NVLIST
:
849 nvlist_free(EMBEDDED_NVL(nvp
));
851 case DATA_TYPE_NVLIST_ARRAY
: {
852 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
855 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
857 nvlist_free(nvlp
[i
]);
866 * nvlist_free - free an unpacked nvlist
869 nvlist_free(nvlist_t
*nvl
)
875 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
879 * Unpacked nvlist are linked through i_nvp_t
881 curr
= priv
->nvp_list
;
882 while (curr
!= NULL
) {
883 nvpair_t
*nvp
= &curr
->nvi_nvp
;
884 curr
= curr
->nvi_next
;
887 nvp_buf_free(nvl
, nvp
);
890 if (!(priv
->nvp_stat
& NV_STAT_EMBEDDED
))
891 nv_mem_free(priv
, nvl
, NV_ALIGN(sizeof (nvlist_t
)));
896 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
900 nvlist_contains_nvp(const nvlist_t
*nvl
, const nvpair_t
*nvp
)
902 const nvpriv_t
*priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
908 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
909 if (&curr
->nvi_nvp
== nvp
)
916 * Make a copy of nvlist
919 nvlist_dup(const nvlist_t
*nvl
, nvlist_t
**nvlp
, int kmflag
)
921 return (nvlist_xdup(nvl
, nvlp
, nvlist_nv_alloc(kmflag
)));
925 nvlist_xdup(const nvlist_t
*nvl
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
930 if (nvl
== NULL
|| nvlp
== NULL
)
933 if ((err
= nvlist_xalloc(&ret
, nvl
->nvl_nvflag
, nva
)) != 0)
936 if ((err
= nvlist_copy_pairs(nvl
, ret
)) != 0)
945 * Remove all with matching name
948 nvlist_remove_all(nvlist_t
*nvl
, const char *name
)
952 if (nvl
== NULL
|| name
== NULL
|| nvl
->nvl_priv
== 0)
956 while ((nvp
= nvt_lookup_name(nvl
, name
)) != NULL
) {
957 VERIFY0(nvlist_remove_nvpair(nvl
, nvp
));
965 * Remove first one with matching name and type
968 nvlist_remove(nvlist_t
*nvl
, const char *name
, data_type_t type
)
970 if (nvl
== NULL
|| name
== NULL
|| nvl
->nvl_priv
== 0)
973 nvpair_t
*nvp
= nvt_lookup_name_type(nvl
, name
, type
);
977 return (nvlist_remove_nvpair(nvl
, nvp
));
981 nvlist_remove_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
983 if (nvl
== NULL
|| nvp
== NULL
)
986 int err
= nvt_remove_nvpair(nvl
, nvp
);
990 nvp_buf_unlink(nvl
, nvp
);
992 nvp_buf_free(nvl
, nvp
);
997 * This function calculates the size of an nvpair value.
999 * The data argument controls the behavior in case of the data types
1000 * DATA_TYPE_STRING and
1001 * DATA_TYPE_STRING_ARRAY
1002 * Is data == NULL then the size of the string(s) is excluded.
1005 i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
)
1009 if (i_validate_type_nelem(type
, nelem
) != 0)
1012 /* Calculate required size for holding value */
1014 case DATA_TYPE_BOOLEAN
:
1017 case DATA_TYPE_BOOLEAN_VALUE
:
1018 value_sz
= sizeof (boolean_t
);
1020 case DATA_TYPE_BYTE
:
1021 value_sz
= sizeof (uchar_t
);
1023 case DATA_TYPE_INT8
:
1024 value_sz
= sizeof (int8_t);
1026 case DATA_TYPE_UINT8
:
1027 value_sz
= sizeof (uint8_t);
1029 case DATA_TYPE_INT16
:
1030 value_sz
= sizeof (int16_t);
1032 case DATA_TYPE_UINT16
:
1033 value_sz
= sizeof (uint16_t);
1035 case DATA_TYPE_INT32
:
1036 value_sz
= sizeof (int32_t);
1038 case DATA_TYPE_UINT32
:
1039 value_sz
= sizeof (uint32_t);
1041 case DATA_TYPE_INT64
:
1042 value_sz
= sizeof (int64_t);
1044 case DATA_TYPE_UINT64
:
1045 value_sz
= sizeof (uint64_t);
1047 #if !defined(_KERNEL)
1048 case DATA_TYPE_DOUBLE
:
1049 value_sz
= sizeof (double);
1052 case DATA_TYPE_STRING
:
1056 value_sz
= strlen(data
) + 1;
1058 case DATA_TYPE_BOOLEAN_ARRAY
:
1059 value_sz
= (uint64_t)nelem
* sizeof (boolean_t
);
1061 case DATA_TYPE_BYTE_ARRAY
:
1062 value_sz
= (uint64_t)nelem
* sizeof (uchar_t
);
1064 case DATA_TYPE_INT8_ARRAY
:
1065 value_sz
= (uint64_t)nelem
* sizeof (int8_t);
1067 case DATA_TYPE_UINT8_ARRAY
:
1068 value_sz
= (uint64_t)nelem
* sizeof (uint8_t);
1070 case DATA_TYPE_INT16_ARRAY
:
1071 value_sz
= (uint64_t)nelem
* sizeof (int16_t);
1073 case DATA_TYPE_UINT16_ARRAY
:
1074 value_sz
= (uint64_t)nelem
* sizeof (uint16_t);
1076 case DATA_TYPE_INT32_ARRAY
:
1077 value_sz
= (uint64_t)nelem
* sizeof (int32_t);
1079 case DATA_TYPE_UINT32_ARRAY
:
1080 value_sz
= (uint64_t)nelem
* sizeof (uint32_t);
1082 case DATA_TYPE_INT64_ARRAY
:
1083 value_sz
= (uint64_t)nelem
* sizeof (int64_t);
1085 case DATA_TYPE_UINT64_ARRAY
:
1086 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
1088 case DATA_TYPE_STRING_ARRAY
:
1089 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
1092 char *const *strs
= data
;
1095 /* no alignment requirement for strings */
1096 for (i
= 0; i
< nelem
; i
++) {
1097 if (strs
[i
] == NULL
)
1099 value_sz
+= strlen(strs
[i
]) + 1;
1103 case DATA_TYPE_HRTIME
:
1104 value_sz
= sizeof (hrtime_t
);
1106 case DATA_TYPE_NVLIST
:
1107 value_sz
= NV_ALIGN(sizeof (nvlist_t
));
1109 case DATA_TYPE_NVLIST_ARRAY
:
1110 value_sz
= (uint64_t)nelem
* sizeof (uint64_t) +
1111 (uint64_t)nelem
* NV_ALIGN(sizeof (nvlist_t
));
1117 return (value_sz
> INT32_MAX
? -1 : (int)value_sz
);
1121 nvlist_copy_embedded(nvlist_t
*nvl
, nvlist_t
*onvl
, nvlist_t
*emb_nvl
)
1126 if ((priv
= nv_priv_alloc_embedded((nvpriv_t
*)(uintptr_t)
1127 nvl
->nvl_priv
)) == NULL
)
1130 nvlist_init(emb_nvl
, onvl
->nvl_nvflag
, priv
);
1132 if ((err
= nvlist_copy_pairs(onvl
, emb_nvl
)) != 0) {
1133 nvlist_free(emb_nvl
);
1134 emb_nvl
->nvl_priv
= 0;
1141 * nvlist_add_common - Add new <name,value> pair to nvlist
1144 nvlist_add_common(nvlist_t
*nvl
, const char *name
,
1145 data_type_t type
, uint_t nelem
, const void *data
)
1150 int nvp_sz
, name_sz
, value_sz
;
1153 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
1156 if (nelem
!= 0 && data
== NULL
)
1160 * Verify type and nelem and get the value size.
1161 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1162 * is the size of the string(s) included.
1164 if ((value_sz
= i_get_value_size(type
, data
, nelem
)) < 0)
1167 if (i_validate_nvpair_value(type
, nelem
, data
) != 0)
1171 * If we're adding an nvlist or nvlist array, ensure that we are not
1172 * adding the input nvlist to itself, which would cause recursion,
1173 * and ensure that no NULL nvlist pointers are present.
1176 case DATA_TYPE_NVLIST
:
1177 if (data
== nvl
|| data
== NULL
)
1180 case DATA_TYPE_NVLIST_ARRAY
: {
1181 nvlist_t
**onvlp
= (nvlist_t
**)data
;
1182 for (i
= 0; i
< nelem
; i
++) {
1183 if (onvlp
[i
] == nvl
|| onvlp
[i
] == NULL
)
1192 /* calculate sizes of the nvpair elements and the nvpair itself */
1193 name_sz
= strlen(name
) + 1;
1194 if (name_sz
>= 1ULL << (sizeof (nvp
->nvp_name_sz
) * NBBY
- 1))
1197 nvp_sz
= NVP_SIZE_CALC(name_sz
, value_sz
);
1199 if ((nvp
= nvp_buf_alloc(nvl
, nvp_sz
)) == NULL
)
1202 ASSERT(nvp
->nvp_size
== nvp_sz
);
1203 nvp
->nvp_name_sz
= name_sz
;
1204 nvp
->nvp_value_elem
= nelem
;
1205 nvp
->nvp_type
= type
;
1206 memcpy(NVP_NAME(nvp
), name
, name_sz
);
1209 case DATA_TYPE_BOOLEAN
:
1211 case DATA_TYPE_STRING_ARRAY
: {
1212 char *const *strs
= data
;
1213 char *buf
= NVP_VALUE(nvp
);
1214 char **cstrs
= (void *)buf
;
1216 /* skip pre-allocated space for pointer array */
1217 buf
+= nelem
* sizeof (uint64_t);
1218 for (i
= 0; i
< nelem
; i
++) {
1219 int slen
= strlen(strs
[i
]) + 1;
1220 memcpy(buf
, strs
[i
], slen
);
1226 case DATA_TYPE_NVLIST
: {
1227 nvlist_t
*nnvl
= EMBEDDED_NVL(nvp
);
1228 nvlist_t
*onvl
= (nvlist_t
*)data
;
1230 if ((err
= nvlist_copy_embedded(nvl
, onvl
, nnvl
)) != 0) {
1231 nvp_buf_free(nvl
, nvp
);
1236 case DATA_TYPE_NVLIST_ARRAY
: {
1237 nvlist_t
**onvlp
= (nvlist_t
**)data
;
1238 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
1239 nvlist_t
*embedded
= (nvlist_t
*)
1240 ((uintptr_t)nvlp
+ nelem
* sizeof (uint64_t));
1242 for (i
= 0; i
< nelem
; i
++) {
1243 if ((err
= nvlist_copy_embedded(nvl
,
1244 onvlp
[i
], embedded
)) != 0) {
1246 * Free any successfully created lists
1249 nvp_buf_free(nvl
, nvp
);
1253 nvlp
[i
] = embedded
++;
1258 memcpy(NVP_VALUE(nvp
), data
, value_sz
);
1261 /* if unique name, remove before add */
1262 if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME
)
1263 (void) nvlist_remove_all(nvl
, name
);
1264 else if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME_TYPE
)
1265 (void) nvlist_remove(nvl
, name
, type
);
1267 err
= nvt_add_nvpair(nvl
, nvp
);
1270 nvp_buf_free(nvl
, nvp
);
1273 nvp_buf_link(nvl
, nvp
);
1279 nvlist_add_boolean(nvlist_t
*nvl
, const char *name
)
1281 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN
, 0, NULL
));
1285 nvlist_add_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t val
)
1287 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_VALUE
, 1, &val
));
1291 nvlist_add_byte(nvlist_t
*nvl
, const char *name
, uchar_t val
)
1293 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE
, 1, &val
));
1297 nvlist_add_int8(nvlist_t
*nvl
, const char *name
, int8_t val
)
1299 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8
, 1, &val
));
1303 nvlist_add_uint8(nvlist_t
*nvl
, const char *name
, uint8_t val
)
1305 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8
, 1, &val
));
1309 nvlist_add_int16(nvlist_t
*nvl
, const char *name
, int16_t val
)
1311 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16
, 1, &val
));
1315 nvlist_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
1317 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16
, 1, &val
));
1321 nvlist_add_int32(nvlist_t
*nvl
, const char *name
, int32_t val
)
1323 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32
, 1, &val
));
1327 nvlist_add_uint32(nvlist_t
*nvl
, const char *name
, uint32_t val
)
1329 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32
, 1, &val
));
1333 nvlist_add_int64(nvlist_t
*nvl
, const char *name
, int64_t val
)
1335 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64
, 1, &val
));
1339 nvlist_add_uint64(nvlist_t
*nvl
, const char *name
, uint64_t val
)
1341 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64
, 1, &val
));
1344 #if !defined(_KERNEL)
1346 nvlist_add_double(nvlist_t
*nvl
, const char *name
, double val
)
1348 return (nvlist_add_common(nvl
, name
, DATA_TYPE_DOUBLE
, 1, &val
));
1353 nvlist_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
1355 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING
, 1, (void *)val
));
1359 nvlist_add_boolean_array(nvlist_t
*nvl
, const char *name
,
1360 const boolean_t
*a
, uint_t n
)
1362 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1366 nvlist_add_byte_array(nvlist_t
*nvl
, const char *name
, const uchar_t
*a
,
1369 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1373 nvlist_add_int8_array(nvlist_t
*nvl
, const char *name
, const int8_t *a
,
1376 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1380 nvlist_add_uint8_array(nvlist_t
*nvl
, const char *name
, const uint8_t *a
,
1383 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1387 nvlist_add_int16_array(nvlist_t
*nvl
, const char *name
, const int16_t *a
,
1390 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1394 nvlist_add_uint16_array(nvlist_t
*nvl
, const char *name
, const uint16_t *a
,
1397 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1401 nvlist_add_int32_array(nvlist_t
*nvl
, const char *name
, const int32_t *a
,
1404 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1408 nvlist_add_uint32_array(nvlist_t
*nvl
, const char *name
, const uint32_t *a
,
1411 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1415 nvlist_add_int64_array(nvlist_t
*nvl
, const char *name
, const int64_t *a
,
1418 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1422 nvlist_add_uint64_array(nvlist_t
*nvl
, const char *name
, const uint64_t *a
,
1425 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1429 nvlist_add_string_array(nvlist_t
*nvl
, const char *name
,
1430 const char *const *a
, uint_t n
)
1432 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1436 nvlist_add_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t val
)
1438 return (nvlist_add_common(nvl
, name
, DATA_TYPE_HRTIME
, 1, &val
));
1442 nvlist_add_nvlist(nvlist_t
*nvl
, const char *name
, const nvlist_t
*val
)
1444 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST
, 1, val
));
1448 nvlist_add_nvlist_array(nvlist_t
*nvl
, const char *name
,
1449 const nvlist_t
* const *a
, uint_t n
)
1451 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1454 /* reading name-value pairs */
1456 nvlist_next_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
1462 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1465 curr
= NVPAIR2I_NVP(nvp
);
1468 * Ensure that nvp is a valid nvpair on this nvlist.
1469 * NB: nvp_curr is used only as a hint so that we don't always
1470 * have to walk the list to determine if nvp is still on the list.
1473 curr
= priv
->nvp_list
;
1474 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1475 curr
= curr
->nvi_next
;
1479 priv
->nvp_curr
= curr
;
1481 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1485 nvlist_prev_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
1491 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1494 curr
= NVPAIR2I_NVP(nvp
);
1497 curr
= priv
->nvp_last
;
1498 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1499 curr
= curr
->nvi_prev
;
1503 priv
->nvp_curr
= curr
;
1505 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1509 nvlist_empty(const nvlist_t
*nvl
)
1511 const nvpriv_t
*priv
;
1514 (priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1517 return (priv
->nvp_list
== NULL
);
1521 nvpair_name(const nvpair_t
*nvp
)
1523 return (NVP_NAME(nvp
));
1527 nvpair_type(const nvpair_t
*nvp
)
1529 return (NVP_TYPE(nvp
));
1533 nvpair_type_is_array(const nvpair_t
*nvp
)
1535 data_type_t type
= NVP_TYPE(nvp
);
1537 if ((type
== DATA_TYPE_BYTE_ARRAY
) ||
1538 (type
== DATA_TYPE_INT8_ARRAY
) ||
1539 (type
== DATA_TYPE_UINT8_ARRAY
) ||
1540 (type
== DATA_TYPE_INT16_ARRAY
) ||
1541 (type
== DATA_TYPE_UINT16_ARRAY
) ||
1542 (type
== DATA_TYPE_INT32_ARRAY
) ||
1543 (type
== DATA_TYPE_UINT32_ARRAY
) ||
1544 (type
== DATA_TYPE_INT64_ARRAY
) ||
1545 (type
== DATA_TYPE_UINT64_ARRAY
) ||
1546 (type
== DATA_TYPE_BOOLEAN_ARRAY
) ||
1547 (type
== DATA_TYPE_STRING_ARRAY
) ||
1548 (type
== DATA_TYPE_NVLIST_ARRAY
))
1555 nvpair_value_common(const nvpair_t
*nvp
, data_type_t type
, uint_t
*nelem
,
1560 if (nvp
== NULL
|| nvpair_type(nvp
) != type
)
1564 * For non-array types, we copy the data.
1565 * For array types (including string), we set a pointer.
1568 case DATA_TYPE_BOOLEAN
:
1573 case DATA_TYPE_BOOLEAN_VALUE
:
1574 case DATA_TYPE_BYTE
:
1575 case DATA_TYPE_INT8
:
1576 case DATA_TYPE_UINT8
:
1577 case DATA_TYPE_INT16
:
1578 case DATA_TYPE_UINT16
:
1579 case DATA_TYPE_INT32
:
1580 case DATA_TYPE_UINT32
:
1581 case DATA_TYPE_INT64
:
1582 case DATA_TYPE_UINT64
:
1583 case DATA_TYPE_HRTIME
:
1584 #if !defined(_KERNEL)
1585 case DATA_TYPE_DOUBLE
:
1589 if ((value_sz
= i_get_value_size(type
, NULL
, 1)) < 0)
1591 memcpy(data
, NVP_VALUE(nvp
), (size_t)value_sz
);
1596 case DATA_TYPE_NVLIST
:
1597 case DATA_TYPE_STRING
:
1601 * This discards the const from nvp, so all callers for these
1602 * types must not accept const nvpairs.
1604 *(void **)data
= (void *)NVP_VALUE(nvp
);
1609 case DATA_TYPE_BOOLEAN_ARRAY
:
1610 case DATA_TYPE_BYTE_ARRAY
:
1611 case DATA_TYPE_INT8_ARRAY
:
1612 case DATA_TYPE_UINT8_ARRAY
:
1613 case DATA_TYPE_INT16_ARRAY
:
1614 case DATA_TYPE_UINT16_ARRAY
:
1615 case DATA_TYPE_INT32_ARRAY
:
1616 case DATA_TYPE_UINT32_ARRAY
:
1617 case DATA_TYPE_INT64_ARRAY
:
1618 case DATA_TYPE_UINT64_ARRAY
:
1619 case DATA_TYPE_STRING_ARRAY
:
1620 case DATA_TYPE_NVLIST_ARRAY
:
1621 if (nelem
== NULL
|| data
== NULL
)
1624 * This discards the const from nvp, so all callers for these
1625 * types must not accept const nvpairs.
1627 if ((*nelem
= NVP_NELEM(nvp
)) != 0)
1628 *(void **)data
= (void *)NVP_VALUE(nvp
);
1630 *(void **)data
= NULL
;
1641 nvlist_lookup_common(const nvlist_t
*nvl
, const char *name
, data_type_t type
,
1642 uint_t
*nelem
, void *data
)
1644 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
1647 if (!(nvl
->nvl_nvflag
& (NV_UNIQUE_NAME
| NV_UNIQUE_NAME_TYPE
)))
1650 nvpair_t
*nvp
= nvt_lookup_name_type(nvl
, name
, type
);
1654 return (nvpair_value_common(nvp
, type
, nelem
, data
));
1658 nvlist_lookup_boolean(const nvlist_t
*nvl
, const char *name
)
1660 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BOOLEAN
, NULL
, NULL
));
1664 nvlist_lookup_boolean_value(const nvlist_t
*nvl
, const char *name
,
1667 return (nvlist_lookup_common(nvl
, name
,
1668 DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1672 nvlist_lookup_byte(const nvlist_t
*nvl
, const char *name
, uchar_t
*val
)
1674 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE
, NULL
, val
));
1678 nvlist_lookup_int8(const nvlist_t
*nvl
, const char *name
, int8_t *val
)
1680 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8
, NULL
, val
));
1684 nvlist_lookup_uint8(const nvlist_t
*nvl
, const char *name
, uint8_t *val
)
1686 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8
, NULL
, val
));
1690 nvlist_lookup_int16(const nvlist_t
*nvl
, const char *name
, int16_t *val
)
1692 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16
, NULL
, val
));
1696 nvlist_lookup_uint16(const nvlist_t
*nvl
, const char *name
, uint16_t *val
)
1698 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16
, NULL
, val
));
1702 nvlist_lookup_int32(const nvlist_t
*nvl
, const char *name
, int32_t *val
)
1704 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32
, NULL
, val
));
1708 nvlist_lookup_uint32(const nvlist_t
*nvl
, const char *name
, uint32_t *val
)
1710 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32
, NULL
, val
));
1714 nvlist_lookup_int64(const nvlist_t
*nvl
, const char *name
, int64_t *val
)
1716 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64
, NULL
, val
));
1720 nvlist_lookup_uint64(const nvlist_t
*nvl
, const char *name
, uint64_t *val
)
1722 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64
, NULL
, val
));
1725 #if !defined(_KERNEL)
1727 nvlist_lookup_double(const nvlist_t
*nvl
, const char *name
, double *val
)
1729 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_DOUBLE
, NULL
, val
));
1734 nvlist_lookup_string(const nvlist_t
*nvl
, const char *name
, const char **val
)
1736 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING
, NULL
, val
));
1740 nvlist_lookup_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
**val
)
1742 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST
, NULL
, val
));
1746 nvlist_lookup_boolean_array(nvlist_t
*nvl
, const char *name
,
1747 boolean_t
**a
, uint_t
*n
)
1749 return (nvlist_lookup_common(nvl
, name
,
1750 DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1754 nvlist_lookup_byte_array(nvlist_t
*nvl
, const char *name
,
1755 uchar_t
**a
, uint_t
*n
)
1757 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1761 nvlist_lookup_int8_array(nvlist_t
*nvl
, const char *name
, int8_t **a
, uint_t
*n
)
1763 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1767 nvlist_lookup_uint8_array(nvlist_t
*nvl
, const char *name
,
1768 uint8_t **a
, uint_t
*n
)
1770 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1774 nvlist_lookup_int16_array(nvlist_t
*nvl
, const char *name
,
1775 int16_t **a
, uint_t
*n
)
1777 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1781 nvlist_lookup_uint16_array(nvlist_t
*nvl
, const char *name
,
1782 uint16_t **a
, uint_t
*n
)
1784 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1788 nvlist_lookup_int32_array(nvlist_t
*nvl
, const char *name
,
1789 int32_t **a
, uint_t
*n
)
1791 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1795 nvlist_lookup_uint32_array(nvlist_t
*nvl
, const char *name
,
1796 uint32_t **a
, uint_t
*n
)
1798 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1802 nvlist_lookup_int64_array(nvlist_t
*nvl
, const char *name
,
1803 int64_t **a
, uint_t
*n
)
1805 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1809 nvlist_lookup_uint64_array(nvlist_t
*nvl
, const char *name
,
1810 uint64_t **a
, uint_t
*n
)
1812 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1816 nvlist_lookup_string_array(nvlist_t
*nvl
, const char *name
,
1817 char ***a
, uint_t
*n
)
1819 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1823 nvlist_lookup_nvlist_array(nvlist_t
*nvl
, const char *name
,
1824 nvlist_t
***a
, uint_t
*n
)
1826 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1830 nvlist_lookup_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t
*val
)
1832 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_HRTIME
, NULL
, val
));
1836 nvlist_lookup_pairs(nvlist_t
*nvl
, int flag
, ...)
1840 int noentok
= (flag
& NV_FLAG_NOENTOK
? 1 : 0);
1844 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
1849 switch (type
= va_arg(ap
, data_type_t
)) {
1850 case DATA_TYPE_BOOLEAN
:
1851 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, NULL
);
1854 case DATA_TYPE_BOOLEAN_VALUE
:
1855 case DATA_TYPE_BYTE
:
1856 case DATA_TYPE_INT8
:
1857 case DATA_TYPE_UINT8
:
1858 case DATA_TYPE_INT16
:
1859 case DATA_TYPE_UINT16
:
1860 case DATA_TYPE_INT32
:
1861 case DATA_TYPE_UINT32
:
1862 case DATA_TYPE_INT64
:
1863 case DATA_TYPE_UINT64
:
1864 case DATA_TYPE_HRTIME
:
1865 case DATA_TYPE_STRING
:
1866 case DATA_TYPE_NVLIST
:
1867 #if !defined(_KERNEL)
1868 case DATA_TYPE_DOUBLE
:
1870 val
= va_arg(ap
, void *);
1871 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, val
);
1874 case DATA_TYPE_BYTE_ARRAY
:
1875 case DATA_TYPE_BOOLEAN_ARRAY
:
1876 case DATA_TYPE_INT8_ARRAY
:
1877 case DATA_TYPE_UINT8_ARRAY
:
1878 case DATA_TYPE_INT16_ARRAY
:
1879 case DATA_TYPE_UINT16_ARRAY
:
1880 case DATA_TYPE_INT32_ARRAY
:
1881 case DATA_TYPE_UINT32_ARRAY
:
1882 case DATA_TYPE_INT64_ARRAY
:
1883 case DATA_TYPE_UINT64_ARRAY
:
1884 case DATA_TYPE_STRING_ARRAY
:
1885 case DATA_TYPE_NVLIST_ARRAY
:
1886 val
= va_arg(ap
, void *);
1887 nelem
= va_arg(ap
, uint_t
*);
1888 ret
= nvlist_lookup_common(nvl
, name
, type
, nelem
, val
);
1895 if (ret
== ENOENT
&& noentok
)
1904 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1905 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1906 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1907 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1908 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1909 * "a.d[3].e[1]". This matches the C syntax for array embed (for convenience,
1910 * code also supports "a.d[3]e[1]" syntax).
1912 * If 'ip' is non-NULL and the last name component is an array, return the
1913 * value of the "...[index]" array index in *ip. For an array reference that
1914 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1915 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1916 * inside the 'name' string where the syntax error was detected.
1919 nvlist_lookup_nvpair_ei_sep(nvlist_t
*nvl
, const char *name
, const char sep
,
1920 nvpair_t
**ret
, int *ip
, const char **ep
)
1931 *ip
= -1; /* not indexed */
1935 if ((nvl
== NULL
) || (name
== NULL
))
1940 /* step through components of name */
1941 for (np
= name
; np
&& *np
; np
= sepp
) {
1942 /* ensure unique names */
1943 if (!(nvl
->nvl_nvflag
& NV_UNIQUE_NAME
))
1946 /* skip white space */
1947 skip_whitespace(np
);
1951 /* set 'sepp' to end of current component 'np' */
1953 sepp
= strchr(np
, sep
);
1957 /* find start of next "[ index ]..." */
1958 idxp
= strchr(np
, '[');
1960 /* if sepp comes first, set idxp to NULL */
1961 if (sepp
&& idxp
&& (sepp
< idxp
))
1965 * At this point 'idxp' is set if there is an index
1966 * expected for the current component.
1969 /* set 'n' to length of current 'np' name component */
1972 /* keep sepp up to date for *ep use as we advance */
1973 skip_whitespace(idxp
);
1976 /* determine the index value */
1977 #if defined(_KERNEL)
1978 if (ddi_strtol(idxp
, &idxep
, 0, &idx
))
1981 idx
= strtol(idxp
, &idxep
, 0);
1986 /* keep sepp up to date for *ep use as we advance */
1989 /* skip white space index value and check for ']' */
1990 skip_whitespace(sepp
);
1994 /* for embedded arrays, support C syntax: "a[1].b" */
1995 skip_whitespace(sepp
);
1996 if (sep
&& (*sepp
== sep
))
2004 /* trim trailing whitespace by reducing length of 'np' */
2007 for (n
--; (np
[n
] == ' ') || (np
[n
] == '\t'); n
--)
2011 /* skip whitespace, and set sepp to NULL if complete */
2013 skip_whitespace(sepp
);
2020 * o 'n' is the length of current 'np' component.
2021 * o 'idxp' is set if there was an index, and value 'idx'.
2022 * o 'sepp' is set to the beginning of the next component,
2023 * and set to NULL if we have no more components.
2025 * Search for nvpair with matching component name.
2027 for (nvp
= nvlist_next_nvpair(nvl
, NULL
); nvp
!= NULL
;
2028 nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
2030 /* continue if no match on name */
2031 if (strncmp(np
, nvpair_name(nvp
), n
) ||
2032 (strlen(nvpair_name(nvp
)) != n
))
2035 /* if indexed, verify type is array oriented */
2036 if (idxp
&& !nvpair_type_is_array(nvp
))
2040 * Full match found, return nvp and idx if this
2041 * was the last component.
2047 *ip
= (int)idx
; /* return index */
2048 return (0); /* found */
2052 * More components: current match must be
2053 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2054 * to support going deeper.
2056 if (nvpair_type(nvp
) == DATA_TYPE_NVLIST
) {
2057 nvl
= EMBEDDED_NVL(nvp
);
2059 } else if (nvpair_type(nvp
) == DATA_TYPE_NVLIST_ARRAY
) {
2060 if (nvpair_value_nvlist_array(nvp
,
2061 &nva
, (uint_t
*)&n
) != 0)
2065 if ((n
< 0) || (idx
>= n
))
2071 /* type does not support more levels */
2075 goto fail
; /* 'name' not found */
2077 /* search for match of next component in embedded 'nvl' list */
2080 fail
: if (ep
&& sepp
)
2086 * Return pointer to nvpair with specified 'name'.
2089 nvlist_lookup_nvpair(nvlist_t
*nvl
, const char *name
, nvpair_t
**ret
)
2091 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, 0, ret
, NULL
, NULL
));
2095 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2096 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2099 int nvlist_lookup_nvpair_embedded_index(nvlist_t
*nvl
,
2100 const char *name
, nvpair_t
**ret
, int *ip
, const char **ep
)
2102 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, '.', ret
, ip
, ep
));
2106 nvlist_exists(const nvlist_t
*nvl
, const char *name
)
2112 if (name
== NULL
|| nvl
== NULL
||
2113 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2116 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2117 nvp
= &curr
->nvi_nvp
;
2119 if (strcmp(name
, NVP_NAME(nvp
)) == 0)
2127 nvpair_value_boolean_value(const nvpair_t
*nvp
, boolean_t
*val
)
2129 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
2133 nvpair_value_byte(const nvpair_t
*nvp
, uchar_t
*val
)
2135 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE
, NULL
, val
));
2139 nvpair_value_int8(const nvpair_t
*nvp
, int8_t *val
)
2141 return (nvpair_value_common(nvp
, DATA_TYPE_INT8
, NULL
, val
));
2145 nvpair_value_uint8(const nvpair_t
*nvp
, uint8_t *val
)
2147 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8
, NULL
, val
));
2151 nvpair_value_int16(const nvpair_t
*nvp
, int16_t *val
)
2153 return (nvpair_value_common(nvp
, DATA_TYPE_INT16
, NULL
, val
));
2157 nvpair_value_uint16(const nvpair_t
*nvp
, uint16_t *val
)
2159 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16
, NULL
, val
));
2163 nvpair_value_int32(const nvpair_t
*nvp
, int32_t *val
)
2165 return (nvpair_value_common(nvp
, DATA_TYPE_INT32
, NULL
, val
));
2169 nvpair_value_uint32(const nvpair_t
*nvp
, uint32_t *val
)
2171 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32
, NULL
, val
));
2175 nvpair_value_int64(const nvpair_t
*nvp
, int64_t *val
)
2177 return (nvpair_value_common(nvp
, DATA_TYPE_INT64
, NULL
, val
));
2181 nvpair_value_uint64(const nvpair_t
*nvp
, uint64_t *val
)
2183 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64
, NULL
, val
));
2186 #if !defined(_KERNEL)
2188 nvpair_value_double(const nvpair_t
*nvp
, double *val
)
2190 return (nvpair_value_common(nvp
, DATA_TYPE_DOUBLE
, NULL
, val
));
2195 nvpair_value_string(const nvpair_t
*nvp
, const char **val
)
2197 return (nvpair_value_common(nvp
, DATA_TYPE_STRING
, NULL
, val
));
2201 nvpair_value_nvlist(nvpair_t
*nvp
, nvlist_t
**val
)
2203 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST
, NULL
, val
));
2207 nvpair_value_boolean_array(nvpair_t
*nvp
, boolean_t
**val
, uint_t
*nelem
)
2209 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_ARRAY
, nelem
, val
));
2213 nvpair_value_byte_array(nvpair_t
*nvp
, uchar_t
**val
, uint_t
*nelem
)
2215 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE_ARRAY
, nelem
, val
));
2219 nvpair_value_int8_array(nvpair_t
*nvp
, int8_t **val
, uint_t
*nelem
)
2221 return (nvpair_value_common(nvp
, DATA_TYPE_INT8_ARRAY
, nelem
, val
));
2225 nvpair_value_uint8_array(nvpair_t
*nvp
, uint8_t **val
, uint_t
*nelem
)
2227 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8_ARRAY
, nelem
, val
));
2231 nvpair_value_int16_array(nvpair_t
*nvp
, int16_t **val
, uint_t
*nelem
)
2233 return (nvpair_value_common(nvp
, DATA_TYPE_INT16_ARRAY
, nelem
, val
));
2237 nvpair_value_uint16_array(nvpair_t
*nvp
, uint16_t **val
, uint_t
*nelem
)
2239 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16_ARRAY
, nelem
, val
));
2243 nvpair_value_int32_array(nvpair_t
*nvp
, int32_t **val
, uint_t
*nelem
)
2245 return (nvpair_value_common(nvp
, DATA_TYPE_INT32_ARRAY
, nelem
, val
));
2249 nvpair_value_uint32_array(nvpair_t
*nvp
, uint32_t **val
, uint_t
*nelem
)
2251 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32_ARRAY
, nelem
, val
));
2255 nvpair_value_int64_array(nvpair_t
*nvp
, int64_t **val
, uint_t
*nelem
)
2257 return (nvpair_value_common(nvp
, DATA_TYPE_INT64_ARRAY
, nelem
, val
));
2261 nvpair_value_uint64_array(nvpair_t
*nvp
, uint64_t **val
, uint_t
*nelem
)
2263 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64_ARRAY
, nelem
, val
));
2267 nvpair_value_string_array(nvpair_t
*nvp
, const char ***val
, uint_t
*nelem
)
2269 return (nvpair_value_common(nvp
, DATA_TYPE_STRING_ARRAY
, nelem
, val
));
2273 nvpair_value_nvlist_array(nvpair_t
*nvp
, nvlist_t
***val
, uint_t
*nelem
)
2275 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST_ARRAY
, nelem
, val
));
2279 nvpair_value_hrtime(nvpair_t
*nvp
, hrtime_t
*val
)
2281 return (nvpair_value_common(nvp
, DATA_TYPE_HRTIME
, NULL
, val
));
2285 * Add specified pair to the list.
2288 nvlist_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
2290 if (nvl
== NULL
|| nvp
== NULL
)
2293 return (nvlist_add_common(nvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
2294 NVP_NELEM(nvp
), NVP_VALUE(nvp
)));
2298 * Merge the supplied nvlists and put the result in dst.
2299 * The merged list will contain all names specified in both lists,
2300 * the values are taken from nvl in the case of duplicates.
2301 * Return 0 on success.
2304 nvlist_merge(nvlist_t
*dst
, nvlist_t
*nvl
, int flag
)
2308 if (nvl
== NULL
|| dst
== NULL
)
2312 return (nvlist_copy_pairs(nvl
, dst
));
2318 * Encoding related routines
2320 #define NVS_OP_ENCODE 0
2321 #define NVS_OP_DECODE 1
2322 #define NVS_OP_GETSIZE 2
2324 typedef struct nvs_ops nvs_ops_t
;
2328 const nvs_ops_t
*nvs_ops
;
2335 * nvs operations are:
2337 * encoding / decoding of an nvlist header (nvlist_t)
2338 * calculates the size used for header and end detection
2341 * responsible for the first part of encoding / decoding of an nvpair
2342 * calculates the decoded size of an nvpair
2345 * second part of encoding / decoding of an nvpair
2348 * calculates the encoding size of an nvpair
2351 * encodes the end detection mark (zeros).
2354 int (*nvs_nvlist
)(nvstream_t
*, nvlist_t
*, size_t *);
2355 int (*nvs_nvpair
)(nvstream_t
*, nvpair_t
*, size_t *);
2356 int (*nvs_nvp_op
)(nvstream_t
*, nvpair_t
*);
2357 int (*nvs_nvp_size
)(nvstream_t
*, nvpair_t
*, size_t *);
2358 int (*nvs_nvl_fini
)(nvstream_t
*);
2362 char nvh_encoding
; /* nvs encoding method */
2363 char nvh_endian
; /* nvs endian */
2364 char nvh_reserved1
; /* reserved for future use */
2365 char nvh_reserved2
; /* reserved for future use */
2369 nvs_encode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2371 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2375 * Walk nvpair in list and encode each nvpair
2377 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
2378 if (nvs
->nvs_ops
->nvs_nvpair(nvs
, &curr
->nvi_nvp
, NULL
) != 0)
2381 return (nvs
->nvs_ops
->nvs_nvl_fini(nvs
));
2385 nvs_decode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2392 * Get decoded size of next pair in stream, alloc
2393 * memory for nvpair_t, then decode the nvpair
2395 while ((err
= nvs
->nvs_ops
->nvs_nvpair(nvs
, NULL
, &nvsize
)) == 0) {
2396 if (nvsize
== 0) /* end of list */
2399 /* make sure len makes sense */
2400 if (nvsize
< NVP_SIZE_CALC(1, 0))
2403 if ((nvp
= nvp_buf_alloc(nvl
, nvsize
)) == NULL
)
2406 if ((err
= nvs
->nvs_ops
->nvs_nvp_op(nvs
, nvp
)) != 0) {
2407 nvp_buf_free(nvl
, nvp
);
2411 if (i_validate_nvpair(nvp
) != 0) {
2413 nvp_buf_free(nvl
, nvp
);
2417 err
= nvt_add_nvpair(nvl
, nvp
);
2420 nvp_buf_free(nvl
, nvp
);
2423 nvp_buf_link(nvl
, nvp
);
2429 nvs_getsize_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2431 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2433 uint64_t nvsize
= *buflen
;
2437 * Get encoded size of nvpairs in nvlist
2439 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2440 if (nvs
->nvs_ops
->nvs_nvp_size(nvs
, &curr
->nvi_nvp
, &size
) != 0)
2443 if ((nvsize
+= size
) > INT32_MAX
)
2452 nvs_operation(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2456 if (nvl
->nvl_priv
== 0)
2460 * Perform the operation, starting with header, then each nvpair
2462 if ((err
= nvs
->nvs_ops
->nvs_nvlist(nvs
, nvl
, buflen
)) != 0)
2465 switch (nvs
->nvs_op
) {
2467 err
= nvs_encode_pairs(nvs
, nvl
);
2471 err
= nvs_decode_pairs(nvs
, nvl
);
2474 case NVS_OP_GETSIZE
:
2475 err
= nvs_getsize_pairs(nvs
, nvl
, buflen
);
2486 nvs_embedded(nvstream_t
*nvs
, nvlist_t
*embedded
)
2488 switch (nvs
->nvs_op
) {
2489 case NVS_OP_ENCODE
: {
2492 if (nvs
->nvs_recursion
>= nvpair_max_recursion
)
2494 nvs
->nvs_recursion
++;
2495 err
= nvs_operation(nvs
, embedded
, NULL
);
2496 nvs
->nvs_recursion
--;
2499 case NVS_OP_DECODE
: {
2503 if (embedded
->nvl_version
!= NV_VERSION
)
2506 if ((priv
= nv_priv_alloc_embedded(nvs
->nvs_priv
)) == NULL
)
2509 nvlist_init(embedded
, embedded
->nvl_nvflag
, priv
);
2511 if (nvs
->nvs_recursion
>= nvpair_max_recursion
) {
2512 nvlist_free(embedded
);
2515 nvs
->nvs_recursion
++;
2516 if ((err
= nvs_operation(nvs
, embedded
, NULL
)) != 0)
2517 nvlist_free(embedded
);
2518 nvs
->nvs_recursion
--;
2529 nvs_embedded_nvl_array(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2531 size_t nelem
= NVP_NELEM(nvp
);
2532 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
2535 switch (nvs
->nvs_op
) {
2537 for (i
= 0; i
< nelem
; i
++)
2538 if (nvs_embedded(nvs
, nvlp
[i
]) != 0)
2542 case NVS_OP_DECODE
: {
2543 size_t len
= nelem
* sizeof (uint64_t);
2544 nvlist_t
*embedded
= (nvlist_t
*)((uintptr_t)nvlp
+ len
);
2546 memset(nvlp
, 0, len
); /* don't trust packed data */
2547 for (i
= 0; i
< nelem
; i
++) {
2548 if (nvs_embedded(nvs
, embedded
) != 0) {
2553 nvlp
[i
] = embedded
++;
2557 case NVS_OP_GETSIZE
: {
2558 uint64_t nvsize
= 0;
2560 for (i
= 0; i
< nelem
; i
++) {
2563 if (nvs_operation(nvs
, nvlp
[i
], &nvp_sz
) != 0)
2566 if ((nvsize
+= nvp_sz
) > INT32_MAX
)
2580 static int nvs_native(nvstream_t
*, nvlist_t
*, char *, size_t *);
2581 static int nvs_xdr(nvstream_t
*, nvlist_t
*, char *, size_t *);
2584 * Common routine for nvlist operations:
2585 * encode, decode, getsize (encoded size).
2588 nvlist_common(nvlist_t
*nvl
, char *buf
, size_t *buflen
, int encoding
,
2594 #if defined(_ZFS_LITTLE_ENDIAN)
2595 int host_endian
= 1;
2596 #elif defined(_ZFS_BIG_ENDIAN)
2597 int host_endian
= 0;
2599 #error "No endian defined!"
2600 #endif /* _ZFS_LITTLE_ENDIAN */
2603 if (buflen
== NULL
|| nvl
== NULL
||
2604 (nvs
.nvs_priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2607 nvs
.nvs_op
= nvs_op
;
2608 nvs
.nvs_recursion
= 0;
2611 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2612 * a buffer is allocated. The first 4 bytes in the buffer are
2613 * used for encoding method and host endian.
2617 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2621 nvh
->nvh_encoding
= encoding
;
2622 nvh
->nvh_endian
= nvl_endian
= host_endian
;
2623 nvh
->nvh_reserved1
= 0;
2624 nvh
->nvh_reserved2
= 0;
2628 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2631 /* get method of encoding from first byte */
2633 encoding
= nvh
->nvh_encoding
;
2634 nvl_endian
= nvh
->nvh_endian
;
2637 case NVS_OP_GETSIZE
:
2638 nvl_endian
= host_endian
;
2641 * add the size for encoding
2643 *buflen
= sizeof (nvs_header_t
);
2651 * Create an nvstream with proper encoding method
2654 case NV_ENCODE_NATIVE
:
2656 * check endianness, in case we are unpacking
2659 if (nvl_endian
!= host_endian
)
2661 err
= nvs_native(&nvs
, nvl
, buf
, buflen
);
2664 err
= nvs_xdr(&nvs
, nvl
, buf
, buflen
);
2675 nvlist_size(nvlist_t
*nvl
, size_t *size
, int encoding
)
2677 return (nvlist_common(nvl
, NULL
, size
, encoding
, NVS_OP_GETSIZE
));
2681 * Pack nvlist into contiguous memory
2684 nvlist_pack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2687 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
,
2688 nvlist_nv_alloc(kmflag
)));
2692 nvlist_xpack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2700 if (nva
== NULL
|| nvl
== NULL
|| bufp
== NULL
|| buflen
== NULL
)
2704 return (nvlist_common(nvl
, *bufp
, buflen
, encoding
,
2708 * Here is a difficult situation:
2709 * 1. The nvlist has fixed allocator properties.
2710 * All other nvlist routines (like nvlist_add_*, ...) use
2712 * 2. When using nvlist_pack() the user can specify their own
2713 * allocator properties (e.g. by using KM_NOSLEEP).
2715 * We use the user specified properties (2). A clearer solution
2716 * will be to remove the kmflag from nvlist_pack(), but we will
2717 * not change the interface.
2719 nv_priv_init(&nvpriv
, nva
, 0);
2721 if ((err
= nvlist_size(nvl
, &alloc_size
, encoding
)))
2724 if ((buf
= nv_mem_zalloc(&nvpriv
, alloc_size
)) == NULL
)
2727 if ((err
= nvlist_common(nvl
, buf
, &alloc_size
, encoding
,
2728 NVS_OP_ENCODE
)) != 0) {
2729 nv_mem_free(&nvpriv
, buf
, alloc_size
);
2731 *buflen
= alloc_size
;
2739 * Unpack buf into an nvlist_t
2742 nvlist_unpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, int kmflag
)
2744 return (nvlist_xunpack(buf
, buflen
, nvlp
, nvlist_nv_alloc(kmflag
)));
2748 nvlist_xunpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
2756 if ((err
= nvlist_xalloc(&nvl
, 0, nva
)) != 0)
2759 if ((err
= nvlist_common(nvl
, buf
, &buflen
, NV_ENCODE_NATIVE
,
2760 NVS_OP_DECODE
)) != 0)
2769 * Native encoding functions
2773 * This structure is used when decoding a packed nvpair in
2774 * the native format. n_base points to a buffer containing the
2775 * packed nvpair. n_end is a pointer to the end of the buffer.
2776 * (n_end actually points to the first byte past the end of the
2777 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2778 * It points to the current data that we are decoding.
2779 * The amount of data left in the buffer is equal to n_end - n_curr.
2780 * n_flag is used to recognize a packed embedded list.
2789 nvs_native_create(nvstream_t
*nvs
, nvs_native_t
*native
, char *buf
,
2792 switch (nvs
->nvs_op
) {
2795 nvs
->nvs_private
= native
;
2796 native
->n_curr
= native
->n_base
= buf
;
2797 native
->n_end
= buf
+ buflen
;
2801 case NVS_OP_GETSIZE
:
2802 nvs
->nvs_private
= native
;
2803 native
->n_curr
= native
->n_base
= native
->n_end
= NULL
;
2812 nvs_native_destroy(nvstream_t
*nvs
)
2814 nvs
->nvs_private
= NULL
;
2818 native_cp(nvstream_t
*nvs
, void *buf
, size_t size
)
2820 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2822 if (native
->n_curr
+ size
> native
->n_end
)
2826 * The memcpy() below eliminates alignment requirement
2827 * on the buffer (stream) and is preferred over direct access.
2829 switch (nvs
->nvs_op
) {
2831 memcpy(native
->n_curr
, buf
, size
);
2834 memcpy(buf
, native
->n_curr
, size
);
2840 native
->n_curr
+= size
;
2845 * operate on nvlist_t header
2848 nvs_native_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2850 nvs_native_t
*native
= nvs
->nvs_private
;
2852 switch (nvs
->nvs_op
) {
2856 return (0); /* packed embedded list */
2860 /* copy version and nvflag of the nvlist_t */
2861 if (native_cp(nvs
, &nvl
->nvl_version
, sizeof (int32_t)) != 0 ||
2862 native_cp(nvs
, &nvl
->nvl_nvflag
, sizeof (int32_t)) != 0)
2867 case NVS_OP_GETSIZE
:
2869 * if calculate for packed embedded list
2870 * 4 for end of the embedded list
2872 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2873 * and 4 for end of the entire list
2875 if (native
->n_flag
) {
2879 *size
+= 2 * sizeof (int32_t) + 4;
2890 nvs_native_nvl_fini(nvstream_t
*nvs
)
2892 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2893 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2895 * Add 4 zero bytes at end of nvlist. They are used
2896 * for end detection by the decode routine.
2898 if (native
->n_curr
+ sizeof (int) > native
->n_end
)
2901 memset(native
->n_curr
, 0, sizeof (int));
2902 native
->n_curr
+= sizeof (int);
2909 nvpair_native_embedded(nvstream_t
*nvs
, nvpair_t
*nvp
)
2911 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2912 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2913 nvlist_t
*packed
= (void *)
2914 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2916 * Null out the pointer that is meaningless in the packed
2917 * structure. The address may not be aligned, so we have
2920 memset((char *)packed
+ offsetof(nvlist_t
, nvl_priv
),
2921 0, sizeof (uint64_t));
2924 return (nvs_embedded(nvs
, EMBEDDED_NVL(nvp
)));
2928 nvpair_native_embedded_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2930 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2931 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2932 char *value
= native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
);
2933 size_t len
= NVP_NELEM(nvp
) * sizeof (uint64_t);
2934 nvlist_t
*packed
= (nvlist_t
*)((uintptr_t)value
+ len
);
2937 * Null out pointers that are meaningless in the packed
2938 * structure. The addresses may not be aligned, so we have
2941 memset(value
, 0, len
);
2943 for (i
= 0; i
< NVP_NELEM(nvp
); i
++, packed
++)
2945 * Null out the pointer that is meaningless in the
2946 * packed structure. The address may not be aligned,
2947 * so we have to use memset.
2949 memset((char *)packed
+ offsetof(nvlist_t
, nvl_priv
),
2950 0, sizeof (uint64_t));
2953 return (nvs_embedded_nvl_array(nvs
, nvp
, NULL
));
2957 nvpair_native_string_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2959 switch (nvs
->nvs_op
) {
2960 case NVS_OP_ENCODE
: {
2961 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2962 uint64_t *strp
= (void *)
2963 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2965 * Null out pointers that are meaningless in the packed
2966 * structure. The addresses may not be aligned, so we have
2969 memset(strp
, 0, NVP_NELEM(nvp
) * sizeof (uint64_t));
2972 case NVS_OP_DECODE
: {
2973 char **strp
= (void *)NVP_VALUE(nvp
);
2974 char *buf
= ((char *)strp
+ NVP_NELEM(nvp
) * sizeof (uint64_t));
2977 for (i
= 0; i
< NVP_NELEM(nvp
); i
++) {
2979 buf
+= strlen(buf
) + 1;
2987 nvs_native_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2994 * We do the initial memcpy of the data before we look at
2995 * the nvpair type, because when we're decoding, we won't
2996 * have the correct values for the pair until we do the memcpy.
2998 switch (nvs
->nvs_op
) {
3001 if (native_cp(nvs
, nvp
, nvp
->nvp_size
) != 0)
3008 /* verify nvp_name_sz, check the name string length */
3009 if (i_validate_nvpair_name(nvp
) != 0)
3012 type
= NVP_TYPE(nvp
);
3015 * Verify type and nelem and get the value size.
3016 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3017 * is the size of the string(s) excluded.
3019 if ((value_sz
= i_get_value_size(type
, NULL
, NVP_NELEM(nvp
))) < 0)
3022 if (NVP_SIZE_CALC(nvp
->nvp_name_sz
, value_sz
) > nvp
->nvp_size
)
3026 case DATA_TYPE_NVLIST
:
3027 ret
= nvpair_native_embedded(nvs
, nvp
);
3029 case DATA_TYPE_NVLIST_ARRAY
:
3030 ret
= nvpair_native_embedded_array(nvs
, nvp
);
3032 case DATA_TYPE_STRING_ARRAY
:
3033 nvpair_native_string_array(nvs
, nvp
);
3043 nvs_native_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3045 uint64_t nvp_sz
= nvp
->nvp_size
;
3047 switch (NVP_TYPE(nvp
)) {
3048 case DATA_TYPE_NVLIST
: {
3051 if (nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
) != 0)
3057 case DATA_TYPE_NVLIST_ARRAY
: {
3060 if (nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
) != 0)
3070 if (nvp_sz
> INT32_MAX
)
3079 nvs_native_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3081 switch (nvs
->nvs_op
) {
3083 return (nvs_native_nvp_op(nvs
, nvp
));
3085 case NVS_OP_DECODE
: {
3086 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
3089 /* try to read the size value from the stream */
3090 if (native
->n_curr
+ sizeof (int32_t) > native
->n_end
)
3092 memcpy(&decode_len
, native
->n_curr
, sizeof (int32_t));
3094 /* sanity check the size value */
3095 if (decode_len
< 0 ||
3096 decode_len
> native
->n_end
- native
->n_curr
)
3102 * If at the end of the stream then move the cursor
3103 * forward, otherwise nvpair_native_op() will read
3104 * the entire nvpair at the same cursor position.
3107 native
->n_curr
+= sizeof (int32_t);
3118 static const nvs_ops_t nvs_native_ops
= {
3119 .nvs_nvlist
= nvs_native_nvlist
,
3120 .nvs_nvpair
= nvs_native_nvpair
,
3121 .nvs_nvp_op
= nvs_native_nvp_op
,
3122 .nvs_nvp_size
= nvs_native_nvp_size
,
3123 .nvs_nvl_fini
= nvs_native_nvl_fini
3127 nvs_native(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3129 nvs_native_t native
;
3132 nvs
->nvs_ops
= &nvs_native_ops
;
3134 if ((err
= nvs_native_create(nvs
, &native
, buf
+ sizeof (nvs_header_t
),
3135 *buflen
- sizeof (nvs_header_t
))) != 0)
3138 err
= nvs_operation(nvs
, nvl
, buflen
);
3140 nvs_native_destroy(nvs
);
3146 * XDR encoding functions
3148 * An xdr packed nvlist is encoded as:
3150 * - encoding method and host endian (4 bytes)
3151 * - nvl_version (4 bytes)
3152 * - nvl_nvflag (4 bytes)
3154 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3155 * - encoded size of the nvpair (4 bytes)
3156 * - decoded size of the nvpair (4 bytes)
3157 * - name string, (4 + sizeof(NV_ALIGN4(string))
3158 * a string is coded as size (4 bytes) and data
3159 * - data type (4 bytes)
3160 * - number of elements in the nvpair (4 bytes)
3163 * - 2 zero's for end of the entire list (8 bytes)
3166 nvs_xdr_create(nvstream_t
*nvs
, XDR
*xdr
, char *buf
, size_t buflen
)
3168 /* xdr data must be 4 byte aligned */
3169 if ((ulong_t
)buf
% 4 != 0)
3172 switch (nvs
->nvs_op
) {
3174 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_ENCODE
);
3175 nvs
->nvs_private
= xdr
;
3178 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_DECODE
);
3179 nvs
->nvs_private
= xdr
;
3181 case NVS_OP_GETSIZE
:
3182 nvs
->nvs_private
= NULL
;
3190 nvs_xdr_destroy(nvstream_t
*nvs
)
3192 switch (nvs
->nvs_op
) {
3195 nvs
->nvs_private
= NULL
;
3203 nvs_xdr_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
3205 switch (nvs
->nvs_op
) {
3207 case NVS_OP_DECODE
: {
3208 XDR
*xdr
= nvs
->nvs_private
;
3210 if (!xdr_int(xdr
, &nvl
->nvl_version
) ||
3211 !xdr_u_int(xdr
, &nvl
->nvl_nvflag
))
3215 case NVS_OP_GETSIZE
: {
3217 * 2 * 4 for nvl_version + nvl_nvflag
3218 * and 8 for end of the entire list
3230 nvs_xdr_nvl_fini(nvstream_t
*nvs
)
3232 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
3233 XDR
*xdr
= nvs
->nvs_private
;
3236 if (!xdr_int(xdr
, &zero
) || !xdr_int(xdr
, &zero
))
3244 * xdrproc_t-compatible callbacks for xdr_array()
3247 #if defined(_KERNEL) && defined(__linux__) /* Linux kernel */
3249 #define NVS_BUILD_XDRPROC_T(type) \
3251 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr) \
3253 return (xdr_##type(xdrs, ptr)); \
3256 #elif !defined(_KERNEL) && defined(XDR_CONTROL) /* tirpc */
3258 #define NVS_BUILD_XDRPROC_T(type) \
3260 nvs_xdr_nvp_##type(XDR *xdrs, ...) \
3265 va_start(args, xdrs); \
3266 ptr = va_arg(args, void *); \
3269 return (xdr_##type(xdrs, ptr)); \
3272 #else /* FreeBSD, sunrpc */
3274 #define NVS_BUILD_XDRPROC_T(type) \
3276 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr, ...) \
3278 return (xdr_##type(xdrs, ptr)); \
3284 NVS_BUILD_XDRPROC_T(char);
3285 NVS_BUILD_XDRPROC_T(short);
3286 NVS_BUILD_XDRPROC_T(u_short
);
3287 NVS_BUILD_XDRPROC_T(int);
3288 NVS_BUILD_XDRPROC_T(u_int
);
3289 NVS_BUILD_XDRPROC_T(longlong_t
);
3290 NVS_BUILD_XDRPROC_T(u_longlong_t
);
3294 * The format of xdr encoded nvpair is:
3295 * encode_size, decode_size, name string, data type, nelem, data
3298 nvs_xdr_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
3300 ASSERT(nvs
!= NULL
&& nvp
!= NULL
);
3304 char *buf_end
= (char *)nvp
+ nvp
->nvp_size
;
3306 uint_t nelem
, buflen
;
3308 XDR
*xdr
= nvs
->nvs_private
;
3310 ASSERT(xdr
!= NULL
);
3313 if ((buf
= NVP_NAME(nvp
)) >= buf_end
)
3315 buflen
= buf_end
- buf
;
3317 if (!xdr_string(xdr
, &buf
, buflen
- 1))
3319 nvp
->nvp_name_sz
= strlen(buf
) + 1;
3321 /* type and nelem */
3322 if (!xdr_int(xdr
, (int *)&nvp
->nvp_type
) ||
3323 !xdr_int(xdr
, &nvp
->nvp_value_elem
))
3326 type
= NVP_TYPE(nvp
);
3327 nelem
= nvp
->nvp_value_elem
;
3330 * Verify type and nelem and get the value size.
3331 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3332 * is the size of the string(s) excluded.
3334 if ((value_sz
= i_get_value_size(type
, NULL
, nelem
)) < 0)
3337 /* if there is no data to extract then return */
3342 if ((buf
= NVP_VALUE(nvp
)) >= buf_end
)
3344 buflen
= buf_end
- buf
;
3346 if (buflen
< value_sz
)
3350 case DATA_TYPE_NVLIST
:
3351 if (nvs_embedded(nvs
, (void *)buf
) == 0)
3355 case DATA_TYPE_NVLIST_ARRAY
:
3356 if (nvs_embedded_nvl_array(nvs
, nvp
, NULL
) == 0)
3360 case DATA_TYPE_BOOLEAN
:
3364 case DATA_TYPE_BYTE
:
3365 case DATA_TYPE_INT8
:
3366 case DATA_TYPE_UINT8
:
3367 ret
= xdr_char(xdr
, buf
);
3370 case DATA_TYPE_INT16
:
3371 ret
= xdr_short(xdr
, (void *)buf
);
3374 case DATA_TYPE_UINT16
:
3375 ret
= xdr_u_short(xdr
, (void *)buf
);
3378 case DATA_TYPE_BOOLEAN_VALUE
:
3379 case DATA_TYPE_INT32
:
3380 ret
= xdr_int(xdr
, (void *)buf
);
3383 case DATA_TYPE_UINT32
:
3384 ret
= xdr_u_int(xdr
, (void *)buf
);
3387 case DATA_TYPE_INT64
:
3388 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3391 case DATA_TYPE_UINT64
:
3392 ret
= xdr_u_longlong_t(xdr
, (void *)buf
);
3395 case DATA_TYPE_HRTIME
:
3397 * NOTE: must expose the definition of hrtime_t here
3399 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3401 #if !defined(_KERNEL)
3402 case DATA_TYPE_DOUBLE
:
3403 ret
= xdr_double(xdr
, (void *)buf
);
3406 case DATA_TYPE_STRING
:
3407 ret
= xdr_string(xdr
, &buf
, buflen
- 1);
3410 case DATA_TYPE_BYTE_ARRAY
:
3411 ret
= xdr_opaque(xdr
, buf
, nelem
);
3414 case DATA_TYPE_INT8_ARRAY
:
3415 case DATA_TYPE_UINT8_ARRAY
:
3416 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
, sizeof (int8_t),
3420 case DATA_TYPE_INT16_ARRAY
:
3421 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int16_t),
3422 sizeof (int16_t), nvs_xdr_nvp_short
);
3425 case DATA_TYPE_UINT16_ARRAY
:
3426 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint16_t),
3427 sizeof (uint16_t), nvs_xdr_nvp_u_short
);
3430 case DATA_TYPE_BOOLEAN_ARRAY
:
3431 case DATA_TYPE_INT32_ARRAY
:
3432 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int32_t),
3433 sizeof (int32_t), nvs_xdr_nvp_int
);
3436 case DATA_TYPE_UINT32_ARRAY
:
3437 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint32_t),
3438 sizeof (uint32_t), nvs_xdr_nvp_u_int
);
3441 case DATA_TYPE_INT64_ARRAY
:
3442 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int64_t),
3443 sizeof (int64_t), nvs_xdr_nvp_longlong_t
);
3446 case DATA_TYPE_UINT64_ARRAY
:
3447 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint64_t),
3448 sizeof (uint64_t), nvs_xdr_nvp_u_longlong_t
);
3451 case DATA_TYPE_STRING_ARRAY
: {
3452 size_t len
= nelem
* sizeof (uint64_t);
3453 char **strp
= (void *)buf
;
3456 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3457 memset(buf
, 0, len
); /* don't trust packed data */
3459 for (i
= 0; i
< nelem
; i
++) {
3466 if (xdr_string(xdr
, &buf
, buflen
- 1) != TRUE
)
3469 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3471 len
= strlen(buf
) + 1;
3480 return (ret
== TRUE
? 0 : EFAULT
);
3484 nvs_xdr_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3486 data_type_t type
= NVP_TYPE(nvp
);
3488 * encode_size + decode_size + name string size + data type + nelem
3489 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3491 uint64_t nvp_sz
= 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp
))) + 4 + 4;
3494 case DATA_TYPE_BOOLEAN
:
3497 case DATA_TYPE_BOOLEAN_VALUE
:
3498 case DATA_TYPE_BYTE
:
3499 case DATA_TYPE_INT8
:
3500 case DATA_TYPE_UINT8
:
3501 case DATA_TYPE_INT16
:
3502 case DATA_TYPE_UINT16
:
3503 case DATA_TYPE_INT32
:
3504 case DATA_TYPE_UINT32
:
3505 nvp_sz
+= 4; /* 4 is the minimum xdr unit */
3508 case DATA_TYPE_INT64
:
3509 case DATA_TYPE_UINT64
:
3510 case DATA_TYPE_HRTIME
:
3511 #if !defined(_KERNEL)
3512 case DATA_TYPE_DOUBLE
:
3517 case DATA_TYPE_STRING
:
3518 nvp_sz
+= 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp
)));
3521 case DATA_TYPE_BYTE_ARRAY
:
3522 nvp_sz
+= NV_ALIGN4(NVP_NELEM(nvp
));
3525 case DATA_TYPE_BOOLEAN_ARRAY
:
3526 case DATA_TYPE_INT8_ARRAY
:
3527 case DATA_TYPE_UINT8_ARRAY
:
3528 case DATA_TYPE_INT16_ARRAY
:
3529 case DATA_TYPE_UINT16_ARRAY
:
3530 case DATA_TYPE_INT32_ARRAY
:
3531 case DATA_TYPE_UINT32_ARRAY
:
3532 nvp_sz
+= 4 + 4 * (uint64_t)NVP_NELEM(nvp
);
3535 case DATA_TYPE_INT64_ARRAY
:
3536 case DATA_TYPE_UINT64_ARRAY
:
3537 nvp_sz
+= 4 + 8 * (uint64_t)NVP_NELEM(nvp
);
3540 case DATA_TYPE_STRING_ARRAY
: {
3542 char **strs
= (void *)NVP_VALUE(nvp
);
3544 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
3545 nvp_sz
+= 4 + NV_ALIGN4(strlen(strs
[i
]));
3550 case DATA_TYPE_NVLIST
:
3551 case DATA_TYPE_NVLIST_ARRAY
: {
3553 int old_nvs_op
= nvs
->nvs_op
;
3556 nvs
->nvs_op
= NVS_OP_GETSIZE
;
3557 if (type
== DATA_TYPE_NVLIST
)
3558 err
= nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
);
3560 err
= nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
);
3561 nvs
->nvs_op
= old_nvs_op
;
3574 if (nvp_sz
> INT32_MAX
)
3584 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3585 * the largest nvpair that could be encoded in the buffer.
3587 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3588 * The size of a xdr packed nvpair without any data is 5 words.
3590 * Using the size of the data directly as an estimate would be ok
3591 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3592 * then the actual nvpair has space for an array of pointers to index
3593 * the strings. These pointers are not encoded into the packed xdr buffer.
3595 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3596 * of length 0, then each string is encoded in xdr format as a single word.
3597 * Therefore when expanded to an nvpair there will be 2.25 word used for
3598 * each string. (a int64_t allocated for pointer usage, and a single char
3599 * for the null termination.)
3601 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3603 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3604 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3605 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3606 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3607 (NVS_XDR_DATA_LEN(x) * 2) + \
3608 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3611 nvs_xdr_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3613 XDR
*xdr
= nvs
->nvs_private
;
3614 int32_t encode_len
, decode_len
;
3616 switch (nvs
->nvs_op
) {
3617 case NVS_OP_ENCODE
: {
3620 if (nvs_xdr_nvp_size(nvs
, nvp
, &nvsize
) != 0)
3623 decode_len
= nvp
->nvp_size
;
3624 encode_len
= nvsize
;
3625 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3628 return (nvs_xdr_nvp_op(nvs
, nvp
));
3630 case NVS_OP_DECODE
: {
3631 struct xdr_bytesrec bytesrec
;
3633 /* get the encode and decode size */
3634 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3638 /* are we at the end of the stream? */
3642 /* sanity check the size parameter */
3643 if (!xdr_control(xdr
, XDR_GET_BYTES_AVAIL
, &bytesrec
))
3646 if (*size
> NVS_XDR_MAX_LEN(bytesrec
.xc_num_avail
))
3657 static const struct nvs_ops nvs_xdr_ops
= {
3658 .nvs_nvlist
= nvs_xdr_nvlist
,
3659 .nvs_nvpair
= nvs_xdr_nvpair
,
3660 .nvs_nvp_op
= nvs_xdr_nvp_op
,
3661 .nvs_nvp_size
= nvs_xdr_nvp_size
,
3662 .nvs_nvl_fini
= nvs_xdr_nvl_fini
3666 nvs_xdr(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3671 nvs
->nvs_ops
= &nvs_xdr_ops
;
3673 if ((err
= nvs_xdr_create(nvs
, &xdr
, buf
+ sizeof (nvs_header_t
),
3674 *buflen
- sizeof (nvs_header_t
))) != 0)
3677 err
= nvs_operation(nvs
, nvl
, buflen
);
3679 nvs_xdr_destroy(nvs
);
3684 EXPORT_SYMBOL(nv_alloc_init
);
3685 EXPORT_SYMBOL(nv_alloc_reset
);
3686 EXPORT_SYMBOL(nv_alloc_fini
);
3688 /* list management */
3689 EXPORT_SYMBOL(nvlist_alloc
);
3690 EXPORT_SYMBOL(nvlist_free
);
3691 EXPORT_SYMBOL(nvlist_size
);
3692 EXPORT_SYMBOL(nvlist_pack
);
3693 EXPORT_SYMBOL(nvlist_unpack
);
3694 EXPORT_SYMBOL(nvlist_dup
);
3695 EXPORT_SYMBOL(nvlist_merge
);
3697 EXPORT_SYMBOL(nvlist_xalloc
);
3698 EXPORT_SYMBOL(nvlist_xpack
);
3699 EXPORT_SYMBOL(nvlist_xunpack
);
3700 EXPORT_SYMBOL(nvlist_xdup
);
3701 EXPORT_SYMBOL(nvlist_lookup_nv_alloc
);
3703 EXPORT_SYMBOL(nvlist_add_nvpair
);
3704 EXPORT_SYMBOL(nvlist_add_boolean
);
3705 EXPORT_SYMBOL(nvlist_add_boolean_value
);
3706 EXPORT_SYMBOL(nvlist_add_byte
);
3707 EXPORT_SYMBOL(nvlist_add_int8
);
3708 EXPORT_SYMBOL(nvlist_add_uint8
);
3709 EXPORT_SYMBOL(nvlist_add_int16
);
3710 EXPORT_SYMBOL(nvlist_add_uint16
);
3711 EXPORT_SYMBOL(nvlist_add_int32
);
3712 EXPORT_SYMBOL(nvlist_add_uint32
);
3713 EXPORT_SYMBOL(nvlist_add_int64
);
3714 EXPORT_SYMBOL(nvlist_add_uint64
);
3715 EXPORT_SYMBOL(nvlist_add_string
);
3716 EXPORT_SYMBOL(nvlist_add_nvlist
);
3717 EXPORT_SYMBOL(nvlist_add_boolean_array
);
3718 EXPORT_SYMBOL(nvlist_add_byte_array
);
3719 EXPORT_SYMBOL(nvlist_add_int8_array
);
3720 EXPORT_SYMBOL(nvlist_add_uint8_array
);
3721 EXPORT_SYMBOL(nvlist_add_int16_array
);
3722 EXPORT_SYMBOL(nvlist_add_uint16_array
);
3723 EXPORT_SYMBOL(nvlist_add_int32_array
);
3724 EXPORT_SYMBOL(nvlist_add_uint32_array
);
3725 EXPORT_SYMBOL(nvlist_add_int64_array
);
3726 EXPORT_SYMBOL(nvlist_add_uint64_array
);
3727 EXPORT_SYMBOL(nvlist_add_string_array
);
3728 EXPORT_SYMBOL(nvlist_add_nvlist_array
);
3729 EXPORT_SYMBOL(nvlist_next_nvpair
);
3730 EXPORT_SYMBOL(nvlist_prev_nvpair
);
3731 EXPORT_SYMBOL(nvlist_empty
);
3732 EXPORT_SYMBOL(nvlist_add_hrtime
);
3734 EXPORT_SYMBOL(nvlist_remove
);
3735 EXPORT_SYMBOL(nvlist_remove_nvpair
);
3736 EXPORT_SYMBOL(nvlist_remove_all
);
3738 EXPORT_SYMBOL(nvlist_lookup_boolean
);
3739 EXPORT_SYMBOL(nvlist_lookup_boolean_value
);
3740 EXPORT_SYMBOL(nvlist_lookup_byte
);
3741 EXPORT_SYMBOL(nvlist_lookup_int8
);
3742 EXPORT_SYMBOL(nvlist_lookup_uint8
);
3743 EXPORT_SYMBOL(nvlist_lookup_int16
);
3744 EXPORT_SYMBOL(nvlist_lookup_uint16
);
3745 EXPORT_SYMBOL(nvlist_lookup_int32
);
3746 EXPORT_SYMBOL(nvlist_lookup_uint32
);
3747 EXPORT_SYMBOL(nvlist_lookup_int64
);
3748 EXPORT_SYMBOL(nvlist_lookup_uint64
);
3749 EXPORT_SYMBOL(nvlist_lookup_string
);
3750 EXPORT_SYMBOL(nvlist_lookup_nvlist
);
3751 EXPORT_SYMBOL(nvlist_lookup_boolean_array
);
3752 EXPORT_SYMBOL(nvlist_lookup_byte_array
);
3753 EXPORT_SYMBOL(nvlist_lookup_int8_array
);
3754 EXPORT_SYMBOL(nvlist_lookup_uint8_array
);
3755 EXPORT_SYMBOL(nvlist_lookup_int16_array
);
3756 EXPORT_SYMBOL(nvlist_lookup_uint16_array
);
3757 EXPORT_SYMBOL(nvlist_lookup_int32_array
);
3758 EXPORT_SYMBOL(nvlist_lookup_uint32_array
);
3759 EXPORT_SYMBOL(nvlist_lookup_int64_array
);
3760 EXPORT_SYMBOL(nvlist_lookup_uint64_array
);
3761 EXPORT_SYMBOL(nvlist_lookup_string_array
);
3762 EXPORT_SYMBOL(nvlist_lookup_nvlist_array
);
3763 EXPORT_SYMBOL(nvlist_lookup_hrtime
);
3764 EXPORT_SYMBOL(nvlist_lookup_pairs
);
3766 EXPORT_SYMBOL(nvlist_lookup_nvpair
);
3767 EXPORT_SYMBOL(nvlist_exists
);
3769 /* processing nvpair */
3770 EXPORT_SYMBOL(nvpair_name
);
3771 EXPORT_SYMBOL(nvpair_type
);
3772 EXPORT_SYMBOL(nvpair_value_boolean_value
);
3773 EXPORT_SYMBOL(nvpair_value_byte
);
3774 EXPORT_SYMBOL(nvpair_value_int8
);
3775 EXPORT_SYMBOL(nvpair_value_uint8
);
3776 EXPORT_SYMBOL(nvpair_value_int16
);
3777 EXPORT_SYMBOL(nvpair_value_uint16
);
3778 EXPORT_SYMBOL(nvpair_value_int32
);
3779 EXPORT_SYMBOL(nvpair_value_uint32
);
3780 EXPORT_SYMBOL(nvpair_value_int64
);
3781 EXPORT_SYMBOL(nvpair_value_uint64
);
3782 EXPORT_SYMBOL(nvpair_value_string
);
3783 EXPORT_SYMBOL(nvpair_value_nvlist
);
3784 EXPORT_SYMBOL(nvpair_value_boolean_array
);
3785 EXPORT_SYMBOL(nvpair_value_byte_array
);
3786 EXPORT_SYMBOL(nvpair_value_int8_array
);
3787 EXPORT_SYMBOL(nvpair_value_uint8_array
);
3788 EXPORT_SYMBOL(nvpair_value_int16_array
);
3789 EXPORT_SYMBOL(nvpair_value_uint16_array
);
3790 EXPORT_SYMBOL(nvpair_value_int32_array
);
3791 EXPORT_SYMBOL(nvpair_value_uint32_array
);
3792 EXPORT_SYMBOL(nvpair_value_int64_array
);
3793 EXPORT_SYMBOL(nvpair_value_uint64_array
);
3794 EXPORT_SYMBOL(nvpair_value_string_array
);
3795 EXPORT_SYMBOL(nvpair_value_nvlist_array
);
3796 EXPORT_SYMBOL(nvpair_value_hrtime
);