]> git.proxmox.com Git - mirror_zfs-debian.git/blob - module/nvpair/nvpair.c
Imported Upstream version 0.6.4.2
[mirror_zfs-debian.git] / module / nvpair / nvpair.c
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) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25
26 #include <sys/stropts.h>
27 #include <sys/debug.h>
28 #include <sys/isa_defs.h>
29 #include <sys/int_limits.h>
30 #include <sys/nvpair.h>
31 #include <sys/nvpair_impl.h>
32 #include <rpc/types.h>
33 #include <rpc/xdr.h>
34
35 #if defined(_KERNEL) && !defined(_BOOT)
36 #include <sys/varargs.h>
37 #include <sys/ddi.h>
38 #include <sys/sunddi.h>
39 #else
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <strings.h>
44 #endif
45
46 #ifndef offsetof
47 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
48 #endif
49 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
50
51 /*
52 * nvpair.c - Provides kernel & userland interfaces for manipulating
53 * name-value pairs.
54 *
55 * Overview Diagram
56 *
57 * +--------------+
58 * | nvlist_t |
59 * |--------------|
60 * | nvl_version |
61 * | nvl_nvflag |
62 * | nvl_priv -+-+
63 * | nvl_flag | |
64 * | nvl_pad | |
65 * +--------------+ |
66 * V
67 * +--------------+ last i_nvp in list
68 * | nvpriv_t | +--------------------->
69 * |--------------| |
70 * +--+- nvp_list | | +------------+
71 * | | nvp_last -+--+ + nv_alloc_t |
72 * | | nvp_curr | |------------|
73 * | | nvp_nva -+----> | nva_ops |
74 * | | nvp_stat | | nva_arg |
75 * | +--------------+ +------------+
76 * |
77 * +-------+
78 * V
79 * +---------------------+ +-------------------+
80 * | i_nvp_t | +-->| i_nvp_t | +-->
81 * |---------------------| | |-------------------| |
82 * | nvi_next -+--+ | nvi_next -+--+
83 * | nvi_prev (NULL) | <----+ nvi_prev |
84 * | . . . . . . . . . . | | . . . . . . . . . |
85 * | nvp (nvpair_t) | | nvp (nvpair_t) |
86 * | - nvp_size | | - nvp_size |
87 * | - nvp_name_sz | | - nvp_name_sz |
88 * | - nvp_value_elem | | - nvp_value_elem |
89 * | - nvp_type | | - nvp_type |
90 * | - data ... | | - data ... |
91 * +---------------------+ +-------------------+
92 *
93 *
94 *
95 * +---------------------+ +---------------------+
96 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
97 * |---------------------| | | |---------------------|
98 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
99 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
100 * | . . . . . . . . . | | . . . . . . . . . |
101 * | nvp (nvpair_t) | | nvp (nvpair_t) |
102 * | - nvp_size | | - nvp_size |
103 * | - nvp_name_sz | | - nvp_name_sz |
104 * | - nvp_value_elem | | - nvp_value_elem |
105 * | - DATA_TYPE_NVLIST | | - nvp_type |
106 * | - data (embedded) | | - data ... |
107 * | nvlist name | +---------------------+
108 * | +--------------+ |
109 * | | nvlist_t | |
110 * | |--------------| |
111 * | | nvl_version | |
112 * | | nvl_nvflag | |
113 * | | nvl_priv --+---+---->
114 * | | nvl_flag | |
115 * | | nvl_pad | |
116 * | +--------------+ |
117 * +---------------------+
118 *
119 *
120 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
121 * allow value to be aligned on 8 byte boundary
122 *
123 * name_len is the length of the name string including the null terminator
124 * so it must be >= 1
125 */
126 #define NVP_SIZE_CALC(name_len, data_len) \
127 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
128
129 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
130 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
131 uint_t nelem, const void *data);
132
133 #define NV_STAT_EMBEDDED 0x1
134 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
135 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
136
137 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
138 #define NVPAIR2I_NVP(nvp) \
139 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
140
141
142 int
143 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
144 {
145 va_list valist;
146 int err = 0;
147
148 nva->nva_ops = nvo;
149 nva->nva_arg = NULL;
150
151 va_start(valist, nvo);
152 if (nva->nva_ops->nv_ao_init != NULL)
153 err = nva->nva_ops->nv_ao_init(nva, valist);
154 va_end(valist);
155
156 return (err);
157 }
158
159 void
160 nv_alloc_reset(nv_alloc_t *nva)
161 {
162 if (nva->nva_ops->nv_ao_reset != NULL)
163 nva->nva_ops->nv_ao_reset(nva);
164 }
165
166 void
167 nv_alloc_fini(nv_alloc_t *nva)
168 {
169 if (nva->nva_ops->nv_ao_fini != NULL)
170 nva->nva_ops->nv_ao_fini(nva);
171 }
172
173 nv_alloc_t *
174 nvlist_lookup_nv_alloc(nvlist_t *nvl)
175 {
176 nvpriv_t *priv;
177
178 if (nvl == NULL ||
179 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
180 return (NULL);
181
182 return (priv->nvp_nva);
183 }
184
185 static void *
186 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
187 {
188 nv_alloc_t *nva = nvp->nvp_nva;
189 void *buf;
190
191 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
192 bzero(buf, size);
193
194 return (buf);
195 }
196
197 static void
198 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
199 {
200 nv_alloc_t *nva = nvp->nvp_nva;
201
202 nva->nva_ops->nv_ao_free(nva, buf, size);
203 }
204
205 static void
206 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
207 {
208 bzero(priv, sizeof (nvpriv_t));
209
210 priv->nvp_nva = nva;
211 priv->nvp_stat = stat;
212 }
213
214 static nvpriv_t *
215 nv_priv_alloc(nv_alloc_t *nva)
216 {
217 nvpriv_t *priv;
218
219 /*
220 * nv_mem_alloc() cannot called here because it needs the priv
221 * argument.
222 */
223 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
224 return (NULL);
225
226 nv_priv_init(priv, nva, 0);
227
228 return (priv);
229 }
230
231 /*
232 * Embedded lists need their own nvpriv_t's. We create a new
233 * nvpriv_t using the parameters and allocator from the parent
234 * list's nvpriv_t.
235 */
236 static nvpriv_t *
237 nv_priv_alloc_embedded(nvpriv_t *priv)
238 {
239 nvpriv_t *emb_priv;
240
241 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
242 return (NULL);
243
244 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
245
246 return (emb_priv);
247 }
248
249 static void
250 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
251 {
252 nvl->nvl_version = NV_VERSION;
253 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
254 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
255 nvl->nvl_flag = 0;
256 nvl->nvl_pad = 0;
257 }
258
259 uint_t
260 nvlist_nvflag(nvlist_t *nvl)
261 {
262 return (nvl->nvl_nvflag);
263 }
264
265 static nv_alloc_t *
266 nvlist_nv_alloc(int kmflag)
267 {
268 #if defined(_KERNEL) && !defined(_BOOT)
269 switch (kmflag) {
270 case KM_SLEEP:
271 return (nv_alloc_sleep);
272 case KM_PUSHPAGE:
273 return (nv_alloc_pushpage);
274 default:
275 return (nv_alloc_nosleep);
276 }
277 #else
278 return (nv_alloc_nosleep);
279 #endif /* _KERNEL && !_BOOT */
280 }
281
282 /*
283 * nvlist_alloc - Allocate nvlist.
284 */
285 int
286 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
287 {
288 return (nvlist_xalloc(nvlp, nvflag, nvlist_nv_alloc(kmflag)));
289 }
290
291 int
292 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
293 {
294 nvpriv_t *priv;
295
296 if (nvlp == NULL || nva == NULL)
297 return (EINVAL);
298
299 if ((priv = nv_priv_alloc(nva)) == NULL)
300 return (ENOMEM);
301
302 if ((*nvlp = nv_mem_zalloc(priv,
303 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
304 nv_mem_free(priv, priv, sizeof (nvpriv_t));
305 return (ENOMEM);
306 }
307
308 nvlist_init(*nvlp, nvflag, priv);
309
310 return (0);
311 }
312
313 /*
314 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
315 */
316 static nvpair_t *
317 nvp_buf_alloc(nvlist_t *nvl, size_t len)
318 {
319 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
320 i_nvp_t *buf;
321 nvpair_t *nvp;
322 size_t nvsize;
323
324 /*
325 * Allocate the buffer
326 */
327 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
328
329 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
330 return (NULL);
331
332 nvp = &buf->nvi_nvp;
333 nvp->nvp_size = len;
334
335 return (nvp);
336 }
337
338 /*
339 * nvp_buf_free - de-Allocate an i_nvp_t.
340 */
341 static void
342 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
343 {
344 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
345 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
346
347 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
348 }
349
350 /*
351 * nvp_buf_link - link a new nv pair into the nvlist.
352 */
353 static void
354 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
355 {
356 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
357 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
358
359 /* Put element at end of nvlist */
360 if (priv->nvp_list == NULL) {
361 priv->nvp_list = priv->nvp_last = curr;
362 } else {
363 curr->nvi_prev = priv->nvp_last;
364 priv->nvp_last->nvi_next = curr;
365 priv->nvp_last = curr;
366 }
367 }
368
369 /*
370 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
371 */
372 static void
373 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
374 {
375 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
376 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
377
378 /*
379 * protect nvlist_next_nvpair() against walking on freed memory.
380 */
381 if (priv->nvp_curr == curr)
382 priv->nvp_curr = curr->nvi_next;
383
384 if (curr == priv->nvp_list)
385 priv->nvp_list = curr->nvi_next;
386 else
387 curr->nvi_prev->nvi_next = curr->nvi_next;
388
389 if (curr == priv->nvp_last)
390 priv->nvp_last = curr->nvi_prev;
391 else
392 curr->nvi_next->nvi_prev = curr->nvi_prev;
393 }
394
395 /*
396 * take a nvpair type and number of elements and make sure the are valid
397 */
398 static int
399 i_validate_type_nelem(data_type_t type, uint_t nelem)
400 {
401 switch (type) {
402 case DATA_TYPE_BOOLEAN:
403 if (nelem != 0)
404 return (EINVAL);
405 break;
406 case DATA_TYPE_BOOLEAN_VALUE:
407 case DATA_TYPE_BYTE:
408 case DATA_TYPE_INT8:
409 case DATA_TYPE_UINT8:
410 case DATA_TYPE_INT16:
411 case DATA_TYPE_UINT16:
412 case DATA_TYPE_INT32:
413 case DATA_TYPE_UINT32:
414 case DATA_TYPE_INT64:
415 case DATA_TYPE_UINT64:
416 case DATA_TYPE_STRING:
417 case DATA_TYPE_HRTIME:
418 case DATA_TYPE_NVLIST:
419 #if !defined(_KERNEL)
420 case DATA_TYPE_DOUBLE:
421 #endif
422 if (nelem != 1)
423 return (EINVAL);
424 break;
425 case DATA_TYPE_BOOLEAN_ARRAY:
426 case DATA_TYPE_BYTE_ARRAY:
427 case DATA_TYPE_INT8_ARRAY:
428 case DATA_TYPE_UINT8_ARRAY:
429 case DATA_TYPE_INT16_ARRAY:
430 case DATA_TYPE_UINT16_ARRAY:
431 case DATA_TYPE_INT32_ARRAY:
432 case DATA_TYPE_UINT32_ARRAY:
433 case DATA_TYPE_INT64_ARRAY:
434 case DATA_TYPE_UINT64_ARRAY:
435 case DATA_TYPE_STRING_ARRAY:
436 case DATA_TYPE_NVLIST_ARRAY:
437 /* we allow arrays with 0 elements */
438 break;
439 default:
440 return (EINVAL);
441 }
442 return (0);
443 }
444
445 /*
446 * Verify nvp_name_sz and check the name string length.
447 */
448 static int
449 i_validate_nvpair_name(nvpair_t *nvp)
450 {
451 if ((nvp->nvp_name_sz <= 0) ||
452 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
453 return (EFAULT);
454
455 /* verify the name string, make sure its terminated */
456 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
457 return (EFAULT);
458
459 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
460 }
461
462 static int
463 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
464 {
465 switch (type) {
466 case DATA_TYPE_BOOLEAN_VALUE:
467 if (*(boolean_t *)data != B_TRUE &&
468 *(boolean_t *)data != B_FALSE)
469 return (EINVAL);
470 break;
471 case DATA_TYPE_BOOLEAN_ARRAY: {
472 int i;
473
474 for (i = 0; i < nelem; i++)
475 if (((boolean_t *)data)[i] != B_TRUE &&
476 ((boolean_t *)data)[i] != B_FALSE)
477 return (EINVAL);
478 break;
479 }
480 default:
481 break;
482 }
483
484 return (0);
485 }
486
487 /*
488 * This function takes a pointer to what should be a nvpair and it's size
489 * and then verifies that all the nvpair fields make sense and can be
490 * trusted. This function is used when decoding packed nvpairs.
491 */
492 static int
493 i_validate_nvpair(nvpair_t *nvp)
494 {
495 data_type_t type = NVP_TYPE(nvp);
496 int size1, size2;
497
498 /* verify nvp_name_sz, check the name string length */
499 if (i_validate_nvpair_name(nvp) != 0)
500 return (EFAULT);
501
502 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
503 return (EFAULT);
504
505 /*
506 * verify nvp_type, nvp_value_elem, and also possibly
507 * verify string values and get the value size.
508 */
509 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
510 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
511 if (size2 < 0 || size1 != NV_ALIGN(size2))
512 return (EFAULT);
513
514 return (0);
515 }
516
517 static int
518 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
519 {
520 nvpriv_t *priv;
521 i_nvp_t *curr;
522
523 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
524 return (EINVAL);
525
526 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
527 nvpair_t *nvp = &curr->nvi_nvp;
528 int err;
529
530 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
531 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
532 return (err);
533 }
534
535 return (0);
536 }
537
538 /*
539 * Frees all memory allocated for an nvpair (like embedded lists) with
540 * the exception of the nvpair buffer itself.
541 */
542 static void
543 nvpair_free(nvpair_t *nvp)
544 {
545 switch (NVP_TYPE(nvp)) {
546 case DATA_TYPE_NVLIST:
547 nvlist_free(EMBEDDED_NVL(nvp));
548 break;
549 case DATA_TYPE_NVLIST_ARRAY: {
550 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
551 int i;
552
553 for (i = 0; i < NVP_NELEM(nvp); i++)
554 if (nvlp[i] != NULL)
555 nvlist_free(nvlp[i]);
556 break;
557 }
558 default:
559 break;
560 }
561 }
562
563 /*
564 * nvlist_free - free an unpacked nvlist
565 */
566 void
567 nvlist_free(nvlist_t *nvl)
568 {
569 nvpriv_t *priv;
570 i_nvp_t *curr;
571
572 if (nvl == NULL ||
573 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
574 return;
575
576 /*
577 * Unpacked nvlist are linked through i_nvp_t
578 */
579 curr = priv->nvp_list;
580 while (curr != NULL) {
581 nvpair_t *nvp = &curr->nvi_nvp;
582 curr = curr->nvi_next;
583
584 nvpair_free(nvp);
585 nvp_buf_free(nvl, nvp);
586 }
587
588 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
589 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
590 else
591 nvl->nvl_priv = 0;
592
593 nv_mem_free(priv, priv, sizeof (nvpriv_t));
594 }
595
596 static int
597 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
598 {
599 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
600 i_nvp_t *curr;
601
602 if (nvp == NULL)
603 return (0);
604
605 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
606 if (&curr->nvi_nvp == nvp)
607 return (1);
608
609 return (0);
610 }
611
612 /*
613 * Make a copy of nvlist
614 */
615 int
616 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
617 {
618 return (nvlist_xdup(nvl, nvlp, nvlist_nv_alloc(kmflag)));
619 }
620
621 int
622 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
623 {
624 int err;
625 nvlist_t *ret;
626
627 if (nvl == NULL || nvlp == NULL)
628 return (EINVAL);
629
630 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
631 return (err);
632
633 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
634 nvlist_free(ret);
635 else
636 *nvlp = ret;
637
638 return (err);
639 }
640
641 /*
642 * Remove all with matching name
643 */
644 int
645 nvlist_remove_all(nvlist_t *nvl, const char *name)
646 {
647 nvpriv_t *priv;
648 i_nvp_t *curr;
649 int error = ENOENT;
650
651 if (nvl == NULL || name == NULL ||
652 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
653 return (EINVAL);
654
655 curr = priv->nvp_list;
656 while (curr != NULL) {
657 nvpair_t *nvp = &curr->nvi_nvp;
658
659 curr = curr->nvi_next;
660 if (strcmp(name, NVP_NAME(nvp)) != 0)
661 continue;
662
663 nvp_buf_unlink(nvl, nvp);
664 nvpair_free(nvp);
665 nvp_buf_free(nvl, nvp);
666
667 error = 0;
668 }
669
670 return (error);
671 }
672
673 /*
674 * Remove first one with matching name and type
675 */
676 int
677 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
678 {
679 nvpriv_t *priv;
680 i_nvp_t *curr;
681
682 if (nvl == NULL || name == NULL ||
683 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
684 return (EINVAL);
685
686 curr = priv->nvp_list;
687 while (curr != NULL) {
688 nvpair_t *nvp = &curr->nvi_nvp;
689
690 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
691 nvp_buf_unlink(nvl, nvp);
692 nvpair_free(nvp);
693 nvp_buf_free(nvl, nvp);
694
695 return (0);
696 }
697 curr = curr->nvi_next;
698 }
699
700 return (ENOENT);
701 }
702
703 int
704 nvlist_remove_nvpair(nvlist_t *nvl, nvpair_t *nvp)
705 {
706 if (nvl == NULL || nvp == NULL)
707 return (EINVAL);
708
709 nvp_buf_unlink(nvl, nvp);
710 nvpair_free(nvp);
711 nvp_buf_free(nvl, nvp);
712 return (0);
713 }
714
715 /*
716 * This function calculates the size of an nvpair value.
717 *
718 * The data argument controls the behavior in case of the data types
719 * DATA_TYPE_STRING and
720 * DATA_TYPE_STRING_ARRAY
721 * Is data == NULL then the size of the string(s) is excluded.
722 */
723 static int
724 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
725 {
726 uint64_t value_sz;
727
728 if (i_validate_type_nelem(type, nelem) != 0)
729 return (-1);
730
731 /* Calculate required size for holding value */
732 switch (type) {
733 case DATA_TYPE_BOOLEAN:
734 value_sz = 0;
735 break;
736 case DATA_TYPE_BOOLEAN_VALUE:
737 value_sz = sizeof (boolean_t);
738 break;
739 case DATA_TYPE_BYTE:
740 value_sz = sizeof (uchar_t);
741 break;
742 case DATA_TYPE_INT8:
743 value_sz = sizeof (int8_t);
744 break;
745 case DATA_TYPE_UINT8:
746 value_sz = sizeof (uint8_t);
747 break;
748 case DATA_TYPE_INT16:
749 value_sz = sizeof (int16_t);
750 break;
751 case DATA_TYPE_UINT16:
752 value_sz = sizeof (uint16_t);
753 break;
754 case DATA_TYPE_INT32:
755 value_sz = sizeof (int32_t);
756 break;
757 case DATA_TYPE_UINT32:
758 value_sz = sizeof (uint32_t);
759 break;
760 case DATA_TYPE_INT64:
761 value_sz = sizeof (int64_t);
762 break;
763 case DATA_TYPE_UINT64:
764 value_sz = sizeof (uint64_t);
765 break;
766 #if !defined(_KERNEL)
767 case DATA_TYPE_DOUBLE:
768 value_sz = sizeof (double);
769 break;
770 #endif
771 case DATA_TYPE_STRING:
772 if (data == NULL)
773 value_sz = 0;
774 else
775 value_sz = strlen(data) + 1;
776 break;
777 case DATA_TYPE_BOOLEAN_ARRAY:
778 value_sz = (uint64_t)nelem * sizeof (boolean_t);
779 break;
780 case DATA_TYPE_BYTE_ARRAY:
781 value_sz = (uint64_t)nelem * sizeof (uchar_t);
782 break;
783 case DATA_TYPE_INT8_ARRAY:
784 value_sz = (uint64_t)nelem * sizeof (int8_t);
785 break;
786 case DATA_TYPE_UINT8_ARRAY:
787 value_sz = (uint64_t)nelem * sizeof (uint8_t);
788 break;
789 case DATA_TYPE_INT16_ARRAY:
790 value_sz = (uint64_t)nelem * sizeof (int16_t);
791 break;
792 case DATA_TYPE_UINT16_ARRAY:
793 value_sz = (uint64_t)nelem * sizeof (uint16_t);
794 break;
795 case DATA_TYPE_INT32_ARRAY:
796 value_sz = (uint64_t)nelem * sizeof (int32_t);
797 break;
798 case DATA_TYPE_UINT32_ARRAY:
799 value_sz = (uint64_t)nelem * sizeof (uint32_t);
800 break;
801 case DATA_TYPE_INT64_ARRAY:
802 value_sz = (uint64_t)nelem * sizeof (int64_t);
803 break;
804 case DATA_TYPE_UINT64_ARRAY:
805 value_sz = (uint64_t)nelem * sizeof (uint64_t);
806 break;
807 case DATA_TYPE_STRING_ARRAY:
808 value_sz = (uint64_t)nelem * sizeof (uint64_t);
809
810 if (data != NULL) {
811 char *const *strs = data;
812 uint_t i;
813
814 /* no alignment requirement for strings */
815 for (i = 0; i < nelem; i++) {
816 if (strs[i] == NULL)
817 return (-1);
818 value_sz += strlen(strs[i]) + 1;
819 }
820 }
821 break;
822 case DATA_TYPE_HRTIME:
823 value_sz = sizeof (hrtime_t);
824 break;
825 case DATA_TYPE_NVLIST:
826 value_sz = NV_ALIGN(sizeof (nvlist_t));
827 break;
828 case DATA_TYPE_NVLIST_ARRAY:
829 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
830 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
831 break;
832 default:
833 return (-1);
834 }
835
836 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
837 }
838
839 static int
840 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
841 {
842 nvpriv_t *priv;
843 int err;
844
845 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
846 nvl->nvl_priv)) == NULL)
847 return (ENOMEM);
848
849 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
850
851 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
852 nvlist_free(emb_nvl);
853 emb_nvl->nvl_priv = 0;
854 }
855
856 return (err);
857 }
858
859 /*
860 * nvlist_add_common - Add new <name,value> pair to nvlist
861 */
862 static int
863 nvlist_add_common(nvlist_t *nvl, const char *name,
864 data_type_t type, uint_t nelem, const void *data)
865 {
866 nvpair_t *nvp;
867 uint_t i;
868
869 int nvp_sz, name_sz, value_sz;
870 int err = 0;
871
872 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
873 return (EINVAL);
874
875 if (nelem != 0 && data == NULL)
876 return (EINVAL);
877
878 /*
879 * Verify type and nelem and get the value size.
880 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
881 * is the size of the string(s) included.
882 */
883 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
884 return (EINVAL);
885
886 if (i_validate_nvpair_value(type, nelem, data) != 0)
887 return (EINVAL);
888
889 /*
890 * If we're adding an nvlist or nvlist array, ensure that we are not
891 * adding the input nvlist to itself, which would cause recursion,
892 * and ensure that no NULL nvlist pointers are present.
893 */
894 switch (type) {
895 case DATA_TYPE_NVLIST:
896 if (data == nvl || data == NULL)
897 return (EINVAL);
898 break;
899 case DATA_TYPE_NVLIST_ARRAY: {
900 nvlist_t **onvlp = (nvlist_t **)data;
901 for (i = 0; i < nelem; i++) {
902 if (onvlp[i] == nvl || onvlp[i] == NULL)
903 return (EINVAL);
904 }
905 break;
906 }
907 default:
908 break;
909 }
910
911 /* calculate sizes of the nvpair elements and the nvpair itself */
912 name_sz = strlen(name) + 1;
913
914 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
915
916 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
917 return (ENOMEM);
918
919 ASSERT(nvp->nvp_size == nvp_sz);
920 nvp->nvp_name_sz = name_sz;
921 nvp->nvp_value_elem = nelem;
922 nvp->nvp_type = type;
923 bcopy(name, NVP_NAME(nvp), name_sz);
924
925 switch (type) {
926 case DATA_TYPE_BOOLEAN:
927 break;
928 case DATA_TYPE_STRING_ARRAY: {
929 char *const *strs = data;
930 char *buf = NVP_VALUE(nvp);
931 char **cstrs = (void *)buf;
932
933 /* skip pre-allocated space for pointer array */
934 buf += nelem * sizeof (uint64_t);
935 for (i = 0; i < nelem; i++) {
936 int slen = strlen(strs[i]) + 1;
937 bcopy(strs[i], buf, slen);
938 cstrs[i] = buf;
939 buf += slen;
940 }
941 break;
942 }
943 case DATA_TYPE_NVLIST: {
944 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
945 nvlist_t *onvl = (nvlist_t *)data;
946
947 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
948 nvp_buf_free(nvl, nvp);
949 return (err);
950 }
951 break;
952 }
953 case DATA_TYPE_NVLIST_ARRAY: {
954 nvlist_t **onvlp = (nvlist_t **)data;
955 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
956 nvlist_t *embedded = (nvlist_t *)
957 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
958
959 for (i = 0; i < nelem; i++) {
960 if ((err = nvlist_copy_embedded(nvl,
961 onvlp[i], embedded)) != 0) {
962 /*
963 * Free any successfully created lists
964 */
965 nvpair_free(nvp);
966 nvp_buf_free(nvl, nvp);
967 return (err);
968 }
969
970 nvlp[i] = embedded++;
971 }
972 break;
973 }
974 default:
975 bcopy(data, NVP_VALUE(nvp), value_sz);
976 }
977
978 /* if unique name, remove before add */
979 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
980 (void) nvlist_remove_all(nvl, name);
981 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
982 (void) nvlist_remove(nvl, name, type);
983
984 nvp_buf_link(nvl, nvp);
985
986 return (0);
987 }
988
989 int
990 nvlist_add_boolean(nvlist_t *nvl, const char *name)
991 {
992 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
993 }
994
995 int
996 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
997 {
998 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
999 }
1000
1001 int
1002 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
1003 {
1004 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
1005 }
1006
1007 int
1008 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
1009 {
1010 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
1011 }
1012
1013 int
1014 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
1015 {
1016 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1017 }
1018
1019 int
1020 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1021 {
1022 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1023 }
1024
1025 int
1026 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1027 {
1028 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1029 }
1030
1031 int
1032 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1033 {
1034 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1035 }
1036
1037 int
1038 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1039 {
1040 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1041 }
1042
1043 int
1044 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1045 {
1046 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1047 }
1048
1049 int
1050 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1051 {
1052 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1053 }
1054
1055 #if !defined(_KERNEL)
1056 int
1057 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1058 {
1059 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1060 }
1061 #endif
1062
1063 int
1064 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1065 {
1066 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1067 }
1068
1069 int
1070 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1071 boolean_t *a, uint_t n)
1072 {
1073 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1074 }
1075
1076 int
1077 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1078 {
1079 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1080 }
1081
1082 int
1083 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1084 {
1085 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1086 }
1087
1088 int
1089 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1090 {
1091 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1092 }
1093
1094 int
1095 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1096 {
1097 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1098 }
1099
1100 int
1101 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1102 {
1103 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1104 }
1105
1106 int
1107 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1108 {
1109 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1110 }
1111
1112 int
1113 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1114 {
1115 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1116 }
1117
1118 int
1119 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1120 {
1121 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1122 }
1123
1124 int
1125 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1126 {
1127 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1128 }
1129
1130 int
1131 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1132 char *const *a, uint_t n)
1133 {
1134 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1135 }
1136
1137 int
1138 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1139 {
1140 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1141 }
1142
1143 int
1144 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1145 {
1146 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1147 }
1148
1149 int
1150 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1151 {
1152 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1153 }
1154
1155 /* reading name-value pairs */
1156 nvpair_t *
1157 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1158 {
1159 nvpriv_t *priv;
1160 i_nvp_t *curr;
1161
1162 if (nvl == NULL ||
1163 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1164 return (NULL);
1165
1166 curr = NVPAIR2I_NVP(nvp);
1167
1168 /*
1169 * Ensure that nvp is a valid nvpair on this nvlist.
1170 * NB: nvp_curr is used only as a hint so that we don't always
1171 * have to walk the list to determine if nvp is still on the list.
1172 */
1173 if (nvp == NULL)
1174 curr = priv->nvp_list;
1175 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1176 curr = curr->nvi_next;
1177 else
1178 curr = NULL;
1179
1180 priv->nvp_curr = curr;
1181
1182 return (curr != NULL ? &curr->nvi_nvp : NULL);
1183 }
1184
1185 nvpair_t *
1186 nvlist_prev_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1187 {
1188 nvpriv_t *priv;
1189 i_nvp_t *curr;
1190
1191 if (nvl == NULL ||
1192 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1193 return (NULL);
1194
1195 curr = NVPAIR2I_NVP(nvp);
1196
1197 if (nvp == NULL)
1198 curr = priv->nvp_last;
1199 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1200 curr = curr->nvi_prev;
1201 else
1202 curr = NULL;
1203
1204 priv->nvp_curr = curr;
1205
1206 return (curr != NULL ? &curr->nvi_nvp : NULL);
1207 }
1208
1209 boolean_t
1210 nvlist_empty(nvlist_t *nvl)
1211 {
1212 nvpriv_t *priv;
1213
1214 if (nvl == NULL ||
1215 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1216 return (B_TRUE);
1217
1218 return (priv->nvp_list == NULL);
1219 }
1220
1221 char *
1222 nvpair_name(nvpair_t *nvp)
1223 {
1224 return (NVP_NAME(nvp));
1225 }
1226
1227 data_type_t
1228 nvpair_type(nvpair_t *nvp)
1229 {
1230 return (NVP_TYPE(nvp));
1231 }
1232
1233 int
1234 nvpair_type_is_array(nvpair_t *nvp)
1235 {
1236 data_type_t type = NVP_TYPE(nvp);
1237
1238 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1239 (type == DATA_TYPE_UINT8_ARRAY) ||
1240 (type == DATA_TYPE_INT16_ARRAY) ||
1241 (type == DATA_TYPE_UINT16_ARRAY) ||
1242 (type == DATA_TYPE_INT32_ARRAY) ||
1243 (type == DATA_TYPE_UINT32_ARRAY) ||
1244 (type == DATA_TYPE_INT64_ARRAY) ||
1245 (type == DATA_TYPE_UINT64_ARRAY) ||
1246 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1247 (type == DATA_TYPE_STRING_ARRAY) ||
1248 (type == DATA_TYPE_NVLIST_ARRAY))
1249 return (1);
1250 return (0);
1251
1252 }
1253
1254 static int
1255 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1256 {
1257 if (nvp == NULL || nvpair_type(nvp) != type)
1258 return (EINVAL);
1259
1260 /*
1261 * For non-array types, we copy the data.
1262 * For array types (including string), we set a pointer.
1263 */
1264 switch (type) {
1265 case DATA_TYPE_BOOLEAN:
1266 if (nelem != NULL)
1267 *nelem = 0;
1268 break;
1269
1270 case DATA_TYPE_BOOLEAN_VALUE:
1271 case DATA_TYPE_BYTE:
1272 case DATA_TYPE_INT8:
1273 case DATA_TYPE_UINT8:
1274 case DATA_TYPE_INT16:
1275 case DATA_TYPE_UINT16:
1276 case DATA_TYPE_INT32:
1277 case DATA_TYPE_UINT32:
1278 case DATA_TYPE_INT64:
1279 case DATA_TYPE_UINT64:
1280 case DATA_TYPE_HRTIME:
1281 #if !defined(_KERNEL)
1282 case DATA_TYPE_DOUBLE:
1283 #endif
1284 if (data == NULL)
1285 return (EINVAL);
1286 bcopy(NVP_VALUE(nvp), data,
1287 (size_t)i_get_value_size(type, NULL, 1));
1288 if (nelem != NULL)
1289 *nelem = 1;
1290 break;
1291
1292 case DATA_TYPE_NVLIST:
1293 case DATA_TYPE_STRING:
1294 if (data == NULL)
1295 return (EINVAL);
1296 *(void **)data = (void *)NVP_VALUE(nvp);
1297 if (nelem != NULL)
1298 *nelem = 1;
1299 break;
1300
1301 case DATA_TYPE_BOOLEAN_ARRAY:
1302 case DATA_TYPE_BYTE_ARRAY:
1303 case DATA_TYPE_INT8_ARRAY:
1304 case DATA_TYPE_UINT8_ARRAY:
1305 case DATA_TYPE_INT16_ARRAY:
1306 case DATA_TYPE_UINT16_ARRAY:
1307 case DATA_TYPE_INT32_ARRAY:
1308 case DATA_TYPE_UINT32_ARRAY:
1309 case DATA_TYPE_INT64_ARRAY:
1310 case DATA_TYPE_UINT64_ARRAY:
1311 case DATA_TYPE_STRING_ARRAY:
1312 case DATA_TYPE_NVLIST_ARRAY:
1313 if (nelem == NULL || data == NULL)
1314 return (EINVAL);
1315 if ((*nelem = NVP_NELEM(nvp)) != 0)
1316 *(void **)data = (void *)NVP_VALUE(nvp);
1317 else
1318 *(void **)data = NULL;
1319 break;
1320
1321 default:
1322 return (ENOTSUP);
1323 }
1324
1325 return (0);
1326 }
1327
1328 static int
1329 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1330 uint_t *nelem, void *data)
1331 {
1332 nvpriv_t *priv;
1333 nvpair_t *nvp;
1334 i_nvp_t *curr;
1335
1336 if (name == NULL || nvl == NULL ||
1337 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1338 return (EINVAL);
1339
1340 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1341 return (ENOTSUP);
1342
1343 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1344 nvp = &curr->nvi_nvp;
1345
1346 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1347 return (nvpair_value_common(nvp, type, nelem, data));
1348 }
1349
1350 return (ENOENT);
1351 }
1352
1353 int
1354 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1355 {
1356 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1357 }
1358
1359 int
1360 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1361 {
1362 return (nvlist_lookup_common(nvl, name,
1363 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1364 }
1365
1366 int
1367 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1368 {
1369 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1370 }
1371
1372 int
1373 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1374 {
1375 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1376 }
1377
1378 int
1379 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1380 {
1381 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1382 }
1383
1384 int
1385 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1386 {
1387 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1388 }
1389
1390 int
1391 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1392 {
1393 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1394 }
1395
1396 int
1397 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1398 {
1399 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1400 }
1401
1402 int
1403 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1404 {
1405 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1406 }
1407
1408 int
1409 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1410 {
1411 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1412 }
1413
1414 int
1415 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1416 {
1417 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1418 }
1419
1420 #if !defined(_KERNEL)
1421 int
1422 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1423 {
1424 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1425 }
1426 #endif
1427
1428 int
1429 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1430 {
1431 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1432 }
1433
1434 int
1435 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1436 {
1437 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1438 }
1439
1440 int
1441 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1442 boolean_t **a, uint_t *n)
1443 {
1444 return (nvlist_lookup_common(nvl, name,
1445 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1446 }
1447
1448 int
1449 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1450 uchar_t **a, uint_t *n)
1451 {
1452 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1453 }
1454
1455 int
1456 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1457 {
1458 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1459 }
1460
1461 int
1462 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1463 uint8_t **a, uint_t *n)
1464 {
1465 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1466 }
1467
1468 int
1469 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1470 int16_t **a, uint_t *n)
1471 {
1472 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1473 }
1474
1475 int
1476 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1477 uint16_t **a, uint_t *n)
1478 {
1479 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1480 }
1481
1482 int
1483 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1484 int32_t **a, uint_t *n)
1485 {
1486 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1487 }
1488
1489 int
1490 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1491 uint32_t **a, uint_t *n)
1492 {
1493 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1494 }
1495
1496 int
1497 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1498 int64_t **a, uint_t *n)
1499 {
1500 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1501 }
1502
1503 int
1504 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1505 uint64_t **a, uint_t *n)
1506 {
1507 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1508 }
1509
1510 int
1511 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1512 char ***a, uint_t *n)
1513 {
1514 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1515 }
1516
1517 int
1518 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1519 nvlist_t ***a, uint_t *n)
1520 {
1521 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1522 }
1523
1524 int
1525 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1526 {
1527 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1528 }
1529
1530 int
1531 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1532 {
1533 va_list ap;
1534 char *name;
1535 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1536 int ret = 0;
1537
1538 va_start(ap, flag);
1539 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1540 data_type_t type;
1541 void *val;
1542 uint_t *nelem;
1543
1544 switch (type = va_arg(ap, data_type_t)) {
1545 case DATA_TYPE_BOOLEAN:
1546 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1547 break;
1548
1549 case DATA_TYPE_BOOLEAN_VALUE:
1550 case DATA_TYPE_BYTE:
1551 case DATA_TYPE_INT8:
1552 case DATA_TYPE_UINT8:
1553 case DATA_TYPE_INT16:
1554 case DATA_TYPE_UINT16:
1555 case DATA_TYPE_INT32:
1556 case DATA_TYPE_UINT32:
1557 case DATA_TYPE_INT64:
1558 case DATA_TYPE_UINT64:
1559 case DATA_TYPE_HRTIME:
1560 case DATA_TYPE_STRING:
1561 case DATA_TYPE_NVLIST:
1562 #if !defined(_KERNEL)
1563 case DATA_TYPE_DOUBLE:
1564 #endif
1565 val = va_arg(ap, void *);
1566 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1567 break;
1568
1569 case DATA_TYPE_BYTE_ARRAY:
1570 case DATA_TYPE_BOOLEAN_ARRAY:
1571 case DATA_TYPE_INT8_ARRAY:
1572 case DATA_TYPE_UINT8_ARRAY:
1573 case DATA_TYPE_INT16_ARRAY:
1574 case DATA_TYPE_UINT16_ARRAY:
1575 case DATA_TYPE_INT32_ARRAY:
1576 case DATA_TYPE_UINT32_ARRAY:
1577 case DATA_TYPE_INT64_ARRAY:
1578 case DATA_TYPE_UINT64_ARRAY:
1579 case DATA_TYPE_STRING_ARRAY:
1580 case DATA_TYPE_NVLIST_ARRAY:
1581 val = va_arg(ap, void *);
1582 nelem = va_arg(ap, uint_t *);
1583 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1584 break;
1585
1586 default:
1587 ret = EINVAL;
1588 }
1589
1590 if (ret == ENOENT && noentok)
1591 ret = 0;
1592 }
1593 va_end(ap);
1594
1595 return (ret);
1596 }
1597
1598 /*
1599 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1600 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1601 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1602 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1603 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1604 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1605 * code also supports "a.d[3]e[1]" syntax).
1606 *
1607 * If 'ip' is non-NULL and the last name component is an array, return the
1608 * value of the "...[index]" array index in *ip. For an array reference that
1609 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1610 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1611 * inside the 'name' string where the syntax error was detected.
1612 */
1613 static int
1614 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1615 nvpair_t **ret, int *ip, char **ep)
1616 {
1617 nvpair_t *nvp;
1618 const char *np;
1619 char *sepp = NULL;
1620 char *idxp, *idxep;
1621 nvlist_t **nva;
1622 long idx = 0;
1623 int n;
1624
1625 if (ip)
1626 *ip = -1; /* not indexed */
1627 if (ep)
1628 *ep = NULL;
1629
1630 if ((nvl == NULL) || (name == NULL))
1631 return (EINVAL);
1632
1633 /* step through components of name */
1634 for (np = name; np && *np; np = sepp) {
1635 /* ensure unique names */
1636 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1637 return (ENOTSUP);
1638
1639 /* skip white space */
1640 skip_whitespace(np);
1641 if (*np == 0)
1642 break;
1643
1644 /* set 'sepp' to end of current component 'np' */
1645 if (sep)
1646 sepp = strchr(np, sep);
1647 else
1648 sepp = NULL;
1649
1650 /* find start of next "[ index ]..." */
1651 idxp = strchr(np, '[');
1652
1653 /* if sepp comes first, set idxp to NULL */
1654 if (sepp && idxp && (sepp < idxp))
1655 idxp = NULL;
1656
1657 /*
1658 * At this point 'idxp' is set if there is an index
1659 * expected for the current component.
1660 */
1661 if (idxp) {
1662 /* set 'n' to length of current 'np' name component */
1663 n = idxp++ - np;
1664
1665 /* keep sepp up to date for *ep use as we advance */
1666 skip_whitespace(idxp);
1667 sepp = idxp;
1668
1669 /* determine the index value */
1670 #if defined(_KERNEL) && !defined(_BOOT)
1671 if (ddi_strtol(idxp, &idxep, 0, &idx))
1672 goto fail;
1673 #else
1674 idx = strtol(idxp, &idxep, 0);
1675 #endif
1676 if (idxep == idxp)
1677 goto fail;
1678
1679 /* keep sepp up to date for *ep use as we advance */
1680 sepp = idxep;
1681
1682 /* skip white space index value and check for ']' */
1683 skip_whitespace(sepp);
1684 if (*sepp++ != ']')
1685 goto fail;
1686
1687 /* for embedded arrays, support C syntax: "a[1].b" */
1688 skip_whitespace(sepp);
1689 if (sep && (*sepp == sep))
1690 sepp++;
1691 } else if (sepp) {
1692 n = sepp++ - np;
1693 } else {
1694 n = strlen(np);
1695 }
1696
1697 /* trim trailing whitespace by reducing length of 'np' */
1698 if (n == 0)
1699 goto fail;
1700 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1701 ;
1702 n++;
1703
1704 /* skip whitespace, and set sepp to NULL if complete */
1705 if (sepp) {
1706 skip_whitespace(sepp);
1707 if (*sepp == 0)
1708 sepp = NULL;
1709 }
1710
1711 /*
1712 * At this point:
1713 * o 'n' is the length of current 'np' component.
1714 * o 'idxp' is set if there was an index, and value 'idx'.
1715 * o 'sepp' is set to the beginning of the next component,
1716 * and set to NULL if we have no more components.
1717 *
1718 * Search for nvpair with matching component name.
1719 */
1720 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1721 nvp = nvlist_next_nvpair(nvl, nvp)) {
1722
1723 /* continue if no match on name */
1724 if (strncmp(np, nvpair_name(nvp), n) ||
1725 (strlen(nvpair_name(nvp)) != n))
1726 continue;
1727
1728 /* if indexed, verify type is array oriented */
1729 if (idxp && !nvpair_type_is_array(nvp))
1730 goto fail;
1731
1732 /*
1733 * Full match found, return nvp and idx if this
1734 * was the last component.
1735 */
1736 if (sepp == NULL) {
1737 if (ret)
1738 *ret = nvp;
1739 if (ip && idxp)
1740 *ip = (int)idx; /* return index */
1741 return (0); /* found */
1742 }
1743
1744 /*
1745 * More components: current match must be
1746 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1747 * to support going deeper.
1748 */
1749 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1750 nvl = EMBEDDED_NVL(nvp);
1751 break;
1752 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1753 (void) nvpair_value_nvlist_array(nvp,
1754 &nva, (uint_t *)&n);
1755 if ((n < 0) || (idx >= n))
1756 goto fail;
1757 nvl = nva[idx];
1758 break;
1759 }
1760
1761 /* type does not support more levels */
1762 goto fail;
1763 }
1764 if (nvp == NULL)
1765 goto fail; /* 'name' not found */
1766
1767 /* search for match of next component in embedded 'nvl' list */
1768 }
1769
1770 fail: if (ep && sepp)
1771 *ep = sepp;
1772 return (EINVAL);
1773 }
1774
1775 /*
1776 * Return pointer to nvpair with specified 'name'.
1777 */
1778 int
1779 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1780 {
1781 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1782 }
1783
1784 /*
1785 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1786 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1787 * description.
1788 */
1789 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1790 const char *name, nvpair_t **ret, int *ip, char **ep)
1791 {
1792 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1793 }
1794
1795 boolean_t
1796 nvlist_exists(nvlist_t *nvl, const char *name)
1797 {
1798 nvpriv_t *priv;
1799 nvpair_t *nvp;
1800 i_nvp_t *curr;
1801
1802 if (name == NULL || nvl == NULL ||
1803 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1804 return (B_FALSE);
1805
1806 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1807 nvp = &curr->nvi_nvp;
1808
1809 if (strcmp(name, NVP_NAME(nvp)) == 0)
1810 return (B_TRUE);
1811 }
1812
1813 return (B_FALSE);
1814 }
1815
1816 int
1817 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1818 {
1819 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1820 }
1821
1822 int
1823 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1824 {
1825 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1826 }
1827
1828 int
1829 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1830 {
1831 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1832 }
1833
1834 int
1835 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1836 {
1837 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1838 }
1839
1840 int
1841 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1842 {
1843 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1844 }
1845
1846 int
1847 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1848 {
1849 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1850 }
1851
1852 int
1853 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1854 {
1855 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1856 }
1857
1858 int
1859 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1860 {
1861 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1862 }
1863
1864 int
1865 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1866 {
1867 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1868 }
1869
1870 int
1871 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1872 {
1873 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1874 }
1875
1876 #if !defined(_KERNEL)
1877 int
1878 nvpair_value_double(nvpair_t *nvp, double *val)
1879 {
1880 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1881 }
1882 #endif
1883
1884 int
1885 nvpair_value_string(nvpair_t *nvp, char **val)
1886 {
1887 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1888 }
1889
1890 int
1891 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1892 {
1893 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1894 }
1895
1896 int
1897 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1898 {
1899 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1900 }
1901
1902 int
1903 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1904 {
1905 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1906 }
1907
1908 int
1909 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1910 {
1911 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1912 }
1913
1914 int
1915 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1916 {
1917 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1918 }
1919
1920 int
1921 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1922 {
1923 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1924 }
1925
1926 int
1927 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1928 {
1929 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1930 }
1931
1932 int
1933 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1934 {
1935 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1936 }
1937
1938 int
1939 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1940 {
1941 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1942 }
1943
1944 int
1945 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1946 {
1947 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1948 }
1949
1950 int
1951 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1952 {
1953 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1954 }
1955
1956 int
1957 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1958 {
1959 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1960 }
1961
1962 int
1963 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1964 {
1965 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1966 }
1967
1968 int
1969 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1970 {
1971 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1972 }
1973
1974 /*
1975 * Add specified pair to the list.
1976 */
1977 int
1978 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1979 {
1980 if (nvl == NULL || nvp == NULL)
1981 return (EINVAL);
1982
1983 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1984 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1985 }
1986
1987 /*
1988 * Merge the supplied nvlists and put the result in dst.
1989 * The merged list will contain all names specified in both lists,
1990 * the values are taken from nvl in the case of duplicates.
1991 * Return 0 on success.
1992 */
1993 /*ARGSUSED*/
1994 int
1995 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1996 {
1997 if (nvl == NULL || dst == NULL)
1998 return (EINVAL);
1999
2000 if (dst != nvl)
2001 return (nvlist_copy_pairs(nvl, dst));
2002
2003 return (0);
2004 }
2005
2006 /*
2007 * Encoding related routines
2008 */
2009 #define NVS_OP_ENCODE 0
2010 #define NVS_OP_DECODE 1
2011 #define NVS_OP_GETSIZE 2
2012
2013 typedef struct nvs_ops nvs_ops_t;
2014
2015 typedef struct {
2016 int nvs_op;
2017 const nvs_ops_t *nvs_ops;
2018 void *nvs_private;
2019 nvpriv_t *nvs_priv;
2020 } nvstream_t;
2021
2022 /*
2023 * nvs operations are:
2024 * - nvs_nvlist
2025 * encoding / decoding of a nvlist header (nvlist_t)
2026 * calculates the size used for header and end detection
2027 *
2028 * - nvs_nvpair
2029 * responsible for the first part of encoding / decoding of an nvpair
2030 * calculates the decoded size of an nvpair
2031 *
2032 * - nvs_nvp_op
2033 * second part of encoding / decoding of an nvpair
2034 *
2035 * - nvs_nvp_size
2036 * calculates the encoding size of an nvpair
2037 *
2038 * - nvs_nvl_fini
2039 * encodes the end detection mark (zeros).
2040 */
2041 struct nvs_ops {
2042 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
2043 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
2044 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
2045 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
2046 int (*nvs_nvl_fini)(nvstream_t *);
2047 };
2048
2049 typedef struct {
2050 char nvh_encoding; /* nvs encoding method */
2051 char nvh_endian; /* nvs endian */
2052 char nvh_reserved1; /* reserved for future use */
2053 char nvh_reserved2; /* reserved for future use */
2054 } nvs_header_t;
2055
2056 static int
2057 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2058 {
2059 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2060 i_nvp_t *curr;
2061
2062 /*
2063 * Walk nvpair in list and encode each nvpair
2064 */
2065 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2066 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2067 return (EFAULT);
2068
2069 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2070 }
2071
2072 static int
2073 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2074 {
2075 nvpair_t *nvp;
2076 size_t nvsize;
2077 int err;
2078
2079 /*
2080 * Get decoded size of next pair in stream, alloc
2081 * memory for nvpair_t, then decode the nvpair
2082 */
2083 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2084 if (nvsize == 0) /* end of list */
2085 break;
2086
2087 /* make sure len makes sense */
2088 if (nvsize < NVP_SIZE_CALC(1, 0))
2089 return (EFAULT);
2090
2091 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2092 return (ENOMEM);
2093
2094 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2095 nvp_buf_free(nvl, nvp);
2096 return (err);
2097 }
2098
2099 if (i_validate_nvpair(nvp) != 0) {
2100 nvpair_free(nvp);
2101 nvp_buf_free(nvl, nvp);
2102 return (EFAULT);
2103 }
2104
2105 nvp_buf_link(nvl, nvp);
2106 }
2107 return (err);
2108 }
2109
2110 static int
2111 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2112 {
2113 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2114 i_nvp_t *curr;
2115 uint64_t nvsize = *buflen;
2116 size_t size;
2117
2118 /*
2119 * Get encoded size of nvpairs in nvlist
2120 */
2121 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2122 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2123 return (EINVAL);
2124
2125 if ((nvsize += size) > INT32_MAX)
2126 return (EINVAL);
2127 }
2128
2129 *buflen = nvsize;
2130 return (0);
2131 }
2132
2133 static int
2134 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2135 {
2136 int err;
2137
2138 if (nvl->nvl_priv == 0)
2139 return (EFAULT);
2140
2141 /*
2142 * Perform the operation, starting with header, then each nvpair
2143 */
2144 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2145 return (err);
2146
2147 switch (nvs->nvs_op) {
2148 case NVS_OP_ENCODE:
2149 err = nvs_encode_pairs(nvs, nvl);
2150 break;
2151
2152 case NVS_OP_DECODE:
2153 err = nvs_decode_pairs(nvs, nvl);
2154 break;
2155
2156 case NVS_OP_GETSIZE:
2157 err = nvs_getsize_pairs(nvs, nvl, buflen);
2158 break;
2159
2160 default:
2161 err = EINVAL;
2162 }
2163
2164 return (err);
2165 }
2166
2167 static int
2168 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2169 {
2170 switch (nvs->nvs_op) {
2171 case NVS_OP_ENCODE:
2172 return (nvs_operation(nvs, embedded, NULL));
2173
2174 case NVS_OP_DECODE: {
2175 nvpriv_t *priv;
2176 int err;
2177
2178 if (embedded->nvl_version != NV_VERSION)
2179 return (ENOTSUP);
2180
2181 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2182 return (ENOMEM);
2183
2184 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2185
2186 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2187 nvlist_free(embedded);
2188 return (err);
2189 }
2190 default:
2191 break;
2192 }
2193
2194 return (EINVAL);
2195 }
2196
2197 static int
2198 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2199 {
2200 size_t nelem = NVP_NELEM(nvp);
2201 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2202 int i;
2203
2204 switch (nvs->nvs_op) {
2205 case NVS_OP_ENCODE:
2206 for (i = 0; i < nelem; i++)
2207 if (nvs_embedded(nvs, nvlp[i]) != 0)
2208 return (EFAULT);
2209 break;
2210
2211 case NVS_OP_DECODE: {
2212 size_t len = nelem * sizeof (uint64_t);
2213 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2214
2215 bzero(nvlp, len); /* don't trust packed data */
2216 for (i = 0; i < nelem; i++) {
2217 if (nvs_embedded(nvs, embedded) != 0) {
2218 nvpair_free(nvp);
2219 return (EFAULT);
2220 }
2221
2222 nvlp[i] = embedded++;
2223 }
2224 break;
2225 }
2226 case NVS_OP_GETSIZE: {
2227 uint64_t nvsize = 0;
2228
2229 for (i = 0; i < nelem; i++) {
2230 size_t nvp_sz = 0;
2231
2232 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2233 return (EINVAL);
2234
2235 if ((nvsize += nvp_sz) > INT32_MAX)
2236 return (EINVAL);
2237 }
2238
2239 *size = nvsize;
2240 break;
2241 }
2242 default:
2243 return (EINVAL);
2244 }
2245
2246 return (0);
2247 }
2248
2249 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2250 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2251
2252 /*
2253 * Common routine for nvlist operations:
2254 * encode, decode, getsize (encoded size).
2255 */
2256 static int
2257 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2258 int nvs_op)
2259 {
2260 int err = 0;
2261 nvstream_t nvs;
2262 int nvl_endian;
2263 #ifdef _LITTLE_ENDIAN
2264 int host_endian = 1;
2265 #else
2266 int host_endian = 0;
2267 #endif /* _LITTLE_ENDIAN */
2268 nvs_header_t *nvh = (void *)buf;
2269
2270 if (buflen == NULL || nvl == NULL ||
2271 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2272 return (EINVAL);
2273
2274 nvs.nvs_op = nvs_op;
2275
2276 /*
2277 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2278 * a buffer is allocated. The first 4 bytes in the buffer are
2279 * used for encoding method and host endian.
2280 */
2281 switch (nvs_op) {
2282 case NVS_OP_ENCODE:
2283 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2284 return (EINVAL);
2285
2286 nvh->nvh_encoding = encoding;
2287 nvh->nvh_endian = nvl_endian = host_endian;
2288 nvh->nvh_reserved1 = 0;
2289 nvh->nvh_reserved2 = 0;
2290 break;
2291
2292 case NVS_OP_DECODE:
2293 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2294 return (EINVAL);
2295
2296 /* get method of encoding from first byte */
2297 encoding = nvh->nvh_encoding;
2298 nvl_endian = nvh->nvh_endian;
2299 break;
2300
2301 case NVS_OP_GETSIZE:
2302 nvl_endian = host_endian;
2303
2304 /*
2305 * add the size for encoding
2306 */
2307 *buflen = sizeof (nvs_header_t);
2308 break;
2309
2310 default:
2311 return (ENOTSUP);
2312 }
2313
2314 /*
2315 * Create an nvstream with proper encoding method
2316 */
2317 switch (encoding) {
2318 case NV_ENCODE_NATIVE:
2319 /*
2320 * check endianness, in case we are unpacking
2321 * from a file
2322 */
2323 if (nvl_endian != host_endian)
2324 return (ENOTSUP);
2325 err = nvs_native(&nvs, nvl, buf, buflen);
2326 break;
2327 case NV_ENCODE_XDR:
2328 err = nvs_xdr(&nvs, nvl, buf, buflen);
2329 break;
2330 default:
2331 err = ENOTSUP;
2332 break;
2333 }
2334
2335 return (err);
2336 }
2337
2338 int
2339 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2340 {
2341 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2342 }
2343
2344 /*
2345 * Pack nvlist into contiguous memory
2346 */
2347 int
2348 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2349 int kmflag)
2350 {
2351 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2352 nvlist_nv_alloc(kmflag)));
2353 }
2354
2355 int
2356 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2357 nv_alloc_t *nva)
2358 {
2359 nvpriv_t nvpriv;
2360 size_t alloc_size;
2361 char *buf;
2362 int err;
2363
2364 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2365 return (EINVAL);
2366
2367 if (*bufp != NULL)
2368 return (nvlist_common(nvl, *bufp, buflen, encoding,
2369 NVS_OP_ENCODE));
2370
2371 /*
2372 * Here is a difficult situation:
2373 * 1. The nvlist has fixed allocator properties.
2374 * All other nvlist routines (like nvlist_add_*, ...) use
2375 * these properties.
2376 * 2. When using nvlist_pack() the user can specify his own
2377 * allocator properties (e.g. by using KM_NOSLEEP).
2378 *
2379 * We use the user specified properties (2). A clearer solution
2380 * will be to remove the kmflag from nvlist_pack(), but we will
2381 * not change the interface.
2382 */
2383 nv_priv_init(&nvpriv, nva, 0);
2384
2385 if ((err = nvlist_size(nvl, &alloc_size, encoding)))
2386 return (err);
2387
2388 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2389 return (ENOMEM);
2390
2391 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2392 NVS_OP_ENCODE)) != 0) {
2393 nv_mem_free(&nvpriv, buf, alloc_size);
2394 } else {
2395 *buflen = alloc_size;
2396 *bufp = buf;
2397 }
2398
2399 return (err);
2400 }
2401
2402 /*
2403 * Unpack buf into an nvlist_t
2404 */
2405 int
2406 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2407 {
2408 return (nvlist_xunpack(buf, buflen, nvlp, nvlist_nv_alloc(kmflag)));
2409 }
2410
2411 int
2412 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2413 {
2414 nvlist_t *nvl;
2415 int err;
2416
2417 if (nvlp == NULL)
2418 return (EINVAL);
2419
2420 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2421 return (err);
2422
2423 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2424 nvlist_free(nvl);
2425 else
2426 *nvlp = nvl;
2427
2428 return (err);
2429 }
2430
2431 /*
2432 * Native encoding functions
2433 */
2434 typedef struct {
2435 /*
2436 * This structure is used when decoding a packed nvpair in
2437 * the native format. n_base points to a buffer containing the
2438 * packed nvpair. n_end is a pointer to the end of the buffer.
2439 * (n_end actually points to the first byte past the end of the
2440 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2441 * It points to the current data that we are decoding.
2442 * The amount of data left in the buffer is equal to n_end - n_curr.
2443 * n_flag is used to recognize a packed embedded list.
2444 */
2445 caddr_t n_base;
2446 caddr_t n_end;
2447 caddr_t n_curr;
2448 uint_t n_flag;
2449 } nvs_native_t;
2450
2451 static int
2452 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2453 size_t buflen)
2454 {
2455 switch (nvs->nvs_op) {
2456 case NVS_OP_ENCODE:
2457 case NVS_OP_DECODE:
2458 nvs->nvs_private = native;
2459 native->n_curr = native->n_base = buf;
2460 native->n_end = buf + buflen;
2461 native->n_flag = 0;
2462 return (0);
2463
2464 case NVS_OP_GETSIZE:
2465 nvs->nvs_private = native;
2466 native->n_curr = native->n_base = native->n_end = NULL;
2467 native->n_flag = 0;
2468 return (0);
2469 default:
2470 return (EINVAL);
2471 }
2472 }
2473
2474 /*ARGSUSED*/
2475 static void
2476 nvs_native_destroy(nvstream_t *nvs)
2477 {
2478 }
2479
2480 static int
2481 native_cp(nvstream_t *nvs, void *buf, size_t size)
2482 {
2483 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2484
2485 if (native->n_curr + size > native->n_end)
2486 return (EFAULT);
2487
2488 /*
2489 * The bcopy() below eliminates alignment requirement
2490 * on the buffer (stream) and is preferred over direct access.
2491 */
2492 switch (nvs->nvs_op) {
2493 case NVS_OP_ENCODE:
2494 bcopy(buf, native->n_curr, size);
2495 break;
2496 case NVS_OP_DECODE:
2497 bcopy(native->n_curr, buf, size);
2498 break;
2499 default:
2500 return (EINVAL);
2501 }
2502
2503 native->n_curr += size;
2504 return (0);
2505 }
2506
2507 /*
2508 * operate on nvlist_t header
2509 */
2510 static int
2511 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2512 {
2513 nvs_native_t *native = nvs->nvs_private;
2514
2515 switch (nvs->nvs_op) {
2516 case NVS_OP_ENCODE:
2517 case NVS_OP_DECODE:
2518 if (native->n_flag)
2519 return (0); /* packed embedded list */
2520
2521 native->n_flag = 1;
2522
2523 /* copy version and nvflag of the nvlist_t */
2524 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2525 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2526 return (EFAULT);
2527
2528 return (0);
2529
2530 case NVS_OP_GETSIZE:
2531 /*
2532 * if calculate for packed embedded list
2533 * 4 for end of the embedded list
2534 * else
2535 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2536 * and 4 for end of the entire list
2537 */
2538 if (native->n_flag) {
2539 *size += 4;
2540 } else {
2541 native->n_flag = 1;
2542 *size += 2 * sizeof (int32_t) + 4;
2543 }
2544
2545 return (0);
2546
2547 default:
2548 return (EINVAL);
2549 }
2550 }
2551
2552 static int
2553 nvs_native_nvl_fini(nvstream_t *nvs)
2554 {
2555 if (nvs->nvs_op == NVS_OP_ENCODE) {
2556 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2557 /*
2558 * Add 4 zero bytes at end of nvlist. They are used
2559 * for end detection by the decode routine.
2560 */
2561 if (native->n_curr + sizeof (int) > native->n_end)
2562 return (EFAULT);
2563
2564 bzero(native->n_curr, sizeof (int));
2565 native->n_curr += sizeof (int);
2566 }
2567
2568 return (0);
2569 }
2570
2571 static int
2572 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2573 {
2574 if (nvs->nvs_op == NVS_OP_ENCODE) {
2575 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2576 nvlist_t *packed = (void *)
2577 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2578 /*
2579 * Null out the pointer that is meaningless in the packed
2580 * structure. The address may not be aligned, so we have
2581 * to use bzero.
2582 */
2583 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2584 sizeof (uint64_t));
2585 }
2586
2587 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2588 }
2589
2590 static int
2591 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2592 {
2593 if (nvs->nvs_op == NVS_OP_ENCODE) {
2594 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2595 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2596 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2597 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2598 int i;
2599 /*
2600 * Null out pointers that are meaningless in the packed
2601 * structure. The addresses may not be aligned, so we have
2602 * to use bzero.
2603 */
2604 bzero(value, len);
2605
2606 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2607 /*
2608 * Null out the pointer that is meaningless in the
2609 * packed structure. The address may not be aligned,
2610 * so we have to use bzero.
2611 */
2612 bzero((char *)packed + offsetof(nvlist_t, nvl_priv),
2613 sizeof (uint64_t));
2614 }
2615
2616 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2617 }
2618
2619 static void
2620 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2621 {
2622 switch (nvs->nvs_op) {
2623 case NVS_OP_ENCODE: {
2624 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2625 uint64_t *strp = (void *)
2626 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2627 /*
2628 * Null out pointers that are meaningless in the packed
2629 * structure. The addresses may not be aligned, so we have
2630 * to use bzero.
2631 */
2632 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2633 break;
2634 }
2635 case NVS_OP_DECODE: {
2636 char **strp = (void *)NVP_VALUE(nvp);
2637 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2638 int i;
2639
2640 for (i = 0; i < NVP_NELEM(nvp); i++) {
2641 strp[i] = buf;
2642 buf += strlen(buf) + 1;
2643 }
2644 break;
2645 }
2646 }
2647 }
2648
2649 static int
2650 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2651 {
2652 data_type_t type;
2653 int value_sz;
2654 int ret = 0;
2655
2656 /*
2657 * We do the initial bcopy of the data before we look at
2658 * the nvpair type, because when we're decoding, we won't
2659 * have the correct values for the pair until we do the bcopy.
2660 */
2661 switch (nvs->nvs_op) {
2662 case NVS_OP_ENCODE:
2663 case NVS_OP_DECODE:
2664 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2665 return (EFAULT);
2666 break;
2667 default:
2668 return (EINVAL);
2669 }
2670
2671 /* verify nvp_name_sz, check the name string length */
2672 if (i_validate_nvpair_name(nvp) != 0)
2673 return (EFAULT);
2674
2675 type = NVP_TYPE(nvp);
2676
2677 /*
2678 * Verify type and nelem and get the value size.
2679 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2680 * is the size of the string(s) excluded.
2681 */
2682 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2683 return (EFAULT);
2684
2685 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2686 return (EFAULT);
2687
2688 switch (type) {
2689 case DATA_TYPE_NVLIST:
2690 ret = nvpair_native_embedded(nvs, nvp);
2691 break;
2692 case DATA_TYPE_NVLIST_ARRAY:
2693 ret = nvpair_native_embedded_array(nvs, nvp);
2694 break;
2695 case DATA_TYPE_STRING_ARRAY:
2696 nvpair_native_string_array(nvs, nvp);
2697 break;
2698 default:
2699 break;
2700 }
2701
2702 return (ret);
2703 }
2704
2705 static int
2706 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2707 {
2708 uint64_t nvp_sz = nvp->nvp_size;
2709
2710 switch (NVP_TYPE(nvp)) {
2711 case DATA_TYPE_NVLIST: {
2712 size_t nvsize = 0;
2713
2714 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2715 return (EINVAL);
2716
2717 nvp_sz += nvsize;
2718 break;
2719 }
2720 case DATA_TYPE_NVLIST_ARRAY: {
2721 size_t nvsize;
2722
2723 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2724 return (EINVAL);
2725
2726 nvp_sz += nvsize;
2727 break;
2728 }
2729 default:
2730 break;
2731 }
2732
2733 if (nvp_sz > INT32_MAX)
2734 return (EINVAL);
2735
2736 *size = nvp_sz;
2737
2738 return (0);
2739 }
2740
2741 static int
2742 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2743 {
2744 switch (nvs->nvs_op) {
2745 case NVS_OP_ENCODE:
2746 return (nvs_native_nvp_op(nvs, nvp));
2747
2748 case NVS_OP_DECODE: {
2749 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2750 int32_t decode_len;
2751
2752 /* try to read the size value from the stream */
2753 if (native->n_curr + sizeof (int32_t) > native->n_end)
2754 return (EFAULT);
2755 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2756
2757 /* sanity check the size value */
2758 if (decode_len < 0 ||
2759 decode_len > native->n_end - native->n_curr)
2760 return (EFAULT);
2761
2762 *size = decode_len;
2763
2764 /*
2765 * If at the end of the stream then move the cursor
2766 * forward, otherwise nvpair_native_op() will read
2767 * the entire nvpair at the same cursor position.
2768 */
2769 if (*size == 0)
2770 native->n_curr += sizeof (int32_t);
2771 break;
2772 }
2773
2774 default:
2775 return (EINVAL);
2776 }
2777
2778 return (0);
2779 }
2780
2781 static const nvs_ops_t nvs_native_ops = {
2782 nvs_native_nvlist,
2783 nvs_native_nvpair,
2784 nvs_native_nvp_op,
2785 nvs_native_nvp_size,
2786 nvs_native_nvl_fini
2787 };
2788
2789 static int
2790 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2791 {
2792 nvs_native_t native;
2793 int err;
2794
2795 nvs->nvs_ops = &nvs_native_ops;
2796
2797 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2798 *buflen - sizeof (nvs_header_t))) != 0)
2799 return (err);
2800
2801 err = nvs_operation(nvs, nvl, buflen);
2802
2803 nvs_native_destroy(nvs);
2804
2805 return (err);
2806 }
2807
2808 /*
2809 * XDR encoding functions
2810 *
2811 * An xdr packed nvlist is encoded as:
2812 *
2813 * - encoding methode and host endian (4 bytes)
2814 * - nvl_version (4 bytes)
2815 * - nvl_nvflag (4 bytes)
2816 *
2817 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2818 * - encoded size of the nvpair (4 bytes)
2819 * - decoded size of the nvpair (4 bytes)
2820 * - name string, (4 + sizeof(NV_ALIGN4(string))
2821 * a string is coded as size (4 bytes) and data
2822 * - data type (4 bytes)
2823 * - number of elements in the nvpair (4 bytes)
2824 * - data
2825 *
2826 * - 2 zero's for end of the entire list (8 bytes)
2827 */
2828 static int
2829 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2830 {
2831 /* xdr data must be 4 byte aligned */
2832 if ((ulong_t)buf % 4 != 0)
2833 return (EFAULT);
2834
2835 switch (nvs->nvs_op) {
2836 case NVS_OP_ENCODE:
2837 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2838 nvs->nvs_private = xdr;
2839 return (0);
2840 case NVS_OP_DECODE:
2841 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2842 nvs->nvs_private = xdr;
2843 return (0);
2844 case NVS_OP_GETSIZE:
2845 nvs->nvs_private = NULL;
2846 return (0);
2847 default:
2848 return (EINVAL);
2849 }
2850 }
2851
2852 static void
2853 nvs_xdr_destroy(nvstream_t *nvs)
2854 {
2855 switch (nvs->nvs_op) {
2856 case NVS_OP_ENCODE:
2857 case NVS_OP_DECODE:
2858 xdr_destroy((XDR *)nvs->nvs_private);
2859 break;
2860 default:
2861 break;
2862 }
2863 }
2864
2865 static int
2866 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2867 {
2868 switch (nvs->nvs_op) {
2869 case NVS_OP_ENCODE:
2870 case NVS_OP_DECODE: {
2871 XDR *xdr = nvs->nvs_private;
2872
2873 if (!xdr_int(xdr, &nvl->nvl_version) ||
2874 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2875 return (EFAULT);
2876 break;
2877 }
2878 case NVS_OP_GETSIZE: {
2879 /*
2880 * 2 * 4 for nvl_version + nvl_nvflag
2881 * and 8 for end of the entire list
2882 */
2883 *size += 2 * 4 + 8;
2884 break;
2885 }
2886 default:
2887 return (EINVAL);
2888 }
2889 return (0);
2890 }
2891
2892 static int
2893 nvs_xdr_nvl_fini(nvstream_t *nvs)
2894 {
2895 if (nvs->nvs_op == NVS_OP_ENCODE) {
2896 XDR *xdr = nvs->nvs_private;
2897 int zero = 0;
2898
2899 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2900 return (EFAULT);
2901 }
2902
2903 return (0);
2904 }
2905
2906 /*
2907 * The format of xdr encoded nvpair is:
2908 * encode_size, decode_size, name string, data type, nelem, data
2909 */
2910 static int
2911 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2912 {
2913 data_type_t type;
2914 char *buf;
2915 char *buf_end = (char *)nvp + nvp->nvp_size;
2916 int value_sz;
2917 uint_t nelem, buflen;
2918 bool_t ret = FALSE;
2919 XDR *xdr = nvs->nvs_private;
2920
2921 ASSERT(xdr != NULL && nvp != NULL);
2922
2923 /* name string */
2924 if ((buf = NVP_NAME(nvp)) >= buf_end)
2925 return (EFAULT);
2926 buflen = buf_end - buf;
2927
2928 if (!xdr_string(xdr, &buf, buflen - 1))
2929 return (EFAULT);
2930 nvp->nvp_name_sz = strlen(buf) + 1;
2931
2932 /* type and nelem */
2933 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2934 !xdr_int(xdr, &nvp->nvp_value_elem))
2935 return (EFAULT);
2936
2937 type = NVP_TYPE(nvp);
2938 nelem = nvp->nvp_value_elem;
2939
2940 /*
2941 * Verify type and nelem and get the value size.
2942 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2943 * is the size of the string(s) excluded.
2944 */
2945 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2946 return (EFAULT);
2947
2948 /* if there is no data to extract then return */
2949 if (nelem == 0)
2950 return (0);
2951
2952 /* value */
2953 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2954 return (EFAULT);
2955 buflen = buf_end - buf;
2956
2957 if (buflen < value_sz)
2958 return (EFAULT);
2959
2960 switch (type) {
2961 case DATA_TYPE_NVLIST:
2962 if (nvs_embedded(nvs, (void *)buf) == 0)
2963 return (0);
2964 break;
2965
2966 case DATA_TYPE_NVLIST_ARRAY:
2967 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2968 return (0);
2969 break;
2970
2971 case DATA_TYPE_BOOLEAN:
2972 ret = TRUE;
2973 break;
2974
2975 case DATA_TYPE_BYTE:
2976 case DATA_TYPE_INT8:
2977 case DATA_TYPE_UINT8:
2978 ret = xdr_char(xdr, buf);
2979 break;
2980
2981 case DATA_TYPE_INT16:
2982 ret = xdr_short(xdr, (void *)buf);
2983 break;
2984
2985 case DATA_TYPE_UINT16:
2986 ret = xdr_u_short(xdr, (void *)buf);
2987 break;
2988
2989 case DATA_TYPE_BOOLEAN_VALUE:
2990 case DATA_TYPE_INT32:
2991 ret = xdr_int(xdr, (void *)buf);
2992 break;
2993
2994 case DATA_TYPE_UINT32:
2995 ret = xdr_u_int(xdr, (void *)buf);
2996 break;
2997
2998 case DATA_TYPE_INT64:
2999 ret = xdr_longlong_t(xdr, (void *)buf);
3000 break;
3001
3002 case DATA_TYPE_UINT64:
3003 ret = xdr_u_longlong_t(xdr, (void *)buf);
3004 break;
3005
3006 case DATA_TYPE_HRTIME:
3007 /*
3008 * NOTE: must expose the definition of hrtime_t here
3009 */
3010 ret = xdr_longlong_t(xdr, (void *)buf);
3011 break;
3012 #if !defined(_KERNEL)
3013 case DATA_TYPE_DOUBLE:
3014 ret = xdr_double(xdr, (void *)buf);
3015 break;
3016 #endif
3017 case DATA_TYPE_STRING:
3018 ret = xdr_string(xdr, &buf, buflen - 1);
3019 break;
3020
3021 case DATA_TYPE_BYTE_ARRAY:
3022 ret = xdr_opaque(xdr, buf, nelem);
3023 break;
3024
3025 case DATA_TYPE_INT8_ARRAY:
3026 case DATA_TYPE_UINT8_ARRAY:
3027 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
3028 (xdrproc_t)xdr_char);
3029 break;
3030
3031 case DATA_TYPE_INT16_ARRAY:
3032 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
3033 sizeof (int16_t), (xdrproc_t)xdr_short);
3034 break;
3035
3036 case DATA_TYPE_UINT16_ARRAY:
3037 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
3038 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
3039 break;
3040
3041 case DATA_TYPE_BOOLEAN_ARRAY:
3042 case DATA_TYPE_INT32_ARRAY:
3043 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
3044 sizeof (int32_t), (xdrproc_t)xdr_int);
3045 break;
3046
3047 case DATA_TYPE_UINT32_ARRAY:
3048 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3049 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3050 break;
3051
3052 case DATA_TYPE_INT64_ARRAY:
3053 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3054 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3055 break;
3056
3057 case DATA_TYPE_UINT64_ARRAY:
3058 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3059 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3060 break;
3061
3062 case DATA_TYPE_STRING_ARRAY: {
3063 size_t len = nelem * sizeof (uint64_t);
3064 char **strp = (void *)buf;
3065 int i;
3066
3067 if (nvs->nvs_op == NVS_OP_DECODE)
3068 bzero(buf, len); /* don't trust packed data */
3069
3070 for (i = 0; i < nelem; i++) {
3071 if (buflen <= len)
3072 return (EFAULT);
3073
3074 buf += len;
3075 buflen -= len;
3076
3077 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3078 return (EFAULT);
3079
3080 if (nvs->nvs_op == NVS_OP_DECODE)
3081 strp[i] = buf;
3082 len = strlen(buf) + 1;
3083 }
3084 ret = TRUE;
3085 break;
3086 }
3087 default:
3088 break;
3089 }
3090
3091 return (ret == TRUE ? 0 : EFAULT);
3092 }
3093
3094 static int
3095 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3096 {
3097 data_type_t type = NVP_TYPE(nvp);
3098 /*
3099 * encode_size + decode_size + name string size + data type + nelem
3100 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3101 */
3102 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3103
3104 switch (type) {
3105 case DATA_TYPE_BOOLEAN:
3106 break;
3107
3108 case DATA_TYPE_BOOLEAN_VALUE:
3109 case DATA_TYPE_BYTE:
3110 case DATA_TYPE_INT8:
3111 case DATA_TYPE_UINT8:
3112 case DATA_TYPE_INT16:
3113 case DATA_TYPE_UINT16:
3114 case DATA_TYPE_INT32:
3115 case DATA_TYPE_UINT32:
3116 nvp_sz += 4; /* 4 is the minimum xdr unit */
3117 break;
3118
3119 case DATA_TYPE_INT64:
3120 case DATA_TYPE_UINT64:
3121 case DATA_TYPE_HRTIME:
3122 #if !defined(_KERNEL)
3123 case DATA_TYPE_DOUBLE:
3124 #endif
3125 nvp_sz += 8;
3126 break;
3127
3128 case DATA_TYPE_STRING:
3129 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3130 break;
3131
3132 case DATA_TYPE_BYTE_ARRAY:
3133 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3134 break;
3135
3136 case DATA_TYPE_BOOLEAN_ARRAY:
3137 case DATA_TYPE_INT8_ARRAY:
3138 case DATA_TYPE_UINT8_ARRAY:
3139 case DATA_TYPE_INT16_ARRAY:
3140 case DATA_TYPE_UINT16_ARRAY:
3141 case DATA_TYPE_INT32_ARRAY:
3142 case DATA_TYPE_UINT32_ARRAY:
3143 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3144 break;
3145
3146 case DATA_TYPE_INT64_ARRAY:
3147 case DATA_TYPE_UINT64_ARRAY:
3148 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3149 break;
3150
3151 case DATA_TYPE_STRING_ARRAY: {
3152 int i;
3153 char **strs = (void *)NVP_VALUE(nvp);
3154
3155 for (i = 0; i < NVP_NELEM(nvp); i++)
3156 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3157
3158 break;
3159 }
3160
3161 case DATA_TYPE_NVLIST:
3162 case DATA_TYPE_NVLIST_ARRAY: {
3163 size_t nvsize = 0;
3164 int old_nvs_op = nvs->nvs_op;
3165 int err;
3166
3167 nvs->nvs_op = NVS_OP_GETSIZE;
3168 if (type == DATA_TYPE_NVLIST)
3169 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3170 else
3171 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3172 nvs->nvs_op = old_nvs_op;
3173
3174 if (err != 0)
3175 return (EINVAL);
3176
3177 nvp_sz += nvsize;
3178 break;
3179 }
3180
3181 default:
3182 return (EINVAL);
3183 }
3184
3185 if (nvp_sz > INT32_MAX)
3186 return (EINVAL);
3187
3188 *size = nvp_sz;
3189
3190 return (0);
3191 }
3192
3193
3194 /*
3195 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3196 * the largest nvpair that could be encoded in the buffer.
3197 *
3198 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3199 * The size of a xdr packed nvpair without any data is 5 words.
3200 *
3201 * Using the size of the data directly as an estimate would be ok
3202 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3203 * then the actual nvpair has space for an array of pointers to index
3204 * the strings. These pointers are not encoded into the packed xdr buffer.
3205 *
3206 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3207 * of length 0, then each string is endcoded in xdr format as a single word.
3208 * Therefore when expanded to an nvpair there will be 2.25 word used for
3209 * each string. (a int64_t allocated for pointer usage, and a single char
3210 * for the null termination.)
3211 *
3212 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3213 */
3214 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3215 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3216 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3217 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3218 (NVS_XDR_DATA_LEN(x) * 2) + \
3219 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3220
3221 static int
3222 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3223 {
3224 XDR *xdr = nvs->nvs_private;
3225 int32_t encode_len, decode_len;
3226
3227 switch (nvs->nvs_op) {
3228 case NVS_OP_ENCODE: {
3229 size_t nvsize;
3230
3231 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3232 return (EFAULT);
3233
3234 decode_len = nvp->nvp_size;
3235 encode_len = nvsize;
3236 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3237 return (EFAULT);
3238
3239 return (nvs_xdr_nvp_op(nvs, nvp));
3240 }
3241 case NVS_OP_DECODE: {
3242 struct xdr_bytesrec bytesrec;
3243
3244 /* get the encode and decode size */
3245 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3246 return (EFAULT);
3247 *size = decode_len;
3248
3249 /* are we at the end of the stream? */
3250 if (*size == 0)
3251 return (0);
3252
3253 /* sanity check the size parameter */
3254 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3255 return (EFAULT);
3256
3257 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3258 return (EFAULT);
3259 break;
3260 }
3261
3262 default:
3263 return (EINVAL);
3264 }
3265 return (0);
3266 }
3267
3268 static const struct nvs_ops nvs_xdr_ops = {
3269 nvs_xdr_nvlist,
3270 nvs_xdr_nvpair,
3271 nvs_xdr_nvp_op,
3272 nvs_xdr_nvp_size,
3273 nvs_xdr_nvl_fini
3274 };
3275
3276 static int
3277 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3278 {
3279 XDR xdr;
3280 int err;
3281
3282 nvs->nvs_ops = &nvs_xdr_ops;
3283
3284 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3285 *buflen - sizeof (nvs_header_t))) != 0)
3286 return (err);
3287
3288 err = nvs_operation(nvs, nvl, buflen);
3289
3290 nvs_xdr_destroy(nvs);
3291
3292 return (err);
3293 }
3294
3295 #if defined(_KERNEL) && defined(HAVE_SPL)
3296 static int __init
3297 nvpair_init(void)
3298 {
3299 return (0);
3300 }
3301
3302 static void __exit
3303 nvpair_fini(void)
3304 {
3305 }
3306
3307 module_init(nvpair_init);
3308 module_exit(nvpair_fini);
3309
3310 MODULE_DESCRIPTION("Generic name/value pair implementation");
3311 MODULE_AUTHOR(ZFS_META_AUTHOR);
3312 MODULE_LICENSE(ZFS_META_LICENSE);
3313 MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE);
3314
3315 EXPORT_SYMBOL(nv_alloc_init);
3316 EXPORT_SYMBOL(nv_alloc_reset);
3317 EXPORT_SYMBOL(nv_alloc_fini);
3318
3319 /* list management */
3320 EXPORT_SYMBOL(nvlist_alloc);
3321 EXPORT_SYMBOL(nvlist_free);
3322 EXPORT_SYMBOL(nvlist_size);
3323 EXPORT_SYMBOL(nvlist_pack);
3324 EXPORT_SYMBOL(nvlist_unpack);
3325 EXPORT_SYMBOL(nvlist_dup);
3326 EXPORT_SYMBOL(nvlist_merge);
3327
3328 EXPORT_SYMBOL(nvlist_xalloc);
3329 EXPORT_SYMBOL(nvlist_xpack);
3330 EXPORT_SYMBOL(nvlist_xunpack);
3331 EXPORT_SYMBOL(nvlist_xdup);
3332 EXPORT_SYMBOL(nvlist_lookup_nv_alloc);
3333
3334 EXPORT_SYMBOL(nvlist_add_nvpair);
3335 EXPORT_SYMBOL(nvlist_add_boolean);
3336 EXPORT_SYMBOL(nvlist_add_boolean_value);
3337 EXPORT_SYMBOL(nvlist_add_byte);
3338 EXPORT_SYMBOL(nvlist_add_int8);
3339 EXPORT_SYMBOL(nvlist_add_uint8);
3340 EXPORT_SYMBOL(nvlist_add_int16);
3341 EXPORT_SYMBOL(nvlist_add_uint16);
3342 EXPORT_SYMBOL(nvlist_add_int32);
3343 EXPORT_SYMBOL(nvlist_add_uint32);
3344 EXPORT_SYMBOL(nvlist_add_int64);
3345 EXPORT_SYMBOL(nvlist_add_uint64);
3346 EXPORT_SYMBOL(nvlist_add_string);
3347 EXPORT_SYMBOL(nvlist_add_nvlist);
3348 EXPORT_SYMBOL(nvlist_add_boolean_array);
3349 EXPORT_SYMBOL(nvlist_add_byte_array);
3350 EXPORT_SYMBOL(nvlist_add_int8_array);
3351 EXPORT_SYMBOL(nvlist_add_uint8_array);
3352 EXPORT_SYMBOL(nvlist_add_int16_array);
3353 EXPORT_SYMBOL(nvlist_add_uint16_array);
3354 EXPORT_SYMBOL(nvlist_add_int32_array);
3355 EXPORT_SYMBOL(nvlist_add_uint32_array);
3356 EXPORT_SYMBOL(nvlist_add_int64_array);
3357 EXPORT_SYMBOL(nvlist_add_uint64_array);
3358 EXPORT_SYMBOL(nvlist_add_string_array);
3359 EXPORT_SYMBOL(nvlist_add_nvlist_array);
3360 EXPORT_SYMBOL(nvlist_next_nvpair);
3361 EXPORT_SYMBOL(nvlist_prev_nvpair);
3362 EXPORT_SYMBOL(nvlist_empty);
3363 EXPORT_SYMBOL(nvlist_add_hrtime);
3364
3365 EXPORT_SYMBOL(nvlist_remove);
3366 EXPORT_SYMBOL(nvlist_remove_nvpair);
3367 EXPORT_SYMBOL(nvlist_remove_all);
3368
3369 EXPORT_SYMBOL(nvlist_lookup_boolean);
3370 EXPORT_SYMBOL(nvlist_lookup_boolean_value);
3371 EXPORT_SYMBOL(nvlist_lookup_byte);
3372 EXPORT_SYMBOL(nvlist_lookup_int8);
3373 EXPORT_SYMBOL(nvlist_lookup_uint8);
3374 EXPORT_SYMBOL(nvlist_lookup_int16);
3375 EXPORT_SYMBOL(nvlist_lookup_uint16);
3376 EXPORT_SYMBOL(nvlist_lookup_int32);
3377 EXPORT_SYMBOL(nvlist_lookup_uint32);
3378 EXPORT_SYMBOL(nvlist_lookup_int64);
3379 EXPORT_SYMBOL(nvlist_lookup_uint64);
3380 EXPORT_SYMBOL(nvlist_lookup_string);
3381 EXPORT_SYMBOL(nvlist_lookup_nvlist);
3382 EXPORT_SYMBOL(nvlist_lookup_boolean_array);
3383 EXPORT_SYMBOL(nvlist_lookup_byte_array);
3384 EXPORT_SYMBOL(nvlist_lookup_int8_array);
3385 EXPORT_SYMBOL(nvlist_lookup_uint8_array);
3386 EXPORT_SYMBOL(nvlist_lookup_int16_array);
3387 EXPORT_SYMBOL(nvlist_lookup_uint16_array);
3388 EXPORT_SYMBOL(nvlist_lookup_int32_array);
3389 EXPORT_SYMBOL(nvlist_lookup_uint32_array);
3390 EXPORT_SYMBOL(nvlist_lookup_int64_array);
3391 EXPORT_SYMBOL(nvlist_lookup_uint64_array);
3392 EXPORT_SYMBOL(nvlist_lookup_string_array);
3393 EXPORT_SYMBOL(nvlist_lookup_nvlist_array);
3394 EXPORT_SYMBOL(nvlist_lookup_hrtime);
3395 EXPORT_SYMBOL(nvlist_lookup_pairs);
3396
3397 EXPORT_SYMBOL(nvlist_lookup_nvpair);
3398 EXPORT_SYMBOL(nvlist_exists);
3399
3400 /* processing nvpair */
3401 EXPORT_SYMBOL(nvpair_name);
3402 EXPORT_SYMBOL(nvpair_type);
3403 EXPORT_SYMBOL(nvpair_value_boolean_value);
3404 EXPORT_SYMBOL(nvpair_value_byte);
3405 EXPORT_SYMBOL(nvpair_value_int8);
3406 EXPORT_SYMBOL(nvpair_value_uint8);
3407 EXPORT_SYMBOL(nvpair_value_int16);
3408 EXPORT_SYMBOL(nvpair_value_uint16);
3409 EXPORT_SYMBOL(nvpair_value_int32);
3410 EXPORT_SYMBOL(nvpair_value_uint32);
3411 EXPORT_SYMBOL(nvpair_value_int64);
3412 EXPORT_SYMBOL(nvpair_value_uint64);
3413 EXPORT_SYMBOL(nvpair_value_string);
3414 EXPORT_SYMBOL(nvpair_value_nvlist);
3415 EXPORT_SYMBOL(nvpair_value_boolean_array);
3416 EXPORT_SYMBOL(nvpair_value_byte_array);
3417 EXPORT_SYMBOL(nvpair_value_int8_array);
3418 EXPORT_SYMBOL(nvpair_value_uint8_array);
3419 EXPORT_SYMBOL(nvpair_value_int16_array);
3420 EXPORT_SYMBOL(nvpair_value_uint16_array);
3421 EXPORT_SYMBOL(nvpair_value_int32_array);
3422 EXPORT_SYMBOL(nvpair_value_uint32_array);
3423 EXPORT_SYMBOL(nvpair_value_int64_array);
3424 EXPORT_SYMBOL(nvpair_value_uint64_array);
3425 EXPORT_SYMBOL(nvpair_value_string_array);
3426 EXPORT_SYMBOL(nvpair_value_nvlist_array);
3427 EXPORT_SYMBOL(nvpair_value_hrtime);
3428
3429 #endif