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