Linux: Fix detection of register_sysctl_sz
[zfs.git] / module / nvpair / fnvpair.c
blobcc2233c40391d6a7f9c44f45847d9e1d7c424fab
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
23 * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
26 #include <sys/nvpair.h>
27 #include <sys/kmem.h>
28 #include <sys/debug.h>
29 #include <sys/param.h>
30 #ifndef _KERNEL
31 #include <stdlib.h>
32 #endif
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
41 * a pointer).
43 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate
44 * with KM_SLEEP.
46 * More wrappers should be added as needed -- for example
47 * nvlist_lookup_*_array and nvpair_value_*_array.
50 nvlist_t *
51 fnvlist_alloc(void)
53 nvlist_t *nvl;
54 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP));
55 return (nvl);
58 void
59 fnvlist_free(nvlist_t *nvl)
61 nvlist_free(nvl);
64 size_t
65 fnvlist_size(nvlist_t *nvl)
67 size_t size;
68 VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE));
69 return (size);
73 * Returns allocated buffer of size *sizep. Caller must free the buffer with
74 * fnvlist_pack_free().
76 char *
77 fnvlist_pack(nvlist_t *nvl, size_t *sizep)
79 char *packed = 0;
80 VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE,
81 KM_SLEEP), ==, 0);
82 return (packed);
85 void
86 fnvlist_pack_free(char *pack, size_t size)
88 #ifdef _KERNEL
89 kmem_free(pack, size);
90 #else
91 (void) size;
92 free(pack);
93 #endif
96 nvlist_t *
97 fnvlist_unpack(char *buf, size_t buflen)
99 nvlist_t *rv;
100 VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));
101 return (rv);
104 nvlist_t *
105 fnvlist_dup(const nvlist_t *nvl)
107 nvlist_t *rv;
108 VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP));
109 return (rv);
112 void
113 fnvlist_merge(nvlist_t *dst, nvlist_t *src)
115 VERIFY0(nvlist_merge(dst, src, KM_SLEEP));
118 size_t
119 fnvlist_num_pairs(nvlist_t *nvl)
121 size_t count = 0;
122 nvpair_t *pair;
124 for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL;
125 pair = nvlist_next_nvpair(nvl, pair))
126 count++;
127 return (count);
130 void
131 fnvlist_add_boolean(nvlist_t *nvl, const char *name)
133 VERIFY0(nvlist_add_boolean(nvl, name));
136 void
137 fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
139 VERIFY0(nvlist_add_boolean_value(nvl, name, val));
142 void
143 fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
145 VERIFY0(nvlist_add_byte(nvl, name, val));
148 void
149 fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
151 VERIFY0(nvlist_add_int8(nvl, name, val));
154 void
155 fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
157 VERIFY0(nvlist_add_uint8(nvl, name, val));
160 void
161 fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
163 VERIFY0(nvlist_add_int16(nvl, name, val));
166 void
167 fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
169 VERIFY0(nvlist_add_uint16(nvl, name, val));
172 void
173 fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
175 VERIFY0(nvlist_add_int32(nvl, name, val));
178 void
179 fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
181 VERIFY0(nvlist_add_uint32(nvl, name, val));
184 void
185 fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
187 VERIFY0(nvlist_add_int64(nvl, name, val));
190 void
191 fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
193 VERIFY0(nvlist_add_uint64(nvl, name, val));
196 void
197 fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
199 VERIFY0(nvlist_add_string(nvl, name, val));
202 void
203 fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
205 VERIFY0(nvlist_add_nvlist(nvl, name, val));
208 void
209 fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair)
211 VERIFY0(nvlist_add_nvpair(nvl, pair));
214 void
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));
221 void
222 fnvlist_add_byte_array(nvlist_t *nvl, const char *name, const uchar_t *val,
223 uint_t n)
225 VERIFY0(nvlist_add_byte_array(nvl, name, val, n));
228 void
229 fnvlist_add_int8_array(nvlist_t *nvl, const char *name, const int8_t *val,
230 uint_t n)
232 VERIFY0(nvlist_add_int8_array(nvl, name, val, n));
235 void
236 fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, const uint8_t *val,
237 uint_t n)
239 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));
242 void
243 fnvlist_add_int16_array(nvlist_t *nvl, const char *name, const int16_t *val,
244 uint_t n)
246 VERIFY0(nvlist_add_int16_array(nvl, name, val, n));
249 void
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));
256 void
257 fnvlist_add_int32_array(nvlist_t *nvl, const char *name, const int32_t *val,
258 uint_t n)
260 VERIFY0(nvlist_add_int32_array(nvl, name, val, n));
263 void
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));
270 void
271 fnvlist_add_int64_array(nvlist_t *nvl, const char *name, const int64_t *val,
272 uint_t n)
274 VERIFY0(nvlist_add_int64_array(nvl, name, val, n));
277 void
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));
284 void
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));
291 void
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));
298 void
299 fnvlist_remove(nvlist_t *nvl, const char *name)
301 VERIFY0(nvlist_remove_all(nvl, name));
304 void
305 fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)
307 VERIFY0(nvlist_remove_nvpair(nvl, pair));
310 nvpair_t *
311 fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)
313 nvpair_t *rv;
314 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));
315 return (rv);
318 /* returns B_TRUE if the entry exists */
319 boolean_t
320 fnvlist_lookup_boolean(const nvlist_t *nvl, const char *name)
322 return (nvlist_lookup_boolean(nvl, name) == 0);
325 boolean_t
326 fnvlist_lookup_boolean_value(const nvlist_t *nvl, const char *name)
328 boolean_t rv;
329 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));
330 return (rv);
333 uchar_t
334 fnvlist_lookup_byte(const nvlist_t *nvl, const char *name)
336 uchar_t rv;
337 VERIFY0(nvlist_lookup_byte(nvl, name, &rv));
338 return (rv);
341 int8_t
342 fnvlist_lookup_int8(const nvlist_t *nvl, const char *name)
344 int8_t rv;
345 VERIFY0(nvlist_lookup_int8(nvl, name, &rv));
346 return (rv);
349 int16_t
350 fnvlist_lookup_int16(const nvlist_t *nvl, const char *name)
352 int16_t rv;
353 VERIFY0(nvlist_lookup_int16(nvl, name, &rv));
354 return (rv);
357 int32_t
358 fnvlist_lookup_int32(const nvlist_t *nvl, const char *name)
360 int32_t rv;
361 VERIFY0(nvlist_lookup_int32(nvl, name, &rv));
362 return (rv);
365 int64_t
366 fnvlist_lookup_int64(const nvlist_t *nvl, const char *name)
368 int64_t rv;
369 VERIFY0(nvlist_lookup_int64(nvl, name, &rv));
370 return (rv);
373 uint8_t
374 fnvlist_lookup_uint8(const nvlist_t *nvl, const char *name)
376 uint8_t rv;
377 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
378 return (rv);
381 uint16_t
382 fnvlist_lookup_uint16(const nvlist_t *nvl, const char *name)
384 uint16_t rv;
385 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));
386 return (rv);
389 uint32_t
390 fnvlist_lookup_uint32(const nvlist_t *nvl, const char *name)
392 uint32_t rv;
393 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));
394 return (rv);
397 uint64_t
398 fnvlist_lookup_uint64(const nvlist_t *nvl, const char *name)
400 uint64_t rv;
401 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));
402 return (rv);
405 const char *
406 fnvlist_lookup_string(const nvlist_t *nvl, const char *name)
408 const char *rv;
409 VERIFY0(nvlist_lookup_string(nvl, name, &rv));
410 return (rv);
413 nvlist_t *
414 fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)
416 nvlist_t *rv;
417 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));
418 return (rv);
420 boolean_t *
421 fnvlist_lookup_boolean_array(nvlist_t *nvl, const char *name, uint_t *n)
423 boolean_t *rv;
424 VERIFY0(nvlist_lookup_boolean_array(nvl, name, &rv, n));
425 return (rv);
428 uchar_t *
429 fnvlist_lookup_byte_array(nvlist_t *nvl, const char *name, uint_t *n)
431 uchar_t *rv;
432 VERIFY0(nvlist_lookup_byte_array(nvl, name, &rv, n));
433 return (rv);
436 int8_t *
437 fnvlist_lookup_int8_array(nvlist_t *nvl, const char *name, uint_t *n)
439 int8_t *rv;
440 VERIFY0(nvlist_lookup_int8_array(nvl, name, &rv, n));
441 return (rv);
444 uint8_t *
445 fnvlist_lookup_uint8_array(nvlist_t *nvl, const char *name, uint_t *n)
447 uint8_t *rv;
448 VERIFY0(nvlist_lookup_uint8_array(nvl, name, &rv, n));
449 return (rv);
452 int16_t *
453 fnvlist_lookup_int16_array(nvlist_t *nvl, const char *name, uint_t *n)
455 int16_t *rv;
456 VERIFY0(nvlist_lookup_int16_array(nvl, name, &rv, n));
457 return (rv);
460 uint16_t *
461 fnvlist_lookup_uint16_array(nvlist_t *nvl, const char *name, uint_t *n)
463 uint16_t *rv;
464 VERIFY0(nvlist_lookup_uint16_array(nvl, name, &rv, n));
465 return (rv);
468 int32_t *
469 fnvlist_lookup_int32_array(nvlist_t *nvl, const char *name, uint_t *n)
471 int32_t *rv;
472 VERIFY0(nvlist_lookup_int32_array(nvl, name, &rv, n));
473 return (rv);
476 uint32_t *
477 fnvlist_lookup_uint32_array(nvlist_t *nvl, const char *name, uint_t *n)
479 uint32_t *rv;
480 VERIFY0(nvlist_lookup_uint32_array(nvl, name, &rv, n));
481 return (rv);
484 int64_t *
485 fnvlist_lookup_int64_array(nvlist_t *nvl, const char *name, uint_t *n)
487 int64_t *rv;
488 VERIFY0(nvlist_lookup_int64_array(nvl, name, &rv, n));
489 return (rv);
492 uint64_t *
493 fnvlist_lookup_uint64_array(nvlist_t *nvl, const char *name, uint_t *n)
495 uint64_t *rv;
496 VERIFY0(nvlist_lookup_uint64_array(nvl, name, &rv, n));
497 return (rv);
500 boolean_t
501 fnvpair_value_boolean_value(const nvpair_t *nvp)
503 boolean_t rv;
504 VERIFY0(nvpair_value_boolean_value(nvp, &rv));
505 return (rv);
508 uchar_t
509 fnvpair_value_byte(const nvpair_t *nvp)
511 uchar_t rv;
512 VERIFY0(nvpair_value_byte(nvp, &rv));
513 return (rv);
516 int8_t
517 fnvpair_value_int8(const nvpair_t *nvp)
519 int8_t rv;
520 VERIFY0(nvpair_value_int8(nvp, &rv));
521 return (rv);
524 int16_t
525 fnvpair_value_int16(const nvpair_t *nvp)
527 int16_t rv;
528 VERIFY0(nvpair_value_int16(nvp, &rv));
529 return (rv);
532 int32_t
533 fnvpair_value_int32(const nvpair_t *nvp)
535 int32_t rv;
536 VERIFY0(nvpair_value_int32(nvp, &rv));
537 return (rv);
540 int64_t
541 fnvpair_value_int64(const nvpair_t *nvp)
543 int64_t rv;
544 VERIFY0(nvpair_value_int64(nvp, &rv));
545 return (rv);
548 uint8_t
549 fnvpair_value_uint8(const nvpair_t *nvp)
551 uint8_t rv;
552 VERIFY0(nvpair_value_uint8(nvp, &rv));
553 return (rv);
556 uint16_t
557 fnvpair_value_uint16(const nvpair_t *nvp)
559 uint16_t rv;
560 VERIFY0(nvpair_value_uint16(nvp, &rv));
561 return (rv);
564 uint32_t
565 fnvpair_value_uint32(const nvpair_t *nvp)
567 uint32_t rv;
568 VERIFY0(nvpair_value_uint32(nvp, &rv));
569 return (rv);
572 uint64_t
573 fnvpair_value_uint64(const nvpair_t *nvp)
575 uint64_t rv;
576 VERIFY0(nvpair_value_uint64(nvp, &rv));
577 return (rv);
580 const char *
581 fnvpair_value_string(const nvpair_t *nvp)
583 const char *rv;
584 VERIFY0(nvpair_value_string(nvp, &rv));
585 return (rv);
588 nvlist_t *
589 fnvpair_value_nvlist(nvpair_t *nvp)
591 nvlist_t *rv;
592 VERIFY0(nvpair_value_nvlist(nvp, &rv));
593 return (rv);
596 #if defined(_KERNEL)
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);
666 #endif