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) 2012, 2018 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,
86 fnvlist_pack_free(char *pack, size_t size)
89 kmem_free(pack, size);
97 fnvlist_unpack(char *buf, size_t buflen)
100 VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));
105 fnvlist_dup(const 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 const 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, const uchar_t *val,
225 VERIFY0(nvlist_add_byte_array(nvl, name, val, n));
229 fnvlist_add_int8_array(nvlist_t *nvl, const char *name, const int8_t *val,
232 VERIFY0(nvlist_add_int8_array(nvl, name, val, n));
236 fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, const uint8_t *val,
239 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));
243 fnvlist_add_int16_array(nvlist_t *nvl, const char *name, const int16_t *val,
246 VERIFY0(nvlist_add_int16_array(nvl, name, val, n));
250 fnvlist_add_uint16_array(nvlist_t *nvl, const char *name,
251 const uint16_t *val, uint_t n)
253 VERIFY0(nvlist_add_uint16_array(nvl, name, val, n));
257 fnvlist_add_int32_array(nvlist_t *nvl, const char *name, const int32_t *val,
260 VERIFY0(nvlist_add_int32_array(nvl, name, val, n));
264 fnvlist_add_uint32_array(nvlist_t *nvl, const char *name,
265 const uint32_t *val, uint_t n)
267 VERIFY0(nvlist_add_uint32_array(nvl, name, val, n));
271 fnvlist_add_int64_array(nvlist_t *nvl, const char *name, const int64_t *val,
274 VERIFY0(nvlist_add_int64_array(nvl, name, val, n));
278 fnvlist_add_uint64_array(nvlist_t *nvl, const char *name,
279 const uint64_t *val, uint_t n)
281 VERIFY0(nvlist_add_uint64_array(nvl, name, val, n));
285 fnvlist_add_string_array(nvlist_t *nvl, const char *name,
286 const char * const *val, uint_t n)
288 VERIFY0(nvlist_add_string_array(nvl, name, val, n));
292 fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name,
293 const nvlist_t * const *val, uint_t n)
295 VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n));
299 fnvlist_remove(nvlist_t *nvl, const char *name)
301 VERIFY0(nvlist_remove_all(nvl, name));
305 fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)
307 VERIFY0(nvlist_remove_nvpair(nvl, pair));
311 fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)
314 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));
318 /* returns B_TRUE if the entry exists */
320 fnvlist_lookup_boolean(const nvlist_t *nvl, const char *name)
322 return (nvlist_lookup_boolean(nvl, name) == 0);
326 fnvlist_lookup_boolean_value(const nvlist_t *nvl, const char *name)
329 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));
334 fnvlist_lookup_byte(const nvlist_t *nvl, const char *name)
337 VERIFY0(nvlist_lookup_byte(nvl, name, &rv));
342 fnvlist_lookup_int8(const nvlist_t *nvl, const char *name)
345 VERIFY0(nvlist_lookup_int8(nvl, name, &rv));
350 fnvlist_lookup_int16(const nvlist_t *nvl, const char *name)
353 VERIFY0(nvlist_lookup_int16(nvl, name, &rv));
358 fnvlist_lookup_int32(const nvlist_t *nvl, const char *name)
361 VERIFY0(nvlist_lookup_int32(nvl, name, &rv));
366 fnvlist_lookup_int64(const nvlist_t *nvl, const char *name)
369 VERIFY0(nvlist_lookup_int64(nvl, name, &rv));
374 fnvlist_lookup_uint8(const nvlist_t *nvl, const char *name)
377 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
382 fnvlist_lookup_uint16(const nvlist_t *nvl, const char *name)
385 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));
390 fnvlist_lookup_uint32(const nvlist_t *nvl, const char *name)
393 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));
398 fnvlist_lookup_uint64(const nvlist_t *nvl, const char *name)
401 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));
406 fnvlist_lookup_string(nvlist_t *nvl, const char *name)
409 VERIFY0(nvlist_lookup_string(nvl, name, &rv));
414 fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)
417 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));
421 fnvlist_lookup_boolean_array(nvlist_t *nvl, const char *name, uint_t *n)
424 VERIFY0(nvlist_lookup_boolean_array(nvl, name, &rv, n));
429 fnvlist_lookup_byte_array(nvlist_t *nvl, const char *name, uint_t *n)
432 VERIFY0(nvlist_lookup_byte_array(nvl, name, &rv, n));
437 fnvlist_lookup_int8_array(nvlist_t *nvl, const char *name, uint_t *n)
440 VERIFY0(nvlist_lookup_int8_array(nvl, name, &rv, n));
445 fnvlist_lookup_uint8_array(nvlist_t *nvl, const char *name, uint_t *n)
448 VERIFY0(nvlist_lookup_uint8_array(nvl, name, &rv, n));
453 fnvlist_lookup_int16_array(nvlist_t *nvl, const char *name, uint_t *n)
456 VERIFY0(nvlist_lookup_int16_array(nvl, name, &rv, n));
461 fnvlist_lookup_uint16_array(nvlist_t *nvl, const char *name, uint_t *n)
464 VERIFY0(nvlist_lookup_uint16_array(nvl, name, &rv, n));
469 fnvlist_lookup_int32_array(nvlist_t *nvl, const char *name, uint_t *n)
472 VERIFY0(nvlist_lookup_int32_array(nvl, name, &rv, n));
477 fnvlist_lookup_uint32_array(nvlist_t *nvl, const char *name, uint_t *n)
480 VERIFY0(nvlist_lookup_uint32_array(nvl, name, &rv, n));
485 fnvlist_lookup_int64_array(nvlist_t *nvl, const char *name, uint_t *n)
488 VERIFY0(nvlist_lookup_int64_array(nvl, name, &rv, n));
493 fnvlist_lookup_uint64_array(nvlist_t *nvl, const char *name, uint_t *n)
496 VERIFY0(nvlist_lookup_uint64_array(nvl, name, &rv, n));
501 fnvpair_value_boolean_value(const nvpair_t *nvp)
504 VERIFY0(nvpair_value_boolean_value(nvp, &rv));
509 fnvpair_value_byte(const nvpair_t *nvp)
512 VERIFY0(nvpair_value_byte(nvp, &rv));
517 fnvpair_value_int8(const nvpair_t *nvp)
520 VERIFY0(nvpair_value_int8(nvp, &rv));
525 fnvpair_value_int16(const nvpair_t *nvp)
528 VERIFY0(nvpair_value_int16(nvp, &rv));
533 fnvpair_value_int32(const nvpair_t *nvp)
536 VERIFY0(nvpair_value_int32(nvp, &rv));
541 fnvpair_value_int64(const nvpair_t *nvp)
544 VERIFY0(nvpair_value_int64(nvp, &rv));
549 fnvpair_value_uint8(const nvpair_t *nvp)
552 VERIFY0(nvpair_value_uint8(nvp, &rv));
557 fnvpair_value_uint16(const nvpair_t *nvp)
560 VERIFY0(nvpair_value_uint16(nvp, &rv));
565 fnvpair_value_uint32(const nvpair_t *nvp)
568 VERIFY0(nvpair_value_uint32(nvp, &rv));
573 fnvpair_value_uint64(const nvpair_t *nvp)
576 VERIFY0(nvpair_value_uint64(nvp, &rv));
581 fnvpair_value_string(nvpair_t *nvp)
584 VERIFY0(nvpair_value_string(nvp, &rv));
589 fnvpair_value_nvlist(nvpair_t *nvp)
592 VERIFY0(nvpair_value_nvlist(nvp, &rv));
598 EXPORT_SYMBOL(fnvlist_alloc);
599 EXPORT_SYMBOL(fnvlist_free);
600 EXPORT_SYMBOL(fnvlist_size);
601 EXPORT_SYMBOL(fnvlist_pack);
602 EXPORT_SYMBOL(fnvlist_pack_free);
603 EXPORT_SYMBOL(fnvlist_unpack);
604 EXPORT_SYMBOL(fnvlist_dup);
605 EXPORT_SYMBOL(fnvlist_merge);
607 EXPORT_SYMBOL(fnvlist_add_nvpair);
608 EXPORT_SYMBOL(fnvlist_add_boolean);
609 EXPORT_SYMBOL(fnvlist_add_boolean_value);
610 EXPORT_SYMBOL(fnvlist_add_byte);
611 EXPORT_SYMBOL(fnvlist_add_int8);
612 EXPORT_SYMBOL(fnvlist_add_uint8);
613 EXPORT_SYMBOL(fnvlist_add_int16);
614 EXPORT_SYMBOL(fnvlist_add_uint16);
615 EXPORT_SYMBOL(fnvlist_add_int32);
616 EXPORT_SYMBOL(fnvlist_add_uint32);
617 EXPORT_SYMBOL(fnvlist_add_int64);
618 EXPORT_SYMBOL(fnvlist_add_uint64);
619 EXPORT_SYMBOL(fnvlist_add_string);
620 EXPORT_SYMBOL(fnvlist_add_nvlist);
621 EXPORT_SYMBOL(fnvlist_add_boolean_array);
622 EXPORT_SYMBOL(fnvlist_add_byte_array);
623 EXPORT_SYMBOL(fnvlist_add_int8_array);
624 EXPORT_SYMBOL(fnvlist_add_uint8_array);
625 EXPORT_SYMBOL(fnvlist_add_int16_array);
626 EXPORT_SYMBOL(fnvlist_add_uint16_array);
627 EXPORT_SYMBOL(fnvlist_add_int32_array);
628 EXPORT_SYMBOL(fnvlist_add_uint32_array);
629 EXPORT_SYMBOL(fnvlist_add_int64_array);
630 EXPORT_SYMBOL(fnvlist_add_uint64_array);
631 EXPORT_SYMBOL(fnvlist_add_string_array);
632 EXPORT_SYMBOL(fnvlist_add_nvlist_array);
634 EXPORT_SYMBOL(fnvlist_remove);
635 EXPORT_SYMBOL(fnvlist_remove_nvpair);
637 EXPORT_SYMBOL(fnvlist_lookup_nvpair);
638 EXPORT_SYMBOL(fnvlist_lookup_boolean);
639 EXPORT_SYMBOL(fnvlist_lookup_boolean_value);
640 EXPORT_SYMBOL(fnvlist_lookup_byte);
641 EXPORT_SYMBOL(fnvlist_lookup_int8);
642 EXPORT_SYMBOL(fnvlist_lookup_uint8);
643 EXPORT_SYMBOL(fnvlist_lookup_int16);
644 EXPORT_SYMBOL(fnvlist_lookup_uint16);
645 EXPORT_SYMBOL(fnvlist_lookup_int32);
646 EXPORT_SYMBOL(fnvlist_lookup_uint32);
647 EXPORT_SYMBOL(fnvlist_lookup_int64);
648 EXPORT_SYMBOL(fnvlist_lookup_uint64);
649 EXPORT_SYMBOL(fnvlist_lookup_string);
650 EXPORT_SYMBOL(fnvlist_lookup_nvlist);
652 EXPORT_SYMBOL(fnvpair_value_boolean_value);
653 EXPORT_SYMBOL(fnvpair_value_byte);
654 EXPORT_SYMBOL(fnvpair_value_int8);
655 EXPORT_SYMBOL(fnvpair_value_uint8);
656 EXPORT_SYMBOL(fnvpair_value_int16);
657 EXPORT_SYMBOL(fnvpair_value_uint16);
658 EXPORT_SYMBOL(fnvpair_value_int32);
659 EXPORT_SYMBOL(fnvpair_value_uint32);
660 EXPORT_SYMBOL(fnvpair_value_int64);
661 EXPORT_SYMBOL(fnvpair_value_uint64);
662 EXPORT_SYMBOL(fnvpair_value_string);
663 EXPORT_SYMBOL(fnvpair_value_nvlist);
664 EXPORT_SYMBOL(fnvlist_num_pairs);