4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 #include <sys/nvpair.h>
28 #include <sys/debug.h>
29 #include <sys/param.h>
35 * "Force" nvlist wrapper.
37 * These functions wrap the nvlist_* functions with assertions that assume
38 * the operation is successful. This allows the caller's code to be much
39 * more readable, especially for the fnvlist_lookup_* and fnvpair_value_*
40 * functions, which can return the requested value (rather than filling in
43 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate
46 * More wrappers should be added as needed -- for example
47 * nvlist_lookup_*_array and nvpair_value_*_array.
54 VERIFY0(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
));
59 fnvlist_free(nvlist_t
*nvl
)
65 fnvlist_size(nvlist_t
*nvl
)
68 VERIFY0(nvlist_size(nvl
, &size
, NV_ENCODE_NATIVE
));
73 * Returns allocated buffer of size *sizep. Caller must free the buffer with
74 * fnvlist_pack_free().
77 fnvlist_pack(nvlist_t
*nvl
, size_t *sizep
)
80 VERIFY3U(nvlist_pack(nvl
, &packed
, sizep
, NV_ENCODE_NATIVE
,
87 fnvlist_pack_free(char *pack
, size_t size
)
90 kmem_free(pack
, size
);
97 fnvlist_unpack(char *buf
, size_t buflen
)
100 VERIFY0(nvlist_unpack(buf
, buflen
, &rv
, KM_SLEEP
));
105 fnvlist_dup(nvlist_t
*nvl
)
108 VERIFY0(nvlist_dup(nvl
, &rv
, KM_SLEEP
));
113 fnvlist_merge(nvlist_t
*dst
, nvlist_t
*src
)
115 VERIFY0(nvlist_merge(dst
, src
, KM_SLEEP
));
119 fnvlist_num_pairs(nvlist_t
*nvl
)
124 for (pair
= nvlist_next_nvpair(nvl
, 0); pair
!= NULL
;
125 pair
= nvlist_next_nvpair(nvl
, pair
))
131 fnvlist_add_boolean(nvlist_t
*nvl
, const char *name
)
133 VERIFY0(nvlist_add_boolean(nvl
, name
));
137 fnvlist_add_boolean_value(nvlist_t
*nvl
, const char *name
, boolean_t val
)
139 VERIFY0(nvlist_add_boolean_value(nvl
, name
, val
));
143 fnvlist_add_byte(nvlist_t
*nvl
, const char *name
, uchar_t val
)
145 VERIFY0(nvlist_add_byte(nvl
, name
, val
));
149 fnvlist_add_int8(nvlist_t
*nvl
, const char *name
, int8_t val
)
151 VERIFY0(nvlist_add_int8(nvl
, name
, val
));
155 fnvlist_add_uint8(nvlist_t
*nvl
, const char *name
, uint8_t val
)
157 VERIFY0(nvlist_add_uint8(nvl
, name
, val
));
161 fnvlist_add_int16(nvlist_t
*nvl
, const char *name
, int16_t val
)
163 VERIFY0(nvlist_add_int16(nvl
, name
, val
));
167 fnvlist_add_uint16(nvlist_t
*nvl
, const char *name
, uint16_t val
)
169 VERIFY0(nvlist_add_uint16(nvl
, name
, val
));
173 fnvlist_add_int32(nvlist_t
*nvl
, const char *name
, int32_t val
)
175 VERIFY0(nvlist_add_int32(nvl
, name
, val
));
179 fnvlist_add_uint32(nvlist_t
*nvl
, const char *name
, uint32_t val
)
181 VERIFY0(nvlist_add_uint32(nvl
, name
, val
));
185 fnvlist_add_int64(nvlist_t
*nvl
, const char *name
, int64_t val
)
187 VERIFY0(nvlist_add_int64(nvl
, name
, val
));
191 fnvlist_add_uint64(nvlist_t
*nvl
, const char *name
, uint64_t val
)
193 VERIFY0(nvlist_add_uint64(nvl
, name
, val
));
197 fnvlist_add_string(nvlist_t
*nvl
, const char *name
, const char *val
)
199 VERIFY0(nvlist_add_string(nvl
, name
, val
));
203 fnvlist_add_nvlist(nvlist_t
*nvl
, const char *name
, nvlist_t
*val
)
205 VERIFY0(nvlist_add_nvlist(nvl
, name
, val
));
209 fnvlist_add_nvpair(nvlist_t
*nvl
, nvpair_t
*pair
)
211 VERIFY0(nvlist_add_nvpair(nvl
, pair
));
215 fnvlist_add_boolean_array(nvlist_t
*nvl
, const char *name
,
216 boolean_t
*val
, uint_t n
)
218 VERIFY0(nvlist_add_boolean_array(nvl
, name
, val
, n
));
222 fnvlist_add_byte_array(nvlist_t
*nvl
, const char *name
, uchar_t
*val
, uint_t n
)
224 VERIFY0(nvlist_add_byte_array(nvl
, name
, val
, n
));
228 fnvlist_add_int8_array(nvlist_t
*nvl
, const char *name
, int8_t *val
, uint_t n
)
230 VERIFY0(nvlist_add_int8_array(nvl
, name
, val
, n
));
234 fnvlist_add_uint8_array(nvlist_t
*nvl
, const char *name
, uint8_t *val
, uint_t n
)
236 VERIFY0(nvlist_add_uint8_array(nvl
, name
, val
, n
));
240 fnvlist_add_int16_array(nvlist_t
*nvl
, const char *name
, int16_t *val
, uint_t n
)
242 VERIFY0(nvlist_add_int16_array(nvl
, name
, val
, n
));
246 fnvlist_add_uint16_array(nvlist_t
*nvl
, const char *name
,
247 uint16_t *val
, uint_t n
)
249 VERIFY0(nvlist_add_uint16_array(nvl
, name
, val
, n
));
253 fnvlist_add_int32_array(nvlist_t
*nvl
, const char *name
, int32_t *val
, uint_t n
)
255 VERIFY0(nvlist_add_int32_array(nvl
, name
, val
, n
));
259 fnvlist_add_uint32_array(nvlist_t
*nvl
, const char *name
,
260 uint32_t *val
, uint_t n
)
262 VERIFY0(nvlist_add_uint32_array(nvl
, name
, val
, n
));
266 fnvlist_add_int64_array(nvlist_t
*nvl
, const char *name
, int64_t *val
, uint_t n
)
268 VERIFY0(nvlist_add_int64_array(nvl
, name
, val
, n
));
272 fnvlist_add_uint64_array(nvlist_t
*nvl
, const char *name
,
273 uint64_t *val
, uint_t n
)
275 VERIFY0(nvlist_add_uint64_array(nvl
, name
, val
, n
));
279 fnvlist_add_string_array(nvlist_t
*nvl
, const char *name
,
280 char * const *val
, uint_t n
)
282 VERIFY0(nvlist_add_string_array(nvl
, name
, val
, n
));
286 fnvlist_add_nvlist_array(nvlist_t
*nvl
, const char *name
,
287 nvlist_t
**val
, uint_t n
)
289 VERIFY0(nvlist_add_nvlist_array(nvl
, name
, val
, n
));
293 fnvlist_remove(nvlist_t
*nvl
, const char *name
)
295 VERIFY0(nvlist_remove_all(nvl
, name
));
299 fnvlist_remove_nvpair(nvlist_t
*nvl
, nvpair_t
*pair
)
301 VERIFY0(nvlist_remove_nvpair(nvl
, pair
));
305 fnvlist_lookup_nvpair(nvlist_t
*nvl
, const char *name
)
308 VERIFY0(nvlist_lookup_nvpair(nvl
, name
, &rv
));
312 /* returns B_TRUE if the entry exists */
314 fnvlist_lookup_boolean(nvlist_t
*nvl
, const char *name
)
316 return (nvlist_lookup_boolean(nvl
, name
) == 0);
320 fnvlist_lookup_boolean_value(nvlist_t
*nvl
, const char *name
)
323 VERIFY0(nvlist_lookup_boolean_value(nvl
, name
, &rv
));
328 fnvlist_lookup_byte(nvlist_t
*nvl
, const char *name
)
331 VERIFY0(nvlist_lookup_byte(nvl
, name
, &rv
));
336 fnvlist_lookup_int8(nvlist_t
*nvl
, const char *name
)
339 VERIFY0(nvlist_lookup_int8(nvl
, name
, &rv
));
344 fnvlist_lookup_int16(nvlist_t
*nvl
, const char *name
)
347 VERIFY0(nvlist_lookup_int16(nvl
, name
, &rv
));
352 fnvlist_lookup_int32(nvlist_t
*nvl
, const char *name
)
355 VERIFY0(nvlist_lookup_int32(nvl
, name
, &rv
));
360 fnvlist_lookup_int64(nvlist_t
*nvl
, const char *name
)
363 VERIFY0(nvlist_lookup_int64(nvl
, name
, &rv
));
368 fnvlist_lookup_uint8_t(nvlist_t
*nvl
, const char *name
)
371 VERIFY0(nvlist_lookup_uint8(nvl
, name
, &rv
));
376 fnvlist_lookup_uint16(nvlist_t
*nvl
, const char *name
)
379 VERIFY0(nvlist_lookup_uint16(nvl
, name
, &rv
));
384 fnvlist_lookup_uint32(nvlist_t
*nvl
, const char *name
)
387 VERIFY0(nvlist_lookup_uint32(nvl
, name
, &rv
));
392 fnvlist_lookup_uint64(nvlist_t
*nvl
, const char *name
)
395 VERIFY0(nvlist_lookup_uint64(nvl
, name
, &rv
));
400 fnvlist_lookup_string(nvlist_t
*nvl
, const char *name
)
403 VERIFY0(nvlist_lookup_string(nvl
, name
, &rv
));
408 fnvlist_lookup_nvlist(nvlist_t
*nvl
, const char *name
)
411 VERIFY0(nvlist_lookup_nvlist(nvl
, name
, &rv
));
416 fnvpair_value_boolean_value(nvpair_t
*nvp
)
419 VERIFY0(nvpair_value_boolean_value(nvp
, &rv
));
424 fnvpair_value_byte(nvpair_t
*nvp
)
427 VERIFY0(nvpair_value_byte(nvp
, &rv
));
432 fnvpair_value_int8(nvpair_t
*nvp
)
435 VERIFY0(nvpair_value_int8(nvp
, &rv
));
440 fnvpair_value_int16(nvpair_t
*nvp
)
443 VERIFY0(nvpair_value_int16(nvp
, &rv
));
448 fnvpair_value_int32(nvpair_t
*nvp
)
451 VERIFY0(nvpair_value_int32(nvp
, &rv
));
456 fnvpair_value_int64(nvpair_t
*nvp
)
459 VERIFY0(nvpair_value_int64(nvp
, &rv
));
464 fnvpair_value_uint8_t(nvpair_t
*nvp
)
467 VERIFY0(nvpair_value_uint8(nvp
, &rv
));
472 fnvpair_value_uint16(nvpair_t
*nvp
)
475 VERIFY0(nvpair_value_uint16(nvp
, &rv
));
480 fnvpair_value_uint32(nvpair_t
*nvp
)
483 VERIFY0(nvpair_value_uint32(nvp
, &rv
));
488 fnvpair_value_uint64(nvpair_t
*nvp
)
491 VERIFY0(nvpair_value_uint64(nvp
, &rv
));
496 fnvpair_value_string(nvpair_t
*nvp
)
499 VERIFY0(nvpair_value_string(nvp
, &rv
));
504 fnvpair_value_nvlist(nvpair_t
*nvp
)
507 VERIFY0(nvpair_value_nvlist(nvp
, &rv
));