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