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