aboutsummaryrefslogtreecommitdiffstats
path: root/module/nvpair/fnvpair.c
blob: ce151d6920f197310743ce7c2d1a3d519d5948c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2012 by Delphix. All rights reserved.
 */

#include <sys/nvpair.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <sys/param.h>
#ifndef _KERNEL
#include <stdlib.h>
#endif

/*
 * "Force" nvlist wrapper.
 *
 * These functions wrap the nvlist_* functions with assertions that assume
 * the operation is successful.  This allows the caller's code to be much
 * more readable, especially for the fnvlist_lookup_* and fnvpair_value_*
 * functions, which can return the requested value (rather than filling in
 * a pointer).
 *
 * These functions use NV_UNIQUE_NAME, encoding NV_ENCODE_NATIVE, and allocate
 * with KM_SLEEP.
 *
 * More wrappers should be added as needed -- for example
 * nvlist_lookup_*_array and nvpair_value_*_array.
 */

nvlist_t *
fnvlist_alloc(void)
{
	nvlist_t *nvl;
	VERIFY0(nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP));
	return (nvl);
}

void
fnvlist_free(nvlist_t *nvl)
{
	nvlist_free(nvl);
}

size_t
fnvlist_size(nvlist_t *nvl)
{
	size_t size;
	VERIFY0(nvlist_size(nvl, &size, NV_ENCODE_NATIVE));
	return (size);
}

/*
 * Returns allocated buffer of size *sizep.  Caller must free the buffer with
 * fnvlist_pack_free().
 */
char *
fnvlist_pack(nvlist_t *nvl, size_t *sizep)
{
	char *packed = 0;
	VERIFY3U(nvlist_pack(nvl, &packed, sizep, NV_ENCODE_NATIVE,
	    KM_SLEEP), ==, 0);
	return (packed);
}

/*ARGSUSED*/
void
fnvlist_pack_free(char *pack, size_t size)
{
#ifdef _KERNEL
	kmem_free(pack, size);
#else
	free(pack);
#endif
}

nvlist_t *
fnvlist_unpack(char *buf, size_t buflen)
{
	nvlist_t *rv;
	VERIFY0(nvlist_unpack(buf, buflen, &rv, KM_SLEEP));
	return (rv);
}

nvlist_t *
fnvlist_dup(nvlist_t *nvl)
{
	nvlist_t *rv;
	VERIFY0(nvlist_dup(nvl, &rv, KM_SLEEP));
	return (rv);
}

void
fnvlist_merge(nvlist_t *dst, nvlist_t *src)
{
	VERIFY0(nvlist_merge(dst, src, KM_SLEEP));
}

size_t
fnvlist_num_pairs(nvlist_t *nvl)
{
	size_t count = 0;
	nvpair_t *pair;

	for (pair = nvlist_next_nvpair(nvl, 0); pair != NULL;
	    pair = nvlist_next_nvpair(nvl, pair))
		count++;
	return (count);
}

void
fnvlist_add_boolean(nvlist_t *nvl, const char *name)
{
	VERIFY0(nvlist_add_boolean(nvl, name));
}

void
fnvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
{
	VERIFY0(nvlist_add_boolean_value(nvl, name, val));
}

void
fnvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
{
	VERIFY0(nvlist_add_byte(nvl, name, val));
}

void
fnvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
{
	VERIFY0(nvlist_add_int8(nvl, name, val));
}

void
fnvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
{
	VERIFY0(nvlist_add_uint8(nvl, name, val));
}

void
fnvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
{
	VERIFY0(nvlist_add_int16(nvl, name, val));
}

void
fnvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
{
	VERIFY0(nvlist_add_uint16(nvl, name, val));
}

void
fnvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
{
	VERIFY0(nvlist_add_int32(nvl, name, val));
}

void
fnvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
{
	VERIFY0(nvlist_add_uint32(nvl, name, val));
}

void
fnvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
{
	VERIFY0(nvlist_add_int64(nvl, name, val));
}

void
fnvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
{
	VERIFY0(nvlist_add_uint64(nvl, name, val));
}

void
fnvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
{
	VERIFY0(nvlist_add_string(nvl, name, val));
}

void
fnvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
{
	VERIFY0(nvlist_add_nvlist(nvl, name, val));
}

void
fnvlist_add_nvpair(nvlist_t *nvl, nvpair_t *pair)
{
	VERIFY0(nvlist_add_nvpair(nvl, pair));
}

void
fnvlist_add_boolean_array(nvlist_t *nvl, const char *name,
    boolean_t *val, uint_t n)
{
	VERIFY0(nvlist_add_boolean_array(nvl, name, val, n));
}

void
fnvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *val, uint_t n)
{
	VERIFY0(nvlist_add_byte_array(nvl, name, val, n));
}

void
fnvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *val, uint_t n)
{
	VERIFY0(nvlist_add_int8_array(nvl, name, val, n));
}

void
fnvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *val, uint_t n)
{
	VERIFY0(nvlist_add_uint8_array(nvl, name, val, n));
}

void
fnvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *val, uint_t n)
{
	VERIFY0(nvlist_add_int16_array(nvl, name, val, n));
}

void
fnvlist_add_uint16_array(nvlist_t *nvl, const char *name,
    uint16_t *val, uint_t n)
{
	VERIFY0(nvlist_add_uint16_array(nvl, name, val, n));
}

void
fnvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *val, uint_t n)
{
	VERIFY0(nvlist_add_int32_array(nvl, name, val, n));
}

void
fnvlist_add_uint32_array(nvlist_t *nvl, const char *name,
    uint32_t *val, uint_t n)
{
	VERIFY0(nvlist_add_uint32_array(nvl, name, val, n));
}

void
fnvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *val, uint_t n)
{
	VERIFY0(nvlist_add_int64_array(nvl, name, val, n));
}

void
fnvlist_add_uint64_array(nvlist_t *nvl, const char *name,
    uint64_t *val, uint_t n)
{
	VERIFY0(nvlist_add_uint64_array(nvl, name, val, n));
}

void
fnvlist_add_string_array(nvlist_t *nvl, const char *name,
    char * const *val, uint_t n)
{
	VERIFY0(nvlist_add_string_array(nvl, name, val, n));
}

void
fnvlist_add_nvlist_array(nvlist_t *nvl, const char *name,
    nvlist_t **val, uint_t n)
{
	VERIFY0(nvlist_add_nvlist_array(nvl, name, val, n));
}

void
fnvlist_remove(nvlist_t *nvl, const char *name)
{
	VERIFY0(nvlist_remove_all(nvl, name));
}

void
fnvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *pair)
{
	VERIFY0(nvlist_remove_nvpair(nvl, pair));
}

nvpair_t *
fnvlist_lookup_nvpair(nvlist_t *nvl, const char *name)
{
	nvpair_t *rv;
	VERIFY0(nvlist_lookup_nvpair(nvl, name, &rv));
	return (rv);
}

/* returns B_TRUE if the entry exists */
boolean_t
fnvlist_lookup_boolean(nvlist_t *nvl, const char *name)
{
	return (nvlist_lookup_boolean(nvl, name) == 0);
}

boolean_t
fnvlist_lookup_boolean_value(nvlist_t *nvl, const char *name)
{
	boolean_t rv;
	VERIFY0(nvlist_lookup_boolean_value(nvl, name, &rv));
	return (rv);
}

uchar_t
fnvlist_lookup_byte(nvlist_t *nvl, const char *name)
{
	uchar_t rv;
	VERIFY0(nvlist_lookup_byte(nvl, name, &rv));
	return (rv);
}

int8_t
fnvlist_lookup_int8(nvlist_t *nvl, const char *name)
{
	int8_t rv;
	VERIFY0(nvlist_lookup_int8(nvl, name, &rv));
	return (rv);
}

int16_t
fnvlist_lookup_int16(nvlist_t *nvl, const char *name)
{
	int16_t rv;
	VERIFY0(nvlist_lookup_int16(nvl, name, &rv));
	return (rv);
}

int32_t
fnvlist_lookup_int32(nvlist_t *nvl, const char *name)
{
	int32_t rv;
	VERIFY0(nvlist_lookup_int32(nvl, name, &rv));
	return (rv);
}

int64_t
fnvlist_lookup_int64(nvlist_t *nvl, const char *name)
{
	int64_t rv;
	VERIFY0(nvlist_lookup_int64(nvl, name, &rv));
	return (rv);
}

uint8_t
fnvlist_lookup_uint8(nvlist_t *nvl, const char *name)
{
	uint8_t rv;
	VERIFY0(nvlist_lookup_uint8(nvl, name, &rv));
	return (rv);
}

uint16_t
fnvlist_lookup_uint16(nvlist_t *nvl, const char *name)
{
	uint16_t rv;
	VERIFY0(nvlist_lookup_uint16(nvl, name, &rv));
	return (rv);
}

uint32_t
fnvlist_lookup_uint32(nvlist_t *nvl, const char *name)
{
	uint32_t rv;
	VERIFY0(nvlist_lookup_uint32(nvl, name, &rv));
	return (rv);
}

uint64_t
fnvlist_lookup_uint64(nvlist_t *nvl, const char *name)
{
	uint64_t rv;
	VERIFY0(nvlist_lookup_uint64(nvl, name, &rv));
	return (rv);
}

char *
fnvlist_lookup_string(nvlist_t *nvl, const char *name)
{
	char *rv;
	VERIFY0(nvlist_lookup_string(nvl, name, &rv));
	return (rv);
}

nvlist_t *
fnvlist_lookup_nvlist(nvlist_t *nvl, const char *name)
{
	nvlist_t *rv;
	VERIFY0(nvlist_lookup_nvlist(nvl, name, &rv));
	return (rv);
}

boolean_t
fnvpair_value_boolean_value(nvpair_t *nvp)
{
	boolean_t rv;
	VERIFY0(nvpair_value_boolean_value(nvp, &rv));
	return (rv);
}

uchar_t
fnvpair_value_byte(nvpair_t *nvp)
{
	uchar_t rv;
	VERIFY0(nvpair_value_byte(nvp, &rv));
	return (rv);
}

int8_t
fnvpair_value_int8(nvpair_t *nvp)
{
	int8_t rv;
	VERIFY0(nvpair_value_int8(nvp, &rv));
	return (rv);
}

int16_t
fnvpair_value_int16(nvpair_t *nvp)
{
	int16_t rv;
	VERIFY0(nvpair_value_int16(nvp, &rv));
	return (rv);
}

int32_t
fnvpair_value_int32(nvpair_t *nvp)
{
	int32_t rv;
	VERIFY0(nvpair_value_int32(nvp, &rv));
	return (rv);
}

int64_t
fnvpair_value_int64(nvpair_t *nvp)
{
	int64_t rv;
	VERIFY0(nvpair_value_int64(nvp, &rv));
	return (rv);
}

uint8_t
fnvpair_value_uint8(nvpair_t *nvp)
{
	uint8_t rv;
	VERIFY0(nvpair_value_uint8(nvp, &rv));
	return (rv);
}

uint16_t
fnvpair_value_uint16(nvpair_t *nvp)
{
	uint16_t rv;
	VERIFY0(nvpair_value_uint16(nvp, &rv));
	return (rv);
}

uint32_t
fnvpair_value_uint32(nvpair_t *nvp)
{
	uint32_t rv;
	VERIFY0(nvpair_value_uint32(nvp, &rv));
	return (rv);
}

uint64_t
fnvpair_value_uint64(nvpair_t *nvp)
{
	uint64_t rv;
	VERIFY0(nvpair_value_uint64(nvp, &rv));
	return (rv);
}

char *
fnvpair_value_string(nvpair_t *nvp)
{
	char *rv;
	VERIFY0(nvpair_value_string(nvp, &rv));
	return (rv);
}

nvlist_t *
fnvpair_value_nvlist(nvpair_t *nvp)
{
	nvlist_t *rv;
	VERIFY0(nvpair_value_nvlist(nvp, &rv));
	return (rv);
}

#if defined(_KERNEL)

EXPORT_SYMBOL(fnvlist_alloc);
EXPORT_SYMBOL(fnvlist_free);
EXPORT_SYMBOL(fnvlist_size);
EXPORT_SYMBOL(fnvlist_pack);
EXPORT_SYMBOL(fnvlist_pack_free);
EXPORT_SYMBOL(fnvlist_unpack);
EXPORT_SYMBOL(fnvlist_dup);
EXPORT_SYMBOL(fnvlist_merge);

EXPORT_SYMBOL(fnvlist_add_nvpair);
EXPORT_SYMBOL(fnvlist_add_boolean);
EXPORT_SYMBOL(fnvlist_add_boolean_value);
EXPORT_SYMBOL(fnvlist_add_byte);
EXPORT_SYMBOL(fnvlist_add_int8);
EXPORT_SYMBOL(fnvlist_add_uint8);
EXPORT_SYMBOL(fnvlist_add_int16);
EXPORT_SYMBOL(fnvlist_add_uint16);
EXPORT_SYMBOL(fnvlist_add_int32);
EXPORT_SYMBOL(fnvlist_add_uint32);
EXPORT_SYMBOL(fnvlist_add_int64);
EXPORT_SYMBOL(fnvlist_add_uint64);
EXPORT_SYMBOL(fnvlist_add_string);
EXPORT_SYMBOL(fnvlist_add_nvlist);
EXPORT_SYMBOL(fnvlist_add_boolean_array);
EXPORT_SYMBOL(fnvlist_add_byte_array);
EXPORT_SYMBOL(fnvlist_add_int8_array);
EXPORT_SYMBOL(fnvlist_add_uint8_array);
EXPORT_SYMBOL(fnvlist_add_int16_array);
EXPORT_SYMBOL(fnvlist_add_uint16_array);
EXPORT_SYMBOL(fnvlist_add_int32_array);
EXPORT_SYMBOL(fnvlist_add_uint32_array);
EXPORT_SYMBOL(fnvlist_add_int64_array);
EXPORT_SYMBOL(fnvlist_add_uint64_array);
EXPORT_SYMBOL(fnvlist_add_string_array);
EXPORT_SYMBOL(fnvlist_add_nvlist_array);

EXPORT_SYMBOL(fnvlist_remove);
EXPORT_SYMBOL(fnvlist_remove_nvpair);

EXPORT_SYMBOL(fnvlist_lookup_nvpair);
EXPORT_SYMBOL(fnvlist_lookup_boolean);
EXPORT_SYMBOL(fnvlist_lookup_boolean_value);
EXPORT_SYMBOL(fnvlist_lookup_byte);
EXPORT_SYMBOL(fnvlist_lookup_int8);
EXPORT_SYMBOL(fnvlist_lookup_uint8);
EXPORT_SYMBOL(fnvlist_lookup_int16);
EXPORT_SYMBOL(fnvlist_lookup_uint16);
EXPORT_SYMBOL(fnvlist_lookup_int32);
EXPORT_SYMBOL(fnvlist_lookup_uint32);
EXPORT_SYMBOL(fnvlist_lookup_int64);
EXPORT_SYMBOL(fnvlist_lookup_uint64);
EXPORT_SYMBOL(fnvlist_lookup_string);
EXPORT_SYMBOL(fnvlist_lookup_nvlist);

EXPORT_SYMBOL(fnvpair_value_boolean_value);
EXPORT_SYMBOL(fnvpair_value_byte);
EXPORT_SYMBOL(fnvpair_value_int8);
EXPORT_SYMBOL(fnvpair_value_uint8);
EXPORT_SYMBOL(fnvpair_value_int16);
EXPORT_SYMBOL(fnvpair_value_uint16);
EXPORT_SYMBOL(fnvpair_value_int32);
EXPORT_SYMBOL(fnvpair_value_uint32);
EXPORT_SYMBOL(fnvpair_value_int64);
EXPORT_SYMBOL(fnvpair_value_uint64);
EXPORT_SYMBOL(fnvpair_value_string);
EXPORT_SYMBOL(fnvpair_value_nvlist);
EXPORT_SYMBOL(fnvlist_num_pairs);

#endif