1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2012 by Delphix. All rights reserved.
24 */
25
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
33
34 /*
35 * "Force" nvlist wrapper.
36 *
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).
42 *
43 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate
44 * with KM_SLEEP.
45 *
46 * More wrappers should be added as needed -- for example
47 * nvlist_lookup_*_array and nvpair_value_*_array.
48 */
49
50 nvlist_t *
fnvlist_alloc(void)51 fnvlist_alloc(void)
52 {
53 nvlist_t *nvl;
54 VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP));
55 return (nvl);
56 }
57
58 void
fnvlist_free(nvlist_t * nvl)59 fnvlist_free(nvlist_t *nvl)
60 {
61 nvlist_free(nvl);
62 }
63
64 size_t
fnvlist_size(nvlist_t * nvl)65 fnvlist_size(nvlist_t *nvl)
66 {
67 size_t size;
68 VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE));
69 return (size);
70 }
71
72 /*
73 * Returns allocated buffer of size *sizep. Caller must free the buffer with
74 * fnvlist_pack_free().
75 */
76 char *
fnvlist_pack(nvlist_t * nvl,size_t * sizep)77 fnvlist_pack(nvlist_t *nvl, size_t *sizep)
78 {
79 char *packed = 0;
80 VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE,
81 KM_SLEEP), ==, 0);
82 return (packed);
83 }
84
85 /*ARGSUSED*/
86 void
fnvlist_pack_free(char * pack,size_t size)87 fnvlist_pack_free(char *pack, size_t size)
88 {
89 #ifdef _KERNEL
90 kmem_free(pack, size);
91 #else
92 free(pack);
93 #endif
94 }
95
96 nvlist_t *
fnvlist_unpack(char * buf,size_t buflen)97 fnvlist_unpack(char *buf, size_t buflen)
98 {
99 nvlist_t *rv;
100 VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));
101 return (rv);
102 }
103
104 nvlist_t *
fnvlist_dup(nvlist_t * nvl)105 fnvlist_dup(nvlist_t *nvl)
106 {
107 nvlist_t *rv;
108 VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP));
109 return (rv);
110 }
111
112 void
fnvlist_merge(nvlist_t * dst,nvlist_t * src)113 fnvlist_merge(nvlist_t *dst, nvlist_t *src)
114 {
115 VERIFY0(nvlist_merge(dst, src, KM_SLEEP));
116 }
117
118 size_t
fnvlist_num_pairs(nvlist_t * nvl)119 fnvlist_num_pairs(nvlist_t *nvl)
120 {
121 size_t count = 0;
122 nvpair_t *pair;
123
124 for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL;
125 pair = nvlist_next_nvpair(nvl, pair))
126 count++;
127 return (count);
128 }
129
130 void
fnvlist_add_boolean(nvlist_t * nvl,const char * name)131 fnvlist_add_boolean(nvlist_t *nvl, const char *name)
132 {
133 VERIFY0(nvlist_add_boolean(nvl, name));
134 }
135
136 void
fnvlist_add_boolean_value(nvlist_t * nvl,const char * name,boolean_t val)137 fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
138 {
139 VERIFY0(nvlist_add_boolean_value(nvl, name, val));
140 }
141
142 void
fnvlist_add_byte(nvlist_t * nvl,const char * name,uchar_t val)143 fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
144 {
145 VERIFY0(nvlist_add_byte(nvl, name, val));
146 }
147
148 void
fnvlist_add_int8(nvlist_t * nvl,const char * name,int8_t val)149 fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
150 {
151 VERIFY0(nvlist_add_int8(nvl, name, val));
152 }
153
154 void
fnvlist_add_uint8(nvlist_t * nvl,const char * name,uint8_t val)155 fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
156 {
157 VERIFY0(nvlist_add_uint8(nvl, name, val));
158 }
159
160 void
fnvlist_add_int16(nvlist_t * nvl,const char * name,int16_t val)161 fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
162 {
163 VERIFY0(nvlist_add_int16(nvl, name, val));
164 }
165
166 void
fnvlist_add_uint16(nvlist_t * nvl,const char * name,uint16_t val)167 fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
168 {
169 VERIFY0(nvlist_add_uint16(nvl, name, val));
170 }
171
172 void
fnvlist_add_int32(nvlist_t * nvl,const char * name,int32_t val)173 fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
174 {
175 VERIFY0(nvlist_add_int32(nvl, name, val));
176 }
177
178 void
fnvlist_add_uint32(nvlist_t * nvl,const char * name,uint32_t val)179 fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
180 {
181 VERIFY0(nvlist_add_uint32(nvl, name, val));
182 }
183
184 void
fnvlist_add_int64(nvlist_t * nvl,const char * name,int64_t val)185 fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
186 {
187 VERIFY0(nvlist_add_int64(nvl, name, val));
188 }
189
190 void
fnvlist_add_uint64(nvlist_t * nvl,const char * name,uint64_t val)191 fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
192 {
193 VERIFY0(nvlist_add_uint64(nvl, name, val));
194 }
195
196 void
fnvlist_add_string(nvlist_t * nvl,const char * name,const char * val)197 fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
198 {
199 VERIFY0(nvlist_add_string(nvl, name, val));
200 }
201
202 void
fnvlist_add_nvlist(nvlist_t * nvl,const char * name,nvlist_t * val)203 fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
204 {
205 VERIFY0(nvlist_add_nvlist(nvl, name, val));
206 }
207
208 void
fnvlist_add_nvpair(nvlist_t * nvl,nvpair_t * pair)209 fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair)
210 {
211 VERIFY0(nvlist_add_nvpair(nvl, pair));
212 }
213
214 void
fnvlist_add_boolean_array(nvlist_t * nvl,const char * name,boolean_t * val,uint_t n)215 fnvlist_add_boolean_array(nvlist_t *nvl, const char *name,
216 boolean_t *val, uint_t n)
217 {
218 VERIFY0(nvlist_add_boolean_array(nvl, name, val, n));
219 }
220
221 void
fnvlist_add_byte_array(nvlist_t * nvl,const char * name,uchar_t * val,uint_t n)222 fnvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *val, uint_t n)
223 {
224 VERIFY0(nvlist_add_byte_array(nvl, name, val, n));
225 }
226
227 void
fnvlist_add_int8_array(nvlist_t * nvl,const char * name,int8_t * val,uint_t n)228 fnvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *val, uint_t n)
229 {
230 VERIFY0(nvlist_add_int8_array(nvl, name, val, n));
231 }
232
233 void
fnvlist_add_uint8_array(nvlist_t * nvl,const char * name,uint8_t * val,uint_t n)234 fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *val, uint_t n)
235 {
236 VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));
237 }
238
239 void
fnvlist_add_int16_array(nvlist_t * nvl,const char * name,int16_t * val,uint_t n)240 fnvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *val, uint_t n)
241 {
242 VERIFY0(nvlist_add_int16_array(nvl, name, val, n));
243 }
244
245 void
fnvlist_add_uint16_array(nvlist_t * nvl,const char * name,uint16_t * val,uint_t n)246 fnvlist_add_uint16_array(nvlist_t *nvl, const char *name,
247 uint16_t *val, uint_t n)
248 {
249 VERIFY0(nvlist_add_uint16_array(nvl, name, val, n));
250 }
251
252 void
fnvlist_add_int32_array(nvlist_t * nvl,const char * name,int32_t * val,uint_t n)253 fnvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *val, uint_t n)
254 {
255 VERIFY0(nvlist_add_int32_array(nvl, name, val, n));
256 }
257
258 void
fnvlist_add_uint32_array(nvlist_t * nvl,const char * name,uint32_t * val,uint_t n)259 fnvlist_add_uint32_array(nvlist_t *nvl, const char *name,
260 uint32_t *val, uint_t n)
261 {
262 VERIFY0(nvlist_add_uint32_array(nvl, name, val, n));
263 }
264
265 void
fnvlist_add_int64_array(nvlist_t * nvl,const char * name,int64_t * val,uint_t n)266 fnvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *val, uint_t n)
267 {
268 VERIFY0(nvlist_add_int64_array(nvl, name, val, n));
269 }
270
271 void
fnvlist_add_uint64_array(nvlist_t * nvl,const char * name,uint64_t * val,uint_t n)272 fnvlist_add_uint64_array(nvlist_t *nvl, const char *name,
273 uint64_t *val, uint_t n)
274 {
275 VERIFY0(nvlist_add_uint64_array(nvl, name, val, n));
276 }
277
278 void
fnvlist_add_string_array(nvlist_t * nvl,const char * name,char * const * val,uint_t n)279 fnvlist_add_string_array(nvlist_t *nvl, const char *name,
280 char * const *val, uint_t n)
281 {
282 VERIFY0(nvlist_add_string_array(nvl, name, val, n));
283 }
284
285 void
fnvlist_add_nvlist_array(nvlist_t * nvl,const char * name,nvlist_t ** val,uint_t n)286 fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name,
287 nvlist_t **val, uint_t n)
288 {
289 VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n));
290 }
291
292 void
fnvlist_remove(nvlist_t * nvl,const char * name)293 fnvlist_remove(nvlist_t *nvl, const char *name)
294 {
295 VERIFY0(nvlist_remove_all(nvl, name));
296 }
297
298 void
fnvlist_remove_nvpair(nvlist_t * nvl,nvpair_t * pair)299 fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)
300 {
301 VERIFY0(nvlist_remove_nvpair(nvl, pair));
302 }
303
304 nvpair_t *
fnvlist_lookup_nvpair(nvlist_t * nvl,const char * name)305 fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)
306 {
307 nvpair_t *rv;
308 VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));
309 return (rv);
310 }
311
312 /* returns B_TRUE if the entry exists */
313 boolean_t
fnvlist_lookup_boolean(nvlist_t * nvl,const char * name)314 fnvlist_lookup_boolean(nvlist_t *nvl, const char *name)
315 {
316 return (nvlist_lookup_boolean(nvl, name) == 0);
317 }
318
319 boolean_t
fnvlist_lookup_boolean_value(nvlist_t * nvl,const char * name)320 fnvlist_lookup_boolean_value(nvlist_t *nvl, const char *name)
321 {
322 boolean_t rv;
323 VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));
324 return (rv);
325 }
326
327 uchar_t
fnvlist_lookup_byte(nvlist_t * nvl,const char * name)328 fnvlist_lookup_byte(nvlist_t *nvl, const char *name)
329 {
330 uchar_t rv;
331 VERIFY0(nvlist_lookup_byte(nvl, name, &rv));
332 return (rv);
333 }
334
335 int8_t
fnvlist_lookup_int8(nvlist_t * nvl,const char * name)336 fnvlist_lookup_int8(nvlist_t *nvl, const char *name)
337 {
338 int8_t rv;
339 VERIFY0(nvlist_lookup_int8(nvl, name, &rv));
340 return (rv);
341 }
342
343 int16_t
fnvlist_lookup_int16(nvlist_t * nvl,const char * name)344 fnvlist_lookup_int16(nvlist_t *nvl, const char *name)
345 {
346 int16_t rv;
347 VERIFY0(nvlist_lookup_int16(nvl, name, &rv));
348 return (rv);
349 }
350
351 int32_t
fnvlist_lookup_int32(nvlist_t * nvl,const char * name)352 fnvlist_lookup_int32(nvlist_t *nvl, const char *name)
353 {
354 int32_t rv;
355 VERIFY0(nvlist_lookup_int32(nvl, name, &rv));
356 return (rv);
357 }
358
359 int64_t
fnvlist_lookup_int64(nvlist_t * nvl,const char * name)360 fnvlist_lookup_int64(nvlist_t *nvl, const char *name)
361 {
362 int64_t rv;
363 VERIFY0(nvlist_lookup_int64(nvl, name, &rv));
364 return (rv);
365 }
366
367 uint8_t
fnvlist_lookup_uint8(nvlist_t * nvl,const char * name)368 fnvlist_lookup_uint8(nvlist_t *nvl, const char *name)
369 {
370 uint8_t rv;
371 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
372 return (rv);
373 }
374
375 /*
376 * This version with the _t suffix was put in a public version of the libnvpair
377 * mapfile. While it is no longer in the header file, it remains here for binary
378 * compatibility. All new software should use fnvlist_lookup_uint8().
379 */
380 uint8_t
fnvlist_lookup_uint8_t(nvlist_t * nvl,const char * name)381 fnvlist_lookup_uint8_t(nvlist_t *nvl, const char *name)
382 {
383 uint8_t rv;
384 VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
385 return (rv);
386 }
387
388 uint16_t
fnvlist_lookup_uint16(nvlist_t * nvl,const char * name)389 fnvlist_lookup_uint16(nvlist_t *nvl, const char *name)
390 {
391 uint16_t rv;
392 VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));
393 return (rv);
394 }
395
396 uint32_t
fnvlist_lookup_uint32(nvlist_t * nvl,const char * name)397 fnvlist_lookup_uint32(nvlist_t *nvl, const char *name)
398 {
399 uint32_t rv;
400 VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));
401 return (rv);
402 }
403
404 uint64_t
fnvlist_lookup_uint64(nvlist_t * nvl,const char * name)405 fnvlist_lookup_uint64(nvlist_t *nvl, const char *name)
406 {
407 uint64_t rv;
408 VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));
409 return (rv);
410 }
411
412 char *
fnvlist_lookup_string(nvlist_t * nvl,const char * name)413 fnvlist_lookup_string(nvlist_t *nvl, const char *name)
414 {
415 char *rv;
416 VERIFY0(nvlist_lookup_string(nvl, name, &rv));
417 return (rv);
418 }
419
420 nvlist_t *
fnvlist_lookup_nvlist(nvlist_t * nvl,const char * name)421 fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)
422 {
423 nvlist_t *rv;
424 VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));
425 return (rv);
426 }
427
428 boolean_t
fnvpair_value_boolean_value(nvpair_t * nvp)429 fnvpair_value_boolean_value(nvpair_t *nvp)
430 {
431 boolean_t rv;
432 VERIFY0(nvpair_value_boolean_value(nvp, &rv));
433 return (rv);
434 }
435
436 uchar_t
fnvpair_value_byte(nvpair_t * nvp)437 fnvpair_value_byte(nvpair_t *nvp)
438 {
439 uchar_t rv;
440 VERIFY0(nvpair_value_byte(nvp, &rv));
441 return (rv);
442 }
443
444 int8_t
fnvpair_value_int8(nvpair_t * nvp)445 fnvpair_value_int8(nvpair_t *nvp)
446 {
447 int8_t rv;
448 VERIFY0(nvpair_value_int8(nvp, &rv));
449 return (rv);
450 }
451
452 int16_t
fnvpair_value_int16(nvpair_t * nvp)453 fnvpair_value_int16(nvpair_t *nvp)
454 {
455 int16_t rv;
456 VERIFY0(nvpair_value_int16(nvp, &rv));
457 return (rv);
458 }
459
460 int32_t
fnvpair_value_int32(nvpair_t * nvp)461 fnvpair_value_int32(nvpair_t *nvp)
462 {
463 int32_t rv;
464 VERIFY0(nvpair_value_int32(nvp, &rv));
465 return (rv);
466 }
467
468 int64_t
fnvpair_value_int64(nvpair_t * nvp)469 fnvpair_value_int64(nvpair_t *nvp)
470 {
471 int64_t rv;
472 VERIFY0(nvpair_value_int64(nvp, &rv));
473 return (rv);
474 }
475
476 uint8_t
fnvpair_value_uint8(nvpair_t * nvp)477 fnvpair_value_uint8(nvpair_t *nvp)
478 {
479 uint8_t rv;
480 VERIFY0(nvpair_value_uint8(nvp, &rv));
481 return (rv);
482 }
483
484 /*
485 * This version with the _t suffix was put in a public version of the libnvpair
486 * mapfile. While it is no longer in the header file, it remains here for binary
487 * compatibility. All new software should use fnvpair_value_uint8().
488 */
489 uint8_t
fnvpair_value_uint8_t(nvpair_t * nvp)490 fnvpair_value_uint8_t(nvpair_t *nvp)
491 {
492 uint8_t rv;
493 VERIFY0(nvpair_value_uint8(nvp, &rv));
494 return (rv);
495 }
496
497 uint16_t
fnvpair_value_uint16(nvpair_t * nvp)498 fnvpair_value_uint16(nvpair_t *nvp)
499 {
500 uint16_t rv;
501 VERIFY0(nvpair_value_uint16(nvp, &rv));
502 return (rv);
503 }
504
505 uint32_t
fnvpair_value_uint32(nvpair_t * nvp)506 fnvpair_value_uint32(nvpair_t *nvp)
507 {
508 uint32_t rv;
509 VERIFY0(nvpair_value_uint32(nvp, &rv));
510 return (rv);
511 }
512
513 uint64_t
fnvpair_value_uint64(nvpair_t * nvp)514 fnvpair_value_uint64(nvpair_t *nvp)
515 {
516 uint64_t rv;
517 VERIFY0(nvpair_value_uint64(nvp, &rv));
518 return (rv);
519 }
520
521 char *
fnvpair_value_string(nvpair_t * nvp)522 fnvpair_value_string(nvpair_t *nvp)
523 {
524 char *rv;
525 VERIFY0(nvpair_value_string(nvp, &rv));
526 return (rv);
527 }
528
529 nvlist_t *
fnvpair_value_nvlist(nvpair_t * nvp)530 fnvpair_value_nvlist(nvpair_t *nvp)
531 {
532 nvlist_t *rv;
533 VERIFY0(nvpair_value_nvlist(nvp, &rv));
534 return (rv);
535 }
536