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>
44 #include <rpc/types.h>
49 #include <sys/sunddi.h>
50 #include <sys/sysmacros.h>
57 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) (p)++
60 * nvpair.c - Provides kernel & userland interfaces for manipulating
75 * +--------------+ last i_nvp in list
76 * | nvpriv_t | +--------------------->
78 * +--+- nvp_list | | +------------+
79 * | | nvp_last -+--+ + nv_alloc_t |
80 * | | nvp_curr | |------------|
81 * | | nvp_nva -+----> | nva_ops |
82 * | | nvp_stat | | nva_arg |
83 * | +--------------+ +------------+
87 * +---------------------+ +-------------------+
88 * | i_nvp_t | +-->| i_nvp_t | +-->
89 * |---------------------| | |-------------------| |
90 * | nvi_next -+--+ | nvi_next -+--+
91 * | nvi_prev (NULL) | <----+ nvi_prev |
92 * | . . . . . . . . . . | | . . . . . . . . . |
93 * | nvp (nvpair_t) | | nvp (nvpair_t) |
94 * | - nvp_size | | - nvp_size |
95 * | - nvp_name_sz | | - nvp_name_sz |
96 * | - nvp_value_elem | | - nvp_value_elem |
97 * | - nvp_type | | - nvp_type |
98 * | - data ... | | - data ... |
99 * +---------------------+ +-------------------+
103 * +---------------------+ +---------------------+
104 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
105 * |---------------------| | | |---------------------|
106 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
107 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
108 * | . . . . . . . . . | | . . . . . . . . . |
109 * | nvp (nvpair_t) | | nvp (nvpair_t) |
110 * | - nvp_size | | - nvp_size |
111 * | - nvp_name_sz | | - nvp_name_sz |
112 * | - nvp_value_elem | | - nvp_value_elem |
113 * | - DATA_TYPE_NVLIST | | - nvp_type |
114 * | - data (embedded) | | - data ... |
115 * | nvlist name | +---------------------+
116 * | +--------------+ |
118 * | |--------------| |
119 * | | nvl_version | |
121 * | | nvl_priv --+---+---->
124 * | +--------------+ |
125 * +---------------------+
128 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
129 * allow value to be aligned on 8 byte boundary
131 * name_len is the length of the name string including the null terminator
134 #define NVP_SIZE_CALC(name_len, data_len) \
135 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
137 static int i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
);
138 static int nvlist_add_common(nvlist_t
*nvl
, const char *name
, data_type_t type
,
139 uint_t nelem
, const void *data
);
141 #define NV_STAT_EMBEDDED 0x1
142 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
143 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
145 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
146 #define NVPAIR2I_NVP(nvp) \
147 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
150 static const int nvpair_max_recursion
= 20;
152 static const int nvpair_max_recursion
= 100;
155 static const uint64_t nvlist_hashtable_init_size
= (1 << 4);
158 nv_alloc_init(nv_alloc_t
*nva
, const nv_alloc_ops_t
*nvo
, /* args */ ...)
166 va_start(valist
, nvo
);
167 if (nva
->nva_ops
->nv_ao_init
!= NULL
)
168 err
= nva
->nva_ops
->nv_ao_init(nva
, valist
);
175 nv_alloc_reset(nv_alloc_t
*nva
)
177 if (nva
->nva_ops
->nv_ao_reset
!= NULL
)
178 nva
->nva_ops
->nv_ao_reset(nva
);
182 nv_alloc_fini(nv_alloc_t
*nva
)
184 if (nva
->nva_ops
->nv_ao_fini
!= NULL
)
185 nva
->nva_ops
->nv_ao_fini(nva
);
189 nvlist_lookup_nv_alloc(nvlist_t
*nvl
)
194 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
197 return (priv
->nvp_nva
);
201 nv_mem_zalloc(nvpriv_t
*nvp
, size_t size
)
203 nv_alloc_t
*nva
= nvp
->nvp_nva
;
206 if ((buf
= nva
->nva_ops
->nv_ao_alloc(nva
, size
)) != NULL
)
207 memset(buf
, 0, size
);
213 nv_mem_free(nvpriv_t
*nvp
, void *buf
, size_t size
)
215 nv_alloc_t
*nva
= nvp
->nvp_nva
;
217 nva
->nva_ops
->nv_ao_free(nva
, buf
, size
);
221 nv_priv_init(nvpriv_t
*priv
, nv_alloc_t
*nva
, uint32_t stat
)
223 memset(priv
, 0, sizeof (nvpriv_t
));
226 priv
->nvp_stat
= stat
;
230 nv_priv_alloc(nv_alloc_t
*nva
)
235 * nv_mem_alloc() cannot called here because it needs the priv
238 if ((priv
= nva
->nva_ops
->nv_ao_alloc(nva
, sizeof (nvpriv_t
))) == NULL
)
241 nv_priv_init(priv
, nva
, 0);
247 * Embedded lists need their own nvpriv_t's. We create a new
248 * nvpriv_t using the parameters and allocator from the parent
252 nv_priv_alloc_embedded(nvpriv_t
*priv
)
256 if ((emb_priv
= nv_mem_zalloc(priv
, sizeof (nvpriv_t
))) == NULL
)
259 nv_priv_init(emb_priv
, priv
->nvp_nva
, NV_STAT_EMBEDDED
);
265 nvt_tab_alloc(nvpriv_t
*priv
, uint64_t buckets
)
267 ASSERT3P(priv
->nvp_hashtable
, ==, NULL
);
268 ASSERT0(priv
->nvp_nbuckets
);
269 ASSERT0(priv
->nvp_nentries
);
271 i_nvp_t
**tab
= nv_mem_zalloc(priv
, buckets
* sizeof (i_nvp_t
*));
275 priv
->nvp_hashtable
= tab
;
276 priv
->nvp_nbuckets
= buckets
;
281 nvt_tab_free(nvpriv_t
*priv
)
283 i_nvp_t
**tab
= priv
->nvp_hashtable
;
285 ASSERT0(priv
->nvp_nbuckets
);
286 ASSERT0(priv
->nvp_nentries
);
290 nv_mem_free(priv
, tab
, priv
->nvp_nbuckets
* sizeof (i_nvp_t
*));
292 priv
->nvp_hashtable
= NULL
;
293 priv
->nvp_nbuckets
= 0;
294 priv
->nvp_nentries
= 0;
298 nvt_hash(const char *p
)
300 uint32_t g
, hval
= 0;
303 hval
= (hval
<< 4) + *p
++;
304 if ((g
= (hval
& 0xf0000000)) != 0)
312 nvt_nvpair_match(const nvpair_t
*nvp1
, const nvpair_t
*nvp2
, uint32_t nvflag
)
314 boolean_t match
= B_FALSE
;
315 if (nvflag
& NV_UNIQUE_NAME_TYPE
) {
316 if (strcmp(NVP_NAME(nvp1
), NVP_NAME(nvp2
)) == 0 &&
317 NVP_TYPE(nvp1
) == NVP_TYPE(nvp2
))
320 ASSERT(nvflag
== 0 || nvflag
& NV_UNIQUE_NAME
);
321 if (strcmp(NVP_NAME(nvp1
), NVP_NAME(nvp2
)) == 0)
328 nvt_lookup_name_type(const nvlist_t
*nvl
, const char *name
, data_type_t type
)
330 const nvpriv_t
*priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
331 ASSERT(priv
!= NULL
);
333 i_nvp_t
**tab
= priv
->nvp_hashtable
;
336 ASSERT3P(priv
->nvp_list
, ==, NULL
);
337 ASSERT0(priv
->nvp_nbuckets
);
338 ASSERT0(priv
->nvp_nentries
);
341 ASSERT(priv
->nvp_nbuckets
!= 0);
344 uint64_t hash
= nvt_hash(name
);
345 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
347 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
348 i_nvp_t
*entry
= tab
[index
];
350 for (i_nvp_t
*e
= entry
; e
!= NULL
; e
= e
->nvi_hashtable_next
) {
351 if (strcmp(NVP_NAME(&e
->nvi_nvp
), name
) == 0 &&
352 (type
== DATA_TYPE_DONTCARE
||
353 NVP_TYPE(&e
->nvi_nvp
) == type
))
354 return (&e
->nvi_nvp
);
360 nvt_lookup_name(const nvlist_t
*nvl
, const char *name
)
362 return (nvt_lookup_name_type(nvl
, name
, DATA_TYPE_DONTCARE
));
366 nvt_resize(nvpriv_t
*priv
, uint32_t new_size
)
368 i_nvp_t
**tab
= priv
->nvp_hashtable
;
371 * Migrate all the entries from the current table
372 * to a newly-allocated table with the new size by
373 * re-adjusting the pointers of their entries.
375 uint32_t size
= priv
->nvp_nbuckets
;
376 uint32_t new_mask
= new_size
- 1;
377 ASSERT(ISP2(new_size
));
379 i_nvp_t
**new_tab
= nv_mem_zalloc(priv
, new_size
* sizeof (i_nvp_t
*));
383 uint32_t nentries
= 0;
384 for (uint32_t i
= 0; i
< size
; i
++) {
385 i_nvp_t
*next
, *e
= tab
[i
];
388 next
= e
->nvi_hashtable_next
;
390 uint32_t hash
= nvt_hash(NVP_NAME(&e
->nvi_nvp
));
391 uint32_t index
= hash
& new_mask
;
393 e
->nvi_hashtable_next
= new_tab
[index
];
401 ASSERT3U(nentries
, ==, priv
->nvp_nentries
);
405 priv
->nvp_hashtable
= new_tab
;
406 priv
->nvp_nbuckets
= new_size
;
407 priv
->nvp_nentries
= nentries
;
413 nvt_needs_togrow(nvpriv_t
*priv
)
416 * Grow only when we have more elements than buckets
417 * and the # of buckets doesn't overflow.
419 return (priv
->nvp_nentries
> priv
->nvp_nbuckets
&&
420 (UINT32_MAX
>> 1) >= priv
->nvp_nbuckets
);
424 * Allocate a new table that's twice the size of the old one,
425 * and migrate all the entries from the old one to the new
426 * one by re-adjusting their pointers.
429 nvt_grow(nvpriv_t
*priv
)
431 uint32_t current_size
= priv
->nvp_nbuckets
;
432 /* ensure we won't overflow */
433 ASSERT3U(UINT32_MAX
>> 1, >=, current_size
);
434 return (nvt_resize(priv
, current_size
<< 1));
438 nvt_needs_toshrink(nvpriv_t
*priv
)
441 * Shrink only when the # of elements is less than or
442 * equal to 1/4 the # of buckets. Never shrink less than
443 * nvlist_hashtable_init_size.
445 ASSERT3U(priv
->nvp_nbuckets
, >=, nvlist_hashtable_init_size
);
446 if (priv
->nvp_nbuckets
== nvlist_hashtable_init_size
)
448 return (priv
->nvp_nentries
<= (priv
->nvp_nbuckets
>> 2));
452 * Allocate a new table that's half the size of the old one,
453 * and migrate all the entries from the old one to the new
454 * one by re-adjusting their pointers.
457 nvt_shrink(nvpriv_t
*priv
)
459 uint32_t current_size
= priv
->nvp_nbuckets
;
460 /* ensure we won't overflow */
461 ASSERT3U(current_size
, >=, nvlist_hashtable_init_size
);
462 return (nvt_resize(priv
, current_size
>> 1));
466 nvt_remove_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
468 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
470 if (nvt_needs_toshrink(priv
)) {
471 int err
= nvt_shrink(priv
);
475 i_nvp_t
**tab
= priv
->nvp_hashtable
;
477 const char *name
= NVP_NAME(nvp
);
478 uint64_t hash
= nvt_hash(name
);
479 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
481 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
482 i_nvp_t
*bucket
= tab
[index
];
484 for (i_nvp_t
*prev
= NULL
, *e
= bucket
;
485 e
!= NULL
; prev
= e
, e
= e
->nvi_hashtable_next
) {
486 if (nvt_nvpair_match(&e
->nvi_nvp
, nvp
, nvl
->nvl_nvflag
)) {
488 prev
->nvi_hashtable_next
=
489 e
->nvi_hashtable_next
;
491 ASSERT3P(e
, ==, bucket
);
492 tab
[index
] = e
->nvi_hashtable_next
;
494 e
->nvi_hashtable_next
= NULL
;
495 priv
->nvp_nentries
--;
504 nvt_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
506 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
508 /* initialize nvpair table now if it doesn't exist. */
509 if (priv
->nvp_hashtable
== NULL
) {
510 int err
= nvt_tab_alloc(priv
, nvlist_hashtable_init_size
);
516 * if we don't allow duplicate entries, make sure to
517 * unlink any existing entries from the table.
519 if (nvl
->nvl_nvflag
!= 0) {
520 int err
= nvt_remove_nvpair(nvl
, nvp
);
525 if (nvt_needs_togrow(priv
)) {
526 int err
= nvt_grow(priv
);
530 i_nvp_t
**tab
= priv
->nvp_hashtable
;
532 const char *name
= NVP_NAME(nvp
);
533 uint64_t hash
= nvt_hash(name
);
534 uint64_t index
= hash
& (priv
->nvp_nbuckets
- 1);
536 ASSERT3U(index
, <, priv
->nvp_nbuckets
);
537 // cppcheck-suppress nullPointerRedundantCheck
538 i_nvp_t
*bucket
= tab
[index
];
540 /* insert link at the beginning of the bucket */
541 i_nvp_t
*new_entry
= NVPAIR2I_NVP(nvp
);
542 ASSERT3P(new_entry
->nvi_hashtable_next
, ==, NULL
);
543 new_entry
->nvi_hashtable_next
= bucket
;
544 // cppcheck-suppress nullPointerRedundantCheck
545 tab
[index
] = new_entry
;
547 priv
->nvp_nentries
++;
552 nvlist_init(nvlist_t
*nvl
, uint32_t nvflag
, nvpriv_t
*priv
)
554 nvl
->nvl_version
= NV_VERSION
;
555 nvl
->nvl_nvflag
= nvflag
& (NV_UNIQUE_NAME
|NV_UNIQUE_NAME_TYPE
);
556 nvl
->nvl_priv
= (uint64_t)(uintptr_t)priv
;
562 nvlist_nvflag(nvlist_t
*nvl
)
564 return (nvl
->nvl_nvflag
);
568 nvlist_nv_alloc(int kmflag
)
573 return (nv_alloc_sleep
);
575 return (nv_alloc_nosleep
);
577 return (nv_alloc_pushpage
);
581 return (nv_alloc_nosleep
);
586 * nvlist_alloc - Allocate nvlist.
589 nvlist_alloc(nvlist_t
**nvlp
, uint_t nvflag
, int kmflag
)
591 return (nvlist_xalloc(nvlp
, nvflag
, nvlist_nv_alloc(kmflag
)));
595 nvlist_xalloc(nvlist_t
**nvlp
, uint_t nvflag
, nv_alloc_t
*nva
)
599 if (nvlp
== NULL
|| nva
== NULL
)
602 if ((priv
= nv_priv_alloc(nva
)) == NULL
)
605 if ((*nvlp
= nv_mem_zalloc(priv
,
606 NV_ALIGN(sizeof (nvlist_t
)))) == NULL
) {
607 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
611 nvlist_init(*nvlp
, nvflag
, priv
);
617 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
620 nvp_buf_alloc(nvlist_t
*nvl
, size_t len
)
622 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
628 * Allocate the buffer
630 nvsize
= len
+ offsetof(i_nvp_t
, nvi_nvp
);
632 if ((buf
= nv_mem_zalloc(priv
, nvsize
)) == NULL
)
642 * nvp_buf_free - de-Allocate an i_nvp_t.
645 nvp_buf_free(nvlist_t
*nvl
, nvpair_t
*nvp
)
647 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
648 size_t nvsize
= nvp
->nvp_size
+ offsetof(i_nvp_t
, nvi_nvp
);
650 nv_mem_free(priv
, NVPAIR2I_NVP(nvp
), nvsize
);
654 * nvp_buf_link - link a new nv pair into the nvlist.
657 nvp_buf_link(nvlist_t
*nvl
, nvpair_t
*nvp
)
659 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
660 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
662 /* Put element at end of nvlist */
663 if (priv
->nvp_list
== NULL
) {
664 priv
->nvp_list
= priv
->nvp_last
= curr
;
666 curr
->nvi_prev
= priv
->nvp_last
;
667 priv
->nvp_last
->nvi_next
= curr
;
668 priv
->nvp_last
= curr
;
673 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
676 nvp_buf_unlink(nvlist_t
*nvl
, nvpair_t
*nvp
)
678 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
679 i_nvp_t
*curr
= NVPAIR2I_NVP(nvp
);
682 * protect nvlist_next_nvpair() against walking on freed memory.
684 if (priv
->nvp_curr
== curr
)
685 priv
->nvp_curr
= curr
->nvi_next
;
687 if (curr
== priv
->nvp_list
)
688 priv
->nvp_list
= curr
->nvi_next
;
690 curr
->nvi_prev
->nvi_next
= curr
->nvi_next
;
692 if (curr
== priv
->nvp_last
)
693 priv
->nvp_last
= curr
->nvi_prev
;
695 curr
->nvi_next
->nvi_prev
= curr
->nvi_prev
;
699 * take a nvpair type and number of elements and make sure the are valid
702 i_validate_type_nelem(data_type_t type
, uint_t nelem
)
705 case DATA_TYPE_BOOLEAN
:
709 case DATA_TYPE_BOOLEAN_VALUE
:
712 case DATA_TYPE_UINT8
:
713 case DATA_TYPE_INT16
:
714 case DATA_TYPE_UINT16
:
715 case DATA_TYPE_INT32
:
716 case DATA_TYPE_UINT32
:
717 case DATA_TYPE_INT64
:
718 case DATA_TYPE_UINT64
:
719 case DATA_TYPE_STRING
:
720 case DATA_TYPE_HRTIME
:
721 case DATA_TYPE_NVLIST
:
722 #if !defined(_KERNEL)
723 case DATA_TYPE_DOUBLE
:
728 case DATA_TYPE_BOOLEAN_ARRAY
:
729 case DATA_TYPE_BYTE_ARRAY
:
730 case DATA_TYPE_INT8_ARRAY
:
731 case DATA_TYPE_UINT8_ARRAY
:
732 case DATA_TYPE_INT16_ARRAY
:
733 case DATA_TYPE_UINT16_ARRAY
:
734 case DATA_TYPE_INT32_ARRAY
:
735 case DATA_TYPE_UINT32_ARRAY
:
736 case DATA_TYPE_INT64_ARRAY
:
737 case DATA_TYPE_UINT64_ARRAY
:
738 case DATA_TYPE_STRING_ARRAY
:
739 case DATA_TYPE_NVLIST_ARRAY
:
740 /* we allow arrays with 0 elements */
749 * Verify nvp_name_sz and check the name string length.
752 i_validate_nvpair_name(nvpair_t
*nvp
)
754 if ((nvp
->nvp_name_sz
<= 0) ||
755 (nvp
->nvp_size
< NVP_SIZE_CALC(nvp
->nvp_name_sz
, 0)))
758 /* verify the name string, make sure its terminated */
759 if (NVP_NAME(nvp
)[nvp
->nvp_name_sz
- 1] != '\0')
762 return (strlen(NVP_NAME(nvp
)) == nvp
->nvp_name_sz
- 1 ? 0 : EFAULT
);
766 i_validate_nvpair_value(data_type_t type
, uint_t nelem
, const void *data
)
769 case DATA_TYPE_BOOLEAN_VALUE
:
770 if (*(boolean_t
*)data
!= B_TRUE
&&
771 *(boolean_t
*)data
!= B_FALSE
)
774 case DATA_TYPE_BOOLEAN_ARRAY
: {
777 for (i
= 0; i
< nelem
; i
++)
778 if (((boolean_t
*)data
)[i
] != B_TRUE
&&
779 ((boolean_t
*)data
)[i
] != B_FALSE
)
791 * This function takes a pointer to what should be a nvpair and it's size
792 * and then verifies that all the nvpair fields make sense and can be
793 * trusted. This function is used when decoding packed nvpairs.
796 i_validate_nvpair(nvpair_t
*nvp
)
798 data_type_t type
= NVP_TYPE(nvp
);
801 /* verify nvp_name_sz, check the name string length */
802 if (i_validate_nvpair_name(nvp
) != 0)
805 if (i_validate_nvpair_value(type
, NVP_NELEM(nvp
), NVP_VALUE(nvp
)) != 0)
809 * verify nvp_type, nvp_value_elem, and also possibly
810 * verify string values and get the value size.
812 size2
= i_get_value_size(type
, NVP_VALUE(nvp
), NVP_NELEM(nvp
));
813 size1
= nvp
->nvp_size
- NVP_VALOFF(nvp
);
814 if (size2
< 0 || size1
!= NV_ALIGN(size2
))
821 nvlist_copy_pairs(const nvlist_t
*snvl
, nvlist_t
*dnvl
)
823 const nvpriv_t
*priv
;
826 if ((priv
= (const nvpriv_t
*)(uintptr_t)snvl
->nvl_priv
) == NULL
)
829 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
830 const nvpair_t
*nvp
= &curr
->nvi_nvp
;
833 if ((err
= nvlist_add_common(dnvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
834 NVP_NELEM(nvp
), NVP_VALUE(nvp
))) != 0)
842 * Frees all memory allocated for an nvpair (like embedded lists) with
843 * the exception of the nvpair buffer itself.
846 nvpair_free(nvpair_t
*nvp
)
848 switch (NVP_TYPE(nvp
)) {
849 case DATA_TYPE_NVLIST
:
850 nvlist_free(EMBEDDED_NVL(nvp
));
852 case DATA_TYPE_NVLIST_ARRAY
: {
853 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
856 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
858 nvlist_free(nvlp
[i
]);
867 * nvlist_free - free an unpacked nvlist
870 nvlist_free(nvlist_t
*nvl
)
876 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
880 * Unpacked nvlist are linked through i_nvp_t
882 curr
= priv
->nvp_list
;
883 while (curr
!= NULL
) {
884 nvpair_t
*nvp
= &curr
->nvi_nvp
;
885 curr
= curr
->nvi_next
;
888 nvp_buf_free(nvl
, nvp
);
891 if (!(priv
->nvp_stat
& NV_STAT_EMBEDDED
))
892 nv_mem_free(priv
, nvl
, NV_ALIGN(sizeof (nvlist_t
)));
897 nv_mem_free(priv
, priv
, sizeof (nvpriv_t
));
901 nvlist_contains_nvp(const nvlist_t
*nvl
, const nvpair_t
*nvp
)
903 const nvpriv_t
*priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
909 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
910 if (&curr
->nvi_nvp
== nvp
)
917 * Make a copy of nvlist
920 nvlist_dup(const nvlist_t
*nvl
, nvlist_t
**nvlp
, int kmflag
)
922 return (nvlist_xdup(nvl
, nvlp
, nvlist_nv_alloc(kmflag
)));
926 nvlist_xdup(const nvlist_t
*nvl
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
931 if (nvl
== NULL
|| nvlp
== NULL
)
934 if ((err
= nvlist_xalloc(&ret
, nvl
->nvl_nvflag
, nva
)) != 0)
937 if ((err
= nvlist_copy_pairs(nvl
, ret
)) != 0)
946 * Remove all with matching name
949 nvlist_remove_all(nvlist_t
*nvl
, const char *name
)
953 if (nvl
== NULL
|| name
== NULL
|| nvl
->nvl_priv
== 0)
957 while ((nvp
= nvt_lookup_name(nvl
, name
)) != NULL
) {
958 VERIFY0(nvlist_remove_nvpair(nvl
, nvp
));
966 * Remove first one with matching name and type
969 nvlist_remove(nvlist_t
*nvl
, const char *name
, data_type_t type
)
971 if (nvl
== NULL
|| name
== NULL
|| nvl
->nvl_priv
== 0)
974 nvpair_t
*nvp
= nvt_lookup_name_type(nvl
, name
, type
);
978 return (nvlist_remove_nvpair(nvl
, nvp
));
982 nvlist_remove_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
984 if (nvl
== NULL
|| nvp
== NULL
)
987 int err
= nvt_remove_nvpair(nvl
, nvp
);
991 nvp_buf_unlink(nvl
, nvp
);
993 nvp_buf_free(nvl
, nvp
);
998 * This function calculates the size of an nvpair value.
1000 * The data argument controls the behavior in case of the data types
1001 * DATA_TYPE_STRING and
1002 * DATA_TYPE_STRING_ARRAY
1003 * Is data == NULL then the size of the string(s) is excluded.
1006 i_get_value_size(data_type_t type
, const void *data
, uint_t nelem
)
1010 if (i_validate_type_nelem(type
, nelem
) != 0)
1013 /* Calculate required size for holding value */
1015 case DATA_TYPE_BOOLEAN
:
1018 case DATA_TYPE_BOOLEAN_VALUE
:
1019 value_sz
= sizeof (boolean_t
);
1021 case DATA_TYPE_BYTE
:
1022 value_sz
= sizeof (uchar_t
);
1024 case DATA_TYPE_INT8
:
1025 value_sz
= sizeof (int8_t);
1027 case DATA_TYPE_UINT8
:
1028 value_sz
= sizeof (uint8_t);
1030 case DATA_TYPE_INT16
:
1031 value_sz
= sizeof (int16_t);
1033 case DATA_TYPE_UINT16
:
1034 value_sz
= sizeof (uint16_t);
1036 case DATA_TYPE_INT32
:
1037 value_sz
= sizeof (int32_t);
1039 case DATA_TYPE_UINT32
:
1040 value_sz
= sizeof (uint32_t);
1042 case DATA_TYPE_INT64
:
1043 value_sz
= sizeof (int64_t);
1045 case DATA_TYPE_UINT64
:
1046 value_sz
= sizeof (uint64_t);
1048 #if !defined(_KERNEL)
1049 case DATA_TYPE_DOUBLE
:
1050 value_sz
= sizeof (double);
1053 case DATA_TYPE_STRING
:
1057 value_sz
= strlen(data
) + 1;
1059 case DATA_TYPE_BOOLEAN_ARRAY
:
1060 value_sz
= (uint64_t)nelem
* sizeof (boolean_t
);
1062 case DATA_TYPE_BYTE_ARRAY
:
1063 value_sz
= (uint64_t)nelem
* sizeof (uchar_t
);
1065 case DATA_TYPE_INT8_ARRAY
:
1066 value_sz
= (uint64_t)nelem
* sizeof (int8_t);
1068 case DATA_TYPE_UINT8_ARRAY
:
1069 value_sz
= (uint64_t)nelem
* sizeof (uint8_t);
1071 case DATA_TYPE_INT16_ARRAY
:
1072 value_sz
= (uint64_t)nelem
* sizeof (int16_t);
1074 case DATA_TYPE_UINT16_ARRAY
:
1075 value_sz
= (uint64_t)nelem
* sizeof (uint16_t);
1077 case DATA_TYPE_INT32_ARRAY
:
1078 value_sz
= (uint64_t)nelem
* sizeof (int32_t);
1080 case DATA_TYPE_UINT32_ARRAY
:
1081 value_sz
= (uint64_t)nelem
* sizeof (uint32_t);
1083 case DATA_TYPE_INT64_ARRAY
:
1084 value_sz
= (uint64_t)nelem
* sizeof (int64_t);
1086 case DATA_TYPE_UINT64_ARRAY
:
1087 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
1089 case DATA_TYPE_STRING_ARRAY
:
1090 value_sz
= (uint64_t)nelem
* sizeof (uint64_t);
1093 char *const *strs
= data
;
1096 /* no alignment requirement for strings */
1097 for (i
= 0; i
< nelem
; i
++) {
1098 if (strs
[i
] == NULL
)
1100 value_sz
+= strlen(strs
[i
]) + 1;
1104 case DATA_TYPE_HRTIME
:
1105 value_sz
= sizeof (hrtime_t
);
1107 case DATA_TYPE_NVLIST
:
1108 value_sz
= NV_ALIGN(sizeof (nvlist_t
));
1110 case DATA_TYPE_NVLIST_ARRAY
:
1111 value_sz
= (uint64_t)nelem
* sizeof (uint64_t) +
1112 (uint64_t)nelem
* NV_ALIGN(sizeof (nvlist_t
));
1118 return (value_sz
> INT32_MAX
? -1 : (int)value_sz
);
1122 nvlist_copy_embedded(nvlist_t
*nvl
, nvlist_t
*onvl
, nvlist_t
*emb_nvl
)
1127 if ((priv
= nv_priv_alloc_embedded((nvpriv_t
*)(uintptr_t)
1128 nvl
->nvl_priv
)) == NULL
)
1131 nvlist_init(emb_nvl
, onvl
->nvl_nvflag
, priv
);
1133 if ((err
= nvlist_copy_pairs(onvl
, emb_nvl
)) != 0) {
1134 nvlist_free(emb_nvl
);
1135 emb_nvl
->nvl_priv
= 0;
1142 * nvlist_add_common - Add new <name,value> pair to nvlist
1145 nvlist_add_common(nvlist_t
*nvl
, const char *name
,
1146 data_type_t type
, uint_t nelem
, const void *data
)
1151 int nvp_sz
, name_sz
, value_sz
;
1154 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
1157 if (nelem
!= 0 && data
== NULL
)
1161 * Verify type and nelem and get the value size.
1162 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
1163 * is the size of the string(s) included.
1165 if ((value_sz
= i_get_value_size(type
, data
, nelem
)) < 0)
1168 if (i_validate_nvpair_value(type
, nelem
, data
) != 0)
1172 * If we're adding an nvlist or nvlist array, ensure that we are not
1173 * adding the input nvlist to itself, which would cause recursion,
1174 * and ensure that no NULL nvlist pointers are present.
1177 case DATA_TYPE_NVLIST
:
1178 if (data
== nvl
|| data
== NULL
)
1181 case DATA_TYPE_NVLIST_ARRAY
: {
1182 nvlist_t
**onvlp
= (nvlist_t
**)data
;
1183 for (i
= 0; i
< nelem
; i
++) {
1184 if (onvlp
[i
] == nvl
|| onvlp
[i
] == NULL
)
1193 /* calculate sizes of the nvpair elements and the nvpair itself */
1194 name_sz
= strlen(name
) + 1;
1195 if (name_sz
>= 1ULL << (sizeof (nvp
->nvp_name_sz
) * NBBY
- 1))
1198 nvp_sz
= NVP_SIZE_CALC(name_sz
, value_sz
);
1200 if ((nvp
= nvp_buf_alloc(nvl
, nvp_sz
)) == NULL
)
1203 ASSERT(nvp
->nvp_size
== nvp_sz
);
1204 nvp
->nvp_name_sz
= name_sz
;
1205 nvp
->nvp_value_elem
= nelem
;
1206 nvp
->nvp_type
= type
;
1207 memcpy(NVP_NAME(nvp
), name
, name_sz
);
1210 case DATA_TYPE_BOOLEAN
:
1212 case DATA_TYPE_STRING_ARRAY
: {
1213 char *const *strs
= data
;
1214 char *buf
= NVP_VALUE(nvp
);
1215 char **cstrs
= (void *)buf
;
1217 /* skip pre-allocated space for pointer array */
1218 buf
+= nelem
* sizeof (uint64_t);
1219 for (i
= 0; i
< nelem
; i
++) {
1220 int slen
= strlen(strs
[i
]) + 1;
1221 memcpy(buf
, strs
[i
], slen
);
1227 case DATA_TYPE_NVLIST
: {
1228 nvlist_t
*nnvl
= EMBEDDED_NVL(nvp
);
1229 nvlist_t
*onvl
= (nvlist_t
*)data
;
1231 if ((err
= nvlist_copy_embedded(nvl
, onvl
, nnvl
)) != 0) {
1232 nvp_buf_free(nvl
, nvp
);
1237 case DATA_TYPE_NVLIST_ARRAY
: {
1238 nvlist_t
**onvlp
= (nvlist_t
**)data
;
1239 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
1240 nvlist_t
*embedded
= (nvlist_t
*)
1241 ((uintptr_t)nvlp
+ nelem
* sizeof (uint64_t));
1243 for (i
= 0; i
< nelem
; i
++) {
1244 if ((err
= nvlist_copy_embedded(nvl
,
1245 onvlp
[i
], embedded
)) != 0) {
1247 * Free any successfully created lists
1250 nvp_buf_free(nvl
, nvp
);
1254 nvlp
[i
] = embedded
++;
1259 memcpy(NVP_VALUE(nvp
), data
, value_sz
);
1262 /* if unique name, remove before add */
1263 if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME
)
1264 (void) nvlist_remove_all(nvl
, name
);
1265 else if (nvl
->nvl_nvflag
& NV_UNIQUE_NAME_TYPE
)
1266 (void) nvlist_remove(nvl
, name
, type
);
1268 err
= nvt_add_nvpair(nvl
, nvp
);
1271 nvp_buf_free(nvl
, nvp
);
1274 nvp_buf_link(nvl
, nvp
);
1280 nvlist_add_boolean(nvlist_t
*nvl
, const char *name
)
1282 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN
, 0, NULL
));
1286 nvlist_add_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t val
)
1288 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_VALUE
, 1, &val
));
1292 nvlist_add_byte(nvlist_t
*nvl
, const char *name
, uchar_t val
)
1294 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE
, 1, &val
));
1298 nvlist_add_int8(nvlist_t
*nvl
, const char *name
, int8_t val
)
1300 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8
, 1, &val
));
1304 nvlist_add_uint8(nvlist_t
*nvl
, const char *name
, uint8_t val
)
1306 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8
, 1, &val
));
1310 nvlist_add_int16(nvlist_t
*nvl
, const char *name
, int16_t val
)
1312 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16
, 1, &val
));
1316 nvlist_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
1318 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16
, 1, &val
));
1322 nvlist_add_int32(nvlist_t
*nvl
, const char *name
, int32_t val
)
1324 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32
, 1, &val
));
1328 nvlist_add_uint32(nvlist_t
*nvl
, const char *name
, uint32_t val
)
1330 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32
, 1, &val
));
1334 nvlist_add_int64(nvlist_t
*nvl
, const char *name
, int64_t val
)
1336 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64
, 1, &val
));
1340 nvlist_add_uint64(nvlist_t
*nvl
, const char *name
, uint64_t val
)
1342 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64
, 1, &val
));
1345 #if !defined(_KERNEL)
1347 nvlist_add_double(nvlist_t
*nvl
, const char *name
, double val
)
1349 return (nvlist_add_common(nvl
, name
, DATA_TYPE_DOUBLE
, 1, &val
));
1354 nvlist_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
1356 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING
, 1, (void *)val
));
1360 nvlist_add_boolean_array(nvlist_t
*nvl
, const char *name
,
1361 const boolean_t
*a
, uint_t n
)
1363 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1367 nvlist_add_byte_array(nvlist_t
*nvl
, const char *name
, const uchar_t
*a
,
1370 return (nvlist_add_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1374 nvlist_add_int8_array(nvlist_t
*nvl
, const char *name
, const int8_t *a
,
1377 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1381 nvlist_add_uint8_array(nvlist_t
*nvl
, const char *name
, const uint8_t *a
,
1384 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1388 nvlist_add_int16_array(nvlist_t
*nvl
, const char *name
, const int16_t *a
,
1391 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1395 nvlist_add_uint16_array(nvlist_t
*nvl
, const char *name
, const uint16_t *a
,
1398 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1402 nvlist_add_int32_array(nvlist_t
*nvl
, const char *name
, const int32_t *a
,
1405 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1409 nvlist_add_uint32_array(nvlist_t
*nvl
, const char *name
, const uint32_t *a
,
1412 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1416 nvlist_add_int64_array(nvlist_t
*nvl
, const char *name
, const int64_t *a
,
1419 return (nvlist_add_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1423 nvlist_add_uint64_array(nvlist_t
*nvl
, const char *name
, const uint64_t *a
,
1426 return (nvlist_add_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1430 nvlist_add_string_array(nvlist_t
*nvl
, const char *name
,
1431 const char *const *a
, uint_t n
)
1433 return (nvlist_add_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1437 nvlist_add_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t val
)
1439 return (nvlist_add_common(nvl
, name
, DATA_TYPE_HRTIME
, 1, &val
));
1443 nvlist_add_nvlist(nvlist_t
*nvl
, const char *name
, const nvlist_t
*val
)
1445 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST
, 1, val
));
1449 nvlist_add_nvlist_array(nvlist_t
*nvl
, const char *name
,
1450 const nvlist_t
* const *a
, uint_t n
)
1452 return (nvlist_add_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1455 /* reading name-value pairs */
1457 nvlist_next_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
1463 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1466 curr
= NVPAIR2I_NVP(nvp
);
1469 * Ensure that nvp is a valid nvpair on this nvlist.
1470 * NB: nvp_curr is used only as a hint so that we don't always
1471 * have to walk the list to determine if nvp is still on the list.
1474 curr
= priv
->nvp_list
;
1475 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1476 curr
= curr
->nvi_next
;
1480 priv
->nvp_curr
= curr
;
1482 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1486 nvlist_prev_nvpair(nvlist_t
*nvl
, const nvpair_t
*nvp
)
1492 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1495 curr
= NVPAIR2I_NVP(nvp
);
1498 curr
= priv
->nvp_last
;
1499 else if (priv
->nvp_curr
== curr
|| nvlist_contains_nvp(nvl
, nvp
))
1500 curr
= curr
->nvi_prev
;
1504 priv
->nvp_curr
= curr
;
1506 return (curr
!= NULL
? &curr
->nvi_nvp
: NULL
);
1510 nvlist_empty(const nvlist_t
*nvl
)
1512 const nvpriv_t
*priv
;
1515 (priv
= (const nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
1518 return (priv
->nvp_list
== NULL
);
1522 nvpair_name(const nvpair_t
*nvp
)
1524 return (NVP_NAME(nvp
));
1528 nvpair_type(const nvpair_t
*nvp
)
1530 return (NVP_TYPE(nvp
));
1534 nvpair_type_is_array(const nvpair_t
*nvp
)
1536 data_type_t type
= NVP_TYPE(nvp
);
1538 if ((type
== DATA_TYPE_BYTE_ARRAY
) ||
1539 (type
== DATA_TYPE_INT8_ARRAY
) ||
1540 (type
== DATA_TYPE_UINT8_ARRAY
) ||
1541 (type
== DATA_TYPE_INT16_ARRAY
) ||
1542 (type
== DATA_TYPE_UINT16_ARRAY
) ||
1543 (type
== DATA_TYPE_INT32_ARRAY
) ||
1544 (type
== DATA_TYPE_UINT32_ARRAY
) ||
1545 (type
== DATA_TYPE_INT64_ARRAY
) ||
1546 (type
== DATA_TYPE_UINT64_ARRAY
) ||
1547 (type
== DATA_TYPE_BOOLEAN_ARRAY
) ||
1548 (type
== DATA_TYPE_STRING_ARRAY
) ||
1549 (type
== DATA_TYPE_NVLIST_ARRAY
))
1556 nvpair_value_common(const nvpair_t
*nvp
, data_type_t type
, uint_t
*nelem
,
1561 if (nvp
== NULL
|| nvpair_type(nvp
) != type
)
1565 * For non-array types, we copy the data.
1566 * For array types (including string), we set a pointer.
1569 case DATA_TYPE_BOOLEAN
:
1574 case DATA_TYPE_BOOLEAN_VALUE
:
1575 case DATA_TYPE_BYTE
:
1576 case DATA_TYPE_INT8
:
1577 case DATA_TYPE_UINT8
:
1578 case DATA_TYPE_INT16
:
1579 case DATA_TYPE_UINT16
:
1580 case DATA_TYPE_INT32
:
1581 case DATA_TYPE_UINT32
:
1582 case DATA_TYPE_INT64
:
1583 case DATA_TYPE_UINT64
:
1584 case DATA_TYPE_HRTIME
:
1585 #if !defined(_KERNEL)
1586 case DATA_TYPE_DOUBLE
:
1590 if ((value_sz
= i_get_value_size(type
, NULL
, 1)) < 0)
1592 memcpy(data
, NVP_VALUE(nvp
), (size_t)value_sz
);
1597 case DATA_TYPE_NVLIST
:
1598 case DATA_TYPE_STRING
:
1602 * This discards the const from nvp, so all callers for these
1603 * types must not accept const nvpairs.
1605 *(void **)data
= (void *)NVP_VALUE(nvp
);
1610 case DATA_TYPE_BOOLEAN_ARRAY
:
1611 case DATA_TYPE_BYTE_ARRAY
:
1612 case DATA_TYPE_INT8_ARRAY
:
1613 case DATA_TYPE_UINT8_ARRAY
:
1614 case DATA_TYPE_INT16_ARRAY
:
1615 case DATA_TYPE_UINT16_ARRAY
:
1616 case DATA_TYPE_INT32_ARRAY
:
1617 case DATA_TYPE_UINT32_ARRAY
:
1618 case DATA_TYPE_INT64_ARRAY
:
1619 case DATA_TYPE_UINT64_ARRAY
:
1620 case DATA_TYPE_STRING_ARRAY
:
1621 case DATA_TYPE_NVLIST_ARRAY
:
1622 if (nelem
== NULL
|| data
== NULL
)
1625 * This discards the const from nvp, so all callers for these
1626 * types must not accept const nvpairs.
1628 if ((*nelem
= NVP_NELEM(nvp
)) != 0)
1629 *(void **)data
= (void *)NVP_VALUE(nvp
);
1631 *(void **)data
= NULL
;
1642 nvlist_lookup_common(const nvlist_t
*nvl
, const char *name
, data_type_t type
,
1643 uint_t
*nelem
, void *data
)
1645 if (name
== NULL
|| nvl
== NULL
|| nvl
->nvl_priv
== 0)
1648 if (!(nvl
->nvl_nvflag
& (NV_UNIQUE_NAME
| NV_UNIQUE_NAME_TYPE
)))
1651 nvpair_t
*nvp
= nvt_lookup_name_type(nvl
, name
, type
);
1655 return (nvpair_value_common(nvp
, type
, nelem
, data
));
1659 nvlist_lookup_boolean(const nvlist_t
*nvl
, const char *name
)
1661 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BOOLEAN
, NULL
, NULL
));
1665 nvlist_lookup_boolean_value(const nvlist_t
*nvl
, const char *name
,
1668 return (nvlist_lookup_common(nvl
, name
,
1669 DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
1673 nvlist_lookup_byte(const nvlist_t
*nvl
, const char *name
, uchar_t
*val
)
1675 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE
, NULL
, val
));
1679 nvlist_lookup_int8(const nvlist_t
*nvl
, const char *name
, int8_t *val
)
1681 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8
, NULL
, val
));
1685 nvlist_lookup_uint8(const nvlist_t
*nvl
, const char *name
, uint8_t *val
)
1687 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8
, NULL
, val
));
1691 nvlist_lookup_int16(const nvlist_t
*nvl
, const char *name
, int16_t *val
)
1693 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16
, NULL
, val
));
1697 nvlist_lookup_uint16(const nvlist_t
*nvl
, const char *name
, uint16_t *val
)
1699 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16
, NULL
, val
));
1703 nvlist_lookup_int32(const nvlist_t
*nvl
, const char *name
, int32_t *val
)
1705 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32
, NULL
, val
));
1709 nvlist_lookup_uint32(const nvlist_t
*nvl
, const char *name
, uint32_t *val
)
1711 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32
, NULL
, val
));
1715 nvlist_lookup_int64(const nvlist_t
*nvl
, const char *name
, int64_t *val
)
1717 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64
, NULL
, val
));
1721 nvlist_lookup_uint64(const nvlist_t
*nvl
, const char *name
, uint64_t *val
)
1723 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64
, NULL
, val
));
1726 #if !defined(_KERNEL)
1728 nvlist_lookup_double(const nvlist_t
*nvl
, const char *name
, double *val
)
1730 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_DOUBLE
, NULL
, val
));
1735 nvlist_lookup_string(const nvlist_t
*nvl
, const char *name
, const char **val
)
1737 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING
, NULL
, val
));
1741 nvlist_lookup_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
**val
)
1743 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST
, NULL
, val
));
1747 nvlist_lookup_boolean_array(nvlist_t
*nvl
, const char *name
,
1748 boolean_t
**a
, uint_t
*n
)
1750 return (nvlist_lookup_common(nvl
, name
,
1751 DATA_TYPE_BOOLEAN_ARRAY
, n
, a
));
1755 nvlist_lookup_byte_array(nvlist_t
*nvl
, const char *name
,
1756 uchar_t
**a
, uint_t
*n
)
1758 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_BYTE_ARRAY
, n
, a
));
1762 nvlist_lookup_int8_array(nvlist_t
*nvl
, const char *name
, int8_t **a
, uint_t
*n
)
1764 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT8_ARRAY
, n
, a
));
1768 nvlist_lookup_uint8_array(nvlist_t
*nvl
, const char *name
,
1769 uint8_t **a
, uint_t
*n
)
1771 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT8_ARRAY
, n
, a
));
1775 nvlist_lookup_int16_array(nvlist_t
*nvl
, const char *name
,
1776 int16_t **a
, uint_t
*n
)
1778 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT16_ARRAY
, n
, a
));
1782 nvlist_lookup_uint16_array(nvlist_t
*nvl
, const char *name
,
1783 uint16_t **a
, uint_t
*n
)
1785 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT16_ARRAY
, n
, a
));
1789 nvlist_lookup_int32_array(nvlist_t
*nvl
, const char *name
,
1790 int32_t **a
, uint_t
*n
)
1792 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT32_ARRAY
, n
, a
));
1796 nvlist_lookup_uint32_array(nvlist_t
*nvl
, const char *name
,
1797 uint32_t **a
, uint_t
*n
)
1799 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT32_ARRAY
, n
, a
));
1803 nvlist_lookup_int64_array(nvlist_t
*nvl
, const char *name
,
1804 int64_t **a
, uint_t
*n
)
1806 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_INT64_ARRAY
, n
, a
));
1810 nvlist_lookup_uint64_array(nvlist_t
*nvl
, const char *name
,
1811 uint64_t **a
, uint_t
*n
)
1813 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_UINT64_ARRAY
, n
, a
));
1817 nvlist_lookup_string_array(nvlist_t
*nvl
, const char *name
,
1818 char ***a
, uint_t
*n
)
1820 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_STRING_ARRAY
, n
, a
));
1824 nvlist_lookup_nvlist_array(nvlist_t
*nvl
, const char *name
,
1825 nvlist_t
***a
, uint_t
*n
)
1827 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_NVLIST_ARRAY
, n
, a
));
1831 nvlist_lookup_hrtime(nvlist_t
*nvl
, const char *name
, hrtime_t
*val
)
1833 return (nvlist_lookup_common(nvl
, name
, DATA_TYPE_HRTIME
, NULL
, val
));
1837 nvlist_lookup_pairs(nvlist_t
*nvl
, int flag
, ...)
1841 int noentok
= (flag
& NV_FLAG_NOENTOK
? 1 : 0);
1845 while (ret
== 0 && (name
= va_arg(ap
, char *)) != NULL
) {
1850 switch (type
= va_arg(ap
, data_type_t
)) {
1851 case DATA_TYPE_BOOLEAN
:
1852 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, NULL
);
1855 case DATA_TYPE_BOOLEAN_VALUE
:
1856 case DATA_TYPE_BYTE
:
1857 case DATA_TYPE_INT8
:
1858 case DATA_TYPE_UINT8
:
1859 case DATA_TYPE_INT16
:
1860 case DATA_TYPE_UINT16
:
1861 case DATA_TYPE_INT32
:
1862 case DATA_TYPE_UINT32
:
1863 case DATA_TYPE_INT64
:
1864 case DATA_TYPE_UINT64
:
1865 case DATA_TYPE_HRTIME
:
1866 case DATA_TYPE_STRING
:
1867 case DATA_TYPE_NVLIST
:
1868 #if !defined(_KERNEL)
1869 case DATA_TYPE_DOUBLE
:
1871 val
= va_arg(ap
, void *);
1872 ret
= nvlist_lookup_common(nvl
, name
, type
, NULL
, val
);
1875 case DATA_TYPE_BYTE_ARRAY
:
1876 case DATA_TYPE_BOOLEAN_ARRAY
:
1877 case DATA_TYPE_INT8_ARRAY
:
1878 case DATA_TYPE_UINT8_ARRAY
:
1879 case DATA_TYPE_INT16_ARRAY
:
1880 case DATA_TYPE_UINT16_ARRAY
:
1881 case DATA_TYPE_INT32_ARRAY
:
1882 case DATA_TYPE_UINT32_ARRAY
:
1883 case DATA_TYPE_INT64_ARRAY
:
1884 case DATA_TYPE_UINT64_ARRAY
:
1885 case DATA_TYPE_STRING_ARRAY
:
1886 case DATA_TYPE_NVLIST_ARRAY
:
1887 val
= va_arg(ap
, void *);
1888 nelem
= va_arg(ap
, uint_t
*);
1889 ret
= nvlist_lookup_common(nvl
, name
, type
, nelem
, val
);
1896 if (ret
== ENOENT
&& noentok
)
1905 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1906 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1907 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1908 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1909 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1910 * "a.d[3].e[1]". This matches the C syntax for array embed (for convenience,
1911 * code also supports "a.d[3]e[1]" syntax).
1913 * If 'ip' is non-NULL and the last name component is an array, return the
1914 * value of the "...[index]" array index in *ip. For an array reference that
1915 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1916 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1917 * inside the 'name' string where the syntax error was detected.
1920 nvlist_lookup_nvpair_ei_sep(nvlist_t
*nvl
, const char *name
, const char sep
,
1921 nvpair_t
**ret
, int *ip
, const char **ep
)
1932 *ip
= -1; /* not indexed */
1936 if ((nvl
== NULL
) || (name
== NULL
))
1941 /* step through components of name */
1942 for (np
= name
; np
&& *np
; np
= sepp
) {
1943 /* ensure unique names */
1944 if (!(nvl
->nvl_nvflag
& NV_UNIQUE_NAME
))
1947 /* skip white space */
1948 skip_whitespace(np
);
1952 /* set 'sepp' to end of current component 'np' */
1954 sepp
= strchr(np
, sep
);
1958 /* find start of next "[ index ]..." */
1959 idxp
= strchr(np
, '[');
1961 /* if sepp comes first, set idxp to NULL */
1962 if (sepp
&& idxp
&& (sepp
< idxp
))
1966 * At this point 'idxp' is set if there is an index
1967 * expected for the current component.
1970 /* set 'n' to length of current 'np' name component */
1973 /* keep sepp up to date for *ep use as we advance */
1974 skip_whitespace(idxp
);
1977 /* determine the index value */
1978 #if defined(_KERNEL)
1979 if (ddi_strtol(idxp
, &idxep
, 0, &idx
))
1982 idx
= strtol(idxp
, &idxep
, 0);
1987 /* keep sepp up to date for *ep use as we advance */
1990 /* skip white space index value and check for ']' */
1991 skip_whitespace(sepp
);
1995 /* for embedded arrays, support C syntax: "a[1].b" */
1996 skip_whitespace(sepp
);
1997 if (sep
&& (*sepp
== sep
))
2005 /* trim trailing whitespace by reducing length of 'np' */
2008 for (n
--; (np
[n
] == ' ') || (np
[n
] == '\t'); n
--)
2012 /* skip whitespace, and set sepp to NULL if complete */
2014 skip_whitespace(sepp
);
2021 * o 'n' is the length of current 'np' component.
2022 * o 'idxp' is set if there was an index, and value 'idx'.
2023 * o 'sepp' is set to the beginning of the next component,
2024 * and set to NULL if we have no more components.
2026 * Search for nvpair with matching component name.
2028 for (nvp
= nvlist_next_nvpair(nvl
, NULL
); nvp
!= NULL
;
2029 nvp
= nvlist_next_nvpair(nvl
, nvp
)) {
2031 /* continue if no match on name */
2032 if (strncmp(np
, nvpair_name(nvp
), n
) ||
2033 (strlen(nvpair_name(nvp
)) != n
))
2036 /* if indexed, verify type is array oriented */
2037 if (idxp
&& !nvpair_type_is_array(nvp
))
2041 * Full match found, return nvp and idx if this
2042 * was the last component.
2048 *ip
= (int)idx
; /* return index */
2049 return (0); /* found */
2053 * More components: current match must be
2054 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
2055 * to support going deeper.
2057 if (nvpair_type(nvp
) == DATA_TYPE_NVLIST
) {
2058 nvl
= EMBEDDED_NVL(nvp
);
2060 } else if (nvpair_type(nvp
) == DATA_TYPE_NVLIST_ARRAY
) {
2061 if (nvpair_value_nvlist_array(nvp
,
2062 &nva
, (uint_t
*)&n
) != 0)
2066 if ((n
< 0) || (idx
>= n
))
2072 /* type does not support more levels */
2076 goto fail
; /* 'name' not found */
2078 /* search for match of next component in embedded 'nvl' list */
2081 fail
: if (ep
&& sepp
)
2087 * Return pointer to nvpair with specified 'name'.
2090 nvlist_lookup_nvpair(nvlist_t
*nvl
, const char *name
, nvpair_t
**ret
)
2092 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, 0, ret
, NULL
, NULL
));
2096 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
2097 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
2100 int nvlist_lookup_nvpair_embedded_index(nvlist_t
*nvl
,
2101 const char *name
, nvpair_t
**ret
, int *ip
, const char **ep
)
2103 return (nvlist_lookup_nvpair_ei_sep(nvl
, name
, '.', ret
, ip
, ep
));
2107 nvlist_exists(const nvlist_t
*nvl
, const char *name
)
2113 if (name
== NULL
|| nvl
== NULL
||
2114 (priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2117 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2118 nvp
= &curr
->nvi_nvp
;
2120 if (strcmp(name
, NVP_NAME(nvp
)) == 0)
2128 nvpair_value_boolean_value(const nvpair_t
*nvp
, boolean_t
*val
)
2130 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_VALUE
, NULL
, val
));
2134 nvpair_value_byte(const nvpair_t
*nvp
, uchar_t
*val
)
2136 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE
, NULL
, val
));
2140 nvpair_value_int8(const nvpair_t
*nvp
, int8_t *val
)
2142 return (nvpair_value_common(nvp
, DATA_TYPE_INT8
, NULL
, val
));
2146 nvpair_value_uint8(const nvpair_t
*nvp
, uint8_t *val
)
2148 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8
, NULL
, val
));
2152 nvpair_value_int16(const nvpair_t
*nvp
, int16_t *val
)
2154 return (nvpair_value_common(nvp
, DATA_TYPE_INT16
, NULL
, val
));
2158 nvpair_value_uint16(const nvpair_t
*nvp
, uint16_t *val
)
2160 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16
, NULL
, val
));
2164 nvpair_value_int32(const nvpair_t
*nvp
, int32_t *val
)
2166 return (nvpair_value_common(nvp
, DATA_TYPE_INT32
, NULL
, val
));
2170 nvpair_value_uint32(const nvpair_t
*nvp
, uint32_t *val
)
2172 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32
, NULL
, val
));
2176 nvpair_value_int64(const nvpair_t
*nvp
, int64_t *val
)
2178 return (nvpair_value_common(nvp
, DATA_TYPE_INT64
, NULL
, val
));
2182 nvpair_value_uint64(const nvpair_t
*nvp
, uint64_t *val
)
2184 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64
, NULL
, val
));
2187 #if !defined(_KERNEL)
2189 nvpair_value_double(const nvpair_t
*nvp
, double *val
)
2191 return (nvpair_value_common(nvp
, DATA_TYPE_DOUBLE
, NULL
, val
));
2196 nvpair_value_string(const nvpair_t
*nvp
, const char **val
)
2198 return (nvpair_value_common(nvp
, DATA_TYPE_STRING
, NULL
, val
));
2202 nvpair_value_nvlist(nvpair_t
*nvp
, nvlist_t
**val
)
2204 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST
, NULL
, val
));
2208 nvpair_value_boolean_array(nvpair_t
*nvp
, boolean_t
**val
, uint_t
*nelem
)
2210 return (nvpair_value_common(nvp
, DATA_TYPE_BOOLEAN_ARRAY
, nelem
, val
));
2214 nvpair_value_byte_array(nvpair_t
*nvp
, uchar_t
**val
, uint_t
*nelem
)
2216 return (nvpair_value_common(nvp
, DATA_TYPE_BYTE_ARRAY
, nelem
, val
));
2220 nvpair_value_int8_array(nvpair_t
*nvp
, int8_t **val
, uint_t
*nelem
)
2222 return (nvpair_value_common(nvp
, DATA_TYPE_INT8_ARRAY
, nelem
, val
));
2226 nvpair_value_uint8_array(nvpair_t
*nvp
, uint8_t **val
, uint_t
*nelem
)
2228 return (nvpair_value_common(nvp
, DATA_TYPE_UINT8_ARRAY
, nelem
, val
));
2232 nvpair_value_int16_array(nvpair_t
*nvp
, int16_t **val
, uint_t
*nelem
)
2234 return (nvpair_value_common(nvp
, DATA_TYPE_INT16_ARRAY
, nelem
, val
));
2238 nvpair_value_uint16_array(nvpair_t
*nvp
, uint16_t **val
, uint_t
*nelem
)
2240 return (nvpair_value_common(nvp
, DATA_TYPE_UINT16_ARRAY
, nelem
, val
));
2244 nvpair_value_int32_array(nvpair_t
*nvp
, int32_t **val
, uint_t
*nelem
)
2246 return (nvpair_value_common(nvp
, DATA_TYPE_INT32_ARRAY
, nelem
, val
));
2250 nvpair_value_uint32_array(nvpair_t
*nvp
, uint32_t **val
, uint_t
*nelem
)
2252 return (nvpair_value_common(nvp
, DATA_TYPE_UINT32_ARRAY
, nelem
, val
));
2256 nvpair_value_int64_array(nvpair_t
*nvp
, int64_t **val
, uint_t
*nelem
)
2258 return (nvpair_value_common(nvp
, DATA_TYPE_INT64_ARRAY
, nelem
, val
));
2262 nvpair_value_uint64_array(nvpair_t
*nvp
, uint64_t **val
, uint_t
*nelem
)
2264 return (nvpair_value_common(nvp
, DATA_TYPE_UINT64_ARRAY
, nelem
, val
));
2268 nvpair_value_string_array(nvpair_t
*nvp
, const char ***val
, uint_t
*nelem
)
2270 return (nvpair_value_common(nvp
, DATA_TYPE_STRING_ARRAY
, nelem
, val
));
2274 nvpair_value_nvlist_array(nvpair_t
*nvp
, nvlist_t
***val
, uint_t
*nelem
)
2276 return (nvpair_value_common(nvp
, DATA_TYPE_NVLIST_ARRAY
, nelem
, val
));
2280 nvpair_value_hrtime(nvpair_t
*nvp
, hrtime_t
*val
)
2282 return (nvpair_value_common(nvp
, DATA_TYPE_HRTIME
, NULL
, val
));
2286 * Add specified pair to the list.
2289 nvlist_add_nvpair(nvlist_t
*nvl
, nvpair_t
*nvp
)
2291 if (nvl
== NULL
|| nvp
== NULL
)
2294 return (nvlist_add_common(nvl
, NVP_NAME(nvp
), NVP_TYPE(nvp
),
2295 NVP_NELEM(nvp
), NVP_VALUE(nvp
)));
2299 * Merge the supplied nvlists and put the result in dst.
2300 * The merged list will contain all names specified in both lists,
2301 * the values are taken from nvl in the case of duplicates.
2302 * Return 0 on success.
2305 nvlist_merge(nvlist_t
*dst
, nvlist_t
*nvl
, int flag
)
2309 if (nvl
== NULL
|| dst
== NULL
)
2313 return (nvlist_copy_pairs(nvl
, dst
));
2319 * Encoding related routines
2321 #define NVS_OP_ENCODE 0
2322 #define NVS_OP_DECODE 1
2323 #define NVS_OP_GETSIZE 2
2325 typedef struct nvs_ops nvs_ops_t
;
2329 const nvs_ops_t
*nvs_ops
;
2336 * nvs operations are:
2338 * encoding / decoding of an nvlist header (nvlist_t)
2339 * calculates the size used for header and end detection
2342 * responsible for the first part of encoding / decoding of an nvpair
2343 * calculates the decoded size of an nvpair
2346 * second part of encoding / decoding of an nvpair
2349 * calculates the encoding size of an nvpair
2352 * encodes the end detection mark (zeros).
2355 int (*nvs_nvlist
)(nvstream_t
*, nvlist_t
*, size_t *);
2356 int (*nvs_nvpair
)(nvstream_t
*, nvpair_t
*, size_t *);
2357 int (*nvs_nvp_op
)(nvstream_t
*, nvpair_t
*);
2358 int (*nvs_nvp_size
)(nvstream_t
*, nvpair_t
*, size_t *);
2359 int (*nvs_nvl_fini
)(nvstream_t
*);
2363 char nvh_encoding
; /* nvs encoding method */
2364 char nvh_endian
; /* nvs endian */
2365 char nvh_reserved1
; /* reserved for future use */
2366 char nvh_reserved2
; /* reserved for future use */
2370 nvs_encode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2372 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2376 * Walk nvpair in list and encode each nvpair
2378 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
)
2379 if (nvs
->nvs_ops
->nvs_nvpair(nvs
, &curr
->nvi_nvp
, NULL
) != 0)
2382 return (nvs
->nvs_ops
->nvs_nvl_fini(nvs
));
2386 nvs_decode_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
)
2393 * Get decoded size of next pair in stream, alloc
2394 * memory for nvpair_t, then decode the nvpair
2396 while ((err
= nvs
->nvs_ops
->nvs_nvpair(nvs
, NULL
, &nvsize
)) == 0) {
2397 if (nvsize
== 0) /* end of list */
2400 /* make sure len makes sense */
2401 if (nvsize
< NVP_SIZE_CALC(1, 0))
2404 if ((nvp
= nvp_buf_alloc(nvl
, nvsize
)) == NULL
)
2407 if ((err
= nvs
->nvs_ops
->nvs_nvp_op(nvs
, nvp
)) != 0) {
2408 nvp_buf_free(nvl
, nvp
);
2412 if (i_validate_nvpair(nvp
) != 0) {
2414 nvp_buf_free(nvl
, nvp
);
2418 err
= nvt_add_nvpair(nvl
, nvp
);
2421 nvp_buf_free(nvl
, nvp
);
2424 nvp_buf_link(nvl
, nvp
);
2430 nvs_getsize_pairs(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2432 nvpriv_t
*priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
;
2434 uint64_t nvsize
= *buflen
;
2438 * Get encoded size of nvpairs in nvlist
2440 for (curr
= priv
->nvp_list
; curr
!= NULL
; curr
= curr
->nvi_next
) {
2441 if (nvs
->nvs_ops
->nvs_nvp_size(nvs
, &curr
->nvi_nvp
, &size
) != 0)
2444 if ((nvsize
+= size
) > INT32_MAX
)
2453 nvs_operation(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *buflen
)
2457 if (nvl
->nvl_priv
== 0)
2461 * Perform the operation, starting with header, then each nvpair
2463 if ((err
= nvs
->nvs_ops
->nvs_nvlist(nvs
, nvl
, buflen
)) != 0)
2466 switch (nvs
->nvs_op
) {
2468 err
= nvs_encode_pairs(nvs
, nvl
);
2472 err
= nvs_decode_pairs(nvs
, nvl
);
2475 case NVS_OP_GETSIZE
:
2476 err
= nvs_getsize_pairs(nvs
, nvl
, buflen
);
2487 nvs_embedded(nvstream_t
*nvs
, nvlist_t
*embedded
)
2489 switch (nvs
->nvs_op
) {
2490 case NVS_OP_ENCODE
: {
2493 if (nvs
->nvs_recursion
>= nvpair_max_recursion
)
2495 nvs
->nvs_recursion
++;
2496 err
= nvs_operation(nvs
, embedded
, NULL
);
2497 nvs
->nvs_recursion
--;
2500 case NVS_OP_DECODE
: {
2504 if (embedded
->nvl_version
!= NV_VERSION
)
2507 if ((priv
= nv_priv_alloc_embedded(nvs
->nvs_priv
)) == NULL
)
2510 nvlist_init(embedded
, embedded
->nvl_nvflag
, priv
);
2512 if (nvs
->nvs_recursion
>= nvpair_max_recursion
) {
2513 nvlist_free(embedded
);
2516 nvs
->nvs_recursion
++;
2517 if ((err
= nvs_operation(nvs
, embedded
, NULL
)) != 0)
2518 nvlist_free(embedded
);
2519 nvs
->nvs_recursion
--;
2530 nvs_embedded_nvl_array(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
2532 size_t nelem
= NVP_NELEM(nvp
);
2533 nvlist_t
**nvlp
= EMBEDDED_NVL_ARRAY(nvp
);
2536 switch (nvs
->nvs_op
) {
2538 for (i
= 0; i
< nelem
; i
++)
2539 if (nvs_embedded(nvs
, nvlp
[i
]) != 0)
2543 case NVS_OP_DECODE
: {
2544 size_t len
= nelem
* sizeof (uint64_t);
2545 nvlist_t
*embedded
= (nvlist_t
*)((uintptr_t)nvlp
+ len
);
2547 memset(nvlp
, 0, len
); /* don't trust packed data */
2548 for (i
= 0; i
< nelem
; i
++) {
2549 if (nvs_embedded(nvs
, embedded
) != 0) {
2554 nvlp
[i
] = embedded
++;
2558 case NVS_OP_GETSIZE
: {
2559 uint64_t nvsize
= 0;
2561 for (i
= 0; i
< nelem
; i
++) {
2564 if (nvs_operation(nvs
, nvlp
[i
], &nvp_sz
) != 0)
2567 if ((nvsize
+= nvp_sz
) > INT32_MAX
)
2581 static int nvs_native(nvstream_t
*, nvlist_t
*, char *, size_t *);
2582 static int nvs_xdr(nvstream_t
*, nvlist_t
*, char *, size_t *);
2585 * Common routine for nvlist operations:
2586 * encode, decode, getsize (encoded size).
2589 nvlist_common(nvlist_t
*nvl
, char *buf
, size_t *buflen
, int encoding
,
2595 #if defined(_ZFS_LITTLE_ENDIAN)
2596 int host_endian
= 1;
2597 #elif defined(_ZFS_BIG_ENDIAN)
2598 int host_endian
= 0;
2600 #error "No endian defined!"
2601 #endif /* _ZFS_LITTLE_ENDIAN */
2604 if (buflen
== NULL
|| nvl
== NULL
||
2605 (nvs
.nvs_priv
= (nvpriv_t
*)(uintptr_t)nvl
->nvl_priv
) == NULL
)
2608 nvs
.nvs_op
= nvs_op
;
2609 nvs
.nvs_recursion
= 0;
2612 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2613 * a buffer is allocated. The first 4 bytes in the buffer are
2614 * used for encoding method and host endian.
2618 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2622 nvh
->nvh_encoding
= encoding
;
2623 nvh
->nvh_endian
= nvl_endian
= host_endian
;
2624 nvh
->nvh_reserved1
= 0;
2625 nvh
->nvh_reserved2
= 0;
2629 if (buf
== NULL
|| *buflen
< sizeof (nvs_header_t
))
2632 /* get method of encoding from first byte */
2634 encoding
= nvh
->nvh_encoding
;
2635 nvl_endian
= nvh
->nvh_endian
;
2638 case NVS_OP_GETSIZE
:
2639 nvl_endian
= host_endian
;
2642 * add the size for encoding
2644 *buflen
= sizeof (nvs_header_t
);
2652 * Create an nvstream with proper encoding method
2655 case NV_ENCODE_NATIVE
:
2657 * check endianness, in case we are unpacking
2660 if (nvl_endian
!= host_endian
)
2662 err
= nvs_native(&nvs
, nvl
, buf
, buflen
);
2665 err
= nvs_xdr(&nvs
, nvl
, buf
, buflen
);
2676 nvlist_size(nvlist_t
*nvl
, size_t *size
, int encoding
)
2678 return (nvlist_common(nvl
, NULL
, size
, encoding
, NVS_OP_GETSIZE
));
2682 * Pack nvlist into contiguous memory
2685 nvlist_pack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2688 return (nvlist_xpack(nvl
, bufp
, buflen
, encoding
,
2689 nvlist_nv_alloc(kmflag
)));
2693 nvlist_xpack(nvlist_t
*nvl
, char **bufp
, size_t *buflen
, int encoding
,
2701 if (nva
== NULL
|| nvl
== NULL
|| bufp
== NULL
|| buflen
== NULL
)
2705 return (nvlist_common(nvl
, *bufp
, buflen
, encoding
,
2709 * Here is a difficult situation:
2710 * 1. The nvlist has fixed allocator properties.
2711 * All other nvlist routines (like nvlist_add_*, ...) use
2713 * 2. When using nvlist_pack() the user can specify their own
2714 * allocator properties (e.g. by using KM_NOSLEEP).
2716 * We use the user specified properties (2). A clearer solution
2717 * will be to remove the kmflag from nvlist_pack(), but we will
2718 * not change the interface.
2720 nv_priv_init(&nvpriv
, nva
, 0);
2722 if ((err
= nvlist_size(nvl
, &alloc_size
, encoding
)))
2725 if ((buf
= nv_mem_zalloc(&nvpriv
, alloc_size
)) == NULL
)
2728 if ((err
= nvlist_common(nvl
, buf
, &alloc_size
, encoding
,
2729 NVS_OP_ENCODE
)) != 0) {
2730 nv_mem_free(&nvpriv
, buf
, alloc_size
);
2732 *buflen
= alloc_size
;
2740 * Unpack buf into an nvlist_t
2743 nvlist_unpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, int kmflag
)
2745 return (nvlist_xunpack(buf
, buflen
, nvlp
, nvlist_nv_alloc(kmflag
)));
2749 nvlist_xunpack(char *buf
, size_t buflen
, nvlist_t
**nvlp
, nv_alloc_t
*nva
)
2757 if ((err
= nvlist_xalloc(&nvl
, 0, nva
)) != 0)
2760 if ((err
= nvlist_common(nvl
, buf
, &buflen
, NV_ENCODE_NATIVE
,
2761 NVS_OP_DECODE
)) != 0)
2770 * Native encoding functions
2774 * This structure is used when decoding a packed nvpair in
2775 * the native format. n_base points to a buffer containing the
2776 * packed nvpair. n_end is a pointer to the end of the buffer.
2777 * (n_end actually points to the first byte past the end of the
2778 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2779 * It points to the current data that we are decoding.
2780 * The amount of data left in the buffer is equal to n_end - n_curr.
2781 * n_flag is used to recognize a packed embedded list.
2790 nvs_native_create(nvstream_t
*nvs
, nvs_native_t
*native
, char *buf
,
2793 switch (nvs
->nvs_op
) {
2796 nvs
->nvs_private
= native
;
2797 native
->n_curr
= native
->n_base
= buf
;
2798 native
->n_end
= buf
+ buflen
;
2802 case NVS_OP_GETSIZE
:
2803 nvs
->nvs_private
= native
;
2804 native
->n_curr
= native
->n_base
= native
->n_end
= NULL
;
2813 nvs_native_destroy(nvstream_t
*nvs
)
2815 nvs
->nvs_private
= NULL
;
2819 native_cp(nvstream_t
*nvs
, void *buf
, size_t size
)
2821 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2823 if (native
->n_curr
+ size
> native
->n_end
)
2827 * The memcpy() below eliminates alignment requirement
2828 * on the buffer (stream) and is preferred over direct access.
2830 switch (nvs
->nvs_op
) {
2832 memcpy(native
->n_curr
, buf
, size
);
2835 memcpy(buf
, native
->n_curr
, size
);
2841 native
->n_curr
+= size
;
2846 * operate on nvlist_t header
2849 nvs_native_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
2851 nvs_native_t
*native
= nvs
->nvs_private
;
2853 switch (nvs
->nvs_op
) {
2857 return (0); /* packed embedded list */
2861 /* copy version and nvflag of the nvlist_t */
2862 if (native_cp(nvs
, &nvl
->nvl_version
, sizeof (int32_t)) != 0 ||
2863 native_cp(nvs
, &nvl
->nvl_nvflag
, sizeof (int32_t)) != 0)
2868 case NVS_OP_GETSIZE
:
2870 * if calculate for packed embedded list
2871 * 4 for end of the embedded list
2873 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2874 * and 4 for end of the entire list
2876 if (native
->n_flag
) {
2880 *size
+= 2 * sizeof (int32_t) + 4;
2891 nvs_native_nvl_fini(nvstream_t
*nvs
)
2893 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2894 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2896 * Add 4 zero bytes at end of nvlist. They are used
2897 * for end detection by the decode routine.
2899 if (native
->n_curr
+ sizeof (int) > native
->n_end
)
2902 memset(native
->n_curr
, 0, sizeof (int));
2903 native
->n_curr
+= sizeof (int);
2910 nvpair_native_embedded(nvstream_t
*nvs
, nvpair_t
*nvp
)
2912 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2913 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2914 nvlist_t
*packed
= (void *)
2915 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2917 * Null out the pointer that is meaningless in the packed
2918 * structure. The address may not be aligned, so we have
2921 memset((char *)packed
+ offsetof(nvlist_t
, nvl_priv
),
2922 0, sizeof (uint64_t));
2925 return (nvs_embedded(nvs
, EMBEDDED_NVL(nvp
)));
2929 nvpair_native_embedded_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2931 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
2932 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2933 char *value
= native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
);
2934 size_t len
= NVP_NELEM(nvp
) * sizeof (uint64_t);
2935 nvlist_t
*packed
= (nvlist_t
*)((uintptr_t)value
+ len
);
2938 * Null out pointers that are meaningless in the packed
2939 * structure. The addresses may not be aligned, so we have
2942 memset(value
, 0, len
);
2944 for (i
= 0; i
< NVP_NELEM(nvp
); i
++, packed
++)
2946 * Null out the pointer that is meaningless in the
2947 * packed structure. The address may not be aligned,
2948 * so we have to use memset.
2950 memset((char *)packed
+ offsetof(nvlist_t
, nvl_priv
),
2951 0, sizeof (uint64_t));
2954 return (nvs_embedded_nvl_array(nvs
, nvp
, NULL
));
2958 nvpair_native_string_array(nvstream_t
*nvs
, nvpair_t
*nvp
)
2960 switch (nvs
->nvs_op
) {
2961 case NVS_OP_ENCODE
: {
2962 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
2963 uint64_t *strp
= (void *)
2964 (native
->n_curr
- nvp
->nvp_size
+ NVP_VALOFF(nvp
));
2966 * Null out pointers that are meaningless in the packed
2967 * structure. The addresses may not be aligned, so we have
2970 memset(strp
, 0, NVP_NELEM(nvp
) * sizeof (uint64_t));
2973 case NVS_OP_DECODE
: {
2974 char **strp
= (void *)NVP_VALUE(nvp
);
2975 char *buf
= ((char *)strp
+ NVP_NELEM(nvp
) * sizeof (uint64_t));
2978 for (i
= 0; i
< NVP_NELEM(nvp
); i
++) {
2980 buf
+= strlen(buf
) + 1;
2988 nvs_native_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
2995 * We do the initial memcpy of the data before we look at
2996 * the nvpair type, because when we're decoding, we won't
2997 * have the correct values for the pair until we do the memcpy.
2999 switch (nvs
->nvs_op
) {
3002 if (native_cp(nvs
, nvp
, nvp
->nvp_size
) != 0)
3009 /* verify nvp_name_sz, check the name string length */
3010 if (i_validate_nvpair_name(nvp
) != 0)
3013 type
= NVP_TYPE(nvp
);
3016 * Verify type and nelem and get the value size.
3017 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3018 * is the size of the string(s) excluded.
3020 if ((value_sz
= i_get_value_size(type
, NULL
, NVP_NELEM(nvp
))) < 0)
3023 if (NVP_SIZE_CALC(nvp
->nvp_name_sz
, value_sz
) > nvp
->nvp_size
)
3027 case DATA_TYPE_NVLIST
:
3028 ret
= nvpair_native_embedded(nvs
, nvp
);
3030 case DATA_TYPE_NVLIST_ARRAY
:
3031 ret
= nvpair_native_embedded_array(nvs
, nvp
);
3033 case DATA_TYPE_STRING_ARRAY
:
3034 nvpair_native_string_array(nvs
, nvp
);
3044 nvs_native_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3046 uint64_t nvp_sz
= nvp
->nvp_size
;
3048 switch (NVP_TYPE(nvp
)) {
3049 case DATA_TYPE_NVLIST
: {
3052 if (nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
) != 0)
3058 case DATA_TYPE_NVLIST_ARRAY
: {
3061 if (nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
) != 0)
3071 if (nvp_sz
> INT32_MAX
)
3080 nvs_native_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3082 switch (nvs
->nvs_op
) {
3084 return (nvs_native_nvp_op(nvs
, nvp
));
3086 case NVS_OP_DECODE
: {
3087 nvs_native_t
*native
= (nvs_native_t
*)nvs
->nvs_private
;
3090 /* try to read the size value from the stream */
3091 if (native
->n_curr
+ sizeof (int32_t) > native
->n_end
)
3093 memcpy(&decode_len
, native
->n_curr
, sizeof (int32_t));
3095 /* sanity check the size value */
3096 if (decode_len
< 0 ||
3097 decode_len
> native
->n_end
- native
->n_curr
)
3103 * If at the end of the stream then move the cursor
3104 * forward, otherwise nvpair_native_op() will read
3105 * the entire nvpair at the same cursor position.
3108 native
->n_curr
+= sizeof (int32_t);
3119 static const nvs_ops_t nvs_native_ops
= {
3120 .nvs_nvlist
= nvs_native_nvlist
,
3121 .nvs_nvpair
= nvs_native_nvpair
,
3122 .nvs_nvp_op
= nvs_native_nvp_op
,
3123 .nvs_nvp_size
= nvs_native_nvp_size
,
3124 .nvs_nvl_fini
= nvs_native_nvl_fini
3128 nvs_native(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3130 nvs_native_t native
;
3133 nvs
->nvs_ops
= &nvs_native_ops
;
3135 if ((err
= nvs_native_create(nvs
, &native
, buf
+ sizeof (nvs_header_t
),
3136 *buflen
- sizeof (nvs_header_t
))) != 0)
3139 err
= nvs_operation(nvs
, nvl
, buflen
);
3141 nvs_native_destroy(nvs
);
3147 * XDR encoding functions
3149 * An xdr packed nvlist is encoded as:
3151 * - encoding method and host endian (4 bytes)
3152 * - nvl_version (4 bytes)
3153 * - nvl_nvflag (4 bytes)
3155 * - encoded nvpairs, the format of one xdr encoded nvpair is:
3156 * - encoded size of the nvpair (4 bytes)
3157 * - decoded size of the nvpair (4 bytes)
3158 * - name string, (4 + sizeof(NV_ALIGN4(string))
3159 * a string is coded as size (4 bytes) and data
3160 * - data type (4 bytes)
3161 * - number of elements in the nvpair (4 bytes)
3164 * - 2 zero's for end of the entire list (8 bytes)
3167 nvs_xdr_create(nvstream_t
*nvs
, XDR
*xdr
, char *buf
, size_t buflen
)
3169 /* xdr data must be 4 byte aligned */
3170 if ((ulong_t
)buf
% 4 != 0)
3173 switch (nvs
->nvs_op
) {
3175 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_ENCODE
);
3176 nvs
->nvs_private
= xdr
;
3179 xdrmem_create(xdr
, buf
, (uint_t
)buflen
, XDR_DECODE
);
3180 nvs
->nvs_private
= xdr
;
3182 case NVS_OP_GETSIZE
:
3183 nvs
->nvs_private
= NULL
;
3191 nvs_xdr_destroy(nvstream_t
*nvs
)
3193 switch (nvs
->nvs_op
) {
3196 nvs
->nvs_private
= NULL
;
3204 nvs_xdr_nvlist(nvstream_t
*nvs
, nvlist_t
*nvl
, size_t *size
)
3206 switch (nvs
->nvs_op
) {
3208 case NVS_OP_DECODE
: {
3209 XDR
*xdr
= nvs
->nvs_private
;
3211 if (!xdr_int(xdr
, &nvl
->nvl_version
) ||
3212 !xdr_u_int(xdr
, &nvl
->nvl_nvflag
))
3216 case NVS_OP_GETSIZE
: {
3218 * 2 * 4 for nvl_version + nvl_nvflag
3219 * and 8 for end of the entire list
3231 nvs_xdr_nvl_fini(nvstream_t
*nvs
)
3233 if (nvs
->nvs_op
== NVS_OP_ENCODE
) {
3234 XDR
*xdr
= nvs
->nvs_private
;
3237 if (!xdr_int(xdr
, &zero
) || !xdr_int(xdr
, &zero
))
3245 * xdrproc_t-compatible callbacks for xdr_array()
3248 #if defined(_KERNEL) && defined(__linux__) /* Linux kernel */
3250 #define NVS_BUILD_XDRPROC_T(type) \
3252 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr) \
3254 return (xdr_##type(xdrs, ptr)); \
3257 #elif !defined(_KERNEL) && defined(XDR_CONTROL) /* tirpc */
3259 #define NVS_BUILD_XDRPROC_T(type) \
3261 nvs_xdr_nvp_##type(XDR *xdrs, ...) \
3266 va_start(args, xdrs); \
3267 ptr = va_arg(args, void *); \
3270 return (xdr_##type(xdrs, ptr)); \
3273 #else /* FreeBSD, sunrpc */
3275 #define NVS_BUILD_XDRPROC_T(type) \
3277 nvs_xdr_nvp_##type(XDR *xdrs, void *ptr, ...) \
3279 return (xdr_##type(xdrs, ptr)); \
3285 NVS_BUILD_XDRPROC_T(char);
3286 NVS_BUILD_XDRPROC_T(short);
3287 NVS_BUILD_XDRPROC_T(u_short
);
3288 NVS_BUILD_XDRPROC_T(int);
3289 NVS_BUILD_XDRPROC_T(u_int
);
3290 NVS_BUILD_XDRPROC_T(longlong_t
);
3291 NVS_BUILD_XDRPROC_T(u_longlong_t
);
3295 * The format of xdr encoded nvpair is:
3296 * encode_size, decode_size, name string, data type, nelem, data
3299 nvs_xdr_nvp_op(nvstream_t
*nvs
, nvpair_t
*nvp
)
3301 ASSERT(nvs
!= NULL
&& nvp
!= NULL
);
3305 char *buf_end
= (char *)nvp
+ nvp
->nvp_size
;
3307 uint_t nelem
, buflen
;
3309 XDR
*xdr
= nvs
->nvs_private
;
3311 ASSERT(xdr
!= NULL
);
3314 if ((buf
= NVP_NAME(nvp
)) >= buf_end
)
3316 buflen
= buf_end
- buf
;
3318 if (!xdr_string(xdr
, &buf
, buflen
- 1))
3320 nvp
->nvp_name_sz
= strlen(buf
) + 1;
3322 /* type and nelem */
3323 if (!xdr_int(xdr
, (int *)&nvp
->nvp_type
) ||
3324 !xdr_int(xdr
, &nvp
->nvp_value_elem
))
3327 type
= NVP_TYPE(nvp
);
3328 nelem
= nvp
->nvp_value_elem
;
3331 * Verify type and nelem and get the value size.
3332 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
3333 * is the size of the string(s) excluded.
3335 if ((value_sz
= i_get_value_size(type
, NULL
, nelem
)) < 0)
3338 /* if there is no data to extract then return */
3343 if ((buf
= NVP_VALUE(nvp
)) >= buf_end
)
3345 buflen
= buf_end
- buf
;
3347 if (buflen
< value_sz
)
3351 case DATA_TYPE_NVLIST
:
3352 if (nvs_embedded(nvs
, (void *)buf
) == 0)
3356 case DATA_TYPE_NVLIST_ARRAY
:
3357 if (nvs_embedded_nvl_array(nvs
, nvp
, NULL
) == 0)
3361 case DATA_TYPE_BOOLEAN
:
3365 case DATA_TYPE_BYTE
:
3366 case DATA_TYPE_INT8
:
3367 case DATA_TYPE_UINT8
:
3368 ret
= xdr_char(xdr
, buf
);
3371 case DATA_TYPE_INT16
:
3372 ret
= xdr_short(xdr
, (void *)buf
);
3375 case DATA_TYPE_UINT16
:
3376 ret
= xdr_u_short(xdr
, (void *)buf
);
3379 case DATA_TYPE_BOOLEAN_VALUE
:
3380 case DATA_TYPE_INT32
:
3381 ret
= xdr_int(xdr
, (void *)buf
);
3384 case DATA_TYPE_UINT32
:
3385 ret
= xdr_u_int(xdr
, (void *)buf
);
3388 case DATA_TYPE_INT64
:
3389 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3392 case DATA_TYPE_UINT64
:
3393 ret
= xdr_u_longlong_t(xdr
, (void *)buf
);
3396 case DATA_TYPE_HRTIME
:
3398 * NOTE: must expose the definition of hrtime_t here
3400 ret
= xdr_longlong_t(xdr
, (void *)buf
);
3402 #if !defined(_KERNEL)
3403 case DATA_TYPE_DOUBLE
:
3404 ret
= xdr_double(xdr
, (void *)buf
);
3407 case DATA_TYPE_STRING
:
3408 ret
= xdr_string(xdr
, &buf
, buflen
- 1);
3411 case DATA_TYPE_BYTE_ARRAY
:
3412 ret
= xdr_opaque(xdr
, buf
, nelem
);
3415 case DATA_TYPE_INT8_ARRAY
:
3416 case DATA_TYPE_UINT8_ARRAY
:
3417 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
, sizeof (int8_t),
3421 case DATA_TYPE_INT16_ARRAY
:
3422 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int16_t),
3423 sizeof (int16_t), nvs_xdr_nvp_short
);
3426 case DATA_TYPE_UINT16_ARRAY
:
3427 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint16_t),
3428 sizeof (uint16_t), nvs_xdr_nvp_u_short
);
3431 case DATA_TYPE_BOOLEAN_ARRAY
:
3432 case DATA_TYPE_INT32_ARRAY
:
3433 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int32_t),
3434 sizeof (int32_t), nvs_xdr_nvp_int
);
3437 case DATA_TYPE_UINT32_ARRAY
:
3438 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint32_t),
3439 sizeof (uint32_t), nvs_xdr_nvp_u_int
);
3442 case DATA_TYPE_INT64_ARRAY
:
3443 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (int64_t),
3444 sizeof (int64_t), nvs_xdr_nvp_longlong_t
);
3447 case DATA_TYPE_UINT64_ARRAY
:
3448 ret
= xdr_array(xdr
, &buf
, &nelem
, buflen
/ sizeof (uint64_t),
3449 sizeof (uint64_t), nvs_xdr_nvp_u_longlong_t
);
3452 case DATA_TYPE_STRING_ARRAY
: {
3453 size_t len
= nelem
* sizeof (uint64_t);
3454 char **strp
= (void *)buf
;
3457 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3458 memset(buf
, 0, len
); /* don't trust packed data */
3460 for (i
= 0; i
< nelem
; i
++) {
3467 if (xdr_string(xdr
, &buf
, buflen
- 1) != TRUE
)
3470 if (nvs
->nvs_op
== NVS_OP_DECODE
)
3472 len
= strlen(buf
) + 1;
3481 return (ret
== TRUE
? 0 : EFAULT
);
3485 nvs_xdr_nvp_size(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3487 data_type_t type
= NVP_TYPE(nvp
);
3489 * encode_size + decode_size + name string size + data type + nelem
3490 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3492 uint64_t nvp_sz
= 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp
))) + 4 + 4;
3495 case DATA_TYPE_BOOLEAN
:
3498 case DATA_TYPE_BOOLEAN_VALUE
:
3499 case DATA_TYPE_BYTE
:
3500 case DATA_TYPE_INT8
:
3501 case DATA_TYPE_UINT8
:
3502 case DATA_TYPE_INT16
:
3503 case DATA_TYPE_UINT16
:
3504 case DATA_TYPE_INT32
:
3505 case DATA_TYPE_UINT32
:
3506 nvp_sz
+= 4; /* 4 is the minimum xdr unit */
3509 case DATA_TYPE_INT64
:
3510 case DATA_TYPE_UINT64
:
3511 case DATA_TYPE_HRTIME
:
3512 #if !defined(_KERNEL)
3513 case DATA_TYPE_DOUBLE
:
3518 case DATA_TYPE_STRING
:
3519 nvp_sz
+= 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp
)));
3522 case DATA_TYPE_BYTE_ARRAY
:
3523 nvp_sz
+= NV_ALIGN4(NVP_NELEM(nvp
));
3526 case DATA_TYPE_BOOLEAN_ARRAY
:
3527 case DATA_TYPE_INT8_ARRAY
:
3528 case DATA_TYPE_UINT8_ARRAY
:
3529 case DATA_TYPE_INT16_ARRAY
:
3530 case DATA_TYPE_UINT16_ARRAY
:
3531 case DATA_TYPE_INT32_ARRAY
:
3532 case DATA_TYPE_UINT32_ARRAY
:
3533 nvp_sz
+= 4 + 4 * (uint64_t)NVP_NELEM(nvp
);
3536 case DATA_TYPE_INT64_ARRAY
:
3537 case DATA_TYPE_UINT64_ARRAY
:
3538 nvp_sz
+= 4 + 8 * (uint64_t)NVP_NELEM(nvp
);
3541 case DATA_TYPE_STRING_ARRAY
: {
3543 char **strs
= (void *)NVP_VALUE(nvp
);
3545 for (i
= 0; i
< NVP_NELEM(nvp
); i
++)
3546 nvp_sz
+= 4 + NV_ALIGN4(strlen(strs
[i
]));
3551 case DATA_TYPE_NVLIST
:
3552 case DATA_TYPE_NVLIST_ARRAY
: {
3554 int old_nvs_op
= nvs
->nvs_op
;
3557 nvs
->nvs_op
= NVS_OP_GETSIZE
;
3558 if (type
== DATA_TYPE_NVLIST
)
3559 err
= nvs_operation(nvs
, EMBEDDED_NVL(nvp
), &nvsize
);
3561 err
= nvs_embedded_nvl_array(nvs
, nvp
, &nvsize
);
3562 nvs
->nvs_op
= old_nvs_op
;
3575 if (nvp_sz
> INT32_MAX
)
3585 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3586 * the largest nvpair that could be encoded in the buffer.
3588 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3589 * The size of a xdr packed nvpair without any data is 5 words.
3591 * Using the size of the data directly as an estimate would be ok
3592 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3593 * then the actual nvpair has space for an array of pointers to index
3594 * the strings. These pointers are not encoded into the packed xdr buffer.
3596 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3597 * of length 0, then each string is encoded in xdr format as a single word.
3598 * Therefore when expanded to an nvpair there will be 2.25 word used for
3599 * each string. (a int64_t allocated for pointer usage, and a single char
3600 * for the null termination.)
3602 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3604 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3605 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3606 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3607 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3608 (NVS_XDR_DATA_LEN(x) * 2) + \
3609 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3612 nvs_xdr_nvpair(nvstream_t
*nvs
, nvpair_t
*nvp
, size_t *size
)
3614 XDR
*xdr
= nvs
->nvs_private
;
3615 int32_t encode_len
, decode_len
;
3617 switch (nvs
->nvs_op
) {
3618 case NVS_OP_ENCODE
: {
3621 if (nvs_xdr_nvp_size(nvs
, nvp
, &nvsize
) != 0)
3624 decode_len
= nvp
->nvp_size
;
3625 encode_len
= nvsize
;
3626 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3629 return (nvs_xdr_nvp_op(nvs
, nvp
));
3631 case NVS_OP_DECODE
: {
3632 struct xdr_bytesrec bytesrec
;
3634 /* get the encode and decode size */
3635 if (!xdr_int(xdr
, &encode_len
) || !xdr_int(xdr
, &decode_len
))
3639 /* are we at the end of the stream? */
3643 /* sanity check the size parameter */
3644 if (!xdr_control(xdr
, XDR_GET_BYTES_AVAIL
, &bytesrec
))
3647 if (*size
> NVS_XDR_MAX_LEN(bytesrec
.xc_num_avail
))
3658 static const struct nvs_ops nvs_xdr_ops
= {
3659 .nvs_nvlist
= nvs_xdr_nvlist
,
3660 .nvs_nvpair
= nvs_xdr_nvpair
,
3661 .nvs_nvp_op
= nvs_xdr_nvp_op
,
3662 .nvs_nvp_size
= nvs_xdr_nvp_size
,
3663 .nvs_nvl_fini
= nvs_xdr_nvl_fini
3667 nvs_xdr(nvstream_t
*nvs
, nvlist_t
*nvl
, char *buf
, size_t *buflen
)
3672 nvs
->nvs_ops
= &nvs_xdr_ops
;
3674 if ((err
= nvs_xdr_create(nvs
, &xdr
, buf
+ sizeof (nvs_header_t
),
3675 *buflen
- sizeof (nvs_header_t
))) != 0)
3678 err
= nvs_operation(nvs
, nvl
, buflen
);
3680 nvs_xdr_destroy(nvs
);
3685 EXPORT_SYMBOL(nv_alloc_init
);
3686 EXPORT_SYMBOL(nv_alloc_reset
);
3687 EXPORT_SYMBOL(nv_alloc_fini
);
3689 /* list management */
3690 EXPORT_SYMBOL(nvlist_alloc
);
3691 EXPORT_SYMBOL(nvlist_free
);
3692 EXPORT_SYMBOL(nvlist_size
);
3693 EXPORT_SYMBOL(nvlist_pack
);
3694 EXPORT_SYMBOL(nvlist_unpack
);
3695 EXPORT_SYMBOL(nvlist_dup
);
3696 EXPORT_SYMBOL(nvlist_merge
);
3698 EXPORT_SYMBOL(nvlist_xalloc
);
3699 EXPORT_SYMBOL(nvlist_xpack
);
3700 EXPORT_SYMBOL(nvlist_xunpack
);
3701 EXPORT_SYMBOL(nvlist_xdup
);
3702 EXPORT_SYMBOL(nvlist_lookup_nv_alloc
);
3704 EXPORT_SYMBOL(nvlist_add_nvpair
);
3705 EXPORT_SYMBOL(nvlist_add_boolean
);
3706 EXPORT_SYMBOL(nvlist_add_boolean_value
);
3707 EXPORT_SYMBOL(nvlist_add_byte
);
3708 EXPORT_SYMBOL(nvlist_add_int8
);
3709 EXPORT_SYMBOL(nvlist_add_uint8
);
3710 EXPORT_SYMBOL(nvlist_add_int16
);
3711 EXPORT_SYMBOL(nvlist_add_uint16
);
3712 EXPORT_SYMBOL(nvlist_add_int32
);
3713 EXPORT_SYMBOL(nvlist_add_uint32
);
3714 EXPORT_SYMBOL(nvlist_add_int64
);
3715 EXPORT_SYMBOL(nvlist_add_uint64
);
3716 EXPORT_SYMBOL(nvlist_add_string
);
3717 EXPORT_SYMBOL(nvlist_add_nvlist
);
3718 EXPORT_SYMBOL(nvlist_add_boolean_array
);
3719 EXPORT_SYMBOL(nvlist_add_byte_array
);
3720 EXPORT_SYMBOL(nvlist_add_int8_array
);
3721 EXPORT_SYMBOL(nvlist_add_uint8_array
);
3722 EXPORT_SYMBOL(nvlist_add_int16_array
);
3723 EXPORT_SYMBOL(nvlist_add_uint16_array
);
3724 EXPORT_SYMBOL(nvlist_add_int32_array
);
3725 EXPORT_SYMBOL(nvlist_add_uint32_array
);
3726 EXPORT_SYMBOL(nvlist_add_int64_array
);
3727 EXPORT_SYMBOL(nvlist_add_uint64_array
);
3728 EXPORT_SYMBOL(nvlist_add_string_array
);
3729 EXPORT_SYMBOL(nvlist_add_nvlist_array
);
3730 EXPORT_SYMBOL(nvlist_next_nvpair
);
3731 EXPORT_SYMBOL(nvlist_prev_nvpair
);
3732 EXPORT_SYMBOL(nvlist_empty
);
3733 EXPORT_SYMBOL(nvlist_add_hrtime
);
3735 EXPORT_SYMBOL(nvlist_remove
);
3736 EXPORT_SYMBOL(nvlist_remove_nvpair
);
3737 EXPORT_SYMBOL(nvlist_remove_all
);
3739 EXPORT_SYMBOL(nvlist_lookup_boolean
);
3740 EXPORT_SYMBOL(nvlist_lookup_boolean_value
);
3741 EXPORT_SYMBOL(nvlist_lookup_byte
);
3742 EXPORT_SYMBOL(nvlist_lookup_int8
);
3743 EXPORT_SYMBOL(nvlist_lookup_uint8
);
3744 EXPORT_SYMBOL(nvlist_lookup_int16
);
3745 EXPORT_SYMBOL(nvlist_lookup_uint16
);
3746 EXPORT_SYMBOL(nvlist_lookup_int32
);
3747 EXPORT_SYMBOL(nvlist_lookup_uint32
);
3748 EXPORT_SYMBOL(nvlist_lookup_int64
);
3749 EXPORT_SYMBOL(nvlist_lookup_uint64
);
3750 EXPORT_SYMBOL(nvlist_lookup_string
);
3751 EXPORT_SYMBOL(nvlist_lookup_nvlist
);
3752 EXPORT_SYMBOL(nvlist_lookup_boolean_array
);
3753 EXPORT_SYMBOL(nvlist_lookup_byte_array
);
3754 EXPORT_SYMBOL(nvlist_lookup_int8_array
);
3755 EXPORT_SYMBOL(nvlist_lookup_uint8_array
);
3756 EXPORT_SYMBOL(nvlist_lookup_int16_array
);
3757 EXPORT_SYMBOL(nvlist_lookup_uint16_array
);
3758 EXPORT_SYMBOL(nvlist_lookup_int32_array
);
3759 EXPORT_SYMBOL(nvlist_lookup_uint32_array
);
3760 EXPORT_SYMBOL(nvlist_lookup_int64_array
);
3761 EXPORT_SYMBOL(nvlist_lookup_uint64_array
);
3762 EXPORT_SYMBOL(nvlist_lookup_string_array
);
3763 EXPORT_SYMBOL(nvlist_lookup_nvlist_array
);
3764 EXPORT_SYMBOL(nvlist_lookup_hrtime
);
3765 EXPORT_SYMBOL(nvlist_lookup_pairs
);
3767 EXPORT_SYMBOL(nvlist_lookup_nvpair
);
3768 EXPORT_SYMBOL(nvlist_exists
);
3770 /* processing nvpair */
3771 EXPORT_SYMBOL(nvpair_name
);
3772 EXPORT_SYMBOL(nvpair_type
);
3773 EXPORT_SYMBOL(nvpair_value_boolean_value
);
3774 EXPORT_SYMBOL(nvpair_value_byte
);
3775 EXPORT_SYMBOL(nvpair_value_int8
);
3776 EXPORT_SYMBOL(nvpair_value_uint8
);
3777 EXPORT_SYMBOL(nvpair_value_int16
);
3778 EXPORT_SYMBOL(nvpair_value_uint16
);
3779 EXPORT_SYMBOL(nvpair_value_int32
);
3780 EXPORT_SYMBOL(nvpair_value_uint32
);
3781 EXPORT_SYMBOL(nvpair_value_int64
);
3782 EXPORT_SYMBOL(nvpair_value_uint64
);
3783 EXPORT_SYMBOL(nvpair_value_string
);
3784 EXPORT_SYMBOL(nvpair_value_nvlist
);
3785 EXPORT_SYMBOL(nvpair_value_boolean_array
);
3786 EXPORT_SYMBOL(nvpair_value_byte_array
);
3787 EXPORT_SYMBOL(nvpair_value_int8_array
);
3788 EXPORT_SYMBOL(nvpair_value_uint8_array
);
3789 EXPORT_SYMBOL(nvpair_value_int16_array
);
3790 EXPORT_SYMBOL(nvpair_value_uint16_array
);
3791 EXPORT_SYMBOL(nvpair_value_int32_array
);
3792 EXPORT_SYMBOL(nvpair_value_uint32_array
);
3793 EXPORT_SYMBOL(nvpair_value_int64_array
);
3794 EXPORT_SYMBOL(nvpair_value_uint64_array
);
3795 EXPORT_SYMBOL(nvpair_value_string_array
);
3796 EXPORT_SYMBOL(nvpair_value_nvlist_array
);
3797 EXPORT_SYMBOL(nvpair_value_hrtime
);