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