Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / cddl / osnet / dist / common / nvpair / nvpair.c
blob77891bf7764451de33b5bd18dfeb4453c40cd47b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <sys/stropts.h>
30 #include <sys/debug.h>
31 #include <sys/isa_defs.h>
32 #include <sys/int_limits.h>
33 #include <sys/nvpair.h>
34 #include <sys/nvpair_impl.h>
35 #include <rpc/types.h>
36 #include <rpc/xdr.h>
38 #if defined(_KERNEL) && !defined(_BOOT)
39 #include <sys/varargs.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #else
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
47 #endif
49 #ifndef offsetof
50 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
51 #endif
52 #define skip_whitespace(p) while ((*(p) == ' ') || (*(p) == '\t')) p++
55 * nvpair.c - Provides kernel & userland interfaces for manipulating
56 * name-value pairs.
58 * Overview Diagram
60 * +--------------+
61 * | nvlist_t |
62 * |--------------|
63 * | nvl_version |
64 * | nvl_nvflag |
65 * | nvl_priv -+-+
66 * | nvl_flag | |
67 * | nvl_pad | |
68 * +--------------+ |
69 * V
70 * +--------------+ last i_nvp in list
71 * | nvpriv_t | +--------------------->
72 * |--------------| |
73 * +--+- nvp_list | | +------------+
74 * | | nvp_last -+--+ + nv_alloc_t |
75 * | | nvp_curr | |------------|
76 * | | nvp_nva -+----> | nva_ops |
77 * | | nvp_stat | | nva_arg |
78 * | +--------------+ +------------+
79 * |
80 * +-------+
81 * V
82 * +---------------------+ +-------------------+
83 * | i_nvp_t | +-->| i_nvp_t | +-->
84 * |---------------------| | |-------------------| |
85 * | nvi_next -+--+ | nvi_next -+--+
86 * | nvi_prev (NULL) | <----+ nvi_prev |
87 * | . . . . . . . . . . | | . . . . . . . . . |
88 * | nvp (nvpair_t) | | nvp (nvpair_t) |
89 * | - nvp_size | | - nvp_size |
90 * | - nvp_name_sz | | - nvp_name_sz |
91 * | - nvp_value_elem | | - nvp_value_elem |
92 * | - nvp_type | | - nvp_type |
93 * | - data ... | | - data ... |
94 * +---------------------+ +-------------------+
98 * +---------------------+ +---------------------+
99 * | i_nvp_t | +--> +-->| i_nvp_t (last) |
100 * |---------------------| | | |---------------------|
101 * | nvi_next -+--+ ... --+ | nvi_next (NULL) |
102 * <-+- nvi_prev |<-- ... <----+ nvi_prev |
103 * | . . . . . . . . . | | . . . . . . . . . |
104 * | nvp (nvpair_t) | | nvp (nvpair_t) |
105 * | - nvp_size | | - nvp_size |
106 * | - nvp_name_sz | | - nvp_name_sz |
107 * | - nvp_value_elem | | - nvp_value_elem |
108 * | - DATA_TYPE_NVLIST | | - nvp_type |
109 * | - data (embedded) | | - data ... |
110 * | nvlist name | +---------------------+
111 * | +--------------+ |
112 * | | nvlist_t | |
113 * | |--------------| |
114 * | | nvl_version | |
115 * | | nvl_nvflag | |
116 * | | nvl_priv --+---+---->
117 * | | nvl_flag | |
118 * | | nvl_pad | |
119 * | +--------------+ |
120 * +---------------------+
123 * N.B. nvpair_t may be aligned on 4 byte boundary, so +4 will
124 * allow value to be aligned on 8 byte boundary
126 * name_len is the length of the name string including the null terminator
127 * so it must be >= 1
129 #define NVP_SIZE_CALC(name_len, data_len) \
130 (NV_ALIGN((sizeof (nvpair_t)) + name_len) + NV_ALIGN(data_len))
132 static int i_get_value_size(data_type_t type, const void *data, uint_t nelem);
133 static int nvlist_add_common(nvlist_t *nvl, const char *name, data_type_t type,
134 uint_t nelem, const void *data);
136 #define NV_STAT_EMBEDDED 0x1
137 #define EMBEDDED_NVL(nvp) ((nvlist_t *)(void *)NVP_VALUE(nvp))
138 #define EMBEDDED_NVL_ARRAY(nvp) ((nvlist_t **)(void *)NVP_VALUE(nvp))
140 #define NVP_VALOFF(nvp) (NV_ALIGN(sizeof (nvpair_t) + (nvp)->nvp_name_sz))
141 #define NVPAIR2I_NVP(nvp) \
142 ((i_nvp_t *)((size_t)(nvp) - offsetof(i_nvp_t, nvi_nvp)))
146 nv_alloc_init(nv_alloc_t *nva, const nv_alloc_ops_t *nvo, /* args */ ...)
148 va_list valist;
149 int err = 0;
151 nva->nva_ops = nvo;
152 nva->nva_arg = NULL;
154 va_start(valist, nvo);
155 if (nva->nva_ops->nv_ao_init != NULL)
156 err = nva->nva_ops->nv_ao_init(nva, valist);
157 va_end(valist);
159 return (err);
162 void
163 nv_alloc_reset(nv_alloc_t *nva)
165 if (nva->nva_ops->nv_ao_reset != NULL)
166 nva->nva_ops->nv_ao_reset(nva);
169 void
170 nv_alloc_fini(nv_alloc_t *nva)
172 if (nva->nva_ops->nv_ao_fini != NULL)
173 nva->nva_ops->nv_ao_fini(nva);
176 nv_alloc_t *
177 nvlist_lookup_nv_alloc(nvlist_t *nvl)
179 nvpriv_t *priv;
181 if (nvl == NULL ||
182 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
183 return (NULL);
185 return (priv->nvp_nva);
188 static void *
189 nv_mem_zalloc(nvpriv_t *nvp, size_t size)
191 nv_alloc_t *nva = nvp->nvp_nva;
192 void *buf;
194 if ((buf = nva->nva_ops->nv_ao_alloc(nva, size)) != NULL)
195 bzero(buf, size);
197 return (buf);
200 static void
201 nv_mem_free(nvpriv_t *nvp, void *buf, size_t size)
203 nv_alloc_t *nva = nvp->nvp_nva;
205 nva->nva_ops->nv_ao_free(nva, buf, size);
208 static void
209 nv_priv_init(nvpriv_t *priv, nv_alloc_t *nva, uint32_t stat)
211 bzero(priv, sizeof (nvpriv_t));
213 priv->nvp_nva = nva;
214 priv->nvp_stat = stat;
217 static nvpriv_t *
218 nv_priv_alloc(nv_alloc_t *nva)
220 nvpriv_t *priv;
223 * nv_mem_alloc() cannot called here because it needs the priv
224 * argument.
226 if ((priv = nva->nva_ops->nv_ao_alloc(nva, sizeof (nvpriv_t))) == NULL)
227 return (NULL);
229 nv_priv_init(priv, nva, 0);
231 return (priv);
235 * Embedded lists need their own nvpriv_t's. We create a new
236 * nvpriv_t using the parameters and allocator from the parent
237 * list's nvpriv_t.
239 static nvpriv_t *
240 nv_priv_alloc_embedded(nvpriv_t *priv)
242 nvpriv_t *emb_priv;
244 if ((emb_priv = nv_mem_zalloc(priv, sizeof (nvpriv_t))) == NULL)
245 return (NULL);
247 nv_priv_init(emb_priv, priv->nvp_nva, NV_STAT_EMBEDDED);
249 return (emb_priv);
252 static void
253 nvlist_init(nvlist_t *nvl, uint32_t nvflag, nvpriv_t *priv)
255 nvl->nvl_version = NV_VERSION;
256 nvl->nvl_nvflag = nvflag & (NV_UNIQUE_NAME|NV_UNIQUE_NAME_TYPE);
257 nvl->nvl_priv = (uint64_t)(uintptr_t)priv;
258 nvl->nvl_flag = 0;
259 nvl->nvl_pad = 0;
263 * nvlist_alloc - Allocate nvlist.
265 /*ARGSUSED1*/
267 nvlist_alloc(nvlist_t **nvlp, uint_t nvflag, int kmflag)
269 #if defined(_KERNEL) && !defined(_BOOT)
270 return (nvlist_xalloc(nvlp, nvflag,
271 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
272 #else
273 return (nvlist_xalloc(nvlp, nvflag, nv_alloc_nosleep));
274 #endif
278 nvlist_xalloc(nvlist_t **nvlp, uint_t nvflag, nv_alloc_t *nva)
280 nvpriv_t *priv;
282 if (nvlp == NULL || nva == NULL)
283 return (EINVAL);
285 if ((priv = nv_priv_alloc(nva)) == NULL)
286 return (ENOMEM);
288 if ((*nvlp = nv_mem_zalloc(priv,
289 NV_ALIGN(sizeof (nvlist_t)))) == NULL) {
290 nv_mem_free(priv, priv, sizeof (nvpriv_t));
291 return (ENOMEM);
294 nvlist_init(*nvlp, nvflag, priv);
296 return (0);
300 * nvp_buf_alloc - Allocate i_nvp_t for storing a new nv pair.
302 static nvpair_t *
303 nvp_buf_alloc(nvlist_t *nvl, size_t len)
305 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
306 i_nvp_t *buf;
307 nvpair_t *nvp;
308 size_t nvsize;
311 * Allocate the buffer
313 nvsize = len + offsetof(i_nvp_t, nvi_nvp);
315 if ((buf = nv_mem_zalloc(priv, nvsize)) == NULL)
316 return (NULL);
318 nvp = &buf->nvi_nvp;
319 nvp->nvp_size = len;
321 return (nvp);
325 * nvp_buf_free - de-Allocate an i_nvp_t.
327 static void
328 nvp_buf_free(nvlist_t *nvl, nvpair_t *nvp)
330 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
331 size_t nvsize = nvp->nvp_size + offsetof(i_nvp_t, nvi_nvp);
333 nv_mem_free(priv, NVPAIR2I_NVP(nvp), nvsize);
337 * nvp_buf_link - link a new nv pair into the nvlist.
339 static void
340 nvp_buf_link(nvlist_t *nvl, nvpair_t *nvp)
342 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
343 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
345 /* Put element at end of nvlist */
346 if (priv->nvp_list == NULL) {
347 priv->nvp_list = priv->nvp_last = curr;
348 } else {
349 curr->nvi_prev = priv->nvp_last;
350 priv->nvp_last->nvi_next = curr;
351 priv->nvp_last = curr;
356 * nvp_buf_unlink - unlink an removed nvpair out of the nvlist.
358 static void
359 nvp_buf_unlink(nvlist_t *nvl, nvpair_t *nvp)
361 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
362 i_nvp_t *curr = NVPAIR2I_NVP(nvp);
365 * protect nvlist_next_nvpair() against walking on freed memory.
367 if (priv->nvp_curr == curr)
368 priv->nvp_curr = curr->nvi_next;
370 if (curr == priv->nvp_list)
371 priv->nvp_list = curr->nvi_next;
372 else
373 curr->nvi_prev->nvi_next = curr->nvi_next;
375 if (curr == priv->nvp_last)
376 priv->nvp_last = curr->nvi_prev;
377 else
378 curr->nvi_next->nvi_prev = curr->nvi_prev;
382 * take a nvpair type and number of elements and make sure the are valid
384 static int
385 i_validate_type_nelem(data_type_t type, uint_t nelem)
387 switch (type) {
388 case DATA_TYPE_BOOLEAN:
389 if (nelem != 0)
390 return (EINVAL);
391 break;
392 case DATA_TYPE_BOOLEAN_VALUE:
393 case DATA_TYPE_BYTE:
394 case DATA_TYPE_INT8:
395 case DATA_TYPE_UINT8:
396 case DATA_TYPE_INT16:
397 case DATA_TYPE_UINT16:
398 case DATA_TYPE_INT32:
399 case DATA_TYPE_UINT32:
400 case DATA_TYPE_INT64:
401 case DATA_TYPE_UINT64:
402 case DATA_TYPE_STRING:
403 case DATA_TYPE_HRTIME:
404 case DATA_TYPE_NVLIST:
405 #if !defined(_KERNEL)
406 case DATA_TYPE_DOUBLE:
407 #endif
408 if (nelem != 1)
409 return (EINVAL);
410 break;
411 case DATA_TYPE_BOOLEAN_ARRAY:
412 case DATA_TYPE_BYTE_ARRAY:
413 case DATA_TYPE_INT8_ARRAY:
414 case DATA_TYPE_UINT8_ARRAY:
415 case DATA_TYPE_INT16_ARRAY:
416 case DATA_TYPE_UINT16_ARRAY:
417 case DATA_TYPE_INT32_ARRAY:
418 case DATA_TYPE_UINT32_ARRAY:
419 case DATA_TYPE_INT64_ARRAY:
420 case DATA_TYPE_UINT64_ARRAY:
421 case DATA_TYPE_STRING_ARRAY:
422 case DATA_TYPE_NVLIST_ARRAY:
423 /* we allow arrays with 0 elements */
424 break;
425 default:
426 return (EINVAL);
428 return (0);
432 * Verify nvp_name_sz and check the name string length.
434 static int
435 i_validate_nvpair_name(nvpair_t *nvp)
437 if ((nvp->nvp_name_sz <= 0) ||
438 (nvp->nvp_size < NVP_SIZE_CALC(nvp->nvp_name_sz, 0)))
439 return (EFAULT);
441 /* verify the name string, make sure its terminated */
442 if (NVP_NAME(nvp)[nvp->nvp_name_sz - 1] != '\0')
443 return (EFAULT);
445 return (strlen(NVP_NAME(nvp)) == nvp->nvp_name_sz - 1 ? 0 : EFAULT);
448 static int
449 i_validate_nvpair_value(data_type_t type, uint_t nelem, const void *data)
451 switch (type) {
452 case DATA_TYPE_BOOLEAN_VALUE:
453 if (*(boolean_t *)data != B_TRUE &&
454 *(boolean_t *)data != B_FALSE)
455 return (EINVAL);
456 break;
457 case DATA_TYPE_BOOLEAN_ARRAY: {
458 int i;
460 for (i = 0; i < nelem; i++)
461 if (((boolean_t *)data)[i] != B_TRUE &&
462 ((boolean_t *)data)[i] != B_FALSE)
463 return (EINVAL);
464 break;
466 default:
467 break;
470 return (0);
474 * This function takes a pointer to what should be a nvpair and it's size
475 * and then verifies that all the nvpair fields make sense and can be
476 * trusted. This function is used when decoding packed nvpairs.
478 static int
479 i_validate_nvpair(nvpair_t *nvp)
481 data_type_t type = NVP_TYPE(nvp);
482 int size1, size2;
484 /* verify nvp_name_sz, check the name string length */
485 if (i_validate_nvpair_name(nvp) != 0)
486 return (EFAULT);
488 if (i_validate_nvpair_value(type, NVP_NELEM(nvp), NVP_VALUE(nvp)) != 0)
489 return (EFAULT);
492 * verify nvp_type, nvp_value_elem, and also possibly
493 * verify string values and get the value size.
495 size2 = i_get_value_size(type, NVP_VALUE(nvp), NVP_NELEM(nvp));
496 size1 = nvp->nvp_size - NVP_VALOFF(nvp);
497 if (size2 < 0 || size1 != NV_ALIGN(size2))
498 return (EFAULT);
500 return (0);
503 static int
504 nvlist_copy_pairs(nvlist_t *snvl, nvlist_t *dnvl)
506 nvpriv_t *priv;
507 i_nvp_t *curr;
509 if ((priv = (nvpriv_t *)(uintptr_t)snvl->nvl_priv) == NULL)
510 return (EINVAL);
512 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
513 nvpair_t *nvp = &curr->nvi_nvp;
514 int err;
516 if ((err = nvlist_add_common(dnvl, NVP_NAME(nvp), NVP_TYPE(nvp),
517 NVP_NELEM(nvp), NVP_VALUE(nvp))) != 0)
518 return (err);
521 return (0);
525 * Frees all memory allocated for an nvpair (like embedded lists) with
526 * the exception of the nvpair buffer itself.
528 static void
529 nvpair_free(nvpair_t *nvp)
531 switch (NVP_TYPE(nvp)) {
532 case DATA_TYPE_NVLIST:
533 nvlist_free(EMBEDDED_NVL(nvp));
534 break;
535 case DATA_TYPE_NVLIST_ARRAY: {
536 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
537 int i;
539 for (i = 0; i < NVP_NELEM(nvp); i++)
540 if (nvlp[i] != NULL)
541 nvlist_free(nvlp[i]);
542 break;
544 default:
545 break;
550 * nvlist_free - free an unpacked nvlist
552 void
553 nvlist_free(nvlist_t *nvl)
555 nvpriv_t *priv;
556 i_nvp_t *curr;
558 if (nvl == NULL ||
559 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
560 return;
563 * Unpacked nvlist are linked through i_nvp_t
565 curr = priv->nvp_list;
566 while (curr != NULL) {
567 nvpair_t *nvp = &curr->nvi_nvp;
568 curr = curr->nvi_next;
570 nvpair_free(nvp);
571 nvp_buf_free(nvl, nvp);
574 if (!(priv->nvp_stat & NV_STAT_EMBEDDED))
575 nv_mem_free(priv, nvl, NV_ALIGN(sizeof (nvlist_t)));
576 else
577 nvl->nvl_priv = 0;
579 nv_mem_free(priv, priv, sizeof (nvpriv_t));
582 static int
583 nvlist_contains_nvp(nvlist_t *nvl, nvpair_t *nvp)
585 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
586 i_nvp_t *curr;
588 if (nvp == NULL)
589 return (0);
591 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
592 if (&curr->nvi_nvp == nvp)
593 return (1);
595 return (0);
599 * Make a copy of nvlist
601 /*ARGSUSED1*/
603 nvlist_dup(nvlist_t *nvl, nvlist_t **nvlp, int kmflag)
605 #if defined(_KERNEL) && !defined(_BOOT)
606 return (nvlist_xdup(nvl, nvlp,
607 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
608 #else
609 return (nvlist_xdup(nvl, nvlp, nv_alloc_nosleep));
610 #endif
614 nvlist_xdup(nvlist_t *nvl, nvlist_t **nvlp, nv_alloc_t *nva)
616 int err;
617 nvlist_t *ret;
619 if (nvl == NULL || nvlp == NULL)
620 return (EINVAL);
622 if ((err = nvlist_xalloc(&ret, nvl->nvl_nvflag, nva)) != 0)
623 return (err);
625 if ((err = nvlist_copy_pairs(nvl, ret)) != 0)
626 nvlist_free(ret);
627 else
628 *nvlp = ret;
630 return (err);
634 * Remove all with matching name
637 nvlist_remove_all(nvlist_t *nvl, const char *name)
639 nvpriv_t *priv;
640 i_nvp_t *curr;
641 int error = ENOENT;
643 if (nvl == NULL || name == NULL ||
644 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
645 return (EINVAL);
647 curr = priv->nvp_list;
648 while (curr != NULL) {
649 nvpair_t *nvp = &curr->nvi_nvp;
651 curr = curr->nvi_next;
652 if (strcmp(name, NVP_NAME(nvp)) != 0)
653 continue;
655 nvp_buf_unlink(nvl, nvp);
656 nvpair_free(nvp);
657 nvp_buf_free(nvl, nvp);
659 error = 0;
662 return (error);
666 * Remove first one with matching name and type
669 nvlist_remove(nvlist_t *nvl, const char *name, data_type_t type)
671 nvpriv_t *priv;
672 i_nvp_t *curr;
674 if (nvl == NULL || name == NULL ||
675 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
676 return (EINVAL);
678 curr = priv->nvp_list;
679 while (curr != NULL) {
680 nvpair_t *nvp = &curr->nvi_nvp;
682 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type) {
683 nvp_buf_unlink(nvl, nvp);
684 nvpair_free(nvp);
685 nvp_buf_free(nvl, nvp);
687 return (0);
689 curr = curr->nvi_next;
692 return (ENOENT);
696 * This function calculates the size of an nvpair value.
698 * The data argument controls the behavior in case of the data types
699 * DATA_TYPE_STRING and
700 * DATA_TYPE_STRING_ARRAY
701 * Is data == NULL then the size of the string(s) is excluded.
703 static int
704 i_get_value_size(data_type_t type, const void *data, uint_t nelem)
706 uint64_t value_sz;
708 if (i_validate_type_nelem(type, nelem) != 0)
709 return (-1);
711 /* Calculate required size for holding value */
712 switch (type) {
713 case DATA_TYPE_BOOLEAN:
714 value_sz = 0;
715 break;
716 case DATA_TYPE_BOOLEAN_VALUE:
717 value_sz = sizeof (boolean_t);
718 break;
719 case DATA_TYPE_BYTE:
720 value_sz = sizeof (uchar_t);
721 break;
722 case DATA_TYPE_INT8:
723 value_sz = sizeof (int8_t);
724 break;
725 case DATA_TYPE_UINT8:
726 value_sz = sizeof (uint8_t);
727 break;
728 case DATA_TYPE_INT16:
729 value_sz = sizeof (int16_t);
730 break;
731 case DATA_TYPE_UINT16:
732 value_sz = sizeof (uint16_t);
733 break;
734 case DATA_TYPE_INT32:
735 value_sz = sizeof (int32_t);
736 break;
737 case DATA_TYPE_UINT32:
738 value_sz = sizeof (uint32_t);
739 break;
740 case DATA_TYPE_INT64:
741 value_sz = sizeof (int64_t);
742 break;
743 case DATA_TYPE_UINT64:
744 value_sz = sizeof (uint64_t);
745 break;
746 #if !defined(_KERNEL)
747 case DATA_TYPE_DOUBLE:
748 value_sz = sizeof (double);
749 break;
750 #endif
751 case DATA_TYPE_STRING:
752 if (data == NULL)
753 value_sz = 0;
754 else
755 value_sz = strlen(data) + 1;
756 break;
757 case DATA_TYPE_BOOLEAN_ARRAY:
758 value_sz = (uint64_t)nelem * sizeof (boolean_t);
759 break;
760 case DATA_TYPE_BYTE_ARRAY:
761 value_sz = (uint64_t)nelem * sizeof (uchar_t);
762 break;
763 case DATA_TYPE_INT8_ARRAY:
764 value_sz = (uint64_t)nelem * sizeof (int8_t);
765 break;
766 case DATA_TYPE_UINT8_ARRAY:
767 value_sz = (uint64_t)nelem * sizeof (uint8_t);
768 break;
769 case DATA_TYPE_INT16_ARRAY:
770 value_sz = (uint64_t)nelem * sizeof (int16_t);
771 break;
772 case DATA_TYPE_UINT16_ARRAY:
773 value_sz = (uint64_t)nelem * sizeof (uint16_t);
774 break;
775 case DATA_TYPE_INT32_ARRAY:
776 value_sz = (uint64_t)nelem * sizeof (int32_t);
777 break;
778 case DATA_TYPE_UINT32_ARRAY:
779 value_sz = (uint64_t)nelem * sizeof (uint32_t);
780 break;
781 case DATA_TYPE_INT64_ARRAY:
782 value_sz = (uint64_t)nelem * sizeof (int64_t);
783 break;
784 case DATA_TYPE_UINT64_ARRAY:
785 value_sz = (uint64_t)nelem * sizeof (uint64_t);
786 break;
787 case DATA_TYPE_STRING_ARRAY:
788 value_sz = (uint64_t)nelem * sizeof (uint64_t);
790 if (data != NULL) {
791 char *const *strs = data;
792 uint_t i;
794 /* no alignment requirement for strings */
795 for (i = 0; i < nelem; i++) {
796 if (strs[i] == NULL)
797 return (-1);
798 value_sz += strlen(strs[i]) + 1;
801 break;
802 case DATA_TYPE_HRTIME:
803 value_sz = sizeof (hrtime_t);
804 break;
805 case DATA_TYPE_NVLIST:
806 value_sz = NV_ALIGN(sizeof (nvlist_t));
807 break;
808 case DATA_TYPE_NVLIST_ARRAY:
809 value_sz = (uint64_t)nelem * sizeof (uint64_t) +
810 (uint64_t)nelem * NV_ALIGN(sizeof (nvlist_t));
811 break;
812 default:
813 return (-1);
816 return (value_sz > INT32_MAX ? -1 : (int)value_sz);
819 static int
820 nvlist_copy_embedded(nvlist_t *nvl, nvlist_t *onvl, nvlist_t *emb_nvl)
822 nvpriv_t *priv;
823 int err;
825 if ((priv = nv_priv_alloc_embedded((nvpriv_t *)(uintptr_t)
826 nvl->nvl_priv)) == NULL)
827 return (ENOMEM);
829 nvlist_init(emb_nvl, onvl->nvl_nvflag, priv);
831 if ((err = nvlist_copy_pairs(onvl, emb_nvl)) != 0) {
832 nvlist_free(emb_nvl);
833 emb_nvl->nvl_priv = 0;
836 return (err);
840 * nvlist_add_common - Add new <name,value> pair to nvlist
842 static int
843 nvlist_add_common(nvlist_t *nvl, const char *name,
844 data_type_t type, uint_t nelem, const void *data)
846 nvpair_t *nvp;
847 uint_t i;
849 int nvp_sz, name_sz, value_sz;
850 int err = 0;
852 if (name == NULL || nvl == NULL || nvl->nvl_priv == 0)
853 return (EINVAL);
855 if (nelem != 0 && data == NULL)
856 return (EINVAL);
859 * Verify type and nelem and get the value size.
860 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
861 * is the size of the string(s) included.
863 if ((value_sz = i_get_value_size(type, data, nelem)) < 0)
864 return (EINVAL);
866 if (i_validate_nvpair_value(type, nelem, data) != 0)
867 return (EINVAL);
870 * If we're adding an nvlist or nvlist array, ensure that we are not
871 * adding the input nvlist to itself, which would cause recursion,
872 * and ensure that no NULL nvlist pointers are present.
874 switch (type) {
875 case DATA_TYPE_NVLIST:
876 if (data == nvl || data == NULL)
877 return (EINVAL);
878 break;
879 case DATA_TYPE_NVLIST_ARRAY: {
880 nvlist_t **onvlp = (nvlist_t **)data;
881 for (i = 0; i < nelem; i++) {
882 if (onvlp[i] == nvl || onvlp[i] == NULL)
883 return (EINVAL);
885 break;
887 default:
888 break;
891 /* calculate sizes of the nvpair elements and the nvpair itself */
892 name_sz = strlen(name) + 1;
894 nvp_sz = NVP_SIZE_CALC(name_sz, value_sz);
896 if ((nvp = nvp_buf_alloc(nvl, nvp_sz)) == NULL)
897 return (ENOMEM);
899 ASSERT(nvp->nvp_size == nvp_sz);
900 nvp->nvp_name_sz = name_sz;
901 nvp->nvp_value_elem = nelem;
902 nvp->nvp_type = type;
903 bcopy(name, NVP_NAME(nvp), name_sz);
905 switch (type) {
906 case DATA_TYPE_BOOLEAN:
907 break;
908 case DATA_TYPE_STRING_ARRAY: {
909 char *const *strs = data;
910 char *buf = NVP_VALUE(nvp);
911 char **cstrs = (void *)buf;
913 /* skip pre-allocated space for pointer array */
914 buf += nelem * sizeof (uint64_t);
915 for (i = 0; i < nelem; i++) {
916 int slen = strlen(strs[i]) + 1;
917 bcopy(strs[i], buf, slen);
918 cstrs[i] = buf;
919 buf += slen;
921 break;
923 case DATA_TYPE_NVLIST: {
924 nvlist_t *nnvl = EMBEDDED_NVL(nvp);
925 nvlist_t *onvl = (nvlist_t *)data;
927 if ((err = nvlist_copy_embedded(nvl, onvl, nnvl)) != 0) {
928 nvp_buf_free(nvl, nvp);
929 return (err);
931 break;
933 case DATA_TYPE_NVLIST_ARRAY: {
934 nvlist_t **onvlp = (nvlist_t **)data;
935 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
936 nvlist_t *embedded = (nvlist_t *)
937 ((uintptr_t)nvlp + nelem * sizeof (uint64_t));
939 for (i = 0; i < nelem; i++) {
940 if ((err = nvlist_copy_embedded(nvl,
941 onvlp[i], embedded)) != 0) {
943 * Free any successfully created lists
945 nvpair_free(nvp);
946 nvp_buf_free(nvl, nvp);
947 return (err);
950 nvlp[i] = embedded++;
952 break;
954 default:
955 bcopy(data, NVP_VALUE(nvp), value_sz);
958 /* if unique name, remove before add */
959 if (nvl->nvl_nvflag & NV_UNIQUE_NAME)
960 (void) nvlist_remove_all(nvl, name);
961 else if (nvl->nvl_nvflag & NV_UNIQUE_NAME_TYPE)
962 (void) nvlist_remove(nvl, name, type);
964 nvp_buf_link(nvl, nvp);
966 return (0);
970 nvlist_add_boolean(nvlist_t *nvl, const char *name)
972 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN, 0, NULL));
976 nvlist_add_boolean_value(nvlist_t *nvl, const char *name, boolean_t val)
978 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_VALUE, 1, &val));
982 nvlist_add_byte(nvlist_t *nvl, const char *name, uchar_t val)
984 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE, 1, &val));
988 nvlist_add_int8(nvlist_t *nvl, const char *name, int8_t val)
990 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8, 1, &val));
994 nvlist_add_uint8(nvlist_t *nvl, const char *name, uint8_t val)
996 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8, 1, &val));
1000 nvlist_add_int16(nvlist_t *nvl, const char *name, int16_t val)
1002 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16, 1, &val));
1006 nvlist_add_uint16(nvlist_t *nvl, const char *name, uint16_t val)
1008 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16, 1, &val));
1012 nvlist_add_int32(nvlist_t *nvl, const char *name, int32_t val)
1014 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32, 1, &val));
1018 nvlist_add_uint32(nvlist_t *nvl, const char *name, uint32_t val)
1020 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32, 1, &val));
1024 nvlist_add_int64(nvlist_t *nvl, const char *name, int64_t val)
1026 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64, 1, &val));
1030 nvlist_add_uint64(nvlist_t *nvl, const char *name, uint64_t val)
1032 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64, 1, &val));
1035 #if !defined(_KERNEL)
1037 nvlist_add_double(nvlist_t *nvl, const char *name, double val)
1039 return (nvlist_add_common(nvl, name, DATA_TYPE_DOUBLE, 1, &val));
1041 #endif
1044 nvlist_add_string(nvlist_t *nvl, const char *name, const char *val)
1046 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING, 1, (void *)val));
1050 nvlist_add_boolean_array(nvlist_t *nvl, const char *name,
1051 boolean_t *a, uint_t n)
1053 return (nvlist_add_common(nvl, name, DATA_TYPE_BOOLEAN_ARRAY, n, a));
1057 nvlist_add_byte_array(nvlist_t *nvl, const char *name, uchar_t *a, uint_t n)
1059 return (nvlist_add_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1063 nvlist_add_int8_array(nvlist_t *nvl, const char *name, int8_t *a, uint_t n)
1065 return (nvlist_add_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1069 nvlist_add_uint8_array(nvlist_t *nvl, const char *name, uint8_t *a, uint_t n)
1071 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1075 nvlist_add_int16_array(nvlist_t *nvl, const char *name, int16_t *a, uint_t n)
1077 return (nvlist_add_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1081 nvlist_add_uint16_array(nvlist_t *nvl, const char *name, uint16_t *a, uint_t n)
1083 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1087 nvlist_add_int32_array(nvlist_t *nvl, const char *name, int32_t *a, uint_t n)
1089 return (nvlist_add_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1093 nvlist_add_uint32_array(nvlist_t *nvl, const char *name, uint32_t *a, uint_t n)
1095 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1099 nvlist_add_int64_array(nvlist_t *nvl, const char *name, int64_t *a, uint_t n)
1101 return (nvlist_add_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1105 nvlist_add_uint64_array(nvlist_t *nvl, const char *name, uint64_t *a, uint_t n)
1107 return (nvlist_add_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1111 nvlist_add_string_array(nvlist_t *nvl, const char *name,
1112 char *const *a, uint_t n)
1114 return (nvlist_add_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1118 nvlist_add_hrtime(nvlist_t *nvl, const char *name, hrtime_t val)
1120 return (nvlist_add_common(nvl, name, DATA_TYPE_HRTIME, 1, &val));
1124 nvlist_add_nvlist(nvlist_t *nvl, const char *name, nvlist_t *val)
1126 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST, 1, val));
1130 nvlist_add_nvlist_array(nvlist_t *nvl, const char *name, nvlist_t **a, uint_t n)
1132 return (nvlist_add_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1135 /* reading name-value pairs */
1136 nvpair_t *
1137 nvlist_next_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1139 nvpriv_t *priv;
1140 i_nvp_t *curr;
1142 if (nvl == NULL ||
1143 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1144 return (NULL);
1146 curr = NVPAIR2I_NVP(nvp);
1149 * Ensure that nvp is a valid nvpair on this nvlist.
1150 * NB: nvp_curr is used only as a hint so that we don't always
1151 * have to walk the list to determine if nvp is still on the list.
1153 if (nvp == NULL)
1154 curr = priv->nvp_list;
1155 else if (priv->nvp_curr == curr || nvlist_contains_nvp(nvl, nvp))
1156 curr = curr->nvi_next;
1157 else
1158 curr = NULL;
1160 priv->nvp_curr = curr;
1162 return (curr != NULL ? &curr->nvi_nvp : NULL);
1165 char *
1166 nvpair_name(nvpair_t *nvp)
1168 return (NVP_NAME(nvp));
1171 data_type_t
1172 nvpair_type(nvpair_t *nvp)
1174 return (NVP_TYPE(nvp));
1178 nvpair_type_is_array(nvpair_t *nvp)
1180 data_type_t type = NVP_TYPE(nvp);
1182 if ((type == DATA_TYPE_BYTE_ARRAY) ||
1183 (type == DATA_TYPE_UINT8_ARRAY) ||
1184 (type == DATA_TYPE_INT16_ARRAY) ||
1185 (type == DATA_TYPE_UINT16_ARRAY) ||
1186 (type == DATA_TYPE_INT32_ARRAY) ||
1187 (type == DATA_TYPE_UINT32_ARRAY) ||
1188 (type == DATA_TYPE_INT64_ARRAY) ||
1189 (type == DATA_TYPE_UINT64_ARRAY) ||
1190 (type == DATA_TYPE_BOOLEAN_ARRAY) ||
1191 (type == DATA_TYPE_STRING_ARRAY) ||
1192 (type == DATA_TYPE_NVLIST_ARRAY))
1193 return (1);
1194 return (0);
1198 static int
1199 nvpair_value_common(nvpair_t *nvp, data_type_t type, uint_t *nelem, void *data)
1201 if (nvp == NULL || nvpair_type(nvp) != type)
1202 return (EINVAL);
1205 * For non-array types, we copy the data.
1206 * For array types (including string), we set a pointer.
1208 switch (type) {
1209 case DATA_TYPE_BOOLEAN:
1210 if (nelem != NULL)
1211 *nelem = 0;
1212 break;
1214 case DATA_TYPE_BOOLEAN_VALUE:
1215 case DATA_TYPE_BYTE:
1216 case DATA_TYPE_INT8:
1217 case DATA_TYPE_UINT8:
1218 case DATA_TYPE_INT16:
1219 case DATA_TYPE_UINT16:
1220 case DATA_TYPE_INT32:
1221 case DATA_TYPE_UINT32:
1222 case DATA_TYPE_INT64:
1223 case DATA_TYPE_UINT64:
1224 case DATA_TYPE_HRTIME:
1225 #if !defined(_KERNEL)
1226 case DATA_TYPE_DOUBLE:
1227 #endif
1228 if (data == NULL)
1229 return (EINVAL);
1230 bcopy(NVP_VALUE(nvp), data,
1231 (size_t)i_get_value_size(type, NULL, 1));
1232 if (nelem != NULL)
1233 *nelem = 1;
1234 break;
1236 case DATA_TYPE_NVLIST:
1237 case DATA_TYPE_STRING:
1238 if (data == NULL)
1239 return (EINVAL);
1240 *(void **)data = (void *)NVP_VALUE(nvp);
1241 if (nelem != NULL)
1242 *nelem = 1;
1243 break;
1245 case DATA_TYPE_BOOLEAN_ARRAY:
1246 case DATA_TYPE_BYTE_ARRAY:
1247 case DATA_TYPE_INT8_ARRAY:
1248 case DATA_TYPE_UINT8_ARRAY:
1249 case DATA_TYPE_INT16_ARRAY:
1250 case DATA_TYPE_UINT16_ARRAY:
1251 case DATA_TYPE_INT32_ARRAY:
1252 case DATA_TYPE_UINT32_ARRAY:
1253 case DATA_TYPE_INT64_ARRAY:
1254 case DATA_TYPE_UINT64_ARRAY:
1255 case DATA_TYPE_STRING_ARRAY:
1256 case DATA_TYPE_NVLIST_ARRAY:
1257 if (nelem == NULL || data == NULL)
1258 return (EINVAL);
1259 if ((*nelem = NVP_NELEM(nvp)) != 0)
1260 *(void **)data = (void *)NVP_VALUE(nvp);
1261 else
1262 *(void **)data = NULL;
1263 break;
1265 default:
1266 return (ENOTSUP);
1269 return (0);
1272 static int
1273 nvlist_lookup_common(nvlist_t *nvl, const char *name, data_type_t type,
1274 uint_t *nelem, void *data)
1276 nvpriv_t *priv;
1277 nvpair_t *nvp;
1278 i_nvp_t *curr;
1280 if (name == NULL || nvl == NULL ||
1281 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1282 return (EINVAL);
1284 if (!(nvl->nvl_nvflag & (NV_UNIQUE_NAME | NV_UNIQUE_NAME_TYPE)))
1285 return (ENOTSUP);
1287 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1288 nvp = &curr->nvi_nvp;
1290 if (strcmp(name, NVP_NAME(nvp)) == 0 && NVP_TYPE(nvp) == type)
1291 return (nvpair_value_common(nvp, type, nelem, data));
1294 return (ENOENT);
1298 nvlist_lookup_boolean(nvlist_t *nvl, const char *name)
1300 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BOOLEAN, NULL, NULL));
1304 nvlist_lookup_boolean_value(nvlist_t *nvl, const char *name, boolean_t *val)
1306 return (nvlist_lookup_common(nvl, name,
1307 DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1311 nvlist_lookup_byte(nvlist_t *nvl, const char *name, uchar_t *val)
1313 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE, NULL, val));
1317 nvlist_lookup_int8(nvlist_t *nvl, const char *name, int8_t *val)
1319 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8, NULL, val));
1323 nvlist_lookup_uint8(nvlist_t *nvl, const char *name, uint8_t *val)
1325 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8, NULL, val));
1329 nvlist_lookup_int16(nvlist_t *nvl, const char *name, int16_t *val)
1331 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16, NULL, val));
1335 nvlist_lookup_uint16(nvlist_t *nvl, const char *name, uint16_t *val)
1337 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16, NULL, val));
1341 nvlist_lookup_int32(nvlist_t *nvl, const char *name, int32_t *val)
1343 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32, NULL, val));
1347 nvlist_lookup_uint32(nvlist_t *nvl, const char *name, uint32_t *val)
1349 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32, NULL, val));
1353 nvlist_lookup_int64(nvlist_t *nvl, const char *name, int64_t *val)
1355 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64, NULL, val));
1359 nvlist_lookup_uint64(nvlist_t *nvl, const char *name, uint64_t *val)
1361 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64, NULL, val));
1364 #if !defined(_KERNEL)
1366 nvlist_lookup_double(nvlist_t *nvl, const char *name, double *val)
1368 return (nvlist_lookup_common(nvl, name, DATA_TYPE_DOUBLE, NULL, val));
1370 #endif
1373 nvlist_lookup_string(nvlist_t *nvl, const char *name, char **val)
1375 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING, NULL, val));
1379 nvlist_lookup_nvlist(nvlist_t *nvl, const char *name, nvlist_t **val)
1381 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST, NULL, val));
1385 nvlist_lookup_boolean_array(nvlist_t *nvl, const char *name,
1386 boolean_t **a, uint_t *n)
1388 return (nvlist_lookup_common(nvl, name,
1389 DATA_TYPE_BOOLEAN_ARRAY, n, a));
1393 nvlist_lookup_byte_array(nvlist_t *nvl, const char *name,
1394 uchar_t **a, uint_t *n)
1396 return (nvlist_lookup_common(nvl, name, DATA_TYPE_BYTE_ARRAY, n, a));
1400 nvlist_lookup_int8_array(nvlist_t *nvl, const char *name, int8_t **a, uint_t *n)
1402 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT8_ARRAY, n, a));
1406 nvlist_lookup_uint8_array(nvlist_t *nvl, const char *name,
1407 uint8_t **a, uint_t *n)
1409 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT8_ARRAY, n, a));
1413 nvlist_lookup_int16_array(nvlist_t *nvl, const char *name,
1414 int16_t **a, uint_t *n)
1416 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT16_ARRAY, n, a));
1420 nvlist_lookup_uint16_array(nvlist_t *nvl, const char *name,
1421 uint16_t **a, uint_t *n)
1423 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT16_ARRAY, n, a));
1427 nvlist_lookup_int32_array(nvlist_t *nvl, const char *name,
1428 int32_t **a, uint_t *n)
1430 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT32_ARRAY, n, a));
1434 nvlist_lookup_uint32_array(nvlist_t *nvl, const char *name,
1435 uint32_t **a, uint_t *n)
1437 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT32_ARRAY, n, a));
1441 nvlist_lookup_int64_array(nvlist_t *nvl, const char *name,
1442 int64_t **a, uint_t *n)
1444 return (nvlist_lookup_common(nvl, name, DATA_TYPE_INT64_ARRAY, n, a));
1448 nvlist_lookup_uint64_array(nvlist_t *nvl, const char *name,
1449 uint64_t **a, uint_t *n)
1451 return (nvlist_lookup_common(nvl, name, DATA_TYPE_UINT64_ARRAY, n, a));
1455 nvlist_lookup_string_array(nvlist_t *nvl, const char *name,
1456 char ***a, uint_t *n)
1458 return (nvlist_lookup_common(nvl, name, DATA_TYPE_STRING_ARRAY, n, a));
1462 nvlist_lookup_nvlist_array(nvlist_t *nvl, const char *name,
1463 nvlist_t ***a, uint_t *n)
1465 return (nvlist_lookup_common(nvl, name, DATA_TYPE_NVLIST_ARRAY, n, a));
1469 nvlist_lookup_hrtime(nvlist_t *nvl, const char *name, hrtime_t *val)
1471 return (nvlist_lookup_common(nvl, name, DATA_TYPE_HRTIME, NULL, val));
1475 nvlist_lookup_pairs(nvlist_t *nvl, int flag, ...)
1477 va_list ap;
1478 char *name;
1479 int noentok = (flag & NV_FLAG_NOENTOK ? 1 : 0);
1480 int ret = 0;
1482 va_start(ap, flag);
1483 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
1484 data_type_t type;
1485 void *val;
1486 uint_t *nelem;
1488 switch (type = va_arg(ap, data_type_t)) {
1489 case DATA_TYPE_BOOLEAN:
1490 ret = nvlist_lookup_common(nvl, name, type, NULL, NULL);
1491 break;
1493 case DATA_TYPE_BOOLEAN_VALUE:
1494 case DATA_TYPE_BYTE:
1495 case DATA_TYPE_INT8:
1496 case DATA_TYPE_UINT8:
1497 case DATA_TYPE_INT16:
1498 case DATA_TYPE_UINT16:
1499 case DATA_TYPE_INT32:
1500 case DATA_TYPE_UINT32:
1501 case DATA_TYPE_INT64:
1502 case DATA_TYPE_UINT64:
1503 case DATA_TYPE_HRTIME:
1504 case DATA_TYPE_STRING:
1505 case DATA_TYPE_NVLIST:
1506 #if !defined(_KERNEL)
1507 case DATA_TYPE_DOUBLE:
1508 #endif
1509 val = va_arg(ap, void *);
1510 ret = nvlist_lookup_common(nvl, name, type, NULL, val);
1511 break;
1513 case DATA_TYPE_BYTE_ARRAY:
1514 case DATA_TYPE_BOOLEAN_ARRAY:
1515 case DATA_TYPE_INT8_ARRAY:
1516 case DATA_TYPE_UINT8_ARRAY:
1517 case DATA_TYPE_INT16_ARRAY:
1518 case DATA_TYPE_UINT16_ARRAY:
1519 case DATA_TYPE_INT32_ARRAY:
1520 case DATA_TYPE_UINT32_ARRAY:
1521 case DATA_TYPE_INT64_ARRAY:
1522 case DATA_TYPE_UINT64_ARRAY:
1523 case DATA_TYPE_STRING_ARRAY:
1524 case DATA_TYPE_NVLIST_ARRAY:
1525 val = va_arg(ap, void *);
1526 nelem = va_arg(ap, uint_t *);
1527 ret = nvlist_lookup_common(nvl, name, type, nelem, val);
1528 break;
1530 default:
1531 ret = EINVAL;
1534 if (ret == ENOENT && noentok)
1535 ret = 0;
1537 va_end(ap);
1539 return (ret);
1543 * Find the 'name'ed nvpair in the nvlist 'nvl'. If 'name' found, the function
1544 * returns zero and a pointer to the matching nvpair is returned in '*ret'
1545 * (given 'ret' is non-NULL). If 'sep' is specified then 'name' will penitrate
1546 * multiple levels of embedded nvlists, with 'sep' as the separator. As an
1547 * example, if sep is '.', name might look like: "a" or "a.b" or "a.c[3]" or
1548 * "a.d[3].e[1]". This matches the C syntax for array embed (for convience,
1549 * code also supports "a.d[3]e[1]" syntax).
1551 * If 'ip' is non-NULL and the last name component is an array, return the
1552 * value of the "...[index]" array index in *ip. For an array reference that
1553 * is not indexed, *ip will be returned as -1. If there is a syntax error in
1554 * 'name', and 'ep' is non-NULL then *ep will be set to point to the location
1555 * inside the 'name' string where the syntax error was detected.
1557 static int
1558 nvlist_lookup_nvpair_ei_sep(nvlist_t *nvl, const char *name, const char sep,
1559 nvpair_t **ret, int *ip, char **ep)
1561 nvpair_t *nvp;
1562 const char *np;
1563 char *sepp;
1564 char *idxp, *idxep;
1565 nvlist_t **nva;
1566 long idx;
1567 int n;
1569 if (ip)
1570 *ip = -1; /* not indexed */
1571 if (ep)
1572 *ep = NULL;
1574 if ((nvl == NULL) || (name == NULL))
1575 return (EINVAL);
1577 /* step through components of name */
1578 for (np = name; np && *np; np = sepp) {
1579 /* ensure unique names */
1580 if (!(nvl->nvl_nvflag & NV_UNIQUE_NAME))
1581 return (ENOTSUP);
1583 /* skip white space */
1584 skip_whitespace(np);
1585 if (*np == 0)
1586 break;
1588 /* set 'sepp' to end of current component 'np' */
1589 if (sep)
1590 sepp = strchr(np, sep);
1591 else
1592 sepp = NULL;
1594 /* find start of next "[ index ]..." */
1595 idxp = strchr(np, '[');
1597 /* if sepp comes first, set idxp to NULL */
1598 if (sepp && idxp && (sepp < idxp))
1599 idxp = NULL;
1602 * At this point 'idxp' is set if there is an index
1603 * expected for the current component.
1605 if (idxp) {
1606 /* set 'n' to length of current 'np' name component */
1607 n = idxp++ - np;
1609 /* keep sepp up to date for *ep use as we advance */
1610 skip_whitespace(idxp);
1611 sepp = idxp;
1613 /* determine the index value */
1614 #if defined(_KERNEL) && !defined(_BOOT)
1615 if (ddi_strtol(idxp, &idxep, 0, &idx))
1616 goto fail;
1617 #else
1618 idx = strtol(idxp, &idxep, 0);
1619 #endif
1620 if (idxep == idxp)
1621 goto fail;
1623 /* keep sepp up to date for *ep use as we advance */
1624 sepp = idxep;
1626 /* skip white space index value and check for ']' */
1627 skip_whitespace(sepp);
1628 if (*sepp++ != ']')
1629 goto fail;
1631 /* for embedded arrays, support C syntax: "a[1].b" */
1632 skip_whitespace(sepp);
1633 if (sep && (*sepp == sep))
1634 sepp++;
1635 } else if (sepp) {
1636 n = sepp++ - np;
1637 } else {
1638 n = strlen(np);
1641 /* trim trailing whitespace by reducing length of 'np' */
1642 if (n == 0)
1643 goto fail;
1644 for (n--; (np[n] == ' ') || (np[n] == '\t'); n--)
1646 n++;
1648 /* skip whitespace, and set sepp to NULL if complete */
1649 if (sepp) {
1650 skip_whitespace(sepp);
1651 if (*sepp == 0)
1652 sepp = NULL;
1656 * At this point:
1657 * o 'n' is the length of current 'np' component.
1658 * o 'idxp' is set if there was an index, and value 'idx'.
1659 * o 'sepp' is set to the beginning of the next component,
1660 * and set to NULL if we have no more components.
1662 * Search for nvpair with matching component name.
1664 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
1665 nvp = nvlist_next_nvpair(nvl, nvp)) {
1667 /* continue if no match on name */
1668 if (strncmp(np, nvpair_name(nvp), n) ||
1669 (strlen(nvpair_name(nvp)) != n))
1670 continue;
1672 /* if indexed, verify type is array oriented */
1673 if (idxp && !nvpair_type_is_array(nvp))
1674 goto fail;
1677 * Full match found, return nvp and idx if this
1678 * was the last component.
1680 if (sepp == NULL) {
1681 if (ret)
1682 *ret = nvp;
1683 if (ip && idxp)
1684 *ip = (int)idx; /* return index */
1685 return (0); /* found */
1689 * More components: current match must be
1690 * of DATA_TYPE_NVLIST or DATA_TYPE_NVLIST_ARRAY
1691 * to support going deeper.
1693 if (nvpair_type(nvp) == DATA_TYPE_NVLIST) {
1694 nvl = EMBEDDED_NVL(nvp);
1695 break;
1696 } else if (nvpair_type(nvp) == DATA_TYPE_NVLIST_ARRAY) {
1697 (void) nvpair_value_nvlist_array(nvp,
1698 &nva, (uint_t *)&n);
1699 if ((n < 0) || (idx >= n))
1700 goto fail;
1701 nvl = nva[idx];
1702 break;
1705 /* type does not support more levels */
1706 goto fail;
1708 if (nvp == NULL)
1709 goto fail; /* 'name' not found */
1711 /* search for match of next component in embedded 'nvl' list */
1714 fail: if (ep && sepp)
1715 *ep = sepp;
1716 return (EINVAL);
1720 * Return pointer to nvpair with specified 'name'.
1723 nvlist_lookup_nvpair(nvlist_t *nvl, const char *name, nvpair_t **ret)
1725 return (nvlist_lookup_nvpair_ei_sep(nvl, name, 0, ret, NULL, NULL));
1729 * Determine if named nvpair exists in nvlist (use embedded separator of '.'
1730 * and return array index). See nvlist_lookup_nvpair_ei_sep for more detailed
1731 * description.
1733 int nvlist_lookup_nvpair_embedded_index(nvlist_t *nvl,
1734 const char *name, nvpair_t **ret, int *ip, char **ep)
1736 return (nvlist_lookup_nvpair_ei_sep(nvl, name, '.', ret, ip, ep));
1739 boolean_t
1740 nvlist_exists(nvlist_t *nvl, const char *name)
1742 nvpriv_t *priv;
1743 nvpair_t *nvp;
1744 i_nvp_t *curr;
1746 if (name == NULL || nvl == NULL ||
1747 (priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
1748 return (B_FALSE);
1750 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
1751 nvp = &curr->nvi_nvp;
1753 if (strcmp(name, NVP_NAME(nvp)) == 0)
1754 return (B_TRUE);
1757 return (B_FALSE);
1761 nvpair_value_boolean_value(nvpair_t *nvp, boolean_t *val)
1763 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_VALUE, NULL, val));
1767 nvpair_value_byte(nvpair_t *nvp, uchar_t *val)
1769 return (nvpair_value_common(nvp, DATA_TYPE_BYTE, NULL, val));
1773 nvpair_value_int8(nvpair_t *nvp, int8_t *val)
1775 return (nvpair_value_common(nvp, DATA_TYPE_INT8, NULL, val));
1779 nvpair_value_uint8(nvpair_t *nvp, uint8_t *val)
1781 return (nvpair_value_common(nvp, DATA_TYPE_UINT8, NULL, val));
1785 nvpair_value_int16(nvpair_t *nvp, int16_t *val)
1787 return (nvpair_value_common(nvp, DATA_TYPE_INT16, NULL, val));
1791 nvpair_value_uint16(nvpair_t *nvp, uint16_t *val)
1793 return (nvpair_value_common(nvp, DATA_TYPE_UINT16, NULL, val));
1797 nvpair_value_int32(nvpair_t *nvp, int32_t *val)
1799 return (nvpair_value_common(nvp, DATA_TYPE_INT32, NULL, val));
1803 nvpair_value_uint32(nvpair_t *nvp, uint32_t *val)
1805 return (nvpair_value_common(nvp, DATA_TYPE_UINT32, NULL, val));
1809 nvpair_value_int64(nvpair_t *nvp, int64_t *val)
1811 return (nvpair_value_common(nvp, DATA_TYPE_INT64, NULL, val));
1815 nvpair_value_uint64(nvpair_t *nvp, uint64_t *val)
1817 return (nvpair_value_common(nvp, DATA_TYPE_UINT64, NULL, val));
1820 #if !defined(_KERNEL)
1822 nvpair_value_double(nvpair_t *nvp, double *val)
1824 return (nvpair_value_common(nvp, DATA_TYPE_DOUBLE, NULL, val));
1826 #endif
1829 nvpair_value_string(nvpair_t *nvp, char **val)
1831 return (nvpair_value_common(nvp, DATA_TYPE_STRING, NULL, val));
1835 nvpair_value_nvlist(nvpair_t *nvp, nvlist_t **val)
1837 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST, NULL, val));
1841 nvpair_value_boolean_array(nvpair_t *nvp, boolean_t **val, uint_t *nelem)
1843 return (nvpair_value_common(nvp, DATA_TYPE_BOOLEAN_ARRAY, nelem, val));
1847 nvpair_value_byte_array(nvpair_t *nvp, uchar_t **val, uint_t *nelem)
1849 return (nvpair_value_common(nvp, DATA_TYPE_BYTE_ARRAY, nelem, val));
1853 nvpair_value_int8_array(nvpair_t *nvp, int8_t **val, uint_t *nelem)
1855 return (nvpair_value_common(nvp, DATA_TYPE_INT8_ARRAY, nelem, val));
1859 nvpair_value_uint8_array(nvpair_t *nvp, uint8_t **val, uint_t *nelem)
1861 return (nvpair_value_common(nvp, DATA_TYPE_UINT8_ARRAY, nelem, val));
1865 nvpair_value_int16_array(nvpair_t *nvp, int16_t **val, uint_t *nelem)
1867 return (nvpair_value_common(nvp, DATA_TYPE_INT16_ARRAY, nelem, val));
1871 nvpair_value_uint16_array(nvpair_t *nvp, uint16_t **val, uint_t *nelem)
1873 return (nvpair_value_common(nvp, DATA_TYPE_UINT16_ARRAY, nelem, val));
1877 nvpair_value_int32_array(nvpair_t *nvp, int32_t **val, uint_t *nelem)
1879 return (nvpair_value_common(nvp, DATA_TYPE_INT32_ARRAY, nelem, val));
1883 nvpair_value_uint32_array(nvpair_t *nvp, uint32_t **val, uint_t *nelem)
1885 return (nvpair_value_common(nvp, DATA_TYPE_UINT32_ARRAY, nelem, val));
1889 nvpair_value_int64_array(nvpair_t *nvp, int64_t **val, uint_t *nelem)
1891 return (nvpair_value_common(nvp, DATA_TYPE_INT64_ARRAY, nelem, val));
1895 nvpair_value_uint64_array(nvpair_t *nvp, uint64_t **val, uint_t *nelem)
1897 return (nvpair_value_common(nvp, DATA_TYPE_UINT64_ARRAY, nelem, val));
1901 nvpair_value_string_array(nvpair_t *nvp, char ***val, uint_t *nelem)
1903 return (nvpair_value_common(nvp, DATA_TYPE_STRING_ARRAY, nelem, val));
1907 nvpair_value_nvlist_array(nvpair_t *nvp, nvlist_t ***val, uint_t *nelem)
1909 return (nvpair_value_common(nvp, DATA_TYPE_NVLIST_ARRAY, nelem, val));
1913 nvpair_value_hrtime(nvpair_t *nvp, hrtime_t *val)
1915 return (nvpair_value_common(nvp, DATA_TYPE_HRTIME, NULL, val));
1919 * Add specified pair to the list.
1922 nvlist_add_nvpair(nvlist_t *nvl, nvpair_t *nvp)
1924 if (nvl == NULL || nvp == NULL)
1925 return (EINVAL);
1927 return (nvlist_add_common(nvl, NVP_NAME(nvp), NVP_TYPE(nvp),
1928 NVP_NELEM(nvp), NVP_VALUE(nvp)));
1932 * Merge the supplied nvlists and put the result in dst.
1933 * The merged list will contain all names specified in both lists,
1934 * the values are taken from nvl in the case of duplicates.
1935 * Return 0 on success.
1937 /*ARGSUSED*/
1939 nvlist_merge(nvlist_t *dst, nvlist_t *nvl, int flag)
1941 if (nvl == NULL || dst == NULL)
1942 return (EINVAL);
1944 if (dst != nvl)
1945 return (nvlist_copy_pairs(nvl, dst));
1947 return (0);
1951 * Encoding related routines
1953 #define NVS_OP_ENCODE 0
1954 #define NVS_OP_DECODE 1
1955 #define NVS_OP_GETSIZE 2
1957 typedef struct nvs_ops nvs_ops_t;
1959 typedef struct {
1960 int nvs_op;
1961 const nvs_ops_t *nvs_ops;
1962 void *nvs_private;
1963 nvpriv_t *nvs_priv;
1964 } nvstream_t;
1967 * nvs operations are:
1968 * - nvs_nvlist
1969 * encoding / decoding of a nvlist header (nvlist_t)
1970 * calculates the size used for header and end detection
1972 * - nvs_nvpair
1973 * responsible for the first part of encoding / decoding of an nvpair
1974 * calculates the decoded size of an nvpair
1976 * - nvs_nvp_op
1977 * second part of encoding / decoding of an nvpair
1979 * - nvs_nvp_size
1980 * calculates the encoding size of an nvpair
1982 * - nvs_nvl_fini
1983 * encodes the end detection mark (zeros).
1985 struct nvs_ops {
1986 int (*nvs_nvlist)(nvstream_t *, nvlist_t *, size_t *);
1987 int (*nvs_nvpair)(nvstream_t *, nvpair_t *, size_t *);
1988 int (*nvs_nvp_op)(nvstream_t *, nvpair_t *);
1989 int (*nvs_nvp_size)(nvstream_t *, nvpair_t *, size_t *);
1990 int (*nvs_nvl_fini)(nvstream_t *);
1993 typedef struct {
1994 char nvh_encoding; /* nvs encoding method */
1995 char nvh_endian; /* nvs endian */
1996 char nvh_reserved1; /* reserved for future use */
1997 char nvh_reserved2; /* reserved for future use */
1998 } nvs_header_t;
2000 static int
2001 nvs_encode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2003 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2004 i_nvp_t *curr;
2007 * Walk nvpair in list and encode each nvpair
2009 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next)
2010 if (nvs->nvs_ops->nvs_nvpair(nvs, &curr->nvi_nvp, NULL) != 0)
2011 return (EFAULT);
2013 return (nvs->nvs_ops->nvs_nvl_fini(nvs));
2016 static int
2017 nvs_decode_pairs(nvstream_t *nvs, nvlist_t *nvl)
2019 nvpair_t *nvp;
2020 size_t nvsize;
2021 int err;
2024 * Get decoded size of next pair in stream, alloc
2025 * memory for nvpair_t, then decode the nvpair
2027 while ((err = nvs->nvs_ops->nvs_nvpair(nvs, NULL, &nvsize)) == 0) {
2028 if (nvsize == 0) /* end of list */
2029 break;
2031 /* make sure len makes sense */
2032 if (nvsize < NVP_SIZE_CALC(1, 0))
2033 return (EFAULT);
2035 if ((nvp = nvp_buf_alloc(nvl, nvsize)) == NULL)
2036 return (ENOMEM);
2038 if ((err = nvs->nvs_ops->nvs_nvp_op(nvs, nvp)) != 0) {
2039 nvp_buf_free(nvl, nvp);
2040 return (err);
2043 if (i_validate_nvpair(nvp) != 0) {
2044 nvpair_free(nvp);
2045 nvp_buf_free(nvl, nvp);
2046 return (EFAULT);
2049 nvp_buf_link(nvl, nvp);
2051 return (err);
2054 static int
2055 nvs_getsize_pairs(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2057 nvpriv_t *priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv;
2058 i_nvp_t *curr;
2059 uint64_t nvsize = *buflen;
2060 size_t size;
2063 * Get encoded size of nvpairs in nvlist
2065 for (curr = priv->nvp_list; curr != NULL; curr = curr->nvi_next) {
2066 if (nvs->nvs_ops->nvs_nvp_size(nvs, &curr->nvi_nvp, &size) != 0)
2067 return (EINVAL);
2069 if ((nvsize += size) > INT32_MAX)
2070 return (EINVAL);
2073 *buflen = nvsize;
2074 return (0);
2077 static int
2078 nvs_operation(nvstream_t *nvs, nvlist_t *nvl, size_t *buflen)
2080 int err;
2082 if (nvl->nvl_priv == 0)
2083 return (EFAULT);
2086 * Perform the operation, starting with header, then each nvpair
2088 if ((err = nvs->nvs_ops->nvs_nvlist(nvs, nvl, buflen)) != 0)
2089 return (err);
2091 switch (nvs->nvs_op) {
2092 case NVS_OP_ENCODE:
2093 err = nvs_encode_pairs(nvs, nvl);
2094 break;
2096 case NVS_OP_DECODE:
2097 err = nvs_decode_pairs(nvs, nvl);
2098 break;
2100 case NVS_OP_GETSIZE:
2101 err = nvs_getsize_pairs(nvs, nvl, buflen);
2102 break;
2104 default:
2105 err = EINVAL;
2108 return (err);
2111 static int
2112 nvs_embedded(nvstream_t *nvs, nvlist_t *embedded)
2114 switch (nvs->nvs_op) {
2115 case NVS_OP_ENCODE:
2116 return (nvs_operation(nvs, embedded, NULL));
2118 case NVS_OP_DECODE: {
2119 nvpriv_t *priv;
2120 int err;
2122 if (embedded->nvl_version != NV_VERSION)
2123 return (ENOTSUP);
2125 if ((priv = nv_priv_alloc_embedded(nvs->nvs_priv)) == NULL)
2126 return (ENOMEM);
2128 nvlist_init(embedded, embedded->nvl_nvflag, priv);
2130 if ((err = nvs_operation(nvs, embedded, NULL)) != 0)
2131 nvlist_free(embedded);
2132 return (err);
2134 default:
2135 break;
2138 return (EINVAL);
2141 static int
2142 nvs_embedded_nvl_array(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2144 size_t nelem = NVP_NELEM(nvp);
2145 nvlist_t **nvlp = EMBEDDED_NVL_ARRAY(nvp);
2146 int i;
2148 switch (nvs->nvs_op) {
2149 case NVS_OP_ENCODE:
2150 for (i = 0; i < nelem; i++)
2151 if (nvs_embedded(nvs, nvlp[i]) != 0)
2152 return (EFAULT);
2153 break;
2155 case NVS_OP_DECODE: {
2156 size_t len = nelem * sizeof (uint64_t);
2157 nvlist_t *embedded = (nvlist_t *)((uintptr_t)nvlp + len);
2159 bzero(nvlp, len); /* don't trust packed data */
2160 for (i = 0; i < nelem; i++) {
2161 if (nvs_embedded(nvs, embedded) != 0) {
2162 nvpair_free(nvp);
2163 return (EFAULT);
2166 nvlp[i] = embedded++;
2168 break;
2170 case NVS_OP_GETSIZE: {
2171 uint64_t nvsize = 0;
2173 for (i = 0; i < nelem; i++) {
2174 size_t nvp_sz = 0;
2176 if (nvs_operation(nvs, nvlp[i], &nvp_sz) != 0)
2177 return (EINVAL);
2179 if ((nvsize += nvp_sz) > INT32_MAX)
2180 return (EINVAL);
2183 *size = nvsize;
2184 break;
2186 default:
2187 return (EINVAL);
2190 return (0);
2193 static int nvs_native(nvstream_t *, nvlist_t *, char *, size_t *);
2194 static int nvs_xdr(nvstream_t *, nvlist_t *, char *, size_t *);
2197 * Common routine for nvlist operations:
2198 * encode, decode, getsize (encoded size).
2200 static int
2201 nvlist_common(nvlist_t *nvl, char *buf, size_t *buflen, int encoding,
2202 int nvs_op)
2204 int err = 0;
2205 nvstream_t nvs;
2206 int nvl_endian;
2207 #ifdef _LITTLE_ENDIAN
2208 int host_endian = 1;
2209 #else
2210 int host_endian = 0;
2211 #endif /* _LITTLE_ENDIAN */
2212 nvs_header_t *nvh = (void *)buf;
2214 if (buflen == NULL || nvl == NULL ||
2215 (nvs.nvs_priv = (nvpriv_t *)(uintptr_t)nvl->nvl_priv) == NULL)
2216 return (EINVAL);
2218 nvs.nvs_op = nvs_op;
2221 * For NVS_OP_ENCODE and NVS_OP_DECODE make sure an nvlist and
2222 * a buffer is allocated. The first 4 bytes in the buffer are
2223 * used for encoding method and host endian.
2225 switch (nvs_op) {
2226 case NVS_OP_ENCODE:
2227 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2228 return (EINVAL);
2230 nvh->nvh_encoding = encoding;
2231 nvh->nvh_endian = nvl_endian = host_endian;
2232 nvh->nvh_reserved1 = 0;
2233 nvh->nvh_reserved2 = 0;
2234 break;
2236 case NVS_OP_DECODE:
2237 if (buf == NULL || *buflen < sizeof (nvs_header_t))
2238 return (EINVAL);
2240 /* get method of encoding from first byte */
2241 encoding = nvh->nvh_encoding;
2242 nvl_endian = nvh->nvh_endian;
2243 break;
2245 case NVS_OP_GETSIZE:
2246 nvl_endian = host_endian;
2249 * add the size for encoding
2251 *buflen = sizeof (nvs_header_t);
2252 break;
2254 default:
2255 return (ENOTSUP);
2259 * Create an nvstream with proper encoding method
2261 switch (encoding) {
2262 case NV_ENCODE_NATIVE:
2264 * check endianness, in case we are unpacking
2265 * from a file
2267 if (nvl_endian != host_endian)
2268 return (ENOTSUP);
2269 err = nvs_native(&nvs, nvl, buf, buflen);
2270 break;
2271 case NV_ENCODE_XDR:
2272 err = nvs_xdr(&nvs, nvl, buf, buflen);
2273 break;
2274 default:
2275 err = ENOTSUP;
2276 break;
2279 return (err);
2283 nvlist_size(nvlist_t *nvl, size_t *size, int encoding)
2285 return (nvlist_common(nvl, NULL, size, encoding, NVS_OP_GETSIZE));
2289 * Pack nvlist into contiguous memory
2291 /*ARGSUSED1*/
2293 nvlist_pack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2294 int kmflag)
2296 #if defined(_KERNEL) && !defined(_BOOT)
2297 return (nvlist_xpack(nvl, bufp, buflen, encoding,
2298 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2299 #else
2300 return (nvlist_xpack(nvl, bufp, buflen, encoding, nv_alloc_nosleep));
2301 #endif
2305 nvlist_xpack(nvlist_t *nvl, char **bufp, size_t *buflen, int encoding,
2306 nv_alloc_t *nva)
2308 nvpriv_t nvpriv;
2309 size_t alloc_size;
2310 char *buf;
2311 int err;
2313 if (nva == NULL || nvl == NULL || bufp == NULL || buflen == NULL)
2314 return (EINVAL);
2316 if (*bufp != NULL)
2317 return (nvlist_common(nvl, *bufp, buflen, encoding,
2318 NVS_OP_ENCODE));
2321 * Here is a difficult situation:
2322 * 1. The nvlist has fixed allocator properties.
2323 * All other nvlist routines (like nvlist_add_*, ...) use
2324 * these properties.
2325 * 2. When using nvlist_pack() the user can specify his own
2326 * allocator properties (e.g. by using KM_NOSLEEP).
2328 * We use the user specified properties (2). A clearer solution
2329 * will be to remove the kmflag from nvlist_pack(), but we will
2330 * not change the interface.
2332 nv_priv_init(&nvpriv, nva, 0);
2334 if (err = nvlist_size(nvl, &alloc_size, encoding))
2335 return (err);
2337 if ((buf = nv_mem_zalloc(&nvpriv, alloc_size)) == NULL)
2338 return (ENOMEM);
2340 if ((err = nvlist_common(nvl, buf, &alloc_size, encoding,
2341 NVS_OP_ENCODE)) != 0) {
2342 nv_mem_free(&nvpriv, buf, alloc_size);
2343 } else {
2344 *buflen = alloc_size;
2345 *bufp = buf;
2348 return (err);
2352 * Unpack buf into an nvlist_t
2354 /*ARGSUSED1*/
2356 nvlist_unpack(char *buf, size_t buflen, nvlist_t **nvlp, int kmflag)
2358 #if defined(_KERNEL) && !defined(_BOOT)
2359 return (nvlist_xunpack(buf, buflen, nvlp,
2360 (kmflag == KM_SLEEP ? nv_alloc_sleep : nv_alloc_nosleep)));
2361 #else
2362 return (nvlist_xunpack(buf, buflen, nvlp, nv_alloc_nosleep));
2363 #endif
2367 nvlist_xunpack(char *buf, size_t buflen, nvlist_t **nvlp, nv_alloc_t *nva)
2369 nvlist_t *nvl;
2370 int err;
2372 if (nvlp == NULL)
2373 return (EINVAL);
2375 if ((err = nvlist_xalloc(&nvl, 0, nva)) != 0)
2376 return (err);
2378 if ((err = nvlist_common(nvl, buf, &buflen, 0, NVS_OP_DECODE)) != 0)
2379 nvlist_free(nvl);
2380 else
2381 *nvlp = nvl;
2383 return (err);
2387 * Native encoding functions
2389 typedef struct {
2391 * This structure is used when decoding a packed nvpair in
2392 * the native format. n_base points to a buffer containing the
2393 * packed nvpair. n_end is a pointer to the end of the buffer.
2394 * (n_end actually points to the first byte past the end of the
2395 * buffer.) n_curr is a pointer that lies between n_base and n_end.
2396 * It points to the current data that we are decoding.
2397 * The amount of data left in the buffer is equal to n_end - n_curr.
2398 * n_flag is used to recognize a packed embedded list.
2400 caddr_t n_base;
2401 caddr_t n_end;
2402 caddr_t n_curr;
2403 uint_t n_flag;
2404 } nvs_native_t;
2406 static int
2407 nvs_native_create(nvstream_t *nvs, nvs_native_t *native, char *buf,
2408 size_t buflen)
2410 switch (nvs->nvs_op) {
2411 case NVS_OP_ENCODE:
2412 case NVS_OP_DECODE:
2413 nvs->nvs_private = native;
2414 native->n_curr = native->n_base = buf;
2415 native->n_end = buf + buflen;
2416 native->n_flag = 0;
2417 return (0);
2419 case NVS_OP_GETSIZE:
2420 nvs->nvs_private = native;
2421 native->n_curr = native->n_base = native->n_end = NULL;
2422 native->n_flag = 0;
2423 return (0);
2424 default:
2425 return (EINVAL);
2429 /*ARGSUSED*/
2430 static void
2431 nvs_native_destroy(nvstream_t *nvs)
2435 static int
2436 native_cp(nvstream_t *nvs, void *buf, size_t size)
2438 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2440 if (native->n_curr + size > native->n_end)
2441 return (EFAULT);
2444 * The bcopy() below eliminates alignment requirement
2445 * on the buffer (stream) and is preferred over direct access.
2447 switch (nvs->nvs_op) {
2448 case NVS_OP_ENCODE:
2449 bcopy(buf, native->n_curr, size);
2450 break;
2451 case NVS_OP_DECODE:
2452 bcopy(native->n_curr, buf, size);
2453 break;
2454 default:
2455 return (EINVAL);
2458 native->n_curr += size;
2459 return (0);
2463 * operate on nvlist_t header
2465 static int
2466 nvs_native_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2468 nvs_native_t *native = nvs->nvs_private;
2470 switch (nvs->nvs_op) {
2471 case NVS_OP_ENCODE:
2472 case NVS_OP_DECODE:
2473 if (native->n_flag)
2474 return (0); /* packed embedded list */
2476 native->n_flag = 1;
2478 /* copy version and nvflag of the nvlist_t */
2479 if (native_cp(nvs, &nvl->nvl_version, sizeof (int32_t)) != 0 ||
2480 native_cp(nvs, &nvl->nvl_nvflag, sizeof (int32_t)) != 0)
2481 return (EFAULT);
2483 return (0);
2485 case NVS_OP_GETSIZE:
2487 * if calculate for packed embedded list
2488 * 4 for end of the embedded list
2489 * else
2490 * 2 * sizeof (int32_t) for nvl_version and nvl_nvflag
2491 * and 4 for end of the entire list
2493 if (native->n_flag) {
2494 *size += 4;
2495 } else {
2496 native->n_flag = 1;
2497 *size += 2 * sizeof (int32_t) + 4;
2500 return (0);
2502 default:
2503 return (EINVAL);
2507 static int
2508 nvs_native_nvl_fini(nvstream_t *nvs)
2510 if (nvs->nvs_op == NVS_OP_ENCODE) {
2511 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2513 * Add 4 zero bytes at end of nvlist. They are used
2514 * for end detection by the decode routine.
2516 if (native->n_curr + sizeof (int) > native->n_end)
2517 return (EFAULT);
2519 bzero(native->n_curr, sizeof (int));
2520 native->n_curr += sizeof (int);
2523 return (0);
2526 static int
2527 nvpair_native_embedded(nvstream_t *nvs, nvpair_t *nvp)
2529 if (nvs->nvs_op == NVS_OP_ENCODE) {
2530 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2531 nvlist_t *packed = (void *)
2532 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2534 * Null out the pointer that is meaningless in the packed
2535 * structure. The address may not be aligned, so we have
2536 * to use bzero.
2538 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2541 return (nvs_embedded(nvs, EMBEDDED_NVL(nvp)));
2544 static int
2545 nvpair_native_embedded_array(nvstream_t *nvs, nvpair_t *nvp)
2547 if (nvs->nvs_op == NVS_OP_ENCODE) {
2548 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2549 char *value = native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp);
2550 size_t len = NVP_NELEM(nvp) * sizeof (uint64_t);
2551 nvlist_t *packed = (nvlist_t *)((uintptr_t)value + len);
2552 int i;
2554 * Null out pointers that are meaningless in the packed
2555 * structure. The addresses may not be aligned, so we have
2556 * to use bzero.
2558 bzero(value, len);
2560 for (i = 0; i < NVP_NELEM(nvp); i++, packed++)
2562 * Null out the pointer that is meaningless in the
2563 * packed structure. The address may not be aligned,
2564 * so we have to use bzero.
2566 bzero(&packed->nvl_priv, sizeof (packed->nvl_priv));
2569 return (nvs_embedded_nvl_array(nvs, nvp, NULL));
2572 static void
2573 nvpair_native_string_array(nvstream_t *nvs, nvpair_t *nvp)
2575 switch (nvs->nvs_op) {
2576 case NVS_OP_ENCODE: {
2577 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2578 uint64_t *strp = (void *)
2579 (native->n_curr - nvp->nvp_size + NVP_VALOFF(nvp));
2581 * Null out pointers that are meaningless in the packed
2582 * structure. The addresses may not be aligned, so we have
2583 * to use bzero.
2585 bzero(strp, NVP_NELEM(nvp) * sizeof (uint64_t));
2586 break;
2588 case NVS_OP_DECODE: {
2589 char **strp = (void *)NVP_VALUE(nvp);
2590 char *buf = ((char *)strp + NVP_NELEM(nvp) * sizeof (uint64_t));
2591 int i;
2593 for (i = 0; i < NVP_NELEM(nvp); i++) {
2594 strp[i] = buf;
2595 buf += strlen(buf) + 1;
2597 break;
2602 static int
2603 nvs_native_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2605 data_type_t type;
2606 int value_sz;
2607 int ret = 0;
2610 * We do the initial bcopy of the data before we look at
2611 * the nvpair type, because when we're decoding, we won't
2612 * have the correct values for the pair until we do the bcopy.
2614 switch (nvs->nvs_op) {
2615 case NVS_OP_ENCODE:
2616 case NVS_OP_DECODE:
2617 if (native_cp(nvs, nvp, nvp->nvp_size) != 0)
2618 return (EFAULT);
2619 break;
2620 default:
2621 return (EINVAL);
2624 /* verify nvp_name_sz, check the name string length */
2625 if (i_validate_nvpair_name(nvp) != 0)
2626 return (EFAULT);
2628 type = NVP_TYPE(nvp);
2631 * Verify type and nelem and get the value size.
2632 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2633 * is the size of the string(s) excluded.
2635 if ((value_sz = i_get_value_size(type, NULL, NVP_NELEM(nvp))) < 0)
2636 return (EFAULT);
2638 if (NVP_SIZE_CALC(nvp->nvp_name_sz, value_sz) > nvp->nvp_size)
2639 return (EFAULT);
2641 switch (type) {
2642 case DATA_TYPE_NVLIST:
2643 ret = nvpair_native_embedded(nvs, nvp);
2644 break;
2645 case DATA_TYPE_NVLIST_ARRAY:
2646 ret = nvpair_native_embedded_array(nvs, nvp);
2647 break;
2648 case DATA_TYPE_STRING_ARRAY:
2649 nvpair_native_string_array(nvs, nvp);
2650 break;
2651 default:
2652 break;
2655 return (ret);
2658 static int
2659 nvs_native_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2661 uint64_t nvp_sz = nvp->nvp_size;
2663 switch (NVP_TYPE(nvp)) {
2664 case DATA_TYPE_NVLIST: {
2665 size_t nvsize = 0;
2667 if (nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize) != 0)
2668 return (EINVAL);
2670 nvp_sz += nvsize;
2671 break;
2673 case DATA_TYPE_NVLIST_ARRAY: {
2674 size_t nvsize;
2676 if (nvs_embedded_nvl_array(nvs, nvp, &nvsize) != 0)
2677 return (EINVAL);
2679 nvp_sz += nvsize;
2680 break;
2682 default:
2683 break;
2686 if (nvp_sz > INT32_MAX)
2687 return (EINVAL);
2689 *size = nvp_sz;
2691 return (0);
2694 static int
2695 nvs_native_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
2697 switch (nvs->nvs_op) {
2698 case NVS_OP_ENCODE:
2699 return (nvs_native_nvp_op(nvs, nvp));
2701 case NVS_OP_DECODE: {
2702 nvs_native_t *native = (nvs_native_t *)nvs->nvs_private;
2703 int32_t decode_len;
2705 /* try to read the size value from the stream */
2706 if (native->n_curr + sizeof (int32_t) > native->n_end)
2707 return (EFAULT);
2708 bcopy(native->n_curr, &decode_len, sizeof (int32_t));
2710 /* sanity check the size value */
2711 if (decode_len < 0 ||
2712 decode_len > native->n_end - native->n_curr)
2713 return (EFAULT);
2715 *size = decode_len;
2718 * If at the end of the stream then move the cursor
2719 * forward, otherwise nvpair_native_op() will read
2720 * the entire nvpair at the same cursor position.
2722 if (*size == 0)
2723 native->n_curr += sizeof (int32_t);
2724 break;
2727 default:
2728 return (EINVAL);
2731 return (0);
2734 static const nvs_ops_t nvs_native_ops = {
2735 nvs_native_nvlist,
2736 nvs_native_nvpair,
2737 nvs_native_nvp_op,
2738 nvs_native_nvp_size,
2739 nvs_native_nvl_fini
2742 static int
2743 nvs_native(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
2745 nvs_native_t native;
2746 int err;
2748 nvs->nvs_ops = &nvs_native_ops;
2750 if ((err = nvs_native_create(nvs, &native, buf + sizeof (nvs_header_t),
2751 *buflen - sizeof (nvs_header_t))) != 0)
2752 return (err);
2754 err = nvs_operation(nvs, nvl, buflen);
2756 nvs_native_destroy(nvs);
2758 return (err);
2762 * XDR encoding functions
2764 * An xdr packed nvlist is encoded as:
2766 * - encoding methode and host endian (4 bytes)
2767 * - nvl_version (4 bytes)
2768 * - nvl_nvflag (4 bytes)
2770 * - encoded nvpairs, the format of one xdr encoded nvpair is:
2771 * - encoded size of the nvpair (4 bytes)
2772 * - decoded size of the nvpair (4 bytes)
2773 * - name string, (4 + sizeof(NV_ALIGN4(string))
2774 * a string is coded as size (4 bytes) and data
2775 * - data type (4 bytes)
2776 * - number of elements in the nvpair (4 bytes)
2777 * - data
2779 * - 2 zero's for end of the entire list (8 bytes)
2781 static int
2782 nvs_xdr_create(nvstream_t *nvs, XDR *xdr, char *buf, size_t buflen)
2784 /* xdr data must be 4 byte aligned */
2785 if ((ulong_t)buf % 4 != 0)
2786 return (EFAULT);
2788 switch (nvs->nvs_op) {
2789 case NVS_OP_ENCODE:
2790 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_ENCODE);
2791 nvs->nvs_private = xdr;
2792 return (0);
2793 case NVS_OP_DECODE:
2794 xdrmem_create(xdr, buf, (uint_t)buflen, XDR_DECODE);
2795 nvs->nvs_private = xdr;
2796 return (0);
2797 case NVS_OP_GETSIZE:
2798 nvs->nvs_private = NULL;
2799 return (0);
2800 default:
2801 return (EINVAL);
2805 static void
2806 nvs_xdr_destroy(nvstream_t *nvs)
2808 switch (nvs->nvs_op) {
2809 case NVS_OP_ENCODE:
2810 case NVS_OP_DECODE:
2811 xdr_destroy((XDR *)nvs->nvs_private);
2812 break;
2813 default:
2814 break;
2818 static int
2819 nvs_xdr_nvlist(nvstream_t *nvs, nvlist_t *nvl, size_t *size)
2821 switch (nvs->nvs_op) {
2822 case NVS_OP_ENCODE:
2823 case NVS_OP_DECODE: {
2824 XDR *xdr = nvs->nvs_private;
2826 if (!xdr_int(xdr, &nvl->nvl_version) ||
2827 !xdr_u_int(xdr, &nvl->nvl_nvflag))
2828 return (EFAULT);
2829 break;
2831 case NVS_OP_GETSIZE: {
2833 * 2 * 4 for nvl_version + nvl_nvflag
2834 * and 8 for end of the entire list
2836 *size += 2 * 4 + 8;
2837 break;
2839 default:
2840 return (EINVAL);
2842 return (0);
2845 static int
2846 nvs_xdr_nvl_fini(nvstream_t *nvs)
2848 if (nvs->nvs_op == NVS_OP_ENCODE) {
2849 XDR *xdr = nvs->nvs_private;
2850 int zero = 0;
2852 if (!xdr_int(xdr, &zero) || !xdr_int(xdr, &zero))
2853 return (EFAULT);
2856 return (0);
2860 * The format of xdr encoded nvpair is:
2861 * encode_size, decode_size, name string, data type, nelem, data
2863 static int
2864 nvs_xdr_nvp_op(nvstream_t *nvs, nvpair_t *nvp)
2866 data_type_t type;
2867 char *buf;
2868 char *buf_end = (char *)nvp + nvp->nvp_size;
2869 int value_sz;
2870 uint_t nelem, buflen;
2871 bool_t ret = FALSE;
2872 XDR *xdr = nvs->nvs_private;
2874 ASSERT(xdr != NULL && nvp != NULL);
2876 /* name string */
2877 if ((buf = NVP_NAME(nvp)) >= buf_end)
2878 return (EFAULT);
2879 buflen = buf_end - buf;
2881 if (!xdr_string(xdr, &buf, buflen - 1))
2882 return (EFAULT);
2883 nvp->nvp_name_sz = strlen(buf) + 1;
2885 /* type and nelem */
2886 if (!xdr_int(xdr, (int *)&nvp->nvp_type) ||
2887 !xdr_int(xdr, &nvp->nvp_value_elem))
2888 return (EFAULT);
2890 type = NVP_TYPE(nvp);
2891 nelem = nvp->nvp_value_elem;
2894 * Verify type and nelem and get the value size.
2895 * In case of data types DATA_TYPE_STRING and DATA_TYPE_STRING_ARRAY
2896 * is the size of the string(s) excluded.
2898 if ((value_sz = i_get_value_size(type, NULL, nelem)) < 0)
2899 return (EFAULT);
2901 /* if there is no data to extract then return */
2902 if (nelem == 0)
2903 return (0);
2905 /* value */
2906 if ((buf = NVP_VALUE(nvp)) >= buf_end)
2907 return (EFAULT);
2908 buflen = buf_end - buf;
2910 if (buflen < value_sz)
2911 return (EFAULT);
2913 switch (type) {
2914 case DATA_TYPE_NVLIST:
2915 if (nvs_embedded(nvs, (void *)buf) == 0)
2916 return (0);
2917 break;
2919 case DATA_TYPE_NVLIST_ARRAY:
2920 if (nvs_embedded_nvl_array(nvs, nvp, NULL) == 0)
2921 return (0);
2922 break;
2924 case DATA_TYPE_BOOLEAN:
2925 ret = TRUE;
2926 break;
2928 case DATA_TYPE_BYTE:
2929 case DATA_TYPE_INT8:
2930 case DATA_TYPE_UINT8:
2931 ret = xdr_char(xdr, buf);
2932 break;
2934 case DATA_TYPE_INT16:
2935 ret = xdr_short(xdr, (void *)buf);
2936 break;
2938 case DATA_TYPE_UINT16:
2939 ret = xdr_u_short(xdr, (void *)buf);
2940 break;
2942 case DATA_TYPE_BOOLEAN_VALUE:
2943 case DATA_TYPE_INT32:
2944 ret = xdr_int(xdr, (void *)buf);
2945 break;
2947 case DATA_TYPE_UINT32:
2948 ret = xdr_u_int(xdr, (void *)buf);
2949 break;
2951 case DATA_TYPE_INT64:
2952 ret = xdr_longlong_t(xdr, (void *)buf);
2953 break;
2955 case DATA_TYPE_UINT64:
2956 ret = xdr_u_longlong_t(xdr, (void *)buf);
2957 break;
2959 case DATA_TYPE_HRTIME:
2961 * NOTE: must expose the definition of hrtime_t here
2963 ret = xdr_longlong_t(xdr, (void *)buf);
2964 break;
2965 #if !defined(_KERNEL)
2966 case DATA_TYPE_DOUBLE:
2967 ret = xdr_double(xdr, (void *)buf);
2968 break;
2969 #endif
2970 case DATA_TYPE_STRING:
2971 ret = xdr_string(xdr, &buf, buflen - 1);
2972 break;
2974 case DATA_TYPE_BYTE_ARRAY:
2975 ret = xdr_opaque(xdr, buf, nelem);
2976 break;
2978 case DATA_TYPE_INT8_ARRAY:
2979 case DATA_TYPE_UINT8_ARRAY:
2980 ret = xdr_array(xdr, &buf, &nelem, buflen, sizeof (int8_t),
2981 (xdrproc_t)xdr_char);
2982 break;
2984 case DATA_TYPE_INT16_ARRAY:
2985 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int16_t),
2986 sizeof (int16_t), (xdrproc_t)xdr_short);
2987 break;
2989 case DATA_TYPE_UINT16_ARRAY:
2990 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint16_t),
2991 sizeof (uint16_t), (xdrproc_t)xdr_u_short);
2992 break;
2994 case DATA_TYPE_BOOLEAN_ARRAY:
2995 case DATA_TYPE_INT32_ARRAY:
2996 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int32_t),
2997 sizeof (int32_t), (xdrproc_t)xdr_int);
2998 break;
3000 case DATA_TYPE_UINT32_ARRAY:
3001 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint32_t),
3002 sizeof (uint32_t), (xdrproc_t)xdr_u_int);
3003 break;
3005 case DATA_TYPE_INT64_ARRAY:
3006 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (int64_t),
3007 sizeof (int64_t), (xdrproc_t)xdr_longlong_t);
3008 break;
3010 case DATA_TYPE_UINT64_ARRAY:
3011 ret = xdr_array(xdr, &buf, &nelem, buflen / sizeof (uint64_t),
3012 sizeof (uint64_t), (xdrproc_t)xdr_u_longlong_t);
3013 break;
3015 case DATA_TYPE_STRING_ARRAY: {
3016 size_t len = nelem * sizeof (uint64_t);
3017 char **strp = (void *)buf;
3018 int i;
3020 if (nvs->nvs_op == NVS_OP_DECODE)
3021 bzero(buf, len); /* don't trust packed data */
3023 for (i = 0; i < nelem; i++) {
3024 if (buflen <= len)
3025 return (EFAULT);
3027 buf += len;
3028 buflen -= len;
3030 if (xdr_string(xdr, &buf, buflen - 1) != TRUE)
3031 return (EFAULT);
3033 if (nvs->nvs_op == NVS_OP_DECODE)
3034 strp[i] = buf;
3035 len = strlen(buf) + 1;
3037 ret = TRUE;
3038 break;
3040 default:
3041 break;
3044 return (ret == TRUE ? 0 : EFAULT);
3047 static int
3048 nvs_xdr_nvp_size(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3050 data_type_t type = NVP_TYPE(nvp);
3052 * encode_size + decode_size + name string size + data type + nelem
3053 * where name string size = 4 + NV_ALIGN4(strlen(NVP_NAME(nvp)))
3055 uint64_t nvp_sz = 4 + 4 + 4 + NV_ALIGN4(strlen(NVP_NAME(nvp))) + 4 + 4;
3057 switch (type) {
3058 case DATA_TYPE_BOOLEAN:
3059 break;
3061 case DATA_TYPE_BOOLEAN_VALUE:
3062 case DATA_TYPE_BYTE:
3063 case DATA_TYPE_INT8:
3064 case DATA_TYPE_UINT8:
3065 case DATA_TYPE_INT16:
3066 case DATA_TYPE_UINT16:
3067 case DATA_TYPE_INT32:
3068 case DATA_TYPE_UINT32:
3069 nvp_sz += 4; /* 4 is the minimum xdr unit */
3070 break;
3072 case DATA_TYPE_INT64:
3073 case DATA_TYPE_UINT64:
3074 case DATA_TYPE_HRTIME:
3075 #if !defined(_KERNEL)
3076 case DATA_TYPE_DOUBLE:
3077 #endif
3078 nvp_sz += 8;
3079 break;
3081 case DATA_TYPE_STRING:
3082 nvp_sz += 4 + NV_ALIGN4(strlen((char *)NVP_VALUE(nvp)));
3083 break;
3085 case DATA_TYPE_BYTE_ARRAY:
3086 nvp_sz += NV_ALIGN4(NVP_NELEM(nvp));
3087 break;
3089 case DATA_TYPE_BOOLEAN_ARRAY:
3090 case DATA_TYPE_INT8_ARRAY:
3091 case DATA_TYPE_UINT8_ARRAY:
3092 case DATA_TYPE_INT16_ARRAY:
3093 case DATA_TYPE_UINT16_ARRAY:
3094 case DATA_TYPE_INT32_ARRAY:
3095 case DATA_TYPE_UINT32_ARRAY:
3096 nvp_sz += 4 + 4 * (uint64_t)NVP_NELEM(nvp);
3097 break;
3099 case DATA_TYPE_INT64_ARRAY:
3100 case DATA_TYPE_UINT64_ARRAY:
3101 nvp_sz += 4 + 8 * (uint64_t)NVP_NELEM(nvp);
3102 break;
3104 case DATA_TYPE_STRING_ARRAY: {
3105 int i;
3106 char **strs = (void *)NVP_VALUE(nvp);
3108 for (i = 0; i < NVP_NELEM(nvp); i++)
3109 nvp_sz += 4 + NV_ALIGN4(strlen(strs[i]));
3111 break;
3114 case DATA_TYPE_NVLIST:
3115 case DATA_TYPE_NVLIST_ARRAY: {
3116 size_t nvsize = 0;
3117 int old_nvs_op = nvs->nvs_op;
3118 int err;
3120 nvs->nvs_op = NVS_OP_GETSIZE;
3121 if (type == DATA_TYPE_NVLIST)
3122 err = nvs_operation(nvs, EMBEDDED_NVL(nvp), &nvsize);
3123 else
3124 err = nvs_embedded_nvl_array(nvs, nvp, &nvsize);
3125 nvs->nvs_op = old_nvs_op;
3127 if (err != 0)
3128 return (EINVAL);
3130 nvp_sz += nvsize;
3131 break;
3134 default:
3135 return (EINVAL);
3138 if (nvp_sz > INT32_MAX)
3139 return (EINVAL);
3141 *size = nvp_sz;
3143 return (0);
3148 * The NVS_XDR_MAX_LEN macro takes a packed xdr buffer of size x and estimates
3149 * the largest nvpair that could be encoded in the buffer.
3151 * See comments above nvpair_xdr_op() for the format of xdr encoding.
3152 * The size of a xdr packed nvpair without any data is 5 words.
3154 * Using the size of the data directly as an estimate would be ok
3155 * in all cases except one. If the data type is of DATA_TYPE_STRING_ARRAY
3156 * then the actual nvpair has space for an array of pointers to index
3157 * the strings. These pointers are not encoded into the packed xdr buffer.
3159 * If the data is of type DATA_TYPE_STRING_ARRAY and all the strings are
3160 * of length 0, then each string is endcoded in xdr format as a single word.
3161 * Therefore when expanded to an nvpair there will be 2.25 word used for
3162 * each string. (a int64_t allocated for pointer usage, and a single char
3163 * for the null termination.)
3165 * This is the calculation performed by the NVS_XDR_MAX_LEN macro.
3167 #define NVS_XDR_HDR_LEN ((size_t)(5 * 4))
3168 #define NVS_XDR_DATA_LEN(y) (((size_t)(y) <= NVS_XDR_HDR_LEN) ? \
3169 0 : ((size_t)(y) - NVS_XDR_HDR_LEN))
3170 #define NVS_XDR_MAX_LEN(x) (NVP_SIZE_CALC(1, 0) + \
3171 (NVS_XDR_DATA_LEN(x) * 2) + \
3172 NV_ALIGN4((NVS_XDR_DATA_LEN(x) / 4)))
3174 static int
3175 nvs_xdr_nvpair(nvstream_t *nvs, nvpair_t *nvp, size_t *size)
3177 XDR *xdr = nvs->nvs_private;
3178 int32_t encode_len, decode_len;
3180 switch (nvs->nvs_op) {
3181 case NVS_OP_ENCODE: {
3182 size_t nvsize;
3184 if (nvs_xdr_nvp_size(nvs, nvp, &nvsize) != 0)
3185 return (EFAULT);
3187 decode_len = nvp->nvp_size;
3188 encode_len = nvsize;
3189 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3190 return (EFAULT);
3192 return (nvs_xdr_nvp_op(nvs, nvp));
3194 case NVS_OP_DECODE: {
3195 struct xdr_bytesrec bytesrec;
3197 /* get the encode and decode size */
3198 if (!xdr_int(xdr, &encode_len) || !xdr_int(xdr, &decode_len))
3199 return (EFAULT);
3200 *size = decode_len;
3202 /* are we at the end of the stream? */
3203 if (*size == 0)
3204 return (0);
3206 /* sanity check the size parameter */
3207 if (!xdr_control(xdr, XDR_GET_BYTES_AVAIL, &bytesrec))
3208 return (EFAULT);
3210 if (*size > NVS_XDR_MAX_LEN(bytesrec.xc_num_avail))
3211 return (EFAULT);
3212 break;
3215 default:
3216 return (EINVAL);
3218 return (0);
3221 static const struct nvs_ops nvs_xdr_ops = {
3222 nvs_xdr_nvlist,
3223 nvs_xdr_nvpair,
3224 nvs_xdr_nvp_op,
3225 nvs_xdr_nvp_size,
3226 nvs_xdr_nvl_fini
3229 static int
3230 nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
3232 XDR xdr;
3233 int err;
3235 nvs->nvs_ops = &nvs_xdr_ops;
3237 if ((err = nvs_xdr_create(nvs, &xdr, buf + sizeof (nvs_header_t),
3238 *buflen - sizeof (nvs_header_t))) != 0)
3239 return (err);
3241 err = nvs_operation(nvs, nvl, buflen);
3243 nvs_xdr_destroy(nvs);
3245 return (err);